nv_tests.cc revision 279428
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 279428 2015-03-01 00:21:43Z 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_INIT_TEST_CASES(tp)
947{
948	ATF_ADD_TEST_CASE(tp, nvlist_create__is_empty);
949	ATF_ADD_TEST_CASE(tp, nvlist_add_null__single_insert);
950	ATF_ADD_TEST_CASE(tp, nvlist_add_bool__single_insert);
951	ATF_ADD_TEST_CASE(tp, nvlist_add_number__single_insert);
952	ATF_ADD_TEST_CASE(tp, nvlist_add_string__single_insert);
953	ATF_ADD_TEST_CASE(tp, nvlist_add_nvlist__single_insert);
954	ATF_ADD_TEST_CASE(tp, nvlist_add_binary__single_insert);
955
956	ATF_ADD_TEST_CASE(tp, nvlist_clone__empty_nvlist);
957	ATF_ADD_TEST_CASE(tp, nvlist_clone__nonempty_nvlist);
958	ATF_ADD_TEST_CASE(tp, nvlist_clone__nested_nvlist);
959
960	ATF_ADD_TEST_CASE(tp, nvlist_pack__empty_nvlist);
961	ATF_ADD_TEST_CASE(tp, nvlist_pack__multiple_values);
962	ATF_ADD_TEST_CASE(tp, nvlist_unpack__duplicate_key);
963
964	ATF_ADD_TEST_CASE(tp, nvlist_move_string__single_insert);
965	ATF_ADD_TEST_CASE(tp, nvlist_move_nvlist__single_insert);
966	ATF_ADD_TEST_CASE(tp, nvlist_move_nvlist__null_child);
967	ATF_ADD_TEST_CASE(tp, nvlist_move_binary__single_insert);
968
969	ATF_ADD_TEST_CASE(tp, nvlist_take_bool__single_remove);
970	ATF_ADD_TEST_CASE(tp, nvlist_take_bool__other_keys_unchanged);
971	ATF_ADD_TEST_CASE(tp, nvlist_take_number__single_remove);
972	ATF_ADD_TEST_CASE(tp, nvlist_take_number__other_keys_unchanged);
973	ATF_ADD_TEST_CASE(tp, nvlist_take_string__single_remove);
974	ATF_ADD_TEST_CASE(tp, nvlist_take_string__other_keys_unchanged);
975	ATF_ADD_TEST_CASE(tp, nvlist_take_nvlist__single_remove);
976	ATF_ADD_TEST_CASE(tp, nvlist_take_nvlist__other_keys_unchanged);
977	ATF_ADD_TEST_CASE(tp, nvlist_take_binary__single_remove);
978	ATF_ADD_TEST_CASE(tp, nvlist_take_binary__other_keys_unchanged);
979}
980/*-
981 * Copyright (c) 2014-2015 Sandvine Inc.  All rights reserved.
982 * All rights reserved.
983 *
984 * Redistribution and use in source and binary forms, with or without
985 * modification, are permitted provided that the following conditions
986 * are met:
987 * 1. Redistributions of source code must retain the above copyright
988 *    notice, this list of conditions and the following disclaimer.
989 * 2. Redistributions in binary form must reproduce the above copyright
990 *    notice, this list of conditions and the following disclaimer in the
991 *    documentation and/or other materials provided with the distribution.
992 *
993 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
994 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
995 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
996 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
997 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
998 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
999 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
1000 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
1001 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
1002 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
1003 * SUCH DAMAGE.
1004 */
1005
1006#include <atf-c++.hpp>
1007#include <nv.h>
1008
1009#include <errno.h>
1010/*
1011 * Test that a newly created nvlist has no errors, and is empty.
1012 */
1013ATF_TEST_CASE_WITHOUT_HEAD(nvlist_create__is_empty);
1014ATF_TEST_CASE_BODY(nvlist_create__is_empty)
1015{
1016	nvlist_t *nvl;
1017	int type;
1018	void *it;
1019
1020	nvl = nvlist_create(0);
1021
1022	ATF_REQUIRE(nvl != NULL);
1023
1024	ATF_REQUIRE_EQ(nvlist_error(nvl), 0);
1025	ATF_REQUIRE(nvlist_empty(nvl));
1026
1027	it = NULL;
1028	ATF_REQUIRE_EQ(nvlist_next(nvl, &type, &it), NULL);
1029
1030	nvlist_destroy(nvl);
1031}
1032
1033ATF_TEST_CASE_WITHOUT_HEAD(nvlist_add_null__single_insert);
1034ATF_TEST_CASE_BODY(nvlist_add_null__single_insert)
1035{
1036	nvlist_t *nvl;
1037	void *it;
1038	const char *key;
1039	int type;
1040
1041	key = "key";
1042	nvl = nvlist_create(0);
1043
1044	ATF_REQUIRE(nvl != NULL);
1045	ATF_REQUIRE(!nvlist_exists(nvl, key));
1046
1047	nvlist_add_null(nvl, key);
1048
1049	ATF_REQUIRE(!nvlist_empty(nvl));
1050	ATF_REQUIRE(nvlist_exists(nvl, key));
1051	ATF_REQUIRE(nvlist_existsf(nvl, "%s", key));
1052	ATF_REQUIRE(nvlist_exists_null(nvl, key));
1053	ATF_REQUIRE(nvlist_existsf_null(nvl, "key"));
1054
1055	/* Iterate over the nvlist; ensure that it has only our one key. */
1056	it = NULL;
1057	ATF_REQUIRE_EQ(strcmp(nvlist_next(nvl, &type, &it), key), 0);
1058	ATF_REQUIRE_EQ(type, NV_TYPE_NULL);
1059	ATF_REQUIRE_EQ(nvlist_next(nvl, &type,&it), NULL);
1060
1061	nvlist_destroy(nvl);
1062}
1063
1064ATF_TEST_CASE_WITHOUT_HEAD(nvlist_add_bool__single_insert);
1065ATF_TEST_CASE_BODY(nvlist_add_bool__single_insert)
1066{
1067	nvlist_t *nvl;
1068	void *it;
1069	const char *key;
1070	int type;
1071
1072	key = "name";
1073	nvl = nvlist_create(0);
1074
1075	ATF_REQUIRE(nvl != NULL);
1076	ATF_REQUIRE(!nvlist_exists(nvl, key));
1077
1078	nvlist_add_bool(nvl, key, true);
1079
1080	ATF_REQUIRE(!nvlist_empty(nvl));
1081	ATF_REQUIRE(nvlist_exists(nvl, key));
1082	ATF_REQUIRE(nvlist_existsf(nvl, "%s%s", "na", "me"));
1083	ATF_REQUIRE(nvlist_exists_bool(nvl, key));
1084	ATF_REQUIRE(nvlist_existsf_bool(nvl, "%s%c", "nam", 'e'));
1085	ATF_REQUIRE_EQ(nvlist_get_bool(nvl, key), true);
1086	ATF_REQUIRE_EQ(nvlist_getf_bool(nvl, "%c%s", 'n', "ame"), true);
1087
1088	/* Iterate over the nvlist; ensure that it has only our one key. */
1089	it = NULL;
1090	ATF_REQUIRE_EQ(strcmp(nvlist_next(nvl, &type, &it), key), 0);
1091	ATF_REQUIRE_EQ(type, NV_TYPE_BOOL);
1092	ATF_REQUIRE_EQ(nvlist_next(nvl, &type,&it), NULL);
1093
1094	nvlist_destroy(nvl);
1095}
1096
1097ATF_TEST_CASE_WITHOUT_HEAD(nvlist_add_number__single_insert);
1098ATF_TEST_CASE_BODY(nvlist_add_number__single_insert)
1099{
1100	nvlist_t *nvl;
1101	void *it;
1102	const char *key;
1103	uint64_t value;
1104	int type;
1105
1106	key = "foo123";
1107	value = 71965;
1108	nvl = nvlist_create(0);
1109
1110	ATF_REQUIRE(nvl != NULL);
1111	ATF_REQUIRE(!nvlist_exists(nvl, key));
1112
1113	nvlist_add_number(nvl, key, value);
1114
1115	ATF_REQUIRE(!nvlist_empty(nvl));
1116	ATF_REQUIRE(nvlist_exists(nvl, key));
1117	ATF_REQUIRE(nvlist_existsf(nvl, "%s%d", "foo", 123));
1118	ATF_REQUIRE(nvlist_exists_number(nvl, key));
1119	ATF_REQUIRE(nvlist_existsf_number(nvl, "%s", key));
1120	ATF_REQUIRE_EQ(nvlist_get_number(nvl, key), value);
1121	ATF_REQUIRE_EQ(nvlist_getf_number(nvl, "%s", key), value);
1122
1123	/* Iterate over the nvlist; ensure that it has only our one key. */
1124	it = NULL;
1125	ATF_REQUIRE_EQ(strcmp(nvlist_next(nvl, &type, &it), key), 0);
1126	ATF_REQUIRE_EQ(type, NV_TYPE_NUMBER);
1127	ATF_REQUIRE_EQ(nvlist_next(nvl, &type,&it), NULL);
1128
1129	nvlist_destroy(nvl);
1130}
1131
1132ATF_TEST_CASE_WITHOUT_HEAD(nvlist_add_string__single_insert);
1133ATF_TEST_CASE_BODY(nvlist_add_string__single_insert)
1134{
1135	nvlist_t *nvl;
1136	void *it;
1137	const char *key;
1138	const char *value;
1139	int type;
1140
1141	key = "test";
1142	value = "fgjdkgjdk";
1143	nvl = nvlist_create(0);
1144
1145	ATF_REQUIRE(nvl != NULL);
1146	ATF_REQUIRE(!nvlist_exists(nvl, key));
1147
1148	nvlist_add_string(nvl, key, value);
1149
1150	ATF_REQUIRE(!nvlist_empty(nvl));
1151	ATF_REQUIRE(nvlist_exists(nvl, key));
1152	ATF_REQUIRE(nvlist_existsf(nvl, "%s", key));
1153	ATF_REQUIRE(nvlist_exists_string(nvl, key));
1154	ATF_REQUIRE(nvlist_existsf_string(nvl, "%s", key));
1155	ATF_REQUIRE_EQ(strcmp(nvlist_get_string(nvl, key), value), 0);
1156	ATF_REQUIRE_EQ(strcmp(nvlist_getf_string(nvl, "%s", key), value), 0);
1157
1158	/* nvlist_add_* is required to clone the value, so check for that. */
1159	ATF_REQUIRE(nvlist_get_string(nvl, key) != value);
1160
1161	/* Iterate over the nvlist; ensure that it has only our one key. */
1162	it = NULL;
1163	ATF_REQUIRE_EQ(strcmp(nvlist_next(nvl, &type, &it), key), 0);
1164	ATF_REQUIRE_EQ(type, NV_TYPE_STRING);
1165	ATF_REQUIRE_EQ(nvlist_next(nvl, &type,&it), NULL);
1166
1167	nvlist_destroy(nvl);
1168}
1169
1170ATF_TEST_CASE_WITHOUT_HEAD(nvlist_add_nvlist__single_insert);
1171ATF_TEST_CASE_BODY(nvlist_add_nvlist__single_insert)
1172{
1173	nvlist_t *nvl;
1174	void *it;
1175	const char *key, *subkey;
1176	nvlist_t *sublist;
1177	const nvlist_t *value;
1178	int type;
1179
1180	key = "test";
1181	subkey = "subkey";
1182	sublist = nvlist_create(0);
1183	nvl = nvlist_create(0);
1184
1185	ATF_REQUIRE(nvl != NULL);
1186	ATF_REQUIRE(!nvlist_exists(nvl, key));
1187
1188	nvlist_add_null(sublist, subkey);
1189	nvlist_add_nvlist(nvl, key, sublist);
1190
1191	ATF_REQUIRE(!nvlist_empty(nvl));
1192	ATF_REQUIRE(nvlist_exists(nvl, key));
1193	ATF_REQUIRE(nvlist_existsf(nvl, "%s", key));
1194	ATF_REQUIRE(nvlist_exists_nvlist(nvl, key));
1195	ATF_REQUIRE(nvlist_existsf_nvlist(nvl, "%s", key));
1196
1197	value = nvlist_get_nvlist(nvl, key);
1198	ATF_REQUIRE(nvlist_exists_null(value, subkey));
1199
1200	/* nvlist_add_* is required to clone the value, so check for that. */
1201	ATF_REQUIRE(sublist != value);
1202
1203	value = nvlist_getf_nvlist(nvl, "%s", key);
1204	ATF_REQUIRE(nvlist_exists_null(value, subkey));
1205	ATF_REQUIRE(sublist != value);
1206
1207	/* Iterate over the nvlist; ensure that it has only our one key. */
1208	it = NULL;
1209	ATF_REQUIRE_EQ(strcmp(nvlist_next(nvl, &type, &it), key), 0);
1210	ATF_REQUIRE_EQ(type, NV_TYPE_NVLIST);
1211	ATF_REQUIRE_EQ(nvlist_next(nvl, &type,&it), NULL);
1212
1213	nvlist_destroy(sublist);
1214	nvlist_destroy(nvl);
1215}
1216
1217ATF_TEST_CASE_WITHOUT_HEAD(nvlist_add_binary__single_insert);
1218ATF_TEST_CASE_BODY(nvlist_add_binary__single_insert)
1219{
1220	nvlist_t *nvl;
1221	void *it;
1222	const char *key;
1223	void *value;
1224	const void *ret_value;
1225	size_t value_size, ret_size;
1226	int type;
1227
1228	key = "binary";
1229	value_size = 13;
1230	value = malloc(value_size);
1231	memset(value, 0xa5, value_size);
1232	nvl = nvlist_create(0);
1233
1234	ATF_REQUIRE(nvl != NULL);
1235	ATF_REQUIRE(!nvlist_exists(nvl, key));
1236
1237	nvlist_add_binary(nvl, key, value, value_size);
1238
1239	ATF_REQUIRE(!nvlist_empty(nvl));
1240	ATF_REQUIRE(nvlist_exists(nvl, key));
1241	ATF_REQUIRE(nvlist_existsf(nvl, "%s", key));
1242	ATF_REQUIRE(nvlist_exists_binary(nvl, key));
1243	ATF_REQUIRE(nvlist_existsf_binary(nvl, "%s", key));
1244
1245	ret_value = nvlist_get_binary(nvl, key, &ret_size);
1246	ATF_REQUIRE_EQ(value_size, ret_size);
1247	ATF_REQUIRE_EQ(memcmp(value, ret_value, ret_size), 0);
1248
1249	/* nvlist_add_* is required to clone the value, so check for that. */
1250	ATF_REQUIRE(value != ret_value);
1251
1252	ret_value = nvlist_getf_binary(nvl, &ret_size, "%s", key);
1253	ATF_REQUIRE_EQ(value_size, ret_size);
1254	ATF_REQUIRE_EQ(memcmp(value, ret_value, ret_size), 0);
1255	ATF_REQUIRE(value != ret_value);
1256
1257	/* Iterate over the nvlist; ensure that it has only our one key. */
1258	it = NULL;
1259	ATF_REQUIRE_EQ(strcmp(nvlist_next(nvl, &type, &it), key), 0);
1260	ATF_REQUIRE_EQ(type, NV_TYPE_BINARY);
1261	ATF_REQUIRE_EQ(nvlist_next(nvl, &type,&it), NULL);
1262
1263	nvlist_destroy(nvl);
1264	free(value);
1265}
1266
1267ATF_INIT_TEST_CASES(tp)
1268{
1269	ATF_ADD_TEST_CASE(tp, nvlist_create__is_empty);
1270	ATF_ADD_TEST_CASE(tp, nvlist_add_null__single_insert);
1271	ATF_ADD_TEST_CASE(tp, nvlist_add_bool__single_insert);
1272	ATF_ADD_TEST_CASE(tp, nvlist_add_number__single_insert);
1273	ATF_ADD_TEST_CASE(tp, nvlist_add_string__single_insert);
1274	ATF_ADD_TEST_CASE(tp, nvlist_add_nvlist__single_insert);
1275	ATF_ADD_TEST_CASE(tp, nvlist_add_binary__single_insert);
1276}
1277/*-
1278 * Copyright (c) 2014-2015 Sandvine Inc.  All rights reserved.
1279 * All rights reserved.
1280 *
1281 * Redistribution and use in source and binary forms, with or without
1282 * modification, are permitted provided that the following conditions
1283 * are met:
1284 * 1. Redistributions of source code must retain the above copyright
1285 *    notice, this list of conditions and the following disclaimer.
1286 * 2. Redistributions in binary form must reproduce the above copyright
1287 *    notice, this list of conditions and the following disclaimer in the
1288 *    documentation and/or other materials provided with the distribution.
1289 *
1290 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1291 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1292 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1293 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
1294 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
1295 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
1296 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
1297 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
1298 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
1299 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
1300 * SUCH DAMAGE.
1301 */
1302
1303#include <atf-c++.hpp>
1304#include <nv.h>
1305
1306#include <errno.h>
1307/*
1308 * Test that a newly created nvlist has no errors, and is empty.
1309 */
1310ATF_TEST_CASE_WITHOUT_HEAD(nvlist_create__is_empty);
1311ATF_TEST_CASE_BODY(nvlist_create__is_empty)
1312{
1313	nvlist_t *nvl;
1314	int type;
1315	void *it;
1316
1317	nvl = nvlist_create(0);
1318
1319	ATF_REQUIRE(nvl != NULL);
1320
1321	ATF_REQUIRE_EQ(nvlist_error(nvl), 0);
1322	ATF_REQUIRE(nvlist_empty(nvl));
1323
1324	it = NULL;
1325	ATF_REQUIRE_EQ(nvlist_next(nvl, &type, &it), NULL);
1326
1327	nvlist_destroy(nvl);
1328}
1329
1330ATF_TEST_CASE_WITHOUT_HEAD(nvlist_add_null__single_insert);
1331ATF_TEST_CASE_BODY(nvlist_add_null__single_insert)
1332{
1333	nvlist_t *nvl;
1334	void *it;
1335	const char *key;
1336	int type;
1337
1338	key = "key";
1339	nvl = nvlist_create(0);
1340
1341	ATF_REQUIRE(nvl != NULL);
1342	ATF_REQUIRE(!nvlist_exists(nvl, key));
1343
1344	nvlist_add_null(nvl, key);
1345
1346	ATF_REQUIRE(!nvlist_empty(nvl));
1347	ATF_REQUIRE(nvlist_exists(nvl, key));
1348	ATF_REQUIRE(nvlist_existsf(nvl, "%s", key));
1349	ATF_REQUIRE(nvlist_exists_null(nvl, key));
1350	ATF_REQUIRE(nvlist_existsf_null(nvl, "key"));
1351
1352	/* Iterate over the nvlist; ensure that it has only our one key. */
1353	it = NULL;
1354	ATF_REQUIRE_EQ(strcmp(nvlist_next(nvl, &type, &it), key), 0);
1355	ATF_REQUIRE_EQ(type, NV_TYPE_NULL);
1356	ATF_REQUIRE_EQ(nvlist_next(nvl, &type,&it), NULL);
1357
1358	nvlist_destroy(nvl);
1359}
1360
1361ATF_TEST_CASE_WITHOUT_HEAD(nvlist_add_bool__single_insert);
1362ATF_TEST_CASE_BODY(nvlist_add_bool__single_insert)
1363{
1364	nvlist_t *nvl;
1365	void *it;
1366	const char *key;
1367	int type;
1368
1369	key = "name";
1370	nvl = nvlist_create(0);
1371
1372	ATF_REQUIRE(nvl != NULL);
1373	ATF_REQUIRE(!nvlist_exists(nvl, key));
1374
1375	nvlist_add_bool(nvl, key, true);
1376
1377	ATF_REQUIRE(!nvlist_empty(nvl));
1378	ATF_REQUIRE(nvlist_exists(nvl, key));
1379	ATF_REQUIRE(nvlist_existsf(nvl, "%s%s", "na", "me"));
1380	ATF_REQUIRE(nvlist_exists_bool(nvl, key));
1381	ATF_REQUIRE(nvlist_existsf_bool(nvl, "%s%c", "nam", 'e'));
1382	ATF_REQUIRE_EQ(nvlist_get_bool(nvl, key), true);
1383	ATF_REQUIRE_EQ(nvlist_getf_bool(nvl, "%c%s", 'n', "ame"), true);
1384
1385	/* Iterate over the nvlist; ensure that it has only our one key. */
1386	it = NULL;
1387	ATF_REQUIRE_EQ(strcmp(nvlist_next(nvl, &type, &it), key), 0);
1388	ATF_REQUIRE_EQ(type, NV_TYPE_BOOL);
1389	ATF_REQUIRE_EQ(nvlist_next(nvl, &type,&it), NULL);
1390
1391	nvlist_destroy(nvl);
1392}
1393
1394ATF_TEST_CASE_WITHOUT_HEAD(nvlist_add_number__single_insert);
1395ATF_TEST_CASE_BODY(nvlist_add_number__single_insert)
1396{
1397	nvlist_t *nvl;
1398	void *it;
1399	const char *key;
1400	uint64_t value;
1401	int type;
1402
1403	key = "foo123";
1404	value = 71965;
1405	nvl = nvlist_create(0);
1406
1407	ATF_REQUIRE(nvl != NULL);
1408	ATF_REQUIRE(!nvlist_exists(nvl, key));
1409
1410	nvlist_add_number(nvl, key, value);
1411
1412	ATF_REQUIRE(!nvlist_empty(nvl));
1413	ATF_REQUIRE(nvlist_exists(nvl, key));
1414	ATF_REQUIRE(nvlist_existsf(nvl, "%s%d", "foo", 123));
1415	ATF_REQUIRE(nvlist_exists_number(nvl, key));
1416	ATF_REQUIRE(nvlist_existsf_number(nvl, "%s", key));
1417	ATF_REQUIRE_EQ(nvlist_get_number(nvl, key), value);
1418	ATF_REQUIRE_EQ(nvlist_getf_number(nvl, "%s", key), value);
1419
1420	/* Iterate over the nvlist; ensure that it has only our one key. */
1421	it = NULL;
1422	ATF_REQUIRE_EQ(strcmp(nvlist_next(nvl, &type, &it), key), 0);
1423	ATF_REQUIRE_EQ(type, NV_TYPE_NUMBER);
1424	ATF_REQUIRE_EQ(nvlist_next(nvl, &type,&it), NULL);
1425
1426	nvlist_destroy(nvl);
1427}
1428
1429ATF_TEST_CASE_WITHOUT_HEAD(nvlist_add_string__single_insert);
1430ATF_TEST_CASE_BODY(nvlist_add_string__single_insert)
1431{
1432	nvlist_t *nvl;
1433	void *it;
1434	const char *key;
1435	const char *value;
1436	int type;
1437
1438	key = "test";
1439	value = "fgjdkgjdk";
1440	nvl = nvlist_create(0);
1441
1442	ATF_REQUIRE(nvl != NULL);
1443	ATF_REQUIRE(!nvlist_exists(nvl, key));
1444
1445	nvlist_add_string(nvl, key, value);
1446
1447	ATF_REQUIRE(!nvlist_empty(nvl));
1448	ATF_REQUIRE(nvlist_exists(nvl, key));
1449	ATF_REQUIRE(nvlist_existsf(nvl, "%s", key));
1450	ATF_REQUIRE(nvlist_exists_string(nvl, key));
1451	ATF_REQUIRE(nvlist_existsf_string(nvl, "%s", key));
1452	ATF_REQUIRE_EQ(strcmp(nvlist_get_string(nvl, key), value), 0);
1453	ATF_REQUIRE_EQ(strcmp(nvlist_getf_string(nvl, "%s", key), value), 0);
1454
1455	/* nvlist_add_* is required to clone the value, so check for that. */
1456	ATF_REQUIRE(nvlist_get_string(nvl, key) != value);
1457
1458	/* Iterate over the nvlist; ensure that it has only our one key. */
1459	it = NULL;
1460	ATF_REQUIRE_EQ(strcmp(nvlist_next(nvl, &type, &it), key), 0);
1461	ATF_REQUIRE_EQ(type, NV_TYPE_STRING);
1462	ATF_REQUIRE_EQ(nvlist_next(nvl, &type,&it), NULL);
1463
1464	nvlist_destroy(nvl);
1465}
1466
1467ATF_TEST_CASE_WITHOUT_HEAD(nvlist_add_nvlist__single_insert);
1468ATF_TEST_CASE_BODY(nvlist_add_nvlist__single_insert)
1469{
1470	nvlist_t *nvl;
1471	void *it;
1472	const char *key, *subkey;
1473	nvlist_t *sublist;
1474	const nvlist_t *value;
1475	int type;
1476
1477	key = "test";
1478	subkey = "subkey";
1479	sublist = nvlist_create(0);
1480	nvl = nvlist_create(0);
1481
1482	ATF_REQUIRE(nvl != NULL);
1483	ATF_REQUIRE(!nvlist_exists(nvl, key));
1484
1485	nvlist_add_null(sublist, subkey);
1486	nvlist_add_nvlist(nvl, key, sublist);
1487
1488	ATF_REQUIRE(!nvlist_empty(nvl));
1489	ATF_REQUIRE(nvlist_exists(nvl, key));
1490	ATF_REQUIRE(nvlist_existsf(nvl, "%s", key));
1491	ATF_REQUIRE(nvlist_exists_nvlist(nvl, key));
1492	ATF_REQUIRE(nvlist_existsf_nvlist(nvl, "%s", key));
1493
1494	value = nvlist_get_nvlist(nvl, key);
1495	ATF_REQUIRE(nvlist_exists_null(value, subkey));
1496
1497	/* nvlist_add_* is required to clone the value, so check for that. */
1498	ATF_REQUIRE(sublist != value);
1499
1500	value = nvlist_getf_nvlist(nvl, "%s", key);
1501	ATF_REQUIRE(nvlist_exists_null(value, subkey));
1502	ATF_REQUIRE(sublist != value);
1503
1504	/* Iterate over the nvlist; ensure that it has only our one key. */
1505	it = NULL;
1506	ATF_REQUIRE_EQ(strcmp(nvlist_next(nvl, &type, &it), key), 0);
1507	ATF_REQUIRE_EQ(type, NV_TYPE_NVLIST);
1508	ATF_REQUIRE_EQ(nvlist_next(nvl, &type,&it), NULL);
1509
1510	nvlist_destroy(sublist);
1511	nvlist_destroy(nvl);
1512}
1513
1514ATF_TEST_CASE_WITHOUT_HEAD(nvlist_add_binary__single_insert);
1515ATF_TEST_CASE_BODY(nvlist_add_binary__single_insert)
1516{
1517	nvlist_t *nvl;
1518	void *it;
1519	const char *key;
1520	void *value;
1521	const void *ret_value;
1522	size_t value_size, ret_size;
1523	int type;
1524
1525	key = "binary";
1526	value_size = 13;
1527	value = malloc(value_size);
1528	memset(value, 0xa5, value_size);
1529	nvl = nvlist_create(0);
1530
1531	ATF_REQUIRE(nvl != NULL);
1532	ATF_REQUIRE(!nvlist_exists(nvl, key));
1533
1534	nvlist_add_binary(nvl, key, value, value_size);
1535
1536	ATF_REQUIRE(!nvlist_empty(nvl));
1537	ATF_REQUIRE(nvlist_exists(nvl, key));
1538	ATF_REQUIRE(nvlist_existsf(nvl, "%s", key));
1539	ATF_REQUIRE(nvlist_exists_binary(nvl, key));
1540	ATF_REQUIRE(nvlist_existsf_binary(nvl, "%s", key));
1541
1542	ret_value = nvlist_get_binary(nvl, key, &ret_size);
1543	ATF_REQUIRE_EQ(value_size, ret_size);
1544	ATF_REQUIRE_EQ(memcmp(value, ret_value, ret_size), 0);
1545
1546	/* nvlist_add_* is required to clone the value, so check for that. */
1547	ATF_REQUIRE(value != ret_value);
1548
1549	ret_value = nvlist_getf_binary(nvl, &ret_size, "%s", key);
1550	ATF_REQUIRE_EQ(value_size, ret_size);
1551	ATF_REQUIRE_EQ(memcmp(value, ret_value, ret_size), 0);
1552	ATF_REQUIRE(value != ret_value);
1553
1554	/* Iterate over the nvlist; ensure that it has only our one key. */
1555	it = NULL;
1556	ATF_REQUIRE_EQ(strcmp(nvlist_next(nvl, &type, &it), key), 0);
1557	ATF_REQUIRE_EQ(type, NV_TYPE_BINARY);
1558	ATF_REQUIRE_EQ(nvlist_next(nvl, &type,&it), NULL);
1559
1560	nvlist_destroy(nvl);
1561	free(value);
1562}
1563
1564ATF_INIT_TEST_CASES(tp)
1565{
1566	ATF_ADD_TEST_CASE(tp, nvlist_create__is_empty);
1567	ATF_ADD_TEST_CASE(tp, nvlist_add_null__single_insert);
1568	ATF_ADD_TEST_CASE(tp, nvlist_add_bool__single_insert);
1569	ATF_ADD_TEST_CASE(tp, nvlist_add_number__single_insert);
1570	ATF_ADD_TEST_CASE(tp, nvlist_add_string__single_insert);
1571	ATF_ADD_TEST_CASE(tp, nvlist_add_nvlist__single_insert);
1572	ATF_ADD_TEST_CASE(tp, nvlist_add_binary__single_insert);
1573}
1574/*-
1575 * Copyright (c) 2014-2015 Sandvine Inc.  All rights reserved.
1576 * All rights reserved.
1577 *
1578 * Redistribution and use in source and binary forms, with or without
1579 * modification, are permitted provided that the following conditions
1580 * are met:
1581 * 1. Redistributions of source code must retain the above copyright
1582 *    notice, this list of conditions and the following disclaimer.
1583 * 2. Redistributions in binary form must reproduce the above copyright
1584 *    notice, this list of conditions and the following disclaimer in the
1585 *    documentation and/or other materials provided with the distribution.
1586 *
1587 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1588 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1589 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1590 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
1591 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
1592 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
1593 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
1594 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
1595 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
1596 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
1597 * SUCH DAMAGE.
1598 */
1599
1600#include <atf-c++.hpp>
1601#include <nv.h>
1602
1603#include <errno.h>
1604#include <limits>
1605#include <set>
1606#include <sstream>
1607#include <string>
1608
1609/*
1610 * Test that a newly created nvlist has no errors, and is empty.
1611 */
1612ATF_TEST_CASE_WITHOUT_HEAD(nvlist_create__is_empty);
1613ATF_TEST_CASE_BODY(nvlist_create__is_empty)
1614{
1615	nvlist_t *nvl;
1616	int type;
1617	void *it;
1618
1619	nvl = nvlist_create(0);
1620
1621	ATF_REQUIRE(nvl != NULL);
1622
1623	ATF_REQUIRE_EQ(nvlist_error(nvl), 0);
1624	ATF_REQUIRE(nvlist_empty(nvl));
1625
1626	it = NULL;
1627	ATF_REQUIRE_EQ(nvlist_next(nvl, &type, &it), NULL);
1628
1629	nvlist_destroy(nvl);
1630}
1631
1632ATF_TEST_CASE_WITHOUT_HEAD(nvlist_add_null__single_insert);
1633ATF_TEST_CASE_BODY(nvlist_add_null__single_insert)
1634{
1635	nvlist_t *nvl;
1636	void *it;
1637	const char *key;
1638	int type;
1639
1640	key = "key";
1641	nvl = nvlist_create(0);
1642
1643	ATF_REQUIRE(nvl != NULL);
1644	ATF_REQUIRE(!nvlist_exists(nvl, key));
1645
1646	nvlist_add_null(nvl, key);
1647
1648	ATF_REQUIRE(!nvlist_empty(nvl));
1649	ATF_REQUIRE(nvlist_exists(nvl, key));
1650	ATF_REQUIRE(nvlist_existsf(nvl, "%s", key));
1651	ATF_REQUIRE(nvlist_exists_null(nvl, key));
1652	ATF_REQUIRE(nvlist_existsf_null(nvl, "key"));
1653
1654	/* Iterate over the nvlist; ensure that it has only our one key. */
1655	it = NULL;
1656	ATF_REQUIRE_EQ(strcmp(nvlist_next(nvl, &type, &it), key), 0);
1657	ATF_REQUIRE_EQ(type, NV_TYPE_NULL);
1658	ATF_REQUIRE_EQ(nvlist_next(nvl, &type,&it), NULL);
1659
1660	nvlist_destroy(nvl);
1661}
1662
1663ATF_TEST_CASE_WITHOUT_HEAD(nvlist_add_bool__single_insert);
1664ATF_TEST_CASE_BODY(nvlist_add_bool__single_insert)
1665{
1666	nvlist_t *nvl;
1667	void *it;
1668	const char *key;
1669	int type;
1670
1671	key = "name";
1672	nvl = nvlist_create(0);
1673
1674	ATF_REQUIRE(nvl != NULL);
1675	ATF_REQUIRE(!nvlist_exists(nvl, key));
1676
1677	nvlist_add_bool(nvl, key, true);
1678
1679	ATF_REQUIRE(!nvlist_empty(nvl));
1680	ATF_REQUIRE(nvlist_exists(nvl, key));
1681	ATF_REQUIRE(nvlist_existsf(nvl, "%s%s", "na", "me"));
1682	ATF_REQUIRE(nvlist_exists_bool(nvl, key));
1683	ATF_REQUIRE(nvlist_existsf_bool(nvl, "%s%c", "nam", 'e'));
1684	ATF_REQUIRE_EQ(nvlist_get_bool(nvl, key), true);
1685	ATF_REQUIRE_EQ(nvlist_getf_bool(nvl, "%c%s", 'n', "ame"), true);
1686
1687	/* Iterate over the nvlist; ensure that it has only our one key. */
1688	it = NULL;
1689	ATF_REQUIRE_EQ(strcmp(nvlist_next(nvl, &type, &it), key), 0);
1690	ATF_REQUIRE_EQ(type, NV_TYPE_BOOL);
1691	ATF_REQUIRE_EQ(nvlist_next(nvl, &type,&it), NULL);
1692
1693	nvlist_destroy(nvl);
1694}
1695
1696ATF_TEST_CASE_WITHOUT_HEAD(nvlist_add_number__single_insert);
1697ATF_TEST_CASE_BODY(nvlist_add_number__single_insert)
1698{
1699	nvlist_t *nvl;
1700	void *it;
1701	const char *key;
1702	uint64_t value;
1703	int type;
1704
1705	key = "foo123";
1706	value = 71965;
1707	nvl = nvlist_create(0);
1708
1709	ATF_REQUIRE(nvl != NULL);
1710	ATF_REQUIRE(!nvlist_exists(nvl, key));
1711
1712	nvlist_add_number(nvl, key, value);
1713
1714	ATF_REQUIRE(!nvlist_empty(nvl));
1715	ATF_REQUIRE(nvlist_exists(nvl, key));
1716	ATF_REQUIRE(nvlist_existsf(nvl, "%s%d", "foo", 123));
1717	ATF_REQUIRE(nvlist_exists_number(nvl, key));
1718	ATF_REQUIRE(nvlist_existsf_number(nvl, "%s", key));
1719	ATF_REQUIRE_EQ(nvlist_get_number(nvl, key), value);
1720	ATF_REQUIRE_EQ(nvlist_getf_number(nvl, "%s", key), value);
1721
1722	/* Iterate over the nvlist; ensure that it has only our one key. */
1723	it = NULL;
1724	ATF_REQUIRE_EQ(strcmp(nvlist_next(nvl, &type, &it), key), 0);
1725	ATF_REQUIRE_EQ(type, NV_TYPE_NUMBER);
1726	ATF_REQUIRE_EQ(nvlist_next(nvl, &type,&it), NULL);
1727
1728	nvlist_destroy(nvl);
1729}
1730
1731ATF_TEST_CASE_WITHOUT_HEAD(nvlist_add_string__single_insert);
1732ATF_TEST_CASE_BODY(nvlist_add_string__single_insert)
1733{
1734	nvlist_t *nvl;
1735	void *it;
1736	const char *key;
1737	const char *value;
1738	int type;
1739
1740	key = "test";
1741	value = "fgjdkgjdk";
1742	nvl = nvlist_create(0);
1743
1744	ATF_REQUIRE(nvl != NULL);
1745	ATF_REQUIRE(!nvlist_exists(nvl, key));
1746
1747	nvlist_add_string(nvl, key, value);
1748
1749	ATF_REQUIRE(!nvlist_empty(nvl));
1750	ATF_REQUIRE(nvlist_exists(nvl, key));
1751	ATF_REQUIRE(nvlist_existsf(nvl, "%s", key));
1752	ATF_REQUIRE(nvlist_exists_string(nvl, key));
1753	ATF_REQUIRE(nvlist_existsf_string(nvl, "%s", key));
1754	ATF_REQUIRE_EQ(strcmp(nvlist_get_string(nvl, key), value), 0);
1755	ATF_REQUIRE_EQ(strcmp(nvlist_getf_string(nvl, "%s", key), value), 0);
1756
1757	/* nvlist_add_* is required to clone the value, so check for that. */
1758	ATF_REQUIRE(nvlist_get_string(nvl, key) != 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_STRING);
1764	ATF_REQUIRE_EQ(nvlist_next(nvl, &type,&it), NULL);
1765
1766	nvlist_destroy(nvl);
1767}
1768
1769ATF_TEST_CASE_WITHOUT_HEAD(nvlist_add_nvlist__single_insert);
1770ATF_TEST_CASE_BODY(nvlist_add_nvlist__single_insert)
1771{
1772	nvlist_t *nvl;
1773	void *it;
1774	const char *key, *subkey;
1775	nvlist_t *sublist;
1776	const nvlist_t *value;
1777	int type;
1778
1779	key = "test";
1780	subkey = "subkey";
1781	sublist = nvlist_create(0);
1782	nvl = nvlist_create(0);
1783
1784	ATF_REQUIRE(nvl != NULL);
1785	ATF_REQUIRE(!nvlist_exists(nvl, key));
1786
1787	nvlist_add_null(sublist, subkey);
1788	nvlist_add_nvlist(nvl, key, sublist);
1789
1790	ATF_REQUIRE(!nvlist_empty(nvl));
1791	ATF_REQUIRE(nvlist_exists(nvl, key));
1792	ATF_REQUIRE(nvlist_existsf(nvl, "%s", key));
1793	ATF_REQUIRE(nvlist_exists_nvlist(nvl, key));
1794	ATF_REQUIRE(nvlist_existsf_nvlist(nvl, "%s", key));
1795
1796	value = nvlist_get_nvlist(nvl, key);
1797	ATF_REQUIRE(nvlist_exists_null(value, subkey));
1798
1799	/* nvlist_add_* is required to clone the value, so check for that. */
1800	ATF_REQUIRE(sublist != value);
1801
1802	value = nvlist_getf_nvlist(nvl, "%s", key);
1803	ATF_REQUIRE(nvlist_exists_null(value, subkey));
1804	ATF_REQUIRE(sublist != value);
1805
1806	/* Iterate over the nvlist; ensure that it has only our one key. */
1807	it = NULL;
1808	ATF_REQUIRE_EQ(strcmp(nvlist_next(nvl, &type, &it), key), 0);
1809	ATF_REQUIRE_EQ(type, NV_TYPE_NVLIST);
1810	ATF_REQUIRE_EQ(nvlist_next(nvl, &type,&it), NULL);
1811
1812	nvlist_destroy(sublist);
1813	nvlist_destroy(nvl);
1814}
1815
1816ATF_TEST_CASE_WITHOUT_HEAD(nvlist_add_binary__single_insert);
1817ATF_TEST_CASE_BODY(nvlist_add_binary__single_insert)
1818{
1819	nvlist_t *nvl;
1820	void *it;
1821	const char *key;
1822	void *value;
1823	const void *ret_value;
1824	size_t value_size, ret_size;
1825	int type;
1826
1827	key = "binary";
1828	value_size = 13;
1829	value = malloc(value_size);
1830	memset(value, 0xa5, value_size);
1831	nvl = nvlist_create(0);
1832
1833	ATF_REQUIRE(nvl != NULL);
1834	ATF_REQUIRE(!nvlist_exists(nvl, key));
1835
1836	nvlist_add_binary(nvl, key, value, value_size);
1837
1838	ATF_REQUIRE(!nvlist_empty(nvl));
1839	ATF_REQUIRE(nvlist_exists(nvl, key));
1840	ATF_REQUIRE(nvlist_existsf(nvl, "%s", key));
1841	ATF_REQUIRE(nvlist_exists_binary(nvl, key));
1842	ATF_REQUIRE(nvlist_existsf_binary(nvl, "%s", key));
1843
1844	ret_value = nvlist_get_binary(nvl, key, &ret_size);
1845	ATF_REQUIRE_EQ(value_size, ret_size);
1846	ATF_REQUIRE_EQ(memcmp(value, ret_value, ret_size), 0);
1847
1848	/* nvlist_add_* is required to clone the value, so check for that. */
1849	ATF_REQUIRE(value != ret_value);
1850
1851	ret_value = nvlist_getf_binary(nvl, &ret_size, "%s", key);
1852	ATF_REQUIRE_EQ(value_size, ret_size);
1853	ATF_REQUIRE_EQ(memcmp(value, ret_value, ret_size), 0);
1854	ATF_REQUIRE(value != ret_value);
1855
1856	/* Iterate over the nvlist; ensure that it has only our one key. */
1857	it = NULL;
1858	ATF_REQUIRE_EQ(strcmp(nvlist_next(nvl, &type, &it), key), 0);
1859	ATF_REQUIRE_EQ(type, NV_TYPE_BINARY);
1860	ATF_REQUIRE_EQ(nvlist_next(nvl, &type,&it), NULL);
1861
1862	nvlist_destroy(nvl);
1863	free(value);
1864}
1865
1866ATF_TEST_CASE_WITHOUT_HEAD(nvlist_clone__empty_nvlist);
1867ATF_TEST_CASE_BODY(nvlist_clone__empty_nvlist)
1868{
1869	nvlist_t *nvl, *clone;
1870
1871	nvl = nvlist_create(0);
1872	ATF_REQUIRE(nvl != NULL);
1873
1874	clone = nvlist_clone(nvl);
1875	ATF_REQUIRE(clone != NULL);
1876	ATF_REQUIRE(clone != nvl);
1877	ATF_REQUIRE(nvlist_empty(clone));
1878
1879	nvlist_destroy(clone);
1880	nvlist_destroy(nvl);
1881}
1882
1883ATF_TEST_CASE_WITHOUT_HEAD(nvlist_clone__nonempty_nvlist);
1884ATF_TEST_CASE_BODY(nvlist_clone__nonempty_nvlist)
1885{
1886	nvlist_t *nvl, *clone;
1887	const char *key;
1888	void *it;
1889	uint64_t value;
1890	int type;
1891
1892	nvl = nvlist_create(0);
1893	ATF_REQUIRE(nvl != NULL);
1894
1895	key = "testkey";
1896	value = 684874;
1897	nvlist_add_number(nvl, key, value);
1898
1899	clone = nvlist_clone(nvl);
1900	ATF_REQUIRE(clone != NULL);
1901	ATF_REQUIRE(clone != nvl);
1902	ATF_REQUIRE(nvlist_exists_number(clone, key));
1903	ATF_REQUIRE_EQ(nvlist_get_number(clone, key), value);
1904
1905	/* Iterate over the nvlist; ensure that it has only our one key. */
1906	it = NULL;
1907	ATF_REQUIRE_EQ(strcmp(nvlist_next(clone, &type, &it), key), 0);
1908	ATF_REQUIRE_EQ(type, NV_TYPE_NUMBER);
1909	ATF_REQUIRE_EQ(nvlist_next(clone, &type, &it), NULL);
1910
1911	nvlist_destroy(clone);
1912	nvlist_destroy(nvl);
1913}
1914
1915static const char * const test_subnvlist_key = "nvlist";
1916
1917static const char * const test_string_key = "string";
1918static const char * const test_string_val = "59525";
1919
1920static nvlist_t*
1921create_test_nvlist(void)
1922{
1923	nvlist_t *nvl, *sublist;
1924
1925	nvl = nvlist_create(0);
1926	ATF_REQUIRE(nvl != NULL);
1927
1928	sublist = nvlist_create(0);
1929	ATF_REQUIRE(sublist != NULL);
1930
1931	nvlist_add_string(sublist, test_string_key, test_string_val);
1932	nvlist_move_nvlist(nvl, test_subnvlist_key, sublist);
1933
1934	return (nvl);
1935}
1936
1937static void
1938verify_test_nvlist(const nvlist_t *nvl)
1939{
1940	void *it;
1941	const nvlist_t *value;
1942	int type;
1943
1944	ATF_REQUIRE(nvlist_exists_nvlist(nvl, test_subnvlist_key));
1945
1946	value = nvlist_get_nvlist(nvl, test_subnvlist_key);
1947
1948	ATF_REQUIRE(nvlist_exists_string(value, test_string_key));
1949	ATF_REQUIRE_EQ(strcmp(nvlist_get_string(value, test_string_key), test_string_val), 0);
1950	ATF_REQUIRE(nvlist_get_string(value, test_string_key) != test_string_val);
1951
1952	/* Iterate over both nvlists; ensure that each has only the one key. */
1953	it = NULL;
1954	ATF_REQUIRE_EQ(strcmp(nvlist_next(value, &type, &it),
1955	    test_string_key), 0);
1956	ATF_REQUIRE_EQ(type, NV_TYPE_STRING);
1957	ATF_REQUIRE_EQ(nvlist_next(value, &type, &it), NULL);
1958
1959	it = NULL;
1960	ATF_REQUIRE_EQ(strcmp(nvlist_next(nvl, &type, &it),
1961	    test_subnvlist_key), 0);
1962	ATF_REQUIRE_EQ(type, NV_TYPE_NVLIST);
1963	ATF_REQUIRE_EQ(nvlist_next(nvl, &type, &it), NULL);
1964}
1965
1966ATF_TEST_CASE_WITHOUT_HEAD(nvlist_clone__nested_nvlist);
1967ATF_TEST_CASE_BODY(nvlist_clone__nested_nvlist)
1968{
1969	nvlist_t *nvl, *clone;
1970
1971	nvl = create_test_nvlist();
1972	clone = nvlist_clone(nvl);
1973
1974	ATF_REQUIRE(clone != NULL);
1975	ATF_REQUIRE(clone != nvl);
1976	verify_test_nvlist(clone);
1977
1978	nvlist_destroy(clone);
1979	nvlist_destroy(nvl);
1980}
1981
1982ATF_TEST_CASE_WITHOUT_HEAD(nvlist_pack__empty_nvlist);
1983ATF_TEST_CASE_BODY(nvlist_pack__empty_nvlist)
1984{
1985	nvlist_t *nvl, *unpacked;
1986	void *packed;
1987	size_t packed_size;
1988
1989	nvl = nvlist_create(0);
1990	ATF_REQUIRE(nvl != NULL);
1991
1992	packed = nvlist_pack(nvl, &packed_size);
1993	ATF_REQUIRE(packed != NULL);
1994
1995	unpacked = nvlist_unpack(packed, packed_size);
1996	ATF_REQUIRE(unpacked != NULL);
1997	ATF_REQUIRE(unpacked != nvl);
1998	ATF_REQUIRE(nvlist_empty(unpacked));
1999
2000	nvlist_destroy(unpacked);
2001	nvlist_destroy(nvl);
2002	free(packed);
2003}
2004
2005static void
2006verify_null(const nvlist_t *nvl, int type)
2007{
2008
2009	ATF_REQUIRE_EQ(type, NV_TYPE_NULL);
2010}
2011
2012static void
2013verify_number(const nvlist_t *nvl, const char *name, int type, uint64_t value)
2014{
2015
2016	ATF_REQUIRE_EQ(type, NV_TYPE_NUMBER);
2017	ATF_REQUIRE_EQ(nvlist_get_number(nvl, name), value);
2018}
2019
2020static void
2021verify_string(const nvlist_t *nvl, const char *name, int type,
2022    const char * value)
2023{
2024
2025	ATF_REQUIRE_EQ(type, NV_TYPE_STRING);
2026	ATF_REQUIRE_EQ(strcmp(nvlist_get_string(nvl, name), value), 0);
2027}
2028
2029static void
2030verify_nvlist(const nvlist_t *nvl, const char *name, int type)
2031{
2032
2033	ATF_REQUIRE_EQ(type, NV_TYPE_NVLIST);
2034	verify_test_nvlist(nvlist_get_nvlist(nvl, name));
2035}
2036
2037static void
2038verify_binary(const nvlist_t *nvl, const char *name, int type,
2039    const void * value, size_t size)
2040{
2041	const void *actual_value;
2042	size_t actual_size;
2043
2044	ATF_REQUIRE_EQ(type, NV_TYPE_BINARY);
2045	actual_value = nvlist_get_binary(nvl, name, &actual_size);
2046	ATF_REQUIRE_EQ(size, actual_size);
2047	ATF_REQUIRE_EQ(memcmp(value, actual_value, size), 0);
2048}
2049
2050ATF_TEST_CASE_WITHOUT_HEAD(nvlist_pack__multiple_values);
2051ATF_TEST_CASE_BODY(nvlist_pack__multiple_values)
2052{
2053	std::ostringstream msg;
2054	std::set<std::string> keys_seen;
2055	nvlist_t *nvl, *unpacked, *nvvalue;
2056	const char *nullkey, *numkey, *strkey, *nvkey, *binkey, *name;
2057	int numvalue;
2058	const char * strvalue;
2059	void *binvalue, *packed, *it;
2060	size_t binsize, packed_size;
2061	int type;
2062
2063	nvl = nvlist_create(0);
2064
2065	nullkey = "null";
2066	nvlist_add_null(nvl, nullkey);
2067
2068	numkey = "number";
2069	numvalue = 939853984;
2070	nvlist_add_number(nvl, numkey, numvalue);
2071
2072	strkey = "string";
2073	strvalue = "jfieutijf";
2074	nvlist_add_string(nvl, strkey, strvalue);
2075
2076	nvkey = "nvlist";
2077	nvvalue = create_test_nvlist();
2078	nvlist_move_nvlist(nvl, nvkey, nvvalue);
2079
2080	binkey = "binary";
2081	binsize = 4;
2082	binvalue = malloc(binsize);
2083	memset(binvalue, 'b', binsize);
2084	nvlist_move_binary(nvl, binkey, binvalue, binsize);
2085
2086	packed = nvlist_pack(nvl, &packed_size);
2087	ATF_REQUIRE(packed != NULL);
2088
2089	unpacked = nvlist_unpack(packed, packed_size);
2090	ATF_REQUIRE(unpacked != 0);
2091
2092	it = NULL;
2093	while ((name = nvlist_next(unpacked, &type, &it)) != NULL) {
2094		/* Ensure that we see every key only once. */
2095		ATF_REQUIRE_EQ(keys_seen.count(name), 0);
2096
2097		if (strcmp(name, nullkey) == 0)
2098			verify_null(unpacked, type);
2099		else if (strcmp(name, numkey) == 0)
2100			verify_number(unpacked, name, type, numvalue);
2101		else if (strcmp(name, strkey) == 0)
2102			verify_string(unpacked, name, type, strvalue);
2103		else if (strcmp(name, nvkey) == 0)
2104			verify_nvlist(unpacked, name, type);
2105		else if (strcmp(name, binkey) == 0)
2106			verify_binary(unpacked, name, type, binvalue, binsize);
2107		else {
2108			msg << "Unexpected key :'" << name << "'";
2109			ATF_FAIL(msg.str().c_str());
2110		}
2111
2112		keys_seen.insert(name);
2113	}
2114
2115	/* Ensure that we saw every key. */
2116	ATF_REQUIRE_EQ(keys_seen.size(), 5);
2117
2118	nvlist_destroy(nvl);
2119	nvlist_destroy(unpacked);
2120	free(packed);
2121}
2122
2123ATF_TEST_CASE_WITHOUT_HEAD(nvlist_unpack__duplicate_key);
2124ATF_TEST_CASE_BODY(nvlist_unpack__duplicate_key)
2125{
2126	nvlist_t *nvl, *unpacked;
2127	const char *key1, *key2;
2128	void *packed, *keypos;
2129	size_t size, keylen;
2130
2131	nvl = nvlist_create(0);
2132
2133	key1 = "key1";
2134	keylen = strlen(key1);
2135	nvlist_add_number(nvl, key1, 5);
2136
2137	key2 = "key2";
2138	ATF_REQUIRE_EQ(keylen, strlen(key2));
2139	nvlist_add_number(nvl, key2, 10);
2140
2141	packed = nvlist_pack(nvl, &size);
2142
2143	/*
2144	 * Mangle the packed nvlist by replacing key1 with key2, creating a
2145	 * packed nvlist with a duplicate key.
2146	 */
2147	keypos = memmem(packed, size, key1, keylen);
2148	ATF_REQUIRE(keypos != NULL);
2149	memcpy(keypos, key2, keylen);
2150
2151	unpacked = nvlist_unpack(packed, size);
2152	ATF_REQUIRE(nvlist_error(unpacked) != 0);
2153
2154	free(packed);
2155	nvlist_destroy(nvl);
2156	nvlist_destroy(unpacked);
2157}
2158
2159ATF_TEST_CASE_WITHOUT_HEAD(nvlist_move_string__single_insert);
2160ATF_TEST_CASE_BODY(nvlist_move_string__single_insert)
2161{
2162	nvlist_t *nvl;
2163	const char *key;
2164	char *value;
2165
2166	nvl = nvlist_create(0);
2167	ATF_REQUIRE(nvl != NULL);
2168
2169	key = "testkey";
2170	value = strdup("testval");
2171	ATF_REQUIRE(value != NULL);
2172
2173	nvlist_move_string(nvl, key, value);
2174	ATF_REQUIRE_EQ(nvlist_get_string(nvl, key), value);
2175
2176	nvlist_destroy(nvl);
2177}
2178
2179ATF_TEST_CASE_WITHOUT_HEAD(nvlist_move_nvlist__null_child);
2180ATF_TEST_CASE_BODY(nvlist_move_nvlist__null_child)
2181{
2182	nvlist_t *parent;
2183
2184	parent = nvlist_create(0);
2185
2186	nvlist_move_nvlist(parent, "test", NULL);
2187
2188	ATF_REQUIRE(nvlist_error(parent) != 0);
2189
2190	nvlist_destroy(parent);
2191}
2192
2193ATF_TEST_CASE_WITHOUT_HEAD(nvlist_move_nvlist__single_insert);
2194ATF_TEST_CASE_BODY(nvlist_move_nvlist__single_insert)
2195{
2196	nvlist_t *nvl;
2197	const char *key;
2198	nvlist_t *value;
2199
2200	nvl = nvlist_create(0);
2201	ATF_REQUIRE(nvl != NULL);
2202
2203	key = "testkey";
2204	value = nvlist_create(0);
2205	ATF_REQUIRE(value != NULL);
2206
2207	nvlist_move_nvlist(nvl, key, value);
2208	ATF_REQUIRE_EQ(nvlist_get_nvlist(nvl, key), value);
2209
2210	nvlist_destroy(nvl);
2211}
2212
2213ATF_TEST_CASE_WITHOUT_HEAD(nvlist_move_binary__single_insert);
2214ATF_TEST_CASE_BODY(nvlist_move_binary__single_insert)
2215{
2216	nvlist_t *nvl;
2217	const char *key;
2218	void *value;
2219	size_t size, actual_size;
2220
2221	nvl = nvlist_create(0);
2222	ATF_REQUIRE(nvl != NULL);
2223
2224	key = "testkey";
2225	size = 73;
2226	value = malloc(size);
2227	ATF_REQUIRE(value != NULL);
2228
2229	nvlist_move_binary(nvl, key, value, size);
2230	ATF_REQUIRE_EQ(nvlist_get_binary(nvl, key, &actual_size), value);
2231	ATF_REQUIRE_EQ(size, actual_size);
2232
2233	nvlist_destroy(nvl);
2234}
2235
2236ATF_TEST_CASE_WITHOUT_HEAD(nvlist_take_bool__single_remove);
2237ATF_TEST_CASE_BODY(nvlist_take_bool__single_remove)
2238{
2239	nvlist_t *nvl;
2240	const char *testkey;
2241	bool testval;
2242
2243	nvl = nvlist_create(0);
2244	ATF_REQUIRE(nvl != NULL);
2245
2246	testkey = "boolkey";
2247	testval = false;
2248	nvlist_add_bool(nvl, testkey, testval);
2249
2250	ATF_REQUIRE_EQ(nvlist_take_bool(nvl, testkey), testval);
2251	ATF_REQUIRE(nvlist_empty(nvl));
2252
2253	nvlist_destroy(nvl);
2254}
2255
2256ATF_TEST_CASE_WITHOUT_HEAD(nvlist_take_bool__other_keys_unchanged);
2257ATF_TEST_CASE_BODY(nvlist_take_bool__other_keys_unchanged)
2258{
2259	nvlist_t *nvl;
2260	const char *testkey, *otherkey1, *otherkey2;
2261	bool testval, otherval1;
2262	nvlist_t *otherval2;
2263
2264	nvl = nvlist_create(0);
2265	ATF_REQUIRE(nvl != NULL);
2266
2267	testkey = "boolkey";
2268	testval = true;
2269	nvlist_add_bool(nvl, testkey, testval);
2270
2271	otherkey1 = "key1";
2272	otherval1 = false;
2273	nvlist_add_bool(nvl, otherkey1, otherval1);
2274
2275	otherkey2 = "key2";
2276	otherval2 = create_test_nvlist();
2277	nvlist_move_nvlist(nvl, otherkey2, otherval2);
2278
2279	ATF_REQUIRE_EQ(nvlist_take_bool(nvl, testkey), testval);
2280
2281	ATF_REQUIRE(nvlist_exists_bool(nvl, otherkey1));
2282	ATF_REQUIRE_EQ(nvlist_get_bool(nvl, otherkey1), otherval1);
2283
2284	ATF_REQUIRE(nvlist_exists_nvlist(nvl, otherkey2));
2285	verify_test_nvlist(nvlist_get_nvlist(nvl, otherkey2));
2286
2287	nvlist_destroy(nvl);
2288}
2289
2290ATF_TEST_CASE_WITHOUT_HEAD(nvlist_take_number__single_remove);
2291ATF_TEST_CASE_BODY(nvlist_take_number__single_remove)
2292{
2293	nvlist_t *nvl;
2294	const char *testkey;
2295	uint64_t testval;
2296
2297	nvl = nvlist_create(0);
2298	ATF_REQUIRE(nvl != NULL);
2299
2300	testkey = "numkey";
2301	testval = std::numeric_limits<uint64_t>::max();
2302	nvlist_add_number(nvl, testkey, testval);
2303
2304	ATF_REQUIRE_EQ(nvlist_take_number(nvl, testkey), testval);
2305	ATF_REQUIRE(nvlist_empty(nvl));
2306
2307	nvlist_destroy(nvl);
2308}
2309
2310ATF_TEST_CASE_WITHOUT_HEAD(nvlist_take_number__other_keys_unchanged);
2311ATF_TEST_CASE_BODY(nvlist_take_number__other_keys_unchanged)
2312{
2313	nvlist_t *nvl;
2314	const char *testkey, *otherkey1, *otherkey2;
2315	uint64_t testval, otherval1;
2316	const char *otherval2;
2317
2318	nvl = nvlist_create(0);
2319	ATF_REQUIRE(nvl != NULL);
2320
2321	otherkey1 = "key1";
2322	otherval1 = 5;
2323	nvlist_add_number(nvl, otherkey1, otherval1);
2324
2325	testkey = "numkey";
2326	testval = 1654;
2327	nvlist_add_number(nvl, testkey, testval);
2328
2329	otherkey2 = "key2";
2330	otherval2 = "string";
2331	nvlist_add_string(nvl, otherkey2, otherval2);
2332
2333	ATF_REQUIRE_EQ(nvlist_take_number(nvl, testkey), testval);
2334
2335	ATF_REQUIRE(nvlist_exists_number(nvl, otherkey1));
2336	ATF_REQUIRE_EQ(nvlist_get_number(nvl, otherkey1), otherval1);
2337
2338	ATF_REQUIRE(nvlist_exists_string(nvl, otherkey2));
2339	ATF_REQUIRE_EQ(strcmp(nvlist_get_string(nvl, otherkey2), otherval2), 0);
2340
2341	nvlist_destroy(nvl);
2342}
2343
2344ATF_TEST_CASE_WITHOUT_HEAD(nvlist_take_string__single_remove);
2345ATF_TEST_CASE_BODY(nvlist_take_string__single_remove)
2346{
2347	nvlist_t *nvl;
2348	const char *testkey;
2349	const char *testval;
2350
2351	nvl = nvlist_create(0);
2352	ATF_REQUIRE(nvl != NULL);
2353
2354	testkey = "numkey";
2355	testval = "nvlist";
2356	nvlist_add_string(nvl, testkey, testval);
2357
2358	ATF_REQUIRE_EQ(strcmp(nvlist_take_string(nvl, testkey), testval), 0);
2359	ATF_REQUIRE(nvlist_empty(nvl));
2360
2361	nvlist_destroy(nvl);
2362}
2363
2364ATF_TEST_CASE_WITHOUT_HEAD(nvlist_take_string__other_keys_unchanged);
2365ATF_TEST_CASE_BODY(nvlist_take_string__other_keys_unchanged)
2366{
2367	nvlist_t *nvl;
2368	const char *testkey, *otherkey1, *otherkey2;
2369	const char *testval, *otherval1;
2370	bool otherval2;
2371
2372	nvl = nvlist_create(0);
2373	ATF_REQUIRE(nvl != NULL);
2374
2375	otherkey1 = "key1";
2376	otherval1 = "fjdifjdk";
2377	nvlist_add_string(nvl, otherkey1, otherval1);
2378
2379	otherkey2 = "key2";
2380	otherval2 = true;
2381	nvlist_add_bool(nvl, otherkey2, otherval2);
2382
2383	testkey = "strkey";
2384	testval = "1654";
2385	nvlist_add_string(nvl, testkey, testval);
2386
2387	ATF_REQUIRE_EQ(strcmp(nvlist_take_string(nvl, testkey), testval), 0);
2388
2389	ATF_REQUIRE(nvlist_exists_string(nvl, otherkey1));
2390	ATF_REQUIRE_EQ(strcmp(nvlist_get_string(nvl, otherkey1), otherval1), 0);
2391
2392	ATF_REQUIRE(nvlist_exists_bool(nvl, otherkey2));
2393	ATF_REQUIRE_EQ(nvlist_get_bool(nvl, otherkey2), otherval2);
2394
2395	nvlist_destroy(nvl);
2396}
2397
2398ATF_TEST_CASE_WITHOUT_HEAD(nvlist_take_nvlist__single_remove);
2399ATF_TEST_CASE_BODY(nvlist_take_nvlist__single_remove)
2400{
2401	nvlist_t *nvl;
2402	const char *testkey;
2403	nvlist_t *testval;
2404
2405	nvl = nvlist_create(0);
2406	ATF_REQUIRE(nvl != NULL);
2407
2408	testkey = "numkey";
2409	testval = create_test_nvlist();
2410	nvlist_move_nvlist(nvl, testkey, testval);
2411
2412	verify_test_nvlist(nvlist_take_nvlist(nvl, testkey));
2413	ATF_REQUIRE(nvlist_empty(nvl));
2414
2415	nvlist_destroy(nvl);
2416}
2417
2418ATF_TEST_CASE_WITHOUT_HEAD(nvlist_take_nvlist__other_keys_unchanged);
2419ATF_TEST_CASE_BODY(nvlist_take_nvlist__other_keys_unchanged)
2420{
2421	nvlist_t *nvl;
2422	const char *testkey, *otherkey1, *otherkey2;
2423	nvlist_t *testval, *otherval1;
2424
2425	nvl = nvlist_create(0);
2426	ATF_REQUIRE(nvl != NULL);
2427
2428	testkey = "strkey";
2429	testval = create_test_nvlist();
2430	nvlist_move_nvlist(nvl, testkey, testval);
2431
2432	otherkey1 = "key1";
2433	otherval1 = nvlist_create(0);
2434	nvlist_move_nvlist(nvl, otherkey1, otherval1);
2435
2436	otherkey2 = "key2";
2437	nvlist_add_null(nvl, otherkey2);
2438
2439	verify_test_nvlist(nvlist_take_nvlist(nvl, testkey));
2440
2441	ATF_REQUIRE(nvlist_exists_nvlist(nvl, otherkey1));
2442	ATF_REQUIRE(nvlist_empty(nvlist_get_nvlist(nvl, otherkey1)));
2443
2444	ATF_REQUIRE(nvlist_exists_null(nvl, otherkey2));
2445
2446	nvlist_destroy(nvl);
2447}
2448
2449ATF_TEST_CASE_WITHOUT_HEAD(nvlist_take_binary__single_remove);
2450ATF_TEST_CASE_BODY(nvlist_take_binary__single_remove)
2451{
2452	nvlist_t *nvl;
2453	const char *testkey;
2454	void *testval;
2455	const void *actual_val;
2456	size_t testsize, actual_size;
2457
2458	nvl = nvlist_create(0);
2459	ATF_REQUIRE(nvl != NULL);
2460
2461	testkey = "numkey";
2462	testsize = 457;
2463	testval = malloc(testsize);
2464	memset(testval, '5', testsize);
2465	nvlist_move_binary(nvl, testkey, testval, testsize);
2466
2467	actual_val = nvlist_take_binary(nvl, testkey, &actual_size);
2468	ATF_REQUIRE_EQ(testsize, actual_size);
2469	ATF_REQUIRE_EQ(memcmp(actual_val, testval, testsize), 0);
2470	ATF_REQUIRE(nvlist_empty(nvl));
2471
2472	nvlist_destroy(nvl);
2473}
2474
2475ATF_TEST_CASE_WITHOUT_HEAD(nvlist_take_binary__other_keys_unchanged);
2476ATF_TEST_CASE_BODY(nvlist_take_binary__other_keys_unchanged)
2477{
2478	nvlist_t *nvl;
2479	const char *testkey, *otherkey1, *otherkey2;
2480	const void *actual_value;
2481	char testval[] = "gjiertj";
2482	char otherval1[] = "fdreg";
2483	size_t testsize, othersize, actual_size;
2484	bool otherval2;
2485
2486	nvl = nvlist_create(0);
2487	ATF_REQUIRE(nvl != NULL);
2488
2489	otherkey1 = "key1";
2490	othersize = sizeof(otherval1);
2491	nvlist_add_binary(nvl, otherkey1, otherval1, othersize);
2492
2493	otherkey2 = "key2";
2494	otherval2 = true;
2495	nvlist_add_bool(nvl, otherkey2, otherval2);
2496
2497	testkey = "strkey";
2498	testsize = sizeof(testval);
2499	nvlist_add_binary(nvl, testkey, testval, testsize);
2500
2501	actual_value = nvlist_take_binary(nvl, testkey, &actual_size);
2502	ATF_REQUIRE_EQ(testsize, actual_size);
2503	ATF_REQUIRE_EQ(memcmp(actual_value, testval, testsize), 0);
2504
2505	ATF_REQUIRE(nvlist_exists_binary(nvl, otherkey1));
2506	actual_value = nvlist_get_binary(nvl, otherkey1, &actual_size);
2507	ATF_REQUIRE_EQ(othersize, actual_size);
2508	ATF_REQUIRE_EQ(memcmp(actual_value, otherval1, othersize), 0);
2509
2510	ATF_REQUIRE(nvlist_exists_bool(nvl, otherkey2));
2511	ATF_REQUIRE_EQ(nvlist_get_bool(nvl, otherkey2), otherval2);
2512
2513	nvlist_destroy(nvl);
2514}
2515
2516ATF_TEST_CASE_WITHOUT_HEAD(nvlist_free__single_null);
2517ATF_TEST_CASE_BODY(nvlist_free__single_null)
2518{
2519	nvlist_t *nvl;
2520	const char *key;
2521
2522	nvl = nvlist_create(0);
2523	key = "test";
2524	nvlist_add_null(nvl, key);
2525
2526	nvlist_free(nvl, key);
2527	ATF_REQUIRE(nvlist_empty(nvl));
2528
2529	nvlist_destroy(nvl);
2530}
2531
2532ATF_TEST_CASE_WITHOUT_HEAD(nvlist_free__single_bool);
2533ATF_TEST_CASE_BODY(nvlist_free__single_bool)
2534{
2535	nvlist_t *nvl;
2536	const char *key;
2537
2538	nvl = nvlist_create(0);
2539	key = "test";
2540	nvlist_add_bool(nvl, key, true);
2541
2542	nvlist_free(nvl, key);
2543	ATF_REQUIRE(nvlist_empty(nvl));
2544
2545	nvlist_destroy(nvl);
2546}
2547
2548ATF_TEST_CASE_WITHOUT_HEAD(nvlist_free__single_number);
2549ATF_TEST_CASE_BODY(nvlist_free__single_number)
2550{
2551	nvlist_t *nvl;
2552	const char *key;
2553
2554	nvl = nvlist_create(0);
2555	key = "test";
2556	nvlist_add_number(nvl, key, 584);
2557
2558	nvlist_free(nvl, key);
2559	ATF_REQUIRE(nvlist_empty(nvl));
2560
2561	nvlist_destroy(nvl);
2562}
2563
2564ATF_TEST_CASE_WITHOUT_HEAD(nvlist_free__single_string);
2565ATF_TEST_CASE_BODY(nvlist_free__single_string)
2566{
2567	nvlist_t *nvl;
2568	const char *key;
2569
2570	nvl = nvlist_create(0);
2571	key = "test";
2572	nvlist_add_string(nvl, key, "gjkfkjd");
2573
2574	nvlist_free(nvl, key);
2575	ATF_REQUIRE(nvlist_empty(nvl));
2576
2577	nvlist_destroy(nvl);
2578}
2579
2580ATF_TEST_CASE_WITHOUT_HEAD(nvlist_free__single_nvlist);
2581ATF_TEST_CASE_BODY(nvlist_free__single_nvlist)
2582{
2583	nvlist_t *nvl;
2584	const char *key;
2585
2586	nvl = nvlist_create(0);
2587	key = "test";
2588	nvlist_add_nvlist(nvl, key, nvlist_create(0));
2589
2590	nvlist_free(nvl, key);
2591	ATF_REQUIRE(nvlist_empty(nvl));
2592
2593	nvlist_destroy(nvl);
2594}
2595
2596ATF_TEST_CASE_WITHOUT_HEAD(nvlist_free__single_binary);
2597ATF_TEST_CASE_BODY(nvlist_free__single_binary)
2598{
2599	nvlist_t *nvl;
2600	const char *key;
2601
2602	nvl = nvlist_create(0);
2603	key = "test";
2604	nvlist_add_binary(nvl, key, "jgjgfd", 6);
2605
2606	nvlist_free(nvl, key);
2607	ATF_REQUIRE(nvlist_empty(nvl));
2608
2609	nvlist_destroy(nvl);
2610}
2611
2612ATF_TEST_CASE_WITHOUT_HEAD(nvlist_free_null__single_null);
2613ATF_TEST_CASE_BODY(nvlist_free_null__single_null)
2614{
2615	nvlist_t *nvl;
2616	const char *key;
2617
2618	nvl = nvlist_create(0);
2619	key = "test";
2620	nvlist_add_null(nvl, key);
2621
2622	nvlist_free_null(nvl, key);
2623	ATF_REQUIRE(nvlist_empty(nvl));
2624
2625	nvlist_destroy(nvl);
2626}
2627
2628ATF_TEST_CASE_WITHOUT_HEAD(nvlist_free_bool__single_bool);
2629ATF_TEST_CASE_BODY(nvlist_free_bool__single_bool)
2630{
2631	nvlist_t *nvl;
2632	const char *key;
2633
2634	nvl = nvlist_create(0);
2635	key = "test";
2636	nvlist_add_bool(nvl, key, true);
2637
2638	nvlist_free_bool(nvl, key);
2639	ATF_REQUIRE(nvlist_empty(nvl));
2640
2641	nvlist_destroy(nvl);
2642}
2643
2644ATF_TEST_CASE_WITHOUT_HEAD(nvlist_free_number__single_number);
2645ATF_TEST_CASE_BODY(nvlist_free_number__single_number)
2646{
2647	nvlist_t *nvl;
2648	const char *key;
2649
2650	nvl = nvlist_create(0);
2651	key = "test";
2652	nvlist_add_number(nvl, key, 584);
2653
2654	nvlist_free_number(nvl, key);
2655	ATF_REQUIRE(nvlist_empty(nvl));
2656
2657	nvlist_destroy(nvl);
2658}
2659
2660ATF_TEST_CASE_WITHOUT_HEAD(nvlist_free_string__single_string);
2661ATF_TEST_CASE_BODY(nvlist_free_string__single_string)
2662{
2663	nvlist_t *nvl;
2664	const char *key;
2665
2666	nvl = nvlist_create(0);
2667	key = "test";
2668	nvlist_add_string(nvl, key, "gjkfkjd");
2669
2670	nvlist_free_string(nvl, key);
2671	ATF_REQUIRE(nvlist_empty(nvl));
2672
2673	nvlist_destroy(nvl);
2674}
2675
2676ATF_TEST_CASE_WITHOUT_HEAD(nvlist_free_nvlist__single_nvlist);
2677ATF_TEST_CASE_BODY(nvlist_free_nvlist__single_nvlist)
2678{
2679	nvlist_t *nvl;
2680	const char *key;
2681
2682	nvl = nvlist_create(0);
2683	key = "test";
2684	nvlist_add_nvlist(nvl, key, nvlist_create(0));
2685
2686	nvlist_free_nvlist(nvl, key);
2687	ATF_REQUIRE(nvlist_empty(nvl));
2688
2689	nvlist_destroy(nvl);
2690}
2691
2692ATF_TEST_CASE_WITHOUT_HEAD(nvlist_free_binary__single_binary);
2693ATF_TEST_CASE_BODY(nvlist_free_binary__single_binary)
2694{
2695	nvlist_t *nvl;
2696	const char *key;
2697
2698	nvl = nvlist_create(0);
2699	key = "test";
2700	nvlist_add_binary(nvl, key, "jgjgfd", 6);
2701
2702	nvlist_free_binary(nvl, key);
2703	ATF_REQUIRE(nvlist_empty(nvl));
2704
2705	nvlist_destroy(nvl);
2706}
2707
2708ATF_INIT_TEST_CASES(tp)
2709{
2710	ATF_ADD_TEST_CASE(tp, nvlist_create__is_empty);
2711	ATF_ADD_TEST_CASE(tp, nvlist_add_null__single_insert);
2712	ATF_ADD_TEST_CASE(tp, nvlist_add_bool__single_insert);
2713	ATF_ADD_TEST_CASE(tp, nvlist_add_number__single_insert);
2714	ATF_ADD_TEST_CASE(tp, nvlist_add_string__single_insert);
2715	ATF_ADD_TEST_CASE(tp, nvlist_add_nvlist__single_insert);
2716	ATF_ADD_TEST_CASE(tp, nvlist_add_binary__single_insert);
2717
2718	ATF_ADD_TEST_CASE(tp, nvlist_clone__empty_nvlist);
2719	ATF_ADD_TEST_CASE(tp, nvlist_clone__nonempty_nvlist);
2720	ATF_ADD_TEST_CASE(tp, nvlist_clone__nested_nvlist);
2721
2722	ATF_ADD_TEST_CASE(tp, nvlist_pack__empty_nvlist);
2723	ATF_ADD_TEST_CASE(tp, nvlist_pack__multiple_values);
2724	ATF_ADD_TEST_CASE(tp, nvlist_unpack__duplicate_key);
2725
2726	ATF_ADD_TEST_CASE(tp, nvlist_move_string__single_insert);
2727	ATF_ADD_TEST_CASE(tp, nvlist_move_nvlist__single_insert);
2728	ATF_ADD_TEST_CASE(tp, nvlist_move_nvlist__null_child);
2729	ATF_ADD_TEST_CASE(tp, nvlist_move_binary__single_insert);
2730
2731	ATF_ADD_TEST_CASE(tp, nvlist_take_bool__single_remove);
2732	ATF_ADD_TEST_CASE(tp, nvlist_take_bool__other_keys_unchanged);
2733	ATF_ADD_TEST_CASE(tp, nvlist_take_number__single_remove);
2734	ATF_ADD_TEST_CASE(tp, nvlist_take_number__other_keys_unchanged);
2735	ATF_ADD_TEST_CASE(tp, nvlist_take_string__single_remove);
2736	ATF_ADD_TEST_CASE(tp, nvlist_take_string__other_keys_unchanged);
2737	ATF_ADD_TEST_CASE(tp, nvlist_take_nvlist__single_remove);
2738	ATF_ADD_TEST_CASE(tp, nvlist_take_nvlist__other_keys_unchanged);
2739	ATF_ADD_TEST_CASE(tp, nvlist_take_binary__single_remove);
2740	ATF_ADD_TEST_CASE(tp, nvlist_take_binary__other_keys_unchanged);
2741
2742	ATF_ADD_TEST_CASE(tp, nvlist_free__single_null);
2743	ATF_ADD_TEST_CASE(tp, nvlist_free__single_bool);
2744	ATF_ADD_TEST_CASE(tp, nvlist_free__single_number);
2745	ATF_ADD_TEST_CASE(tp, nvlist_free__single_string);
2746	ATF_ADD_TEST_CASE(tp, nvlist_free__single_nvlist);
2747	ATF_ADD_TEST_CASE(tp, nvlist_free__single_binary);
2748
2749	ATF_ADD_TEST_CASE(tp, nvlist_free_null__single_null);
2750	ATF_ADD_TEST_CASE(tp, nvlist_free_bool__single_bool);
2751	ATF_ADD_TEST_CASE(tp, nvlist_free_number__single_number);
2752	ATF_ADD_TEST_CASE(tp, nvlist_free_string__single_string);
2753	ATF_ADD_TEST_CASE(tp, nvlist_free_nvlist__single_nvlist);
2754	ATF_ADD_TEST_CASE(tp, nvlist_free_binary__single_binary);
2755}
2756