Skip to content

Commit 94e9232

Browse files
committed
tests: unit: peer_manager: add peer manager unit tests
Add first set of unit tests for peer manager library. Signed-off-by: Andreas Moltumyr <andreas.moltumyr@nordicsemi.no>
1 parent fd39197 commit 94e9232

12 files changed

Lines changed: 1782 additions & 7 deletions

File tree

lib/bluetooth/peer_manager/modules/peer_data_storage.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ static void peer_ids_load(void)
179179

180180
peer_data.all_data = peer_data_buffer;
181181

182-
/* Search through existing bonds to look for a duplicate. */
182+
/* Allocate peer IDs for already stored bonds. */
183183
pds_peer_data_iterate_prepare(&peer_id_iter);
184184

185185
while (pds_peer_data_iterate(PM_PEER_DATA_ID_BONDING, &peer_id, &peer_data,
@@ -282,7 +282,9 @@ static void bm_zms_evt_handler(const struct bm_zms_evt *evt)
282282
static void wait_for_init(void)
283283
{
284284
while (!fs.init_flags.initialized) {
285+
#if !defined(CONFIG_UNITY)
285286
k_cpu_idle();
287+
#endif
286288
}
287289
}
288290

lib/bluetooth/peer_manager/modules/peer_id.c

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@
99
#include <string.h>
1010
#include <limits.h>
1111
#include <zephyr/sys/atomic.h>
12-
#include <nrf_bitmask.h>
12+
#if !defined(CONFIG_UNITY)
13+
#include <nrfx.h>
14+
#endif
1315
#include <bm/bluetooth/peer_manager/peer_manager_types.h>
1416
#include <modules/peer_id.h>
1517

@@ -40,7 +42,11 @@ static uint32_t find_and_set_flag(atomic_t *pi_flags, uint32_t flag_count)
4042

4143
while (inverted) {
4244
/* Find lowest zero bit */
45+
#if defined(CONFIG_UNITY)
46+
uint32_t first_zero = __builtin_ctz(inverted);
47+
#else
4348
uint32_t first_zero = NRF_CTZ(inverted);
49+
#endif
4450
uint32_t first_zero_global = first_zero + (i * 32);
4551

4652
if (first_zero_global >= flag_count) {

lib/bluetooth/peer_manager/modules/security_manager.c

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -406,7 +406,7 @@ static void sec_req_process(const struct pm_evt *event)
406406
(void)link_secure(event->conn_handle, null_params, force_repairing, true);
407407
/* The error code has been properly handled inside link_secure(). */
408408
}
409-
#endif
409+
#endif /* CONFIG_SOFTDEVICE_CENTRAL */
410410

411411
/**
412412
* @brief Function for translating an SMD event to an SM event and passing it on to SM event
@@ -434,7 +434,7 @@ void sm_smd_evt_handler(struct pm_evt *event)
434434
case PM_EVT_PERIPHERAL_SECURITY_REQ:
435435
#if defined(CONFIG_SOFTDEVICE_CENTRAL)
436436
sec_req_process(event);
437-
#endif
437+
#endif /* CONFIG_SOFTDEVICE_CENTRAL */
438438
/* fallthrough */
439439
default:
440440
/* Forward the event to all registered Security Manager event handlers. */
@@ -518,6 +518,9 @@ uint32_t sm_init(void)
518518
return NRF_ERROR_INTERNAL;
519519
}
520520

521+
default_sec_params = NULL;
522+
default_sec_params_set = false;
523+
521524
module_initialized = true;
522525

523526
return NRF_SUCCESS;

lib/bluetooth/peer_manager/peer_manager.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -389,6 +389,10 @@ uint32_t pm_register(pm_evt_handler_t event_handler)
389389
return NRF_ERROR_INVALID_STATE;
390390
}
391391

392+
if (!event_handler) {
393+
return NRF_ERROR_NULL;
394+
}
395+
392396
if (n_registrants >= CONFIG_PM_MAX_REGISTRANTS) {
393397
return NRF_ERROR_NO_MEM;
394398
}

subsys/softdevice_handler/nrf_sdh_ble.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ const char *nrf_sdh_ble_evt_to_str(uint32_t evt)
6262
return "BLE_GAP_EVT_SEC_REQUEST";
6363
case BLE_GAP_EVT_CONN_PARAM_UPDATE_REQUEST:
6464
return "BLE_GAP_EVT_CONN_PARAM_UPDATE_REQUEST";
65-
#endif
65+
#endif /* CONFIG_SOFTDEVICE_CENTRAL */
6666
case BLE_GAP_EVT_SCAN_REQ_REPORT:
6767
return "BLE_GAP_EVT_SCAN_REQ_REPORT";
6868
case BLE_GAP_EVT_PHY_UPDATE_REQUEST:
@@ -175,13 +175,13 @@ static int default_cfg_set(void)
175175
ble_cfg.gap_cfg.role_count_cfg.adv_set_count = BLE_GAP_ADV_SET_COUNT_DEFAULT;
176176
#endif
177177

178-
#if CONFIG_SOFTDEVICE_CENTRAL
178+
#if defined(CONFIG_SOFTDEVICE_CENTRAL)
179179
ble_cfg.gap_cfg.role_count_cfg.central_role_count =
180180
CONFIG_NRF_SDH_BLE_CENTRAL_LINK_COUNT;
181181
ble_cfg.gap_cfg.role_count_cfg.central_sec_count =
182182
MIN(CONFIG_NRF_SDH_BLE_CENTRAL_LINK_COUNT,
183183
BLE_GAP_ROLE_COUNT_CENTRAL_SEC_DEFAULT);
184-
#endif
184+
#endif /* CONFIG_SOFTDEVICE_CENTRAL */
185185

186186
err = sd_ble_cfg_set(BLE_GAP_CFG_ROLE_COUNT, &ble_cfg, app_ram_start);
187187
if (err) {
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
#
2+
# Copyright (c) 2025 Nordic Semiconductor ASA
3+
#
4+
# SPDX-License-Identifier: LicenseRef-Nordic-5-Clause
5+
#
6+
7+
cmake_minimum_required(VERSION 3.20.0)
8+
9+
find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE})
10+
11+
project(unit_test_peer_manager)
12+
13+
include(${ZEPHYR_NRF_BM_MODULE_DIR}/cmake/unity/unity_softdevice_setup.cmake)
14+
unity_softdevice_header_setup(VARIANT "s145")
15+
unity_softdevice_event_setup()
16+
17+
cmock_handle(${SOFTDEVICE_INCLUDE_DIR}/ble_gap.h)
18+
cmock_handle(${SOFTDEVICE_INCLUDE_DIR}/ble_gattc.h)
19+
cmock_handle(${SOFTDEVICE_INCLUDE_DIR}/ble_gatts.h)
20+
cmock_handle(${SOFTDEVICE_INCLUDE_DIR}/nrf_soc.h)
21+
cmock_handle(${ZEPHYR_NRF_BM_MODULE_DIR}/include/bm/bm_timer.h)
22+
cmock_handle(${ZEPHYR_NRF_BM_MODULE_DIR}/include/bm/fs/bm_zms.h)
23+
cmock_handle(${ZEPHYR_NRF_BM_MODULE_DIR}/include/bm/softdevice_handler/nrf_sdh_ble.h)
24+
cmock_handle(include/psa/crypto.h) # Local definitions for mocking of psa/crypto.h with CMock
25+
26+
# Increase the memory size for CMock. The default of 32768 is not sufficient for the tests.
27+
zephyr_compile_definitions(CMOCK_MEM_SIZE=65536)
28+
29+
zephyr_include_directories(
30+
${CMAKE_CURRENT_SOURCE_DIR}/include # Include test specific headers
31+
)
32+
33+
# Generate and add test file
34+
test_runner_generate(src/unity_test.c)
35+
target_sources(app PRIVATE src/unity_test.c)
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# Clear dependencies for PEER_MANAGER, PM_SERVICE_CHANGED, and PM_LESC,
2+
# then enable them to allow testing the features.
3+
config PEER_MANAGER
4+
default y
5+
6+
config PM_SERVICE_CHANGED
7+
default y
8+
9+
config PM_LESC
10+
default y
11+
12+
# Redefine Kconfigs used by the tested module that is defined in
13+
# other modules we do not want to enable.
14+
config NRF_SDH_BLE_TOTAL_LINK_COUNT
15+
default 2
16+
17+
config SOFTDEVICE_PERIPHERAL
18+
default y
19+
20+
config SOFTDEVICE_CENTRAL
21+
default y
22+
23+
config BM_STORAGE_BACKEND_SD
24+
default y
25+
26+
source "Kconfig.zephyr"
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
/* Mock values for PEER_MANAGER_NODE, PEER_MANAGER_PARTITION_OFFSET and
2+
* PEER_MANAGER_PARTITION_SIZE in peer manger file peer_data_storage.c.
3+
*/
4+
peer_manager_partition: &storage_partition {};
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/*
2+
* Copyright (c) 2025 Nordic Semiconductor ASA
3+
*
4+
* SPDX-License-Identifier: LicenseRef-Nordic-5-Clause
5+
*/
6+
7+
#ifndef PSA_CRYPTO_FOR_MOCKING_H__
8+
#define PSA_CRYPTO_FOR_MOCKING_H__
9+
10+
#include <stddef.h>
11+
#include <stdint.h>
12+
13+
/* Header for mocking the required psa/crypto.h functionality with CMock.
14+
* For use in nrf_ble_lesc unit tests.
15+
*/
16+
17+
typedef uint32_t psa_key_id_t;
18+
typedef psa_key_id_t mbedtls_svc_key_id_t;
19+
typedef int32_t psa_status_t;
20+
typedef uint16_t psa_key_type_t;
21+
22+
typedef int psa_key_attributes_t;
23+
typedef uint32_t psa_key_usage_t;
24+
typedef uint32_t psa_key_lifetime_t;
25+
typedef uint32_t psa_algorithm_t;
26+
typedef uint8_t psa_ecc_family_t;
27+
28+
#define PSA_KEY_ATTRIBUTES_INIT 0x00C0FFEE
29+
30+
#define PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE(key_type, key_bits) (65)
31+
#define PSA_KEY_TYPE_ECC_KEY_PAIR(curve) 0
32+
#define PSA_ECC_FAMILY_SECP_R1 ((psa_ecc_family_t)0x12)
33+
34+
#define PSA_KEY_USAGE_DERIVE ((psa_key_usage_t)0x00004000)
35+
#define PSA_KEY_LIFETIME_VOLATILE ((psa_key_lifetime_t)0x00000000)
36+
#define PSA_ALG_ECDH ((psa_algorithm_t)0x09020000)
37+
38+
#define PSA_SUCCESS ((psa_status_t)0)
39+
#define PSA_ERROR_INVALID_HANDLE ((psa_status_t)-136)
40+
#define PSA_ERROR_BAD_STATE ((psa_status_t)-137)
41+
42+
psa_status_t psa_crypto_init(void);
43+
psa_status_t psa_destroy_key(mbedtls_svc_key_id_t key);
44+
void psa_set_key_usage_flags(psa_key_attributes_t *attributes, psa_key_usage_t usage_flags);
45+
void psa_set_key_lifetime(psa_key_attributes_t *attributes, psa_key_lifetime_t lifetime);
46+
void psa_set_key_algorithm(psa_key_attributes_t *attributes, psa_algorithm_t alg);
47+
void psa_set_key_type(psa_key_attributes_t *attributes, psa_key_type_t type);
48+
void psa_set_key_bits(psa_key_attributes_t *attributes, size_t bits);
49+
psa_status_t psa_generate_key(const psa_key_attributes_t *attributes, mbedtls_svc_key_id_t *key);
50+
psa_status_t psa_export_public_key(mbedtls_svc_key_id_t key, uint8_t *data, size_t data_size,
51+
size_t *data_length);
52+
psa_status_t psa_raw_key_agreement(psa_algorithm_t alg, mbedtls_svc_key_id_t private_key,
53+
const uint8_t *peer_key, size_t peer_key_length,
54+
uint8_t *output, size_t output_size, size_t *output_length);
55+
psa_status_t psa_generate_random(uint8_t *output, size_t output_size);
56+
57+
#endif /* PSA_CRYPTO_FOR_MOCKING_H__ */
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
CONFIG_UNITY=y
2+
3+
# Enable generation of new LESC key pair after every pairing attempt
4+
# CONFIG_PM_LESC_GENERATE_NEW_KEYS=y
5+
6+
# Repeated Attempts Protection feature
7+
CONFIG_PM_RA_PROTECTION=y
8+
9+
# Include sec delay code
10+
CONFIG_PM_HANDLER_SEC_DELAY_MS=100

0 commit comments

Comments
 (0)