X-Git-Url: http://git.annexia.org/?p=libguestfs.git;a=blobdiff_plain;f=tests.c;h=5f05e6bab53b36ea169a3d0b48dfca9bbe4f5bf4;hp=b3d2f49e80a7ffc8183bd70d3691167ddd3123f0;hb=ecee63de60a2419abf54eb1967dab04643234ed6;hpb=b348eacbc4d84337856cf7cca518d61c63e92631 diff --git a/tests.c b/tests.c index b3d2f49..5f05e6b 100644 --- a/tests.c +++ b/tests.c @@ -1064,7 +1064,7 @@ static int test_umount_1 (void) static int test_write_file_0 (void) { - /* InitEmpty for write_file (0) */ + /* InitBasicFS for write_file (0): create ext2 on /dev/sda1 */ { int r; suppress_error = 0; @@ -1079,7 +1079,6 @@ static int test_write_file_0 (void) if (r == -1) return -1; } - /* TestOutput for write_file (0) */ { char *lines[] = { ",", @@ -1105,6 +1104,7 @@ static int test_write_file_0 (void) if (r == -1) return -1; } + /* TestOutput for write_file (0) */ { int r; suppress_error = 0; @@ -1127,6 +1127,331 @@ static int test_write_file_0 (void) return 0; } +static int test_write_file_1 (void) +{ + /* InitBasicFS for write_file (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 write_file (1) */ + { + int r; + suppress_error = 0; + r = guestfs_write_file (g, "/new", "\nnew file contents\n", 0); + if (r == -1) + return -1; + } + { + char *r; + suppress_error = 0; + r = guestfs_cat (g, "/new"); + if (r == NULL) + return -1; + if (strcmp (r, "\nnew file contents\n") != 0) { + fprintf (stderr, "test_write_file_1: expected \"\nnew file contents\n\" but got \"%s\"\n", r); + return -1; + } + free (r); + } + return 0; +} + +static int test_write_file_2 (void) +{ + /* InitBasicFS for write_file (2): 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 write_file (2) */ + { + int r; + suppress_error = 0; + r = guestfs_write_file (g, "/new", "\n\n", 0); + if (r == -1) + return -1; + } + { + char *r; + suppress_error = 0; + r = guestfs_cat (g, "/new"); + if (r == NULL) + return -1; + if (strcmp (r, "\n\n") != 0) { + fprintf (stderr, "test_write_file_2: expected \"\n\n\" but got \"%s\"\n", r); + return -1; + } + free (r); + } + return 0; +} + +static int test_write_file_3 (void) +{ + /* InitBasicFS for write_file (3): 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 write_file (3) */ + { + int r; + suppress_error = 0; + r = guestfs_write_file (g, "/new", "", 0); + if (r == -1) + return -1; + } + { + char *r; + suppress_error = 0; + r = guestfs_cat (g, "/new"); + if (r == NULL) + return -1; + if (strcmp (r, "") != 0) { + fprintf (stderr, "test_write_file_3: expected \"\" but got \"%s\"\n", r); + return -1; + } + free (r); + } + return 0; +} + +static int test_write_file_4 (void) +{ + /* InitBasicFS for write_file (4): 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 write_file (4) */ + { + int r; + suppress_error = 0; + r = guestfs_write_file (g, "/new", "\n\n\n", 0); + if (r == -1) + return -1; + } + { + char *r; + suppress_error = 0; + r = guestfs_cat (g, "/new"); + if (r == NULL) + return -1; + if (strcmp (r, "\n\n\n") != 0) { + fprintf (stderr, "test_write_file_4: expected \"\n\n\n\" but got \"%s\"\n", r); + return -1; + } + free (r); + } + return 0; +} + +static int test_write_file_5 (void) +{ + /* InitBasicFS for write_file (5): 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 write_file (5) */ + { + int r; + suppress_error = 0; + r = guestfs_write_file (g, "/new", "\n", 0); + if (r == -1) + return -1; + } + { + char *r; + suppress_error = 0; + r = guestfs_cat (g, "/new"); + if (r == NULL) + return -1; + if (strcmp (r, "\n") != 0) { + fprintf (stderr, "test_write_file_5: expected \"\n\" but got \"%s\"\n", r); + return -1; + } + free (r); + } + return 0; +} + static int test_mkfs_0 (void) { /* InitEmpty for mkfs (0) */ @@ -4174,7 +4499,7 @@ int main (int argc, char *argv[]) exit (1); } - nr_tests = 58; + nr_tests = 63; test_num++; printf ("%3d/%3d test_blockdev_rereadpt_0\n", test_num, nr_tests); @@ -4297,6 +4622,36 @@ int main (int argc, char *argv[]) failed++; } test_num++; + printf ("%3d/%3d test_write_file_1\n", test_num, nr_tests); + if (test_write_file_1 () == -1) { + printf ("test_write_file_1 FAILED\n"); + failed++; + } + test_num++; + printf ("%3d/%3d test_write_file_2\n", test_num, nr_tests); + if (test_write_file_2 () == -1) { + printf ("test_write_file_2 FAILED\n"); + failed++; + } + test_num++; + printf ("%3d/%3d test_write_file_3\n", test_num, nr_tests); + if (test_write_file_3 () == -1) { + printf ("test_write_file_3 FAILED\n"); + failed++; + } + test_num++; + printf ("%3d/%3d test_write_file_4\n", test_num, nr_tests); + if (test_write_file_4 () == -1) { + printf ("test_write_file_4 FAILED\n"); + failed++; + } + test_num++; + printf ("%3d/%3d test_write_file_5\n", test_num, nr_tests); + if (test_write_file_5 () == -1) { + printf ("test_write_file_5 FAILED\n"); + failed++; + } + test_num++; printf ("%3d/%3d test_mkfs_0\n", test_num, nr_tests); if (test_mkfs_0 () == -1) { printf ("test_mkfs_0 FAILED\n");