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