xref: /btstack/test/sdp_client/sdp_rfcomm_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 "bluetooth_sdp.h"
19 #include "classic/sdp_client_rfcomm.h"
20 #include "classic/sdp_util.h"
21 #include "classic/spp_server.h"
22 #include "hci.h"
23 #include "hci_cmd.h"
24 #include "hci_dump.h"
25 #include "l2cap.h"
26 #include "mock.h"
27 
28 #include "CppUTest/TestHarness.h"
29 #include "CppUTest/CommandLineTestRunner.h"
30 
31 static uint8_t  service_index;
32 static uint8_t  channel_nr[10];
33 static char*    service_name[10];
34 
35 static uint8_t  sdp_test_record_list[] = { 0x36, 0x02, 0xE7, 0x35, 0x48,
36 0x09, 0x00, 0x01, 0x35, 0x03, 0x19, 0x11, 0x0A, 0x09, 0x00, 0x04, 0x35, 0x10, 0x35, 0x06, 0x19,
37 0x01, 0x00, 0x09, 0x00, 0x19, 0x35, 0x06, 0x19, 0x00, 0x19, 0x09, 0x01, 0x00, 0x09, 0x00, 0x05,
38 0x35, 0x03, 0x19, 0x10, 0x02, 0x09, 0x00, 0x09, 0x35, 0x08, 0x35, 0x06, 0x19, 0x11, 0x0D, 0x09,
39 0x01, 0x00, 0x09, 0x01, 0x00, 0x25, 0x11, 0x41, 0x32, 0x44, 0x50, 0x20, 0x41, 0x75, 0x64, 0x69,
40 0x6F, 0x20, 0x53, 0x6F, 0x75, 0x72, 0x63, 0x65, 0x35, 0x43, 0x09, 0x00, 0x01, 0x35, 0x03, 0x19,
41 0x11, 0x0C, 0x09, 0x00, 0x04, 0x35, 0x10, 0x35, 0x06, 0x19, 0x01, 0x00, 0x09, 0x00, 0x17, 0x35,
42 0x06, 0x19, 0x00, 0x17, 0x09, 0x01, 0x00, 0x09, 0x00, 0x05, 0x35, 0x03, 0x19, 0x10, 0x02, 0x09,
43 0x00, 0x09, 0x35, 0x08, 0x35, 0x06, 0x19, 0x11, 0x0E, 0x09, 0x01, 0x03, 0x09, 0x01, 0x00, 0x25,
44 0x0C, 0x41, 0x56, 0x52, 0x43, 0x50, 0x20, 0x54, 0x61, 0x72, 0x67, 0x65, 0x74, 0x35, 0x56, 0x09,
45 0x00, 0x01, 0x35, 0x03, 0x19, 0x11, 0x05, 0x09, 0x00, 0x04, 0x35, 0x11, 0x35, 0x03, 0x19, 0x01,
46 0x00, 0x35, 0x05, 0x19, 0x00, 0x03, 0x08, 0x0A, 0x35, 0x03, 0x19, 0x00, 0x08, 0x09, 0x00, 0x05,
47 0x35, 0x03, 0x19, 0x10, 0x02, 0x09, 0x00, 0x06, 0x35, 0x09, 0x09, 0x65, 0x6E, 0x09, 0x00, 0x6A,
48 0x09, 0x01, 0x00, 0x09, 0x00, 0x09, 0x35, 0x08, 0x35, 0x06, 0x19, 0x11, 0x05, 0x09, 0x01, 0x00,
49 0x09, 0x01, 0x00, 0x25, 0x10, 0x4F, 0x42, 0x45, 0x58, 0x20, 0x4F, 0x62, 0x6A, 0x65, 0x63, 0x74,
50 0x20, 0x50, 0x75, 0x73, 0x68, 0x35, 0x5A, 0x09, 0x00, 0x01, 0x35, 0x06, 0x19, 0x11, 0x1F, 0x19,
51 0x12, 0x03, 0x09, 0x00, 0x04, 0x35, 0x0C, 0x35, 0x03, 0x19, 0x01, 0x00, 0x35, 0x05, 0x19, 0x00,
52 0x03, 0x08, 0x02, 0x09, 0x00, 0x05, 0x35, 0x03, 0x19, 0x10, 0x02, 0x09, 0x00, 0x06, 0x35, 0x09,
53 0x09, 0x65, 0x6E, 0x09, 0x00, 0x6A, 0x09, 0x01, 0x00, 0x09, 0x00, 0x09, 0x35, 0x06, 0x19, 0x11,
54 0x1E,
55 0x09, 0x01, 0x05, 0x09, 0x01,
56 0x00, 0x25, 0x18, 0x48, 0x61, 0x6E, 0x64, 0x73, 0x20, 0x46, 0x72, 0x65, 0x65, 0x20, 0x41, 0x75,
57 0x64, 0x69, 0x6F, 0x20, 0x47, 0x61, 0x74, 0x65, 0x77, 0x61, 0x79, 0x35, 0x27, 0x09, 0x00, 0x05,
58 0x35, 0x03, 0x19, 0x10, 0x02, 0x09, 0x01, 0x00, 0x25, 0x1A, 0x41, 0x70, 0x70, 0x6C, 0x65, 0x20,
59 0x4D, 0x61, 0x63, 0x69, 0x6E, 0x74, 0x6F, 0x73, 0x68, 0x20, 0x41, 0x74, 0x74, 0x72, 0x69, 0x62,
60 0x75, 0x74, 0x65, 0x73, 0x35, 0x58, 0x09, 0x00, 0x01, 0x35, 0x03, 0x19, 0x11, 0x06, 0x09, 0x00,
61 0x04, 0x35, 0x11, 0x35, 0x03, 0x19, 0x01, 0x00, 0x35, 0x05, 0x19, 0x00, 0x03, 0x08, 0x0F, 0x35,
62 0x03, 0x19, 0x00, 0x08, 0x09, 0x00, 0x05, 0x35, 0x03, 0x19, 0x10, 0x02, 0x09, 0x00, 0x06, 0x35,
63 0x09, 0x09, 0x65, 0x6E, 0x09, 0x00, 0x6A, 0x09, 0x01, 0x00, 0x09, 0x00, 0x09, 0x35, 0x08, 0x35,
64 0x06, 0x19, 0x11, 0x06, 0x09, 0x01, 0x00, 0x09, 0x01, 0x00, 0x25, 0x12, 0x4F, 0x42, 0x45, 0x58,
65 0x20, 0x46, 0x69, 0x6C, 0x65, 0x20, 0x54, 0x72, 0x61, 0x6E, 0x73, 0x66, 0x65, 0x72, 0x35, 0x53,
66 0x09, 0x00, 0x01, 0x35, 0x03, 0x19, 0x11, 0x01, 0x09, 0x00, 0x04, 0x35, 0x0C, 0x35, 0x03, 0x19,
67 0x01, 0x00, 0x35, 0x05, 0x19, 0x00, 0x03, 0x08, 0x03, 0x09, 0x00, 0x05, 0x35, 0x03, 0x19, 0x10,
68 0x02, 0x09, 0x00, 0x06, 0x35, 0x09, 0x09, 0x65, 0x6E, 0x09, 0x00, 0x6A, 0x09, 0x01, 0x00, 0x09,
69 0x00, 0x09, 0x35, 0x08, 0x35, 0x06, 0x19, 0x11, 0x01, 0x09, 0x01, 0x00, 0x09, 0x01, 0x00, 0x25,
70 0x12, 0x42, 0x6C, 0x75, 0x65, 0x74, 0x6F, 0x6F, 0x74, 0x68, 0x2D, 0x50, 0x44, 0x41, 0x2D, 0x53,
71 0x79, 0x6E, 0x63, 0x35, 0x59, 0x09, 0x00, 0x01, 0x35, 0x06, 0x19, 0x11, 0x12, 0x19, 0x12, 0x03,
72 0x09, 0x00, 0x04, 0x35, 0x0C, 0x35, 0x03, 0x19, 0x01, 0x00, 0x35, 0x05, 0x19, 0x00, 0x03, 0x08,
73 0x04, 0x09, 0x00, 0x05, 0x35, 0x03, 0x19, 0x10, 0x02, 0x09, 0x00, 0x06, 0x35, 0x09, 0x09, 0x65,
74 0x6E,
75 0x09, 0x00, 0x6A, 0x09, 0x01,
76 0x00, 0x09, 0x00, 0x09, 0x35, 0x08, 0x35, 0x06, 0x19, 0x11, 0x08, 0x09, 0x01, 0x02, 0x09, 0x01,
77 0x00, 0x25, 0x15, 0x48, 0x65, 0x61, 0x64, 0x73, 0x65, 0x74, 0x20, 0x41, 0x75, 0x64, 0x69, 0x6F,
78 0x20, 0x47, 0x61, 0x74, 0x65, 0x77, 0x61, 0x79, 0x35, 0x6F, 0x09, 0x00, 0x01, 0x35, 0x03, 0x19,
79 0x11, 0x17, 0x09, 0x00, 0x04, 0x35, 0x1E, 0x35, 0x06, 0x19, 0x01, 0x00, 0x09, 0x00, 0x0F, 0x35,
80 0x14, 0x19, 0x00, 0x0F, 0x09, 0x01, 0x00, 0x35, 0x0C, 0x09, 0x08, 0x00, 0x09, 0x08, 0x06, 0x09,
81 0x86, 0xDD, 0x09, 0x88, 0x0B, 0x09, 0x00, 0x05, 0x35, 0x03, 0x19, 0x10, 0x02, 0x09, 0x00, 0x06,
82 0x35, 0x09, 0x09, 0x65, 0x6E, 0x09, 0x00, 0x6A, 0x09, 0x01, 0x00, 0x09, 0x00, 0x09, 0x35, 0x08,
83 0x35, 0x06, 0x19, 0x11, 0x17, 0x09, 0x01, 0x00, 0x09, 0x01, 0x00, 0x25, 0x1C, 0x47, 0x72, 0x6F,
84 0x75, 0x70, 0x20, 0x41, 0x64, 0x2D, 0x68, 0x6F, 0x63, 0x20, 0x4E, 0x65, 0x74, 0x77, 0x6F, 0x72,
85 0x6B, 0x20, 0x53, 0x65, 0x72, 0x76, 0x69, 0x63, 0x65
86 };
87 
88 
89 void handle_query_rfcomm_event(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
90     switch (packet[0]){
91         case SDP_EVENT_QUERY_RFCOMM_SERVICE:
92             channel_nr[service_index] = sdp_event_query_rfcomm_service_get_rfcomm_channel(packet);
93             service_name[service_index] = (char*) malloc(SDP_SERVICE_NAME_LEN+1);
94             strncpy(service_name[service_index], sdp_event_query_rfcomm_service_get_name(packet), SDP_SERVICE_NAME_LEN);
95             service_name[service_index][SDP_SERVICE_NAME_LEN] = 0;
96             // printf("CALLBACK: Service name: '%s', RFCOMM port %u, service index %d\n", service_name[service_index], channel_nr[service_index], service_index);
97             service_index++;
98             break;
99         case SDP_EVENT_QUERY_COMPLETE:
100             // printf("CALLBACK: Client query response done. \n");
101             break;
102     }
103 }
104 
105 
106 TEST_GROUP(SDPClient){
107     uint8_t spp_buffer[sizeof(sdp_test_record_list)];
108     bd_addr_t address;
109 
110     void setup(void){
111         service_index = 0;
112         sdp_client_reset(); // avoid "not ready" warning
113     }
114 };
115 
116 
117 TEST(SDPClient, QueryRFCOMMWithSyntheticData){
118     const char* expected_name[] = {"SDP Response Test1", "SDP Response Test2", "SDP Response Test3"};
119     uint8_t expected_channel[] = {10, 11, 12};
120     int record_nr = sizeof(expected_channel)/sizeof(uint8_t);
121     int i;
122 
123     // start query using public API although data will be injected
124     sdp_client_query_rfcomm_channel_and_name_for_uuid(&handle_query_rfcomm_event, address, 0x1234);
125 
126     de_create_sequence(spp_buffer);
127     for (i=0; i<record_nr; i++){
128         uint8_t * record_start = de_push_sequence(spp_buffer);
129         spp_create_sdp_record(record_start, 0x10001, expected_channel[i], expected_name[i]);
130         de_pop_sequence(spp_buffer, record_start);
131     }
132 
133     sdp_parser_handle_chunk(spp_buffer, de_get_len(spp_buffer));
134 
135     CHECK_EQUAL(record_nr, service_index);
136     for (i=0; i<service_index; i++){
137         STRCMP_EQUAL(expected_name[i], service_name[i]);
138         CHECK_EQUAL(expected_channel[i], channel_nr[i]);
139     }
140 }
141 
142 TEST(SDPClient, QueryRFCOMMWithMacOSXData){
143 
144     const char* expected_name[] = {"OBEX Object Push",
145                                     "Hands Free Audio Gat", "OBEX File Transfer",
146                                     "Bluetooth-PDA-Sync", "Headset Audio Gatewa"};
147     uint8_t expected_channel[] = {10, 2, 15, 3, 4};
148 
149     // start query using public API although data will be injected
150     sdp_client_query_rfcomm_channel_and_name_for_uuid(&handle_query_rfcomm_event, address, 0x1234);
151 
152     // de_dump_data_element(sdp_test_record_list);
153     sdp_parser_handle_chunk(sdp_test_record_list, de_get_len(sdp_test_record_list));
154 
155     CHECK_EQUAL(5, service_index);
156     int i;
157     for (i=0; i<service_index; i++){
158         STRCMP_EQUAL(expected_name[i], service_name[i]);
159         CHECK_EQUAL(expected_channel[i], channel_nr[i]);
160     }
161 }
162 
163 TEST(SDPClient, QueryClassListValid){
164     // start query using public API although data will be injected
165     sdp_client_query_rfcomm_channel_and_name_for_service_class_uuid(&handle_query_rfcomm_event, address, BLUETOOTH_SERVICE_CLASS_HANDSFREE_AUDIO_GATEWAY);
166 
167     // de_dump_data_element(sdp_test_record_list);
168     sdp_parser_handle_chunk(sdp_test_record_list, de_get_len(sdp_test_record_list));
169 
170     CHECK_EQUAL(1, service_index);
171 }
172 
173 TEST(SDPClient, QueryClassListInvalid){
174     // start query using public API although data will be injected
175     sdp_client_query_rfcomm_channel_and_name_for_service_class_uuid(&handle_query_rfcomm_event, address, BLUETOOTH_SERVICE_CLASS_HANDSFREE);
176     sdp_parser_handle_chunk(sdp_test_record_list, de_get_len(sdp_test_record_list));
177 
178     CHECK_EQUAL(0, service_index);
179 }
180 
181 int main (int argc, const char * argv[]){
182     return CommandLineTestRunner::RunAllTests(argc, argv);
183 }
184