xref: /freebsd/lib/libnv/tests/nv_tests.cc (revision 86c9d9918f1db7cdd968b60f8902466887bcd9e9)
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$");
29 
30 #include <sys/nv.h>
31 
32 #include <atf-c++.hpp>
33 
34 #include <errno.h>
35 #include <limits>
36 #include <set>
37 #include <sstream>
38 #include <string>
39 
40 /*
41  * Test that a newly created nvlist has no errors, and is empty.
42  */
43 ATF_TEST_CASE_WITHOUT_HEAD(nvlist_create__is_empty);
44 ATF_TEST_CASE_BODY(nvlist_create__is_empty)
45 {
46 	nvlist_t *nvl;
47 	int type;
48 	void *it;
49 
50 	nvl = nvlist_create(0);
51 
52 	ATF_REQUIRE(nvl != NULL);
53 
54 	ATF_REQUIRE_EQ(nvlist_error(nvl), 0);
55 	ATF_REQUIRE(nvlist_empty(nvl));
56 
57 	it = NULL;
58 	ATF_REQUIRE_EQ(nvlist_next(nvl, &type, &it), static_cast<const char *>(NULL));
59 
60 	nvlist_destroy(nvl);
61 }
62 
63 ATF_TEST_CASE_WITHOUT_HEAD(nvlist_add_null__single_insert);
64 ATF_TEST_CASE_BODY(nvlist_add_null__single_insert)
65 {
66 	nvlist_t *nvl;
67 	void *it;
68 	const char *key;
69 	int type;
70 
71 	key = "key";
72 	nvl = nvlist_create(0);
73 
74 	ATF_REQUIRE(nvl != NULL);
75 	ATF_REQUIRE(!nvlist_exists(nvl, key));
76 
77 	nvlist_add_null(nvl, key);
78 
79 	ATF_REQUIRE(!nvlist_empty(nvl));
80 	ATF_REQUIRE(nvlist_exists(nvl, key));
81 	ATF_REQUIRE(nvlist_exists_null(nvl, key));
82 	ATF_REQUIRE(nvlist_exists_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), static_cast<const char *>(NULL));
89 
90 	nvlist_destroy(nvl);
91 }
92 
93 ATF_TEST_CASE_WITHOUT_HEAD(nvlist_add_bool__single_insert);
94 ATF_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_exists(nvl, "name"));
112 	ATF_REQUIRE(nvlist_exists_bool(nvl, key));
113 	ATF_REQUIRE(nvlist_exists_bool(nvl, "name"));
114 	ATF_REQUIRE_EQ(nvlist_get_bool(nvl, key), 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), static_cast<const char *>(NULL));
121 
122 	nvlist_destroy(nvl);
123 }
124 
125 ATF_TEST_CASE_WITHOUT_HEAD(nvlist_add_number__single_insert);
126 ATF_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_exists(nvl, "foo123"));
146 	ATF_REQUIRE(nvlist_exists_number(nvl, key));
147 	ATF_REQUIRE_EQ(nvlist_get_number(nvl, key), value);
148 
149 	/* Iterate over the nvlist; ensure that it has only our one key. */
150 	it = NULL;
151 	ATF_REQUIRE_EQ(strcmp(nvlist_next(nvl, &type, &it), key), 0);
152 	ATF_REQUIRE_EQ(type, NV_TYPE_NUMBER);
153 	ATF_REQUIRE_EQ(nvlist_next(nvl, &type,&it), static_cast<const char *>(NULL));
154 
155 	nvlist_destroy(nvl);
156 }
157 
158 ATF_TEST_CASE_WITHOUT_HEAD(nvlist_add_string__single_insert);
159 ATF_TEST_CASE_BODY(nvlist_add_string__single_insert)
160 {
161 	nvlist_t *nvl;
162 	void *it;
163 	const char *key;
164 	const char *value;
165 	int type;
166 
167 	key = "test";
168 	value = "fgjdkgjdk";
169 	nvl = nvlist_create(0);
170 
171 	ATF_REQUIRE(nvl != NULL);
172 	ATF_REQUIRE(!nvlist_exists(nvl, key));
173 
174 	nvlist_add_string(nvl, key, value);
175 
176 	ATF_REQUIRE(!nvlist_empty(nvl));
177 	ATF_REQUIRE(nvlist_exists(nvl, key));
178 	ATF_REQUIRE(nvlist_exists(nvl, "test"));
179 	ATF_REQUIRE(nvlist_exists_string(nvl, key));
180 	ATF_REQUIRE(nvlist_exists_string(nvl, "test"));
181 	ATF_REQUIRE_EQ(strcmp(nvlist_get_string(nvl, key), value), 0);
182 
183 	/* nvlist_add_* is required to clone the value, so check for that. */
184 	ATF_REQUIRE(nvlist_get_string(nvl, key) != value);
185 
186 	/* Iterate over the nvlist; ensure that it has only our one key. */
187 	it = NULL;
188 	ATF_REQUIRE_EQ(strcmp(nvlist_next(nvl, &type, &it), key), 0);
189 	ATF_REQUIRE_EQ(type, NV_TYPE_STRING);
190 	ATF_REQUIRE_EQ(nvlist_next(nvl, &type,&it), static_cast<const char *>(NULL));
191 
192 	nvlist_destroy(nvl);
193 }
194 
195 ATF_TEST_CASE_WITHOUT_HEAD(nvlist_add_nvlist__single_insert);
196 ATF_TEST_CASE_BODY(nvlist_add_nvlist__single_insert)
197 {
198 	nvlist_t *nvl;
199 	void *it;
200 	const char *key, *subkey;
201 	nvlist_t *sublist;
202 	const nvlist_t *value;
203 	int type;
204 
205 	key = "test";
206 	subkey = "subkey";
207 	sublist = nvlist_create(0);
208 	nvl = nvlist_create(0);
209 
210 	ATF_REQUIRE(nvl != NULL);
211 	ATF_REQUIRE(!nvlist_exists(nvl, key));
212 
213 	nvlist_add_null(sublist, subkey);
214 	nvlist_add_nvlist(nvl, key, sublist);
215 
216 	ATF_REQUIRE(!nvlist_empty(nvl));
217 	ATF_REQUIRE(nvlist_exists(nvl, key));
218 	ATF_REQUIRE(nvlist_exists(nvl, "test"));
219 	ATF_REQUIRE(nvlist_exists_nvlist(nvl, key));
220 	ATF_REQUIRE(nvlist_exists_nvlist(nvl, "test"));
221 
222 	value = nvlist_get_nvlist(nvl, key);
223 	ATF_REQUIRE(nvlist_exists_null(value, subkey));
224 
225 	/* nvlist_add_* is required to clone the value, so check for that. */
226 	ATF_REQUIRE(sublist != value);
227 
228 	/* Iterate over the nvlist; ensure that it has only our one key. */
229 	it = NULL;
230 	ATF_REQUIRE_EQ(strcmp(nvlist_next(nvl, &type, &it), key), 0);
231 	ATF_REQUIRE_EQ(type, NV_TYPE_NVLIST);
232 	ATF_REQUIRE_EQ(nvlist_next(nvl, &type,&it), static_cast<const char *>(NULL));
233 
234 	nvlist_destroy(sublist);
235 	nvlist_destroy(nvl);
236 }
237 
238 ATF_TEST_CASE_WITHOUT_HEAD(nvlist_add_nvlist__child_with_error);
239 ATF_TEST_CASE_BODY(nvlist_add_nvlist__child_with_error)
240 {
241 	nvlist_t *nvl, *parent;
242 
243 	nvl = nvlist_create(0);
244 	parent = nvlist_create(0);
245 
246 	nvlist_set_error(nvl, EBADF);
247 	nvlist_add_nvlist(parent, "test", nvl);
248 	ATF_REQUIRE_EQ(nvlist_error(parent), EBADF);
249 
250 	nvlist_destroy(nvl);
251 	nvlist_destroy(parent);
252 }
253 
254 ATF_TEST_CASE_WITHOUT_HEAD(nvlist_add_binary__single_insert);
255 ATF_TEST_CASE_BODY(nvlist_add_binary__single_insert)
256 {
257 	nvlist_t *nvl;
258 	void *it;
259 	const char *key;
260 	void *value;
261 	const void *ret_value;
262 	size_t value_size, ret_size;
263 	int type;
264 
265 	key = "binary";
266 	value_size = 13;
267 	value = malloc(value_size);
268 	memset(value, 0xa5, value_size);
269 	nvl = nvlist_create(0);
270 
271 	ATF_REQUIRE(nvl != NULL);
272 	ATF_REQUIRE(!nvlist_exists(nvl, key));
273 
274 	nvlist_add_binary(nvl, key, value, value_size);
275 
276 	ATF_REQUIRE(!nvlist_empty(nvl));
277 	ATF_REQUIRE(nvlist_exists(nvl, key));
278 	ATF_REQUIRE(nvlist_exists(nvl, "binary"));
279 	ATF_REQUIRE(nvlist_exists_binary(nvl, key));
280 	ATF_REQUIRE(nvlist_exists_binary(nvl, "binary"));
281 
282 	ret_value = nvlist_get_binary(nvl, key, &ret_size);
283 	ATF_REQUIRE_EQ(value_size, ret_size);
284 	ATF_REQUIRE_EQ(memcmp(value, ret_value, ret_size), 0);
285 
286 	/* nvlist_add_* is required to clone the value, so check for that. */
287 	ATF_REQUIRE(value != ret_value);
288 
289 	/* Iterate over the nvlist; ensure that it has only our one key. */
290 	it = NULL;
291 	ATF_REQUIRE_EQ(strcmp(nvlist_next(nvl, &type, &it), key), 0);
292 	ATF_REQUIRE_EQ(type, NV_TYPE_BINARY);
293 	ATF_REQUIRE_EQ(nvlist_next(nvl, &type,&it), static_cast<const char *>(NULL));
294 
295 	nvlist_destroy(nvl);
296 	free(value);
297 }
298 
299 ATF_TEST_CASE_WITHOUT_HEAD(nvlist_clone__empty_nvlist);
300 ATF_TEST_CASE_BODY(nvlist_clone__empty_nvlist)
301 {
302 	nvlist_t *nvl, *clone;
303 
304 	nvl = nvlist_create(0);
305 	ATF_REQUIRE(nvl != NULL);
306 
307 	clone = nvlist_clone(nvl);
308 	ATF_REQUIRE(clone != NULL);
309 	ATF_REQUIRE(clone != nvl);
310 	ATF_REQUIRE(nvlist_empty(clone));
311 
312 	nvlist_destroy(clone);
313 	nvlist_destroy(nvl);
314 }
315 
316 ATF_TEST_CASE_WITHOUT_HEAD(nvlist_clone__nonempty_nvlist);
317 ATF_TEST_CASE_BODY(nvlist_clone__nonempty_nvlist)
318 {
319 	nvlist_t *nvl, *clone;
320 	const char *key;
321 	void *it;
322 	uint64_t value;
323 	int type;
324 
325 	nvl = nvlist_create(0);
326 	ATF_REQUIRE(nvl != NULL);
327 
328 	key = "testkey";
329 	value = 684874;
330 	nvlist_add_number(nvl, key, value);
331 
332 	clone = nvlist_clone(nvl);
333 	ATF_REQUIRE(clone != NULL);
334 	ATF_REQUIRE(clone != nvl);
335 	ATF_REQUIRE(nvlist_exists_number(clone, key));
336 	ATF_REQUIRE_EQ(nvlist_get_number(clone, key), value);
337 
338 	/* Iterate over the nvlist; ensure that it has only our one key. */
339 	it = NULL;
340 	ATF_REQUIRE_EQ(strcmp(nvlist_next(clone, &type, &it), key), 0);
341 	ATF_REQUIRE_EQ(type, NV_TYPE_NUMBER);
342 	ATF_REQUIRE_EQ(nvlist_next(clone, &type, &it), static_cast<const char *>(NULL));
343 
344 	nvlist_destroy(clone);
345 	nvlist_destroy(nvl);
346 }
347 
348 static const char * const test_subnvlist_key = "nvlist";
349 
350 static const char * const test_string_key = "string";
351 static const char * const test_string_val = "59525";
352 
353 static nvlist_t*
354 create_test_nvlist(void)
355 {
356 	nvlist_t *nvl, *sublist;
357 
358 	nvl = nvlist_create(0);
359 	ATF_REQUIRE(nvl != NULL);
360 
361 	sublist = nvlist_create(0);
362 	ATF_REQUIRE(sublist != NULL);
363 
364 	nvlist_add_string(sublist, test_string_key, test_string_val);
365 	nvlist_move_nvlist(nvl, test_subnvlist_key, sublist);
366 
367 	return (nvl);
368 }
369 
370 static void
371 verify_test_nvlist(const nvlist_t *nvl)
372 {
373 	void *it;
374 	const nvlist_t *value;
375 	int type;
376 
377 	ATF_REQUIRE(nvlist_exists_nvlist(nvl, test_subnvlist_key));
378 
379 	value = nvlist_get_nvlist(nvl, test_subnvlist_key);
380 
381 	ATF_REQUIRE(nvlist_exists_string(value, test_string_key));
382 	ATF_REQUIRE_EQ(strcmp(nvlist_get_string(value, test_string_key), test_string_val), 0);
383 	ATF_REQUIRE(nvlist_get_string(value, test_string_key) != test_string_val);
384 
385 	/* Iterate over both nvlists; ensure that each has only the one key. */
386 	it = NULL;
387 	ATF_REQUIRE_EQ(strcmp(nvlist_next(value, &type, &it),
388 	    test_string_key), 0);
389 	ATF_REQUIRE_EQ(type, NV_TYPE_STRING);
390 	ATF_REQUIRE_EQ(nvlist_next(value, &type, &it), static_cast<const char *>(NULL));
391 
392 	it = NULL;
393 	ATF_REQUIRE_EQ(strcmp(nvlist_next(nvl, &type, &it),
394 	    test_subnvlist_key), 0);
395 	ATF_REQUIRE_EQ(type, NV_TYPE_NVLIST);
396 	ATF_REQUIRE_EQ(nvlist_next(nvl, &type, &it), static_cast<const char *>(NULL));
397 }
398 
399 ATF_TEST_CASE_WITHOUT_HEAD(nvlist_clone__nested_nvlist);
400 ATF_TEST_CASE_BODY(nvlist_clone__nested_nvlist)
401 {
402 	nvlist_t *nvl, *clone;
403 
404 	nvl = create_test_nvlist();
405 	clone = nvlist_clone(nvl);
406 
407 	ATF_REQUIRE(clone != NULL);
408 	ATF_REQUIRE(clone != nvl);
409 	verify_test_nvlist(clone);
410 
411 	nvlist_destroy(clone);
412 	nvlist_destroy(nvl);
413 }
414 
415 ATF_TEST_CASE_WITHOUT_HEAD(nvlist_clone__error_nvlist);
416 ATF_TEST_CASE_BODY(nvlist_clone__error_nvlist)
417 {
418 	nvlist_t *nvl, *clone;
419 
420 	nvl = nvlist_create(0);
421 	ATF_REQUIRE(nvl != NULL);
422 
423 	nvlist_set_error(nvl, ENOMEM);
424 
425 	clone = nvlist_clone(nvl);
426 	ATF_REQUIRE(clone == NULL);
427 
428 	nvlist_destroy(nvl);
429 }
430 
431 ATF_TEST_CASE_WITHOUT_HEAD(nvlist_pack__empty_nvlist);
432 ATF_TEST_CASE_BODY(nvlist_pack__empty_nvlist)
433 {
434 	nvlist_t *nvl, *unpacked;
435 	void *packed;
436 	size_t packed_size;
437 
438 	nvl = nvlist_create(0);
439 	ATF_REQUIRE(nvl != NULL);
440 
441 	packed = nvlist_pack(nvl, &packed_size);
442 	ATF_REQUIRE(packed != NULL);
443 
444 	unpacked = nvlist_unpack(packed, packed_size, 0);
445 	ATF_REQUIRE(unpacked != NULL);
446 	ATF_REQUIRE(unpacked != nvl);
447 	ATF_REQUIRE(nvlist_empty(unpacked));
448 
449 	nvlist_destroy(unpacked);
450 	nvlist_destroy(nvl);
451 	free(packed);
452 }
453 
454 ATF_TEST_CASE_WITHOUT_HEAD(nvlist_unpack__flags_nvlist);
455 ATF_TEST_CASE_BODY(nvlist_unpack__flags_nvlist)
456 {
457 	nvlist_t *nvl, *unpacked;
458 	void *packed;
459 	size_t packed_size;
460 
461 	nvl = nvlist_create(NV_FLAG_NO_UNIQUE);
462 	ATF_REQUIRE(nvl != NULL);
463 
464 	nvlist_add_bool(nvl, "name", true);
465 	ATF_REQUIRE(!nvlist_empty(nvl));
466 	ATF_REQUIRE(nvlist_exists_bool(nvl, "name"));
467 
468 	packed = nvlist_pack(nvl, &packed_size);
469 	ATF_REQUIRE(packed != NULL);
470 
471 	unpacked = nvlist_unpack(packed, packed_size, 0);
472 	ATF_REQUIRE(unpacked == NULL);
473 
474 	unpacked = nvlist_unpack(packed, packed_size, NV_FLAG_IGNORE_CASE);
475 	ATF_REQUIRE(unpacked == NULL);
476 
477 	unpacked = nvlist_unpack(packed, packed_size, NV_FLAG_NO_UNIQUE);
478 	ATF_REQUIRE(unpacked != NULL);
479 	ATF_REQUIRE(unpacked != nvl);
480 	ATF_REQUIRE(!nvlist_empty(unpacked));
481 	ATF_REQUIRE(nvlist_exists_bool(unpacked, "name"));
482 
483 	nvlist_destroy(unpacked);
484 	nvlist_destroy(nvl);
485 	free(packed);
486 }
487 
488 static void
489 verify_null(const nvlist_t *nvl, int type)
490 {
491 
492 	ATF_REQUIRE_EQ(type, NV_TYPE_NULL);
493 }
494 
495 static void
496 verify_number(const nvlist_t *nvl, const char *name, int type, uint64_t value)
497 {
498 
499 	ATF_REQUIRE_EQ(type, NV_TYPE_NUMBER);
500 	ATF_REQUIRE_EQ(nvlist_get_number(nvl, name), value);
501 }
502 
503 static void
504 verify_string(const nvlist_t *nvl, const char *name, int type,
505     const char * value)
506 {
507 
508 	ATF_REQUIRE_EQ(type, NV_TYPE_STRING);
509 	ATF_REQUIRE_EQ(strcmp(nvlist_get_string(nvl, name), value), 0);
510 }
511 
512 static void
513 verify_nvlist(const nvlist_t *nvl, const char *name, int type)
514 {
515 
516 	ATF_REQUIRE_EQ(type, NV_TYPE_NVLIST);
517 	verify_test_nvlist(nvlist_get_nvlist(nvl, name));
518 }
519 
520 static void
521 verify_binary(const nvlist_t *nvl, const char *name, int type,
522     const void * value, size_t size)
523 {
524 	const void *actual_value;
525 	size_t actual_size;
526 
527 	ATF_REQUIRE_EQ(type, NV_TYPE_BINARY);
528 	actual_value = nvlist_get_binary(nvl, name, &actual_size);
529 	ATF_REQUIRE_EQ(size, actual_size);
530 	ATF_REQUIRE_EQ(memcmp(value, actual_value, size), 0);
531 }
532 
533 ATF_TEST_CASE_WITHOUT_HEAD(nvlist_pack__multiple_values);
534 ATF_TEST_CASE_BODY(nvlist_pack__multiple_values)
535 {
536 	std::ostringstream msg;
537 	std::set<std::string> keys_seen;
538 	nvlist_t *nvl, *unpacked, *nvvalue;
539 	const char *nullkey, *numkey, *strkey, *nvkey, *binkey, *name;
540 	int numvalue;
541 	const char * strvalue;
542 	void *binvalue, *packed, *it;
543 	size_t binsize, packed_size;
544 	int type;
545 
546 	nvl = nvlist_create(0);
547 
548 	nullkey = "null";
549 	nvlist_add_null(nvl, nullkey);
550 
551 	numkey = "number";
552 	numvalue = 939853984;
553 	nvlist_add_number(nvl, numkey, numvalue);
554 
555 	strkey = "string";
556 	strvalue = "jfieutijf";
557 	nvlist_add_string(nvl, strkey, strvalue);
558 
559 	nvkey = "nvlist";
560 	nvvalue = create_test_nvlist();
561 	nvlist_move_nvlist(nvl, nvkey, nvvalue);
562 
563 	binkey = "binary";
564 	binsize = 4;
565 	binvalue = malloc(binsize);
566 	memset(binvalue, 'b', binsize);
567 	nvlist_move_binary(nvl, binkey, binvalue, binsize);
568 
569 	packed = nvlist_pack(nvl, &packed_size);
570 	ATF_REQUIRE(packed != NULL);
571 
572 	unpacked = nvlist_unpack(packed, packed_size, 0);
573 	ATF_REQUIRE(unpacked != 0);
574 
575 	it = NULL;
576 	while ((name = nvlist_next(unpacked, &type, &it)) != NULL) {
577 		/* Ensure that we see every key only once. */
578 		ATF_REQUIRE_EQ(keys_seen.count(name), 0);
579 
580 		if (strcmp(name, nullkey) == 0)
581 			verify_null(unpacked, type);
582 		else if (strcmp(name, numkey) == 0)
583 			verify_number(unpacked, name, type, numvalue);
584 		else if (strcmp(name, strkey) == 0)
585 			verify_string(unpacked, name, type, strvalue);
586 		else if (strcmp(name, nvkey) == 0)
587 			verify_nvlist(unpacked, name, type);
588 		else if (strcmp(name, binkey) == 0)
589 			verify_binary(unpacked, name, type, binvalue, binsize);
590 		else {
591 			msg << "Unexpected key :'" << name << "'";
592 			ATF_FAIL(msg.str().c_str());
593 		}
594 
595 		keys_seen.insert(name);
596 	}
597 
598 	/* Ensure that we saw every key. */
599 	ATF_REQUIRE_EQ(keys_seen.size(), 5);
600 
601 	nvlist_destroy(nvl);
602 	nvlist_destroy(unpacked);
603 	free(packed);
604 }
605 
606 ATF_TEST_CASE_WITHOUT_HEAD(nvlist_pack__error_nvlist);
607 ATF_TEST_CASE_BODY(nvlist_pack__error_nvlist)
608 {
609 	nvlist_t *nvl;
610 	void *packed;
611 	size_t size;
612 
613 	nvl = nvlist_create(0);
614 	ATF_REQUIRE(nvl != NULL);
615 
616 	nvlist_set_error(nvl, ENOMEM);
617 
618 	packed = nvlist_pack(nvl, &size);
619 	ATF_REQUIRE(packed == NULL);
620 
621 	nvlist_destroy(nvl);
622 }
623 
624 ATF_TEST_CASE_WITHOUT_HEAD(nvlist_unpack__duplicate_key);
625 ATF_TEST_CASE_BODY(nvlist_unpack__duplicate_key)
626 {
627 	nvlist_t *nvl, *unpacked;
628 	const char *key1, *key2;
629 	void *packed, *keypos;
630 	size_t size, keylen;
631 
632 	nvl = nvlist_create(0);
633 
634 	key1 = "key1";
635 	keylen = strlen(key1);
636 	nvlist_add_number(nvl, key1, 5);
637 
638 	key2 = "key2";
639 	ATF_REQUIRE_EQ(keylen, strlen(key2));
640 	nvlist_add_number(nvl, key2, 10);
641 
642 	packed = nvlist_pack(nvl, &size);
643 
644 	/*
645 	 * Mangle the packed nvlist by replacing key1 with key2, creating a
646 	 * packed nvlist with a duplicate key.
647 	 */
648 	keypos = memmem(packed, size, key1, keylen);
649 	ATF_REQUIRE(keypos != NULL);
650 	memcpy(keypos, key2, keylen);
651 
652 	unpacked = nvlist_unpack(packed, size, 0);
653 	ATF_REQUIRE(nvlist_error(unpacked) != 0);
654 
655 	free(packed);
656 	nvlist_destroy(nvl);
657 	nvlist_destroy(unpacked);
658 }
659 
660 ATF_TEST_CASE_WITHOUT_HEAD(nvlist_move_string__single_insert);
661 ATF_TEST_CASE_BODY(nvlist_move_string__single_insert)
662 {
663 	nvlist_t *nvl;
664 	const char *key;
665 	char *value;
666 
667 	nvl = nvlist_create(0);
668 	ATF_REQUIRE(nvl != NULL);
669 
670 	key = "testkey";
671 	value = strdup("testval");
672 	ATF_REQUIRE(value != NULL);
673 
674 	nvlist_move_string(nvl, key, value);
675 	ATF_REQUIRE_EQ(nvlist_get_string(nvl, key), value);
676 
677 	nvlist_destroy(nvl);
678 }
679 
680 ATF_TEST_CASE_WITHOUT_HEAD(nvlist_move_nvlist__null_child);
681 ATF_TEST_CASE_BODY(nvlist_move_nvlist__null_child)
682 {
683 	nvlist_t *parent;
684 
685 	parent = nvlist_create(0);
686 
687 	nvlist_move_nvlist(parent, "test", NULL);
688 
689 	ATF_REQUIRE(nvlist_error(parent) != 0);
690 
691 	nvlist_destroy(parent);
692 }
693 
694 ATF_TEST_CASE_WITHOUT_HEAD(nvlist_move_nvlist__child_with_error);
695 ATF_TEST_CASE_BODY(nvlist_move_nvlist__child_with_error)
696 {
697 	nvlist_t *nvl, *parent;
698 
699 	nvl = nvlist_create(0);
700 	parent = nvlist_create(0);
701 
702 	nvlist_set_error(nvl, EBADF);
703 	nvlist_move_nvlist(parent, "test", nvl);
704 	ATF_REQUIRE_EQ(nvlist_error(parent), EBADF);
705 
706 	nvlist_destroy(parent);
707 }
708 
709 ATF_TEST_CASE_WITHOUT_HEAD(nvlist_move_nvlist__single_insert);
710 ATF_TEST_CASE_BODY(nvlist_move_nvlist__single_insert)
711 {
712 	nvlist_t *nvl;
713 	const char *key;
714 	nvlist_t *value;
715 
716 	nvl = nvlist_create(0);
717 	ATF_REQUIRE(nvl != NULL);
718 
719 	key = "testkey";
720 	value = nvlist_create(0);
721 	ATF_REQUIRE(value != NULL);
722 
723 	nvlist_move_nvlist(nvl, key, value);
724 	ATF_REQUIRE_EQ(nvlist_get_nvlist(nvl, key), value);
725 
726 	nvlist_destroy(nvl);
727 }
728 
729 ATF_TEST_CASE_WITHOUT_HEAD(nvlist_move_binary__single_insert);
730 ATF_TEST_CASE_BODY(nvlist_move_binary__single_insert)
731 {
732 	nvlist_t *nvl;
733 	const char *key;
734 	void *value;
735 	size_t size, actual_size;
736 
737 	nvl = nvlist_create(0);
738 	ATF_REQUIRE(nvl != NULL);
739 
740 	key = "testkey";
741 	size = 73;
742 	value = malloc(size);
743 	ATF_REQUIRE(value != NULL);
744 
745 	nvlist_move_binary(nvl, key, value, size);
746 	ATF_REQUIRE_EQ(nvlist_get_binary(nvl, key, &actual_size), value);
747 	ATF_REQUIRE_EQ(size, actual_size);
748 
749 	nvlist_destroy(nvl);
750 }
751 
752 ATF_TEST_CASE_WITHOUT_HEAD(nvlist_take_bool__single_remove);
753 ATF_TEST_CASE_BODY(nvlist_take_bool__single_remove)
754 {
755 	nvlist_t *nvl;
756 	const char *testkey;
757 	bool testval;
758 
759 	nvl = nvlist_create(0);
760 	ATF_REQUIRE(nvl != NULL);
761 
762 	testkey = "boolkey";
763 	testval = false;
764 	nvlist_add_bool(nvl, testkey, testval);
765 
766 	ATF_REQUIRE_EQ(nvlist_take_bool(nvl, testkey), testval);
767 	ATF_REQUIRE(nvlist_empty(nvl));
768 
769 	nvlist_destroy(nvl);
770 }
771 
772 ATF_TEST_CASE_WITHOUT_HEAD(nvlist_take_bool__other_keys_unchanged);
773 ATF_TEST_CASE_BODY(nvlist_take_bool__other_keys_unchanged)
774 {
775 	nvlist_t *nvl;
776 	const char *testkey, *otherkey1, *otherkey2;
777 	bool testval, otherval1;
778 	nvlist_t *otherval2;
779 
780 	nvl = nvlist_create(0);
781 	ATF_REQUIRE(nvl != NULL);
782 
783 	testkey = "boolkey";
784 	testval = true;
785 	nvlist_add_bool(nvl, testkey, testval);
786 
787 	otherkey1 = "key1";
788 	otherval1 = false;
789 	nvlist_add_bool(nvl, otherkey1, otherval1);
790 
791 	otherkey2 = "key2";
792 	otherval2 = create_test_nvlist();
793 	nvlist_move_nvlist(nvl, otherkey2, otherval2);
794 
795 	ATF_REQUIRE_EQ(nvlist_take_bool(nvl, testkey), testval);
796 
797 	ATF_REQUIRE(nvlist_exists_bool(nvl, otherkey1));
798 	ATF_REQUIRE_EQ(nvlist_get_bool(nvl, otherkey1), otherval1);
799 
800 	ATF_REQUIRE(nvlist_exists_nvlist(nvl, otherkey2));
801 	verify_test_nvlist(nvlist_get_nvlist(nvl, otherkey2));
802 
803 	nvlist_destroy(nvl);
804 }
805 
806 ATF_TEST_CASE_WITHOUT_HEAD(nvlist_take_number__single_remove);
807 ATF_TEST_CASE_BODY(nvlist_take_number__single_remove)
808 {
809 	nvlist_t *nvl;
810 	const char *testkey;
811 	uint64_t testval;
812 
813 	nvl = nvlist_create(0);
814 	ATF_REQUIRE(nvl != NULL);
815 
816 	testkey = "numkey";
817 	testval = std::numeric_limits<uint64_t>::max();
818 	nvlist_add_number(nvl, testkey, testval);
819 
820 	ATF_REQUIRE_EQ(nvlist_take_number(nvl, testkey), testval);
821 	ATF_REQUIRE(nvlist_empty(nvl));
822 
823 	nvlist_destroy(nvl);
824 }
825 
826 ATF_TEST_CASE_WITHOUT_HEAD(nvlist_take_number__other_keys_unchanged);
827 ATF_TEST_CASE_BODY(nvlist_take_number__other_keys_unchanged)
828 {
829 	nvlist_t *nvl;
830 	const char *testkey, *otherkey1, *otherkey2;
831 	uint64_t testval, otherval1;
832 	const char *otherval2;
833 
834 	nvl = nvlist_create(0);
835 	ATF_REQUIRE(nvl != NULL);
836 
837 	otherkey1 = "key1";
838 	otherval1 = 5;
839 	nvlist_add_number(nvl, otherkey1, otherval1);
840 
841 	testkey = "numkey";
842 	testval = 1654;
843 	nvlist_add_number(nvl, testkey, testval);
844 
845 	otherkey2 = "key2";
846 	otherval2 = "string";
847 	nvlist_add_string(nvl, otherkey2, otherval2);
848 
849 	ATF_REQUIRE_EQ(nvlist_take_number(nvl, testkey), testval);
850 
851 	ATF_REQUIRE(nvlist_exists_number(nvl, otherkey1));
852 	ATF_REQUIRE_EQ(nvlist_get_number(nvl, otherkey1), otherval1);
853 
854 	ATF_REQUIRE(nvlist_exists_string(nvl, otherkey2));
855 	ATF_REQUIRE_EQ(strcmp(nvlist_get_string(nvl, otherkey2), otherval2), 0);
856 
857 	nvlist_destroy(nvl);
858 }
859 
860 ATF_TEST_CASE_WITHOUT_HEAD(nvlist_take_string__single_remove);
861 ATF_TEST_CASE_BODY(nvlist_take_string__single_remove)
862 {
863 	nvlist_t *nvl;
864 	const char *testkey;
865 	const char *testval;
866 
867 	nvl = nvlist_create(0);
868 	ATF_REQUIRE(nvl != NULL);
869 
870 	testkey = "numkey";
871 	testval = "nvlist";
872 	nvlist_add_string(nvl, testkey, testval);
873 
874 	ATF_REQUIRE_EQ(strcmp(nvlist_take_string(nvl, testkey), testval), 0);
875 	ATF_REQUIRE(nvlist_empty(nvl));
876 
877 	nvlist_destroy(nvl);
878 }
879 
880 ATF_TEST_CASE_WITHOUT_HEAD(nvlist_take_string__other_keys_unchanged);
881 ATF_TEST_CASE_BODY(nvlist_take_string__other_keys_unchanged)
882 {
883 	nvlist_t *nvl;
884 	const char *testkey, *otherkey1, *otherkey2;
885 	const char *testval, *otherval1;
886 	bool otherval2;
887 
888 	nvl = nvlist_create(0);
889 	ATF_REQUIRE(nvl != NULL);
890 
891 	otherkey1 = "key1";
892 	otherval1 = "fjdifjdk";
893 	nvlist_add_string(nvl, otherkey1, otherval1);
894 
895 	otherkey2 = "key2";
896 	otherval2 = true;
897 	nvlist_add_bool(nvl, otherkey2, otherval2);
898 
899 	testkey = "strkey";
900 	testval = "1654";
901 	nvlist_add_string(nvl, testkey, testval);
902 
903 	ATF_REQUIRE_EQ(strcmp(nvlist_take_string(nvl, testkey), testval), 0);
904 
905 	ATF_REQUIRE(nvlist_exists_string(nvl, otherkey1));
906 	ATF_REQUIRE_EQ(strcmp(nvlist_get_string(nvl, otherkey1), otherval1), 0);
907 
908 	ATF_REQUIRE(nvlist_exists_bool(nvl, otherkey2));
909 	ATF_REQUIRE_EQ(nvlist_get_bool(nvl, otherkey2), otherval2);
910 
911 	nvlist_destroy(nvl);
912 }
913 
914 ATF_TEST_CASE_WITHOUT_HEAD(nvlist_take_nvlist__single_remove);
915 ATF_TEST_CASE_BODY(nvlist_take_nvlist__single_remove)
916 {
917 	nvlist_t *nvl;
918 	const char *testkey;
919 	nvlist_t *testval;
920 
921 	nvl = nvlist_create(0);
922 	ATF_REQUIRE(nvl != NULL);
923 
924 	testkey = "numkey";
925 	testval = create_test_nvlist();
926 	nvlist_move_nvlist(nvl, testkey, testval);
927 
928 	verify_test_nvlist(nvlist_take_nvlist(nvl, testkey));
929 	ATF_REQUIRE(nvlist_empty(nvl));
930 
931 	nvlist_destroy(nvl);
932 }
933 
934 ATF_TEST_CASE_WITHOUT_HEAD(nvlist_take_nvlist__other_keys_unchanged);
935 ATF_TEST_CASE_BODY(nvlist_take_nvlist__other_keys_unchanged)
936 {
937 	nvlist_t *nvl;
938 	const char *testkey, *otherkey1, *otherkey2;
939 	nvlist_t *testval, *otherval1;
940 
941 	nvl = nvlist_create(0);
942 	ATF_REQUIRE(nvl != NULL);
943 
944 	testkey = "strkey";
945 	testval = create_test_nvlist();
946 	nvlist_move_nvlist(nvl, testkey, testval);
947 
948 	otherkey1 = "key1";
949 	otherval1 = nvlist_create(0);
950 	nvlist_move_nvlist(nvl, otherkey1, otherval1);
951 
952 	otherkey2 = "key2";
953 	nvlist_add_null(nvl, otherkey2);
954 
955 	verify_test_nvlist(nvlist_take_nvlist(nvl, testkey));
956 
957 	ATF_REQUIRE(nvlist_exists_nvlist(nvl, otherkey1));
958 	ATF_REQUIRE(nvlist_empty(nvlist_get_nvlist(nvl, otherkey1)));
959 
960 	ATF_REQUIRE(nvlist_exists_null(nvl, otherkey2));
961 
962 	nvlist_destroy(nvl);
963 }
964 
965 ATF_TEST_CASE_WITHOUT_HEAD(nvlist_take_binary__single_remove);
966 ATF_TEST_CASE_BODY(nvlist_take_binary__single_remove)
967 {
968 	nvlist_t *nvl;
969 	const char *testkey;
970 	void *testval;
971 	const void *actual_val;
972 	size_t testsize, actual_size;
973 
974 	nvl = nvlist_create(0);
975 	ATF_REQUIRE(nvl != NULL);
976 
977 	testkey = "numkey";
978 	testsize = 457;
979 	testval = malloc(testsize);
980 	memset(testval, '5', testsize);
981 	nvlist_move_binary(nvl, testkey, testval, testsize);
982 
983 	actual_val = nvlist_take_binary(nvl, testkey, &actual_size);
984 	ATF_REQUIRE_EQ(testsize, actual_size);
985 	ATF_REQUIRE_EQ(memcmp(actual_val, testval, testsize), 0);
986 	ATF_REQUIRE(nvlist_empty(nvl));
987 
988 	nvlist_destroy(nvl);
989 }
990 
991 ATF_TEST_CASE_WITHOUT_HEAD(nvlist_take_binary__other_keys_unchanged);
992 ATF_TEST_CASE_BODY(nvlist_take_binary__other_keys_unchanged)
993 {
994 	nvlist_t *nvl;
995 	const char *testkey, *otherkey1, *otherkey2;
996 	const void *actual_value;
997 	char testval[] = "gjiertj";
998 	char otherval1[] = "fdreg";
999 	size_t testsize, othersize, actual_size;
1000 	bool otherval2;
1001 
1002 	nvl = nvlist_create(0);
1003 	ATF_REQUIRE(nvl != NULL);
1004 
1005 	otherkey1 = "key1";
1006 	othersize = sizeof(otherval1);
1007 	nvlist_add_binary(nvl, otherkey1, otherval1, othersize);
1008 
1009 	otherkey2 = "key2";
1010 	otherval2 = true;
1011 	nvlist_add_bool(nvl, otherkey2, otherval2);
1012 
1013 	testkey = "strkey";
1014 	testsize = sizeof(testval);
1015 	nvlist_add_binary(nvl, testkey, testval, testsize);
1016 
1017 	actual_value = nvlist_take_binary(nvl, testkey, &actual_size);
1018 	ATF_REQUIRE_EQ(testsize, actual_size);
1019 	ATF_REQUIRE_EQ(memcmp(actual_value, testval, testsize), 0);
1020 
1021 	ATF_REQUIRE(nvlist_exists_binary(nvl, otherkey1));
1022 	actual_value = nvlist_get_binary(nvl, otherkey1, &actual_size);
1023 	ATF_REQUIRE_EQ(othersize, actual_size);
1024 	ATF_REQUIRE_EQ(memcmp(actual_value, otherval1, othersize), 0);
1025 
1026 	ATF_REQUIRE(nvlist_exists_bool(nvl, otherkey2));
1027 	ATF_REQUIRE_EQ(nvlist_get_bool(nvl, otherkey2), otherval2);
1028 
1029 	nvlist_destroy(nvl);
1030 }
1031 
1032 ATF_TEST_CASE_WITHOUT_HEAD(nvlist_free__single_null);
1033 ATF_TEST_CASE_BODY(nvlist_free__single_null)
1034 {
1035 	nvlist_t *nvl;
1036 	const char *key;
1037 
1038 	nvl = nvlist_create(0);
1039 	key = "test";
1040 	nvlist_add_null(nvl, key);
1041 
1042 	nvlist_free(nvl, key);
1043 	ATF_REQUIRE(nvlist_empty(nvl));
1044 
1045 	nvlist_destroy(nvl);
1046 }
1047 
1048 ATF_TEST_CASE_WITHOUT_HEAD(nvlist_free__single_bool);
1049 ATF_TEST_CASE_BODY(nvlist_free__single_bool)
1050 {
1051 	nvlist_t *nvl;
1052 	const char *key;
1053 
1054 	nvl = nvlist_create(0);
1055 	key = "test";
1056 	nvlist_add_bool(nvl, key, true);
1057 
1058 	nvlist_free(nvl, key);
1059 	ATF_REQUIRE(nvlist_empty(nvl));
1060 
1061 	nvlist_destroy(nvl);
1062 }
1063 
1064 ATF_TEST_CASE_WITHOUT_HEAD(nvlist_free__single_number);
1065 ATF_TEST_CASE_BODY(nvlist_free__single_number)
1066 {
1067 	nvlist_t *nvl;
1068 	const char *key;
1069 
1070 	nvl = nvlist_create(0);
1071 	key = "test";
1072 	nvlist_add_number(nvl, key, 584);
1073 
1074 	nvlist_free(nvl, key);
1075 	ATF_REQUIRE(nvlist_empty(nvl));
1076 
1077 	nvlist_destroy(nvl);
1078 }
1079 
1080 ATF_TEST_CASE_WITHOUT_HEAD(nvlist_free__single_string);
1081 ATF_TEST_CASE_BODY(nvlist_free__single_string)
1082 {
1083 	nvlist_t *nvl;
1084 	const char *key;
1085 
1086 	nvl = nvlist_create(0);
1087 	key = "test";
1088 	nvlist_add_string(nvl, key, "gjkfkjd");
1089 
1090 	nvlist_free(nvl, key);
1091 	ATF_REQUIRE(nvlist_empty(nvl));
1092 
1093 	nvlist_destroy(nvl);
1094 }
1095 
1096 ATF_TEST_CASE_WITHOUT_HEAD(nvlist_free__single_nvlist);
1097 ATF_TEST_CASE_BODY(nvlist_free__single_nvlist)
1098 {
1099 	nvlist_t *nvl;
1100 	const char *key;
1101 
1102 	nvl = nvlist_create(0);
1103 	key = "test";
1104 	nvlist_add_nvlist(nvl, key, nvlist_create(0));
1105 
1106 	nvlist_free(nvl, key);
1107 	ATF_REQUIRE(nvlist_empty(nvl));
1108 
1109 	nvlist_destroy(nvl);
1110 }
1111 
1112 ATF_TEST_CASE_WITHOUT_HEAD(nvlist_free__single_binary);
1113 ATF_TEST_CASE_BODY(nvlist_free__single_binary)
1114 {
1115 	nvlist_t *nvl;
1116 	const char *key;
1117 
1118 	nvl = nvlist_create(0);
1119 	key = "test";
1120 	nvlist_add_binary(nvl, key, "jgjgfd", 6);
1121 
1122 	nvlist_free(nvl, key);
1123 	ATF_REQUIRE(nvlist_empty(nvl));
1124 
1125 	nvlist_destroy(nvl);
1126 }
1127 
1128 ATF_TEST_CASE_WITHOUT_HEAD(nvlist_free_null__single_null);
1129 ATF_TEST_CASE_BODY(nvlist_free_null__single_null)
1130 {
1131 	nvlist_t *nvl;
1132 	const char *key;
1133 
1134 	nvl = nvlist_create(0);
1135 	key = "test";
1136 	nvlist_add_null(nvl, key);
1137 
1138 	nvlist_free_null(nvl, key);
1139 	ATF_REQUIRE(nvlist_empty(nvl));
1140 
1141 	nvlist_destroy(nvl);
1142 }
1143 
1144 ATF_TEST_CASE_WITHOUT_HEAD(nvlist_free_bool__single_bool);
1145 ATF_TEST_CASE_BODY(nvlist_free_bool__single_bool)
1146 {
1147 	nvlist_t *nvl;
1148 	const char *key;
1149 
1150 	nvl = nvlist_create(0);
1151 	key = "test";
1152 	nvlist_add_bool(nvl, key, true);
1153 
1154 	nvlist_free_bool(nvl, key);
1155 	ATF_REQUIRE(nvlist_empty(nvl));
1156 
1157 	nvlist_destroy(nvl);
1158 }
1159 
1160 ATF_TEST_CASE_WITHOUT_HEAD(nvlist_free_number__single_number);
1161 ATF_TEST_CASE_BODY(nvlist_free_number__single_number)
1162 {
1163 	nvlist_t *nvl;
1164 	const char *key;
1165 
1166 	nvl = nvlist_create(0);
1167 	key = "test";
1168 	nvlist_add_number(nvl, key, 584);
1169 
1170 	nvlist_free_number(nvl, key);
1171 	ATF_REQUIRE(nvlist_empty(nvl));
1172 
1173 	nvlist_destroy(nvl);
1174 }
1175 
1176 ATF_TEST_CASE_WITHOUT_HEAD(nvlist_free_string__single_string);
1177 ATF_TEST_CASE_BODY(nvlist_free_string__single_string)
1178 {
1179 	nvlist_t *nvl;
1180 	const char *key;
1181 
1182 	nvl = nvlist_create(0);
1183 	key = "test";
1184 	nvlist_add_string(nvl, key, "gjkfkjd");
1185 
1186 	nvlist_free_string(nvl, key);
1187 	ATF_REQUIRE(nvlist_empty(nvl));
1188 
1189 	nvlist_destroy(nvl);
1190 }
1191 
1192 ATF_TEST_CASE_WITHOUT_HEAD(nvlist_free_nvlist__single_nvlist);
1193 ATF_TEST_CASE_BODY(nvlist_free_nvlist__single_nvlist)
1194 {
1195 	nvlist_t *nvl;
1196 	const char *key;
1197 
1198 	nvl = nvlist_create(0);
1199 	key = "test";
1200 	nvlist_add_nvlist(nvl, key, nvlist_create(0));
1201 
1202 	nvlist_free_nvlist(nvl, key);
1203 	ATF_REQUIRE(nvlist_empty(nvl));
1204 
1205 	nvlist_destroy(nvl);
1206 }
1207 
1208 ATF_TEST_CASE_WITHOUT_HEAD(nvlist_free_binary__single_binary);
1209 ATF_TEST_CASE_BODY(nvlist_free_binary__single_binary)
1210 {
1211 	nvlist_t *nvl;
1212 	const char *key;
1213 
1214 	nvl = nvlist_create(0);
1215 	key = "test";
1216 	nvlist_add_binary(nvl, key, "jgjgfd", 6);
1217 
1218 	nvlist_free_binary(nvl, key);
1219 	ATF_REQUIRE(nvlist_empty(nvl));
1220 
1221 	nvlist_destroy(nvl);
1222 }
1223 
1224 ATF_INIT_TEST_CASES(tp)
1225 {
1226 	ATF_ADD_TEST_CASE(tp, nvlist_create__is_empty);
1227 	ATF_ADD_TEST_CASE(tp, nvlist_add_null__single_insert);
1228 	ATF_ADD_TEST_CASE(tp, nvlist_add_bool__single_insert);
1229 	ATF_ADD_TEST_CASE(tp, nvlist_add_number__single_insert);
1230 	ATF_ADD_TEST_CASE(tp, nvlist_add_string__single_insert);
1231 	ATF_ADD_TEST_CASE(tp, nvlist_add_nvlist__single_insert);
1232 	ATF_ADD_TEST_CASE(tp, nvlist_add_nvlist__child_with_error);
1233 	ATF_ADD_TEST_CASE(tp, nvlist_add_binary__single_insert);
1234 
1235 	ATF_ADD_TEST_CASE(tp, nvlist_clone__empty_nvlist);
1236 	ATF_ADD_TEST_CASE(tp, nvlist_clone__nonempty_nvlist);
1237 	ATF_ADD_TEST_CASE(tp, nvlist_clone__nested_nvlist);
1238 	ATF_ADD_TEST_CASE(tp, nvlist_clone__error_nvlist);
1239 
1240 	ATF_ADD_TEST_CASE(tp, nvlist_pack__empty_nvlist);
1241 	ATF_ADD_TEST_CASE(tp, nvlist_pack__multiple_values);
1242 	ATF_ADD_TEST_CASE(tp, nvlist_pack__error_nvlist);
1243 	ATF_ADD_TEST_CASE(tp, nvlist_unpack__duplicate_key);
1244 	ATF_ADD_TEST_CASE(tp, nvlist_unpack__flags_nvlist);
1245 
1246 	ATF_ADD_TEST_CASE(tp, nvlist_move_string__single_insert);
1247 	ATF_ADD_TEST_CASE(tp, nvlist_move_nvlist__single_insert);
1248 	ATF_ADD_TEST_CASE(tp, nvlist_move_nvlist__null_child);
1249 	ATF_ADD_TEST_CASE(tp, nvlist_move_nvlist__child_with_error);
1250 	ATF_ADD_TEST_CASE(tp, nvlist_move_binary__single_insert);
1251 
1252 	ATF_ADD_TEST_CASE(tp, nvlist_take_bool__single_remove);
1253 	ATF_ADD_TEST_CASE(tp, nvlist_take_bool__other_keys_unchanged);
1254 	ATF_ADD_TEST_CASE(tp, nvlist_take_number__single_remove);
1255 	ATF_ADD_TEST_CASE(tp, nvlist_take_number__other_keys_unchanged);
1256 	ATF_ADD_TEST_CASE(tp, nvlist_take_string__single_remove);
1257 	ATF_ADD_TEST_CASE(tp, nvlist_take_string__other_keys_unchanged);
1258 	ATF_ADD_TEST_CASE(tp, nvlist_take_nvlist__single_remove);
1259 	ATF_ADD_TEST_CASE(tp, nvlist_take_nvlist__other_keys_unchanged);
1260 	ATF_ADD_TEST_CASE(tp, nvlist_take_binary__single_remove);
1261 	ATF_ADD_TEST_CASE(tp, nvlist_take_binary__other_keys_unchanged);
1262 
1263 	ATF_ADD_TEST_CASE(tp, nvlist_free__single_null);
1264 	ATF_ADD_TEST_CASE(tp, nvlist_free__single_bool);
1265 	ATF_ADD_TEST_CASE(tp, nvlist_free__single_number);
1266 	ATF_ADD_TEST_CASE(tp, nvlist_free__single_string);
1267 	ATF_ADD_TEST_CASE(tp, nvlist_free__single_nvlist);
1268 	ATF_ADD_TEST_CASE(tp, nvlist_free__single_binary);
1269 
1270 	ATF_ADD_TEST_CASE(tp, nvlist_free_null__single_null);
1271 	ATF_ADD_TEST_CASE(tp, nvlist_free_bool__single_bool);
1272 	ATF_ADD_TEST_CASE(tp, nvlist_free_number__single_number);
1273 	ATF_ADD_TEST_CASE(tp, nvlist_free_string__single_string);
1274 	ATF_ADD_TEST_CASE(tp, nvlist_free_nvlist__single_nvlist);
1275 	ATF_ADD_TEST_CASE(tp, nvlist_free_binary__single_binary);
1276 }
1277