Bluetooth: Consolidate duplicated code
The file `tests/bluetooth/host/keys/bt_keys_get_addr/src/main.c` was using functions from `tests/bluetooth/host/host_mocks/print_utils.c` which were exactly the same as the one in `common/log.h`. The code was duplicated because the test author wanted to include `common/log.h` to get the stringifying functions, but could not due to unwanted side-effects. See the comment from the unit test author here: https://github.com/zephyrproject-rtos/zephyr/pull/48676#discussion_r973672151 The new `common/bt_str.h` does not have side-effects, so it is suitable for unit tests. The `snprintk` function has been redefined inside the `main.c` to use the libc because of the kernel being not compiled entirely. Also, the `CONFIG_LOG`, `CONFIG_BT_DEBUG_LOG` and `CONFIG_TEST_LOGGING_DEFAULTS` have been disabled in the Kconfig files of each `keys` tests. That so logs are not compiled and so, there is no need to link the `printk` functions that are used by the logs. Signed-off-by: Théo Battrel <theo.battrel@nordicsemi.no> Co-authored-by: Aleksander Wasaznik <aleksander.wasaznik@nordicsemi.no>
This commit is contained in:
parent
c9d68a5a4f
commit
a57dbb15db
12 changed files with 69 additions and 139 deletions
|
@ -4,7 +4,6 @@
|
||||||
|
|
||||||
add_library(host_mocks STATIC
|
add_library(host_mocks STATIC
|
||||||
host_mocks/assert.c
|
host_mocks/assert.c
|
||||||
host_mocks/print_utils.c
|
|
||||||
)
|
)
|
||||||
|
|
||||||
target_include_directories(host_mocks PUBLIC
|
target_include_directories(host_mocks PUBLIC
|
||||||
|
|
|
@ -1,89 +0,0 @@
|
||||||
/*
|
|
||||||
* Copyright (c) 2022 Nordic Semiconductor ASA
|
|
||||||
*
|
|
||||||
* SPDX-License-Identifier: Apache-2.0
|
|
||||||
*/
|
|
||||||
|
|
||||||
#include <zephyr/kernel.h>
|
|
||||||
#include "host_mocks/print_utils.h"
|
|
||||||
|
|
||||||
static int bt_addr_le_to_str_mod(const bt_addr_le_t *addr, char *str,
|
|
||||||
size_t len)
|
|
||||||
{
|
|
||||||
char type[10];
|
|
||||||
|
|
||||||
switch (addr->type) {
|
|
||||||
case BT_ADDR_LE_PUBLIC:
|
|
||||||
strcpy(type, "public");
|
|
||||||
break;
|
|
||||||
case BT_ADDR_LE_RANDOM:
|
|
||||||
strcpy(type, "random");
|
|
||||||
break;
|
|
||||||
case BT_ADDR_LE_PUBLIC_ID:
|
|
||||||
strcpy(type, "public-id");
|
|
||||||
break;
|
|
||||||
case BT_ADDR_LE_RANDOM_ID:
|
|
||||||
strcpy(type, "random-id");
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
snprintf(type, sizeof(type), "0x%02x", addr->type);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
return snprintf(str, len, "%02X:%02X:%02X:%02X:%02X:%02X (%s)",
|
|
||||||
addr->a.val[5], addr->a.val[4], addr->a.val[3],
|
|
||||||
addr->a.val[2], addr->a.val[1], addr->a.val[0], type);
|
|
||||||
}
|
|
||||||
|
|
||||||
static int bt_addr_to_str_mod(const bt_addr_t *addr, char *str, size_t len)
|
|
||||||
{
|
|
||||||
return snprintf(str, len, "%02X:%02X:%02X:%02X:%02X:%02X",
|
|
||||||
addr->val[5], addr->val[4], addr->val[3],
|
|
||||||
addr->val[2], addr->val[1], addr->val[0]);
|
|
||||||
}
|
|
||||||
|
|
||||||
const char *bt_addr_str_real(const bt_addr_t *addr)
|
|
||||||
{
|
|
||||||
static char str[BT_ADDR_STR_LEN];
|
|
||||||
|
|
||||||
bt_addr_to_str_mod(addr, str, sizeof(str));
|
|
||||||
|
|
||||||
return str;
|
|
||||||
}
|
|
||||||
|
|
||||||
const char *bt_addr_le_str_real(const bt_addr_le_t *addr)
|
|
||||||
{
|
|
||||||
static char str[BT_ADDR_LE_STR_LEN];
|
|
||||||
|
|
||||||
bt_addr_le_to_str_mod(addr, str, sizeof(str));
|
|
||||||
|
|
||||||
return str;
|
|
||||||
}
|
|
||||||
|
|
||||||
const char *bt_hex_real(const void *buf, size_t len)
|
|
||||||
{
|
|
||||||
static const char hex[] = "0123456789abcdef";
|
|
||||||
static char str[129];
|
|
||||||
const uint8_t *b = buf;
|
|
||||||
size_t i;
|
|
||||||
|
|
||||||
len = MIN(len, (sizeof(str) - 1) / 2);
|
|
||||||
|
|
||||||
for (i = 0; i < len; i++) {
|
|
||||||
str[i * 2] = hex[b[i] >> 4];
|
|
||||||
str[i * 2 + 1] = hex[b[i] & 0xf];
|
|
||||||
}
|
|
||||||
|
|
||||||
str[i * 2] = '\0';
|
|
||||||
|
|
||||||
return str;
|
|
||||||
}
|
|
||||||
|
|
||||||
void z_log_minimal_printk(const char *fmt, ...)
|
|
||||||
{
|
|
||||||
va_list ap;
|
|
||||||
|
|
||||||
va_start(ap, fmt);
|
|
||||||
vprintk(fmt, ap);
|
|
||||||
va_end(ap);
|
|
||||||
}
|
|
|
@ -1,17 +0,0 @@
|
||||||
/*
|
|
||||||
* Copyright (c) 2022 Nordic Semiconductor ASA
|
|
||||||
*
|
|
||||||
* SPDX-License-Identifier: Apache-2.0
|
|
||||||
*/
|
|
||||||
|
|
||||||
#include <zephyr/kernel.h>
|
|
||||||
#include <zephyr/bluetooth/addr.h>
|
|
||||||
|
|
||||||
const char *bt_addr_str_real(const bt_addr_t *addr);
|
|
||||||
const char *bt_addr_le_str_real(const bt_addr_le_t *addr);
|
|
||||||
const char *bt_hex_real(const void *buf, size_t len);
|
|
||||||
void z_log_minimal_printk(const char *fmt, ...);
|
|
||||||
|
|
||||||
#ifndef bt_addr_le_str
|
|
||||||
#define bt_addr_le_str(addr) bt_addr_le_str_real(addr)
|
|
||||||
#endif
|
|
|
@ -1,24 +0,0 @@
|
||||||
#
|
|
||||||
# CMakeLists.txt file for creating of mocks library.
|
|
||||||
#
|
|
||||||
|
|
||||||
add_library(mocks STATIC
|
|
||||||
mocks/id.c
|
|
||||||
mocks/rpa.c
|
|
||||||
mocks/conn.c
|
|
||||||
mocks/hci_core.c
|
|
||||||
mocks/hci_core_expects.c
|
|
||||||
mocks/keys_help_utils.c
|
|
||||||
|
|
||||||
${ZEPHYR_BASE}/subsys/bluetooth/host/keys.c
|
|
||||||
${ZEPHYR_BASE}/subsys/bluetooth/common/addr.c
|
|
||||||
)
|
|
||||||
|
|
||||||
target_include_directories(mocks PUBLIC
|
|
||||||
.
|
|
||||||
${ZEPHYR_BASE}/tests/bluetooth/host
|
|
||||||
${ZEPHYR_BASE}/subsys/bluetooth
|
|
||||||
${ZEPHYR_BASE}/subsys/bluetooth/host
|
|
||||||
)
|
|
||||||
|
|
||||||
target_link_libraries(mocks PRIVATE test_interface)
|
|
|
@ -6,11 +6,16 @@ project(bt_keys_foreach_bond)
|
||||||
find_package(Zephyr COMPONENTS unittest HINTS $ENV{ZEPHYR_BASE})
|
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 host_mocks)
|
||||||
add_subdirectory(${ZEPHYR_BASE}/tests/bluetooth/host/keys mocks)
|
add_subdirectory(${ZEPHYR_BASE}/tests/bluetooth/host/keys/mocks mocks)
|
||||||
|
|
||||||
|
target_link_libraries(testbinary PRIVATE mocks host_mocks)
|
||||||
|
|
||||||
target_sources(testbinary
|
target_sources(testbinary
|
||||||
PRIVATE
|
PRIVATE
|
||||||
src/main.c
|
src/main.c
|
||||||
src/test_suite_foreach_bond_invalid_inputs.c
|
src/test_suite_foreach_bond_invalid_inputs.c
|
||||||
|
|
||||||
|
# Unit under test
|
||||||
|
$ENV{ZEPHYR_BASE}/subsys/bluetooth/host/keys.c
|
||||||
|
$ENV{ZEPHYR_BASE}/subsys/bluetooth/common/addr.c
|
||||||
)
|
)
|
||||||
target_link_libraries(testbinary PRIVATE mocks host_mocks)
|
|
||||||
|
|
|
@ -6,3 +6,6 @@ CONFIG_BT_MAX_PAIRED=4
|
||||||
CONFIG_ASSERT=y
|
CONFIG_ASSERT=y
|
||||||
CONFIG_ASSERT_LEVEL=2
|
CONFIG_ASSERT_LEVEL=2
|
||||||
CONFIG_ASSERT_VERBOSE=y
|
CONFIG_ASSERT_VERBOSE=y
|
||||||
|
CONFIG_LOG=n
|
||||||
|
CONFIG_BT_DEBUG_LOG=n
|
||||||
|
CONFIG_TEST_LOGGING_DEFAULTS=n
|
||||||
|
|
|
@ -6,11 +6,16 @@ project(bt_keys_foreach_type)
|
||||||
find_package(Zephyr COMPONENTS unittest HINTS $ENV{ZEPHYR_BASE})
|
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 host_mocks)
|
||||||
add_subdirectory(${ZEPHYR_BASE}/tests/bluetooth/host/keys mocks)
|
add_subdirectory(${ZEPHYR_BASE}/tests/bluetooth/host/keys/mocks mocks)
|
||||||
|
|
||||||
target_link_libraries(testbinary PRIVATE mocks host_mocks)
|
target_link_libraries(testbinary PRIVATE mocks host_mocks)
|
||||||
|
|
||||||
target_sources(testbinary
|
target_sources(testbinary
|
||||||
PRIVATE
|
PRIVATE
|
||||||
src/main.c
|
src/main.c
|
||||||
src/test_suite_foreach_type_invalid_inputs.c
|
src/test_suite_foreach_type_invalid_inputs.c
|
||||||
|
|
||||||
|
# Unit under test
|
||||||
|
$ENV{ZEPHYR_BASE}/subsys/bluetooth/host/keys.c
|
||||||
|
$ENV{ZEPHYR_BASE}/subsys/bluetooth/common/addr.c
|
||||||
)
|
)
|
||||||
|
|
|
@ -6,3 +6,6 @@ CONFIG_BT_MAX_PAIRED=7
|
||||||
CONFIG_ASSERT=y
|
CONFIG_ASSERT=y
|
||||||
CONFIG_ASSERT_LEVEL=2
|
CONFIG_ASSERT_LEVEL=2
|
||||||
CONFIG_ASSERT_VERBOSE=y
|
CONFIG_ASSERT_VERBOSE=y
|
||||||
|
CONFIG_LOG=n
|
||||||
|
CONFIG_BT_DEBUG_LOG=n
|
||||||
|
CONFIG_TEST_LOGGING_DEFAULTS=n
|
||||||
|
|
|
@ -6,13 +6,20 @@ find_package(Zephyr COMPONENTS unittest HINTS $ENV{ZEPHYR_BASE})
|
||||||
project(bt_keys_get_addr)
|
project(bt_keys_get_addr)
|
||||||
|
|
||||||
add_subdirectory(${ZEPHYR_BASE}/tests/bluetooth/host host_mocks)
|
add_subdirectory(${ZEPHYR_BASE}/tests/bluetooth/host host_mocks)
|
||||||
add_subdirectory(${ZEPHYR_BASE}/tests/bluetooth/host/keys mocks)
|
add_subdirectory(${ZEPHYR_BASE}/tests/bluetooth/host/keys/mocks mocks)
|
||||||
|
|
||||||
target_link_libraries(testbinary PRIVATE mocks host_mocks)
|
target_link_libraries(testbinary PRIVATE mocks host_mocks)
|
||||||
|
|
||||||
target_sources(testbinary
|
target_sources(testbinary
|
||||||
PRIVATE
|
PRIVATE
|
||||||
src/main.c
|
src/main.c
|
||||||
src/test_suite_full_list_invalid_values.c
|
src/test_suite_full_list_invalid_values.c
|
||||||
src/test_suite_full_list_no_overwrite.c
|
src/test_suite_full_list_no_overwrite.c
|
||||||
src/test_suite_full_list_overwrite_oldest.c
|
src/test_suite_full_list_overwrite_oldest.c
|
||||||
|
$ENV{ZEPHYR_BASE}/subsys/bluetooth/common/bt_str.c
|
||||||
|
$ENV{ZEPHYR_BASE}/subsys/bluetooth/host/uuid.c
|
||||||
|
|
||||||
|
# Unit under test
|
||||||
|
$ENV{ZEPHYR_BASE}/subsys/bluetooth/host/keys.c
|
||||||
|
$ENV{ZEPHYR_BASE}/subsys/bluetooth/common/addr.c
|
||||||
)
|
)
|
||||||
|
|
|
@ -6,3 +6,6 @@ CONFIG_BT_MAX_PAIRED=4
|
||||||
CONFIG_ASSERT=y
|
CONFIG_ASSERT=y
|
||||||
CONFIG_ASSERT_LEVEL=2
|
CONFIG_ASSERT_LEVEL=2
|
||||||
CONFIG_ASSERT_VERBOSE=y
|
CONFIG_ASSERT_VERBOSE=y
|
||||||
|
CONFIG_LOG=n
|
||||||
|
CONFIG_BT_DEBUG_LOG=n
|
||||||
|
CONFIG_TEST_LOGGING_DEFAULTS=n
|
||||||
|
|
|
@ -4,6 +4,7 @@
|
||||||
* SPDX-License-Identifier: Apache-2.0
|
* SPDX-License-Identifier: Apache-2.0
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
#include <stdio.h>
|
||||||
#include <zephyr/kernel.h>
|
#include <zephyr/kernel.h>
|
||||||
#include <zephyr/bluetooth/addr.h>
|
#include <zephyr/bluetooth/addr.h>
|
||||||
#include <host/keys.h>
|
#include <host/keys.h>
|
||||||
|
@ -11,11 +12,24 @@
|
||||||
#include "mocks/conn.h"
|
#include "mocks/conn.h"
|
||||||
#include "mocks/hci_core.h"
|
#include "mocks/hci_core.h"
|
||||||
#include "mocks/keys_help_utils.h"
|
#include "mocks/keys_help_utils.h"
|
||||||
#include "host_mocks/print_utils.h"
|
|
||||||
#include "testing_common_defs.h"
|
#include "testing_common_defs.h"
|
||||||
|
#include "common/bt_str.h"
|
||||||
|
|
||||||
DEFINE_FFF_GLOBALS;
|
DEFINE_FFF_GLOBALS;
|
||||||
|
|
||||||
|
/* Define snprintk to use libc since we are not compiling the whole kernel. */
|
||||||
|
int snprintk(char *str, size_t size, const char *fmt, ...)
|
||||||
|
{
|
||||||
|
va_list ap;
|
||||||
|
int ret;
|
||||||
|
|
||||||
|
va_start(ap, fmt);
|
||||||
|
ret = snprintf(str, size, fmt, ap);
|
||||||
|
va_end(ap);
|
||||||
|
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
/* This LUT contains different combinations of ID and Address pairs */
|
/* This LUT contains different combinations of ID and Address pairs */
|
||||||
const struct id_addr_pair testing_id_addr_pair_lut[] = {
|
const struct id_addr_pair testing_id_addr_pair_lut[] = {
|
||||||
{ BT_ADDR_ID_1, BT_ADDR_LE_1 },
|
{ BT_ADDR_ID_1, BT_ADDR_LE_1 },
|
||||||
|
|
21
tests/bluetooth/host/keys/mocks/CMakeLists.txt
Normal file
21
tests/bluetooth/host/keys/mocks/CMakeLists.txt
Normal file
|
@ -0,0 +1,21 @@
|
||||||
|
#
|
||||||
|
# CMakeLists.txt file for creating of mocks library.
|
||||||
|
#
|
||||||
|
|
||||||
|
add_library(mocks STATIC
|
||||||
|
id.c
|
||||||
|
rpa.c
|
||||||
|
conn.c
|
||||||
|
hci_core.c
|
||||||
|
hci_core_expects.c
|
||||||
|
keys_help_utils.c
|
||||||
|
)
|
||||||
|
|
||||||
|
target_include_directories(mocks PUBLIC
|
||||||
|
..
|
||||||
|
${ZEPHYR_BASE}/tests/bluetooth/host
|
||||||
|
${ZEPHYR_BASE}/subsys/bluetooth
|
||||||
|
${ZEPHYR_BASE}/subsys/bluetooth/host
|
||||||
|
)
|
||||||
|
|
||||||
|
target_link_libraries(mocks PRIVATE test_interface)
|
Loading…
Add table
Add a link
Reference in a new issue