Skip to content

Commit 6ac7bda

Browse files
sjanccarlescufi
authored andcommitted
tests: Bluetooth: Add test for invalid LLCP PDU sized
This verifies that invalid size PDUs are detected. Signed-off-by: Szymon Janc <[email protected]>
1 parent 57a94bf commit 6ac7bda

File tree

6 files changed

+216
-0
lines changed

6 files changed

+216
-0
lines changed

tests/bluetooth/controller/common/include/helper_pdu.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,8 @@ void helper_pdu_encode_cte_req(struct pdu_data *pdu, void *param);
5252
void helper_pdu_encode_cte_rsp(struct pdu_data *pdu, void *param);
5353
void helper_node_encode_cte_rsp(struct node_rx_pdu *rx, void *param);
5454

55+
void helper_pdu_encode_zero(struct pdu_data *pdu, void *param);
56+
5557
void helper_pdu_verify_ping_req(const char *file, uint32_t line, struct pdu_data *pdu, void *param);
5658
void helper_pdu_verify_ping_rsp(const char *file, uint32_t line, struct pdu_data *pdu, void *param);
5759

@@ -161,6 +163,7 @@ enum helper_pdu_opcode {
161163
LL_LENGTH_RSP,
162164
LL_CTE_REQ,
163165
LL_CTE_RSP,
166+
LL_ZERO,
164167
};
165168

166169
enum helper_node_opcode {

tests/bluetooth/controller/common/src/helper_pdu.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -391,6 +391,12 @@ void helper_pdu_encode_cte_rsp(struct pdu_data *pdu, void *param)
391391
pdu->llctrl.opcode = PDU_DATA_LLCTRL_TYPE_CTE_RSP;
392392
}
393393

394+
void helper_pdu_encode_zero(struct pdu_data *pdu, void *param)
395+
{
396+
pdu->ll_id = PDU_DATA_LLID_CTRL;
397+
pdu->len = 0;
398+
}
399+
394400
void helper_node_encode_cte_rsp(struct node_rx_pdu *rx, void *param)
395401
{
396402
rx->hdr.rx_ftr.iq_report = (struct cte_conn_iq_report *)param;

tests/bluetooth/controller/common/src/helper_util.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ helper_pdu_encode_func_t *const helper_pdu_encode[] = {
7878
[LL_LENGTH_RSP] = helper_pdu_encode_length_rsp,
7979
[LL_CTE_REQ] = helper_pdu_encode_cte_req,
8080
[LL_CTE_RSP] = helper_pdu_encode_cte_rsp,
81+
[LL_ZERO] = helper_pdu_encode_zero,
8182
};
8283

8384
helper_pdu_verify_func_t *const helper_pdu_verify[] = {
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# SPDX-License-Identifier: Apache-2.0
2+
3+
cmake_minimum_required(VERSION 3.20.0)
4+
5+
if (NOT BOARD STREQUAL unit_testing)
6+
message(FATAL_ERROR "This project can only be used with '-DBOARD=unit_testing'.")
7+
endif()
8+
9+
FILE(GLOB SOURCES
10+
src/*.c
11+
)
12+
13+
project(bluetooth_ull_llcp_invalid)
14+
find_package(ZephyrUnittest HINTS $ENV{ZEPHYR_BASE})
15+
include(${ZEPHYR_BASE}/tests/bluetooth/controller/common/defaults_cmake.txt)
16+
17+
target_sources(testbinary PRIVATE ${ll_sw_sources} ${mock_sources} ${common_sources})
Lines changed: 184 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,184 @@
1+
/*
2+
* Copyright (c) 2022 Nordic Semiconductor ASA
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
#include <zephyr/types.h>
8+
#include <ztest.h>
9+
10+
#include <bluetooth/hci.h>
11+
#include <sys/byteorder.h>
12+
#include <sys/slist.h>
13+
#include <sys/util.h>
14+
#include "hal/ccm.h"
15+
16+
#include "util/util.h"
17+
#include "util/mem.h"
18+
#include "util/memq.h"
19+
#include "util/dbuf.h"
20+
21+
#include "pdu.h"
22+
#include "ll.h"
23+
#include "ll_settings.h"
24+
#include "ll_feat.h"
25+
26+
#include "lll.h"
27+
#include "lll_df_types.h"
28+
#include "lll_conn.h"
29+
#include "ull_tx_queue.h"
30+
31+
#include "ull_conn_types.h"
32+
#include "ull_llcp.h"
33+
#include "ull_llcp_internal.h"
34+
35+
#include "helper_pdu.h"
36+
#include "helper_util.h"
37+
38+
struct ll_conn test_conn;
39+
40+
static void setup(void)
41+
{
42+
test_setup(&test_conn);
43+
}
44+
45+
#define LLCTRL_PDU_SIZE (offsetof(struct pdu_data, llctrl) + sizeof(struct pdu_data_llctrl))
46+
47+
/* +-----+ +-------+ +-----+
48+
* | UT | | LL_A | | LT |
49+
* +-----+ +-------+ +-----+
50+
* | | |
51+
* | | <PDU> |
52+
* | |<------------------|
53+
* | | |
54+
*/
55+
56+
static void lt_tx_invalid_pdu_size(enum helper_pdu_opcode opcode, int adj_size)
57+
{
58+
struct pdu_data_llctrl_unknown_rsp unknown_rsp;
59+
struct pdu_data pdu;
60+
struct node_tx *tx;
61+
/* PDU contents does not matter when testing for invalid PDU size */
62+
uint8_t data[LLCTRL_PDU_SIZE] = { 0 };
63+
64+
/* Encode a PDU for the opcode */
65+
encode_pdu(opcode, &pdu, &data);
66+
67+
/* Setup the LL_UNKNOWN_RSP expected for the PDU */
68+
if (opcode == LL_ZERO) {
69+
/* we use 0xff in response if length was 0 */
70+
unknown_rsp.type = PDU_DATA_LLCTRL_TYPE_UNUSED;
71+
} else {
72+
unknown_rsp.type = pdu.llctrl.opcode;
73+
}
74+
75+
/* adjust PDU len */
76+
pdu.len += adj_size;
77+
78+
/* Connect */
79+
ull_cp_state_set(&test_conn, ULL_CP_CONNECTED);
80+
81+
/* Prepare */
82+
event_prepare(&test_conn);
83+
84+
/* Rx */
85+
lt_tx_no_encode(&pdu, &test_conn, NULL);
86+
87+
/* Done */
88+
event_done(&test_conn);
89+
90+
/* Prepare */
91+
event_prepare(&test_conn);
92+
93+
/* Tx Queue should have one LL Control PDU */
94+
lt_rx(LL_UNKNOWN_RSP, &test_conn, &tx, &unknown_rsp);
95+
lt_rx_q_is_empty(&test_conn);
96+
97+
/* Done */
98+
event_done(&test_conn);
99+
100+
/* Release Tx */
101+
ull_cp_release_tx(&test_conn, tx);
102+
103+
/* There should not be a host notifications */
104+
ut_rx_q_is_empty();
105+
106+
zassert_equal(ctx_buffers_free(), test_ctx_buffers_cnt(),
107+
"Free CTX buffers %d", ctx_buffers_free());
108+
}
109+
110+
void test_invalid_pdu_ignore_rx(void)
111+
{
112+
/* Role */
113+
test_set_role(&test_conn, BT_HCI_ROLE_PERIPHERAL);
114+
115+
/* Test too small PDUs */
116+
lt_tx_invalid_pdu_size(LL_ZERO, 0); /* 0 length PDU */
117+
lt_tx_invalid_pdu_size(LL_VERSION_IND, -1);
118+
/* lt_tx_invalid_pdu_size(LL_LE_PING_REQ, -1); */
119+
/* lt_tx_invalid_pdu_size(LL_LE_PING_RSP, -1); */
120+
lt_tx_invalid_pdu_size(LL_FEATURE_REQ, -1);
121+
lt_tx_invalid_pdu_size(LL_PERIPH_FEAT_XCHG, -1);
122+
lt_tx_invalid_pdu_size(LL_FEATURE_RSP, -1);
123+
lt_tx_invalid_pdu_size(LL_MIN_USED_CHANS_IND, -1);
124+
lt_tx_invalid_pdu_size(LL_REJECT_IND, -1);
125+
lt_tx_invalid_pdu_size(LL_REJECT_EXT_IND, -1);
126+
lt_tx_invalid_pdu_size(LL_ENC_REQ, -1);
127+
lt_tx_invalid_pdu_size(LL_ENC_RSP, -1);
128+
/* lt_tx_invalid_pdu_size(LL_START_ENC_REQ, -1); 0 length */
129+
/* lt_tx_invalid_pdu_size(LL_START_ENC_RSP, -1); 0 length */
130+
/* lt_tx_invalid_pdu_size(LL_PAUSE_ENC_REQ, -1); 0 length */
131+
/* lt_tx_invalid_pdu_size(LL_PAUSE_ENC_RSP, -1); 0 length */
132+
lt_tx_invalid_pdu_size(LL_PHY_REQ, -1);
133+
lt_tx_invalid_pdu_size(LL_PHY_RSP, -1);
134+
lt_tx_invalid_pdu_size(LL_PHY_UPDATE_IND, -1);
135+
lt_tx_invalid_pdu_size(LL_UNKNOWN_RSP, -1);
136+
lt_tx_invalid_pdu_size(LL_CONNECTION_UPDATE_IND, -1);
137+
lt_tx_invalid_pdu_size(LL_CONNECTION_PARAM_REQ, -1);
138+
lt_tx_invalid_pdu_size(LL_CONNECTION_PARAM_RSP, -1);
139+
lt_tx_invalid_pdu_size(LL_TERMINATE_IND, -1);
140+
lt_tx_invalid_pdu_size(LL_CHAN_MAP_UPDATE_IND, -1);
141+
lt_tx_invalid_pdu_size(LL_LENGTH_REQ, -1);
142+
lt_tx_invalid_pdu_size(LL_LENGTH_RSP, -1);
143+
lt_tx_invalid_pdu_size(LL_CTE_REQ, -1);
144+
/* lt_tx_invalid_pdu_size(LL_CTE_RSP, -1); 0 length */
145+
146+
/* Test too big PDUs */
147+
lt_tx_invalid_pdu_size(LL_VERSION_IND, 1);
148+
lt_tx_invalid_pdu_size(LL_LE_PING_REQ, 1);
149+
lt_tx_invalid_pdu_size(LL_LE_PING_RSP, 1);
150+
lt_tx_invalid_pdu_size(LL_FEATURE_REQ, 1);
151+
lt_tx_invalid_pdu_size(LL_PERIPH_FEAT_XCHG, 1);
152+
lt_tx_invalid_pdu_size(LL_FEATURE_RSP, 1);
153+
lt_tx_invalid_pdu_size(LL_MIN_USED_CHANS_IND, 1);
154+
lt_tx_invalid_pdu_size(LL_REJECT_IND, 1);
155+
lt_tx_invalid_pdu_size(LL_REJECT_EXT_IND, 1);
156+
lt_tx_invalid_pdu_size(LL_ENC_REQ, 1);
157+
lt_tx_invalid_pdu_size(LL_ENC_RSP, 1);
158+
lt_tx_invalid_pdu_size(LL_START_ENC_REQ, 1);
159+
lt_tx_invalid_pdu_size(LL_START_ENC_RSP, 1);
160+
lt_tx_invalid_pdu_size(LL_PAUSE_ENC_REQ, 1);
161+
lt_tx_invalid_pdu_size(LL_PAUSE_ENC_RSP, 1);
162+
lt_tx_invalid_pdu_size(LL_PHY_REQ, 1);
163+
lt_tx_invalid_pdu_size(LL_PHY_RSP, 1);
164+
lt_tx_invalid_pdu_size(LL_PHY_UPDATE_IND, 1);
165+
lt_tx_invalid_pdu_size(LL_UNKNOWN_RSP, 1);
166+
lt_tx_invalid_pdu_size(LL_CONNECTION_UPDATE_IND, 1);
167+
lt_tx_invalid_pdu_size(LL_CONNECTION_PARAM_REQ, 1);
168+
lt_tx_invalid_pdu_size(LL_CONNECTION_PARAM_RSP, 1);
169+
lt_tx_invalid_pdu_size(LL_TERMINATE_IND, 1);
170+
lt_tx_invalid_pdu_size(LL_CHAN_MAP_UPDATE_IND, 1);
171+
lt_tx_invalid_pdu_size(LL_LENGTH_REQ, 1);
172+
lt_tx_invalid_pdu_size(LL_LENGTH_RSP, 1);
173+
lt_tx_invalid_pdu_size(LL_CTE_REQ, 1);
174+
lt_tx_invalid_pdu_size(LL_CTE_RSP, 1);
175+
}
176+
177+
void test_main(void)
178+
{
179+
ztest_test_suite(invalid,
180+
ztest_unit_test_setup_teardown(test_invalid_pdu_ignore_rx, setup,
181+
unit_test_noop));
182+
183+
ztest_run_test_suite(invalid);
184+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
common:
2+
tags: test_framework bluetooth bt_invalid bt_ull_llcp
3+
tests:
4+
bluetooth.controller.ctrl_invalid.test:
5+
type: unit

0 commit comments

Comments
 (0)