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