xref: /btstack/test/sdp_client/general_sdp_query.cpp (revision 4a9eead824c50b40e12b6f72611a74a3f57a47f6)
1 
2 // *****************************************************************************
3 //
4 // test rfcomm query tests
5 //
6 // *****************************************************************************
7 
8 #include "btstack_config.h"
9 
10 #include <stdint.h>
11 #include <stdio.h>
12 #include <stdlib.h>
13 #include <string.h>
14 
15 #include "btstack_event.h"
16 #include "btstack_memory.h"
17 #include "btstack_run_loop.h"
18 #include "hci.h"
19 #include "hci_cmd.h"
20 #include "hci_dump.h"
21 #include "l2cap.h"
22 #include "mock.h"
23 #include "classic/sdp_util.h"
24 
25 #include "CppUTest/TestHarness.h"
26 #include "CppUTest/CommandLineTestRunner.h"
27 
28 
29 static uint8_t  sdp_test_record_list[] = {
30                                                                   0x36, 0x03, 0xDE, 0x35, 0x62,
31 0x09, 0x00, 0x01, 0x35, 0x03, 0x19, 0x11, 0x0A, 0x09, 0x00, 0x04, 0x35, 0x10, 0x35, 0x06, 0x19,
32 0x01, 0x00, 0x09, 0x00, 0x19, 0x35, 0x06, 0x19, 0x00, 0x19, 0x09, 0x01, 0x00, 0x09, 0x00, 0x05,
33 0x35, 0x03, 0x19, 0x10, 0x02, 0x09, 0x00, 0x09, 0x35, 0x08, 0x35, 0x06, 0x19, 0x11, 0x0D, 0x09,
34 0x01, 0x00, 0x09, 0x01, 0x00, 0x25, 0x11, 0x41, 0x32, 0x44, 0x50, 0x20, 0x41, 0x75, 0x64, 0x69,
35 0x6F, 0x20, 0x53, 0x6F, 0x75, 0x72, 0x63, 0x65, 0x09, 0x03, 0x11, 0x09, 0x00, 0x01, 0x09, 0x07,
36 0x77, 0x1C, 0x6F, 0x6D, 0x98, 0xF2, 0x3C, 0x3A, 0x11, 0xD6, 0x95, 0x6A, 0x00, 0x03, 0x93, 0x53,
37 0xE8, 0x58, 0x35, 0x5D, 0x09, 0x00, 0x01, 0x35, 0x03, 0x19, 0x11, 0x0C, 0x09, 0x00, 0x04, 0x35,
38 0x10, 0x35, 0x06, 0x19, 0x01, 0x00, 0x09, 0x00, 0x17, 0x35, 0x06, 0x19, 0x00, 0x17, 0x09, 0x01,
39 0x00, 0x09, 0x00, 0x05, 0x35, 0x03, 0x19, 0x10, 0x02, 0x09, 0x00, 0x09, 0x35, 0x08, 0x35, 0x06,
40 0x19, 0x11, 0x0E, 0x09, 0x01, 0x03, 0x09, 0x01, 0x00, 0x25, 0x0C, 0x41, 0x56, 0x52, 0x43, 0x50,
41 0x20, 0x54, 0x61, 0x72, 0x67, 0x65, 0x74, 0x09, 0x03, 0x11, 0x09, 0x00, 0x01, 0x09, 0x07, 0x77,
42 0x1C, 0x6F, 0x6D, 0x98, 0xF2, 0x3C, 0x3A, 0x11, 0xD6, 0x95, 0x6A, 0x00, 0x03, 0x93, 0x53, 0xE8,
43 0x58, 0x35, 0x71, 0x09, 0x00, 0x01, 0x35, 0x03, 0x19, 0x11, 0x05, 0x09, 0x00, 0x04, 0x35, 0x11,
44 0x35, 0x03, 0x19, 0x01, 0x00, 0x35, 0x05, 0x19, 0x00, 0x03, 0x08, 0x0A, 0x35, 0x03, 0x19, 0x00,
45 0x08, 0x09, 0x00, 0x05, 0x35, 0x03, 0x19, 0x10, 0x02, 0x09, 0x00, 0x06, 0x35, 0x09, 0x09, 0x65,
46 0x6E, 0x09, 0x00, 0x6A, 0x09, 0x01, 0x00, 0x09, 0x00, 0x09, 0x35, 0x08, 0x35, 0x06, 0x19, 0x11,
47 0x05, 0x09, 0x01, 0x00, 0x09, 0x01, 0x00, 0x25, 0x10, 0x4F, 0x42, 0x45, 0x58, 0x20, 0x4F, 0x62,
48 0x6A, 0x65, 0x63, 0x74, 0x20, 0x50, 0x75, 0x73, 0x68, 0x09, 0x03, 0x03, 0x35, 0x02, 0x08, 0xFF,
49 0x09, 0x07, 0x77, 0x1C, 0x6F, 0x6D, 0x98, 0xF2, 0x3C, 0x3A, 0x11, 0xD6, 0x95, 0x6A, 0x00, 0x03,
50 0x93, 0x53, 0xE8, 0x58, 0x35, 0x65, 0x09, 0x00, 0x01, 0x35, 0x06, 0x19, 0x11, 0x1F, 0x19, 0x12,
51 0x03, 0x09, 0x00, 0x04, 0x35, 0x0C, 0x35, 0x03, 0x19, 0x01, 0x00, 0x35, 0x05, 0x19, 0x00, 0x03,
52 0x08, 0x02, 0x09, 0x00, 0x05, 0x35, 0x03, 0x19, 0x10, 0x02, 0x09, 0x00, 0x06, 0x35, 0x09, 0x09,
53 0x65, 0x6E, 0x09, 0x00, 0x6A, 0x09, 0x01, 0x00, 0x09, 0x00, 0x09,
54 
55                                                                   0x35, 0x06, 0x19, 0x11, 0x1E,
56 0x09, 0x01, 0x05, 0x09, 0x01, 0x00, 0x25, 0x18, 0x48, 0x61, 0x6E, 0x64, 0x73, 0x20, 0x46, 0x72,
57 0x65, 0x65, 0x20, 0x41, 0x75, 0x64, 0x69, 0x6F, 0x20, 0x47, 0x61, 0x74, 0x65, 0x77, 0x61, 0x79,
58 0x09, 0x03, 0x01, 0x08, 0x00, 0x09, 0x03, 0x11, 0x09, 0x00, 0x00, 0x35, 0x80, 0x09, 0x00, 0x05,
59 0x35, 0x03, 0x19, 0x10, 0x02, 0x09, 0x01, 0x00, 0x25, 0x1A, 0x41, 0x70, 0x70, 0x6C, 0x65, 0x20,
60 0x4D, 0x61, 0x63, 0x69, 0x6E, 0x74, 0x6F, 0x73, 0x68, 0x20, 0x41, 0x74, 0x74, 0x72, 0x69, 0x62,
61 0x75, 0x74, 0x65, 0x73, 0x09, 0x07, 0x80, 0x1C, 0xF0, 0x72, 0x2E, 0x20, 0x0F, 0x8B, 0x4E, 0x90,
62 0x8C, 0xC2, 0x1B, 0x46, 0xF5, 0xF2, 0xEF, 0xE2, 0x09, 0x07, 0x81, 0x25, 0x09, 0x3C, 0x75, 0x6E,
63 0x6B, 0x6E, 0x6F, 0x77, 0x6E, 0x3E, 0x09, 0x07, 0x82, 0x25, 0x0D, 0x4D, 0x61, 0x63, 0x42, 0x6F,
64 0x6F, 0x6B, 0x41, 0x69, 0x72, 0x34, 0x2C, 0x31, 0x09, 0x07, 0x83, 0x28, 0x01, 0x09, 0x07, 0x84,
65 0x25, 0x0D, 0x34, 0x2E, 0x31, 0x2E, 0x33, 0x66, 0x33, 0x20, 0x31, 0x31, 0x33, 0x34, 0x39, 0x09,
66 0x07, 0x85, 0x0A, 0x00, 0x00, 0x00, 0x03, 0x09, 0x07, 0x86, 0x19, 0x12, 0x34, 0x35, 0x73, 0x09,
67 0x00, 0x01, 0x35, 0x03, 0x19, 0x11, 0x06, 0x09, 0x00, 0x04, 0x35, 0x11, 0x35, 0x03, 0x19, 0x01,
68 0x00, 0x35, 0x05, 0x19, 0x00, 0x03, 0x08, 0x0F, 0x35, 0x03, 0x19, 0x00, 0x08, 0x09, 0x00, 0x05,
69 0x35, 0x03, 0x19, 0x10, 0x02, 0x09, 0x00, 0x06, 0x35, 0x09, 0x09, 0x65, 0x6E, 0x09, 0x00, 0x6A,
70 0x09, 0x01, 0x00, 0x09, 0x00, 0x09, 0x35, 0x08, 0x35, 0x06, 0x19, 0x11, 0x06, 0x09, 0x01, 0x00,
71 0x09, 0x01, 0x00, 0x25, 0x12, 0x4F, 0x42, 0x45, 0x58, 0x20, 0x46, 0x69, 0x6C, 0x65, 0x20, 0x54,
72 0x72, 0x61, 0x6E, 0x73, 0x66, 0x65, 0x72, 0x09, 0x03, 0x03, 0x35, 0x02, 0x08, 0xFF, 0x09, 0x07,
73 0x77, 0x1C, 0x6F, 0x6D, 0x98, 0xF2, 0x3C, 0x3A, 0x11, 0xD6, 0x95, 0x6A, 0x00, 0x03, 0x93, 0x53,
74 0xE8, 0x58, 0x35, 0x53, 0x09, 0x00, 0x01, 0x35, 0x03, 0x19, 0x11, 0x01, 0x09, 0x00, 0x04, 0x35,
75 0x0C, 0x35, 0x03, 0x19, 0x01, 0x00, 0x35, 0x05, 0x19, 0x00, 0x03, 0x08, 0x03, 0x09, 0x00, 0x05,
76 0x35, 0x03, 0x19, 0x10, 0x02, 0x09, 0x00, 0x06, 0x35, 0x09, 0x09, 0x65, 0x6E, 0x09, 0x00, 0x6A,
77 0x09, 0x01, 0x00, 0x09, 0x00, 0x09, 0x35, 0x08, 0x35, 0x06, 0x19, 0x11, 0x01, 0x09, 0x01, 0x00,
78 0x09, 0x01, 0x00, 0x25, 0x12, 0x42, 0x6C, 0x75, 0x65, 0x74, 0x6F,
79 
80                                                                   0x6F, 0x74, 0x68, 0x2D, 0x50,
81 0x44, 0x41, 0x2D, 0x53, 0x79, 0x6E, 0x63, 0x35, 0x59, 0x09, 0x00, 0x01, 0x35, 0x06, 0x19, 0x11,
82 0x12, 0x19, 0x12, 0x03, 0x09, 0x00, 0x04, 0x35, 0x0C, 0x35, 0x03, 0x19, 0x01, 0x00, 0x35, 0x05,
83 0x19, 0x00, 0x03, 0x08, 0x04, 0x09, 0x00, 0x05, 0x35, 0x03, 0x19, 0x10, 0x02, 0x09, 0x00, 0x06,
84 0x35, 0x09, 0x09, 0x65, 0x6E, 0x09, 0x00, 0x6A, 0x09, 0x01, 0x00, 0x09, 0x00, 0x09, 0x35, 0x08,
85 0x35, 0x06, 0x19, 0x11, 0x08, 0x09, 0x01, 0x02, 0x09, 0x01, 0x00, 0x25, 0x15, 0x48, 0x65, 0x61,
86 0x64, 0x73, 0x65, 0x74, 0x20, 0x41, 0x75, 0x64, 0x69, 0x6F, 0x20, 0x47, 0x61, 0x74, 0x65, 0x77,
87 0x61, 0x79, 0x35, 0x98, 0x09, 0x00, 0x01, 0x35, 0x03, 0x19, 0x11, 0x17, 0x09, 0x00, 0x04, 0x35,
88 0x1E, 0x35, 0x06, 0x19, 0x01, 0x00, 0x09, 0x00, 0x0F, 0x35, 0x14, 0x19, 0x00, 0x0F, 0x09, 0x01,
89 0x00, 0x35, 0x0C, 0x09, 0x08, 0x00, 0x09, 0x08, 0x06, 0x09, 0x86, 0xDD, 0x09, 0x88, 0x0B, 0x09,
90 0x00, 0x05, 0x35, 0x03, 0x19, 0x10, 0x02, 0x09, 0x00, 0x06, 0x35, 0x09, 0x09, 0x65, 0x6E, 0x09,
91 0x00, 0x6A, 0x09, 0x01, 0x00, 0x09, 0x00, 0x09, 0x35, 0x08, 0x35, 0x06, 0x19, 0x11, 0x17, 0x09,
92 0x01, 0x00, 0x09, 0x01, 0x00, 0x25, 0x1C, 0x47, 0x72, 0x6F, 0x75, 0x70, 0x20, 0x41, 0x64, 0x2D,
93 0x68, 0x6F, 0x63, 0x20, 0x4E, 0x65, 0x74, 0x77, 0x6F, 0x72, 0x6B, 0x20, 0x53, 0x65, 0x72, 0x76,
94 0x69, 0x63, 0x65, 0x09, 0x01, 0x01, 0x25, 0x18, 0x50, 0x41, 0x4E, 0x20, 0x47, 0x72, 0x6F, 0x75,
95 0x70, 0x20, 0x41, 0x64, 0x2D, 0x68, 0x6F, 0x63, 0x20, 0x4E, 0x65, 0x74, 0x77, 0x6F, 0x72, 0x6B,
96 0x09, 0x03, 0x0A, 0x09, 0x00, 0x01, 0x09, 0x03, 0x0B, 0x09, 0x00, 0x05
97 };
98 
99 uint16_t attribute_id = -1;
100 uint16_t record_id = -1;
101 
102 uint8_t * attribute_value = NULL;
103 int       attribute_value_buffer_size = 1000;
104 
105 void assertBuffer(int size){
106     if (size > attribute_value_buffer_size){
107         attribute_value_buffer_size *= 2;
108         uint8_t * new_attribute = (uint8_t *) realloc(attribute_value, attribute_value_buffer_size);
109         if (!new_attribute) return;
110         attribute_value = new_attribute;
111     }
112 }
113 
114 static void test_attribute_value_event(const uint8_t * event){
115     static int recordId = 0;
116     static int attributeId = 0;
117     static int attributeOffset = 0;
118     static int attributeLength = 0;
119 
120     CHECK_EQUAL(event[0], SDP_EVENT_QUERY_ATTRIBUTE_VALUE);
121 
122     // record ids are sequential
123     if (sdp_event_query_attribute_byte_get_record_id(event) != recordId){
124         recordId++;
125     }
126     CHECK_EQUAL(sdp_event_query_attribute_byte_get_record_id(event), recordId);
127 
128     // is attribute value complete
129     if (sdp_event_query_attribute_byte_get_attribute_id(event) != attributeId ){
130         if (attributeLength > 0){
131             CHECK_EQUAL(attributeLength, attributeOffset+1);
132         }
133         attributeId = sdp_event_query_attribute_byte_get_attribute_id(event);
134         attributeOffset = 0;
135     }
136 
137     // count attribute value bytes
138     if (sdp_event_query_attribute_byte_get_data_offset(event) != attributeOffset){
139         attributeOffset++;
140     }
141     attributeLength = sdp_event_query_attribute_byte_get_attribute_length(event);
142 
143     CHECK_EQUAL(sdp_event_query_attribute_byte_get_data_offset(event), attributeOffset);
144 }
145 
146 
147 static void handle_sdp_parser_event(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
148     switch (packet[0]){
149         case SDP_EVENT_QUERY_ATTRIBUTE_VALUE:
150             test_attribute_value_event(packet);
151 
152             // handle new record
153             if (sdp_event_query_attribute_byte_get_record_id(packet) != record_id){
154                 record_id = sdp_event_query_attribute_byte_get_record_id(packet);
155             }
156             // buffer data
157             assertBuffer(sdp_event_query_attribute_byte_get_attribute_length(packet));
158             attribute_value[sdp_event_query_attribute_byte_get_data_offset(packet)] = sdp_event_query_attribute_byte_get_data(packet);
159 
160             break;
161         case SDP_EVENT_QUERY_COMPLETE:
162             printf("General query done with status %d.\n", sdp_event_query_complete_get_status(packet));
163             break;
164     }
165 }
166 
167 
168 TEST_GROUP(SDPClient){
169     void setup(void){
170         attribute_value_buffer_size = 1000;
171         attribute_value = (uint8_t*) malloc(attribute_value_buffer_size);
172         record_id = -1;
173         sdp_parser_init(&handle_sdp_parser_event);
174     }
175 };
176 
177 
178 TEST(SDPClient, QueryWithMacOSXData){
179     uint16_t expected_last_attribute_id = 0xffff;
180     uint16_t expected_last_record_id = 8;
181     uint8_t  expected_attribute_value[3] = {0x09, 0x00, 0x05};
182 
183     sdp_parser_handle_chunk(sdp_test_record_list, de_get_len(sdp_test_record_list));
184 
185     CHECK_EQUAL(expected_last_attribute_id, attribute_id);
186     CHECK_EQUAL(expected_last_record_id, record_id);
187 
188     uint16_t i;
189     for (i=0; i<sizeof(expected_attribute_value); i++){
190        CHECK_EQUAL(expected_attribute_value[i], attribute_value[i]);
191     }
192 }
193 
194 
195 int main (int argc, const char * argv[]){
196     return CommandLineTestRunner::RunAllTests(argc, argv);
197 }
198