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 <atf-c++.hpp> 31 #include <dnv.h> 32 #include <nv.h> 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("5", "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 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 nvlist_destroy(nvl); 410 } 411 412 ATF_TEST_CASE_WITHOUT_HEAD(dnvlist_take_nvlist__present); 413 ATF_TEST_CASE_BODY(dnvlist_take_nvlist__present) 414 { 415 nvlist_t *nvl; 416 const char *key; 417 nvlist_t *value, *default_val, *actual_val; 418 419 nvl = nvlist_create(0); 420 421 key = "name"; 422 value = nvlist_create(0); 423 default_val = nvlist_create(0); 424 nvlist_move_nvlist(nvl, key, value); 425 426 actual_val = dnvlist_take_nvlist(nvl, key, default_val); 427 ATF_REQUIRE_EQ(actual_val, value); 428 ATF_REQUIRE(nvlist_empty(nvl)); 429 430 free(actual_val); 431 free(default_val); 432 nvlist_destroy(nvl); 433 } 434 435 ATF_TEST_CASE_WITHOUT_HEAD(dnvlist_take_nvlist__empty); 436 ATF_TEST_CASE_BODY(dnvlist_take_nvlist__empty) 437 { 438 nvlist_t *nvl, *actual_val; 439 440 nvl = nvlist_create(0); 441 442 actual_val = dnvlist_take_nvlist(nvl, "123", NULL); 443 ATF_REQUIRE_EQ(actual_val, static_cast<nvlist_t *>(NULL)); 444 445 free(actual_val); 446 nvlist_destroy(nvl); 447 } 448 449 ATF_TEST_CASE_WITHOUT_HEAD(dnvlist_take_nvlist__default_value); 450 ATF_TEST_CASE_BODY(dnvlist_take_nvlist__default_value) 451 { 452 nvlist_t *nvl; 453 nvlist_t *default_val, *actual_val; 454 455 nvl = nvlist_create(0); 456 nvlist_move_nvlist(nvl, "key", nvlist_create(0)); 457 default_val = nvlist_create(0); 458 459 actual_val = dnvlist_take_nvlist(nvl, "otherkey", default_val); 460 ATF_REQUIRE_EQ(actual_val, default_val); 461 462 free(actual_val); 463 nvlist_destroy(nvl); 464 } 465 466 static void 467 set_binary_value(void *&value, size_t &size, const char *str) 468 { 469 470 value = strdup(str); 471 size = strlen(str) + 1; /* +1 to include '\0' */ 472 } 473 474 ATF_TEST_CASE_WITHOUT_HEAD(dnvlist_take_binary__present); 475 ATF_TEST_CASE_BODY(dnvlist_take_binary__present) 476 { 477 nvlist_t *nvl; 478 const char *key; 479 void *value, *default_val, *actual_val; 480 size_t value_size, default_size, actual_size; 481 482 nvl = nvlist_create(0); 483 484 key = "name"; 485 set_binary_value(value, value_size, "fkdojvmo908"); 486 set_binary_value(default_val, default_size, "16546"); 487 nvlist_add_binary(nvl, key, value, value_size); 488 489 actual_val = dnvlist_take_binary(nvl, key, &actual_size, default_val, 490 default_size); 491 ATF_REQUIRE_EQ(value_size, actual_size); 492 ATF_REQUIRE_EQ(memcmp(actual_val, value, value_size), 0); 493 ATF_REQUIRE(nvlist_empty(nvl)); 494 495 free(actual_val); 496 free(default_val); 497 nvlist_destroy(nvl); 498 } 499 500 ATF_TEST_CASE_WITHOUT_HEAD(dnvlist_take_binary__empty); 501 ATF_TEST_CASE_BODY(dnvlist_take_binary__empty) 502 { 503 nvlist_t *nvl; 504 void *default_val, *actual_val; 505 size_t default_size, actual_size; 506 507 nvl = nvlist_create(0); 508 set_binary_value(default_val, default_size, "\xa8\x89\x49\xff\xe2\x08"); 509 510 actual_val = dnvlist_take_binary(nvl, "123", &actual_size, default_val, 511 default_size); 512 ATF_REQUIRE_EQ(default_size, actual_size); 513 ATF_REQUIRE_EQ(memcmp(actual_val, default_val, actual_size), 0); 514 515 free(actual_val); 516 nvlist_destroy(nvl); 517 } 518 519 ATF_TEST_CASE_WITHOUT_HEAD(dnvlist_take_binary__default_value); 520 ATF_TEST_CASE_BODY(dnvlist_take_binary__default_value) 521 { 522 nvlist_t *nvl; 523 void *default_val, *actual_val; 524 size_t default_size, actual_size; 525 526 nvl = nvlist_create(0); 527 nvlist_add_binary(nvl, "key", "foobar", 6); 528 set_binary_value(default_val, default_size, "vbhag"); 529 530 actual_val = dnvlist_take_binary(nvl, "otherkey", &actual_size, 531 default_val, default_size); 532 ATF_REQUIRE_EQ(default_size, actual_size); 533 ATF_REQUIRE_EQ(memcmp(actual_val, default_val, default_size), 0); 534 535 free(actual_val); 536 nvlist_destroy(nvl); 537 } 538 539 ATF_INIT_TEST_CASES(tp) 540 { 541 ATF_ADD_TEST_CASE(tp, dnvlist_get_bool__present); 542 ATF_ADD_TEST_CASE(tp, dnvlist_get_bool__default_value); 543 ATF_ADD_TEST_CASE(tp, dnvlist_get_number__present); 544 ATF_ADD_TEST_CASE(tp, dnvlist_get_number__default_value); 545 ATF_ADD_TEST_CASE(tp, dnvlist_get_string__present); 546 ATF_ADD_TEST_CASE(tp, dnvlist_get_string__default_value); 547 ATF_ADD_TEST_CASE(tp, dnvlist_get_nvlist__present); 548 ATF_ADD_TEST_CASE(tp, dnvlist_get_nvlist__default_value); 549 ATF_ADD_TEST_CASE(tp, dnvlist_get_binary__present); 550 ATF_ADD_TEST_CASE(tp, dnvlist_get_binary__default_value); 551 552 ATF_ADD_TEST_CASE(tp, dnvlist_take_bool__present); 553 ATF_ADD_TEST_CASE(tp, dnvlist_take_bool__empty); 554 ATF_ADD_TEST_CASE(tp, dnvlist_take_bool__default_value); 555 ATF_ADD_TEST_CASE(tp, dnvlist_take_number__present); 556 ATF_ADD_TEST_CASE(tp, dnvlist_take_number__empty); 557 ATF_ADD_TEST_CASE(tp, dnvlist_take_number__default_value); 558 ATF_ADD_TEST_CASE(tp, dnvlist_take_string__present); 559 ATF_ADD_TEST_CASE(tp, dnvlist_take_string__empty); 560 ATF_ADD_TEST_CASE(tp, dnvlist_take_string__default_value); 561 ATF_ADD_TEST_CASE(tp, dnvlist_take_nvlist__present); 562 ATF_ADD_TEST_CASE(tp, dnvlist_take_nvlist__empty); 563 ATF_ADD_TEST_CASE(tp, dnvlist_take_nvlist__default_value); 564 ATF_ADD_TEST_CASE(tp, dnvlist_take_binary__present); 565 ATF_ADD_TEST_CASE(tp, dnvlist_take_binary__empty); 566 ATF_ADD_TEST_CASE(tp, dnvlist_take_binary__default_value); 567 } 568