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);
610 * Mangle the packed nvlist by replacing key1 with key2, creating a
611 * packed nvlist with a duplicate key.
613 keypos = memmem(packed, size, key1, keylen);
614 ATF_REQUIRE(keypos != NULL);
615 memcpy(keypos, key2, keylen);
617 unpacked = nvlist_unpack(packed, size);
618 ATF_REQUIRE(nvlist_error(unpacked) != 0);
622 nvlist_destroy(unpacked);
625 ATF_TEST_CASE_WITHOUT_HEAD(nvlist_move_string__single_insert);
626 ATF_TEST_CASE_BODY(nvlist_move_string__single_insert)
632 nvl = nvlist_create(0);
633 ATF_REQUIRE(nvl != NULL);
636 value = strdup("testval");
637 ATF_REQUIRE(value != NULL);
639 nvlist_move_string(nvl, key, value);
640 ATF_REQUIRE_EQ(nvlist_get_string(nvl, key), value);
645 ATF_TEST_CASE_WITHOUT_HEAD(nvlist_move_nvlist__null_child);
646 ATF_TEST_CASE_BODY(nvlist_move_nvlist__null_child)
650 parent = nvlist_create(0);
652 nvlist_move_nvlist(parent, "test", NULL);
654 ATF_REQUIRE(nvlist_error(parent) != 0);
656 nvlist_destroy(parent);
659 ATF_TEST_CASE_WITHOUT_HEAD(nvlist_move_nvlist__child_with_error);
660 ATF_TEST_CASE_BODY(nvlist_move_nvlist__child_with_error)
662 nvlist_t *nvl, *parent;
664 nvl = nvlist_create(0);
665 parent = nvlist_create(0);
667 nvlist_set_error(nvl, EBADF);
668 nvlist_move_nvlist(parent, "test", nvl);
669 ATF_REQUIRE_EQ(nvlist_error(parent), EBADF);
671 nvlist_destroy(parent);
674 ATF_TEST_CASE_WITHOUT_HEAD(nvlist_move_nvlist__single_insert);
675 ATF_TEST_CASE_BODY(nvlist_move_nvlist__single_insert)
681 nvl = nvlist_create(0);
682 ATF_REQUIRE(nvl != NULL);
685 value = nvlist_create(0);
686 ATF_REQUIRE(value != NULL);
688 nvlist_move_nvlist(nvl, key, value);
689 ATF_REQUIRE_EQ(nvlist_get_nvlist(nvl, key), value);
694 ATF_TEST_CASE_WITHOUT_HEAD(nvlist_move_binary__single_insert);
695 ATF_TEST_CASE_BODY(nvlist_move_binary__single_insert)
700 size_t size, actual_size;
702 nvl = nvlist_create(0);
703 ATF_REQUIRE(nvl != NULL);
707 value = malloc(size);
708 ATF_REQUIRE(value != NULL);
710 nvlist_move_binary(nvl, key, value, size);
711 ATF_REQUIRE_EQ(nvlist_get_binary(nvl, key, &actual_size), value);
712 ATF_REQUIRE_EQ(size, actual_size);
717 ATF_TEST_CASE_WITHOUT_HEAD(nvlist_take_bool__single_remove);
718 ATF_TEST_CASE_BODY(nvlist_take_bool__single_remove)
724 nvl = nvlist_create(0);
725 ATF_REQUIRE(nvl != NULL);
729 nvlist_add_bool(nvl, testkey, testval);
731 ATF_REQUIRE_EQ(nvlist_take_bool(nvl, testkey), testval);
732 ATF_REQUIRE(nvlist_empty(nvl));
737 ATF_TEST_CASE_WITHOUT_HEAD(nvlist_take_bool__other_keys_unchanged);
738 ATF_TEST_CASE_BODY(nvlist_take_bool__other_keys_unchanged)
741 const char *testkey, *otherkey1, *otherkey2;
742 bool testval, otherval1;
745 nvl = nvlist_create(0);
746 ATF_REQUIRE(nvl != NULL);
750 nvlist_add_bool(nvl, testkey, testval);
754 nvlist_add_bool(nvl, otherkey1, otherval1);
757 otherval2 = create_test_nvlist();
758 nvlist_move_nvlist(nvl, otherkey2, otherval2);
760 ATF_REQUIRE_EQ(nvlist_take_bool(nvl, testkey), testval);
762 ATF_REQUIRE(nvlist_exists_bool(nvl, otherkey1));
763 ATF_REQUIRE_EQ(nvlist_get_bool(nvl, otherkey1), otherval1);
765 ATF_REQUIRE(nvlist_exists_nvlist(nvl, otherkey2));
766 verify_test_nvlist(nvlist_get_nvlist(nvl, otherkey2));
771 ATF_TEST_CASE_WITHOUT_HEAD(nvlist_take_number__single_remove);
772 ATF_TEST_CASE_BODY(nvlist_take_number__single_remove)
778 nvl = nvlist_create(0);
779 ATF_REQUIRE(nvl != NULL);
782 testval = std::numeric_limits<uint64_t>::max();
783 nvlist_add_number(nvl, testkey, testval);
785 ATF_REQUIRE_EQ(nvlist_take_number(nvl, testkey), testval);
786 ATF_REQUIRE(nvlist_empty(nvl));
791 ATF_TEST_CASE_WITHOUT_HEAD(nvlist_take_number__other_keys_unchanged);
792 ATF_TEST_CASE_BODY(nvlist_take_number__other_keys_unchanged)
795 const char *testkey, *otherkey1, *otherkey2;
796 uint64_t testval, otherval1;
797 const char *otherval2;
799 nvl = nvlist_create(0);
800 ATF_REQUIRE(nvl != NULL);
804 nvlist_add_number(nvl, otherkey1, otherval1);
808 nvlist_add_number(nvl, testkey, testval);
811 otherval2 = "string";
812 nvlist_add_string(nvl, otherkey2, otherval2);
814 ATF_REQUIRE_EQ(nvlist_take_number(nvl, testkey), testval);
816 ATF_REQUIRE(nvlist_exists_number(nvl, otherkey1));
817 ATF_REQUIRE_EQ(nvlist_get_number(nvl, otherkey1), otherval1);
819 ATF_REQUIRE(nvlist_exists_string(nvl, otherkey2));
820 ATF_REQUIRE_EQ(strcmp(nvlist_get_string(nvl, otherkey2), otherval2), 0);
825 ATF_TEST_CASE_WITHOUT_HEAD(nvlist_take_string__single_remove);
826 ATF_TEST_CASE_BODY(nvlist_take_string__single_remove)
832 nvl = nvlist_create(0);
833 ATF_REQUIRE(nvl != NULL);
837 nvlist_add_string(nvl, testkey, testval);
839 ATF_REQUIRE_EQ(strcmp(nvlist_take_string(nvl, testkey), testval), 0);
840 ATF_REQUIRE(nvlist_empty(nvl));
845 ATF_TEST_CASE_WITHOUT_HEAD(nvlist_take_string__other_keys_unchanged);
846 ATF_TEST_CASE_BODY(nvlist_take_string__other_keys_unchanged)
849 const char *testkey, *otherkey1, *otherkey2;
850 const char *testval, *otherval1;
853 nvl = nvlist_create(0);
854 ATF_REQUIRE(nvl != NULL);
857 otherval1 = "fjdifjdk";
858 nvlist_add_string(nvl, otherkey1, otherval1);
862 nvlist_add_bool(nvl, otherkey2, otherval2);
866 nvlist_add_string(nvl, testkey, testval);
868 ATF_REQUIRE_EQ(strcmp(nvlist_take_string(nvl, testkey), testval), 0);
870 ATF_REQUIRE(nvlist_exists_string(nvl, otherkey1));
871 ATF_REQUIRE_EQ(strcmp(nvlist_get_string(nvl, otherkey1), otherval1), 0);
873 ATF_REQUIRE(nvlist_exists_bool(nvl, otherkey2));
874 ATF_REQUIRE_EQ(nvlist_get_bool(nvl, otherkey2), otherval2);
879 ATF_TEST_CASE_WITHOUT_HEAD(nvlist_take_nvlist__single_remove);
880 ATF_TEST_CASE_BODY(nvlist_take_nvlist__single_remove)
886 nvl = nvlist_create(0);
887 ATF_REQUIRE(nvl != NULL);
890 testval = create_test_nvlist();
891 nvlist_move_nvlist(nvl, testkey, testval);
893 verify_test_nvlist(nvlist_take_nvlist(nvl, testkey));
894 ATF_REQUIRE(nvlist_empty(nvl));
899 ATF_TEST_CASE_WITHOUT_HEAD(nvlist_take_nvlist__other_keys_unchanged);
900 ATF_TEST_CASE_BODY(nvlist_take_nvlist__other_keys_unchanged)
903 const char *testkey, *otherkey1, *otherkey2;
904 nvlist_t *testval, *otherval1;
906 nvl = nvlist_create(0);
907 ATF_REQUIRE(nvl != NULL);
910 testval = create_test_nvlist();
911 nvlist_move_nvlist(nvl, testkey, testval);
914 otherval1 = nvlist_create(0);
915 nvlist_move_nvlist(nvl, otherkey1, otherval1);
918 nvlist_add_null(nvl, otherkey2);
920 verify_test_nvlist(nvlist_take_nvlist(nvl, testkey));
922 ATF_REQUIRE(nvlist_exists_nvlist(nvl, otherkey1));
923 ATF_REQUIRE(nvlist_empty(nvlist_get_nvlist(nvl, otherkey1)));
925 ATF_REQUIRE(nvlist_exists_null(nvl, otherkey2));
930 ATF_TEST_CASE_WITHOUT_HEAD(nvlist_take_binary__single_remove);
931 ATF_TEST_CASE_BODY(nvlist_take_binary__single_remove)
936 const void *actual_val;
937 size_t testsize, actual_size;
939 nvl = nvlist_create(0);
940 ATF_REQUIRE(nvl != NULL);
944 testval = malloc(testsize);
945 memset(testval, '5', testsize);
946 nvlist_move_binary(nvl, testkey, testval, testsize);
948 actual_val = nvlist_take_binary(nvl, testkey, &actual_size);
949 ATF_REQUIRE_EQ(testsize, actual_size);
950 ATF_REQUIRE_EQ(memcmp(actual_val, testval, testsize), 0);
951 ATF_REQUIRE(nvlist_empty(nvl));
956 ATF_TEST_CASE_WITHOUT_HEAD(nvlist_take_binary__other_keys_unchanged);
957 ATF_TEST_CASE_BODY(nvlist_take_binary__other_keys_unchanged)
960 const char *testkey, *otherkey1, *otherkey2;
961 const void *actual_value;
962 char testval[] = "gjiertj";
963 char otherval1[] = "fdreg";
964 size_t testsize, othersize, actual_size;
967 nvl = nvlist_create(0);
968 ATF_REQUIRE(nvl != NULL);
971 othersize = sizeof(otherval1);
972 nvlist_add_binary(nvl, otherkey1, otherval1, othersize);
976 nvlist_add_bool(nvl, otherkey2, otherval2);
979 testsize = sizeof(testval);
980 nvlist_add_binary(nvl, testkey, testval, testsize);
982 actual_value = nvlist_take_binary(nvl, testkey, &actual_size);
983 ATF_REQUIRE_EQ(testsize, actual_size);
984 ATF_REQUIRE_EQ(memcmp(actual_value, testval, testsize), 0);
986 ATF_REQUIRE(nvlist_exists_binary(nvl, otherkey1));
987 actual_value = nvlist_get_binary(nvl, otherkey1, &actual_size);
988 ATF_REQUIRE_EQ(othersize, actual_size);
989 ATF_REQUIRE_EQ(memcmp(actual_value, otherval1, othersize), 0);
991 ATF_REQUIRE(nvlist_exists_bool(nvl, otherkey2));
992 ATF_REQUIRE_EQ(nvlist_get_bool(nvl, otherkey2), otherval2);
997 ATF_TEST_CASE_WITHOUT_HEAD(nvlist_free__single_null);
998 ATF_TEST_CASE_BODY(nvlist_free__single_null)
1003 nvl = nvlist_create(0);
1005 nvlist_add_null(nvl, key);
1007 nvlist_free(nvl, key);
1008 ATF_REQUIRE(nvlist_empty(nvl));
1010 nvlist_destroy(nvl);
1013 ATF_TEST_CASE_WITHOUT_HEAD(nvlist_free__single_bool);
1014 ATF_TEST_CASE_BODY(nvlist_free__single_bool)
1019 nvl = nvlist_create(0);
1021 nvlist_add_bool(nvl, key, true);
1023 nvlist_free(nvl, key);
1024 ATF_REQUIRE(nvlist_empty(nvl));
1026 nvlist_destroy(nvl);
1029 ATF_TEST_CASE_WITHOUT_HEAD(nvlist_free__single_number);
1030 ATF_TEST_CASE_BODY(nvlist_free__single_number)
1035 nvl = nvlist_create(0);
1037 nvlist_add_number(nvl, key, 584);
1039 nvlist_free(nvl, key);
1040 ATF_REQUIRE(nvlist_empty(nvl));
1042 nvlist_destroy(nvl);
1045 ATF_TEST_CASE_WITHOUT_HEAD(nvlist_free__single_string);
1046 ATF_TEST_CASE_BODY(nvlist_free__single_string)
1051 nvl = nvlist_create(0);
1053 nvlist_add_string(nvl, key, "gjkfkjd");
1055 nvlist_free(nvl, key);
1056 ATF_REQUIRE(nvlist_empty(nvl));
1058 nvlist_destroy(nvl);
1061 ATF_TEST_CASE_WITHOUT_HEAD(nvlist_free__single_nvlist);
1062 ATF_TEST_CASE_BODY(nvlist_free__single_nvlist)
1067 nvl = nvlist_create(0);
1069 nvlist_add_nvlist(nvl, key, nvlist_create(0));
1071 nvlist_free(nvl, key);
1072 ATF_REQUIRE(nvlist_empty(nvl));
1074 nvlist_destroy(nvl);
1077 ATF_TEST_CASE_WITHOUT_HEAD(nvlist_free__single_binary);
1078 ATF_TEST_CASE_BODY(nvlist_free__single_binary)
1083 nvl = nvlist_create(0);
1085 nvlist_add_binary(nvl, key, "jgjgfd", 6);
1087 nvlist_free(nvl, key);
1088 ATF_REQUIRE(nvlist_empty(nvl));
1090 nvlist_destroy(nvl);
1093 ATF_TEST_CASE_WITHOUT_HEAD(nvlist_free_null__single_null);
1094 ATF_TEST_CASE_BODY(nvlist_free_null__single_null)
1099 nvl = nvlist_create(0);
1101 nvlist_add_null(nvl, key);
1103 nvlist_free_null(nvl, key);
1104 ATF_REQUIRE(nvlist_empty(nvl));
1106 nvlist_destroy(nvl);
1109 ATF_TEST_CASE_WITHOUT_HEAD(nvlist_free_bool__single_bool);
1110 ATF_TEST_CASE_BODY(nvlist_free_bool__single_bool)
1115 nvl = nvlist_create(0);
1117 nvlist_add_bool(nvl, key, true);
1119 nvlist_free_bool(nvl, key);
1120 ATF_REQUIRE(nvlist_empty(nvl));
1122 nvlist_destroy(nvl);
1125 ATF_TEST_CASE_WITHOUT_HEAD(nvlist_free_number__single_number);
1126 ATF_TEST_CASE_BODY(nvlist_free_number__single_number)
1131 nvl = nvlist_create(0);
1133 nvlist_add_number(nvl, key, 584);
1135 nvlist_free_number(nvl, key);
1136 ATF_REQUIRE(nvlist_empty(nvl));
1138 nvlist_destroy(nvl);
1141 ATF_TEST_CASE_WITHOUT_HEAD(nvlist_free_string__single_string);
1142 ATF_TEST_CASE_BODY(nvlist_free_string__single_string)
1147 nvl = nvlist_create(0);
1149 nvlist_add_string(nvl, key, "gjkfkjd");
1151 nvlist_free_string(nvl, key);
1152 ATF_REQUIRE(nvlist_empty(nvl));
1154 nvlist_destroy(nvl);
1157 ATF_TEST_CASE_WITHOUT_HEAD(nvlist_free_nvlist__single_nvlist);
1158 ATF_TEST_CASE_BODY(nvlist_free_nvlist__single_nvlist)
1163 nvl = nvlist_create(0);
1165 nvlist_add_nvlist(nvl, key, nvlist_create(0));
1167 nvlist_free_nvlist(nvl, key);
1168 ATF_REQUIRE(nvlist_empty(nvl));
1170 nvlist_destroy(nvl);
1173 ATF_TEST_CASE_WITHOUT_HEAD(nvlist_free_binary__single_binary);
1174 ATF_TEST_CASE_BODY(nvlist_free_binary__single_binary)
1179 nvl = nvlist_create(0);
1181 nvlist_add_binary(nvl, key, "jgjgfd", 6);
1183 nvlist_free_binary(nvl, key);
1184 ATF_REQUIRE(nvlist_empty(nvl));
1186 nvlist_destroy(nvl);
1189 ATF_INIT_TEST_CASES(tp)
1191 ATF_ADD_TEST_CASE(tp, nvlist_create__is_empty);
1192 ATF_ADD_TEST_CASE(tp, nvlist_add_null__single_insert);
1193 ATF_ADD_TEST_CASE(tp, nvlist_add_bool__single_insert);
1194 ATF_ADD_TEST_CASE(tp, nvlist_add_number__single_insert);
1195 ATF_ADD_TEST_CASE(tp, nvlist_add_string__single_insert);
1196 ATF_ADD_TEST_CASE(tp, nvlist_add_nvlist__single_insert);
1197 ATF_ADD_TEST_CASE(tp, nvlist_add_nvlist__child_with_error);
1198 ATF_ADD_TEST_CASE(tp, nvlist_add_binary__single_insert);
1200 ATF_ADD_TEST_CASE(tp, nvlist_clone__empty_nvlist);
1201 ATF_ADD_TEST_CASE(tp, nvlist_clone__nonempty_nvlist);
1202 ATF_ADD_TEST_CASE(tp, nvlist_clone__nested_nvlist);
1203 ATF_ADD_TEST_CASE(tp, nvlist_clone__error_nvlist);
1205 ATF_ADD_TEST_CASE(tp, nvlist_pack__empty_nvlist);
1206 ATF_ADD_TEST_CASE(tp, nvlist_pack__multiple_values);
1207 ATF_ADD_TEST_CASE(tp, nvlist_pack__error_nvlist);
1208 ATF_ADD_TEST_CASE(tp, nvlist_unpack__duplicate_key);
1210 ATF_ADD_TEST_CASE(tp, nvlist_move_string__single_insert);
1211 ATF_ADD_TEST_CASE(tp, nvlist_move_nvlist__single_insert);
1212 ATF_ADD_TEST_CASE(tp, nvlist_move_nvlist__null_child);
1213 ATF_ADD_TEST_CASE(tp, nvlist_move_nvlist__child_with_error);
1214 ATF_ADD_TEST_CASE(tp, nvlist_move_binary__single_insert);
1216 ATF_ADD_TEST_CASE(tp, nvlist_take_bool__single_remove);
1217 ATF_ADD_TEST_CASE(tp, nvlist_take_bool__other_keys_unchanged);
1218 ATF_ADD_TEST_CASE(tp, nvlist_take_number__single_remove);
1219 ATF_ADD_TEST_CASE(tp, nvlist_take_number__other_keys_unchanged);
1220 ATF_ADD_TEST_CASE(tp, nvlist_take_string__single_remove);
1221 ATF_ADD_TEST_CASE(tp, nvlist_take_string__other_keys_unchanged);
1222 ATF_ADD_TEST_CASE(tp, nvlist_take_nvlist__single_remove);
1223 ATF_ADD_TEST_CASE(tp, nvlist_take_nvlist__other_keys_unchanged);
1224 ATF_ADD_TEST_CASE(tp, nvlist_take_binary__single_remove);
1225 ATF_ADD_TEST_CASE(tp, nvlist_take_binary__other_keys_unchanged);
1227 ATF_ADD_TEST_CASE(tp, nvlist_free__single_null);
1228 ATF_ADD_TEST_CASE(tp, nvlist_free__single_bool);
1229 ATF_ADD_TEST_CASE(tp, nvlist_free__single_number);
1230 ATF_ADD_TEST_CASE(tp, nvlist_free__single_string);
1231 ATF_ADD_TEST_CASE(tp, nvlist_free__single_nvlist);
1232 ATF_ADD_TEST_CASE(tp, nvlist_free__single_binary);
1234 ATF_ADD_TEST_CASE(tp, nvlist_free_null__single_null);
1235 ATF_ADD_TEST_CASE(tp, nvlist_free_bool__single_bool);
1236 ATF_ADD_TEST_CASE(tp, nvlist_free_number__single_number);
1237 ATF_ADD_TEST_CASE(tp, nvlist_free_string__single_string);
1238 ATF_ADD_TEST_CASE(tp, nvlist_free_nvlist__single_nvlist);
1239 ATF_ADD_TEST_CASE(tp, nvlist_free_binary__single_binary);