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