nv_tests.cc revision 279434
1/*-
2 * Copyright (c) 2014-2015 Sandvine Inc.  All rights reserved.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
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 *
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24 * SUCH DAMAGE.
25 */
26
27#include <sys/cdefs.h>
28__FBSDID("$FreeBSD: head/lib/libnv/tests/nv_tests.cc 279434 2015-03-01 00:22:23Z rstone $");
29
30#include <atf-c++.hpp>
31#include <nv.h>
32
33#include <errno.h>
34#include <limits>
35#include <set>
36#include <sstream>
37#include <string>
38
39/*
40 * Test that a newly created nvlist has no errors, and is empty.
41 */
42ATF_TEST_CASE_WITHOUT_HEAD(nvlist_create__is_empty);
43ATF_TEST_CASE_BODY(nvlist_create__is_empty)
44{
45	nvlist_t *nvl;
46	int type;
47	void *it;
48
49	nvl = nvlist_create(0);
50
51	ATF_REQUIRE(nvl != NULL);
52
53	ATF_REQUIRE_EQ(nvlist_error(nvl), 0);
54	ATF_REQUIRE(nvlist_empty(nvl));
55
56	it = NULL;
57	ATF_REQUIRE_EQ(nvlist_next(nvl, &type, &it), NULL);
58
59	nvlist_destroy(nvl);
60}
61
62ATF_TEST_CASE_WITHOUT_HEAD(nvlist_add_null__single_insert);
63ATF_TEST_CASE_BODY(nvlist_add_null__single_insert)
64{
65	nvlist_t *nvl;
66	void *it;
67	const char *key;
68	int type;
69
70	key = "key";
71	nvl = nvlist_create(0);
72
73	ATF_REQUIRE(nvl != NULL);
74	ATF_REQUIRE(!nvlist_exists(nvl, key));
75
76	nvlist_add_null(nvl, key);
77
78	ATF_REQUIRE(!nvlist_empty(nvl));
79	ATF_REQUIRE(nvlist_exists(nvl, key));
80	ATF_REQUIRE(nvlist_existsf(nvl, "%s", key));
81	ATF_REQUIRE(nvlist_exists_null(nvl, key));
82	ATF_REQUIRE(nvlist_existsf_null(nvl, "key"));
83
84	/* Iterate over the nvlist; ensure that it has only our one key. */
85	it = NULL;
86	ATF_REQUIRE_EQ(strcmp(nvlist_next(nvl, &type, &it), key), 0);
87	ATF_REQUIRE_EQ(type, NV_TYPE_NULL);
88	ATF_REQUIRE_EQ(nvlist_next(nvl, &type,&it), NULL);
89
90	nvlist_destroy(nvl);
91}
92
93ATF_TEST_CASE_WITHOUT_HEAD(nvlist_add_bool__single_insert);
94ATF_TEST_CASE_BODY(nvlist_add_bool__single_insert)
95{
96	nvlist_t *nvl;
97	void *it;
98	const char *key;
99	int type;
100
101	key = "name";
102	nvl = nvlist_create(0);
103
104	ATF_REQUIRE(nvl != NULL);
105	ATF_REQUIRE(!nvlist_exists(nvl, key));
106
107	nvlist_add_bool(nvl, key, true);
108
109	ATF_REQUIRE(!nvlist_empty(nvl));
110	ATF_REQUIRE(nvlist_exists(nvl, key));
111	ATF_REQUIRE(nvlist_existsf(nvl, "%s%s", "na", "me"));
112	ATF_REQUIRE(nvlist_exists_bool(nvl, key));
113	ATF_REQUIRE(nvlist_existsf_bool(nvl, "%s%c", "nam", 'e'));
114	ATF_REQUIRE_EQ(nvlist_get_bool(nvl, key), true);
115	ATF_REQUIRE_EQ(nvlist_getf_bool(nvl, "%c%s", 'n', "ame"), true);
116
117	/* Iterate over the nvlist; ensure that it has only our one key. */
118	it = NULL;
119	ATF_REQUIRE_EQ(strcmp(nvlist_next(nvl, &type, &it), key), 0);
120	ATF_REQUIRE_EQ(type, NV_TYPE_BOOL);
121	ATF_REQUIRE_EQ(nvlist_next(nvl, &type,&it), NULL);
122
123	nvlist_destroy(nvl);
124}
125
126ATF_TEST_CASE_WITHOUT_HEAD(nvlist_add_number__single_insert);
127ATF_TEST_CASE_BODY(nvlist_add_number__single_insert)
128{
129	nvlist_t *nvl;
130	void *it;
131	const char *key;
132	uint64_t value;
133	int type;
134
135	key = "foo123";
136	value = 71965;
137	nvl = nvlist_create(0);
138
139	ATF_REQUIRE(nvl != NULL);
140	ATF_REQUIRE(!nvlist_exists(nvl, key));
141
142	nvlist_add_number(nvl, key, value);
143
144	ATF_REQUIRE(!nvlist_empty(nvl));
145	ATF_REQUIRE(nvlist_exists(nvl, key));
146	ATF_REQUIRE(nvlist_existsf(nvl, "%s%d", "foo", 123));
147	ATF_REQUIRE(nvlist_exists_number(nvl, key));
148	ATF_REQUIRE(nvlist_existsf_number(nvl, "%s", key));
149	ATF_REQUIRE_EQ(nvlist_get_number(nvl, key), value);
150	ATF_REQUIRE_EQ(nvlist_getf_number(nvl, "%s", key), value);
151
152	/* Iterate over the nvlist; ensure that it has only our one key. */
153	it = NULL;
154	ATF_REQUIRE_EQ(strcmp(nvlist_next(nvl, &type, &it), key), 0);
155	ATF_REQUIRE_EQ(type, NV_TYPE_NUMBER);
156	ATF_REQUIRE_EQ(nvlist_next(nvl, &type,&it), NULL);
157
158	nvlist_destroy(nvl);
159}
160
161ATF_TEST_CASE_WITHOUT_HEAD(nvlist_add_string__single_insert);
162ATF_TEST_CASE_BODY(nvlist_add_string__single_insert)
163{
164	nvlist_t *nvl;
165	void *it;
166	const char *key;
167	const char *value;
168	int type;
169
170	key = "test";
171	value = "fgjdkgjdk";
172	nvl = nvlist_create(0);
173
174	ATF_REQUIRE(nvl != NULL);
175	ATF_REQUIRE(!nvlist_exists(nvl, key));
176
177	nvlist_add_string(nvl, key, value);
178
179	ATF_REQUIRE(!nvlist_empty(nvl));
180	ATF_REQUIRE(nvlist_exists(nvl, key));
181	ATF_REQUIRE(nvlist_existsf(nvl, "%s", key));
182	ATF_REQUIRE(nvlist_exists_string(nvl, key));
183	ATF_REQUIRE(nvlist_existsf_string(nvl, "%s", key));
184	ATF_REQUIRE_EQ(strcmp(nvlist_get_string(nvl, key), value), 0);
185	ATF_REQUIRE_EQ(strcmp(nvlist_getf_string(nvl, "%s", key), value), 0);
186
187	/* nvlist_add_* is required to clone the value, so check for that. */
188	ATF_REQUIRE(nvlist_get_string(nvl, key) != value);
189
190	/* Iterate over the nvlist; ensure that it has only our one key. */
191	it = NULL;
192	ATF_REQUIRE_EQ(strcmp(nvlist_next(nvl, &type, &it), key), 0);
193	ATF_REQUIRE_EQ(type, NV_TYPE_STRING);
194	ATF_REQUIRE_EQ(nvlist_next(nvl, &type,&it), NULL);
195
196	nvlist_destroy(nvl);
197}
198
199ATF_TEST_CASE_WITHOUT_HEAD(nvlist_add_nvlist__single_insert);
200ATF_TEST_CASE_BODY(nvlist_add_nvlist__single_insert)
201{
202	nvlist_t *nvl;
203	void *it;
204	const char *key, *subkey;
205	nvlist_t *sublist;
206	const nvlist_t *value;
207	int type;
208
209	key = "test";
210	subkey = "subkey";
211	sublist = nvlist_create(0);
212	nvl = nvlist_create(0);
213
214	ATF_REQUIRE(nvl != NULL);
215	ATF_REQUIRE(!nvlist_exists(nvl, key));
216
217	nvlist_add_null(sublist, subkey);
218	nvlist_add_nvlist(nvl, key, sublist);
219
220	ATF_REQUIRE(!nvlist_empty(nvl));
221	ATF_REQUIRE(nvlist_exists(nvl, key));
222	ATF_REQUIRE(nvlist_existsf(nvl, "%s", key));
223	ATF_REQUIRE(nvlist_exists_nvlist(nvl, key));
224	ATF_REQUIRE(nvlist_existsf_nvlist(nvl, "%s", key));
225
226	value = nvlist_get_nvlist(nvl, key);
227	ATF_REQUIRE(nvlist_exists_null(value, subkey));
228
229	/* nvlist_add_* is required to clone the value, so check for that. */
230	ATF_REQUIRE(sublist != value);
231
232	value = nvlist_getf_nvlist(nvl, "%s", key);
233	ATF_REQUIRE(nvlist_exists_null(value, subkey));
234	ATF_REQUIRE(sublist != value);
235
236	/* Iterate over the nvlist; ensure that it has only our one key. */
237	it = NULL;
238	ATF_REQUIRE_EQ(strcmp(nvlist_next(nvl, &type, &it), key), 0);
239	ATF_REQUIRE_EQ(type, NV_TYPE_NVLIST);
240	ATF_REQUIRE_EQ(nvlist_next(nvl, &type,&it), NULL);
241
242	nvlist_destroy(sublist);
243	nvlist_destroy(nvl);
244}
245
246ATF_TEST_CASE_WITHOUT_HEAD(nvlist_add_binary__single_insert);
247ATF_TEST_CASE_BODY(nvlist_add_binary__single_insert)
248{
249	nvlist_t *nvl;
250	void *it;
251	const char *key;
252	void *value;
253	const void *ret_value;
254	size_t value_size, ret_size;
255	int type;
256
257	key = "binary";
258	value_size = 13;
259	value = malloc(value_size);
260	memset(value, 0xa5, value_size);
261	nvl = nvlist_create(0);
262
263	ATF_REQUIRE(nvl != NULL);
264	ATF_REQUIRE(!nvlist_exists(nvl, key));
265
266	nvlist_add_binary(nvl, key, value, value_size);
267
268	ATF_REQUIRE(!nvlist_empty(nvl));
269	ATF_REQUIRE(nvlist_exists(nvl, key));
270	ATF_REQUIRE(nvlist_existsf(nvl, "%s", key));
271	ATF_REQUIRE(nvlist_exists_binary(nvl, key));
272	ATF_REQUIRE(nvlist_existsf_binary(nvl, "%s", key));
273
274	ret_value = nvlist_get_binary(nvl, key, &ret_size);
275	ATF_REQUIRE_EQ(value_size, ret_size);
276	ATF_REQUIRE_EQ(memcmp(value, ret_value, ret_size), 0);
277
278	/* nvlist_add_* is required to clone the value, so check for that. */
279	ATF_REQUIRE(value != ret_value);
280
281	ret_value = nvlist_getf_binary(nvl, &ret_size, "%s", key);
282	ATF_REQUIRE_EQ(value_size, ret_size);
283	ATF_REQUIRE_EQ(memcmp(value, ret_value, ret_size), 0);
284	ATF_REQUIRE(value != ret_value);
285
286	/* Iterate over the nvlist; ensure that it has only our one key. */
287	it = NULL;
288	ATF_REQUIRE_EQ(strcmp(nvlist_next(nvl, &type, &it), key), 0);
289	ATF_REQUIRE_EQ(type, NV_TYPE_BINARY);
290	ATF_REQUIRE_EQ(nvlist_next(nvl, &type,&it), NULL);
291
292	nvlist_destroy(nvl);
293	free(value);
294}
295
296ATF_TEST_CASE_WITHOUT_HEAD(nvlist_clone__empty_nvlist);
297ATF_TEST_CASE_BODY(nvlist_clone__empty_nvlist)
298{
299	nvlist_t *nvl, *clone;
300
301	nvl = nvlist_create(0);
302	ATF_REQUIRE(nvl != NULL);
303
304	clone = nvlist_clone(nvl);
305	ATF_REQUIRE(clone != NULL);
306	ATF_REQUIRE(clone != nvl);
307	ATF_REQUIRE(nvlist_empty(clone));
308
309	nvlist_destroy(clone);
310	nvlist_destroy(nvl);
311}
312
313ATF_TEST_CASE_WITHOUT_HEAD(nvlist_clone__nonempty_nvlist);
314ATF_TEST_CASE_BODY(nvlist_clone__nonempty_nvlist)
315{
316	nvlist_t *nvl, *clone;
317	const char *key;
318	void *it;
319	uint64_t value;
320	int type;
321
322	nvl = nvlist_create(0);
323	ATF_REQUIRE(nvl != NULL);
324
325	key = "testkey";
326	value = 684874;
327	nvlist_add_number(nvl, key, value);
328
329	clone = nvlist_clone(nvl);
330	ATF_REQUIRE(clone != NULL);
331	ATF_REQUIRE(clone != nvl);
332	ATF_REQUIRE(nvlist_exists_number(clone, key));
333	ATF_REQUIRE_EQ(nvlist_get_number(clone, key), value);
334
335	/* Iterate over the nvlist; ensure that it has only our one key. */
336	it = NULL;
337	ATF_REQUIRE_EQ(strcmp(nvlist_next(clone, &type, &it), key), 0);
338	ATF_REQUIRE_EQ(type, NV_TYPE_NUMBER);
339	ATF_REQUIRE_EQ(nvlist_next(clone, &type, &it), NULL);
340
341	nvlist_destroy(clone);
342	nvlist_destroy(nvl);
343}
344
345static const char * const test_subnvlist_key = "nvlist";
346
347static const char * const test_string_key = "string";
348static const char * const test_string_val = "59525";
349
350static nvlist_t*
351create_test_nvlist(void)
352{
353	nvlist_t *nvl, *sublist;
354
355	nvl = nvlist_create(0);
356	ATF_REQUIRE(nvl != NULL);
357
358	sublist = nvlist_create(0);
359	ATF_REQUIRE(sublist != NULL);
360
361	nvlist_add_string(sublist, test_string_key, test_string_val);
362	nvlist_move_nvlist(nvl, test_subnvlist_key, sublist);
363
364	return (nvl);
365}
366
367static void
368verify_test_nvlist(const nvlist_t *nvl)
369{
370	void *it;
371	const nvlist_t *value;
372	int type;
373
374	ATF_REQUIRE(nvlist_exists_nvlist(nvl, test_subnvlist_key));
375
376	value = nvlist_get_nvlist(nvl, test_subnvlist_key);
377
378	ATF_REQUIRE(nvlist_exists_string(value, test_string_key));
379	ATF_REQUIRE_EQ(strcmp(nvlist_get_string(value, test_string_key), test_string_val), 0);
380	ATF_REQUIRE(nvlist_get_string(value, test_string_key) != test_string_val);
381
382	/* Iterate over both nvlists; ensure that each has only the one key. */
383	it = NULL;
384	ATF_REQUIRE_EQ(strcmp(nvlist_next(value, &type, &it),
385	    test_string_key), 0);
386	ATF_REQUIRE_EQ(type, NV_TYPE_STRING);
387	ATF_REQUIRE_EQ(nvlist_next(value, &type, &it), NULL);
388
389	it = NULL;
390	ATF_REQUIRE_EQ(strcmp(nvlist_next(nvl, &type, &it),
391	    test_subnvlist_key), 0);
392	ATF_REQUIRE_EQ(type, NV_TYPE_NVLIST);
393	ATF_REQUIRE_EQ(nvlist_next(nvl, &type, &it), NULL);
394}
395
396ATF_TEST_CASE_WITHOUT_HEAD(nvlist_clone__nested_nvlist);
397ATF_TEST_CASE_BODY(nvlist_clone__nested_nvlist)
398{
399	nvlist_t *nvl, *clone;
400
401	nvl = create_test_nvlist();
402	clone = nvlist_clone(nvl);
403
404	ATF_REQUIRE(clone != NULL);
405	ATF_REQUIRE(clone != nvl);
406	verify_test_nvlist(clone);
407
408	nvlist_destroy(clone);
409	nvlist_destroy(nvl);
410}
411
412ATF_TEST_CASE_WITHOUT_HEAD(nvlist_clone__error_nvlist);
413ATF_TEST_CASE_BODY(nvlist_clone__error_nvlist)
414{
415	nvlist_t *nvl, *clone;
416
417	nvl = nvlist_create(0);
418	ATF_REQUIRE(nvl != NULL);
419
420	nvlist_set_error(nvl, ENOMEM);
421
422	clone = nvlist_clone(nvl);
423	ATF_REQUIRE(clone == NULL);
424
425	nvlist_destroy(nvl);
426}
427
428ATF_TEST_CASE_WITHOUT_HEAD(nvlist_pack__empty_nvlist);
429ATF_TEST_CASE_BODY(nvlist_pack__empty_nvlist)
430{
431	nvlist_t *nvl, *unpacked;
432	void *packed;
433	size_t packed_size;
434
435	nvl = nvlist_create(0);
436	ATF_REQUIRE(nvl != NULL);
437
438	packed = nvlist_pack(nvl, &packed_size);
439	ATF_REQUIRE(packed != NULL);
440
441	unpacked = nvlist_unpack(packed, packed_size);
442	ATF_REQUIRE(unpacked != NULL);
443	ATF_REQUIRE(unpacked != nvl);
444	ATF_REQUIRE(nvlist_empty(unpacked));
445
446	nvlist_destroy(unpacked);
447	nvlist_destroy(nvl);
448	free(packed);
449}
450
451static void
452verify_null(const nvlist_t *nvl, int type)
453{
454
455	ATF_REQUIRE_EQ(type, NV_TYPE_NULL);
456}
457
458static void
459verify_number(const nvlist_t *nvl, const char *name, int type, uint64_t value)
460{
461
462	ATF_REQUIRE_EQ(type, NV_TYPE_NUMBER);
463	ATF_REQUIRE_EQ(nvlist_get_number(nvl, name), value);
464}
465
466static void
467verify_string(const nvlist_t *nvl, const char *name, int type,
468    const char * value)
469{
470
471	ATF_REQUIRE_EQ(type, NV_TYPE_STRING);
472	ATF_REQUIRE_EQ(strcmp(nvlist_get_string(nvl, name), value), 0);
473}
474
475static void
476verify_nvlist(const nvlist_t *nvl, const char *name, int type)
477{
478
479	ATF_REQUIRE_EQ(type, NV_TYPE_NVLIST);
480	verify_test_nvlist(nvlist_get_nvlist(nvl, name));
481}
482
483static void
484verify_binary(const nvlist_t *nvl, const char *name, int type,
485    const void * value, size_t size)
486{
487	const void *actual_value;
488	size_t actual_size;
489
490	ATF_REQUIRE_EQ(type, NV_TYPE_BINARY);
491	actual_value = nvlist_get_binary(nvl, name, &actual_size);
492	ATF_REQUIRE_EQ(size, actual_size);
493	ATF_REQUIRE_EQ(memcmp(value, actual_value, size), 0);
494}
495
496ATF_TEST_CASE_WITHOUT_HEAD(nvlist_pack__multiple_values);
497ATF_TEST_CASE_BODY(nvlist_pack__multiple_values)
498{
499	std::ostringstream msg;
500	std::set<std::string> keys_seen;
501	nvlist_t *nvl, *unpacked, *nvvalue;
502	const char *nullkey, *numkey, *strkey, *nvkey, *binkey, *name;
503	int numvalue;
504	const char * strvalue;
505	void *binvalue, *packed, *it;
506	size_t binsize, packed_size;
507	int type;
508
509	nvl = nvlist_create(0);
510
511	nullkey = "null";
512	nvlist_add_null(nvl, nullkey);
513
514	numkey = "number";
515	numvalue = 939853984;
516	nvlist_add_number(nvl, numkey, numvalue);
517
518	strkey = "string";
519	strvalue = "jfieutijf";
520	nvlist_add_string(nvl, strkey, strvalue);
521
522	nvkey = "nvlist";
523	nvvalue = create_test_nvlist();
524	nvlist_move_nvlist(nvl, nvkey, nvvalue);
525
526	binkey = "binary";
527	binsize = 4;
528	binvalue = malloc(binsize);
529	memset(binvalue, 'b', binsize);
530	nvlist_move_binary(nvl, binkey, binvalue, binsize);
531
532	packed = nvlist_pack(nvl, &packed_size);
533	ATF_REQUIRE(packed != NULL);
534
535	unpacked = nvlist_unpack(packed, packed_size);
536	ATF_REQUIRE(unpacked != 0);
537
538	it = NULL;
539	while ((name = nvlist_next(unpacked, &type, &it)) != NULL) {
540		/* Ensure that we see every key only once. */
541		ATF_REQUIRE_EQ(keys_seen.count(name), 0);
542
543		if (strcmp(name, nullkey) == 0)
544			verify_null(unpacked, type);
545		else if (strcmp(name, numkey) == 0)
546			verify_number(unpacked, name, type, numvalue);
547		else if (strcmp(name, strkey) == 0)
548			verify_string(unpacked, name, type, strvalue);
549		else if (strcmp(name, nvkey) == 0)
550			verify_nvlist(unpacked, name, type);
551		else if (strcmp(name, binkey) == 0)
552			verify_binary(unpacked, name, type, binvalue, binsize);
553		else {
554			msg << "Unexpected key :'" << name << "'";
555			ATF_FAIL(msg.str().c_str());
556		}
557
558		keys_seen.insert(name);
559	}
560
561	/* Ensure that we saw every key. */
562	ATF_REQUIRE_EQ(keys_seen.size(), 5);
563
564	nvlist_destroy(nvl);
565	nvlist_destroy(unpacked);
566	free(packed);
567}
568
569ATF_TEST_CASE_WITHOUT_HEAD(nvlist_pack__error_nvlist);
570ATF_TEST_CASE_BODY(nvlist_pack__error_nvlist)
571{
572	nvlist_t *nvl;
573	void *packed;
574	size_t size;
575
576	nvl = nvlist_create(0);
577	ATF_REQUIRE(nvl != NULL);
578
579	nvlist_set_error(nvl, ENOMEM);
580
581	packed = nvlist_pack(nvl, &size);
582	ATF_REQUIRE(packed == NULL);
583
584	nvlist_destroy(nvl);
585}
586
587ATF_TEST_CASE_WITHOUT_HEAD(nvlist_unpack__duplicate_key);
588ATF_TEST_CASE_BODY(nvlist_unpack__duplicate_key)
589{
590	nvlist_t *nvl, *unpacked;
591	const char *key1, *key2;
592	void *packed, *keypos;
593	size_t size, keylen;
594
595	nvl = nvlist_create(0);
596
597	key1 = "key1";
598	keylen = strlen(key1);
599	nvlist_add_number(nvl, key1, 5);
600
601	key2 = "key2";
602	ATF_REQUIRE_EQ(keylen, strlen(key2));
603	nvlist_add_number(nvl, key2, 10);
604
605	packed = nvlist_pack(nvl, &size);
606
607	/*
608	 * Mangle the packed nvlist by replacing key1 with key2, creating a
609	 * packed nvlist with a duplicate key.
610	 */
611	keypos = memmem(packed, size, key1, keylen);
612	ATF_REQUIRE(keypos != NULL);
613	memcpy(keypos, key2, keylen);
614
615	unpacked = nvlist_unpack(packed, size);
616	ATF_REQUIRE(nvlist_error(unpacked) != 0);
617
618	free(packed);
619	nvlist_destroy(nvl);
620	nvlist_destroy(unpacked);
621}
622
623ATF_TEST_CASE_WITHOUT_HEAD(nvlist_move_string__single_insert);
624ATF_TEST_CASE_BODY(nvlist_move_string__single_insert)
625{
626	nvlist_t *nvl;
627	const char *key;
628	char *value;
629
630	nvl = nvlist_create(0);
631	ATF_REQUIRE(nvl != NULL);
632
633	key = "testkey";
634	value = strdup("testval");
635	ATF_REQUIRE(value != NULL);
636
637	nvlist_move_string(nvl, key, value);
638	ATF_REQUIRE_EQ(nvlist_get_string(nvl, key), value);
639
640	nvlist_destroy(nvl);
641}
642
643ATF_TEST_CASE_WITHOUT_HEAD(nvlist_move_nvlist__null_child);
644ATF_TEST_CASE_BODY(nvlist_move_nvlist__null_child)
645{
646	nvlist_t *parent;
647
648	parent = nvlist_create(0);
649
650	nvlist_move_nvlist(parent, "test", NULL);
651
652	ATF_REQUIRE(nvlist_error(parent) != 0);
653
654	nvlist_destroy(parent);
655}
656
657ATF_TEST_CASE_WITHOUT_HEAD(nvlist_move_nvlist__single_insert);
658ATF_TEST_CASE_BODY(nvlist_move_nvlist__single_insert)
659{
660	nvlist_t *nvl;
661	const char *key;
662	nvlist_t *value;
663
664	nvl = nvlist_create(0);
665	ATF_REQUIRE(nvl != NULL);
666
667	key = "testkey";
668	value = nvlist_create(0);
669	ATF_REQUIRE(value != NULL);
670
671	nvlist_move_nvlist(nvl, key, value);
672	ATF_REQUIRE_EQ(nvlist_get_nvlist(nvl, key), value);
673
674	nvlist_destroy(nvl);
675}
676
677ATF_TEST_CASE_WITHOUT_HEAD(nvlist_move_binary__single_insert);
678ATF_TEST_CASE_BODY(nvlist_move_binary__single_insert)
679{
680	nvlist_t *nvl;
681	const char *key;
682	void *value;
683	size_t size, actual_size;
684
685	nvl = nvlist_create(0);
686	ATF_REQUIRE(nvl != NULL);
687
688	key = "testkey";
689	size = 73;
690	value = malloc(size);
691	ATF_REQUIRE(value != NULL);
692
693	nvlist_move_binary(nvl, key, value, size);
694	ATF_REQUIRE_EQ(nvlist_get_binary(nvl, key, &actual_size), value);
695	ATF_REQUIRE_EQ(size, actual_size);
696
697	nvlist_destroy(nvl);
698}
699
700ATF_TEST_CASE_WITHOUT_HEAD(nvlist_take_bool__single_remove);
701ATF_TEST_CASE_BODY(nvlist_take_bool__single_remove)
702{
703	nvlist_t *nvl;
704	const char *testkey;
705	bool testval;
706
707	nvl = nvlist_create(0);
708	ATF_REQUIRE(nvl != NULL);
709
710	testkey = "boolkey";
711	testval = false;
712	nvlist_add_bool(nvl, testkey, testval);
713
714	ATF_REQUIRE_EQ(nvlist_take_bool(nvl, testkey), testval);
715	ATF_REQUIRE(nvlist_empty(nvl));
716
717	nvlist_destroy(nvl);
718}
719
720ATF_TEST_CASE_WITHOUT_HEAD(nvlist_take_bool__other_keys_unchanged);
721ATF_TEST_CASE_BODY(nvlist_take_bool__other_keys_unchanged)
722{
723	nvlist_t *nvl;
724	const char *testkey, *otherkey1, *otherkey2;
725	bool testval, otherval1;
726	nvlist_t *otherval2;
727
728	nvl = nvlist_create(0);
729	ATF_REQUIRE(nvl != NULL);
730
731	testkey = "boolkey";
732	testval = true;
733	nvlist_add_bool(nvl, testkey, testval);
734
735	otherkey1 = "key1";
736	otherval1 = false;
737	nvlist_add_bool(nvl, otherkey1, otherval1);
738
739	otherkey2 = "key2";
740	otherval2 = create_test_nvlist();
741	nvlist_move_nvlist(nvl, otherkey2, otherval2);
742
743	ATF_REQUIRE_EQ(nvlist_take_bool(nvl, testkey), testval);
744
745	ATF_REQUIRE(nvlist_exists_bool(nvl, otherkey1));
746	ATF_REQUIRE_EQ(nvlist_get_bool(nvl, otherkey1), otherval1);
747
748	ATF_REQUIRE(nvlist_exists_nvlist(nvl, otherkey2));
749	verify_test_nvlist(nvlist_get_nvlist(nvl, otherkey2));
750
751	nvlist_destroy(nvl);
752}
753
754ATF_TEST_CASE_WITHOUT_HEAD(nvlist_take_number__single_remove);
755ATF_TEST_CASE_BODY(nvlist_take_number__single_remove)
756{
757	nvlist_t *nvl;
758	const char *testkey;
759	uint64_t testval;
760
761	nvl = nvlist_create(0);
762	ATF_REQUIRE(nvl != NULL);
763
764	testkey = "numkey";
765	testval = std::numeric_limits<uint64_t>::max();
766	nvlist_add_number(nvl, testkey, testval);
767
768	ATF_REQUIRE_EQ(nvlist_take_number(nvl, testkey), testval);
769	ATF_REQUIRE(nvlist_empty(nvl));
770
771	nvlist_destroy(nvl);
772}
773
774ATF_TEST_CASE_WITHOUT_HEAD(nvlist_take_number__other_keys_unchanged);
775ATF_TEST_CASE_BODY(nvlist_take_number__other_keys_unchanged)
776{
777	nvlist_t *nvl;
778	const char *testkey, *otherkey1, *otherkey2;
779	uint64_t testval, otherval1;
780	const char *otherval2;
781
782	nvl = nvlist_create(0);
783	ATF_REQUIRE(nvl != NULL);
784
785	otherkey1 = "key1";
786	otherval1 = 5;
787	nvlist_add_number(nvl, otherkey1, otherval1);
788
789	testkey = "numkey";
790	testval = 1654;
791	nvlist_add_number(nvl, testkey, testval);
792
793	otherkey2 = "key2";
794	otherval2 = "string";
795	nvlist_add_string(nvl, otherkey2, otherval2);
796
797	ATF_REQUIRE_EQ(nvlist_take_number(nvl, testkey), testval);
798
799	ATF_REQUIRE(nvlist_exists_number(nvl, otherkey1));
800	ATF_REQUIRE_EQ(nvlist_get_number(nvl, otherkey1), otherval1);
801
802	ATF_REQUIRE(nvlist_exists_string(nvl, otherkey2));
803	ATF_REQUIRE_EQ(strcmp(nvlist_get_string(nvl, otherkey2), otherval2), 0);
804
805	nvlist_destroy(nvl);
806}
807
808ATF_TEST_CASE_WITHOUT_HEAD(nvlist_take_string__single_remove);
809ATF_TEST_CASE_BODY(nvlist_take_string__single_remove)
810{
811	nvlist_t *nvl;
812	const char *testkey;
813	const char *testval;
814
815	nvl = nvlist_create(0);
816	ATF_REQUIRE(nvl != NULL);
817
818	testkey = "numkey";
819	testval = "nvlist";
820	nvlist_add_string(nvl, testkey, testval);
821
822	ATF_REQUIRE_EQ(strcmp(nvlist_take_string(nvl, testkey), testval), 0);
823	ATF_REQUIRE(nvlist_empty(nvl));
824
825	nvlist_destroy(nvl);
826}
827
828ATF_TEST_CASE_WITHOUT_HEAD(nvlist_take_string__other_keys_unchanged);
829ATF_TEST_CASE_BODY(nvlist_take_string__other_keys_unchanged)
830{
831	nvlist_t *nvl;
832	const char *testkey, *otherkey1, *otherkey2;
833	const char *testval, *otherval1;
834	bool otherval2;
835
836	nvl = nvlist_create(0);
837	ATF_REQUIRE(nvl != NULL);
838
839	otherkey1 = "key1";
840	otherval1 = "fjdifjdk";
841	nvlist_add_string(nvl, otherkey1, otherval1);
842
843	otherkey2 = "key2";
844	otherval2 = true;
845	nvlist_add_bool(nvl, otherkey2, otherval2);
846
847	testkey = "strkey";
848	testval = "1654";
849	nvlist_add_string(nvl, testkey, testval);
850
851	ATF_REQUIRE_EQ(strcmp(nvlist_take_string(nvl, testkey), testval), 0);
852
853	ATF_REQUIRE(nvlist_exists_string(nvl, otherkey1));
854	ATF_REQUIRE_EQ(strcmp(nvlist_get_string(nvl, otherkey1), otherval1), 0);
855
856	ATF_REQUIRE(nvlist_exists_bool(nvl, otherkey2));
857	ATF_REQUIRE_EQ(nvlist_get_bool(nvl, otherkey2), otherval2);
858
859	nvlist_destroy(nvl);
860}
861
862ATF_TEST_CASE_WITHOUT_HEAD(nvlist_take_nvlist__single_remove);
863ATF_TEST_CASE_BODY(nvlist_take_nvlist__single_remove)
864{
865	nvlist_t *nvl;
866	const char *testkey;
867	nvlist_t *testval;
868
869	nvl = nvlist_create(0);
870	ATF_REQUIRE(nvl != NULL);
871
872	testkey = "numkey";
873	testval = create_test_nvlist();
874	nvlist_move_nvlist(nvl, testkey, testval);
875
876	verify_test_nvlist(nvlist_take_nvlist(nvl, testkey));
877	ATF_REQUIRE(nvlist_empty(nvl));
878
879	nvlist_destroy(nvl);
880}
881
882ATF_TEST_CASE_WITHOUT_HEAD(nvlist_take_nvlist__other_keys_unchanged);
883ATF_TEST_CASE_BODY(nvlist_take_nvlist__other_keys_unchanged)
884{
885	nvlist_t *nvl;
886	const char *testkey, *otherkey1, *otherkey2;
887	nvlist_t *testval, *otherval1;
888
889	nvl = nvlist_create(0);
890	ATF_REQUIRE(nvl != NULL);
891
892	testkey = "strkey";
893	testval = create_test_nvlist();
894	nvlist_move_nvlist(nvl, testkey, testval);
895
896	otherkey1 = "key1";
897	otherval1 = nvlist_create(0);
898	nvlist_move_nvlist(nvl, otherkey1, otherval1);
899
900	otherkey2 = "key2";
901	nvlist_add_null(nvl, otherkey2);
902
903	verify_test_nvlist(nvlist_take_nvlist(nvl, testkey));
904
905	ATF_REQUIRE(nvlist_exists_nvlist(nvl, otherkey1));
906	ATF_REQUIRE(nvlist_empty(nvlist_get_nvlist(nvl, otherkey1)));
907
908	ATF_REQUIRE(nvlist_exists_null(nvl, otherkey2));
909
910	nvlist_destroy(nvl);
911}
912
913ATF_TEST_CASE_WITHOUT_HEAD(nvlist_take_binary__single_remove);
914ATF_TEST_CASE_BODY(nvlist_take_binary__single_remove)
915{
916	nvlist_t *nvl;
917	const char *testkey;
918	void *testval;
919	const void *actual_val;
920	size_t testsize, actual_size;
921
922	nvl = nvlist_create(0);
923	ATF_REQUIRE(nvl != NULL);
924
925	testkey = "numkey";
926	testsize = 457;
927	testval = malloc(testsize);
928	memset(testval, '5', testsize);
929	nvlist_move_binary(nvl, testkey, testval, testsize);
930
931	actual_val = nvlist_take_binary(nvl, testkey, &actual_size);
932	ATF_REQUIRE_EQ(testsize, actual_size);
933	ATF_REQUIRE_EQ(memcmp(actual_val, testval, testsize), 0);
934	ATF_REQUIRE(nvlist_empty(nvl));
935
936	nvlist_destroy(nvl);
937}
938
939ATF_TEST_CASE_WITHOUT_HEAD(nvlist_take_binary__other_keys_unchanged);
940ATF_TEST_CASE_BODY(nvlist_take_binary__other_keys_unchanged)
941{
942	nvlist_t *nvl;
943	const char *testkey, *otherkey1, *otherkey2;
944	const void *actual_value;
945	char testval[] = "gjiertj";
946	char otherval1[] = "fdreg";
947	size_t testsize, othersize, actual_size;
948	bool otherval2;
949
950	nvl = nvlist_create(0);
951	ATF_REQUIRE(nvl != NULL);
952
953	otherkey1 = "key1";
954	othersize = sizeof(otherval1);
955	nvlist_add_binary(nvl, otherkey1, otherval1, othersize);
956
957	otherkey2 = "key2";
958	otherval2 = true;
959	nvlist_add_bool(nvl, otherkey2, otherval2);
960
961	testkey = "strkey";
962	testsize = sizeof(testval);
963	nvlist_add_binary(nvl, testkey, testval, testsize);
964
965	actual_value = nvlist_take_binary(nvl, testkey, &actual_size);
966	ATF_REQUIRE_EQ(testsize, actual_size);
967	ATF_REQUIRE_EQ(memcmp(actual_value, testval, testsize), 0);
968
969	ATF_REQUIRE(nvlist_exists_binary(nvl, otherkey1));
970	actual_value = nvlist_get_binary(nvl, otherkey1, &actual_size);
971	ATF_REQUIRE_EQ(othersize, actual_size);
972	ATF_REQUIRE_EQ(memcmp(actual_value, otherval1, othersize), 0);
973
974	ATF_REQUIRE(nvlist_exists_bool(nvl, otherkey2));
975	ATF_REQUIRE_EQ(nvlist_get_bool(nvl, otherkey2), otherval2);
976
977	nvlist_destroy(nvl);
978}
979
980ATF_TEST_CASE_WITHOUT_HEAD(nvlist_free__single_null);
981ATF_TEST_CASE_BODY(nvlist_free__single_null)
982{
983	nvlist_t *nvl;
984	const char *key;
985
986	nvl = nvlist_create(0);
987	key = "test";
988	nvlist_add_null(nvl, key);
989
990	nvlist_free(nvl, key);
991	ATF_REQUIRE(nvlist_empty(nvl));
992
993	nvlist_destroy(nvl);
994}
995
996ATF_TEST_CASE_WITHOUT_HEAD(nvlist_free__single_bool);
997ATF_TEST_CASE_BODY(nvlist_free__single_bool)
998{
999	nvlist_t *nvl;
1000	const char *key;
1001
1002	nvl = nvlist_create(0);
1003	key = "test";
1004	nvlist_add_bool(nvl, key, true);
1005
1006	nvlist_free(nvl, key);
1007	ATF_REQUIRE(nvlist_empty(nvl));
1008
1009	nvlist_destroy(nvl);
1010}
1011
1012ATF_TEST_CASE_WITHOUT_HEAD(nvlist_free__single_number);
1013ATF_TEST_CASE_BODY(nvlist_free__single_number)
1014{
1015	nvlist_t *nvl;
1016	const char *key;
1017
1018	nvl = nvlist_create(0);
1019	key = "test";
1020	nvlist_add_number(nvl, key, 584);
1021
1022	nvlist_free(nvl, key);
1023	ATF_REQUIRE(nvlist_empty(nvl));
1024
1025	nvlist_destroy(nvl);
1026}
1027
1028ATF_TEST_CASE_WITHOUT_HEAD(nvlist_free__single_string);
1029ATF_TEST_CASE_BODY(nvlist_free__single_string)
1030{
1031	nvlist_t *nvl;
1032	const char *key;
1033
1034	nvl = nvlist_create(0);
1035	key = "test";
1036	nvlist_add_string(nvl, key, "gjkfkjd");
1037
1038	nvlist_free(nvl, key);
1039	ATF_REQUIRE(nvlist_empty(nvl));
1040
1041	nvlist_destroy(nvl);
1042}
1043
1044ATF_TEST_CASE_WITHOUT_HEAD(nvlist_free__single_nvlist);
1045ATF_TEST_CASE_BODY(nvlist_free__single_nvlist)
1046{
1047	nvlist_t *nvl;
1048	const char *key;
1049
1050	nvl = nvlist_create(0);
1051	key = "test";
1052	nvlist_add_nvlist(nvl, key, nvlist_create(0));
1053
1054	nvlist_free(nvl, key);
1055	ATF_REQUIRE(nvlist_empty(nvl));
1056
1057	nvlist_destroy(nvl);
1058}
1059
1060ATF_TEST_CASE_WITHOUT_HEAD(nvlist_free__single_binary);
1061ATF_TEST_CASE_BODY(nvlist_free__single_binary)
1062{
1063	nvlist_t *nvl;
1064	const char *key;
1065
1066	nvl = nvlist_create(0);
1067	key = "test";
1068	nvlist_add_binary(nvl, key, "jgjgfd", 6);
1069
1070	nvlist_free(nvl, key);
1071	ATF_REQUIRE(nvlist_empty(nvl));
1072
1073	nvlist_destroy(nvl);
1074}
1075
1076ATF_TEST_CASE_WITHOUT_HEAD(nvlist_free_null__single_null);
1077ATF_TEST_CASE_BODY(nvlist_free_null__single_null)
1078{
1079	nvlist_t *nvl;
1080	const char *key;
1081
1082	nvl = nvlist_create(0);
1083	key = "test";
1084	nvlist_add_null(nvl, key);
1085
1086	nvlist_free_null(nvl, key);
1087	ATF_REQUIRE(nvlist_empty(nvl));
1088
1089	nvlist_destroy(nvl);
1090}
1091
1092ATF_TEST_CASE_WITHOUT_HEAD(nvlist_free_bool__single_bool);
1093ATF_TEST_CASE_BODY(nvlist_free_bool__single_bool)
1094{
1095	nvlist_t *nvl;
1096	const char *key;
1097
1098	nvl = nvlist_create(0);
1099	key = "test";
1100	nvlist_add_bool(nvl, key, true);
1101
1102	nvlist_free_bool(nvl, key);
1103	ATF_REQUIRE(nvlist_empty(nvl));
1104
1105	nvlist_destroy(nvl);
1106}
1107
1108ATF_TEST_CASE_WITHOUT_HEAD(nvlist_free_number__single_number);
1109ATF_TEST_CASE_BODY(nvlist_free_number__single_number)
1110{
1111	nvlist_t *nvl;
1112	const char *key;
1113
1114	nvl = nvlist_create(0);
1115	key = "test";
1116	nvlist_add_number(nvl, key, 584);
1117
1118	nvlist_free_number(nvl, key);
1119	ATF_REQUIRE(nvlist_empty(nvl));
1120
1121	nvlist_destroy(nvl);
1122}
1123
1124ATF_TEST_CASE_WITHOUT_HEAD(nvlist_free_string__single_string);
1125ATF_TEST_CASE_BODY(nvlist_free_string__single_string)
1126{
1127	nvlist_t *nvl;
1128	const char *key;
1129
1130	nvl = nvlist_create(0);
1131	key = "test";
1132	nvlist_add_string(nvl, key, "gjkfkjd");
1133
1134	nvlist_free_string(nvl, key);
1135	ATF_REQUIRE(nvlist_empty(nvl));
1136
1137	nvlist_destroy(nvl);
1138}
1139
1140ATF_TEST_CASE_WITHOUT_HEAD(nvlist_free_nvlist__single_nvlist);
1141ATF_TEST_CASE_BODY(nvlist_free_nvlist__single_nvlist)
1142{
1143	nvlist_t *nvl;
1144	const char *key;
1145
1146	nvl = nvlist_create(0);
1147	key = "test";
1148	nvlist_add_nvlist(nvl, key, nvlist_create(0));
1149
1150	nvlist_free_nvlist(nvl, key);
1151	ATF_REQUIRE(nvlist_empty(nvl));
1152
1153	nvlist_destroy(nvl);
1154}
1155
1156ATF_TEST_CASE_WITHOUT_HEAD(nvlist_free_binary__single_binary);
1157ATF_TEST_CASE_BODY(nvlist_free_binary__single_binary)
1158{
1159	nvlist_t *nvl;
1160	const char *key;
1161
1162	nvl = nvlist_create(0);
1163	key = "test";
1164	nvlist_add_binary(nvl, key, "jgjgfd", 6);
1165
1166	nvlist_free_binary(nvl, key);
1167	ATF_REQUIRE(nvlist_empty(nvl));
1168
1169	nvlist_destroy(nvl);
1170}
1171
1172ATF_INIT_TEST_CASES(tp)
1173{
1174	ATF_ADD_TEST_CASE(tp, nvlist_create__is_empty);
1175	ATF_ADD_TEST_CASE(tp, nvlist_add_null__single_insert);
1176	ATF_ADD_TEST_CASE(tp, nvlist_add_bool__single_insert);
1177	ATF_ADD_TEST_CASE(tp, nvlist_add_number__single_insert);
1178	ATF_ADD_TEST_CASE(tp, nvlist_add_string__single_insert);
1179	ATF_ADD_TEST_CASE(tp, nvlist_add_nvlist__single_insert);
1180	ATF_ADD_TEST_CASE(tp, nvlist_add_binary__single_insert);
1181
1182	ATF_ADD_TEST_CASE(tp, nvlist_clone__empty_nvlist);
1183	ATF_ADD_TEST_CASE(tp, nvlist_clone__nonempty_nvlist);
1184	ATF_ADD_TEST_CASE(tp, nvlist_clone__nested_nvlist);
1185	ATF_ADD_TEST_CASE(tp, nvlist_clone__error_nvlist);
1186
1187	ATF_ADD_TEST_CASE(tp, nvlist_pack__empty_nvlist);
1188	ATF_ADD_TEST_CASE(tp, nvlist_pack__multiple_values);
1189	ATF_ADD_TEST_CASE(tp, nvlist_pack__error_nvlist);
1190	ATF_ADD_TEST_CASE(tp, nvlist_unpack__duplicate_key);
1191
1192	ATF_ADD_TEST_CASE(tp, nvlist_move_string__single_insert);
1193	ATF_ADD_TEST_CASE(tp, nvlist_move_nvlist__single_insert);
1194	ATF_ADD_TEST_CASE(tp, nvlist_move_nvlist__null_child);
1195	ATF_ADD_TEST_CASE(tp, nvlist_move_binary__single_insert);
1196
1197	ATF_ADD_TEST_CASE(tp, nvlist_take_bool__single_remove);
1198	ATF_ADD_TEST_CASE(tp, nvlist_take_bool__other_keys_unchanged);
1199	ATF_ADD_TEST_CASE(tp, nvlist_take_number__single_remove);
1200	ATF_ADD_TEST_CASE(tp, nvlist_take_number__other_keys_unchanged);
1201	ATF_ADD_TEST_CASE(tp, nvlist_take_string__single_remove);
1202	ATF_ADD_TEST_CASE(tp, nvlist_take_string__other_keys_unchanged);
1203	ATF_ADD_TEST_CASE(tp, nvlist_take_nvlist__single_remove);
1204	ATF_ADD_TEST_CASE(tp, nvlist_take_nvlist__other_keys_unchanged);
1205	ATF_ADD_TEST_CASE(tp, nvlist_take_binary__single_remove);
1206	ATF_ADD_TEST_CASE(tp, nvlist_take_binary__other_keys_unchanged);
1207
1208	ATF_ADD_TEST_CASE(tp, nvlist_free__single_null);
1209	ATF_ADD_TEST_CASE(tp, nvlist_free__single_bool);
1210	ATF_ADD_TEST_CASE(tp, nvlist_free__single_number);
1211	ATF_ADD_TEST_CASE(tp, nvlist_free__single_string);
1212	ATF_ADD_TEST_CASE(tp, nvlist_free__single_nvlist);
1213	ATF_ADD_TEST_CASE(tp, nvlist_free__single_binary);
1214
1215	ATF_ADD_TEST_CASE(tp, nvlist_free_null__single_null);
1216	ATF_ADD_TEST_CASE(tp, nvlist_free_bool__single_bool);
1217	ATF_ADD_TEST_CASE(tp, nvlist_free_number__single_number);
1218	ATF_ADD_TEST_CASE(tp, nvlist_free_string__single_string);
1219	ATF_ADD_TEST_CASE(tp, nvlist_free_nvlist__single_nvlist);
1220	ATF_ADD_TEST_CASE(tp, nvlist_free_binary__single_binary);
1221}
1222/*-
1223 * Copyright (c) 2014-2015 Sandvine Inc.  All rights reserved.
1224 * All rights reserved.
1225 *
1226 * Redistribution and use in source and binary forms, with or without
1227 * modification, are permitted provided that the following conditions
1228 * are met:
1229 * 1. Redistributions of source code must retain the above copyright
1230 *    notice, this list of conditions and the following disclaimer.
1231 * 2. Redistributions in binary form must reproduce the above copyright
1232 *    notice, this list of conditions and the following disclaimer in the
1233 *    documentation and/or other materials provided with the distribution.
1234 *
1235 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1236 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1237 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1238 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
1239 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
1240 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
1241 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
1242 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
1243 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
1244 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
1245 * SUCH DAMAGE.
1246 */
1247
1248#include <atf-c++.hpp>
1249#include <nv.h>
1250
1251#include <errno.h>
1252/*
1253 * Test that a newly created nvlist has no errors, and is empty.
1254 */
1255ATF_TEST_CASE_WITHOUT_HEAD(nvlist_create__is_empty);
1256ATF_TEST_CASE_BODY(nvlist_create__is_empty)
1257{
1258	nvlist_t *nvl;
1259	int type;
1260	void *it;
1261
1262	nvl = nvlist_create(0);
1263
1264	ATF_REQUIRE(nvl != NULL);
1265
1266	ATF_REQUIRE_EQ(nvlist_error(nvl), 0);
1267	ATF_REQUIRE(nvlist_empty(nvl));
1268
1269	it = NULL;
1270	ATF_REQUIRE_EQ(nvlist_next(nvl, &type, &it), NULL);
1271
1272	nvlist_destroy(nvl);
1273}
1274
1275ATF_TEST_CASE_WITHOUT_HEAD(nvlist_add_null__single_insert);
1276ATF_TEST_CASE_BODY(nvlist_add_null__single_insert)
1277{
1278	nvlist_t *nvl;
1279	void *it;
1280	const char *key;
1281	int type;
1282
1283	key = "key";
1284	nvl = nvlist_create(0);
1285
1286	ATF_REQUIRE(nvl != NULL);
1287	ATF_REQUIRE(!nvlist_exists(nvl, key));
1288
1289	nvlist_add_null(nvl, key);
1290
1291	ATF_REQUIRE(!nvlist_empty(nvl));
1292	ATF_REQUIRE(nvlist_exists(nvl, key));
1293	ATF_REQUIRE(nvlist_existsf(nvl, "%s", key));
1294	ATF_REQUIRE(nvlist_exists_null(nvl, key));
1295	ATF_REQUIRE(nvlist_existsf_null(nvl, "key"));
1296
1297	/* Iterate over the nvlist; ensure that it has only our one key. */
1298	it = NULL;
1299	ATF_REQUIRE_EQ(strcmp(nvlist_next(nvl, &type, &it), key), 0);
1300	ATF_REQUIRE_EQ(type, NV_TYPE_NULL);
1301	ATF_REQUIRE_EQ(nvlist_next(nvl, &type,&it), NULL);
1302
1303	nvlist_destroy(nvl);
1304}
1305
1306ATF_TEST_CASE_WITHOUT_HEAD(nvlist_add_bool__single_insert);
1307ATF_TEST_CASE_BODY(nvlist_add_bool__single_insert)
1308{
1309	nvlist_t *nvl;
1310	void *it;
1311	const char *key;
1312	int type;
1313
1314	key = "name";
1315	nvl = nvlist_create(0);
1316
1317	ATF_REQUIRE(nvl != NULL);
1318	ATF_REQUIRE(!nvlist_exists(nvl, key));
1319
1320	nvlist_add_bool(nvl, key, true);
1321
1322	ATF_REQUIRE(!nvlist_empty(nvl));
1323	ATF_REQUIRE(nvlist_exists(nvl, key));
1324	ATF_REQUIRE(nvlist_existsf(nvl, "%s%s", "na", "me"));
1325	ATF_REQUIRE(nvlist_exists_bool(nvl, key));
1326	ATF_REQUIRE(nvlist_existsf_bool(nvl, "%s%c", "nam", 'e'));
1327	ATF_REQUIRE_EQ(nvlist_get_bool(nvl, key), true);
1328	ATF_REQUIRE_EQ(nvlist_getf_bool(nvl, "%c%s", 'n', "ame"), true);
1329
1330	/* Iterate over the nvlist; ensure that it has only our one key. */
1331	it = NULL;
1332	ATF_REQUIRE_EQ(strcmp(nvlist_next(nvl, &type, &it), key), 0);
1333	ATF_REQUIRE_EQ(type, NV_TYPE_BOOL);
1334	ATF_REQUIRE_EQ(nvlist_next(nvl, &type,&it), NULL);
1335
1336	nvlist_destroy(nvl);
1337}
1338
1339ATF_TEST_CASE_WITHOUT_HEAD(nvlist_add_number__single_insert);
1340ATF_TEST_CASE_BODY(nvlist_add_number__single_insert)
1341{
1342	nvlist_t *nvl;
1343	void *it;
1344	const char *key;
1345	uint64_t value;
1346	int type;
1347
1348	key = "foo123";
1349	value = 71965;
1350	nvl = nvlist_create(0);
1351
1352	ATF_REQUIRE(nvl != NULL);
1353	ATF_REQUIRE(!nvlist_exists(nvl, key));
1354
1355	nvlist_add_number(nvl, key, value);
1356
1357	ATF_REQUIRE(!nvlist_empty(nvl));
1358	ATF_REQUIRE(nvlist_exists(nvl, key));
1359	ATF_REQUIRE(nvlist_existsf(nvl, "%s%d", "foo", 123));
1360	ATF_REQUIRE(nvlist_exists_number(nvl, key));
1361	ATF_REQUIRE(nvlist_existsf_number(nvl, "%s", key));
1362	ATF_REQUIRE_EQ(nvlist_get_number(nvl, key), value);
1363	ATF_REQUIRE_EQ(nvlist_getf_number(nvl, "%s", key), value);
1364
1365	/* Iterate over the nvlist; ensure that it has only our one key. */
1366	it = NULL;
1367	ATF_REQUIRE_EQ(strcmp(nvlist_next(nvl, &type, &it), key), 0);
1368	ATF_REQUIRE_EQ(type, NV_TYPE_NUMBER);
1369	ATF_REQUIRE_EQ(nvlist_next(nvl, &type,&it), NULL);
1370
1371	nvlist_destroy(nvl);
1372}
1373
1374ATF_TEST_CASE_WITHOUT_HEAD(nvlist_add_string__single_insert);
1375ATF_TEST_CASE_BODY(nvlist_add_string__single_insert)
1376{
1377	nvlist_t *nvl;
1378	void *it;
1379	const char *key;
1380	const char *value;
1381	int type;
1382
1383	key = "test";
1384	value = "fgjdkgjdk";
1385	nvl = nvlist_create(0);
1386
1387	ATF_REQUIRE(nvl != NULL);
1388	ATF_REQUIRE(!nvlist_exists(nvl, key));
1389
1390	nvlist_add_string(nvl, key, value);
1391
1392	ATF_REQUIRE(!nvlist_empty(nvl));
1393	ATF_REQUIRE(nvlist_exists(nvl, key));
1394	ATF_REQUIRE(nvlist_existsf(nvl, "%s", key));
1395	ATF_REQUIRE(nvlist_exists_string(nvl, key));
1396	ATF_REQUIRE(nvlist_existsf_string(nvl, "%s", key));
1397	ATF_REQUIRE_EQ(strcmp(nvlist_get_string(nvl, key), value), 0);
1398	ATF_REQUIRE_EQ(strcmp(nvlist_getf_string(nvl, "%s", key), value), 0);
1399
1400	/* nvlist_add_* is required to clone the value, so check for that. */
1401	ATF_REQUIRE(nvlist_get_string(nvl, key) != value);
1402
1403	/* Iterate over the nvlist; ensure that it has only our one key. */
1404	it = NULL;
1405	ATF_REQUIRE_EQ(strcmp(nvlist_next(nvl, &type, &it), key), 0);
1406	ATF_REQUIRE_EQ(type, NV_TYPE_STRING);
1407	ATF_REQUIRE_EQ(nvlist_next(nvl, &type,&it), NULL);
1408
1409	nvlist_destroy(nvl);
1410}
1411
1412ATF_TEST_CASE_WITHOUT_HEAD(nvlist_add_nvlist__single_insert);
1413ATF_TEST_CASE_BODY(nvlist_add_nvlist__single_insert)
1414{
1415	nvlist_t *nvl;
1416	void *it;
1417	const char *key, *subkey;
1418	nvlist_t *sublist;
1419	const nvlist_t *value;
1420	int type;
1421
1422	key = "test";
1423	subkey = "subkey";
1424	sublist = nvlist_create(0);
1425	nvl = nvlist_create(0);
1426
1427	ATF_REQUIRE(nvl != NULL);
1428	ATF_REQUIRE(!nvlist_exists(nvl, key));
1429
1430	nvlist_add_null(sublist, subkey);
1431	nvlist_add_nvlist(nvl, key, sublist);
1432
1433	ATF_REQUIRE(!nvlist_empty(nvl));
1434	ATF_REQUIRE(nvlist_exists(nvl, key));
1435	ATF_REQUIRE(nvlist_existsf(nvl, "%s", key));
1436	ATF_REQUIRE(nvlist_exists_nvlist(nvl, key));
1437	ATF_REQUIRE(nvlist_existsf_nvlist(nvl, "%s", key));
1438
1439	value = nvlist_get_nvlist(nvl, key);
1440	ATF_REQUIRE(nvlist_exists_null(value, subkey));
1441
1442	/* nvlist_add_* is required to clone the value, so check for that. */
1443	ATF_REQUIRE(sublist != value);
1444
1445	value = nvlist_getf_nvlist(nvl, "%s", key);
1446	ATF_REQUIRE(nvlist_exists_null(value, subkey));
1447	ATF_REQUIRE(sublist != value);
1448
1449	/* Iterate over the nvlist; ensure that it has only our one key. */
1450	it = NULL;
1451	ATF_REQUIRE_EQ(strcmp(nvlist_next(nvl, &type, &it), key), 0);
1452	ATF_REQUIRE_EQ(type, NV_TYPE_NVLIST);
1453	ATF_REQUIRE_EQ(nvlist_next(nvl, &type,&it), NULL);
1454
1455	nvlist_destroy(sublist);
1456	nvlist_destroy(nvl);
1457}
1458
1459ATF_TEST_CASE_WITHOUT_HEAD(nvlist_add_binary__single_insert);
1460ATF_TEST_CASE_BODY(nvlist_add_binary__single_insert)
1461{
1462	nvlist_t *nvl;
1463	void *it;
1464	const char *key;
1465	void *value;
1466	const void *ret_value;
1467	size_t value_size, ret_size;
1468	int type;
1469
1470	key = "binary";
1471	value_size = 13;
1472	value = malloc(value_size);
1473	memset(value, 0xa5, value_size);
1474	nvl = nvlist_create(0);
1475
1476	ATF_REQUIRE(nvl != NULL);
1477	ATF_REQUIRE(!nvlist_exists(nvl, key));
1478
1479	nvlist_add_binary(nvl, key, value, value_size);
1480
1481	ATF_REQUIRE(!nvlist_empty(nvl));
1482	ATF_REQUIRE(nvlist_exists(nvl, key));
1483	ATF_REQUIRE(nvlist_existsf(nvl, "%s", key));
1484	ATF_REQUIRE(nvlist_exists_binary(nvl, key));
1485	ATF_REQUIRE(nvlist_existsf_binary(nvl, "%s", key));
1486
1487	ret_value = nvlist_get_binary(nvl, key, &ret_size);
1488	ATF_REQUIRE_EQ(value_size, ret_size);
1489	ATF_REQUIRE_EQ(memcmp(value, ret_value, ret_size), 0);
1490
1491	/* nvlist_add_* is required to clone the value, so check for that. */
1492	ATF_REQUIRE(value != ret_value);
1493
1494	ret_value = nvlist_getf_binary(nvl, &ret_size, "%s", key);
1495	ATF_REQUIRE_EQ(value_size, ret_size);
1496	ATF_REQUIRE_EQ(memcmp(value, ret_value, ret_size), 0);
1497	ATF_REQUIRE(value != ret_value);
1498
1499	/* Iterate over the nvlist; ensure that it has only our one key. */
1500	it = NULL;
1501	ATF_REQUIRE_EQ(strcmp(nvlist_next(nvl, &type, &it), key), 0);
1502	ATF_REQUIRE_EQ(type, NV_TYPE_BINARY);
1503	ATF_REQUIRE_EQ(nvlist_next(nvl, &type,&it), NULL);
1504
1505	nvlist_destroy(nvl);
1506	free(value);
1507}
1508
1509ATF_INIT_TEST_CASES(tp)
1510{
1511	ATF_ADD_TEST_CASE(tp, nvlist_create__is_empty);
1512	ATF_ADD_TEST_CASE(tp, nvlist_add_null__single_insert);
1513	ATF_ADD_TEST_CASE(tp, nvlist_add_bool__single_insert);
1514	ATF_ADD_TEST_CASE(tp, nvlist_add_number__single_insert);
1515	ATF_ADD_TEST_CASE(tp, nvlist_add_string__single_insert);
1516	ATF_ADD_TEST_CASE(tp, nvlist_add_nvlist__single_insert);
1517	ATF_ADD_TEST_CASE(tp, nvlist_add_binary__single_insert);
1518}
1519/*-
1520 * Copyright (c) 2014-2015 Sandvine Inc.  All rights reserved.
1521 * All rights reserved.
1522 *
1523 * Redistribution and use in source and binary forms, with or without
1524 * modification, are permitted provided that the following conditions
1525 * are met:
1526 * 1. Redistributions of source code must retain the above copyright
1527 *    notice, this list of conditions and the following disclaimer.
1528 * 2. Redistributions in binary form must reproduce the above copyright
1529 *    notice, this list of conditions and the following disclaimer in the
1530 *    documentation and/or other materials provided with the distribution.
1531 *
1532 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1533 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1534 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1535 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
1536 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
1537 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
1538 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
1539 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
1540 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
1541 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
1542 * SUCH DAMAGE.
1543 */
1544
1545#include <atf-c++.hpp>
1546#include <nv.h>
1547
1548#include <errno.h>
1549/*
1550 * Test that a newly created nvlist has no errors, and is empty.
1551 */
1552ATF_TEST_CASE_WITHOUT_HEAD(nvlist_create__is_empty);
1553ATF_TEST_CASE_BODY(nvlist_create__is_empty)
1554{
1555	nvlist_t *nvl;
1556	int type;
1557	void *it;
1558
1559	nvl = nvlist_create(0);
1560
1561	ATF_REQUIRE(nvl != NULL);
1562
1563	ATF_REQUIRE_EQ(nvlist_error(nvl), 0);
1564	ATF_REQUIRE(nvlist_empty(nvl));
1565
1566	it = NULL;
1567	ATF_REQUIRE_EQ(nvlist_next(nvl, &type, &it), NULL);
1568
1569	nvlist_destroy(nvl);
1570}
1571
1572ATF_TEST_CASE_WITHOUT_HEAD(nvlist_add_null__single_insert);
1573ATF_TEST_CASE_BODY(nvlist_add_null__single_insert)
1574{
1575	nvlist_t *nvl;
1576	void *it;
1577	const char *key;
1578	int type;
1579
1580	key = "key";
1581	nvl = nvlist_create(0);
1582
1583	ATF_REQUIRE(nvl != NULL);
1584	ATF_REQUIRE(!nvlist_exists(nvl, key));
1585
1586	nvlist_add_null(nvl, key);
1587
1588	ATF_REQUIRE(!nvlist_empty(nvl));
1589	ATF_REQUIRE(nvlist_exists(nvl, key));
1590	ATF_REQUIRE(nvlist_existsf(nvl, "%s", key));
1591	ATF_REQUIRE(nvlist_exists_null(nvl, key));
1592	ATF_REQUIRE(nvlist_existsf_null(nvl, "key"));
1593
1594	/* Iterate over the nvlist; ensure that it has only our one key. */
1595	it = NULL;
1596	ATF_REQUIRE_EQ(strcmp(nvlist_next(nvl, &type, &it), key), 0);
1597	ATF_REQUIRE_EQ(type, NV_TYPE_NULL);
1598	ATF_REQUIRE_EQ(nvlist_next(nvl, &type,&it), NULL);
1599
1600	nvlist_destroy(nvl);
1601}
1602
1603ATF_TEST_CASE_WITHOUT_HEAD(nvlist_add_bool__single_insert);
1604ATF_TEST_CASE_BODY(nvlist_add_bool__single_insert)
1605{
1606	nvlist_t *nvl;
1607	void *it;
1608	const char *key;
1609	int type;
1610
1611	key = "name";
1612	nvl = nvlist_create(0);
1613
1614	ATF_REQUIRE(nvl != NULL);
1615	ATF_REQUIRE(!nvlist_exists(nvl, key));
1616
1617	nvlist_add_bool(nvl, key, true);
1618
1619	ATF_REQUIRE(!nvlist_empty(nvl));
1620	ATF_REQUIRE(nvlist_exists(nvl, key));
1621	ATF_REQUIRE(nvlist_existsf(nvl, "%s%s", "na", "me"));
1622	ATF_REQUIRE(nvlist_exists_bool(nvl, key));
1623	ATF_REQUIRE(nvlist_existsf_bool(nvl, "%s%c", "nam", 'e'));
1624	ATF_REQUIRE_EQ(nvlist_get_bool(nvl, key), true);
1625	ATF_REQUIRE_EQ(nvlist_getf_bool(nvl, "%c%s", 'n', "ame"), true);
1626
1627	/* Iterate over the nvlist; ensure that it has only our one key. */
1628	it = NULL;
1629	ATF_REQUIRE_EQ(strcmp(nvlist_next(nvl, &type, &it), key), 0);
1630	ATF_REQUIRE_EQ(type, NV_TYPE_BOOL);
1631	ATF_REQUIRE_EQ(nvlist_next(nvl, &type,&it), NULL);
1632
1633	nvlist_destroy(nvl);
1634}
1635
1636ATF_TEST_CASE_WITHOUT_HEAD(nvlist_add_number__single_insert);
1637ATF_TEST_CASE_BODY(nvlist_add_number__single_insert)
1638{
1639	nvlist_t *nvl;
1640	void *it;
1641	const char *key;
1642	uint64_t value;
1643	int type;
1644
1645	key = "foo123";
1646	value = 71965;
1647	nvl = nvlist_create(0);
1648
1649	ATF_REQUIRE(nvl != NULL);
1650	ATF_REQUIRE(!nvlist_exists(nvl, key));
1651
1652	nvlist_add_number(nvl, key, value);
1653
1654	ATF_REQUIRE(!nvlist_empty(nvl));
1655	ATF_REQUIRE(nvlist_exists(nvl, key));
1656	ATF_REQUIRE(nvlist_existsf(nvl, "%s%d", "foo", 123));
1657	ATF_REQUIRE(nvlist_exists_number(nvl, key));
1658	ATF_REQUIRE(nvlist_existsf_number(nvl, "%s", key));
1659	ATF_REQUIRE_EQ(nvlist_get_number(nvl, key), value);
1660	ATF_REQUIRE_EQ(nvlist_getf_number(nvl, "%s", key), value);
1661
1662	/* Iterate over the nvlist; ensure that it has only our one key. */
1663	it = NULL;
1664	ATF_REQUIRE_EQ(strcmp(nvlist_next(nvl, &type, &it), key), 0);
1665	ATF_REQUIRE_EQ(type, NV_TYPE_NUMBER);
1666	ATF_REQUIRE_EQ(nvlist_next(nvl, &type,&it), NULL);
1667
1668	nvlist_destroy(nvl);
1669}
1670
1671ATF_TEST_CASE_WITHOUT_HEAD(nvlist_add_string__single_insert);
1672ATF_TEST_CASE_BODY(nvlist_add_string__single_insert)
1673{
1674	nvlist_t *nvl;
1675	void *it;
1676	const char *key;
1677	const char *value;
1678	int type;
1679
1680	key = "test";
1681	value = "fgjdkgjdk";
1682	nvl = nvlist_create(0);
1683
1684	ATF_REQUIRE(nvl != NULL);
1685	ATF_REQUIRE(!nvlist_exists(nvl, key));
1686
1687	nvlist_add_string(nvl, key, value);
1688
1689	ATF_REQUIRE(!nvlist_empty(nvl));
1690	ATF_REQUIRE(nvlist_exists(nvl, key));
1691	ATF_REQUIRE(nvlist_existsf(nvl, "%s", key));
1692	ATF_REQUIRE(nvlist_exists_string(nvl, key));
1693	ATF_REQUIRE(nvlist_existsf_string(nvl, "%s", key));
1694	ATF_REQUIRE_EQ(strcmp(nvlist_get_string(nvl, key), value), 0);
1695	ATF_REQUIRE_EQ(strcmp(nvlist_getf_string(nvl, "%s", key), value), 0);
1696
1697	/* nvlist_add_* is required to clone the value, so check for that. */
1698	ATF_REQUIRE(nvlist_get_string(nvl, key) != value);
1699
1700	/* Iterate over the nvlist; ensure that it has only our one key. */
1701	it = NULL;
1702	ATF_REQUIRE_EQ(strcmp(nvlist_next(nvl, &type, &it), key), 0);
1703	ATF_REQUIRE_EQ(type, NV_TYPE_STRING);
1704	ATF_REQUIRE_EQ(nvlist_next(nvl, &type,&it), NULL);
1705
1706	nvlist_destroy(nvl);
1707}
1708
1709ATF_TEST_CASE_WITHOUT_HEAD(nvlist_add_nvlist__single_insert);
1710ATF_TEST_CASE_BODY(nvlist_add_nvlist__single_insert)
1711{
1712	nvlist_t *nvl;
1713	void *it;
1714	const char *key, *subkey;
1715	nvlist_t *sublist;
1716	const nvlist_t *value;
1717	int type;
1718
1719	key = "test";
1720	subkey = "subkey";
1721	sublist = nvlist_create(0);
1722	nvl = nvlist_create(0);
1723
1724	ATF_REQUIRE(nvl != NULL);
1725	ATF_REQUIRE(!nvlist_exists(nvl, key));
1726
1727	nvlist_add_null(sublist, subkey);
1728	nvlist_add_nvlist(nvl, key, sublist);
1729
1730	ATF_REQUIRE(!nvlist_empty(nvl));
1731	ATF_REQUIRE(nvlist_exists(nvl, key));
1732	ATF_REQUIRE(nvlist_existsf(nvl, "%s", key));
1733	ATF_REQUIRE(nvlist_exists_nvlist(nvl, key));
1734	ATF_REQUIRE(nvlist_existsf_nvlist(nvl, "%s", key));
1735
1736	value = nvlist_get_nvlist(nvl, key);
1737	ATF_REQUIRE(nvlist_exists_null(value, subkey));
1738
1739	/* nvlist_add_* is required to clone the value, so check for that. */
1740	ATF_REQUIRE(sublist != value);
1741
1742	value = nvlist_getf_nvlist(nvl, "%s", key);
1743	ATF_REQUIRE(nvlist_exists_null(value, subkey));
1744	ATF_REQUIRE(sublist != value);
1745
1746	/* Iterate over the nvlist; ensure that it has only our one key. */
1747	it = NULL;
1748	ATF_REQUIRE_EQ(strcmp(nvlist_next(nvl, &type, &it), key), 0);
1749	ATF_REQUIRE_EQ(type, NV_TYPE_NVLIST);
1750	ATF_REQUIRE_EQ(nvlist_next(nvl, &type,&it), NULL);
1751
1752	nvlist_destroy(sublist);
1753	nvlist_destroy(nvl);
1754}
1755
1756ATF_TEST_CASE_WITHOUT_HEAD(nvlist_add_binary__single_insert);
1757ATF_TEST_CASE_BODY(nvlist_add_binary__single_insert)
1758{
1759	nvlist_t *nvl;
1760	void *it;
1761	const char *key;
1762	void *value;
1763	const void *ret_value;
1764	size_t value_size, ret_size;
1765	int type;
1766
1767	key = "binary";
1768	value_size = 13;
1769	value = malloc(value_size);
1770	memset(value, 0xa5, value_size);
1771	nvl = nvlist_create(0);
1772
1773	ATF_REQUIRE(nvl != NULL);
1774	ATF_REQUIRE(!nvlist_exists(nvl, key));
1775
1776	nvlist_add_binary(nvl, key, value, value_size);
1777
1778	ATF_REQUIRE(!nvlist_empty(nvl));
1779	ATF_REQUIRE(nvlist_exists(nvl, key));
1780	ATF_REQUIRE(nvlist_existsf(nvl, "%s", key));
1781	ATF_REQUIRE(nvlist_exists_binary(nvl, key));
1782	ATF_REQUIRE(nvlist_existsf_binary(nvl, "%s", key));
1783
1784	ret_value = nvlist_get_binary(nvl, key, &ret_size);
1785	ATF_REQUIRE_EQ(value_size, ret_size);
1786	ATF_REQUIRE_EQ(memcmp(value, ret_value, ret_size), 0);
1787
1788	/* nvlist_add_* is required to clone the value, so check for that. */
1789	ATF_REQUIRE(value != ret_value);
1790
1791	ret_value = nvlist_getf_binary(nvl, &ret_size, "%s", key);
1792	ATF_REQUIRE_EQ(value_size, ret_size);
1793	ATF_REQUIRE_EQ(memcmp(value, ret_value, ret_size), 0);
1794	ATF_REQUIRE(value != ret_value);
1795
1796	/* Iterate over the nvlist; ensure that it has only our one key. */
1797	it = NULL;
1798	ATF_REQUIRE_EQ(strcmp(nvlist_next(nvl, &type, &it), key), 0);
1799	ATF_REQUIRE_EQ(type, NV_TYPE_BINARY);
1800	ATF_REQUIRE_EQ(nvlist_next(nvl, &type,&it), NULL);
1801
1802	nvlist_destroy(nvl);
1803	free(value);
1804}
1805
1806ATF_INIT_TEST_CASES(tp)
1807{
1808	ATF_ADD_TEST_CASE(tp, nvlist_create__is_empty);
1809	ATF_ADD_TEST_CASE(tp, nvlist_add_null__single_insert);
1810	ATF_ADD_TEST_CASE(tp, nvlist_add_bool__single_insert);
1811	ATF_ADD_TEST_CASE(tp, nvlist_add_number__single_insert);
1812	ATF_ADD_TEST_CASE(tp, nvlist_add_string__single_insert);
1813	ATF_ADD_TEST_CASE(tp, nvlist_add_nvlist__single_insert);
1814	ATF_ADD_TEST_CASE(tp, nvlist_add_binary__single_insert);
1815}
1816/*-
1817 * Copyright (c) 2014-2015 Sandvine Inc.  All rights reserved.
1818 * All rights reserved.
1819 *
1820 * Redistribution and use in source and binary forms, with or without
1821 * modification, are permitted provided that the following conditions
1822 * are met:
1823 * 1. Redistributions of source code must retain the above copyright
1824 *    notice, this list of conditions and the following disclaimer.
1825 * 2. Redistributions in binary form must reproduce the above copyright
1826 *    notice, this list of conditions and the following disclaimer in the
1827 *    documentation and/or other materials provided with the distribution.
1828 *
1829 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1830 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1831 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1832 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
1833 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
1834 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
1835 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
1836 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
1837 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
1838 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
1839 * SUCH DAMAGE.
1840 */
1841
1842#include <atf-c++.hpp>
1843#include <nv.h>
1844
1845#include <errno.h>
1846#include <limits>
1847#include <set>
1848#include <sstream>
1849#include <string>
1850
1851/*
1852 * Test that a newly created nvlist has no errors, and is empty.
1853 */
1854ATF_TEST_CASE_WITHOUT_HEAD(nvlist_create__is_empty);
1855ATF_TEST_CASE_BODY(nvlist_create__is_empty)
1856{
1857	nvlist_t *nvl;
1858	int type;
1859	void *it;
1860
1861	nvl = nvlist_create(0);
1862
1863	ATF_REQUIRE(nvl != NULL);
1864
1865	ATF_REQUIRE_EQ(nvlist_error(nvl), 0);
1866	ATF_REQUIRE(nvlist_empty(nvl));
1867
1868	it = NULL;
1869	ATF_REQUIRE_EQ(nvlist_next(nvl, &type, &it), NULL);
1870
1871	nvlist_destroy(nvl);
1872}
1873
1874ATF_TEST_CASE_WITHOUT_HEAD(nvlist_add_null__single_insert);
1875ATF_TEST_CASE_BODY(nvlist_add_null__single_insert)
1876{
1877	nvlist_t *nvl;
1878	void *it;
1879	const char *key;
1880	int type;
1881
1882	key = "key";
1883	nvl = nvlist_create(0);
1884
1885	ATF_REQUIRE(nvl != NULL);
1886	ATF_REQUIRE(!nvlist_exists(nvl, key));
1887
1888	nvlist_add_null(nvl, key);
1889
1890	ATF_REQUIRE(!nvlist_empty(nvl));
1891	ATF_REQUIRE(nvlist_exists(nvl, key));
1892	ATF_REQUIRE(nvlist_existsf(nvl, "%s", key));
1893	ATF_REQUIRE(nvlist_exists_null(nvl, key));
1894	ATF_REQUIRE(nvlist_existsf_null(nvl, "key"));
1895
1896	/* Iterate over the nvlist; ensure that it has only our one key. */
1897	it = NULL;
1898	ATF_REQUIRE_EQ(strcmp(nvlist_next(nvl, &type, &it), key), 0);
1899	ATF_REQUIRE_EQ(type, NV_TYPE_NULL);
1900	ATF_REQUIRE_EQ(nvlist_next(nvl, &type,&it), NULL);
1901
1902	nvlist_destroy(nvl);
1903}
1904
1905ATF_TEST_CASE_WITHOUT_HEAD(nvlist_add_bool__single_insert);
1906ATF_TEST_CASE_BODY(nvlist_add_bool__single_insert)
1907{
1908	nvlist_t *nvl;
1909	void *it;
1910	const char *key;
1911	int type;
1912
1913	key = "name";
1914	nvl = nvlist_create(0);
1915
1916	ATF_REQUIRE(nvl != NULL);
1917	ATF_REQUIRE(!nvlist_exists(nvl, key));
1918
1919	nvlist_add_bool(nvl, key, true);
1920
1921	ATF_REQUIRE(!nvlist_empty(nvl));
1922	ATF_REQUIRE(nvlist_exists(nvl, key));
1923	ATF_REQUIRE(nvlist_existsf(nvl, "%s%s", "na", "me"));
1924	ATF_REQUIRE(nvlist_exists_bool(nvl, key));
1925	ATF_REQUIRE(nvlist_existsf_bool(nvl, "%s%c", "nam", 'e'));
1926	ATF_REQUIRE_EQ(nvlist_get_bool(nvl, key), true);
1927	ATF_REQUIRE_EQ(nvlist_getf_bool(nvl, "%c%s", 'n', "ame"), true);
1928
1929	/* Iterate over the nvlist; ensure that it has only our one key. */
1930	it = NULL;
1931	ATF_REQUIRE_EQ(strcmp(nvlist_next(nvl, &type, &it), key), 0);
1932	ATF_REQUIRE_EQ(type, NV_TYPE_BOOL);
1933	ATF_REQUIRE_EQ(nvlist_next(nvl, &type,&it), NULL);
1934
1935	nvlist_destroy(nvl);
1936}
1937
1938ATF_TEST_CASE_WITHOUT_HEAD(nvlist_add_number__single_insert);
1939ATF_TEST_CASE_BODY(nvlist_add_number__single_insert)
1940{
1941	nvlist_t *nvl;
1942	void *it;
1943	const char *key;
1944	uint64_t value;
1945	int type;
1946
1947	key = "foo123";
1948	value = 71965;
1949	nvl = nvlist_create(0);
1950
1951	ATF_REQUIRE(nvl != NULL);
1952	ATF_REQUIRE(!nvlist_exists(nvl, key));
1953
1954	nvlist_add_number(nvl, key, value);
1955
1956	ATF_REQUIRE(!nvlist_empty(nvl));
1957	ATF_REQUIRE(nvlist_exists(nvl, key));
1958	ATF_REQUIRE(nvlist_existsf(nvl, "%s%d", "foo", 123));
1959	ATF_REQUIRE(nvlist_exists_number(nvl, key));
1960	ATF_REQUIRE(nvlist_existsf_number(nvl, "%s", key));
1961	ATF_REQUIRE_EQ(nvlist_get_number(nvl, key), value);
1962	ATF_REQUIRE_EQ(nvlist_getf_number(nvl, "%s", key), value);
1963
1964	/* Iterate over the nvlist; ensure that it has only our one key. */
1965	it = NULL;
1966	ATF_REQUIRE_EQ(strcmp(nvlist_next(nvl, &type, &it), key), 0);
1967	ATF_REQUIRE_EQ(type, NV_TYPE_NUMBER);
1968	ATF_REQUIRE_EQ(nvlist_next(nvl, &type,&it), NULL);
1969
1970	nvlist_destroy(nvl);
1971}
1972
1973ATF_TEST_CASE_WITHOUT_HEAD(nvlist_add_string__single_insert);
1974ATF_TEST_CASE_BODY(nvlist_add_string__single_insert)
1975{
1976	nvlist_t *nvl;
1977	void *it;
1978	const char *key;
1979	const char *value;
1980	int type;
1981
1982	key = "test";
1983	value = "fgjdkgjdk";
1984	nvl = nvlist_create(0);
1985
1986	ATF_REQUIRE(nvl != NULL);
1987	ATF_REQUIRE(!nvlist_exists(nvl, key));
1988
1989	nvlist_add_string(nvl, key, value);
1990
1991	ATF_REQUIRE(!nvlist_empty(nvl));
1992	ATF_REQUIRE(nvlist_exists(nvl, key));
1993	ATF_REQUIRE(nvlist_existsf(nvl, "%s", key));
1994	ATF_REQUIRE(nvlist_exists_string(nvl, key));
1995	ATF_REQUIRE(nvlist_existsf_string(nvl, "%s", key));
1996	ATF_REQUIRE_EQ(strcmp(nvlist_get_string(nvl, key), value), 0);
1997	ATF_REQUIRE_EQ(strcmp(nvlist_getf_string(nvl, "%s", key), value), 0);
1998
1999	/* nvlist_add_* is required to clone the value, so check for that. */
2000	ATF_REQUIRE(nvlist_get_string(nvl, key) != value);
2001
2002	/* Iterate over the nvlist; ensure that it has only our one key. */
2003	it = NULL;
2004	ATF_REQUIRE_EQ(strcmp(nvlist_next(nvl, &type, &it), key), 0);
2005	ATF_REQUIRE_EQ(type, NV_TYPE_STRING);
2006	ATF_REQUIRE_EQ(nvlist_next(nvl, &type,&it), NULL);
2007
2008	nvlist_destroy(nvl);
2009}
2010
2011ATF_TEST_CASE_WITHOUT_HEAD(nvlist_add_nvlist__single_insert);
2012ATF_TEST_CASE_BODY(nvlist_add_nvlist__single_insert)
2013{
2014	nvlist_t *nvl;
2015	void *it;
2016	const char *key, *subkey;
2017	nvlist_t *sublist;
2018	const nvlist_t *value;
2019	int type;
2020
2021	key = "test";
2022	subkey = "subkey";
2023	sublist = nvlist_create(0);
2024	nvl = nvlist_create(0);
2025
2026	ATF_REQUIRE(nvl != NULL);
2027	ATF_REQUIRE(!nvlist_exists(nvl, key));
2028
2029	nvlist_add_null(sublist, subkey);
2030	nvlist_add_nvlist(nvl, key, sublist);
2031
2032	ATF_REQUIRE(!nvlist_empty(nvl));
2033	ATF_REQUIRE(nvlist_exists(nvl, key));
2034	ATF_REQUIRE(nvlist_existsf(nvl, "%s", key));
2035	ATF_REQUIRE(nvlist_exists_nvlist(nvl, key));
2036	ATF_REQUIRE(nvlist_existsf_nvlist(nvl, "%s", key));
2037
2038	value = nvlist_get_nvlist(nvl, key);
2039	ATF_REQUIRE(nvlist_exists_null(value, subkey));
2040
2041	/* nvlist_add_* is required to clone the value, so check for that. */
2042	ATF_REQUIRE(sublist != value);
2043
2044	value = nvlist_getf_nvlist(nvl, "%s", key);
2045	ATF_REQUIRE(nvlist_exists_null(value, subkey));
2046	ATF_REQUIRE(sublist != value);
2047
2048	/* Iterate over the nvlist; ensure that it has only our one key. */
2049	it = NULL;
2050	ATF_REQUIRE_EQ(strcmp(nvlist_next(nvl, &type, &it), key), 0);
2051	ATF_REQUIRE_EQ(type, NV_TYPE_NVLIST);
2052	ATF_REQUIRE_EQ(nvlist_next(nvl, &type,&it), NULL);
2053
2054	nvlist_destroy(sublist);
2055	nvlist_destroy(nvl);
2056}
2057
2058ATF_TEST_CASE_WITHOUT_HEAD(nvlist_add_binary__single_insert);
2059ATF_TEST_CASE_BODY(nvlist_add_binary__single_insert)
2060{
2061	nvlist_t *nvl;
2062	void *it;
2063	const char *key;
2064	void *value;
2065	const void *ret_value;
2066	size_t value_size, ret_size;
2067	int type;
2068
2069	key = "binary";
2070	value_size = 13;
2071	value = malloc(value_size);
2072	memset(value, 0xa5, value_size);
2073	nvl = nvlist_create(0);
2074
2075	ATF_REQUIRE(nvl != NULL);
2076	ATF_REQUIRE(!nvlist_exists(nvl, key));
2077
2078	nvlist_add_binary(nvl, key, value, value_size);
2079
2080	ATF_REQUIRE(!nvlist_empty(nvl));
2081	ATF_REQUIRE(nvlist_exists(nvl, key));
2082	ATF_REQUIRE(nvlist_existsf(nvl, "%s", key));
2083	ATF_REQUIRE(nvlist_exists_binary(nvl, key));
2084	ATF_REQUIRE(nvlist_existsf_binary(nvl, "%s", key));
2085
2086	ret_value = nvlist_get_binary(nvl, key, &ret_size);
2087	ATF_REQUIRE_EQ(value_size, ret_size);
2088	ATF_REQUIRE_EQ(memcmp(value, ret_value, ret_size), 0);
2089
2090	/* nvlist_add_* is required to clone the value, so check for that. */
2091	ATF_REQUIRE(value != ret_value);
2092
2093	ret_value = nvlist_getf_binary(nvl, &ret_size, "%s", key);
2094	ATF_REQUIRE_EQ(value_size, ret_size);
2095	ATF_REQUIRE_EQ(memcmp(value, ret_value, ret_size), 0);
2096	ATF_REQUIRE(value != ret_value);
2097
2098	/* Iterate over the nvlist; ensure that it has only our one key. */
2099	it = NULL;
2100	ATF_REQUIRE_EQ(strcmp(nvlist_next(nvl, &type, &it), key), 0);
2101	ATF_REQUIRE_EQ(type, NV_TYPE_BINARY);
2102	ATF_REQUIRE_EQ(nvlist_next(nvl, &type,&it), NULL);
2103
2104	nvlist_destroy(nvl);
2105	free(value);
2106}
2107
2108ATF_TEST_CASE_WITHOUT_HEAD(nvlist_clone__empty_nvlist);
2109ATF_TEST_CASE_BODY(nvlist_clone__empty_nvlist)
2110{
2111	nvlist_t *nvl, *clone;
2112
2113	nvl = nvlist_create(0);
2114	ATF_REQUIRE(nvl != NULL);
2115
2116	clone = nvlist_clone(nvl);
2117	ATF_REQUIRE(clone != NULL);
2118	ATF_REQUIRE(clone != nvl);
2119	ATF_REQUIRE(nvlist_empty(clone));
2120
2121	nvlist_destroy(clone);
2122	nvlist_destroy(nvl);
2123}
2124
2125ATF_TEST_CASE_WITHOUT_HEAD(nvlist_clone__nonempty_nvlist);
2126ATF_TEST_CASE_BODY(nvlist_clone__nonempty_nvlist)
2127{
2128	nvlist_t *nvl, *clone;
2129	const char *key;
2130	void *it;
2131	uint64_t value;
2132	int type;
2133
2134	nvl = nvlist_create(0);
2135	ATF_REQUIRE(nvl != NULL);
2136
2137	key = "testkey";
2138	value = 684874;
2139	nvlist_add_number(nvl, key, value);
2140
2141	clone = nvlist_clone(nvl);
2142	ATF_REQUIRE(clone != NULL);
2143	ATF_REQUIRE(clone != nvl);
2144	ATF_REQUIRE(nvlist_exists_number(clone, key));
2145	ATF_REQUIRE_EQ(nvlist_get_number(clone, key), value);
2146
2147	/* Iterate over the nvlist; ensure that it has only our one key. */
2148	it = NULL;
2149	ATF_REQUIRE_EQ(strcmp(nvlist_next(clone, &type, &it), key), 0);
2150	ATF_REQUIRE_EQ(type, NV_TYPE_NUMBER);
2151	ATF_REQUIRE_EQ(nvlist_next(clone, &type, &it), NULL);
2152
2153	nvlist_destroy(clone);
2154	nvlist_destroy(nvl);
2155}
2156
2157static const char * const test_subnvlist_key = "nvlist";
2158
2159static const char * const test_string_key = "string";
2160static const char * const test_string_val = "59525";
2161
2162static nvlist_t*
2163create_test_nvlist(void)
2164{
2165	nvlist_t *nvl, *sublist;
2166
2167	nvl = nvlist_create(0);
2168	ATF_REQUIRE(nvl != NULL);
2169
2170	sublist = nvlist_create(0);
2171	ATF_REQUIRE(sublist != NULL);
2172
2173	nvlist_add_string(sublist, test_string_key, test_string_val);
2174	nvlist_move_nvlist(nvl, test_subnvlist_key, sublist);
2175
2176	return (nvl);
2177}
2178
2179static void
2180verify_test_nvlist(const nvlist_t *nvl)
2181{
2182	void *it;
2183	const nvlist_t *value;
2184	int type;
2185
2186	ATF_REQUIRE(nvlist_exists_nvlist(nvl, test_subnvlist_key));
2187
2188	value = nvlist_get_nvlist(nvl, test_subnvlist_key);
2189
2190	ATF_REQUIRE(nvlist_exists_string(value, test_string_key));
2191	ATF_REQUIRE_EQ(strcmp(nvlist_get_string(value, test_string_key), test_string_val), 0);
2192	ATF_REQUIRE(nvlist_get_string(value, test_string_key) != test_string_val);
2193
2194	/* Iterate over both nvlists; ensure that each has only the one key. */
2195	it = NULL;
2196	ATF_REQUIRE_EQ(strcmp(nvlist_next(value, &type, &it),
2197	    test_string_key), 0);
2198	ATF_REQUIRE_EQ(type, NV_TYPE_STRING);
2199	ATF_REQUIRE_EQ(nvlist_next(value, &type, &it), NULL);
2200
2201	it = NULL;
2202	ATF_REQUIRE_EQ(strcmp(nvlist_next(nvl, &type, &it),
2203	    test_subnvlist_key), 0);
2204	ATF_REQUIRE_EQ(type, NV_TYPE_NVLIST);
2205	ATF_REQUIRE_EQ(nvlist_next(nvl, &type, &it), NULL);
2206}
2207
2208ATF_TEST_CASE_WITHOUT_HEAD(nvlist_clone__nested_nvlist);
2209ATF_TEST_CASE_BODY(nvlist_clone__nested_nvlist)
2210{
2211	nvlist_t *nvl, *clone;
2212
2213	nvl = create_test_nvlist();
2214	clone = nvlist_clone(nvl);
2215
2216	ATF_REQUIRE(clone != NULL);
2217	ATF_REQUIRE(clone != nvl);
2218	verify_test_nvlist(clone);
2219
2220	nvlist_destroy(clone);
2221	nvlist_destroy(nvl);
2222}
2223
2224ATF_TEST_CASE_WITHOUT_HEAD(nvlist_pack__empty_nvlist);
2225ATF_TEST_CASE_BODY(nvlist_pack__empty_nvlist)
2226{
2227	nvlist_t *nvl, *unpacked;
2228	void *packed;
2229	size_t packed_size;
2230
2231	nvl = nvlist_create(0);
2232	ATF_REQUIRE(nvl != NULL);
2233
2234	packed = nvlist_pack(nvl, &packed_size);
2235	ATF_REQUIRE(packed != NULL);
2236
2237	unpacked = nvlist_unpack(packed, packed_size);
2238	ATF_REQUIRE(unpacked != NULL);
2239	ATF_REQUIRE(unpacked != nvl);
2240	ATF_REQUIRE(nvlist_empty(unpacked));
2241
2242	nvlist_destroy(unpacked);
2243	nvlist_destroy(nvl);
2244	free(packed);
2245}
2246
2247static void
2248verify_null(const nvlist_t *nvl, int type)
2249{
2250
2251	ATF_REQUIRE_EQ(type, NV_TYPE_NULL);
2252}
2253
2254static void
2255verify_number(const nvlist_t *nvl, const char *name, int type, uint64_t value)
2256{
2257
2258	ATF_REQUIRE_EQ(type, NV_TYPE_NUMBER);
2259	ATF_REQUIRE_EQ(nvlist_get_number(nvl, name), value);
2260}
2261
2262static void
2263verify_string(const nvlist_t *nvl, const char *name, int type,
2264    const char * value)
2265{
2266
2267	ATF_REQUIRE_EQ(type, NV_TYPE_STRING);
2268	ATF_REQUIRE_EQ(strcmp(nvlist_get_string(nvl, name), value), 0);
2269}
2270
2271static void
2272verify_nvlist(const nvlist_t *nvl, const char *name, int type)
2273{
2274
2275	ATF_REQUIRE_EQ(type, NV_TYPE_NVLIST);
2276	verify_test_nvlist(nvlist_get_nvlist(nvl, name));
2277}
2278
2279static void
2280verify_binary(const nvlist_t *nvl, const char *name, int type,
2281    const void * value, size_t size)
2282{
2283	const void *actual_value;
2284	size_t actual_size;
2285
2286	ATF_REQUIRE_EQ(type, NV_TYPE_BINARY);
2287	actual_value = nvlist_get_binary(nvl, name, &actual_size);
2288	ATF_REQUIRE_EQ(size, actual_size);
2289	ATF_REQUIRE_EQ(memcmp(value, actual_value, size), 0);
2290}
2291
2292ATF_TEST_CASE_WITHOUT_HEAD(nvlist_pack__multiple_values);
2293ATF_TEST_CASE_BODY(nvlist_pack__multiple_values)
2294{
2295	std::ostringstream msg;
2296	std::set<std::string> keys_seen;
2297	nvlist_t *nvl, *unpacked, *nvvalue;
2298	const char *nullkey, *numkey, *strkey, *nvkey, *binkey, *name;
2299	int numvalue;
2300	const char * strvalue;
2301	void *binvalue, *packed, *it;
2302	size_t binsize, packed_size;
2303	int type;
2304
2305	nvl = nvlist_create(0);
2306
2307	nullkey = "null";
2308	nvlist_add_null(nvl, nullkey);
2309
2310	numkey = "number";
2311	numvalue = 939853984;
2312	nvlist_add_number(nvl, numkey, numvalue);
2313
2314	strkey = "string";
2315	strvalue = "jfieutijf";
2316	nvlist_add_string(nvl, strkey, strvalue);
2317
2318	nvkey = "nvlist";
2319	nvvalue = create_test_nvlist();
2320	nvlist_move_nvlist(nvl, nvkey, nvvalue);
2321
2322	binkey = "binary";
2323	binsize = 4;
2324	binvalue = malloc(binsize);
2325	memset(binvalue, 'b', binsize);
2326	nvlist_move_binary(nvl, binkey, binvalue, binsize);
2327
2328	packed = nvlist_pack(nvl, &packed_size);
2329	ATF_REQUIRE(packed != NULL);
2330
2331	unpacked = nvlist_unpack(packed, packed_size);
2332	ATF_REQUIRE(unpacked != 0);
2333
2334	it = NULL;
2335	while ((name = nvlist_next(unpacked, &type, &it)) != NULL) {
2336		/* Ensure that we see every key only once. */
2337		ATF_REQUIRE_EQ(keys_seen.count(name), 0);
2338
2339		if (strcmp(name, nullkey) == 0)
2340			verify_null(unpacked, type);
2341		else if (strcmp(name, numkey) == 0)
2342			verify_number(unpacked, name, type, numvalue);
2343		else if (strcmp(name, strkey) == 0)
2344			verify_string(unpacked, name, type, strvalue);
2345		else if (strcmp(name, nvkey) == 0)
2346			verify_nvlist(unpacked, name, type);
2347		else if (strcmp(name, binkey) == 0)
2348			verify_binary(unpacked, name, type, binvalue, binsize);
2349		else {
2350			msg << "Unexpected key :'" << name << "'";
2351			ATF_FAIL(msg.str().c_str());
2352		}
2353
2354		keys_seen.insert(name);
2355	}
2356
2357	/* Ensure that we saw every key. */
2358	ATF_REQUIRE_EQ(keys_seen.size(), 5);
2359
2360	nvlist_destroy(nvl);
2361	nvlist_destroy(unpacked);
2362	free(packed);
2363}
2364
2365ATF_TEST_CASE_WITHOUT_HEAD(nvlist_unpack__duplicate_key);
2366ATF_TEST_CASE_BODY(nvlist_unpack__duplicate_key)
2367{
2368	nvlist_t *nvl, *unpacked;
2369	const char *key1, *key2;
2370	void *packed, *keypos;
2371	size_t size, keylen;
2372
2373	nvl = nvlist_create(0);
2374
2375	key1 = "key1";
2376	keylen = strlen(key1);
2377	nvlist_add_number(nvl, key1, 5);
2378
2379	key2 = "key2";
2380	ATF_REQUIRE_EQ(keylen, strlen(key2));
2381	nvlist_add_number(nvl, key2, 10);
2382
2383	packed = nvlist_pack(nvl, &size);
2384
2385	/*
2386	 * Mangle the packed nvlist by replacing key1 with key2, creating a
2387	 * packed nvlist with a duplicate key.
2388	 */
2389	keypos = memmem(packed, size, key1, keylen);
2390	ATF_REQUIRE(keypos != NULL);
2391	memcpy(keypos, key2, keylen);
2392
2393	unpacked = nvlist_unpack(packed, size);
2394	ATF_REQUIRE(nvlist_error(unpacked) != 0);
2395
2396	free(packed);
2397	nvlist_destroy(nvl);
2398	nvlist_destroy(unpacked);
2399}
2400
2401ATF_TEST_CASE_WITHOUT_HEAD(nvlist_move_string__single_insert);
2402ATF_TEST_CASE_BODY(nvlist_move_string__single_insert)
2403{
2404	nvlist_t *nvl;
2405	const char *key;
2406	char *value;
2407
2408	nvl = nvlist_create(0);
2409	ATF_REQUIRE(nvl != NULL);
2410
2411	key = "testkey";
2412	value = strdup("testval");
2413	ATF_REQUIRE(value != NULL);
2414
2415	nvlist_move_string(nvl, key, value);
2416	ATF_REQUIRE_EQ(nvlist_get_string(nvl, key), value);
2417
2418	nvlist_destroy(nvl);
2419}
2420
2421ATF_TEST_CASE_WITHOUT_HEAD(nvlist_move_nvlist__null_child);
2422ATF_TEST_CASE_BODY(nvlist_move_nvlist__null_child)
2423{
2424	nvlist_t *parent;
2425
2426	parent = nvlist_create(0);
2427
2428	nvlist_move_nvlist(parent, "test", NULL);
2429
2430	ATF_REQUIRE(nvlist_error(parent) != 0);
2431
2432	nvlist_destroy(parent);
2433}
2434
2435ATF_TEST_CASE_WITHOUT_HEAD(nvlist_move_nvlist__single_insert);
2436ATF_TEST_CASE_BODY(nvlist_move_nvlist__single_insert)
2437{
2438	nvlist_t *nvl;
2439	const char *key;
2440	nvlist_t *value;
2441
2442	nvl = nvlist_create(0);
2443	ATF_REQUIRE(nvl != NULL);
2444
2445	key = "testkey";
2446	value = nvlist_create(0);
2447	ATF_REQUIRE(value != NULL);
2448
2449	nvlist_move_nvlist(nvl, key, value);
2450	ATF_REQUIRE_EQ(nvlist_get_nvlist(nvl, key), value);
2451
2452	nvlist_destroy(nvl);
2453}
2454
2455ATF_TEST_CASE_WITHOUT_HEAD(nvlist_move_binary__single_insert);
2456ATF_TEST_CASE_BODY(nvlist_move_binary__single_insert)
2457{
2458	nvlist_t *nvl;
2459	const char *key;
2460	void *value;
2461	size_t size, actual_size;
2462
2463	nvl = nvlist_create(0);
2464	ATF_REQUIRE(nvl != NULL);
2465
2466	key = "testkey";
2467	size = 73;
2468	value = malloc(size);
2469	ATF_REQUIRE(value != NULL);
2470
2471	nvlist_move_binary(nvl, key, value, size);
2472	ATF_REQUIRE_EQ(nvlist_get_binary(nvl, key, &actual_size), value);
2473	ATF_REQUIRE_EQ(size, actual_size);
2474
2475	nvlist_destroy(nvl);
2476}
2477
2478ATF_TEST_CASE_WITHOUT_HEAD(nvlist_take_bool__single_remove);
2479ATF_TEST_CASE_BODY(nvlist_take_bool__single_remove)
2480{
2481	nvlist_t *nvl;
2482	const char *testkey;
2483	bool testval;
2484
2485	nvl = nvlist_create(0);
2486	ATF_REQUIRE(nvl != NULL);
2487
2488	testkey = "boolkey";
2489	testval = false;
2490	nvlist_add_bool(nvl, testkey, testval);
2491
2492	ATF_REQUIRE_EQ(nvlist_take_bool(nvl, testkey), testval);
2493	ATF_REQUIRE(nvlist_empty(nvl));
2494
2495	nvlist_destroy(nvl);
2496}
2497
2498ATF_TEST_CASE_WITHOUT_HEAD(nvlist_take_bool__other_keys_unchanged);
2499ATF_TEST_CASE_BODY(nvlist_take_bool__other_keys_unchanged)
2500{
2501	nvlist_t *nvl;
2502	const char *testkey, *otherkey1, *otherkey2;
2503	bool testval, otherval1;
2504	nvlist_t *otherval2;
2505
2506	nvl = nvlist_create(0);
2507	ATF_REQUIRE(nvl != NULL);
2508
2509	testkey = "boolkey";
2510	testval = true;
2511	nvlist_add_bool(nvl, testkey, testval);
2512
2513	otherkey1 = "key1";
2514	otherval1 = false;
2515	nvlist_add_bool(nvl, otherkey1, otherval1);
2516
2517	otherkey2 = "key2";
2518	otherval2 = create_test_nvlist();
2519	nvlist_move_nvlist(nvl, otherkey2, otherval2);
2520
2521	ATF_REQUIRE_EQ(nvlist_take_bool(nvl, testkey), testval);
2522
2523	ATF_REQUIRE(nvlist_exists_bool(nvl, otherkey1));
2524	ATF_REQUIRE_EQ(nvlist_get_bool(nvl, otherkey1), otherval1);
2525
2526	ATF_REQUIRE(nvlist_exists_nvlist(nvl, otherkey2));
2527	verify_test_nvlist(nvlist_get_nvlist(nvl, otherkey2));
2528
2529	nvlist_destroy(nvl);
2530}
2531
2532ATF_TEST_CASE_WITHOUT_HEAD(nvlist_take_number__single_remove);
2533ATF_TEST_CASE_BODY(nvlist_take_number__single_remove)
2534{
2535	nvlist_t *nvl;
2536	const char *testkey;
2537	uint64_t testval;
2538
2539	nvl = nvlist_create(0);
2540	ATF_REQUIRE(nvl != NULL);
2541
2542	testkey = "numkey";
2543	testval = std::numeric_limits<uint64_t>::max();
2544	nvlist_add_number(nvl, testkey, testval);
2545
2546	ATF_REQUIRE_EQ(nvlist_take_number(nvl, testkey), testval);
2547	ATF_REQUIRE(nvlist_empty(nvl));
2548
2549	nvlist_destroy(nvl);
2550}
2551
2552ATF_TEST_CASE_WITHOUT_HEAD(nvlist_take_number__other_keys_unchanged);
2553ATF_TEST_CASE_BODY(nvlist_take_number__other_keys_unchanged)
2554{
2555	nvlist_t *nvl;
2556	const char *testkey, *otherkey1, *otherkey2;
2557	uint64_t testval, otherval1;
2558	const char *otherval2;
2559
2560	nvl = nvlist_create(0);
2561	ATF_REQUIRE(nvl != NULL);
2562
2563	otherkey1 = "key1";
2564	otherval1 = 5;
2565	nvlist_add_number(nvl, otherkey1, otherval1);
2566
2567	testkey = "numkey";
2568	testval = 1654;
2569	nvlist_add_number(nvl, testkey, testval);
2570
2571	otherkey2 = "key2";
2572	otherval2 = "string";
2573	nvlist_add_string(nvl, otherkey2, otherval2);
2574
2575	ATF_REQUIRE_EQ(nvlist_take_number(nvl, testkey), testval);
2576
2577	ATF_REQUIRE(nvlist_exists_number(nvl, otherkey1));
2578	ATF_REQUIRE_EQ(nvlist_get_number(nvl, otherkey1), otherval1);
2579
2580	ATF_REQUIRE(nvlist_exists_string(nvl, otherkey2));
2581	ATF_REQUIRE_EQ(strcmp(nvlist_get_string(nvl, otherkey2), otherval2), 0);
2582
2583	nvlist_destroy(nvl);
2584}
2585
2586ATF_TEST_CASE_WITHOUT_HEAD(nvlist_take_string__single_remove);
2587ATF_TEST_CASE_BODY(nvlist_take_string__single_remove)
2588{
2589	nvlist_t *nvl;
2590	const char *testkey;
2591	const char *testval;
2592
2593	nvl = nvlist_create(0);
2594	ATF_REQUIRE(nvl != NULL);
2595
2596	testkey = "numkey";
2597	testval = "nvlist";
2598	nvlist_add_string(nvl, testkey, testval);
2599
2600	ATF_REQUIRE_EQ(strcmp(nvlist_take_string(nvl, testkey), testval), 0);
2601	ATF_REQUIRE(nvlist_empty(nvl));
2602
2603	nvlist_destroy(nvl);
2604}
2605
2606ATF_TEST_CASE_WITHOUT_HEAD(nvlist_take_string__other_keys_unchanged);
2607ATF_TEST_CASE_BODY(nvlist_take_string__other_keys_unchanged)
2608{
2609	nvlist_t *nvl;
2610	const char *testkey, *otherkey1, *otherkey2;
2611	const char *testval, *otherval1;
2612	bool otherval2;
2613
2614	nvl = nvlist_create(0);
2615	ATF_REQUIRE(nvl != NULL);
2616
2617	otherkey1 = "key1";
2618	otherval1 = "fjdifjdk";
2619	nvlist_add_string(nvl, otherkey1, otherval1);
2620
2621	otherkey2 = "key2";
2622	otherval2 = true;
2623	nvlist_add_bool(nvl, otherkey2, otherval2);
2624
2625	testkey = "strkey";
2626	testval = "1654";
2627	nvlist_add_string(nvl, testkey, testval);
2628
2629	ATF_REQUIRE_EQ(strcmp(nvlist_take_string(nvl, testkey), testval), 0);
2630
2631	ATF_REQUIRE(nvlist_exists_string(nvl, otherkey1));
2632	ATF_REQUIRE_EQ(strcmp(nvlist_get_string(nvl, otherkey1), otherval1), 0);
2633
2634	ATF_REQUIRE(nvlist_exists_bool(nvl, otherkey2));
2635	ATF_REQUIRE_EQ(nvlist_get_bool(nvl, otherkey2), otherval2);
2636
2637	nvlist_destroy(nvl);
2638}
2639
2640ATF_TEST_CASE_WITHOUT_HEAD(nvlist_take_nvlist__single_remove);
2641ATF_TEST_CASE_BODY(nvlist_take_nvlist__single_remove)
2642{
2643	nvlist_t *nvl;
2644	const char *testkey;
2645	nvlist_t *testval;
2646
2647	nvl = nvlist_create(0);
2648	ATF_REQUIRE(nvl != NULL);
2649
2650	testkey = "numkey";
2651	testval = create_test_nvlist();
2652	nvlist_move_nvlist(nvl, testkey, testval);
2653
2654	verify_test_nvlist(nvlist_take_nvlist(nvl, testkey));
2655	ATF_REQUIRE(nvlist_empty(nvl));
2656
2657	nvlist_destroy(nvl);
2658}
2659
2660ATF_TEST_CASE_WITHOUT_HEAD(nvlist_take_nvlist__other_keys_unchanged);
2661ATF_TEST_CASE_BODY(nvlist_take_nvlist__other_keys_unchanged)
2662{
2663	nvlist_t *nvl;
2664	const char *testkey, *otherkey1, *otherkey2;
2665	nvlist_t *testval, *otherval1;
2666
2667	nvl = nvlist_create(0);
2668	ATF_REQUIRE(nvl != NULL);
2669
2670	testkey = "strkey";
2671	testval = create_test_nvlist();
2672	nvlist_move_nvlist(nvl, testkey, testval);
2673
2674	otherkey1 = "key1";
2675	otherval1 = nvlist_create(0);
2676	nvlist_move_nvlist(nvl, otherkey1, otherval1);
2677
2678	otherkey2 = "key2";
2679	nvlist_add_null(nvl, otherkey2);
2680
2681	verify_test_nvlist(nvlist_take_nvlist(nvl, testkey));
2682
2683	ATF_REQUIRE(nvlist_exists_nvlist(nvl, otherkey1));
2684	ATF_REQUIRE(nvlist_empty(nvlist_get_nvlist(nvl, otherkey1)));
2685
2686	ATF_REQUIRE(nvlist_exists_null(nvl, otherkey2));
2687
2688	nvlist_destroy(nvl);
2689}
2690
2691ATF_TEST_CASE_WITHOUT_HEAD(nvlist_take_binary__single_remove);
2692ATF_TEST_CASE_BODY(nvlist_take_binary__single_remove)
2693{
2694	nvlist_t *nvl;
2695	const char *testkey;
2696	void *testval;
2697	const void *actual_val;
2698	size_t testsize, actual_size;
2699
2700	nvl = nvlist_create(0);
2701	ATF_REQUIRE(nvl != NULL);
2702
2703	testkey = "numkey";
2704	testsize = 457;
2705	testval = malloc(testsize);
2706	memset(testval, '5', testsize);
2707	nvlist_move_binary(nvl, testkey, testval, testsize);
2708
2709	actual_val = nvlist_take_binary(nvl, testkey, &actual_size);
2710	ATF_REQUIRE_EQ(testsize, actual_size);
2711	ATF_REQUIRE_EQ(memcmp(actual_val, testval, testsize), 0);
2712	ATF_REQUIRE(nvlist_empty(nvl));
2713
2714	nvlist_destroy(nvl);
2715}
2716
2717ATF_TEST_CASE_WITHOUT_HEAD(nvlist_take_binary__other_keys_unchanged);
2718ATF_TEST_CASE_BODY(nvlist_take_binary__other_keys_unchanged)
2719{
2720	nvlist_t *nvl;
2721	const char *testkey, *otherkey1, *otherkey2;
2722	const void *actual_value;
2723	char testval[] = "gjiertj";
2724	char otherval1[] = "fdreg";
2725	size_t testsize, othersize, actual_size;
2726	bool otherval2;
2727
2728	nvl = nvlist_create(0);
2729	ATF_REQUIRE(nvl != NULL);
2730
2731	otherkey1 = "key1";
2732	othersize = sizeof(otherval1);
2733	nvlist_add_binary(nvl, otherkey1, otherval1, othersize);
2734
2735	otherkey2 = "key2";
2736	otherval2 = true;
2737	nvlist_add_bool(nvl, otherkey2, otherval2);
2738
2739	testkey = "strkey";
2740	testsize = sizeof(testval);
2741	nvlist_add_binary(nvl, testkey, testval, testsize);
2742
2743	actual_value = nvlist_take_binary(nvl, testkey, &actual_size);
2744	ATF_REQUIRE_EQ(testsize, actual_size);
2745	ATF_REQUIRE_EQ(memcmp(actual_value, testval, testsize), 0);
2746
2747	ATF_REQUIRE(nvlist_exists_binary(nvl, otherkey1));
2748	actual_value = nvlist_get_binary(nvl, otherkey1, &actual_size);
2749	ATF_REQUIRE_EQ(othersize, actual_size);
2750	ATF_REQUIRE_EQ(memcmp(actual_value, otherval1, othersize), 0);
2751
2752	ATF_REQUIRE(nvlist_exists_bool(nvl, otherkey2));
2753	ATF_REQUIRE_EQ(nvlist_get_bool(nvl, otherkey2), otherval2);
2754
2755	nvlist_destroy(nvl);
2756}
2757
2758ATF_TEST_CASE_WITHOUT_HEAD(nvlist_free__single_null);
2759ATF_TEST_CASE_BODY(nvlist_free__single_null)
2760{
2761	nvlist_t *nvl;
2762	const char *key;
2763
2764	nvl = nvlist_create(0);
2765	key = "test";
2766	nvlist_add_null(nvl, key);
2767
2768	nvlist_free(nvl, key);
2769	ATF_REQUIRE(nvlist_empty(nvl));
2770
2771	nvlist_destroy(nvl);
2772}
2773
2774ATF_TEST_CASE_WITHOUT_HEAD(nvlist_free__single_bool);
2775ATF_TEST_CASE_BODY(nvlist_free__single_bool)
2776{
2777	nvlist_t *nvl;
2778	const char *key;
2779
2780	nvl = nvlist_create(0);
2781	key = "test";
2782	nvlist_add_bool(nvl, key, true);
2783
2784	nvlist_free(nvl, key);
2785	ATF_REQUIRE(nvlist_empty(nvl));
2786
2787	nvlist_destroy(nvl);
2788}
2789
2790ATF_TEST_CASE_WITHOUT_HEAD(nvlist_free__single_number);
2791ATF_TEST_CASE_BODY(nvlist_free__single_number)
2792{
2793	nvlist_t *nvl;
2794	const char *key;
2795
2796	nvl = nvlist_create(0);
2797	key = "test";
2798	nvlist_add_number(nvl, key, 584);
2799
2800	nvlist_free(nvl, key);
2801	ATF_REQUIRE(nvlist_empty(nvl));
2802
2803	nvlist_destroy(nvl);
2804}
2805
2806ATF_TEST_CASE_WITHOUT_HEAD(nvlist_free__single_string);
2807ATF_TEST_CASE_BODY(nvlist_free__single_string)
2808{
2809	nvlist_t *nvl;
2810	const char *key;
2811
2812	nvl = nvlist_create(0);
2813	key = "test";
2814	nvlist_add_string(nvl, key, "gjkfkjd");
2815
2816	nvlist_free(nvl, key);
2817	ATF_REQUIRE(nvlist_empty(nvl));
2818
2819	nvlist_destroy(nvl);
2820}
2821
2822ATF_TEST_CASE_WITHOUT_HEAD(nvlist_free__single_nvlist);
2823ATF_TEST_CASE_BODY(nvlist_free__single_nvlist)
2824{
2825	nvlist_t *nvl;
2826	const char *key;
2827
2828	nvl = nvlist_create(0);
2829	key = "test";
2830	nvlist_add_nvlist(nvl, key, nvlist_create(0));
2831
2832	nvlist_free(nvl, key);
2833	ATF_REQUIRE(nvlist_empty(nvl));
2834
2835	nvlist_destroy(nvl);
2836}
2837
2838ATF_TEST_CASE_WITHOUT_HEAD(nvlist_free__single_binary);
2839ATF_TEST_CASE_BODY(nvlist_free__single_binary)
2840{
2841	nvlist_t *nvl;
2842	const char *key;
2843
2844	nvl = nvlist_create(0);
2845	key = "test";
2846	nvlist_add_binary(nvl, key, "jgjgfd", 6);
2847
2848	nvlist_free(nvl, key);
2849	ATF_REQUIRE(nvlist_empty(nvl));
2850
2851	nvlist_destroy(nvl);
2852}
2853
2854ATF_TEST_CASE_WITHOUT_HEAD(nvlist_free_null__single_null);
2855ATF_TEST_CASE_BODY(nvlist_free_null__single_null)
2856{
2857	nvlist_t *nvl;
2858	const char *key;
2859
2860	nvl = nvlist_create(0);
2861	key = "test";
2862	nvlist_add_null(nvl, key);
2863
2864	nvlist_free_null(nvl, key);
2865	ATF_REQUIRE(nvlist_empty(nvl));
2866
2867	nvlist_destroy(nvl);
2868}
2869
2870ATF_TEST_CASE_WITHOUT_HEAD(nvlist_free_bool__single_bool);
2871ATF_TEST_CASE_BODY(nvlist_free_bool__single_bool)
2872{
2873	nvlist_t *nvl;
2874	const char *key;
2875
2876	nvl = nvlist_create(0);
2877	key = "test";
2878	nvlist_add_bool(nvl, key, true);
2879
2880	nvlist_free_bool(nvl, key);
2881	ATF_REQUIRE(nvlist_empty(nvl));
2882
2883	nvlist_destroy(nvl);
2884}
2885
2886ATF_TEST_CASE_WITHOUT_HEAD(nvlist_free_number__single_number);
2887ATF_TEST_CASE_BODY(nvlist_free_number__single_number)
2888{
2889	nvlist_t *nvl;
2890	const char *key;
2891
2892	nvl = nvlist_create(0);
2893	key = "test";
2894	nvlist_add_number(nvl, key, 584);
2895
2896	nvlist_free_number(nvl, key);
2897	ATF_REQUIRE(nvlist_empty(nvl));
2898
2899	nvlist_destroy(nvl);
2900}
2901
2902ATF_TEST_CASE_WITHOUT_HEAD(nvlist_free_string__single_string);
2903ATF_TEST_CASE_BODY(nvlist_free_string__single_string)
2904{
2905	nvlist_t *nvl;
2906	const char *key;
2907
2908	nvl = nvlist_create(0);
2909	key = "test";
2910	nvlist_add_string(nvl, key, "gjkfkjd");
2911
2912	nvlist_free_string(nvl, key);
2913	ATF_REQUIRE(nvlist_empty(nvl));
2914
2915	nvlist_destroy(nvl);
2916}
2917
2918ATF_TEST_CASE_WITHOUT_HEAD(nvlist_free_nvlist__single_nvlist);
2919ATF_TEST_CASE_BODY(nvlist_free_nvlist__single_nvlist)
2920{
2921	nvlist_t *nvl;
2922	const char *key;
2923
2924	nvl = nvlist_create(0);
2925	key = "test";
2926	nvlist_add_nvlist(nvl, key, nvlist_create(0));
2927
2928	nvlist_free_nvlist(nvl, key);
2929	ATF_REQUIRE(nvlist_empty(nvl));
2930
2931	nvlist_destroy(nvl);
2932}
2933
2934ATF_TEST_CASE_WITHOUT_HEAD(nvlist_free_binary__single_binary);
2935ATF_TEST_CASE_BODY(nvlist_free_binary__single_binary)
2936{
2937	nvlist_t *nvl;
2938	const char *key;
2939
2940	nvl = nvlist_create(0);
2941	key = "test";
2942	nvlist_add_binary(nvl, key, "jgjgfd", 6);
2943
2944	nvlist_free_binary(nvl, key);
2945	ATF_REQUIRE(nvlist_empty(nvl));
2946
2947	nvlist_destroy(nvl);
2948}
2949
2950ATF_INIT_TEST_CASES(tp)
2951{
2952	ATF_ADD_TEST_CASE(tp, nvlist_create__is_empty);
2953	ATF_ADD_TEST_CASE(tp, nvlist_add_null__single_insert);
2954	ATF_ADD_TEST_CASE(tp, nvlist_add_bool__single_insert);
2955	ATF_ADD_TEST_CASE(tp, nvlist_add_number__single_insert);
2956	ATF_ADD_TEST_CASE(tp, nvlist_add_string__single_insert);
2957	ATF_ADD_TEST_CASE(tp, nvlist_add_nvlist__single_insert);
2958	ATF_ADD_TEST_CASE(tp, nvlist_add_binary__single_insert);
2959
2960	ATF_ADD_TEST_CASE(tp, nvlist_clone__empty_nvlist);
2961	ATF_ADD_TEST_CASE(tp, nvlist_clone__nonempty_nvlist);
2962	ATF_ADD_TEST_CASE(tp, nvlist_clone__nested_nvlist);
2963
2964	ATF_ADD_TEST_CASE(tp, nvlist_pack__empty_nvlist);
2965	ATF_ADD_TEST_CASE(tp, nvlist_pack__multiple_values);
2966	ATF_ADD_TEST_CASE(tp, nvlist_unpack__duplicate_key);
2967
2968	ATF_ADD_TEST_CASE(tp, nvlist_move_string__single_insert);
2969	ATF_ADD_TEST_CASE(tp, nvlist_move_nvlist__single_insert);
2970	ATF_ADD_TEST_CASE(tp, nvlist_move_nvlist__null_child);
2971	ATF_ADD_TEST_CASE(tp, nvlist_move_binary__single_insert);
2972
2973	ATF_ADD_TEST_CASE(tp, nvlist_take_bool__single_remove);
2974	ATF_ADD_TEST_CASE(tp, nvlist_take_bool__other_keys_unchanged);
2975	ATF_ADD_TEST_CASE(tp, nvlist_take_number__single_remove);
2976	ATF_ADD_TEST_CASE(tp, nvlist_take_number__other_keys_unchanged);
2977	ATF_ADD_TEST_CASE(tp, nvlist_take_string__single_remove);
2978	ATF_ADD_TEST_CASE(tp, nvlist_take_string__other_keys_unchanged);
2979	ATF_ADD_TEST_CASE(tp, nvlist_take_nvlist__single_remove);
2980	ATF_ADD_TEST_CASE(tp, nvlist_take_nvlist__other_keys_unchanged);
2981	ATF_ADD_TEST_CASE(tp, nvlist_take_binary__single_remove);
2982	ATF_ADD_TEST_CASE(tp, nvlist_take_binary__other_keys_unchanged);
2983
2984	ATF_ADD_TEST_CASE(tp, nvlist_free__single_null);
2985	ATF_ADD_TEST_CASE(tp, nvlist_free__single_bool);
2986	ATF_ADD_TEST_CASE(tp, nvlist_free__single_number);
2987	ATF_ADD_TEST_CASE(tp, nvlist_free__single_string);
2988	ATF_ADD_TEST_CASE(tp, nvlist_free__single_nvlist);
2989	ATF_ADD_TEST_CASE(tp, nvlist_free__single_binary);
2990
2991	ATF_ADD_TEST_CASE(tp, nvlist_free_null__single_null);
2992	ATF_ADD_TEST_CASE(tp, nvlist_free_bool__single_bool);
2993	ATF_ADD_TEST_CASE(tp, nvlist_free_number__single_number);
2994	ATF_ADD_TEST_CASE(tp, nvlist_free_string__single_string);
2995	ATF_ADD_TEST_CASE(tp, nvlist_free_nvlist__single_nvlist);
2996	ATF_ADD_TEST_CASE(tp, nvlist_free_binary__single_binary);
2997}
2998