1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549
|
#define _GNU_SOURCE
#include <stddef.h>
#include <stdarg.h>
#include <setjmp.h>
#include <cmocka.h>
#include "../libmultipath/propsel.c"
#include "globals.c"
static void test_af_null_features_ptr(void **state)
{
assert_int_equal(add_feature(NULL, "test"), 1);
}
static void af_helper(const char *features_start, const char *addition,
const char *features_end, int result)
{
char *f = NULL, *orig = NULL;
if (features_start) {
f = strdup(features_start);
assert_non_null(f);
orig = f;
}
assert_int_equal(add_feature(&f, addition), result);
if (result != 0 || features_end == NULL)
assert_ptr_equal(orig, f);
else
assert_string_equal(f, features_end);
free(f);
}
static void test_af_null_addition1(void **state)
{
af_helper("0", NULL, NULL, 0);
}
static void test_af_null_addition2(void **state)
{
af_helper("1 queue_if_no_path", NULL, NULL, 0);
}
static void test_af_empty_addition(void **state)
{
af_helper("2 pg_init_retries 5", "", NULL, 0);
}
static void test_af_invalid_addition1(void **state)
{
af_helper("2 pg_init_retries 5", " ", NULL, 1);
}
static void test_af_invalid_addition2(void **state)
{
af_helper("2 pg_init_retries 5", "\tbad", NULL, 1);
}
static void test_af_invalid_addition3(void **state)
{
af_helper("2 pg_init_retries 5", "bad ", NULL, 1);
}
static void test_af_invalid_addition4(void **state)
{
af_helper("2 pg_init_retries 5", " bad ", NULL, 1);
}
static void test_af_null_features1(void **state)
{
af_helper(NULL, "test", "1 test", 0);
}
static void test_af_null_features2(void **state)
{
af_helper(NULL, "test\t more", "2 test\t more", 0);
}
static void test_af_null_features3(void **state)
{
af_helper(NULL, "test\neven\tmore", "3 test\neven\tmore", 0);
}
static void test_af_already_exists1(void **state)
{
af_helper("4 this is a test", "test", NULL, 0);
}
static void test_af_already_exists2(void **state)
{
af_helper("5 contest testy intestine test retest", "test", NULL, 0);
}
static void test_af_almost_exists(void **state)
{
af_helper("3 contest testy intestine", "test",
"4 contest testy intestine test", 0);
}
static void test_af_bad_features1(void **state)
{
af_helper("bad", "test", NULL, 1);
}
static void test_af_bad_features2(void **state)
{
af_helper("1bad", "test", NULL, 1);
}
static void test_af_add1(void **state)
{
af_helper("0", "test", "1 test", 0);
}
static void test_af_add2(void **state)
{
af_helper("0", "this is a test", "4 this is a test", 0);
}
static void test_af_add3(void **state)
{
af_helper("1 features", "more values", "3 features more values", 0);
}
static void test_af_add4(void **state)
{
af_helper("2 one\ttwo", "three\t four", "4 one\ttwo three\t four", 0);
}
static int test_add_features(void)
{
const struct CMUnitTest tests[] = {
cmocka_unit_test(test_af_null_features_ptr),
cmocka_unit_test(test_af_null_addition1),
cmocka_unit_test(test_af_null_addition2),
cmocka_unit_test(test_af_empty_addition),
cmocka_unit_test(test_af_invalid_addition1),
cmocka_unit_test(test_af_invalid_addition2),
cmocka_unit_test(test_af_invalid_addition3),
cmocka_unit_test(test_af_invalid_addition4),
cmocka_unit_test(test_af_null_features1),
cmocka_unit_test(test_af_null_features2),
cmocka_unit_test(test_af_null_features3),
cmocka_unit_test(test_af_already_exists1),
cmocka_unit_test(test_af_already_exists2),
cmocka_unit_test(test_af_almost_exists),
cmocka_unit_test(test_af_bad_features1),
cmocka_unit_test(test_af_bad_features2),
cmocka_unit_test(test_af_add1),
cmocka_unit_test(test_af_add2),
cmocka_unit_test(test_af_add3),
cmocka_unit_test(test_af_add4),
};
return cmocka_run_group_tests(tests, NULL, NULL);
}
static void test_rf_null_features_ptr(void **state)
{
assert_int_equal(remove_feature(NULL, "test"), 1);
}
static void test_rf_null_features(void **state)
{
char *f = NULL;
assert_int_equal(remove_feature(&f, "test"), 1);
}
static void rf_helper(const char *features_start, const char *removal,
const char *features_end, int result)
{
char *f = strdup(features_start);
char *orig = f;
assert_non_null(f);
assert_int_equal(remove_feature(&f, removal), result);
if (result != 0 || features_end == NULL)
assert_ptr_equal(orig, f);
else
assert_string_equal(f, features_end);
free(f);
}
static void test_rf_null_removal(void **state)
{
rf_helper("1 feature", NULL, NULL, 0);
}
static void test_rf_empty_removal(void **state)
{
rf_helper("1 feature", "", NULL, 0);
}
static void test_rf_invalid_removal1(void **state)
{
rf_helper("1 feature", " ", NULL, 1);
}
static void test_rf_invalid_removal2(void **state)
{
rf_helper("1 feature", " bad", NULL, 1);
}
static void test_rf_invalid_removal3(void **state)
{
rf_helper("1 feature", "bad\n", NULL, 1);
}
static void test_rf_invalid_removal4(void **state)
{
rf_helper("1 feature", "\tbad \n", NULL, 1);
}
static void test_rf_bad_features1(void **state)
{
rf_helper("invalid feature test string", "test", NULL, 1);
}
static void test_rf_bad_features2(void **state)
{
rf_helper("2no space test", "test", NULL, 1);
}
static void test_rf_missing_removal1(void **state)
{
rf_helper("0", "test", NULL, 0);
}
static void test_rf_missing_removal2(void **state)
{
rf_helper("1 detest", "test", NULL, 0);
}
static void test_rf_missing_removal3(void **state)
{
rf_helper("4 testing one two three", "test", NULL, 0);
}
static void test_rf_missing_removal4(void **state)
{
rf_helper("1 contestant", "test", NULL, 0);
}
static void test_rf_missing_removal5(void **state)
{
rf_helper("3 testament protest detestable", "test", NULL, 0);
}
static void test_rf_remove_all_features1(void **state)
{
rf_helper("1 test", "test", "0", 0);
}
static void test_rf_remove_all_features2(void **state)
{
rf_helper("2 another\t test", "another\t test", "0", 0);
}
static void test_rf_remove1(void **state)
{
rf_helper("2 feature1 feature2", "feature2", "1 feature1", 0);
}
static void test_rf_remove2(void **state)
{
rf_helper("2 feature1 feature2", "feature1", "1 feature2", 0);
}
static void test_rf_remove3(void **state)
{
rf_helper("3 test1 test\ttest2", "test", "2 test1 test2", 0);
}
static void test_rf_remove4(void **state)
{
rf_helper("4 this\t is a test", "is a", "2 this\t test", 0);
}
static void test_rf_remove5(void **state)
{
rf_helper("3 one more test", "more test", "1 one", 0);
}
static int test_remove_features(void)
{
const struct CMUnitTest tests[] = {
cmocka_unit_test(test_rf_null_features_ptr),
cmocka_unit_test(test_rf_null_features),
cmocka_unit_test(test_rf_null_removal),
cmocka_unit_test(test_rf_empty_removal),
cmocka_unit_test(test_rf_invalid_removal1),
cmocka_unit_test(test_rf_invalid_removal2),
cmocka_unit_test(test_rf_invalid_removal3),
cmocka_unit_test(test_rf_invalid_removal4),
cmocka_unit_test(test_rf_bad_features1),
cmocka_unit_test(test_rf_bad_features2),
cmocka_unit_test(test_rf_missing_removal1),
cmocka_unit_test(test_rf_missing_removal2),
cmocka_unit_test(test_rf_missing_removal3),
cmocka_unit_test(test_rf_missing_removal4),
cmocka_unit_test(test_rf_missing_removal5),
cmocka_unit_test(test_rf_remove_all_features1),
cmocka_unit_test(test_rf_remove_all_features2),
cmocka_unit_test(test_rf_remove1),
cmocka_unit_test(test_rf_remove2),
cmocka_unit_test(test_rf_remove3),
cmocka_unit_test(test_rf_remove4),
cmocka_unit_test(test_rf_remove5),
};
return cmocka_run_group_tests(tests, NULL, NULL);
}
static void test_cf_null_features(void **state)
{
struct multipath mp = {
.alias = "test",
};
reconcile_features_with_queue_mode(&mp);
assert_null(mp.features);
}
static void cf_helper(const char *features_start, const char *features_end,
int queue_mode_start, int queue_mode_end)
{
struct multipath mp = {
.alias = "test",
.features = strdup(features_start),
.queue_mode = queue_mode_start,
};
char *orig = mp.features;
assert_non_null(orig);
reconcile_features_with_queue_mode(&mp);
if (!features_end)
assert_ptr_equal(orig, mp.features);
else
assert_string_equal(mp.features, features_end);
free(mp.features);
assert_int_equal(mp.queue_mode, queue_mode_end);
}
static void test_cf_unset_unset1(void **state)
{
cf_helper("0", NULL, QUEUE_MODE_UNDEF, QUEUE_MODE_UNDEF);
}
static void test_cf_unset_unset2(void **state)
{
cf_helper("1 queue_mode", NULL, QUEUE_MODE_UNDEF, QUEUE_MODE_UNDEF);
}
static void test_cf_unset_unset3(void **state)
{
cf_helper("queue_mode", NULL, QUEUE_MODE_UNDEF, QUEUE_MODE_UNDEF);
}
static void test_cf_unset_unset4(void **state)
{
cf_helper("2 queue_model bio", NULL, QUEUE_MODE_UNDEF,
QUEUE_MODE_UNDEF);
}
static void test_cf_unset_unset5(void **state)
{
cf_helper("1 queue_if_no_path", NULL, QUEUE_MODE_UNDEF,
QUEUE_MODE_UNDEF);
}
static void test_cf_invalid_unset1(void **state)
{
cf_helper("2 queue_mode biop", "0", QUEUE_MODE_UNDEF, QUEUE_MODE_UNDEF);
}
static void test_cf_invalid_unset2(void **state)
{
cf_helper("3 queue_mode rqs queue_if_no_path", "1 queue_if_no_path",
QUEUE_MODE_UNDEF, QUEUE_MODE_UNDEF);
}
static void test_cf_rq_unset1(void **state)
{
cf_helper("2 queue_mode rq", NULL, QUEUE_MODE_UNDEF, QUEUE_MODE_RQ);
}
static void test_cf_rq_unset2(void **state)
{
cf_helper("2 queue_mode mq", NULL, QUEUE_MODE_UNDEF, QUEUE_MODE_RQ);
}
static void test_cf_bio_unset(void **state)
{
cf_helper("2 queue_mode bio", NULL, QUEUE_MODE_UNDEF, QUEUE_MODE_BIO);
}
static void test_cf_unset_bio1(void **state)
{
cf_helper("1 queue_if_no_path", "3 queue_if_no_path queue_mode bio",
QUEUE_MODE_BIO, QUEUE_MODE_BIO);
}
static void test_cf_unset_bio2(void **state)
{
cf_helper("0", "2 queue_mode bio", QUEUE_MODE_BIO, QUEUE_MODE_BIO);
}
static void test_cf_unset_bio3(void **state)
{
cf_helper("2 pg_init_retries 50", "4 pg_init_retries 50 queue_mode bio",
QUEUE_MODE_BIO, QUEUE_MODE_BIO);
}
static void test_cf_invalid_bio1(void **state)
{
cf_helper("2 queue_mode bad", "2 queue_mode bio",
QUEUE_MODE_BIO, QUEUE_MODE_BIO);
}
static void test_cf_invalid_bio2(void **state)
{
cf_helper("3 queue_if_no_path queue_mode\tbad", "3 queue_if_no_path queue_mode bio",
QUEUE_MODE_BIO, QUEUE_MODE_BIO);
}
static void test_cf_bio_bio1(void **state)
{
cf_helper("2 queue_mode bio", NULL, QUEUE_MODE_BIO, QUEUE_MODE_BIO);
}
static void test_cf_bio_bio2(void **state)
{
cf_helper("3 queue_if_no_path queue_mode bio", NULL, QUEUE_MODE_BIO, QUEUE_MODE_BIO);
}
static void test_cf_bio_bio3(void **state)
{
cf_helper("3 queue_mode\nbio queue_if_no_path", NULL, QUEUE_MODE_BIO, QUEUE_MODE_BIO);
}
static void test_cf_bio_rq1(void **state)
{
cf_helper("2\nqueue_mode\tbio", "0", QUEUE_MODE_RQ, QUEUE_MODE_RQ);
}
static void test_cf_bio_rq2(void **state)
{
cf_helper("3 queue_if_no_path\nqueue_mode bio", "1 queue_if_no_path",
QUEUE_MODE_RQ, QUEUE_MODE_RQ);
}
static void test_cf_bio_rq3(void **state)
{
cf_helper("4 queue_mode bio pg_init_retries 20", "2 pg_init_retries 20",
QUEUE_MODE_RQ, QUEUE_MODE_RQ);
}
static void test_cf_unset_rq1(void **state)
{
cf_helper("0", NULL, QUEUE_MODE_RQ, QUEUE_MODE_RQ);
}
static void test_cf_unset_rq2(void **state)
{
cf_helper("2 pg_init_retries 15", NULL, QUEUE_MODE_RQ, QUEUE_MODE_RQ);
}
static void test_cf_invalid_rq1(void **state)
{
cf_helper("2 queue_mode bionic", "0", QUEUE_MODE_RQ, QUEUE_MODE_RQ);
}
static void test_cf_invalid_rq2(void **state)
{
cf_helper("3 queue_mode b\nqueue_if_no_path", "1 queue_if_no_path",
QUEUE_MODE_RQ, QUEUE_MODE_RQ);
}
static void test_cf_rq_rq1(void **state)
{
cf_helper("2 queue_mode rq", NULL, QUEUE_MODE_RQ, QUEUE_MODE_RQ);
}
static void test_cf_rq_rq2(void **state)
{
cf_helper("3 queue_mode\t \trq\nqueue_if_no_path", NULL, QUEUE_MODE_RQ, QUEUE_MODE_RQ);
}
static void test_cf_rq_bio1(void **state)
{
cf_helper("2 queue_mode rq", "2 queue_mode bio", QUEUE_MODE_BIO,
QUEUE_MODE_BIO);
}
static void test_cf_rq_bio2(void **state)
{
cf_helper("3 queue_if_no_path\nqueue_mode rq", "3 queue_if_no_path queue_mode bio", QUEUE_MODE_BIO, QUEUE_MODE_BIO);
}
static void test_cf_rq_bio3(void **state)
{
cf_helper("3 queue_mode rq\nqueue_if_no_path", "3 queue_if_no_path queue_mode bio", QUEUE_MODE_BIO, QUEUE_MODE_BIO);
}
static int test_reconcile_features(void)
{
const struct CMUnitTest tests[] = {
cmocka_unit_test(test_cf_null_features),
cmocka_unit_test(test_cf_unset_unset1),
cmocka_unit_test(test_cf_unset_unset2),
cmocka_unit_test(test_cf_unset_unset3),
cmocka_unit_test(test_cf_unset_unset4),
cmocka_unit_test(test_cf_unset_unset5),
cmocka_unit_test(test_cf_invalid_unset1),
cmocka_unit_test(test_cf_invalid_unset2),
cmocka_unit_test(test_cf_rq_unset1),
cmocka_unit_test(test_cf_rq_unset2),
cmocka_unit_test(test_cf_bio_unset),
cmocka_unit_test(test_cf_unset_bio1),
cmocka_unit_test(test_cf_unset_bio2),
cmocka_unit_test(test_cf_unset_bio3),
cmocka_unit_test(test_cf_invalid_bio1),
cmocka_unit_test(test_cf_invalid_bio2),
cmocka_unit_test(test_cf_bio_bio1),
cmocka_unit_test(test_cf_bio_bio2),
cmocka_unit_test(test_cf_bio_bio3),
cmocka_unit_test(test_cf_bio_rq1),
cmocka_unit_test(test_cf_bio_rq2),
cmocka_unit_test(test_cf_bio_rq3),
cmocka_unit_test(test_cf_unset_rq1),
cmocka_unit_test(test_cf_unset_rq2),
cmocka_unit_test(test_cf_invalid_rq1),
cmocka_unit_test(test_cf_invalid_rq2),
cmocka_unit_test(test_cf_rq_rq1),
cmocka_unit_test(test_cf_rq_rq2),
cmocka_unit_test(test_cf_rq_bio1),
cmocka_unit_test(test_cf_rq_bio2),
cmocka_unit_test(test_cf_rq_bio3),
};
return cmocka_run_group_tests(tests, NULL, NULL);
}
int main(void)
{
int ret = 0;
init_test_verbosity(-1);
ret += test_add_features();
ret += test_remove_features();
ret += test_reconcile_features();
return ret;
}
|