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