Skip to content

Commit 686a8c9

Browse files
Thalleyhenrikbrixandersen
authored andcommitted
tests: bsim: Bluetooth: Add GATT encrypt tests
Add tests for the GATT permissions to read and write for characteristics that require encryption and LESC encryption. Signed-off-by: Emil Gydesen <[email protected]>
1 parent fa4a6f3 commit 686a8c9

File tree

4 files changed

+113
-16
lines changed

4 files changed

+113
-16
lines changed
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
CONFIG_BT=y
2+
CONFIG_BT_SMP=y
23
CONFIG_BT_DEVICE_NAME="GATT tester"
34
CONFIG_BT_PERIPHERAL=y
45
CONFIG_BT_CENTRAL=y
56
CONFIG_BT_GATT_CLIENT=y
67
CONFIG_BT_ATT_PREPARE_COUNT=3
8+
# Disable auto security so that we can test security errors
9+
CONFIG_BT_ATT_RETRY_ON_SEC_ERR=n

tests/bsim/bluetooth/host/gatt/general/src/common.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,5 +62,13 @@ extern enum bst_result_t bst_result;
6262
BT_UUID_DECLARE_128(0x01, 0x23, 0x45, 0x67, 0x89, 0x01, 0x02, 0x03, \
6363
0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0xFF, 0x11)
6464

65+
#define TEST_ENC_CHRC_UUID \
66+
BT_UUID_DECLARE_128(0x01, 0x23, 0x45, 0x67, 0x89, 0x01, 0x02, 0x03, \
67+
0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0xFF, 0x22)
68+
69+
#define TEST_LESC_CHRC_UUID \
70+
BT_UUID_DECLARE_128(0x01, 0x23, 0x45, 0x67, 0x89, 0x01, 0x02, 0x03, \
71+
0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0xFF, 0x33)
72+
6573
void test_tick(bs_time_t HW_device_time);
6674
void test_init(void);

tests/bsim/bluetooth/host/gatt/general/src/gatt_client_test.c

Lines changed: 94 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,16 @@
1111

1212
CREATE_FLAG(flag_is_connected);
1313
CREATE_FLAG(flag_discover_complete);
14+
CREATE_FLAG(flag_security_changed);
1415
CREATE_FLAG(flag_write_complete);
1516
CREATE_FLAG(flag_read_complete);
1617

1718
static struct bt_conn *g_conn;
1819
static uint16_t chrc_handle;
1920
static uint16_t long_chrc_handle;
21+
static uint16_t enc_chrc_handle;
22+
static uint16_t lesc_chrc_handle;
23+
static uint8_t att_err;
2024
static const struct bt_uuid *test_svc_uuid = TEST_SERVICE_UUID;
2125

2226
#define ARRAY_ITEM(i, _) i
@@ -61,9 +65,19 @@ static void disconnected(struct bt_conn *conn, uint8_t reason)
6165
UNSET_FLAG(flag_is_connected);
6266
}
6367

68+
static void security_changed(struct bt_conn *conn, bt_security_t level, enum bt_security_err err)
69+
{
70+
if (err != BT_SECURITY_ERR_SUCCESS) {
71+
FAIL("Security failed (err %d)\n", err);
72+
} else {
73+
SET_FLAG(flag_security_changed);
74+
}
75+
}
76+
6477
BT_CONN_CB_DEFINE(conn_callbacks) = {
6578
.connected = connected,
6679
.disconnected = disconnected,
80+
.security_changed = security_changed,
6781
};
6882

6983
void device_found(const bt_addr_le_t *addr, int8_t rssi, uint8_t type,
@@ -141,6 +155,12 @@ static uint8_t discover_func(struct bt_conn *conn,
141155
} else if (bt_uuid_cmp(chrc->uuid, TEST_LONG_CHRC_UUID) == 0) {
142156
printk("Found long_chrc\n");
143157
long_chrc_handle = chrc->value_handle;
158+
} else if (bt_uuid_cmp(chrc->uuid, TEST_ENC_CHRC_UUID) == 0) {
159+
printk("Found enc_chrc_handle\n");
160+
enc_chrc_handle = chrc->value_handle;
161+
} else if (bt_uuid_cmp(chrc->uuid, TEST_LESC_CHRC_UUID) == 0) {
162+
printk("Found lesc_chrc_handle\n");
163+
lesc_chrc_handle = chrc->value_handle;
144164
}
145165
}
146166

@@ -169,31 +189,49 @@ static void gatt_discover(void)
169189
printk("Discover complete\n");
170190
}
171191

172-
static void gatt_write_cb(struct bt_conn *conn, uint8_t err,
173-
struct bt_gatt_write_params *params)
192+
static void update_security(void)
174193
{
175-
if (err != BT_ATT_ERR_SUCCESS) {
176-
FAIL("Write failed: 0x%02X\n", err);
194+
int err;
195+
196+
printk("Updating security\n");
197+
err = bt_conn_set_security(g_conn, BT_SECURITY_L2);
198+
if (err != 0) {
199+
FAIL("Set security failed (err %d)\n", err);
177200
}
178201

202+
WAIT_FOR_FLAG(flag_security_changed);
203+
printk("Security changed\n");
204+
}
205+
206+
static void gatt_write_cb(struct bt_conn *conn, uint8_t err, struct bt_gatt_write_params *params)
207+
{
179208
(void)memset(params, 0, sizeof(*params));
209+
att_err = err;
180210

181211
SET_FLAG(flag_write_complete);
182212
}
183213

184-
static void gatt_write(uint16_t handle)
214+
static void gatt_write(uint16_t handle, uint8_t expect_att_err)
185215
{
186216
static struct bt_gatt_write_params write_params;
187217
int err;
188218

189219
if (handle == chrc_handle) {
190-
printk("Writing to chrc\n");
220+
printk("Writing to chrc and expecting 0x%02X\n", expect_att_err);
191221
write_params.data = chrc_data;
192222
write_params.length = sizeof(chrc_data);
193223
} else if (handle == long_chrc_handle) {
194-
printk("Writing to long_chrc\n");
224+
printk("Writing to long_chrc and expecting 0x%02X\n", expect_att_err);
195225
write_params.data = long_chrc_data;
196226
write_params.length = sizeof(long_chrc_data);
227+
} else if (handle == enc_chrc_handle) {
228+
printk("Writing to enc_chrc and expecting 0x%02X\n", expect_att_err);
229+
write_params.data = chrc_data;
230+
write_params.length = sizeof(chrc_data);
231+
} else if (handle == lesc_chrc_handle) {
232+
printk("Writing to lesc_chrc and expecting 0x%02X\n", expect_att_err);
233+
write_params.data = chrc_data;
234+
write_params.length = sizeof(chrc_data);
197235
}
198236

199237
write_params.func = gatt_write_cb;
@@ -207,15 +245,25 @@ static void gatt_write(uint16_t handle)
207245
}
208246

209247
WAIT_FOR_FLAG(flag_write_complete);
248+
249+
if (att_err != expect_att_err) {
250+
FAIL("Write failed: 0x%02X\n", att_err);
251+
}
252+
210253
printk("success\n");
211254
}
212255

213256
static uint8_t gatt_read_cb(struct bt_conn *conn, uint8_t err,
214257
struct bt_gatt_read_params *params,
215258
const void *data, uint16_t length)
216259
{
260+
att_err = err;
261+
217262
if (err != BT_ATT_ERR_SUCCESS) {
218-
FAIL("Read failed: 0x%02X\n", err);
263+
printk("Read failed: 0x%02X\n", err);
264+
265+
(void)memset(params, 0, sizeof(*params));
266+
SET_FLAG(flag_read_complete);
219267

220268
return BT_GATT_ITER_STOP;
221269
}
@@ -242,16 +290,25 @@ static uint8_t gatt_read_cb(struct bt_conn *conn, uint8_t err,
242290
FAIL("long_chrc data different than expected (%u %u)\n", length,
243291
LONG_CHRC_SIZE);
244292
}
293+
} else if (params->single.handle == enc_chrc_handle) {
294+
if (data_received_size != CHRC_SIZE ||
295+
memcmp(data_received, chrc_data, data_received_size) != 0) {
296+
FAIL("enc_chrc data different than expected (%u %u)\n", length, CHRC_SIZE);
297+
}
298+
} else if (params->single.handle == lesc_chrc_handle) {
299+
if (data_received_size != CHRC_SIZE ||
300+
memcmp(data_received, chrc_data, data_received_size) != 0) {
301+
FAIL("lesc_chrc data different than expected (%u %u)\n", length, CHRC_SIZE);
302+
}
245303
}
246304

247305
(void)memset(params, 0, sizeof(*params));
248-
249306
SET_FLAG(flag_read_complete);
250307

251308
return BT_GATT_ITER_STOP;
252309
}
253310

254-
static void gatt_read(uint16_t handle)
311+
static void gatt_read(uint16_t handle, uint8_t expect_att_err)
255312
{
256313
static struct bt_gatt_read_params read_params;
257314
int err;
@@ -260,9 +317,13 @@ static void gatt_read(uint16_t handle)
260317
memset(data_received, 0, sizeof(data_received));
261318

262319
if (handle == chrc_handle) {
263-
printk("Reading chrc\n");
320+
printk("Reading chrc and expecting 0x%02X\n", expect_att_err);
264321
} else if (handle == long_chrc_handle) {
265-
printk("Reading long_chrc\n");
322+
printk("Reading long_chrc and expecting 0x%02X\n", expect_att_err);
323+
} else if (handle == enc_chrc_handle) {
324+
printk("Reading enc_chrc and expecting 0x%02X\n", expect_att_err);
325+
} else if (handle == lesc_chrc_handle) {
326+
printk("Reading lesc_chrc and expecting 0x%02X\n", expect_att_err);
266327
}
267328

268329
read_params.func = gatt_read_cb;
@@ -278,6 +339,11 @@ static void gatt_read(uint16_t handle)
278339
}
279340

280341
WAIT_FOR_FLAG(flag_read_complete);
342+
343+
if (att_err != expect_att_err) {
344+
FAIL("Read failed: 0x%02X\n", att_err);
345+
}
346+
281347
printk("success\n");
282348
}
283349

@@ -303,12 +369,24 @@ static void test_main(void)
303369

304370
/* Write and read a few times to ensure stateless behavior */
305371
for (size_t i = 0; i < 3; i++) {
306-
gatt_write(chrc_handle);
307-
gatt_read(chrc_handle);
308-
gatt_write(long_chrc_handle);
309-
gatt_read(long_chrc_handle);
372+
gatt_write(chrc_handle, BT_ATT_ERR_SUCCESS);
373+
gatt_read(chrc_handle, BT_ATT_ERR_SUCCESS);
374+
gatt_write(long_chrc_handle, BT_ATT_ERR_SUCCESS);
375+
gatt_read(long_chrc_handle, BT_ATT_ERR_SUCCESS);
310376
}
311377

378+
gatt_write(enc_chrc_handle, BT_ATT_ERR_AUTHENTICATION);
379+
gatt_read(enc_chrc_handle, BT_ATT_ERR_AUTHENTICATION);
380+
gatt_write(lesc_chrc_handle, BT_ATT_ERR_AUTHENTICATION);
381+
gatt_read(lesc_chrc_handle, BT_ATT_ERR_AUTHENTICATION);
382+
383+
update_security();
384+
385+
gatt_write(enc_chrc_handle, BT_ATT_ERR_SUCCESS);
386+
gatt_read(enc_chrc_handle, BT_ATT_ERR_SUCCESS);
387+
gatt_write(lesc_chrc_handle, BT_ATT_ERR_SUCCESS);
388+
gatt_read(lesc_chrc_handle, BT_ATT_ERR_SUCCESS);
389+
312390
PASS("GATT client Passed\n");
313391
}
314392

tests/bsim/bluetooth/host/gatt/general/src/gatt_server_test.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,14 @@ BT_GATT_SERVICE_DEFINE(test_svc,
134134
BT_GATT_CHRC_WRITE | BT_GATT_CHRC_READ,
135135
BT_GATT_PERM_WRITE | BT_GATT_PERM_READ | BT_GATT_PERM_PREPARE_WRITE,
136136
read_long_test_chrc, write_long_test_chrc, NULL),
137+
BT_GATT_CHARACTERISTIC(TEST_ENC_CHRC_UUID,
138+
BT_GATT_CHRC_WRITE | BT_GATT_CHRC_READ,
139+
BT_GATT_PERM_READ_ENCRYPT | BT_GATT_PERM_WRITE_ENCRYPT,
140+
read_test_chrc, write_test_chrc, NULL),
141+
BT_GATT_CHARACTERISTIC(TEST_LESC_CHRC_UUID,
142+
BT_GATT_CHRC_WRITE | BT_GATT_CHRC_READ,
143+
BT_GATT_PERM_READ_LESC | BT_GATT_PERM_WRITE_LESC,
144+
read_test_chrc, write_test_chrc, NULL),
137145
);
138146

139147
static void test_main(void)

0 commit comments

Comments
 (0)