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$"); 29 30 #include <atf-c++.hpp> 31 #include <dnv.h> 32 #include <nv.h> 33 34 ATF_TEST_CASE_WITHOUT_HEAD(dnvlist_get_bool__present); 35 ATF_TEST_CASE_BODY(dnvlist_get_bool__present) 36 { 37 nvlist_t *nvl; 38 const char *key; 39 bool value; 40 41 nvl = nvlist_create(0); 42 43 key = "name"; 44 value = true; 45 nvlist_add_bool(nvl, key, value); 46 47 ATF_REQUIRE_EQ(dnvlist_get_bool(nvl, key, false), value); 48 ATF_REQUIRE_EQ(dnvlist_getf_bool(nvl, false, "%c%s", 'n', "ame"), value); 49 50 nvlist_destroy(nvl); 51 } 52 53 ATF_TEST_CASE_WITHOUT_HEAD(dnvlist_get_bool__default_value); 54 ATF_TEST_CASE_BODY(dnvlist_get_bool__default_value) 55 { 56 nvlist_t *nvl; 57 const char *key; 58 59 key = "123"; 60 nvl = nvlist_create(0); 61 62 ATF_REQUIRE_EQ(dnvlist_get_bool(nvl, key, false), false); 63 ATF_REQUIRE_EQ(dnvlist_getf_bool(nvl, true, "%d", 123), true); 64 65 nvlist_add_bool(nvl, key, true); 66 67 ATF_REQUIRE_EQ(dnvlist_get_bool(nvl, "otherkey", true), true); 68 ATF_REQUIRE_EQ(dnvlist_getf_bool(nvl, false, "%d%c", 12, 'c'), false); 69 70 nvlist_destroy(nvl); 71 } 72 73 ATF_TEST_CASE_WITHOUT_HEAD(dnvlist_get_number__present); 74 ATF_TEST_CASE_BODY(dnvlist_get_number__present) 75 { 76 nvlist_t *nvl; 77 const char *key; 78 uint64_t value; 79 80 nvl = nvlist_create(0); 81 82 key = "key"; 83 value = 48952; 84 nvlist_add_number(nvl, key, value); 85 86 ATF_REQUIRE_EQ(dnvlist_get_number(nvl, key, 19), value); 87 ATF_REQUIRE_EQ(dnvlist_getf_number(nvl, 65, "key"), value); 88 89 nvlist_destroy(nvl); 90 } 91 92 ATF_TEST_CASE_WITHOUT_HEAD(dnvlist_get_number__default_value); 93 ATF_TEST_CASE_BODY(dnvlist_get_number__default_value) 94 { 95 nvlist_t *nvl; 96 const char *key; 97 98 key = "123"; 99 nvl = nvlist_create(0); 100 101 ATF_REQUIRE_EQ(dnvlist_get_number(nvl, key, 5), 5); 102 ATF_REQUIRE_EQ(dnvlist_getf_number(nvl, 12, "%s", key), 12); 103 104 nvlist_add_number(nvl, key, 24841); 105 106 ATF_REQUIRE_EQ(dnvlist_get_number(nvl, "hthth", 184), 184); 107 ATF_REQUIRE_EQ(dnvlist_getf_number(nvl, 5641, "%d", 1234), 5641); 108 109 nvlist_destroy(nvl); 110 } 111 112 ATF_TEST_CASE_WITHOUT_HEAD(dnvlist_get_string__present); 113 ATF_TEST_CASE_BODY(dnvlist_get_string__present) 114 { 115 nvlist_t *nvl; 116 const char *key; 117 const char *value, *actual_value; 118 119 nvl = nvlist_create(0); 120 121 key = "string"; 122 value = "fjdojfdi"; 123 nvlist_add_string(nvl, key, value); 124 125 ATF_REQUIRE_EQ(strcmp(dnvlist_get_string(nvl, key, "g"), value), 0); 126 127 actual_value = dnvlist_getf_string(nvl, "rs", "%s", key); 128 ATF_REQUIRE_EQ(strcmp(actual_value, value), 0); 129 130 nvlist_destroy(nvl); 131 } 132 133 ATF_TEST_CASE_WITHOUT_HEAD(dnvlist_get_string__default_value); 134 ATF_TEST_CASE_BODY(dnvlist_get_string__default_value) 135 { 136 nvlist_t *nvl; 137 const char *key; 138 const char *actual_value; 139 140 key = "123"; 141 nvl = nvlist_create(0); 142 143 ATF_REQUIRE_EQ(strcmp(dnvlist_get_string(nvl, key, "bar"), "bar"), 0); 144 145 actual_value = dnvlist_getf_string(nvl, "d", "%s", key); 146 ATF_REQUIRE_EQ(strcmp(actual_value, "d"), 0); 147 148 nvlist_add_string(nvl, key, "cxhweh"); 149 150 ATF_REQUIRE_EQ(strcmp(dnvlist_get_string(nvl, "hthth", "fd"), "fd"), 0); 151 actual_value = dnvlist_getf_string(nvl, "5", "%s", "5"); 152 ATF_REQUIRE_EQ(strcmp("5", "5"), 0); 153 154 nvlist_destroy(nvl); 155 } 156 157 ATF_TEST_CASE_WITHOUT_HEAD(dnvlist_get_nvlist__present); 158 ATF_TEST_CASE_BODY(dnvlist_get_nvlist__present) 159 { 160 nvlist_t *nvl; 161 const char *key; 162 nvlist_t *value; 163 const nvlist_t *actual_value; 164 165 nvl = nvlist_create(0); 166 167 key = "nvlist"; 168 value = nvlist_create(0); 169 nvlist_move_nvlist(nvl, key, value); 170 171 actual_value = dnvlist_get_nvlist(nvl, key, NULL); 172 ATF_REQUIRE(actual_value != NULL); 173 ATF_REQUIRE(nvlist_empty(actual_value)); 174 175 actual_value = dnvlist_getf_nvlist(nvl, NULL, "%s", key); 176 ATF_REQUIRE(actual_value != NULL); 177 ATF_REQUIRE(nvlist_empty(actual_value)); 178 179 nvlist_destroy(nvl); 180 } 181 182 ATF_TEST_CASE_WITHOUT_HEAD(dnvlist_get_nvlist__default_value); 183 ATF_TEST_CASE_BODY(dnvlist_get_nvlist__default_value) 184 { 185 nvlist_t *nvl; 186 const char *key; 187 nvlist_t *dummy; 188 189 key = "123"; 190 nvl = nvlist_create(0); 191 dummy = nvlist_create(0); 192 193 ATF_REQUIRE_EQ(dnvlist_get_nvlist(nvl, key, dummy), dummy); 194 ATF_REQUIRE_EQ(dnvlist_getf_nvlist(nvl, dummy, "%s", key), dummy); 195 196 nvlist_move_nvlist(nvl, key, nvlist_create(0)); 197 ATF_REQUIRE_EQ(dnvlist_get_nvlist(nvl, "456", dummy), dummy); 198 ATF_REQUIRE_EQ(dnvlist_getf_nvlist(nvl, dummy, "%s", "gh"), dummy); 199 200 nvlist_destroy(nvl); 201 } 202 203 static void 204 set_const_binary_value(const void *&value, size_t &size, const char *str) 205 { 206 207 value = str; 208 size = strlen(str) + 1; /* +1 to include '\0' */ 209 } 210 211 ATF_TEST_CASE_WITHOUT_HEAD(dnvlist_get_binary__present); 212 ATF_TEST_CASE_BODY(dnvlist_get_binary__present) 213 { 214 nvlist_t *nvl; 215 const char *k; 216 const void *value, *actual_value; 217 size_t value_size, actual_size; 218 219 nvl = nvlist_create(0); 220 221 k = "binary"; 222 set_const_binary_value(value, value_size, "fjdojfdi"); 223 nvlist_add_binary(nvl, k, value, value_size); 224 225 actual_value = dnvlist_get_binary(nvl, k, &actual_size, "g", 1); 226 ATF_REQUIRE_EQ(value_size, actual_size); 227 ATF_REQUIRE_EQ(memcmp(actual_value, value, actual_size), 0); 228 229 actual_value = dnvlist_getf_binary(nvl, &actual_size, "g", 1, "%s", k); 230 ATF_REQUIRE_EQ(value_size, actual_size); 231 ATF_REQUIRE_EQ(memcmp(actual_value, value, actual_size), 0); 232 233 nvlist_destroy(nvl); 234 } 235 236 ATF_TEST_CASE_WITHOUT_HEAD(dnvlist_get_binary__default_value); 237 ATF_TEST_CASE_BODY(dnvlist_get_binary__default_value) 238 { 239 nvlist_t *nvl; 240 const char *key; 241 const void *default_value, *actual_value; 242 size_t default_size, actual_size; 243 244 key = "123"; 245 nvl = nvlist_create(0); 246 247 set_const_binary_value(default_value, default_size, "bar"); 248 actual_value = dnvlist_get_binary(nvl, key, &actual_size, default_value, 249 default_size); 250 ATF_REQUIRE_EQ(default_size, actual_size); 251 ATF_REQUIRE_EQ(memcmp(actual_value, default_value, actual_size), 0); 252 253 set_const_binary_value(default_value, default_size, "atf"); 254 actual_value = dnvlist_getf_binary(nvl, &actual_size, default_value, 255 default_size, "%s", key); 256 ATF_REQUIRE_EQ(default_size, actual_size); 257 ATF_REQUIRE_EQ(memcmp(actual_value, default_value, actual_size), 0); 258 259 nvlist_add_binary(nvl, key, "test", 4); 260 261 set_const_binary_value(default_value, default_size, "bthrg"); 262 actual_value = dnvlist_get_binary(nvl, "k", &actual_size, default_value, 263 default_size); 264 ATF_REQUIRE_EQ(default_size, actual_size); 265 ATF_REQUIRE_EQ(memcmp(actual_value, default_value, actual_size), 0); 266 267 set_const_binary_value(default_value, default_size, 268 "rrhgrythtyrtgbrhgrtdsvdfbtjlkul"); 269 actual_value = dnvlist_getf_binary(nvl, &actual_size, default_value, 270 default_size, "s"); 271 ATF_REQUIRE_EQ(default_size, actual_size); 272 ATF_REQUIRE_EQ(memcmp(actual_value, default_value, actual_size), 0); 273 274 nvlist_destroy(nvl); 275 } 276 277 ATF_TEST_CASE_WITHOUT_HEAD(dnvlist_take_bool__present); 278 ATF_TEST_CASE_BODY(dnvlist_take_bool__present) 279 { 280 nvlist_t *nvl; 281 const char *key; 282 bool value; 283 284 nvl = nvlist_create(0); 285 286 key = "name"; 287 value = true; 288 nvlist_add_bool(nvl, key, value); 289 290 ATF_REQUIRE_EQ(dnvlist_take_bool(nvl, key, false), value); 291 ATF_REQUIRE(nvlist_empty(nvl)); 292 293 nvlist_destroy(nvl); 294 } 295 296 ATF_TEST_CASE_WITHOUT_HEAD(dnvlist_take_bool__empty); 297 ATF_TEST_CASE_BODY(dnvlist_take_bool__empty) 298 { 299 nvlist_t *nvl; 300 301 nvl = nvlist_create(0); 302 303 ATF_REQUIRE_EQ(dnvlist_take_bool(nvl, "123", false), false); 304 305 nvlist_destroy(nvl); 306 } 307 308 ATF_TEST_CASE_WITHOUT_HEAD(dnvlist_take_bool__default_value); 309 ATF_TEST_CASE_BODY(dnvlist_take_bool__default_value) 310 { 311 nvlist_t *nvl; 312 313 nvl = nvlist_create(0); 314 nvlist_add_bool(nvl, "key", true); 315 316 ATF_REQUIRE_EQ(dnvlist_take_bool(nvl, "otherkey", true), true); 317 318 nvlist_destroy(nvl); 319 } 320 321 ATF_TEST_CASE_WITHOUT_HEAD(dnvlist_take_number__present); 322 ATF_TEST_CASE_BODY(dnvlist_take_number__present) 323 { 324 nvlist_t *nvl; 325 const char *key; 326 uint64_t value; 327 328 nvl = nvlist_create(0); 329 330 key = "name"; 331 value = 194154; 332 nvlist_add_number(nvl, key, value); 333 334 ATF_REQUIRE_EQ(dnvlist_take_number(nvl, key, 2), value); 335 ATF_REQUIRE(nvlist_empty(nvl)); 336 337 nvlist_destroy(nvl); 338 } 339 340 ATF_TEST_CASE_WITHOUT_HEAD(dnvlist_take_number__empty); 341 ATF_TEST_CASE_BODY(dnvlist_take_number__empty) 342 { 343 nvlist_t *nvl; 344 345 nvl = nvlist_create(0); 346 347 ATF_REQUIRE_EQ(dnvlist_take_number(nvl, "123", 126484), 126484); 348 349 nvlist_destroy(nvl); 350 } 351 352 ATF_TEST_CASE_WITHOUT_HEAD(dnvlist_take_number__default_value); 353 ATF_TEST_CASE_BODY(dnvlist_take_number__default_value) 354 { 355 nvlist_t *nvl; 356 357 nvl = nvlist_create(0); 358 nvlist_add_number(nvl, "key", 12); 359 360 ATF_REQUIRE_EQ(dnvlist_take_number(nvl, "otherkey", 13), 13); 361 362 nvlist_destroy(nvl); 363 } 364 365 ATF_TEST_CASE_WITHOUT_HEAD(dnvlist_take_string__present); 366 ATF_TEST_CASE_BODY(dnvlist_take_string__present) 367 { 368 nvlist_t *nvl; 369 const char *key; 370 const char *value; 371 char *default_val, *actual_val; 372 373 nvl = nvlist_create(0); 374 375 key = "name"; 376 value = "wrowm"; 377 default_val = strdup("default"); 378 nvlist_add_string(nvl, key, value); 379 380 actual_val = dnvlist_take_string(nvl, key, default_val); 381 ATF_REQUIRE_EQ(strcmp(actual_val, value), 0); 382 ATF_REQUIRE(nvlist_empty(nvl)); 383 384 free(actual_val); 385 free(default_val); 386 nvlist_destroy(nvl); 387 } 388 389 ATF_TEST_CASE_WITHOUT_HEAD(dnvlist_take_string__empty); 390 ATF_TEST_CASE_BODY(dnvlist_take_string__empty) 391 { 392 nvlist_t *nvl; 393 char *default_val, *actual_val; 394 395 nvl = nvlist_create(0); 396 default_val = strdup(""); 397 398 actual_val = dnvlist_take_string(nvl, "123", default_val); 399 ATF_REQUIRE_EQ(strcmp(actual_val, default_val), 0); 400 401 free(actual_val); 402 nvlist_destroy(nvl); 403 } 404 405 ATF_TEST_CASE_WITHOUT_HEAD(dnvlist_take_string__default_value); 406 ATF_TEST_CASE_BODY(dnvlist_take_string__default_value) 407 { 408 nvlist_t *nvl; 409 char *default_val, *actual_val; 410 411 nvl = nvlist_create(0); 412 nvlist_add_string(nvl, "key", "foobar"); 413 default_val = strdup("other"); 414 415 actual_val = dnvlist_take_string(nvl, "otherkey", default_val); 416 ATF_REQUIRE_EQ(strcmp(actual_val, default_val), 0); 417 418 free(actual_val); 419 nvlist_destroy(nvl); 420 } 421 422 ATF_TEST_CASE_WITHOUT_HEAD(dnvlist_take_nvlist__present); 423 ATF_TEST_CASE_BODY(dnvlist_take_nvlist__present) 424 { 425 nvlist_t *nvl; 426 const char *key; 427 nvlist_t *value, *default_val, *actual_val; 428 429 nvl = nvlist_create(0); 430 431 key = "name"; 432 value = nvlist_create(0); 433 default_val = nvlist_create(0); 434 nvlist_move_nvlist(nvl, key, value); 435 436 actual_val = dnvlist_take_nvlist(nvl, key, default_val); 437 ATF_REQUIRE_EQ(actual_val, value); 438 ATF_REQUIRE(nvlist_empty(nvl)); 439 440 free(actual_val); 441 free(default_val); 442 nvlist_destroy(nvl); 443 } 444 445 ATF_TEST_CASE_WITHOUT_HEAD(dnvlist_take_nvlist__empty); 446 ATF_TEST_CASE_BODY(dnvlist_take_nvlist__empty) 447 { 448 nvlist_t *nvl, *actual_val; 449 450 nvl = nvlist_create(0); 451 452 actual_val = dnvlist_take_nvlist(nvl, "123", NULL); 453 ATF_REQUIRE_EQ(actual_val, static_cast<nvlist_t *>(NULL)); 454 455 free(actual_val); 456 nvlist_destroy(nvl); 457 } 458 459 ATF_TEST_CASE_WITHOUT_HEAD(dnvlist_take_nvlist__default_value); 460 ATF_TEST_CASE_BODY(dnvlist_take_nvlist__default_value) 461 { 462 nvlist_t *nvl; 463 nvlist_t *default_val, *actual_val; 464 465 nvl = nvlist_create(0); 466 nvlist_move_nvlist(nvl, "key", nvlist_create(0)); 467 default_val = nvlist_create(0); 468 469 actual_val = dnvlist_take_nvlist(nvl, "otherkey", default_val); 470 ATF_REQUIRE_EQ(actual_val, default_val); 471 472 free(actual_val); 473 nvlist_destroy(nvl); 474 } 475 476 static void 477 set_binary_value(void *&value, size_t &size, const char *str) 478 { 479 480 value = strdup(str); 481 size = strlen(str) + 1; /* +1 to include '\0' */ 482 } 483 484 ATF_TEST_CASE_WITHOUT_HEAD(dnvlist_take_binary__present); 485 ATF_TEST_CASE_BODY(dnvlist_take_binary__present) 486 { 487 nvlist_t *nvl; 488 const char *key; 489 void *value, *default_val, *actual_val; 490 size_t value_size, default_size, actual_size; 491 492 nvl = nvlist_create(0); 493 494 key = "name"; 495 set_binary_value(value, value_size, "fkdojvmo908"); 496 set_binary_value(default_val, default_size, "16546"); 497 nvlist_add_binary(nvl, key, value, value_size); 498 499 actual_val = dnvlist_take_binary(nvl, key, &actual_size, default_val, 500 default_size); 501 ATF_REQUIRE_EQ(value_size, actual_size); 502 ATF_REQUIRE_EQ(memcmp(actual_val, value, value_size), 0); 503 ATF_REQUIRE(nvlist_empty(nvl)); 504 505 free(actual_val); 506 free(default_val); 507 nvlist_destroy(nvl); 508 } 509 510 ATF_TEST_CASE_WITHOUT_HEAD(dnvlist_take_binary__empty); 511 ATF_TEST_CASE_BODY(dnvlist_take_binary__empty) 512 { 513 nvlist_t *nvl; 514 void *default_val, *actual_val; 515 size_t default_size, actual_size; 516 517 nvl = nvlist_create(0); 518 set_binary_value(default_val, default_size, "\xa8\x89\x49\xff\xe2\x08"); 519 520 actual_val = dnvlist_take_binary(nvl, "123", &actual_size, default_val, 521 default_size); 522 ATF_REQUIRE_EQ(default_size, actual_size); 523 ATF_REQUIRE_EQ(memcmp(actual_val, default_val, actual_size), 0); 524 525 free(actual_val); 526 nvlist_destroy(nvl); 527 } 528 529 ATF_TEST_CASE_WITHOUT_HEAD(dnvlist_take_binary__default_value); 530 ATF_TEST_CASE_BODY(dnvlist_take_binary__default_value) 531 { 532 nvlist_t *nvl; 533 void *default_val, *actual_val; 534 size_t default_size, actual_size; 535 536 nvl = nvlist_create(0); 537 nvlist_add_binary(nvl, "key", "foobar", 6); 538 set_binary_value(default_val, default_size, "vbhag"); 539 540 actual_val = dnvlist_take_binary(nvl, "otherkey", &actual_size, 541 default_val, default_size); 542 ATF_REQUIRE_EQ(default_size, actual_size); 543 ATF_REQUIRE_EQ(memcmp(actual_val, default_val, default_size), 0); 544 545 free(actual_val); 546 nvlist_destroy(nvl); 547 } 548 549 ATF_INIT_TEST_CASES(tp) 550 { 551 ATF_ADD_TEST_CASE(tp, dnvlist_get_bool__present); 552 ATF_ADD_TEST_CASE(tp, dnvlist_get_bool__default_value); 553 ATF_ADD_TEST_CASE(tp, dnvlist_get_number__present); 554 ATF_ADD_TEST_CASE(tp, dnvlist_get_number__default_value); 555 ATF_ADD_TEST_CASE(tp, dnvlist_get_string__present); 556 ATF_ADD_TEST_CASE(tp, dnvlist_get_string__default_value); 557 ATF_ADD_TEST_CASE(tp, dnvlist_get_nvlist__present); 558 ATF_ADD_TEST_CASE(tp, dnvlist_get_nvlist__default_value); 559 ATF_ADD_TEST_CASE(tp, dnvlist_get_binary__present); 560 ATF_ADD_TEST_CASE(tp, dnvlist_get_binary__default_value); 561 562 ATF_ADD_TEST_CASE(tp, dnvlist_take_bool__present); 563 ATF_ADD_TEST_CASE(tp, dnvlist_take_bool__empty); 564 ATF_ADD_TEST_CASE(tp, dnvlist_take_bool__default_value); 565 ATF_ADD_TEST_CASE(tp, dnvlist_take_number__present); 566 ATF_ADD_TEST_CASE(tp, dnvlist_take_number__empty); 567 ATF_ADD_TEST_CASE(tp, dnvlist_take_number__default_value); 568 ATF_ADD_TEST_CASE(tp, dnvlist_take_string__present); 569 ATF_ADD_TEST_CASE(tp, dnvlist_take_string__empty); 570 ATF_ADD_TEST_CASE(tp, dnvlist_take_string__default_value); 571 ATF_ADD_TEST_CASE(tp, dnvlist_take_nvlist__present); 572 ATF_ADD_TEST_CASE(tp, dnvlist_take_nvlist__empty); 573 ATF_ADD_TEST_CASE(tp, dnvlist_take_nvlist__default_value); 574 ATF_ADD_TEST_CASE(tp, dnvlist_take_binary__present); 575 ATF_ADD_TEST_CASE(tp, dnvlist_take_binary__empty); 576 ATF_ADD_TEST_CASE(tp, dnvlist_take_binary__default_value); 577 } 578