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