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