virstoragetest.c 61.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
#include "storage/storage_driver.h"
35
#include "storage/storage_source.h"
36

37 38
#define VIR_FROM_THIS VIR_FROM_NONE

39 40
VIR_LOG_INIT("tests.storagetest");

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

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

92
    virFileDeleteTree(datadir);
93 94
}

95 96 97 98 99 100 101

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

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

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

111 112 113 114 115 116 117 118 119
    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;
        }
    }

120
    if (VIR_STRDUP(ret->path, path) < 0)
121 122
        goto error;

123
    if (virStorageFileGetMetadata(ret, uid, gid, allow_probe, false) < 0)
124 125 126 127 128 129 130 131 132
        goto error;

    return ret;

 error:
    virStorageSourceFree(ret);
    return NULL;
}

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

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

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

148 149 150 151 152 153 154 155 156 157 158 159
    /* 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);

160 161 162 163
    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 已提交
164
        virAsprintf(&absdir, "%s/dir", datadir) < 0 ||
165
        virAsprintf(&abslink2, "%s/sub/link2", datadir) < 0)
166 167 168 169 170 171
        goto cleanup;

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

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

186 187
    if (virAsprintf(&buf, "%1024d", 0) < 0 ||
        virFileWriteStr("raw", buf, 0600) < 0) {
188
        fprintf(stderr, "unable to create raw file\n");
189
        goto cleanup;
190
    }
191 192 193 194 195 196 197 198
    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);
199 200 201 202
    cmd = virCommandNewArgList(qemuimg, "create", "-f", "qcow2", NULL);
    virCommandAddArgFormat(cmd, "-obacking_file=raw,backing_fmt=raw%s",
                           compat ? ",compat=0.10" : "");
    virCommandAddArg(cmd, "qcow2");
203
    if (virCommandRun(cmd, NULL) < 0)
204
        goto skip;
205 206
    /* Make sure our later uses of 'qemu-img rebase' will work */
    virCommandFree(cmd);
207
    cmd = virCommandNewArgList(qemuimg, "rebase", "-u", "-f", "qcow2",
208
                               "-F", "raw", "-b", "raw", "qcow2", NULL);
209 210
    if (virCommandRun(cmd, NULL) < 0)
        goto skip;
211 212 213 214 215 216 217 218
    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);
219
    cmd = virCommandNewArgList(qemuimg, "create", "-f", "qcow2", NULL);
220 221
    virCommandAddArgFormat(cmd, "-obacking_file=%s,backing_fmt=qcow2%s",
                           absqcow2, compat ? ",compat=1.1" : "");
222 223
    virCommandAddArg(cmd, "wrap");
    if (virCommandRun(cmd, NULL) < 0)
224
        goto skip;
E
Eric Blake 已提交
225 226 227 228
    if (!(canonwrap = canonicalize_file_name(abswrap))) {
        virReportOOMError();
        goto cleanup;
    }
229 230 231

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

243
#ifdef HAVE_SYMLINK
244 245 246 247 248 249
    /* 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;
    }
250
#endif
251 252

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

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

266 267 268 269 270
/* 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.  */
271 272 273 274 275 276
typedef struct _testFileData testFileData;
struct _testFileData
{
    const char *expBackingStoreRaw;
    unsigned long long expCapacity;
    bool expEncrypted;
277
    const char *pathRel;
278
    const char *path;
E
Eric Blake 已提交
279 280
    int type;
    int format;
281 282 283
    const char *secret;
    const char *hostname;
    int protocol;
284 285 286 287 288 289 290 291 292 293 294 295
};

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

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

302 303

static const char testStorageChainFormat[] =
304
    "chain member: %zu\n"
305
    "path:%s\n"
306 307 308 309 310
    "backingStoreRaw: %s\n"
    "capacity: %lld\n"
    "encryption: %d\n"
    "relPath:%s\n"
    "type:%d\n"
311 312 313 314
    "format:%d\n"
    "protocol:%s\n"
    "hostname:%s\n"
    "secret:%s\n";
315

316 317 318 319 320
static int
testStorageChain(const void *args)
{
    const struct testChainData *data = args;
    int ret = -1;
321 322
    virStorageSourcePtr meta;
    virStorageSourcePtr elt;
323
    size_t i = 0;
E
Eric Blake 已提交
324
    char *broken = NULL;
325

326 327
    meta = testStorageFileGetMetadata(data->start, data->format, -1, -1,
                                      (data->flags & ALLOW_PROBE) != 0);
328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343
    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 已提交
344 345 346 347 348 349 350 351 352 353 354 355 356
        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;
        }
357 358 359 360 361 362 363 364 365 366 367 368 369
    }

    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,
370
                        testStorageChainFormat, i,
371
                        NULLSTR(data->files[i]->path),
372 373
                        NULLSTR(data->files[i]->expBackingStoreRaw),
                        data->files[i]->expCapacity,
374
                        data->files[i]->expEncrypted,
375
                        NULLSTR(data->files[i]->pathRel),
E
Eric Blake 已提交
376
                        data->files[i]->type,
377 378 379 380
                        data->files[i]->format,
                        virStorageNetProtocolTypeToString(data->files[i]->protocol),
                        NULLSTR(data->files[i]->hostname),
                        NULLSTR(data->files[i]->secret)) < 0 ||
381
            virAsprintf(&actual,
382
                        testStorageChainFormat, i,
383
                        NULLSTR(elt->path),
384
                        NULLSTR(elt->backingStoreRaw),
385 386
                        elt->capacity,
                        !!elt->encryption,
387
                        NULLSTR(elt->relPath),
388
                        elt->type,
389 390 391 392
                        elt->format,
                        virStorageNetProtocolTypeToString(elt->protocol),
                        NULLSTR(elt->nhosts ? elt->hosts[0].name : NULL),
                        NULLSTR(elt->auth ? elt->auth->username : NULL)) < 0) {
393 394 395 396 397
            VIR_FREE(expect);
            VIR_FREE(actual);
            goto cleanup;
        }
        if (STRNEQ(expect, actual)) {
398
            virTestDifference(stderr, expect, actual);
399 400 401 402 403 404
            VIR_FREE(expect);
            VIR_FREE(actual);
            goto cleanup;
        }
        VIR_FREE(expect);
        VIR_FREE(actual);
405
        elt = elt->backingStore;
406 407 408 409 410 411 412 413
        i++;
    }
    if (i != data->nfiles) {
        fprintf(stderr, "probed chain was too short\n");
        goto cleanup;
    }

    ret = 0;
414
 cleanup:
E
Eric Blake 已提交
415
    VIR_FREE(broken);
416
    virStorageSourceFree(meta);
417 418 419
    return ret;
}

E
Eric Blake 已提交
420 421
struct testLookupData
{
422
    virStorageSourcePtr chain;
423
    const char *target;
E
Eric Blake 已提交
424
    virStorageSourcePtr from;
E
Eric Blake 已提交
425
    const char *name;
426
    unsigned int expIndex;
E
Eric Blake 已提交
427
    const char *expResult;
428
    virStorageSourcePtr expMeta;
429
    virStorageSourcePtr expParent;
E
Eric Blake 已提交
430 431 432 433 434 435 436
};

static int
testStorageLookup(const void *args)
{
    const struct testLookupData *data = args;
    int ret = 0;
437
    virStorageSourcePtr result;
438
    virStorageSourcePtr actualParent;
439 440 441 442 443 444 445 446 447 448 449
    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 已提交
450

451
     /* Test twice to ensure optional parameter doesn't cause NULL deref. */
E
Eric Blake 已提交
452
    result = virStorageFileChainLookup(data->chain, data->from,
453 454
                                       idx ? NULL : data->name,
                                       idx, NULL);
E
Eric Blake 已提交
455 456 457 458 459 460 461 462 463 464 465 466 467 468

    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;
        }
    }

469 470 471 472 473 474 475
    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 已提交
476
        fprintf(stderr, "result 1: expected %s, got %s\n",
477
                NULLSTR(data->expResult), NULLSTR(result->path));
E
Eric Blake 已提交
478 479 480
        ret = -1;
    }

E
Eric Blake 已提交
481
    result = virStorageFileChainLookup(data->chain, data->from,
482
                                       data->name, idx, &actualParent);
E
Eric Blake 已提交
483 484
    if (!data->expResult)
        virResetLastError();
485 486 487 488 489 490 491 492

    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 已提交
493
        fprintf(stderr, "result 2: expected %s, got %s\n",
494
                NULLSTR(data->expResult), NULLSTR(result->path));
E
Eric Blake 已提交
495 496
        ret = -1;
    }
497
    if (data->expMeta != result) {
E
Eric Blake 已提交
498
        fprintf(stderr, "meta: expected %p, got %p\n",
499
                data->expMeta, result);
E
Eric Blake 已提交
500 501
        ret = -1;
    }
502
    if (data->expParent != actualParent) {
E
Eric Blake 已提交
503
        fprintf(stderr, "parent: expected %s, got %s\n",
504 505
                NULLSTR(data->expParent ? data->expParent->path : NULL),
                NULLSTR(actualParent ? actualParent->path : NULL));
E
Eric Blake 已提交
506 507 508 509 510 511
        ret = -1;
    }

    return ret;
}

512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528

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,
529
                             char **linkpath,
530 531 532 533
                             void *data ATTRIBUTE_UNUSED)
{
    size_t i;

534
    *linkpath = NULL;
535 536 537

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

576 577 578 579 580 581 582 583 584 585 586 587 588
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;

589
        backingchain[i].relPath = NULL;
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 665 666 667
    }

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

668

669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701
struct testBackingParseData {
    const char *backing;
    const char *expect;
};

static int
testBackingParse(const void *args)
{
    const struct testBackingParseData *data = args;
    virBuffer buf = VIR_BUFFER_INITIALIZER;
    virStorageSourcePtr src = NULL;
    char *xml = NULL;
    int ret = -1;

    if (!(src = virStorageSourceNewFromBackingAbsolute(data->backing))) {
        if (!data->expect)
            ret = 0;

        goto cleanup;
    }

    if (src && !data->expect) {
        fprintf(stderr, "parsing of backing store string '%s' should "
                        "have failed\n", data->backing);
        goto cleanup;
    }

    if (virDomainDiskSourceFormat(&buf, src, 0, 0) < 0 ||
        !(xml = virBufferContentAndReset(&buf))) {
        fprintf(stderr, "failed to format disk source xml\n");
        goto cleanup;
    }

702
    if (STRNEQ(xml, data->expect)) {
703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720
        fprintf(stderr, "\n backing store string '%s'\n"
                        "expected storage source xml:\n%s\n"
                        "actual storage source xml:\n%s\n",
                        data->backing, data->expect, xml);
        goto cleanup;
    }

    ret = 0;

 cleanup:
    virStorageSourceFree(src);
    virBufferFreeAndReset(&buf);
    VIR_FREE(xml);

    return ret;
}


721 722 723 724 725
static int
mymain(void)
{
    int ret;
    virCommandPtr cmd = NULL;
726
    struct testChainData data;
E
Eric Blake 已提交
727
    struct testLookupData data2;
728
    struct testPathCanonicalizeData data3;
729
    struct testPathRelativeBacking data4;
730
    struct testBackingParseData data5;
731
    virStorageSourcePtr chain = NULL;
E
Eric Blake 已提交
732 733
    virStorageSourcePtr chain2; /* short for chain->backingStore */
    virStorageSourcePtr chain3; /* short for chain2->backingStore */
734

735 736
    if (storageRegisterAll() < 0)
       return EXIT_FAILURE;
737

738 739 740 741 742
    /* 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;

743
#define TEST_ONE_CHAIN(start, format, flags, ...)                    \
744
    do {                                                             \
745 746 747 748
        size_t i;                                                    \
        memset(&data, 0, sizeof(data));                              \
        data = (struct testChainData){                               \
            start, format, { __VA_ARGS__ }, 0, flags,                \
749
        };                                                           \
750 751 752
        for (i = 0; i < ARRAY_CARDINALITY(data.files); i++)          \
            if (data.files[i])                                       \
                data.nfiles++;                                       \
753
        if (virTestRun(virTestCounterNext(),                         \
754
                       testStorageChain, &data) < 0)                 \
755 756 757
            ret = -1;                                                \
    } while (0)

758 759 760
#define VIR_FLATTEN_2(...) __VA_ARGS__
#define VIR_FLATTEN_1(_1) VIR_FLATTEN_2 _1

761 762 763 764
#define TEST_CHAIN(path, format, chain1, flags1, chain2, flags2)      \
    do {                                                              \
        TEST_ONE_CHAIN(path, format, flags1, VIR_FLATTEN_1(chain1));  \
        TEST_ONE_CHAIN(path, format, flags2, VIR_FLATTEN_1(chain2));  \
765 766 767
    } while (0)

    /* The actual tests, in several groups. */
768
    virTestCounterReset("Storage backing chain ");
769 770

    /* Missing file */
771
    TEST_ONE_CHAIN("bogus", VIR_STORAGE_FILE_RAW, EXP_FAIL);
772 773

    /* Raw image, whether with right format or no specified format */
774
    testFileData raw = {
775
        .path = canonraw,
E
Eric Blake 已提交
776 777
        .type = VIR_STORAGE_TYPE_FILE,
        .format = VIR_STORAGE_FILE_RAW,
778
    };
779
    TEST_CHAIN(absraw, VIR_STORAGE_FILE_RAW,
780 781
               (&raw), EXP_PASS,
               (&raw), ALLOW_PROBE | EXP_PASS);
782
    TEST_CHAIN(absraw, VIR_STORAGE_FILE_AUTO,
783 784
               (&raw), EXP_PASS,
               (&raw), ALLOW_PROBE | EXP_PASS);
785 786

    /* Qcow2 file with relative raw backing, format provided */
787
    raw.pathRel = "raw";
788
    testFileData qcow2 = {
789 790
        .expBackingStoreRaw = "raw",
        .expCapacity = 1024,
791
        .path = canonqcow2,
E
Eric Blake 已提交
792 793
        .type = VIR_STORAGE_TYPE_FILE,
        .format = VIR_STORAGE_FILE_QCOW2,
794 795
    };
    testFileData qcow2_as_raw = {
796
        .path = canonqcow2,
E
Eric Blake 已提交
797 798
        .type = VIR_STORAGE_TYPE_FILE,
        .format = VIR_STORAGE_FILE_RAW,
799
    };
800
    TEST_CHAIN(absqcow2, VIR_STORAGE_FILE_QCOW2,
801 802
               (&qcow2, &raw), EXP_PASS,
               (&qcow2, &raw), ALLOW_PROBE | EXP_PASS);
803
    TEST_CHAIN(absqcow2, VIR_STORAGE_FILE_AUTO,
804
               (&qcow2_as_raw), EXP_PASS,
805
               (&qcow2, &raw), ALLOW_PROBE | EXP_PASS);
806 807 808 809 810 811 812

    /* 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;
813
    qcow2.expBackingStoreRaw = absraw;
814
    raw.pathRel = NULL;
815 816

    /* Qcow2 file with raw as absolute backing, backing format provided */
817
    TEST_CHAIN(absqcow2, VIR_STORAGE_FILE_QCOW2,
818 819
               (&qcow2, &raw), EXP_PASS,
               (&qcow2, &raw), ALLOW_PROBE | EXP_PASS);
820
    TEST_CHAIN(absqcow2, VIR_STORAGE_FILE_AUTO,
821
               (&qcow2_as_raw), EXP_PASS,
822
               (&qcow2, &raw), ALLOW_PROBE | EXP_PASS);
823 824

    /* Wrapped file access */
825 826 827
    testFileData wrap = {
        .expBackingStoreRaw = absqcow2,
        .expCapacity = 1024,
828
        .path = canonwrap,
E
Eric Blake 已提交
829 830
        .type = VIR_STORAGE_TYPE_FILE,
        .format = VIR_STORAGE_FILE_QCOW2,
831
    };
832
    TEST_CHAIN(abswrap, VIR_STORAGE_FILE_QCOW2,
833 834
               (&wrap, &qcow2, &raw), EXP_PASS,
               (&wrap, &qcow2, &raw), ALLOW_PROBE | EXP_PASS);
835 836 837 838 839 840 841 842 843 844 845 846 847 848 849

    /* 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 */
850 851 852
    testFileData wrap_as_raw = {
        .expBackingStoreRaw = absqcow2,
        .expCapacity = 1024,
853
        .path = canonwrap,
E
Eric Blake 已提交
854 855
        .type = VIR_STORAGE_TYPE_FILE,
        .format = VIR_STORAGE_FILE_QCOW2,
856
    };
857
    TEST_CHAIN(abswrap, VIR_STORAGE_FILE_QCOW2,
858
               (&wrap_as_raw, &qcow2_as_raw), EXP_PASS,
859
               (&wrap, &qcow2, &raw), ALLOW_PROBE | EXP_PASS);
860 861 862 863 864 865 866 867

    /* 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;
868
    qcow2.expBackingStoreRaw = datadir "/bogus";
869 870

    /* Qcow2 file with missing backing file but specified type */
871
    TEST_CHAIN(absqcow2, VIR_STORAGE_FILE_QCOW2,
872 873
               (&qcow2), EXP_WARN,
               (&qcow2), ALLOW_PROBE | EXP_WARN);
874 875 876 877 878 879 880 881 882

    /* 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 */
883
    TEST_CHAIN(absqcow2, VIR_STORAGE_FILE_QCOW2,
884 885
               (&qcow2), EXP_WARN,
               (&qcow2), ALLOW_PROBE | EXP_WARN);
886 887 888 889

    /* Rewrite qcow2 to use an nbd: protocol as backend */
    virCommandFree(cmd);
    cmd = virCommandNewArgList(qemuimg, "rebase", "-u", "-f", "qcow2",
890
                               "-F", "raw", "-b", "nbd:example.org:6000:exportname=blah",
891 892 893
                               "qcow2", NULL);
    if (virCommandRun(cmd, NULL) < 0)
        ret = -1;
894
    qcow2.expBackingStoreRaw = "nbd:example.org:6000:exportname=blah";
895 896

    /* Qcow2 file with backing protocol instead of file */
897
    testFileData nbd = {
898
        .path = "blah",
899 900
        .type = VIR_STORAGE_TYPE_NETWORK,
        .format = VIR_STORAGE_FILE_RAW,
901 902
        .protocol = VIR_STORAGE_NET_PROTOCOL_NBD,
        .hostname = "example.org",
903
    };
904
    TEST_CHAIN(absqcow2, VIR_STORAGE_FILE_QCOW2,
905 906
               (&qcow2, &nbd), EXP_PASS,
               (&qcow2, &nbd), ALLOW_PROBE | EXP_PASS);
907

908 909 910 911 912 913 914 915 916 917 918 919 920 921
    /* Rewrite qcow2 to use an nbd: protocol as backend */
    virCommandFree(cmd);
    cmd = virCommandNewArgList(qemuimg, "rebase", "-u", "-f", "qcow2",
                               "-F", "raw", "-b", "nbd+tcp://example.org:6000/blah",
                               "qcow2", NULL);
    if (virCommandRun(cmd, NULL) < 0)
        ret = -1;
    qcow2.expBackingStoreRaw = "nbd+tcp://example.org:6000/blah";

    /* Qcow2 file with backing protocol instead of file */
    testFileData nbd2 = {
        .path = "blah",
        .type = VIR_STORAGE_TYPE_NETWORK,
        .format = VIR_STORAGE_FILE_RAW,
922 923
        .protocol = VIR_STORAGE_NET_PROTOCOL_NBD,
        .hostname = "example.org",
924
    };
925
    TEST_CHAIN(absqcow2, VIR_STORAGE_FILE_QCOW2,
926 927 928
               (&qcow2, &nbd2), EXP_PASS,
               (&qcow2, &nbd2), ALLOW_PROBE | EXP_PASS);

929 930 931 932 933 934 935 936 937 938 939 940 941 942
    /* Rewrite qcow2 to use an nbd: protocol without path as backend */
    virCommandFree(cmd);
    cmd = virCommandNewArgList(qemuimg, "rebase", "-u", "-f", "qcow2",
                               "-F", "raw", "-b", "nbd://example.org",
                               "qcow2", NULL);
    if (virCommandRun(cmd, NULL) < 0)
        ret = -1;
    qcow2.expBackingStoreRaw = "nbd://example.org";

    nbd2.path = NULL;
    TEST_CHAIN(absqcow2, VIR_STORAGE_FILE_QCOW2,
               (&qcow2, &nbd2), EXP_PASS,
               (&qcow2, &nbd2), ALLOW_PROBE | EXP_PASS);

943
    /* qed file */
944 945 946
    testFileData qed = {
        .expBackingStoreRaw = absraw,
        .expCapacity = 1024,
947
        .path = canonqed,
E
Eric Blake 已提交
948 949
        .type = VIR_STORAGE_TYPE_FILE,
        .format = VIR_STORAGE_FILE_QED,
950 951
    };
    testFileData qed_as_raw = {
952
        .path = canonqed,
E
Eric Blake 已提交
953 954
        .type = VIR_STORAGE_TYPE_FILE,
        .format = VIR_STORAGE_FILE_RAW,
955
    };
956
    TEST_CHAIN(absqed, VIR_STORAGE_FILE_AUTO,
957
               (&qed_as_raw), EXP_PASS,
958
               (&qed, &raw), ALLOW_PROBE | EXP_PASS);
959

E
Eric Blake 已提交
960
    /* directory */
961
    testFileData dir = {
962
        .path = canondir,
E
Eric Blake 已提交
963 964
        .type = VIR_STORAGE_TYPE_DIR,
        .format = VIR_STORAGE_FILE_DIR,
965
    };
966
    TEST_CHAIN(absdir, VIR_STORAGE_FILE_AUTO,
E
Eric Blake 已提交
967 968
               (&dir), EXP_PASS,
               (&dir), ALLOW_PROBE | EXP_PASS);
969
    TEST_CHAIN(absdir, VIR_STORAGE_FILE_DIR,
E
Eric Blake 已提交
970 971 972
               (&dir), EXP_PASS,
               (&dir), ALLOW_PROBE | EXP_PASS);

973
#ifdef HAVE_SYMLINK
974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989
    /* 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 */
990
    testFileData link1 = {
991 992
        .expBackingStoreRaw = "../raw",
        .expCapacity = 1024,
993
        .pathRel = "../sub/link1",
994
        .path = datadir "/sub/../sub/link1",
E
Eric Blake 已提交
995 996
        .type = VIR_STORAGE_TYPE_FILE,
        .format = VIR_STORAGE_FILE_QCOW2,
997
    };
998
    testFileData link2 = {
999 1000
        .expBackingStoreRaw = "../sub/link1",
        .expCapacity = 1024,
1001
        .path = abslink2,
E
Eric Blake 已提交
1002 1003
        .type = VIR_STORAGE_TYPE_FILE,
        .format = VIR_STORAGE_FILE_QCOW2,
1004
    };
1005 1006

    raw.path = datadir "/sub/../sub/../raw";
1007
    raw.pathRel = "../raw";
1008
    TEST_CHAIN(abslink2, VIR_STORAGE_FILE_QCOW2,
1009 1010
               (&link2, &link1, &raw), EXP_PASS,
               (&link2, &link1, &raw), ALLOW_PROBE | EXP_PASS);
1011
#endif
E
Eric Blake 已提交
1012 1013 1014 1015 1016 1017 1018

    /* 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;
1019
    qcow2.expBackingStoreRaw = "qcow2";
E
Eric Blake 已提交
1020 1021

    /* Behavior of an infinite loop chain */
1022
    TEST_CHAIN(absqcow2, VIR_STORAGE_FILE_QCOW2,
1023 1024
               (&qcow2), EXP_WARN,
               (&qcow2), ALLOW_PROBE | EXP_WARN);
E
Eric Blake 已提交
1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037

    /* 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;
1038
    qcow2.expBackingStoreRaw = "wrap";
E
Eric Blake 已提交
1039 1040

    /* Behavior of an infinite loop chain */
1041
    TEST_CHAIN(abswrap, VIR_STORAGE_FILE_QCOW2,
1042 1043
               (&wrap, &qcow2), EXP_WARN,
               (&wrap, &qcow2), ALLOW_PROBE | EXP_WARN);
E
Eric Blake 已提交
1044

1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060
    /* Rewrite qcow2 to use an rbd: protocol as backend */
    virCommandFree(cmd);
    cmd = virCommandNewArgList(qemuimg, "rebase", "-u", "-f", "qcow2",
                               "-F", "raw", "-b", "rbd:testshare",
                               "qcow2", NULL);
    if (virCommandRun(cmd, NULL) < 0)
        ret = -1;
    qcow2.expBackingStoreRaw = "rbd:testshare";

    /* Qcow2 file with backing protocol instead of file */
    testFileData rbd1 = {
        .path = "testshare",
        .type = VIR_STORAGE_TYPE_NETWORK,
        .format = VIR_STORAGE_FILE_RAW,
        .protocol = VIR_STORAGE_NET_PROTOCOL_RBD,
    };
1061
    TEST_CHAIN(absqcow2, VIR_STORAGE_FILE_QCOW2,
1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082
               (&qcow2, &rbd1), EXP_PASS,
               (&qcow2, &rbd1), ALLOW_PROBE | EXP_PASS);

    /* Rewrite qcow2 to use an rbd: protocol as backend */
    virCommandFree(cmd);
    cmd = virCommandNewArgList(qemuimg, "rebase", "-u", "-f", "qcow2",
                               "-F", "raw", "-b", "rbd:testshare:id=asdf:mon_host=example.com",
                               "qcow2", NULL);
    if (virCommandRun(cmd, NULL) < 0)
        ret = -1;
    qcow2.expBackingStoreRaw = "rbd:testshare:id=asdf:mon_host=example.com";

    /* Qcow2 file with backing protocol instead of file */
    testFileData rbd2 = {
        .path = "testshare",
        .type = VIR_STORAGE_TYPE_NETWORK,
        .format = VIR_STORAGE_FILE_RAW,
        .protocol = VIR_STORAGE_NET_PROTOCOL_RBD,
        .secret = "asdf",
        .hostname = "example.com",
    };
1083
    TEST_CHAIN(absqcow2, VIR_STORAGE_FILE_QCOW2,
1084 1085 1086 1087
               (&qcow2, &rbd2), EXP_PASS,
               (&qcow2, &rbd2), ALLOW_PROBE | EXP_PASS);


E
Eric Blake 已提交
1088 1089 1090 1091 1092 1093 1094
    /* 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;

1095
    /* Test behavior of chain lookups, absolute backing from relative start */
1096 1097
    chain = testStorageFileGetMetadata("wrap", VIR_STORAGE_FILE_QCOW2,
                                       -1, -1, false);
E
Eric Blake 已提交
1098 1099 1100 1101
    if (!chain) {
        ret = -1;
        goto cleanup;
    }
E
Eric Blake 已提交
1102 1103
    chain2 = chain->backingStore;
    chain3 = chain2->backingStore;
E
Eric Blake 已提交
1104

E
Eric Blake 已提交
1105
#define TEST_LOOKUP_TARGET(id, target, from, name, index, result,       \
E
Eric Blake 已提交
1106
                           meta, parent)                                \
E
Eric Blake 已提交
1107 1108 1109 1110
    do {                                                                \
        data2 = (struct testLookupData){                                \
            chain, target, from, name, index,                           \
            result, meta, parent, };                                    \
1111 1112
        if (virTestRun("Chain lookup " #id,                             \
                       testStorageLookup, &data2) < 0)                  \
E
Eric Blake 已提交
1113
            ret = -1;                                                   \
E
Eric Blake 已提交
1114
    } while (0)
E
Eric Blake 已提交
1115 1116 1117 1118
#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 已提交
1119
    TEST_LOOKUP(1, chain, "bogus", NULL, NULL, NULL);
E
Eric Blake 已提交
1120
    TEST_LOOKUP(2, NULL, "wrap", chain->path, chain, NULL);
E
Eric Blake 已提交
1121
    TEST_LOOKUP(3, chain, "wrap", NULL, NULL, NULL);
E
Eric Blake 已提交
1122
    TEST_LOOKUP(4, chain2, "wrap", NULL, NULL, NULL);
E
Eric Blake 已提交
1123
    TEST_LOOKUP(5, NULL, abswrap, chain->path, chain, NULL);
E
Eric Blake 已提交
1124
    TEST_LOOKUP(6, chain, abswrap, NULL, NULL, NULL);
E
Eric Blake 已提交
1125
    TEST_LOOKUP(7, chain2, abswrap, NULL, NULL, NULL);
1126 1127
    TEST_LOOKUP(8, NULL, "qcow2", chain2->path, chain2, chain);
    TEST_LOOKUP(9, chain, "qcow2", chain2->path, chain2, chain);
E
Eric Blake 已提交
1128
    TEST_LOOKUP(10, chain2, "qcow2", NULL, NULL, NULL);
E
Eric Blake 已提交
1129
    TEST_LOOKUP(11, chain3, "qcow2", NULL, NULL, NULL);
1130 1131
    TEST_LOOKUP(12, NULL, absqcow2, chain2->path, chain2, chain);
    TEST_LOOKUP(13, chain, absqcow2, chain2->path, chain2, chain);
E
Eric Blake 已提交
1132
    TEST_LOOKUP(14, chain2, absqcow2, NULL, NULL, NULL);
E
Eric Blake 已提交
1133
    TEST_LOOKUP(15, chain3, absqcow2, NULL, NULL, NULL);
1134 1135 1136
    TEST_LOOKUP(16, NULL, "raw", chain3->path, chain3, chain2);
    TEST_LOOKUP(17, chain, "raw", chain3->path, chain3, chain2);
    TEST_LOOKUP(18, chain2, "raw", chain3->path, chain3, chain2);
E
Eric Blake 已提交
1137
    TEST_LOOKUP(19, chain3, "raw", NULL, NULL, NULL);
1138 1139 1140
    TEST_LOOKUP(20, NULL, absraw, chain3->path, chain3, chain2);
    TEST_LOOKUP(21, chain, absraw, chain3->path, chain3, chain2);
    TEST_LOOKUP(22, chain2, absraw, chain3->path, chain3, chain2);
E
Eric Blake 已提交
1141
    TEST_LOOKUP(23, chain3, absraw, NULL, NULL, NULL);
1142 1143 1144
    TEST_LOOKUP(24, NULL, NULL, chain3->path, chain3, chain2);
    TEST_LOOKUP(25, chain, NULL, chain3->path, chain3, chain2);
    TEST_LOOKUP(26, chain2, NULL, chain3->path, chain3, chain2);
E
Eric Blake 已提交
1145
    TEST_LOOKUP(27, chain3, NULL, NULL, NULL, NULL);
E
Eric Blake 已提交
1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159

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

1160
    /* Test behavior of chain lookups, relative backing from absolute start */
1161
    virStorageSourceFree(chain);
1162 1163
    chain = testStorageFileGetMetadata(abswrap, VIR_STORAGE_FILE_QCOW2,
                                       -1, -1, false);
E
Eric Blake 已提交
1164 1165 1166 1167
    if (!chain) {
        ret = -1;
        goto cleanup;
    }
E
Eric Blake 已提交
1168 1169
    chain2 = chain->backingStore;
    chain3 = chain2->backingStore;
E
Eric Blake 已提交
1170

E
Eric Blake 已提交
1171
    TEST_LOOKUP(28, NULL, "bogus", NULL, NULL, NULL);
E
Eric Blake 已提交
1172
    TEST_LOOKUP(29, chain, "bogus", NULL, NULL, NULL);
E
Eric Blake 已提交
1173
    TEST_LOOKUP(30, NULL, "wrap", chain->path, chain, NULL);
E
Eric Blake 已提交
1174
    TEST_LOOKUP(31, chain, "wrap", NULL, NULL, NULL);
E
Eric Blake 已提交
1175
    TEST_LOOKUP(32, chain2, "wrap", NULL, NULL, NULL);
E
Eric Blake 已提交
1176
    TEST_LOOKUP(33, NULL, abswrap, chain->path, chain, NULL);
E
Eric Blake 已提交
1177
    TEST_LOOKUP(34, chain, abswrap, NULL, NULL, NULL);
E
Eric Blake 已提交
1178
    TEST_LOOKUP(35, chain2, abswrap, NULL, NULL, NULL);
1179 1180
    TEST_LOOKUP(36, NULL, "qcow2", chain2->path, chain2, chain);
    TEST_LOOKUP(37, chain, "qcow2", chain2->path, chain2, chain);
E
Eric Blake 已提交
1181
    TEST_LOOKUP(38, chain2, "qcow2", NULL, NULL, NULL);
E
Eric Blake 已提交
1182
    TEST_LOOKUP(39, chain3, "qcow2", NULL, NULL, NULL);
1183 1184
    TEST_LOOKUP(40, NULL, absqcow2, chain2->path, chain2, chain);
    TEST_LOOKUP(41, chain, absqcow2, chain2->path, chain2, chain);
E
Eric Blake 已提交
1185
    TEST_LOOKUP(42, chain2, absqcow2, NULL, NULL, NULL);
E
Eric Blake 已提交
1186
    TEST_LOOKUP(43, chain3, absqcow2, NULL, NULL, NULL);
1187 1188 1189
    TEST_LOOKUP(44, NULL, "raw", chain3->path, chain3, chain2);
    TEST_LOOKUP(45, chain, "raw", chain3->path, chain3, chain2);
    TEST_LOOKUP(46, chain2, "raw", chain3->path, chain3, chain2);
E
Eric Blake 已提交
1190
    TEST_LOOKUP(47, chain3, "raw", NULL, NULL, NULL);
1191 1192 1193
    TEST_LOOKUP(48, NULL, absraw, chain3->path, chain3, chain2);
    TEST_LOOKUP(49, chain, absraw, chain3->path, chain3, chain2);
    TEST_LOOKUP(50, chain2, absraw, chain3->path, chain3, chain2);
E
Eric Blake 已提交
1194
    TEST_LOOKUP(51, chain3, absraw, NULL, NULL, NULL);
1195 1196 1197
    TEST_LOOKUP(52, NULL, NULL, chain3->path, chain3, chain2);
    TEST_LOOKUP(53, chain, NULL, chain3->path, chain3, chain2);
    TEST_LOOKUP(54, chain2, NULL, chain3->path, chain3, chain2);
E
Eric Blake 已提交
1198
    TEST_LOOKUP(55, chain3, NULL, NULL, NULL, NULL);
E
Eric Blake 已提交
1199 1200 1201 1202 1203 1204 1205 1206 1207

    /* 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 */
1208
    virStorageSourceFree(chain);
1209 1210
    chain = testStorageFileGetMetadata("sub/link2", VIR_STORAGE_FILE_QCOW2,
                                       -1, -1, false);
E
Eric Blake 已提交
1211 1212 1213 1214
    if (!chain) {
        ret = -1;
        goto cleanup;
    }
E
Eric Blake 已提交
1215 1216
    chain2 = chain->backingStore;
    chain3 = chain2->backingStore;
E
Eric Blake 已提交
1217

E
Eric Blake 已提交
1218 1219 1220 1221
    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);
1222
    TEST_LOOKUP(60, NULL, "../qcow2", chain2->path, chain2, chain);
E
Eric Blake 已提交
1223
    TEST_LOOKUP(61, NULL, "qcow2", NULL, NULL, NULL);
1224 1225 1226 1227
    TEST_LOOKUP(62, NULL, absqcow2, chain2->path, chain2, chain);
    TEST_LOOKUP(63, NULL, "raw", chain3->path, chain3, chain2);
    TEST_LOOKUP(64, NULL, absraw, chain3->path, chain3, chain2);
    TEST_LOOKUP(65, NULL, NULL, chain3->path, chain3, chain2);
E
Eric Blake 已提交
1228 1229 1230 1231 1232

    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 已提交
1233
    TEST_LOOKUP_TARGET(70, "vda", chain, "wrap", 0, NULL, NULL, NULL);
E
Eric Blake 已提交
1234
    TEST_LOOKUP_TARGET(71, "vda", chain2, "wrap", 0, NULL, NULL, NULL);
E
Eric Blake 已提交
1235
    TEST_LOOKUP_TARGET(72, "vda", NULL, "vda[0]", 0, NULL, NULL, NULL);
1236 1237
    TEST_LOOKUP_TARGET(73, "vda", NULL, "vda[1]", 1, chain2->path, chain2, chain);
    TEST_LOOKUP_TARGET(74, "vda", chain, "vda[1]", 1, chain2->path, chain2, chain);
E
Eric Blake 已提交
1238
    TEST_LOOKUP_TARGET(75, "vda", chain2, "vda[1]", 1, NULL, NULL, NULL);
E
Eric Blake 已提交
1239
    TEST_LOOKUP_TARGET(76, "vda", chain3, "vda[1]", 1, NULL, NULL, NULL);
1240 1241 1242
    TEST_LOOKUP_TARGET(77, "vda", NULL, "vda[2]", 2, chain3->path, chain3, chain2);
    TEST_LOOKUP_TARGET(78, "vda", chain, "vda[2]", 2, chain3->path, chain3, chain2);
    TEST_LOOKUP_TARGET(79, "vda", chain2, "vda[2]", 2, chain3->path, chain3, chain2);
E
Eric Blake 已提交
1243
    TEST_LOOKUP_TARGET(80, "vda", chain3, "vda[2]", 2, NULL, NULL, NULL);
E
Eric Blake 已提交
1244
    TEST_LOOKUP_TARGET(81, "vda", NULL, "vda[3]", 3, NULL, NULL, NULL);
1245

1246 1247 1248 1249
#define TEST_PATH_CANONICALIZE(id, PATH, EXPECT)                            \
    do {                                                                    \
        data3.path = PATH;                                                  \
        data3.expect = EXPECT;                                              \
1250 1251
        if (virTestRun("Path canonicalize " #id,                            \
                       testPathCanonicalize, &data3) < 0)                   \
1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288
            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, "///", "/");

1289 1290 1291 1292 1293
#define TEST_RELATIVE_BACKING(id, TOP, BASE, EXPECT)                        \
    do {                                                                    \
        data4.top = &TOP;                                                   \
        data4.base = &BASE;                                                 \
        data4.expect = EXPECT;                                              \
1294 1295
        if (virTestRun("Path relative resolve " #id,                        \
                       testPathRelative, &data4) < 0)                       \
1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335
            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");

1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361

    virTestCounterReset("Backing store parse ");

#define TEST_BACKING_PARSE(bck, xml)                                           \
    do {                                                                       \
        data5.backing = bck;                                                   \
        data5.expect = xml;                                                    \
        if (virTestRun(virTestCounterNext(),                                   \
                       testBackingParse, &data5) < 0)                          \
            ret = -1;                                                          \
    } while (0)

    TEST_BACKING_PARSE("path", "<source file='path'/>\n");
    TEST_BACKING_PARSE("://", NULL);
    TEST_BACKING_PARSE("http://example.com/file",
                       "<source protocol='http' name='file'>\n"
                       "  <host name='example.com'/>\n"
                       "</source>\n");
    TEST_BACKING_PARSE("rbd:testshare:id=asdf:mon_host=example.com",
                       "<source protocol='rbd' name='testshare'>\n"
                       "  <host name='example.com'/>\n"
                       "</source>\n");
    TEST_BACKING_PARSE("nbd:example.org:6000:exportname=blah",
                       "<source protocol='nbd' name='blah'>\n"
                       "  <host name='example.org' port='6000'/>\n"
                       "</source>\n");
1362 1363

#ifdef WITH_YAJL
1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378
    TEST_BACKING_PARSE("json:", NULL);
    TEST_BACKING_PARSE("json:asdgsdfg", NULL);
    TEST_BACKING_PARSE("json:{}", NULL);
    TEST_BACKING_PARSE("json: { \"file.driver\":\"blah\"}", NULL);
    TEST_BACKING_PARSE("json:{\"file.driver\":\"file\"}", NULL);
    TEST_BACKING_PARSE("json:{\"file.driver\":\"file\", "
                             "\"file.filename\":\"/path/to/file\"}",
                       "<source file='/path/to/file'/>\n");
    TEST_BACKING_PARSE("json:{\"file.driver\":\"file\", "
                             "\"filename\":\"/path/to/file\"}", NULL);
    TEST_BACKING_PARSE("json:{\"file\" : { \"driver\":\"file\","
                                          "\"filename\":\"/path/to/file\""
                                        "}"
                            "}",
                       "<source file='/path/to/file'/>\n");
1379 1380 1381 1382 1383 1384
    TEST_BACKING_PARSE("json:{\"file.driver\":\"host_device\", "
                             "\"file.filename\":\"/path/to/dev\"}",
                       "<source dev='/path/to/dev'/>\n");
    TEST_BACKING_PARSE("json:{\"file.driver\":\"host_cdrom\", "
                             "\"file.filename\":\"/path/to/cdrom\"}",
                       "<source dev='/path/to/cdrom'/>\n");
1385
    TEST_BACKING_PARSE("json:{\"file.driver\":\"http\", "
1386
                             "\"file.url\":\"http://example.com/file\"}",
1387 1388 1389 1390
                       "<source protocol='http' name='file'>\n"
                       "  <host name='example.com'/>\n"
                       "</source>\n");
    TEST_BACKING_PARSE("json:{\"file\":{ \"driver\":\"http\","
1391
                                        "\"url\":\"http://example.com/file\""
1392 1393 1394 1395 1396 1397
                                      "}"
                            "}",
                       "<source protocol='http' name='file'>\n"
                       "  <host name='example.com'/>\n"
                       "</source>\n");
    TEST_BACKING_PARSE("json:{\"file.driver\":\"ftp\", "
1398
                             "\"file.url\":\"http://example.com/file\"}",
1399
                       NULL);
1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420
    TEST_BACKING_PARSE("json:{\"file.driver\":\"gluster\", "
                             "\"file.filename\":\"gluster://example.com/vol/file\"}",
                       "<source protocol='gluster' name='vol/file'>\n"
                       "  <host name='example.com'/>\n"
                       "</source>\n");
    TEST_BACKING_PARSE("json:{\"file\":{\"driver\":\"gluster\","
                                       "\"volume\":\"testvol\","
                                       "\"path\":\"img.qcow2\","
                                       "\"server\":[ { \"type\":\"tcp\","
                                                      "\"host\":\"example.com\","
                                                      "\"port\":\"1234\""
                                                    "},"
                                                    "{ \"type\":\"unix\","
                                                      "\"socket\":\"/path/socket\""
                                                    "},"
                                                    "{ \"type\":\"tcp\","
                                                      "\"host\":\"example.com\""
                                                    "}"
                                                  "]"
                                      "}"
                             "}",
1421
                        "<source protocol='gluster' name='testvol/img.qcow2'>\n"
1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435
                        "  <host name='example.com' port='1234'/>\n"
                        "  <host transport='unix' socket='/path/socket'/>\n"
                        "  <host name='example.com'/>\n"
                        "</source>\n");
    TEST_BACKING_PARSE("json:{\"file.driver\":\"gluster\","
                             "\"file.volume\":\"testvol\","
                             "\"file.path\":\"img.qcow2\","
                             "\"file.server\":[ { \"type\":\"tcp\","
                                                 "\"host\":\"example.com\","
                                                 "\"port\":\"1234\""
                                               "},"
                                               "{ \"type\":\"unix\","
                                                 "\"socket\":\"/path/socket\""
                                               "},"
1436
                                               "{ \"type\":\"inet\","
1437 1438 1439 1440
                                                 "\"host\":\"example.com\""
                                               "}"
                                             "]"
                            "}",
1441
                        "<source protocol='gluster' name='testvol/img.qcow2'>\n"
1442 1443 1444 1445
                        "  <host name='example.com' port='1234'/>\n"
                        "  <host transport='unix' socket='/path/socket'/>\n"
                        "  <host name='example.com'/>\n"
                        "</source>\n");
1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475
    TEST_BACKING_PARSE("json:{\"file\":{\"driver\":\"nbd\","
                                       "\"path\":\"/path/to/socket\""
                                      "}"
                            "}",
                       "<source protocol='nbd'>\n"
                       "  <host transport='unix' socket='/path/to/socket'/>\n"
                       "</source>\n");
    TEST_BACKING_PARSE("json:{\"file.driver\":\"nbd\","
                             "\"file.path\":\"/path/to/socket\""
                            "}",
                       "<source protocol='nbd'>\n"
                       "  <host transport='unix' socket='/path/to/socket'/>\n"
                       "</source>\n");
    TEST_BACKING_PARSE("json:{\"file\":{\"driver\":\"nbd\","
                                       "\"export\":\"blah\","
                                       "\"host\":\"example.org\","
                                       "\"port\":\"6000\""
                                      "}"
                            "}",
                       "<source protocol='nbd' name='blah'>\n"
                       "  <host name='example.org' port='6000'/>\n"
                       "</source>\n");
    TEST_BACKING_PARSE("json:{\"file.driver\":\"nbd\","
                             "\"file.export\":\"blah\","
                             "\"file.host\":\"example.org\","
                             "\"file.port\":\"6000\""
                            "}",
                       "<source protocol='nbd' name='blah'>\n"
                       "  <host name='example.org' port='6000'/>\n"
                       "</source>\n");
1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486
    TEST_BACKING_PARSE("json:{\"file\":{\"driver\":\"nbd\","
                                       "\"export\":\"blah\","
                                       "\"server\": { \"type\":\"inet\","
                                                     "\"host\":\"example.org\","
                                                     "\"port\":\"6000\""
                                                   "}"
                                      "}"
                            "}",
                       "<source protocol='nbd' name='blah'>\n"
                       "  <host name='example.org' port='6000'/>\n"
                       "</source>\n");
1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505
    TEST_BACKING_PARSE("json:{\"file\":{\"driver\":\"ssh\","
                                       "\"host\":\"example.org\","
                                       "\"port\":\"6000\","
                                       "\"path\":\"blah\","
                                       "\"user\":\"user\""
                                      "}"
                            "}",
                       "<source protocol='ssh' name='blah'>\n"
                       "  <host name='example.org' port='6000'/>\n"
                       "</source>\n");
    TEST_BACKING_PARSE("json:{\"file.driver\":\"ssh\","
                             "\"file.host\":\"example.org\","
                             "\"file.port\":\"6000\","
                             "\"file.path\":\"blah\","
                             "\"file.user\":\"user\""
                            "}",
                       "<source protocol='ssh' name='blah'>\n"
                       "  <host name='example.org' port='6000'/>\n"
                       "</source>\n");
1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516
    TEST_BACKING_PARSE("json:{\"file\":{\"driver\":\"ssh\","
                                       "\"path\":\"blah\","
                                       "\"server\":{ \"host\":\"example.org\","
                                                    "\"port\":\"6000\""
                                                  "},"
                                       "\"user\":\"user\""
                                      "}"
                            "}",
                       "<source protocol='ssh' name='blah'>\n"
                       "  <host name='example.org' port='6000'/>\n"
                       "</source>\n");
1517 1518 1519 1520 1521 1522
    TEST_BACKING_PARSE("json:{\"file.driver\":\"rbd\","
                             "\"file.filename\":\"rbd:testshare:id=asdf:mon_host=example.com\""
                            "}",
                       "<source protocol='rbd' name='testshare'>\n"
                       "  <host name='example.com'/>\n"
                       "</source>\n");
1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541
    TEST_BACKING_PARSE("json:{\"file\":{\"driver\":\"rbd\","
                                       "\"image\":\"test\","
                                       "\"pool\":\"libvirt\","
                                       "\"conf\":\"/path/to/conf\","
                                       "\"snapshot\":\"snapshotname\","
                                       "\"server\":[ {\"host\":\"example.com\","
                                                      "\"port\":\"1234\""
                                                    "},"
                                                    "{\"host\":\"example2.com\""
                                                    "}"
                                                  "]"
                                      "}"
                             "}",
                        "<source protocol='rbd' name='libvirt/test'>\n"
                        "  <host name='example.com' port='1234'/>\n"
                        "  <host name='example2.com'/>\n"
                        "  <snapshot name='snapshotname'/>\n"
                        "  <config file='/path/to/conf'/>\n"
                        "</source>\n");
1542 1543 1544 1545 1546 1547
    TEST_BACKING_PARSE("json:{ \"file\": { "
                                "\"driver\": \"raw\","
                                "\"file\": {"
                                    "\"driver\": \"file\","
                                    "\"filename\": \"/path/to/file\" } } }",
                       "<source file='/path/to/file'/>\n");
1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566
    TEST_BACKING_PARSE("json:{\"file\":{\"driver\":\"iscsi\","
                                       "\"transport\":\"tcp\","
                                       "\"portal\":\"test.org\","
                                       "\"target\":\"iqn.2016-12.com.virttest:emulated-iscsi-noauth.target\""
                                      "}"
                            "}",
                       "<source protocol='iscsi' name='iqn.2016-12.com.virttest:emulated-iscsi-noauth.target/0'>\n"
                       "  <host name='test.org'/>\n"
                       "</source>\n");
    TEST_BACKING_PARSE("json:{\"file\":{\"driver\":\"iscsi\","
                                       "\"transport\":\"tcp\","
                                       "\"portal\":\"test.org:1234\","
                                       "\"target\":\"iqn.2016-12.com.virttest:emulated-iscsi-noauth.target\","
                                       "\"lun\":6"
                                      "}"
                            "}",
                       "<source protocol='iscsi' name='iqn.2016-12.com.virttest:emulated-iscsi-noauth.target/6'>\n"
                       "  <host name='test.org' port='1234'/>\n"
                       "</source>\n");
1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577
    TEST_BACKING_PARSE("json:{\"file\":{\"driver\":\"sheepdog\","
                                       "\"vdi\":\"test\","
                                       "\"server\":{ \"type\":\"inet\","
                                                    "\"host\":\"example.com\","
                                                    "\"port\":\"321\""
                                                  "}"
                                      "}"
                            "}",
                       "<source protocol='sheepdog' name='test'>\n"
                       "  <host name='example.com' port='321'/>\n"
                       "</source>\n");
1578 1579 1580 1581 1582 1583 1584 1585 1586 1587
    TEST_BACKING_PARSE("json:{\"driver\": \"raw\","
                             "\"file\": {\"server.host\": \"10.10.10.10\","
                                        "\"server.port\": \"7000\","
                                        "\"tag\": \"\","
                                        "\"driver\": \"sheepdog\","
                                        "\"server.type\": \"inet\","
                                        "\"vdi\": \"Alice\"}}",
                       "<source protocol='sheepdog' name='Alice'>\n"
                       "  <host name='10.10.10.10' port='7000'/>\n"
                       "</source>\n");
1588
#endif /* WITH_YAJL */
1589

E
Eric Blake 已提交
1590
 cleanup:
1591
    /* Final cleanup */
1592
    virStorageSourceFree(chain);
1593 1594 1595 1596 1597 1598
    testCleanupImages();
    virCommandFree(cmd);

    return ret == 0 ? EXIT_SUCCESS : EXIT_FAILURE;
}

1599
VIR_TEST_MAIN(mymain)