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