Skip to content

Commit

Permalink
tests: Bluetooth: BAP: Add test to test invalid bcode
Browse files Browse the repository at this point in the history
Add a babblesim test for testing the behavior when providing
an invalid broadcast code as the broadcast sink.

Signed-off-by: Emil Gydesen <emil.gydesen@nordicsemi.no>
  • Loading branch information
Thalley authored and nashif committed Aug 30, 2024
1 parent 1fa2788 commit 303d0b7
Show file tree
Hide file tree
Showing 3 changed files with 110 additions and 16 deletions.
91 changes: 76 additions & 15 deletions tests/bsim/bluetooth/audio/src/bap_broadcast_sink_test.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2021-2023 Nordic Semiconductor ASA
* Copyright (c) 2021-2024 Nordic Semiconductor ASA
*
* SPDX-License-Identifier: Apache-2.0
*/
Expand All @@ -20,6 +20,7 @@
#include <zephyr/bluetooth/audio/pacs.h>
#include <zephyr/bluetooth/bluetooth.h>
#include <zephyr/bluetooth/gap.h>
#include <zephyr/bluetooth/hci_types.h>
#include <zephyr/bluetooth/iso.h>
#include <zephyr/bluetooth/uuid.h>
#include <zephyr/kernel.h>
Expand All @@ -46,6 +47,7 @@ CREATE_FLAG(flag_pa_sync_lost);
CREATE_FLAG(flag_received);
CREATE_FLAG(flag_pa_request);
CREATE_FLAG(flag_bis_sync_requested);
CREATE_FLAG(flag_big_sync_mic_failure);

static struct bt_bap_broadcast_sink *g_sink;
static struct bt_le_scan_recv_info broadcaster_info;
Expand Down Expand Up @@ -551,6 +553,10 @@ static void stopped_cb(struct bt_bap_stream *stream, uint8_t reason)
{
printk("Stream %p stopped with reason 0x%02X\n", stream, reason);
k_sem_give(&sem_stopped);

if (reason == BT_HCI_ERR_TERM_DUE_TO_MIC_FAIL) {
SET_FLAG(flag_big_sync_mic_failure);
}
}

static void recv_cb(struct bt_bap_stream *stream,
Expand Down Expand Up @@ -745,23 +751,16 @@ static void test_broadcast_sink_create_inval(void)
}
}

static void test_broadcast_sync(bool encryption)
static void test_broadcast_sync(const uint8_t broadcast_code[BT_AUDIO_BROADCAST_CODE_SIZE])
{
int err;

printk("Syncing the sink\n");
err = bt_bap_broadcast_sink_sync(g_sink, bis_index_bitfield, streams,
encryption ? BROADCAST_CODE : NULL);
err = bt_bap_broadcast_sink_sync(g_sink, bis_index_bitfield, streams, broadcast_code);
if (err != 0) {
FAIL("Unable to sync the sink: %d\n", err);
return;
}

/* Wait for all to be started */
printk("Waiting for streams to be started\n");
for (size_t i = 0U; i < ARRAY_SIZE(streams); i++) {
k_sem_take(&sem_started, K_FOREVER);
}
}

static void test_broadcast_sync_inval(void)
Expand Down Expand Up @@ -932,7 +931,13 @@ static void test_common(void)
WAIT_FOR_FLAG(flag_syncable);

test_broadcast_sync_inval();
test_broadcast_sync(false);
test_broadcast_sync(NULL);

/* Wait for all to be started */
printk("Waiting for streams to be started\n");
for (size_t i = 0U; i < ARRAY_SIZE(streams); i++) {
k_sem_take(&sem_started, K_FOREVER);
}

printk("Waiting for data\n");
WAIT_FOR_FLAG(flag_received);
Expand Down Expand Up @@ -974,7 +979,14 @@ static void test_sink_disconnect(void)
test_broadcast_stop();

/* Retry sync*/
test_broadcast_sync(false);
test_broadcast_sync(NULL);

/* Wait for all to be started */
printk("Waiting for streams to be started\n");
for (size_t i = 0U; i < ARRAY_SIZE(streams); i++) {
k_sem_take(&sem_started, K_FOREVER);
}

test_broadcast_stop();

test_broadcast_delete_inval();
Expand Down Expand Up @@ -1006,7 +1018,13 @@ static void test_sink_encrypted(void)
printk("Waiting for BIG syncable\n");
WAIT_FOR_FLAG(flag_syncable);

test_broadcast_sync(true);
test_broadcast_sync(BROADCAST_CODE);

/* Wait for all to be started */
printk("Waiting for streams to be started\n");
for (size_t i = 0U; i < ARRAY_SIZE(streams); i++) {
k_sem_take(&sem_started, K_FOREVER);
}

printk("Waiting for data\n");
WAIT_FOR_FLAG(flag_received);
Expand All @@ -1027,7 +1045,38 @@ static void test_sink_encrypted(void)
k_sem_take(&sem_stopped, K_FOREVER);
}

PASS("Broadcast sink passed\n");
PASS("Broadcast sink encrypted passed\n");
}

static void test_sink_encrypted_incorrect_code(void)
{
int err;

err = init();
if (err) {
FAIL("Init failed (err %d)\n", err);
return;
}

test_scan_and_pa_sync();

test_broadcast_sink_create();

printk("Broadcast source PA synced, waiting for BASE\n");
WAIT_FOR_FLAG(flag_base_received);
printk("BASE received\n");

printk("Waiting for BIG syncable\n");
WAIT_FOR_FLAG(flag_syncable);

test_broadcast_sync(INCORRECT_BROADCAST_CODE);
/* Wait for MIC failure */
WAIT_FOR_FLAG(flag_big_sync_mic_failure);

backchannel_sync_send_all(); /* let other devices know we have received data */
backchannel_sync_send_all(); /* let the broadcast source know it can stop */

PASS("Broadcast sink incorrect code passed\n");
}

static void broadcast_sink_with_assistant(void)
Expand Down Expand Up @@ -1058,7 +1107,13 @@ static void broadcast_sink_with_assistant(void)

printk("Waiting for BIG sync request\n");
WAIT_FOR_FLAG(flag_bis_sync_requested);
test_broadcast_sync(false);
test_broadcast_sync(NULL);

/* Wait for all to be started */
printk("Waiting for streams to be started\n");
for (size_t i = 0U; i < ARRAY_SIZE(streams); i++) {
k_sem_take(&sem_started, K_FOREVER);
}

printk("Waiting for data\n");
WAIT_FOR_FLAG(flag_received);
Expand Down Expand Up @@ -1102,6 +1157,12 @@ static const struct bst_test_instance test_broadcast_sink[] = {
.test_tick_f = test_tick,
.test_main_f = test_sink_encrypted,
},
{
.test_id = "broadcast_sink_encrypted_incorrect_code",
.test_pre_init_f = test_init,
.test_tick_f = test_tick,
.test_main_f = test_sink_encrypted_incorrect_code,
},
{
.test_id = "broadcast_sink_with_assistant",
.test_pre_init_f = test_init,
Expand Down
4 changes: 3 additions & 1 deletion tests/bsim/bluetooth/audio/src/bap_common.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,9 @@
#define BROADCAST_CODE \
((uint8_t[]){0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xaa, 0xbb, 0xcc, \
0xdd, 0xee, 0xff})

#define INCORRECT_BROADCAST_CODE \
((uint8_t[]){0xDE, 0xAD, 0xBE, 0xEF, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xaa, 0xbb, 0xcc, \
0xdd, 0xee, 0xff})
struct unicast_stream {
struct bt_cap_stream stream;
struct bt_audio_codec_cfg codec_cfg;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#!/usr/bin/env bash
#
# Copyright (c) 2021-2024 Nordic Semiconductor ASA
#
# SPDX-License-Identifier: Apache-2.0

VERBOSITY_LEVEL=2
EXECUTE_TIMEOUT=100

source ${ZEPHYR_BASE}/tests/bsim/sh_common.source

cd ${BSIM_OUT_PATH}/bin

printf "\n\n======== Broadcaster encrypted incorrect code test =========\n\n"

SIMULATION_ID="broadcaster_encrypted_incorrect_code"

Execute ./bs_${BOARD_TS}_tests_bsim_bluetooth_audio_prj_conf \
-v=${VERBOSITY_LEVEL} -s=${SIMULATION_ID} -d=0 -testid=broadcast_source_encrypted \
-RealEncryption=1 -rs=23 -D=2


Execute ./bs_${BOARD_TS}_tests_bsim_bluetooth_audio_prj_conf \
-v=${VERBOSITY_LEVEL} -s=${SIMULATION_ID} -d=1 -testid=broadcast_sink_encrypted_incorrect_code \
-RealEncryption=1 -rs=27 -D=2

# Simulation time should be larger than the WAIT_TIME in common.h
Execute ./bs_2G4_phy_v1 -v=${VERBOSITY_LEVEL} -s=${SIMULATION_ID} \
-D=2 -sim_length=60e6 $@

wait_for_background_jobs

0 comments on commit 303d0b7

Please sign in to comment.