zephyr
zephyr copied to clipboard
Add bluetooth host buf unit test
A set of unit test projects for 'bluetooth/host/buf.c'. These unit test projects should 1 - Test the behavior of module APIs 2 - Increase the test coverage report percentage of the module
RFC on something similar to property based testing: We should have a collection of statements that are always true, and parameterize them over all the variables that may affect the UUT.
Here are some example properties for bt_buf_get_cmd_complete
:
- Any calls to
net_buf_alloc_fixed
pass ontimeout
. - If
net_buf_alloc_fixed
returns non-null, that buffer is returned. - If NULL is returned, at least one
net_buf_alloc_fixed
was attempted. - The returned value must not be found in
bt_dev.sent_cmd
afterwards unless it's NULL. - If
bt_dev.sent_cmd != NULL
, that value is returned. - If
CONFIG_BT_HCI_ACL_FLOW_CONTROL
is set, any call tonet_buf_alloc_fixed
usesbt_buf_get_evt_pool()
. - If
CONFIG_BT_HCI_ACL_FLOW_CONTROL
is not set, any call tonet_buf_alloc_fixed
usesbt_buf_get_hci_rx_pool()
. - If the retval is not NULL, its
bt_buf_get_type
isBT_BUF_EVT
.
We express these in parameterized functions and select interesting combinations of values for those parameters, like sketched below.
/* This test should pass for for all values of bt_dev__sent_cmd, timeout, net_buf_alloc_fixed_return */
void test_bt_dev_sent_cmd_cleared_parameterized(bt_dev__sent_cmd, timeout, net_buf_alloc_fixed__retval) {
bt_dev.sent_cmd = bt_dev__sent_cmd;
ztest_returns_value(net_buf_alloc_fixed, net_buf_alloc_fixed__retval);
struct net_buf * returned = bt_buf_get_cmd_complete(timeout);
if (bt_dev.sent_cmd != NULL) {
zassert_not_equal(bt_dev.sent_cmd, returned, "The returned buffer is still stored in `sent_cmd`");
}
}
void test_bt_dev_sent_cmd_cleared() {
/* Test with interesting values of the "forall" variables. */
/* Here we use loops to create a test matrix, but hand-picked combinations may be just as good. */
for (bt_dev__sent_cmd <- NULL, 'a')
for (net_buf_alloc_fixed__retval <- NULL, 'b')
for (timeout <- 0, 10) {
test_sent_cmd_cleared_parameterized(bt_dev__sent_cmd, timeout, net_buf_alloc_fixed__retval);
}
}
Please do not use ztest_mock, it's actively being removed. Use FFF instead.
Yes we know about it. As @ahmedmoheb-nordic is still ramping up on zephyr, we'll try to merge it with ztest (as the PR is almost in a mergeable state), to get something running in CI, and the very next PR (next week or so) will be migrating this test to FFF. Is this okay with you ?
Hi @yperess and @thoh-ot , could you review again or mark your change request as resolved?
@yperess regarding moving from ZTest mocks to FFF, a following PR will do the required changes.
Thanks
Since ahmed didn't mention it, here's the following PR he's talking about @yperess https://github.com/zephyrproject-rtos/zephyr/pull/48461
I'll take a look at the unittest / Kconfig support.
Kconfig support is now ready here: #48732
@ahmedmoheb-nordic please try it out for Kconfig settings together with this PR and provide feedback.
I'll take a look at the unittest / Kconfig support.
Kconfig support is now ready here: #48732
@ahmedmoheb-nordic please try it out for Kconfig settings together with this PR and provide feedback.
@tejlmand I've rebased my branch on top of your modifications and modified the files, Could you re-check?
Only thing is that I really would like @cvinayak or @Thalley to take a look at this comment: #47794 (comment)
so will give a little time for them to take a look, else will +1 the PR.
also summoning: @alwa-nordic Aleksande
Only thing is that I really would like @cvinayak or @Thalley to take a look at this comment: #47794 (comment) so will give a little time for them to take a look, else will +1 the PR.
also summoning: @alwa-nordic Aleksande
Also, @jori-nordic can assist, I already discussed that part with him
lgtm, thanks for the effort +1
Thanks to vote again for the changes