xref: /freebsd/contrib/netbsd-tests/lib/libbluetooth/t_sdp_set.c (revision 3416500aef140042c64bc149cb1ec6620483bc44)
1 /*	$NetBSD: t_sdp_set.c,v 1.2 2011/04/07 08:29:50 plunky Exp $	*/
2 
3 /*-
4  * Copyright (c) 2011 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Iain Hibbert.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29  * POSSIBILITY OF SUCH DAMAGE.
30  */
31 
32 #include <atf-c.h>
33 
34 #include <limits.h>
35 #include <sdp.h>
36 #include <string.h>
37 
38 ATF_TC(check_sdp_set_bool);
39 
40 ATF_TC_HEAD(check_sdp_set_bool, tc)
41 {
42 
43 	atf_tc_set_md_var(tc, "descr", "Test sdp_set_bool results");
44 }
45 
46 ATF_TC_BODY(check_sdp_set_bool, tc)
47 {
48 	uint8_t data[] = {
49 		0x28, 0x00,	// bool	false
50 		0x00,		// nil
51 		0x28,		// bool <invalid>
52 	};
53 	sdp_data_t test = { data, data + sizeof(data) };
54 	sdp_data_t discard;
55 
56 	ATF_CHECK_EQ(sdp_data_type(&test), SDP_DATA_BOOL);
57 	ATF_REQUIRE(sdp_set_bool(&test, true));
58 	ATF_CHECK_EQ(test.next[1], 0x01);
59 	ATF_REQUIRE(sdp_set_bool(&test, false));
60 	ATF_CHECK_EQ(test.next[1], 0x00);
61 	ATF_REQUIRE(sdp_get_data(&test, &discard));
62 
63 	ATF_CHECK_EQ(sdp_data_type(&test), SDP_DATA_NIL);
64 	ATF_CHECK_EQ(sdp_set_bool(&test, true), false);		/* not bool */
65 	ATF_REQUIRE(sdp_get_data(&test, &discard));
66 
67 	ATF_CHECK_EQ(sdp_data_type(&test), SDP_DATA_BOOL);
68 	ATF_CHECK_EQ(sdp_set_bool(&test, true), false);		/* no value */
69 }
70 
71 ATF_TC(check_sdp_set_uint);
72 
73 ATF_TC_HEAD(check_sdp_set_uint, tc)
74 {
75 
76 	atf_tc_set_md_var(tc, "descr", "Test sdp_set_uint results");
77 }
78 
79 ATF_TC_BODY(check_sdp_set_uint, tc)
80 {
81 	uint8_t data[] = {
82 		0x08, 0x00,		// uint8	0x00
83 		0x00,			// nil
84 		0x09, 0x00, 0x00,	// uint16	0x0000
85 		0x0a, 0x00, 0x00, 0x00,	// uint32	0x00000000
86 		0x00,
87 		0x0b, 0x00, 0x00, 0x00,	// uint64	0x0000000000000000
88 		0x00, 0x00, 0x00, 0x00,
89 		0x00,
90 		0x0c, 0x00, 0x44, 0x00,	// uint128	0x00440044004400440044004400440044
91 		0x44, 0x00, 0x44, 0x00,
92 		0x44, 0x00, 0x44, 0x00,
93 		0x44, 0x00, 0x44, 0x00,
94 		0x00,
95 		0x09, 0x00,		// uint16	<invalid>
96 	};
97 	sdp_data_t test = { data, data + sizeof(data) };
98 	sdp_data_t discard;
99 
100 	ATF_CHECK_EQ(sdp_data_type(&test), SDP_DATA_UINT8);
101 	ATF_REQUIRE(sdp_set_uint(&test, 0x44));
102 	ATF_CHECK_EQ(sdp_set_uint(&test, UINT8_MAX + 1), false);	/* too big */
103 	ATF_REQUIRE(sdp_get_data(&test, &discard));
104 
105 	ATF_CHECK_EQ(sdp_data_type(&test), SDP_DATA_NIL);
106 	ATF_CHECK_EQ(sdp_set_uint(&test, 0x00), false);			/* not uint */
107 	ATF_REQUIRE(sdp_get_data(&test, &discard));
108 
109 	ATF_CHECK_EQ(sdp_data_type(&test), SDP_DATA_UINT16);
110 	ATF_REQUIRE(sdp_set_uint(&test, 0xabcd));
111 	ATF_CHECK_EQ(sdp_set_uint(&test, UINT16_MAX + 1), false);	/* too big */
112 	ATF_REQUIRE(sdp_get_data(&test, &discard));
113 
114 	ATF_CHECK_EQ(sdp_data_type(&test), SDP_DATA_UINT32);
115 	ATF_REQUIRE(sdp_set_uint(&test, 0xdeadbeef));
116 	ATF_CHECK_EQ(sdp_set_uint(&test, (uintmax_t)UINT32_MAX + 1), false);	/* too big */
117 	ATF_REQUIRE(sdp_get_data(&test, &discard));
118 
119 	ATF_CHECK_EQ(sdp_data_type(&test), SDP_DATA_UINT64);
120 	ATF_REQUIRE(sdp_set_uint(&test, 0xc0ffeecafec0ffee));
121 	ATF_REQUIRE(sdp_get_data(&test, &discard));
122 
123 	ATF_CHECK_EQ(sdp_data_type(&test), SDP_DATA_UINT128);
124 	ATF_REQUIRE(sdp_set_uint(&test, 0xabcdef0123456789));
125 	ATF_REQUIRE(sdp_get_data(&test, &discard));
126 
127 	ATF_CHECK_EQ(sdp_data_type(&test), SDP_DATA_UINT16);
128 	ATF_CHECK_EQ(sdp_set_uint(&test, 0x3344), false);		/* no value */
129 
130 	const uint8_t expect[] = {
131 		0x08, 0x44,		// uint8	0x44
132 		0x00,			// nil
133 		0x09, 0xab, 0xcd,	// uint16	0xabcd
134 		0x0a, 0xde, 0xad, 0xbe,	// uint32	0xdeadbeef
135 		0xef,
136 		0x0b, 0xc0, 0xff, 0xee,	// uint64	0xc0ffeecafec0ffee
137 		0xca, 0xfe, 0xc0, 0xff,
138 		0xee,
139 		0x0c, 0x00, 0x00, 0x00,	// uint128	0x0000000000000000abcdef0123456789
140 		0x00, 0x00, 0x00, 0x00,
141 		0x00, 0xab, 0xcd, 0xef,
142 		0x01, 0x23, 0x45, 0x67,
143 		0x89,
144 		0x09, 0x00,		// uint16	<invalid>
145 	};
146 
147 	ATF_REQUIRE_EQ(sizeof(data), sizeof(expect));
148 	ATF_CHECK(memcmp(expect, data, sizeof(expect)) == 0);
149 }
150 
151 ATF_TC(check_sdp_set_int);
152 
153 ATF_TC_HEAD(check_sdp_set_int, tc)
154 {
155 
156 	atf_tc_set_md_var(tc, "descr", "Test sdp_set_int results");
157 }
158 
159 ATF_TC_BODY(check_sdp_set_int, tc)
160 {
161 	uint8_t data[] = {
162 		0x10, 0x00,		// int8		0
163 		0x00,			// nil
164 		0x11, 0x00, 0x00,	// int16	0
165 		0x12, 0x00, 0x00, 0x00,	// int32	0
166 		0x00,
167 		0x13, 0x00, 0x00, 0x00,	// int64	0
168 		0x00, 0x00, 0x00, 0x00,
169 		0x00,
170 		0x14, 0x00, 0x44, 0x00,	// int128	0x00440044004400440044004400440044
171 		0x44, 0x00, 0x44, 0x00,
172 		0x44, 0x00, 0x44, 0x00,
173 		0x44, 0x00, 0x44, 0x00,
174 		0x00,
175 		0x11, 0x00,		// int16	<invalid>
176 	};
177 	sdp_data_t test = { data, data + sizeof(data) };
178 	sdp_data_t discard;
179 
180 	ATF_CHECK_EQ(sdp_data_type(&test), SDP_DATA_INT8);
181 	ATF_REQUIRE(sdp_set_int(&test, -1));
182 	ATF_CHECK_EQ(sdp_set_int(&test, INT8_MAX + 1), false);	/* too big */
183 	ATF_REQUIRE(sdp_get_data(&test, &discard));
184 
185 	ATF_CHECK_EQ(sdp_data_type(&test), SDP_DATA_NIL);
186 	ATF_CHECK_EQ(sdp_set_int(&test, 33), false);		/* not int */
187 	ATF_REQUIRE(sdp_get_data(&test, &discard));
188 
189 	ATF_CHECK_EQ(sdp_data_type(&test), SDP_DATA_INT16);
190 	ATF_REQUIRE(sdp_set_int(&test, 789));
191 	ATF_CHECK_EQ(sdp_set_int(&test, INT16_MIN - 1), false);	/* too big */
192 	ATF_REQUIRE(sdp_get_data(&test, &discard));
193 
194 	ATF_CHECK_EQ(sdp_data_type(&test), SDP_DATA_INT32);
195 	ATF_REQUIRE(sdp_set_int(&test, -4567));
196 	ATF_CHECK_EQ(sdp_set_int(&test, (intmax_t)INT32_MAX + 1), false);	/* too big */
197 	ATF_REQUIRE(sdp_get_data(&test, &discard));
198 
199 	ATF_CHECK_EQ(sdp_data_type(&test), SDP_DATA_INT64);
200 	ATF_REQUIRE(sdp_set_int(&test, -3483738234));
201 	ATF_REQUIRE(sdp_get_data(&test, &discard));
202 
203 	ATF_CHECK_EQ(sdp_data_type(&test), SDP_DATA_INT128);
204 	ATF_REQUIRE(sdp_set_int(&test, 3423489463464));
205 	ATF_REQUIRE(sdp_get_data(&test, &discard));
206 
207 	ATF_CHECK_EQ(sdp_data_type(&test), SDP_DATA_INT16);
208 	ATF_CHECK_EQ(sdp_set_int(&test, 1234), false);		/* no value */
209 
210 	const uint8_t expect[] = {
211 		0x10, 0xff,		// int8		-1
212 		0x00,			// nil
213 		0x11, 0x03, 0x15,	// int16	789
214 		0x12, 0xff, 0xff, 0xee,	// int32	-4567
215 		0x29,
216 		0x13, 0xff, 0xff, 0xff,	// int64	-3483738234
217 		0xff, 0x30, 0x5a, 0x5f,
218 		0x86,
219 		0x14, 0x00, 0x00, 0x00,	// int128	3423489463464
220 		0x00, 0x00, 0x00, 0x00,
221 		0x00, 0x00, 0x00, 0x03,
222 		0x1d, 0x17, 0xdf, 0x94,
223 		0xa8,
224 		0x11, 0x00,		// int16	<invalid>
225 	};
226 
227 	ATF_REQUIRE_EQ(sizeof(data), sizeof(expect));
228 	ATF_CHECK(memcmp(expect, data, sizeof(expect)) == 0);
229 }
230 
231 ATF_TC(check_sdp_set_seq);
232 
233 ATF_TC_HEAD(check_sdp_set_seq, tc)
234 {
235 
236 	atf_tc_set_md_var(tc, "descr", "Test sdp_set_seq results");
237 }
238 
239 ATF_TC_BODY(check_sdp_set_seq, tc)
240 {
241 	uint8_t data[] = {
242 		0x35, 0x03,		// seq8(3)
243 		0x11, 0xff, 0xff,	//   int16	-1
244 		0x36, 0x01, 0x00,	// seq16(256)
245 		0x09, 0xff, 0xff,	// uint16	0xffff
246 		0x37, 0x01, 0x02, 0x03,	// seq32(16909060)
247 		0x04,
248 		0x36, 0x00,		// seq16(<invalid>)
249 	};
250 	sdp_data_t test = { data, data + sizeof(data) };
251 	sdp_data_t discard;
252 
253 	ATF_CHECK_EQ(sdp_data_type(&test), SDP_DATA_SEQ8);
254 	ATF_REQUIRE(sdp_set_seq(&test, 0));
255 	ATF_CHECK_EQ(sdp_set_seq(&test, UINT8_MAX), false);	/* data too big */
256 	ATF_CHECK_EQ(sdp_set_seq(&test, UINT16_MAX), false);	/* size too big */
257 	ATF_REQUIRE(sdp_get_data(&test, &discard));
258 
259 	ATF_CHECK_EQ(sdp_data_type(&test), SDP_DATA_INT16);
260 	ATF_CHECK_EQ(sdp_set_seq(&test, 33), false);		/* not seq */
261 	ATF_REQUIRE(sdp_get_data(&test, &discard));
262 
263 	ATF_CHECK_EQ(sdp_data_type(&test), SDP_DATA_SEQ16);
264 	ATF_REQUIRE(sdp_set_seq(&test, 3));
265 	ATF_CHECK_EQ(sdp_set_seq(&test, SSIZE_MAX), false);	/* size too big */
266 	ATF_REQUIRE(sdp_get_data(&test, &discard));
267 
268 	ATF_CHECK_EQ(sdp_data_type(&test), SDP_DATA_SEQ32);
269 	ATF_REQUIRE(sdp_set_seq(&test, 0));
270 	ATF_REQUIRE(sdp_get_data(&test, &discard));
271 
272 	ATF_CHECK_EQ(sdp_data_type(&test), SDP_DATA_SEQ16);
273 	ATF_CHECK_EQ(sdp_set_seq(&test, 22), false);		/* no size */
274 
275 	const uint8_t expect[] = {
276 		0x35, 0x00,		// seq8(0)
277 		0x11, 0xff, 0xff,	// int16	-1
278 		0x36, 0x00, 0x03,	// seq16(3)
279 		0x09, 0xff, 0xff,	//   uint16	0xffff
280 		0x37, 0x00, 0x00, 0x00,	// seq32(0)
281 		0x00,
282 		0x36, 0x00,		// seq16(<invalid>)
283 	};
284 
285 	ATF_REQUIRE_EQ(sizeof(data), sizeof(expect));
286 	ATF_CHECK(memcmp(expect, data, sizeof(expect)) == 0);
287 }
288 
289 ATF_TC(check_sdp_set_alt);
290 
291 ATF_TC_HEAD(check_sdp_set_alt, tc)
292 {
293 
294 	atf_tc_set_md_var(tc, "descr", "Test sdp_set_alt results");
295 }
296 
297 ATF_TC_BODY(check_sdp_set_alt, tc)
298 {
299 	uint8_t data[] = {
300 		0x3d, 0x06,		// alt8(6)
301 		0x11, 0xff, 0xff,	//   int16	-1
302 		0x3e, 0xff, 0xff,	//   alt16(65535)
303 		0x3f, 0x01, 0x02, 0x03,	// alt32(16909060)
304 		0x04,
305 		0x0a, 0x00, 0x00, 0x00,	// uint32	0x00000003
306 		0x03,
307 		0x3e, 0x00,		// alt16(<invalid>)
308 	};
309 	sdp_data_t test = { data, data + sizeof(data) };
310 	sdp_data_t discard;
311 
312 	ATF_CHECK_EQ(sdp_data_type(&test), SDP_DATA_ALT8);
313 	ATF_REQUIRE(sdp_set_alt(&test, 0));
314 	ATF_CHECK_EQ(sdp_set_alt(&test, UINT8_MAX), false);	/* data too big */
315 	ATF_CHECK_EQ(sdp_set_alt(&test, UINT16_MAX), false);	/* size too big */
316 	ATF_REQUIRE(sdp_get_data(&test, &discard));
317 
318 	ATF_CHECK_EQ(sdp_data_type(&test), SDP_DATA_INT16);
319 	ATF_CHECK_EQ(sdp_set_alt(&test, 27), false);		/* not alt */
320 	ATF_REQUIRE(sdp_get_data(&test, &discard));
321 
322 	ATF_CHECK_EQ(sdp_data_type(&test), SDP_DATA_ALT16);
323 	ATF_REQUIRE(sdp_set_alt(&test, 10));
324 	ATF_CHECK_EQ(sdp_set_alt(&test, SSIZE_MAX), false);	/* size too big */
325 	ATF_REQUIRE(sdp_get_alt(&test, &discard));
326 	ATF_CHECK_EQ(sdp_data_type(&discard), SDP_DATA_ALT32);
327 	ATF_CHECK(sdp_set_alt(&discard, -1));			/* end of alt16 */
328 	ATF_CHECK_EQ(sdp_set_alt(&discard, 6), false);		/* data too big */
329 
330 	ATF_CHECK_EQ(sdp_data_type(&test), SDP_DATA_ALT16);
331 	ATF_CHECK_EQ(sdp_set_alt(&test, 22), false);		/* no size */
332 
333 	const uint8_t expect[] = {
334 		0x3d, 0x00,		// alt8(0)
335 		0x11, 0xff, 0xff,	// int16	-1
336 		0x3e, 0x00, 0x0a,	// alt16(10)
337 		0x3f, 0x00, 0x00, 0x00,	//   alt32(5)
338 		0x05,
339 		0x0a, 0x00, 0x00, 0x00,	//     uint32	0x00000003
340 		0x03,
341 		0x3e, 0x00,		// alt16(<invalid>)
342 	};
343 
344 	ATF_REQUIRE_EQ(sizeof(data), sizeof(expect));
345 	ATF_CHECK(memcmp(expect, data, sizeof(expect)) == 0);
346 }
347 
348 
349 ATF_TP_ADD_TCS(tp)
350 {
351 
352 	ATF_TP_ADD_TC(tp, check_sdp_set_bool);
353 	ATF_TP_ADD_TC(tp, check_sdp_set_uint);
354 	ATF_TP_ADD_TC(tp, check_sdp_set_int);
355 	ATF_TP_ADD_TC(tp, check_sdp_set_seq);
356 	ATF_TP_ADD_TC(tp, check_sdp_set_alt);
357 
358 	return atf_no_error();
359 }
360