From 4abff8a625f0c77ba9f3a811eca52bd59d05cc2e Mon Sep 17 00:00:00 2001 From: Ahmed Moheb Date: Tue, 30 Aug 2022 19:28:53 +0200 Subject: [PATCH] tests: bluetooth: host: Add UT for bt_buf_get_rx() Unit test project for bt_buf_get_rx(). This part of subsys/bluetooth/host/buf.c unit testing Signed-off-by: Ahmed Moheb --- .../host/buf/bt_buf_get_rx/CMakeLists.txt | 14 + .../bluetooth/host/buf/bt_buf_get_rx/prj.conf | 5 + .../host/buf/bt_buf_get_rx/src/main.c | 309 ++++++++++++++++++ .../src/test_suite_invalid_inputs.c | 83 +++++ .../host/buf/bt_buf_get_rx/testcase.yaml | 19 ++ 5 files changed, 430 insertions(+) create mode 100644 tests/bluetooth/host/buf/bt_buf_get_rx/CMakeLists.txt create mode 100644 tests/bluetooth/host/buf/bt_buf_get_rx/prj.conf create mode 100644 tests/bluetooth/host/buf/bt_buf_get_rx/src/main.c create mode 100644 tests/bluetooth/host/buf/bt_buf_get_rx/src/test_suite_invalid_inputs.c create mode 100644 tests/bluetooth/host/buf/bt_buf_get_rx/testcase.yaml diff --git a/tests/bluetooth/host/buf/bt_buf_get_rx/CMakeLists.txt b/tests/bluetooth/host/buf/bt_buf_get_rx/CMakeLists.txt new file mode 100644 index 00000000000..e060e58489b --- /dev/null +++ b/tests/bluetooth/host/buf/bt_buf_get_rx/CMakeLists.txt @@ -0,0 +1,14 @@ +# SPDX-License-Identifier: Apache-2.0 + +cmake_minimum_required(VERSION 3.20.0) + +FILE(GLOB SOURCES src/*.c) + +project(bluetooth_bt_buf_get_rx) + +find_package(Zephyr COMPONENTS unittest HINTS $ENV{ZEPHYR_BASE}) + +add_subdirectory(${ZEPHYR_BASE}/tests/bluetooth/host host_mocks) +add_subdirectory(${ZEPHYR_BASE}/tests/bluetooth/host/buf mocks) + +target_link_libraries(testbinary PRIVATE mocks host_mocks) diff --git a/tests/bluetooth/host/buf/bt_buf_get_rx/prj.conf b/tests/bluetooth/host/buf/bt_buf_get_rx/prj.conf new file mode 100644 index 00000000000..652e7e5d169 --- /dev/null +++ b/tests/bluetooth/host/buf/bt_buf_get_rx/prj.conf @@ -0,0 +1,5 @@ +CONFIG_ZTEST=y +CONFIG_BT=y +CONFIG_ASSERT=y +CONFIG_ASSERT_LEVEL=2 +CONFIG_ASSERT_VERBOSE=y diff --git a/tests/bluetooth/host/buf/bt_buf_get_rx/src/main.c b/tests/bluetooth/host/buf/bt_buf_get_rx/src/main.c new file mode 100644 index 00000000000..8ab902980a1 --- /dev/null +++ b/tests/bluetooth/host/buf/bt_buf_get_rx/src/main.c @@ -0,0 +1,309 @@ +/* + * Copyright (c) 2022 Nordic Semiconductor ASA + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#include +#include +#include "mocks/net_buf.h" +#include "mocks/net_buf_expects.h" +#include "mocks/buf_help_utils.h" + +/* + * Return value from bt_buf_get_rx() should be NULL + * + * This is to test the behaviour when memory allocation request fails + * + * Constraints: + * - Use valid buffer type 'BT_BUF_EVT' + * - Timeout value is a positive non-zero value + * - net_buf_alloc() returns a NULL value + * + * Expected behaviour: + * - net_buf_alloc() to be called with the correct memory allocation pool + * and the same timeout value passed to bt_buf_get_rx() + * - bt_buf_get_rx() returns NULL + */ +void test_returns_null_type_bt_buf_evt(void) +{ + struct net_buf *returned_buf; + k_timeout_t timeout = Z_TIMEOUT_TICKS(1000); + + struct net_buf_pool *memory_pool; + + if ((IS_ENABLED(CONFIG_BT_HCI_ACL_FLOW_CONTROL))) { + memory_pool = bt_buf_get_evt_pool(); + } else { + memory_pool = bt_buf_get_hci_rx_pool(); + } + + net_buf_alloc_fixed_fake.return_val = NULL; + + returned_buf = bt_buf_get_rx(BT_BUF_EVT, timeout); + + expect_single_call_net_buf_alloc(memory_pool, &timeout); + expect_not_called_net_buf_reserve(); + expect_not_called_net_buf_ref(); + + zassert_is_null(returned_buf, + "bt_buf_get_rx() returned non-NULL value while expecting NULL"); +} + +/* + * Return value from bt_buf_get_rx() should be NULL + * + * This is to test the behaviour when memory allocation request fails + * + * Constraints: + * - Use valid buffer type 'BT_BUF_ACL_IN' + * - Timeout value is a positive non-zero value + * - net_buf_alloc() returns a NULL value + * + * Expected behaviour: + * - net_buf_alloc() to be called with the correct memory allocation pool + * and the same timeout value passed to bt_buf_get_rx() + * - bt_buf_get_rx() returns NULL + */ +void test_returns_null_type_bt_buf_acl_in(void) +{ + struct net_buf *returned_buf; + k_timeout_t timeout = Z_TIMEOUT_TICKS(1000); + + struct net_buf_pool *memory_pool; + + if ((IS_ENABLED(CONFIG_BT_HCI_ACL_FLOW_CONTROL))) { + memory_pool = bt_buf_get_acl_in_pool(); + } else { + memory_pool = bt_buf_get_hci_rx_pool(); + } + + net_buf_alloc_fixed_fake.return_val = NULL; + + returned_buf = bt_buf_get_rx(BT_BUF_ACL_IN, timeout); + + expect_single_call_net_buf_alloc(memory_pool, &timeout); + expect_not_called_net_buf_reserve(); + expect_not_called_net_buf_ref(); + + zassert_is_null(returned_buf, + "bt_buf_get_rx() returned non-NULL value while expecting NULL"); +} + +/* + * Return value from bt_buf_get_rx() should be NULL + * + * This is to test the behaviour when memory allocation request fails + * + * Constraints: + * - Use valid buffer type 'BT_BUF_ISO_IN' + * - Timeout value is a positive non-zero value + * - net_buf_alloc() returns a NULL value + * + * Expected behaviour: + * - net_buf_alloc() to be called with the correct memory allocation pool + * and the same timeout value passed to bt_buf_get_rx() + * - bt_buf_get_rx() returns NULL + */ +void test_returns_null_type_bt_buf_iso_in(void) +{ + struct net_buf *returned_buf; + k_timeout_t timeout = Z_TIMEOUT_TICKS(1000); + + struct net_buf_pool *memory_pool; + + if ((IS_ENABLED(CONFIG_BT_ISO_UNICAST) || IS_ENABLED(CONFIG_BT_ISO_SYNC_RECEIVER))) { + memory_pool = bt_buf_get_iso_rx_pool(); + } else { + if ((IS_ENABLED(CONFIG_BT_HCI_ACL_FLOW_CONTROL))) { + memory_pool = bt_buf_get_acl_in_pool(); + } else { + memory_pool = bt_buf_get_hci_rx_pool(); + } + } + + net_buf_alloc_fixed_fake.return_val = NULL; + + returned_buf = bt_buf_get_rx(BT_BUF_ISO_IN, timeout); + + expect_single_call_net_buf_alloc(memory_pool, &timeout); + expect_not_called_net_buf_reserve(); + expect_not_called_net_buf_ref(); + + zassert_is_null(returned_buf, + "bt_buf_get_rx() returned non-NULL value while expecting NULL"); +} + +/* + * Return value from bt_buf_get_rx() shouldn't be NULL + * + * Constraints: + * - Use valid buffer type 'BT_BUF_EVT' + * - Timeout value is a positive non-zero value + * - net_buf_alloc() return a not NULL value + * + * Expected behaviour: + * - net_buf_alloc() to be called with the correct memory allocation pool + * and the same timeout value passed to bt_buf_get_rx() + * - bt_buf_get_rx() returns the same value returned by net_buf_alloc_fixed() + * - Return buffer matches the buffer type requested + */ +void test_returns_not_null_type_bt_buf_evt(void) +{ + static struct net_buf expected_buf; + struct net_buf *returned_buf; + uint8_t returned_buffer_type; + k_timeout_t timeout = Z_TIMEOUT_TICKS(1000); + + struct net_buf_pool *memory_pool; + + if ((IS_ENABLED(CONFIG_BT_HCI_ACL_FLOW_CONTROL))) { + memory_pool = bt_buf_get_evt_pool(); + } else { + memory_pool = bt_buf_get_hci_rx_pool(); + } + + net_buf_alloc_fixed_fake.return_val = &expected_buf; + + returned_buf = bt_buf_get_rx(BT_BUF_EVT, timeout); + + expect_single_call_net_buf_alloc(memory_pool, &timeout); + expect_single_call_net_buf_reserve(&expected_buf); + expect_not_called_net_buf_ref(); + + zassert_equal(returned_buf, &expected_buf, + "bt_buf_get_rx() returned incorrect buffer pointer value"); + + returned_buffer_type = bt_buf_get_type(returned_buf); + zassert_equal(returned_buffer_type, BT_BUF_EVT, + "bt_buf_get_rx() returned incorrect buffer type %u, expected %u (%s)", + returned_buffer_type, BT_BUF_EVT, STRINGIFY(BT_BUF_EVT)); +} + +/* + * Return value from bt_buf_get_rx() shouldn't be NULL + * + * Constraints: + * - Use valid buffer type 'BT_BUF_ACL_IN' + * - Timeout value is a positive non-zero value + * - net_buf_alloc() return a not NULL value + * + * Expected behaviour: + * - net_buf_alloc() to be called with the correct memory allocation pool + * and the same timeout value passed to bt_buf_get_rx() + * - bt_buf_get_rx() returns the same value returned by net_buf_alloc_fixed() + * - Return buffer matches the buffer type requested + */ +void test_returns_not_null_type_bt_buf_acl_in(void) +{ + static struct net_buf expected_buf; + struct net_buf *returned_buf; + uint8_t returned_buffer_type; + k_timeout_t timeout = Z_TIMEOUT_TICKS(1000); + + struct net_buf_pool *memory_pool; + + if ((IS_ENABLED(CONFIG_BT_HCI_ACL_FLOW_CONTROL))) { + memory_pool = bt_buf_get_acl_in_pool(); + } else { + memory_pool = bt_buf_get_hci_rx_pool(); + } + + net_buf_alloc_fixed_fake.return_val = &expected_buf; + + returned_buf = bt_buf_get_rx(BT_BUF_ACL_IN, timeout); + + expect_single_call_net_buf_alloc(memory_pool, &timeout); + expect_single_call_net_buf_reserve(&expected_buf); + expect_not_called_net_buf_ref(); + + zassert_equal(returned_buf, &expected_buf, + "bt_buf_get_rx() returned incorrect buffer pointer value"); + + returned_buffer_type = bt_buf_get_type(returned_buf); + zassert_equal(returned_buffer_type, BT_BUF_ACL_IN, + "bt_buf_get_rx() returned incorrect buffer type %u, expected %u (%s)", + returned_buffer_type, BT_BUF_ACL_IN, STRINGIFY(BT_BUF_ACL_IN)); +} + +/* + * Return value from bt_buf_get_rx() shouldn't be NULL + * + * Constraints: + * - Use valid buffer type 'BT_BUF_ISO_IN' + * - Timeout value is a positive non-zero value + * - net_buf_alloc() return a not NULL value + * + * Expected behaviour: + * - net_buf_alloc() to be called with the correct memory allocation pool + * and the same timeout value passed to bt_buf_get_rx() + * - bt_buf_get_rx() returns the same value returned by net_buf_alloc_fixed() + * - Return buffer matches the buffer type requested + */ +void test_returns_not_null_type_bt_buf_iso_in(void) +{ + static struct net_buf expected_buf; + struct net_buf *returned_buf; + uint8_t returned_buffer_type; + k_timeout_t timeout = Z_TIMEOUT_TICKS(1000); + + struct net_buf_pool *memory_pool; + + if ((IS_ENABLED(CONFIG_BT_ISO_UNICAST) || IS_ENABLED(CONFIG_BT_ISO_SYNC_RECEIVER))) { + memory_pool = bt_buf_get_iso_rx_pool(); + } else { + if ((IS_ENABLED(CONFIG_BT_HCI_ACL_FLOW_CONTROL))) { + memory_pool = bt_buf_get_acl_in_pool(); + } else { + memory_pool = bt_buf_get_hci_rx_pool(); + } + } + + net_buf_alloc_fixed_fake.return_val = &expected_buf; + + returned_buf = bt_buf_get_rx(BT_BUF_ISO_IN, timeout); + + expect_single_call_net_buf_alloc(memory_pool, &timeout); + expect_single_call_net_buf_reserve(&expected_buf); + expect_not_called_net_buf_ref(); + + zassert_equal(returned_buf, &expected_buf, + "bt_buf_get_rx() returned incorrect buffer pointer value"); + + returned_buffer_type = bt_buf_get_type(returned_buf); + zassert_equal(returned_buffer_type, BT_BUF_ISO_IN, + "bt_buf_get_rx() returned incorrect buffer type %u, expected %u (%s)", + returned_buffer_type, BT_BUF_ISO_IN, STRINGIFY(BT_BUF_ISO_IN)); +} + +/* Setup test variables */ +static void unit_test_setup(void) +{ + /* Register resets */ + NET_BUF_FFF_FAKES_LIST(RESET_FAKE); +} + +void test_main(void) +{ + ztest_test_suite( + test_bt_buf_get_rx_returns_null, + ztest_unit_test_setup(test_returns_null_type_bt_buf_evt, unit_test_setup), + ztest_unit_test_setup(test_returns_null_type_bt_buf_acl_in, unit_test_setup), + ztest_unit_test_setup(test_returns_null_type_bt_buf_iso_in, unit_test_setup) + ); + + ztest_run_test_suite(test_bt_buf_get_rx_returns_null); + + ztest_test_suite( + test_bt_buf_get_rx_returns_not_null, + ztest_unit_test_setup(test_returns_not_null_type_bt_buf_evt, unit_test_setup), + ztest_unit_test_setup(test_returns_not_null_type_bt_buf_acl_in, unit_test_setup), + ztest_unit_test_setup(test_returns_not_null_type_bt_buf_iso_in, unit_test_setup) + ); + + ztest_run_test_suite(test_bt_buf_get_rx_returns_not_null); + + uint32_t state; + + ztest_run_registered_test_suites(&state); +} diff --git a/tests/bluetooth/host/buf/bt_buf_get_rx/src/test_suite_invalid_inputs.c b/tests/bluetooth/host/buf/bt_buf_get_rx/src/test_suite_invalid_inputs.c new file mode 100644 index 00000000000..f4274dfc58c --- /dev/null +++ b/tests/bluetooth/host/buf/bt_buf_get_rx/src/test_suite_invalid_inputs.c @@ -0,0 +1,83 @@ +/* + * Copyright (c) 2022 Nordic Semiconductor ASA + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#include +#include +#include "host_mocks/assert.h" +#include "mocks/buf_help_utils.h" + +/* + * Test passing invalid buffer type to bt_buf_get_rx() + * + * Constraints: + * - Use invalid buffer type 'BT_BUF_CMD' + * + * Expected behaviour: + * - An assertion should be raised as an invalid parameter was used + */ +void test_invalid_input_type_bt_buf_cmd(void) +{ + expect_assert(); + bt_buf_get_rx(BT_BUF_CMD, Z_TIMEOUT_TICKS(1000)); +} + +/* + * Test passing invalid buffer type to bt_buf_get_rx() + * + * Constraints: + * - Use invalid buffer type 'BT_BUF_ACL_OUT' + * + * Expected behaviour: + * - An assertion should be raised as an invalid parameter was used + */ +void test_invalid_input_type_bt_buf_acl_out(void) +{ + expect_assert(); + bt_buf_get_rx(BT_BUF_ACL_OUT, Z_TIMEOUT_TICKS(1000)); +} + +/* + * Test passing invalid buffer type to bt_buf_get_rx() + * + * Constraints: + * - Use invalid buffer type 'BT_BUF_ISO_OUT' + * + * Expected behaviour: + * - An assertion should be raised as an invalid parameter was used + */ +void test_invalid_input_type_bt_buf_iso_out(void) +{ + expect_assert(); + bt_buf_get_rx(BT_BUF_ISO_OUT, Z_TIMEOUT_TICKS(1000)); +} + +/* + * Test passing invalid buffer type to bt_buf_get_rx() + * + * Constraints: + * - Use invalid buffer type 'BT_BUF_H4' + * + * Expected behaviour: + * - An assertion should be raised as an invalid parameter was used + */ +void test_invalid_input_type_bt_buf_h4(void) +{ + expect_assert(); + bt_buf_get_rx(BT_BUF_H4, Z_TIMEOUT_TICKS(1000)); +} + +/* Setup test variables */ +static void unit_test_setup(void) +{ + /* Register resets */ + ASSERT_FFF_FAKES_LIST(RESET_FAKE); +} + +ztest_register_test_suite(test_bt_buf_get_rx_invalid_input, NULL, + ztest_unit_test_setup(test_invalid_input_type_bt_buf_cmd, unit_test_setup), + ztest_unit_test_setup(test_invalid_input_type_bt_buf_acl_out, unit_test_setup), + ztest_unit_test_setup(test_invalid_input_type_bt_buf_iso_out, unit_test_setup), + ztest_unit_test_setup(test_invalid_input_type_bt_buf_h4, unit_test_setup)); diff --git a/tests/bluetooth/host/buf/bt_buf_get_rx/testcase.yaml b/tests/bluetooth/host/buf/bt_buf_get_rx/testcase.yaml new file mode 100644 index 00000000000..9ec3e17e70a --- /dev/null +++ b/tests/bluetooth/host/buf/bt_buf_get_rx/testcase.yaml @@ -0,0 +1,19 @@ +common: + tags: test_framework bluetooth host +tests: + bluetooth.host.bt_buf_get_rx.default: + type: unit + bluetooth.host.bt_buf_get_rx.hci_acl_flow_control: + type: unit + extra_configs: + - CONFIG_BT_CENTRAL=y + - CONFIG_BT_HCI_ACL_FLOW_CONTROL=y + bluetooth.host.bt_buf_get_rx.iso_unicast: + type: unit + # enable CONFIG_BT_ISO_UNICAST + extra_configs: + - CONFIG_BT_ISO_CENTRAL=y + bluetooth.host.bt_buf_get_rx.iso_sync_receiver: + type: unit + extra_configs: + - CONFIG_BT_ISO_SYNC_RECEIVER=y