xref: /btstack/test/sdp_client/service_attribute_search_query.cpp (revision 1d3bd1e51ca491d6783233c8d7431c44f06daa5a)
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 0x35, 0x98, 0x09, 0x00, 0x01, 0x35, 0x03, 0x19, 0x11, 0x17, 0x09, 0x00, 0x04, 0x35, 0x1e, 0x35,
31 0x06, 0x19, 0x01, 0x00, 0x09, 0x00, 0x0f, 0x35, 0x14, 0x19, 0x00, 0x0f, 0x09, 0x01, 0x00, 0x35,
32 0x0c, 0x09, 0x08, 0x00, 0x09, 0x08, 0x06, 0x09, 0x86, 0xdd, 0x09, 0x88, 0x0b, 0x09, 0x00, 0x05,
33 0x35, 0x03, 0x19, 0x10, 0x02, 0x09, 0x00, 0x06, 0x35, 0x09, 0x09, 0x65, 0x6e, 0x09, 0x00, 0x6a,
34 0x09, 0x01, 0x00, 0x09, 0x00, 0x09, 0x35, 0x08, 0x35, 0x06, 0x19, 0x11, 0x17, 0x09, 0x01, 0x00,
35 0x09, 0x01, 0x00, 0x25, 0x1c, 0x47, 0x72, 0x6f, 0x75, 0x70, 0x20, 0x41, 0x64, 0x2d, 0x68, 0x6f,
36 0x63, 0x20, 0x4e, 0x65, 0x74, 0x77, 0x6f, 0x72, 0x6b, 0x20, 0x53, 0x65, 0x72, 0x76, 0x69, 0x63,
37 0x65, 0x09, 0x01, 0x01, 0x25, 0x18, 0x50, 0x41, 0x4e, 0x20, 0x47, 0x72, 0x6f, 0x75, 0x70, 0x20,
38 0x41, 0x64, 0x2d, 0x68, 0x6f, 0x63, 0x20, 0x4e, 0x65, 0x74, 0x77, 0x6f, 0x72, 0x6b, 0x09, 0x03,
39 0x0a, 0x09, 0x00, 0x01, 0x09, 0x03, 0x0b, 0x09, 0x00, 0x05
40 };
41 
42 
43 uint16_t attribute_id = -1;
44 uint16_t record_id = -1;
45 
46 uint8_t * attribute_value = NULL;
47 int       attribute_value_buffer_size = 1000;
48 
assertBuffer(int size)49 void assertBuffer(int size){
50     if (size > attribute_value_buffer_size){
51         attribute_value_buffer_size *= 2;
52         uint8_t * new_attribute = (uint8_t *) realloc(attribute_value, attribute_value_buffer_size);
53         if (!new_attribute) return;
54         attribute_value = new_attribute;
55     }
56 }
57 
test_attribute_value_event(const uint8_t * event)58 static void test_attribute_value_event(const uint8_t * event){
59     static int recordId = 0;
60     static int attributeId = 0;
61     static int attributeOffset = 0;
62     static int attributeLength = 0;
63 
64     CHECK_EQUAL(event[0], SDP_EVENT_QUERY_ATTRIBUTE_VALUE);
65 
66     // record ids are sequential
67    // printf("sdp_event_query_attribute_byte_get_record_id(event) %d",sdp_event_query_attribute_byte_get_record_id(event));
68 
69     if (sdp_event_query_attribute_byte_get_record_id(event) != recordId){
70         recordId++;
71     }
72     CHECK_EQUAL(sdp_event_query_attribute_byte_get_record_id(event), recordId);
73 
74     // is attribute value complete
75     if (sdp_event_query_attribute_byte_get_attribute_id(event) != attributeId ){
76         if (attributeLength > 0){
77             CHECK_EQUAL(attributeLength, attributeOffset+1);
78         }
79         attributeId = sdp_event_query_attribute_byte_get_attribute_id(event);
80         attributeOffset = 0;
81     }
82 
83     // count attribute value bytes
84     if (sdp_event_query_attribute_byte_get_data_offset(event) != attributeOffset){
85         attributeOffset++;
86     }
87     attributeLength = sdp_event_query_attribute_byte_get_attribute_length(event);
88 
89     CHECK_EQUAL(sdp_event_query_attribute_byte_get_data_offset(event), attributeOffset);
90 }
91 
92 
handle_sdp_parser_event(uint8_t packet_type,uint16_t channel,uint8_t * packet,uint16_t size)93 static void handle_sdp_parser_event(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
94     switch (packet[0]){
95         case SDP_EVENT_QUERY_ATTRIBUTE_VALUE:
96             test_attribute_value_event(packet);
97 
98             // handle new record
99             if (sdp_event_query_attribute_byte_get_record_id(packet) != record_id){
100                 record_id = sdp_event_query_attribute_byte_get_record_id(packet);
101             }
102             // buffer data
103             assertBuffer(sdp_event_query_attribute_byte_get_attribute_length(packet));
104             attribute_value[sdp_event_query_attribute_byte_get_data_offset(packet)] = sdp_event_query_attribute_byte_get_data(packet);
105 
106             break;
107         case SDP_EVENT_QUERY_COMPLETE:
108             printf("General query done with status %d.\n", sdp_event_query_complete_get_status(packet));
109             break;
110     }
111 }
112 
113 
TEST_GROUP(SDPClient)114 TEST_GROUP(SDPClient){
115     void setup(void){
116         attribute_value_buffer_size = 1000;
117         attribute_value = (uint8_t*) malloc(attribute_value_buffer_size);
118         record_id = -1;
119         sdp_parser_init(&handle_sdp_parser_event);
120         sdp_parser_init_service_attribute_search();
121     }
122 };
123 
124 
TEST(SDPClient,QueryRFCOMMWithMacOSXData)125 TEST(SDPClient, QueryRFCOMMWithMacOSXData){
126     uint16_t expected_last_attribute_id = 0xffff;
127     uint16_t expected_last_record_id = 0;
128     uint8_t  expected_attribute_value[3] = {0x09, 0x00, 0x05};
129 
130     sdp_parser_handle_chunk(sdp_test_record_list, de_get_len(sdp_test_record_list));
131 
132     CHECK_EQUAL(expected_last_attribute_id, attribute_id);
133     CHECK_EQUAL(expected_last_record_id, record_id);
134 
135     uint16_t i;
136     for (i=0; i<sizeof(expected_attribute_value); i++){
137        CHECK_EQUAL(expected_attribute_value[i], attribute_value[i]);
138     }
139 }
140 
141 
main(int argc,const char * argv[])142 int main (int argc, const char * argv[]){
143     return CommandLineTestRunner::RunAllTests(argc, argv);
144 }
145