xref: /btstack/test/avdtp_util/avdtp_util_test.cpp (revision a64cbea79fa5fb9daa58e135a19c6cce10f3e642)
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 //
40 // test rfcomm query tests
41 //
42 // *****************************************************************************
43 
44 
45 #include <stdint.h>
46 #include <stdio.h>
47 #include <stdlib.h>
48 #include <string.h>
49 
50 #include "CppUTest/TestHarness.h"
51 #include "CppUTest/CommandLineTestRunner.h"
52 
53 #include "classic/avdtp_util.h"
54 #include "btstack.h"
55 #include "classic/avdtp.h"
56 #include "classic/avdtp_util.h"
57 
58 
59 // mock start
60 extern "C" uint16_t l2cap_get_remote_mtu_for_local_cid(uint16_t local_cid){
61     return 1024;
62 }
63 extern "C" void avdtp_emit_sink_and_source(uint8_t * packet, uint16_t size){}
64 extern "C" void avdtp_emit_source(uint8_t * packet, uint16_t size){}
65 extern "C" uint8_t l2cap_request_can_send_now_event(uint16_t local_cid){
66     return ERROR_CODE_SUCCESS;
67 }
68 extern "C" btstack_packet_handler_t avdtp_packet_handler_for_stream_endpoint(const avdtp_stream_endpoint_t * stream_endpoint){
69     UNUSED(stream_endpoint);
70     return NULL;
71 }
72 // mock end
73 
74 static uint8_t sbc_codec_capabilities[] = {
75     0xFF, // (AVDTP_SBC_44100 << 4) | AVDTP_SBC_STEREO,
76     0xFF, //(AVDTP_SBC_BLOCK_LENGTH_16 << 4) | (AVDTP_SBC_SUBBANDS_8 << 2) | AVDTP_SBC_ALLOCATION_METHOD_LOUDNESS,
77     2, 53
78 };
79 
80 static uint8_t packed_capabilities[] = {
81     0x01, 0x00,
82     0x07, 0x06, 0x00, 0x00, 0xff, 0xff, 0x02, 0x35,
83     0x08, 0x00
84 };
85 
86 static uint16_t configuration_bitmap = (1 << AVDTP_MEDIA_TRANSPORT) | (1 << AVDTP_MEDIA_CODEC) | (1 << AVDTP_DELAY_REPORTING);
87 
88 TEST_GROUP(AvdtpUtil){
89     avdtp_connection_t connection;
90     avdtp_capabilities_t caps;
91     avdtp_signaling_packet_t signaling_packet;
92 
93     void setup(){
94         caps.recovery.recovery_type = 0x01;                  // 0x01 = RFC2733
95         caps.recovery.maximum_recovery_window_size = 0x01;   // 0x01 to 0x18, for a Transport Packet
96         caps.recovery.maximum_number_media_packets = 0x01;
97 
98         caps.content_protection.cp_type = 0x1111;
99         caps.content_protection.cp_type_value_len = AVDTP_MAX_CONTENT_PROTECTION_TYPE_VALUE_LEN;
100 
101         int i;
102         for (i=0; i<AVDTP_MAX_CONTENT_PROTECTION_TYPE_VALUE_LEN; i++){
103             caps.content_protection.cp_type_value[i] = i;
104         }
105 
106         caps.header_compression.back_ch = 0x01;
107         caps.header_compression.media = 0x01;
108         caps.header_compression.recovery = 0x01;
109 
110         caps.multiplexing_mode.fragmentation = 0x11;
111         caps.multiplexing_mode.transport_identifiers_num = 3;
112 
113         for (i=0; i<3; i++){
114             caps.multiplexing_mode.transport_session_identifiers[i] = i;
115             caps.multiplexing_mode.tcid[i] = (i | 0x20);
116         }
117 
118         caps.media_codec.media_type = AVDTP_AUDIO;
119         caps.media_codec.media_codec_type = AVDTP_CODEC_SBC;
120         caps.media_codec.media_codec_information_len = 4;
121         caps.media_codec.media_codec_information = sbc_codec_capabilities;
122     }
123 };
124 
125 
126 TEST(AvdtpUtil, avdtp_pack_service_capabilities_test){
127     uint8_t packet[200];
128     {
129         // 0 - packet length
130         uint8_t packed_capabilities[] = {0x00};
131         memset(packet, 0, sizeof(packet));
132         uint16_t packet_length = avdtp_pack_service_capabilities(packet, sizeof(packet), caps, AVDTP_SERVICE_CATEGORY_INVALID_0);
133         CHECK_EQUAL(sizeof(packed_capabilities), packet_length);
134         MEMCMP_EQUAL(packed_capabilities, packet, packet_length);
135     }
136 
137     {
138         uint8_t packed_capabilities[] = {0x00};
139         memset(packet, 0, sizeof(packet));
140         uint16_t packet_length = avdtp_pack_service_capabilities(packet, sizeof(packet), caps, AVDTP_MEDIA_TRANSPORT);
141         CHECK_EQUAL(sizeof(packed_capabilities), packet_length);
142         MEMCMP_EQUAL(packed_capabilities, packet, packet_length);
143     }
144 
145     {
146         uint8_t packed_capabilities[] = {0x00};
147         memset(packet, 0, sizeof(packet));
148         uint16_t packet_length = avdtp_pack_service_capabilities(packet, sizeof(packet), caps, AVDTP_MEDIA_TRANSPORT);
149         CHECK_EQUAL(sizeof(packed_capabilities), packet_length);
150         MEMCMP_EQUAL(packed_capabilities, packet, packet_length);
151     }
152 
153     {
154         uint8_t packed_capabilities[] = {0x00};
155         memset(packet, 0, sizeof(packet));
156         uint16_t packet_length = avdtp_pack_service_capabilities(packet, sizeof(packet), caps, AVDTP_REPORTING);
157         CHECK_EQUAL(sizeof(packed_capabilities), packet_length);
158         MEMCMP_EQUAL(packed_capabilities, packet, packet_length);
159     }
160 
161     {
162         uint8_t packed_capabilities[] = {0x03, 0x01, 0x01, 0x01};
163         memset(packet, 0, sizeof(packet));
164         uint16_t packet_length = avdtp_pack_service_capabilities(packet, sizeof(packet), caps, AVDTP_RECOVERY);
165         CHECK_EQUAL(sizeof(packed_capabilities), packet_length);
166         MEMCMP_EQUAL(packed_capabilities, packet, packet_length);
167     }
168 
169     {
170         uint8_t packed_capabilities[] = {0x0D, 0x0A + 2, 0x11, 0x11, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09};
171         memset(packet, 0, sizeof(packet));
172         uint16_t packet_length = avdtp_pack_service_capabilities(packet, sizeof(packet), caps, AVDTP_CONTENT_PROTECTION);
173         CHECK_EQUAL(sizeof(packed_capabilities), packet_length);
174         MEMCMP_EQUAL(packed_capabilities, packet, packet_length);
175     }
176 
177     {
178         uint8_t packed_capabilities[] = {0x01, 0xE0};
179         memset(packet, 0, sizeof(packet));
180         uint16_t packet_length = avdtp_pack_service_capabilities(packet, sizeof(packet), caps, AVDTP_HEADER_COMPRESSION);
181         CHECK_EQUAL(sizeof(packed_capabilities), packet_length);
182         MEMCMP_EQUAL(packed_capabilities, packet, packet_length);
183     }
184 
185     {
186         uint8_t packed_capabilities[] = {0x07, (uint8_t)(0x11 << 7), (uint8_t)(0x00 << 7), (uint8_t)(0x20 << 7), (uint8_t)(0x01 << 7), (uint8_t)(0x21 << 7), (uint8_t)(0x02 << 7), (uint8_t)(0x22 << 7)};
187         memset(packet, 0, sizeof(packet));
188         uint16_t packet_length = avdtp_pack_service_capabilities(packet, sizeof(packet), caps, AVDTP_MULTIPLEXING);
189         CHECK_EQUAL(sizeof(packed_capabilities), packet_length);
190         MEMCMP_EQUAL(packed_capabilities, packet, packet_length);
191     }
192 
193     {
194         uint8_t packed_capabilities[] = {0x06, 0x00, 0x00, 0xff, 0xff, 0x02, 0x35};
195         memset(packet, 0, sizeof(packet));
196         uint16_t packet_length = avdtp_pack_service_capabilities(packet, sizeof(packet), caps, AVDTP_MEDIA_CODEC);
197         CHECK_EQUAL(sizeof(packed_capabilities), packet_length);
198         MEMCMP_EQUAL(packed_capabilities, packet, packet_length);
199     }
200 
201     {
202         uint8_t packed_capabilities[] = {0x00};
203         memset(packet, 0, sizeof(packet));
204         uint16_t packet_length = avdtp_pack_service_capabilities(packet, sizeof(packet), caps, AVDTP_DELAY_REPORTING);
205         CHECK_EQUAL(sizeof(packed_capabilities), packet_length);
206         MEMCMP_EQUAL(packed_capabilities, packet, packet_length);
207     }
208 }
209 
210 TEST(AvdtpUtil, avdtp_prepare_capabilities){
211     avdtp_prepare_capabilities(&signaling_packet, 0x01, configuration_bitmap, caps, AVDTP_SI_GET_ALL_CAPABILITIES);
212     MEMCMP_EQUAL(packed_capabilities, signaling_packet.command, sizeof(packed_capabilities));
213 }
214 
215 
216 TEST(AvdtpUtil, avdtp_unpack_service_capabilities_test){
217     avdtp_capabilities_t capabilities;
218 
219     CHECK_EQUAL(configuration_bitmap, avdtp_unpack_service_capabilities(&connection, AVDTP_SI_GET_CONFIGURATION, &capabilities, &packed_capabilities[0], sizeof(packed_capabilities)));
220     // media codec:
221     CHECK_EQUAL(AVDTP_CODEC_SBC, capabilities.media_codec.media_codec_type);
222     CHECK_EQUAL(4,  capabilities.media_codec.media_codec_information_len);
223     MEMCMP_EQUAL(sbc_codec_capabilities, capabilities.media_codec.media_codec_information, 4);
224 
225 
226 }
227 
228 int main (int argc, const char * argv[]){
229     return CommandLineTestRunner::RunAllTests(argc, argv);
230 }
231