virstoragetest.c 42.6 KB
Newer Older
1
/*
2
 * Copyright (C) 2013-2014 Red Hat, Inc.
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
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library.  If not, see
 * <http://www.gnu.org/licenses/>.
 *
 * Author: Eric Blake <eblake@redhat.com>
 */

#include <config.h>

#include <stdlib.h>

#include "testutils.h"
#include "vircommand.h"
#include "virerror.h"
28
#include "virfile.h"
29 30
#include "virlog.h"
#include "virstoragefile.h"
31
#include "virstring.h"
32
#include "dirname.h"
33

34 35
#include "storage/storage_driver.h"

36 37
#define VIR_FROM_THIS VIR_FROM_NONE

38 39
VIR_LOG_INIT("tests.storagetest");

40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
#define datadir abs_builddir "/virstoragedata"

/* This test creates the following files, all in datadir:

 * raw: 1024-byte raw file
 * qcow2: qcow2 file with 'raw' as backing
 * wrap: qcow2 file with 'qcow2' as backing
 * qed: qed file with 'raw' as backing
 * sub/link1: symlink to qcow2
 * sub/link2: symlink to wrap
 *
 * Relative names to these files are known at compile time, but absolute
 * and canonical names depend on where the test is run; for convenience,
 * we pre-populate the computation of these names for use during the test.
*/

static char *qemuimg;
static char *absraw;
static char *canonraw;
static char *absqcow2;
static char *canonqcow2;
static char *abswrap;
E
Eric Blake 已提交
62
static char *canonwrap;
63
static char *absqed;
E
Eric Blake 已提交
64
static char *canonqed;
E
Eric Blake 已提交
65
static char *absdir;
E
Eric Blake 已提交
66
static char *canondir;
67 68 69 70 71 72 73 74 75 76 77
static char *abslink2;

static void
testCleanupImages(void)
{
    VIR_FREE(qemuimg);
    VIR_FREE(absraw);
    VIR_FREE(canonraw);
    VIR_FREE(absqcow2);
    VIR_FREE(canonqcow2);
    VIR_FREE(abswrap);
E
Eric Blake 已提交
78
    VIR_FREE(canonwrap);
79
    VIR_FREE(absqed);
E
Eric Blake 已提交
80
    VIR_FREE(canonqed);
E
Eric Blake 已提交
81
    VIR_FREE(absdir);
E
Eric Blake 已提交
82
    VIR_FREE(canondir);
83 84 85 86 87 88 89 90
    VIR_FREE(abslink2);

    if (chdir(abs_builddir) < 0) {
        fprintf(stderr, "unable to return to correct directory, refusing to "
                "clean up %s\n", datadir);
        return;
    }

91
    virFileDeleteTree(datadir);
92 93
}

94 95 96 97 98 99 100

static virStorageSourcePtr
testStorageFileGetMetadata(const char *path,
                           int format,
                           uid_t uid, gid_t gid,
                           bool allow_probe)
{
101
    struct stat st;
102 103 104 105 106 107 108 109
    virStorageSourcePtr ret = NULL;

    if (VIR_ALLOC(ret) < 0)
        return NULL;

    ret->type = VIR_STORAGE_TYPE_FILE;
    ret->format = format;

110 111 112 113 114 115 116 117 118
    if (stat(path, &st) == 0) {
        if (S_ISDIR(st.st_mode)) {
            ret->type = VIR_STORAGE_TYPE_DIR;
            ret->format = VIR_STORAGE_FILE_DIR;
        } else if (S_ISBLK(st.st_mode)) {
            ret->type = VIR_STORAGE_TYPE_BLOCK;
        }
    }

119
    if (VIR_STRDUP(ret->path, path) < 0)
120 121 122 123 124 125 126 127 128 129 130 131
        goto error;

    if (virStorageFileGetMetadata(ret, uid, gid, allow_probe) < 0)
        goto error;

    return ret;

 error:
    virStorageSourceFree(ret);
    return NULL;
}

132 133 134 135 136
static int
testPrepImages(void)
{
    int ret = EXIT_FAILURE;
    virCommandPtr cmd = NULL;
137 138
    char *buf = NULL;
    bool compat = false;
139 140 141 142

    qemuimg = virFindFileInPath("kvm-img");
    if (!qemuimg)
        qemuimg = virFindFileInPath("qemu-img");
143 144
    if (!qemuimg)
        goto skip;
145

146 147 148
    /* Clean up from any earlier failed tests */
    virFileDeleteTree(datadir);

149 150 151 152 153 154 155 156 157 158 159 160
    /* See if qemu-img supports '-o compat=xxx'.  If so, we force the
     * use of both v2 and v3 files; if not, it is v2 only but the test
     * still works. */
    cmd = virCommandNewArgList(qemuimg, "create", "-f", "qcow2",
                               "-o?", "/dev/null", NULL);
    virCommandSetOutputBuffer(cmd, &buf);
    if (virCommandRun(cmd, NULL) < 0)
        goto skip;
    if (strstr(buf, "compat "))
        compat = true;
    VIR_FREE(buf);

161 162 163 164
    if (virAsprintf(&absraw, "%s/raw", datadir) < 0 ||
        virAsprintf(&absqcow2, "%s/qcow2", datadir) < 0 ||
        virAsprintf(&abswrap, "%s/wrap", datadir) < 0 ||
        virAsprintf(&absqed, "%s/qed", datadir) < 0 ||
E
Eric Blake 已提交
165
        virAsprintf(&absdir, "%s/dir", datadir) < 0 ||
166
        virAsprintf(&abslink2, "%s/sub/link2", datadir) < 0)
167 168 169 170 171 172
        goto cleanup;

    if (virFileMakePath(datadir "/sub") < 0) {
        fprintf(stderr, "unable to create directory %s\n", datadir "/sub");
        goto cleanup;
    }
E
Eric Blake 已提交
173 174 175 176
    if (virFileMakePath(datadir "/dir") < 0) {
        fprintf(stderr, "unable to create directory %s\n", datadir "/dir");
        goto cleanup;
    }
E
Eric Blake 已提交
177 178 179 180
    if (!(canondir = canonicalize_file_name(absdir))) {
        virReportOOMError();
        goto cleanup;
    }
181 182 183 184 185 186

    if (chdir(datadir) < 0) {
        fprintf(stderr, "unable to test relative backing chains\n");
        goto cleanup;
    }

187 188
    if (virAsprintf(&buf, "%1024d", 0) < 0 ||
        virFileWriteStr("raw", buf, 0600) < 0) {
189
        fprintf(stderr, "unable to create raw file\n");
190
        goto cleanup;
191
    }
192 193 194 195 196 197 198 199
    if (!(canonraw = canonicalize_file_name(absraw))) {
        virReportOOMError();
        goto cleanup;
    }

    /* Create a qcow2 wrapping relative raw; later on, we modify its
     * metadata to test other configurations */
    virCommandFree(cmd);
200 201 202 203
    cmd = virCommandNewArgList(qemuimg, "create", "-f", "qcow2", NULL);
    virCommandAddArgFormat(cmd, "-obacking_file=raw,backing_fmt=raw%s",
                           compat ? ",compat=0.10" : "");
    virCommandAddArg(cmd, "qcow2");
204
    if (virCommandRun(cmd, NULL) < 0)
205
        goto skip;
206 207
    /* Make sure our later uses of 'qemu-img rebase' will work */
    virCommandFree(cmd);
208
    cmd = virCommandNewArgList(qemuimg, "rebase", "-u", "-f", "qcow2",
209
                               "-F", "raw", "-b", "raw", "qcow2", NULL);
210 211
    if (virCommandRun(cmd, NULL) < 0)
        goto skip;
212 213 214 215 216 217 218 219
    if (!(canonqcow2 = canonicalize_file_name(absqcow2))) {
        virReportOOMError();
        goto cleanup;
    }

    /* Create a second qcow2 wrapping the first, to be sure that we
     * can correctly avoid insecure probing.  */
    virCommandFree(cmd);
220
    cmd = virCommandNewArgList(qemuimg, "create", "-f", "qcow2", NULL);
221 222
    virCommandAddArgFormat(cmd, "-obacking_file=%s,backing_fmt=qcow2%s",
                           absqcow2, compat ? ",compat=1.1" : "");
223 224
    virCommandAddArg(cmd, "wrap");
    if (virCommandRun(cmd, NULL) < 0)
225
        goto skip;
E
Eric Blake 已提交
226 227 228 229
    if (!(canonwrap = canonicalize_file_name(abswrap))) {
        virReportOOMError();
        goto cleanup;
    }
230 231 232

    /* Create a qed file. */
    virCommandFree(cmd);
233
    cmd = virCommandNewArgList(qemuimg, "create", "-f", "qed", NULL);
234 235 236 237
    virCommandAddArgFormat(cmd, "-obacking_file=%s,backing_fmt=raw",
                           absraw);
    virCommandAddArg(cmd, "qed");
    if (virCommandRun(cmd, NULL) < 0)
238
        goto skip;
E
Eric Blake 已提交
239 240 241 242
    if (!(canonqed = canonicalize_file_name(absqed))) {
        virReportOOMError();
        goto cleanup;
    }
243

244
#ifdef HAVE_SYMLINK
245 246 247 248 249 250
    /* Create some symlinks in a sub-directory. */
    if (symlink("../qcow2", datadir "/sub/link1") < 0 ||
        symlink("../wrap", datadir "/sub/link2") < 0) {
        fprintf(stderr, "unable to create symlink");
        goto cleanup;
    }
251
#endif
252 253

    ret = 0;
254
 cleanup:
255
    VIR_FREE(buf);
256 257 258 259
    virCommandFree(cmd);
    if (ret)
        testCleanupImages();
    return ret;
260

261
 skip:
262 263 264
    fputs("qemu-img is too old; skipping this test\n", stderr);
    ret = EXIT_AM_SKIP;
    goto cleanup;
265 266
}

267 268 269 270 271
/* Many fields of virStorageFileMetadata have the same content whether
 * we access the file relatively or absolutely; but file names differ
 * depending on how the chain was opened.  For ease of testing, we
 * test both relative and absolute starts, and use a flag to say which
 * of the two variations to compare against.  */
272 273 274 275 276 277
typedef struct _testFileData testFileData;
struct _testFileData
{
    const char *expBackingStoreRaw;
    unsigned long long expCapacity;
    bool expEncrypted;
278
    const char *pathRel;
279
    const char *path;
E
Eric Blake 已提交
280 281
    int type;
    int format;
282 283 284 285 286 287 288 289 290 291 292 293
};

enum {
    EXP_PASS = 0,
    EXP_FAIL = 1,
    EXP_WARN = 2,
    ALLOW_PROBE = 4,
};

struct testChainData
{
    const char *start;
294
    virStorageFileFormat format;
295
    const testFileData *files[4];
296 297 298 299
    int nfiles;
    unsigned int flags;
};

300 301

static const char testStorageChainFormat[] =
302
    "chain member: %zu\n"
303
    "path:%s\n"
304 305 306 307 308 309 310
    "backingStoreRaw: %s\n"
    "capacity: %lld\n"
    "encryption: %d\n"
    "relPath:%s\n"
    "type:%d\n"
    "format:%d\n";

311 312 313 314 315
static int
testStorageChain(const void *args)
{
    const struct testChainData *data = args;
    int ret = -1;
316 317
    virStorageSourcePtr meta;
    virStorageSourcePtr elt;
318
    size_t i = 0;
E
Eric Blake 已提交
319
    char *broken = NULL;
320

321 322
    meta = testStorageFileGetMetadata(data->start, data->format, -1, -1,
                                      (data->flags & ALLOW_PROBE) != 0);
323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338
    if (!meta) {
        if (data->flags & EXP_FAIL) {
            virResetLastError();
            ret = 0;
        }
        goto cleanup;
    } else if (data->flags & EXP_FAIL) {
        fprintf(stderr, "call should have failed\n");
        goto cleanup;
    }
    if (data->flags & EXP_WARN) {
        if (!virGetLastError()) {
            fprintf(stderr, "call should have warned\n");
            goto cleanup;
        }
        virResetLastError();
E
Eric Blake 已提交
339 340 341 342 343 344 345 346 347 348 349 350 351
        if (virStorageFileChainGetBroken(meta, &broken) || !broken) {
            fprintf(stderr, "call should identify broken part of chain\n");
            goto cleanup;
        }
    } else {
        if (virGetLastError()) {
            fprintf(stderr, "call should not have warned\n");
            goto cleanup;
        }
        if (virStorageFileChainGetBroken(meta, &broken) || broken) {
            fprintf(stderr, "chain should not be identified as broken\n");
            goto cleanup;
        }
352 353 354 355 356 357 358 359 360 361 362 363 364
    }

    elt = meta;
    while (elt) {
        char *expect = NULL;
        char *actual = NULL;

        if (i == data->nfiles) {
            fprintf(stderr, "probed chain was too long\n");
            goto cleanup;
        }

        if (virAsprintf(&expect,
365
                        testStorageChainFormat, i,
366
                        NULLSTR(data->files[i]->path),
367 368
                        NULLSTR(data->files[i]->expBackingStoreRaw),
                        data->files[i]->expCapacity,
369
                        data->files[i]->expEncrypted,
370
                        NULLSTR(data->files[i]->pathRel),
E
Eric Blake 已提交
371 372
                        data->files[i]->type,
                        data->files[i]->format) < 0 ||
373
            virAsprintf(&actual,
374
                        testStorageChainFormat, i,
375
                        NULLSTR(elt->path),
376
                        NULLSTR(elt->backingStoreRaw),
377 378
                        elt->capacity,
                        !!elt->encryption,
379
                        NULLSTR(elt->relPath),
380 381
                        elt->type,
                        elt->format) < 0) {
382 383 384 385 386 387 388 389 390 391 392 393
            VIR_FREE(expect);
            VIR_FREE(actual);
            goto cleanup;
        }
        if (STRNEQ(expect, actual)) {
            virtTestDifference(stderr, expect, actual);
            VIR_FREE(expect);
            VIR_FREE(actual);
            goto cleanup;
        }
        VIR_FREE(expect);
        VIR_FREE(actual);
394
        elt = elt->backingStore;
395 396 397 398 399 400 401 402
        i++;
    }
    if (i != data->nfiles) {
        fprintf(stderr, "probed chain was too short\n");
        goto cleanup;
    }

    ret = 0;
403
 cleanup:
E
Eric Blake 已提交
404
    VIR_FREE(broken);
405
    virStorageSourceFree(meta);
406 407 408
    return ret;
}

E
Eric Blake 已提交
409 410
struct testLookupData
{
411
    virStorageSourcePtr chain;
412
    const char *target;
E
Eric Blake 已提交
413
    virStorageSourcePtr from;
E
Eric Blake 已提交
414
    const char *name;
415
    unsigned int expIndex;
E
Eric Blake 已提交
416
    const char *expResult;
417
    virStorageSourcePtr expMeta;
E
Eric Blake 已提交
418 419 420 421 422 423 424 425
    const char *expParent;
};

static int
testStorageLookup(const void *args)
{
    const struct testLookupData *data = args;
    int ret = 0;
426
    virStorageSourcePtr result;
E
Eric Blake 已提交
427
    const char *actualParent;
428 429 430 431 432 433 434 435 436 437 438
    unsigned int idx;

    if (virStorageFileParseChainIndex(data->target, data->name, &idx) < 0 &&
        data->expIndex) {
        fprintf(stderr, "call should not have failed\n");
        ret = -1;
    }
    if (idx != data->expIndex) {
        fprintf(stderr, "index: expected %u, got %u\n", data->expIndex, idx);
        ret = -1;
    }
E
Eric Blake 已提交
439

440
     /* Test twice to ensure optional parameter doesn't cause NULL deref. */
E
Eric Blake 已提交
441
    result = virStorageFileChainLookup(data->chain, data->from,
442 443
                                       idx ? NULL : data->name,
                                       idx, NULL);
E
Eric Blake 已提交
444 445 446 447 448 449 450 451 452 453 454 455 456 457

    if (!data->expResult) {
        if (!virGetLastError()) {
            fprintf(stderr, "call should have failed\n");
            ret = -1;
        }
        virResetLastError();
    } else {
        if (virGetLastError()) {
            fprintf(stderr, "call should not have warned\n");
            ret = -1;
        }
    }

458 459 460 461 462 463 464
    if (!result) {
        if (data->expResult) {
            fprintf(stderr, "result 1: expected %s, got NULL\n",
                    data->expResult);
            ret = -1;
        }
    } else if (STRNEQ_NULLABLE(data->expResult, result->path)) {
E
Eric Blake 已提交
465
        fprintf(stderr, "result 1: expected %s, got %s\n",
466
                NULLSTR(data->expResult), NULLSTR(result->path));
E
Eric Blake 已提交
467 468 469
        ret = -1;
    }

E
Eric Blake 已提交
470
    result = virStorageFileChainLookup(data->chain, data->from,
471
                                       data->name, idx, &actualParent);
E
Eric Blake 已提交
472 473
    if (!data->expResult)
        virResetLastError();
474 475 476 477 478 479 480 481

    if (!result) {
        if (data->expResult) {
            fprintf(stderr, "result 2: expected %s, got NULL\n",
                    data->expResult);
            ret = -1;
        }
    } else if (STRNEQ_NULLABLE(data->expResult, result->path)) {
E
Eric Blake 已提交
482
        fprintf(stderr, "result 2: expected %s, got %s\n",
483
                NULLSTR(data->expResult), NULLSTR(result->path));
E
Eric Blake 已提交
484 485
        ret = -1;
    }
486
    if (data->expMeta != result) {
E
Eric Blake 已提交
487
        fprintf(stderr, "meta: expected %p, got %p\n",
488
                data->expMeta, result);
E
Eric Blake 已提交
489 490 491 492 493 494 495 496 497 498 499
        ret = -1;
    }
    if (STRNEQ_NULLABLE(data->expParent, actualParent)) {
        fprintf(stderr, "parent: expected %s, got %s\n",
                NULLSTR(data->expParent), NULLSTR(actualParent));
        ret = -1;
    }

    return ret;
}

500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516

struct testPathCanonicalizeData
{
    const char *path;
    const char *expect;
};

static const char *testPathCanonicalizeSymlinks[][2] =
{
    {"/path/blah", "/other/path/huzah"},
    {"/path/to/relative/symlink", "../../actual/file"},
    {"/cycle", "/cycle"},
    {"/cycle2/link", "./link"},
};

static int
testPathCanonicalizeReadlink(const char *path,
517
                             char **linkpath,
518 519 520 521
                             void *data ATTRIBUTE_UNUSED)
{
    size_t i;

522
    *linkpath = NULL;
523 524 525

    for (i = 0; i < ARRAY_CARDINALITY(testPathCanonicalizeSymlinks); i++) {
        if (STREQ(path, testPathCanonicalizeSymlinks[i][0])) {
526
            if (VIR_STRDUP(*linkpath, testPathCanonicalizeSymlinks[i][1]) < 0)
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
                return -1;

            return 0;
        }
    }

    return 1;
}


static int
testPathCanonicalize(const void *args)
{
    const struct testPathCanonicalizeData *data = args;
    char *canon = NULL;
    int ret = -1;

    canon = virStorageFileCanonicalizePath(data->path,
                                           testPathCanonicalizeReadlink,
                                           NULL);

    if (STRNEQ_NULLABLE(data->expect, canon)) {
        fprintf(stderr,
                "path canonicalization of '%s' failed: expected '%s' got '%s'\n",
                data->path, NULLSTR(data->expect), NULLSTR(canon));

        goto cleanup;
    }

    ret = 0;

 cleanup:
    VIR_FREE(canon);

    return ret;
}

564 565 566 567 568 569 570 571 572 573 574 575 576
static virStorageSource backingchain[12];

static void
testPathRelativePrepare(void)
{
    size_t i;

    for (i = 0; i < ARRAY_CARDINALITY(backingchain); i++) {
        if (i < ARRAY_CARDINALITY(backingchain) - 1)
            backingchain[i].backingStore = &backingchain[i + 1];
        else
            backingchain[i].backingStore = NULL;

577
        backingchain[i].relPath = NULL;
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
    }

    /* normal relative backing chain */
    backingchain[0].path = (char *) "/path/to/some/img";

    backingchain[1].path = (char *) "/path/to/some/asdf";
    backingchain[1].relPath = (char *) "asdf";

    backingchain[2].path = (char *) "/path/to/some/test";
    backingchain[2].relPath = (char *) "test";

    backingchain[3].path = (char *) "/path/to/some/blah";
    backingchain[3].relPath = (char *) "blah";

    /* ovirt's backing chain */
    backingchain[4].path = (char *) "/path/to/volume/image1";

    backingchain[5].path = (char *) "/path/to/volume/image2";
    backingchain[5].relPath = (char *) "../volume/image2";

    backingchain[6].path = (char *) "/path/to/volume/image3";
    backingchain[6].relPath = (char *) "../volume/image3";

    backingchain[7].path = (char *) "/path/to/volume/image4";
    backingchain[7].relPath = (char *) "../volume/image4";

    /* some arbitrarily crazy backing chains */
    backingchain[8].path = (char *) "/crazy/base/image";

    backingchain[9].path = (char *) "/crazy/base/directory/stuff/volumes/garbage/image2";
    backingchain[9].relPath = (char *) "directory/stuff/volumes/garbage/image2";

    backingchain[10].path = (char *) "/crazy/base/directory/image3";
    backingchain[10].relPath = (char *) "../../../image3";

    backingchain[11].path = (char *) "/crazy/base/blah/image4";
    backingchain[11].relPath = (char *) "../blah/image4";
}


struct testPathRelativeBacking
{
    virStorageSourcePtr top;
    virStorageSourcePtr base;

    const char *expect;
};

static int
testPathRelative(const void *args)
{
    const struct testPathRelativeBacking *data = args;
    char *actual = NULL;
    int ret = -1;

    if (virStorageFileGetRelativeBackingPath(data->top,
                                             data->base,
                                             &actual) < 0) {
        fprintf(stderr, "relative backing path resolution failed\n");
        goto cleanup;
    }

    if (STRNEQ_NULLABLE(data->expect, actual)) {
        fprintf(stderr, "relative path resolution from '%s' to '%s': "
                "expected '%s', got '%s'\n",
                data->top->path, data->base->path,
                NULLSTR(data->expect), NULLSTR(actual));
        goto cleanup;
    }

    ret = 0;

 cleanup:
    VIR_FREE(actual);

    return ret;
}

656

657 658 659 660 661
static int
mymain(void)
{
    int ret;
    virCommandPtr cmd = NULL;
662
    struct testChainData data;
E
Eric Blake 已提交
663
    struct testLookupData data2;
664
    struct testPathCanonicalizeData data3;
665
    struct testPathRelativeBacking data4;
666
    virStorageSourcePtr chain = NULL;
E
Eric Blake 已提交
667 668
    virStorageSourcePtr chain2; /* short for chain->backingStore */
    virStorageSourcePtr chain3; /* short for chain2->backingStore */
669 670 671 672 673 674

    /* Prep some files with qemu-img; if that is not found on PATH, or
     * if it lacks support for qcow2 and qed, skip this test.  */
    if ((ret = testPrepImages()) != 0)
        return ret;

675
#define TEST_ONE_CHAIN(id, start, format, flags, ...)                \
676
    do {                                                             \
677 678 679 680
        size_t i;                                                    \
        memset(&data, 0, sizeof(data));                              \
        data = (struct testChainData){                               \
            start, format, { __VA_ARGS__ }, 0, flags,                \
681
        };                                                           \
682 683 684
        for (i = 0; i < ARRAY_CARDINALITY(data.files); i++)          \
            if (data.files[i])                                       \
                data.nfiles++;                                       \
685
        if (virtTestRun("Storage backing chain " id,                 \
686 687 688 689
                        testStorageChain, &data) < 0)                \
            ret = -1;                                                \
    } while (0)

690 691 692
#define VIR_FLATTEN_2(...) __VA_ARGS__
#define VIR_FLATTEN_1(_1) VIR_FLATTEN_2 _1

693 694 695 696
#define TEST_CHAIN(id, path, format, chain1, flags1, chain2, flags2)           \
    do {                                                                       \
        TEST_ONE_CHAIN(#id "a", path, format, flags1, VIR_FLATTEN_1(chain1));  \
        TEST_ONE_CHAIN(#id "b", path, format, flags2, VIR_FLATTEN_1(chain2));  \
697 698 699 700 701
    } while (0)

    /* The actual tests, in several groups. */

    /* Missing file */
702
    TEST_ONE_CHAIN("0", "bogus", VIR_STORAGE_FILE_RAW, EXP_FAIL);
703 704

    /* Raw image, whether with right format or no specified format */
705
    testFileData raw = {
706
        .path = canonraw,
E
Eric Blake 已提交
707 708
        .type = VIR_STORAGE_TYPE_FILE,
        .format = VIR_STORAGE_FILE_RAW,
709
    };
710
    TEST_CHAIN(1, absraw, VIR_STORAGE_FILE_RAW,
711 712
               (&raw), EXP_PASS,
               (&raw), ALLOW_PROBE | EXP_PASS);
713
    TEST_CHAIN(2, absraw, VIR_STORAGE_FILE_AUTO,
714 715
               (&raw), EXP_PASS,
               (&raw), ALLOW_PROBE | EXP_PASS);
716 717

    /* Qcow2 file with relative raw backing, format provided */
718
    raw.pathRel = "raw";
719
    testFileData qcow2 = {
720 721
        .expBackingStoreRaw = "raw",
        .expCapacity = 1024,
722
        .path = canonqcow2,
E
Eric Blake 已提交
723 724
        .type = VIR_STORAGE_TYPE_FILE,
        .format = VIR_STORAGE_FILE_QCOW2,
725 726
    };
    testFileData qcow2_as_raw = {
727
        .path = canonqcow2,
E
Eric Blake 已提交
728 729
        .type = VIR_STORAGE_TYPE_FILE,
        .format = VIR_STORAGE_FILE_RAW,
730
    };
731
    TEST_CHAIN(3, absqcow2, VIR_STORAGE_FILE_QCOW2,
732 733
               (&qcow2, &raw), EXP_PASS,
               (&qcow2, &raw), ALLOW_PROBE | EXP_PASS);
734
    TEST_CHAIN(4, absqcow2, VIR_STORAGE_FILE_AUTO,
735
               (&qcow2_as_raw), EXP_PASS,
736
               (&qcow2, &raw), ALLOW_PROBE | EXP_PASS);
737 738 739 740 741 742 743

    /* Rewrite qcow2 file to use absolute backing name */
    virCommandFree(cmd);
    cmd = virCommandNewArgList(qemuimg, "rebase", "-u", "-f", "qcow2",
                               "-F", "raw", "-b", absraw, "qcow2", NULL);
    if (virCommandRun(cmd, NULL) < 0)
        ret = -1;
744
    qcow2.expBackingStoreRaw = absraw;
745
    raw.pathRel = NULL;
746 747

    /* Qcow2 file with raw as absolute backing, backing format provided */
748
    TEST_CHAIN(5, absqcow2, VIR_STORAGE_FILE_QCOW2,
749 750
               (&qcow2, &raw), EXP_PASS,
               (&qcow2, &raw), ALLOW_PROBE | EXP_PASS);
751
    TEST_CHAIN(6, absqcow2, VIR_STORAGE_FILE_AUTO,
752
               (&qcow2_as_raw), EXP_PASS,
753
               (&qcow2, &raw), ALLOW_PROBE | EXP_PASS);
754 755

    /* Wrapped file access */
756 757 758
    testFileData wrap = {
        .expBackingStoreRaw = absqcow2,
        .expCapacity = 1024,
759
        .path = canonwrap,
E
Eric Blake 已提交
760 761
        .type = VIR_STORAGE_TYPE_FILE,
        .format = VIR_STORAGE_FILE_QCOW2,
762
    };
763
    TEST_CHAIN(7, abswrap, VIR_STORAGE_FILE_QCOW2,
764 765
               (&wrap, &qcow2, &raw), EXP_PASS,
               (&wrap, &qcow2, &raw), ALLOW_PROBE | EXP_PASS);
766 767 768 769 770 771 772 773 774 775 776 777 778 779 780

    /* Rewrite qcow2 and wrap file to omit backing file type */
    virCommandFree(cmd);
    cmd = virCommandNewArgList(qemuimg, "rebase", "-u", "-f", "qcow2",
                               "-b", absraw, "qcow2", NULL);
    if (virCommandRun(cmd, NULL) < 0)
        ret = -1;

    virCommandFree(cmd);
    cmd = virCommandNewArgList(qemuimg, "rebase", "-u", "-f", "qcow2",
                               "-b", absqcow2, "wrap", NULL);
    if (virCommandRun(cmd, NULL) < 0)
        ret = -1;

    /* Qcow2 file with raw as absolute backing, backing format omitted */
781 782 783
    testFileData wrap_as_raw = {
        .expBackingStoreRaw = absqcow2,
        .expCapacity = 1024,
784
        .path = canonwrap,
E
Eric Blake 已提交
785 786
        .type = VIR_STORAGE_TYPE_FILE,
        .format = VIR_STORAGE_FILE_QCOW2,
787
    };
788
    TEST_CHAIN(8, abswrap, VIR_STORAGE_FILE_QCOW2,
789
               (&wrap_as_raw, &qcow2_as_raw), EXP_PASS,
790
               (&wrap, &qcow2, &raw), ALLOW_PROBE | EXP_PASS);
791 792 793 794 795 796 797 798

    /* Rewrite qcow2 to a missing backing file, with backing type */
    virCommandFree(cmd);
    cmd = virCommandNewArgList(qemuimg, "rebase", "-u", "-f", "qcow2",
                               "-F", "qcow2", "-b", datadir "/bogus",
                               "qcow2", NULL);
    if (virCommandRun(cmd, NULL) < 0)
        ret = -1;
799
    qcow2.expBackingStoreRaw = datadir "/bogus";
800 801

    /* Qcow2 file with missing backing file but specified type */
802
    TEST_CHAIN(9, absqcow2, VIR_STORAGE_FILE_QCOW2,
803 804
               (&qcow2), EXP_WARN,
               (&qcow2), ALLOW_PROBE | EXP_WARN);
805 806 807 808 809 810 811 812 813

    /* Rewrite qcow2 to a missing backing file, without backing type */
    virCommandFree(cmd);
    cmd = virCommandNewArgList(qemuimg, "rebase", "-u", "-f", "qcow2",
                               "-b", datadir "/bogus", "qcow2", NULL);
    if (virCommandRun(cmd, NULL) < 0)
        ret = -1;

    /* Qcow2 file with missing backing file and no specified type */
814
    TEST_CHAIN(10, absqcow2, VIR_STORAGE_FILE_QCOW2,
815 816
               (&qcow2), EXP_WARN,
               (&qcow2), ALLOW_PROBE | EXP_WARN);
817 818 819 820

    /* Rewrite qcow2 to use an nbd: protocol as backend */
    virCommandFree(cmd);
    cmd = virCommandNewArgList(qemuimg, "rebase", "-u", "-f", "qcow2",
821
                               "-F", "raw", "-b", "nbd:example.org:6000:exportname=blah",
822 823 824
                               "qcow2", NULL);
    if (virCommandRun(cmd, NULL) < 0)
        ret = -1;
825
    qcow2.expBackingStoreRaw = "nbd:example.org:6000:exportname=blah";
826 827

    /* Qcow2 file with backing protocol instead of file */
828
    testFileData nbd = {
829
        .path = "blah",
830 831 832
        .type = VIR_STORAGE_TYPE_NETWORK,
        .format = VIR_STORAGE_FILE_RAW,
    };
833
    TEST_CHAIN(11, absqcow2, VIR_STORAGE_FILE_QCOW2,
834 835
               (&qcow2, &nbd), EXP_PASS,
               (&qcow2, &nbd), ALLOW_PROBE | EXP_PASS);
836 837

    /* qed file */
838 839 840
    testFileData qed = {
        .expBackingStoreRaw = absraw,
        .expCapacity = 1024,
841
        .path = canonqed,
E
Eric Blake 已提交
842 843
        .type = VIR_STORAGE_TYPE_FILE,
        .format = VIR_STORAGE_FILE_QED,
844 845
    };
    testFileData qed_as_raw = {
846
        .path = canonqed,
E
Eric Blake 已提交
847 848
        .type = VIR_STORAGE_TYPE_FILE,
        .format = VIR_STORAGE_FILE_RAW,
849
    };
850
    TEST_CHAIN(12, absqed, VIR_STORAGE_FILE_AUTO,
851
               (&qed_as_raw), EXP_PASS,
852
               (&qed, &raw), ALLOW_PROBE | EXP_PASS);
853

E
Eric Blake 已提交
854
    /* directory */
855
    testFileData dir = {
856
        .path = canondir,
E
Eric Blake 已提交
857 858
        .type = VIR_STORAGE_TYPE_DIR,
        .format = VIR_STORAGE_FILE_DIR,
859
    };
860
    TEST_CHAIN(13, absdir, VIR_STORAGE_FILE_AUTO,
E
Eric Blake 已提交
861 862
               (&dir), EXP_PASS,
               (&dir), ALLOW_PROBE | EXP_PASS);
863
    TEST_CHAIN(14, absdir, VIR_STORAGE_FILE_DIR,
E
Eric Blake 已提交
864 865 866
               (&dir), EXP_PASS,
               (&dir), ALLOW_PROBE | EXP_PASS);

867
#ifdef HAVE_SYMLINK
868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883
    /* Rewrite qcow2 and wrap file to use backing names relative to a
     * symlink from a different directory */
    virCommandFree(cmd);
    cmd = virCommandNewArgList(qemuimg, "rebase", "-u", "-f", "qcow2",
                               "-F", "raw", "-b", "../raw", "qcow2", NULL);
    if (virCommandRun(cmd, NULL) < 0)
        ret = -1;

    virCommandFree(cmd);
    cmd = virCommandNewArgList(qemuimg, "rebase", "-u", "-f", "qcow2",
                               "-F", "qcow2", "-b", "../sub/link1", "wrap",
                               NULL);
    if (virCommandRun(cmd, NULL) < 0)
        ret = -1;

    /* Behavior of symlinks to qcow2 with relative backing files */
884
    testFileData link1 = {
885 886
        .expBackingStoreRaw = "../raw",
        .expCapacity = 1024,
887
        .pathRel = "../sub/link1",
888
        .path = datadir "/sub/../sub/link1",
E
Eric Blake 已提交
889 890
        .type = VIR_STORAGE_TYPE_FILE,
        .format = VIR_STORAGE_FILE_QCOW2,
891
    };
892
    testFileData link2 = {
893 894
        .expBackingStoreRaw = "../sub/link1",
        .expCapacity = 1024,
895
        .path = abslink2,
E
Eric Blake 已提交
896 897
        .type = VIR_STORAGE_TYPE_FILE,
        .format = VIR_STORAGE_FILE_QCOW2,
898
    };
899 900

    raw.path = datadir "/sub/../sub/../raw";
901
    raw.pathRel = "../raw";
902
    TEST_CHAIN(15, abslink2, VIR_STORAGE_FILE_QCOW2,
903 904
               (&link2, &link1, &raw), EXP_PASS,
               (&link2, &link1, &raw), ALLOW_PROBE | EXP_PASS);
905
#endif
E
Eric Blake 已提交
906 907 908 909 910 911 912

    /* Rewrite qcow2 to be a self-referential loop */
    virCommandFree(cmd);
    cmd = virCommandNewArgList(qemuimg, "rebase", "-u", "-f", "qcow2",
                               "-F", "qcow2", "-b", "qcow2", "qcow2", NULL);
    if (virCommandRun(cmd, NULL) < 0)
        ret = -1;
913
    qcow2.expBackingStoreRaw = "qcow2";
E
Eric Blake 已提交
914 915

    /* Behavior of an infinite loop chain */
916
    TEST_CHAIN(16, absqcow2, VIR_STORAGE_FILE_QCOW2,
917 918
               (&qcow2), EXP_WARN,
               (&qcow2), ALLOW_PROBE | EXP_WARN);
E
Eric Blake 已提交
919 920 921 922 923 924 925 926 927 928 929 930 931

    /* Rewrite wrap and qcow2 to be mutually-referential loop */
    virCommandFree(cmd);
    cmd = virCommandNewArgList(qemuimg, "rebase", "-u", "-f", "qcow2",
                               "-F", "qcow2", "-b", "wrap", "qcow2", NULL);
    if (virCommandRun(cmd, NULL) < 0)
        ret = -1;

    virCommandFree(cmd);
    cmd = virCommandNewArgList(qemuimg, "rebase", "-u", "-f", "qcow2",
                               "-F", "qcow2", "-b", absqcow2, "wrap", NULL);
    if (virCommandRun(cmd, NULL) < 0)
        ret = -1;
932
    qcow2.expBackingStoreRaw = "wrap";
E
Eric Blake 已提交
933 934

    /* Behavior of an infinite loop chain */
935
    TEST_CHAIN(17, abswrap, VIR_STORAGE_FILE_QCOW2,
936 937
               (&wrap, &qcow2), EXP_WARN,
               (&wrap, &qcow2), ALLOW_PROBE | EXP_WARN);
E
Eric Blake 已提交
938

E
Eric Blake 已提交
939 940 941 942 943 944 945
    /* Rewrite wrap and qcow2 back to 3-deep chain, absolute backing */
    virCommandFree(cmd);
    cmd = virCommandNewArgList(qemuimg, "rebase", "-u", "-f", "qcow2",
                               "-F", "qcow2", "-b", absraw, "qcow2", NULL);
    if (virCommandRun(cmd, NULL) < 0)
        ret = -1;

946
    /* Test behavior of chain lookups, absolute backing from relative start */
947 948
    chain = testStorageFileGetMetadata("wrap", VIR_STORAGE_FILE_QCOW2,
                                       -1, -1, false);
E
Eric Blake 已提交
949 950 951 952
    if (!chain) {
        ret = -1;
        goto cleanup;
    }
E
Eric Blake 已提交
953 954
    chain2 = chain->backingStore;
    chain3 = chain2->backingStore;
E
Eric Blake 已提交
955

E
Eric Blake 已提交
956
#define TEST_LOOKUP_TARGET(id, target, from, name, index, result,       \
E
Eric Blake 已提交
957
                           meta, parent)                                \
E
Eric Blake 已提交
958 959 960 961 962 963 964
    do {                                                                \
        data2 = (struct testLookupData){                                \
            chain, target, from, name, index,                           \
            result, meta, parent, };                                    \
        if (virtTestRun("Chain lookup " #id,                            \
                        testStorageLookup, &data2) < 0)                 \
            ret = -1;                                                   \
E
Eric Blake 已提交
965
    } while (0)
E
Eric Blake 已提交
966 967 968 969
#define TEST_LOOKUP(id, from, name, result, meta, parent)               \
    TEST_LOOKUP_TARGET(id, NULL, from, name, 0, result, meta, parent)

    TEST_LOOKUP(0, NULL, "bogus", NULL, NULL, NULL);
E
Eric Blake 已提交
970
    TEST_LOOKUP(1, chain, "bogus", NULL, NULL, NULL);
E
Eric Blake 已提交
971
    TEST_LOOKUP(2, NULL, "wrap", chain->path, chain, NULL);
E
Eric Blake 已提交
972
    TEST_LOOKUP(3, chain, "wrap", NULL, NULL, NULL);
E
Eric Blake 已提交
973
    TEST_LOOKUP(4, chain2, "wrap", NULL, NULL, NULL);
E
Eric Blake 已提交
974
    TEST_LOOKUP(5, NULL, abswrap, chain->path, chain, NULL);
E
Eric Blake 已提交
975
    TEST_LOOKUP(6, chain, abswrap, NULL, NULL, NULL);
E
Eric Blake 已提交
976
    TEST_LOOKUP(7, chain2, abswrap, NULL, NULL, NULL);
E
Eric Blake 已提交
977
    TEST_LOOKUP(8, NULL, "qcow2", chain2->path, chain2, chain->path);
E
Eric Blake 已提交
978
    TEST_LOOKUP(9, chain, "qcow2", chain2->path, chain2, chain->path);
E
Eric Blake 已提交
979
    TEST_LOOKUP(10, chain2, "qcow2", NULL, NULL, NULL);
E
Eric Blake 已提交
980
    TEST_LOOKUP(11, chain3, "qcow2", NULL, NULL, NULL);
E
Eric Blake 已提交
981
    TEST_LOOKUP(12, NULL, absqcow2, chain2->path, chain2, chain->path);
E
Eric Blake 已提交
982
    TEST_LOOKUP(13, chain, absqcow2, chain2->path, chain2, chain->path);
E
Eric Blake 已提交
983
    TEST_LOOKUP(14, chain2, absqcow2, NULL, NULL, NULL);
E
Eric Blake 已提交
984
    TEST_LOOKUP(15, chain3, absqcow2, NULL, NULL, NULL);
E
Eric Blake 已提交
985
    TEST_LOOKUP(16, NULL, "raw", chain3->path, chain3, chain2->path);
E
Eric Blake 已提交
986 987
    TEST_LOOKUP(17, chain, "raw", chain3->path, chain3, chain2->path);
    TEST_LOOKUP(18, chain2, "raw", chain3->path, chain3, chain2->path);
E
Eric Blake 已提交
988
    TEST_LOOKUP(19, chain3, "raw", NULL, NULL, NULL);
E
Eric Blake 已提交
989
    TEST_LOOKUP(20, NULL, absraw, chain3->path, chain3, chain2->path);
E
Eric Blake 已提交
990 991
    TEST_LOOKUP(21, chain, absraw, chain3->path, chain3, chain2->path);
    TEST_LOOKUP(22, chain2, absraw, chain3->path, chain3, chain2->path);
E
Eric Blake 已提交
992
    TEST_LOOKUP(23, chain3, absraw, NULL, NULL, NULL);
E
Eric Blake 已提交
993
    TEST_LOOKUP(24, NULL, NULL, chain3->path, chain3, chain2->path);
E
Eric Blake 已提交
994 995
    TEST_LOOKUP(25, chain, NULL, chain3->path, chain3, chain2->path);
    TEST_LOOKUP(26, chain2, NULL, chain3->path, chain3, chain2->path);
E
Eric Blake 已提交
996
    TEST_LOOKUP(27, chain3, NULL, NULL, NULL, NULL);
E
Eric Blake 已提交
997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010

    /* Rewrite wrap and qcow2 back to 3-deep chain, relative backing */
    virCommandFree(cmd);
    cmd = virCommandNewArgList(qemuimg, "rebase", "-u", "-f", "qcow2",
                               "-F", "raw", "-b", "raw", "qcow2", NULL);
    if (virCommandRun(cmd, NULL) < 0)
        ret = -1;

    virCommandFree(cmd);
    cmd = virCommandNewArgList(qemuimg, "rebase", "-u", "-f", "qcow2",
                               "-F", "qcow2", "-b", "qcow2", "wrap", NULL);
    if (virCommandRun(cmd, NULL) < 0)
        ret = -1;

1011
    /* Test behavior of chain lookups, relative backing from absolute start */
1012
    virStorageSourceFree(chain);
1013 1014
    chain = testStorageFileGetMetadata(abswrap, VIR_STORAGE_FILE_QCOW2,
                                       -1, -1, false);
E
Eric Blake 已提交
1015 1016 1017 1018
    if (!chain) {
        ret = -1;
        goto cleanup;
    }
E
Eric Blake 已提交
1019 1020
    chain2 = chain->backingStore;
    chain3 = chain2->backingStore;
E
Eric Blake 已提交
1021

E
Eric Blake 已提交
1022
    TEST_LOOKUP(28, NULL, "bogus", NULL, NULL, NULL);
E
Eric Blake 已提交
1023
    TEST_LOOKUP(29, chain, "bogus", NULL, NULL, NULL);
E
Eric Blake 已提交
1024
    TEST_LOOKUP(30, NULL, "wrap", chain->path, chain, NULL);
E
Eric Blake 已提交
1025
    TEST_LOOKUP(31, chain, "wrap", NULL, NULL, NULL);
E
Eric Blake 已提交
1026
    TEST_LOOKUP(32, chain2, "wrap", NULL, NULL, NULL);
E
Eric Blake 已提交
1027
    TEST_LOOKUP(33, NULL, abswrap, chain->path, chain, NULL);
E
Eric Blake 已提交
1028
    TEST_LOOKUP(34, chain, abswrap, NULL, NULL, NULL);
E
Eric Blake 已提交
1029
    TEST_LOOKUP(35, chain2, abswrap, NULL, NULL, NULL);
E
Eric Blake 已提交
1030
    TEST_LOOKUP(36, NULL, "qcow2", chain2->path, chain2, chain->path);
E
Eric Blake 已提交
1031
    TEST_LOOKUP(37, chain, "qcow2", chain2->path, chain2, chain->path);
E
Eric Blake 已提交
1032
    TEST_LOOKUP(38, chain2, "qcow2", NULL, NULL, NULL);
E
Eric Blake 已提交
1033
    TEST_LOOKUP(39, chain3, "qcow2", NULL, NULL, NULL);
E
Eric Blake 已提交
1034
    TEST_LOOKUP(40, NULL, absqcow2, chain2->path, chain2, chain->path);
E
Eric Blake 已提交
1035
    TEST_LOOKUP(41, chain, absqcow2, chain2->path, chain2, chain->path);
E
Eric Blake 已提交
1036
    TEST_LOOKUP(42, chain2, absqcow2, NULL, NULL, NULL);
E
Eric Blake 已提交
1037
    TEST_LOOKUP(43, chain3, absqcow2, NULL, NULL, NULL);
E
Eric Blake 已提交
1038
    TEST_LOOKUP(44, NULL, "raw", chain3->path, chain3, chain2->path);
E
Eric Blake 已提交
1039 1040
    TEST_LOOKUP(45, chain, "raw", chain3->path, chain3, chain2->path);
    TEST_LOOKUP(46, chain2, "raw", chain3->path, chain3, chain2->path);
E
Eric Blake 已提交
1041
    TEST_LOOKUP(47, chain3, "raw", NULL, NULL, NULL);
E
Eric Blake 已提交
1042
    TEST_LOOKUP(48, NULL, absraw, chain3->path, chain3, chain2->path);
E
Eric Blake 已提交
1043 1044
    TEST_LOOKUP(49, chain, absraw, chain3->path, chain3, chain2->path);
    TEST_LOOKUP(50, chain2, absraw, chain3->path, chain3, chain2->path);
E
Eric Blake 已提交
1045
    TEST_LOOKUP(51, chain3, absraw, NULL, NULL, NULL);
E
Eric Blake 已提交
1046
    TEST_LOOKUP(52, NULL, NULL, chain3->path, chain3, chain2->path);
E
Eric Blake 已提交
1047 1048
    TEST_LOOKUP(53, chain, NULL, chain3->path, chain3, chain2->path);
    TEST_LOOKUP(54, chain2, NULL, chain3->path, chain3, chain2->path);
E
Eric Blake 已提交
1049
    TEST_LOOKUP(55, chain3, NULL, NULL, NULL, NULL);
E
Eric Blake 已提交
1050 1051 1052 1053 1054 1055 1056 1057 1058

    /* Use link to wrap with cross-directory relative backing */
    virCommandFree(cmd);
    cmd = virCommandNewArgList(qemuimg, "rebase", "-u", "-f", "qcow2",
                               "-F", "qcow2", "-b", "../qcow2", "wrap", NULL);
    if (virCommandRun(cmd, NULL) < 0)
        ret = -1;

    /* Test behavior of chain lookups, relative backing */
1059
    virStorageSourceFree(chain);
1060 1061
    chain = testStorageFileGetMetadata("sub/link2", VIR_STORAGE_FILE_QCOW2,
                                       -1, -1, false);
E
Eric Blake 已提交
1062 1063 1064 1065
    if (!chain) {
        ret = -1;
        goto cleanup;
    }
E
Eric Blake 已提交
1066 1067
    chain2 = chain->backingStore;
    chain3 = chain2->backingStore;
E
Eric Blake 已提交
1068

E
Eric Blake 已提交
1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083
    TEST_LOOKUP(56, NULL, "bogus", NULL, NULL, NULL);
    TEST_LOOKUP(57, NULL, "sub/link2", chain->path, chain, NULL);
    TEST_LOOKUP(58, NULL, "wrap", chain->path, chain, NULL);
    TEST_LOOKUP(59, NULL, abswrap, chain->path, chain, NULL);
    TEST_LOOKUP(60, NULL, "../qcow2", chain2->path, chain2, chain->path);
    TEST_LOOKUP(61, NULL, "qcow2", NULL, NULL, NULL);
    TEST_LOOKUP(62, NULL, absqcow2, chain2->path, chain2, chain->path);
    TEST_LOOKUP(63, NULL, "raw", chain3->path, chain3, chain2->path);
    TEST_LOOKUP(64, NULL, absraw, chain3->path, chain3, chain2->path);
    TEST_LOOKUP(65, NULL, NULL, chain3->path, chain3, chain2->path);

    TEST_LOOKUP_TARGET(66, "vda", NULL, "bogus[1]", 0, NULL, NULL, NULL);
    TEST_LOOKUP_TARGET(67, "vda", NULL, "vda[-1]", 0, NULL, NULL, NULL);
    TEST_LOOKUP_TARGET(68, "vda", NULL, "vda[1][1]", 0, NULL, NULL, NULL);
    TEST_LOOKUP_TARGET(69, "vda", NULL, "wrap", 0, chain->path, chain, NULL);
E
Eric Blake 已提交
1084
    TEST_LOOKUP_TARGET(70, "vda", chain, "wrap", 0, NULL, NULL, NULL);
E
Eric Blake 已提交
1085
    TEST_LOOKUP_TARGET(71, "vda", chain2, "wrap", 0, NULL, NULL, NULL);
E
Eric Blake 已提交
1086 1087
    TEST_LOOKUP_TARGET(72, "vda", NULL, "vda[0]", 0, NULL, NULL, NULL);
    TEST_LOOKUP_TARGET(73, "vda", NULL, "vda[1]", 1, chain2->path, chain2,
1088
                       chain->path);
E
Eric Blake 已提交
1089 1090
    TEST_LOOKUP_TARGET(74, "vda", chain, "vda[1]", 1, chain2->path, chain2,
                       chain->path);
E
Eric Blake 已提交
1091
    TEST_LOOKUP_TARGET(75, "vda", chain2, "vda[1]", 1, NULL, NULL, NULL);
E
Eric Blake 已提交
1092
    TEST_LOOKUP_TARGET(76, "vda", chain3, "vda[1]", 1, NULL, NULL, NULL);
E
Eric Blake 已提交
1093
    TEST_LOOKUP_TARGET(77, "vda", NULL, "vda[2]", 2, chain3->path, chain3,
E
Eric Blake 已提交
1094
                       chain2->path);
E
Eric Blake 已提交
1095 1096 1097 1098
    TEST_LOOKUP_TARGET(78, "vda", chain, "vda[2]", 2, chain3->path, chain3,
                       chain2->path);
    TEST_LOOKUP_TARGET(79, "vda", chain2, "vda[2]", 2, chain3->path, chain3,
                       chain2->path);
E
Eric Blake 已提交
1099
    TEST_LOOKUP_TARGET(80, "vda", chain3, "vda[2]", 2, NULL, NULL, NULL);
E
Eric Blake 已提交
1100
    TEST_LOOKUP_TARGET(81, "vda", NULL, "vda[3]", 3, NULL, NULL, NULL);
1101

1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144
#define TEST_PATH_CANONICALIZE(id, PATH, EXPECT)                            \
    do {                                                                    \
        data3.path = PATH;                                                  \
        data3.expect = EXPECT;                                              \
        if (virtTestRun("Path canonicalize " #id,                           \
                        testPathCanonicalize, &data3) < 0)                  \
            ret = -1;                                                       \
    } while (0)

    TEST_PATH_CANONICALIZE(1, "/", "/");
    TEST_PATH_CANONICALIZE(2, "/path", "/path");
    TEST_PATH_CANONICALIZE(3, "/path/to/blah", "/path/to/blah");
    TEST_PATH_CANONICALIZE(4, "/path/", "/path");
    TEST_PATH_CANONICALIZE(5, "///////", "/");
    TEST_PATH_CANONICALIZE(6, "//", "//");
    TEST_PATH_CANONICALIZE(7, "", "");
    TEST_PATH_CANONICALIZE(8, ".", ".");
    TEST_PATH_CANONICALIZE(9, "../", "..");
    TEST_PATH_CANONICALIZE(10, "../../", "../..");
    TEST_PATH_CANONICALIZE(11, "../../blah", "../../blah");
    TEST_PATH_CANONICALIZE(12, "/./././blah", "/blah");
    TEST_PATH_CANONICALIZE(13, ".././../././../blah", "../../../blah");
    TEST_PATH_CANONICALIZE(14, "/././", "/");
    TEST_PATH_CANONICALIZE(15, "./././", ".");
    TEST_PATH_CANONICALIZE(16, "blah/../foo", "foo");
    TEST_PATH_CANONICALIZE(17, "foo/bar/../blah", "foo/blah");
    TEST_PATH_CANONICALIZE(18, "foo/bar/.././blah", "foo/blah");
    TEST_PATH_CANONICALIZE(19, "/path/to/foo/bar/../../../../../../../../baz", "/baz");
    TEST_PATH_CANONICALIZE(20, "path/to/foo/bar/../../../../../../../../baz", "../../../../baz");
    TEST_PATH_CANONICALIZE(21, "path/to/foo/bar", "path/to/foo/bar");
    TEST_PATH_CANONICALIZE(22, "//foo//bar", "//foo/bar");
    TEST_PATH_CANONICALIZE(23, "/bar//foo", "/bar/foo");
    TEST_PATH_CANONICALIZE(24, "//../blah", "//blah");

    /* test paths with symlinks */
    TEST_PATH_CANONICALIZE(25, "/path/blah", "/other/path/huzah");
    TEST_PATH_CANONICALIZE(26, "/path/to/relative/symlink", "/path/actual/file");
    TEST_PATH_CANONICALIZE(27, "/path/to/relative/symlink/blah", "/path/actual/file/blah");
    TEST_PATH_CANONICALIZE(28, "/path/blah/yippee", "/other/path/huzah/yippee");
    TEST_PATH_CANONICALIZE(29, "/cycle", NULL);
    TEST_PATH_CANONICALIZE(30, "/cycle2/link", NULL);
    TEST_PATH_CANONICALIZE(31, "///", "/");

1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191
#define TEST_RELATIVE_BACKING(id, TOP, BASE, EXPECT)                        \
    do {                                                                    \
        data4.top = &TOP;                                                   \
        data4.base = &BASE;                                                 \
        data4.expect = EXPECT;                                              \
        if (virtTestRun("Path relative resolve " #id,                       \
                        testPathRelative, &data4) < 0)                      \
            ret = -1;                                                       \
    } while (0)

    testPathRelativePrepare();

    /* few negative tests first */

    /* a non-relative image is in the backing chain span */
    TEST_RELATIVE_BACKING(1, backingchain[0], backingchain[1], NULL);
    TEST_RELATIVE_BACKING(2, backingchain[0], backingchain[2], NULL);
    TEST_RELATIVE_BACKING(3, backingchain[0], backingchain[3], NULL);
    TEST_RELATIVE_BACKING(4, backingchain[1], backingchain[5], NULL);

    /* image is not in chain (specified backwards) */
    TEST_RELATIVE_BACKING(5, backingchain[2], backingchain[1], NULL);

    /* positive tests */
    TEST_RELATIVE_BACKING(6, backingchain[1], backingchain[1], "asdf");
    TEST_RELATIVE_BACKING(7, backingchain[1], backingchain[2], "test");
    TEST_RELATIVE_BACKING(8, backingchain[1], backingchain[3], "blah");
    TEST_RELATIVE_BACKING(9, backingchain[2], backingchain[2], "test");
    TEST_RELATIVE_BACKING(10, backingchain[2], backingchain[3], "blah");
    TEST_RELATIVE_BACKING(11, backingchain[3], backingchain[3], "blah");

    /* oVirt spelling */
    TEST_RELATIVE_BACKING(12, backingchain[5], backingchain[5], "../volume/image2");
    TEST_RELATIVE_BACKING(13, backingchain[5], backingchain[6], "../volume/../volume/image3");
    TEST_RELATIVE_BACKING(14, backingchain[5], backingchain[7], "../volume/../volume/../volume/image4");
    TEST_RELATIVE_BACKING(15, backingchain[6], backingchain[6], "../volume/image3");
    TEST_RELATIVE_BACKING(16, backingchain[6], backingchain[7], "../volume/../volume/image4");
    TEST_RELATIVE_BACKING(17, backingchain[7], backingchain[7], "../volume/image4");

    /* crazy spellings */
    TEST_RELATIVE_BACKING(17, backingchain[9], backingchain[9], "directory/stuff/volumes/garbage/image2");
    TEST_RELATIVE_BACKING(18, backingchain[9], backingchain[10], "directory/stuff/volumes/garbage/../../../image3");
    TEST_RELATIVE_BACKING(19, backingchain[9], backingchain[11], "directory/stuff/volumes/garbage/../../../../blah/image4");
    TEST_RELATIVE_BACKING(20, backingchain[10], backingchain[10], "../../../image3");
    TEST_RELATIVE_BACKING(21, backingchain[10], backingchain[11], "../../../../blah/image4");
    TEST_RELATIVE_BACKING(22, backingchain[11], backingchain[11], "../blah/image4");

E
Eric Blake 已提交
1192
 cleanup:
1193
    /* Final cleanup */
1194
    virStorageSourceFree(chain);
1195 1196 1197 1198 1199 1200 1201
    testCleanupImages();
    virCommandFree(cmd);

    return ret == 0 ? EXIT_SUCCESS : EXIT_FAILURE;
}

VIRT_TEST_MAIN(mymain)