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