2 * Copyright (c) 2014-2015 Sandvine Inc. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
32 #include <atf-c++.hpp>
41 * Test that a newly created nvlist has no errors, and is empty.
43 ATF_TEST_CASE_WITHOUT_HEAD(nvlist_create__is_empty);
44 ATF_TEST_CASE_BODY(nvlist_create__is_empty)
50 nvl = nvlist_create(0);
52 ATF_REQUIRE(nvl != NULL);
54 ATF_REQUIRE_EQ(nvlist_error(nvl), 0);
55 ATF_REQUIRE(nvlist_empty(nvl));
58 ATF_REQUIRE_EQ(nvlist_next(nvl, &type, &it), static_cast<const char *>(NULL));
63 ATF_TEST_CASE_WITHOUT_HEAD(nvlist_add_null__single_insert);
64 ATF_TEST_CASE_BODY(nvlist_add_null__single_insert)
72 nvl = nvlist_create(0);
74 ATF_REQUIRE(nvl != NULL);
75 ATF_REQUIRE(!nvlist_exists(nvl, key));
77 nvlist_add_null(nvl, key);
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"));
84 /* Iterate over the nvlist; ensure that it has only our one key. */
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));
93 ATF_TEST_CASE_WITHOUT_HEAD(nvlist_add_bool__single_insert);
94 ATF_TEST_CASE_BODY(nvlist_add_bool__single_insert)
102 nvl = nvlist_create(0);
104 ATF_REQUIRE(nvl != NULL);
105 ATF_REQUIRE(!nvlist_exists(nvl, key));
107 nvlist_add_bool(nvl, key, true);
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);
116 /* Iterate over the nvlist; ensure that it has only our one key. */
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));
125 ATF_TEST_CASE_WITHOUT_HEAD(nvlist_add_number__single_insert);
126 ATF_TEST_CASE_BODY(nvlist_add_number__single_insert)
136 nvl = nvlist_create(0);
138 ATF_REQUIRE(nvl != NULL);
139 ATF_REQUIRE(!nvlist_exists(nvl, key));
141 nvlist_add_number(nvl, key, value);
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);
149 /* Iterate over the nvlist; ensure that it has only our one key. */
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));
158 ATF_TEST_CASE_WITHOUT_HEAD(nvlist_add_string__single_insert);
159 ATF_TEST_CASE_BODY(nvlist_add_string__single_insert)
169 nvl = nvlist_create(0);
171 ATF_REQUIRE(nvl != NULL);
172 ATF_REQUIRE(!nvlist_exists(nvl, key));
174 nvlist_add_string(nvl, key, value);
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);
183 /* nvlist_add_* is required to clone the value, so check for that. */
184 ATF_REQUIRE(nvlist_get_string(nvl, key) != value);
186 /* Iterate over the nvlist; ensure that it has only our one key. */
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));
195 ATF_TEST_CASE_WITHOUT_HEAD(nvlist_add_nvlist__single_insert);
196 ATF_TEST_CASE_BODY(nvlist_add_nvlist__single_insert)
200 const char *key, *subkey;
202 const nvlist_t *value;
207 sublist = nvlist_create(0);
208 nvl = nvlist_create(0);
210 ATF_REQUIRE(nvl != NULL);
211 ATF_REQUIRE(!nvlist_exists(nvl, key));
213 nvlist_add_null(sublist, subkey);
214 nvlist_add_nvlist(nvl, key, sublist);
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"));
222 value = nvlist_get_nvlist(nvl, key);
223 ATF_REQUIRE(nvlist_exists_null(value, subkey));
225 /* nvlist_add_* is required to clone the value, so check for that. */
226 ATF_REQUIRE(sublist != value);
228 /* Iterate over the nvlist; ensure that it has only our one key. */
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));
234 nvlist_destroy(sublist);
238 ATF_TEST_CASE_WITHOUT_HEAD(nvlist_add_nvlist__child_with_error);
239 ATF_TEST_CASE_BODY(nvlist_add_nvlist__child_with_error)
241 nvlist_t *nvl, *parent;
243 nvl = nvlist_create(0);
244 parent = nvlist_create(0);
246 nvlist_set_error(nvl, EBADF);
247 nvlist_add_nvlist(parent, "test", nvl);
248 ATF_REQUIRE_EQ(nvlist_error(parent), EBADF);
251 nvlist_destroy(parent);
254 ATF_TEST_CASE_WITHOUT_HEAD(nvlist_add_binary__single_insert);
255 ATF_TEST_CASE_BODY(nvlist_add_binary__single_insert)
261 const void *ret_value;
262 size_t value_size, ret_size;
267 value = malloc(value_size);
268 memset(value, 0xa5, value_size);
269 nvl = nvlist_create(0);
271 ATF_REQUIRE(nvl != NULL);
272 ATF_REQUIRE(!nvlist_exists(nvl, key));
274 nvlist_add_binary(nvl, key, value, value_size);
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"));
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);
286 /* nvlist_add_* is required to clone the value, so check for that. */
287 ATF_REQUIRE(value != ret_value);
289 /* Iterate over the nvlist; ensure that it has only our one key. */
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));
299 ATF_TEST_CASE_WITHOUT_HEAD(nvlist_clone__empty_nvlist);
300 ATF_TEST_CASE_BODY(nvlist_clone__empty_nvlist)
302 nvlist_t *nvl, *clone;
304 nvl = nvlist_create(0);
305 ATF_REQUIRE(nvl != NULL);
307 clone = nvlist_clone(nvl);
308 ATF_REQUIRE(clone != NULL);
309 ATF_REQUIRE(clone != nvl);
310 ATF_REQUIRE(nvlist_empty(clone));
312 nvlist_destroy(clone);
316 ATF_TEST_CASE_WITHOUT_HEAD(nvlist_clone__nonempty_nvlist);
317 ATF_TEST_CASE_BODY(nvlist_clone__nonempty_nvlist)
319 nvlist_t *nvl, *clone;
325 nvl = nvlist_create(0);
326 ATF_REQUIRE(nvl != NULL);
330 nvlist_add_number(nvl, key, value);
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);
338 /* Iterate over the nvlist; ensure that it has only our one key. */
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));
344 nvlist_destroy(clone);
348 static const char * const test_subnvlist_key = "nvlist";
350 static const char * const test_string_key = "string";
351 static const char * const test_string_val = "59525";
354 create_test_nvlist(void)
356 nvlist_t *nvl, *sublist;
358 nvl = nvlist_create(0);
359 ATF_REQUIRE(nvl != NULL);
361 sublist = nvlist_create(0);
362 ATF_REQUIRE(sublist != NULL);
364 nvlist_add_string(sublist, test_string_key, test_string_val);
365 nvlist_move_nvlist(nvl, test_subnvlist_key, sublist);
371 verify_test_nvlist(const nvlist_t *nvl)
374 const nvlist_t *value;
377 ATF_REQUIRE(nvlist_exists_nvlist(nvl, test_subnvlist_key));
379 value = nvlist_get_nvlist(nvl, test_subnvlist_key);
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);
385 /* Iterate over both nvlists; ensure that each has only the one key. */
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));
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));
399 ATF_TEST_CASE_WITHOUT_HEAD(nvlist_clone__nested_nvlist);
400 ATF_TEST_CASE_BODY(nvlist_clone__nested_nvlist)
402 nvlist_t *nvl, *clone;
404 nvl = create_test_nvlist();
405 clone = nvlist_clone(nvl);
407 ATF_REQUIRE(clone != NULL);
408 ATF_REQUIRE(clone != nvl);
409 verify_test_nvlist(clone);
411 nvlist_destroy(clone);
415 ATF_TEST_CASE_WITHOUT_HEAD(nvlist_clone__error_nvlist);
416 ATF_TEST_CASE_BODY(nvlist_clone__error_nvlist)
418 nvlist_t *nvl, *clone;
420 nvl = nvlist_create(0);
421 ATF_REQUIRE(nvl != NULL);
423 nvlist_set_error(nvl, ENOMEM);
425 clone = nvlist_clone(nvl);
426 ATF_REQUIRE(clone == NULL);
431 ATF_TEST_CASE_WITHOUT_HEAD(nvlist_pack__empty_nvlist);
432 ATF_TEST_CASE_BODY(nvlist_pack__empty_nvlist)
434 nvlist_t *nvl, *unpacked;
438 nvl = nvlist_create(0);
439 ATF_REQUIRE(nvl != NULL);
441 packed = nvlist_pack(nvl, &packed_size);
442 ATF_REQUIRE(packed != NULL);
444 unpacked = nvlist_unpack(packed, packed_size, 0);
445 ATF_REQUIRE(unpacked != NULL);
446 ATF_REQUIRE(unpacked != nvl);
447 ATF_REQUIRE(nvlist_empty(unpacked));
449 nvlist_destroy(unpacked);
454 ATF_TEST_CASE_WITHOUT_HEAD(nvlist_unpack__flags_nvlist);
455 ATF_TEST_CASE_BODY(nvlist_unpack__flags_nvlist)
457 nvlist_t *nvl, *unpacked;
461 nvl = nvlist_create(NV_FLAG_NO_UNIQUE);
462 ATF_REQUIRE(nvl != NULL);
464 nvlist_add_bool(nvl, "name", true);
465 ATF_REQUIRE(!nvlist_empty(nvl));
466 ATF_REQUIRE(nvlist_exists_bool(nvl, "name"));
468 packed = nvlist_pack(nvl, &packed_size);
469 ATF_REQUIRE(packed != NULL);
471 unpacked = nvlist_unpack(packed, packed_size, 0);
472 ATF_REQUIRE(unpacked == NULL);
474 unpacked = nvlist_unpack(packed, packed_size, NV_FLAG_IGNORE_CASE);
475 ATF_REQUIRE(unpacked == NULL);
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"));
483 nvlist_destroy(unpacked);
489 verify_null(const nvlist_t *nvl, int type)
492 ATF_REQUIRE_EQ(type, NV_TYPE_NULL);
496 verify_number(const nvlist_t *nvl, const char *name, int type, uint64_t value)
499 ATF_REQUIRE_EQ(type, NV_TYPE_NUMBER);
500 ATF_REQUIRE_EQ(nvlist_get_number(nvl, name), value);
504 verify_string(const nvlist_t *nvl, const char *name, int type,
508 ATF_REQUIRE_EQ(type, NV_TYPE_STRING);
509 ATF_REQUIRE_EQ(strcmp(nvlist_get_string(nvl, name), value), 0);
513 verify_nvlist(const nvlist_t *nvl, const char *name, int type)
516 ATF_REQUIRE_EQ(type, NV_TYPE_NVLIST);
517 verify_test_nvlist(nvlist_get_nvlist(nvl, name));
521 verify_binary(const nvlist_t *nvl, const char *name, int type,
522 const void * value, size_t size)
524 const void *actual_value;
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);
533 ATF_TEST_CASE_WITHOUT_HEAD(nvlist_pack__multiple_values);
534 ATF_TEST_CASE_BODY(nvlist_pack__multiple_values)
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;
541 const char * strvalue;
542 void *binvalue, *packed, *it;
543 size_t binsize, packed_size;
546 nvl = nvlist_create(0);
549 nvlist_add_null(nvl, nullkey);
552 numvalue = 939853984;
553 nvlist_add_number(nvl, numkey, numvalue);
556 strvalue = "jfieutijf";
557 nvlist_add_string(nvl, strkey, strvalue);
560 nvvalue = create_test_nvlist();
561 nvlist_move_nvlist(nvl, nvkey, nvvalue);
565 binvalue = malloc(binsize);
566 memset(binvalue, 'b', binsize);
567 nvlist_move_binary(nvl, binkey, binvalue, binsize);
569 packed = nvlist_pack(nvl, &packed_size);
570 ATF_REQUIRE(packed != NULL);
572 unpacked = nvlist_unpack(packed, packed_size, 0);
573 ATF_REQUIRE(unpacked != 0);
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);
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);
591 msg << "Unexpected key :'" << name << "'";
592 ATF_FAIL(msg.str().c_str());
595 keys_seen.insert(name);
598 /* Ensure that we saw every key. */
599 ATF_REQUIRE_EQ(keys_seen.size(), 5);
602 nvlist_destroy(unpacked);
606 ATF_TEST_CASE_WITHOUT_HEAD(nvlist_pack__error_nvlist);
607 ATF_TEST_CASE_BODY(nvlist_pack__error_nvlist)
613 nvl = nvlist_create(0);
614 ATF_REQUIRE(nvl != NULL);
616 nvlist_set_error(nvl, ENOMEM);
618 packed = nvlist_pack(nvl, &size);
619 ATF_REQUIRE(packed == NULL);
624 ATF_TEST_CASE_WITHOUT_HEAD(nvlist_unpack__duplicate_key);
625 ATF_TEST_CASE_BODY(nvlist_unpack__duplicate_key)
627 nvlist_t *nvl, *unpacked;
628 const char *key1, *key2;
629 void *packed, *keypos;
632 nvl = nvlist_create(0);
635 keylen = strlen(key1);
636 nvlist_add_number(nvl, key1, 5);
639 ATF_REQUIRE_EQ(keylen, strlen(key2));
640 nvlist_add_number(nvl, key2, 10);
642 packed = nvlist_pack(nvl, &size);
645 * Mangle the packed nvlist by replacing key1 with key2, creating a
646 * packed nvlist with a duplicate key.
648 keypos = memmem(packed, size, key1, keylen);
649 ATF_REQUIRE(keypos != NULL);
650 memcpy(keypos, key2, keylen);
652 unpacked = nvlist_unpack(packed, size, 0);
653 ATF_REQUIRE(nvlist_error(unpacked) != 0);
657 nvlist_destroy(unpacked);
660 ATF_TEST_CASE_WITHOUT_HEAD(nvlist_move_string__single_insert);
661 ATF_TEST_CASE_BODY(nvlist_move_string__single_insert)
667 nvl = nvlist_create(0);
668 ATF_REQUIRE(nvl != NULL);
671 value = strdup("testval");
672 ATF_REQUIRE(value != NULL);
674 nvlist_move_string(nvl, key, value);
675 ATF_REQUIRE_EQ(nvlist_get_string(nvl, key), value);
680 ATF_TEST_CASE_WITHOUT_HEAD(nvlist_move_nvlist__null_child);
681 ATF_TEST_CASE_BODY(nvlist_move_nvlist__null_child)
685 parent = nvlist_create(0);
687 nvlist_move_nvlist(parent, "test", NULL);
689 ATF_REQUIRE(nvlist_error(parent) != 0);
691 nvlist_destroy(parent);
694 ATF_TEST_CASE_WITHOUT_HEAD(nvlist_move_nvlist__child_with_error);
695 ATF_TEST_CASE_BODY(nvlist_move_nvlist__child_with_error)
697 nvlist_t *nvl, *parent;
699 nvl = nvlist_create(0);
700 parent = nvlist_create(0);
702 nvlist_set_error(nvl, EBADF);
703 nvlist_move_nvlist(parent, "test", nvl);
704 ATF_REQUIRE_EQ(nvlist_error(parent), EBADF);
706 nvlist_destroy(parent);
709 ATF_TEST_CASE_WITHOUT_HEAD(nvlist_move_nvlist__single_insert);
710 ATF_TEST_CASE_BODY(nvlist_move_nvlist__single_insert)
716 nvl = nvlist_create(0);
717 ATF_REQUIRE(nvl != NULL);
720 value = nvlist_create(0);
721 ATF_REQUIRE(value != NULL);
723 nvlist_move_nvlist(nvl, key, value);
724 ATF_REQUIRE_EQ(nvlist_get_nvlist(nvl, key), value);
729 ATF_TEST_CASE_WITHOUT_HEAD(nvlist_move_binary__single_insert);
730 ATF_TEST_CASE_BODY(nvlist_move_binary__single_insert)
735 size_t size, actual_size;
737 nvl = nvlist_create(0);
738 ATF_REQUIRE(nvl != NULL);
742 value = malloc(size);
743 ATF_REQUIRE(value != NULL);
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);
752 ATF_TEST_CASE_WITHOUT_HEAD(nvlist_take_bool__single_remove);
753 ATF_TEST_CASE_BODY(nvlist_take_bool__single_remove)
759 nvl = nvlist_create(0);
760 ATF_REQUIRE(nvl != NULL);
764 nvlist_add_bool(nvl, testkey, testval);
766 ATF_REQUIRE_EQ(nvlist_take_bool(nvl, testkey), testval);
767 ATF_REQUIRE(nvlist_empty(nvl));
772 ATF_TEST_CASE_WITHOUT_HEAD(nvlist_take_bool__other_keys_unchanged);
773 ATF_TEST_CASE_BODY(nvlist_take_bool__other_keys_unchanged)
776 const char *testkey, *otherkey1, *otherkey2;
777 bool testval, otherval1;
780 nvl = nvlist_create(0);
781 ATF_REQUIRE(nvl != NULL);
785 nvlist_add_bool(nvl, testkey, testval);
789 nvlist_add_bool(nvl, otherkey1, otherval1);
792 otherval2 = create_test_nvlist();
793 nvlist_move_nvlist(nvl, otherkey2, otherval2);
795 ATF_REQUIRE_EQ(nvlist_take_bool(nvl, testkey), testval);
797 ATF_REQUIRE(nvlist_exists_bool(nvl, otherkey1));
798 ATF_REQUIRE_EQ(nvlist_get_bool(nvl, otherkey1), otherval1);
800 ATF_REQUIRE(nvlist_exists_nvlist(nvl, otherkey2));
801 verify_test_nvlist(nvlist_get_nvlist(nvl, otherkey2));
806 ATF_TEST_CASE_WITHOUT_HEAD(nvlist_take_number__single_remove);
807 ATF_TEST_CASE_BODY(nvlist_take_number__single_remove)
813 nvl = nvlist_create(0);
814 ATF_REQUIRE(nvl != NULL);
817 testval = std::numeric_limits<uint64_t>::max();
818 nvlist_add_number(nvl, testkey, testval);
820 ATF_REQUIRE_EQ(nvlist_take_number(nvl, testkey), testval);
821 ATF_REQUIRE(nvlist_empty(nvl));
826 ATF_TEST_CASE_WITHOUT_HEAD(nvlist_take_number__other_keys_unchanged);
827 ATF_TEST_CASE_BODY(nvlist_take_number__other_keys_unchanged)
830 const char *testkey, *otherkey1, *otherkey2;
831 uint64_t testval, otherval1;
832 const char *otherval2;
834 nvl = nvlist_create(0);
835 ATF_REQUIRE(nvl != NULL);
839 nvlist_add_number(nvl, otherkey1, otherval1);
843 nvlist_add_number(nvl, testkey, testval);
846 otherval2 = "string";
847 nvlist_add_string(nvl, otherkey2, otherval2);
849 ATF_REQUIRE_EQ(nvlist_take_number(nvl, testkey), testval);
851 ATF_REQUIRE(nvlist_exists_number(nvl, otherkey1));
852 ATF_REQUIRE_EQ(nvlist_get_number(nvl, otherkey1), otherval1);
854 ATF_REQUIRE(nvlist_exists_string(nvl, otherkey2));
855 ATF_REQUIRE_EQ(strcmp(nvlist_get_string(nvl, otherkey2), otherval2), 0);
860 ATF_TEST_CASE_WITHOUT_HEAD(nvlist_take_string__single_remove);
861 ATF_TEST_CASE_BODY(nvlist_take_string__single_remove)
867 nvl = nvlist_create(0);
868 ATF_REQUIRE(nvl != NULL);
872 nvlist_add_string(nvl, testkey, testval);
874 ATF_REQUIRE_EQ(strcmp(nvlist_take_string(nvl, testkey), testval), 0);
875 ATF_REQUIRE(nvlist_empty(nvl));
880 ATF_TEST_CASE_WITHOUT_HEAD(nvlist_take_string__other_keys_unchanged);
881 ATF_TEST_CASE_BODY(nvlist_take_string__other_keys_unchanged)
884 const char *testkey, *otherkey1, *otherkey2;
885 const char *testval, *otherval1;
888 nvl = nvlist_create(0);
889 ATF_REQUIRE(nvl != NULL);
892 otherval1 = "fjdifjdk";
893 nvlist_add_string(nvl, otherkey1, otherval1);
897 nvlist_add_bool(nvl, otherkey2, otherval2);
901 nvlist_add_string(nvl, testkey, testval);
903 ATF_REQUIRE_EQ(strcmp(nvlist_take_string(nvl, testkey), testval), 0);
905 ATF_REQUIRE(nvlist_exists_string(nvl, otherkey1));
906 ATF_REQUIRE_EQ(strcmp(nvlist_get_string(nvl, otherkey1), otherval1), 0);
908 ATF_REQUIRE(nvlist_exists_bool(nvl, otherkey2));
909 ATF_REQUIRE_EQ(nvlist_get_bool(nvl, otherkey2), otherval2);
914 ATF_TEST_CASE_WITHOUT_HEAD(nvlist_take_nvlist__single_remove);
915 ATF_TEST_CASE_BODY(nvlist_take_nvlist__single_remove)
921 nvl = nvlist_create(0);
922 ATF_REQUIRE(nvl != NULL);
925 testval = create_test_nvlist();
926 nvlist_move_nvlist(nvl, testkey, testval);
928 verify_test_nvlist(nvlist_take_nvlist(nvl, testkey));
929 ATF_REQUIRE(nvlist_empty(nvl));
934 ATF_TEST_CASE_WITHOUT_HEAD(nvlist_take_nvlist__other_keys_unchanged);
935 ATF_TEST_CASE_BODY(nvlist_take_nvlist__other_keys_unchanged)
938 const char *testkey, *otherkey1, *otherkey2;
939 nvlist_t *testval, *otherval1;
941 nvl = nvlist_create(0);
942 ATF_REQUIRE(nvl != NULL);
945 testval = create_test_nvlist();
946 nvlist_move_nvlist(nvl, testkey, testval);
949 otherval1 = nvlist_create(0);
950 nvlist_move_nvlist(nvl, otherkey1, otherval1);
953 nvlist_add_null(nvl, otherkey2);
955 verify_test_nvlist(nvlist_take_nvlist(nvl, testkey));
957 ATF_REQUIRE(nvlist_exists_nvlist(nvl, otherkey1));
958 ATF_REQUIRE(nvlist_empty(nvlist_get_nvlist(nvl, otherkey1)));
960 ATF_REQUIRE(nvlist_exists_null(nvl, otherkey2));
965 ATF_TEST_CASE_WITHOUT_HEAD(nvlist_take_binary__single_remove);
966 ATF_TEST_CASE_BODY(nvlist_take_binary__single_remove)
971 const void *actual_val;
972 size_t testsize, actual_size;
974 nvl = nvlist_create(0);
975 ATF_REQUIRE(nvl != NULL);
979 testval = malloc(testsize);
980 memset(testval, '5', testsize);
981 nvlist_move_binary(nvl, testkey, testval, testsize);
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));
991 ATF_TEST_CASE_WITHOUT_HEAD(nvlist_take_binary__other_keys_unchanged);
992 ATF_TEST_CASE_BODY(nvlist_take_binary__other_keys_unchanged)
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;
1002 nvl = nvlist_create(0);
1003 ATF_REQUIRE(nvl != NULL);
1006 othersize = sizeof(otherval1);
1007 nvlist_add_binary(nvl, otherkey1, otherval1, othersize);
1011 nvlist_add_bool(nvl, otherkey2, otherval2);
1014 testsize = sizeof(testval);
1015 nvlist_add_binary(nvl, testkey, testval, testsize);
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);
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);
1026 ATF_REQUIRE(nvlist_exists_bool(nvl, otherkey2));
1027 ATF_REQUIRE_EQ(nvlist_get_bool(nvl, otherkey2), otherval2);
1029 nvlist_destroy(nvl);
1032 ATF_TEST_CASE_WITHOUT_HEAD(nvlist_free__single_null);
1033 ATF_TEST_CASE_BODY(nvlist_free__single_null)
1038 nvl = nvlist_create(0);
1040 nvlist_add_null(nvl, key);
1042 nvlist_free(nvl, key);
1043 ATF_REQUIRE(nvlist_empty(nvl));
1045 nvlist_destroy(nvl);
1048 ATF_TEST_CASE_WITHOUT_HEAD(nvlist_free__single_bool);
1049 ATF_TEST_CASE_BODY(nvlist_free__single_bool)
1054 nvl = nvlist_create(0);
1056 nvlist_add_bool(nvl, key, true);
1058 nvlist_free(nvl, key);
1059 ATF_REQUIRE(nvlist_empty(nvl));
1061 nvlist_destroy(nvl);
1064 ATF_TEST_CASE_WITHOUT_HEAD(nvlist_free__single_number);
1065 ATF_TEST_CASE_BODY(nvlist_free__single_number)
1070 nvl = nvlist_create(0);
1072 nvlist_add_number(nvl, key, 584);
1074 nvlist_free(nvl, key);
1075 ATF_REQUIRE(nvlist_empty(nvl));
1077 nvlist_destroy(nvl);
1080 ATF_TEST_CASE_WITHOUT_HEAD(nvlist_free__single_string);
1081 ATF_TEST_CASE_BODY(nvlist_free__single_string)
1086 nvl = nvlist_create(0);
1088 nvlist_add_string(nvl, key, "gjkfkjd");
1090 nvlist_free(nvl, key);
1091 ATF_REQUIRE(nvlist_empty(nvl));
1093 nvlist_destroy(nvl);
1096 ATF_TEST_CASE_WITHOUT_HEAD(nvlist_free__single_nvlist);
1097 ATF_TEST_CASE_BODY(nvlist_free__single_nvlist)
1102 nvl = nvlist_create(0);
1104 nvlist_add_nvlist(nvl, key, nvlist_create(0));
1106 nvlist_free(nvl, key);
1107 ATF_REQUIRE(nvlist_empty(nvl));
1109 nvlist_destroy(nvl);
1112 ATF_TEST_CASE_WITHOUT_HEAD(nvlist_free__single_binary);
1113 ATF_TEST_CASE_BODY(nvlist_free__single_binary)
1118 nvl = nvlist_create(0);
1120 nvlist_add_binary(nvl, key, "jgjgfd", 6);
1122 nvlist_free(nvl, key);
1123 ATF_REQUIRE(nvlist_empty(nvl));
1125 nvlist_destroy(nvl);
1128 ATF_TEST_CASE_WITHOUT_HEAD(nvlist_free_null__single_null);
1129 ATF_TEST_CASE_BODY(nvlist_free_null__single_null)
1134 nvl = nvlist_create(0);
1136 nvlist_add_null(nvl, key);
1138 nvlist_free_null(nvl, key);
1139 ATF_REQUIRE(nvlist_empty(nvl));
1141 nvlist_destroy(nvl);
1144 ATF_TEST_CASE_WITHOUT_HEAD(nvlist_free_bool__single_bool);
1145 ATF_TEST_CASE_BODY(nvlist_free_bool__single_bool)
1150 nvl = nvlist_create(0);
1152 nvlist_add_bool(nvl, key, true);
1154 nvlist_free_bool(nvl, key);
1155 ATF_REQUIRE(nvlist_empty(nvl));
1157 nvlist_destroy(nvl);
1160 ATF_TEST_CASE_WITHOUT_HEAD(nvlist_free_number__single_number);
1161 ATF_TEST_CASE_BODY(nvlist_free_number__single_number)
1166 nvl = nvlist_create(0);
1168 nvlist_add_number(nvl, key, 584);
1170 nvlist_free_number(nvl, key);
1171 ATF_REQUIRE(nvlist_empty(nvl));
1173 nvlist_destroy(nvl);
1176 ATF_TEST_CASE_WITHOUT_HEAD(nvlist_free_string__single_string);
1177 ATF_TEST_CASE_BODY(nvlist_free_string__single_string)
1182 nvl = nvlist_create(0);
1184 nvlist_add_string(nvl, key, "gjkfkjd");
1186 nvlist_free_string(nvl, key);
1187 ATF_REQUIRE(nvlist_empty(nvl));
1189 nvlist_destroy(nvl);
1192 ATF_TEST_CASE_WITHOUT_HEAD(nvlist_free_nvlist__single_nvlist);
1193 ATF_TEST_CASE_BODY(nvlist_free_nvlist__single_nvlist)
1198 nvl = nvlist_create(0);
1200 nvlist_add_nvlist(nvl, key, nvlist_create(0));
1202 nvlist_free_nvlist(nvl, key);
1203 ATF_REQUIRE(nvlist_empty(nvl));
1205 nvlist_destroy(nvl);
1208 ATF_TEST_CASE_WITHOUT_HEAD(nvlist_free_binary__single_binary);
1209 ATF_TEST_CASE_BODY(nvlist_free_binary__single_binary)
1214 nvl = nvlist_create(0);
1216 nvlist_add_binary(nvl, key, "jgjgfd", 6);
1218 nvlist_free_binary(nvl, key);
1219 ATF_REQUIRE(nvlist_empty(nvl));
1221 nvlist_destroy(nvl);
1224 ATF_INIT_TEST_CASES(tp)
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);
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);
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);
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);
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);
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);
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);