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/dnv.h> 31 #include <sys/nv.h> 32 33 #include <atf-c++.hpp> 34 35 ATF_TEST_CASE_WITHOUT_HEAD(dnvlist_get_bool__present); 36 ATF_TEST_CASE_BODY(dnvlist_get_bool__present) 37 { 38 nvlist_t *nvl; 39 const char *key; 40 bool value; 41 42 nvl = nvlist_create(0); 43 44 key = "name"; 45 value = true; 46 nvlist_add_bool(nvl, key, value); 47 48 ATF_REQUIRE_EQ(dnvlist_get_bool(nvl, key, false), value); 49 ATF_REQUIRE_EQ(dnvlist_get_bool(nvl, "name", false), value); 50 51 nvlist_destroy(nvl); 52 } 53 54 ATF_TEST_CASE_WITHOUT_HEAD(dnvlist_get_bool__default_value); 55 ATF_TEST_CASE_BODY(dnvlist_get_bool__default_value) 56 { 57 nvlist_t *nvl; 58 const char *key; 59 60 key = "123"; 61 nvl = nvlist_create(0); 62 63 ATF_REQUIRE_EQ(dnvlist_get_bool(nvl, key, false), false); 64 ATF_REQUIRE_EQ(dnvlist_get_bool(nvl, "123", true), true); 65 66 nvlist_add_bool(nvl, key, true); 67 68 ATF_REQUIRE_EQ(dnvlist_get_bool(nvl, "otherkey", true), true); 69 ATF_REQUIRE_EQ(dnvlist_get_bool(nvl, "12c", false), false); 70 71 nvlist_destroy(nvl); 72 } 73 74 ATF_TEST_CASE_WITHOUT_HEAD(dnvlist_get_number__present); 75 ATF_TEST_CASE_BODY(dnvlist_get_number__present) 76 { 77 nvlist_t *nvl; 78 const char *key; 79 uint64_t value; 80 81 nvl = nvlist_create(0); 82 83 key = "key"; 84 value = 48952; 85 nvlist_add_number(nvl, key, value); 86 87 ATF_REQUIRE_EQ(dnvlist_get_number(nvl, key, 19), value); 88 ATF_REQUIRE_EQ(dnvlist_get_number(nvl, "key", 65), value); 89 90 nvlist_destroy(nvl); 91 } 92 93 ATF_TEST_CASE_WITHOUT_HEAD(dnvlist_get_number__default_value); 94 ATF_TEST_CASE_BODY(dnvlist_get_number__default_value) 95 { 96 nvlist_t *nvl; 97 const char *key; 98 99 key = "123"; 100 nvl = nvlist_create(0); 101 102 ATF_REQUIRE_EQ(dnvlist_get_number(nvl, key, 5), 5); 103 ATF_REQUIRE_EQ(dnvlist_get_number(nvl, "1234", 5), 5); 104 105 nvlist_add_number(nvl, key, 24841); 106 107 ATF_REQUIRE_EQ(dnvlist_get_number(nvl, "1234", 5641), 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_get_string(nvl, key, "rs"); 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_get_string(nvl, key, "d"); 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_get_string(nvl, "5", "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 nvlist_destroy(nvl); 176 } 177 178 ATF_TEST_CASE_WITHOUT_HEAD(dnvlist_get_nvlist__default_value); 179 ATF_TEST_CASE_BODY(dnvlist_get_nvlist__default_value) 180 { 181 nvlist_t *nvl; 182 const char *key; 183 nvlist_t *dummy; 184 185 key = "123"; 186 nvl = nvlist_create(0); 187 dummy = nvlist_create(0); 188 189 ATF_REQUIRE_EQ(dnvlist_get_nvlist(nvl, key, dummy), dummy); 190 191 nvlist_move_nvlist(nvl, key, nvlist_create(0)); 192 ATF_REQUIRE_EQ(dnvlist_get_nvlist(nvl, "456", dummy), dummy); 193 ATF_REQUIRE_EQ(dnvlist_get_nvlist(nvl, "gh", dummy), dummy); 194 195 nvlist_destroy(nvl); 196 } 197 198 static void 199 set_const_binary_value(const void *&value, size_t &size, const char *str) 200 { 201 202 value = str; 203 size = strlen(str) + 1; /* +1 to include '\0' */ 204 } 205 206 ATF_TEST_CASE_WITHOUT_HEAD(dnvlist_get_binary__present); 207 ATF_TEST_CASE_BODY(dnvlist_get_binary__present) 208 { 209 nvlist_t *nvl; 210 const char *k; 211 const void *value, *actual_value; 212 size_t value_size, actual_size; 213 214 nvl = nvlist_create(0); 215 216 k = "binary"; 217 set_const_binary_value(value, value_size, "fjdojfdi"); 218 nvlist_add_binary(nvl, k, value, value_size); 219 220 actual_value = dnvlist_get_binary(nvl, k, &actual_size, "g", 1); 221 ATF_REQUIRE_EQ(value_size, actual_size); 222 ATF_REQUIRE_EQ(memcmp(actual_value, value, actual_size), 0); 223 224 nvlist_destroy(nvl); 225 } 226 227 ATF_TEST_CASE_WITHOUT_HEAD(dnvlist_get_binary__default_value); 228 ATF_TEST_CASE_BODY(dnvlist_get_binary__default_value) 229 { 230 nvlist_t *nvl; 231 const char *key; 232 const void *default_value, *actual_value; 233 size_t default_size, actual_size; 234 235 key = "123"; 236 nvl = nvlist_create(0); 237 238 set_const_binary_value(default_value, default_size, "bar"); 239 actual_value = dnvlist_get_binary(nvl, key, &actual_size, default_value, 240 default_size); 241 ATF_REQUIRE_EQ(default_size, actual_size); 242 ATF_REQUIRE_EQ(memcmp(actual_value, default_value, actual_size), 0); 243 244 set_const_binary_value(default_value, default_size, "atf"); 245 actual_value = dnvlist_get_binary(nvl, key, &actual_size, default_value, 246 default_size); 247 ATF_REQUIRE_EQ(default_size, actual_size); 248 ATF_REQUIRE_EQ(memcmp(actual_value, default_value, actual_size), 0); 249 250 nvlist_add_binary(nvl, key, "test", 4); 251 252 set_const_binary_value(default_value, default_size, "bthrg"); 253 actual_value = dnvlist_get_binary(nvl, "k", &actual_size, default_value, 254 default_size); 255 ATF_REQUIRE_EQ(default_size, actual_size); 256 ATF_REQUIRE_EQ(memcmp(actual_value, default_value, actual_size), 0); 257 258 set_const_binary_value(default_value, default_size, 259 "rrhgrythtyrtgbrhgrtdsvdfbtjlkul"); 260 actual_value = dnvlist_get_binary(nvl, "s", &actual_size, default_value, 261 default_size); 262 ATF_REQUIRE_EQ(default_size, actual_size); 263 ATF_REQUIRE_EQ(memcmp(actual_value, default_value, actual_size), 0); 264 265 nvlist_destroy(nvl); 266 } 267 268 ATF_TEST_CASE_WITHOUT_HEAD(dnvlist_take_bool__present); 269 ATF_TEST_CASE_BODY(dnvlist_take_bool__present) 270 { 271 nvlist_t *nvl; 272 const char *key; 273 bool value; 274 275 nvl = nvlist_create(0); 276 277 key = "name"; 278 value = true; 279 nvlist_add_bool(nvl, key, value); 280 281 ATF_REQUIRE_EQ(dnvlist_take_bool(nvl, key, false), value); 282 ATF_REQUIRE(nvlist_empty(nvl)); 283 284 nvlist_destroy(nvl); 285 } 286 287 ATF_TEST_CASE_WITHOUT_HEAD(dnvlist_take_bool__empty); 288 ATF_TEST_CASE_BODY(dnvlist_take_bool__empty) 289 { 290 nvlist_t *nvl; 291 292 nvl = nvlist_create(0); 293 294 ATF_REQUIRE_EQ(dnvlist_take_bool(nvl, "123", false), false); 295 296 nvlist_destroy(nvl); 297 } 298 299 ATF_TEST_CASE_WITHOUT_HEAD(dnvlist_take_bool__default_value); 300 ATF_TEST_CASE_BODY(dnvlist_take_bool__default_value) 301 { 302 nvlist_t *nvl; 303 304 nvl = nvlist_create(0); 305 nvlist_add_bool(nvl, "key", true); 306 307 ATF_REQUIRE_EQ(dnvlist_take_bool(nvl, "otherkey", true), true); 308 309 nvlist_destroy(nvl); 310 } 311 312 ATF_TEST_CASE_WITHOUT_HEAD(dnvlist_take_number__present); 313 ATF_TEST_CASE_BODY(dnvlist_take_number__present) 314 { 315 nvlist_t *nvl; 316 const char *key; 317 uint64_t value; 318 319 nvl = nvlist_create(0); 320 321 key = "name"; 322 value = 194154; 323 nvlist_add_number(nvl, key, value); 324 325 ATF_REQUIRE_EQ(dnvlist_take_number(nvl, key, 2), value); 326 ATF_REQUIRE(nvlist_empty(nvl)); 327 328 nvlist_destroy(nvl); 329 } 330 331 ATF_TEST_CASE_WITHOUT_HEAD(dnvlist_take_number__empty); 332 ATF_TEST_CASE_BODY(dnvlist_take_number__empty) 333 { 334 nvlist_t *nvl; 335 336 nvl = nvlist_create(0); 337 338 ATF_REQUIRE_EQ(dnvlist_take_number(nvl, "123", 126484), 126484); 339 340 nvlist_destroy(nvl); 341 } 342 343 ATF_TEST_CASE_WITHOUT_HEAD(dnvlist_take_number__default_value); 344 ATF_TEST_CASE_BODY(dnvlist_take_number__default_value) 345 { 346 nvlist_t *nvl; 347 348 nvl = nvlist_create(0); 349 nvlist_add_number(nvl, "key", 12); 350 351 ATF_REQUIRE_EQ(dnvlist_take_number(nvl, "otherkey", 13), 13); 352 353 nvlist_destroy(nvl); 354 } 355 356 ATF_TEST_CASE_WITHOUT_HEAD(dnvlist_take_string__present); 357 ATF_TEST_CASE_BODY(dnvlist_take_string__present) 358 { 359 nvlist_t *nvl; 360 const char *key; 361 const char *value; 362 char *default_val, *actual_val; 363 364 nvl = nvlist_create(0); 365 366 key = "name"; 367 value = "wrowm"; 368 default_val = strdup("default"); 369 nvlist_add_string(nvl, key, value); 370 371 actual_val = dnvlist_take_string(nvl, key, default_val); 372 ATF_REQUIRE_EQ(strcmp(actual_val, value), 0); 373 ATF_REQUIRE(nvlist_empty(nvl)); 374 375 free(actual_val); 376 free(default_val); 377 nvlist_destroy(nvl); 378 } 379 380 ATF_TEST_CASE_WITHOUT_HEAD(dnvlist_take_string__empty); 381 ATF_TEST_CASE_BODY(dnvlist_take_string__empty) 382 { 383 nvlist_t *nvl; 384 char *default_val, *actual_val; 385 386 nvl = nvlist_create(0); 387 default_val = strdup(""); 388 389 actual_val = dnvlist_take_string(nvl, "123", default_val); 390 ATF_REQUIRE_EQ(strcmp(actual_val, default_val), 0); 391 392 free(actual_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 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 nvlist_destroy(nvl); 499 } 500 501 ATF_TEST_CASE_WITHOUT_HEAD(dnvlist_take_binary__empty); 502 ATF_TEST_CASE_BODY(dnvlist_take_binary__empty) 503 { 504 nvlist_t *nvl; 505 void *default_val, *actual_val; 506 size_t default_size, actual_size; 507 508 nvl = nvlist_create(0); 509 set_binary_value(default_val, default_size, "\xa8\x89\x49\xff\xe2\x08"); 510 511 actual_val = dnvlist_take_binary(nvl, "123", &actual_size, default_val, 512 default_size); 513 ATF_REQUIRE_EQ(default_size, actual_size); 514 ATF_REQUIRE_EQ(memcmp(actual_val, default_val, actual_size), 0); 515 516 free(actual_val); 517 nvlist_destroy(nvl); 518 } 519 520 ATF_TEST_CASE_WITHOUT_HEAD(dnvlist_take_binary__default_value); 521 ATF_TEST_CASE_BODY(dnvlist_take_binary__default_value) 522 { 523 nvlist_t *nvl; 524 void *default_val, *actual_val; 525 size_t default_size, actual_size; 526 527 nvl = nvlist_create(0); 528 nvlist_add_binary(nvl, "key", "foobar", 6); 529 set_binary_value(default_val, default_size, "vbhag"); 530 531 actual_val = dnvlist_take_binary(nvl, "otherkey", &actual_size, 532 default_val, default_size); 533 ATF_REQUIRE_EQ(default_size, actual_size); 534 ATF_REQUIRE_EQ(memcmp(actual_val, default_val, default_size), 0); 535 536 free(actual_val); 537 nvlist_destroy(nvl); 538 } 539 540 ATF_INIT_TEST_CASES(tp) 541 { 542 ATF_ADD_TEST_CASE(tp, dnvlist_get_bool__present); 543 ATF_ADD_TEST_CASE(tp, dnvlist_get_bool__default_value); 544 ATF_ADD_TEST_CASE(tp, dnvlist_get_number__present); 545 ATF_ADD_TEST_CASE(tp, dnvlist_get_number__default_value); 546 ATF_ADD_TEST_CASE(tp, dnvlist_get_string__present); 547 ATF_ADD_TEST_CASE(tp, dnvlist_get_string__default_value); 548 ATF_ADD_TEST_CASE(tp, dnvlist_get_nvlist__present); 549 ATF_ADD_TEST_CASE(tp, dnvlist_get_nvlist__default_value); 550 ATF_ADD_TEST_CASE(tp, dnvlist_get_binary__present); 551 ATF_ADD_TEST_CASE(tp, dnvlist_get_binary__default_value); 552 553 ATF_ADD_TEST_CASE(tp, dnvlist_take_bool__present); 554 ATF_ADD_TEST_CASE(tp, dnvlist_take_bool__empty); 555 ATF_ADD_TEST_CASE(tp, dnvlist_take_bool__default_value); 556 ATF_ADD_TEST_CASE(tp, dnvlist_take_number__present); 557 ATF_ADD_TEST_CASE(tp, dnvlist_take_number__empty); 558 ATF_ADD_TEST_CASE(tp, dnvlist_take_number__default_value); 559 ATF_ADD_TEST_CASE(tp, dnvlist_take_string__present); 560 ATF_ADD_TEST_CASE(tp, dnvlist_take_string__empty); 561 ATF_ADD_TEST_CASE(tp, dnvlist_take_string__default_value); 562 ATF_ADD_TEST_CASE(tp, dnvlist_take_nvlist__present); 563 ATF_ADD_TEST_CASE(tp, dnvlist_take_nvlist__empty); 564 ATF_ADD_TEST_CASE(tp, dnvlist_take_nvlist__default_value); 565 ATF_ADD_TEST_CASE(tp, dnvlist_take_binary__present); 566 ATF_ADD_TEST_CASE(tp, dnvlist_take_binary__empty); 567 ATF_ADD_TEST_CASE(tp, dnvlist_take_binary__default_value); 568 } 569