From 9cd1810b5e5b0a09f1a6eaf64fa14b924fbb0eae Mon Sep 17 00:00:00 2001 From: "Richard W.M. Jones" Date: Wed, 3 Nov 2010 17:24:50 +0000 Subject: [PATCH] capitests: Test guestfs_last_errno call. --- .gitignore | 1 + capitests/Makefile.am | 13 +++- capitests/test-last-errno.c | 142 ++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 154 insertions(+), 2 deletions(-) create mode 100644 capitests/test-last-errno.c diff --git a/.gitignore b/.gitignore index 14060ad..89ee54f 100644 --- a/.gitignore +++ b/.gitignore @@ -24,6 +24,7 @@ capitests/test-add-drive-opts capitests/test-command capitests/test-config capitests/test-create-handle +capitests/test-last-errno capitests/test*.img capitests/tests capitests/tests.c diff --git a/capitests/Makefile.am b/capitests/Makefile.am index 435b1ee..1dded1e 100644 --- a/capitests/Makefile.am +++ b/capitests/Makefile.am @@ -28,13 +28,15 @@ check_PROGRAMS = \ test-command \ test-create-handle \ test-config \ - test-add-drive-opts + test-add-drive-opts \ + test-last-errno TESTS = \ tests \ test-create-handle \ test-config \ - test-add-drive-opts + test-add-drive-opts \ + test-last-errno TESTS_ENVIRONMENT = \ SKIP_TEST_COMMAND=$(shell ldd test-command | grep -sq 'not a dynamic executable' || echo 1) \ SKIP_TEST_COMMAND_LINES=$(shell ldd test-command | grep -sq 'not a dynamic executable' || echo 1) \ @@ -76,6 +78,13 @@ test_add_drive_opts_CFLAGS = \ test_add_drive_opts_LDADD = \ $(top_builddir)/src/libguestfs.la +test_last_errno_SOURCES = test-last-errno.c +test_last_errno_CFLAGS = \ + -I$(top_srcdir)/src -I$(top_builddir)/src \ + $(WARN_CFLAGS) $(WERROR_CFLAGS) +test_last_errno_LDADD = \ + $(top_builddir)/src/libguestfs.la + # Run the tests under valgrind. valgrind: diff --git a/capitests/test-last-errno.c b/capitests/test-last-errno.c new file mode 100644 index 0000000..31a5c4d --- /dev/null +++ b/capitests/test-last-errno.c @@ -0,0 +1,142 @@ +/* libguestfs + * Copyright (C) 2010 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. + */ + +/* Test that we can get correct errnos all the way back from the + * appliance, translated to the local operating system. + */ + +#include + +#include +#include +#include +#include +#include +#include + +#include "guestfs.h" + +int +main (int argc, char *argv[]) +{ + guestfs_h *g; + int fd, r, err; + struct guestfs_stat *stat; + const char *filename = "test1.img"; + + g = guestfs_create (); + if (g == NULL) { + fprintf (stderr, "failed to create handle\n"); + exit (EXIT_FAILURE); + } + + fd = open (filename, O_WRONLY|O_CREAT|O_NOCTTY|O_TRUNC, 0666); + if (fd == -1) { + perror (filename); + exit (EXIT_FAILURE); + } + if (ftruncate (fd, 524288000) == -1) { + perror (filename); + close (fd); + unlink (filename); + exit (EXIT_FAILURE); + } + if (close (fd) == -1) { + perror (filename); + unlink (filename); + exit (EXIT_FAILURE); + } + + if (guestfs_add_drive_opts (g, filename, + GUESTFS_ADD_DRIVE_OPTS_FORMAT, "raw", + -1) == -1) + exit (EXIT_FAILURE); + + if (guestfs_launch (g) == -1) + exit (EXIT_FAILURE); + + if (guestfs_part_disk (g, "/dev/sda", "mbr") == -1) + exit (EXIT_FAILURE); + + if (guestfs_mkfs (g, "ext2", "/dev/sda1") == -1) + exit (EXIT_FAILURE); + + /* Mount read-only, and check that errno == EROFS is passed back when + * we create a file. + */ + if (guestfs_mount_ro (g, "/dev/sda1", "/") == -1) + exit (EXIT_FAILURE); + + r = guestfs_touch (g, "/test"); + if (r != -1) { + fprintf (stderr, + "guestfs_touch: expected error for read-only filesystem\n"); + exit (EXIT_FAILURE); + } + + err = guestfs_last_errno (g); + if (err != EROFS) { + fprintf (stderr, + "guestfs_touch: expected errno == EROFS, but got %d\n", err); + exit (EXIT_FAILURE); + } + + if (guestfs_umount (g, "/") == -1) + exit (EXIT_FAILURE); + + /* Mount it writable and test some other errors. */ + if (guestfs_mount_options (g, "", "/dev/sda1", "/") == -1) + exit (EXIT_FAILURE); + + stat = guestfs_lstat (g, "/nosuchfile"); + if (stat != NULL) { + fprintf (stderr, + "guestfs_lstat: expected error for missing file\n"); + exit (EXIT_FAILURE); + } + + err = guestfs_last_errno (g); + if (err != ENOENT) { + fprintf (stderr, + "guestfs_lstat: expected errno == ENOENT, but got %d\n", err); + exit (EXIT_FAILURE); + } + + if (guestfs_touch (g, "/test") == -1) + exit (EXIT_FAILURE); + + r = guestfs_mkdir (g, "/test"); + if (r != -1) { + fprintf (stderr, + "guestfs_mkdir: expected error for file which exists\n"); + exit (EXIT_FAILURE); + } + + err = guestfs_last_errno (g); + if (err != EEXIST) { + fprintf (stderr, + "guestfs_mkdir: expected errno == EEXIST, but got %d\n", err); + exit (EXIT_FAILURE); + } + + guestfs_close (g); + + unlink (filename); + + exit (EXIT_SUCCESS); +} -- 1.8.3.1