1 /*- 2 * Copyright (c) 2016 Adam Starak <starak.adam@gmail.com> 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 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. 13 * 14 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS 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 AUTHORS 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 24 * SUCH DAMAGE. 25 * 26 * $FreeBSD$ 27 */ 28 29 #include <sys/cdefs.h> 30 __FBSDID("$FreeBSD$"); 31 32 #include <sys/cnv.h> 33 #include <sys/nv.h> 34 #include <sys/types.h> 35 36 #include <atf-c++.hpp> 37 #include <fcntl.h> 38 #include <errno.h> 39 40 #define fd_is_valid(fd) (fcntl((fd), F_GETFL) != -1 || errno != EBADF) 41 42 /* ATF cnvlist_get tests. */ 43 44 ATF_TEST_CASE_WITHOUT_HEAD(cnvlist_get_bool); 45 ATF_TEST_CASE_BODY(cnvlist_get_bool) 46 { 47 nvlist_t *nvl; 48 const char *key; 49 bool value; 50 void *cookie; 51 int type; 52 53 nvl = nvlist_create(0); 54 ATF_REQUIRE(nvl != NULL); 55 ATF_REQUIRE_EQ(nvlist_error(nvl), 0); 56 ATF_REQUIRE(nvlist_empty(nvl)); 57 58 cookie = NULL; 59 key = "name"; 60 value = true; 61 62 nvlist_add_bool(nvl, key, value); 63 ATF_REQUIRE_EQ(strcmp(key, nvlist_next(nvl, &type, &cookie)), 0); 64 ATF_REQUIRE_EQ(nvlist_error(nvl), 0); 65 ATF_REQUIRE_EQ(type, NV_TYPE_BOOL); 66 ATF_REQUIRE(!nvlist_empty(nvl)); 67 ATF_REQUIRE(nvlist_exists(nvl, key)); 68 ATF_REQUIRE(nvlist_exists_bool(nvl, key)); 69 70 ATF_REQUIRE_EQ(cnvlist_get_bool(cookie), value); 71 72 ATF_REQUIRE_EQ(nvlist_next(nvl, &type, &cookie), 73 static_cast<const char *>(NULL)); 74 75 nvlist_destroy(nvl); 76 } 77 78 ATF_TEST_CASE_WITHOUT_HEAD(cnvlist_get_number); 79 ATF_TEST_CASE_BODY(cnvlist_get_number) 80 { 81 nvlist_t *nvl; 82 const char *key; 83 uint64_t value; 84 void *cookie; 85 int type; 86 87 nvl = nvlist_create(0); 88 ATF_REQUIRE(nvl != NULL); 89 ATF_REQUIRE_EQ(nvlist_error(nvl), 0); 90 ATF_REQUIRE(nvlist_empty(nvl)); 91 92 cookie = NULL; 93 key = "name"; 94 value = 420; 95 96 nvlist_add_number(nvl, key, value); 97 ATF_REQUIRE_EQ(strcmp(key, nvlist_next(nvl, &type, &cookie)), 0); 98 ATF_REQUIRE_EQ(nvlist_error(nvl), 0); 99 ATF_REQUIRE_EQ(type, NV_TYPE_NUMBER); 100 ATF_REQUIRE(!nvlist_empty(nvl)); 101 ATF_REQUIRE(nvlist_exists(nvl, key)); 102 ATF_REQUIRE(nvlist_exists_number(nvl, key)); 103 104 ATF_REQUIRE_EQ(cnvlist_get_number(cookie), value); 105 106 ATF_REQUIRE_EQ(nvlist_next(nvl, &type, &cookie), 107 static_cast<const char *>(NULL)); 108 109 nvlist_destroy(nvl); 110 } 111 112 113 ATF_TEST_CASE_WITHOUT_HEAD(cnvlist_get_string); 114 ATF_TEST_CASE_BODY(cnvlist_get_string) 115 { 116 nvlist_t *nvl; 117 const char *key; 118 const char *value; 119 void *cookie; 120 int type; 121 122 nvl = nvlist_create(0); 123 ATF_REQUIRE(nvl != NULL); 124 ATF_REQUIRE_EQ(nvlist_error(nvl), 0); 125 ATF_REQUIRE(nvlist_empty(nvl)); 126 127 cookie = NULL; 128 key = "name"; 129 value = "text"; 130 131 nvlist_add_string(nvl, key, value); 132 ATF_REQUIRE_EQ(strcmp(key, nvlist_next(nvl, &type, &cookie)), 0); 133 ATF_REQUIRE_EQ(nvlist_error(nvl), 0); 134 ATF_REQUIRE_EQ(type, NV_TYPE_STRING); 135 ATF_REQUIRE(!nvlist_empty(nvl)); 136 ATF_REQUIRE(nvlist_exists(nvl, key)); 137 ATF_REQUIRE(nvlist_exists_string(nvl, key)); 138 139 ATF_REQUIRE_EQ(strcmp(cnvlist_get_string(cookie), value), 0); 140 141 ATF_REQUIRE_EQ(nvlist_next(nvl, &type, &cookie), 142 static_cast<const char *>(NULL)); 143 144 nvlist_destroy(nvl); 145 } 146 147 ATF_TEST_CASE_WITHOUT_HEAD(cnvlist_get_nvlist); 148 ATF_TEST_CASE_BODY(cnvlist_get_nvlist) 149 { 150 nvlist_t *nvl, *value; 151 const nvlist_t *result; 152 const char *key, *subkey; 153 void *cookie; 154 int type; 155 156 nvl = nvlist_create(0); 157 ATF_REQUIRE(nvl != NULL); 158 ATF_REQUIRE_EQ(nvlist_error(nvl), 0); 159 ATF_REQUIRE(nvlist_empty(nvl)); 160 161 value = nvlist_create(0); 162 ATF_REQUIRE(nvl != NULL); 163 ATF_REQUIRE_EQ(nvlist_error(nvl), 0); 164 ATF_REQUIRE(nvlist_empty(nvl)); 165 166 key = "name"; 167 subkey = "subname"; 168 cookie = NULL; 169 170 /* Add null to 'value' nvlist. */ 171 nvlist_add_null(value, subkey); 172 ATF_REQUIRE_EQ(strcmp(subkey, nvlist_next(value, &type, &cookie)), 0); 173 ATF_REQUIRE_EQ(nvlist_error(value), 0); 174 ATF_REQUIRE_EQ(type, NV_TYPE_NULL); 175 ATF_REQUIRE(!nvlist_empty(value)); 176 ATF_REQUIRE(nvlist_exists(value, subkey)); 177 ATF_REQUIRE(nvlist_exists_null(value, subkey)); 178 ATF_REQUIRE_EQ(nvlist_next(value, &type, &cookie), 179 static_cast<const char *>(NULL)); 180 181 /* Add 'value' nvlist. */ 182 cookie = NULL; 183 nvlist_add_nvlist(nvl, key, value); 184 ATF_REQUIRE_EQ(strcmp(key, nvlist_next(nvl, &type, &cookie)), 0); 185 ATF_REQUIRE_EQ(nvlist_error(nvl), 0); 186 ATF_REQUIRE_EQ(type, NV_TYPE_NVLIST); 187 ATF_REQUIRE(!nvlist_empty(nvl)); 188 ATF_REQUIRE(nvlist_exists(nvl, key)); 189 ATF_REQUIRE(nvlist_exists_nvlist(nvl, key)); 190 191 /* 192 * Assuming nvlist_get_nvlist() is correct check if cnvlist returns 193 * the same pointer. 194 */ 195 result = cnvlist_get_nvlist(cookie); 196 ATF_REQUIRE_EQ(result, nvlist_get_nvlist(nvl, key)); 197 ATF_REQUIRE(result != value); 198 ATF_REQUIRE_EQ(nvlist_next(nvl, &type, &cookie), 199 static_cast<const char *>(NULL)); 200 201 /* Validate data inside nvlist. */ 202 cookie = NULL; 203 ATF_REQUIRE_EQ(strcmp(subkey, nvlist_next(result, &type, &cookie)), 0); 204 ATF_REQUIRE_EQ(nvlist_error(result), 0); 205 ATF_REQUIRE_EQ(type, NV_TYPE_NULL); 206 ATF_REQUIRE(!nvlist_empty(result)); 207 ATF_REQUIRE(nvlist_exists(result, subkey)); 208 ATF_REQUIRE(nvlist_exists_null(result, subkey)); 209 ATF_REQUIRE_EQ(nvlist_next(result, &type, &cookie), 210 static_cast<const char *>(NULL)); 211 212 nvlist_destroy(nvl); 213 nvlist_destroy(value); 214 } 215 216 ATF_TEST_CASE_WITHOUT_HEAD(cnvlist_get_descriptor); 217 ATF_TEST_CASE_BODY(cnvlist_get_descriptor) 218 { 219 nvlist_t *nvl; 220 const char *key; 221 void *cookie; 222 int type; 223 224 nvl = nvlist_create(0); 225 ATF_REQUIRE(nvl != NULL); 226 ATF_REQUIRE_EQ(nvlist_error(nvl), 0); 227 ATF_REQUIRE(nvlist_empty(nvl)); 228 229 cookie = NULL; 230 key = "name"; 231 232 nvlist_add_descriptor(nvl, key, STDERR_FILENO); 233 ATF_REQUIRE_EQ(strcmp(key, nvlist_next(nvl, &type, &cookie)), 0); 234 ATF_REQUIRE_EQ(nvlist_error(nvl), 0); 235 ATF_REQUIRE_EQ(type, NV_TYPE_DESCRIPTOR); 236 ATF_REQUIRE(!nvlist_empty(nvl)); 237 ATF_REQUIRE(nvlist_exists(nvl, key)); 238 ATF_REQUIRE(nvlist_exists_descriptor(nvl, key)); 239 240 ATF_REQUIRE_EQ(fd_is_valid(cnvlist_get_descriptor(cookie)), 1); 241 242 ATF_REQUIRE_EQ(nvlist_next(nvl, &type, &cookie), 243 static_cast<const char *>(NULL)); 244 245 nvlist_destroy(nvl); 246 } 247 248 ATF_TEST_CASE_WITHOUT_HEAD(cnvlist_get_binary); 249 ATF_TEST_CASE_BODY(cnvlist_get_binary) 250 { 251 nvlist_t *nvl; 252 const char *key; 253 void *in_binary; 254 const void *out_binary; 255 void *cookie; 256 int type; 257 size_t in_size, out_size; 258 259 nvl = nvlist_create(0); 260 ATF_REQUIRE(nvl != NULL); 261 ATF_REQUIRE_EQ(nvlist_error(nvl), 0); 262 ATF_REQUIRE(nvlist_empty(nvl)); 263 264 cookie = NULL; 265 key = "name"; 266 in_size = 13; 267 268 in_binary = malloc(in_size); 269 ATF_REQUIRE(in_binary != NULL); 270 memset(in_binary, 0xa5, in_size); 271 272 nvlist_add_binary(nvl, key, in_binary, in_size); 273 ATF_REQUIRE_EQ(strcmp(key, nvlist_next(nvl, &type, &cookie)), 0); 274 ATF_REQUIRE_EQ(nvlist_error(nvl), 0); 275 ATF_REQUIRE_EQ(type, NV_TYPE_BINARY); 276 ATF_REQUIRE(!nvlist_empty(nvl)); 277 ATF_REQUIRE(nvlist_exists(nvl, key)); 278 ATF_REQUIRE(nvlist_exists_binary(nvl, key)); 279 280 out_binary = cnvlist_get_binary(cookie, &out_size); 281 ATF_REQUIRE_EQ(out_size, in_size); 282 ATF_REQUIRE_EQ(memcmp(in_binary, out_binary, out_size), 0); 283 284 ATF_REQUIRE_EQ(nvlist_next(nvl, &type, &cookie), 285 static_cast<const char *>(NULL)); 286 287 nvlist_destroy(nvl); 288 } 289 290 /* ATF cnvlist_get array tests. */ 291 292 ATF_TEST_CASE_WITHOUT_HEAD(cnvlist_get_bool_array); 293 ATF_TEST_CASE_BODY(cnvlist_get_bool_array) 294 { 295 nvlist_t *nvl; 296 bool in_array[16]; 297 const bool *out_array; 298 const char *key; 299 void *cookie; 300 int type, i; 301 size_t nitems; 302 303 for (i = 0; i < 16; i++) 304 in_array[i] = (i % 2 == 0); 305 306 nvl = nvlist_create(0); 307 ATF_REQUIRE(nvl != NULL); 308 ATF_REQUIRE_EQ(nvlist_error(nvl), 0); 309 ATF_REQUIRE(nvlist_empty(nvl)); 310 311 cookie = NULL; 312 key = "name"; 313 314 nvlist_add_bool_array(nvl, key, in_array, 16); 315 ATF_REQUIRE_EQ(strcmp(key, nvlist_next(nvl, &type, &cookie)), 0); 316 ATF_REQUIRE_EQ(nvlist_error(nvl), 0); 317 ATF_REQUIRE_EQ(type, NV_TYPE_BOOL_ARRAY); 318 ATF_REQUIRE(!nvlist_empty(nvl)); 319 ATF_REQUIRE(nvlist_exists(nvl, key)); 320 ATF_REQUIRE(nvlist_exists_bool_array(nvl, key)); 321 322 out_array = cnvlist_get_bool_array(cookie, &nitems); 323 ATF_REQUIRE_EQ(nitems, 16); 324 ATF_REQUIRE(out_array != NULL); 325 for (i = 0; i < 16; i++) 326 ATF_REQUIRE_EQ(out_array[i], in_array[i]); 327 328 ATF_REQUIRE_EQ(nvlist_next(nvl, &type, &cookie), 329 static_cast<const char *>(NULL)); 330 331 nvlist_destroy(nvl); 332 } 333 334 ATF_TEST_CASE_WITHOUT_HEAD(cnvlist_get_number_array); 335 ATF_TEST_CASE_BODY(cnvlist_get_number_array) 336 { 337 nvlist_t *nvl; 338 uint64_t in_array[16]; 339 const uint64_t *out_array; 340 const char *key; 341 void *cookie; 342 int type, i; 343 size_t nitems; 344 345 for (i = 0; i < 16; i++) 346 in_array[i] = i; 347 348 nvl = nvlist_create(0); 349 ATF_REQUIRE(nvl != NULL); 350 ATF_REQUIRE_EQ(nvlist_error(nvl), 0); 351 ATF_REQUIRE(nvlist_empty(nvl)); 352 353 cookie = NULL; 354 key = "name"; 355 356 nvlist_add_number_array(nvl, key, in_array, 16); 357 ATF_REQUIRE_EQ(strcmp(key, nvlist_next(nvl, &type, &cookie)), 0); 358 ATF_REQUIRE_EQ(nvlist_error(nvl), 0); 359 ATF_REQUIRE_EQ(type, NV_TYPE_NUMBER_ARRAY); 360 ATF_REQUIRE(!nvlist_empty(nvl)); 361 ATF_REQUIRE(nvlist_exists(nvl, key)); 362 ATF_REQUIRE(nvlist_exists_number_array(nvl, key)); 363 364 out_array = cnvlist_get_number_array(cookie, &nitems); 365 ATF_REQUIRE(out_array != NULL); 366 ATF_REQUIRE_EQ(nitems, 16); 367 for (i = 0; i < 16; i++) 368 ATF_REQUIRE_EQ(out_array[i], in_array[i]); 369 370 ATF_REQUIRE_EQ(nvlist_next(nvl, &type, &cookie), 371 static_cast<const char *>(NULL)); 372 373 nvlist_destroy(nvl); 374 } 375 376 ATF_TEST_CASE_WITHOUT_HEAD(cnvlist_get_string_array); 377 ATF_TEST_CASE_BODY(cnvlist_get_string_array) 378 { 379 nvlist_t *nvl; 380 const char *in_array[4] = {"inequality", "sucks", ".", ""}; 381 const char * const *out_array; 382 const char *key; 383 void *cookie; 384 int type, i; 385 size_t nitems; 386 387 nvl = nvlist_create(0); 388 ATF_REQUIRE(nvl != NULL); 389 ATF_REQUIRE_EQ(nvlist_error(nvl), 0); 390 ATF_REQUIRE(nvlist_empty(nvl)); 391 392 cookie = NULL; 393 key = "name"; 394 395 nvlist_add_string_array(nvl, key, in_array, 4); 396 ATF_REQUIRE_EQ(strcmp(key, nvlist_next(nvl, &type, &cookie)), 0); 397 ATF_REQUIRE_EQ(nvlist_error(nvl), 0); 398 ATF_REQUIRE_EQ(type, NV_TYPE_STRING_ARRAY); 399 ATF_REQUIRE(!nvlist_empty(nvl)); 400 ATF_REQUIRE(nvlist_exists(nvl, key)); 401 ATF_REQUIRE(nvlist_exists_string_array(nvl, key)); 402 403 out_array = cnvlist_get_string_array(cookie, &nitems); 404 ATF_REQUIRE_EQ(nitems, 4); 405 ATF_REQUIRE(out_array != NULL); 406 for (i = 0; i < 4; i++) { 407 ATF_REQUIRE(out_array[i] != NULL); 408 ATF_REQUIRE_EQ(strcmp(out_array[i], in_array[i]), 0); 409 } 410 411 ATF_REQUIRE_EQ(nvlist_next(nvl, &type, &cookie), 412 static_cast<const char *>(NULL)); 413 414 nvlist_destroy(nvl); 415 } 416 417 ATF_TEST_CASE_WITHOUT_HEAD(cnvlist_get_nvlist_array); 418 ATF_TEST_CASE_BODY(cnvlist_get_nvlist_array) 419 { 420 nvlist_t *nvl; 421 nvlist_t *in_array[6]; 422 const nvlist_t * const *out_array; 423 const nvlist_t * const *out_result; 424 void *cookie; 425 const char *key; 426 const char *subkeys; 427 int type, i; 428 size_t nitems; 429 430 nvl = nvlist_create(0); 431 ATF_REQUIRE(nvl != NULL); 432 ATF_REQUIRE_EQ(nvlist_error(nvl), 0); 433 ATF_REQUIRE(nvlist_empty(nvl)); 434 435 subkeys = "123456"; 436 for (i = 0; i < 6; i++) { 437 in_array[i] = nvlist_create(0); 438 ATF_REQUIRE(in_array[i] != NULL); 439 ATF_REQUIRE_EQ(nvlist_error(in_array[i]), 0); 440 ATF_REQUIRE(nvlist_empty(in_array[i])); 441 442 cookie = NULL; 443 444 nvlist_add_null(in_array[i], subkeys+i); 445 ATF_REQUIRE_EQ(strcmp(subkeys+i, nvlist_next(in_array[i], 446 &type, &cookie)),0); 447 ATF_REQUIRE_EQ(nvlist_error(in_array[i]), 0); 448 ATF_REQUIRE_EQ(type, NV_TYPE_NULL); 449 ATF_REQUIRE(!nvlist_empty(in_array[i])); 450 ATF_REQUIRE(nvlist_exists(in_array[i], subkeys+i)); 451 ATF_REQUIRE(nvlist_exists_null(in_array[i], subkeys+i)); 452 ATF_REQUIRE_EQ(nvlist_next(in_array[i], &type, &cookie), 453 static_cast<const char *>(NULL)); 454 } 455 456 cookie = NULL; 457 key = "name"; 458 459 nvlist_add_nvlist_array(nvl, key, in_array, 6); 460 ATF_REQUIRE_EQ(strcmp(key, nvlist_next(nvl, &type, &cookie)), 0); 461 ATF_REQUIRE_EQ(nvlist_error(nvl), 0); 462 ATF_REQUIRE_EQ(type, NV_TYPE_NVLIST_ARRAY); 463 ATF_REQUIRE(!nvlist_empty(nvl)); 464 ATF_REQUIRE(nvlist_exists(nvl, key)); 465 ATF_REQUIRE(nvlist_exists_nvlist_array(nvl, key)); 466 467 /* Get nvlist array by cnvlist function. */ 468 out_array = cnvlist_get_nvlist_array(cookie, &nitems); 469 ATF_REQUIRE(out_array != NULL); 470 ATF_REQUIRE_EQ(nitems, 6); 471 ATF_REQUIRE_EQ(nvlist_next(nvl, &type, &cookie), 472 static_cast<const char *>(NULL)); 473 474 /* Get nvlist array by nvlist function. */ 475 out_result = nvlist_get_nvlist_array(nvl, key, &nitems); 476 ATF_REQUIRE(out_result != NULL); 477 ATF_REQUIRE_EQ(nitems, 6); 478 479 /* Validate assuming that nvlist returned a proper pointer */ 480 for (i = 0; i < 6; i++) { 481 ATF_REQUIRE_EQ(out_result[i], out_array[i]); 482 ATF_REQUIRE(out_array[i] != in_array[i]); 483 484 /* Validate data inside nvlist. */ 485 cookie = NULL; 486 ATF_REQUIRE_EQ(strcmp(subkeys+i, nvlist_next(out_array[i], 487 &type, &cookie)), 0); 488 ATF_REQUIRE_EQ(nvlist_error(out_array[i]), 0); 489 ATF_REQUIRE_EQ(type, NV_TYPE_NULL); 490 ATF_REQUIRE(!nvlist_empty(out_array[i])); 491 ATF_REQUIRE(nvlist_exists(out_array[i], subkeys+i)); 492 ATF_REQUIRE(nvlist_exists_null(out_array[i], subkeys+i)); 493 ATF_REQUIRE_EQ(nvlist_next(out_array[i], &type, &cookie), 494 static_cast<const char *>(NULL)); 495 } 496 497 nvlist_destroy(nvl); 498 } 499 500 ATF_TEST_CASE_WITHOUT_HEAD(cnvlist_get_descriptor_array); 501 ATF_TEST_CASE_BODY(cnvlist_get_descriptor_array) 502 { 503 nvlist_t *nvl; 504 size_t count, i, nitems; 505 const int *out_array; 506 int *in_array, type; 507 const char *key; 508 void *cookie; 509 510 nvl = nvlist_create(0); 511 ATF_REQUIRE(nvl != NULL); 512 ATF_REQUIRE_EQ(nvlist_error(nvl), 0); 513 ATF_REQUIRE(nvlist_empty(nvl)); 514 515 cookie = NULL; 516 key = "name"; 517 count = 50; 518 519 in_array = static_cast<int *>(malloc(sizeof(*in_array)*count)); 520 ATF_REQUIRE(in_array != NULL); 521 for (i = 0; i < count; i++) { 522 in_array[i] = dup(STDERR_FILENO); 523 ATF_REQUIRE(fd_is_valid(in_array[i])); 524 } 525 526 nvlist_add_descriptor_array(nvl, key, in_array, count); 527 ATF_REQUIRE_EQ(strcmp(key, nvlist_next(nvl, &type, &cookie)), 0); 528 ATF_REQUIRE_EQ(nvlist_error(nvl), 0); 529 ATF_REQUIRE_EQ(type, NV_TYPE_DESCRIPTOR_ARRAY); 530 ATF_REQUIRE(!nvlist_empty(nvl)); 531 ATF_REQUIRE(nvlist_exists(nvl, key)); 532 ATF_REQUIRE(nvlist_exists_descriptor_array(nvl, key)); 533 534 out_array = cnvlist_get_descriptor_array(cookie, &nitems); 535 ATF_REQUIRE_EQ(nitems, count); 536 ATF_REQUIRE(out_array != NULL); 537 for (i = 0; i < count; i++) 538 ATF_REQUIRE_EQ(fd_is_valid(out_array[i]), 1); 539 540 ATF_REQUIRE_EQ(nvlist_next(nvl, &type, &cookie), 541 static_cast<const char *>(NULL)); 542 543 nvlist_destroy(nvl); 544 } 545 546 /* ATF cnvlist_take tests. */ 547 548 ATF_TEST_CASE_WITHOUT_HEAD(cnvlist_take_bool); 549 ATF_TEST_CASE_BODY(cnvlist_take_bool) 550 { 551 nvlist_t *nvl; 552 const char *key; 553 bool value; 554 void *cookie; 555 int type; 556 557 nvl = nvlist_create(0); 558 ATF_REQUIRE(nvl != NULL); 559 ATF_REQUIRE_EQ(nvlist_error(nvl), 0); 560 ATF_REQUIRE(nvlist_empty(nvl)); 561 562 cookie = NULL; 563 key = "name"; 564 value = true; 565 566 nvlist_add_bool(nvl, key, value); 567 ATF_REQUIRE_EQ(strcmp(key, nvlist_next(nvl, &type, &cookie)), 0); 568 ATF_REQUIRE_EQ(nvlist_error(nvl), 0); 569 ATF_REQUIRE_EQ(type, NV_TYPE_BOOL); 570 ATF_REQUIRE(!nvlist_empty(nvl)); 571 ATF_REQUIRE(nvlist_exists(nvl, key)); 572 ATF_REQUIRE(nvlist_exists_bool(nvl, key)); 573 ATF_REQUIRE_EQ(nvlist_next(nvl, &type, &cookie), 574 static_cast<const char *>(NULL)); 575 576 cookie = NULL; 577 ATF_REQUIRE_EQ(strcmp(key, nvlist_next(nvl, &type, &cookie)), 0); 578 ATF_REQUIRE_EQ(cnvlist_take_bool(nvl, cookie), value); 579 580 cookie = NULL; 581 ATF_REQUIRE_EQ(nvlist_error(nvl), 0); 582 ATF_REQUIRE(nvlist_empty(nvl)); 583 ATF_REQUIRE(!nvlist_exists(nvl, key)); 584 ATF_REQUIRE(!nvlist_exists_bool(nvl, key)); 585 ATF_REQUIRE_EQ(nvlist_next(nvl, &type, &cookie), 586 static_cast<const char *>(NULL)); 587 588 nvlist_destroy(nvl); 589 } 590 591 ATF_TEST_CASE_WITHOUT_HEAD(cnvlist_take_number); 592 ATF_TEST_CASE_BODY(cnvlist_take_number) 593 { 594 nvlist_t *nvl; 595 const char *key; 596 uint64_t value; 597 void *cookie; 598 int type; 599 600 nvl = nvlist_create(0); 601 ATF_REQUIRE(nvl != NULL); 602 ATF_REQUIRE_EQ(nvlist_error(nvl), 0); 603 ATF_REQUIRE(nvlist_empty(nvl)); 604 605 cookie = NULL; 606 key = "name"; 607 value = 69; 608 609 nvlist_add_number(nvl, key, value); 610 ATF_REQUIRE_EQ(strcmp(key, nvlist_next(nvl, &type, &cookie)), 0); 611 ATF_REQUIRE_EQ(nvlist_error(nvl), 0); 612 ATF_REQUIRE_EQ(type, NV_TYPE_NUMBER); 613 ATF_REQUIRE(!nvlist_empty(nvl)); 614 ATF_REQUIRE(nvlist_exists(nvl, key)); 615 ATF_REQUIRE(nvlist_exists_number(nvl, key)); 616 ATF_REQUIRE_EQ(nvlist_next(nvl, &type, &cookie), 617 static_cast<const char *>(NULL)); 618 619 cookie = NULL; 620 ATF_REQUIRE_EQ(strcmp(key, nvlist_next(nvl, &type, &cookie)), 0); 621 ATF_REQUIRE_EQ(cnvlist_take_number(nvl, cookie), value); 622 623 cookie = NULL; 624 ATF_REQUIRE_EQ(nvlist_error(nvl), 0); 625 ATF_REQUIRE(nvlist_empty(nvl)); 626 ATF_REQUIRE(!nvlist_exists(nvl, key)); 627 ATF_REQUIRE(!nvlist_exists_number(nvl, key)); 628 ATF_REQUIRE_EQ(nvlist_next(nvl, &type, &cookie), 629 static_cast<const char *>(NULL)); 630 631 nvlist_destroy(nvl); 632 } 633 634 ATF_TEST_CASE_WITHOUT_HEAD(cnvlist_take_string); 635 ATF_TEST_CASE_BODY(cnvlist_take_string) 636 { 637 nvlist_t *nvl; 638 const char *key; 639 const char *value; 640 char *out_string; 641 void *cookie; 642 int type; 643 644 nvl = nvlist_create(0); 645 ATF_REQUIRE(nvl != NULL); 646 ATF_REQUIRE_EQ(nvlist_error(nvl), 0); 647 ATF_REQUIRE(nvlist_empty(nvl)); 648 649 cookie = NULL; 650 key = "name"; 651 value = "text"; 652 653 nvlist_add_string(nvl, key, value); 654 ATF_REQUIRE_EQ(strcmp(key, nvlist_next(nvl, &type, &cookie)), 0); 655 ATF_REQUIRE_EQ(nvlist_error(nvl), 0); 656 ATF_REQUIRE_EQ(type, NV_TYPE_STRING); 657 ATF_REQUIRE(!nvlist_empty(nvl)); 658 ATF_REQUIRE(nvlist_exists(nvl, key)); 659 ATF_REQUIRE(nvlist_exists_string(nvl, key)); 660 ATF_REQUIRE_EQ(nvlist_next(nvl, &type, &cookie), 661 static_cast<const char *>(NULL)); 662 663 cookie = NULL; 664 ATF_REQUIRE_EQ(strcmp(key, nvlist_next(nvl, &type, &cookie)), 0); 665 out_string = cnvlist_take_string(nvl, cookie); 666 ATF_REQUIRE(out_string != NULL); 667 ATF_REQUIRE_EQ(strcmp(out_string, value), 0); 668 669 cookie = NULL; 670 ATF_REQUIRE_EQ(nvlist_error(nvl), 0); 671 ATF_REQUIRE(nvlist_empty(nvl)); 672 ATF_REQUIRE(!nvlist_exists(nvl, key)); 673 ATF_REQUIRE(!nvlist_exists_string(nvl, key)); 674 ATF_REQUIRE_EQ(nvlist_next(nvl, &type, &cookie), 675 static_cast<const char *>(NULL)); 676 677 free(out_string); 678 nvlist_destroy(nvl); 679 } 680 681 ATF_TEST_CASE_WITHOUT_HEAD(cnvlist_take_nvlist); 682 ATF_TEST_CASE_BODY(cnvlist_take_nvlist) 683 { 684 nvlist_t *nvl, *value, *result; 685 const char *key, *subkey; 686 void *cookie; 687 int type; 688 689 nvl = nvlist_create(0); 690 ATF_REQUIRE(nvl != NULL); 691 ATF_REQUIRE_EQ(nvlist_error(nvl), 0); 692 ATF_REQUIRE(nvlist_empty(nvl)); 693 694 value = nvlist_create(0); 695 ATF_REQUIRE(value != NULL); 696 ATF_REQUIRE_EQ(nvlist_error(value), 0); 697 ATF_REQUIRE(nvlist_empty(value)); 698 699 key = "name"; 700 subkey = "subname"; 701 cookie = NULL; 702 703 /* Add null to 'value' nvlist. */ 704 nvlist_add_null(value, subkey); 705 ATF_REQUIRE_EQ(strcmp(subkey, nvlist_next(value, &type, &cookie)), 0); 706 ATF_REQUIRE_EQ(nvlist_error(value), 0); 707 ATF_REQUIRE_EQ(type, NV_TYPE_NULL); 708 ATF_REQUIRE(!nvlist_empty(value)); 709 ATF_REQUIRE(nvlist_exists(value, subkey)); 710 ATF_REQUIRE(nvlist_exists_null(value, subkey)); 711 ATF_REQUIRE_EQ(nvlist_next(value, &type, &cookie), 712 static_cast<const char *>(NULL)); 713 714 /* Add 'value' nvlist. */ 715 cookie = NULL; 716 nvlist_move_nvlist(nvl, key, value); 717 ATF_REQUIRE_EQ(strcmp(key, nvlist_next(nvl, &type, &cookie)), 0); 718 ATF_REQUIRE_EQ(nvlist_error(nvl), 0); 719 ATF_REQUIRE_EQ(type, NV_TYPE_NVLIST); 720 ATF_REQUIRE(!nvlist_empty(nvl)); 721 ATF_REQUIRE(nvlist_exists(nvl, key)); 722 ATF_REQUIRE(nvlist_exists_nvlist(nvl, key)); 723 ATF_REQUIRE_EQ(nvlist_next(nvl, &type, &cookie), 724 static_cast<const char *>(NULL)); 725 726 cookie = NULL; 727 ATF_REQUIRE_EQ(strcmp(key, nvlist_next(nvl, &type, &cookie)), 0); 728 result = cnvlist_take_nvlist(nvl, cookie); 729 ATF_REQUIRE(!nvlist_exists_nvlist(nvl, key)); 730 ATF_REQUIRE(result == value); 731 732 /* Validate data inside nvlist. */ 733 cookie = NULL; 734 ATF_REQUIRE_EQ(strcmp(subkey, nvlist_next(result, &type, &cookie)), 0); 735 ATF_REQUIRE_EQ(nvlist_error(value), 0); 736 ATF_REQUIRE_EQ(type, NV_TYPE_NULL); 737 ATF_REQUIRE(!nvlist_empty(value)); 738 ATF_REQUIRE(nvlist_exists(value, subkey)); 739 ATF_REQUIRE(nvlist_exists_null(value, subkey)); 740 ATF_REQUIRE_EQ(nvlist_next(value, &type, &cookie), 741 static_cast<const char *>(NULL)); 742 743 cookie = NULL; 744 ATF_REQUIRE_EQ(nvlist_next(nvl, &type, &cookie), 745 static_cast<const char *>(NULL)); 746 747 nvlist_destroy(nvl); 748 nvlist_destroy(value); 749 } 750 751 /* ATF cnvlist_take array tests */ 752 753 ATF_TEST_CASE_WITHOUT_HEAD(cnvlist_take_bool_array); 754 ATF_TEST_CASE_BODY(cnvlist_take_bool_array) 755 { 756 nvlist_t *nvl; 757 bool in_array[16]; 758 const bool *out_array; 759 const char *key; 760 void *cookie; 761 int type, i; 762 size_t nitems; 763 764 for (i = 0; i < 16; i++) 765 in_array[i] = (i % 2 == 0); 766 767 nvl = nvlist_create(0); 768 ATF_REQUIRE(nvl != NULL); 769 ATF_REQUIRE_EQ(nvlist_error(nvl), 0); 770 ATF_REQUIRE(nvlist_empty(nvl)); 771 772 cookie = NULL; 773 key = "name"; 774 775 nvlist_add_bool_array(nvl, key, in_array, 16); 776 ATF_REQUIRE_EQ(strcmp(key, nvlist_next(nvl, &type, &cookie)), 0); 777 ATF_REQUIRE_EQ(nvlist_error(nvl), 0); 778 ATF_REQUIRE_EQ(type, NV_TYPE_BOOL_ARRAY); 779 ATF_REQUIRE(!nvlist_empty(nvl)); 780 ATF_REQUIRE(nvlist_exists(nvl, key)); 781 ATF_REQUIRE(nvlist_exists_bool_array(nvl, key)); 782 ATF_REQUIRE_EQ(nvlist_next(nvl, &type, &cookie), 783 static_cast<const char *>(NULL)); 784 785 cookie = NULL; 786 ATF_REQUIRE_EQ(strcmp(key, nvlist_next(nvl, &type, &cookie)), 0); 787 out_array = cnvlist_take_bool_array(nvl, cookie, &nitems); 788 ATF_REQUIRE_EQ(nitems, 16); 789 ATF_REQUIRE(out_array != NULL); 790 for (i = 0; i < 16; i++) 791 ATF_REQUIRE_EQ(out_array[i], in_array[i]); 792 793 cookie = NULL; 794 ATF_REQUIRE(!nvlist_exists_bool_array(nvl, key)); 795 ATF_REQUIRE_EQ(nvlist_error(nvl), 0); 796 ATF_REQUIRE(nvlist_empty(nvl)); 797 ATF_REQUIRE(!nvlist_exists(nvl, key)); 798 ATF_REQUIRE_EQ(nvlist_next(nvl, &type, &cookie), 799 static_cast<const char *>(NULL)); 800 801 802 nvlist_destroy(nvl); 803 } 804 805 ATF_TEST_CASE_WITHOUT_HEAD(cnvlist_take_number_array); 806 ATF_TEST_CASE_BODY(cnvlist_take_number_array) 807 { 808 nvlist_t *nvl; 809 uint64_t in_array[16]; 810 const uint64_t *out_array; 811 const char *key; 812 void *cookie; 813 int type, i; 814 size_t nitems; 815 816 for (i = 0; i < 16; i++) 817 in_array[i] = i; 818 819 nvl = nvlist_create(0); 820 ATF_REQUIRE(nvl != NULL); 821 ATF_REQUIRE_EQ(nvlist_error(nvl), 0); 822 ATF_REQUIRE(nvlist_empty(nvl)); 823 824 cookie = NULL; 825 key = "name"; 826 827 nvlist_add_number_array(nvl, key, in_array, 16); 828 ATF_REQUIRE_EQ(strcmp(key, nvlist_next(nvl, &type, &cookie)), 0); 829 ATF_REQUIRE_EQ(nvlist_error(nvl), 0); 830 ATF_REQUIRE_EQ(type, NV_TYPE_NUMBER_ARRAY); 831 ATF_REQUIRE(!nvlist_empty(nvl)); 832 ATF_REQUIRE(nvlist_exists(nvl, key)); 833 ATF_REQUIRE(nvlist_exists_number_array(nvl, key)); 834 ATF_REQUIRE_EQ(nvlist_next(nvl, &type, &cookie), 835 static_cast<const char *>(NULL)); 836 837 cookie = NULL; 838 ATF_REQUIRE_EQ(strcmp(key, nvlist_next(nvl, &type, &cookie)), 0); 839 out_array = cnvlist_take_number_array(nvl, cookie, &nitems); 840 841 ATF_REQUIRE(out_array != NULL); 842 ATF_REQUIRE_EQ(nitems, 16); 843 for (i = 0; i < 16; i++) 844 ATF_REQUIRE_EQ(out_array[i], in_array[i]); 845 846 cookie = NULL; 847 ATF_REQUIRE_EQ(nvlist_error(nvl), 0); 848 ATF_REQUIRE(nvlist_empty(nvl)); 849 ATF_REQUIRE(!nvlist_exists(nvl, key)); 850 ATF_REQUIRE(!nvlist_exists_number_array(nvl, key)); 851 ATF_REQUIRE_EQ(nvlist_next(nvl, &type, &cookie), 852 static_cast<const char *>(NULL)); 853 854 nvlist_destroy(nvl); 855 } 856 857 ATF_TEST_CASE_WITHOUT_HEAD(cnvlist_take_string_array); 858 ATF_TEST_CASE_BODY(cnvlist_take_string_array) 859 { 860 nvlist_t *nvl; 861 const char *in_array[4] = {"inequality", "sks", ".", ""}; 862 char **out_array; 863 const char *key; 864 void *cookie; 865 int type, i; 866 size_t nitems; 867 868 nvl = nvlist_create(0); 869 ATF_REQUIRE(nvl != NULL); 870 ATF_REQUIRE_EQ(nvlist_error(nvl), 0); 871 ATF_REQUIRE(nvlist_empty(nvl)); 872 873 cookie = NULL; 874 key = "name"; 875 876 nvlist_add_string_array(nvl, key, in_array, 4); 877 ATF_REQUIRE_EQ(strcmp(key, nvlist_next(nvl, &type, &cookie)), 0); 878 ATF_REQUIRE_EQ(nvlist_error(nvl), 0); 879 ATF_REQUIRE_EQ(type, NV_TYPE_STRING_ARRAY); 880 ATF_REQUIRE(!nvlist_empty(nvl)); 881 ATF_REQUIRE(nvlist_exists(nvl, key)); 882 ATF_REQUIRE(nvlist_exists_string_array(nvl, key)); 883 ATF_REQUIRE_EQ(nvlist_next(nvl, &type, &cookie), 884 static_cast<const char *>(NULL)); 885 886 cookie = NULL; 887 ATF_REQUIRE_EQ(strcmp(key, nvlist_next(nvl, &type, &cookie)), 0); 888 out_array = cnvlist_take_string_array(nvl, cookie, &nitems); 889 ATF_REQUIRE_EQ(nitems, 4); 890 for (i = 0; i < 4; i++) { 891 ATF_REQUIRE(out_array[i] != NULL); 892 ATF_REQUIRE_EQ(strcmp(out_array[i], in_array[i]), 0); 893 } 894 ATF_REQUIRE(nvlist_empty(nvl)); 895 896 cookie = NULL; 897 ATF_REQUIRE_EQ(nvlist_error(nvl), 0); 898 ATF_REQUIRE(nvlist_empty(nvl)); 899 ATF_REQUIRE(!nvlist_exists(nvl, key)); 900 ATF_REQUIRE(!nvlist_exists_number_array(nvl, key)); 901 ATF_REQUIRE_EQ(nvlist_next(nvl, &type, &cookie), 902 static_cast<const char *>(NULL)); 903 904 free(out_array); 905 nvlist_destroy(nvl); 906 } 907 908 ATF_TEST_CASE_WITHOUT_HEAD(cnvlist_take_nvlist_array); 909 ATF_TEST_CASE_BODY(cnvlist_take_nvlist_array) 910 { 911 nvlist_t *testnvl[8]; 912 nvlist_t **result; 913 nvlist_t *nvl; 914 void *cookie; 915 size_t num_items; 916 unsigned int i; 917 int type; 918 const char *somestr[8] = { "a", "b", "c", "d", "e", "f", "g", "h" }; 919 const char *key; 920 921 for (i = 0; i < 8; i++) { 922 testnvl[i] = nvlist_create(0); 923 ATF_REQUIRE(testnvl[i] != NULL); 924 ATF_REQUIRE_EQ(nvlist_error(testnvl[i]), 0); 925 ATF_REQUIRE(nvlist_empty(testnvl[i])); 926 nvlist_add_string(testnvl[i], "nvl/string", somestr[i]); 927 928 cookie = NULL; 929 ATF_REQUIRE_EQ(strcmp("nvl/string", nvlist_next(testnvl[i], 930 &type, &cookie)), 0); 931 ATF_REQUIRE_EQ(nvlist_error(testnvl[i]), 0); 932 ATF_REQUIRE_EQ(type, NV_TYPE_STRING); 933 ATF_REQUIRE(!nvlist_empty(testnvl[i])); 934 ATF_REQUIRE(nvlist_exists(testnvl[i], "nvl/string")); 935 ATF_REQUIRE(nvlist_exists_string(testnvl[i], "nvl/string")); 936 ATF_REQUIRE_EQ(nvlist_next(testnvl[i], &type, &cookie), 937 static_cast<const char *>(NULL)); 938 } 939 940 nvl = nvlist_create(0); 941 ATF_REQUIRE(nvl != NULL); 942 ATF_REQUIRE_EQ(nvlist_error(nvl), 0); 943 ATF_REQUIRE(nvlist_empty(nvl)); 944 945 key = "nvl/nvlist"; 946 cookie = NULL; 947 948 nvlist_add_nvlist_array(nvl, key, (const nvlist_t * const *)testnvl, 8); 949 ATF_REQUIRE_EQ(strcmp(key, nvlist_next(nvl, &type, &cookie)), 0); 950 ATF_REQUIRE_EQ(nvlist_error(nvl), 0); 951 ATF_REQUIRE_EQ(type, NV_TYPE_NVLIST_ARRAY); 952 ATF_REQUIRE(!nvlist_empty(nvl)); 953 ATF_REQUIRE(nvlist_exists(nvl, key)); 954 ATF_REQUIRE(nvlist_exists_nvlist_array(nvl, key)); 955 ATF_REQUIRE_EQ(nvlist_next(nvl, &type, &cookie), 956 static_cast<const char *>(NULL)); 957 958 cookie = NULL; 959 ATF_REQUIRE_EQ(strcmp(key, nvlist_next(nvl, &type, &cookie)), 0); 960 result = cnvlist_take_nvlist_array(nvl, cookie, &num_items); 961 962 ATF_REQUIRE(result != NULL); 963 ATF_REQUIRE_EQ(num_items, 8); 964 for (i = 0; i < num_items; i++) { 965 ATF_REQUIRE_EQ(nvlist_error(result[i]), 0); 966 ATF_REQUIRE(nvlist_get_array_next(result[i]) == NULL); 967 } 968 969 ATF_REQUIRE(!nvlist_exists_string_array(nvl, key)); 970 ATF_REQUIRE(nvlist_empty(nvl)); 971 ATF_REQUIRE_EQ(nvlist_error(nvl), 0); 972 973 cookie = NULL; 974 ATF_REQUIRE_EQ(nvlist_error(nvl), 0); 975 ATF_REQUIRE(nvlist_empty(nvl)); 976 ATF_REQUIRE(!nvlist_exists(nvl, key)); 977 ATF_REQUIRE(!nvlist_exists_nvlist_array(nvl, key)); 978 ATF_REQUIRE_EQ(nvlist_next(nvl, &type, &cookie), 979 static_cast<const char *>(NULL)); 980 981 for (i = 0; i < 8; i++) { 982 nvlist_destroy(result[i]); 983 nvlist_destroy(testnvl[i]); 984 } 985 986 free(result); 987 nvlist_destroy(nvl); 988 } 989 990 ATF_TEST_CASE_WITHOUT_HEAD(cnvlist_take_binary); 991 ATF_TEST_CASE_BODY(cnvlist_take_binary) 992 { 993 nvlist_t *nvl; 994 const char *key; 995 void *in_binary; 996 const void *out_binary; 997 void *cookie; 998 int type; 999 size_t in_size, out_size; 1000 1001 nvl = nvlist_create(0); 1002 ATF_REQUIRE(nvl != NULL); 1003 ATF_REQUIRE_EQ(nvlist_error(nvl), 0); 1004 ATF_REQUIRE(nvlist_empty(nvl)); 1005 1006 cookie = NULL; 1007 key = "name"; 1008 in_size = 13; 1009 in_binary = malloc(in_size); 1010 ATF_REQUIRE(in_binary != NULL); 1011 memset(in_binary, 0xa5, in_size); 1012 1013 nvlist_add_binary(nvl, key, in_binary, in_size); 1014 ATF_REQUIRE_EQ(strcmp(key, nvlist_next(nvl, &type, &cookie)), 0); 1015 ATF_REQUIRE_EQ(nvlist_error(nvl), 0); 1016 ATF_REQUIRE_EQ(type, NV_TYPE_BINARY); 1017 ATF_REQUIRE(!nvlist_empty(nvl)); 1018 ATF_REQUIRE(nvlist_exists(nvl, key)); 1019 ATF_REQUIRE(nvlist_exists_binary(nvl, key)); 1020 ATF_REQUIRE_EQ(nvlist_next(nvl, &type, &cookie), 1021 static_cast<const char *>(NULL)); 1022 1023 cookie = NULL; 1024 ATF_REQUIRE_EQ(strcmp(key, nvlist_next(nvl, &type, &cookie)), 0); 1025 out_binary = cnvlist_take_binary(nvl, cookie, &out_size); 1026 ATF_REQUIRE_EQ(out_size, in_size); 1027 ATF_REQUIRE_EQ(memcmp(in_binary, out_binary, out_size), 0); 1028 1029 cookie = NULL; 1030 ATF_REQUIRE_EQ(nvlist_error(nvl), 0); 1031 ATF_REQUIRE(nvlist_empty(nvl)); 1032 ATF_REQUIRE(!nvlist_exists(nvl, key)); 1033 ATF_REQUIRE(!nvlist_exists_binary(nvl, key)); 1034 ATF_REQUIRE_EQ(nvlist_next(nvl, &type, &cookie), 1035 static_cast<const char *>(NULL)); 1036 1037 nvlist_destroy(nvl); 1038 } 1039 1040 /* ATF cnvlist_free tests. */ 1041 1042 ATF_TEST_CASE_WITHOUT_HEAD(cnvlist_free_bool); 1043 ATF_TEST_CASE_BODY(cnvlist_free_bool) 1044 { 1045 nvlist_t *nvl; 1046 const char *key; 1047 bool value; 1048 void *cookie; 1049 int type; 1050 1051 nvl = nvlist_create(0); 1052 ATF_REQUIRE(nvl != NULL); 1053 ATF_REQUIRE_EQ(nvlist_error(nvl), 0); 1054 ATF_REQUIRE(nvlist_empty(nvl)); 1055 1056 cookie = NULL; 1057 key = "name"; 1058 value = true; 1059 1060 nvlist_add_bool(nvl, key, value); 1061 ATF_REQUIRE_EQ(strcmp(key, nvlist_next(nvl, &type, &cookie)), 0); 1062 ATF_REQUIRE_EQ(nvlist_error(nvl), 0); 1063 ATF_REQUIRE_EQ(type, NV_TYPE_BOOL); 1064 ATF_REQUIRE(!nvlist_empty(nvl)); 1065 ATF_REQUIRE(nvlist_exists(nvl, key)); 1066 ATF_REQUIRE(nvlist_exists_bool(nvl, key)); 1067 ATF_REQUIRE_EQ(nvlist_next(nvl, &type, &cookie), 1068 static_cast<const char *>(NULL)); 1069 1070 cookie = NULL; 1071 ATF_REQUIRE_EQ(strcmp(key, nvlist_next(nvl, &type, &cookie)), 0); 1072 cnvlist_free_bool(nvl, cookie); 1073 1074 cookie = NULL; 1075 ATF_REQUIRE_EQ(nvlist_error(nvl), 0); 1076 ATF_REQUIRE(nvlist_empty(nvl)); 1077 ATF_REQUIRE(!nvlist_exists(nvl, key)); 1078 ATF_REQUIRE(!nvlist_exists_bool(nvl, key)); 1079 ATF_REQUIRE_EQ(nvlist_next(nvl, &type, &cookie), 1080 static_cast<const char *>(NULL)); 1081 1082 nvlist_destroy(nvl); 1083 } 1084 1085 ATF_TEST_CASE_WITHOUT_HEAD(cnvlist_free_number); 1086 ATF_TEST_CASE_BODY(cnvlist_free_number) 1087 { 1088 nvlist_t *nvl; 1089 const char *key; 1090 uint64_t value; 1091 void *cookie; 1092 int type; 1093 1094 nvl = nvlist_create(0); 1095 ATF_REQUIRE(nvl != NULL); 1096 ATF_REQUIRE_EQ(nvlist_error(nvl), 0); 1097 ATF_REQUIRE(nvlist_empty(nvl)); 1098 1099 cookie = NULL; 1100 key = "name"; 1101 value = 69; 1102 1103 nvlist_add_number(nvl, key, value); 1104 ATF_REQUIRE_EQ(strcmp(key, nvlist_next(nvl, &type, &cookie)), 0); 1105 ATF_REQUIRE_EQ(nvlist_error(nvl), 0); 1106 ATF_REQUIRE_EQ(type, NV_TYPE_NUMBER); 1107 ATF_REQUIRE(!nvlist_empty(nvl)); 1108 ATF_REQUIRE(nvlist_exists(nvl, key)); 1109 ATF_REQUIRE(nvlist_exists_number(nvl, key)); 1110 ATF_REQUIRE_EQ(nvlist_next(nvl, &type, &cookie), 1111 static_cast<const char *>(NULL)); 1112 1113 cookie = NULL; 1114 ATF_REQUIRE_EQ(strcmp(key, nvlist_next(nvl, &type, &cookie)), 0); 1115 cnvlist_free_number(nvl, cookie); 1116 1117 cookie = NULL; 1118 ATF_REQUIRE_EQ(nvlist_error(nvl), 0); 1119 ATF_REQUIRE(nvlist_empty(nvl)); 1120 ATF_REQUIRE(!nvlist_exists(nvl, key)); 1121 ATF_REQUIRE(!nvlist_exists_number(nvl, key)); 1122 ATF_REQUIRE_EQ(nvlist_next(nvl, &type, &cookie), 1123 static_cast<const char *>(NULL)); 1124 1125 nvlist_destroy(nvl); 1126 } 1127 1128 ATF_TEST_CASE_WITHOUT_HEAD(cnvlist_free_string); 1129 ATF_TEST_CASE_BODY(cnvlist_free_string) 1130 { 1131 nvlist_t *nvl; 1132 const char *key; 1133 const char *value; 1134 void *cookie; 1135 int type; 1136 1137 nvl = nvlist_create(0); 1138 ATF_REQUIRE(nvl != NULL); 1139 ATF_REQUIRE_EQ(nvlist_error(nvl), 0); 1140 ATF_REQUIRE(nvlist_empty(nvl)); 1141 1142 cookie = NULL; 1143 key = "name"; 1144 value = "text"; 1145 1146 nvlist_add_string(nvl, key, value); 1147 ATF_REQUIRE_EQ(strcmp(key, nvlist_next(nvl, &type, &cookie)), 0); 1148 ATF_REQUIRE_EQ(nvlist_error(nvl), 0); 1149 ATF_REQUIRE_EQ(type, NV_TYPE_STRING); 1150 ATF_REQUIRE(!nvlist_empty(nvl)); 1151 ATF_REQUIRE(nvlist_exists(nvl, key)); 1152 ATF_REQUIRE(nvlist_exists_string(nvl, key)); 1153 ATF_REQUIRE_EQ(nvlist_next(nvl, &type, &cookie), 1154 static_cast<const char *>(NULL)); 1155 1156 cookie = NULL; 1157 ATF_REQUIRE_EQ(strcmp(key, nvlist_next(nvl, &type, &cookie)), 0); 1158 cnvlist_free_string(nvl, cookie); 1159 1160 cookie = NULL; 1161 ATF_REQUIRE_EQ(nvlist_error(nvl), 0); 1162 ATF_REQUIRE(nvlist_empty(nvl)); 1163 ATF_REQUIRE(!nvlist_exists(nvl, key)); 1164 ATF_REQUIRE(!nvlist_exists_string(nvl, key)); 1165 ATF_REQUIRE_EQ(nvlist_next(nvl, &type, &cookie), 1166 static_cast<const char *>(NULL)); 1167 1168 nvlist_destroy(nvl); 1169 } 1170 1171 ATF_TEST_CASE_WITHOUT_HEAD(cnvlist_free_nvlist); 1172 ATF_TEST_CASE_BODY(cnvlist_free_nvlist) 1173 { 1174 nvlist_t *nvl, *value; 1175 const char *key, *subkey; 1176 void *cookie; 1177 int type; 1178 1179 nvl = nvlist_create(0); 1180 ATF_REQUIRE(nvl != NULL); 1181 ATF_REQUIRE_EQ(nvlist_error(nvl), 0); 1182 ATF_REQUIRE(nvlist_empty(nvl)); 1183 1184 value = nvlist_create(0); 1185 ATF_REQUIRE(nvl != NULL); 1186 ATF_REQUIRE_EQ(nvlist_error(nvl), 0); 1187 ATF_REQUIRE(nvlist_empty(nvl)); 1188 1189 key = "name"; 1190 subkey = "subname"; 1191 cookie = NULL; 1192 1193 /* Add null to 'value' nvlist. */ 1194 nvlist_add_null(value, subkey); 1195 ATF_REQUIRE_EQ(strcmp(subkey, nvlist_next(value, &type, &cookie)), 0); 1196 ATF_REQUIRE_EQ(nvlist_error(value), 0); 1197 ATF_REQUIRE_EQ(type, NV_TYPE_NULL); 1198 ATF_REQUIRE(!nvlist_empty(value)); 1199 ATF_REQUIRE(nvlist_exists(value, subkey)); 1200 ATF_REQUIRE(nvlist_exists_null(value, subkey)); 1201 ATF_REQUIRE_EQ(nvlist_next(value, &type, &cookie), 1202 static_cast<const char *>(NULL)); 1203 1204 /* Add 'value' nvlist. */ 1205 cookie = NULL; 1206 nvlist_move_nvlist(nvl, key, value); 1207 ATF_REQUIRE_EQ(strcmp(key, nvlist_next(nvl, &type, &cookie)), 0); 1208 ATF_REQUIRE_EQ(nvlist_error(nvl), 0); 1209 ATF_REQUIRE_EQ(type, NV_TYPE_NVLIST); 1210 ATF_REQUIRE(!nvlist_empty(nvl)); 1211 ATF_REQUIRE(nvlist_exists(nvl, key)); 1212 ATF_REQUIRE(nvlist_exists_nvlist(nvl, key)); 1213 ATF_REQUIRE_EQ(nvlist_next(nvl, &type, &cookie), 1214 static_cast<const char *>(NULL)); 1215 1216 cookie = NULL; 1217 ATF_REQUIRE_EQ(strcmp(key, nvlist_next(nvl, &type, &cookie)), 0); 1218 cnvlist_free_nvlist(nvl, cookie); 1219 1220 cookie = NULL; 1221 ATF_REQUIRE_EQ(nvlist_error(nvl), 0); 1222 ATF_REQUIRE(nvlist_empty(nvl)); 1223 ATF_REQUIRE(!nvlist_exists(nvl, key)); 1224 ATF_REQUIRE(!nvlist_exists_nvlist(nvl, key)); 1225 ATF_REQUIRE_EQ(nvlist_next(nvl, &type, &cookie), 1226 static_cast<const char *>(NULL)); 1227 1228 nvlist_destroy(nvl); 1229 } 1230 1231 ATF_TEST_CASE_WITHOUT_HEAD(cnvlist_free_binary); 1232 ATF_TEST_CASE_BODY(cnvlist_free_binary) 1233 { 1234 nvlist_t *nvl; 1235 const char *key; 1236 void *in_binary; 1237 void *cookie; 1238 int type; 1239 size_t in_size; 1240 1241 nvl = nvlist_create(0); 1242 ATF_REQUIRE(nvl != NULL); 1243 ATF_REQUIRE_EQ(nvlist_error(nvl), 0); 1244 ATF_REQUIRE(nvlist_empty(nvl)); 1245 1246 cookie = NULL; 1247 key = "name"; 1248 in_size = 13; 1249 in_binary = malloc(in_size); 1250 ATF_REQUIRE(in_binary != NULL); 1251 memset(in_binary, 0xa5, in_size); 1252 1253 nvlist_add_binary(nvl, key, in_binary, in_size); 1254 ATF_REQUIRE_EQ(strcmp(key, nvlist_next(nvl, &type, &cookie)), 0); 1255 ATF_REQUIRE_EQ(nvlist_error(nvl), 0); 1256 ATF_REQUIRE_EQ(type, NV_TYPE_BINARY); 1257 ATF_REQUIRE(!nvlist_empty(nvl)); 1258 ATF_REQUIRE(nvlist_exists(nvl, key)); 1259 ATF_REQUIRE(nvlist_exists_binary(nvl, key)); 1260 ATF_REQUIRE_EQ(nvlist_next(nvl, &type, &cookie), 1261 static_cast<const char *>(NULL)); 1262 1263 cookie = NULL; 1264 ATF_REQUIRE_EQ(strcmp(key, nvlist_next(nvl, &type, &cookie)), 0); 1265 cnvlist_free_binary(nvl, cookie); 1266 1267 cookie = NULL; 1268 ATF_REQUIRE_EQ(nvlist_error(nvl), 0); 1269 ATF_REQUIRE(nvlist_empty(nvl)); 1270 ATF_REQUIRE(!nvlist_exists(nvl, key)); 1271 ATF_REQUIRE(!nvlist_exists_nvlist(nvl, key)); 1272 ATF_REQUIRE_EQ(nvlist_next(nvl, &type, &cookie), 1273 static_cast<const char *>(NULL)); 1274 1275 nvlist_destroy(nvl); 1276 } 1277 1278 /* ATF cnvlist_free array tests. */ 1279 1280 ATF_TEST_CASE_WITHOUT_HEAD(cnvlist_free_bool_array); 1281 ATF_TEST_CASE_BODY(cnvlist_free_bool_array) 1282 { 1283 nvlist_t *nvl; 1284 bool in_array[16]; 1285 const char *key; 1286 void *cookie; 1287 int type, i; 1288 1289 for (i = 0; i < 16; i++) 1290 in_array[i] = (i % 2 == 0); 1291 1292 nvl = nvlist_create(0); 1293 ATF_REQUIRE(nvl != NULL); 1294 ATF_REQUIRE_EQ(nvlist_error(nvl), 0); 1295 ATF_REQUIRE(nvlist_empty(nvl)); 1296 1297 cookie = NULL; 1298 key = "name"; 1299 1300 nvlist_add_bool_array(nvl, key, in_array, 16); 1301 ATF_REQUIRE_EQ(strcmp(key, nvlist_next(nvl, &type, &cookie)), 0); 1302 ATF_REQUIRE_EQ(nvlist_error(nvl), 0); 1303 ATF_REQUIRE_EQ(type, NV_TYPE_BOOL_ARRAY); 1304 ATF_REQUIRE(!nvlist_empty(nvl)); 1305 ATF_REQUIRE(nvlist_exists(nvl, key)); 1306 ATF_REQUIRE(nvlist_exists_bool_array(nvl, key)); 1307 ATF_REQUIRE_EQ(nvlist_next(nvl, &type, &cookie), 1308 static_cast<const char *>(NULL)); 1309 1310 cookie = NULL; 1311 ATF_REQUIRE_EQ(strcmp(key, nvlist_next(nvl, &type, &cookie)), 0); 1312 cnvlist_free_bool_array(nvl, cookie); 1313 1314 cookie = NULL; 1315 ATF_REQUIRE_EQ(nvlist_error(nvl), 0); 1316 ATF_REQUIRE(nvlist_empty(nvl)); 1317 ATF_REQUIRE(!nvlist_exists(nvl, key)); 1318 ATF_REQUIRE(!nvlist_exists_bool(nvl, key)); 1319 ATF_REQUIRE_EQ(nvlist_next(nvl, &type, &cookie), 1320 static_cast<const char *>(NULL)); 1321 1322 nvlist_destroy(nvl); 1323 } 1324 1325 ATF_TEST_CASE_WITHOUT_HEAD(cnvlist_free_number_array); 1326 ATF_TEST_CASE_BODY(cnvlist_free_number_array) 1327 { 1328 nvlist_t *nvl; 1329 uint64_t in_array[16]; 1330 const char *key; 1331 void *cookie; 1332 int type, i; 1333 1334 for (i = 0; i < 16; i++) 1335 in_array[i] = i; 1336 1337 nvl = nvlist_create(0); 1338 ATF_REQUIRE(nvl != NULL); 1339 ATF_REQUIRE_EQ(nvlist_error(nvl), 0); 1340 ATF_REQUIRE(nvlist_empty(nvl)); 1341 1342 cookie = NULL; 1343 key = "name"; 1344 1345 nvlist_add_number_array(nvl, key, in_array, 16); 1346 ATF_REQUIRE_EQ(strcmp(key, nvlist_next(nvl, &type, &cookie)), 0); 1347 ATF_REQUIRE_EQ(nvlist_error(nvl), 0); 1348 ATF_REQUIRE_EQ(type, NV_TYPE_NUMBER_ARRAY); 1349 ATF_REQUIRE(!nvlist_empty(nvl)); 1350 ATF_REQUIRE(nvlist_exists(nvl, key)); 1351 ATF_REQUIRE(nvlist_exists_number_array(nvl, key)); 1352 ATF_REQUIRE_EQ(nvlist_next(nvl, &type, &cookie), 1353 static_cast<const char *>(NULL)); 1354 1355 cookie = NULL; 1356 ATF_REQUIRE_EQ(strcmp(key, nvlist_next(nvl, &type, &cookie)), 0); 1357 cnvlist_free_number_array(nvl, cookie); 1358 1359 cookie = NULL; 1360 ATF_REQUIRE_EQ(nvlist_error(nvl), 0); 1361 ATF_REQUIRE(nvlist_empty(nvl)); 1362 ATF_REQUIRE(!nvlist_exists(nvl, key)); 1363 ATF_REQUIRE(!nvlist_exists_number_array(nvl, key)); 1364 ATF_REQUIRE_EQ(nvlist_next(nvl, &type, &cookie), 1365 static_cast<const char *>(NULL)); 1366 1367 nvlist_destroy(nvl); 1368 } 1369 1370 ATF_TEST_CASE_WITHOUT_HEAD(cnvlist_free_string_array); 1371 ATF_TEST_CASE_BODY(cnvlist_free_string_array) 1372 { 1373 nvlist_t *nvl; 1374 const char *in_array[4] = {"inequality", "sucks", ".", ""}; 1375 const char *key; 1376 void *cookie; 1377 int type; 1378 1379 nvl = nvlist_create(0); 1380 ATF_REQUIRE(nvl != NULL); 1381 ATF_REQUIRE_EQ(nvlist_error(nvl), 0); 1382 ATF_REQUIRE(nvlist_empty(nvl)); 1383 1384 cookie = NULL; 1385 key = "name"; 1386 1387 nvlist_add_string_array(nvl, key, in_array, 4); 1388 ATF_REQUIRE_EQ(strcmp(key, nvlist_next(nvl, &type, &cookie)), 0); 1389 ATF_REQUIRE_EQ(nvlist_error(nvl), 0); 1390 ATF_REQUIRE_EQ(type, NV_TYPE_STRING_ARRAY); 1391 ATF_REQUIRE(!nvlist_empty(nvl)); 1392 ATF_REQUIRE(nvlist_exists(nvl, key)); 1393 ATF_REQUIRE(nvlist_exists_string_array(nvl, key)); 1394 ATF_REQUIRE_EQ(nvlist_next(nvl, &type, &cookie), 1395 static_cast<const char *>(NULL)); 1396 1397 cookie = NULL; 1398 ATF_REQUIRE_EQ(strcmp(key, nvlist_next(nvl, &type, &cookie)), 0); 1399 cnvlist_free_string_array(nvl, cookie); 1400 1401 cookie = NULL; 1402 ATF_REQUIRE_EQ(nvlist_error(nvl), 0); 1403 ATF_REQUIRE(nvlist_empty(nvl)); 1404 ATF_REQUIRE(!nvlist_exists(nvl, key)); 1405 ATF_REQUIRE(!nvlist_exists_string_array(nvl, key)); 1406 ATF_REQUIRE_EQ(nvlist_next(nvl, &type, &cookie), 1407 static_cast<const char *>(NULL)); 1408 1409 nvlist_destroy(nvl); 1410 } 1411 1412 ATF_TEST_CASE_WITHOUT_HEAD(cnvlist_free_nvlist_array); 1413 ATF_TEST_CASE_BODY(cnvlist_free_nvlist_array) 1414 { 1415 nvlist_t *testnvl[8]; 1416 nvlist_t *nvl; 1417 void *cookie; 1418 unsigned int i; 1419 int type; 1420 const char *somestr[8] = { "a", "b", "c", "d", "e", "f", "g", "h" }; 1421 const char *key; 1422 1423 for (i = 0; i < 8; i++) { 1424 testnvl[i] = nvlist_create(0); 1425 ATF_REQUIRE(testnvl[i] != NULL); 1426 ATF_REQUIRE_EQ(nvlist_error(testnvl[i]), 0); 1427 ATF_REQUIRE(nvlist_empty(testnvl[i])); 1428 nvlist_add_string(testnvl[i], "nvl/string", somestr[i]); 1429 1430 cookie = NULL; 1431 ATF_REQUIRE_EQ(strcmp("nvl/string", nvlist_next(testnvl[i], 1432 &type, &cookie)), 0); 1433 ATF_REQUIRE_EQ(nvlist_error(testnvl[i]), 0); 1434 ATF_REQUIRE_EQ(type, NV_TYPE_STRING); 1435 ATF_REQUIRE(!nvlist_empty(testnvl[i])); 1436 ATF_REQUIRE(nvlist_exists(testnvl[i], "nvl/string")); 1437 ATF_REQUIRE(nvlist_exists_string(testnvl[i], "nvl/string")); 1438 ATF_REQUIRE_EQ(nvlist_next(testnvl[i], &type, &cookie), 1439 static_cast<const char *>(NULL)); 1440 } 1441 1442 nvl = nvlist_create(0); 1443 ATF_REQUIRE(nvl != NULL); 1444 ATF_REQUIRE_EQ(nvlist_error(nvl), 0); 1445 ATF_REQUIRE(nvlist_empty(nvl)); 1446 1447 key = "nvl/nvlist"; 1448 cookie = NULL; 1449 1450 nvlist_add_nvlist_array(nvl, key, (const nvlist_t * const *)testnvl, 8); 1451 ATF_REQUIRE_EQ(strcmp(key, nvlist_next(nvl, &type, &cookie)), 0); 1452 ATF_REQUIRE_EQ(nvlist_error(nvl), 0); 1453 ATF_REQUIRE_EQ(type, NV_TYPE_NVLIST_ARRAY); 1454 ATF_REQUIRE(!nvlist_empty(nvl)); 1455 ATF_REQUIRE(nvlist_exists(nvl, key)); 1456 ATF_REQUIRE(nvlist_exists_nvlist_array(nvl, key)); 1457 ATF_REQUIRE_EQ(nvlist_next(nvl, &type, &cookie), 1458 static_cast<const char *>(NULL)); 1459 1460 cookie = NULL; 1461 ATF_REQUIRE_EQ(strcmp(key, nvlist_next(nvl, &type, &cookie)), 0); 1462 cnvlist_free_nvlist_array(nvl, cookie); 1463 1464 cookie = NULL; 1465 ATF_REQUIRE_EQ(nvlist_error(nvl), 0); 1466 ATF_REQUIRE(nvlist_empty(nvl)); 1467 ATF_REQUIRE(!nvlist_exists(nvl, key)); 1468 ATF_REQUIRE(!nvlist_exists_nvlist_array(nvl, key)); 1469 ATF_REQUIRE_EQ(nvlist_next(nvl, &type, &cookie), 1470 static_cast<const char *>(NULL)); 1471 1472 for (i = 0; i < 8; i++) 1473 nvlist_destroy(testnvl[i]); 1474 nvlist_destroy(nvl); 1475 } 1476 1477 ATF_INIT_TEST_CASES(tp) 1478 { 1479 ATF_ADD_TEST_CASE(tp, cnvlist_get_bool); 1480 ATF_ADD_TEST_CASE(tp, cnvlist_get_bool_array); 1481 ATF_ADD_TEST_CASE(tp, cnvlist_get_number); 1482 ATF_ADD_TEST_CASE(tp, cnvlist_get_string); 1483 ATF_ADD_TEST_CASE(tp, cnvlist_get_nvlist); 1484 ATF_ADD_TEST_CASE(tp, cnvlist_get_descriptor); 1485 ATF_ADD_TEST_CASE(tp, cnvlist_get_binary); 1486 ATF_ADD_TEST_CASE(tp, cnvlist_get_number_array); 1487 ATF_ADD_TEST_CASE(tp, cnvlist_get_string_array); 1488 ATF_ADD_TEST_CASE(tp, cnvlist_get_nvlist_array); 1489 ATF_ADD_TEST_CASE(tp, cnvlist_get_descriptor_array); 1490 ATF_ADD_TEST_CASE(tp, cnvlist_take_bool); 1491 ATF_ADD_TEST_CASE(tp, cnvlist_take_number); 1492 ATF_ADD_TEST_CASE(tp, cnvlist_take_string); 1493 ATF_ADD_TEST_CASE(tp, cnvlist_take_nvlist); 1494 ATF_ADD_TEST_CASE(tp, cnvlist_take_binary); 1495 ATF_ADD_TEST_CASE(tp, cnvlist_take_bool_array); 1496 ATF_ADD_TEST_CASE(tp, cnvlist_take_number_array); 1497 ATF_ADD_TEST_CASE(tp, cnvlist_take_string_array); 1498 ATF_ADD_TEST_CASE(tp, cnvlist_take_nvlist_array); 1499 ATF_ADD_TEST_CASE(tp, cnvlist_free_bool); 1500 ATF_ADD_TEST_CASE(tp, cnvlist_free_number); 1501 ATF_ADD_TEST_CASE(tp, cnvlist_free_string); 1502 ATF_ADD_TEST_CASE(tp, cnvlist_free_nvlist); 1503 ATF_ADD_TEST_CASE(tp, cnvlist_free_binary); 1504 ATF_ADD_TEST_CASE(tp, cnvlist_free_bool_array); 1505 ATF_ADD_TEST_CASE(tp, cnvlist_free_number_array); 1506 ATF_ADD_TEST_CASE(tp, cnvlist_free_string_array); 1507 ATF_ADD_TEST_CASE(tp, cnvlist_free_nvlist_array); 1508 } 1509