1 2 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 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 88 89 90 91 92 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 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 306 307 308 309 310 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 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449
|
/*
* 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>
#include <fcntl.h>
#include <unistd.h>
#include "testutils.h"
#include "virfile.h"
#ifdef __linux__
# include <linux/falloc.h>
#endif
#define VIR_FROM_THIS VIR_FROM_NONE
#if defined WITH_MNTENT_H && defined WITH_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)
{
g_auto(GStrv) gotmounts = NULL;
size_t gotnmounts = 0;
const struct testFileGetMountSubtreeData *data = opaque;
if (data->rev) {
if (virFileGetMountReverseSubtree(data->path,
data->prefix,
&gotmounts,
&gotnmounts) < 0)
return -1;
} else {
if (virFileGetMountSubtree(data->path,
data->prefix,
&gotmounts,
&gotnmounts) < 0)
return -1;
}
return testFileCheckMounts(data->prefix,
gotmounts, gotnmounts,
data->mounts, data->nmounts);
}
#endif /* ! defined WITH_MNTENT_H && defined WITH_GETMNTENT_R */
struct testFileSanitizePathData
{
const char *path;
const char *expect;
};
static int
testFileSanitizePath(const void *opaque)
{
const struct testFileSanitizePathData *data = opaque;
g_autofree char *actual = NULL;
if (!(actual = virFileSanitizePath(data->path)))
return -1;
if (STRNEQ(actual, data->expect)) {
fprintf(stderr, "\nexpect: '%s'\nactual: '%s'\n", data->expect, actual);
return -1;
}
return 0;
}
#if WITH_DECL_SEEK_HOLE && defined(__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 = g_mkstemp_full(path, O_RDWR | O_CLOEXEC, S_IRUSR | S_IWUSR)) < 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;
}
# define EXTENT 4
static bool
holesSupported(void)
{
off_t offsets[] = {EXTENT, EXTENT, EXTENT, -1};
off_t tmp;
VIR_AUTOCLOSE fd = -1;
if ((fd = makeSparseFile(offsets, true)) < 0)
return false;
/* 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)
return false;
if (tmp != 0)
return false;
if ((tmp = lseek(fd, tmp, SEEK_HOLE)) == (off_t) -1)
return false;
if (tmp != EXTENT * 1024)
return false;
if ((tmp = lseek(fd, tmp, SEEK_DATA)) == (off_t) -1)
return false;
if (tmp != 2 * EXTENT * 1024)
return false;
if ((tmp = lseek(fd, tmp, SEEK_HOLE)) == (off_t) -1)
return false;
if (tmp != 3 * EXTENT * 1024)
return false;
return true;
}
#else /* !WITH_DECL_SEEK_HOLE || !defined(__linux__)*/
static int
makeSparseFile(const off_t offsets[] G_GNUC_UNUSED,
const bool startData G_GNUC_UNUSED)
{
return -1;
}
static bool
holesSupported(void)
{
return false;
}
#endif /* !WITH_DECL_SEEK_HOLE || !defined(__linux__)*/
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;
VIR_AUTOCLOSE fd = -1;
size_t i;
if ((fd = makeSparseFile(data->offsets, data->startData)) < 0)
return -1;
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)
return -1;
if (realInData != shouldInData) {
fprintf(stderr, "Unexpected data/hole. Expected %s got %s\n",
shouldInData ? "data" : "hole",
realInData ? "data" : "hole");
return -1;
}
shouldLen = data->offsets[i] * 1024;
if (realLen != shouldLen) {
fprintf(stderr, "Unexpected section length. Expected %lld got %lld\n",
shouldLen, realLen);
return -1;
}
if (lseek(fd, shouldLen, SEEK_CUR) < 0) {
fprintf(stderr, "Unable to seek\n");
return -1;
}
}
return 0;
}
struct testFileIsSharedFSType {
const char *mtabFile;
const char *filename;
const bool expected;
};
static int
testFileIsSharedFSType(const void *opaque G_GNUC_UNUSED)
{
#ifndef __linux__
return EXIT_AM_SKIP;
#else
const struct testFileIsSharedFSType *data = opaque;
g_autofree char *mtabFile = NULL;
bool actual;
int ret = -1;
mtabFile = g_strdup_printf(abs_srcdir "/virfiledata/%s", data->mtabFile);
if (g_setenv("LIBVIRT_MTAB", mtabFile, TRUE) == FALSE) {
fprintf(stderr, "Unable to set env variable\n");
goto cleanup;
}
actual = virFileIsSharedFS(data->filename, NULL);
if (actual != data->expected) {
fprintf(stderr, "Unexpected FS type. Expected %d got %d\n",
data->expected, actual);
goto cleanup;
}
ret = 0;
cleanup:
g_unsetenv("LIBVIRT_MTAB");
return ret;
#endif
}
static int
mymain(void)
{
int ret = 0;
struct testFileSanitizePathData data1;
#if defined WITH_MNTENT_H && defined WITH_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"
};
static const char *wantmounts2a[] = {
"/etc/aliases"
};
static const char *wantmounts2b[] = {
"/etc/aliases.db"
};
# define DO_TEST_MOUNT_SUBTREE(name, path, prefix, mounts, rev) \
do { \
struct testFileGetMountSubtreeData data = { \
path, prefix, mounts, G_N_ELEMENTS(mounts), rev \
}; \
if (virTestRun(name, testFileGetMountSubtree, &data) < 0) \
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);
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);
#endif /* ! defined WITH_MNTENT_H && defined WITH_GETMNTENT_R */
#define DO_TEST_SANITIZE_PATH(PATH, EXPECT) \
do { \
data1.path = PATH; \
data1.expect = EXPECT; \
if (virTestRun(virTestCounterNext(), testFileSanitizePath, \
&data1) < 0) \
ret = -1; \
} while (0)
#define DO_TEST_SANITIZE_PATH_SAME(PATH) DO_TEST_SANITIZE_PATH(PATH, PATH)
virTestCounterReset("testFileSanitizePath ");
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");
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");
#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()) {
virTestCounterReset("testFileInData ");
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);
}
#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);
DO_TEST_FILE_IS_SHARED_FS_TYPE("mounts3.txt", "/nfs/blah", false);
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);
DO_TEST_FILE_IS_SHARED_FS_TYPE("mounts3.txt", "/some/symlink/file", true);
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);
DO_TEST_FILE_IS_SHARED_FS_TYPE("mounts3.txt", "/gpfs/data", true);
DO_TEST_FILE_IS_SHARED_FS_TYPE("mounts3.txt", "/quobyte", true);
return ret != 0 ? EXIT_FAILURE : EXIT_SUCCESS;
}
#ifdef __linux__
VIR_TEST_MAIN_PRELOAD(mymain, VIR_TEST_MOCK("virfile"))
#else
VIR_TEST_MAIN(mymain)
#endif
|