backup: Wire up qemu full pull backup commands over QMP
[libvirt/ericb.git] / tests / virfiletest.c
blobaa4c3435e5313a9841eca5483c397231437dfe83
1 /*
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/>.
19 #include <config.h>
21 #include <fcntl.h>
23 #include "testutils.h"
24 #include "virfile.h"
25 #include "virstring.h"
27 #ifdef __linux__
28 # include <linux/falloc.h>
29 #endif
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,
35 char **gotmounts,
36 size_t gotnmounts,
37 const char *const*wantmounts,
38 size_t wantnmounts)
40 size_t i;
41 if (gotnmounts != wantnmounts) {
42 fprintf(stderr, "Expected %zu mounts under %s, but got %zu\n",
43 wantnmounts, prefix, gotnmounts);
44 return -1;
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]);
50 return -1;
53 return 0;
56 struct testFileGetMountSubtreeData {
57 const char *path;
58 const char *prefix;
59 const char *const *mounts;
60 size_t nmounts;
61 bool rev;
64 static int testFileGetMountSubtree(const void *opaque)
66 int ret = -1;
67 char **gotmounts = NULL;
68 size_t gotnmounts = 0;
69 const struct testFileGetMountSubtreeData *data = opaque;
71 if (data->rev) {
72 if (virFileGetMountReverseSubtree(data->path,
73 data->prefix,
74 &gotmounts,
75 &gotnmounts) < 0)
76 goto cleanup;
77 } else {
78 if (virFileGetMountSubtree(data->path,
79 data->prefix,
80 &gotmounts,
81 &gotnmounts) < 0)
82 goto cleanup;
85 ret = testFileCheckMounts(data->prefix,
86 gotmounts, gotnmounts,
87 data->mounts, data->nmounts);
89 cleanup:
90 virStringListFree(gotmounts);
91 return ret;
93 #endif /* ! defined HAVE_MNTENT_H && defined HAVE_GETMNTENT_R */
95 struct testFileSanitizePathData
97 const char *path;
98 const char *expect;
101 static int
102 testFileSanitizePath(const void *opaque)
104 const struct testFileSanitizePathData *data = opaque;
105 int ret = -1;
106 char *actual;
108 if (!(actual = virFileSanitizePath(data->path)))
109 return -1;
111 if (STRNEQ(actual, data->expect)) {
112 fprintf(stderr, "\nexpect: '%s'\nactual: '%s'\n", data->expect, actual);
113 goto cleanup;
116 ret = 0;
118 cleanup:
119 VIR_FREE(actual);
120 return ret;
124 #if HAVE_DECL_SEEK_HOLE && defined(__linux__)
126 /* Create a sparse file. @offsets in KiB. */
127 static int
128 makeSparseFile(const off_t offsets[],
129 const bool startData)
131 int fd = -1;
132 char path[] = abs_builddir "fileInData.XXXXXX";
133 off_t len = 0;
134 size_t i;
136 if ((fd = mkostemp(path, O_CLOEXEC|O_RDWR)) < 0)
137 goto error;
139 if (unlink(path) < 0)
140 goto error;
142 for (i = 0; offsets[i] != (off_t) -1; i++)
143 len += offsets[i] * 1024;
145 while (len) {
146 const char buf[] = "abcdefghijklmnopqrstuvwxyz";
147 off_t toWrite = sizeof(buf);
149 if (toWrite > len)
150 toWrite = len;
152 if (safewrite(fd, buf, toWrite) < 0) {
153 fprintf(stderr, "unable to write to %s (errno=%d)\n", path, errno);
154 goto error;
157 len -= toWrite;
160 len = 0;
161 for (i = 0; offsets[i] != (off_t) -1; i++) {
162 bool inData = startData;
164 if (i % 2)
165 inData = !inData;
167 if (!inData &&
168 fallocate(fd,
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]);
173 goto error;
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);
181 goto error;
184 return fd;
185 error:
186 VIR_FORCE_CLOSE(fd);
187 return -1;
191 # define EXTENT 4
192 static bool
193 holesSupported(void)
195 off_t offsets[] = {EXTENT, EXTENT, EXTENT, -1};
196 off_t tmp;
197 int fd;
198 bool ret = false;
200 if ((fd = makeSparseFile(offsets, true)) < 0)
201 goto cleanup;
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)
207 goto cleanup;
209 if (tmp != 0)
210 goto cleanup;
212 if ((tmp = lseek(fd, tmp, SEEK_HOLE)) == (off_t) -1)
213 goto cleanup;
215 if (tmp != EXTENT * 1024)
216 goto cleanup;
218 if ((tmp = lseek(fd, tmp, SEEK_DATA)) == (off_t) -1)
219 goto cleanup;
221 if (tmp != 2 * EXTENT * 1024)
222 goto cleanup;
224 if ((tmp = lseek(fd, tmp, SEEK_HOLE)) == (off_t) -1)
225 goto cleanup;
227 if (tmp != 3 * EXTENT * 1024)
228 goto cleanup;
230 ret = true;
231 cleanup:
232 VIR_FORCE_CLOSE(fd);
233 return ret;
236 #else /* !HAVE_DECL_SEEK_HOLE || !defined(__linux__)*/
238 static int
239 makeSparseFile(const off_t offsets[] ATTRIBUTE_UNUSED,
240 const bool startData ATTRIBUTE_UNUSED)
242 return -1;
246 static bool
247 holesSupported(void)
249 return false;
252 #endif /* !HAVE_DECL_SEEK_HOLE || !defined(__linux__)*/
254 struct testFileInData {
255 bool startData; /* whether the list of offsets starts with data section */
256 off_t *offsets;
260 static int
261 testFileInData(const void *opaque)
263 const struct testFileInData *data = opaque;
264 int fd = -1;
265 int ret = -1;
266 size_t i;
268 if ((fd = makeSparseFile(data->offsets, data->startData)) < 0)
269 goto cleanup;
271 for (i = 0; data->offsets[i] != (off_t) -1; i++) {
272 bool shouldInData = data->startData;
273 int realInData;
274 long long shouldLen;
275 long long realLen;
277 if (i % 2)
278 shouldInData = !shouldInData;
280 if (virFileInData(fd, &realInData, &realLen) < 0)
281 goto cleanup;
283 if (realInData != shouldInData) {
284 fprintf(stderr, "Unexpected data/hole. Expected %s got %s\n",
285 shouldInData ? "data" : "hole",
286 realInData ? "data" : "hole");
287 goto cleanup;
290 shouldLen = data->offsets[i] * 1024;
291 if (realLen != shouldLen) {
292 fprintf(stderr, "Unexpected section length. Expected %lld got %lld\n",
293 shouldLen, realLen);
294 goto cleanup;
297 if (lseek(fd, shouldLen, SEEK_CUR) < 0) {
298 fprintf(stderr, "Unable to seek\n");
299 goto cleanup;
303 ret = 0;
305 cleanup:
306 VIR_FORCE_CLOSE(fd);
307 return ret;
311 struct testFileIsSharedFSType {
312 const char *mtabFile;
313 const char *filename;
314 const bool expected;
317 static int
318 testFileIsSharedFSType(const void *opaque ATTRIBUTE_UNUSED)
320 #ifndef __linux__
321 return EXIT_AM_SKIP;
322 #else
323 const struct testFileIsSharedFSType *data = opaque;
324 char *mtabFile = NULL;
325 bool actual;
326 int ret = -1;
328 if (virAsprintf(&mtabFile, abs_srcdir "/virfiledata/%s", data->mtabFile) < 0)
329 return -1;
331 if (setenv("LIBVIRT_MTAB", mtabFile, 1) < 0) {
332 fprintf(stderr, "Unable to set env variable\n");
333 goto cleanup;
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);
341 goto cleanup;
344 ret = 0;
345 cleanup:
346 VIR_FREE(mtabFile);
347 unsetenv("LIBVIRT_MTAB");
348 return ret;
349 #endif
353 static int
354 mymain(void)
356 int ret = 0;
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[] = {
370 "/etc/aliases"
372 static const char *wantmounts2b[] = {
373 "/etc/aliases.db"
376 # define DO_TEST_MOUNT_SUBTREE(name, path, prefix, mounts, rev) \
377 do { \
378 struct testFileGetMountSubtreeData data = { \
379 path, prefix, mounts, ARRAY_CARDINALITY(mounts), rev \
380 }; \
381 if (virTestRun(name, testFileGetMountSubtree, &data) < 0) \
382 ret = -1; \
383 } while (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) \
392 do { \
393 data1.path = PATH; \
394 data1.expect = EXPECT; \
395 if (virTestRun(virTestCounterNext(), testFileSanitizePath, \
396 &data1) < 0) \
397 ret = -1; \
398 } while (0)
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, ...) \
422 do { \
423 off_t offsets[] = {__VA_ARGS__, -1}; \
424 struct testFileInData data = { \
425 .startData = inData, .offsets = offsets, \
426 }; \
427 if (virTestRun(virTestCounterNext(), testFileInData, &data) < 0) \
428 ret = -1; \
429 } while (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) \
442 do { \
443 struct testFileIsSharedFSType data = { \
444 .mtabFile = mtab, .filename = file, .expected = exp \
445 }; \
446 if (virTestRun(virTestCounterNext(), testFileIsSharedFSType, &data) < 0) \
447 ret = -1; \
448 } while (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;
466 #ifdef __linux__
467 VIR_TEST_MAIN_PRELOAD(mymain, abs_builddir "/.libs/virfilemock.so")
468 #else
469 VIR_TEST_MAIN(mymain)
470 #endif