X-Git-Url: http://git.annexia.org/?a=blobdiff_plain;f=tests.c;h=9c102cdf536b9e482a6ca4156b2df20eb3acabe3;hb=6fd5bc84893c600af75ef64fa9f9a043b665c76a;hp=9744575f9e4bcaddb4d9cc66f736592135cec813;hpb=24ccbb29ac475187f51a27dcd318db2b4824a0c1;p=libguestfs.git diff --git a/tests.c b/tests.c index 9744575..9c102cd 100644 --- a/tests.c +++ b/tests.c @@ -63,6 +63,8 @@ static void no_test_warnings (void) fprintf (stderr, "warning: \"guestfs_add_drive\" has no tests\n"); fprintf (stderr, "warning: \"guestfs_add_cdrom\" has no tests\n"); fprintf (stderr, "warning: \"guestfs_config\" has no tests\n"); + fprintf (stderr, "warning: \"guestfs_set_qemu\" has no tests\n"); + fprintf (stderr, "warning: \"guestfs_get_qemu\" has no tests\n"); fprintf (stderr, "warning: \"guestfs_set_path\" has no tests\n"); fprintf (stderr, "warning: \"guestfs_get_path\" has no tests\n"); fprintf (stderr, "warning: \"guestfs_set_autosync\" has no tests\n"); @@ -101,8 +103,286 @@ static void no_test_warnings (void) fprintf (stderr, "warning: \"guestfs_command_lines\" has no tests\n"); fprintf (stderr, "warning: \"guestfs_tune2fs_l\" has no tests\n"); fprintf (stderr, "warning: \"guestfs_blockdev_setbsz\" has no tests\n"); - fprintf (stderr, "warning: \"guestfs_upload\" has no tests\n"); - fprintf (stderr, "warning: \"guestfs_download\" has no tests\n"); + fprintf (stderr, "warning: \"guestfs_tar_out\" has no tests\n"); + fprintf (stderr, "warning: \"guestfs_tgz_out\" has no tests\n"); + fprintf (stderr, "warning: \"guestfs_mount_options\" has no tests\n"); + fprintf (stderr, "warning: \"guestfs_mount_vfs\" has no tests\n"); +} + +static int test_mount_ro_0 (void) +{ + /* InitBasicFS for mount_ro (0): create ext2 on /dev/sda1 */ + { + int r; + suppress_error = 0; + r = guestfs_umount_all (g); + if (r == -1) + return -1; + } + { + int r; + suppress_error = 0; + r = guestfs_lvm_remove_all (g); + if (r == -1) + return -1; + } + { + char *lines[] = { + ",", + NULL + }; + int r; + suppress_error = 0; + r = guestfs_sfdisk (g, "/dev/sda", 0, 0, 0, lines); + if (r == -1) + return -1; + } + { + int r; + suppress_error = 0; + r = guestfs_mkfs (g, "ext2", "/dev/sda1"); + if (r == -1) + return -1; + } + { + int r; + suppress_error = 0; + r = guestfs_mount (g, "/dev/sda1", "/"); + if (r == -1) + return -1; + } + /* TestLastFail for mount_ro (0) */ + { + int r; + suppress_error = 0; + r = guestfs_umount (g, "/"); + if (r == -1) + return -1; + } + { + int r; + suppress_error = 0; + r = guestfs_mount_ro (g, "/dev/sda1", "/"); + if (r == -1) + return -1; + } + { + int r; + suppress_error = 1; + r = guestfs_touch (g, "/new"); + if (r != -1) + return -1; + } + return 0; +} + +static int test_mount_ro_1 (void) +{ + /* InitBasicFS for mount_ro (1): create ext2 on /dev/sda1 */ + { + int r; + suppress_error = 0; + r = guestfs_umount_all (g); + if (r == -1) + return -1; + } + { + int r; + suppress_error = 0; + r = guestfs_lvm_remove_all (g); + if (r == -1) + return -1; + } + { + char *lines[] = { + ",", + NULL + }; + int r; + suppress_error = 0; + r = guestfs_sfdisk (g, "/dev/sda", 0, 0, 0, lines); + if (r == -1) + return -1; + } + { + int r; + suppress_error = 0; + r = guestfs_mkfs (g, "ext2", "/dev/sda1"); + if (r == -1) + return -1; + } + { + int r; + suppress_error = 0; + r = guestfs_mount (g, "/dev/sda1", "/"); + if (r == -1) + return -1; + } + /* TestOutput for mount_ro (1) */ + { + int r; + suppress_error = 0; + r = guestfs_write_file (g, "/new", "data", 0); + if (r == -1) + return -1; + } + { + int r; + suppress_error = 0; + r = guestfs_umount (g, "/"); + if (r == -1) + return -1; + } + { + int r; + suppress_error = 0; + r = guestfs_mount_ro (g, "/dev/sda1", "/"); + if (r == -1) + return -1; + } + { + char *r; + suppress_error = 0; + r = guestfs_cat (g, "/new"); + if (r == NULL) + return -1; + if (strcmp (r, "data") != 0) { + fprintf (stderr, "test_mount_ro_1: expected \"data\" but got \"%s\"\n", r); + return -1; + } + free (r); + } + return 0; +} + +static int test_tgz_in_0 (void) +{ + /* InitBasicFS for tgz_in (0): create ext2 on /dev/sda1 */ + { + int r; + suppress_error = 0; + r = guestfs_umount_all (g); + if (r == -1) + return -1; + } + { + int r; + suppress_error = 0; + r = guestfs_lvm_remove_all (g); + if (r == -1) + return -1; + } + { + char *lines[] = { + ",", + NULL + }; + int r; + suppress_error = 0; + r = guestfs_sfdisk (g, "/dev/sda", 0, 0, 0, lines); + if (r == -1) + return -1; + } + { + int r; + suppress_error = 0; + r = guestfs_mkfs (g, "ext2", "/dev/sda1"); + if (r == -1) + return -1; + } + { + int r; + suppress_error = 0; + r = guestfs_mount (g, "/dev/sda1", "/"); + if (r == -1) + return -1; + } + /* TestOutput for tgz_in (0) */ + { + int r; + suppress_error = 0; + r = guestfs_tgz_in (g, "images/helloworld.tar.gz", "/"); + if (r == -1) + return -1; + } + { + char *r; + suppress_error = 0; + r = guestfs_cat (g, "/hello"); + if (r == NULL) + return -1; + if (strcmp (r, "hello\n") != 0) { + fprintf (stderr, "test_tgz_in_0: expected \"hello\n\" but got \"%s\"\n", r); + return -1; + } + free (r); + } + return 0; +} + +static int test_tar_in_0 (void) +{ + /* InitBasicFS for tar_in (0): create ext2 on /dev/sda1 */ + { + int r; + suppress_error = 0; + r = guestfs_umount_all (g); + if (r == -1) + return -1; + } + { + int r; + suppress_error = 0; + r = guestfs_lvm_remove_all (g); + if (r == -1) + return -1; + } + { + char *lines[] = { + ",", + NULL + }; + int r; + suppress_error = 0; + r = guestfs_sfdisk (g, "/dev/sda", 0, 0, 0, lines); + if (r == -1) + return -1; + } + { + int r; + suppress_error = 0; + r = guestfs_mkfs (g, "ext2", "/dev/sda1"); + if (r == -1) + return -1; + } + { + int r; + suppress_error = 0; + r = guestfs_mount (g, "/dev/sda1", "/"); + if (r == -1) + return -1; + } + /* TestOutput for tar_in (0) */ + { + int r; + suppress_error = 0; + r = guestfs_tar_in (g, "images/helloworld.tar", "/"); + if (r == -1) + return -1; + } + { + char *r; + suppress_error = 0; + r = guestfs_cat (g, "/hello"); + if (r == NULL) + return -1; + if (strcmp (r, "hello\n") != 0) { + fprintf (stderr, "test_tar_in_0: expected \"hello\n\" but got \"%s\"\n", r); + return -1; + } + free (r); + } + return 0; } static int test_checksum_0 (void) @@ -614,6 +894,150 @@ static int test_checksum_7 (void) return 0; } +static int test_download_0 (void) +{ + /* InitBasicFS for download (0): create ext2 on /dev/sda1 */ + { + int r; + suppress_error = 0; + r = guestfs_umount_all (g); + if (r == -1) + return -1; + } + { + int r; + suppress_error = 0; + r = guestfs_lvm_remove_all (g); + if (r == -1) + return -1; + } + { + char *lines[] = { + ",", + NULL + }; + int r; + suppress_error = 0; + r = guestfs_sfdisk (g, "/dev/sda", 0, 0, 0, lines); + if (r == -1) + return -1; + } + { + int r; + suppress_error = 0; + r = guestfs_mkfs (g, "ext2", "/dev/sda1"); + if (r == -1) + return -1; + } + { + int r; + suppress_error = 0; + r = guestfs_mount (g, "/dev/sda1", "/"); + if (r == -1) + return -1; + } + /* TestOutput for download (0) */ + { + int r; + suppress_error = 0; + r = guestfs_upload (g, "COPYING.LIB", "/COPYING.LIB"); + if (r == -1) + return -1; + } + { + int r; + suppress_error = 0; + r = guestfs_download (g, "/COPYING.LIB", "testdownload.tmp"); + if (r == -1) + return -1; + } + { + int r; + suppress_error = 0; + r = guestfs_upload (g, "testdownload.tmp", "/upload"); + if (r == -1) + return -1; + } + { + char *r; + suppress_error = 0; + r = guestfs_checksum (g, "md5", "/upload"); + if (r == NULL) + return -1; + if (strcmp (r, "e3eda01d9815f8d24aae2dbd89b68b06") != 0) { + fprintf (stderr, "test_download_0: expected \"e3eda01d9815f8d24aae2dbd89b68b06\" but got \"%s\"\n", r); + return -1; + } + free (r); + } + return 0; +} + +static int test_upload_0 (void) +{ + /* InitBasicFS for upload (0): create ext2 on /dev/sda1 */ + { + int r; + suppress_error = 0; + r = guestfs_umount_all (g); + if (r == -1) + return -1; + } + { + int r; + suppress_error = 0; + r = guestfs_lvm_remove_all (g); + if (r == -1) + return -1; + } + { + char *lines[] = { + ",", + NULL + }; + int r; + suppress_error = 0; + r = guestfs_sfdisk (g, "/dev/sda", 0, 0, 0, lines); + if (r == -1) + return -1; + } + { + int r; + suppress_error = 0; + r = guestfs_mkfs (g, "ext2", "/dev/sda1"); + if (r == -1) + return -1; + } + { + int r; + suppress_error = 0; + r = guestfs_mount (g, "/dev/sda1", "/"); + if (r == -1) + return -1; + } + /* TestOutput for upload (0) */ + { + int r; + suppress_error = 0; + r = guestfs_upload (g, "COPYING.LIB", "/COPYING.LIB"); + if (r == -1) + return -1; + } + { + char *r; + suppress_error = 0; + r = guestfs_checksum (g, "md5", "/COPYING.LIB"); + if (r == NULL) + return -1; + if (strcmp (r, "e3eda01d9815f8d24aae2dbd89b68b06") != 0) { + fprintf (stderr, "test_upload_0: expected \"e3eda01d9815f8d24aae2dbd89b68b06\" but got \"%s\"\n", r); + return -1; + } + free (r); + } + return 0; +} + static int test_blockdev_rereadpt_0 (void) { /* InitEmpty for blockdev_rereadpt (0) */ @@ -4906,8 +5330,8 @@ int main (int argc, char *argv[]) char c = 0; int failed = 0; const char *srcdir; + const char *filename; int fd; - char buf[256]; int nr_tests, test_num = 0; no_test_warnings (); @@ -4922,89 +5346,90 @@ int main (int argc, char *argv[]) srcdir = getenv ("srcdir"); if (!srcdir) srcdir = "."; - guestfs_set_path (g, srcdir); + chdir (srcdir); + guestfs_set_path (g, "."); - snprintf (buf, sizeof buf, "%s/test1.img", srcdir); - fd = open (buf, O_WRONLY|O_CREAT|O_NOCTTY|O_NONBLOCK|O_TRUNC, 0666); + filename = "test1.img"; + fd = open (filename, O_WRONLY|O_CREAT|O_NOCTTY|O_NONBLOCK|O_TRUNC, 0666); if (fd == -1) { - perror (buf); + perror (filename); exit (1); } if (lseek (fd, 524288000, SEEK_SET) == -1) { perror ("lseek"); close (fd); - unlink (buf); + unlink (filename); exit (1); } if (write (fd, &c, 1) == -1) { perror ("write"); close (fd); - unlink (buf); + unlink (filename); exit (1); } if (close (fd) == -1) { - perror (buf); - unlink (buf); + perror (filename); + unlink (filename); exit (1); } - if (guestfs_add_drive (g, buf) == -1) { - printf ("guestfs_add_drive %s FAILED\n", buf); + if (guestfs_add_drive (g, filename) == -1) { + printf ("guestfs_add_drive %s FAILED\n", filename); exit (1); } - snprintf (buf, sizeof buf, "%s/test2.img", srcdir); - fd = open (buf, O_WRONLY|O_CREAT|O_NOCTTY|O_NONBLOCK|O_TRUNC, 0666); + filename = "test2.img"; + fd = open (filename, O_WRONLY|O_CREAT|O_NOCTTY|O_NONBLOCK|O_TRUNC, 0666); if (fd == -1) { - perror (buf); + perror (filename); exit (1); } if (lseek (fd, 52428800, SEEK_SET) == -1) { perror ("lseek"); close (fd); - unlink (buf); + unlink (filename); exit (1); } if (write (fd, &c, 1) == -1) { perror ("write"); close (fd); - unlink (buf); + unlink (filename); exit (1); } if (close (fd) == -1) { - perror (buf); - unlink (buf); + perror (filename); + unlink (filename); exit (1); } - if (guestfs_add_drive (g, buf) == -1) { - printf ("guestfs_add_drive %s FAILED\n", buf); + if (guestfs_add_drive (g, filename) == -1) { + printf ("guestfs_add_drive %s FAILED\n", filename); exit (1); } - snprintf (buf, sizeof buf, "%s/test3.img", srcdir); - fd = open (buf, O_WRONLY|O_CREAT|O_NOCTTY|O_NONBLOCK|O_TRUNC, 0666); + filename = "test3.img"; + fd = open (filename, O_WRONLY|O_CREAT|O_NOCTTY|O_NONBLOCK|O_TRUNC, 0666); if (fd == -1) { - perror (buf); + perror (filename); exit (1); } if (lseek (fd, 10485760, SEEK_SET) == -1) { perror ("lseek"); close (fd); - unlink (buf); + unlink (filename); exit (1); } if (write (fd, &c, 1) == -1) { perror ("write"); close (fd); - unlink (buf); + unlink (filename); exit (1); } if (close (fd) == -1) { - perror (buf); - unlink (buf); + perror (filename); + unlink (filename); exit (1); } - if (guestfs_add_drive (g, buf) == -1) { - printf ("guestfs_add_drive %s FAILED\n", buf); + if (guestfs_add_drive (g, filename) == -1) { + printf ("guestfs_add_drive %s FAILED\n", filename); exit (1); } @@ -5017,9 +5442,33 @@ int main (int argc, char *argv[]) exit (1); } - nr_tests = 71; + nr_tests = 77; test_num++; + printf ("%3d/%3d test_mount_ro_0\n", test_num, nr_tests); + if (test_mount_ro_0 () == -1) { + printf ("test_mount_ro_0 FAILED\n"); + failed++; + } + test_num++; + printf ("%3d/%3d test_mount_ro_1\n", test_num, nr_tests); + if (test_mount_ro_1 () == -1) { + printf ("test_mount_ro_1 FAILED\n"); + failed++; + } + test_num++; + printf ("%3d/%3d test_tgz_in_0\n", test_num, nr_tests); + if (test_tgz_in_0 () == -1) { + printf ("test_tgz_in_0 FAILED\n"); + failed++; + } + test_num++; + printf ("%3d/%3d test_tar_in_0\n", test_num, nr_tests); + if (test_tar_in_0 () == -1) { + printf ("test_tar_in_0 FAILED\n"); + failed++; + } + test_num++; printf ("%3d/%3d test_checksum_0\n", test_num, nr_tests); if (test_checksum_0 () == -1) { printf ("test_checksum_0 FAILED\n"); @@ -5068,6 +5517,18 @@ int main (int argc, char *argv[]) failed++; } test_num++; + printf ("%3d/%3d test_download_0\n", test_num, nr_tests); + if (test_download_0 () == -1) { + printf ("test_download_0 FAILED\n"); + failed++; + } + test_num++; + printf ("%3d/%3d test_upload_0\n", test_num, nr_tests); + if (test_upload_0 () == -1) { + printf ("test_upload_0 FAILED\n"); + failed++; + } + test_num++; printf ("%3d/%3d test_blockdev_rereadpt_0\n", test_num, nr_tests); if (test_blockdev_rereadpt_0 () == -1) { printf ("test_blockdev_rereadpt_0 FAILED\n"); @@ -5447,12 +5908,9 @@ int main (int argc, char *argv[]) } guestfs_close (g); - snprintf (buf, sizeof buf, "%s/test1.img", srcdir); - unlink (buf); - snprintf (buf, sizeof buf, "%s/test2.img", srcdir); - unlink (buf); - snprintf (buf, sizeof buf, "%s/test3.img", srcdir); - unlink (buf); + unlink ("test1.img"); + unlink ("test2.img"); + unlink ("test3.img"); if (failed > 0) { printf ("***** %d / %d tests FAILED *****\n", failed, nr_tests);