nv_tests.cc revision 279424
1/*- 2 * Copyright (c) 2014-2015 Sandvine Inc. All rights reserved. 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 AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24 * SUCH DAMAGE. 25 */ 26 27#include <sys/cdefs.h> 28__FBSDID("$FreeBSD: head/lib/libnv/tests/nv_tests.cc 279424 2015-03-01 00:20:57Z rstone $"); 29 30#include <atf-c++.hpp> 31#include <nv.h> 32 33#include <errno.h> 34/* 35 * Test that a newly created nvlist has no errors, and is empty. 36 */ 37ATF_TEST_CASE_WITHOUT_HEAD(nvlist_create__is_empty); 38ATF_TEST_CASE_BODY(nvlist_create__is_empty) 39{ 40 nvlist_t *nvl; 41 int type; 42 void *it; 43 44 nvl = nvlist_create(0); 45 46 ATF_REQUIRE(nvl != NULL); 47 48 ATF_REQUIRE_EQ(nvlist_error(nvl), 0); 49 ATF_REQUIRE(nvlist_empty(nvl)); 50 51 it = NULL; 52 ATF_REQUIRE_EQ(nvlist_next(nvl, &type, &it), NULL); 53 54 nvlist_destroy(nvl); 55} 56 57ATF_TEST_CASE_WITHOUT_HEAD(nvlist_add_null__single_insert); 58ATF_TEST_CASE_BODY(nvlist_add_null__single_insert) 59{ 60 nvlist_t *nvl; 61 void *it; 62 const char *key; 63 int type; 64 65 key = "key"; 66 nvl = nvlist_create(0); 67 68 ATF_REQUIRE(nvl != NULL); 69 ATF_REQUIRE(!nvlist_exists(nvl, key)); 70 71 nvlist_add_null(nvl, key); 72 73 ATF_REQUIRE(!nvlist_empty(nvl)); 74 ATF_REQUIRE(nvlist_exists(nvl, key)); 75 ATF_REQUIRE(nvlist_existsf(nvl, "%s", key)); 76 ATF_REQUIRE(nvlist_exists_null(nvl, key)); 77 ATF_REQUIRE(nvlist_existsf_null(nvl, "key")); 78 79 /* Iterate over the nvlist; ensure that it has only our one key. */ 80 it = NULL; 81 ATF_REQUIRE_EQ(strcmp(nvlist_next(nvl, &type, &it), key), 0); 82 ATF_REQUIRE_EQ(type, NV_TYPE_NULL); 83 ATF_REQUIRE_EQ(nvlist_next(nvl, &type,&it), NULL); 84 85 nvlist_destroy(nvl); 86} 87 88ATF_TEST_CASE_WITHOUT_HEAD(nvlist_add_bool__single_insert); 89ATF_TEST_CASE_BODY(nvlist_add_bool__single_insert) 90{ 91 nvlist_t *nvl; 92 void *it; 93 const char *key; 94 int type; 95 96 key = "name"; 97 nvl = nvlist_create(0); 98 99 ATF_REQUIRE(nvl != NULL); 100 ATF_REQUIRE(!nvlist_exists(nvl, key)); 101 102 nvlist_add_bool(nvl, key, true); 103 104 ATF_REQUIRE(!nvlist_empty(nvl)); 105 ATF_REQUIRE(nvlist_exists(nvl, key)); 106 ATF_REQUIRE(nvlist_existsf(nvl, "%s%s", "na", "me")); 107 ATF_REQUIRE(nvlist_exists_bool(nvl, key)); 108 ATF_REQUIRE(nvlist_existsf_bool(nvl, "%s%c", "nam", 'e')); 109 ATF_REQUIRE_EQ(nvlist_get_bool(nvl, key), true); 110 ATF_REQUIRE_EQ(nvlist_getf_bool(nvl, "%c%s", 'n', "ame"), true); 111 112 /* Iterate over the nvlist; ensure that it has only our one key. */ 113 it = NULL; 114 ATF_REQUIRE_EQ(strcmp(nvlist_next(nvl, &type, &it), key), 0); 115 ATF_REQUIRE_EQ(type, NV_TYPE_BOOL); 116 ATF_REQUIRE_EQ(nvlist_next(nvl, &type,&it), NULL); 117 118 nvlist_destroy(nvl); 119} 120 121ATF_TEST_CASE_WITHOUT_HEAD(nvlist_add_number__single_insert); 122ATF_TEST_CASE_BODY(nvlist_add_number__single_insert) 123{ 124 nvlist_t *nvl; 125 void *it; 126 const char *key; 127 uint64_t value; 128 int type; 129 130 key = "foo123"; 131 value = 71965; 132 nvl = nvlist_create(0); 133 134 ATF_REQUIRE(nvl != NULL); 135 ATF_REQUIRE(!nvlist_exists(nvl, key)); 136 137 nvlist_add_number(nvl, key, value); 138 139 ATF_REQUIRE(!nvlist_empty(nvl)); 140 ATF_REQUIRE(nvlist_exists(nvl, key)); 141 ATF_REQUIRE(nvlist_existsf(nvl, "%s%d", "foo", 123)); 142 ATF_REQUIRE(nvlist_exists_number(nvl, key)); 143 ATF_REQUIRE(nvlist_existsf_number(nvl, "%s", key)); 144 ATF_REQUIRE_EQ(nvlist_get_number(nvl, key), value); 145 ATF_REQUIRE_EQ(nvlist_getf_number(nvl, "%s", key), value); 146 147 /* Iterate over the nvlist; ensure that it has only our one key. */ 148 it = NULL; 149 ATF_REQUIRE_EQ(strcmp(nvlist_next(nvl, &type, &it), key), 0); 150 ATF_REQUIRE_EQ(type, NV_TYPE_NUMBER); 151 ATF_REQUIRE_EQ(nvlist_next(nvl, &type,&it), NULL); 152 153 nvlist_destroy(nvl); 154} 155 156ATF_TEST_CASE_WITHOUT_HEAD(nvlist_add_string__single_insert); 157ATF_TEST_CASE_BODY(nvlist_add_string__single_insert) 158{ 159 nvlist_t *nvl; 160 void *it; 161 const char *key; 162 const char *value; 163 int type; 164 165 key = "test"; 166 value = "fgjdkgjdk"; 167 nvl = nvlist_create(0); 168 169 ATF_REQUIRE(nvl != NULL); 170 ATF_REQUIRE(!nvlist_exists(nvl, key)); 171 172 nvlist_add_string(nvl, key, value); 173 174 ATF_REQUIRE(!nvlist_empty(nvl)); 175 ATF_REQUIRE(nvlist_exists(nvl, key)); 176 ATF_REQUIRE(nvlist_existsf(nvl, "%s", key)); 177 ATF_REQUIRE(nvlist_exists_string(nvl, key)); 178 ATF_REQUIRE(nvlist_existsf_string(nvl, "%s", key)); 179 ATF_REQUIRE_EQ(strcmp(nvlist_get_string(nvl, key), value), 0); 180 ATF_REQUIRE_EQ(strcmp(nvlist_getf_string(nvl, "%s", key), value), 0); 181 182 /* nvlist_add_* is required to clone the value, so check for that. */ 183 ATF_REQUIRE(nvlist_get_string(nvl, key) != value); 184 185 /* Iterate over the nvlist; ensure that it has only our one key. */ 186 it = NULL; 187 ATF_REQUIRE_EQ(strcmp(nvlist_next(nvl, &type, &it), key), 0); 188 ATF_REQUIRE_EQ(type, NV_TYPE_STRING); 189 ATF_REQUIRE_EQ(nvlist_next(nvl, &type,&it), NULL); 190 191 nvlist_destroy(nvl); 192} 193 194ATF_TEST_CASE_WITHOUT_HEAD(nvlist_add_nvlist__single_insert); 195ATF_TEST_CASE_BODY(nvlist_add_nvlist__single_insert) 196{ 197 nvlist_t *nvl; 198 void *it; 199 const char *key, *subkey; 200 nvlist_t *sublist; 201 const nvlist_t *value; 202 int type; 203 204 key = "test"; 205 subkey = "subkey"; 206 sublist = nvlist_create(0); 207 nvl = nvlist_create(0); 208 209 ATF_REQUIRE(nvl != NULL); 210 ATF_REQUIRE(!nvlist_exists(nvl, key)); 211 212 nvlist_add_null(sublist, subkey); 213 nvlist_add_nvlist(nvl, key, sublist); 214 215 ATF_REQUIRE(!nvlist_empty(nvl)); 216 ATF_REQUIRE(nvlist_exists(nvl, key)); 217 ATF_REQUIRE(nvlist_existsf(nvl, "%s", key)); 218 ATF_REQUIRE(nvlist_exists_nvlist(nvl, key)); 219 ATF_REQUIRE(nvlist_existsf_nvlist(nvl, "%s", key)); 220 221 value = nvlist_get_nvlist(nvl, key); 222 ATF_REQUIRE(nvlist_exists_null(value, subkey)); 223 224 /* nvlist_add_* is required to clone the value, so check for that. */ 225 ATF_REQUIRE(sublist != value); 226 227 value = nvlist_getf_nvlist(nvl, "%s", key); 228 ATF_REQUIRE(nvlist_exists_null(value, subkey)); 229 ATF_REQUIRE(sublist != value); 230 231 /* Iterate over the nvlist; ensure that it has only our one key. */ 232 it = NULL; 233 ATF_REQUIRE_EQ(strcmp(nvlist_next(nvl, &type, &it), key), 0); 234 ATF_REQUIRE_EQ(type, NV_TYPE_NVLIST); 235 ATF_REQUIRE_EQ(nvlist_next(nvl, &type,&it), NULL); 236 237 nvlist_destroy(sublist); 238 nvlist_destroy(nvl); 239} 240 241ATF_TEST_CASE_WITHOUT_HEAD(nvlist_add_binary__single_insert); 242ATF_TEST_CASE_BODY(nvlist_add_binary__single_insert) 243{ 244 nvlist_t *nvl; 245 void *it; 246 const char *key; 247 void *value; 248 const void *ret_value; 249 size_t value_size, ret_size; 250 int type; 251 252 key = "binary"; 253 value_size = 13; 254 value = malloc(value_size); 255 memset(value, 0xa5, value_size); 256 nvl = nvlist_create(0); 257 258 ATF_REQUIRE(nvl != NULL); 259 ATF_REQUIRE(!nvlist_exists(nvl, key)); 260 261 nvlist_add_binary(nvl, key, value, value_size); 262 263 ATF_REQUIRE(!nvlist_empty(nvl)); 264 ATF_REQUIRE(nvlist_exists(nvl, key)); 265 ATF_REQUIRE(nvlist_existsf(nvl, "%s", key)); 266 ATF_REQUIRE(nvlist_exists_binary(nvl, key)); 267 ATF_REQUIRE(nvlist_existsf_binary(nvl, "%s", key)); 268 269 ret_value = nvlist_get_binary(nvl, key, &ret_size); 270 ATF_REQUIRE_EQ(value_size, ret_size); 271 ATF_REQUIRE_EQ(memcmp(value, ret_value, ret_size), 0); 272 273 /* nvlist_add_* is required to clone the value, so check for that. */ 274 ATF_REQUIRE(value != ret_value); 275 276 ret_value = nvlist_getf_binary(nvl, &ret_size, "%s", key); 277 ATF_REQUIRE_EQ(value_size, ret_size); 278 ATF_REQUIRE_EQ(memcmp(value, ret_value, ret_size), 0); 279 ATF_REQUIRE(value != ret_value); 280 281 /* Iterate over the nvlist; ensure that it has only our one key. */ 282 it = NULL; 283 ATF_REQUIRE_EQ(strcmp(nvlist_next(nvl, &type, &it), key), 0); 284 ATF_REQUIRE_EQ(type, NV_TYPE_BINARY); 285 ATF_REQUIRE_EQ(nvlist_next(nvl, &type,&it), NULL); 286 287 nvlist_destroy(nvl); 288 free(value); 289} 290 291ATF_INIT_TEST_CASES(tp) 292{ 293 ATF_ADD_TEST_CASE(tp, nvlist_create__is_empty); 294 ATF_ADD_TEST_CASE(tp, nvlist_add_null__single_insert); 295 ATF_ADD_TEST_CASE(tp, nvlist_add_bool__single_insert); 296 ATF_ADD_TEST_CASE(tp, nvlist_add_number__single_insert); 297 ATF_ADD_TEST_CASE(tp, nvlist_add_string__single_insert); 298 ATF_ADD_TEST_CASE(tp, nvlist_add_nvlist__single_insert); 299 ATF_ADD_TEST_CASE(tp, nvlist_add_binary__single_insert); 300} 301/*- 302 * Copyright (c) 2014-2015 Sandvine Inc. All rights reserved. 303 * All rights reserved. 304 * 305 * Redistribution and use in source and binary forms, with or without 306 * modification, are permitted provided that the following conditions 307 * are met: 308 * 1. Redistributions of source code must retain the above copyright 309 * notice, this list of conditions and the following disclaimer. 310 * 2. Redistributions in binary form must reproduce the above copyright 311 * notice, this list of conditions and the following disclaimer in the 312 * documentation and/or other materials provided with the distribution. 313 * 314 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 315 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 316 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 317 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 318 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 319 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 320 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 321 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 322 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 323 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 324 * SUCH DAMAGE. 325 */ 326 327#include <atf-c++.hpp> 328#include <nv.h> 329 330#include <errno.h> 331/* 332 * Test that a newly created nvlist has no errors, and is empty. 333 */ 334ATF_TEST_CASE_WITHOUT_HEAD(nvlist_create__is_empty); 335ATF_TEST_CASE_BODY(nvlist_create__is_empty) 336{ 337 nvlist_t *nvl; 338 int type; 339 void *it; 340 341 nvl = nvlist_create(0); 342 343 ATF_REQUIRE(nvl != NULL); 344 345 ATF_REQUIRE_EQ(nvlist_error(nvl), 0); 346 ATF_REQUIRE(nvlist_empty(nvl)); 347 348 it = NULL; 349 ATF_REQUIRE_EQ(nvlist_next(nvl, &type, &it), NULL); 350 351 nvlist_destroy(nvl); 352} 353 354ATF_TEST_CASE_WITHOUT_HEAD(nvlist_add_null__single_insert); 355ATF_TEST_CASE_BODY(nvlist_add_null__single_insert) 356{ 357 nvlist_t *nvl; 358 void *it; 359 const char *key; 360 int type; 361 362 key = "key"; 363 nvl = nvlist_create(0); 364 365 ATF_REQUIRE(nvl != NULL); 366 ATF_REQUIRE(!nvlist_exists(nvl, key)); 367 368 nvlist_add_null(nvl, key); 369 370 ATF_REQUIRE(!nvlist_empty(nvl)); 371 ATF_REQUIRE(nvlist_exists(nvl, key)); 372 ATF_REQUIRE(nvlist_existsf(nvl, "%s", key)); 373 ATF_REQUIRE(nvlist_exists_null(nvl, key)); 374 ATF_REQUIRE(nvlist_existsf_null(nvl, "key")); 375 376 /* Iterate over the nvlist; ensure that it has only our one key. */ 377 it = NULL; 378 ATF_REQUIRE_EQ(strcmp(nvlist_next(nvl, &type, &it), key), 0); 379 ATF_REQUIRE_EQ(type, NV_TYPE_NULL); 380 ATF_REQUIRE_EQ(nvlist_next(nvl, &type,&it), NULL); 381 382 nvlist_destroy(nvl); 383} 384 385ATF_TEST_CASE_WITHOUT_HEAD(nvlist_add_bool__single_insert); 386ATF_TEST_CASE_BODY(nvlist_add_bool__single_insert) 387{ 388 nvlist_t *nvl; 389 void *it; 390 const char *key; 391 int type; 392 393 key = "name"; 394 nvl = nvlist_create(0); 395 396 ATF_REQUIRE(nvl != NULL); 397 ATF_REQUIRE(!nvlist_exists(nvl, key)); 398 399 nvlist_add_bool(nvl, key, true); 400 401 ATF_REQUIRE(!nvlist_empty(nvl)); 402 ATF_REQUIRE(nvlist_exists(nvl, key)); 403 ATF_REQUIRE(nvlist_existsf(nvl, "%s%s", "na", "me")); 404 ATF_REQUIRE(nvlist_exists_bool(nvl, key)); 405 ATF_REQUIRE(nvlist_existsf_bool(nvl, "%s%c", "nam", 'e')); 406 ATF_REQUIRE_EQ(nvlist_get_bool(nvl, key), true); 407 ATF_REQUIRE_EQ(nvlist_getf_bool(nvl, "%c%s", 'n', "ame"), true); 408 409 /* Iterate over the nvlist; ensure that it has only our one key. */ 410 it = NULL; 411 ATF_REQUIRE_EQ(strcmp(nvlist_next(nvl, &type, &it), key), 0); 412 ATF_REQUIRE_EQ(type, NV_TYPE_BOOL); 413 ATF_REQUIRE_EQ(nvlist_next(nvl, &type,&it), NULL); 414 415 nvlist_destroy(nvl); 416} 417 418ATF_TEST_CASE_WITHOUT_HEAD(nvlist_add_number__single_insert); 419ATF_TEST_CASE_BODY(nvlist_add_number__single_insert) 420{ 421 nvlist_t *nvl; 422 void *it; 423 const char *key; 424 uint64_t value; 425 int type; 426 427 key = "foo123"; 428 value = 71965; 429 nvl = nvlist_create(0); 430 431 ATF_REQUIRE(nvl != NULL); 432 ATF_REQUIRE(!nvlist_exists(nvl, key)); 433 434 nvlist_add_number(nvl, key, value); 435 436 ATF_REQUIRE(!nvlist_empty(nvl)); 437 ATF_REQUIRE(nvlist_exists(nvl, key)); 438 ATF_REQUIRE(nvlist_existsf(nvl, "%s%d", "foo", 123)); 439 ATF_REQUIRE(nvlist_exists_number(nvl, key)); 440 ATF_REQUIRE(nvlist_existsf_number(nvl, "%s", key)); 441 ATF_REQUIRE_EQ(nvlist_get_number(nvl, key), value); 442 ATF_REQUIRE_EQ(nvlist_getf_number(nvl, "%s", key), value); 443 444 /* Iterate over the nvlist; ensure that it has only our one key. */ 445 it = NULL; 446 ATF_REQUIRE_EQ(strcmp(nvlist_next(nvl, &type, &it), key), 0); 447 ATF_REQUIRE_EQ(type, NV_TYPE_NUMBER); 448 ATF_REQUIRE_EQ(nvlist_next(nvl, &type,&it), NULL); 449 450 nvlist_destroy(nvl); 451} 452 453ATF_TEST_CASE_WITHOUT_HEAD(nvlist_add_string__single_insert); 454ATF_TEST_CASE_BODY(nvlist_add_string__single_insert) 455{ 456 nvlist_t *nvl; 457 void *it; 458 const char *key; 459 const char *value; 460 int type; 461 462 key = "test"; 463 value = "fgjdkgjdk"; 464 nvl = nvlist_create(0); 465 466 ATF_REQUIRE(nvl != NULL); 467 ATF_REQUIRE(!nvlist_exists(nvl, key)); 468 469 nvlist_add_string(nvl, key, value); 470 471 ATF_REQUIRE(!nvlist_empty(nvl)); 472 ATF_REQUIRE(nvlist_exists(nvl, key)); 473 ATF_REQUIRE(nvlist_existsf(nvl, "%s", key)); 474 ATF_REQUIRE(nvlist_exists_string(nvl, key)); 475 ATF_REQUIRE(nvlist_existsf_string(nvl, "%s", key)); 476 ATF_REQUIRE_EQ(strcmp(nvlist_get_string(nvl, key), value), 0); 477 ATF_REQUIRE_EQ(strcmp(nvlist_getf_string(nvl, "%s", key), value), 0); 478 479 /* nvlist_add_* is required to clone the value, so check for that. */ 480 ATF_REQUIRE(nvlist_get_string(nvl, key) != value); 481 482 /* Iterate over the nvlist; ensure that it has only our one key. */ 483 it = NULL; 484 ATF_REQUIRE_EQ(strcmp(nvlist_next(nvl, &type, &it), key), 0); 485 ATF_REQUIRE_EQ(type, NV_TYPE_STRING); 486 ATF_REQUIRE_EQ(nvlist_next(nvl, &type,&it), NULL); 487 488 nvlist_destroy(nvl); 489} 490 491ATF_TEST_CASE_WITHOUT_HEAD(nvlist_add_nvlist__single_insert); 492ATF_TEST_CASE_BODY(nvlist_add_nvlist__single_insert) 493{ 494 nvlist_t *nvl; 495 void *it; 496 const char *key, *subkey; 497 nvlist_t *sublist; 498 const nvlist_t *value; 499 int type; 500 501 key = "test"; 502 subkey = "subkey"; 503 sublist = nvlist_create(0); 504 nvl = nvlist_create(0); 505 506 ATF_REQUIRE(nvl != NULL); 507 ATF_REQUIRE(!nvlist_exists(nvl, key)); 508 509 nvlist_add_null(sublist, subkey); 510 nvlist_add_nvlist(nvl, key, sublist); 511 512 ATF_REQUIRE(!nvlist_empty(nvl)); 513 ATF_REQUIRE(nvlist_exists(nvl, key)); 514 ATF_REQUIRE(nvlist_existsf(nvl, "%s", key)); 515 ATF_REQUIRE(nvlist_exists_nvlist(nvl, key)); 516 ATF_REQUIRE(nvlist_existsf_nvlist(nvl, "%s", key)); 517 518 value = nvlist_get_nvlist(nvl, key); 519 ATF_REQUIRE(nvlist_exists_null(value, subkey)); 520 521 /* nvlist_add_* is required to clone the value, so check for that. */ 522 ATF_REQUIRE(sublist != value); 523 524 value = nvlist_getf_nvlist(nvl, "%s", key); 525 ATF_REQUIRE(nvlist_exists_null(value, subkey)); 526 ATF_REQUIRE(sublist != value); 527 528 /* Iterate over the nvlist; ensure that it has only our one key. */ 529 it = NULL; 530 ATF_REQUIRE_EQ(strcmp(nvlist_next(nvl, &type, &it), key), 0); 531 ATF_REQUIRE_EQ(type, NV_TYPE_NVLIST); 532 ATF_REQUIRE_EQ(nvlist_next(nvl, &type,&it), NULL); 533 534 nvlist_destroy(sublist); 535 nvlist_destroy(nvl); 536} 537 538ATF_TEST_CASE_WITHOUT_HEAD(nvlist_add_binary__single_insert); 539ATF_TEST_CASE_BODY(nvlist_add_binary__single_insert) 540{ 541 nvlist_t *nvl; 542 void *it; 543 const char *key; 544 void *value; 545 const void *ret_value; 546 size_t value_size, ret_size; 547 int type; 548 549 key = "binary"; 550 value_size = 13; 551 value = malloc(value_size); 552 memset(value, 0xa5, value_size); 553 nvl = nvlist_create(0); 554 555 ATF_REQUIRE(nvl != NULL); 556 ATF_REQUIRE(!nvlist_exists(nvl, key)); 557 558 nvlist_add_binary(nvl, key, value, value_size); 559 560 ATF_REQUIRE(!nvlist_empty(nvl)); 561 ATF_REQUIRE(nvlist_exists(nvl, key)); 562 ATF_REQUIRE(nvlist_existsf(nvl, "%s", key)); 563 ATF_REQUIRE(nvlist_exists_binary(nvl, key)); 564 ATF_REQUIRE(nvlist_existsf_binary(nvl, "%s", key)); 565 566 ret_value = nvlist_get_binary(nvl, key, &ret_size); 567 ATF_REQUIRE_EQ(value_size, ret_size); 568 ATF_REQUIRE_EQ(memcmp(value, ret_value, ret_size), 0); 569 570 /* nvlist_add_* is required to clone the value, so check for that. */ 571 ATF_REQUIRE(value != ret_value); 572 573 ret_value = nvlist_getf_binary(nvl, &ret_size, "%s", key); 574 ATF_REQUIRE_EQ(value_size, ret_size); 575 ATF_REQUIRE_EQ(memcmp(value, ret_value, ret_size), 0); 576 ATF_REQUIRE(value != ret_value); 577 578 /* Iterate over the nvlist; ensure that it has only our one key. */ 579 it = NULL; 580 ATF_REQUIRE_EQ(strcmp(nvlist_next(nvl, &type, &it), key), 0); 581 ATF_REQUIRE_EQ(type, NV_TYPE_BINARY); 582 ATF_REQUIRE_EQ(nvlist_next(nvl, &type,&it), NULL); 583 584 nvlist_destroy(nvl); 585 free(value); 586} 587 588ATF_INIT_TEST_CASES(tp) 589{ 590 ATF_ADD_TEST_CASE(tp, nvlist_create__is_empty); 591 ATF_ADD_TEST_CASE(tp, nvlist_add_null__single_insert); 592 ATF_ADD_TEST_CASE(tp, nvlist_add_bool__single_insert); 593 ATF_ADD_TEST_CASE(tp, nvlist_add_number__single_insert); 594 ATF_ADD_TEST_CASE(tp, nvlist_add_string__single_insert); 595 ATF_ADD_TEST_CASE(tp, nvlist_add_nvlist__single_insert); 596 ATF_ADD_TEST_CASE(tp, nvlist_add_binary__single_insert); 597} 598/*- 599 * Copyright (c) 2014-2015 Sandvine Inc. All rights reserved. 600 * All rights reserved. 601 * 602 * Redistribution and use in source and binary forms, with or without 603 * modification, are permitted provided that the following conditions 604 * are met: 605 * 1. Redistributions of source code must retain the above copyright 606 * notice, this list of conditions and the following disclaimer. 607 * 2. Redistributions in binary form must reproduce the above copyright 608 * notice, this list of conditions and the following disclaimer in the 609 * documentation and/or other materials provided with the distribution. 610 * 611 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 612 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 613 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 614 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 615 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 616 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 617 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 618 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 619 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 620 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 621 * SUCH DAMAGE. 622 */ 623 624#include <atf-c++.hpp> 625#include <nv.h> 626 627#include <errno.h> 628/* 629 * Test that a newly created nvlist has no errors, and is empty. 630 */ 631ATF_TEST_CASE_WITHOUT_HEAD(nvlist_create__is_empty); 632ATF_TEST_CASE_BODY(nvlist_create__is_empty) 633{ 634 nvlist_t *nvl; 635 int type; 636 void *it; 637 638 nvl = nvlist_create(0); 639 640 ATF_REQUIRE(nvl != NULL); 641 642 ATF_REQUIRE_EQ(nvlist_error(nvl), 0); 643 ATF_REQUIRE(nvlist_empty(nvl)); 644 645 it = NULL; 646 ATF_REQUIRE_EQ(nvlist_next(nvl, &type, &it), NULL); 647 648 nvlist_destroy(nvl); 649} 650 651ATF_TEST_CASE_WITHOUT_HEAD(nvlist_add_null__single_insert); 652ATF_TEST_CASE_BODY(nvlist_add_null__single_insert) 653{ 654 nvlist_t *nvl; 655 void *it; 656 const char *key; 657 int type; 658 659 key = "key"; 660 nvl = nvlist_create(0); 661 662 ATF_REQUIRE(nvl != NULL); 663 ATF_REQUIRE(!nvlist_exists(nvl, key)); 664 665 nvlist_add_null(nvl, key); 666 667 ATF_REQUIRE(!nvlist_empty(nvl)); 668 ATF_REQUIRE(nvlist_exists(nvl, key)); 669 ATF_REQUIRE(nvlist_existsf(nvl, "%s", key)); 670 ATF_REQUIRE(nvlist_exists_null(nvl, key)); 671 ATF_REQUIRE(nvlist_existsf_null(nvl, "key")); 672 673 /* Iterate over the nvlist; ensure that it has only our one key. */ 674 it = NULL; 675 ATF_REQUIRE_EQ(strcmp(nvlist_next(nvl, &type, &it), key), 0); 676 ATF_REQUIRE_EQ(type, NV_TYPE_NULL); 677 ATF_REQUIRE_EQ(nvlist_next(nvl, &type,&it), NULL); 678 679 nvlist_destroy(nvl); 680} 681 682ATF_TEST_CASE_WITHOUT_HEAD(nvlist_add_bool__single_insert); 683ATF_TEST_CASE_BODY(nvlist_add_bool__single_insert) 684{ 685 nvlist_t *nvl; 686 void *it; 687 const char *key; 688 int type; 689 690 key = "name"; 691 nvl = nvlist_create(0); 692 693 ATF_REQUIRE(nvl != NULL); 694 ATF_REQUIRE(!nvlist_exists(nvl, key)); 695 696 nvlist_add_bool(nvl, key, true); 697 698 ATF_REQUIRE(!nvlist_empty(nvl)); 699 ATF_REQUIRE(nvlist_exists(nvl, key)); 700 ATF_REQUIRE(nvlist_existsf(nvl, "%s%s", "na", "me")); 701 ATF_REQUIRE(nvlist_exists_bool(nvl, key)); 702 ATF_REQUIRE(nvlist_existsf_bool(nvl, "%s%c", "nam", 'e')); 703 ATF_REQUIRE_EQ(nvlist_get_bool(nvl, key), true); 704 ATF_REQUIRE_EQ(nvlist_getf_bool(nvl, "%c%s", 'n', "ame"), true); 705 706 /* Iterate over the nvlist; ensure that it has only our one key. */ 707 it = NULL; 708 ATF_REQUIRE_EQ(strcmp(nvlist_next(nvl, &type, &it), key), 0); 709 ATF_REQUIRE_EQ(type, NV_TYPE_BOOL); 710 ATF_REQUIRE_EQ(nvlist_next(nvl, &type,&it), NULL); 711 712 nvlist_destroy(nvl); 713} 714 715ATF_TEST_CASE_WITHOUT_HEAD(nvlist_add_number__single_insert); 716ATF_TEST_CASE_BODY(nvlist_add_number__single_insert) 717{ 718 nvlist_t *nvl; 719 void *it; 720 const char *key; 721 uint64_t value; 722 int type; 723 724 key = "foo123"; 725 value = 71965; 726 nvl = nvlist_create(0); 727 728 ATF_REQUIRE(nvl != NULL); 729 ATF_REQUIRE(!nvlist_exists(nvl, key)); 730 731 nvlist_add_number(nvl, key, value); 732 733 ATF_REQUIRE(!nvlist_empty(nvl)); 734 ATF_REQUIRE(nvlist_exists(nvl, key)); 735 ATF_REQUIRE(nvlist_existsf(nvl, "%s%d", "foo", 123)); 736 ATF_REQUIRE(nvlist_exists_number(nvl, key)); 737 ATF_REQUIRE(nvlist_existsf_number(nvl, "%s", key)); 738 ATF_REQUIRE_EQ(nvlist_get_number(nvl, key), value); 739 ATF_REQUIRE_EQ(nvlist_getf_number(nvl, "%s", key), value); 740 741 /* Iterate over the nvlist; ensure that it has only our one key. */ 742 it = NULL; 743 ATF_REQUIRE_EQ(strcmp(nvlist_next(nvl, &type, &it), key), 0); 744 ATF_REQUIRE_EQ(type, NV_TYPE_NUMBER); 745 ATF_REQUIRE_EQ(nvlist_next(nvl, &type,&it), NULL); 746 747 nvlist_destroy(nvl); 748} 749 750ATF_TEST_CASE_WITHOUT_HEAD(nvlist_add_string__single_insert); 751ATF_TEST_CASE_BODY(nvlist_add_string__single_insert) 752{ 753 nvlist_t *nvl; 754 void *it; 755 const char *key; 756 const char *value; 757 int type; 758 759 key = "test"; 760 value = "fgjdkgjdk"; 761 nvl = nvlist_create(0); 762 763 ATF_REQUIRE(nvl != NULL); 764 ATF_REQUIRE(!nvlist_exists(nvl, key)); 765 766 nvlist_add_string(nvl, key, value); 767 768 ATF_REQUIRE(!nvlist_empty(nvl)); 769 ATF_REQUIRE(nvlist_exists(nvl, key)); 770 ATF_REQUIRE(nvlist_existsf(nvl, "%s", key)); 771 ATF_REQUIRE(nvlist_exists_string(nvl, key)); 772 ATF_REQUIRE(nvlist_existsf_string(nvl, "%s", key)); 773 ATF_REQUIRE_EQ(strcmp(nvlist_get_string(nvl, key), value), 0); 774 ATF_REQUIRE_EQ(strcmp(nvlist_getf_string(nvl, "%s", key), value), 0); 775 776 /* nvlist_add_* is required to clone the value, so check for that. */ 777 ATF_REQUIRE(nvlist_get_string(nvl, key) != value); 778 779 /* Iterate over the nvlist; ensure that it has only our one key. */ 780 it = NULL; 781 ATF_REQUIRE_EQ(strcmp(nvlist_next(nvl, &type, &it), key), 0); 782 ATF_REQUIRE_EQ(type, NV_TYPE_STRING); 783 ATF_REQUIRE_EQ(nvlist_next(nvl, &type,&it), NULL); 784 785 nvlist_destroy(nvl); 786} 787 788ATF_TEST_CASE_WITHOUT_HEAD(nvlist_add_nvlist__single_insert); 789ATF_TEST_CASE_BODY(nvlist_add_nvlist__single_insert) 790{ 791 nvlist_t *nvl; 792 void *it; 793 const char *key, *subkey; 794 nvlist_t *sublist; 795 const nvlist_t *value; 796 int type; 797 798 key = "test"; 799 subkey = "subkey"; 800 sublist = nvlist_create(0); 801 nvl = nvlist_create(0); 802 803 ATF_REQUIRE(nvl != NULL); 804 ATF_REQUIRE(!nvlist_exists(nvl, key)); 805 806 nvlist_add_null(sublist, subkey); 807 nvlist_add_nvlist(nvl, key, sublist); 808 809 ATF_REQUIRE(!nvlist_empty(nvl)); 810 ATF_REQUIRE(nvlist_exists(nvl, key)); 811 ATF_REQUIRE(nvlist_existsf(nvl, "%s", key)); 812 ATF_REQUIRE(nvlist_exists_nvlist(nvl, key)); 813 ATF_REQUIRE(nvlist_existsf_nvlist(nvl, "%s", key)); 814 815 value = nvlist_get_nvlist(nvl, key); 816 ATF_REQUIRE(nvlist_exists_null(value, subkey)); 817 818 /* nvlist_add_* is required to clone the value, so check for that. */ 819 ATF_REQUIRE(sublist != value); 820 821 value = nvlist_getf_nvlist(nvl, "%s", key); 822 ATF_REQUIRE(nvlist_exists_null(value, subkey)); 823 ATF_REQUIRE(sublist != value); 824 825 /* Iterate over the nvlist; ensure that it has only our one key. */ 826 it = NULL; 827 ATF_REQUIRE_EQ(strcmp(nvlist_next(nvl, &type, &it), key), 0); 828 ATF_REQUIRE_EQ(type, NV_TYPE_NVLIST); 829 ATF_REQUIRE_EQ(nvlist_next(nvl, &type,&it), NULL); 830 831 nvlist_destroy(sublist); 832 nvlist_destroy(nvl); 833} 834 835ATF_TEST_CASE_WITHOUT_HEAD(nvlist_add_binary__single_insert); 836ATF_TEST_CASE_BODY(nvlist_add_binary__single_insert) 837{ 838 nvlist_t *nvl; 839 void *it; 840 const char *key; 841 void *value; 842 const void *ret_value; 843 size_t value_size, ret_size; 844 int type; 845 846 key = "binary"; 847 value_size = 13; 848 value = malloc(value_size); 849 memset(value, 0xa5, value_size); 850 nvl = nvlist_create(0); 851 852 ATF_REQUIRE(nvl != NULL); 853 ATF_REQUIRE(!nvlist_exists(nvl, key)); 854 855 nvlist_add_binary(nvl, key, value, value_size); 856 857 ATF_REQUIRE(!nvlist_empty(nvl)); 858 ATF_REQUIRE(nvlist_exists(nvl, key)); 859 ATF_REQUIRE(nvlist_existsf(nvl, "%s", key)); 860 ATF_REQUIRE(nvlist_exists_binary(nvl, key)); 861 ATF_REQUIRE(nvlist_existsf_binary(nvl, "%s", key)); 862 863 ret_value = nvlist_get_binary(nvl, key, &ret_size); 864 ATF_REQUIRE_EQ(value_size, ret_size); 865 ATF_REQUIRE_EQ(memcmp(value, ret_value, ret_size), 0); 866 867 /* nvlist_add_* is required to clone the value, so check for that. */ 868 ATF_REQUIRE(value != ret_value); 869 870 ret_value = nvlist_getf_binary(nvl, &ret_size, "%s", key); 871 ATF_REQUIRE_EQ(value_size, ret_size); 872 ATF_REQUIRE_EQ(memcmp(value, ret_value, ret_size), 0); 873 ATF_REQUIRE(value != ret_value); 874 875 /* Iterate over the nvlist; ensure that it has only our one key. */ 876 it = NULL; 877 ATF_REQUIRE_EQ(strcmp(nvlist_next(nvl, &type, &it), key), 0); 878 ATF_REQUIRE_EQ(type, NV_TYPE_BINARY); 879 ATF_REQUIRE_EQ(nvlist_next(nvl, &type,&it), NULL); 880 881 nvlist_destroy(nvl); 882 free(value); 883} 884 885ATF_INIT_TEST_CASES(tp) 886{ 887 ATF_ADD_TEST_CASE(tp, nvlist_create__is_empty); 888 ATF_ADD_TEST_CASE(tp, nvlist_add_null__single_insert); 889 ATF_ADD_TEST_CASE(tp, nvlist_add_bool__single_insert); 890 ATF_ADD_TEST_CASE(tp, nvlist_add_number__single_insert); 891 ATF_ADD_TEST_CASE(tp, nvlist_add_string__single_insert); 892 ATF_ADD_TEST_CASE(tp, nvlist_add_nvlist__single_insert); 893 ATF_ADD_TEST_CASE(tp, nvlist_add_binary__single_insert); 894} 895/*- 896 * Copyright (c) 2014-2015 Sandvine Inc. All rights reserved. 897 * All rights reserved. 898 * 899 * Redistribution and use in source and binary forms, with or without 900 * modification, are permitted provided that the following conditions 901 * are met: 902 * 1. Redistributions of source code must retain the above copyright 903 * notice, this list of conditions and the following disclaimer. 904 * 2. Redistributions in binary form must reproduce the above copyright 905 * notice, this list of conditions and the following disclaimer in the 906 * documentation and/or other materials provided with the distribution. 907 * 908 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 909 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 910 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 911 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 912 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 913 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 914 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 915 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 916 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 917 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 918 * SUCH DAMAGE. 919 */ 920 921#include <atf-c++.hpp> 922#include <nv.h> 923 924#include <errno.h> 925#include <limits> 926#include <set> 927#include <sstream> 928#include <string> 929 930/* 931 * Test that a newly created nvlist has no errors, and is empty. 932 */ 933ATF_TEST_CASE_WITHOUT_HEAD(nvlist_create__is_empty); 934ATF_TEST_CASE_BODY(nvlist_create__is_empty) 935{ 936 nvlist_t *nvl; 937 int type; 938 void *it; 939 940 nvl = nvlist_create(0); 941 942 ATF_REQUIRE(nvl != NULL); 943 944 ATF_REQUIRE_EQ(nvlist_error(nvl), 0); 945 ATF_REQUIRE(nvlist_empty(nvl)); 946 947 it = NULL; 948 ATF_REQUIRE_EQ(nvlist_next(nvl, &type, &it), NULL); 949 950 nvlist_destroy(nvl); 951} 952 953ATF_TEST_CASE_WITHOUT_HEAD(nvlist_add_null__single_insert); 954ATF_TEST_CASE_BODY(nvlist_add_null__single_insert) 955{ 956 nvlist_t *nvl; 957 void *it; 958 const char *key; 959 int type; 960 961 key = "key"; 962 nvl = nvlist_create(0); 963 964 ATF_REQUIRE(nvl != NULL); 965 ATF_REQUIRE(!nvlist_exists(nvl, key)); 966 967 nvlist_add_null(nvl, key); 968 969 ATF_REQUIRE(!nvlist_empty(nvl)); 970 ATF_REQUIRE(nvlist_exists(nvl, key)); 971 ATF_REQUIRE(nvlist_existsf(nvl, "%s", key)); 972 ATF_REQUIRE(nvlist_exists_null(nvl, key)); 973 ATF_REQUIRE(nvlist_existsf_null(nvl, "key")); 974 975 /* Iterate over the nvlist; ensure that it has only our one key. */ 976 it = NULL; 977 ATF_REQUIRE_EQ(strcmp(nvlist_next(nvl, &type, &it), key), 0); 978 ATF_REQUIRE_EQ(type, NV_TYPE_NULL); 979 ATF_REQUIRE_EQ(nvlist_next(nvl, &type,&it), NULL); 980 981 nvlist_destroy(nvl); 982} 983 984ATF_TEST_CASE_WITHOUT_HEAD(nvlist_add_bool__single_insert); 985ATF_TEST_CASE_BODY(nvlist_add_bool__single_insert) 986{ 987 nvlist_t *nvl; 988 void *it; 989 const char *key; 990 int type; 991 992 key = "name"; 993 nvl = nvlist_create(0); 994 995 ATF_REQUIRE(nvl != NULL); 996 ATF_REQUIRE(!nvlist_exists(nvl, key)); 997 998 nvlist_add_bool(nvl, key, true); 999 1000 ATF_REQUIRE(!nvlist_empty(nvl)); 1001 ATF_REQUIRE(nvlist_exists(nvl, key)); 1002 ATF_REQUIRE(nvlist_existsf(nvl, "%s%s", "na", "me")); 1003 ATF_REQUIRE(nvlist_exists_bool(nvl, key)); 1004 ATF_REQUIRE(nvlist_existsf_bool(nvl, "%s%c", "nam", 'e')); 1005 ATF_REQUIRE_EQ(nvlist_get_bool(nvl, key), true); 1006 ATF_REQUIRE_EQ(nvlist_getf_bool(nvl, "%c%s", 'n', "ame"), true); 1007 1008 /* Iterate over the nvlist; ensure that it has only our one key. */ 1009 it = NULL; 1010 ATF_REQUIRE_EQ(strcmp(nvlist_next(nvl, &type, &it), key), 0); 1011 ATF_REQUIRE_EQ(type, NV_TYPE_BOOL); 1012 ATF_REQUIRE_EQ(nvlist_next(nvl, &type,&it), NULL); 1013 1014 nvlist_destroy(nvl); 1015} 1016 1017ATF_TEST_CASE_WITHOUT_HEAD(nvlist_add_number__single_insert); 1018ATF_TEST_CASE_BODY(nvlist_add_number__single_insert) 1019{ 1020 nvlist_t *nvl; 1021 void *it; 1022 const char *key; 1023 uint64_t value; 1024 int type; 1025 1026 key = "foo123"; 1027 value = 71965; 1028 nvl = nvlist_create(0); 1029 1030 ATF_REQUIRE(nvl != NULL); 1031 ATF_REQUIRE(!nvlist_exists(nvl, key)); 1032 1033 nvlist_add_number(nvl, key, value); 1034 1035 ATF_REQUIRE(!nvlist_empty(nvl)); 1036 ATF_REQUIRE(nvlist_exists(nvl, key)); 1037 ATF_REQUIRE(nvlist_existsf(nvl, "%s%d", "foo", 123)); 1038 ATF_REQUIRE(nvlist_exists_number(nvl, key)); 1039 ATF_REQUIRE(nvlist_existsf_number(nvl, "%s", key)); 1040 ATF_REQUIRE_EQ(nvlist_get_number(nvl, key), value); 1041 ATF_REQUIRE_EQ(nvlist_getf_number(nvl, "%s", key), value); 1042 1043 /* Iterate over the nvlist; ensure that it has only our one key. */ 1044 it = NULL; 1045 ATF_REQUIRE_EQ(strcmp(nvlist_next(nvl, &type, &it), key), 0); 1046 ATF_REQUIRE_EQ(type, NV_TYPE_NUMBER); 1047 ATF_REQUIRE_EQ(nvlist_next(nvl, &type,&it), NULL); 1048 1049 nvlist_destroy(nvl); 1050} 1051 1052ATF_TEST_CASE_WITHOUT_HEAD(nvlist_add_string__single_insert); 1053ATF_TEST_CASE_BODY(nvlist_add_string__single_insert) 1054{ 1055 nvlist_t *nvl; 1056 void *it; 1057 const char *key; 1058 const char *value; 1059 int type; 1060 1061 key = "test"; 1062 value = "fgjdkgjdk"; 1063 nvl = nvlist_create(0); 1064 1065 ATF_REQUIRE(nvl != NULL); 1066 ATF_REQUIRE(!nvlist_exists(nvl, key)); 1067 1068 nvlist_add_string(nvl, key, value); 1069 1070 ATF_REQUIRE(!nvlist_empty(nvl)); 1071 ATF_REQUIRE(nvlist_exists(nvl, key)); 1072 ATF_REQUIRE(nvlist_existsf(nvl, "%s", key)); 1073 ATF_REQUIRE(nvlist_exists_string(nvl, key)); 1074 ATF_REQUIRE(nvlist_existsf_string(nvl, "%s", key)); 1075 ATF_REQUIRE_EQ(strcmp(nvlist_get_string(nvl, key), value), 0); 1076 ATF_REQUIRE_EQ(strcmp(nvlist_getf_string(nvl, "%s", key), value), 0); 1077 1078 /* nvlist_add_* is required to clone the value, so check for that. */ 1079 ATF_REQUIRE(nvlist_get_string(nvl, key) != value); 1080 1081 /* Iterate over the nvlist; ensure that it has only our one key. */ 1082 it = NULL; 1083 ATF_REQUIRE_EQ(strcmp(nvlist_next(nvl, &type, &it), key), 0); 1084 ATF_REQUIRE_EQ(type, NV_TYPE_STRING); 1085 ATF_REQUIRE_EQ(nvlist_next(nvl, &type,&it), NULL); 1086 1087 nvlist_destroy(nvl); 1088} 1089 1090ATF_TEST_CASE_WITHOUT_HEAD(nvlist_add_nvlist__single_insert); 1091ATF_TEST_CASE_BODY(nvlist_add_nvlist__single_insert) 1092{ 1093 nvlist_t *nvl; 1094 void *it; 1095 const char *key, *subkey; 1096 nvlist_t *sublist; 1097 const nvlist_t *value; 1098 int type; 1099 1100 key = "test"; 1101 subkey = "subkey"; 1102 sublist = nvlist_create(0); 1103 nvl = nvlist_create(0); 1104 1105 ATF_REQUIRE(nvl != NULL); 1106 ATF_REQUIRE(!nvlist_exists(nvl, key)); 1107 1108 nvlist_add_null(sublist, subkey); 1109 nvlist_add_nvlist(nvl, key, sublist); 1110 1111 ATF_REQUIRE(!nvlist_empty(nvl)); 1112 ATF_REQUIRE(nvlist_exists(nvl, key)); 1113 ATF_REQUIRE(nvlist_existsf(nvl, "%s", key)); 1114 ATF_REQUIRE(nvlist_exists_nvlist(nvl, key)); 1115 ATF_REQUIRE(nvlist_existsf_nvlist(nvl, "%s", key)); 1116 1117 value = nvlist_get_nvlist(nvl, key); 1118 ATF_REQUIRE(nvlist_exists_null(value, subkey)); 1119 1120 /* nvlist_add_* is required to clone the value, so check for that. */ 1121 ATF_REQUIRE(sublist != value); 1122 1123 value = nvlist_getf_nvlist(nvl, "%s", key); 1124 ATF_REQUIRE(nvlist_exists_null(value, subkey)); 1125 ATF_REQUIRE(sublist != value); 1126 1127 /* Iterate over the nvlist; ensure that it has only our one key. */ 1128 it = NULL; 1129 ATF_REQUIRE_EQ(strcmp(nvlist_next(nvl, &type, &it), key), 0); 1130 ATF_REQUIRE_EQ(type, NV_TYPE_NVLIST); 1131 ATF_REQUIRE_EQ(nvlist_next(nvl, &type,&it), NULL); 1132 1133 nvlist_destroy(sublist); 1134 nvlist_destroy(nvl); 1135} 1136 1137ATF_TEST_CASE_WITHOUT_HEAD(nvlist_add_binary__single_insert); 1138ATF_TEST_CASE_BODY(nvlist_add_binary__single_insert) 1139{ 1140 nvlist_t *nvl; 1141 void *it; 1142 const char *key; 1143 void *value; 1144 const void *ret_value; 1145 size_t value_size, ret_size; 1146 int type; 1147 1148 key = "binary"; 1149 value_size = 13; 1150 value = malloc(value_size); 1151 memset(value, 0xa5, value_size); 1152 nvl = nvlist_create(0); 1153 1154 ATF_REQUIRE(nvl != NULL); 1155 ATF_REQUIRE(!nvlist_exists(nvl, key)); 1156 1157 nvlist_add_binary(nvl, key, value, value_size); 1158 1159 ATF_REQUIRE(!nvlist_empty(nvl)); 1160 ATF_REQUIRE(nvlist_exists(nvl, key)); 1161 ATF_REQUIRE(nvlist_existsf(nvl, "%s", key)); 1162 ATF_REQUIRE(nvlist_exists_binary(nvl, key)); 1163 ATF_REQUIRE(nvlist_existsf_binary(nvl, "%s", key)); 1164 1165 ret_value = nvlist_get_binary(nvl, key, &ret_size); 1166 ATF_REQUIRE_EQ(value_size, ret_size); 1167 ATF_REQUIRE_EQ(memcmp(value, ret_value, ret_size), 0); 1168 1169 /* nvlist_add_* is required to clone the value, so check for that. */ 1170 ATF_REQUIRE(value != ret_value); 1171 1172 ret_value = nvlist_getf_binary(nvl, &ret_size, "%s", key); 1173 ATF_REQUIRE_EQ(value_size, ret_size); 1174 ATF_REQUIRE_EQ(memcmp(value, ret_value, ret_size), 0); 1175 ATF_REQUIRE(value != ret_value); 1176 1177 /* Iterate over the nvlist; ensure that it has only our one key. */ 1178 it = NULL; 1179 ATF_REQUIRE_EQ(strcmp(nvlist_next(nvl, &type, &it), key), 0); 1180 ATF_REQUIRE_EQ(type, NV_TYPE_BINARY); 1181 ATF_REQUIRE_EQ(nvlist_next(nvl, &type,&it), NULL); 1182 1183 nvlist_destroy(nvl); 1184 free(value); 1185} 1186 1187ATF_TEST_CASE_WITHOUT_HEAD(nvlist_clone__empty_nvlist); 1188ATF_TEST_CASE_BODY(nvlist_clone__empty_nvlist) 1189{ 1190 nvlist_t *nvl, *clone; 1191 1192 nvl = nvlist_create(0); 1193 ATF_REQUIRE(nvl != NULL); 1194 1195 clone = nvlist_clone(nvl); 1196 ATF_REQUIRE(clone != NULL); 1197 ATF_REQUIRE(clone != nvl); 1198 ATF_REQUIRE(nvlist_empty(clone)); 1199 1200 nvlist_destroy(clone); 1201 nvlist_destroy(nvl); 1202} 1203 1204ATF_TEST_CASE_WITHOUT_HEAD(nvlist_clone__nonempty_nvlist); 1205ATF_TEST_CASE_BODY(nvlist_clone__nonempty_nvlist) 1206{ 1207 nvlist_t *nvl, *clone; 1208 const char *key; 1209 void *it; 1210 uint64_t value; 1211 int type; 1212 1213 nvl = nvlist_create(0); 1214 ATF_REQUIRE(nvl != NULL); 1215 1216 key = "testkey"; 1217 value = 684874; 1218 nvlist_add_number(nvl, key, value); 1219 1220 clone = nvlist_clone(nvl); 1221 ATF_REQUIRE(clone != NULL); 1222 ATF_REQUIRE(clone != nvl); 1223 ATF_REQUIRE(nvlist_exists_number(clone, key)); 1224 ATF_REQUIRE_EQ(nvlist_get_number(clone, key), value); 1225 1226 /* Iterate over the nvlist; ensure that it has only our one key. */ 1227 it = NULL; 1228 ATF_REQUIRE_EQ(strcmp(nvlist_next(clone, &type, &it), key), 0); 1229 ATF_REQUIRE_EQ(type, NV_TYPE_NUMBER); 1230 ATF_REQUIRE_EQ(nvlist_next(clone, &type, &it), NULL); 1231 1232 nvlist_destroy(clone); 1233 nvlist_destroy(nvl); 1234} 1235 1236static const char * const test_subnvlist_key = "nvlist"; 1237 1238static const char * const test_string_key = "string"; 1239static const char * const test_string_val = "59525"; 1240 1241static nvlist_t* 1242create_test_nvlist(void) 1243{ 1244 nvlist_t *nvl, *sublist; 1245 1246 nvl = nvlist_create(0); 1247 ATF_REQUIRE(nvl != NULL); 1248 1249 sublist = nvlist_create(0); 1250 ATF_REQUIRE(sublist != NULL); 1251 1252 nvlist_add_string(sublist, test_string_key, test_string_val); 1253 nvlist_move_nvlist(nvl, test_subnvlist_key, sublist); 1254 1255 return (nvl); 1256} 1257 1258static void 1259verify_test_nvlist(const nvlist_t *nvl) 1260{ 1261 void *it; 1262 const nvlist_t *value; 1263 int type; 1264 1265 ATF_REQUIRE(nvlist_exists_nvlist(nvl, test_subnvlist_key)); 1266 1267 value = nvlist_get_nvlist(nvl, test_subnvlist_key); 1268 1269 ATF_REQUIRE(nvlist_exists_string(value, test_string_key)); 1270 ATF_REQUIRE_EQ(strcmp(nvlist_get_string(value, test_string_key), test_string_val), 0); 1271 ATF_REQUIRE(nvlist_get_string(value, test_string_key) != test_string_val); 1272 1273 /* Iterate over both nvlists; ensure that each has only the one key. */ 1274 it = NULL; 1275 ATF_REQUIRE_EQ(strcmp(nvlist_next(value, &type, &it), 1276 test_string_key), 0); 1277 ATF_REQUIRE_EQ(type, NV_TYPE_STRING); 1278 ATF_REQUIRE_EQ(nvlist_next(value, &type, &it), NULL); 1279 1280 it = NULL; 1281 ATF_REQUIRE_EQ(strcmp(nvlist_next(nvl, &type, &it), 1282 test_subnvlist_key), 0); 1283 ATF_REQUIRE_EQ(type, NV_TYPE_NVLIST); 1284 ATF_REQUIRE_EQ(nvlist_next(nvl, &type, &it), NULL); 1285} 1286 1287ATF_TEST_CASE_WITHOUT_HEAD(nvlist_clone__nested_nvlist); 1288ATF_TEST_CASE_BODY(nvlist_clone__nested_nvlist) 1289{ 1290 nvlist_t *nvl, *clone; 1291 1292 nvl = create_test_nvlist(); 1293 clone = nvlist_clone(nvl); 1294 1295 ATF_REQUIRE(clone != NULL); 1296 ATF_REQUIRE(clone != nvl); 1297 verify_test_nvlist(clone); 1298 1299 nvlist_destroy(clone); 1300 nvlist_destroy(nvl); 1301} 1302 1303ATF_TEST_CASE_WITHOUT_HEAD(nvlist_pack__empty_nvlist); 1304ATF_TEST_CASE_BODY(nvlist_pack__empty_nvlist) 1305{ 1306 nvlist_t *nvl, *unpacked; 1307 void *packed; 1308 size_t packed_size; 1309 1310 nvl = nvlist_create(0); 1311 ATF_REQUIRE(nvl != NULL); 1312 1313 packed = nvlist_pack(nvl, &packed_size); 1314 ATF_REQUIRE(packed != NULL); 1315 1316 unpacked = nvlist_unpack(packed, packed_size); 1317 ATF_REQUIRE(unpacked != NULL); 1318 ATF_REQUIRE(unpacked != nvl); 1319 ATF_REQUIRE(nvlist_empty(unpacked)); 1320 1321 nvlist_destroy(unpacked); 1322 nvlist_destroy(nvl); 1323 free(packed); 1324} 1325 1326static void 1327verify_null(const nvlist_t *nvl, int type) 1328{ 1329 1330 ATF_REQUIRE_EQ(type, NV_TYPE_NULL); 1331} 1332 1333static void 1334verify_number(const nvlist_t *nvl, const char *name, int type, uint64_t value) 1335{ 1336 1337 ATF_REQUIRE_EQ(type, NV_TYPE_NUMBER); 1338 ATF_REQUIRE_EQ(nvlist_get_number(nvl, name), value); 1339} 1340 1341static void 1342verify_string(const nvlist_t *nvl, const char *name, int type, 1343 const char * value) 1344{ 1345 1346 ATF_REQUIRE_EQ(type, NV_TYPE_STRING); 1347 ATF_REQUIRE_EQ(strcmp(nvlist_get_string(nvl, name), value), 0); 1348} 1349 1350static void 1351verify_nvlist(const nvlist_t *nvl, const char *name, int type) 1352{ 1353 1354 ATF_REQUIRE_EQ(type, NV_TYPE_NVLIST); 1355 verify_test_nvlist(nvlist_get_nvlist(nvl, name)); 1356} 1357 1358static void 1359verify_binary(const nvlist_t *nvl, const char *name, int type, 1360 const void * value, size_t size) 1361{ 1362 const void *actual_value; 1363 size_t actual_size; 1364 1365 ATF_REQUIRE_EQ(type, NV_TYPE_BINARY); 1366 actual_value = nvlist_get_binary(nvl, name, &actual_size); 1367 ATF_REQUIRE_EQ(size, actual_size); 1368 ATF_REQUIRE_EQ(memcmp(value, actual_value, size), 0); 1369} 1370 1371ATF_TEST_CASE_WITHOUT_HEAD(nvlist_pack__multiple_values); 1372ATF_TEST_CASE_BODY(nvlist_pack__multiple_values) 1373{ 1374 std::ostringstream msg; 1375 std::set<std::string> keys_seen; 1376 nvlist_t *nvl, *unpacked, *nvvalue; 1377 const char *nullkey, *numkey, *strkey, *nvkey, *binkey, *name; 1378 int numvalue; 1379 const char * strvalue; 1380 void *binvalue, *packed, *it; 1381 size_t binsize, packed_size; 1382 int type; 1383 1384 nvl = nvlist_create(0); 1385 1386 nullkey = "null"; 1387 nvlist_add_null(nvl, nullkey); 1388 1389 numkey = "number"; 1390 numvalue = 939853984; 1391 nvlist_add_number(nvl, numkey, numvalue); 1392 1393 strkey = "string"; 1394 strvalue = "jfieutijf"; 1395 nvlist_add_string(nvl, strkey, strvalue); 1396 1397 nvkey = "nvlist"; 1398 nvvalue = create_test_nvlist(); 1399 nvlist_move_nvlist(nvl, nvkey, nvvalue); 1400 1401 binkey = "binary"; 1402 binsize = 4; 1403 binvalue = malloc(binsize); 1404 memset(binvalue, 'b', binsize); 1405 nvlist_move_binary(nvl, binkey, binvalue, binsize); 1406 1407 packed = nvlist_pack(nvl, &packed_size); 1408 ATF_REQUIRE(packed != NULL); 1409 1410 unpacked = nvlist_unpack(packed, packed_size); 1411 ATF_REQUIRE(unpacked != 0); 1412 1413 it = NULL; 1414 while ((name = nvlist_next(unpacked, &type, &it)) != NULL) { 1415 /* Ensure that we see every key only once. */ 1416 ATF_REQUIRE_EQ(keys_seen.count(name), 0); 1417 1418 if (strcmp(name, nullkey) == 0) 1419 verify_null(unpacked, type); 1420 else if (strcmp(name, numkey) == 0) 1421 verify_number(unpacked, name, type, numvalue); 1422 else if (strcmp(name, strkey) == 0) 1423 verify_string(unpacked, name, type, strvalue); 1424 else if (strcmp(name, nvkey) == 0) 1425 verify_nvlist(unpacked, name, type); 1426 else if (strcmp(name, binkey) == 0) 1427 verify_binary(unpacked, name, type, binvalue, binsize); 1428 else { 1429 msg << "Unexpected key :'" << name << "'"; 1430 ATF_FAIL(msg.str().c_str()); 1431 } 1432 1433 keys_seen.insert(name); 1434 } 1435 1436 /* Ensure that we saw every key. */ 1437 ATF_REQUIRE_EQ(keys_seen.size(), 5); 1438 1439 nvlist_destroy(nvl); 1440 nvlist_destroy(unpacked); 1441 free(packed); 1442} 1443 1444ATF_TEST_CASE_WITHOUT_HEAD(nvlist_unpack__duplicate_key); 1445ATF_TEST_CASE_BODY(nvlist_unpack__duplicate_key) 1446{ 1447 nvlist_t *nvl, *unpacked; 1448 const char *key1, *key2; 1449 void *packed, *keypos; 1450 size_t size, keylen; 1451 1452 nvl = nvlist_create(0); 1453 1454 key1 = "key1"; 1455 keylen = strlen(key1); 1456 nvlist_add_number(nvl, key1, 5); 1457 1458 key2 = "key2"; 1459 ATF_REQUIRE_EQ(keylen, strlen(key2)); 1460 nvlist_add_number(nvl, key2, 10); 1461 1462 packed = nvlist_pack(nvl, &size); 1463 1464 /* 1465 * Mangle the packed nvlist by replacing key1 with key2, creating a 1466 * packed nvlist with a duplicate key. 1467 */ 1468 keypos = memmem(packed, size, key1, keylen); 1469 ATF_REQUIRE(keypos != NULL); 1470 memcpy(keypos, key2, keylen); 1471 1472 unpacked = nvlist_unpack(packed, size); 1473 ATF_REQUIRE(nvlist_error(unpacked) != 0); 1474 1475 free(packed); 1476 nvlist_destroy(nvl); 1477 nvlist_destroy(unpacked); 1478} 1479 1480ATF_TEST_CASE_WITHOUT_HEAD(nvlist_move_string__single_insert); 1481ATF_TEST_CASE_BODY(nvlist_move_string__single_insert) 1482{ 1483 nvlist_t *nvl; 1484 const char *key; 1485 char *value; 1486 1487 nvl = nvlist_create(0); 1488 ATF_REQUIRE(nvl != NULL); 1489 1490 key = "testkey"; 1491 value = strdup("testval"); 1492 ATF_REQUIRE(value != NULL); 1493 1494 nvlist_move_string(nvl, key, value); 1495 ATF_REQUIRE_EQ(nvlist_get_string(nvl, key), value); 1496 1497 nvlist_destroy(nvl); 1498} 1499 1500ATF_TEST_CASE_WITHOUT_HEAD(nvlist_move_nvlist__null_child); 1501ATF_TEST_CASE_BODY(nvlist_move_nvlist__null_child) 1502{ 1503 nvlist_t *parent; 1504 1505 parent = nvlist_create(0); 1506 1507 nvlist_move_nvlist(parent, "test", NULL); 1508 1509 ATF_REQUIRE(nvlist_error(parent) != 0); 1510 1511 nvlist_destroy(parent); 1512} 1513 1514ATF_TEST_CASE_WITHOUT_HEAD(nvlist_move_nvlist__single_insert); 1515ATF_TEST_CASE_BODY(nvlist_move_nvlist__single_insert) 1516{ 1517 nvlist_t *nvl; 1518 const char *key; 1519 nvlist_t *value; 1520 1521 nvl = nvlist_create(0); 1522 ATF_REQUIRE(nvl != NULL); 1523 1524 key = "testkey"; 1525 value = nvlist_create(0); 1526 ATF_REQUIRE(value != NULL); 1527 1528 nvlist_move_nvlist(nvl, key, value); 1529 ATF_REQUIRE_EQ(nvlist_get_nvlist(nvl, key), value); 1530 1531 nvlist_destroy(nvl); 1532} 1533 1534ATF_TEST_CASE_WITHOUT_HEAD(nvlist_move_binary__single_insert); 1535ATF_TEST_CASE_BODY(nvlist_move_binary__single_insert) 1536{ 1537 nvlist_t *nvl; 1538 const char *key; 1539 void *value; 1540 size_t size, actual_size; 1541 1542 nvl = nvlist_create(0); 1543 ATF_REQUIRE(nvl != NULL); 1544 1545 key = "testkey"; 1546 size = 73; 1547 value = malloc(size); 1548 ATF_REQUIRE(value != NULL); 1549 1550 nvlist_move_binary(nvl, key, value, size); 1551 ATF_REQUIRE_EQ(nvlist_get_binary(nvl, key, &actual_size), value); 1552 ATF_REQUIRE_EQ(size, actual_size); 1553 1554 nvlist_destroy(nvl); 1555} 1556 1557ATF_TEST_CASE_WITHOUT_HEAD(nvlist_take_bool__single_remove); 1558ATF_TEST_CASE_BODY(nvlist_take_bool__single_remove) 1559{ 1560 nvlist_t *nvl; 1561 const char *testkey; 1562 bool testval; 1563 1564 nvl = nvlist_create(0); 1565 ATF_REQUIRE(nvl != NULL); 1566 1567 testkey = "boolkey"; 1568 testval = false; 1569 nvlist_add_bool(nvl, testkey, testval); 1570 1571 ATF_REQUIRE_EQ(nvlist_take_bool(nvl, testkey), testval); 1572 ATF_REQUIRE(nvlist_empty(nvl)); 1573 1574 nvlist_destroy(nvl); 1575} 1576 1577ATF_TEST_CASE_WITHOUT_HEAD(nvlist_take_bool__other_keys_unchanged); 1578ATF_TEST_CASE_BODY(nvlist_take_bool__other_keys_unchanged) 1579{ 1580 nvlist_t *nvl; 1581 const char *testkey, *otherkey1, *otherkey2; 1582 bool testval, otherval1; 1583 nvlist_t *otherval2; 1584 1585 nvl = nvlist_create(0); 1586 ATF_REQUIRE(nvl != NULL); 1587 1588 testkey = "boolkey"; 1589 testval = true; 1590 nvlist_add_bool(nvl, testkey, testval); 1591 1592 otherkey1 = "key1"; 1593 otherval1 = false; 1594 nvlist_add_bool(nvl, otherkey1, otherval1); 1595 1596 otherkey2 = "key2"; 1597 otherval2 = create_test_nvlist(); 1598 nvlist_move_nvlist(nvl, otherkey2, otherval2); 1599 1600 ATF_REQUIRE_EQ(nvlist_take_bool(nvl, testkey), testval); 1601 1602 ATF_REQUIRE(nvlist_exists_bool(nvl, otherkey1)); 1603 ATF_REQUIRE_EQ(nvlist_get_bool(nvl, otherkey1), otherval1); 1604 1605 ATF_REQUIRE(nvlist_exists_nvlist(nvl, otherkey2)); 1606 verify_test_nvlist(nvlist_get_nvlist(nvl, otherkey2)); 1607 1608 nvlist_destroy(nvl); 1609} 1610 1611ATF_TEST_CASE_WITHOUT_HEAD(nvlist_take_number__single_remove); 1612ATF_TEST_CASE_BODY(nvlist_take_number__single_remove) 1613{ 1614 nvlist_t *nvl; 1615 const char *testkey; 1616 uint64_t testval; 1617 1618 nvl = nvlist_create(0); 1619 ATF_REQUIRE(nvl != NULL); 1620 1621 testkey = "numkey"; 1622 testval = std::numeric_limits<uint64_t>::max(); 1623 nvlist_add_number(nvl, testkey, testval); 1624 1625 ATF_REQUIRE_EQ(nvlist_take_number(nvl, testkey), testval); 1626 ATF_REQUIRE(nvlist_empty(nvl)); 1627 1628 nvlist_destroy(nvl); 1629} 1630 1631ATF_TEST_CASE_WITHOUT_HEAD(nvlist_take_number__other_keys_unchanged); 1632ATF_TEST_CASE_BODY(nvlist_take_number__other_keys_unchanged) 1633{ 1634 nvlist_t *nvl; 1635 const char *testkey, *otherkey1, *otherkey2; 1636 uint64_t testval, otherval1; 1637 const char *otherval2; 1638 1639 nvl = nvlist_create(0); 1640 ATF_REQUIRE(nvl != NULL); 1641 1642 otherkey1 = "key1"; 1643 otherval1 = 5; 1644 nvlist_add_number(nvl, otherkey1, otherval1); 1645 1646 testkey = "numkey"; 1647 testval = 1654; 1648 nvlist_add_number(nvl, testkey, testval); 1649 1650 otherkey2 = "key2"; 1651 otherval2 = "string"; 1652 nvlist_add_string(nvl, otherkey2, otherval2); 1653 1654 ATF_REQUIRE_EQ(nvlist_take_number(nvl, testkey), testval); 1655 1656 ATF_REQUIRE(nvlist_exists_number(nvl, otherkey1)); 1657 ATF_REQUIRE_EQ(nvlist_get_number(nvl, otherkey1), otherval1); 1658 1659 ATF_REQUIRE(nvlist_exists_string(nvl, otherkey2)); 1660 ATF_REQUIRE_EQ(strcmp(nvlist_get_string(nvl, otherkey2), otherval2), 0); 1661 1662 nvlist_destroy(nvl); 1663} 1664 1665ATF_TEST_CASE_WITHOUT_HEAD(nvlist_take_string__single_remove); 1666ATF_TEST_CASE_BODY(nvlist_take_string__single_remove) 1667{ 1668 nvlist_t *nvl; 1669 const char *testkey; 1670 const char *testval; 1671 1672 nvl = nvlist_create(0); 1673 ATF_REQUIRE(nvl != NULL); 1674 1675 testkey = "numkey"; 1676 testval = "nvlist"; 1677 nvlist_add_string(nvl, testkey, testval); 1678 1679 ATF_REQUIRE_EQ(strcmp(nvlist_take_string(nvl, testkey), testval), 0); 1680 ATF_REQUIRE(nvlist_empty(nvl)); 1681 1682 nvlist_destroy(nvl); 1683} 1684 1685ATF_TEST_CASE_WITHOUT_HEAD(nvlist_take_string__other_keys_unchanged); 1686ATF_TEST_CASE_BODY(nvlist_take_string__other_keys_unchanged) 1687{ 1688 nvlist_t *nvl; 1689 const char *testkey, *otherkey1, *otherkey2; 1690 const char *testval, *otherval1; 1691 bool otherval2; 1692 1693 nvl = nvlist_create(0); 1694 ATF_REQUIRE(nvl != NULL); 1695 1696 otherkey1 = "key1"; 1697 otherval1 = "fjdifjdk"; 1698 nvlist_add_string(nvl, otherkey1, otherval1); 1699 1700 otherkey2 = "key2"; 1701 otherval2 = true; 1702 nvlist_add_bool(nvl, otherkey2, otherval2); 1703 1704 testkey = "strkey"; 1705 testval = "1654"; 1706 nvlist_add_string(nvl, testkey, testval); 1707 1708 ATF_REQUIRE_EQ(strcmp(nvlist_take_string(nvl, testkey), testval), 0); 1709 1710 ATF_REQUIRE(nvlist_exists_string(nvl, otherkey1)); 1711 ATF_REQUIRE_EQ(strcmp(nvlist_get_string(nvl, otherkey1), otherval1), 0); 1712 1713 ATF_REQUIRE(nvlist_exists_bool(nvl, otherkey2)); 1714 ATF_REQUIRE_EQ(nvlist_get_bool(nvl, otherkey2), otherval2); 1715 1716 nvlist_destroy(nvl); 1717} 1718 1719ATF_TEST_CASE_WITHOUT_HEAD(nvlist_take_nvlist__single_remove); 1720ATF_TEST_CASE_BODY(nvlist_take_nvlist__single_remove) 1721{ 1722 nvlist_t *nvl; 1723 const char *testkey; 1724 nvlist_t *testval; 1725 1726 nvl = nvlist_create(0); 1727 ATF_REQUIRE(nvl != NULL); 1728 1729 testkey = "numkey"; 1730 testval = create_test_nvlist(); 1731 nvlist_move_nvlist(nvl, testkey, testval); 1732 1733 verify_test_nvlist(nvlist_take_nvlist(nvl, testkey)); 1734 ATF_REQUIRE(nvlist_empty(nvl)); 1735 1736 nvlist_destroy(nvl); 1737} 1738 1739ATF_TEST_CASE_WITHOUT_HEAD(nvlist_take_nvlist__other_keys_unchanged); 1740ATF_TEST_CASE_BODY(nvlist_take_nvlist__other_keys_unchanged) 1741{ 1742 nvlist_t *nvl; 1743 const char *testkey, *otherkey1, *otherkey2; 1744 nvlist_t *testval, *otherval1; 1745 1746 nvl = nvlist_create(0); 1747 ATF_REQUIRE(nvl != NULL); 1748 1749 testkey = "strkey"; 1750 testval = create_test_nvlist(); 1751 nvlist_move_nvlist(nvl, testkey, testval); 1752 1753 otherkey1 = "key1"; 1754 otherval1 = nvlist_create(0); 1755 nvlist_move_nvlist(nvl, otherkey1, otherval1); 1756 1757 otherkey2 = "key2"; 1758 nvlist_add_null(nvl, otherkey2); 1759 1760 verify_test_nvlist(nvlist_take_nvlist(nvl, testkey)); 1761 1762 ATF_REQUIRE(nvlist_exists_nvlist(nvl, otherkey1)); 1763 ATF_REQUIRE(nvlist_empty(nvlist_get_nvlist(nvl, otherkey1))); 1764 1765 ATF_REQUIRE(nvlist_exists_null(nvl, otherkey2)); 1766 1767 nvlist_destroy(nvl); 1768} 1769 1770ATF_TEST_CASE_WITHOUT_HEAD(nvlist_take_binary__single_remove); 1771ATF_TEST_CASE_BODY(nvlist_take_binary__single_remove) 1772{ 1773 nvlist_t *nvl; 1774 const char *testkey; 1775 void *testval; 1776 const void *actual_val; 1777 size_t testsize, actual_size; 1778 1779 nvl = nvlist_create(0); 1780 ATF_REQUIRE(nvl != NULL); 1781 1782 testkey = "numkey"; 1783 testsize = 457; 1784 testval = malloc(testsize); 1785 memset(testval, '5', testsize); 1786 nvlist_move_binary(nvl, testkey, testval, testsize); 1787 1788 actual_val = nvlist_take_binary(nvl, testkey, &actual_size); 1789 ATF_REQUIRE_EQ(testsize, actual_size); 1790 ATF_REQUIRE_EQ(memcmp(actual_val, testval, testsize), 0); 1791 ATF_REQUIRE(nvlist_empty(nvl)); 1792 1793 nvlist_destroy(nvl); 1794} 1795 1796ATF_TEST_CASE_WITHOUT_HEAD(nvlist_take_binary__other_keys_unchanged); 1797ATF_TEST_CASE_BODY(nvlist_take_binary__other_keys_unchanged) 1798{ 1799 nvlist_t *nvl; 1800 const char *testkey, *otherkey1, *otherkey2; 1801 const void *actual_value; 1802 char testval[] = "gjiertj"; 1803 char otherval1[] = "fdreg"; 1804 size_t testsize, othersize, actual_size; 1805 bool otherval2; 1806 1807 nvl = nvlist_create(0); 1808 ATF_REQUIRE(nvl != NULL); 1809 1810 otherkey1 = "key1"; 1811 othersize = sizeof(otherval1); 1812 nvlist_add_binary(nvl, otherkey1, otherval1, othersize); 1813 1814 otherkey2 = "key2"; 1815 otherval2 = true; 1816 nvlist_add_bool(nvl, otherkey2, otherval2); 1817 1818 testkey = "strkey"; 1819 testsize = sizeof(testval); 1820 nvlist_add_binary(nvl, testkey, testval, testsize); 1821 1822 actual_value = nvlist_take_binary(nvl, testkey, &actual_size); 1823 ATF_REQUIRE_EQ(testsize, actual_size); 1824 ATF_REQUIRE_EQ(memcmp(actual_value, testval, testsize), 0); 1825 1826 ATF_REQUIRE(nvlist_exists_binary(nvl, otherkey1)); 1827 actual_value = nvlist_get_binary(nvl, otherkey1, &actual_size); 1828 ATF_REQUIRE_EQ(othersize, actual_size); 1829 ATF_REQUIRE_EQ(memcmp(actual_value, otherval1, othersize), 0); 1830 1831 ATF_REQUIRE(nvlist_exists_bool(nvl, otherkey2)); 1832 ATF_REQUIRE_EQ(nvlist_get_bool(nvl, otherkey2), otherval2); 1833 1834 nvlist_destroy(nvl); 1835} 1836 1837ATF_TEST_CASE_WITHOUT_HEAD(nvlist_free__single_null); 1838ATF_TEST_CASE_BODY(nvlist_free__single_null) 1839{ 1840 nvlist_t *nvl; 1841 const char *key; 1842 1843 nvl = nvlist_create(0); 1844 key = "test"; 1845 nvlist_add_null(nvl, key); 1846 1847 nvlist_free(nvl, key); 1848 ATF_REQUIRE(nvlist_empty(nvl)); 1849 1850 nvlist_destroy(nvl); 1851} 1852 1853ATF_TEST_CASE_WITHOUT_HEAD(nvlist_free__single_bool); 1854ATF_TEST_CASE_BODY(nvlist_free__single_bool) 1855{ 1856 nvlist_t *nvl; 1857 const char *key; 1858 1859 nvl = nvlist_create(0); 1860 key = "test"; 1861 nvlist_add_bool(nvl, key, true); 1862 1863 nvlist_free(nvl, key); 1864 ATF_REQUIRE(nvlist_empty(nvl)); 1865 1866 nvlist_destroy(nvl); 1867} 1868 1869ATF_TEST_CASE_WITHOUT_HEAD(nvlist_free__single_number); 1870ATF_TEST_CASE_BODY(nvlist_free__single_number) 1871{ 1872 nvlist_t *nvl; 1873 const char *key; 1874 1875 nvl = nvlist_create(0); 1876 key = "test"; 1877 nvlist_add_number(nvl, key, 584); 1878 1879 nvlist_free(nvl, key); 1880 ATF_REQUIRE(nvlist_empty(nvl)); 1881 1882 nvlist_destroy(nvl); 1883} 1884 1885ATF_TEST_CASE_WITHOUT_HEAD(nvlist_free__single_string); 1886ATF_TEST_CASE_BODY(nvlist_free__single_string) 1887{ 1888 nvlist_t *nvl; 1889 const char *key; 1890 1891 nvl = nvlist_create(0); 1892 key = "test"; 1893 nvlist_add_string(nvl, key, "gjkfkjd"); 1894 1895 nvlist_free(nvl, key); 1896 ATF_REQUIRE(nvlist_empty(nvl)); 1897 1898 nvlist_destroy(nvl); 1899} 1900 1901ATF_TEST_CASE_WITHOUT_HEAD(nvlist_free__single_nvlist); 1902ATF_TEST_CASE_BODY(nvlist_free__single_nvlist) 1903{ 1904 nvlist_t *nvl; 1905 const char *key; 1906 1907 nvl = nvlist_create(0); 1908 key = "test"; 1909 nvlist_add_nvlist(nvl, key, nvlist_create(0)); 1910 1911 nvlist_free(nvl, key); 1912 ATF_REQUIRE(nvlist_empty(nvl)); 1913 1914 nvlist_destroy(nvl); 1915} 1916 1917ATF_TEST_CASE_WITHOUT_HEAD(nvlist_free__single_binary); 1918ATF_TEST_CASE_BODY(nvlist_free__single_binary) 1919{ 1920 nvlist_t *nvl; 1921 const char *key; 1922 1923 nvl = nvlist_create(0); 1924 key = "test"; 1925 nvlist_add_binary(nvl, key, "jgjgfd", 6); 1926 1927 nvlist_free(nvl, key); 1928 ATF_REQUIRE(nvlist_empty(nvl)); 1929 1930 nvlist_destroy(nvl); 1931} 1932 1933ATF_TEST_CASE_WITHOUT_HEAD(nvlist_free_null__single_null); 1934ATF_TEST_CASE_BODY(nvlist_free_null__single_null) 1935{ 1936 nvlist_t *nvl; 1937 const char *key; 1938 1939 nvl = nvlist_create(0); 1940 key = "test"; 1941 nvlist_add_null(nvl, key); 1942 1943 nvlist_free_null(nvl, key); 1944 ATF_REQUIRE(nvlist_empty(nvl)); 1945 1946 nvlist_destroy(nvl); 1947} 1948 1949ATF_TEST_CASE_WITHOUT_HEAD(nvlist_free_bool__single_bool); 1950ATF_TEST_CASE_BODY(nvlist_free_bool__single_bool) 1951{ 1952 nvlist_t *nvl; 1953 const char *key; 1954 1955 nvl = nvlist_create(0); 1956 key = "test"; 1957 nvlist_add_bool(nvl, key, true); 1958 1959 nvlist_free_bool(nvl, key); 1960 ATF_REQUIRE(nvlist_empty(nvl)); 1961 1962 nvlist_destroy(nvl); 1963} 1964 1965ATF_TEST_CASE_WITHOUT_HEAD(nvlist_free_number__single_number); 1966ATF_TEST_CASE_BODY(nvlist_free_number__single_number) 1967{ 1968 nvlist_t *nvl; 1969 const char *key; 1970 1971 nvl = nvlist_create(0); 1972 key = "test"; 1973 nvlist_add_number(nvl, key, 584); 1974 1975 nvlist_free_number(nvl, key); 1976 ATF_REQUIRE(nvlist_empty(nvl)); 1977 1978 nvlist_destroy(nvl); 1979} 1980 1981ATF_TEST_CASE_WITHOUT_HEAD(nvlist_free_string__single_string); 1982ATF_TEST_CASE_BODY(nvlist_free_string__single_string) 1983{ 1984 nvlist_t *nvl; 1985 const char *key; 1986 1987 nvl = nvlist_create(0); 1988 key = "test"; 1989 nvlist_add_string(nvl, key, "gjkfkjd"); 1990 1991 nvlist_free_string(nvl, key); 1992 ATF_REQUIRE(nvlist_empty(nvl)); 1993 1994 nvlist_destroy(nvl); 1995} 1996 1997ATF_TEST_CASE_WITHOUT_HEAD(nvlist_free_nvlist__single_nvlist); 1998ATF_TEST_CASE_BODY(nvlist_free_nvlist__single_nvlist) 1999{ 2000 nvlist_t *nvl; 2001 const char *key; 2002 2003 nvl = nvlist_create(0); 2004 key = "test"; 2005 nvlist_add_nvlist(nvl, key, nvlist_create(0)); 2006 2007 nvlist_free_nvlist(nvl, key); 2008 ATF_REQUIRE(nvlist_empty(nvl)); 2009 2010 nvlist_destroy(nvl); 2011} 2012 2013ATF_TEST_CASE_WITHOUT_HEAD(nvlist_free_binary__single_binary); 2014ATF_TEST_CASE_BODY(nvlist_free_binary__single_binary) 2015{ 2016 nvlist_t *nvl; 2017 const char *key; 2018 2019 nvl = nvlist_create(0); 2020 key = "test"; 2021 nvlist_add_binary(nvl, key, "jgjgfd", 6); 2022 2023 nvlist_free_binary(nvl, key); 2024 ATF_REQUIRE(nvlist_empty(nvl)); 2025 2026 nvlist_destroy(nvl); 2027} 2028 2029ATF_INIT_TEST_CASES(tp) 2030{ 2031 ATF_ADD_TEST_CASE(tp, nvlist_create__is_empty); 2032 ATF_ADD_TEST_CASE(tp, nvlist_add_null__single_insert); 2033 ATF_ADD_TEST_CASE(tp, nvlist_add_bool__single_insert); 2034 ATF_ADD_TEST_CASE(tp, nvlist_add_number__single_insert); 2035 ATF_ADD_TEST_CASE(tp, nvlist_add_string__single_insert); 2036 ATF_ADD_TEST_CASE(tp, nvlist_add_nvlist__single_insert); 2037 ATF_ADD_TEST_CASE(tp, nvlist_add_binary__single_insert); 2038 2039 ATF_ADD_TEST_CASE(tp, nvlist_clone__empty_nvlist); 2040 ATF_ADD_TEST_CASE(tp, nvlist_clone__nonempty_nvlist); 2041 ATF_ADD_TEST_CASE(tp, nvlist_clone__nested_nvlist); 2042 2043 ATF_ADD_TEST_CASE(tp, nvlist_pack__empty_nvlist); 2044 ATF_ADD_TEST_CASE(tp, nvlist_pack__multiple_values); 2045 ATF_ADD_TEST_CASE(tp, nvlist_unpack__duplicate_key); 2046 2047 ATF_ADD_TEST_CASE(tp, nvlist_move_string__single_insert); 2048 ATF_ADD_TEST_CASE(tp, nvlist_move_nvlist__single_insert); 2049 ATF_ADD_TEST_CASE(tp, nvlist_move_nvlist__null_child); 2050 ATF_ADD_TEST_CASE(tp, nvlist_move_binary__single_insert); 2051 2052 ATF_ADD_TEST_CASE(tp, nvlist_take_bool__single_remove); 2053 ATF_ADD_TEST_CASE(tp, nvlist_take_bool__other_keys_unchanged); 2054 ATF_ADD_TEST_CASE(tp, nvlist_take_number__single_remove); 2055 ATF_ADD_TEST_CASE(tp, nvlist_take_number__other_keys_unchanged); 2056 ATF_ADD_TEST_CASE(tp, nvlist_take_string__single_remove); 2057 ATF_ADD_TEST_CASE(tp, nvlist_take_string__other_keys_unchanged); 2058 ATF_ADD_TEST_CASE(tp, nvlist_take_nvlist__single_remove); 2059 ATF_ADD_TEST_CASE(tp, nvlist_take_nvlist__other_keys_unchanged); 2060 ATF_ADD_TEST_CASE(tp, nvlist_take_binary__single_remove); 2061 ATF_ADD_TEST_CASE(tp, nvlist_take_binary__other_keys_unchanged); 2062 2063 ATF_ADD_TEST_CASE(tp, nvlist_free__single_null); 2064 ATF_ADD_TEST_CASE(tp, nvlist_free__single_bool); 2065 ATF_ADD_TEST_CASE(tp, nvlist_free__single_number); 2066 ATF_ADD_TEST_CASE(tp, nvlist_free__single_string); 2067 ATF_ADD_TEST_CASE(tp, nvlist_free__single_nvlist); 2068 ATF_ADD_TEST_CASE(tp, nvlist_free__single_binary); 2069 2070 ATF_ADD_TEST_CASE(tp, nvlist_free_null__single_null); 2071 ATF_ADD_TEST_CASE(tp, nvlist_free_bool__single_bool); 2072 ATF_ADD_TEST_CASE(tp, nvlist_free_number__single_number); 2073 ATF_ADD_TEST_CASE(tp, nvlist_free_string__single_string); 2074 ATF_ADD_TEST_CASE(tp, nvlist_free_nvlist__single_nvlist); 2075 ATF_ADD_TEST_CASE(tp, nvlist_free_binary__single_binary); 2076} 2077