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);
643 ATF_REQUIRE(packed != NULL);
646 * Mangle the packed nvlist by replacing key1 with key2, creating a
647 * packed nvlist with a duplicate key.
649 keypos = memmem(packed, size, key1, keylen);
650 ATF_REQUIRE(keypos != NULL);
651 memcpy(keypos, key2, keylen);
653 unpacked = nvlist_unpack(packed, size, 0);
654 ATF_REQUIRE(nvlist_error(unpacked) != 0);
658 nvlist_destroy(unpacked);
661 ATF_TEST_CASE_WITHOUT_HEAD(nvlist_move_string__single_insert);
662 ATF_TEST_CASE_BODY(nvlist_move_string__single_insert)
668 nvl = nvlist_create(0);
669 ATF_REQUIRE(nvl != NULL);
672 value = strdup("testval");
673 ATF_REQUIRE(value != NULL);
675 nvlist_move_string(nvl, key, value);
676 ATF_REQUIRE_EQ(nvlist_get_string(nvl, key), value);
681 ATF_TEST_CASE_WITHOUT_HEAD(nvlist_move_nvlist__null_child);
682 ATF_TEST_CASE_BODY(nvlist_move_nvlist__null_child)
686 parent = nvlist_create(0);
688 nvlist_move_nvlist(parent, "test", NULL);
690 ATF_REQUIRE(nvlist_error(parent) != 0);
692 nvlist_destroy(parent);
695 ATF_TEST_CASE_WITHOUT_HEAD(nvlist_move_nvlist__child_with_error);
696 ATF_TEST_CASE_BODY(nvlist_move_nvlist__child_with_error)
698 nvlist_t *nvl, *parent;
700 nvl = nvlist_create(0);
701 parent = nvlist_create(0);
703 nvlist_set_error(nvl, EBADF);
704 nvlist_move_nvlist(parent, "test", nvl);
705 ATF_REQUIRE_EQ(nvlist_error(parent), EBADF);
707 nvlist_destroy(parent);
710 ATF_TEST_CASE_WITHOUT_HEAD(nvlist_move_nvlist__single_insert);
711 ATF_TEST_CASE_BODY(nvlist_move_nvlist__single_insert)
717 nvl = nvlist_create(0);
718 ATF_REQUIRE(nvl != NULL);
721 value = nvlist_create(0);
722 ATF_REQUIRE(value != NULL);
724 nvlist_move_nvlist(nvl, key, value);
725 ATF_REQUIRE_EQ(nvlist_get_nvlist(nvl, key), value);
730 ATF_TEST_CASE_WITHOUT_HEAD(nvlist_move_binary__single_insert);
731 ATF_TEST_CASE_BODY(nvlist_move_binary__single_insert)
736 size_t size, actual_size;
738 nvl = nvlist_create(0);
739 ATF_REQUIRE(nvl != NULL);
743 value = malloc(size);
744 ATF_REQUIRE(value != NULL);
746 nvlist_move_binary(nvl, key, value, size);
747 ATF_REQUIRE_EQ(nvlist_get_binary(nvl, key, &actual_size), value);
748 ATF_REQUIRE_EQ(size, actual_size);
753 ATF_TEST_CASE_WITHOUT_HEAD(nvlist_take_bool__single_remove);
754 ATF_TEST_CASE_BODY(nvlist_take_bool__single_remove)
760 nvl = nvlist_create(0);
761 ATF_REQUIRE(nvl != NULL);
765 nvlist_add_bool(nvl, testkey, testval);
767 ATF_REQUIRE_EQ(nvlist_take_bool(nvl, testkey), testval);
768 ATF_REQUIRE(nvlist_empty(nvl));
773 ATF_TEST_CASE_WITHOUT_HEAD(nvlist_take_bool__other_keys_unchanged);
774 ATF_TEST_CASE_BODY(nvlist_take_bool__other_keys_unchanged)
777 const char *testkey, *otherkey1, *otherkey2;
778 bool testval, otherval1;
781 nvl = nvlist_create(0);
782 ATF_REQUIRE(nvl != NULL);
786 nvlist_add_bool(nvl, testkey, testval);
790 nvlist_add_bool(nvl, otherkey1, otherval1);
793 otherval2 = create_test_nvlist();
794 nvlist_move_nvlist(nvl, otherkey2, otherval2);
796 ATF_REQUIRE_EQ(nvlist_take_bool(nvl, testkey), testval);
798 ATF_REQUIRE(nvlist_exists_bool(nvl, otherkey1));
799 ATF_REQUIRE_EQ(nvlist_get_bool(nvl, otherkey1), otherval1);
801 ATF_REQUIRE(nvlist_exists_nvlist(nvl, otherkey2));
802 verify_test_nvlist(nvlist_get_nvlist(nvl, otherkey2));
807 ATF_TEST_CASE_WITHOUT_HEAD(nvlist_take_number__single_remove);
808 ATF_TEST_CASE_BODY(nvlist_take_number__single_remove)
814 nvl = nvlist_create(0);
815 ATF_REQUIRE(nvl != NULL);
818 testval = std::numeric_limits<uint64_t>::max();
819 nvlist_add_number(nvl, testkey, testval);
821 ATF_REQUIRE_EQ(nvlist_take_number(nvl, testkey), testval);
822 ATF_REQUIRE(nvlist_empty(nvl));
827 ATF_TEST_CASE_WITHOUT_HEAD(nvlist_take_number__other_keys_unchanged);
828 ATF_TEST_CASE_BODY(nvlist_take_number__other_keys_unchanged)
831 const char *testkey, *otherkey1, *otherkey2;
832 uint64_t testval, otherval1;
833 const char *otherval2;
835 nvl = nvlist_create(0);
836 ATF_REQUIRE(nvl != NULL);
840 nvlist_add_number(nvl, otherkey1, otherval1);
844 nvlist_add_number(nvl, testkey, testval);
847 otherval2 = "string";
848 nvlist_add_string(nvl, otherkey2, otherval2);
850 ATF_REQUIRE_EQ(nvlist_take_number(nvl, testkey), testval);
852 ATF_REQUIRE(nvlist_exists_number(nvl, otherkey1));
853 ATF_REQUIRE_EQ(nvlist_get_number(nvl, otherkey1), otherval1);
855 ATF_REQUIRE(nvlist_exists_string(nvl, otherkey2));
856 ATF_REQUIRE_EQ(strcmp(nvlist_get_string(nvl, otherkey2), otherval2), 0);
861 ATF_TEST_CASE_WITHOUT_HEAD(nvlist_take_string__single_remove);
862 ATF_TEST_CASE_BODY(nvlist_take_string__single_remove)
868 nvl = nvlist_create(0);
869 ATF_REQUIRE(nvl != NULL);
873 nvlist_add_string(nvl, testkey, testval);
875 ATF_REQUIRE_EQ(strcmp(nvlist_take_string(nvl, testkey), testval), 0);
876 ATF_REQUIRE(nvlist_empty(nvl));
881 ATF_TEST_CASE_WITHOUT_HEAD(nvlist_take_string__other_keys_unchanged);
882 ATF_TEST_CASE_BODY(nvlist_take_string__other_keys_unchanged)
885 const char *testkey, *otherkey1, *otherkey2;
886 const char *testval, *otherval1;
889 nvl = nvlist_create(0);
890 ATF_REQUIRE(nvl != NULL);
893 otherval1 = "fjdifjdk";
894 nvlist_add_string(nvl, otherkey1, otherval1);
898 nvlist_add_bool(nvl, otherkey2, otherval2);
902 nvlist_add_string(nvl, testkey, testval);
904 ATF_REQUIRE_EQ(strcmp(nvlist_take_string(nvl, testkey), testval), 0);
906 ATF_REQUIRE(nvlist_exists_string(nvl, otherkey1));
907 ATF_REQUIRE_EQ(strcmp(nvlist_get_string(nvl, otherkey1), otherval1), 0);
909 ATF_REQUIRE(nvlist_exists_bool(nvl, otherkey2));
910 ATF_REQUIRE_EQ(nvlist_get_bool(nvl, otherkey2), otherval2);
915 ATF_TEST_CASE_WITHOUT_HEAD(nvlist_take_nvlist__single_remove);
916 ATF_TEST_CASE_BODY(nvlist_take_nvlist__single_remove)
922 nvl = nvlist_create(0);
923 ATF_REQUIRE(nvl != NULL);
926 testval = create_test_nvlist();
927 nvlist_move_nvlist(nvl, testkey, testval);
929 verify_test_nvlist(nvlist_take_nvlist(nvl, testkey));
930 ATF_REQUIRE(nvlist_empty(nvl));
935 ATF_TEST_CASE_WITHOUT_HEAD(nvlist_take_nvlist__other_keys_unchanged);
936 ATF_TEST_CASE_BODY(nvlist_take_nvlist__other_keys_unchanged)
939 const char *testkey, *otherkey1, *otherkey2;
940 nvlist_t *testval, *otherval1;
942 nvl = nvlist_create(0);
943 ATF_REQUIRE(nvl != NULL);
946 testval = create_test_nvlist();
947 nvlist_move_nvlist(nvl, testkey, testval);
950 otherval1 = nvlist_create(0);
951 nvlist_move_nvlist(nvl, otherkey1, otherval1);
954 nvlist_add_null(nvl, otherkey2);
956 verify_test_nvlist(nvlist_take_nvlist(nvl, testkey));
958 ATF_REQUIRE(nvlist_exists_nvlist(nvl, otherkey1));
959 ATF_REQUIRE(nvlist_empty(nvlist_get_nvlist(nvl, otherkey1)));
961 ATF_REQUIRE(nvlist_exists_null(nvl, otherkey2));
966 ATF_TEST_CASE_WITHOUT_HEAD(nvlist_take_binary__single_remove);
967 ATF_TEST_CASE_BODY(nvlist_take_binary__single_remove)
972 const void *actual_val;
973 size_t testsize, actual_size;
975 nvl = nvlist_create(0);
976 ATF_REQUIRE(nvl != NULL);
980 testval = malloc(testsize);
981 memset(testval, '5', testsize);
982 nvlist_move_binary(nvl, testkey, testval, testsize);
984 actual_val = nvlist_take_binary(nvl, testkey, &actual_size);
985 ATF_REQUIRE_EQ(testsize, actual_size);
986 ATF_REQUIRE_EQ(memcmp(actual_val, testval, testsize), 0);
987 ATF_REQUIRE(nvlist_empty(nvl));
992 ATF_TEST_CASE_WITHOUT_HEAD(nvlist_take_binary__other_keys_unchanged);
993 ATF_TEST_CASE_BODY(nvlist_take_binary__other_keys_unchanged)
996 const char *testkey, *otherkey1, *otherkey2;
997 const void *actual_value;
998 char testval[] = "gjiertj";
999 char otherval1[] = "fdreg";
1000 size_t testsize, othersize, actual_size;
1003 nvl = nvlist_create(0);
1004 ATF_REQUIRE(nvl != NULL);
1007 othersize = sizeof(otherval1);
1008 nvlist_add_binary(nvl, otherkey1, otherval1, othersize);
1012 nvlist_add_bool(nvl, otherkey2, otherval2);
1015 testsize = sizeof(testval);
1016 nvlist_add_binary(nvl, testkey, testval, testsize);
1018 actual_value = nvlist_take_binary(nvl, testkey, &actual_size);
1019 ATF_REQUIRE_EQ(testsize, actual_size);
1020 ATF_REQUIRE_EQ(memcmp(actual_value, testval, testsize), 0);
1022 ATF_REQUIRE(nvlist_exists_binary(nvl, otherkey1));
1023 actual_value = nvlist_get_binary(nvl, otherkey1, &actual_size);
1024 ATF_REQUIRE_EQ(othersize, actual_size);
1025 ATF_REQUIRE_EQ(memcmp(actual_value, otherval1, othersize), 0);
1027 ATF_REQUIRE(nvlist_exists_bool(nvl, otherkey2));
1028 ATF_REQUIRE_EQ(nvlist_get_bool(nvl, otherkey2), otherval2);
1030 nvlist_destroy(nvl);
1033 ATF_TEST_CASE_WITHOUT_HEAD(nvlist_free__single_null);
1034 ATF_TEST_CASE_BODY(nvlist_free__single_null)
1039 nvl = nvlist_create(0);
1041 nvlist_add_null(nvl, key);
1043 nvlist_free(nvl, key);
1044 ATF_REQUIRE(nvlist_empty(nvl));
1046 nvlist_destroy(nvl);
1049 ATF_TEST_CASE_WITHOUT_HEAD(nvlist_free__single_bool);
1050 ATF_TEST_CASE_BODY(nvlist_free__single_bool)
1055 nvl = nvlist_create(0);
1057 nvlist_add_bool(nvl, key, true);
1059 nvlist_free(nvl, key);
1060 ATF_REQUIRE(nvlist_empty(nvl));
1062 nvlist_destroy(nvl);
1065 ATF_TEST_CASE_WITHOUT_HEAD(nvlist_free__single_number);
1066 ATF_TEST_CASE_BODY(nvlist_free__single_number)
1071 nvl = nvlist_create(0);
1073 nvlist_add_number(nvl, key, 584);
1075 nvlist_free(nvl, key);
1076 ATF_REQUIRE(nvlist_empty(nvl));
1078 nvlist_destroy(nvl);
1081 ATF_TEST_CASE_WITHOUT_HEAD(nvlist_free__single_string);
1082 ATF_TEST_CASE_BODY(nvlist_free__single_string)
1087 nvl = nvlist_create(0);
1089 nvlist_add_string(nvl, key, "gjkfkjd");
1091 nvlist_free(nvl, key);
1092 ATF_REQUIRE(nvlist_empty(nvl));
1094 nvlist_destroy(nvl);
1097 ATF_TEST_CASE_WITHOUT_HEAD(nvlist_free__single_nvlist);
1098 ATF_TEST_CASE_BODY(nvlist_free__single_nvlist)
1103 nvl = nvlist_create(0);
1105 nvlist_add_nvlist(nvl, key, nvlist_create(0));
1107 nvlist_free(nvl, key);
1108 ATF_REQUIRE(nvlist_empty(nvl));
1110 nvlist_destroy(nvl);
1113 ATF_TEST_CASE_WITHOUT_HEAD(nvlist_free__single_binary);
1114 ATF_TEST_CASE_BODY(nvlist_free__single_binary)
1119 nvl = nvlist_create(0);
1121 nvlist_add_binary(nvl, key, "jgjgfd", 6);
1123 nvlist_free(nvl, key);
1124 ATF_REQUIRE(nvlist_empty(nvl));
1126 nvlist_destroy(nvl);
1129 ATF_TEST_CASE_WITHOUT_HEAD(nvlist_free_null__single_null);
1130 ATF_TEST_CASE_BODY(nvlist_free_null__single_null)
1135 nvl = nvlist_create(0);
1137 nvlist_add_null(nvl, key);
1139 nvlist_free_null(nvl, key);
1140 ATF_REQUIRE(nvlist_empty(nvl));
1142 nvlist_destroy(nvl);
1145 ATF_TEST_CASE_WITHOUT_HEAD(nvlist_free_bool__single_bool);
1146 ATF_TEST_CASE_BODY(nvlist_free_bool__single_bool)
1151 nvl = nvlist_create(0);
1153 nvlist_add_bool(nvl, key, true);
1155 nvlist_free_bool(nvl, key);
1156 ATF_REQUIRE(nvlist_empty(nvl));
1158 nvlist_destroy(nvl);
1161 ATF_TEST_CASE_WITHOUT_HEAD(nvlist_free_number__single_number);
1162 ATF_TEST_CASE_BODY(nvlist_free_number__single_number)
1167 nvl = nvlist_create(0);
1169 nvlist_add_number(nvl, key, 584);
1171 nvlist_free_number(nvl, key);
1172 ATF_REQUIRE(nvlist_empty(nvl));
1174 nvlist_destroy(nvl);
1177 ATF_TEST_CASE_WITHOUT_HEAD(nvlist_free_string__single_string);
1178 ATF_TEST_CASE_BODY(nvlist_free_string__single_string)
1183 nvl = nvlist_create(0);
1185 nvlist_add_string(nvl, key, "gjkfkjd");
1187 nvlist_free_string(nvl, key);
1188 ATF_REQUIRE(nvlist_empty(nvl));
1190 nvlist_destroy(nvl);
1193 ATF_TEST_CASE_WITHOUT_HEAD(nvlist_free_nvlist__single_nvlist);
1194 ATF_TEST_CASE_BODY(nvlist_free_nvlist__single_nvlist)
1199 nvl = nvlist_create(0);
1201 nvlist_add_nvlist(nvl, key, nvlist_create(0));
1203 nvlist_free_nvlist(nvl, key);
1204 ATF_REQUIRE(nvlist_empty(nvl));
1206 nvlist_destroy(nvl);
1209 ATF_TEST_CASE_WITHOUT_HEAD(nvlist_free_binary__single_binary);
1210 ATF_TEST_CASE_BODY(nvlist_free_binary__single_binary)
1215 nvl = nvlist_create(0);
1217 nvlist_add_binary(nvl, key, "jgjgfd", 6);
1219 nvlist_free_binary(nvl, key);
1220 ATF_REQUIRE(nvlist_empty(nvl));
1222 nvlist_destroy(nvl);
1225 ATF_INIT_TEST_CASES(tp)
1227 ATF_ADD_TEST_CASE(tp, nvlist_create__is_empty);
1228 ATF_ADD_TEST_CASE(tp, nvlist_add_null__single_insert);
1229 ATF_ADD_TEST_CASE(tp, nvlist_add_bool__single_insert);
1230 ATF_ADD_TEST_CASE(tp, nvlist_add_number__single_insert);
1231 ATF_ADD_TEST_CASE(tp, nvlist_add_string__single_insert);
1232 ATF_ADD_TEST_CASE(tp, nvlist_add_nvlist__single_insert);
1233 ATF_ADD_TEST_CASE(tp, nvlist_add_nvlist__child_with_error);
1234 ATF_ADD_TEST_CASE(tp, nvlist_add_binary__single_insert);
1236 ATF_ADD_TEST_CASE(tp, nvlist_clone__empty_nvlist);
1237 ATF_ADD_TEST_CASE(tp, nvlist_clone__nonempty_nvlist);
1238 ATF_ADD_TEST_CASE(tp, nvlist_clone__nested_nvlist);
1239 ATF_ADD_TEST_CASE(tp, nvlist_clone__error_nvlist);
1241 ATF_ADD_TEST_CASE(tp, nvlist_pack__empty_nvlist);
1242 ATF_ADD_TEST_CASE(tp, nvlist_pack__multiple_values);
1243 ATF_ADD_TEST_CASE(tp, nvlist_pack__error_nvlist);
1244 ATF_ADD_TEST_CASE(tp, nvlist_unpack__duplicate_key);
1245 ATF_ADD_TEST_CASE(tp, nvlist_unpack__flags_nvlist);
1247 ATF_ADD_TEST_CASE(tp, nvlist_move_string__single_insert);
1248 ATF_ADD_TEST_CASE(tp, nvlist_move_nvlist__single_insert);
1249 ATF_ADD_TEST_CASE(tp, nvlist_move_nvlist__null_child);
1250 ATF_ADD_TEST_CASE(tp, nvlist_move_nvlist__child_with_error);
1251 ATF_ADD_TEST_CASE(tp, nvlist_move_binary__single_insert);
1253 ATF_ADD_TEST_CASE(tp, nvlist_take_bool__single_remove);
1254 ATF_ADD_TEST_CASE(tp, nvlist_take_bool__other_keys_unchanged);
1255 ATF_ADD_TEST_CASE(tp, nvlist_take_number__single_remove);
1256 ATF_ADD_TEST_CASE(tp, nvlist_take_number__other_keys_unchanged);
1257 ATF_ADD_TEST_CASE(tp, nvlist_take_string__single_remove);
1258 ATF_ADD_TEST_CASE(tp, nvlist_take_string__other_keys_unchanged);
1259 ATF_ADD_TEST_CASE(tp, nvlist_take_nvlist__single_remove);
1260 ATF_ADD_TEST_CASE(tp, nvlist_take_nvlist__other_keys_unchanged);
1261 ATF_ADD_TEST_CASE(tp, nvlist_take_binary__single_remove);
1262 ATF_ADD_TEST_CASE(tp, nvlist_take_binary__other_keys_unchanged);
1264 ATF_ADD_TEST_CASE(tp, nvlist_free__single_null);
1265 ATF_ADD_TEST_CASE(tp, nvlist_free__single_bool);
1266 ATF_ADD_TEST_CASE(tp, nvlist_free__single_number);
1267 ATF_ADD_TEST_CASE(tp, nvlist_free__single_string);
1268 ATF_ADD_TEST_CASE(tp, nvlist_free__single_nvlist);
1269 ATF_ADD_TEST_CASE(tp, nvlist_free__single_binary);
1271 ATF_ADD_TEST_CASE(tp, nvlist_free_null__single_null);
1272 ATF_ADD_TEST_CASE(tp, nvlist_free_bool__single_bool);
1273 ATF_ADD_TEST_CASE(tp, nvlist_free_number__single_number);
1274 ATF_ADD_TEST_CASE(tp, nvlist_free_string__single_string);
1275 ATF_ADD_TEST_CASE(tp, nvlist_free_nvlist__single_nvlist);
1276 ATF_ADD_TEST_CASE(tp, nvlist_free_binary__single_binary);