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$");
30 #include <atf-c++.hpp>
40 * Test that a newly created nvlist has no errors, and is empty.
42 ATF_TEST_CASE_WITHOUT_HEAD(nvlist_create__is_empty);
43 ATF_TEST_CASE_BODY(nvlist_create__is_empty)
49 nvl = nvlist_create(0);
51 ATF_REQUIRE(nvl != NULL);
53 ATF_REQUIRE_EQ(nvlist_error(nvl), 0);
54 ATF_REQUIRE(nvlist_empty(nvl));
57 ATF_REQUIRE_EQ(nvlist_next(nvl, &type, &it), static_cast<const char *>(NULL));
62 ATF_TEST_CASE_WITHOUT_HEAD(nvlist_add_null__single_insert);
63 ATF_TEST_CASE_BODY(nvlist_add_null__single_insert)
71 nvl = nvlist_create(0);
73 ATF_REQUIRE(nvl != NULL);
74 ATF_REQUIRE(!nvlist_exists(nvl, key));
76 nvlist_add_null(nvl, key);
78 ATF_REQUIRE(!nvlist_empty(nvl));
79 ATF_REQUIRE(nvlist_exists(nvl, key));
80 ATF_REQUIRE(nvlist_exists_null(nvl, key));
81 ATF_REQUIRE(nvlist_exists_null(nvl, "key"));
83 /* Iterate over the nvlist; ensure that it has only our one key. */
85 ATF_REQUIRE_EQ(strcmp(nvlist_next(nvl, &type, &it), key), 0);
86 ATF_REQUIRE_EQ(type, NV_TYPE_NULL);
87 ATF_REQUIRE_EQ(nvlist_next(nvl, &type,&it), static_cast<const char *>(NULL));
92 ATF_TEST_CASE_WITHOUT_HEAD(nvlist_add_bool__single_insert);
93 ATF_TEST_CASE_BODY(nvlist_add_bool__single_insert)
101 nvl = nvlist_create(0);
103 ATF_REQUIRE(nvl != NULL);
104 ATF_REQUIRE(!nvlist_exists(nvl, key));
106 nvlist_add_bool(nvl, key, true);
108 ATF_REQUIRE(!nvlist_empty(nvl));
109 ATF_REQUIRE(nvlist_exists(nvl, key));
110 ATF_REQUIRE(nvlist_exists(nvl, "name"));
111 ATF_REQUIRE(nvlist_exists_bool(nvl, key));
112 ATF_REQUIRE(nvlist_exists_bool(nvl, "name"));
113 ATF_REQUIRE_EQ(nvlist_get_bool(nvl, key), true);
115 /* Iterate over the nvlist; ensure that it has only our one key. */
117 ATF_REQUIRE_EQ(strcmp(nvlist_next(nvl, &type, &it), key), 0);
118 ATF_REQUIRE_EQ(type, NV_TYPE_BOOL);
119 ATF_REQUIRE_EQ(nvlist_next(nvl, &type,&it), static_cast<const char *>(NULL));
124 ATF_TEST_CASE_WITHOUT_HEAD(nvlist_add_number__single_insert);
125 ATF_TEST_CASE_BODY(nvlist_add_number__single_insert)
135 nvl = nvlist_create(0);
137 ATF_REQUIRE(nvl != NULL);
138 ATF_REQUIRE(!nvlist_exists(nvl, key));
140 nvlist_add_number(nvl, key, value);
142 ATF_REQUIRE(!nvlist_empty(nvl));
143 ATF_REQUIRE(nvlist_exists(nvl, key));
144 ATF_REQUIRE(nvlist_exists(nvl, "foo123"));
145 ATF_REQUIRE(nvlist_exists_number(nvl, key));
146 ATF_REQUIRE_EQ(nvlist_get_number(nvl, key), value);
148 /* Iterate over the nvlist; ensure that it has only our one key. */
150 ATF_REQUIRE_EQ(strcmp(nvlist_next(nvl, &type, &it), key), 0);
151 ATF_REQUIRE_EQ(type, NV_TYPE_NUMBER);
152 ATF_REQUIRE_EQ(nvlist_next(nvl, &type,&it), static_cast<const char *>(NULL));
157 ATF_TEST_CASE_WITHOUT_HEAD(nvlist_add_string__single_insert);
158 ATF_TEST_CASE_BODY(nvlist_add_string__single_insert)
168 nvl = nvlist_create(0);
170 ATF_REQUIRE(nvl != NULL);
171 ATF_REQUIRE(!nvlist_exists(nvl, key));
173 nvlist_add_string(nvl, key, value);
175 ATF_REQUIRE(!nvlist_empty(nvl));
176 ATF_REQUIRE(nvlist_exists(nvl, key));
177 ATF_REQUIRE(nvlist_exists(nvl, "test"));
178 ATF_REQUIRE(nvlist_exists_string(nvl, key));
179 ATF_REQUIRE(nvlist_exists_string(nvl, "test"));
180 ATF_REQUIRE_EQ(strcmp(nvlist_get_string(nvl, key), value), 0);
182 /* nvlist_add_* is required to clone the value, so check for that. */
183 ATF_REQUIRE(nvlist_get_string(nvl, key) != value);
185 /* Iterate over the nvlist; ensure that it has only our one key. */
187 ATF_REQUIRE_EQ(strcmp(nvlist_next(nvl, &type, &it), key), 0);
188 ATF_REQUIRE_EQ(type, NV_TYPE_STRING);
189 ATF_REQUIRE_EQ(nvlist_next(nvl, &type,&it), static_cast<const char *>(NULL));
194 ATF_TEST_CASE_WITHOUT_HEAD(nvlist_add_nvlist__single_insert);
195 ATF_TEST_CASE_BODY(nvlist_add_nvlist__single_insert)
199 const char *key, *subkey;
201 const nvlist_t *value;
206 sublist = nvlist_create(0);
207 nvl = nvlist_create(0);
209 ATF_REQUIRE(nvl != NULL);
210 ATF_REQUIRE(!nvlist_exists(nvl, key));
212 nvlist_add_null(sublist, subkey);
213 nvlist_add_nvlist(nvl, key, sublist);
215 ATF_REQUIRE(!nvlist_empty(nvl));
216 ATF_REQUIRE(nvlist_exists(nvl, key));
217 ATF_REQUIRE(nvlist_exists(nvl, "test"));
218 ATF_REQUIRE(nvlist_exists_nvlist(nvl, key));
219 ATF_REQUIRE(nvlist_exists_nvlist(nvl, "test"));
221 value = nvlist_get_nvlist(nvl, key);
222 ATF_REQUIRE(nvlist_exists_null(value, subkey));
224 /* nvlist_add_* is required to clone the value, so check for that. */
225 ATF_REQUIRE(sublist != value);
227 /* Iterate over the nvlist; ensure that it has only our one key. */
229 ATF_REQUIRE_EQ(strcmp(nvlist_next(nvl, &type, &it), key), 0);
230 ATF_REQUIRE_EQ(type, NV_TYPE_NVLIST);
231 ATF_REQUIRE_EQ(nvlist_next(nvl, &type,&it), static_cast<const char *>(NULL));
233 nvlist_destroy(sublist);
237 ATF_TEST_CASE_WITHOUT_HEAD(nvlist_add_nvlist__child_with_error);
238 ATF_TEST_CASE_BODY(nvlist_add_nvlist__child_with_error)
240 nvlist_t *nvl, *parent;
242 nvl = nvlist_create(0);
243 parent = nvlist_create(0);
245 nvlist_set_error(nvl, EBADF);
246 nvlist_add_nvlist(parent, "test", nvl);
247 ATF_REQUIRE_EQ(nvlist_error(parent), EBADF);
250 nvlist_destroy(parent);
253 ATF_TEST_CASE_WITHOUT_HEAD(nvlist_add_binary__single_insert);
254 ATF_TEST_CASE_BODY(nvlist_add_binary__single_insert)
260 const void *ret_value;
261 size_t value_size, ret_size;
266 value = malloc(value_size);
267 memset(value, 0xa5, value_size);
268 nvl = nvlist_create(0);
270 ATF_REQUIRE(nvl != NULL);
271 ATF_REQUIRE(!nvlist_exists(nvl, key));
273 nvlist_add_binary(nvl, key, value, value_size);
275 ATF_REQUIRE(!nvlist_empty(nvl));
276 ATF_REQUIRE(nvlist_exists(nvl, key));
277 ATF_REQUIRE(nvlist_exists(nvl, "binary"));
278 ATF_REQUIRE(nvlist_exists_binary(nvl, key));
279 ATF_REQUIRE(nvlist_exists_binary(nvl, "binary"));
281 ret_value = nvlist_get_binary(nvl, key, &ret_size);
282 ATF_REQUIRE_EQ(value_size, ret_size);
283 ATF_REQUIRE_EQ(memcmp(value, ret_value, ret_size), 0);
285 /* nvlist_add_* is required to clone the value, so check for that. */
286 ATF_REQUIRE(value != ret_value);
288 /* Iterate over the nvlist; ensure that it has only our one key. */
290 ATF_REQUIRE_EQ(strcmp(nvlist_next(nvl, &type, &it), key), 0);
291 ATF_REQUIRE_EQ(type, NV_TYPE_BINARY);
292 ATF_REQUIRE_EQ(nvlist_next(nvl, &type,&it), static_cast<const char *>(NULL));
298 ATF_TEST_CASE_WITHOUT_HEAD(nvlist_clone__empty_nvlist);
299 ATF_TEST_CASE_BODY(nvlist_clone__empty_nvlist)
301 nvlist_t *nvl, *clone;
303 nvl = nvlist_create(0);
304 ATF_REQUIRE(nvl != NULL);
306 clone = nvlist_clone(nvl);
307 ATF_REQUIRE(clone != NULL);
308 ATF_REQUIRE(clone != nvl);
309 ATF_REQUIRE(nvlist_empty(clone));
311 nvlist_destroy(clone);
315 ATF_TEST_CASE_WITHOUT_HEAD(nvlist_clone__nonempty_nvlist);
316 ATF_TEST_CASE_BODY(nvlist_clone__nonempty_nvlist)
318 nvlist_t *nvl, *clone;
324 nvl = nvlist_create(0);
325 ATF_REQUIRE(nvl != NULL);
329 nvlist_add_number(nvl, key, value);
331 clone = nvlist_clone(nvl);
332 ATF_REQUIRE(clone != NULL);
333 ATF_REQUIRE(clone != nvl);
334 ATF_REQUIRE(nvlist_exists_number(clone, key));
335 ATF_REQUIRE_EQ(nvlist_get_number(clone, key), value);
337 /* Iterate over the nvlist; ensure that it has only our one key. */
339 ATF_REQUIRE_EQ(strcmp(nvlist_next(clone, &type, &it), key), 0);
340 ATF_REQUIRE_EQ(type, NV_TYPE_NUMBER);
341 ATF_REQUIRE_EQ(nvlist_next(clone, &type, &it), static_cast<const char *>(NULL));
343 nvlist_destroy(clone);
347 static const char * const test_subnvlist_key = "nvlist";
349 static const char * const test_string_key = "string";
350 static const char * const test_string_val = "59525";
353 create_test_nvlist(void)
355 nvlist_t *nvl, *sublist;
357 nvl = nvlist_create(0);
358 ATF_REQUIRE(nvl != NULL);
360 sublist = nvlist_create(0);
361 ATF_REQUIRE(sublist != NULL);
363 nvlist_add_string(sublist, test_string_key, test_string_val);
364 nvlist_move_nvlist(nvl, test_subnvlist_key, sublist);
370 verify_test_nvlist(const nvlist_t *nvl)
373 const nvlist_t *value;
376 ATF_REQUIRE(nvlist_exists_nvlist(nvl, test_subnvlist_key));
378 value = nvlist_get_nvlist(nvl, test_subnvlist_key);
380 ATF_REQUIRE(nvlist_exists_string(value, test_string_key));
381 ATF_REQUIRE_EQ(strcmp(nvlist_get_string(value, test_string_key), test_string_val), 0);
382 ATF_REQUIRE(nvlist_get_string(value, test_string_key) != test_string_val);
384 /* Iterate over both nvlists; ensure that each has only the one key. */
386 ATF_REQUIRE_EQ(strcmp(nvlist_next(value, &type, &it),
387 test_string_key), 0);
388 ATF_REQUIRE_EQ(type, NV_TYPE_STRING);
389 ATF_REQUIRE_EQ(nvlist_next(value, &type, &it), static_cast<const char *>(NULL));
392 ATF_REQUIRE_EQ(strcmp(nvlist_next(nvl, &type, &it),
393 test_subnvlist_key), 0);
394 ATF_REQUIRE_EQ(type, NV_TYPE_NVLIST);
395 ATF_REQUIRE_EQ(nvlist_next(nvl, &type, &it), static_cast<const char *>(NULL));
398 ATF_TEST_CASE_WITHOUT_HEAD(nvlist_clone__nested_nvlist);
399 ATF_TEST_CASE_BODY(nvlist_clone__nested_nvlist)
401 nvlist_t *nvl, *clone;
403 nvl = create_test_nvlist();
404 clone = nvlist_clone(nvl);
406 ATF_REQUIRE(clone != NULL);
407 ATF_REQUIRE(clone != nvl);
408 verify_test_nvlist(clone);
410 nvlist_destroy(clone);
414 ATF_TEST_CASE_WITHOUT_HEAD(nvlist_clone__error_nvlist);
415 ATF_TEST_CASE_BODY(nvlist_clone__error_nvlist)
417 nvlist_t *nvl, *clone;
419 nvl = nvlist_create(0);
420 ATF_REQUIRE(nvl != NULL);
422 nvlist_set_error(nvl, ENOMEM);
424 clone = nvlist_clone(nvl);
425 ATF_REQUIRE(clone == NULL);
430 ATF_TEST_CASE_WITHOUT_HEAD(nvlist_pack__empty_nvlist);
431 ATF_TEST_CASE_BODY(nvlist_pack__empty_nvlist)
433 nvlist_t *nvl, *unpacked;
437 nvl = nvlist_create(0);
438 ATF_REQUIRE(nvl != NULL);
440 packed = nvlist_pack(nvl, &packed_size);
441 ATF_REQUIRE(packed != NULL);
443 unpacked = nvlist_unpack(packed, packed_size);
444 ATF_REQUIRE(unpacked != NULL);
445 ATF_REQUIRE(unpacked != nvl);
446 ATF_REQUIRE(nvlist_empty(unpacked));
448 nvlist_destroy(unpacked);
454 verify_null(const nvlist_t *nvl, int type)
457 ATF_REQUIRE_EQ(type, NV_TYPE_NULL);
461 verify_number(const nvlist_t *nvl, const char *name, int type, uint64_t value)
464 ATF_REQUIRE_EQ(type, NV_TYPE_NUMBER);
465 ATF_REQUIRE_EQ(nvlist_get_number(nvl, name), value);
469 verify_string(const nvlist_t *nvl, const char *name, int type,
473 ATF_REQUIRE_EQ(type, NV_TYPE_STRING);
474 ATF_REQUIRE_EQ(strcmp(nvlist_get_string(nvl, name), value), 0);
478 verify_nvlist(const nvlist_t *nvl, const char *name, int type)
481 ATF_REQUIRE_EQ(type, NV_TYPE_NVLIST);
482 verify_test_nvlist(nvlist_get_nvlist(nvl, name));
486 verify_binary(const nvlist_t *nvl, const char *name, int type,
487 const void * value, size_t size)
489 const void *actual_value;
492 ATF_REQUIRE_EQ(type, NV_TYPE_BINARY);
493 actual_value = nvlist_get_binary(nvl, name, &actual_size);
494 ATF_REQUIRE_EQ(size, actual_size);
495 ATF_REQUIRE_EQ(memcmp(value, actual_value, size), 0);
498 ATF_TEST_CASE_WITHOUT_HEAD(nvlist_pack__multiple_values);
499 ATF_TEST_CASE_BODY(nvlist_pack__multiple_values)
501 std::ostringstream msg;
502 std::set<std::string> keys_seen;
503 nvlist_t *nvl, *unpacked, *nvvalue;
504 const char *nullkey, *numkey, *strkey, *nvkey, *binkey, *name;
506 const char * strvalue;
507 void *binvalue, *packed, *it;
508 size_t binsize, packed_size;
511 nvl = nvlist_create(0);
514 nvlist_add_null(nvl, nullkey);
517 numvalue = 939853984;
518 nvlist_add_number(nvl, numkey, numvalue);
521 strvalue = "jfieutijf";
522 nvlist_add_string(nvl, strkey, strvalue);
525 nvvalue = create_test_nvlist();
526 nvlist_move_nvlist(nvl, nvkey, nvvalue);
530 binvalue = malloc(binsize);
531 memset(binvalue, 'b', binsize);
532 nvlist_move_binary(nvl, binkey, binvalue, binsize);
534 packed = nvlist_pack(nvl, &packed_size);
535 ATF_REQUIRE(packed != NULL);
537 unpacked = nvlist_unpack(packed, packed_size);
538 ATF_REQUIRE(unpacked != 0);
541 while ((name = nvlist_next(unpacked, &type, &it)) != NULL) {
542 /* Ensure that we see every key only once. */
543 ATF_REQUIRE_EQ(keys_seen.count(name), 0);
545 if (strcmp(name, nullkey) == 0)
546 verify_null(unpacked, type);
547 else if (strcmp(name, numkey) == 0)
548 verify_number(unpacked, name, type, numvalue);
549 else if (strcmp(name, strkey) == 0)
550 verify_string(unpacked, name, type, strvalue);
551 else if (strcmp(name, nvkey) == 0)
552 verify_nvlist(unpacked, name, type);
553 else if (strcmp(name, binkey) == 0)
554 verify_binary(unpacked, name, type, binvalue, binsize);
556 msg << "Unexpected key :'" << name << "'";
557 ATF_FAIL(msg.str().c_str());
560 keys_seen.insert(name);
563 /* Ensure that we saw every key. */
564 ATF_REQUIRE_EQ(keys_seen.size(), 5);
567 nvlist_destroy(unpacked);
571 ATF_TEST_CASE_WITHOUT_HEAD(nvlist_pack__error_nvlist);
572 ATF_TEST_CASE_BODY(nvlist_pack__error_nvlist)
578 nvl = nvlist_create(0);
579 ATF_REQUIRE(nvl != NULL);
581 nvlist_set_error(nvl, ENOMEM);
583 packed = nvlist_pack(nvl, &size);
584 ATF_REQUIRE(packed == NULL);
589 ATF_TEST_CASE_WITHOUT_HEAD(nvlist_unpack__duplicate_key);
590 ATF_TEST_CASE_BODY(nvlist_unpack__duplicate_key)
592 nvlist_t *nvl, *unpacked;
593 const char *key1, *key2;
594 void *packed, *keypos;
597 nvl = nvlist_create(0);
600 keylen = strlen(key1);
601 nvlist_add_number(nvl, key1, 5);
604 ATF_REQUIRE_EQ(keylen, strlen(key2));
605 nvlist_add_number(nvl, key2, 10);
607 packed = nvlist_pack(nvl, &size);
608 ATF_REQUIRE(packed != NULL);
611 * Mangle the packed nvlist by replacing key1 with key2, creating a
612 * packed nvlist with a duplicate key.
614 keypos = memmem(packed, size, key1, keylen);
615 ATF_REQUIRE(keypos != NULL);
616 memcpy(keypos, key2, keylen);
618 unpacked = nvlist_unpack(packed, size);
619 ATF_REQUIRE(nvlist_error(unpacked) != 0);
623 nvlist_destroy(unpacked);
626 ATF_TEST_CASE_WITHOUT_HEAD(nvlist_move_string__single_insert);
627 ATF_TEST_CASE_BODY(nvlist_move_string__single_insert)
633 nvl = nvlist_create(0);
634 ATF_REQUIRE(nvl != NULL);
637 value = strdup("testval");
638 ATF_REQUIRE(value != NULL);
640 nvlist_move_string(nvl, key, value);
641 ATF_REQUIRE_EQ(nvlist_get_string(nvl, key), value);
646 ATF_TEST_CASE_WITHOUT_HEAD(nvlist_move_nvlist__null_child);
647 ATF_TEST_CASE_BODY(nvlist_move_nvlist__null_child)
651 parent = nvlist_create(0);
653 nvlist_move_nvlist(parent, "test", NULL);
655 ATF_REQUIRE(nvlist_error(parent) != 0);
657 nvlist_destroy(parent);
660 ATF_TEST_CASE_WITHOUT_HEAD(nvlist_move_nvlist__child_with_error);
661 ATF_TEST_CASE_BODY(nvlist_move_nvlist__child_with_error)
663 nvlist_t *nvl, *parent;
665 nvl = nvlist_create(0);
666 parent = nvlist_create(0);
668 nvlist_set_error(nvl, EBADF);
669 nvlist_move_nvlist(parent, "test", nvl);
670 ATF_REQUIRE_EQ(nvlist_error(parent), EBADF);
672 nvlist_destroy(parent);
675 ATF_TEST_CASE_WITHOUT_HEAD(nvlist_move_nvlist__single_insert);
676 ATF_TEST_CASE_BODY(nvlist_move_nvlist__single_insert)
682 nvl = nvlist_create(0);
683 ATF_REQUIRE(nvl != NULL);
686 value = nvlist_create(0);
687 ATF_REQUIRE(value != NULL);
689 nvlist_move_nvlist(nvl, key, value);
690 ATF_REQUIRE_EQ(nvlist_get_nvlist(nvl, key), value);
695 ATF_TEST_CASE_WITHOUT_HEAD(nvlist_move_binary__single_insert);
696 ATF_TEST_CASE_BODY(nvlist_move_binary__single_insert)
701 size_t size, actual_size;
703 nvl = nvlist_create(0);
704 ATF_REQUIRE(nvl != NULL);
708 value = malloc(size);
709 ATF_REQUIRE(value != NULL);
711 nvlist_move_binary(nvl, key, value, size);
712 ATF_REQUIRE_EQ(nvlist_get_binary(nvl, key, &actual_size), value);
713 ATF_REQUIRE_EQ(size, actual_size);
718 ATF_TEST_CASE_WITHOUT_HEAD(nvlist_take_bool__single_remove);
719 ATF_TEST_CASE_BODY(nvlist_take_bool__single_remove)
725 nvl = nvlist_create(0);
726 ATF_REQUIRE(nvl != NULL);
730 nvlist_add_bool(nvl, testkey, testval);
732 ATF_REQUIRE_EQ(nvlist_take_bool(nvl, testkey), testval);
733 ATF_REQUIRE(nvlist_empty(nvl));
738 ATF_TEST_CASE_WITHOUT_HEAD(nvlist_take_bool__other_keys_unchanged);
739 ATF_TEST_CASE_BODY(nvlist_take_bool__other_keys_unchanged)
742 const char *testkey, *otherkey1, *otherkey2;
743 bool testval, otherval1;
746 nvl = nvlist_create(0);
747 ATF_REQUIRE(nvl != NULL);
751 nvlist_add_bool(nvl, testkey, testval);
755 nvlist_add_bool(nvl, otherkey1, otherval1);
758 otherval2 = create_test_nvlist();
759 nvlist_move_nvlist(nvl, otherkey2, otherval2);
761 ATF_REQUIRE_EQ(nvlist_take_bool(nvl, testkey), testval);
763 ATF_REQUIRE(nvlist_exists_bool(nvl, otherkey1));
764 ATF_REQUIRE_EQ(nvlist_get_bool(nvl, otherkey1), otherval1);
766 ATF_REQUIRE(nvlist_exists_nvlist(nvl, otherkey2));
767 verify_test_nvlist(nvlist_get_nvlist(nvl, otherkey2));
772 ATF_TEST_CASE_WITHOUT_HEAD(nvlist_take_number__single_remove);
773 ATF_TEST_CASE_BODY(nvlist_take_number__single_remove)
779 nvl = nvlist_create(0);
780 ATF_REQUIRE(nvl != NULL);
783 testval = std::numeric_limits<uint64_t>::max();
784 nvlist_add_number(nvl, testkey, testval);
786 ATF_REQUIRE_EQ(nvlist_take_number(nvl, testkey), testval);
787 ATF_REQUIRE(nvlist_empty(nvl));
792 ATF_TEST_CASE_WITHOUT_HEAD(nvlist_take_number__other_keys_unchanged);
793 ATF_TEST_CASE_BODY(nvlist_take_number__other_keys_unchanged)
796 const char *testkey, *otherkey1, *otherkey2;
797 uint64_t testval, otherval1;
798 const char *otherval2;
800 nvl = nvlist_create(0);
801 ATF_REQUIRE(nvl != NULL);
805 nvlist_add_number(nvl, otherkey1, otherval1);
809 nvlist_add_number(nvl, testkey, testval);
812 otherval2 = "string";
813 nvlist_add_string(nvl, otherkey2, otherval2);
815 ATF_REQUIRE_EQ(nvlist_take_number(nvl, testkey), testval);
817 ATF_REQUIRE(nvlist_exists_number(nvl, otherkey1));
818 ATF_REQUIRE_EQ(nvlist_get_number(nvl, otherkey1), otherval1);
820 ATF_REQUIRE(nvlist_exists_string(nvl, otherkey2));
821 ATF_REQUIRE_EQ(strcmp(nvlist_get_string(nvl, otherkey2), otherval2), 0);
826 ATF_TEST_CASE_WITHOUT_HEAD(nvlist_take_string__single_remove);
827 ATF_TEST_CASE_BODY(nvlist_take_string__single_remove)
833 nvl = nvlist_create(0);
834 ATF_REQUIRE(nvl != NULL);
838 nvlist_add_string(nvl, testkey, testval);
840 ATF_REQUIRE_EQ(strcmp(nvlist_take_string(nvl, testkey), testval), 0);
841 ATF_REQUIRE(nvlist_empty(nvl));
846 ATF_TEST_CASE_WITHOUT_HEAD(nvlist_take_string__other_keys_unchanged);
847 ATF_TEST_CASE_BODY(nvlist_take_string__other_keys_unchanged)
850 const char *testkey, *otherkey1, *otherkey2;
851 const char *testval, *otherval1;
854 nvl = nvlist_create(0);
855 ATF_REQUIRE(nvl != NULL);
858 otherval1 = "fjdifjdk";
859 nvlist_add_string(nvl, otherkey1, otherval1);
863 nvlist_add_bool(nvl, otherkey2, otherval2);
867 nvlist_add_string(nvl, testkey, testval);
869 ATF_REQUIRE_EQ(strcmp(nvlist_take_string(nvl, testkey), testval), 0);
871 ATF_REQUIRE(nvlist_exists_string(nvl, otherkey1));
872 ATF_REQUIRE_EQ(strcmp(nvlist_get_string(nvl, otherkey1), otherval1), 0);
874 ATF_REQUIRE(nvlist_exists_bool(nvl, otherkey2));
875 ATF_REQUIRE_EQ(nvlist_get_bool(nvl, otherkey2), otherval2);
880 ATF_TEST_CASE_WITHOUT_HEAD(nvlist_take_nvlist__single_remove);
881 ATF_TEST_CASE_BODY(nvlist_take_nvlist__single_remove)
887 nvl = nvlist_create(0);
888 ATF_REQUIRE(nvl != NULL);
891 testval = create_test_nvlist();
892 nvlist_move_nvlist(nvl, testkey, testval);
894 verify_test_nvlist(nvlist_take_nvlist(nvl, testkey));
895 ATF_REQUIRE(nvlist_empty(nvl));
900 ATF_TEST_CASE_WITHOUT_HEAD(nvlist_take_nvlist__other_keys_unchanged);
901 ATF_TEST_CASE_BODY(nvlist_take_nvlist__other_keys_unchanged)
904 const char *testkey, *otherkey1, *otherkey2;
905 nvlist_t *testval, *otherval1;
907 nvl = nvlist_create(0);
908 ATF_REQUIRE(nvl != NULL);
911 testval = create_test_nvlist();
912 nvlist_move_nvlist(nvl, testkey, testval);
915 otherval1 = nvlist_create(0);
916 nvlist_move_nvlist(nvl, otherkey1, otherval1);
919 nvlist_add_null(nvl, otherkey2);
921 verify_test_nvlist(nvlist_take_nvlist(nvl, testkey));
923 ATF_REQUIRE(nvlist_exists_nvlist(nvl, otherkey1));
924 ATF_REQUIRE(nvlist_empty(nvlist_get_nvlist(nvl, otherkey1)));
926 ATF_REQUIRE(nvlist_exists_null(nvl, otherkey2));
931 ATF_TEST_CASE_WITHOUT_HEAD(nvlist_take_binary__single_remove);
932 ATF_TEST_CASE_BODY(nvlist_take_binary__single_remove)
937 const void *actual_val;
938 size_t testsize, actual_size;
940 nvl = nvlist_create(0);
941 ATF_REQUIRE(nvl != NULL);
945 testval = malloc(testsize);
946 memset(testval, '5', testsize);
947 nvlist_move_binary(nvl, testkey, testval, testsize);
949 actual_val = nvlist_take_binary(nvl, testkey, &actual_size);
950 ATF_REQUIRE_EQ(testsize, actual_size);
951 ATF_REQUIRE_EQ(memcmp(actual_val, testval, testsize), 0);
952 ATF_REQUIRE(nvlist_empty(nvl));
957 ATF_TEST_CASE_WITHOUT_HEAD(nvlist_take_binary__other_keys_unchanged);
958 ATF_TEST_CASE_BODY(nvlist_take_binary__other_keys_unchanged)
961 const char *testkey, *otherkey1, *otherkey2;
962 const void *actual_value;
963 char testval[] = "gjiertj";
964 char otherval1[] = "fdreg";
965 size_t testsize, othersize, actual_size;
968 nvl = nvlist_create(0);
969 ATF_REQUIRE(nvl != NULL);
972 othersize = sizeof(otherval1);
973 nvlist_add_binary(nvl, otherkey1, otherval1, othersize);
977 nvlist_add_bool(nvl, otherkey2, otherval2);
980 testsize = sizeof(testval);
981 nvlist_add_binary(nvl, testkey, testval, testsize);
983 actual_value = nvlist_take_binary(nvl, testkey, &actual_size);
984 ATF_REQUIRE_EQ(testsize, actual_size);
985 ATF_REQUIRE_EQ(memcmp(actual_value, testval, testsize), 0);
987 ATF_REQUIRE(nvlist_exists_binary(nvl, otherkey1));
988 actual_value = nvlist_get_binary(nvl, otherkey1, &actual_size);
989 ATF_REQUIRE_EQ(othersize, actual_size);
990 ATF_REQUIRE_EQ(memcmp(actual_value, otherval1, othersize), 0);
992 ATF_REQUIRE(nvlist_exists_bool(nvl, otherkey2));
993 ATF_REQUIRE_EQ(nvlist_get_bool(nvl, otherkey2), otherval2);
998 ATF_TEST_CASE_WITHOUT_HEAD(nvlist_free__single_null);
999 ATF_TEST_CASE_BODY(nvlist_free__single_null)
1004 nvl = nvlist_create(0);
1006 nvlist_add_null(nvl, key);
1008 nvlist_free(nvl, key);
1009 ATF_REQUIRE(nvlist_empty(nvl));
1011 nvlist_destroy(nvl);
1014 ATF_TEST_CASE_WITHOUT_HEAD(nvlist_free__single_bool);
1015 ATF_TEST_CASE_BODY(nvlist_free__single_bool)
1020 nvl = nvlist_create(0);
1022 nvlist_add_bool(nvl, key, true);
1024 nvlist_free(nvl, key);
1025 ATF_REQUIRE(nvlist_empty(nvl));
1027 nvlist_destroy(nvl);
1030 ATF_TEST_CASE_WITHOUT_HEAD(nvlist_free__single_number);
1031 ATF_TEST_CASE_BODY(nvlist_free__single_number)
1036 nvl = nvlist_create(0);
1038 nvlist_add_number(nvl, key, 584);
1040 nvlist_free(nvl, key);
1041 ATF_REQUIRE(nvlist_empty(nvl));
1043 nvlist_destroy(nvl);
1046 ATF_TEST_CASE_WITHOUT_HEAD(nvlist_free__single_string);
1047 ATF_TEST_CASE_BODY(nvlist_free__single_string)
1052 nvl = nvlist_create(0);
1054 nvlist_add_string(nvl, key, "gjkfkjd");
1056 nvlist_free(nvl, key);
1057 ATF_REQUIRE(nvlist_empty(nvl));
1059 nvlist_destroy(nvl);
1062 ATF_TEST_CASE_WITHOUT_HEAD(nvlist_free__single_nvlist);
1063 ATF_TEST_CASE_BODY(nvlist_free__single_nvlist)
1068 nvl = nvlist_create(0);
1070 nvlist_add_nvlist(nvl, key, nvlist_create(0));
1072 nvlist_free(nvl, key);
1073 ATF_REQUIRE(nvlist_empty(nvl));
1075 nvlist_destroy(nvl);
1078 ATF_TEST_CASE_WITHOUT_HEAD(nvlist_free__single_binary);
1079 ATF_TEST_CASE_BODY(nvlist_free__single_binary)
1084 nvl = nvlist_create(0);
1086 nvlist_add_binary(nvl, key, "jgjgfd", 6);
1088 nvlist_free(nvl, key);
1089 ATF_REQUIRE(nvlist_empty(nvl));
1091 nvlist_destroy(nvl);
1094 ATF_TEST_CASE_WITHOUT_HEAD(nvlist_free_null__single_null);
1095 ATF_TEST_CASE_BODY(nvlist_free_null__single_null)
1100 nvl = nvlist_create(0);
1102 nvlist_add_null(nvl, key);
1104 nvlist_free_null(nvl, key);
1105 ATF_REQUIRE(nvlist_empty(nvl));
1107 nvlist_destroy(nvl);
1110 ATF_TEST_CASE_WITHOUT_HEAD(nvlist_free_bool__single_bool);
1111 ATF_TEST_CASE_BODY(nvlist_free_bool__single_bool)
1116 nvl = nvlist_create(0);
1118 nvlist_add_bool(nvl, key, true);
1120 nvlist_free_bool(nvl, key);
1121 ATF_REQUIRE(nvlist_empty(nvl));
1123 nvlist_destroy(nvl);
1126 ATF_TEST_CASE_WITHOUT_HEAD(nvlist_free_number__single_number);
1127 ATF_TEST_CASE_BODY(nvlist_free_number__single_number)
1132 nvl = nvlist_create(0);
1134 nvlist_add_number(nvl, key, 584);
1136 nvlist_free_number(nvl, key);
1137 ATF_REQUIRE(nvlist_empty(nvl));
1139 nvlist_destroy(nvl);
1142 ATF_TEST_CASE_WITHOUT_HEAD(nvlist_free_string__single_string);
1143 ATF_TEST_CASE_BODY(nvlist_free_string__single_string)
1148 nvl = nvlist_create(0);
1150 nvlist_add_string(nvl, key, "gjkfkjd");
1152 nvlist_free_string(nvl, key);
1153 ATF_REQUIRE(nvlist_empty(nvl));
1155 nvlist_destroy(nvl);
1158 ATF_TEST_CASE_WITHOUT_HEAD(nvlist_free_nvlist__single_nvlist);
1159 ATF_TEST_CASE_BODY(nvlist_free_nvlist__single_nvlist)
1164 nvl = nvlist_create(0);
1166 nvlist_add_nvlist(nvl, key, nvlist_create(0));
1168 nvlist_free_nvlist(nvl, key);
1169 ATF_REQUIRE(nvlist_empty(nvl));
1171 nvlist_destroy(nvl);
1174 ATF_TEST_CASE_WITHOUT_HEAD(nvlist_free_binary__single_binary);
1175 ATF_TEST_CASE_BODY(nvlist_free_binary__single_binary)
1180 nvl = nvlist_create(0);
1182 nvlist_add_binary(nvl, key, "jgjgfd", 6);
1184 nvlist_free_binary(nvl, key);
1185 ATF_REQUIRE(nvlist_empty(nvl));
1187 nvlist_destroy(nvl);
1190 ATF_INIT_TEST_CASES(tp)
1192 ATF_ADD_TEST_CASE(tp, nvlist_create__is_empty);
1193 ATF_ADD_TEST_CASE(tp, nvlist_add_null__single_insert);
1194 ATF_ADD_TEST_CASE(tp, nvlist_add_bool__single_insert);
1195 ATF_ADD_TEST_CASE(tp, nvlist_add_number__single_insert);
1196 ATF_ADD_TEST_CASE(tp, nvlist_add_string__single_insert);
1197 ATF_ADD_TEST_CASE(tp, nvlist_add_nvlist__single_insert);
1198 ATF_ADD_TEST_CASE(tp, nvlist_add_nvlist__child_with_error);
1199 ATF_ADD_TEST_CASE(tp, nvlist_add_binary__single_insert);
1201 ATF_ADD_TEST_CASE(tp, nvlist_clone__empty_nvlist);
1202 ATF_ADD_TEST_CASE(tp, nvlist_clone__nonempty_nvlist);
1203 ATF_ADD_TEST_CASE(tp, nvlist_clone__nested_nvlist);
1204 ATF_ADD_TEST_CASE(tp, nvlist_clone__error_nvlist);
1206 ATF_ADD_TEST_CASE(tp, nvlist_pack__empty_nvlist);
1207 ATF_ADD_TEST_CASE(tp, nvlist_pack__multiple_values);
1208 ATF_ADD_TEST_CASE(tp, nvlist_pack__error_nvlist);
1209 ATF_ADD_TEST_CASE(tp, nvlist_unpack__duplicate_key);
1211 ATF_ADD_TEST_CASE(tp, nvlist_move_string__single_insert);
1212 ATF_ADD_TEST_CASE(tp, nvlist_move_nvlist__single_insert);
1213 ATF_ADD_TEST_CASE(tp, nvlist_move_nvlist__null_child);
1214 ATF_ADD_TEST_CASE(tp, nvlist_move_nvlist__child_with_error);
1215 ATF_ADD_TEST_CASE(tp, nvlist_move_binary__single_insert);
1217 ATF_ADD_TEST_CASE(tp, nvlist_take_bool__single_remove);
1218 ATF_ADD_TEST_CASE(tp, nvlist_take_bool__other_keys_unchanged);
1219 ATF_ADD_TEST_CASE(tp, nvlist_take_number__single_remove);
1220 ATF_ADD_TEST_CASE(tp, nvlist_take_number__other_keys_unchanged);
1221 ATF_ADD_TEST_CASE(tp, nvlist_take_string__single_remove);
1222 ATF_ADD_TEST_CASE(tp, nvlist_take_string__other_keys_unchanged);
1223 ATF_ADD_TEST_CASE(tp, nvlist_take_nvlist__single_remove);
1224 ATF_ADD_TEST_CASE(tp, nvlist_take_nvlist__other_keys_unchanged);
1225 ATF_ADD_TEST_CASE(tp, nvlist_take_binary__single_remove);
1226 ATF_ADD_TEST_CASE(tp, nvlist_take_binary__other_keys_unchanged);
1228 ATF_ADD_TEST_CASE(tp, nvlist_free__single_null);
1229 ATF_ADD_TEST_CASE(tp, nvlist_free__single_bool);
1230 ATF_ADD_TEST_CASE(tp, nvlist_free__single_number);
1231 ATF_ADD_TEST_CASE(tp, nvlist_free__single_string);
1232 ATF_ADD_TEST_CASE(tp, nvlist_free__single_nvlist);
1233 ATF_ADD_TEST_CASE(tp, nvlist_free__single_binary);
1235 ATF_ADD_TEST_CASE(tp, nvlist_free_null__single_null);
1236 ATF_ADD_TEST_CASE(tp, nvlist_free_bool__single_bool);
1237 ATF_ADD_TEST_CASE(tp, nvlist_free_number__single_number);
1238 ATF_ADD_TEST_CASE(tp, nvlist_free_string__single_string);
1239 ATF_ADD_TEST_CASE(tp, nvlist_free_nvlist__single_nvlist);
1240 ATF_ADD_TEST_CASE(tp, nvlist_free_binary__single_binary);