virfiletest.c 12.9 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
/*
 * Copyright (C) 2013 Red Hat, Inc.
 *
 * 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/>.
 */

#include <config.h>

M
Michal Privoznik 已提交
21
#include <fcntl.h>
22 23 24 25 26

#include "testutils.h"
#include "virfile.h"
#include "virstring.h"

27 28 29 30
#ifdef __linux__
# include <linux/falloc.h>
#endif

31
#define VIR_FROM_THIS VIR_FROM_NONE
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89

#if defined HAVE_MNTENT_H && defined HAVE_GETMNTENT_R
static int testFileCheckMounts(const char *prefix,
                               char **gotmounts,
                               size_t gotnmounts,
                               const char *const*wantmounts,
                               size_t wantnmounts)
{
    size_t i;
    if (gotnmounts != wantnmounts) {
        fprintf(stderr, "Expected %zu mounts under %s, but got %zu\n",
                wantnmounts, prefix, gotnmounts);
        return -1;
    }
    for (i = 0; i < gotnmounts; i++) {
        if (STRNEQ(gotmounts[i], wantmounts[i])) {
            fprintf(stderr, "Expected mount[%zu] '%s' but got '%s'\n",
                    i, wantmounts[i], gotmounts[i]);
            return -1;
        }
    }
    return 0;
}

struct testFileGetMountSubtreeData {
    const char *path;
    const char *prefix;
    const char *const *mounts;
    size_t nmounts;
    bool rev;
};

static int testFileGetMountSubtree(const void *opaque)
{
    int ret = -1;
    char **gotmounts = NULL;
    size_t gotnmounts = 0;
    const struct testFileGetMountSubtreeData *data = opaque;

    if (data->rev) {
        if (virFileGetMountReverseSubtree(data->path,
                                          data->prefix,
                                          &gotmounts,
                                          &gotnmounts) < 0)
            goto cleanup;
    } else {
        if (virFileGetMountSubtree(data->path,
                                   data->prefix,
                                   &gotmounts,
                                   &gotnmounts) < 0)
            goto cleanup;
    }

    ret = testFileCheckMounts(data->prefix,
                              gotmounts, gotnmounts,
                              data->mounts, data->nmounts);

 cleanup:
90
    virStringListFree(gotmounts);
91 92 93 94
    return ret;
}
#endif /* ! defined HAVE_MNTENT_H && defined HAVE_GETMNTENT_R */

95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123
struct testFileSanitizePathData
{
    const char *path;
    const char *expect;
};

static int
testFileSanitizePath(const void *opaque)
{
    const struct testFileSanitizePathData *data = opaque;
    int ret = -1;
    char *actual;

    if (!(actual = virFileSanitizePath(data->path)))
        return -1;

    if (STRNEQ(actual, data->expect)) {
        fprintf(stderr, "\nexpect: '%s'\nactual: '%s'\n", data->expect, actual);
        goto cleanup;
    }

    ret = 0;

 cleanup:
    VIR_FREE(actual);
    return ret;
}


124
#if HAVE_DECL_SEEK_HOLE && defined(__linux__)
M
Michal Privoznik 已提交
125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190

/* Create a sparse file. @offsets in KiB. */
static int
makeSparseFile(const off_t offsets[],
               const bool startData)
{
    int fd = -1;
    char path[] = abs_builddir "fileInData.XXXXXX";
    off_t len = 0;
    size_t i;

    if ((fd = mkostemp(path,  O_CLOEXEC|O_RDWR)) < 0)
        goto error;

    if (unlink(path) < 0)
        goto error;

    for (i = 0; offsets[i] != (off_t) -1; i++)
        len += offsets[i] * 1024;

    while (len) {
        const char buf[] = "abcdefghijklmnopqrstuvwxyz";
        off_t toWrite = sizeof(buf);

        if (toWrite > len)
            toWrite = len;

        if (safewrite(fd, buf, toWrite) < 0) {
            fprintf(stderr, "unable to write to %s (errno=%d)\n", path, errno);
            goto error;
        }

        len -= toWrite;
    }

    len = 0;
    for (i = 0; offsets[i] != (off_t) -1; i++) {
        bool inData = startData;

        if (i % 2)
            inData = !inData;

        if (!inData &&
            fallocate(fd,
                      FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE,
                      len, offsets[i] * 1024) < 0) {
            fprintf(stderr, "unable to punch a hole at offset %lld length %lld\n",
                    (long long) len, (long long) offsets[i]);
            goto error;
        }

        len += offsets[i] * 1024;
    }

    if (lseek(fd, 0, SEEK_SET) == (off_t) -1) {
        fprintf(stderr, "unable to lseek (errno=%d)\n", errno);
        goto error;
    }

    return fd;
 error:
    VIR_FORCE_CLOSE(fd);
    return -1;
}


191
# define EXTENT 4
M
Michal Privoznik 已提交
192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235
static bool
holesSupported(void)
{
    off_t offsets[] = {EXTENT, EXTENT, EXTENT, -1};
    off_t tmp;
    int fd;
    bool ret = false;

    if ((fd = makeSparseFile(offsets, true)) < 0)
        goto cleanup;

    /* The way this works is: there are 4K of data followed by 4K hole followed
     * by 4K hole again. Check if the filesystem we are running the test suite
     * on supports holes. */
    if ((tmp = lseek(fd, 0, SEEK_DATA)) == (off_t) -1)
        goto cleanup;

    if (tmp != 0)
        goto cleanup;

    if ((tmp = lseek(fd, tmp, SEEK_HOLE)) == (off_t) -1)
        goto cleanup;

    if (tmp != EXTENT * 1024)
        goto cleanup;

    if ((tmp = lseek(fd, tmp, SEEK_DATA)) == (off_t) -1)
        goto cleanup;

    if (tmp != 2 * EXTENT * 1024)
        goto cleanup;

    if ((tmp = lseek(fd, tmp, SEEK_HOLE)) == (off_t) -1)
        goto cleanup;

    if (tmp != 3 * EXTENT * 1024)
        goto cleanup;

    ret = true;
 cleanup:
    VIR_FORCE_CLOSE(fd);
    return ret;
}

236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252
#else /* !HAVE_DECL_SEEK_HOLE || !defined(__linux__)*/

static int
makeSparseFile(const off_t offsets[] ATTRIBUTE_UNUSED,
               const bool startData ATTRIBUTE_UNUSED)
{
    return -1;
}


static bool
holesSupported(void)
{
    return false;
}

#endif /* !HAVE_DECL_SEEK_HOLE || !defined(__linux__)*/
M
Michal Privoznik 已提交
253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310

struct testFileInData {
    bool startData;     /* whether the list of offsets starts with data section */
    off_t *offsets;
};


static int
testFileInData(const void *opaque)
{
    const struct testFileInData *data = opaque;
    int fd = -1;
    int ret = -1;
    size_t i;

    if ((fd = makeSparseFile(data->offsets, data->startData)) < 0)
        goto cleanup;

    for (i = 0; data->offsets[i] != (off_t) -1; i++) {
        bool shouldInData = data->startData;
        int realInData;
        long long shouldLen;
        long long realLen;

        if (i % 2)
            shouldInData = !shouldInData;

        if (virFileInData(fd, &realInData, &realLen) < 0)
            goto cleanup;

        if (realInData != shouldInData) {
            fprintf(stderr, "Unexpected data/hole. Expected %s got %s\n",
                    shouldInData ? "data" : "hole",
                    realInData ? "data" : "hole");
            goto cleanup;
        }

        shouldLen = data->offsets[i] * 1024;
        if (realLen != shouldLen) {
            fprintf(stderr, "Unexpected section length. Expected %lld got %lld\n",
                    shouldLen, realLen);
            goto cleanup;
        }

        if (lseek(fd, shouldLen, SEEK_CUR) < 0) {
            fprintf(stderr, "Unable to seek\n");
            goto cleanup;
        }
    }

    ret = 0;

 cleanup:
    VIR_FORCE_CLOSE(fd);
    return ret;
}


311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352
struct testFileIsSharedFSType {
    const char *mtabFile;
    const char *filename;
    const bool expected;
};

static int
testFileIsSharedFSType(const void *opaque ATTRIBUTE_UNUSED)
{
#ifndef __linux__
    return EXIT_AM_SKIP;
#else
    const struct testFileIsSharedFSType *data = opaque;
    char *mtabFile = NULL;
    bool actual;
    int ret = -1;

    if (virAsprintf(&mtabFile, abs_srcdir "/virfiledata/%s", data->mtabFile) < 0)
        return -1;

    if (setenv("LIBVIRT_MTAB", mtabFile, 1) < 0) {
        fprintf(stderr, "Unable to set env variable\n");
        goto cleanup;
    }

    actual = virFileIsSharedFS(data->filename);

    if (actual != data->expected) {
        fprintf(stderr, "Unexpected FS type. Expected %d got %d\n",
                data->expected, actual);
        goto cleanup;
    }

    ret = 0;
 cleanup:
    VIR_FREE(mtabFile);
    unsetenv("LIBVIRT_MTAB");
    return ret;
#endif
}


353 354 355 356
static int
mymain(void)
{
    int ret = 0;
357
    struct testFileSanitizePathData data1;
358 359 360 361 362 363 364 365 366 367 368

#if defined HAVE_MNTENT_H && defined HAVE_GETMNTENT_R
# define MTAB_PATH1 abs_srcdir "/virfiledata/mounts1.txt"
# define MTAB_PATH2 abs_srcdir "/virfiledata/mounts2.txt"

    static const char *wantmounts1[] = {
        "/proc", "/proc/sys/fs/binfmt_misc", "/proc/sys/fs/binfmt_misc",
    };
    static const char *wantmounts1rev[] = {
        "/proc/sys/fs/binfmt_misc", "/proc/sys/fs/binfmt_misc", "/proc"
    };
369 370 371 372 373 374
    static const char *wantmounts2a[] = {
        "/etc/aliases"
    };
    static const char *wantmounts2b[] = {
        "/etc/aliases.db"
    };
375

376 377 378 379 380 381 382
# define DO_TEST_MOUNT_SUBTREE(name, path, prefix, mounts, rev) \
    do { \
        struct testFileGetMountSubtreeData data = { \
            path, prefix, mounts, ARRAY_CARDINALITY(mounts), rev \
        }; \
        if (virTestRun(name, testFileGetMountSubtree, &data) < 0) \
            ret = -1; \
383 384 385 386
    } while (0)

    DO_TEST_MOUNT_SUBTREE("/proc normal", MTAB_PATH1, "/proc", wantmounts1, false);
    DO_TEST_MOUNT_SUBTREE("/proc reverse", MTAB_PATH1, "/proc", wantmounts1rev, true);
387 388
    DO_TEST_MOUNT_SUBTREE("/etc/aliases", MTAB_PATH2, "/etc/aliases", wantmounts2a, false);
    DO_TEST_MOUNT_SUBTREE("/etc/aliases.db", MTAB_PATH2, "/etc/aliases.db", wantmounts2b, false);
389 390
#endif /* ! defined HAVE_MNTENT_H && defined HAVE_GETMNTENT_R */

391 392 393 394 395 396 397
#define DO_TEST_SANITIZE_PATH(PATH, EXPECT) \
    do { \
        data1.path = PATH; \
        data1.expect = EXPECT; \
        if (virTestRun(virTestCounterNext(), testFileSanitizePath, \
                       &data1) < 0) \
            ret = -1; \
398 399
    } while (0)

400 401
#define DO_TEST_SANITIZE_PATH_SAME(PATH) DO_TEST_SANITIZE_PATH(PATH, PATH)

402
    virTestCounterReset("testFileSanitizePath ");
403 404 405 406 407 408 409 410 411 412 413 414
    DO_TEST_SANITIZE_PATH("", "");
    DO_TEST_SANITIZE_PATH("/", "/");
    DO_TEST_SANITIZE_PATH("/path", "/path");
    DO_TEST_SANITIZE_PATH("/path/to/blah", "/path/to/blah");
    DO_TEST_SANITIZE_PATH("/path/", "/path");
    DO_TEST_SANITIZE_PATH("///////", "/");
    DO_TEST_SANITIZE_PATH("//", "//");
    DO_TEST_SANITIZE_PATH(".", ".");
    DO_TEST_SANITIZE_PATH("../", "..");
    DO_TEST_SANITIZE_PATH("../../", "../..");
    DO_TEST_SANITIZE_PATH("//foo//bar", "//foo/bar");
    DO_TEST_SANITIZE_PATH("/bar//foo", "/bar/foo");
415 416 417 418 419
    DO_TEST_SANITIZE_PATH_SAME("gluster://bar.baz/foo/hoo");
    DO_TEST_SANITIZE_PATH_SAME("gluster://bar.baz//fooo/hoo");
    DO_TEST_SANITIZE_PATH_SAME("gluster://bar.baz//////fooo/hoo");
    DO_TEST_SANITIZE_PATH_SAME("gluster://bar.baz/fooo//hoo");
    DO_TEST_SANITIZE_PATH_SAME("gluster://bar.baz/fooo///////hoo");
420

421 422 423 424 425 426 427 428
#define DO_TEST_IN_DATA(inData, ...) \
    do { \
        off_t offsets[] = {__VA_ARGS__, -1}; \
        struct testFileInData data = { \
            .startData = inData, .offsets = offsets, \
        }; \
        if (virTestRun(virTestCounterNext(), testFileInData, &data) < 0) \
            ret = -1; \
M
Michal Privoznik 已提交
429 430 431
    } while (0)

    if (holesSupported()) {
432
        virTestCounterReset("testFileInData ");
M
Michal Privoznik 已提交
433 434 435 436 437 438 439
        DO_TEST_IN_DATA(true, 4, 4, 4);
        DO_TEST_IN_DATA(false, 4, 4, 4);
        DO_TEST_IN_DATA(true, 8, 8, 8);
        DO_TEST_IN_DATA(false, 8, 8, 8);
        DO_TEST_IN_DATA(true, 8, 16, 32, 64, 128, 256, 512);
        DO_TEST_IN_DATA(false, 8, 16, 32, 64, 128, 256, 512);
    }
440 441 442 443 444 445 446 447 448 449 450 451 452 453

#define DO_TEST_FILE_IS_SHARED_FS_TYPE(mtab, file, exp) \
    do { \
        struct testFileIsSharedFSType data = { \
            .mtabFile = mtab, .filename = file, .expected = exp \
        }; \
        if (virTestRun(virTestCounterNext(), testFileIsSharedFSType, &data) < 0) \
            ret = -1; \
    } while (0)

    virTestCounterReset("testFileIsSharedFSType ");
    DO_TEST_FILE_IS_SHARED_FS_TYPE("mounts1.txt", "/boot/vmlinuz", false);
    DO_TEST_FILE_IS_SHARED_FS_TYPE("mounts2.txt", "/run/user/501/gvfs/some/file", false);
    DO_TEST_FILE_IS_SHARED_FS_TYPE("mounts3.txt", "/nfs/file", true);
454
    DO_TEST_FILE_IS_SHARED_FS_TYPE("mounts3.txt", "/nfs/blah", false);
455 456
    DO_TEST_FILE_IS_SHARED_FS_TYPE("mounts3.txt", "/gluster/file", true);
    DO_TEST_FILE_IS_SHARED_FS_TYPE("mounts3.txt", "/gluster/sshfs/file", false);
457
    DO_TEST_FILE_IS_SHARED_FS_TYPE("mounts3.txt", "/some/symlink/file", true);
458 459
    DO_TEST_FILE_IS_SHARED_FS_TYPE("mounts3.txt", "/ceph/file", true);
    DO_TEST_FILE_IS_SHARED_FS_TYPE("mounts3.txt", "/ceph/multi/file", true);
460

461 462 463
    return ret != 0 ? EXIT_FAILURE : EXIT_SUCCESS;
}

464
#ifdef __linux__
465
VIR_TEST_MAIN_PRELOAD(mymain, abs_builddir "/.libs/virfilemock.so")
466 467 468
#else
VIR_TEST_MAIN(mymain)
#endif