Commit 71aa52d7 authored by Ahmed Moheb's avatar Ahmed Moheb Committed by Carles Cufi
Browse files

tests: bluetooth: host: Add UT for bt_keys_find_addr()



Unit test project for bt_keys_find_addr().
This part of subsys/bluetooth/host/keys.c unit testing.

Signed-off-by: default avatarAhmed Moheb <ahmed.moheb@nordicsemi.no>
parent d99052b3
Loading
Loading
Loading
Loading
+21 −0
Original line number Diff line number Diff line
# SPDX-License-Identifier: Apache-2.0

cmake_minimum_required(VERSION 3.20.0)

project(bt_keys_find_addr)
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_find_addr_invalid_inputs.c

    # Unit under test
    $ENV{ZEPHYR_BASE}/subsys/bluetooth/host/keys.c
    $ENV{ZEPHYR_BASE}/subsys/bluetooth/common/addr.c
)
+12 −0
Original line number Diff line number Diff line
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
+110 −0
Original line number Diff line number Diff line
/*
 * Copyright (c) 2022 Nordic Semiconductor ASA
 *
 * SPDX-License-Identifier: Apache-2.0
 */

#include "mocks/keys_help_utils.h"
#include "mocks/rpa.h"
#include "testing_common_defs.h"

#include <zephyr/bluetooth/bluetooth.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.
 */
static 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 */
static struct bt_keys *returned_keys_refs[CONFIG_BT_MAX_PAIRED];

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 *empty_list_ts_setup(void)
{
	clear_key_pool();

	return NULL;
}

ZTEST_SUITE(bt_keys_find_addr_initially_empty_list, NULL, empty_list_ts_setup, NULL, NULL, NULL);

/*
 *  Find a non-existing key reference for ID and Address pair
 *
 *  Constraints:
 *   - Empty keys pool list
 *
 *  Expected behaviour:
 *   - A NULL value is returned
 */
ZTEST(bt_keys_find_addr_initially_empty_list, test_find_non_existing_key)
{
	for (uint32_t i = 0; i < ARRAY_SIZE(testing_id_addr_pair_lut); i++) {
		struct bt_keys *returned_ref;
		struct id_addr_pair const *params_vector = &testing_id_addr_pair_lut[i];
		uint8_t id = params_vector->id;
		const bt_addr_le_t *addr = params_vector->addr;

		returned_ref = bt_keys_find_addr(id, addr);

		zassert_true(returned_ref == NULL, "bt_keys_find() returned a non-NULL reference");
	}
}

static void *filled_list_ts_setup(void)
{
	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);

	return NULL;
}

ZTEST_SUITE(bt_keys_find_addr_initially_filled_list, NULL, filled_list_ts_setup, NULL, NULL, NULL);

/*
 *  Find an existing key reference by ID and Address
 *
 *  Constraints:
 *   - ID and address pair does exist in keys pool
 *
 *  Expected behaviour:
 *   - A valid reference value is returned
 */
ZTEST(bt_keys_find_addr_initially_filled_list, test_find_existing_key_by_id_and_address)
{
	for (uint32_t i = 0; i < ARRAY_SIZE(testing_id_addr_pair_lut); i++) {
		struct bt_keys *returned_ref, *expected_key_ref;
		struct id_addr_pair const *params_vector = &testing_id_addr_pair_lut[i];
		uint8_t id = params_vector->id;
		const bt_addr_le_t *addr = params_vector->addr;

		expected_key_ref = returned_keys_refs[i];

		returned_ref = bt_keys_find_addr(id, addr);

		zassert_true(returned_ref != NULL, "bt_keys_find_addr() returned a NULL reference");
		zassert_equal_ptr(returned_ref, expected_key_ref,
				  "bt_keys_find_addr() returned unexpected reference");
	}
}
+30 −0
Original line number Diff line number Diff line
/*
 * 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_find_addr_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_find_addr_invalid_inputs, test_null_device_address)
{
	expect_assert();
	bt_keys_find_addr(0x00, NULL);
}
+5 −0
Original line number Diff line number Diff line
common:
    tags: test_framework bluetooth host
tests:
  bluetooth.host.bt_keys_find_addr.default:
     type: unit