1/*	$NetBSD: t_sdp_put.c,v 1.3 2011/04/16 07:32:27 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
38ATF_TC(check_sdp_put_data);
39
40ATF_TC_HEAD(check_sdp_put_data, tc)
41{
42
43	atf_tc_set_md_var(tc, "descr", "Test sdp_put_data results");
44}
45
46ATF_TC_BODY(check_sdp_put_data, tc)
47{
48	uint8_t buf[256];
49	sdp_data_t test = { buf, buf + sizeof(buf) };
50	uint8_t data[] = {
51		0x35, 0x05,		// seq8(5)
52		0x08, 0x00,		//   uint8	0x00
53		0x09, 0x12, 0x34,	//   uint16	0x1234
54	};
55	sdp_data_t value = { data, data + sizeof(data) };
56
57	ATF_REQUIRE(sdp_put_data(&test, &value));
58	test.end = test.next;
59	test.next = buf;
60
61	const uint8_t expect[] = {
62		0x35, 0x05,		// seq8(5)
63		0x08, 0x00,		//   uint8	0x00
64		0x09, 0x12, 0x34,	//   uint16	0x1234
65	};
66
67	ATF_REQUIRE_EQ(test.end - test.next, sizeof(expect));
68	ATF_CHECK(memcmp(expect, test.next, sizeof(expect)) == 0);
69}
70
71ATF_TC(check_sdp_put_attr);
72
73ATF_TC_HEAD(check_sdp_put_attr, tc)
74{
75
76	atf_tc_set_md_var(tc, "descr", "Test sdp_put_attr results");
77}
78
79ATF_TC_BODY(check_sdp_put_attr, tc)
80{
81	uint8_t buf[256];
82	sdp_data_t test = { buf, buf + sizeof(buf) };
83	uint8_t data[] = {
84		0x00,			// nil
85		0x19, 0x33, 0x44,	// uuid16	0x3344
86	};
87	sdp_data_t value = { data, data + sizeof(data) };
88
89	ATF_REQUIRE_EQ(sdp_put_attr(&test, 0xabcd, &value), false);
90	value.next += 1; // skip "nil"
91	ATF_REQUIRE(sdp_put_attr(&test, 0x1337, &value));
92	test.end = test.next;
93	test.next = buf;
94
95	const uint8_t expect[] = {
96		0x09, 0x13, 0x37,	// uint16	0x1337
97		0x19, 0x33, 0x44,	// uuid16	0x3344
98	};
99
100	ATF_REQUIRE_EQ(test.end - test.next, sizeof(expect));
101	ATF_CHECK(memcmp(expect, test.next, sizeof(expect)) == 0);
102}
103
104ATF_TC(check_sdp_put_uuid);
105
106ATF_TC_HEAD(check_sdp_put_uuid, tc)
107{
108
109	atf_tc_set_md_var(tc, "descr", "Test sdp_put_uuid results");
110}
111
112ATF_TC_BODY(check_sdp_put_uuid, tc)
113{
114	uint8_t buf[256];
115	sdp_data_t test = { buf, buf + sizeof(buf) };
116	const uuid_t u16 = {
117		0x00001234,
118		0x0000,
119		0x1000,
120		0x80,
121		0x00,
122		{ 0x00, 0x80, 0x5f, 0x9b, 0x34, 0xfb }
123	};
124	const uuid_t u32 = {
125		0x12345678,
126		0x0000,
127		0x1000,
128		0x80,
129		0x00,
130		{ 0x00, 0x80, 0x5f, 0x9b, 0x34, 0xfb }
131	};
132	const uuid_t u128 = {
133		0x00112233,
134		0x4444,
135		0x5555,
136		0x66,
137		0x77,
138		{ 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd }
139	};
140
141	ATF_REQUIRE(sdp_put_uuid(&test, &u16));
142	ATF_REQUIRE(sdp_put_uuid(&test, &u32));
143	ATF_REQUIRE(sdp_put_uuid(&test, &u128));
144	test.end = test.next;
145	test.next = buf;
146
147	const uint8_t expect[] = {
148		0x19, 0x12, 0x34,	// uuid16	0x1234
149		0x1a, 0x12, 0x34, 0x56, // uuid32	0x12345678
150		0x78,
151		0x1c, 0x00, 0x11, 0x22,	// uuid128	00112233-4444-5555-6677-8899aabbccdd
152		0x33, 0x44, 0x44, 0x55,
153		0x55, 0x66, 0x77, 0x88,
154		0x99, 0xaa, 0xbb, 0xcc,
155		0xdd,
156	};
157
158	ATF_REQUIRE_EQ(test.end - test.next, sizeof(expect));
159	ATF_CHECK(memcmp(expect, test.next, sizeof(expect)) == 0);
160}
161
162ATF_TC(check_sdp_put_uuid16);
163
164ATF_TC_HEAD(check_sdp_put_uuid16, tc)
165{
166
167	atf_tc_set_md_var(tc, "descr", "Test sdp_put_uuid16 results");
168}
169
170ATF_TC_BODY(check_sdp_put_uuid16, tc)
171{
172	uint8_t buf[256];
173	sdp_data_t test = { buf, buf + sizeof(buf) };
174
175	ATF_REQUIRE(sdp_put_uuid16(&test, 0x4567));
176	test.end = test.next;
177	test.next = buf;
178
179	const uint8_t expect[] = {
180		0x19, 0x45, 0x67,	// uuid16	0x4567
181	};
182
183	ATF_REQUIRE_EQ(test.end - test.next, sizeof(expect));
184	ATF_CHECK(memcmp(expect, test.next, sizeof(expect)) == 0);
185}
186
187ATF_TC(check_sdp_put_uuid32);
188
189ATF_TC_HEAD(check_sdp_put_uuid32, tc)
190{
191
192	atf_tc_set_md_var(tc, "descr", "Test sdp_put_uuid32 results");
193}
194
195ATF_TC_BODY(check_sdp_put_uuid32, tc)
196{
197	uint8_t buf[256];
198	sdp_data_t test = { buf, buf + sizeof(buf) };
199
200	ATF_REQUIRE(sdp_put_uuid32(&test, 0xabcdef00));
201	test.end = test.next;
202	test.next = buf;
203
204	const uint8_t expect[] = {
205		0x1a, 0xab, 0xcd, 0xef, // uuid32	0xabcdef00
206		0x00,
207	};
208
209	ATF_REQUIRE_EQ(test.end - test.next, sizeof(expect));
210	ATF_CHECK(memcmp(expect, test.next, sizeof(expect)) == 0);
211}
212
213ATF_TC(check_sdp_put_uuid128);
214
215ATF_TC_HEAD(check_sdp_put_uuid128, tc)
216{
217
218	atf_tc_set_md_var(tc, "descr", "Test sdp_put_uuid128 results");
219}
220
221ATF_TC_BODY(check_sdp_put_uuid128, tc)
222{
223	uint8_t buf[256];
224	sdp_data_t test = { buf, buf + sizeof(buf) };
225	uuid_t value = {
226		0x00000100,
227		0x0000,
228		0x1000,
229		0x80,
230		0x00,
231		{ 0x00, 0x80, 0x5f, 0x9b, 0x34, 0xfb }
232	};
233
234	ATF_REQUIRE(sdp_put_uuid128(&test, &value));
235	test.end = test.next;
236	test.next = buf;
237
238	const uint8_t expect[] = {
239		0x1c, 0x00, 0x00, 0x01,	// uuid128	0000100-0000-1000-8000-00805f9b34fb
240		0x00, 0x00, 0x00, 0x10,	//			(L2CAP protocol)
241		0x00, 0x80, 0x00, 0x00,
242		0x80, 0x5f, 0x9b, 0x34,
243		0xfb,
244	};
245
246	ATF_REQUIRE_EQ(test.end - test.next, sizeof(expect));
247	ATF_CHECK(memcmp(expect, test.next, sizeof(expect)) == 0);
248}
249
250ATF_TC(check_sdp_put_bool);
251
252ATF_TC_HEAD(check_sdp_put_bool, tc)
253{
254
255	atf_tc_set_md_var(tc, "descr", "Test sdp_put_bool results");
256}
257
258ATF_TC_BODY(check_sdp_put_bool, tc)
259{
260	uint8_t buf[256];
261	sdp_data_t test = { buf, buf + sizeof(buf) };
262
263	ATF_REQUIRE(sdp_put_bool(&test, true));
264	ATF_REQUIRE(sdp_put_bool(&test, false));
265	test.end = test.next;
266	test.next = buf;
267
268	const uint8_t expect[] = {
269		0x28, 0x01,		// bool	true
270		0x28, 0x00,		// bool	false
271	};
272
273	ATF_REQUIRE_EQ(test.end - test.next, sizeof(expect));
274	ATF_CHECK(memcmp(expect, test.next, sizeof(expect)) == 0);
275}
276
277ATF_TC(check_sdp_put_uint);
278
279ATF_TC_HEAD(check_sdp_put_uint, tc)
280{
281
282	atf_tc_set_md_var(tc, "descr", "Test sdp_put_uint results");
283}
284
285ATF_TC_BODY(check_sdp_put_uint, tc)
286{
287	uint8_t buf[256];
288	sdp_data_t test = { buf, buf + sizeof(buf) };
289
290	ATF_REQUIRE(sdp_put_uint(&test, (uintmax_t)0));
291	ATF_REQUIRE(sdp_put_uint(&test, (uintmax_t)UINT8_MAX));
292	ATF_REQUIRE(sdp_put_uint(&test, (uintmax_t)UINT8_MAX + 1));
293	ATF_REQUIRE(sdp_put_uint(&test, (uintmax_t)UINT16_MAX));
294	ATF_REQUIRE(sdp_put_uint(&test, (uintmax_t)UINT16_MAX + 1));
295	ATF_REQUIRE(sdp_put_uint(&test, (uintmax_t)UINT32_MAX));
296	ATF_REQUIRE(sdp_put_uint(&test, (uintmax_t)UINT32_MAX + 1));
297	ATF_REQUIRE(sdp_put_uint(&test, (uintmax_t)UINT64_MAX));
298	test.end = test.next;
299	test.next = buf;
300
301	const uint8_t expect[] = {
302		0x08, 0x00,		// uint8	0x00
303		0x08, 0xff,		// uint8	0xff
304		0x09, 0x01, 0x00,	// uint16	0x0100
305		0x09, 0xff, 0xff,	// uint16	0xffff
306		0x0a, 0x00, 0x01, 0x00,	// uint32	0x00010000
307		0x00,
308		0x0a, 0xff, 0xff, 0xff,	// uint32	0xffffffff
309		0xff,
310		0x0b, 0x00, 0x00, 0x00,	// uint64	0x0000000100000000
311		0x01, 0x00, 0x00, 0x00,
312		0x00,
313		0x0b, 0xff, 0xff, 0xff,	// uint64	0xffffffffffffffff
314		0xff, 0xff, 0xff, 0xff,
315		0xff,
316	};
317
318	ATF_REQUIRE_EQ(test.end - test.next, sizeof(expect));
319	ATF_CHECK(memcmp(expect, test.next, sizeof(expect)) == 0);
320}
321
322ATF_TC(check_sdp_put_uint8);
323
324ATF_TC_HEAD(check_sdp_put_uint8, tc)
325{
326
327	atf_tc_set_md_var(tc, "descr", "Test sdp_put_uint8 results");
328}
329
330ATF_TC_BODY(check_sdp_put_uint8, tc)
331{
332	uint8_t buf[256];
333	sdp_data_t test = { buf, buf + sizeof(buf) };
334
335	ATF_REQUIRE(sdp_put_uint8(&test, (uint8_t)0));
336	ATF_REQUIRE(sdp_put_uint8(&test, (uint8_t)UINT8_MAX));
337	test.end = test.next;
338	test.next = buf;
339
340	const uint8_t expect[] = {
341		0x08, 0x00,		// uint8	0x00
342		0x08, 0xff,		// uint8	0xff
343	};
344
345	ATF_REQUIRE_EQ(test.end - test.next, sizeof(expect));
346	ATF_CHECK(memcmp(expect, test.next, sizeof(expect)) == 0);
347}
348
349ATF_TC(check_sdp_put_uint16);
350
351ATF_TC_HEAD(check_sdp_put_uint16, tc)
352{
353
354	atf_tc_set_md_var(tc, "descr", "Test sdp_put_uint16 results");
355}
356
357ATF_TC_BODY(check_sdp_put_uint16, tc)
358{
359	uint8_t buf[256];
360	sdp_data_t test = { buf, buf + sizeof(buf) };
361
362	ATF_REQUIRE(sdp_put_uint16(&test, (uint16_t)0));
363	ATF_REQUIRE(sdp_put_uint16(&test, (uint16_t)UINT8_MAX));
364	ATF_REQUIRE(sdp_put_uint16(&test, (uint16_t)UINT16_MAX));
365	ATF_REQUIRE(sdp_put_uint16(&test, (uint16_t)0xabcd));
366	test.end = test.next;
367	test.next = buf;
368
369	const uint8_t expect[] = {
370		0x09, 0x00, 0x00,	// uint16	0x0000
371		0x09, 0x00, 0xff,	// uint16	0x00ff
372		0x09, 0xff, 0xff,	// uint16	0xffff
373		0x09, 0xab, 0xcd,	// uint16	0xabcd
374	};
375
376	ATF_REQUIRE_EQ(test.end - test.next, sizeof(expect));
377	ATF_CHECK(memcmp(expect, test.next, sizeof(expect)) == 0);
378}
379
380ATF_TC(check_sdp_put_uint32);
381
382ATF_TC_HEAD(check_sdp_put_uint32, tc)
383{
384
385	atf_tc_set_md_var(tc, "descr", "Test sdp_put_uint32 results");
386}
387
388ATF_TC_BODY(check_sdp_put_uint32, tc)
389{
390	uint8_t buf[256];
391	sdp_data_t test = { buf, buf + sizeof(buf) };
392
393	ATF_REQUIRE(sdp_put_uint32(&test, (uint32_t)0));
394	ATF_REQUIRE(sdp_put_uint32(&test, (uint32_t)UINT8_MAX));
395	ATF_REQUIRE(sdp_put_uint32(&test, (uint32_t)UINT16_MAX));
396	ATF_REQUIRE(sdp_put_uint32(&test, (uint32_t)UINT32_MAX));
397	ATF_REQUIRE(sdp_put_uint32(&test, (uint32_t)0xdeadbeef));
398	test.end = test.next;
399	test.next = buf;
400
401	const uint8_t expect[] = {
402		0x0a, 0x00, 0x00, 0x00,	// uint32	0x00000000
403		0x00,
404		0x0a, 0x00, 0x00, 0x00,	// uint32	0x000000ff
405		0xff,
406		0x0a, 0x00, 0x00, 0xff,	// uint32	0x0000ffff
407		0xff,
408		0x0a, 0xff, 0xff, 0xff,	// uint32	0xffffffff
409		0xff,
410		0x0a, 0xde, 0xad, 0xbe,	// uint32	0xdeadbeef
411		0xef,
412	};
413
414	ATF_REQUIRE_EQ(test.end - test.next, sizeof(expect));
415	ATF_CHECK(memcmp(expect, test.next, sizeof(expect)) == 0);
416}
417
418ATF_TC(check_sdp_put_uint64);
419
420ATF_TC_HEAD(check_sdp_put_uint64, tc)
421{
422
423	atf_tc_set_md_var(tc, "descr", "Test sdp_put_uint64 results");
424}
425
426ATF_TC_BODY(check_sdp_put_uint64, tc)
427{
428	uint8_t buf[256];
429	sdp_data_t test = { buf, buf + sizeof(buf) };
430
431	ATF_REQUIRE(sdp_put_uint64(&test, (uint64_t)0));
432	ATF_REQUIRE(sdp_put_uint64(&test, (uint64_t)UINT8_MAX));
433	ATF_REQUIRE(sdp_put_uint64(&test, (uint64_t)UINT16_MAX));
434	ATF_REQUIRE(sdp_put_uint64(&test, (uint64_t)UINT32_MAX));
435	ATF_REQUIRE(sdp_put_uint64(&test, (uint64_t)UINT64_MAX));
436	ATF_REQUIRE(sdp_put_uint64(&test, (uint64_t)0xc0ffeecafec0ffee));
437	test.end = test.next;
438	test.next = buf;
439
440	const uint8_t expect[] = {
441		0x0b, 0x00, 0x00, 0x00,	// uint64	0x0000000000000000
442		0x00, 0x00, 0x00, 0x00,
443		0x00,
444		0x0b, 0x00, 0x00, 0x00,	// uint64	0x00000000000000ff
445		0x00, 0x00, 0x00, 0x00,
446		0xff,
447		0x0b, 0x00, 0x00, 0x00,	// uint64	0x000000000000ffff
448		0x00, 0x00, 0x00, 0xff,
449		0xff,
450		0x0b, 0x00, 0x00, 0x00,	// uint64	0x00000000ffffffff
451		0x00, 0xff, 0xff, 0xff,
452		0xff,
453		0x0b, 0xff, 0xff, 0xff,	// uint64	0xffffffffffffffff
454		0xff, 0xff, 0xff, 0xff,
455		0xff,
456		0x0b, 0xc0, 0xff, 0xee,	// uint64	0xc0ffeecafec0ffee
457		0xca, 0xfe, 0xc0, 0xff,
458		0xee,
459	};
460
461	ATF_REQUIRE_EQ(test.end - test.next, sizeof(expect));
462	ATF_CHECK(memcmp(expect, test.next, sizeof(expect)) == 0);
463}
464
465ATF_TC(check_sdp_put_int);
466
467ATF_TC_HEAD(check_sdp_put_int, tc)
468{
469
470	atf_tc_set_md_var(tc, "descr", "Test sdp_put_int results");
471}
472
473ATF_TC_BODY(check_sdp_put_int, tc)
474{
475	uint8_t buf[256];
476	sdp_data_t test = { buf, buf + sizeof(buf) };
477
478	ATF_REQUIRE(sdp_put_int(&test, (intmax_t)0));
479	ATF_REQUIRE(sdp_put_int(&test, (intmax_t)INT8_MIN));
480	ATF_REQUIRE(sdp_put_int(&test, (intmax_t)INT8_MAX));
481	ATF_REQUIRE(sdp_put_int(&test, (intmax_t)INT8_MIN - 1));
482	ATF_REQUIRE(sdp_put_int(&test, (intmax_t)INT8_MAX + 1));
483	ATF_REQUIRE(sdp_put_int(&test, (intmax_t)INT16_MIN));
484	ATF_REQUIRE(sdp_put_int(&test, (intmax_t)INT16_MAX));
485	ATF_REQUIRE(sdp_put_int(&test, (intmax_t)INT16_MIN - 1));
486	ATF_REQUIRE(sdp_put_int(&test, (intmax_t)INT16_MAX + 1));
487	ATF_REQUIRE(sdp_put_int(&test, (intmax_t)INT32_MIN));
488	ATF_REQUIRE(sdp_put_int(&test, (intmax_t)INT32_MAX));
489	ATF_REQUIRE(sdp_put_int(&test, (intmax_t)INT32_MIN - 1));
490	ATF_REQUIRE(sdp_put_int(&test, (intmax_t)INT32_MAX + 1));
491	ATF_REQUIRE(sdp_put_int(&test, (intmax_t)INT64_MIN));
492	ATF_REQUIRE(sdp_put_int(&test, (intmax_t)INT64_MAX));
493	test.end = test.next;
494	test.next = buf;
495
496	const uint8_t expect[] = {
497		0x10, 0x00,		// int8		0
498		0x10, 0x80,		// int8		-128
499		0x10, 0x7f,		// int8		127
500		0x11, 0xff, 0x7f,	// int16	-129
501		0x11, 0x00, 0x80,	// int16	128
502		0x11, 0x80, 0x00,	// int16	-32768
503		0x11, 0x7f, 0xff,	// int16	32767
504		0x12, 0xff, 0xff, 0x7f,	// int32	-32769
505		0xff,
506		0x12, 0x00, 0x00, 0x80,	// int32	32768
507		0x00,
508		0x12, 0x80, 0x00, 0x00,	// int32	-2147483648
509		0x00,
510		0x12, 0x7f, 0xff, 0xff,	// int32	2147483647
511		0xff,
512		0x13, 0xff, 0xff, 0xff,	// int64	-2147483649
513		0xff, 0x7f, 0xff, 0xff,
514		0xff,
515		0x13, 0x00, 0x00, 0x00,	// int64	2147483648
516		0x00, 0x80, 0x00, 0x00,
517		0x00,
518		0x13, 0x80, 0x00, 0x00,	// int64	-9223372036854775808
519		0x00, 0x00, 0x00, 0x00,
520		0x00,
521		0x13, 0x7f, 0xff, 0xff,	// int64	9223372036854775807
522		0xff, 0xff, 0xff, 0xff,
523		0xff,
524	};
525
526	ATF_REQUIRE_EQ(test.end - test.next, sizeof(expect));
527	ATF_CHECK(memcmp(expect, test.next, sizeof(expect)) == 0);
528}
529
530ATF_TC(check_sdp_put_int8);
531
532ATF_TC_HEAD(check_sdp_put_int8, tc)
533{
534
535	atf_tc_set_md_var(tc, "descr", "Test sdp_put_int8 results");
536}
537
538ATF_TC_BODY(check_sdp_put_int8, tc)
539{
540	uint8_t buf[256];
541	sdp_data_t test = { buf, buf + sizeof(buf) };
542
543	ATF_REQUIRE(sdp_put_int8(&test, (int8_t)0));
544	ATF_REQUIRE(sdp_put_int8(&test, (int8_t)INT8_MIN));
545	ATF_REQUIRE(sdp_put_int8(&test, (int8_t)INT8_MAX));
546	test.end = test.next;
547	test.next = buf;
548
549	const uint8_t expect[] = {
550		0x10, 0x00,		// int8		0
551		0x10, 0x80,		// int8		-128
552		0x10, 0x7f,		// int8		127
553	};
554
555	ATF_REQUIRE_EQ(test.end - test.next, sizeof(expect));
556	ATF_CHECK(memcmp(expect, test.next, sizeof(expect)) == 0);
557}
558
559ATF_TC(check_sdp_put_int16);
560
561ATF_TC_HEAD(check_sdp_put_int16, tc)
562{
563
564	atf_tc_set_md_var(tc, "descr", "Test sdp_put_int16 results");
565}
566
567ATF_TC_BODY(check_sdp_put_int16, tc)
568{
569	uint8_t buf[256];
570	sdp_data_t test = { buf, buf + sizeof(buf) };
571
572	ATF_REQUIRE(sdp_put_int16(&test, (int16_t)0));
573	ATF_REQUIRE(sdp_put_int16(&test, (int16_t)INT8_MIN));
574	ATF_REQUIRE(sdp_put_int16(&test, (int16_t)INT8_MAX));
575	ATF_REQUIRE(sdp_put_int16(&test, (int16_t)INT16_MIN));
576	ATF_REQUIRE(sdp_put_int16(&test, (int16_t)INT16_MAX));
577	test.end = test.next;
578	test.next = buf;
579
580	const uint8_t expect[] = {
581		0x11, 0x00, 0x00,	// int16	0
582		0x11, 0xff, 0x80,	// int16	-128
583		0x11, 0x00, 0x7f,	// int16	127
584		0x11, 0x80, 0x00,	// int16	-32768
585		0x11, 0x7f, 0xff,	// int16	32767
586	};
587
588	ATF_REQUIRE_EQ(test.end - test.next, sizeof(expect));
589	ATF_CHECK(memcmp(expect, test.next, sizeof(expect)) == 0);
590}
591
592ATF_TC(check_sdp_put_int32);
593
594ATF_TC_HEAD(check_sdp_put_int32, tc)
595{
596
597	atf_tc_set_md_var(tc, "descr", "Test sdp_put_int32 results");
598}
599
600ATF_TC_BODY(check_sdp_put_int32, tc)
601{
602	uint8_t buf[256];
603	sdp_data_t test = { buf, buf + sizeof(buf) };
604
605	ATF_REQUIRE(sdp_put_int32(&test, (int32_t)0));
606	ATF_REQUIRE(sdp_put_int32(&test, (int32_t)INT8_MIN));
607	ATF_REQUIRE(sdp_put_int32(&test, (int32_t)INT8_MAX));
608	ATF_REQUIRE(sdp_put_int32(&test, (int32_t)INT16_MIN));
609	ATF_REQUIRE(sdp_put_int32(&test, (int32_t)INT16_MAX));
610	ATF_REQUIRE(sdp_put_int32(&test, (int32_t)INT32_MIN));
611	ATF_REQUIRE(sdp_put_int32(&test, (int32_t)INT32_MAX));
612	test.end = test.next;
613	test.next = buf;
614
615	const uint8_t expect[] = {
616		0x12, 0x00, 0x00, 0x00,	// int32	0
617		0x00,
618		0x12, 0xff, 0xff, 0xff,	// int32	-128
619		0x80,
620		0x12, 0x00, 0x00, 0x00,	// int32	127
621		0x7f,
622		0x12, 0xff, 0xff, 0x80,	// int32	-32768
623		0x00,
624		0x12, 0x00, 0x00, 0x7f,	// int32	32767
625		0xff,
626		0x12, 0x80, 0x00, 0x00,	// int32	-2147483648
627		0x00,
628		0x12, 0x7f, 0xff, 0xff,	// int32	2147483647
629		0xff,
630	};
631
632	ATF_REQUIRE_EQ(test.end - test.next, sizeof(expect));
633	ATF_CHECK(memcmp(expect, test.next, sizeof(expect)) == 0);
634}
635
636ATF_TC(check_sdp_put_int64);
637
638ATF_TC_HEAD(check_sdp_put_int64, tc)
639{
640
641	atf_tc_set_md_var(tc, "descr", "Test sdp_put_int64 results");
642}
643
644ATF_TC_BODY(check_sdp_put_int64, tc)
645{
646	uint8_t buf[256];
647	sdp_data_t test = { buf, buf + sizeof(buf) };
648
649	ATF_REQUIRE(sdp_put_int64(&test, (int64_t)0));
650	ATF_REQUIRE(sdp_put_int64(&test, (int64_t)INT8_MIN));
651	ATF_REQUIRE(sdp_put_int64(&test, (int64_t)INT8_MAX));
652	ATF_REQUIRE(sdp_put_int64(&test, (int64_t)INT16_MIN));
653	ATF_REQUIRE(sdp_put_int64(&test, (int64_t)INT16_MAX));
654	ATF_REQUIRE(sdp_put_int64(&test, (int64_t)INT32_MIN));
655	ATF_REQUIRE(sdp_put_int64(&test, (int64_t)INT32_MAX));
656	ATF_REQUIRE(sdp_put_int64(&test, (int64_t)INT64_MIN));
657	ATF_REQUIRE(sdp_put_int64(&test, (int64_t)INT64_MAX));
658	test.end = test.next;
659	test.next = buf;
660
661	const uint8_t expect[] = {
662		0x13, 0x00, 0x00, 0x00,	// int64	0
663		0x00, 0x00, 0x00, 0x00,
664		0x00,
665		0x13, 0xff, 0xff, 0xff,	// int64	-128
666		0xff, 0xff, 0xff, 0xff,
667		0x80,
668		0x13, 0x00, 0x00, 0x00,	// int64	127
669		0x00, 0x00, 0x00, 0x00,
670		0x7f,
671		0x13, 0xff, 0xff, 0xff,	// int64	-32768
672		0xff, 0xff, 0xff, 0x80,
673		0x00,
674		0x13, 0x00, 0x00, 0x00,	// int64	32767
675		0x00, 0x00, 0x00, 0x7f,
676		0xff,
677		0x13, 0xff, 0xff, 0xff,	// int64	-2147483648
678		0xff, 0x80, 0x00, 0x00,
679		0x00,
680		0x13, 0x00, 0x00, 0x00,	// int64	2147483647
681		0x00, 0x7f, 0xff, 0xff,
682		0xff,
683		0x13, 0x80, 0x00, 0x00,	// int64	-9223372036854775808
684		0x00, 0x00, 0x00, 0x00,
685		0x00,
686		0x13, 0x7f, 0xff, 0xff,	// int64	9223372036854775807
687		0xff, 0xff, 0xff, 0xff,
688		0xff,
689	};
690
691	ATF_REQUIRE_EQ(test.end - test.next, sizeof(expect));
692	ATF_CHECK(memcmp(expect, test.next, sizeof(expect)) == 0);
693}
694
695ATF_TC(check_sdp_put_seq);
696
697ATF_TC_HEAD(check_sdp_put_seq, tc)
698{
699
700	atf_tc_set_md_var(tc, "descr", "Test sdp_put_seq results");
701}
702
703ATF_TC_BODY(check_sdp_put_seq, tc)
704{
705	uint8_t buf[512];
706	sdp_data_t test = { buf, buf + sizeof(buf) };
707
708	ATF_REQUIRE(sdp_put_seq(&test, (ssize_t)0));
709	ATF_REQUIRE(sdp_put_seq(&test, (ssize_t)UINT8_MAX));
710	ATF_REQUIRE(sdp_put_seq(&test, (ssize_t)UINT8_MAX + 1));
711	ATF_REQUIRE(sdp_put_seq(&test, (ssize_t)-1));
712	ATF_CHECK_EQ(sdp_put_seq(&test, (ssize_t)UINT16_MAX), false);	/* no room */
713	ATF_CHECK_EQ(sdp_put_seq(&test, (ssize_t)SSIZE_MAX), false);	/* no room */
714	test.end = test.next;
715	test.next = buf;
716
717	/* (not a valid element list) */
718	const uint8_t expect[] = {
719		0x35, 0x00,		// seq8(0)
720		0x35, 0xff,		// seq8(255)
721		0x36, 0x01, 0x00,	// seq16(256)
722		0x36, 0x01, 0xf6,	// seq16(502)	<- sizeof(buf) - 7 - 3
723	};
724
725	ATF_REQUIRE_EQ(test.end - test.next, sizeof(expect));
726	ATF_CHECK(memcmp(expect, test.next, sizeof(expect)) == 0);
727}
728
729ATF_TC(check_sdp_put_alt);
730
731ATF_TC_HEAD(check_sdp_put_alt, tc)
732{
733
734	atf_tc_set_md_var(tc, "descr", "Test sdp_put_alt results");
735}
736
737ATF_TC_BODY(check_sdp_put_alt, tc)
738{
739	uint8_t buf[512];
740	sdp_data_t test = { buf, buf + sizeof(buf) };
741
742	ATF_REQUIRE(sdp_put_alt(&test, (ssize_t)0));
743	ATF_REQUIRE(sdp_put_alt(&test, (ssize_t)UINT8_MAX));
744	ATF_REQUIRE(sdp_put_alt(&test, (ssize_t)UINT8_MAX + 1));
745	ATF_REQUIRE(sdp_put_alt(&test, (ssize_t)-1));
746	ATF_CHECK_EQ(sdp_put_alt(&test, (ssize_t)UINT16_MAX), false);	/* no room */
747	ATF_CHECK_EQ(sdp_put_alt(&test, (ssize_t)SSIZE_MAX), false);	/* no room */
748	test.end = test.next;
749	test.next = buf;
750
751	/* (not a valid element list) */
752	const uint8_t expect[] = {
753		0x3d, 0x00,		// alt8(0)
754		0x3d, 0xff,		// alt8(255)
755		0x3e, 0x01, 0x00,	// alt16(256)
756		0x3e, 0x01, 0xf6,	// alt16(502)	<- sizeof(buf) - 7 - 3
757	};
758
759	ATF_REQUIRE_EQ(test.end - test.next, sizeof(expect));
760	ATF_CHECK(memcmp(expect, test.next, sizeof(expect)) == 0);
761}
762
763ATF_TC(check_sdp_put_str);
764
765ATF_TC_HEAD(check_sdp_put_str, tc)
766{
767
768	atf_tc_set_md_var(tc, "descr", "Test sdp_put_str results");
769}
770
771ATF_TC_BODY(check_sdp_put_str, tc)
772{
773	uint8_t buf[512];
774	sdp_data_t test = { buf, buf + sizeof(buf) };
775
776	/*
777	 * this does not test str16 or str32, but that is
778	 * handled by the same code as sdp_put_seq above..
779	 */
780
781	ATF_REQUIRE(sdp_put_str(&test, "Hello World!", 5));
782	ATF_REQUIRE(sdp_put_str(&test, "Hello\0World", 11));
783	ATF_REQUIRE(sdp_put_str(&test, "Hello World!", -1));
784	ATF_REQUIRE(sdp_put_str(&test, "Hello\0World", -1));
785	test.end = test.next;
786	test.next = buf;
787
788	const uint8_t expect[] = {
789		0x25, 0x05, 0x48, 0x65,	// str8		"Hello"
790		0x6c, 0x6c, 0x6f,
791		0x25, 0x0b, 0x48, 0x65,	// str8		"Hello\0World"
792		0x6c, 0x6c, 0x6f, 0x00,
793		0x57, 0x6f, 0x72, 0x6c,
794		0x64,
795		0x25, 0x0c, 0x48, 0x65,	// str8		"Hello World!"
796		0x6c, 0x6c, 0x6f, 0x20,
797		0x57, 0x6f, 0x72, 0x6c,
798		0x64, 0x21,
799		0x25, 0x05, 0x48, 0x65,	// str8		"Hello"
800		0x6c, 0x6c, 0x6f,
801	};
802
803	ATF_REQUIRE_EQ(test.end - test.next, sizeof(expect));
804	ATF_CHECK(memcmp(expect, test.next, sizeof(expect)) == 0);
805}
806
807ATF_TC(check_sdp_put_url);
808
809ATF_TC_HEAD(check_sdp_put_url, tc)
810{
811
812	atf_tc_set_md_var(tc, "descr", "Test sdp_put_url results");
813}
814
815ATF_TC_BODY(check_sdp_put_url, tc)
816{
817	uint8_t buf[512];
818	sdp_data_t test = { buf, buf + sizeof(buf) };
819
820	/*
821	 * this does not test url16 or url32, but that is
822	 * handled by the same code as sdp_put_seq above..
823	 */
824
825	ATF_REQUIRE(sdp_put_url(&test, "http://www.netbsd.org/", 21));
826	ATF_REQUIRE(sdp_put_url(&test, "http://www.netbsd.org/", -1));
827	test.end = test.next;
828	test.next = buf;
829
830	const uint8_t expect[] = {
831		0x45, 0x15, 0x68, 0x74,	// url8	"http://www.netbsd.org"
832		0x74, 0x70, 0x3a, 0x2f,
833		0x2f, 0x77, 0x77, 0x77,
834		0x2e, 0x6e, 0x65, 0x74,
835		0x62, 0x73, 0x64, 0x2e,
836		0x6f, 0x72, 0x67,
837		0x45, 0x16, 0x68, 0x74,	// url8	"http://www.netbsd.org/"
838		0x74, 0x70, 0x3a, 0x2f,
839		0x2f, 0x77, 0x77, 0x77,
840		0x2e, 0x6e, 0x65, 0x74,
841		0x62, 0x73, 0x64, 0x2e,
842		0x6f, 0x72, 0x67, 0x2f,
843	};
844
845	ATF_REQUIRE_EQ(test.end - test.next, sizeof(expect));
846	ATF_CHECK(memcmp(expect, test.next, sizeof(expect)) == 0);
847}
848
849ATF_TP_ADD_TCS(tp)
850{
851
852	ATF_TP_ADD_TC(tp, check_sdp_put_data);
853	ATF_TP_ADD_TC(tp, check_sdp_put_attr);
854	ATF_TP_ADD_TC(tp, check_sdp_put_uuid);
855	ATF_TP_ADD_TC(tp, check_sdp_put_uuid16);
856	ATF_TP_ADD_TC(tp, check_sdp_put_uuid32);
857	ATF_TP_ADD_TC(tp, check_sdp_put_uuid128);
858	ATF_TP_ADD_TC(tp, check_sdp_put_bool);
859	ATF_TP_ADD_TC(tp, check_sdp_put_uint);
860	ATF_TP_ADD_TC(tp, check_sdp_put_uint8);
861	ATF_TP_ADD_TC(tp, check_sdp_put_uint16);
862	ATF_TP_ADD_TC(tp, check_sdp_put_uint32);
863	ATF_TP_ADD_TC(tp, check_sdp_put_uint64);
864	ATF_TP_ADD_TC(tp, check_sdp_put_int);
865	ATF_TP_ADD_TC(tp, check_sdp_put_int8);
866	ATF_TP_ADD_TC(tp, check_sdp_put_int16);
867	ATF_TP_ADD_TC(tp, check_sdp_put_int32);
868	ATF_TP_ADD_TC(tp, check_sdp_put_int64);
869	ATF_TP_ADD_TC(tp, check_sdp_put_seq);
870	ATF_TP_ADD_TC(tp, check_sdp_put_alt);
871	ATF_TP_ADD_TC(tp, check_sdp_put_str);
872	ATF_TP_ADD_TC(tp, check_sdp_put_url);
873
874	return atf_no_error();
875}
876