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