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 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704
|
/* Test FUSE.
* Copyright (C) 2009-2019 Red Hat Inc.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
/* This used to be a shell script test, but using C gives us finer
* control over exactly which system calls are being used, as well as
* allowing us to avoid one launch of the appliance during the test.
*/
#include <config.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <signal.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <errno.h>
#include <error.h>
#ifdef HAVE_ACL
#include <sys/acl.h>
#include <acl/libacl.h>
#endif
#ifdef HAVE_SYS_XATTR_H
#include <sys/xattr.h>
#endif
#include <guestfs.h>
#include "guestfs-utils.h"
#include "ignore-value.h"
static guestfs_h *g;
#define SIZE INT64_C(1024*1024*1024)
/* NB: Must be a path that does not need quoting. */
static char mountpoint[] = "/tmp/testfuseXXXXXX";
static int acl_available;
static int linuxxattrs_available;
static void create_initial_filesystem (void);
static int test_fuse (void);
int
main (int argc, char *argv[])
{
const char *s;
const char *acl_group[] = { "acl", NULL };
const char *linuxxattrs_group[] = { "linuxxattrs", NULL };
int debug_calls, r, res;
pid_t pid;
struct sigaction sa;
char cmd[128];
/* Allow the test to be skipped. Note I'm using the old shell
* script name here.
*/
s = getenv ("SKIP_TEST_FUSE_SH");
if (s && STRNEQ (s, "")) {
printf ("%s: test skipped because environment variable is set\n",
argv[0]);
exit (77);
}
if (access ("/dev/fuse", W_OK) == -1)
error (77, errno, "access: /dev/fuse");
g = guestfs_create ();
if (g == NULL)
error (EXIT_FAILURE, errno, "guestfs_create");
if (guestfs_add_drive_scratch (g, SIZE, -1) == -1)
exit (EXIT_FAILURE);
if (guestfs_launch (g) == -1)
exit (EXIT_FAILURE);
/* Test features. */
acl_available = guestfs_feature_available (g, (char **) acl_group);
if (acl_available == -1) exit (EXIT_FAILURE);
linuxxattrs_available =
guestfs_feature_available (g, (char **) linuxxattrs_group);
if (linuxxattrs_available == -1) exit (EXIT_FAILURE);
create_initial_filesystem ();
/* Make a mountpoint. */
if (mkdtemp (mountpoint) == NULL)
exit (EXIT_FAILURE);
/* Mount the filesystem on the host using FUSE. */
debug_calls = guestfs_get_trace (g);
if (guestfs_mount_local (g, mountpoint,
GUESTFS_MOUNT_LOCAL_DEBUGCALLS, debug_calls,
-1) == -1)
exit (EXIT_FAILURE);
/* Fork to run the next part of the test. */
pid = fork ();
if (pid == -1)
error (EXIT_FAILURE, errno, "fork");
if (pid == 0) { /* Child. */
/* Move into the mountpoint for the tests. */
if (chdir (mountpoint) == -1) {
perror (mountpoint);
_exit (EXIT_FAILURE);
}
res = test_fuse ();
printf ("test_fuse() returned %d\n", res);
fflush (stdout);
/* Move out of the mountpoint (otherwise our cwd will prevent the
* mountpoint from being unmounted below).
*/
ignore_value (chdir ("/"));
/* Who's using the mountpoint? Should be no one. */
snprintf (cmd, sizeof cmd, "%s %s", FUSER, mountpoint);
printf ("%s\n", cmd);
fflush (stdout);
ignore_value (system (cmd));
/* Unmount it. */
snprintf (cmd, sizeof cmd, "guestunmount %s", mountpoint);
printf ("%s\n", cmd);
fflush (stdout);
r = system (cmd);
if (!WIFEXITED (r) || WEXITSTATUS (r) != EXIT_SUCCESS)
fprintf (stderr, "%s: warning: guestunmount command failed\n", argv[0]);
_exit (res == 0 ? EXIT_SUCCESS : EXIT_FAILURE);
}
/* Parent. */
/* Ignore signals in the parent while running the child. */
memset (&sa, 0, sizeof sa);
sa.sa_handler = SIG_IGN;
sigaction (SIGINT, &sa, NULL);
sigaction (SIGTERM, &sa, NULL);
if (guestfs_mount_local_run (g) == -1)
exit (EXIT_FAILURE);
/* Clean up and exit. */
if (waitpid (pid, &r, 0) == -1)
error (EXIT_FAILURE, errno, "waitpid");
if (rmdir (mountpoint) == -1)
error (EXIT_FAILURE, errno, "rmdir: %s", mountpoint);
if (guestfs_shutdown (g) == -1)
exit (EXIT_FAILURE);
guestfs_close (g);
/* Did the child process fail? */
exit (!WIFEXITED (r) || WEXITSTATUS (r) != 0 ? EXIT_FAILURE : EXIT_SUCCESS);
}
/* Create a filesystem with some initial content. */
static void
create_initial_filesystem (void)
{
if (guestfs_part_disk (g, "/dev/sda", "mbr") == -1)
exit (EXIT_FAILURE);
/* Use ext4 because it supports modern features. Use >= 256 byte
* inodes because these support nanosecond timestamps.
*/
if (guestfs_mkfs_opts (g, "ext4", "/dev/sda1",
GUESTFS_MKFS_OPTS_INODE, 256,
-1) == -1)
exit (EXIT_FAILURE);
if (guestfs_mount_options (g, "acl,user_xattr", "/dev/sda1", "/") == -1)
exit (EXIT_FAILURE);
if (guestfs_write (g, "/hello.txt", "hello", 5) == -1)
exit (EXIT_FAILURE);
if (guestfs_write (g, "/world.txt", "hello world", 11) == -1)
exit (EXIT_FAILURE);
if (guestfs_touch (g, "/empty") == -1)
exit (EXIT_FAILURE);
if (linuxxattrs_available) {
if (guestfs_touch (g, "/user_xattr") == -1)
exit (EXIT_FAILURE);
if (guestfs_setxattr (g, "user.test", "hello123", 8, "/user_xattr") == -1)
exit (EXIT_FAILURE);
}
if (acl_available) {
if (guestfs_touch (g, "/acl") == -1)
exit (EXIT_FAILURE);
if (guestfs_acl_set_file (g, "/acl", "access",
"u::rwx,u:500:r,g::rwx,m::rwx,o::r-x") == -1)
exit (EXIT_FAILURE);
}
}
/* Run FUSE tests. Mountpoint is current directory. */
static int
test_fuse (void)
{
int stage = 0;
#define STAGE(fs,...) \
printf ("%02d: " fs "\n", ++stage, ##__VA_ARGS__); \
fflush (stdout)
FILE *fp;
char *line = NULL;
size_t len = 0;
struct stat statbuf;
char buf[128];
ssize_t r;
unsigned u, u1;
int fd;
struct timeval tv[2];
struct timespec ts[2];
#ifdef HAVE_ACL
acl_t acl;
char *acl_text;
#endif
STAGE ("checking initial files exist");
if (access ("empty", F_OK) == -1) {
perror ("access: empty");
return -1;
}
if (access ("hello.txt", F_OK) == -1) {
perror ("access: hello.txt");
return -1;
}
if (access ("world.txt", F_OK) == -1) {
perror ("access: world.txt");
return -1;
}
STAGE ("checking initial files contain expected content");
fp = fopen ("hello.txt", "r");
if (fp == NULL) {
perror ("open: hello.txt");
fclose (fp);
return -1;
}
if (getline (&line, &len, fp) == -1) {
perror ("getline: hello.txt");
fclose (fp);
return -1;
}
if (STRNEQ (line, "hello")) {
fprintf (stderr, "'hello.txt' does not contain expected content\n");
fclose (fp);
return -1;
}
fclose (fp);
fp = fopen ("world.txt", "r");
if (fp == NULL) {
perror ("open: world.txt");
fclose (fp);
return -1;
}
if (getline (&line, &len, fp) == -1) {
perror ("getline: world.txt");
fclose (fp);
return -1;
}
if (STRNEQ (line, "hello world")) {
fprintf (stderr, "'world.txt' does not contain expected content\n");
fclose (fp);
return -1;
}
fclose (fp);
STAGE ("checking file modes and sizes of initial content");
if (stat ("empty", &statbuf) == -1) {
perror ("stat: empty");
return -1;
}
if ((statbuf.st_mode & 0777) != 0644) {
fprintf (stderr, "'empty' has invalid mode (%o)\n", statbuf.st_mode);
return -1;
}
if (statbuf.st_size != 0) {
fprintf (stderr, "'empty' has invalid size (%d)\n", (int) statbuf.st_size);
return -1;
}
if (stat ("hello.txt", &statbuf) == -1) {
perror ("stat: hello.txt");
return -1;
}
if ((statbuf.st_mode & 0777) != 0644) {
fprintf (stderr, "'hello.txt' has invalid mode (%o)\n", statbuf.st_mode);
return -1;
}
if (statbuf.st_size != 5) {
fprintf (stderr, "'hello.txt' has invalid size (%d)\n",
(int) statbuf.st_size);
return -1;
}
if (stat ("world.txt", &statbuf) == -1) {
perror ("stat: world.txt");
return -1;
}
if ((statbuf.st_mode & 0777) != 0644) {
fprintf (stderr, "'world.txt' has invalid mode (%o)\n", statbuf.st_mode);
return -1;
}
if (statbuf.st_size != 11) {
fprintf (stderr, "'world.txt' has invalid size (%d)\n",
(int) statbuf.st_size);
return -1;
}
STAGE ("checking unlink");
fp = fopen ("new", "w");
if (fp == NULL) {
perror ("open: new");
fclose (fp);
return -1;
}
fclose (fp);
if (unlink ("new") == -1) {
perror ("unlink: new");
return -1;
}
STAGE ("checking symbolic link");
if (symlink ("hello.txt", "symlink") == -1) {
perror ("symlink: hello.txt, symlink");
return -1;
}
if (lstat ("symlink", &statbuf) == -1) {
perror ("lstat: symlink");
return -1;
}
if (!S_ISLNK (statbuf.st_mode)) {
fprintf (stderr, "'symlink' is not a symlink (mode = %o)\n",
statbuf.st_mode);
return -1;
}
STAGE ("checking readlink");
r = readlink ("symlink", buf, sizeof buf);
if (r == -1) {
perror ("readlink: symlink");
return -1;
}
/* readlink return buffer is not \0-terminated, hence: */
if (r != 9 || memcmp (buf, "hello.txt", r) != 0) {
fprintf (stderr, "readlink on 'symlink' returned incorrect result\n");
return -1;
}
STAGE ("checking hard link");
if (stat ("hello.txt", &statbuf) == -1) {
perror ("stat: hello.txt");
return -1;
}
if (statbuf.st_nlink != 1) {
fprintf (stderr, "nlink of 'hello.txt' was %d (expected 1)\n",
(int) statbuf.st_nlink);
return -1;
}
if (link ("hello.txt", "link") == -1) {
perror ("link: hello.txt, link");
return -1;
}
if (stat ("link", &statbuf) == -1) {
perror ("stat: link");
return -1;
}
if (statbuf.st_nlink != 2) {
fprintf (stderr, "nlink of 'link' was %d (expected 2)\n",
(int) statbuf.st_nlink);
return -1;
}
if (stat ("hello.txt", &statbuf) == -1) {
perror ("stat: hello.txt");
return -1;
}
if (statbuf.st_nlink != 2) {
fprintf (stderr, "nlink of 'hello.txt' was %d (expected 2)\n",
(int) statbuf.st_nlink);
return -1;
}
if (unlink ("link") == -1) {
perror ("unlink: link");
return -1;
}
#if 0
/* This fails because of caching. The problem is that the linked file
* ("hello.txt") is cached with a link count of 2. unlink("link")
* invalidates the cache for "link", but _not_ for "hello.txt" which
* still has the now-incorrect cached value. However there's not much
* we can do about this since searching for all linked inodes of a file
* is an O(n) operation.
*/
if (stat ("hello.txt", &statbuf) == -1) {
perror ("stat: hello.txt");
return -1;
}
if (statbuf.st_nlink != 1) {
fprintf (stderr, "nlink of 'hello.txt' was %d (expected 1)\n",
(int) statbuf.st_nlink);
return -1;
}
#endif
STAGE ("checking mkdir");
if (mkdir ("newdir", 0755) == -1) {
perror ("mkdir: newdir");
return -1;
}
STAGE ("checking rmdir");
if (rmdir ("newdir") == -1) {
perror ("rmdir: newdir");
return -1;
}
STAGE ("checking rename");
fp = fopen ("old", "w");
if (fp == NULL) {
perror ("open: old");
return -1;
}
fclose (fp);
if (rename ("old", "new") == -1) {
perror ("rename: old, new");
return -1;
}
if (access ("new", F_OK) == -1) {
perror ("access: new");
return -1;
}
if (access ("old", F_OK) == 0) {
fprintf (stderr, "file 'old' exists after rename\n");
return -1;
}
if (unlink ("new") == -1) {
perror ("unlink: new");
return -1;
}
STAGE ("checking chmod");
fp = fopen ("new", "w");
if (fp == NULL) {
perror ("open: new");
return -1;
}
fclose (fp);
for (u = 0; u < 0777; u += 0111) {
if (chmod ("new", u) == -1) {
perror ("chmod: new");
return -1;
}
if (stat ("new", &statbuf) == -1) {
perror ("stat: new");
return -1;
}
if ((statbuf.st_mode & 0777) != u) {
fprintf (stderr, "unexpected mode: was %o expected %o\n",
statbuf.st_mode, u);
return -1;
}
}
if (unlink ("new") == -1) {
perror ("unlink: new");
return -1;
}
STAGE ("checking truncate");
fp = fopen ("truncated", "w");
if (fp == NULL) {
perror ("open: truncated");
return -1;
}
fclose (fp);
for (u = 10000; u <= 10000; u -= 1000) {
if (truncate ("truncated", u) == -1) {
perror ("truncate");
return -1;
}
if (stat ("truncated", &statbuf) == -1) {
perror ("stat: truncated");
return -1;
}
if (statbuf.st_size != u) {
fprintf (stderr, "unexpected size: was %u expected %u\n",
(unsigned) statbuf.st_size, u);
return -1;
}
}
if (unlink ("truncated") == -1) {
perror ("unlink: truncated");
return -1;
}
STAGE ("checking utimes");
fd = open ("timestamp", O_WRONLY|O_CREAT|O_TRUNC|O_NOCTTY|O_CLOEXEC, 0644);
if (fd == -1) {
perror ("open: timestamp");
return -1;
}
close (fd);
tv[0].tv_sec = 23; /* atime */
tv[0].tv_usec = 45;
tv[1].tv_sec = 67; /* mtime */
tv[1].tv_usec = 89;
if (utimes ("timestamp", tv) == -1) {
perror ("utimes: timestamp");
return -1;
}
if (stat ("timestamp", &statbuf) == -1) {
perror ("fstat: timestamp");
return -1;
}
if (statbuf.st_atime != 23 ||
statbuf.st_atim.tv_nsec != 45000 ||
statbuf.st_mtime != 67 ||
statbuf.st_mtim.tv_nsec != 89000) {
fprintf (stderr, "utimes did not set time (%d/%d/%d/%d)\n",
(int) statbuf.st_atime, (int) statbuf.st_atim.tv_nsec,
(int) statbuf.st_mtime, (int) statbuf.st_mtim.tv_nsec);
return -1;
}
STAGE ("checking utimens");
fd = open ("timestamp", O_WRONLY|O_CREAT|O_TRUNC|O_NOCTTY|O_CLOEXEC, 0644);
if (fd == -1) {
perror ("open: timestamp");
return -1;
}
ts[0].tv_sec = 12; /* atime */
ts[0].tv_nsec = 34;
ts[1].tv_sec = 56; /* mtime */
ts[1].tv_nsec = 78;
if (futimens (fd, ts) == -1) {
perror ("futimens: timestamp");
close (fd);
return -1;
}
if (fstat (fd, &statbuf) == -1) {
perror ("fstat: timestamp");
close (fd);
return -1;
}
if (statbuf.st_atime != 12 ||
statbuf.st_atim.tv_nsec != 34 ||
statbuf.st_mtime != 56 ||
statbuf.st_mtim.tv_nsec != 78) {
fprintf (stderr, "utimens did not set time (%d/%d/%d/%d)\n",
(int) statbuf.st_atime, (int) statbuf.st_atim.tv_nsec,
(int) statbuf.st_mtime, (int) statbuf.st_mtim.tv_nsec);
close (fd);
return -1;
}
close (fd);
STAGE ("checking writes");
fp = fopen ("new.txt", "w");
if (fp == NULL) {
perror ("open: new.txt");
fclose (fp);
return -1;
}
for (u = 0; u < 1000; ++u) {
if (fprintf (fp, "line %u\n", u) == -1) {
perror ("fprintf: new.txt");
fclose (fp);
return -1;
}
}
if (fclose (fp) == -1) {
perror ("fclose: new.txt");
return -1;
}
fp = fopen ("new.txt", "r");
if (fp == NULL) {
perror ("open: new.txt");
return -1;
}
for (u = 0; u < 1000; ++u) {
if (getline (&line, &len, fp) == -1) {
perror ("getline: new.txt");
fclose (fp);
return -1;
}
if (sscanf (line, "line %u", &u1) != 1 || u != u1) {
fprintf (stderr, "unexpected content: %s\n", line);
fclose (fp);
return -1;
}
}
fclose (fp);
#ifdef HAVE_ACL
if (acl_available) {
STAGE ("checking POSIX ACL read operation");
acl = acl_get_file ("acl", ACL_TYPE_ACCESS);
if (acl == (acl_t) NULL) {
perror ("acl_get_file: acl");
return -1;
}
acl_text = acl_to_any_text (acl, NULL, '\n', TEXT_SOME_EFFECTIVE | TEXT_NUMERIC_IDS);
if (acl_text == NULL) {
perror ("acl_to_any_text: acl");
return -1;
}
if (STRNEQ (acl_text, "user::rwx\nuser:500:r--\ngroup::rwx\nmask::rwx\nother::r-x")) {
fprintf (stderr, "unexpected acl: %s\n", acl_text);
return -1;
}
acl_free (acl_text);
acl_free (acl);
}
#endif
#if HAVE_GETXATTR
if (linuxxattrs_available) {
STAGE ("checking extended attribute (xattr) read operation");
r = getxattr ("user_xattr", "user.test", buf, sizeof buf);
if (r == -1) {
perror ("getxattr");
return -1;
}
if (r != 8 || memcmp (buf, "hello123", r) != 0) {
fprintf (stderr, "user.test xattr on file user_xattr was incorrect\n");
return -1;
}
}
#endif
/* XXX:
These ones are not yet tested by the current program:
- statfs/statvfs
These ones cannot easily be tested by the current program, because
this program doesn't run as root:
- fsync
- chown
- mknod
*/
free (line);
return 0;
}
|