2 * Copyright (c) 2015 Mariusz Zaborski <oshogbo@FreeBSD.org>
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 <sys/param.h>
31 #include <sys/types.h>
33 #include <sys/socket.h>
35 #include <atf-c++.hpp>
45 #define fd_is_valid(fd) (fcntl((fd), F_GETFL) != -1 || errno != EBADF)
47 ATF_TEST_CASE_WITHOUT_HEAD(nvlist_bool_array__basic);
48 ATF_TEST_CASE_BODY(nvlist_bool_array__basic)
51 const bool *const_result;
59 nvl = nvlist_create(0);
60 ATF_REQUIRE(nvl != NULL);
61 ATF_REQUIRE(nvlist_empty(nvl));
62 ATF_REQUIRE(!nvlist_exists_string_array(nvl, key));
64 for (i = 0; i < 16; i++)
65 testbool[i] = (i % 2 == 0);
67 nvlist_add_bool_array(nvl, key, testbool, 16);
68 ATF_REQUIRE_EQ(nvlist_error(nvl), 0);
69 ATF_REQUIRE(!nvlist_empty(nvl));
70 ATF_REQUIRE(nvlist_exists_bool_array(nvl, key));
71 ATF_REQUIRE(nvlist_exists_bool_array(nvl, "nvl/bool"));
73 const_result = nvlist_get_bool_array(nvl, key, &num_items);
74 ATF_REQUIRE_EQ(num_items, 16);
75 ATF_REQUIRE(const_result != NULL);
76 for (i = 0; i < num_items; i++)
77 ATF_REQUIRE_EQ(const_result[i], testbool[i]);
79 result = nvlist_take_bool_array(nvl, key, &num_items);
80 ATF_REQUIRE_EQ(num_items, 16);
81 ATF_REQUIRE(const_result != NULL);
82 for (i = 0; i < num_items; i++)
83 ATF_REQUIRE_EQ(result[i], testbool[i]);
85 ATF_REQUIRE(!nvlist_exists_bool_array(nvl, key));
86 ATF_REQUIRE(nvlist_empty(nvl));
87 ATF_REQUIRE_EQ(nvlist_error(nvl), 0);
93 ATF_TEST_CASE_WITHOUT_HEAD(nvlist_string_array__basic);
94 ATF_TEST_CASE_BODY(nvlist_string_array__basic)
96 const char * const *const_result;
102 const char *string_arr[8] = { "a", "b", "kot", "foo",
103 "tests", "nice test", "", "abcdef" };
106 nvl = nvlist_create(0);
107 ATF_REQUIRE(nvl != NULL);
108 ATF_REQUIRE(nvlist_empty(nvl));
109 ATF_REQUIRE(!nvlist_exists_string_array(nvl, key));
111 nvlist_add_string_array(nvl, key, string_arr, nitems(string_arr));
112 ATF_REQUIRE_EQ(nvlist_error(nvl), 0);
113 ATF_REQUIRE(!nvlist_empty(nvl));
114 ATF_REQUIRE(nvlist_exists_string_array(nvl, key));
115 ATF_REQUIRE(nvlist_exists_string_array(nvl, "nvl/string"));
117 const_result = nvlist_get_string_array(nvl, key, &num_items);
118 ATF_REQUIRE(!nvlist_empty(nvl));
119 ATF_REQUIRE(const_result != NULL);
120 ATF_REQUIRE(num_items == nitems(string_arr));
121 for (i = 0; i < num_items; i++) {
122 if (string_arr[i] != NULL) {
123 ATF_REQUIRE(strcmp(const_result[i],
124 string_arr[i]) == 0);
126 ATF_REQUIRE(const_result[i] == string_arr[i]);
130 result = nvlist_take_string_array(nvl, key, &num_items);
131 ATF_REQUIRE(result != NULL);
132 ATF_REQUIRE_EQ(num_items, nitems(string_arr));
133 for (i = 0; i < num_items; i++) {
134 if (string_arr[i] != NULL) {
135 ATF_REQUIRE_EQ(strcmp(result[i], string_arr[i]), 0);
137 ATF_REQUIRE_EQ(result[i], string_arr[i]);
141 ATF_REQUIRE(!nvlist_exists_string_array(nvl, key));
142 ATF_REQUIRE(nvlist_empty(nvl));
143 ATF_REQUIRE_EQ(nvlist_error(nvl), 0);
145 for (i = 0; i < num_items; i++)
151 ATF_TEST_CASE_WITHOUT_HEAD(nvlist_descriptor_array__basic);
152 ATF_TEST_CASE_BODY(nvlist_descriptor_array__basic)
155 const int *const_result;
161 for (i = 0; i < nitems(fd); i++) {
162 fd[i] = dup(STDERR_FILENO);
163 ATF_REQUIRE(fd_is_valid(fd[i]));
166 key = "nvl/descriptor";
167 nvl = nvlist_create(0);
168 ATF_REQUIRE(nvl != NULL);
169 ATF_REQUIRE(nvlist_empty(nvl));
170 ATF_REQUIRE(!nvlist_exists_descriptor_array(nvl, key));
172 nvlist_add_descriptor_array(nvl, key, fd, nitems(fd));
173 ATF_REQUIRE_EQ(nvlist_error(nvl), 0);
174 ATF_REQUIRE(!nvlist_empty(nvl));
175 ATF_REQUIRE(nvlist_exists_descriptor_array(nvl, key));
176 ATF_REQUIRE(nvlist_exists_descriptor_array(nvl, "nvl/descriptor"));
178 const_result = nvlist_get_descriptor_array(nvl, key, &num_items);
179 ATF_REQUIRE(!nvlist_empty(nvl));
180 ATF_REQUIRE(const_result != NULL);
181 ATF_REQUIRE(num_items == nitems(fd));
182 for (i = 0; i < num_items; i++) {
183 ATF_REQUIRE(fd_is_valid(const_result[i]));
185 ATF_REQUIRE(const_result[i] != const_result[i - 1]);
188 result = nvlist_take_descriptor_array(nvl, key, &num_items);
189 ATF_REQUIRE(result != NULL);
190 ATF_REQUIRE_EQ(num_items, nitems(fd));
191 for (i = 0; i < num_items; i++) {
192 ATF_REQUIRE(fd_is_valid(result[i]));
194 ATF_REQUIRE(const_result[i] != const_result[i - 1]);
197 ATF_REQUIRE(!nvlist_exists_string_array(nvl, key));
198 ATF_REQUIRE(nvlist_empty(nvl));
199 ATF_REQUIRE_EQ(nvlist_error(nvl), 0);
201 for (i = 0; i < num_items; i++) {
209 ATF_TEST_CASE_WITHOUT_HEAD(nvlist_number_array__basic);
210 ATF_TEST_CASE_BODY(nvlist_number_array__basic)
212 const uint64_t *const_result;
218 const uint64_t number[8] = { 0, UINT_MAX, 7, 123, 90,
222 nvl = nvlist_create(0);
223 ATF_REQUIRE(nvl != NULL);
224 ATF_REQUIRE(nvlist_empty(nvl));
225 ATF_REQUIRE(!nvlist_exists_string_array(nvl, key));
227 nvlist_add_number_array(nvl, key, number, nitems(number));
228 ATF_REQUIRE_EQ(nvlist_error(nvl), 0);
229 ATF_REQUIRE(!nvlist_empty(nvl));
230 ATF_REQUIRE(nvlist_exists_number_array(nvl, key));
231 ATF_REQUIRE(nvlist_exists_number_array(nvl, "nvl/number"));
233 const_result = nvlist_get_number_array(nvl, key, &num_items);
234 ATF_REQUIRE(!nvlist_empty(nvl));
235 ATF_REQUIRE(const_result != NULL);
236 ATF_REQUIRE(num_items == nitems(number));
237 for (i = 0; i < num_items; i++)
238 ATF_REQUIRE_EQ(const_result[i], number[i]);
240 result = nvlist_take_number_array(nvl, key, &num_items);
241 ATF_REQUIRE(result != NULL);
242 ATF_REQUIRE_EQ(num_items, nitems(number));
243 for (i = 0; i < num_items; i++)
244 ATF_REQUIRE_EQ(result[i], number[i]);
246 ATF_REQUIRE(!nvlist_exists_string_array(nvl, key));
247 ATF_REQUIRE(nvlist_empty(nvl));
248 ATF_REQUIRE_EQ(nvlist_error(nvl), 0);
254 ATF_TEST_CASE_WITHOUT_HEAD(nvlist_nvlist_array__basic);
255 ATF_TEST_CASE_BODY(nvlist_nvlist_array__basic)
257 nvlist_t *testnvl[8];
258 const nvlist_t * const *const_result;
263 const char *somestr[8] = { "a", "b", "c", "d", "e", "f", "g", "h" };
266 for (i = 0; i < 8; i++) {
267 testnvl[i] = nvlist_create(0);
268 ATF_REQUIRE(testnvl[i] != NULL);
269 ATF_REQUIRE_EQ(nvlist_error(testnvl[i]), 0);
270 nvlist_add_string(testnvl[i], "nvl/string", somestr[i]);
271 ATF_REQUIRE_EQ(nvlist_error(testnvl[i]), 0);
272 ATF_REQUIRE(nvlist_exists_string(testnvl[i], "nvl/string"));
276 nvl = nvlist_create(0);
277 ATF_REQUIRE(nvl != NULL);
278 ATF_REQUIRE(nvlist_empty(nvl));
279 ATF_REQUIRE(!nvlist_exists_string_array(nvl, key));
281 nvlist_add_nvlist_array(nvl, key, (const nvlist_t * const *)testnvl, 8);
282 ATF_REQUIRE_EQ(nvlist_error(nvl), 0);
283 ATF_REQUIRE(!nvlist_empty(nvl));
284 ATF_REQUIRE(nvlist_exists_nvlist_array(nvl, key));
285 ATF_REQUIRE(nvlist_exists_nvlist_array(nvl, "nvl/nvlist"));
287 const_result = nvlist_get_nvlist_array(nvl, key, &num_items);
288 ATF_REQUIRE(!nvlist_empty(nvl));
289 ATF_REQUIRE(const_result != NULL);
290 ATF_REQUIRE(num_items == nitems(testnvl));
292 for (i = 0; i < num_items; i++) {
293 ATF_REQUIRE_EQ(nvlist_error(const_result[i]), 0);
294 if (i < num_items - 1) {
295 ATF_REQUIRE(nvlist_get_array_next(const_result[i]) ==
296 const_result[i + 1]);
298 ATF_REQUIRE(nvlist_get_array_next(const_result[i]) ==
301 ATF_REQUIRE(nvlist_get_parent(const_result[i], NULL) == nvl);
302 ATF_REQUIRE(nvlist_in_array(const_result[i]));
303 ATF_REQUIRE(nvlist_exists_string(const_result[i],
305 ATF_REQUIRE(strcmp(nvlist_get_string(const_result[i],
306 "nvl/string"), somestr[i]) == 0);
309 result = nvlist_take_nvlist_array(nvl, key, &num_items);
310 ATF_REQUIRE(result != NULL);
311 ATF_REQUIRE_EQ(num_items, 8);
312 for (i = 0; i < num_items; i++) {
313 ATF_REQUIRE_EQ(nvlist_error(result[i]), 0);
314 ATF_REQUIRE(nvlist_get_array_next(result[i]) == NULL);
315 ATF_REQUIRE(nvlist_get_parent(result[i], NULL) == NULL);
316 ATF_REQUIRE(nvlist_get_array_next(const_result[i]) == NULL);
317 ATF_REQUIRE(!nvlist_in_array(const_result[i]));
320 ATF_REQUIRE(!nvlist_exists_string_array(nvl, key));
321 ATF_REQUIRE(nvlist_empty(nvl));
322 ATF_REQUIRE_EQ(nvlist_error(nvl), 0);
324 for (i = 0; i < 8; i++) {
325 nvlist_destroy(result[i]);
326 nvlist_destroy(testnvl[i]);
333 ATF_TEST_CASE_WITHOUT_HEAD(nvlist_clone_array);
334 ATF_TEST_CASE_BODY(nvlist_clone_array)
336 nvlist_t *testnvl[8];
342 const char *string_arr[8] = { "a", "b", "kot", "foo",
343 "tests", "nice test", "", "abcdef" };
344 const char *somestr[8] = { "a", "b", "c", "d", "e", "f", "g", "h" };
345 const uint64_t number[8] = { 0, UINT_MAX, 7, 123, 90,
348 for (i = 0; i < nitems(testfd); i++) {
349 testbool[i] = (i % 2 == 0);
350 testfd[i] = dup(STDERR_FILENO);
351 ATF_REQUIRE(fd_is_valid(testfd[i]));
353 for (i = 0; i < nitems(testnvl); i++) {
354 testnvl[i] = nvlist_create(0);
355 ATF_REQUIRE(nvlist_error(testnvl[i]) == 0);
356 nvlist_add_string(testnvl[i], "nvl/nvl/teststr", somestr[i]);
357 ATF_REQUIRE(nvlist_error(testnvl[i]) == 0);
360 src = nvlist_create(0);
361 ATF_REQUIRE(nvlist_error(src) == 0);
363 ATF_REQUIRE(!nvlist_exists_bool_array(src, "nvl/bool"));
364 nvlist_add_bool_array(src, "nvl/bool", testbool, nitems(testbool));
365 ATF_REQUIRE_EQ(nvlist_error(src), 0);
366 ATF_REQUIRE(nvlist_exists_bool_array(src, "nvl/bool"));
368 ATF_REQUIRE(!nvlist_exists_string_array(src, "nvl/string"));
369 nvlist_add_string_array(src, "nvl/string", string_arr,
371 ATF_REQUIRE_EQ(nvlist_error(src), 0);
372 ATF_REQUIRE(nvlist_exists_string_array(src, "nvl/string"));
374 ATF_REQUIRE(!nvlist_exists_descriptor_array(src, "nvl/fd"));
375 nvlist_add_descriptor_array(src, "nvl/fd", testfd, nitems(testfd));
376 ATF_REQUIRE_EQ(nvlist_error(src), 0);
377 ATF_REQUIRE(nvlist_exists_descriptor_array(src, "nvl/fd"));
379 ATF_REQUIRE(!nvlist_exists_number_array(src, "nvl/number"));
380 nvlist_add_number_array(src, "nvl/number", number,
382 ATF_REQUIRE_EQ(nvlist_error(src), 0);
383 ATF_REQUIRE(nvlist_exists_number_array(src, "nvl/number"));
385 ATF_REQUIRE(!nvlist_exists_nvlist_array(src, "nvl/array"));
386 nvlist_add_nvlist_array(src, "nvl/array",
387 (const nvlist_t * const *)testnvl, nitems(testnvl));
388 ATF_REQUIRE_EQ(nvlist_error(src), 0);
389 ATF_REQUIRE(nvlist_exists_nvlist_array(src, "nvl/array"));
391 dst = nvlist_clone(src);
392 ATF_REQUIRE(dst != NULL);
394 ATF_REQUIRE(nvlist_exists_bool_array(dst, "nvl/bool"));
395 (void) nvlist_get_bool_array(dst, "nvl/bool", &num_items);
396 ATF_REQUIRE_EQ(num_items, nitems(testbool));
397 for (i = 0; i < num_items; i++) {
399 nvlist_get_bool_array(dst, "nvl/bool", &num_items)[i] ==
400 nvlist_get_bool_array(src, "nvl/bool", &num_items)[i]);
403 ATF_REQUIRE(nvlist_exists_string_array(dst, "nvl/string"));
404 (void) nvlist_get_string_array(dst, "nvl/string", &num_items);
405 ATF_REQUIRE_EQ(num_items, nitems(string_arr));
406 for (i = 0; i < num_items; i++) {
407 if (nvlist_get_string_array(dst, "nvl/string",
408 &num_items)[i] == NULL) {
409 ATF_REQUIRE(nvlist_get_string_array(dst, "nvl/string",
410 &num_items)[i] == nvlist_get_string_array(src,
411 "nvl/string", &num_items)[i]);
413 ATF_REQUIRE(strcmp(nvlist_get_string_array(dst,
414 "nvl/string", &num_items)[i], nvlist_get_string_array(
415 src, "nvl/string", &num_items)[i]) == 0);
419 ATF_REQUIRE(nvlist_exists_descriptor_array(dst, "nvl/fd"));
420 (void) nvlist_get_descriptor_array(dst, "nvl/fd", &num_items);
421 ATF_REQUIRE_EQ(num_items, nitems(testfd));
422 for (i = 0; i < num_items; i++) {
423 ATF_REQUIRE(fd_is_valid(
424 nvlist_get_descriptor_array(dst, "nvl/fd", &num_items)[i]));
426 ATF_REQUIRE(nvlist_exists_number_array(dst, "nvl/number"));
427 (void) nvlist_get_number_array(dst, "nvl/number", &num_items);
428 ATF_REQUIRE_EQ(num_items, nitems(number));
430 for (i = 0; i < num_items; i++) {
432 nvlist_get_number_array(dst, "nvl/number", &num_items)[i] ==
433 nvlist_get_number_array(src, "nvl/number", &num_items)[i]);
436 ATF_REQUIRE(nvlist_exists_nvlist_array(dst, "nvl/array"));
437 (void) nvlist_get_nvlist_array(dst, "nvl/array", &num_items);
438 ATF_REQUIRE_EQ(num_items, nitems(testnvl));
439 for (i = 0; i < num_items; i++) {
440 nvl = nvlist_get_nvlist_array(dst, "nvl/array", &num_items)[i];
441 ATF_REQUIRE(nvlist_exists_string(nvl, "nvl/nvl/teststr"));
442 ATF_REQUIRE(strcmp(nvlist_get_string(nvl, "nvl/nvl/teststr"),
446 for (i = 0; i < nitems(testfd); i++) {
449 for (i = 0; i < nitems(testnvl); i++) {
450 nvlist_destroy(testnvl[i]);
456 ATF_TEST_CASE_WITHOUT_HEAD(nvlist_bool_array__move);
457 ATF_TEST_CASE_BODY(nvlist_bool_array__move)
460 const bool *const_result;
462 size_t num_items, count;
467 nvl = nvlist_create(0);
468 ATF_REQUIRE(nvl != NULL);
469 ATF_REQUIRE(nvlist_empty(nvl));
470 ATF_REQUIRE(!nvlist_exists_string_array(nvl, key));
473 testbool = (bool*)malloc(sizeof(*testbool) * count);
474 ATF_REQUIRE(testbool != NULL);
475 for (i = 0; i < count; i++)
476 testbool[i] = (i % 2 == 0);
478 nvlist_move_bool_array(nvl, key, testbool, count);
479 ATF_REQUIRE_EQ(nvlist_error(nvl), 0);
480 ATF_REQUIRE(!nvlist_empty(nvl));
481 ATF_REQUIRE(nvlist_exists_bool_array(nvl, key));
483 const_result = nvlist_get_bool_array(nvl, key, &num_items);
484 ATF_REQUIRE_EQ(num_items, count);
485 ATF_REQUIRE(const_result != NULL);
486 ATF_REQUIRE(const_result == testbool);
487 for (i = 0; i < num_items; i++)
488 ATF_REQUIRE_EQ(const_result[i], (i % 2 == 0));
493 ATF_TEST_CASE_WITHOUT_HEAD(nvlist_string_array__move);
494 ATF_TEST_CASE_BODY(nvlist_string_array__move)
497 const char * const *const_result;
499 size_t num_items, count;
504 nvl = nvlist_create(0);
505 ATF_REQUIRE(nvl != NULL);
506 ATF_REQUIRE(nvlist_empty(nvl));
507 ATF_REQUIRE(!nvlist_exists_string_array(nvl, key));
510 teststr = (char**)malloc(sizeof(*teststr) * count);
511 ATF_REQUIRE(teststr != NULL);
512 for (i = 0; i < count; i++) {
513 teststr[i] = (char*)malloc(sizeof(**teststr) * 2);
514 ATF_REQUIRE(teststr[i] != NULL);
515 teststr[i][0] = 'a' + i;
516 teststr[i][1] = '\0';
519 nvlist_move_string_array(nvl, key, teststr, count);
520 ATF_REQUIRE_EQ(nvlist_error(nvl), 0);
521 ATF_REQUIRE(!nvlist_empty(nvl));
522 ATF_REQUIRE(nvlist_exists_string_array(nvl, key));
524 const_result = nvlist_get_string_array(nvl, key, &num_items);
525 ATF_REQUIRE_EQ(num_items, count);
526 ATF_REQUIRE(const_result != NULL);
527 ATF_REQUIRE((intptr_t)const_result == (intptr_t)teststr);
528 for (i = 0; i < num_items; i++) {
529 ATF_REQUIRE_EQ(const_result[i][0], (char)('a' + i));
530 ATF_REQUIRE_EQ(const_result[i][1], '\0');
536 ATF_TEST_CASE_WITHOUT_HEAD(nvlist_nvlist_array__move);
537 ATF_TEST_CASE_BODY(nvlist_nvlist_array__move)
540 const nvlist * const *const_result;
542 size_t num_items, count;
547 nvl = nvlist_create(0);
548 ATF_REQUIRE(nvl != NULL);
549 ATF_REQUIRE(nvlist_empty(nvl));
550 ATF_REQUIRE(!nvlist_exists_nvlist_array(nvl, key));
553 testnv = (nvlist**)malloc(sizeof(*testnv) * count);
554 ATF_REQUIRE(testnv != NULL);
555 for (i = 0; i < count; i++) {
556 testnv[i] = nvlist_create(0);
557 ATF_REQUIRE(testnv[i] != NULL);
560 nvlist_move_nvlist_array(nvl, key, testnv, count);
561 ATF_REQUIRE_EQ(nvlist_error(nvl), 0);
562 ATF_REQUIRE(!nvlist_empty(nvl));
563 ATF_REQUIRE(nvlist_exists_nvlist_array(nvl, key));
565 const_result = nvlist_get_nvlist_array(nvl, key, &num_items);
566 ATF_REQUIRE_EQ(num_items, count);
567 ATF_REQUIRE(const_result != NULL);
568 ATF_REQUIRE((intptr_t)const_result == (intptr_t)testnv);
569 for (i = 0; i < num_items; i++) {
570 ATF_REQUIRE_EQ(nvlist_error(const_result[i]), 0);
571 ATF_REQUIRE(nvlist_empty(const_result[i]));
572 if (i < num_items - 1) {
573 ATF_REQUIRE(nvlist_get_array_next(const_result[i]) ==
574 const_result[i + 1]);
576 ATF_REQUIRE(nvlist_get_array_next(const_result[i]) ==
579 ATF_REQUIRE(nvlist_get_parent(const_result[i], NULL) == nvl);
580 ATF_REQUIRE(nvlist_in_array(const_result[i]));
586 ATF_TEST_CASE_WITHOUT_HEAD(nvlist_number_array__move);
587 ATF_TEST_CASE_BODY(nvlist_number_array__move)
589 uint64_t *testnumber;
590 const uint64_t *const_result;
592 size_t num_items, count;
597 nvl = nvlist_create(0);
598 ATF_REQUIRE(nvl != NULL);
599 ATF_REQUIRE(nvlist_empty(nvl));
600 ATF_REQUIRE(!nvlist_exists_string_array(nvl, key));
603 testnumber = (uint64_t*)malloc(sizeof(*testnumber) * count);
604 ATF_REQUIRE(testnumber != NULL);
605 for (i = 0; i < count; i++)
608 nvlist_move_number_array(nvl, key, testnumber, count);
609 ATF_REQUIRE_EQ(nvlist_error(nvl), 0);
610 ATF_REQUIRE(!nvlist_empty(nvl));
611 ATF_REQUIRE(nvlist_exists_number_array(nvl, key));
613 const_result = nvlist_get_number_array(nvl, key, &num_items);
614 ATF_REQUIRE_EQ(num_items, count);
615 ATF_REQUIRE(const_result != NULL);
616 ATF_REQUIRE(const_result == testnumber);
617 for (i = 0; i < num_items; i++)
618 ATF_REQUIRE_EQ(const_result[i], i);
623 ATF_TEST_CASE_WITHOUT_HEAD(nvlist_descriptor_array__move);
624 ATF_TEST_CASE_BODY(nvlist_descriptor_array__move)
627 const int *const_result;
629 size_t num_items, count;
634 nvl = nvlist_create(0);
635 ATF_REQUIRE(nvl != NULL);
636 ATF_REQUIRE(nvlist_empty(nvl));
637 ATF_REQUIRE(!nvlist_exists_string_array(nvl, key));
640 testfd = (int*)malloc(sizeof(*testfd) * count);
641 ATF_REQUIRE(testfd != NULL);
642 for (i = 0; i < count; i++) {
643 testfd[i] = dup(STDERR_FILENO);
644 ATF_REQUIRE(fd_is_valid(testfd[i]));
647 nvlist_move_descriptor_array(nvl, key, testfd, count);
648 ATF_REQUIRE_EQ(nvlist_error(nvl), 0);
649 ATF_REQUIRE(!nvlist_empty(nvl));
650 ATF_REQUIRE(nvlist_exists_descriptor_array(nvl, key));
652 const_result = nvlist_get_descriptor_array(nvl, key, &num_items);
653 ATF_REQUIRE_EQ(num_items, count);
654 ATF_REQUIRE(const_result != NULL);
655 ATF_REQUIRE(const_result == testfd);
656 for (i = 0; i < num_items; i++)
657 ATF_REQUIRE(fd_is_valid(const_result[i]));
662 ATF_TEST_CASE_WITHOUT_HEAD(nvlist_arrays__error_null);
663 ATF_TEST_CASE_BODY(nvlist_arrays__error_null)
667 nvl = nvlist_create(0);
668 ATF_REQUIRE(nvl != NULL);
669 nvlist_add_number_array(nvl, "nvl/number", NULL, 0);
670 ATF_REQUIRE(nvlist_error(nvl) != 0);
673 nvl = nvlist_create(0);
674 ATF_REQUIRE(nvl != NULL);
675 nvlist_move_number_array(nvl, "nvl/number", NULL, 0);
676 ATF_REQUIRE(nvlist_error(nvl) != 0);
679 nvl = nvlist_create(0);
680 ATF_REQUIRE(nvl != NULL);
681 nvlist_add_descriptor_array(nvl, "nvl/fd", NULL, 0);
682 ATF_REQUIRE(nvlist_error(nvl) != 0);
685 nvl = nvlist_create(0);
686 ATF_REQUIRE(nvl != NULL);
687 nvlist_move_descriptor_array(nvl, "nvl/fd", NULL, 0);
688 ATF_REQUIRE(nvlist_error(nvl) != 0);
691 nvl = nvlist_create(0);
692 ATF_REQUIRE(nvl != NULL);
693 nvlist_add_string_array(nvl, "nvl/string", NULL, 0);
694 ATF_REQUIRE(nvlist_error(nvl) != 0);
697 nvl = nvlist_create(0);
698 ATF_REQUIRE(nvl != NULL);
699 nvlist_move_string_array(nvl, "nvl/string", NULL, 0);
700 ATF_REQUIRE(nvlist_error(nvl) != 0);
703 nvl = nvlist_create(0);
704 ATF_REQUIRE(nvl != NULL);
705 nvlist_add_nvlist_array(nvl, "nvl/nvlist", NULL, 0);
706 ATF_REQUIRE(nvlist_error(nvl) != 0);
709 nvl = nvlist_create(0);
710 ATF_REQUIRE(nvl != NULL);
711 nvlist_move_nvlist_array(nvl, "nvl/nvlist", NULL, 0);
712 ATF_REQUIRE(nvlist_error(nvl) != 0);
715 nvl = nvlist_create(0);
716 ATF_REQUIRE(nvl != NULL);
717 nvlist_add_bool_array(nvl, "nvl/bool", NULL, 0);
718 ATF_REQUIRE(nvlist_error(nvl) != 0);
721 nvl = nvlist_create(0);
722 ATF_REQUIRE(nvl != NULL);
723 nvlist_move_bool_array(nvl, "nvl/bool", NULL, 0);
724 ATF_REQUIRE(nvlist_error(nvl) != 0);
728 ATF_TEST_CASE_WITHOUT_HEAD(nvlist_arrays__bad_value);
729 ATF_TEST_CASE_BODY(nvlist_arrays__bad_value)
731 nvlist_t *nvl, *nvladd[1], **nvlmove;
732 int fdadd[1], *fdmove;
735 nvl = nvlist_create(0);
736 ATF_REQUIRE(nvl != NULL);
737 nvlist_add_nvlist_array(nvl, "nvl/nvlist", nvladd, 1);
738 ATF_REQUIRE(nvlist_error(nvl) != 0);
741 nvlmove = (nvlist_t**)malloc(sizeof(*nvlmove));
742 ATF_REQUIRE(nvlmove != NULL);
744 nvl = nvlist_create(0);
745 ATF_REQUIRE(nvl != NULL);
746 nvlist_move_nvlist_array(nvl, "nvl/nvlist", nvlmove, 1);
747 ATF_REQUIRE(nvlist_error(nvl) != 0);
751 nvl = nvlist_create(0);
752 ATF_REQUIRE(nvl != NULL);
753 nvlist_add_descriptor_array(nvl, "nvl/fd", fdadd, 1);
754 ATF_REQUIRE(nvlist_error(nvl) != 0);
757 fdmove = (int*)malloc(sizeof(*fdmove));
758 ATF_REQUIRE(fdmove != NULL);
760 nvl = nvlist_create(0);
761 ATF_REQUIRE(nvl != NULL);
762 nvlist_move_descriptor_array(nvl, "nvl/fd", fdmove, 1);
763 ATF_REQUIRE(nvlist_error(nvl) != 0);
767 ATF_TEST_CASE_WITHOUT_HEAD(nvlist_nvlist_array__travel);
768 ATF_TEST_CASE_BODY(nvlist_nvlist_array__travel)
770 nvlist_t *nvl, *test[5], *nasted;
771 const nvlist_t *travel;
775 unsigned int i, index;
777 for (i = 0; i < nitems(test); i++) {
778 test[i] = nvlist_create(0);
779 ATF_REQUIRE(test[i] != NULL);
780 nvlist_add_number(test[i], "nvl/number", i);
781 ATF_REQUIRE(nvlist_error(test[i]) == 0);
783 nvl = nvlist_create(0);
784 ATF_REQUIRE(nvl != NULL);
785 nvlist_add_nvlist_array(nvl, "nvl/nvlist_array", test, nitems(test));
786 ATF_REQUIRE(nvlist_error(nvl) == 0);
787 nasted = nvlist_create(0);
788 ATF_REQUIRE(nasted != NULL);
789 nvlist_add_nvlist_array(nasted, "nvl/nvl/nvlist_array", test,
791 ATF_REQUIRE(nvlist_error(nasted) == 0);
792 nvlist_move_nvlist(nvl, "nvl/nvl", nasted);
793 ATF_REQUIRE(nvlist_error(nvl) == 0);
794 nvlist_add_string(nvl, "nvl/string", "END");
795 ATF_REQUIRE(nvlist_error(nvl) == 0);
801 while ((name = nvlist_next(travel, &type, &cookie)) != NULL) {
803 ATF_REQUIRE(type == NV_TYPE_NVLIST_ARRAY);
804 } else if (index >= 1 && index <= nitems(test)) {
805 ATF_REQUIRE(type == NV_TYPE_NUMBER);
806 } else if (index == nitems(test) + 1) {
807 ATF_REQUIRE(type == NV_TYPE_NVLIST);
808 } else if (index == nitems(test) + 2) {
809 ATF_REQUIRE(type == NV_TYPE_NVLIST_ARRAY);
810 } else if (index >= nitems(test) + 3 &&
811 index <= 2 * nitems(test) + 2) {
812 ATF_REQUIRE(type == NV_TYPE_NUMBER);
813 } else if (index == 2 * nitems(test) + 3) {
814 ATF_REQUIRE(type == NV_TYPE_STRING);
817 if (type == NV_TYPE_NVLIST) {
818 travel = nvlist_get_nvlist(travel, name);
820 } else if (type == NV_TYPE_NVLIST_ARRAY) {
821 travel = nvlist_get_nvlist_array(travel, name,
827 } while ((travel = nvlist_get_pararr(travel, &cookie)) != NULL);
829 for (i = 0; i < nitems(test); i++)
830 nvlist_destroy(test[i]);
835 ATF_TEST_CASE_WITHOUT_HEAD(nvlist_nvlist_array__travel_alternative);
836 ATF_TEST_CASE_BODY(nvlist_nvlist_array__travel_alternative)
838 nvlist_t *nvl, *test[5], *nasted;
839 const nvlist_t *travel, *tmp;
844 for (i = 0; i < 5; i++) {
845 test[i] = nvlist_create(0);
846 ATF_REQUIRE(test[i] != NULL);
847 nvlist_add_number(test[i], "nvl/number", i);
848 ATF_REQUIRE(nvlist_error(test[i]) == 0);
850 nvl = nvlist_create(0);
851 ATF_REQUIRE(nvl != NULL);
852 nvlist_add_nvlist_array(nvl, "nvl/nvlist_array", test, 5);
853 ATF_REQUIRE(nvlist_error(nvl) == 0);
854 nasted = nvlist_create(0);
855 ATF_REQUIRE(nasted != NULL);
856 nvlist_add_nvlist_array(nasted, "nvl/nvl/nvlist_array", test, 5);
857 ATF_REQUIRE(nvlist_error(nasted) == 0);
858 nvlist_move_nvlist(nvl, "nvl/nvl", nasted);
859 ATF_REQUIRE(nvlist_error(nvl) == 0);
860 nvlist_add_string(nvl, "nvl/string", "END");
861 ATF_REQUIRE(nvlist_error(nvl) == 0);
869 while ((name = nvlist_next(travel, &type, &cookie)) !=
873 NV_TYPE_NVLIST_ARRAY);
874 } else if (index >= 1 && index <= 5) {
875 ATF_REQUIRE(type == NV_TYPE_NUMBER);
876 } else if (index == 6) {
877 ATF_REQUIRE(type == NV_TYPE_NVLIST);
878 } else if (index == 7) {
880 NV_TYPE_NVLIST_ARRAY);
881 } else if (index >= 8 && index <= 12) {
882 ATF_REQUIRE(type == NV_TYPE_NUMBER);
883 } else if (index == 13) {
884 ATF_REQUIRE(type == NV_TYPE_STRING);
887 if (type == NV_TYPE_NVLIST) {
888 travel = nvlist_get_nvlist(travel,
891 } else if (type == NV_TYPE_NVLIST_ARRAY) {
892 travel = nvlist_get_nvlist_array(travel,
899 } while ((tmp = nvlist_get_array_next(travel)) != NULL);
900 } while ((tmp = nvlist_get_parent(travel, &cookie)) != NULL);
902 for (i = 0; i < 5; i++)
903 nvlist_destroy(test[i]);
908 ATF_TEST_CASE_WITHOUT_HEAD(nvlist_bool_array__pack);
909 ATF_TEST_CASE_BODY(nvlist_bool_array__pack)
911 nvlist_t *nvl, *unpacked;
913 size_t packed_size, count;
916 const bool *const_result;
919 for (i = 0; i < nitems(testbool); i++)
920 testbool[i] = (i % 2 == 0);
923 nvl = nvlist_create(0);
924 ATF_REQUIRE(nvl != NULL);
925 ATF_REQUIRE(nvlist_empty(nvl));
926 ATF_REQUIRE(!nvlist_exists_string_array(nvl, key));
928 nvlist_add_bool_array(nvl, key, testbool, nitems(testbool));
929 ATF_REQUIRE_EQ(nvlist_error(nvl), 0);
930 ATF_REQUIRE(!nvlist_empty(nvl));
931 ATF_REQUIRE(nvlist_exists_bool_array(nvl, key));
933 packed = nvlist_pack(nvl, &packed_size);
934 ATF_REQUIRE(packed != NULL);
936 unpacked = nvlist_unpack(packed, packed_size, 0);
937 ATF_REQUIRE(unpacked != NULL);
938 ATF_REQUIRE_EQ(nvlist_error(unpacked), 0);
939 ATF_REQUIRE(nvlist_exists_bool_array(unpacked, key));
941 const_result = nvlist_get_bool_array(unpacked, key, &count);
942 ATF_REQUIRE_EQ(count, nitems(testbool));
943 for (i = 0; i < count; i++) {
944 ATF_REQUIRE_EQ(testbool[i], const_result[i]);
948 nvlist_destroy(unpacked);
952 ATF_TEST_CASE_WITHOUT_HEAD(nvlist_number_array__pack);
953 ATF_TEST_CASE_BODY(nvlist_number_array__pack)
955 nvlist_t *nvl, *unpacked;
957 size_t packed_size, count;
960 const uint64_t *const_result;
961 const uint64_t number[8] = { 0, UINT_MAX, 7, 123, 90,
965 nvl = nvlist_create(0);
966 ATF_REQUIRE(nvl != NULL);
967 ATF_REQUIRE(nvlist_empty(nvl));
968 ATF_REQUIRE(!nvlist_exists_string_array(nvl, key));
970 nvlist_add_number_array(nvl, key, number, 8);
971 ATF_REQUIRE_EQ(nvlist_error(nvl), 0);
972 ATF_REQUIRE(!nvlist_empty(nvl));
973 ATF_REQUIRE(nvlist_exists_number_array(nvl, key));
975 packed = nvlist_pack(nvl, &packed_size);
976 ATF_REQUIRE(packed != NULL);
978 unpacked = nvlist_unpack(packed, packed_size, 0);
979 ATF_REQUIRE(unpacked != NULL);
980 ATF_REQUIRE_EQ(nvlist_error(unpacked), 0);
981 ATF_REQUIRE(nvlist_exists_number_array(unpacked, key));
983 const_result = nvlist_get_number_array(unpacked, key, &count);
984 ATF_REQUIRE_EQ(count, nitems(number));
985 for (i = 0; i < count; i++) {
986 ATF_REQUIRE_EQ(number[i], const_result[i]);
990 nvlist_destroy(unpacked);
994 ATF_TEST_CASE_WITHOUT_HEAD(nvlist_descriptor_array__pack);
995 ATF_TEST_CASE_BODY(nvlist_descriptor_array__pack)
1001 const int *const_result;
1002 int desc[32], fd, socks[2];
1005 key = "nvl/descriptor";
1007 ATF_REQUIRE_EQ(socketpair(PF_UNIX, SOCK_STREAM, 0, socks), 0);
1009 pid = atf::utils::fork();
1010 ATF_REQUIRE(pid >= 0);
1015 for (i = 0; i < nitems(desc); i++) {
1016 desc[i] = dup(STDERR_FILENO);
1017 ATF_REQUIRE(fd_is_valid(desc[i]));
1020 nvl = nvlist_create(0);
1021 ATF_REQUIRE(nvl != NULL);
1022 ATF_REQUIRE(nvlist_empty(nvl));
1023 ATF_REQUIRE(!nvlist_exists_descriptor_array(nvl, key));
1025 nvlist_add_descriptor_array(nvl, key, desc, nitems(desc));
1026 ATF_REQUIRE_EQ(nvlist_error(nvl), 0);
1027 ATF_REQUIRE(!nvlist_empty(nvl));
1028 ATF_REQUIRE(nvlist_exists_descriptor_array(nvl, key));
1030 ATF_REQUIRE(nvlist_send(fd, nvl) >= 0);
1032 for (i = 0; i < nitems(desc); i++)
1040 nvl = nvlist_recv(fd, 0);
1041 ATF_REQUIRE(nvl != NULL);
1042 ATF_REQUIRE_EQ(nvlist_error(nvl), 0);
1043 ATF_REQUIRE(nvlist_exists_descriptor_array(nvl, key));
1045 const_result = nvlist_get_descriptor_array(nvl, key, &num_items);
1046 ATF_REQUIRE(const_result != NULL);
1047 ATF_REQUIRE_EQ(num_items, nitems(desc));
1048 for (i = 0; i < num_items; i++)
1049 ATF_REQUIRE(fd_is_valid(const_result[i]));
1051 atf::utils::wait(pid, 0, "", "");
1054 nvlist_destroy(nvl);
1058 ATF_TEST_CASE_WITHOUT_HEAD(nvlist_string_array__pack);
1059 ATF_TEST_CASE_BODY(nvlist_string_array__pack)
1061 nvlist_t *nvl, *unpacked;
1063 size_t packed_size, count;
1066 const char * const *const_result;
1067 const char *string_arr[8] = { "a", "b", "kot", "foo",
1068 "tests", "nice test", "", "abcdef" };
1071 nvl = nvlist_create(0);
1072 ATF_REQUIRE(nvl != NULL);
1073 ATF_REQUIRE(nvlist_empty(nvl));
1074 ATF_REQUIRE(!nvlist_exists_string_array(nvl, key));
1076 nvlist_add_string_array(nvl, key, string_arr, nitems(string_arr));
1077 ATF_REQUIRE_EQ(nvlist_error(nvl), 0);
1078 ATF_REQUIRE(!nvlist_empty(nvl));
1079 ATF_REQUIRE(nvlist_exists_string_array(nvl, key));
1081 packed = nvlist_pack(nvl, &packed_size);
1082 ATF_REQUIRE(packed != NULL);
1084 unpacked = nvlist_unpack(packed, packed_size, 0);
1085 ATF_REQUIRE(unpacked != NULL);
1086 ATF_REQUIRE_EQ(nvlist_error(unpacked), 0);
1087 ATF_REQUIRE(nvlist_exists_string_array(unpacked, key));
1089 const_result = nvlist_get_string_array(unpacked, key, &count);
1090 ATF_REQUIRE_EQ(count, nitems(string_arr));
1091 for (i = 0; i < count; i++) {
1092 ATF_REQUIRE_EQ(strcmp(string_arr[i], const_result[i]), 0);
1095 nvlist_destroy(nvl);
1096 nvlist_destroy(unpacked);
1100 ATF_TEST_CASE_WITHOUT_HEAD(nvlist_nvlist_array__pack);
1101 ATF_TEST_CASE_BODY(nvlist_nvlist_array__pack)
1103 nvlist_t *testnvl[8], *unpacked;
1104 const nvlist_t * const *const_result;
1106 size_t num_items, packed_size;
1109 const char *somestr[8] = { "a", "b", "c", "d", "e", "f", "g", "h" };
1112 for (i = 0; i < nitems(testnvl); i++) {
1113 testnvl[i] = nvlist_create(0);
1114 ATF_REQUIRE(testnvl[i] != NULL);
1115 ATF_REQUIRE_EQ(nvlist_error(testnvl[i]), 0);
1116 nvlist_add_string(testnvl[i], "nvl/string", somestr[i]);
1117 ATF_REQUIRE_EQ(nvlist_error(testnvl[i]), 0);
1118 ATF_REQUIRE(nvlist_exists_string(testnvl[i], "nvl/string"));
1122 nvl = nvlist_create(0);
1123 ATF_REQUIRE(nvl != NULL);
1124 ATF_REQUIRE(nvlist_empty(nvl));
1125 ATF_REQUIRE(!nvlist_exists_string_array(nvl, key));
1127 nvlist_add_nvlist_array(nvl, key, (const nvlist_t * const *)testnvl, 8);
1128 ATF_REQUIRE_EQ(nvlist_error(nvl), 0);
1129 ATF_REQUIRE(!nvlist_empty(nvl));
1130 ATF_REQUIRE(nvlist_exists_nvlist_array(nvl, key));
1131 ATF_REQUIRE(nvlist_exists_nvlist_array(nvl, "nvl/nvlist"));
1132 packed = nvlist_pack(nvl, &packed_size);
1133 ATF_REQUIRE(packed != NULL);
1135 unpacked = nvlist_unpack(packed, packed_size, 0);
1136 ATF_REQUIRE(unpacked != NULL);
1137 ATF_REQUIRE_EQ(nvlist_error(unpacked), 0);
1138 ATF_REQUIRE(nvlist_exists_nvlist_array(unpacked, key));
1140 const_result = nvlist_get_nvlist_array(unpacked, key, &num_items);
1141 ATF_REQUIRE(const_result != NULL);
1142 ATF_REQUIRE_EQ(num_items, nitems(testnvl));
1143 for (i = 0; i < num_items; i++) {
1144 ATF_REQUIRE_EQ(nvlist_error(const_result[i]), 0);
1145 if (i < num_items - 1) {
1146 ATF_REQUIRE(nvlist_get_array_next(const_result[i]) ==
1147 const_result[i + 1]);
1149 ATF_REQUIRE(nvlist_get_array_next(const_result[i]) ==
1152 ATF_REQUIRE(nvlist_get_parent(const_result[i], NULL) == unpacked);
1153 ATF_REQUIRE(nvlist_in_array(const_result[i]));
1154 ATF_REQUIRE(nvlist_exists_string(const_result[i],
1156 ATF_REQUIRE(strcmp(nvlist_get_string(const_result[i],
1157 "nvl/string"), somestr[i]) == 0);
1160 for (i = 0; i < nitems(testnvl); i++)
1161 nvlist_destroy(testnvl[i]);
1162 nvlist_destroy(nvl);
1163 nvlist_destroy(unpacked);
1167 ATF_INIT_TEST_CASES(tp)
1170 ATF_ADD_TEST_CASE(tp, nvlist_bool_array__basic);
1171 ATF_ADD_TEST_CASE(tp, nvlist_string_array__basic);
1172 ATF_ADD_TEST_CASE(tp, nvlist_descriptor_array__basic);
1173 ATF_ADD_TEST_CASE(tp, nvlist_number_array__basic);
1174 ATF_ADD_TEST_CASE(tp, nvlist_nvlist_array__basic)
1176 ATF_ADD_TEST_CASE(tp, nvlist_clone_array)
1178 ATF_ADD_TEST_CASE(tp, nvlist_bool_array__move);
1179 ATF_ADD_TEST_CASE(tp, nvlist_string_array__move);
1180 ATF_ADD_TEST_CASE(tp, nvlist_nvlist_array__move);
1181 ATF_ADD_TEST_CASE(tp, nvlist_number_array__move);
1182 ATF_ADD_TEST_CASE(tp, nvlist_descriptor_array__move);
1184 ATF_ADD_TEST_CASE(tp, nvlist_arrays__error_null);
1186 ATF_ADD_TEST_CASE(tp, nvlist_arrays__bad_value)
1188 ATF_ADD_TEST_CASE(tp, nvlist_nvlist_array__travel)
1189 ATF_ADD_TEST_CASE(tp, nvlist_nvlist_array__travel_alternative)
1191 ATF_ADD_TEST_CASE(tp, nvlist_bool_array__pack)
1192 ATF_ADD_TEST_CASE(tp, nvlist_number_array__pack)
1193 ATF_ADD_TEST_CASE(tp, nvlist_descriptor_array__pack)
1194 ATF_ADD_TEST_CASE(tp, nvlist_string_array__pack)
1195 ATF_ADD_TEST_CASE(tp, nvlist_nvlist_array__pack)