virfiletest.c 11.5 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
/*
 * 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/>.
 *
 * Author: Daniel P. Berrange <berrange@redhat.com>
 */

#include <config.h>

#include <stdlib.h>
M
Michal Privoznik 已提交
24
#include <fcntl.h>
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87

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


#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:
88
    virStringListFree(gotmounts);
89 90 91 92
    return ret;
}
#endif /* ! defined HAVE_MNTENT_H && defined HAVE_GETMNTENT_R */

93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121
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;
}


M
Michal Privoznik 已提交
122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305
static int
makeSparseFile(const off_t offsets[],
               const bool startData);

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

#else /* !__linux__ */

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

#endif /* !__linux__ */


#define EXTENT 4
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;
}


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


306 307 308 309
static int
mymain(void)
{
    int ret = 0;
310
    struct testFileSanitizePathData data1;
311 312 313 314 315 316 317 318 319 320 321

#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"
    };
322 323 324 325 326 327
    static const char *wantmounts2a[] = {
        "/etc/aliases"
    };
    static const char *wantmounts2b[] = {
        "/etc/aliases.db"
    };
328 329 330 331 332 333

# define DO_TEST_MOUNT_SUBTREE(name, path, prefix, mounts, rev)    \
    do {                                                           \
        struct testFileGetMountSubtreeData data = {                \
            path, prefix, mounts, ARRAY_CARDINALITY(mounts), rev   \
        };                                                         \
334
        if (virTestRun(name, testFileGetMountSubtree, &data) < 0)  \
335 336 337 338 339
            ret = -1;                                              \
    } while (0)

    DO_TEST_MOUNT_SUBTREE("/proc normal", MTAB_PATH1, "/proc", wantmounts1, false);
    DO_TEST_MOUNT_SUBTREE("/proc reverse", MTAB_PATH1, "/proc", wantmounts1rev, true);
340 341
    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);
342 343
#endif /* ! defined HAVE_MNTENT_H && defined HAVE_GETMNTENT_R */

344 345 346 347
#define DO_TEST_SANITIZE_PATH(PATH, EXPECT)                                    \
    do {                                                                       \
        data1.path = PATH;                                                     \
        data1.expect = EXPECT;                                                 \
348
        if (virTestRun(virTestCounterNext(), testFileSanitizePath,             \
349
                       &data1) < 0)                                            \
350 351 352
            ret = -1;                                                          \
    } while (0)

353 354
#define DO_TEST_SANITIZE_PATH_SAME(PATH) DO_TEST_SANITIZE_PATH(PATH, PATH)

355
    virTestCounterReset("testFileSanitizePath ");
356 357 358 359 360 361 362 363 364 365 366 367
    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");
368 369 370 371 372
    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");
373

M
Michal Privoznik 已提交
374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391
#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;                                                       \
    } while (0)

    if (holesSupported()) {
        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);
    }
392 393 394
    return ret != 0 ? EXIT_FAILURE : EXIT_SUCCESS;
}

395
VIR_TEST_MAIN(mymain)