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 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664
|
/* Test file.set attribute creation/deletion.
Copyright (C) 2022 Petr Tesarik <ptesarik@suse.com>
This file is free software; you can redistribute it and/or modify
it under the terms of either
* the GNU Lesser General Public License as published by the Free
Software Foundation; either version 3 of the License, or (at
your option) any later version
or
* the GNU General Public License as published by the Free
Software Foundation; either version 2 of the License, or (at
your option) any later version
or both in parallel, as here.
libkdumpfile is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received copies of the GNU General Public License and
the GNU Lesser General Public License along with this program. If
not, see <http://www.gnu.org/licenses/>.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <libkdumpfile/kdumpfile.h>
#include "testutil.h"
#define FILENAME_0 "fileA"
#define FILENAME_1 "fileB"
/** Check that file.set.key does not exist.
*/
static int
check_not_exist(kdump_ctx_t *ctx, kdump_attr_ref_t *fileset, const char *key)
{
kdump_attr_ref_t tmpref;
kdump_status status;
int ret;
ret = TEST_OK;
status = kdump_sub_attr_ref(ctx, fileset, key, &tmpref);
if (status == KDUMP_OK) {
fprintf(stderr, "file.set.%s exists, but it should not!\n", key);
kdump_attr_unref(ctx, &tmpref);
ret = TEST_FAIL;
} else if (status != KDUMP_ERR_NOKEY) {
fprintf(stderr, "file.set.%s cannot be referenced: %s\n",
key, kdump_get_err(ctx));
ret = TEST_FAIL;
}
return ret;
}
/** Check that file.set.key exists and has the expected type but no value.
*/
static int
check_unset_file(kdump_ctx_t *ctx, kdump_attr_ref_t *fileset, const char *key,
kdump_attr_type_t exp_type)
{
kdump_attr_ref_t tmpref;
kdump_status status;
kdump_attr_t attr;
int ret;
ret = TEST_OK;
status = kdump_sub_attr_ref(ctx, fileset, key, &tmpref);
if (status != KDUMP_OK) {
fprintf(stderr, "file.set.%s cannot be referenced: %s\n",
key, kdump_get_err(ctx));
ret = TEST_FAIL;
} else {
kdump_attr_type_t type = kdump_attr_ref_type(&tmpref);
if (type != exp_type) {
fprintf(stderr, "Wrong file.set.%s type: %d\n",
key, type);
ret = TEST_FAIL;
}
status = kdump_attr_ref_get(ctx, &tmpref, &attr);
if (status == KDUMP_OK) {
fprintf(stderr, "file.set.%s has an initial value!\n",
key);
ret = TEST_FAIL;
} else if (status != KDUMP_ERR_NODATA) {
fprintf(stderr, "Cannot get file.set.%s: %s\n",
key, kdump_get_err(ctx));
ret = TEST_FAIL;
}
kdump_attr_unref(ctx, &tmpref);
}
return ret;
}
static int
check_fileset_size(kdump_ctx_t *ctx, int num)
{
kdump_attr_t attr;
kdump_status status;
int ret;
ret = TEST_OK;
status = kdump_get_attr(ctx, KDUMP_ATTR_FILE_SET ".number", &attr);
if (status != KDUMP_OK) {
fprintf(stderr, "%s cannot be retrieved: %s\n",
"file.set.number", kdump_get_err(ctx));
ret = TEST_FAIL;
} else if (attr.val.number != num) {
fprintf(stderr, "Wrong %s value: %" KDUMP_PRIuNUM " != %d\n",
"file.set.number", attr.val.number, num);
ret = TEST_FAIL;
}
return ret;
}
static int
check_fileset_zero(kdump_ctx_t *ctx, kdump_attr_ref_t *fileset, int fd)
{
kdump_attr_ref_t tmpref;
kdump_attr_t attr;
kdump_status status;
int ret;
ret = TEST_OK;
status = kdump_sub_attr_ref(ctx, fileset, "0.fd", &tmpref);
if (status != KDUMP_OK) {
fprintf(stderr, "Cannot reference attribute %s: %s\n",
KDUMP_ATTR_FILE_SET ".0.fd", kdump_get_err(ctx));
return TEST_ERR;
}
status = kdump_attr_ref_get(ctx, &tmpref, &attr);
if (status != KDUMP_OK) {
fprintf(stderr, "Cannot get file.set.0.fd: %s\n",
kdump_get_err(ctx));
ret = TEST_FAIL;
} else if (attr.type != KDUMP_NUMBER) {
fprintf(stderr, "Wrong file.set.0.fd attribute type: %d\n",
(int)attr.type);
ret = TEST_FAIL;
} else if (attr.val.number != fd) {
fprintf(stderr, "Wrong %s value: %" KDUMP_PRIuNUM " != %d\n",
"file.set.0.fd", attr.val.number, fd);
ret = TEST_FAIL;
}
kdump_attr_unref(ctx, &tmpref);
return ret;
}
static int
check_filename(kdump_ctx_t *ctx, kdump_attr_ref_t *fileset,
const char *subkey, const char *name)
{
kdump_attr_ref_t tmpref;
kdump_attr_t attr;
kdump_status status;
int ret;
ret = TEST_OK;
status = kdump_sub_attr_ref(ctx, fileset, subkey, &tmpref);
if (status != KDUMP_OK) {
fprintf(stderr, "Cannot reference attribute %s.%s: %s\n",
KDUMP_ATTR_FILE_SET, subkey, kdump_get_err(ctx));
return TEST_ERR;
}
status = kdump_attr_ref_get(ctx, &tmpref, &attr);
if (status != KDUMP_OK) {
fprintf(stderr, "Cannot get %s.%s: %s\n",
KDUMP_ATTR_FILE_SET, subkey, kdump_get_err(ctx));
ret = TEST_FAIL;
} else if (attr.type != KDUMP_STRING) {
fprintf(stderr, "Wrong %s.%s attribute type: %d\n",
KDUMP_ATTR_FILE_SET, subkey, (int)attr.type);
ret = TEST_FAIL;
} else if (strcmp(attr.val.string, name)) {
fprintf(stderr, "Wrong %s.%s value: %s != %s\n",
KDUMP_ATTR_FILE_SET, subkey, attr.val.string, name);
ret = TEST_FAIL;
}
kdump_attr_unref(ctx, &tmpref);
return ret;
}
int
main(int argc, char **argv)
{
kdump_attr_ref_t fileset;
kdump_attr_ref_t number;
kdump_attr_t attr;
kdump_ctx_t *ctx;
kdump_status status;
const char *names[2];
int fd;
int ret;
int rc;
ret = TEST_OK;
/*************************************************************
* Initialize context and base attribute references.
*/
ctx = kdump_new();
if (!ctx) {
perror("Cannot initialize dump context");
return TEST_ERR;
}
status = kdump_attr_ref(ctx, KDUMP_ATTR_FILE_SET, &fileset);
if (status != KDUMP_OK) {
fprintf(stderr, "Cannot reference attribute %s: %s\n",
KDUMP_ATTR_FILE_SET, kdump_get_err(ctx));
return TEST_ERR;
}
kdump_attr_unref(ctx, &fileset);
status = kdump_sub_attr_ref(ctx, &fileset, "number", &number);
if (status != KDUMP_OK) {
fprintf(stderr, "Cannot reference attribute %s: %s\n",
KDUMP_ATTR_FILE_SET ".number", kdump_get_err(ctx));
return TEST_ERR;
}
/*************************************************************
* Check initial state.
*/
status = kdump_attr_ref_get(ctx, &number, &attr);
if (status != KDUMP_OK) {
fprintf(stderr, "Cannot get number of dump files: %s\n",
kdump_get_err(ctx));
ret = TEST_FAIL;
} else if (attr.type != KDUMP_NUMBER) {
fprintf(stderr, "Wrong dump file attribute type: %d\n",
(int)attr.type);
ret = TEST_FAIL;
} else if (attr.val.number != 0) {
fprintf(stderr, "Wrong initial number of dump files: %" KDUMP_PRIuNUM "\n",
attr.val.number);
ret = TEST_FAIL;
}
/* Check that file.set.0.fd does not exist. */
rc = check_not_exist(ctx, &fileset, "0.fd");
if (rc == TEST_ERR)
return rc;
else if (rc != TEST_OK)
ret = rc;
/* Check that file.set.0.name does not exist. */
rc = check_not_exist(ctx, &fileset, "0.name");
if (rc == TEST_ERR)
return rc;
else if (rc != TEST_OK)
ret = rc;
/*************************************************************
* File set with one file.
*/
attr.type = KDUMP_NUMBER;
attr.val.number = 1;
status = kdump_attr_ref_set(ctx, &number, &attr);
if (status != KDUMP_OK) {
fprintf(stderr, "Cannot set number of dump files: %s\n",
kdump_get_err(ctx));
ret = TEST_FAIL;
}
/* Check that file.set.0.fd exists now but has no value. */
rc = check_unset_file(ctx, &fileset, "0.fd", KDUMP_NUMBER);
if (rc == TEST_ERR)
return rc;
else if (rc != TEST_OK)
ret = rc;
/* Check that file.set.0.name also exists. */
rc = check_unset_file(ctx, &fileset, "0.name", KDUMP_STRING);
if (rc == TEST_ERR)
return rc;
else if (rc != TEST_OK)
ret = rc;
/* Check that file.set.1.fd does not exist. */
rc = check_not_exist(ctx, &fileset, "1.fd");
if (rc == TEST_ERR)
return rc;
else if (rc != TEST_OK)
ret = rc;
/* Neither does file.set.1.name. */
rc = check_not_exist(ctx, &fileset, "1.name");
if (rc == TEST_ERR)
return rc;
else if (rc != TEST_OK)
ret = rc;
/*************************************************************
* Expand set to three files.
*/
attr.type = KDUMP_NUMBER;
attr.val.number = 3;
status = kdump_attr_ref_set(ctx, &number, &attr);
if (status != KDUMP_OK) {
fprintf(stderr, "Cannot set number of dump files: %s\n",
kdump_get_err(ctx));
ret = TEST_FAIL;
}
/* Check that file.set.1.fd and file.set.1.name exist now but
* have no value. */
rc = check_unset_file(ctx, &fileset, "1.fd", KDUMP_NUMBER);
if (rc == TEST_ERR)
return rc;
else if (rc != TEST_OK)
ret = rc;
rc = check_unset_file(ctx, &fileset, "1.name", KDUMP_STRING);
if (rc == TEST_ERR)
return rc;
else if (rc != TEST_OK)
ret = rc;
/* Check that file.set.2.fd and file.set.2.name also exist and
* have no value. */
rc = check_unset_file(ctx, &fileset, "2.fd", KDUMP_NUMBER);
if (rc == TEST_ERR)
return rc;
else if (rc != TEST_OK)
ret = rc;
rc = check_unset_file(ctx, &fileset, "2.name", KDUMP_STRING);
if (rc == TEST_ERR)
return rc;
else if (rc != TEST_OK)
ret = rc;
/* Check that file.set.3.fd and file.set.3.name do not exist. */
rc = check_not_exist(ctx, &fileset, "3.fd");
if (rc == TEST_ERR)
return rc;
else if (rc != TEST_OK)
ret = rc;
rc = check_not_exist(ctx, &fileset, "3.name");
if (rc == TEST_ERR)
return rc;
else if (rc != TEST_OK)
ret = rc;
/*************************************************************
* Store a file name in the attribute.
*/
/* Set file.set.0.name. */
attr.type = KDUMP_STRING;
attr.val.string = FILENAME_0;
status = kdump_set_sub_attr(ctx, &fileset, "0.name", &attr);
if (status != KDUMP_OK) {
fprintf(stderr, "Cannot set %s.0.name: %s\n",
KDUMP_ATTR_FILE_SET, kdump_get_err(ctx));
ret = TEST_FAIL;
}
/* Set file.set.1.name. */
attr.type = KDUMP_STRING;
attr.val.string = FILENAME_1;
status = kdump_set_sub_attr(ctx, &fileset, "1.name", &attr);
if (status != KDUMP_OK) {
fprintf(stderr, "Cannot set %s.1.name: %s\n",
KDUMP_ATTR_FILE_SET, kdump_get_err(ctx));
ret = TEST_FAIL;
}
/* Verify file.set.0.name. */
rc = check_filename(ctx, &fileset, "0.name", FILENAME_0);
if (rc == TEST_ERR)
return rc;
else if (rc != TEST_OK)
ret = rc;
/* Verify file.set.1.name. */
rc = check_filename(ctx, &fileset, "1.name", FILENAME_1);
if (rc == TEST_ERR)
return rc;
else if (rc != TEST_OK)
ret = rc;
/*************************************************************
* Reduce set to two files.
*/
attr.type = KDUMP_NUMBER;
attr.val.number = 2;
status = kdump_attr_ref_set(ctx, &number, &attr);
if (status != KDUMP_OK) {
fprintf(stderr, "Cannot set number of dump files: %s\n",
kdump_get_err(ctx));
ret = TEST_FAIL;
}
/* Check that file.set.2.fd and file.set.2.name no longer exist. */
rc = check_not_exist(ctx, &fileset, "2.fd");
if (rc == TEST_ERR)
return rc;
else if (rc != TEST_OK)
ret = rc;
rc = check_not_exist(ctx, &fileset, "2.name");
if (rc == TEST_ERR)
return rc;
else if (rc != TEST_OK)
ret = rc;
/* But file.set.1.fd and file.set.1.name still exist. */
rc = check_unset_file(ctx, &fileset, "1.fd", KDUMP_NUMBER);
if (rc == TEST_ERR)
return rc;
else if (rc != TEST_OK)
ret = rc;
rc = check_filename(ctx, &fileset, "1.name", FILENAME_1);
if (rc == TEST_ERR)
return rc;
else if (rc != TEST_OK)
ret = rc;
/*************************************************************
* Empty set.
*/
attr.type = KDUMP_NUMBER;
attr.val.number = 0;
status = kdump_attr_ref_set(ctx, &number, &attr);
if (status != KDUMP_OK) {
fprintf(stderr, "Cannot set number of dump files: %s\n",
kdump_get_err(ctx));
ret = TEST_FAIL;
}
/* Check that file.set.1.fd and file.set.1.name no longer exist. */
rc = check_not_exist(ctx, &fileset, "1.fd");
if (rc == TEST_ERR)
return rc;
else if (rc != TEST_OK)
ret = rc;
rc = check_not_exist(ctx, &fileset, "1.name");
if (rc == TEST_ERR)
return rc;
else if (rc != TEST_OK)
ret = rc;
/* Neither does file.set.0.fd or file.set.0.name. */
rc = check_not_exist(ctx, &fileset, "0.fd");
if (rc == TEST_ERR)
return rc;
else if (rc != TEST_OK)
ret = rc;
rc = check_not_exist(ctx, &fileset, "0.name");
if (rc == TEST_ERR)
return rc;
else if (rc != TEST_OK)
ret = rc;
/*************************************************************
* Interaction with kdump_open_fdset()
*/
char fname[] = "fdset.XXXXXX";
fd = mkstemp(fname);
if (fd < 0) {
perror("Cannot create temporary file");
return TEST_ERR;
}
remove(fname);
status = kdump_open_fd(ctx, fd);
if (status != KDUMP_OK && status != KDUMP_ERR_NOTIMPL) {
fprintf(stderr, "Cannot set dump file descriptor: %s\n",
kdump_get_err(ctx));
return TEST_ERR;
}
/* Check that fdset size is one. */
rc = check_fileset_size(ctx, 1);
if (rc == TEST_ERR)
return rc;
else if (rc != TEST_OK)
ret = rc;
/* Check that the file descriptor is found as file.set.0.fd. */
rc = check_fileset_zero(ctx, &fileset, fd);
if (rc == TEST_ERR)
return rc;
else if (rc != TEST_OK)
ret = rc;
/* Check that legacy file.fd is not set. */
status = kdump_get_attr(ctx, KDUMP_ATTR_FILE_FD, &attr);
if (status == KDUMP_OK) {
fprintf(stderr, "%s has a value, but it should not!\n",
KDUMP_ATTR_FILE_FD);
ret = TEST_FAIL;
} else if (status != KDUMP_ERR_NODATA) {
fprintf(stderr, "%s cannot be retrieved: %s\n",
KDUMP_ATTR_FILE_FD, kdump_get_err(ctx));
ret = TEST_FAIL;
}
/* Close dump. */
status = kdump_set_number_attr(ctx, KDUMP_ATTR_FILE_SET ".number", 0);
if (status != KDUMP_OK) {
fprintf(stderr, "Cannot set dump number to zero: %s\n",
kdump_get_err(ctx));
return TEST_ERR;
}
/*************************************************************
* Interaction with legacy file.fd attribute.
*/
status = kdump_set_number_attr(ctx, KDUMP_ATTR_FILE_FD, fd);
if (status != KDUMP_OK && status != KDUMP_ERR_NOTIMPL) {
fprintf(stderr, "Cannot set dump file descriptor: %s\n",
kdump_get_err(ctx));
return TEST_ERR;
}
/* Check that legacy file.fd is set. */
status = kdump_get_attr(ctx, KDUMP_ATTR_FILE_FD, &attr);
if (status != KDUMP_OK) {
fprintf(stderr, "%s cannot be retrieved: %s\n",
KDUMP_ATTR_FILE_FD, kdump_get_err(ctx));
ret = TEST_FAIL;
} else if (attr.val.number != fd) {
fprintf(stderr, "Wrong %s value: %" KDUMP_PRIuNUM " != %d\n",
KDUMP_ATTR_FILE_FD, attr.val.number, fd);
ret = TEST_FAIL;
}
/* Check that file set size is one. */
rc = check_fileset_size(ctx, 1);
if (rc == TEST_ERR)
return rc;
else if (rc != TEST_OK)
ret = rc;
/* Check that the file descriptor is also found as file.set.0.fd. */
rc = check_fileset_zero(ctx, &fileset, fd);
if (rc == TEST_ERR)
return rc;
else if (rc != TEST_OK)
ret = rc;
/*************************************************************
* RESET.
*/
attr.type = KDUMP_NUMBER;
attr.val.number = 0;
status = kdump_attr_ref_set(ctx, &number, &attr);
if (status != KDUMP_OK) {
fprintf(stderr, "Cannot clean up the file set: %s\n",
kdump_get_err(ctx));
rc = TEST_FAIL;
}
/*************************************************************
* Unset file names.
*/
names[0] = NULL;
names[1] = NULL;
status = kdump_set_filenames(ctx, 2, names);
if (status != KDUMP_OK) {
fprintf(stderr, "Cannot set file names: %s\n",
kdump_get_err(ctx));
rc = TEST_FAIL;
}
/* Check that fdset size is two. */
rc = check_fileset_size(ctx, 2);
if (rc == TEST_ERR)
return rc;
else if (rc != TEST_OK)
ret = rc;
/* Check that file.set.0.name and file.set.1.name are unset */
rc = check_unset_file(ctx, &fileset, "0.name", KDUMP_STRING);
if (rc == TEST_ERR)
return rc;
else if (rc != TEST_OK)
ret = rc;
rc = check_unset_file(ctx, &fileset, "1.name", KDUMP_STRING);
if (rc == TEST_ERR)
return rc;
else if (rc != TEST_OK)
ret = rc;
/*************************************************************
* Real file names.
*/
names[0] = FILENAME_0;
names[1] = FILENAME_1;
status = kdump_set_filenames(ctx, 2, names);
if (status != KDUMP_OK) {
fprintf(stderr, "Cannot set file names: %s\n",
kdump_get_err(ctx));
rc = TEST_FAIL;
}
/* Check that fdset size is two. */
rc = check_fileset_size(ctx, 2);
if (rc == TEST_ERR)
return rc;
else if (rc != TEST_OK)
ret = rc;
/* Verify file.set.0.name and file.set.1.name. */
rc = check_filename(ctx, &fileset, "0.name", FILENAME_0);
if (rc == TEST_ERR)
return rc;
else if (rc != TEST_OK)
ret = rc;
rc = check_filename(ctx, &fileset, "1.name", FILENAME_1);
if (rc == TEST_ERR)
return rc;
else if (rc != TEST_OK)
ret = rc;
/* Set the file descriptor and reduce the set to one file. */
status = kdump_open_fd(ctx, fd);
if (status != KDUMP_OK && status != KDUMP_ERR_NOTIMPL) {
fprintf(stderr, "Cannot set dump file descriptor: %s\n",
kdump_get_err(ctx));
return TEST_ERR;
}
/* Verify file.set.0.name is still set. */
rc = check_filename(ctx, &fileset, "0.name", FILENAME_0);
if (rc == TEST_ERR)
return rc;
else if (rc != TEST_OK)
ret = rc;
/* But file.set.1.name no longer exists. */
rc = check_not_exist(ctx, &fileset, "1.name");
if (rc == TEST_ERR)
return rc;
else if (rc != TEST_OK)
ret = rc;
/*************************************************************
* Clean up.
*/
close(fd);
kdump_attr_unref(ctx, &number);
kdump_attr_unref(ctx, &fileset);
kdump_free(ctx);
return ret;
}
|