1
2 // *****************************************************************************
3 //
4 // test rfcomm query tests
5 //
6 // *****************************************************************************
7
8
9 #include <stdint.h>
10 #include <stdio.h>
11 #include <stdlib.h>
12 #include <string.h>
13
14 #include "CppUTest/TestHarness.h"
15 #include "CppUTest/CommandLineTestRunner.h"
16
17 #include "hci.h"
18 #include "ble/att_db.h"
19 #include "ble/att_db_util.h"
20 #include "ble/att_server.h"
21 #include "btstack_util.h"
22 #include "bluetooth.h"
23 #include "btstack_tlv.h"
24 #include "mock_btstack_tlv.h"
25
26 #include "bluetooth_gatt.h"
27
28 static uint8_t battery_level = 100;
29 static const uint8_t uuid128_with_bluetooth_base[] = { 0x00, 0x00, 0xBB, 0xBB, 0x00, 0x00, 0x10, 0x00, 0x80, 0x00, 0x00, 0x80, 0x5F, 0x9B, 0x34, 0xFB};
30 static const uint8_t uuid128_no_bluetooth_base[] = { 0xFB, 0x34, 0x9B, 0x5F, 0x80, 0x00, 0x00, 0x80, 0x00, 0x10, 0x00, 0x00, 0xAA, 0xAA, 0x00, 0x00 };
31
32 extern "C" void l2cap_can_send_fixed_channel_packet_now_set_status(uint8_t status);
33 extern "C" void mock_call_att_server_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size);
34 extern "C" void hci_setup_le_connection(uint16_t con_handle);
35 extern "C" void mock_call_att_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size);
36 extern "C" void mock_l2cap_set_max_mtu(uint16_t mtu);
37 extern "C" void hci_setup_classic_connection(uint16_t con_handle);
38 extern "C" void set_cmac_ready(int ready);
39
40 static uint8_t att_request[255];
att_write_request(uint16_t request_type,uint16_t attribute_handle,uint16_t value_length,const uint8_t * value)41 static uint16_t att_write_request(uint16_t request_type, uint16_t attribute_handle, uint16_t value_length, const uint8_t * value){
42 att_request[0] = request_type;
43 little_endian_store_16(att_request, 1, attribute_handle);
44 (void)memcpy(&att_request[3], value, value_length);
45 return 3 + value_length;
46 }
47
att_read_request(uint16_t request_type,uint16_t attribute_handle)48 static uint16_t att_read_request(uint16_t request_type, uint16_t attribute_handle){
49 att_request[0] = request_type;
50 little_endian_store_16(att_request, 1, attribute_handle);
51 return 3;
52 }
53
att_read_callback(hci_con_handle_t connection_handle,uint16_t att_handle,uint16_t offset,uint8_t * buffer,uint16_t buffer_size)54 static uint16_t att_read_callback(hci_con_handle_t connection_handle, uint16_t att_handle, uint16_t offset, uint8_t * buffer, uint16_t buffer_size){
55 UNUSED(connection_handle);
56 UNUSED(att_handle);
57 UNUSED(offset);
58 UNUSED(buffer);
59 UNUSED(buffer_size);
60
61 return 0;
62 }
63
att_write_callback(hci_con_handle_t connection_handle,uint16_t att_handle,uint16_t transaction_mode,uint16_t offset,uint8_t * buffer,uint16_t buffer_size)64 static int att_write_callback(hci_con_handle_t connection_handle, uint16_t att_handle, uint16_t transaction_mode, uint16_t offset, uint8_t *buffer, uint16_t buffer_size){
65 UNUSED(connection_handle);
66 UNUSED(att_handle);
67 UNUSED(transaction_mode);
68 UNUSED(offset);
69 UNUSED(buffer);
70 UNUSED(buffer_size);
71
72 return 0;
73 }
74
att_client_indication_callback(void * context)75 static void att_client_indication_callback(void * context){
76 }
att_client_notification_callback(void * context)77 static void att_client_notification_callback(void * context){
78 }
att_event_packet_handler(uint8_t packet_type,uint16_t channel,uint8_t * packet,uint16_t size)79 static void att_event_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size) {
80 }
81
82
TEST_GROUP(ATT_SERVER)83 TEST_GROUP(ATT_SERVER){
84 uint16_t att_con_handle;
85 mock_btstack_tlv_t tlv_context;
86 const btstack_tlv_t * tlv_impl;
87 btstack_context_callback_registration_t indication_callback;
88 btstack_context_callback_registration_t notification_callback;
89
90 void setup(void){
91 att_con_handle = 0x01;
92
93 hci_setup_le_connection(att_con_handle);
94
95 tlv_impl = mock_btstack_tlv_init_instance(&tlv_context);
96 btstack_tlv_set_instance(tlv_impl, &tlv_context);
97
98 l2cap_can_send_fixed_channel_packet_now_set_status(1);
99 indication_callback.callback = &att_client_indication_callback;
100 notification_callback.callback = &att_client_notification_callback;
101
102 // init att db util and add a service and characteristic
103 att_db_util_init();
104 // 0x180F
105 att_db_util_add_service_uuid16(ORG_BLUETOOTH_SERVICE_BATTERY_SERVICE);
106 // 0x2A19
107 att_db_util_add_characteristic_uuid16(ORG_BLUETOOTH_CHARACTERISTIC_BATTERY_LEVEL, ATT_PROPERTY_WRITE | ATT_PROPERTY_READ | ATT_PROPERTY_INDICATE, ATT_SECURITY_NONE, ATT_SECURITY_NONE, &battery_level, 1);
108 // 0x2A1B
109 att_db_util_add_characteristic_uuid16(ORG_BLUETOOTH_CHARACTERISTIC_BATTERY_LEVEL_STATE, ATT_PROPERTY_NOTIFY, ATT_SECURITY_NONE, ATT_SECURITY_NONE, &battery_level, 1);
110 // 0x2A1A
111 att_db_util_add_characteristic_uuid16(ORG_BLUETOOTH_CHARACTERISTIC_BATTERY_POWER_STATE, ATT_PROPERTY_READ | ATT_PROPERTY_NOTIFY, ATT_SECURITY_AUTHENTICATED, ATT_SECURITY_AUTHENTICATED, &battery_level, 1);
112 // 0x2A49
113 att_db_util_add_characteristic_uuid16(ORG_BLUETOOTH_CHARACTERISTIC_BLOOD_PRESSURE_FEATURE, ATT_PROPERTY_DYNAMIC | ATT_PROPERTY_READ | ATT_PROPERTY_NOTIFY, ATT_SECURITY_NONE, ATT_SECURITY_NONE, &battery_level, 1);
114 // 0x2A35
115 att_db_util_add_characteristic_uuid16(ORG_BLUETOOTH_CHARACTERISTIC_BLOOD_PRESSURE_MEASUREMENT, ATT_PROPERTY_WRITE | ATT_PROPERTY_DYNAMIC, ATT_SECURITY_AUTHENTICATED, ATT_SECURITY_AUTHENTICATED, &battery_level, 1);
116
117
118 att_db_util_add_characteristic_uuid128(uuid128_no_bluetooth_base, ATT_PROPERTY_WRITE | ATT_PROPERTY_DYNAMIC | ATT_PROPERTY_NOTIFY, ATT_SECURITY_NONE, ATT_SECURITY_NONE, &battery_level, 1);
119 // 0x2A38btstack_tlv_set_instance
120 att_db_util_add_characteristic_uuid16(ORG_BLUETOOTH_CHARACTERISTIC_BODY_SENSOR_LOCATION, ATT_PROPERTY_WRITE | ATT_PROPERTY_DYNAMIC | ATT_PROPERTY_NOTIFY, ATT_SECURITY_NONE, ATT_SECURITY_NONE, &battery_level, 1);
121
122
123 att_db_util_add_characteristic_uuid128(uuid128_with_bluetooth_base, ATT_PROPERTY_WRITE | ATT_PROPERTY_DYNAMIC | ATT_PROPERTY_NOTIFY, ATT_SECURITY_NONE, ATT_SECURITY_NONE, &battery_level, 1);
124 // 0x2AAB
125 att_db_util_add_characteristic_uuid16(ORG_BLUETOOTH_CHARACTERISTIC_CGM_SESSION_RUN_TIME, ATT_PROPERTY_WRITE_WITHOUT_RESPONSE | ATT_PROPERTY_DYNAMIC | ATT_PROPERTY_NOTIFY, ATT_SECURITY_NONE, ATT_SECURITY_NONE, &battery_level, 1);
126 // 0x2A5C
127 att_db_util_add_characteristic_uuid16(ORG_BLUETOOTH_CHARACTERISTIC_CSC_FEATURE, ATT_PROPERTY_AUTHENTICATED_SIGNED_WRITE | ATT_PROPERTY_DYNAMIC, ATT_SECURITY_NONE, ATT_SECURITY_NONE, &battery_level, 1);
128 // setup ATT server
129 att_server_init(att_db_util_get_address(), att_read_callback, att_write_callback);
130 }
131
132 void teardown(void) {
133 mock_btstack_tlv_deinit(&tlv_context);
134 att_server_deinit();
135 hci_deinit();
136 }
137 };
138
TEST(ATT_SERVER,gatt_server_get_value_handle_for_characteristic_with_uuid16)139 TEST(ATT_SERVER, gatt_server_get_value_handle_for_characteristic_with_uuid16){
140 // att_dump_attributes();
141 uint16_t value_handle;
142
143 // start handle > value handle
144 value_handle = gatt_server_get_value_handle_for_characteristic_with_uuid16(0xf000, 0xffff, ORG_BLUETOOTH_CHARACTERISTIC_BATTERY_LEVEL);
145 CHECK_EQUAL(0, value_handle);
146
147 // end handle < value handle
148 value_handle = gatt_server_get_value_handle_for_characteristic_with_uuid16(0, 0x02, ORG_BLUETOOTH_CHARACTERISTIC_BATTERY_LEVEL);
149 CHECK_EQUAL(0, value_handle);
150
151 // search value handle for unknown UUID
152 value_handle = gatt_server_get_value_handle_for_characteristic_with_uuid16(0, 0xffff, 0xffff);
153 CHECK_EQUAL(0, value_handle);
154
155 value_handle = gatt_server_get_value_handle_for_characteristic_with_uuid16(0, 0xffff, 0);
156 CHECK_EQUAL(0, value_handle);
157
158 // search value handle after one with uuid128_no_bluetooth_base
159 value_handle = gatt_server_get_value_handle_for_characteristic_with_uuid16(0, 0xffff, ORG_BLUETOOTH_CHARACTERISTIC_BODY_SENSOR_LOCATION);
160 CHECK_EQUAL(0x0014, value_handle);
161
162 // search value handle after one with uuid128_with_bluetooth_base
163 value_handle = gatt_server_get_value_handle_for_characteristic_with_uuid16(0, 0xffff, ORG_BLUETOOTH_CHARACTERISTIC_CGM_SESSION_RUN_TIME);
164 CHECK_EQUAL(0x001a, value_handle);
165
166 // search value handle registered with bluetooth_base
167 value_handle = gatt_server_get_value_handle_for_characteristic_with_uuid16(0, 0xffff, 0xbbbb);
168 CHECK_EQUAL(0x0017, value_handle);
169
170 // correct read
171 value_handle = gatt_server_get_value_handle_for_characteristic_with_uuid16(0, 0xffff, ORG_BLUETOOTH_CHARACTERISTIC_BATTERY_LEVEL);
172 CHECK_EQUAL(0x03, value_handle);
173 }
174
175
TEST(ATT_SERVER,att_server_indicate)176 TEST(ATT_SERVER, att_server_indicate){
177 static uint8_t value[] = {0x55};
178 uint16_t value_handle = gatt_server_get_value_handle_for_characteristic_with_uuid16(0, 0xffff, ORG_BLUETOOTH_CHARACTERISTIC_BATTERY_LEVEL);
179 uint8_t status;
180
181 // invalid connection handle
182 status = att_server_indicate(0x50, value_handle, &value[0], 0);
183 CHECK_EQUAL(ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER, status);
184
185 // L2CAP cannot send
186 l2cap_can_send_fixed_channel_packet_now_set_status(0);
187 status = att_server_indicate(att_con_handle, value_handle, &value[0], 0);
188 CHECK_EQUAL(BTSTACK_ACL_BUFFERS_FULL, status);
189 l2cap_can_send_fixed_channel_packet_now_set_status(1);
190
191 // correct command
192 status = att_server_indicate(att_con_handle, value_handle, &value[0], 0);
193 CHECK_EQUAL(ERROR_CODE_SUCCESS, status);
194
195 // already in progress
196 status = att_server_indicate(att_con_handle, value_handle, &value[0], 0);
197 CHECK_EQUAL(ATT_HANDLE_VALUE_INDICATION_IN_PROGRESS, status);
198 }
199
TEST(ATT_SERVER,att_server_notify)200 TEST(ATT_SERVER, att_server_notify){
201 static uint8_t value[] = {0x55};
202 uint16_t value_handle = gatt_server_get_value_handle_for_characteristic_with_uuid16(0, 0xffff, ORG_BLUETOOTH_CHARACTERISTIC_BATTERY_LEVEL);
203 uint8_t status;
204
205 // invalid connection handle
206 status = att_server_notify(0x50, value_handle, &value[0], 0);
207 CHECK_EQUAL(ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER, status);
208
209 // L2CAP cannot send
210 l2cap_can_send_fixed_channel_packet_now_set_status(0);
211 status = att_server_notify(att_con_handle, value_handle, &value[0], 0);
212 CHECK_EQUAL(BTSTACK_ACL_BUFFERS_FULL, status);
213 l2cap_can_send_fixed_channel_packet_now_set_status(1);
214
215 // correct command
216 status = att_server_notify(att_con_handle, value_handle, &value[0], 0);
217 CHECK_EQUAL(ERROR_CODE_SUCCESS, status);
218 }
219
TEST(ATT_SERVER,att_server_get_mtu)220 TEST(ATT_SERVER, att_server_get_mtu){
221 // invalid connection handle
222 uint8_t mtu = att_server_get_mtu(0x50);
223 CHECK_EQUAL(0, mtu);
224
225 mtu = att_server_get_mtu(att_con_handle);
226 CHECK_EQUAL(23, mtu);
227 }
228
TEST(ATT_SERVER,att_server_request_can_send_now_event)229 TEST(ATT_SERVER, att_server_request_can_send_now_event){
230 att_server_request_can_send_now_event(att_con_handle);
231 }
232
TEST(ATT_SERVER,att_server_can_send_packet_now)233 TEST(ATT_SERVER, att_server_can_send_packet_now){
234 int status = att_server_can_send_packet_now(att_con_handle);
235 CHECK_EQUAL(1, status);
236
237 status = att_server_can_send_packet_now(0x50);
238 CHECK_EQUAL(0, status);
239 }
240
TEST(ATT_SERVER,att_server_request_to_send_indication)241 TEST(ATT_SERVER, att_server_request_to_send_indication){
242 int status = att_server_request_to_send_indication(&indication_callback, 0x55);
243 CHECK_EQUAL(ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER, status);
244
245 l2cap_can_send_fixed_channel_packet_now_set_status(0);
246
247 status = att_server_request_to_send_indication(&indication_callback, att_con_handle);
248 CHECK_EQUAL(ERROR_CODE_SUCCESS, status);
249
250 status = att_server_request_to_send_indication(&indication_callback, att_con_handle);
251 CHECK_EQUAL(ERROR_CODE_COMMAND_DISALLOWED, status);
252 }
253
TEST(ATT_SERVER,att_server_request_to_send_notification)254 TEST(ATT_SERVER, att_server_request_to_send_notification){
255 int status = att_server_request_to_send_notification(¬ification_callback, 0x55);
256 CHECK_EQUAL(ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER, status);
257
258 l2cap_can_send_fixed_channel_packet_now_set_status(0);
259
260 status = att_server_request_to_send_notification(¬ification_callback, att_con_handle);
261 CHECK_EQUAL(ERROR_CODE_SUCCESS, status);
262
263 status = att_server_request_to_send_notification(¬ification_callback, att_con_handle);
264 CHECK_EQUAL(ERROR_CODE_COMMAND_DISALLOWED, status);
265 }
266
TEST(ATT_SERVER,att_server_register_can_send_now_callback)267 TEST(ATT_SERVER, att_server_register_can_send_now_callback){
268 int status = att_server_register_can_send_now_callback(¬ification_callback, 0x55);
269 CHECK_EQUAL(ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER, status);
270
271 att_server_register_packet_handler(&att_event_packet_handler);
272 }
273
TEST(ATT_SERVER,att_packet_handler_ATT_DATA_PACKET)274 TEST(ATT_SERVER, att_packet_handler_ATT_DATA_PACKET){
275 uint16_t value_handle = gatt_server_get_client_configuration_handle_for_characteristic_with_uuid16(0, 0xffff, ORG_BLUETOOTH_CHARACTERISTIC_BATTERY_LEVEL);
276 uint8_t buffer[] = {1, 0};
277
278 uint16_t att_request_len = att_write_request(ATT_WRITE_REQUEST, value_handle, sizeof(buffer), buffer);
279 mock_call_att_server_packet_handler(ATT_DATA_PACKET, att_con_handle, &att_request[0], att_request_len);
280 }
281
TEST(ATT_SERVER,att_packet_handler_ATT_DATA_PACKET1)282 TEST(ATT_SERVER, att_packet_handler_ATT_DATA_PACKET1){
283 uint16_t value_handle;
284 uint16_t att_request_len;
285
286 value_handle = gatt_server_get_client_configuration_handle_for_characteristic_with_uuid16(0, 0xffff, ORG_BLUETOOTH_CHARACTERISTIC_BATTERY_LEVEL);
287 att_request_len = att_read_request(ATT_READ_REQUEST, value_handle);
288 mock_call_att_server_packet_handler(ATT_DATA_PACKET, att_con_handle, &att_request[0], att_request_len);
289 }
290
TEST(ATT_SERVER,att_packet_handler_invalid_opcode)291 TEST(ATT_SERVER, att_packet_handler_invalid_opcode){
292 uint16_t value_handle = gatt_server_get_client_configuration_handle_for_characteristic_with_uuid16(0, 0xffff, ORG_BLUETOOTH_CHARACTERISTIC_BATTERY_POWER_STATE);
293 uint8_t buffer[] = {1, 0};
294
295 uint16_t att_request_len = att_write_request(0xFF, value_handle, sizeof(buffer), buffer);
296 mock_call_att_server_packet_handler(ATT_DATA_PACKET, att_con_handle, &att_request[0], att_request_len);
297 }
298
TEST(ATT_SERVER,att_packet_handler_ATT_HANDLE_VALUE_CONFIRMATION)299 TEST(ATT_SERVER, att_packet_handler_ATT_HANDLE_VALUE_CONFIRMATION){
300 hci_setup_le_connection(att_con_handle);
301 static uint8_t value[] = {0x55};
302 uint16_t value_handle = gatt_server_get_value_handle_for_characteristic_with_uuid16(0, 0xffff, ORG_BLUETOOTH_CHARACTERISTIC_BATTERY_LEVEL);
303 l2cap_can_send_fixed_channel_packet_now_set_status(1);
304 // correct command
305 uint8_t status = att_server_indicate(att_con_handle, value_handle, &value[0], 0);
306 CHECK_EQUAL(ERROR_CODE_SUCCESS, status);
307
308 static uint8_t buffer[0];
309 uint16_t att_request_len = att_write_request(ATT_HANDLE_VALUE_CONFIRMATION, value_handle, 0, buffer);
310 mock_call_att_server_packet_handler(ATT_DATA_PACKET, att_con_handle, &att_request[0], att_request_len);
311 }
312
TEST(ATT_SERVER,att_packet_handler_ATT_WRITE_COMMAND)313 TEST(ATT_SERVER, att_packet_handler_ATT_WRITE_COMMAND){
314 uint16_t value_handle = gatt_server_get_value_handle_for_characteristic_with_uuid16(0, 0xffff, ORG_BLUETOOTH_CHARACTERISTIC_BATTERY_LEVEL);
315 static uint8_t buffer[] = {0x55};
316 uint16_t att_request_len = att_write_request(ATT_WRITE_COMMAND, value_handle, sizeof(buffer), buffer);
317 mock_call_att_server_packet_handler(ATT_DATA_PACKET, att_con_handle, &att_request[0], att_request_len);
318 }
319
TEST(ATT_SERVER,att_packet_handler_ATT_WRITE_COMMAND_wrong_buffer_size)320 TEST(ATT_SERVER, att_packet_handler_ATT_WRITE_COMMAND_wrong_buffer_size){
321 uint16_t value_handle = gatt_server_get_value_handle_for_characteristic_with_uuid16(0, 0xffff, ORG_BLUETOOTH_CHARACTERISTIC_BATTERY_LEVEL);
322 static uint8_t buffer[] = {0x55};
323 att_write_request(ATT_SIGNED_WRITE_COMMAND, value_handle, sizeof(buffer), buffer);
324 mock_call_att_server_packet_handler(ATT_DATA_PACKET, att_con_handle, &att_request[0], ATT_REQUEST_BUFFER_SIZE + 100);
325 }
326
TEST(ATT_SERVER,att_packet_handler_ATT_WRITE_COMMAND_signed_write_confirmation)327 TEST(ATT_SERVER, att_packet_handler_ATT_WRITE_COMMAND_signed_write_confirmation){
328 hci_setup_le_connection(att_con_handle);
329
330 uint8_t signed_write_value[] = {0x12};
331 uint8_t hash[] = {0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88};
332 uint16_t signed_write_characteristic_handle = gatt_server_get_value_handle_for_characteristic_with_uuid16(0, 0xffff, ORG_BLUETOOTH_CHARACTERISTIC_CSC_FEATURE);
333
334 uint8_t att_request[20];
335 int value_length = sizeof(signed_write_value);
336 att_request[0] = ATT_SIGNED_WRITE_COMMAND;
337 little_endian_store_16(att_request, 1, signed_write_characteristic_handle);
338 memcpy(&att_request[3], signed_write_value, value_length);
339 little_endian_store_32(att_request, 3 + value_length, 0);
340 reverse_64(hash, &att_request[3 + value_length + 4]);
341
342 // wrong size
343 mock_call_att_server_packet_handler(ATT_DATA_PACKET, att_con_handle, &att_request[0], value_length + 12u);
344
345 set_cmac_ready(0);
346 mock_call_att_server_packet_handler(ATT_DATA_PACKET, att_con_handle, &att_request[0], value_length + 12u + 3u);
347
348 set_cmac_ready(1);
349 mock_call_att_server_packet_handler(ATT_DATA_PACKET, att_con_handle, &att_request[0], value_length + 12u + 3u);
350
351 // set ir_lookup_active = 1
352 uint8_t buffer[20];
353 buffer[0] = SM_EVENT_IDENTITY_RESOLVING_STARTED;
354 buffer[1] = 9;
355 little_endian_store_16(buffer, 2, att_con_handle);
356 mock_call_att_packet_handler(HCI_EVENT_PACKET, 0, &buffer[0], 11);
357
358 mock_call_att_server_packet_handler(ATT_DATA_PACKET, att_con_handle, &att_request[0], value_length + 12u + 3u);
359
360 buffer[0] = SM_EVENT_IDENTITY_RESOLVING_SUCCEEDED;
361 buffer[1] = 18; // H1B1B2
362 little_endian_store_16(buffer, 18, 0);
363 mock_call_att_packet_handler(HCI_EVENT_PACKET, 0, &buffer[0], 11);
364 }
365
366
TEST(ATT_SERVER,att_packet_handler_ATT_UNKNOWN_PACKET)367 TEST(ATT_SERVER, att_packet_handler_ATT_UNKNOWN_PACKET){
368 uint16_t value_handle = gatt_server_get_client_configuration_handle_for_characteristic_with_uuid16(0, 0xffff, ORG_BLUETOOTH_CHARACTERISTIC_BATTERY_LEVEL);
369 uint8_t buffer[] = {1, 0};
370
371 uint16_t att_request_len = att_write_request(ATT_WRITE_REQUEST, value_handle, sizeof(buffer), buffer);
372 mock_call_att_server_packet_handler(0xFF, att_con_handle, &att_request[0], att_request_len);
373 }
374
TEST(ATT_SERVER,att_packet_handler_HCI_EVENT_PACKET_L2CAP_EVENT_CAN_SEND_NOW)375 TEST(ATT_SERVER, att_packet_handler_HCI_EVENT_PACKET_L2CAP_EVENT_CAN_SEND_NOW){
376 uint16_t value_handle = gatt_server_get_client_configuration_handle_for_characteristic_with_uuid16(0, 0xffff, ORG_BLUETOOTH_CHARACTERISTIC_BATTERY_LEVEL);
377 // local cid
378 uint8_t buffer[] = {1, 0};
379
380 uint16_t att_request_len = att_write_request(L2CAP_EVENT_CAN_SEND_NOW, value_handle, sizeof(buffer), buffer);
381 mock_call_att_server_packet_handler(HCI_EVENT_PACKET, att_con_handle, &att_request[0], att_request_len);
382 }
383
TEST(ATT_SERVER,att_packet_handler_HCI_EVENT_PACKET_ATT_EVENT_MTU_EXCHANGE_COMPLETE)384 TEST(ATT_SERVER, att_packet_handler_HCI_EVENT_PACKET_ATT_EVENT_MTU_EXCHANGE_COMPLETE){
385 uint16_t value_handle = gatt_server_get_client_configuration_handle_for_characteristic_with_uuid16(0, 0xffff, ORG_BLUETOOTH_CHARACTERISTIC_BATTERY_LEVEL);
386 uint8_t buffer[4];
387
388 // invalid con handle
389 memset(buffer, 0, sizeof(buffer));
390 little_endian_store_16(buffer, 2, 256);
391
392 uint16_t att_request_len = att_write_request(ATT_EVENT_MTU_EXCHANGE_COMPLETE, value_handle, sizeof(buffer), buffer);
393 mock_call_att_server_packet_handler(HCI_EVENT_PACKET, 0, &att_request[0], att_request_len);
394
395 little_endian_store_16(buffer, 0, att_con_handle);
396 att_request_len = att_write_request(ATT_EVENT_MTU_EXCHANGE_COMPLETE, value_handle, sizeof(buffer), buffer);
397 mock_call_att_server_packet_handler(HCI_EVENT_PACKET, att_con_handle, &att_request[0], att_request_len);
398 }
399
TEST(ATT_SERVER,att_packet_handler_HCI_EVENT_PACKET_ATT_EVENT_UNKNOWN)400 TEST(ATT_SERVER, att_packet_handler_HCI_EVENT_PACKET_ATT_EVENT_UNKNOWN){
401 uint16_t value_handle = gatt_server_get_client_configuration_handle_for_characteristic_with_uuid16(0, 0xffff, ORG_BLUETOOTH_CHARACTERISTIC_BATTERY_LEVEL);
402 uint8_t buffer[] = {1, 0};
403
404 // unknown subevent
405 uint16_t att_request_len = att_write_request(0x7bu, value_handle, sizeof(buffer), buffer);
406 mock_call_att_server_packet_handler(HCI_EVENT_PACKET, att_con_handle, &att_request[0], att_request_len);
407 }
408
TEST(ATT_SERVER,connection_complete_event)409 TEST(ATT_SERVER, connection_complete_event){
410 uint8_t buffer[21];
411 buffer[0] = HCI_EVENT_LE_META;
412 buffer[1] = 19;
413 buffer[2] = HCI_SUBEVENT_LE_CONNECTION_COMPLETE;
414
415 // call with wrong con_handle
416 little_endian_store_16(buffer,4,HCI_CON_HANDLE_INVALID);
417 mock_call_att_packet_handler(HCI_EVENT_PACKET, 0, &buffer[0], sizeof(buffer));
418
419 // call with correct con_handle
420 little_endian_store_16(buffer,4,att_con_handle);
421 mock_call_att_packet_handler(HCI_EVENT_PACKET, 0, &buffer[0], sizeof(buffer));
422
423 // use max MTU > ATT_REQUEST_BUFFER_SIZE
424 mock_l2cap_set_max_mtu(ATT_REQUEST_BUFFER_SIZE + 10);
425 mock_call_att_packet_handler(HCI_EVENT_PACKET, 0, &buffer[0], sizeof(buffer));
426
427 // wrong subevent
428 buffer[2] = 0xFF;
429 mock_call_att_packet_handler(HCI_EVENT_PACKET, 0, &buffer[0], sizeof(buffer));
430 }
431
TEST(ATT_SERVER,unknown_packet_type)432 TEST(ATT_SERVER, unknown_packet_type){
433 uint8_t buffer[21];
434 buffer[0] = HCI_EVENT_LE_META;
435 buffer[1] = 1;
436 buffer[2] = HCI_SUBEVENT_LE_CONNECTION_COMPLETE;
437
438 mock_call_att_packet_handler(0xFF, 0, &buffer[0], sizeof(buffer));
439 }
440
441
TEST(ATT_SERVER,connection_disconnect_complete_event)442 TEST(ATT_SERVER, connection_disconnect_complete_event) {
443 uint8_t buffer[6];
444 buffer[0] = HCI_EVENT_DISCONNECTION_COMPLETE;
445 buffer[1] = 19;
446 buffer[2] = 0;
447
448 // call with wrong con_handle
449 hci_setup_le_connection(att_con_handle);
450 little_endian_store_16(buffer, 3, HCI_CON_HANDLE_INVALID);
451 mock_call_att_packet_handler(HCI_EVENT_PACKET, 0, &buffer[0], sizeof(buffer));
452
453 // call with correct con_handle, classic connection
454 hci_setup_classic_connection(att_con_handle);
455 little_endian_store_16(buffer, 3, att_con_handle);
456 mock_call_att_packet_handler(HCI_EVENT_PACKET, 0, &buffer[0], sizeof(buffer));
457
458 // call with correct con_handle, le connection
459 hci_setup_le_connection(att_con_handle);
460 little_endian_store_16(buffer, 3, att_con_handle);
461 mock_call_att_packet_handler(HCI_EVENT_PACKET, 0, &buffer[0], sizeof(buffer));
462
463
464 hci_setup_le_connection(att_con_handle);
465 static uint8_t value[] = {0x55};
466 uint16_t value_handle = gatt_server_get_value_handle_for_characteristic_with_uuid16(0, 0xffff, ORG_BLUETOOTH_CHARACTERISTIC_BATTERY_LEVEL);
467 l2cap_can_send_fixed_channel_packet_now_set_status(1);
468 // correct command
469 uint8_t status = att_server_indicate(att_con_handle, value_handle, &value[0], 0);
470 CHECK_EQUAL(ERROR_CODE_SUCCESS, status);
471
472 mock_call_att_packet_handler(HCI_EVENT_PACKET, 0, &buffer[0], sizeof(buffer));
473 }
474
test_hci_event_encryption_events(hci_con_handle_t con_handle,uint8_t con_handle_index,uint8_t * buffer,uint16_t buffer_size)475 static void test_hci_event_encryption_events(hci_con_handle_t con_handle, uint8_t con_handle_index, uint8_t * buffer, uint16_t buffer_size){
476 // call with wrong con_handle
477 hci_setup_le_connection(con_handle);
478 little_endian_store_16(buffer, con_handle_index, HCI_CON_HANDLE_INVALID);
479 mock_call_att_packet_handler(HCI_EVENT_PACKET, 0, &buffer[0], buffer_size);
480
481 // call with correct con_handle, classic connection
482 hci_setup_classic_connection(con_handle);
483 little_endian_store_16(buffer, con_handle_index, con_handle);
484 mock_call_att_packet_handler(HCI_EVENT_PACKET, 0, &buffer[0], buffer_size);
485
486 // call with correct con_handle, le connection
487 hci_setup_le_connection(con_handle);
488 little_endian_store_16(buffer, con_handle_index, con_handle);
489 mock_call_att_packet_handler(HCI_EVENT_PACKET, 0, &buffer[0], buffer_size);
490 }
491
TEST(ATT_SERVER,att_server_multiple_notify)492 TEST(ATT_SERVER, att_server_multiple_notify) {
493
494 const uint16_t attribute_handles[] = {gatt_server_get_value_handle_for_characteristic_with_uuid16(0, 0xffff, ORG_BLUETOOTH_CHARACTERISTIC_BATTERY_LEVEL)};
495 const uint16_t value_lens[] = { 1};
496 static uint8_t battery_value[] = {0x55};
497 const uint8_t * value_data[] = {battery_value };
498 uint8_t num_attributes = 1;
499
500 hci_setup_le_connection(att_con_handle);
501 // correct command
502 uint8_t status = att_server_multiple_notify(att_con_handle, 1, attribute_handles, value_data, value_lens);
503 CHECK_EQUAL(ERROR_CODE_SUCCESS, status);
504
505 // invalid con handle
506 status = att_server_multiple_notify(HCI_CON_HANDLE_INVALID, 1, attribute_handles, value_data, value_lens);
507 CHECK_EQUAL(ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER, status);
508 }
509
TEST(ATT_SERVER,hci_event_encryption_key_refresh_complete_event)510 TEST(ATT_SERVER, hci_event_encryption_key_refresh_complete_event) {
511 uint8_t buffer[5];
512 buffer[0] = HCI_EVENT_ENCRYPTION_KEY_REFRESH_COMPLETE;
513 buffer[1] = 3;
514 buffer[2] = 0;
515 // con_handle (2)
516 test_hci_event_encryption_events(att_con_handle, 3, buffer, sizeof(buffer));
517 }
518
TEST(ATT_SERVER,hci_event_encryption_change_v2_event)519 TEST(ATT_SERVER, hci_event_encryption_change_v2_event) {
520 uint8_t buffer[7];
521 buffer[0] = HCI_EVENT_ENCRYPTION_CHANGE_V2;
522 buffer[1] = 5;
523 buffer[2] = 0;
524 // con_handle (2)
525 // encryption_key_size
526 buffer[6] = 1;
527 // encryption_enabled (1)
528 buffer[5] = 0;
529 test_hci_event_encryption_events(att_con_handle, 3, buffer, sizeof(buffer));
530
531 // encryption_enabled (1)
532 buffer[5] = 1;
533 test_hci_event_encryption_events(att_con_handle, 3, buffer, sizeof(buffer));
534 }
535
TEST(ATT_SERVER,hci_event_encryption_change_event)536 TEST(ATT_SERVER, hci_event_encryption_change_event) {
537 uint8_t buffer[6];
538 buffer[0] = HCI_EVENT_ENCRYPTION_CHANGE;
539 buffer[1] = 4;
540 buffer[2] = 0;
541 // con_handle (2)att_packet_handler_ATT_UNKNOWN_PACKET
542
543 // encryption_enabled (1)
544 buffer[5] = 0;
545 test_hci_event_encryption_events(att_con_handle, 3, buffer, sizeof(buffer));
546
547 // encryption_enabled (1)
548 buffer[5] = 1;
549 test_hci_event_encryption_events(att_con_handle, 3, buffer, sizeof(buffer));
550 }
551
TEST(ATT_SERVER,sm_event_identity_resolving_started_event)552 TEST(ATT_SERVER, sm_event_identity_resolving_started_event) {
553 uint8_t buffer[11];
554 buffer[0] = SM_EVENT_IDENTITY_RESOLVING_STARTED;
555 buffer[1] = 9;
556
557 test_hci_event_encryption_events(att_con_handle, 2, buffer, sizeof(buffer));
558 }
559
TEST(ATT_SERVER,sm_event_identity_resolving_succeeded_event)560 TEST(ATT_SERVER, sm_event_identity_resolving_succeeded_event) {
561 uint8_t buffer[20];
562 buffer[0] = SM_EVENT_IDENTITY_RESOLVING_SUCCEEDED;
563 buffer[1] = 18; // H1B1B2
564 little_endian_store_16(buffer, 18, 0);
565 test_hci_event_encryption_events(att_con_handle, 2, buffer, sizeof(buffer));
566 }
567
TEST(ATT_SERVER,sm_event_identity_resolving_failed_event)568 TEST(ATT_SERVER, sm_event_identity_resolving_failed_event) {
569 uint8_t buffer[11];
570 buffer[0] = SM_EVENT_IDENTITY_RESOLVING_FAILED;
571 buffer[1] = 9;
572
573 test_hci_event_encryption_events(att_con_handle, 2, buffer, sizeof(buffer));
574 }
575
576 //
577
TEST(ATT_SERVER,sm_event_just_works_request_event)578 TEST(ATT_SERVER, sm_event_just_works_request_event) {
579 uint8_t buffer[11];
580 buffer[0] = SM_EVENT_JUST_WORKS_REQUEST;
581 buffer[1] = 9;
582
583 test_hci_event_encryption_events(att_con_handle, 2, buffer, sizeof(buffer));
584 }
585
TEST(ATT_SERVER,sm_event_passkey_display_number_event)586 TEST(ATT_SERVER, sm_event_passkey_display_number_event) {
587 uint8_t buffer[11];
588 buffer[0] = SM_EVENT_PASSKEY_DISPLAY_NUMBER;
589 buffer[1] = 9;
590
591 test_hci_event_encryption_events(att_con_handle, 2, buffer, sizeof(buffer));
592 }
593
TEST(ATT_SERVER,sm_event_passkey_input_number_event)594 TEST(ATT_SERVER, sm_event_passkey_input_number_event) {
595 uint8_t buffer[11];
596 buffer[0] = SM_EVENT_PASSKEY_INPUT_NUMBER;
597 buffer[1] = 9;
598
599 test_hci_event_encryption_events(att_con_handle, 2, buffer, sizeof(buffer));
600 }
601
TEST(ATT_SERVER,sm_event_numeric_comparison_request_event)602 TEST(ATT_SERVER, sm_event_numeric_comparison_request_event) {
603 uint8_t buffer[11];
604 buffer[0] = SM_EVENT_NUMERIC_COMPARISON_REQUEST;
605 buffer[1] = 9;
606
607 test_hci_event_encryption_events(att_con_handle, 2, buffer, sizeof(buffer));
608 }
609
TEST(ATT_SERVER,sm_event_identity_created_event)610 TEST(ATT_SERVER, sm_event_identity_created_event) {
611 uint8_t buffer[20];
612 buffer[0] = SM_EVENT_IDENTITY_CREATED;
613 buffer[1] = 9;
614
615 test_hci_event_encryption_events(att_con_handle, 2, buffer, sizeof(buffer));
616 }
617
TEST(ATT_SERVER,sm_event_pairing_complete_event)618 TEST(ATT_SERVER, sm_event_pairing_complete_event) {
619 uint8_t buffer[11];
620 buffer[0] = SM_EVENT_PAIRING_COMPLETE;
621 buffer[1] = 9;
622
623 test_hci_event_encryption_events(att_con_handle, 2, buffer, sizeof(buffer));
624 }
625
TEST(ATT_SERVER,sm_event_authorization_result_event)626 TEST(ATT_SERVER, sm_event_authorization_result_event) {
627 uint8_t buffer[12];
628 buffer[0] = SM_EVENT_AUTHORIZATION_RESULT;
629 buffer[1] = 9;
630
631 test_hci_event_encryption_events(att_con_handle, 2, buffer, sizeof(buffer));
632 }
633
TEST(ATT_SERVER,unknown_event)634 TEST(ATT_SERVER, unknown_event) {
635 uint8_t buffer[11];
636 buffer[0] = 0xFF;
637 buffer[1] = 9;
638
639 test_hci_event_encryption_events(att_con_handle, 2, buffer, sizeof(buffer));
640 }
641
TEST(ATT_SERVER,att_server_register_service_handler)642 TEST(ATT_SERVER, att_server_register_service_handler) {
643 att_service_handler_t test_service;
644 test_service.read_callback = att_read_callback;
645 test_service.write_callback = att_write_callback;
646
647 uint16_t start_handle = 0;
648 uint16_t end_handle = 0xffff;
649 int service_found = gatt_server_get_handle_range_for_service_with_uuid16(ORG_BLUETOOTH_SERVICE_BATTERY_SERVICE, &start_handle, &end_handle);
650 CHECK_EQUAL(service_found, 1);
651
652 test_service.start_handle = start_handle;
653 test_service.end_handle = end_handle;
654 att_server_register_service_handler(&test_service);
655
656 test_service.start_handle = start_handle;
657 test_service.end_handle = 0;
658 att_server_register_service_handler(&test_service);
659
660 test_service.start_handle = 0;
661 test_service.end_handle = end_handle;
662 att_server_register_service_handler(&test_service);
663
664 test_service.start_handle = 0;
665 test_service.end_handle = 0;
666 att_server_register_service_handler(&test_service);
667 }
668
main(int argc,const char * argv[])669 int main (int argc, const char * argv[]){
670 return CommandLineTestRunner::RunAllTests(argc, argv);
671 }
672