summaryrefslogtreecommitdiffstats
path: root/capitests
diff options
context:
space:
mode:
authorRichard W.M. Jones <rjones@redhat.com>2010-11-03 17:24:50 +0000
committerRichard W.M. Jones <rjones@redhat.com>2010-11-03 18:49:00 +0000
commit9cd1810b5e5b0a09f1a6eaf64fa14b924fbb0eae (patch)
tree0f7f25a478e1d71b6bedcbf05936d8d152861acd /capitests
parent39052d270fcce991238fc5f9939677b5d6e31d15 (diff)
downloadlibguestfs-9cd1810b5e5b0a09f1a6eaf64fa14b924fbb0eae.tar.gz
libguestfs-9cd1810b5e5b0a09f1a6eaf64fa14b924fbb0eae.tar.xz
libguestfs-9cd1810b5e5b0a09f1a6eaf64fa14b924fbb0eae.zip
capitests: Test guestfs_last_errno call.
Diffstat (limited to 'capitests')
-rw-r--r--capitests/Makefile.am13
-rw-r--r--capitests/test-last-errno.c142
2 files changed, 153 insertions, 2 deletions
diff --git a/capitests/Makefile.am b/capitests/Makefile.am
index 435b1ee7..1dded1e0 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 00000000..31a5c4de
--- /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 <config.h>
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+#include <fcntl.h>
+#include <errno.h>
+
+#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);
+}