1 /*
2 * Copyright (C) 2014 BlueKitchen GmbH
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. Neither the name of the copyright holders nor the names of
14 * contributors may be used to endorse or promote products derived
15 * from this software without specific prior written permission.
16 * 4. Any redistribution, use, or modification is done solely for
17 * personal benefit and not for any commercial purpose or for
18 * monetary gain.
19 *
20 * THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS
21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MATTHIAS
24 * RINGWALD OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
25 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
26 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
27 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
28 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
29 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
30 * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 *
33 * Please inquire about commercial licensing options at
34 * [email protected]
35 *
36 */
37
38
39 #include <stdint.h>
40 #include <stdio.h>
41 #include <stdlib.h>
42 #include <string.h>
43
44 #include "CppUTest/TestHarness.h"
45 #include "CppUTest/CommandLineTestRunner.h"
46
47 #include "hci.h"
48 #include "ble/att_db.h"
49 #include "ble/att_db_util.h"
50 #include "btstack_util.h"
51 #include "bluetooth.h"
52
53 #include "btstack_crypto.h"
54
55 #include "att_db_util_test.h"
56
57 static btstack_crypto_aes128_cmac_t cmac_context;
58 static uint8_t cmac_calculated[16];
59
60 // 0000FF10-0000-1000-8000-00805F9B34FB
61 uint8_t counter_service_uuid[] = { 0x00, 0x00, 0xFF, 0x10, 0x00, 0x00, 0x10, 0x00, 0x80, 0x00, 0x00, 0x80, 0x5F, 0x9B, 0x34, 0xFB};
62 // 0000FF11-0000-1000-8000-00805F9B34FB
63 uint8_t counter_characteristic_uuid[] = { 0x00, 0x00, 0xFF, 0x11, 0x00, 0x00, 0x10, 0x00, 0x80, 0x00, 0x00, 0x80, 0x5F, 0x9B, 0x34, 0xFB};
64
65 // gatt database hash test message
66 static const uint8_t gatt_database_hash_test_message[] = {
67 0x01, 0x00, 0x00, 0x28, 0x00, 0x18, 0x02, 0x00, 0x03, 0x28, 0x0A, 0x03, 0x00, 0x00, 0x2A, 0x04,
68 0x00, 0x03, 0x28, 0x02, 0x05, 0x00, 0x01, 0x2A, 0x06, 0x00, 0x00, 0x28, 0x01, 0x18, 0x07, 0x00,
69 0x03, 0x28, 0x20, 0x08, 0x00, 0x05, 0x2A, 0x09, 0x00, 0x02, 0x29, 0x0A, 0x00, 0x03, 0x28, 0x0A,
70 0x0B, 0x00, 0x29, 0x2B, 0x0C, 0x00, 0x03, 0x28, 0x02, 0x0D, 0x00, 0x2A, 0x2B, 0x0E, 0x00, 0x00,
71 0x28, 0x08, 0x18, 0x0F, 0x00, 0x02, 0x28, 0x14, 0x00, 0x16, 0x00, 0x0F, 0x18, 0x10, 0x00, 0x03,
72 0x28, 0xA2, 0x11, 0x00, 0x18, 0x2A, 0x12, 0x00, 0x02, 0x29, 0x13, 0x00, 0x00, 0x29, 0x00, 0x00,
73 0x14, 0x00, 0x01, 0x28, 0x0F, 0x18, 0x15, 0x00, 0x03, 0x28, 0x02, 0x16, 0x00, 0x19, 0x2A
74 };
75
76 static const uint8_t gatt_database_hash_expected[] = {
77 0xF1, 0xCA, 0x2D, 0x48, 0xEC, 0xF5, 0x8B, 0xAC, 0x8A, 0x88, 0x30, 0xBB, 0xB9, 0xFB, 0xA9, 0x90
78 };
79
gatt_hash_calculated(void * arg)80 static void gatt_hash_calculated(void * arg){
81 UNUSED(arg);
82 }
83
CHECK_EQUAL_ARRAY(const uint8_t * expected,uint8_t * actual,int size)84 void CHECK_EQUAL_ARRAY(const uint8_t * expected, uint8_t * actual, int size){
85 for (int i=0; i<size; i++){
86 // printf("%03u: %02x - %02x\n", i, expected[i], actual[i]);
87 BYTES_EQUAL(expected[i], actual[i]);
88 }
89 }
90
91 // mock
92 extern "C" {
93
att_set_db(uint8_t const * db)94 void att_set_db(uint8_t const * db){
95 }
hci_add_event_handler(btstack_packet_callback_registration_t * callback_handler)96 void hci_add_event_handler(btstack_packet_callback_registration_t * callback_handler){
97 }
hci_can_send_command_packet_now(void)98 bool hci_can_send_command_packet_now(void){
99 return true;
100 }
hci_get_state(void)101 HCI_STATE hci_get_state(void){
102 return HCI_STATE_WORKING;
103 }
hci_halting_defer(void)104 void hci_halting_defer(void){
105 }
hci_send_cmd(const hci_cmd_t * cmd,...)106 uint8_t hci_send_cmd(const hci_cmd_t *cmd, ...){
107 printf("hci_send_cmd opcode %04x\n", cmd->opcode);
108 return ERROR_CODE_SUCCESS;
109 }
110 }
111
TEST_GROUP(AttDbUtil)112 TEST_GROUP(AttDbUtil){
113 void setup(void){
114 att_db_util_init();
115 }
116 };
117
TEST(AttDbUtil,LeCounterDb)118 TEST(AttDbUtil, LeCounterDb){
119 att_db_util_add_service_uuid16(GAP_SERVICE_UUID);
120 att_db_util_add_characteristic_uuid16(GAP_DEVICE_NAME_UUID, ATT_PROPERTY_READ, ATT_SECURITY_NONE, ATT_SECURITY_NONE, (uint8_t*)"SPP+LE Counter", 14);
121
122 att_db_util_add_service_uuid16(0x1801);
123 att_db_util_add_characteristic_uuid16(GAP_SERVICE_CHANGED, ATT_PROPERTY_READ, ATT_SECURITY_NONE, ATT_SECURITY_NONE, NULL, 0);
124
125 att_db_util_add_service_uuid128(counter_service_uuid);
126 att_db_util_add_characteristic_uuid128(counter_characteristic_uuid, ATT_PROPERTY_READ | ATT_PROPERTY_NOTIFY | ATT_PROPERTY_DYNAMIC, ATT_SECURITY_NONE, ATT_SECURITY_NONE, NULL, 0);
127
128 uint8_t * addr = att_db_util_get_address();
129 uint16_t size = att_db_util_get_size();
130
131 printf("LE Counter DB\n");
132 printf_hexdump(addr, size);
133
134 CHECK_EQUAL((uint16_t)sizeof(profile_data), size);
135 CHECK_EQUAL_ARRAY(profile_data, addr, size);
136 }
137
TEST(AttDbUtil,GattHash)138 TEST(AttDbUtil, GattHash){
139 const uint8_t appearance[] = {0};
140 const uint8_t service_changed[] = {0} ;
141 const uint8_t supported_features[] = {0} ;
142 const uint8_t extended_properties[] = {0,0} ;
143 const uint8_t battery_level[] = { 100 } ;
144 att_db_util_add_service_uuid16(GAP_SERVICE_UUID);
145 att_db_util_add_characteristic_uuid16(GAP_DEVICE_NAME_UUID, ATT_PROPERTY_READ | ATT_PROPERTY_WRITE, ATT_SECURITY_NONE, ATT_SECURITY_NONE, (uint8_t*)"HASH", 4);
146 att_db_util_add_characteristic_uuid16(GAP_APPEARANCE_UUID, ATT_PROPERTY_READ, ATT_SECURITY_NONE, ATT_SECURITY_NONE, (uint8_t*)appearance, sizeof(appearance));
147 att_db_util_add_service_uuid16(0x1801);
148 att_db_util_add_characteristic_uuid16(GAP_SERVICE_CHANGED, ATT_PROPERTY_INDICATE, ATT_SECURITY_NONE, ATT_SECURITY_NONE, (uint8_t*)service_changed, sizeof(service_changed));
149 att_db_util_add_characteristic_uuid16(0x2b29, ATT_PROPERTY_READ | ATT_PROPERTY_WRITE, ATT_SECURITY_NONE, ATT_SECURITY_NONE, (uint8_t*)supported_features, sizeof(supported_features));
150 att_db_util_add_characteristic_uuid16(0x2b2a, ATT_PROPERTY_READ | ATT_PROPERTY_DYNAMIC, ATT_SECURITY_NONE, ATT_SECURITY_NONE, NULL, 0);
151 att_db_util_add_service_uuid16(0x1808);
152 att_db_util_add_included_service_uuid16(0x0014, 0x0016, 0x180f);
153 att_db_util_add_characteristic_uuid16(0x2a18, ATT_PROPERTY_READ | ATT_PROPERTY_EXTENDED_PROPERTIES | ATT_PROPERTY_INDICATE, ATT_SECURITY_NONE, ATT_SECURITY_NONE, NULL, 0);
154 att_db_util_add_descriptor_uuid16(0x2900, 0, ATT_SECURITY_NONE, ATT_SECURITY_NONE, (uint8_t*)extended_properties, sizeof(extended_properties));
155 att_db_util_add_secondary_service_uuid16(0x180f);
156 att_db_util_add_characteristic_uuid16(0x2a19, ATT_PROPERTY_READ, ATT_SECURITY_NONE, ATT_SECURITY_NONE, (uint8_t*)battery_level, sizeof(battery_level));
157
158 uint16_t hash_len = att_db_util_hash_len();
159 CHECK_EQUAL((uint16_t)sizeof(gatt_database_hash_test_message), hash_len);
160
161 uint16_t i;
162 att_db_util_hash_init();
163 for (i=0;i<hash_len;i++){
164 uint8_t actual_byte = att_db_util_hash_get_next();
165 uint8_t expected_byte = gatt_database_hash_test_message[i];
166 CHECK_EQUAL(expected_byte, actual_byte);
167 }
168
169 // calc cmac
170 att_db_util_hash_calc(&cmac_context, cmac_calculated, &gatt_hash_calculated, NULL);
171 CHECK_EQUAL_ARRAY(gatt_database_hash_expected, cmac_calculated, 16);
172 }
173
main(int argc,const char * argv[])174 int main (int argc, const char * argv[]){
175 return CommandLineTestRunner::RunAllTests(argc, argv);
176 }
177