X-Git-Url: http://git.annexia.org/?p=libguestfs.git;a=blobdiff_plain;f=generator%2Fgenerator_capitests.ml;h=190e10f7e387e87164b6690ee51b01454c92ff4e;hp=f6c71be7c87fdb3278b95310e5361ad9d467762f;hb=fab75c0337d7897d10ea5e95e47ac05eab81ace9;hpb=04d8209077d2227eb1d42695ba71147f78987050 diff --git a/generator/generator_capitests.ml b/generator/generator_capitests.ml index f6c71be..190e10f 100644 --- a/generator/generator_capitests.ml +++ b/generator/generator_capitests.ml @@ -91,6 +91,28 @@ incr (guestfs_h *g, void *iv) (*i)++; } +/* Get md5sum of the named file. */ +static void +md5sum (const char *filename, char *result) +{ + char cmd[256]; + snprintf (cmd, sizeof cmd, \"md5sum %%s\", filename); + FILE *pp = popen (cmd, \"r\"); + if (pp == NULL) { + perror (cmd); + exit (EXIT_FAILURE); + } + if (fread (result, 1, 32, pp) != 32) { + perror (\"md5sum: fread\"); + exit (EXIT_FAILURE); + } + if (pclose (pp) == -1) { + perror (\"pclose\"); + exit (EXIT_FAILURE); + } + result[32] = '\\0'; +} + "; (* Generate a list of commands which are not tested anywhere. *) @@ -153,22 +175,14 @@ int main (int argc, char *argv[]) guestfs_set_error_handler (g, print_error, NULL); - guestfs_set_path (g, \"../appliance\"); - filename = \"test1.img\"; - fd = open (filename, O_WRONLY|O_CREAT|O_NOCTTY|O_NONBLOCK|O_TRUNC, 0666); + fd = open (filename, O_WRONLY|O_CREAT|O_NOCTTY|O_TRUNC, 0666); if (fd == -1) { perror (filename); exit (EXIT_FAILURE); } - if (lseek (fd, %d, SEEK_SET) == -1) { - perror (\"lseek\"); - close (fd); - unlink (filename); - exit (EXIT_FAILURE); - } - if (write (fd, &c, 1) == -1) { - perror (\"write\"); + if (ftruncate (fd, %d) == -1) { + perror (\"ftruncate\"); close (fd); unlink (filename); exit (EXIT_FAILURE); @@ -184,19 +198,13 @@ int main (int argc, char *argv[]) } filename = \"test2.img\"; - fd = open (filename, O_WRONLY|O_CREAT|O_NOCTTY|O_NONBLOCK|O_TRUNC, 0666); + fd = open (filename, O_WRONLY|O_CREAT|O_NOCTTY|O_TRUNC, 0666); if (fd == -1) { perror (filename); exit (EXIT_FAILURE); } - if (lseek (fd, %d, SEEK_SET) == -1) { - perror (\"lseek\"); - close (fd); - unlink (filename); - exit (EXIT_FAILURE); - } - if (write (fd, &c, 1) == -1) { - perror (\"write\"); + if (ftruncate (fd, %d) == -1) { + perror (\"ftruncate\"); close (fd); unlink (filename); exit (EXIT_FAILURE); @@ -212,19 +220,13 @@ int main (int argc, char *argv[]) } filename = \"test3.img\"; - fd = open (filename, O_WRONLY|O_CREAT|O_NOCTTY|O_NONBLOCK|O_TRUNC, 0666); + fd = open (filename, O_WRONLY|O_CREAT|O_NOCTTY|O_TRUNC, 0666); if (fd == -1) { perror (filename); exit (EXIT_FAILURE); } - if (lseek (fd, %d, SEEK_SET) == -1) { - perror (\"lseek\"); - close (fd); - unlink (filename); - exit (EXIT_FAILURE); - } - if (write (fd, &c, 1) == -1) { - perror (\"write\"); + if (ftruncate (fd, %d) == -1) { + perror (\"ftruncate\"); close (fd); unlink (filename); exit (EXIT_FAILURE); @@ -650,6 +652,32 @@ and generate_one_test_body name i test_name init test = in List.iter (generate_test_command_call test_name) seq; generate_test_command_call ~test test_name last + | TestOutputFileMD5 (seq, filename) -> + pr " /* TestOutputFileMD5 for %s (%d) */\n" name i; + pr " char expected[33];\n"; + pr " md5sum (\"%s\", expected);\n" filename; + let seq, last = get_seq_last seq in + let test () = + pr " if (STRNEQ (r, expected)) {\n"; + pr " fprintf (stderr, \"%s: expected \\\"%%s\\\" but got \\\"%%s\\\"\\n\", expected, r);\n" test_name; + pr " return -1;\n"; + pr " }\n" + in + List.iter (generate_test_command_call test_name) seq; + generate_test_command_call ~test test_name last + | TestOutputDevice (seq, expected) -> + pr " /* TestOutputDevice for %s (%d) */\n" name i; + pr " const char *expected = \"%s\";\n" (c_quote expected); + let seq, last = get_seq_last seq in + let test () = + pr " r[5] = 's';\n"; + pr " if (STRNEQ (r, expected)) {\n"; + pr " fprintf (stderr, \"%s: expected \\\"%%s\\\" but got \\\"%%s\\\"\\n\", expected, r);\n" test_name; + pr " return -1;\n"; + pr " }\n" + in + List.iter (generate_test_command_call test_name) seq; + generate_test_command_call ~test test_name last | TestLastFail seq -> pr " /* TestLastFail for %s (%d) */\n" name i; let seq, last = get_seq_last seq in @@ -672,6 +700,13 @@ and generate_test_command_call ?(expect_error = false) ?test test_name cmd = with Not_found -> failwithf "%s: in test, command %s was not found" test_name name in + (* If the call has optional args, fold them all together. We cannot + * test partial optional args yet. + *) + let style = + let ret, args, optargs = style in + ret, args@optargs in + if List.length (snd style) <> List.length args then failwithf "%s: in test, wrong number of args given to %s" test_name name; @@ -709,6 +744,9 @@ and generate_test_command_call ?(expect_error = false) ?test test_name cmd = ) strs; pr " NULL\n"; pr " };\n"; + | Pointer _, _ -> + (* Difficult to make these pointers in order to run a test. *) + assert false ) (List.combine (snd style) args); let error_code = @@ -764,6 +802,7 @@ and generate_test_command_call ?(expect_error = false) ?test test_name cmd = pr ", %Ld" i | Bool _, arg -> let b = bool_of_string arg in pr ", %d" (if b then 1 else 0) + | Pointer _, _ -> assert false ) (List.combine (snd style) args); (match fst style with