2 * Copyright (C) 2013 Red Hat, Inc.
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library. If not, see
16 * <http://www.gnu.org/licenses/>.
23 #include "testutils.h"
25 #include "virstring.h"
28 # include <linux/falloc.h>
31 #define VIR_FROM_THIS VIR_FROM_NONE
33 #if defined HAVE_MNTENT_H && defined HAVE_GETMNTENT_R
34 static int testFileCheckMounts(const char *prefix
,
37 const char *const*wantmounts
,
41 if (gotnmounts
!= wantnmounts
) {
42 fprintf(stderr
, "Expected %zu mounts under %s, but got %zu\n",
43 wantnmounts
, prefix
, gotnmounts
);
46 for (i
= 0; i
< gotnmounts
; i
++) {
47 if (STRNEQ(gotmounts
[i
], wantmounts
[i
])) {
48 fprintf(stderr
, "Expected mount[%zu] '%s' but got '%s'\n",
49 i
, wantmounts
[i
], gotmounts
[i
]);
56 struct testFileGetMountSubtreeData
{
59 const char *const *mounts
;
64 static int testFileGetMountSubtree(const void *opaque
)
67 char **gotmounts
= NULL
;
68 size_t gotnmounts
= 0;
69 const struct testFileGetMountSubtreeData
*data
= opaque
;
72 if (virFileGetMountReverseSubtree(data
->path
,
78 if (virFileGetMountSubtree(data
->path
,
85 ret
= testFileCheckMounts(data
->prefix
,
86 gotmounts
, gotnmounts
,
87 data
->mounts
, data
->nmounts
);
90 virStringListFree(gotmounts
);
93 #endif /* ! defined HAVE_MNTENT_H && defined HAVE_GETMNTENT_R */
95 struct testFileSanitizePathData
102 testFileSanitizePath(const void *opaque
)
104 const struct testFileSanitizePathData
*data
= opaque
;
108 if (!(actual
= virFileSanitizePath(data
->path
)))
111 if (STRNEQ(actual
, data
->expect
)) {
112 fprintf(stderr
, "\nexpect: '%s'\nactual: '%s'\n", data
->expect
, actual
);
124 #if HAVE_DECL_SEEK_HOLE && defined(__linux__)
126 /* Create a sparse file. @offsets in KiB. */
128 makeSparseFile(const off_t offsets
[],
129 const bool startData
)
132 char path
[] = abs_builddir
"fileInData.XXXXXX";
136 if ((fd
= mkostemp(path
, O_CLOEXEC
|O_RDWR
)) < 0)
139 if (unlink(path
) < 0)
142 for (i
= 0; offsets
[i
] != (off_t
) -1; i
++)
143 len
+= offsets
[i
] * 1024;
146 const char buf
[] = "abcdefghijklmnopqrstuvwxyz";
147 off_t toWrite
= sizeof(buf
);
152 if (safewrite(fd
, buf
, toWrite
) < 0) {
153 fprintf(stderr
, "unable to write to %s (errno=%d)\n", path
, errno
);
161 for (i
= 0; offsets
[i
] != (off_t
) -1; i
++) {
162 bool inData
= startData
;
169 FALLOC_FL_PUNCH_HOLE
| FALLOC_FL_KEEP_SIZE
,
170 len
, offsets
[i
] * 1024) < 0) {
171 fprintf(stderr
, "unable to punch a hole at offset %lld length %lld\n",
172 (long long) len
, (long long) offsets
[i
]);
176 len
+= offsets
[i
] * 1024;
179 if (lseek(fd
, 0, SEEK_SET
) == (off_t
) -1) {
180 fprintf(stderr
, "unable to lseek (errno=%d)\n", errno
);
195 off_t offsets
[] = {EXTENT
, EXTENT
, EXTENT
, -1};
200 if ((fd
= makeSparseFile(offsets
, true)) < 0)
203 /* The way this works is: there are 4K of data followed by 4K hole followed
204 * by 4K hole again. Check if the filesystem we are running the test suite
205 * on supports holes. */
206 if ((tmp
= lseek(fd
, 0, SEEK_DATA
)) == (off_t
) -1)
212 if ((tmp
= lseek(fd
, tmp
, SEEK_HOLE
)) == (off_t
) -1)
215 if (tmp
!= EXTENT
* 1024)
218 if ((tmp
= lseek(fd
, tmp
, SEEK_DATA
)) == (off_t
) -1)
221 if (tmp
!= 2 * EXTENT
* 1024)
224 if ((tmp
= lseek(fd
, tmp
, SEEK_HOLE
)) == (off_t
) -1)
227 if (tmp
!= 3 * EXTENT
* 1024)
236 #else /* !HAVE_DECL_SEEK_HOLE || !defined(__linux__)*/
239 makeSparseFile(const off_t offsets
[] ATTRIBUTE_UNUSED
,
240 const bool startData ATTRIBUTE_UNUSED
)
252 #endif /* !HAVE_DECL_SEEK_HOLE || !defined(__linux__)*/
254 struct testFileInData
{
255 bool startData
; /* whether the list of offsets starts with data section */
261 testFileInData(const void *opaque
)
263 const struct testFileInData
*data
= opaque
;
268 if ((fd
= makeSparseFile(data
->offsets
, data
->startData
)) < 0)
271 for (i
= 0; data
->offsets
[i
] != (off_t
) -1; i
++) {
272 bool shouldInData
= data
->startData
;
278 shouldInData
= !shouldInData
;
280 if (virFileInData(fd
, &realInData
, &realLen
) < 0)
283 if (realInData
!= shouldInData
) {
284 fprintf(stderr
, "Unexpected data/hole. Expected %s got %s\n",
285 shouldInData
? "data" : "hole",
286 realInData
? "data" : "hole");
290 shouldLen
= data
->offsets
[i
] * 1024;
291 if (realLen
!= shouldLen
) {
292 fprintf(stderr
, "Unexpected section length. Expected %lld got %lld\n",
297 if (lseek(fd
, shouldLen
, SEEK_CUR
) < 0) {
298 fprintf(stderr
, "Unable to seek\n");
311 struct testFileIsSharedFSType
{
312 const char *mtabFile
;
313 const char *filename
;
318 testFileIsSharedFSType(const void *opaque ATTRIBUTE_UNUSED
)
323 const struct testFileIsSharedFSType
*data
= opaque
;
324 char *mtabFile
= NULL
;
328 if (virAsprintf(&mtabFile
, abs_srcdir
"/virfiledata/%s", data
->mtabFile
) < 0)
331 if (setenv("LIBVIRT_MTAB", mtabFile
, 1) < 0) {
332 fprintf(stderr
, "Unable to set env variable\n");
336 actual
= virFileIsSharedFS(data
->filename
);
338 if (actual
!= data
->expected
) {
339 fprintf(stderr
, "Unexpected FS type. Expected %d got %d\n",
340 data
->expected
, actual
);
347 unsetenv("LIBVIRT_MTAB");
357 struct testFileSanitizePathData data1
;
359 #if defined HAVE_MNTENT_H && defined HAVE_GETMNTENT_R
360 # define MTAB_PATH1 abs_srcdir "/virfiledata/mounts1.txt"
361 # define MTAB_PATH2 abs_srcdir "/virfiledata/mounts2.txt"
363 static const char *wantmounts1
[] = {
364 "/proc", "/proc/sys/fs/binfmt_misc", "/proc/sys/fs/binfmt_misc",
366 static const char *wantmounts1rev
[] = {
367 "/proc/sys/fs/binfmt_misc", "/proc/sys/fs/binfmt_misc", "/proc"
369 static const char *wantmounts2a
[] = {
372 static const char *wantmounts2b
[] = {
376 # define DO_TEST_MOUNT_SUBTREE(name, path, prefix, mounts, rev) \
378 struct testFileGetMountSubtreeData data = { \
379 path, prefix, mounts, ARRAY_CARDINALITY(mounts), rev \
381 if (virTestRun(name, testFileGetMountSubtree, &data) < 0) \
385 DO_TEST_MOUNT_SUBTREE("/proc normal", MTAB_PATH1
, "/proc", wantmounts1
, false);
386 DO_TEST_MOUNT_SUBTREE("/proc reverse", MTAB_PATH1
, "/proc", wantmounts1rev
, true);
387 DO_TEST_MOUNT_SUBTREE("/etc/aliases", MTAB_PATH2
, "/etc/aliases", wantmounts2a
, false);
388 DO_TEST_MOUNT_SUBTREE("/etc/aliases.db", MTAB_PATH2
, "/etc/aliases.db", wantmounts2b
, false);
389 #endif /* ! defined HAVE_MNTENT_H && defined HAVE_GETMNTENT_R */
391 #define DO_TEST_SANITIZE_PATH(PATH, EXPECT) \
394 data1.expect = EXPECT; \
395 if (virTestRun(virTestCounterNext(), testFileSanitizePath, \
400 #define DO_TEST_SANITIZE_PATH_SAME(PATH) DO_TEST_SANITIZE_PATH(PATH, PATH)
402 virTestCounterReset("testFileSanitizePath ");
403 DO_TEST_SANITIZE_PATH("", "");
404 DO_TEST_SANITIZE_PATH("/", "/");
405 DO_TEST_SANITIZE_PATH("/path", "/path");
406 DO_TEST_SANITIZE_PATH("/path/to/blah", "/path/to/blah");
407 DO_TEST_SANITIZE_PATH("/path/", "/path");
408 DO_TEST_SANITIZE_PATH("///////", "/");
409 DO_TEST_SANITIZE_PATH("//", "//");
410 DO_TEST_SANITIZE_PATH(".", ".");
411 DO_TEST_SANITIZE_PATH("../", "..");
412 DO_TEST_SANITIZE_PATH("../../", "../..");
413 DO_TEST_SANITIZE_PATH("//foo//bar", "//foo/bar");
414 DO_TEST_SANITIZE_PATH("/bar//foo", "/bar/foo");
415 DO_TEST_SANITIZE_PATH_SAME("gluster://bar.baz/foo/hoo");
416 DO_TEST_SANITIZE_PATH_SAME("gluster://bar.baz//fooo/hoo");
417 DO_TEST_SANITIZE_PATH_SAME("gluster://bar.baz//////fooo/hoo");
418 DO_TEST_SANITIZE_PATH_SAME("gluster://bar.baz/fooo//hoo");
419 DO_TEST_SANITIZE_PATH_SAME("gluster://bar.baz/fooo///////hoo");
421 #define DO_TEST_IN_DATA(inData, ...) \
423 off_t offsets[] = {__VA_ARGS__, -1}; \
424 struct testFileInData data = { \
425 .startData = inData, .offsets = offsets, \
427 if (virTestRun(virTestCounterNext(), testFileInData, &data) < 0) \
431 if (holesSupported()) {
432 virTestCounterReset("testFileInData ");
433 DO_TEST_IN_DATA(true, 4, 4, 4);
434 DO_TEST_IN_DATA(false, 4, 4, 4);
435 DO_TEST_IN_DATA(true, 8, 8, 8);
436 DO_TEST_IN_DATA(false, 8, 8, 8);
437 DO_TEST_IN_DATA(true, 8, 16, 32, 64, 128, 256, 512);
438 DO_TEST_IN_DATA(false, 8, 16, 32, 64, 128, 256, 512);
441 #define DO_TEST_FILE_IS_SHARED_FS_TYPE(mtab, file, exp) \
443 struct testFileIsSharedFSType data = { \
444 .mtabFile = mtab, .filename = file, .expected = exp \
446 if (virTestRun(virTestCounterNext(), testFileIsSharedFSType, &data) < 0) \
450 virTestCounterReset("testFileIsSharedFSType ");
451 DO_TEST_FILE_IS_SHARED_FS_TYPE("mounts1.txt", "/boot/vmlinuz", false);
452 DO_TEST_FILE_IS_SHARED_FS_TYPE("mounts2.txt", "/run/user/501/gvfs/some/file", false);
453 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 DO_TEST_FILE_IS_SHARED_FS_TYPE("mounts3.txt", "/gluster/file", true);
456 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 DO_TEST_FILE_IS_SHARED_FS_TYPE("mounts3.txt", "/ceph/file", true);
459 DO_TEST_FILE_IS_SHARED_FS_TYPE("mounts3.txt", "/ceph/multi/file", true);
460 DO_TEST_FILE_IS_SHARED_FS_TYPE("mounts3.txt", "/gpfs/data", true);
461 DO_TEST_FILE_IS_SHARED_FS_TYPE("mounts3.txt", "/quobyte", true);
463 return ret
!= 0 ? EXIT_FAILURE
: EXIT_SUCCESS
;
467 VIR_TEST_MAIN_PRELOAD(mymain
, abs_builddir
"/.libs/virfilemock.so")
469 VIR_TEST_MAIN(mymain
)