zephyr icon indicating copy to clipboard operation
zephyr copied to clipboard

Add bluetooth host buf unit test

Open ahmedmoheb-nordic opened this issue 2 years ago • 5 comments

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

ahmedmoheb-nordic avatar Jul 14 '22 14:07 ahmedmoheb-nordic

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 on timeout.
  • 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 to net_buf_alloc_fixed uses bt_buf_get_evt_pool().
  • If CONFIG_BT_HCI_ACL_FLOW_CONTROL is not set, any call to net_buf_alloc_fixed uses bt_buf_get_hci_rx_pool().
  • If the retval is not NULL, its bt_buf_get_type is BT_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);
  }
}

alwa-nordic avatar Jul 19 '22 09:07 alwa-nordic

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 ?

jori-nordic avatar Jul 22 '22 08:07 jori-nordic

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

ahmedmoheb-nordic avatar Aug 02 '22 14:08 ahmedmoheb-nordic

Since ahmed didn't mention it, here's the following PR he's talking about @yperess https://github.com/zephyrproject-rtos/zephyr/pull/48461

jori-nordic avatar Aug 02 '22 14:08 jori-nordic

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 avatar Aug 05 '22 09:08 tejlmand

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?

ahmedmoheb-nordic avatar Aug 18 '22 15:08 ahmedmoheb-nordic

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

tejlmand avatar Sep 07 '22 11:09 tejlmand

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

ahmedmoheb-nordic avatar Sep 07 '22 12:09 ahmedmoheb-nordic

lgtm, thanks for the effort +1

Thanks to vote again for the changes

ahmedmoheb-nordic avatar Sep 07 '22 15:09 ahmedmoheb-nordic