tests: bluetooth: host: Add UT for bt_keys_update_usage()
Unit test project for bt_keys_update_usage(). This part of subsys/bluetooth/host/keys.c unit testing. Signed-off-by: Ahmed Moheb <ahmed.moheb@nordicsemi.no>
This commit is contained in:
parent
5768c45a9d
commit
60345c1ddc
10 changed files with 323 additions and 0 deletions
|
@ -542,5 +542,10 @@ uint32_t bt_keys_get_aging_counter_val(void)
|
||||||
{
|
{
|
||||||
return aging_counter_val;
|
return aging_counter_val;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
struct bt_keys *bt_keys_get_last_keys_updated(void)
|
||||||
|
{
|
||||||
|
return last_keys_updated;
|
||||||
|
}
|
||||||
#endif /* CONFIG_BT_KEYS_OVERWRITE_OLDEST */
|
#endif /* CONFIG_BT_KEYS_OVERWRITE_OLDEST */
|
||||||
#endif /* ZTEST_UNITTEST */
|
#endif /* ZTEST_UNITTEST */
|
||||||
|
|
|
@ -0,0 +1,22 @@
|
||||||
|
# SPDX-License-Identifier: Apache-2.0
|
||||||
|
|
||||||
|
cmake_minimum_required(VERSION 3.20.0)
|
||||||
|
|
||||||
|
project(bt_keys_update_usage)
|
||||||
|
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/keys/mocks mocks)
|
||||||
|
|
||||||
|
target_link_libraries(testbinary PRIVATE mocks host_mocks)
|
||||||
|
|
||||||
|
target_sources(testbinary
|
||||||
|
PRIVATE
|
||||||
|
src/main.c
|
||||||
|
src/test_suite_save_aging_counter.c
|
||||||
|
src/test_suite_update_usage_invalid_inputs.c
|
||||||
|
|
||||||
|
# Unit under test
|
||||||
|
$ENV{ZEPHYR_BASE}/subsys/bluetooth/host/keys.c
|
||||||
|
$ENV{ZEPHYR_BASE}/subsys/bluetooth/common/addr.c
|
||||||
|
)
|
12
tests/bluetooth/host/keys/bt_keys_update_usage/prj.conf
Normal file
12
tests/bluetooth/host/keys/bt_keys_update_usage/prj.conf
Normal file
|
@ -0,0 +1,12 @@
|
||||||
|
CONFIG_ZTEST=y
|
||||||
|
CONFIG_ZTEST_NEW_API=y
|
||||||
|
CONFIG_BT=y
|
||||||
|
CONFIG_BT_CENTRAL=y
|
||||||
|
CONFIG_BT_MAX_PAIRED=11
|
||||||
|
CONFIG_ASSERT=y
|
||||||
|
CONFIG_ASSERT_LEVEL=2
|
||||||
|
CONFIG_ASSERT_VERBOSE=y
|
||||||
|
|
||||||
|
CONFIG_LOG=n
|
||||||
|
CONFIG_BT_DEBUG_LOG=n
|
||||||
|
CONFIG_TEST_LOGGING_DEFAULTS=n
|
139
tests/bluetooth/host/keys/bt_keys_update_usage/src/main.c
Normal file
139
tests/bluetooth/host/keys/bt_keys_update_usage/src/main.c
Normal file
|
@ -0,0 +1,139 @@
|
||||||
|
/*
|
||||||
|
* Copyright (c) 2022 Nordic Semiconductor ASA
|
||||||
|
*
|
||||||
|
* SPDX-License-Identifier: Apache-2.0
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include "mocks/keys_help_utils.h"
|
||||||
|
#include "mocks/settings_store_expects.h"
|
||||||
|
#include "testing_common_defs.h"
|
||||||
|
|
||||||
|
#include <zephyr/bluetooth/addr.h>
|
||||||
|
#include <zephyr/fff.h>
|
||||||
|
#include <zephyr/kernel.h>
|
||||||
|
|
||||||
|
#include <host/keys.h>
|
||||||
|
|
||||||
|
DEFINE_FFF_GLOBALS;
|
||||||
|
|
||||||
|
/* This LUT contains different combinations of ID, Address and key type.
|
||||||
|
* Item in this list will be used to fill keys pool.
|
||||||
|
*/
|
||||||
|
const struct id_addr_pair testing_id_addr_pair_lut[] = {
|
||||||
|
|
||||||
|
{BT_ADDR_ID_1, BT_ADDR_LE_1}, {BT_ADDR_ID_1, BT_RPA_ADDR_LE_1},
|
||||||
|
{BT_ADDR_ID_1, BT_RPA_ADDR_LE_2}, {BT_ADDR_ID_1, BT_ADDR_LE_3},
|
||||||
|
|
||||||
|
{BT_ADDR_ID_2, BT_ADDR_LE_1}, {BT_ADDR_ID_2, BT_RPA_ADDR_LE_2},
|
||||||
|
{BT_ADDR_ID_2, BT_RPA_ADDR_LE_3}, {BT_ADDR_ID_2, BT_ADDR_LE_2},
|
||||||
|
|
||||||
|
{BT_ADDR_ID_3, BT_ADDR_LE_1}, {BT_ADDR_ID_3, BT_ADDR_LE_2},
|
||||||
|
|
||||||
|
{BT_ADDR_ID_4, BT_ADDR_LE_1}};
|
||||||
|
|
||||||
|
/* This list will hold returned references while filling keys pool */
|
||||||
|
struct bt_keys *returned_keys_refs[CONFIG_BT_MAX_PAIRED];
|
||||||
|
|
||||||
|
/* Holds the last key reference updated */
|
||||||
|
static struct bt_keys *last_keys_updated;
|
||||||
|
|
||||||
|
BUILD_ASSERT(ARRAY_SIZE(testing_id_addr_pair_lut) == CONFIG_BT_MAX_PAIRED);
|
||||||
|
BUILD_ASSERT(ARRAY_SIZE(testing_id_addr_pair_lut) == ARRAY_SIZE(returned_keys_refs));
|
||||||
|
|
||||||
|
static void tc_setup(void *f)
|
||||||
|
{
|
||||||
|
clear_key_pool();
|
||||||
|
int rv = fill_key_pool_by_id_addr(testing_id_addr_pair_lut,
|
||||||
|
ARRAY_SIZE(testing_id_addr_pair_lut), returned_keys_refs);
|
||||||
|
|
||||||
|
zassert_true(rv == 0, "Failed to fill keys pool list, error code %d", -rv);
|
||||||
|
|
||||||
|
last_keys_updated = returned_keys_refs[CONFIG_BT_MAX_PAIRED - 1];
|
||||||
|
}
|
||||||
|
|
||||||
|
ZTEST_SUITE(bt_keys_update_usage_overwrite_oldest_enabled, NULL, NULL, tc_setup, NULL, NULL);
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Request updating non-existing item in the keys pool list
|
||||||
|
*
|
||||||
|
* Constraints:
|
||||||
|
* - Keys pool list is filled with items that are different from the testing ID and address pair
|
||||||
|
* used
|
||||||
|
*
|
||||||
|
* Expected behaviour:
|
||||||
|
* - Last updated key reference isn't changed
|
||||||
|
*/
|
||||||
|
ZTEST(bt_keys_update_usage_overwrite_oldest_enabled, test_update_non_existing_key)
|
||||||
|
{
|
||||||
|
uint8_t id = BT_ADDR_ID_5;
|
||||||
|
bt_addr_le_t *addr = BT_ADDR_LE_5;
|
||||||
|
|
||||||
|
bt_keys_update_usage(id, addr);
|
||||||
|
|
||||||
|
zassert_equal_ptr(bt_keys_get_last_keys_updated(), last_keys_updated,
|
||||||
|
"bt_keys_update_usage() changed last updated key reference unexpectedly");
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Request updating the latest key reference
|
||||||
|
*
|
||||||
|
* Constraints:
|
||||||
|
* - Keys pool list is filled with items
|
||||||
|
* - ID and address pair used are the last added pair to the list
|
||||||
|
*
|
||||||
|
* Expected behaviour:
|
||||||
|
* - Last updated key reference isn't changed
|
||||||
|
*/
|
||||||
|
ZTEST(bt_keys_update_usage_overwrite_oldest_enabled, test_update_latest_reference)
|
||||||
|
{
|
||||||
|
uint8_t id = testing_id_addr_pair_lut[CONFIG_BT_MAX_PAIRED - 1].id;
|
||||||
|
bt_addr_le_t *addr = testing_id_addr_pair_lut[CONFIG_BT_MAX_PAIRED - 1].addr;
|
||||||
|
|
||||||
|
bt_keys_update_usage(id, addr);
|
||||||
|
|
||||||
|
zassert_equal_ptr(bt_keys_get_last_keys_updated(), last_keys_updated,
|
||||||
|
"bt_keys_update_usage() changed last updated key reference unexpectedly");
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Request updating existing items aging counter
|
||||||
|
*
|
||||||
|
* Constraints:
|
||||||
|
* - Keys pool list is filled with items
|
||||||
|
* - ID and address used exist in the keys pool list
|
||||||
|
* - CONFIG_BT_KEYS_SAVE_AGING_COUNTER_ON_PAIRING isn't enabled
|
||||||
|
*
|
||||||
|
* Expected behaviour:
|
||||||
|
* - Last updated key reference matches the last updated key reference
|
||||||
|
*/
|
||||||
|
ZTEST(bt_keys_update_usage_overwrite_oldest_enabled, test_update_non_latest_reference)
|
||||||
|
{
|
||||||
|
uint8_t id;
|
||||||
|
uint32_t old_aging_counter;
|
||||||
|
bt_addr_le_t *addr;
|
||||||
|
struct bt_keys *expected_updated_keys;
|
||||||
|
struct id_addr_pair const *params_vector;
|
||||||
|
|
||||||
|
if (IS_ENABLED(CONFIG_BT_KEYS_SAVE_AGING_COUNTER_ON_PAIRING)) {
|
||||||
|
ztest_test_skip();
|
||||||
|
}
|
||||||
|
|
||||||
|
for (size_t it = 0; it < ARRAY_SIZE(testing_id_addr_pair_lut); it++) {
|
||||||
|
params_vector = &testing_id_addr_pair_lut[it];
|
||||||
|
id = params_vector->id;
|
||||||
|
addr = params_vector->addr;
|
||||||
|
expected_updated_keys = returned_keys_refs[it];
|
||||||
|
old_aging_counter = expected_updated_keys->aging_counter;
|
||||||
|
|
||||||
|
bt_keys_update_usage(id, addr);
|
||||||
|
|
||||||
|
zassert_true(expected_updated_keys->aging_counter > (old_aging_counter),
|
||||||
|
"bt_keys_update_usage() set incorrect aging counter");
|
||||||
|
|
||||||
|
zassert_equal_ptr(
|
||||||
|
bt_keys_get_last_keys_updated(), expected_updated_keys,
|
||||||
|
"bt_keys_update_usage() changed last updated key reference unexpectedly");
|
||||||
|
|
||||||
|
expect_not_called_settings_save_one();
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,82 @@
|
||||||
|
/*
|
||||||
|
* Copyright (c) 2022 Nordic Semiconductor ASA
|
||||||
|
*
|
||||||
|
* SPDX-License-Identifier: Apache-2.0
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include "mocks/keys_help_utils.h"
|
||||||
|
#include "mocks/settings_store.h"
|
||||||
|
#include "mocks/settings_store_expects.h"
|
||||||
|
#include "testing_common_defs.h"
|
||||||
|
|
||||||
|
#include <zephyr/bluetooth/addr.h>
|
||||||
|
#include <zephyr/kernel.h>
|
||||||
|
|
||||||
|
#include <host/keys.h>
|
||||||
|
|
||||||
|
/* This LUT contains different combinations of ID and Address pairs.
|
||||||
|
* It is defined in main.c.
|
||||||
|
*/
|
||||||
|
extern const struct id_addr_pair testing_id_addr_pair_lut[CONFIG_BT_MAX_PAIRED];
|
||||||
|
|
||||||
|
/* This list holds returned references while filling keys pool.
|
||||||
|
* It is defined in main.c.
|
||||||
|
*/
|
||||||
|
extern struct bt_keys *returned_keys_refs[CONFIG_BT_MAX_PAIRED];
|
||||||
|
|
||||||
|
static void tc_setup(void *f)
|
||||||
|
{
|
||||||
|
Z_TEST_SKIP_IFNDEF(CONFIG_BT_KEYS_SAVE_AGING_COUNTER_ON_PAIRING);
|
||||||
|
|
||||||
|
clear_key_pool();
|
||||||
|
int rv = fill_key_pool_by_id_addr(testing_id_addr_pair_lut,
|
||||||
|
ARRAY_SIZE(testing_id_addr_pair_lut), returned_keys_refs);
|
||||||
|
|
||||||
|
zassert_true(rv == 0, "Failed to fill keys pool list, error code %d", -rv);
|
||||||
|
}
|
||||||
|
|
||||||
|
ZTEST_SUITE(bt_keys_update_usage_save_aging_counter, NULL, NULL, tc_setup, NULL, NULL);
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Request updating existing items aging counter
|
||||||
|
*
|
||||||
|
* Constraints:
|
||||||
|
* - Keys pool list is filled with items
|
||||||
|
* - ID and address used exist in the keys pool list
|
||||||
|
* - CONFIG_BT_KEYS_SAVE_AGING_COUNTER_ON_PAIRING is enabled
|
||||||
|
*
|
||||||
|
* Expected behaviour:
|
||||||
|
* - Last updated key reference matches the last updated key reference
|
||||||
|
* - bt_keys_store() is called once with the correct parameters
|
||||||
|
*/
|
||||||
|
ZTEST(bt_keys_update_usage_save_aging_counter, test_update_usage_and_save_aging_counter)
|
||||||
|
{
|
||||||
|
uint8_t id;
|
||||||
|
uint32_t old_aging_counter;
|
||||||
|
bt_addr_le_t *addr;
|
||||||
|
struct bt_keys *expected_updated_keys;
|
||||||
|
struct id_addr_pair const *params_vector;
|
||||||
|
|
||||||
|
for (size_t it = 0; it < ARRAY_SIZE(testing_id_addr_pair_lut); it++) {
|
||||||
|
params_vector = &testing_id_addr_pair_lut[it];
|
||||||
|
id = params_vector->id;
|
||||||
|
addr = params_vector->addr;
|
||||||
|
expected_updated_keys = returned_keys_refs[it];
|
||||||
|
old_aging_counter = expected_updated_keys->aging_counter;
|
||||||
|
|
||||||
|
/* Reset fake functions call counter */
|
||||||
|
SETTINGS_STORE_FFF_FAKES_LIST(RESET_FAKE);
|
||||||
|
|
||||||
|
bt_keys_update_usage(id, addr);
|
||||||
|
|
||||||
|
zassert_true(expected_updated_keys->aging_counter > (old_aging_counter),
|
||||||
|
"bt_keys_update_usage() set incorrect aging counter");
|
||||||
|
|
||||||
|
zassert_equal_ptr(
|
||||||
|
bt_keys_get_last_keys_updated(), expected_updated_keys,
|
||||||
|
"bt_keys_update_usage() changed last updated key reference unexpectedly");
|
||||||
|
|
||||||
|
/* Check if bt_keys_store() was called */
|
||||||
|
expect_single_call_settings_save_one(expected_updated_keys->storage_start);
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,30 @@
|
||||||
|
/*
|
||||||
|
* Copyright (c) 2022 Nordic Semiconductor ASA
|
||||||
|
*
|
||||||
|
* SPDX-License-Identifier: Apache-2.0
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include "host_mocks/assert.h"
|
||||||
|
|
||||||
|
#include <zephyr/bluetooth/bluetooth.h>
|
||||||
|
#include <zephyr/kernel.h>
|
||||||
|
|
||||||
|
#include <host/keys.h>
|
||||||
|
|
||||||
|
ZTEST_SUITE(bt_keys_update_usage_invalid_inputs, NULL, NULL, NULL, NULL, NULL);
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Test function with NULL address
|
||||||
|
*
|
||||||
|
* Constraints:
|
||||||
|
* - Any ID value can be used
|
||||||
|
* - Address is passed as NULL
|
||||||
|
*
|
||||||
|
* Expected behaviour:
|
||||||
|
* - An assertion fails and execution stops
|
||||||
|
*/
|
||||||
|
ZTEST(bt_keys_update_usage_invalid_inputs, test_null_device_address)
|
||||||
|
{
|
||||||
|
expect_assert();
|
||||||
|
bt_keys_update_usage(0x00, NULL);
|
||||||
|
}
|
16
tests/bluetooth/host/keys/bt_keys_update_usage/testcase.yaml
Normal file
16
tests/bluetooth/host/keys/bt_keys_update_usage/testcase.yaml
Normal file
|
@ -0,0 +1,16 @@
|
||||||
|
common:
|
||||||
|
tags: test_framework bluetooth host
|
||||||
|
tests:
|
||||||
|
bluetooth.host.bt_keys_update_usage.default:
|
||||||
|
type: unit
|
||||||
|
extra_configs:
|
||||||
|
- CONFIG_BT_SMP=y
|
||||||
|
- CONFIG_BT_KEYS_OVERWRITE_OLDEST=y
|
||||||
|
bluetooth.host.bt_keys_update_usage.save_aging_counter:
|
||||||
|
type: unit
|
||||||
|
extra_configs:
|
||||||
|
- CONFIG_SETTINGS=y
|
||||||
|
- CONFIG_BT_SETTINGS=y
|
||||||
|
- CONFIG_BT_SMP=y
|
||||||
|
- CONFIG_BT_KEYS_OVERWRITE_OLDEST=y
|
||||||
|
- CONFIG_BT_KEYS_SAVE_AGING_COUNTER_ON_PAIRING=y
|
|
@ -23,6 +23,7 @@ struct id_addr_type {
|
||||||
struct bt_keys *bt_keys_get_key_pool(void);
|
struct bt_keys *bt_keys_get_key_pool(void);
|
||||||
#if defined(CONFIG_BT_KEYS_OVERWRITE_OLDEST)
|
#if defined(CONFIG_BT_KEYS_OVERWRITE_OLDEST)
|
||||||
uint32_t bt_keys_get_aging_counter_val(void);
|
uint32_t bt_keys_get_aging_counter_val(void);
|
||||||
|
struct bt_keys *bt_keys_get_last_keys_updated(void);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
/* keys_help_utils.c declarations */
|
/* keys_help_utils.c declarations */
|
||||||
|
|
|
@ -35,3 +35,11 @@ void expect_single_call_settings_save_one(const void *value)
|
||||||
zassert_equal(settings_save_one_fake.arg2_val, BT_KEYS_STORAGE_LEN,
|
zassert_equal(settings_save_one_fake.arg2_val, BT_KEYS_STORAGE_LEN,
|
||||||
"'%s()' was called with incorrect '%s' value", func_name, "val_len");
|
"'%s()' was called with incorrect '%s' value", func_name, "val_len");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void expect_not_called_settings_save_one(void)
|
||||||
|
{
|
||||||
|
const char *func_name = "settings_save_one";
|
||||||
|
|
||||||
|
zassert_equal(settings_save_one_fake.call_count, 0, "'%s()' was called unexpectedly",
|
||||||
|
func_name);
|
||||||
|
}
|
||||||
|
|
|
@ -21,3 +21,11 @@ void expect_single_call_settings_delete(void);
|
||||||
* - settings_save_one() to be called once with correct parameters
|
* - settings_save_one() to be called once with correct parameters
|
||||||
*/
|
*/
|
||||||
void expect_single_call_settings_save_one(const void *value);
|
void expect_single_call_settings_save_one(const void *value);
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Validate expected behaviour when settings_save_one() isn't called
|
||||||
|
*
|
||||||
|
* Expected behaviour:
|
||||||
|
* - settings_save_one() isn't called at all
|
||||||
|
*/
|
||||||
|
void expect_not_called_settings_save_one(void);
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue