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("5", "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 nvlist_destroy(nvl); 395 } 396 397 ATF_TEST_CASE_WITHOUT_HEAD(dnvlist_take_string__default_value); 398 ATF_TEST_CASE_BODY(dnvlist_take_string__default_value) 399 { 400 nvlist_t *nvl; 401 char *default_val, *actual_val; 402 403 nvl = nvlist_create(0); 404 nvlist_add_string(nvl, "key", "foobar"); 405 default_val = strdup("other"); 406 407 actual_val = dnvlist_take_string(nvl, "otherkey", default_val); 408 ATF_REQUIRE_EQ(strcmp(actual_val, default_val), 0); 409 410 free(actual_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 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 nvlist_destroy(nvl); 519 } 520 521 ATF_TEST_CASE_WITHOUT_HEAD(dnvlist_take_binary__default_value); 522 ATF_TEST_CASE_BODY(dnvlist_take_binary__default_value) 523 { 524 nvlist_t *nvl; 525 void *default_val, *actual_val; 526 size_t default_size, actual_size; 527 528 nvl = nvlist_create(0); 529 nvlist_add_binary(nvl, "key", "foobar", 6); 530 set_binary_value(default_val, default_size, "vbhag"); 531 532 actual_val = dnvlist_take_binary(nvl, "otherkey", &actual_size, 533 default_val, default_size); 534 ATF_REQUIRE_EQ(default_size, actual_size); 535 ATF_REQUIRE_EQ(memcmp(actual_val, default_val, default_size), 0); 536 537 free(actual_val); 538 nvlist_destroy(nvl); 539 } 540 541 ATF_INIT_TEST_CASES(tp) 542 { 543 ATF_ADD_TEST_CASE(tp, dnvlist_get_bool__present); 544 ATF_ADD_TEST_CASE(tp, dnvlist_get_bool__default_value); 545 ATF_ADD_TEST_CASE(tp, dnvlist_get_number__present); 546 ATF_ADD_TEST_CASE(tp, dnvlist_get_number__default_value); 547 ATF_ADD_TEST_CASE(tp, dnvlist_get_string__present); 548 ATF_ADD_TEST_CASE(tp, dnvlist_get_string__default_value); 549 ATF_ADD_TEST_CASE(tp, dnvlist_get_nvlist__present); 550 ATF_ADD_TEST_CASE(tp, dnvlist_get_nvlist__default_value); 551 ATF_ADD_TEST_CASE(tp, dnvlist_get_binary__present); 552 ATF_ADD_TEST_CASE(tp, dnvlist_get_binary__default_value); 553 554 ATF_ADD_TEST_CASE(tp, dnvlist_take_bool__present); 555 ATF_ADD_TEST_CASE(tp, dnvlist_take_bool__empty); 556 ATF_ADD_TEST_CASE(tp, dnvlist_take_bool__default_value); 557 ATF_ADD_TEST_CASE(tp, dnvlist_take_number__present); 558 ATF_ADD_TEST_CASE(tp, dnvlist_take_number__empty); 559 ATF_ADD_TEST_CASE(tp, dnvlist_take_number__default_value); 560 ATF_ADD_TEST_CASE(tp, dnvlist_take_string__present); 561 ATF_ADD_TEST_CASE(tp, dnvlist_take_string__empty); 562 ATF_ADD_TEST_CASE(tp, dnvlist_take_string__default_value); 563 ATF_ADD_TEST_CASE(tp, dnvlist_take_nvlist__present); 564 ATF_ADD_TEST_CASE(tp, dnvlist_take_nvlist__empty); 565 ATF_ADD_TEST_CASE(tp, dnvlist_take_nvlist__default_value); 566 ATF_ADD_TEST_CASE(tp, dnvlist_take_binary__present); 567 ATF_ADD_TEST_CASE(tp, dnvlist_take_binary__empty); 568 ATF_ADD_TEST_CASE(tp, dnvlist_take_binary__default_value); 569 } 570