virstoragetest.c 27.5 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 33 34

#define VIR_FROM_THIS VIR_FROM_NONE

35 36
VIR_LOG_INIT("tests.storagetest");

37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
#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 已提交
59
static char *canonwrap;
60
static char *absqed;
E
Eric Blake 已提交
61
static char *canonqed;
E
Eric Blake 已提交
62
static char *absdir;
E
Eric Blake 已提交
63
static char *canondir;
64 65 66 67 68 69 70 71 72 73 74
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 已提交
75
    VIR_FREE(canonwrap);
76
    VIR_FREE(absqed);
E
Eric Blake 已提交
77
    VIR_FREE(canonqed);
E
Eric Blake 已提交
78
    VIR_FREE(absdir);
E
Eric Blake 已提交
79
    VIR_FREE(canondir);
80 81 82 83 84 85 86 87
    VIR_FREE(abslink2);

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

88
    virFileDeleteTree(datadir);
89 90 91 92 93 94 95
}

static int
testPrepImages(void)
{
    int ret = EXIT_FAILURE;
    virCommandPtr cmd = NULL;
96 97
    char *buf = NULL;
    bool compat = false;
98 99 100 101

    qemuimg = virFindFileInPath("kvm-img");
    if (!qemuimg)
        qemuimg = virFindFileInPath("qemu-img");
102 103
    if (!qemuimg)
        goto skip;
104

105 106 107 108 109 110 111 112 113 114 115 116
    /* 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);

117 118 119 120
    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 已提交
121
        virAsprintf(&absdir, "%s/dir", datadir) < 0 ||
122
        virAsprintf(&abslink2, "%s/sub/link2", datadir) < 0)
123 124 125 126 127 128
        goto cleanup;

    if (virFileMakePath(datadir "/sub") < 0) {
        fprintf(stderr, "unable to create directory %s\n", datadir "/sub");
        goto cleanup;
    }
E
Eric Blake 已提交
129 130 131 132
    if (virFileMakePath(datadir "/dir") < 0) {
        fprintf(stderr, "unable to create directory %s\n", datadir "/dir");
        goto cleanup;
    }
E
Eric Blake 已提交
133 134 135 136
    if (!(canondir = canonicalize_file_name(absdir))) {
        virReportOOMError();
        goto cleanup;
    }
137 138 139 140 141 142

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

143 144
    if (virAsprintf(&buf, "%1024d", 0) < 0 ||
        virFileWriteStr("raw", buf, 0600) < 0) {
145
        fprintf(stderr, "unable to create raw file\n");
146
        goto cleanup;
147
    }
148 149 150 151 152 153 154 155
    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);
156 157 158 159
    cmd = virCommandNewArgList(qemuimg, "create", "-f", "qcow2", NULL);
    virCommandAddArgFormat(cmd, "-obacking_file=raw,backing_fmt=raw%s",
                           compat ? ",compat=0.10" : "");
    virCommandAddArg(cmd, "qcow2");
160
    if (virCommandRun(cmd, NULL) < 0)
161
        goto skip;
162 163
    /* Make sure our later uses of 'qemu-img rebase' will work */
    virCommandFree(cmd);
164
    cmd = virCommandNewArgList(qemuimg, "rebase", "-u", "-f", "qcow2",
165
                               "-F", "raw", "-b", "raw", "qcow2", NULL);
166 167
    if (virCommandRun(cmd, NULL) < 0)
        goto skip;
168 169 170 171 172 173 174 175
    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);
176
    cmd = virCommandNewArgList(qemuimg, "create", "-f", "qcow2", NULL);
177 178
    virCommandAddArgFormat(cmd, "-obacking_file=%s,backing_fmt=qcow2%s",
                           absqcow2, compat ? ",compat=1.1" : "");
179 180
    virCommandAddArg(cmd, "wrap");
    if (virCommandRun(cmd, NULL) < 0)
181
        goto skip;
E
Eric Blake 已提交
182 183 184 185
    if (!(canonwrap = canonicalize_file_name(abswrap))) {
        virReportOOMError();
        goto cleanup;
    }
186 187 188

    /* Create a qed file. */
    virCommandFree(cmd);
189
    cmd = virCommandNewArgList(qemuimg, "create", "-f", "qed", NULL);
190 191 192 193
    virCommandAddArgFormat(cmd, "-obacking_file=%s,backing_fmt=raw",
                           absraw);
    virCommandAddArg(cmd, "qed");
    if (virCommandRun(cmd, NULL) < 0)
194
        goto skip;
E
Eric Blake 已提交
195 196 197 198
    if (!(canonqed = canonicalize_file_name(absqed))) {
        virReportOOMError();
        goto cleanup;
    }
199

200
#ifdef HAVE_SYMLINK
201 202 203 204 205 206
    /* 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;
    }
207
#endif
208 209

    ret = 0;
210
 cleanup:
211
    VIR_FREE(buf);
212 213 214 215
    virCommandFree(cmd);
    if (ret)
        testCleanupImages();
    return ret;
216

217
 skip:
218 219 220
    fputs("qemu-img is too old; skipping this test\n", stderr);
    ret = EXIT_AM_SKIP;
    goto cleanup;
221 222
}

223 224 225 226 227
/* 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.  */
228 229 230 231 232
typedef struct _testFileData testFileData;
struct _testFileData
{
    const char *expBackingStore;
    const char *expBackingStoreRaw;
E
Eric Blake 已提交
233 234 235
    const char *expBackingDirRel;
    const char *expBackingDirAbs;
    enum virStorageFileFormat expBackingFormat;
236 237 238
    bool expIsFile;
    unsigned long long expCapacity;
    bool expEncrypted;
239 240
    const char *pathRel;
    const char *pathAbs;
E
Eric Blake 已提交
241 242 243 244 245
    const char *canonPath;
    const char *relDirRel;
    const char *relDirAbs;
    int type;
    int format;
246 247 248 249 250 251 252
};

enum {
    EXP_PASS = 0,
    EXP_FAIL = 1,
    EXP_WARN = 2,
    ALLOW_PROBE = 4,
253
    ABS_START = 8,
254 255 256 257 258 259
};

struct testChainData
{
    const char *start;
    enum virStorageFileFormat format;
260
    const testFileData *files[4];
261 262 263 264 265 266 267 268 269 270 271
    int nfiles;
    unsigned int flags;
};

static int
testStorageChain(const void *args)
{
    const struct testChainData *data = args;
    int ret = -1;
    virStorageFileMetadataPtr meta;
    virStorageFileMetadataPtr elt;
272
    size_t i = 0;
E
Eric Blake 已提交
273
    char *broken = NULL;
274
    bool isAbs = !!(data->flags & ABS_START);
275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293

    meta = virStorageFileGetMetadata(data->start, data->format, -1, -1,
                                     (data->flags & ALLOW_PROBE) != 0);
    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 已提交
294 295 296 297 298 299 300 301 302 303 304 305 306
        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;
        }
307 308 309 310 311 312
    }

    elt = meta;
    while (elt) {
        char *expect = NULL;
        char *actual = NULL;
E
Eric Blake 已提交
313
        const char *expBackingDirectory;
314
        const char *expPath;
E
Eric Blake 已提交
315
        const char *expRelDir;
316 317 318 319 320 321

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

E
Eric Blake 已提交
322 323
        expBackingDirectory = isAbs ? data->files[i]->expBackingDirAbs
            : data->files[i]->expBackingDirRel;
324 325
        expPath = isAbs ? data->files[i]->pathAbs
            : data->files[i]->pathRel;
E
Eric Blake 已提交
326 327
        expRelDir = isAbs ? data->files[i]->relDirAbs
            : data->files[i]->relDirRel;
328
        if (virAsprintf(&expect,
329
                        "store:%s\nraw:%s\ndirectory:%s\nother:%d %d %lld %d\n"
E
Eric Blake 已提交
330
                        "path:%s\ncanon:%s\nrelDir:%s\ntype:%d %d\n",
331 332
                        NULLSTR(data->files[i]->expBackingStore),
                        NULLSTR(data->files[i]->expBackingStoreRaw),
E
Eric Blake 已提交
333 334
                        NULLSTR(expBackingDirectory),
                        data->files[i]->expBackingFormat,
335 336
                        data->files[i]->expIsFile,
                        data->files[i]->expCapacity,
337
                        data->files[i]->expEncrypted,
E
Eric Blake 已提交
338 339 340 341 342
                        NULLSTR(expPath),
                        NULLSTR(data->files[i]->canonPath),
                        NULLSTR(expRelDir),
                        data->files[i]->type,
                        data->files[i]->format) < 0 ||
343
            virAsprintf(&actual,
344
                        "store:%s\nraw:%s\ndirectory:%s\nother:%d %d %lld %d\n"
E
Eric Blake 已提交
345
                        "path:%s\ncanon:%s\nrelDir:%s\ntype:%d %d\n",
346 347 348 349
                        NULLSTR(elt->backingStore),
                        NULLSTR(elt->backingStoreRaw),
                        NULLSTR(elt->directory),
                        elt->backingStoreFormat, elt->backingStoreIsFile,
350
                        elt->capacity, !!elt->encryption,
E
Eric Blake 已提交
351 352 353 354
                        NULLSTR(elt->path),
                        NULLSTR(elt->canonPath),
                        NULLSTR(elt->relDir),
                        elt->type, elt->format) < 0) {
355 356 357 358 359
            VIR_FREE(expect);
            VIR_FREE(actual);
            goto cleanup;
        }
        if (STRNEQ(expect, actual)) {
360
            fprintf(stderr, "chain member %zu", i);
361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376
            virtTestDifference(stderr, expect, actual);
            VIR_FREE(expect);
            VIR_FREE(actual);
            goto cleanup;
        }
        VIR_FREE(expect);
        VIR_FREE(actual);
        elt = elt->backingMeta;
        i++;
    }
    if (i != data->nfiles) {
        fprintf(stderr, "probed chain was too short\n");
        goto cleanup;
    }

    ret = 0;
377
 cleanup:
E
Eric Blake 已提交
378
    VIR_FREE(broken);
379 380 381 382 383 384 385 386 387
    virStorageFileFreeMetadata(meta);
    return ret;
}

static int
mymain(void)
{
    int ret;
    virCommandPtr cmd = NULL;
388
    struct testChainData data;
389 390 391 392 393 394

    /* 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;

395
#define TEST_ONE_CHAIN(id, start, format, flags, ...)                \
396
    do {                                                             \
397 398 399 400
        size_t i;                                                    \
        memset(&data, 0, sizeof(data));                              \
        data = (struct testChainData){                               \
            start, format, { __VA_ARGS__ }, 0, flags,                \
401
        };                                                           \
402 403 404
        for (i = 0; i < ARRAY_CARDINALITY(data.files); i++)          \
            if (data.files[i])                                       \
                data.nfiles++;                                       \
405
        if (virtTestRun("Storage backing chain " id,                 \
406 407 408 409
                        testStorageChain, &data) < 0)                \
            ret = -1;                                                \
    } while (0)

410 411 412
#define VIR_FLATTEN_2(...) __VA_ARGS__
#define VIR_FLATTEN_1(_1) VIR_FLATTEN_2 _1

413 414 415
#define TEST_CHAIN(id, relstart, absstart, format, chain1, flags1,   \
                   chain2, flags2, chain3, flags3, chain4, flags4)   \
    do {                                                             \
416 417 418 419
        TEST_ONE_CHAIN(#id "a", relstart, format, flags1,            \
                       VIR_FLATTEN_1(chain1));                       \
        TEST_ONE_CHAIN(#id "b", relstart, format, flags2,            \
                       VIR_FLATTEN_1(chain2));                       \
420
        TEST_ONE_CHAIN(#id "c", absstart, format, flags3 | ABS_START,\
421
                       VIR_FLATTEN_1(chain3));                       \
422
        TEST_ONE_CHAIN(#id "d", absstart, format, flags4 | ABS_START,\
423
                       VIR_FLATTEN_1(chain4));                       \
424 425 426 427 428
    } while (0)

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

    /* Missing file */
429
    TEST_ONE_CHAIN("0", "bogus", VIR_STORAGE_FILE_RAW, EXP_FAIL);
430 431

    /* Raw image, whether with right format or no specified format */
432
    testFileData raw = {
E
Eric Blake 已提交
433
        .expBackingFormat = VIR_STORAGE_FILE_NONE,
434 435
        .pathRel = "raw",
        .pathAbs = canonraw,
E
Eric Blake 已提交
436 437 438 439 440
        .canonPath = canonraw,
        .relDirRel = ".",
        .relDirAbs = datadir,
        .type = VIR_STORAGE_TYPE_FILE,
        .format = VIR_STORAGE_FILE_RAW,
441
    };
442
    TEST_CHAIN(1, "raw", absraw, VIR_STORAGE_FILE_RAW,
443 444 445 446
               (&raw), EXP_PASS,
               (&raw), ALLOW_PROBE | EXP_PASS,
               (&raw), EXP_PASS,
               (&raw), ALLOW_PROBE | EXP_PASS);
447
    TEST_CHAIN(2, "raw", absraw, VIR_STORAGE_FILE_AUTO,
448 449 450 451
               (&raw), EXP_PASS,
               (&raw), ALLOW_PROBE | EXP_PASS,
               (&raw), EXP_PASS,
               (&raw), ALLOW_PROBE | EXP_PASS);
452 453

    /* Qcow2 file with relative raw backing, format provided */
454
    raw.pathAbs = "raw";
455
    testFileData qcow2 = {
456 457
        .expBackingStore = canonraw,
        .expBackingStoreRaw = "raw",
E
Eric Blake 已提交
458 459 460
        .expBackingDirRel = ".",
        .expBackingDirAbs = datadir,
        .expBackingFormat = VIR_STORAGE_FILE_RAW,
461 462
        .expIsFile = true,
        .expCapacity = 1024,
463 464
        .pathRel = "qcow2",
        .pathAbs = canonqcow2,
E
Eric Blake 已提交
465 466 467 468 469
        .canonPath = canonqcow2,
        .relDirRel = ".",
        .relDirAbs = datadir,
        .type = VIR_STORAGE_TYPE_FILE,
        .format = VIR_STORAGE_FILE_QCOW2,
470 471 472 473 474
    };
    testFileData qcow2_as_raw = {
        .expBackingFormat = VIR_STORAGE_FILE_NONE,
        .pathRel = "qcow2",
        .pathAbs = canonqcow2,
E
Eric Blake 已提交
475 476 477 478 479
        .canonPath = canonqcow2,
        .relDirRel = ".",
        .relDirAbs = datadir,
        .type = VIR_STORAGE_TYPE_FILE,
        .format = VIR_STORAGE_FILE_RAW,
480
    };
481
    TEST_CHAIN(3, "qcow2", absqcow2, VIR_STORAGE_FILE_QCOW2,
482 483 484 485
               (&qcow2, &raw), EXP_PASS,
               (&qcow2, &raw), ALLOW_PROBE | EXP_PASS,
               (&qcow2, &raw), EXP_PASS,
               (&qcow2, &raw), ALLOW_PROBE | EXP_PASS);
486
    TEST_CHAIN(4, "qcow2", absqcow2, VIR_STORAGE_FILE_AUTO,
487
               (&qcow2_as_raw), EXP_PASS,
488
               (&qcow2, &raw), ALLOW_PROBE | EXP_PASS,
489
               (&qcow2_as_raw), EXP_PASS,
490
               (&qcow2, &raw), ALLOW_PROBE | EXP_PASS);
491 492 493 494 495 496 497

    /* 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;
498
    qcow2.expBackingStoreRaw = absraw;
E
Eric Blake 已提交
499
    qcow2.expBackingDirRel = datadir;
500 501
    raw.pathRel = absraw;
    raw.pathAbs = absraw;
E
Eric Blake 已提交
502
    raw.relDirRel = datadir;
503 504 505

    /* Qcow2 file with raw as absolute backing, backing format provided */
    TEST_CHAIN(5, "qcow2", absqcow2, VIR_STORAGE_FILE_QCOW2,
506 507 508 509
               (&qcow2, &raw), EXP_PASS,
               (&qcow2, &raw), ALLOW_PROBE | EXP_PASS,
               (&qcow2, &raw), EXP_PASS,
               (&qcow2, &raw), ALLOW_PROBE | EXP_PASS);
510
    TEST_CHAIN(6, "qcow2", absqcow2, VIR_STORAGE_FILE_AUTO,
511
               (&qcow2_as_raw), EXP_PASS,
512
               (&qcow2, &raw), ALLOW_PROBE | EXP_PASS,
513
               (&qcow2_as_raw), EXP_PASS,
514
               (&qcow2, &raw), ALLOW_PROBE | EXP_PASS);
515 516

    /* Wrapped file access */
517 518 519
    testFileData wrap = {
        .expBackingStore = canonqcow2,
        .expBackingStoreRaw = absqcow2,
E
Eric Blake 已提交
520 521 522
        .expBackingDirRel = datadir,
        .expBackingDirAbs = datadir,
        .expBackingFormat = VIR_STORAGE_FILE_QCOW2,
523 524
        .expIsFile = true,
        .expCapacity = 1024,
525 526
        .pathRel = "wrap",
        .pathAbs = abswrap,
E
Eric Blake 已提交
527 528 529 530 531
        .canonPath = canonwrap,
        .relDirRel = ".",
        .relDirAbs = datadir,
        .type = VIR_STORAGE_TYPE_FILE,
        .format = VIR_STORAGE_FILE_QCOW2,
532
    };
533
    qcow2.pathRel = absqcow2;
E
Eric Blake 已提交
534
    qcow2.relDirRel = datadir;
535
    TEST_CHAIN(7, "wrap", abswrap, VIR_STORAGE_FILE_QCOW2,
536 537 538 539
               (&wrap, &qcow2, &raw), EXP_PASS,
               (&wrap, &qcow2, &raw), ALLOW_PROBE | EXP_PASS,
               (&wrap, &qcow2, &raw), EXP_PASS,
               (&wrap, &qcow2, &raw), ALLOW_PROBE | EXP_PASS);
540 541 542 543 544 545 546 547 548 549 550 551 552

    /* 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;
E
Eric Blake 已提交
553 554
    wrap.expBackingFormat = VIR_STORAGE_FILE_AUTO;
    qcow2.expBackingFormat = VIR_STORAGE_FILE_AUTO;
555
    qcow2_as_raw.pathRel = absqcow2;
E
Eric Blake 已提交
556
    qcow2_as_raw.relDirRel = datadir;
557 558

    /* Qcow2 file with raw as absolute backing, backing format omitted */
559 560 561
    testFileData wrap_as_raw = {
        .expBackingStore = canonqcow2,
        .expBackingStoreRaw = absqcow2,
E
Eric Blake 已提交
562 563 564
        .expBackingDirRel = datadir,
        .expBackingDirAbs = datadir,
        .expBackingFormat = VIR_STORAGE_FILE_RAW,
565 566
        .expIsFile = true,
        .expCapacity = 1024,
567 568
        .pathRel = "wrap",
        .pathAbs = abswrap,
E
Eric Blake 已提交
569 570 571 572 573
        .canonPath = canonwrap,
        .relDirRel = ".",
        .relDirAbs = datadir,
        .type = VIR_STORAGE_TYPE_FILE,
        .format = VIR_STORAGE_FILE_QCOW2,
574
    };
575
    TEST_CHAIN(8, "wrap", abswrap, VIR_STORAGE_FILE_QCOW2,
576
               (&wrap_as_raw, &qcow2_as_raw), EXP_PASS,
577
               (&wrap, &qcow2, &raw), ALLOW_PROBE | EXP_PASS,
578
               (&wrap_as_raw, &qcow2_as_raw), EXP_PASS,
579
               (&wrap, &qcow2, &raw), ALLOW_PROBE | EXP_PASS);
580 581 582 583 584 585 586 587

    /* 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;
588 589
    qcow2.expBackingStore = NULL;
    qcow2.expBackingStoreRaw = datadir "/bogus";
E
Eric Blake 已提交
590
    qcow2.expBackingFormat = VIR_STORAGE_FILE_NONE;
591
    qcow2.expIsFile = false;
592
    qcow2.pathRel = "qcow2";
E
Eric Blake 已提交
593
    qcow2.relDirRel = ".";
594 595 596

    /* Qcow2 file with missing backing file but specified type */
    TEST_CHAIN(9, "qcow2", absqcow2, VIR_STORAGE_FILE_QCOW2,
597 598 599 600
               (&qcow2), EXP_WARN,
               (&qcow2), ALLOW_PROBE | EXP_WARN,
               (&qcow2), EXP_WARN,
               (&qcow2), ALLOW_PROBE | EXP_WARN);
601 602 603 604 605 606 607 608 609 610

    /* 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 */
    TEST_CHAIN(10, "qcow2", absqcow2, VIR_STORAGE_FILE_QCOW2,
611 612 613 614
               (&qcow2), EXP_WARN,
               (&qcow2), ALLOW_PROBE | EXP_WARN,
               (&qcow2), EXP_WARN,
               (&qcow2), ALLOW_PROBE | EXP_WARN);
615 616 617 618 619 620 621 622

    /* Rewrite qcow2 to use an nbd: protocol as backend */
    virCommandFree(cmd);
    cmd = virCommandNewArgList(qemuimg, "rebase", "-u", "-f", "qcow2",
                               "-F", "raw", "-b", "nbd:example.org:6000",
                               "qcow2", NULL);
    if (virCommandRun(cmd, NULL) < 0)
        ret = -1;
623 624
    qcow2.expBackingStore = "nbd:example.org:6000";
    qcow2.expBackingStoreRaw = NULL;
E
Eric Blake 已提交
625 626 627
    qcow2.expBackingDirRel = NULL;
    qcow2.expBackingDirAbs = NULL;
    qcow2.expBackingFormat = VIR_STORAGE_FILE_RAW;
628 629 630

    /* Qcow2 file with backing protocol instead of file */
    TEST_CHAIN(11, "qcow2", absqcow2, VIR_STORAGE_FILE_QCOW2,
631 632 633 634
               (&qcow2), EXP_PASS,
               (&qcow2), ALLOW_PROBE | EXP_PASS,
               (&qcow2), EXP_PASS,
               (&qcow2), ALLOW_PROBE | EXP_PASS);
635 636

    /* qed file */
637 638 639
    testFileData qed = {
        .expBackingStore = canonraw,
        .expBackingStoreRaw = absraw,
E
Eric Blake 已提交
640 641 642
        .expBackingDirRel = datadir,
        .expBackingDirAbs = datadir,
        .expBackingFormat = VIR_STORAGE_FILE_RAW,
643 644
        .expIsFile = true,
        .expCapacity = 1024,
645 646
        .pathRel = "qed",
        .pathAbs = absqed,
E
Eric Blake 已提交
647 648 649 650 651
        .canonPath = canonqed,
        .relDirRel = ".",
        .relDirAbs = datadir,
        .type = VIR_STORAGE_TYPE_FILE,
        .format = VIR_STORAGE_FILE_QED,
652 653 654 655 656
    };
    testFileData qed_as_raw = {
        .expBackingFormat = VIR_STORAGE_FILE_NONE,
        .pathRel = "qed",
        .pathAbs = absqed,
E
Eric Blake 已提交
657 658 659 660 661
        .canonPath = canonqed,
        .relDirRel = ".",
        .relDirAbs = datadir,
        .type = VIR_STORAGE_TYPE_FILE,
        .format = VIR_STORAGE_FILE_RAW,
662
    };
663
    TEST_CHAIN(12, "qed", absqed, VIR_STORAGE_FILE_AUTO,
664
               (&qed_as_raw), EXP_PASS,
665
               (&qed, &raw), ALLOW_PROBE | EXP_PASS,
666
               (&qed_as_raw), EXP_PASS,
667
               (&qed, &raw), ALLOW_PROBE | EXP_PASS);
668

E
Eric Blake 已提交
669
    /* directory */
670
    testFileData dir = {
671 672
        .pathRel = "dir",
        .pathAbs = absdir,
E
Eric Blake 已提交
673 674
        .type = VIR_STORAGE_TYPE_DIR,
        .format = VIR_STORAGE_FILE_DIR,
675
    };
E
Eric Blake 已提交
676 677 678 679 680 681 682 683 684 685 686
    TEST_CHAIN(13, "dir", absdir, VIR_STORAGE_FILE_AUTO,
               (&dir), EXP_PASS,
               (&dir), ALLOW_PROBE | EXP_PASS,
               (&dir), EXP_PASS,
               (&dir), ALLOW_PROBE | EXP_PASS);
    TEST_CHAIN(14, "dir", absdir, VIR_STORAGE_FILE_DIR,
               (&dir), EXP_PASS,
               (&dir), ALLOW_PROBE | EXP_PASS,
               (&dir), EXP_PASS,
               (&dir), ALLOW_PROBE | EXP_PASS);

687
#ifdef HAVE_SYMLINK
688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703
    /* 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 */
704
    testFileData link1 = {
705 706
        .expBackingStore = canonraw,
        .expBackingStoreRaw = "../raw",
E
Eric Blake 已提交
707 708 709
        .expBackingDirRel = "sub/../sub/..",
        .expBackingDirAbs = datadir "/sub/../sub/..",
        .expBackingFormat = VIR_STORAGE_FILE_RAW,
710 711
        .expIsFile = true,
        .expCapacity = 1024,
712 713
        .pathRel = "../sub/link1",
        .pathAbs = "../sub/link1",
E
Eric Blake 已提交
714 715 716 717 718
        .canonPath = canonqcow2,
        .relDirRel = "sub/../sub",
        .relDirAbs = datadir "/sub/../sub",
        .type = VIR_STORAGE_TYPE_FILE,
        .format = VIR_STORAGE_FILE_QCOW2,
719
    };
720
    testFileData link2 = {
721 722
        .expBackingStore = canonqcow2,
        .expBackingStoreRaw = "../sub/link1",
E
Eric Blake 已提交
723 724 725
        .expBackingDirRel = "sub/../sub",
        .expBackingDirAbs = datadir "/sub/../sub",
        .expBackingFormat = VIR_STORAGE_FILE_QCOW2,
726 727
        .expIsFile = true,
        .expCapacity = 1024,
728 729
        .pathRel = "sub/link2",
        .pathAbs = abslink2,
E
Eric Blake 已提交
730 731 732 733 734
        .canonPath = canonwrap,
        .relDirRel = "sub",
        .relDirAbs = datadir "/sub",
        .type = VIR_STORAGE_TYPE_FILE,
        .format = VIR_STORAGE_FILE_QCOW2,
735
    };
736 737
    raw.pathRel = "../raw";
    raw.pathAbs = "../raw";
E
Eric Blake 已提交
738 739
    raw.relDirRel = "sub/../sub/..";
    raw.relDirAbs = datadir "/sub/../sub/..";
E
Eric Blake 已提交
740
    TEST_CHAIN(15, "sub/link2", abslink2, VIR_STORAGE_FILE_QCOW2,
741 742 743 744
               (&link2, &link1, &raw), EXP_PASS,
               (&link2, &link1, &raw), ALLOW_PROBE | EXP_PASS,
               (&link2, &link1, &raw), EXP_PASS,
               (&link2, &link1, &raw), ALLOW_PROBE | EXP_PASS);
745
#endif
E
Eric Blake 已提交
746 747 748 749 750 751 752

    /* 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;
753 754
    qcow2.expBackingStore = NULL;
    qcow2.expBackingStoreRaw = "qcow2";
E
Eric Blake 已提交
755 756 757
    qcow2.expBackingDirRel = ".";
    qcow2.expBackingDirAbs = datadir;
    qcow2.expBackingFormat= VIR_STORAGE_FILE_NONE;
758
    qcow2.expIsFile = true;
E
Eric Blake 已提交
759 760 761

    /* Behavior of an infinite loop chain */
    TEST_CHAIN(16, "qcow2", absqcow2, VIR_STORAGE_FILE_QCOW2,
762 763 764 765
               (&qcow2), EXP_WARN,
               (&qcow2), ALLOW_PROBE | EXP_WARN,
               (&qcow2), EXP_WARN,
               (&qcow2), ALLOW_PROBE | EXP_WARN);
E
Eric Blake 已提交
766 767 768 769 770 771 772 773 774 775 776 777 778

    /* 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;
779
    qcow2.expBackingStoreRaw = "wrap";
E
Eric Blake 已提交
780
    qcow2.expBackingDirRel = datadir;
781
    qcow2.pathRel = absqcow2;
E
Eric Blake 已提交
782
    qcow2.relDirRel =  datadir;
E
Eric Blake 已提交
783
    wrap.expBackingFormat = VIR_STORAGE_FILE_QCOW2;
E
Eric Blake 已提交
784 785 786

    /* Behavior of an infinite loop chain */
    TEST_CHAIN(17, "wrap", abswrap, VIR_STORAGE_FILE_QCOW2,
787 788 789 790
               (&wrap, &qcow2), EXP_WARN,
               (&wrap, &qcow2), ALLOW_PROBE | EXP_WARN,
               (&wrap, &qcow2), EXP_WARN,
               (&wrap, &qcow2), ALLOW_PROBE | EXP_WARN);
E
Eric Blake 已提交
791

792 793 794 795 796 797 798 799
    /* Final cleanup */
    testCleanupImages();
    virCommandFree(cmd);

    return ret == 0 ? EXIT_SUCCESS : EXIT_FAILURE;
}

VIRT_TEST_MAIN(mymain)