2 * Copyright (C) 2009-2010 Red Hat Inc.
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 (* Please read generator/README first. *)
26 open Generator_docstrings
27 open Generator_optgroups
28 open Generator_actions
29 open Generator_structs
31 (* Generate the tests. *)
32 let rec generate_tests () =
33 generate_header CStyle GPLv2plus;
40 #include <sys/types.h>
43 #include \"guestfs.h\"
44 #include \"guestfs-internal.h\"
47 static int suppress_error = 0;
49 static void print_error (guestfs_h *g, void *data, const char *msg)
52 fprintf (stderr, \"%%s\\n\", msg);
55 /* FIXME: nearly identical code appears in fish.c */
56 static void print_strings (char *const *argv)
60 for (argc = 0; argv[argc] != NULL; ++argc)
61 printf (\"\\t%%s\\n\", argv[argc]);
65 static void print_table (char const *const *argv)
69 for (i = 0; argv[i] != NULL; i += 2)
70 printf (\"%%s: %%s\\n\", argv[i], argv[i+1]);
75 is_available (const char *group)
77 const char *groups[] = { group, NULL };
81 r = guestfs_available (g, (char **) groups);
88 incr (guestfs_h *g, void *iv)
94 /* Get md5sum of the named file. */
96 md5sum (const char *filename, char *result)
99 snprintf (cmd, sizeof cmd, \"md5sum %%s\", filename);
100 FILE *pp = popen (cmd, \"r\");
105 if (fread (result, 1, 32, pp) != 32) {
106 perror (\"md5sum: fread\");
109 if (pclose (pp) != 0) {
116 /* Return the value for a key in a hashtable.
117 * Note: the return value is part of the hash and should not be freed.
120 get_key (char **hash, const char *key)
124 for (i = 0; hash[i] != NULL; i += 2) {
125 if (STREQ (hash[i], key))
129 return NULL; /* key not found */
134 (* Generate a list of commands which are not tested anywhere. *)
135 pr "static void no_test_warnings (void)\n";
138 let hash : (string, bool) Hashtbl.t = Hashtbl.create 13 in
140 fun (_, _, _, _, tests, _, _) ->
141 let tests = filter_map (
143 | (_, (Always|If _|Unless _|IfAvailable _), test) -> Some test
144 | (_, Disabled, _) -> None
146 let seq = List.concat (List.map seq_of_test tests) in
147 let cmds_tested = List.map List.hd seq in
148 List.iter (fun cmd -> Hashtbl.replace hash cmd true) cmds_tested
152 fun (name, _, _, _, _, _, _) ->
153 if not (Hashtbl.mem hash name) then
154 pr " fprintf (stderr, \"warning: \\\"guestfs_%s\\\" has no tests\\n\");\n" name
160 (* Generate the actual tests. Note that we generate the tests
161 * in reverse order, deliberately, so that (in general) the
162 * newest tests run first. This makes it quicker and easier to
167 fun (name, _, _, flags, tests, _, _) ->
168 mapi (generate_one_test name flags) tests
169 ) (List.rev all_functions) in
170 let test_names = List.concat test_names in
171 let nr_tests = List.length test_names in
174 int main (int argc, char *argv[])
177 unsigned long int n_failed = 0;
178 const char *filename;
180 int nr_tests, test_num = 0;
182 setbuf (stdout, NULL);
186 g = guestfs_create ();
188 printf (\"guestfs_create FAILED\\n\");
192 guestfs_set_error_handler (g, print_error, NULL);
194 filename = \"test1.img\";
195 fd = open (filename, O_WRONLY|O_CREAT|O_NOCTTY|O_TRUNC, 0666);
200 if (ftruncate (fd, %d) == -1) {
201 perror (\"ftruncate\");
206 if (close (fd) == -1) {
211 if (guestfs_add_drive (g, filename) == -1) {
212 printf (\"guestfs_add_drive %%s FAILED\\n\", filename);
216 filename = \"test2.img\";
217 fd = open (filename, O_WRONLY|O_CREAT|O_NOCTTY|O_TRUNC, 0666);
222 if (ftruncate (fd, %d) == -1) {
223 perror (\"ftruncate\");
228 if (close (fd) == -1) {
233 if (guestfs_add_drive (g, filename) == -1) {
234 printf (\"guestfs_add_drive %%s FAILED\\n\", filename);
238 filename = \"test3.img\";
239 fd = open (filename, O_WRONLY|O_CREAT|O_NOCTTY|O_TRUNC, 0666);
244 if (ftruncate (fd, %d) == -1) {
245 perror (\"ftruncate\");
250 if (close (fd) == -1) {
255 if (guestfs_add_drive (g, filename) == -1) {
256 printf (\"guestfs_add_drive %%s FAILED\\n\", filename);
260 if (guestfs_add_drive_ro (g, \"../images/test.iso\") == -1) {
261 printf (\"guestfs_add_drive_ro ../images/test.iso FAILED\\n\");
265 /* Set a timeout in case qemu hangs during launch (RHBZ#505329). */
268 if (guestfs_launch (g) == -1) {
269 printf (\"guestfs_launch FAILED\\n\");
273 /* Cancel previous alarm. */
276 /* Create ext2 filesystem on /dev/sdb1 partition. */
277 if (guestfs_part_disk (g, \"/dev/sdb\", \"mbr\") == -1) {
278 printf (\"guestfs_part_disk FAILED\\n\");
281 if (guestfs_mkfs (g, \"ext2\", \"/dev/sdb1\") == -1) {
282 printf (\"guestfs_mkfs (/dev/sdb1) FAILED\\n\");
288 " (500 * 1024 * 1024) (50 * 1024 * 1024) (10 * 1024 * 1024) nr_tests;
293 pr " if (guestfs_get_verbose (g))\n";
294 pr " printf (\"-------------------------------------------------------------------------------\\n\");\n";
295 pr " printf (\"%%3d/%%3d %s\\n\", test_num, nr_tests);\n" test_name;
296 pr " if (%s () == -1) {\n" test_name;
297 pr " printf (\"%s FAILED\\n\");\n" test_name;
303 pr " /* Check close callback is called. */
304 int close_sentinel = 1;
305 guestfs_set_close_callback (g, incr, &close_sentinel);
309 if (close_sentinel != 2) {
310 fprintf (stderr, \"close callback was not called\\n\");
314 unlink (\"test1.img\");
315 unlink (\"test2.img\");
316 unlink (\"test3.img\");
320 pr " if (n_failed > 0) {\n";
321 pr " printf (\"***** %%lu / %%d tests FAILED *****\\n\", n_failed, nr_tests);\n";
322 pr " exit (EXIT_FAILURE);\n";
326 pr " exit (EXIT_SUCCESS);\n";
329 and generate_one_test name flags i (init, prereq, test) =
330 let test_name = sprintf "test_%s_%d" name i in
333 static int %s_skip (void)
337 str = getenv (\"TEST_ONLY\");
339 return strstr (str, \"%s\") == NULL;
340 str = getenv (\"SKIP_%s\");
341 if (str && STREQ (str, \"1\")) return 1;
342 str = getenv (\"SKIP_TEST_%s\");
343 if (str && STREQ (str, \"1\")) return 1;
347 " test_name name (String.uppercase test_name) (String.uppercase name);
350 | Disabled | Always | IfAvailable _ -> ()
351 | If code | Unless code ->
352 pr "static int %s_prereq (void)\n" test_name;
363 printf (\" %%s skipped (reason: environment variable set)\\n\", \"%s\");
367 " test_name test_name test_name;
369 (* Optional functions should only be tested if the relevant
370 * support is available in the daemon.
375 pr " if (!is_available (\"%s\")) {\n" group;
376 pr " printf (\" %%s skipped (reason: group %%s not available in daemon)\\n\", \"%s\", \"%s\");\n" test_name group;
384 pr " printf (\" %%s skipped (reason: test disabled in generator)\\n\", \"%s\");\n" test_name
386 pr " if (! %s_prereq ()) {\n" test_name;
387 pr " printf (\" %%s skipped (reason: test prerequisite)\\n\", \"%s\");\n" test_name;
391 generate_one_test_body name i test_name init test;
393 pr " if (%s_prereq ()) {\n" test_name;
394 pr " printf (\" %%s skipped (reason: test prerequisite)\\n\", \"%s\");\n" test_name;
398 generate_one_test_body name i test_name init test;
399 | IfAvailable group ->
400 pr " if (!is_available (\"%s\")) {\n" group;
401 pr " printf (\" %%s skipped (reason: %%s not available)\\n\", \"%s\", \"%s\");\n" test_name group;
405 generate_one_test_body name i test_name init test;
407 generate_one_test_body name i test_name init test
415 and generate_one_test_body name i test_name init test =
417 | InitNone (* XXX at some point, InitNone and InitEmpty became
418 * folded together as the same thing. Really we should
419 * make InitNone do nothing at all, but the tests may
420 * need to be checked to make sure this is OK.
423 pr " /* InitNone|InitEmpty for %s */\n" test_name;
424 List.iter (generate_test_command_call test_name)
425 [["blockdev_setrw"; "/dev/sda"];
429 pr " /* InitPartition for %s: create /dev/sda1 */\n" test_name;
430 List.iter (generate_test_command_call test_name)
431 [["blockdev_setrw"; "/dev/sda"];
434 ["part_disk"; "/dev/sda"; "mbr"]]
436 pr " /* InitBasicFS for %s: create ext2 on /dev/sda1 */\n" test_name;
437 List.iter (generate_test_command_call test_name)
438 [["blockdev_setrw"; "/dev/sda"];
441 ["part_disk"; "/dev/sda"; "mbr"];
442 ["mkfs"; "ext2"; "/dev/sda1"];
443 ["mount_options"; ""; "/dev/sda1"; "/"]]
444 | InitBasicFSonLVM ->
445 pr " /* InitBasicFSonLVM for %s: create ext2 on /dev/VG/LV */\n"
447 List.iter (generate_test_command_call test_name)
448 [["blockdev_setrw"; "/dev/sda"];
451 ["part_disk"; "/dev/sda"; "mbr"];
452 ["pvcreate"; "/dev/sda1"];
453 ["vgcreate"; "VG"; "/dev/sda1"];
454 ["lvcreate"; "LV"; "VG"; "8"];
455 ["mkfs"; "ext2"; "/dev/VG/LV"];
456 ["mount_options"; ""; "/dev/VG/LV"; "/"]]
458 pr " /* InitISOFS for %s */\n" test_name;
459 List.iter (generate_test_command_call test_name)
460 [["blockdev_setrw"; "/dev/sda"];
463 ["mount_ro"; "/dev/sdd"; "/"]]
465 pr " /* InitScratchFS for %s */\n" test_name;
466 List.iter (generate_test_command_call test_name)
467 [["blockdev_setrw"; "/dev/sda"];
470 ["mount_options"; ""; "/dev/sdb1"; "/"]]
473 let get_seq_last = function
475 failwithf "%s: you cannot use [] (empty list) when expecting a command"
478 let seq = List.rev seq in
479 List.rev (List.tl seq), List.hd seq
484 pr " /* TestRun for %s (%d) */\n" name i;
485 List.iter (generate_test_command_call test_name) seq
486 | TestOutput (seq, expected) ->
487 pr " /* TestOutput for %s (%d) */\n" name i;
488 pr " const char *expected = \"%s\";\n" (c_quote expected);
489 let seq, last = get_seq_last seq in
491 pr " if (STRNEQ (r, expected)) {\n";
492 pr " fprintf (stderr, \"%s: expected \\\"%%s\\\" but got \\\"%%s\\\"\\n\", expected, r);\n" test_name;
496 List.iter (generate_test_command_call test_name) seq;
497 generate_test_command_call ~test test_name last
498 | TestOutputList (seq, expected) ->
499 pr " /* TestOutputList for %s (%d) */\n" name i;
500 let seq, last = get_seq_last seq in
504 pr " if (!r[%d]) {\n" i;
505 pr " fprintf (stderr, \"%s: short list returned from command\\n\");\n" test_name;
506 pr " print_strings (r);\n";
510 pr " const char *expected = \"%s\";\n" (c_quote str);
511 pr " if (STRNEQ (r[%d], expected)) {\n" i;
512 pr " fprintf (stderr, \"%s: expected \\\"%%s\\\" but got \\\"%%s\\\"\\n\", expected, r[%d]);\n" test_name i;
517 pr " if (r[%d] != NULL) {\n" (List.length expected);
518 pr " fprintf (stderr, \"%s: extra elements returned from command\\n\");\n"
520 pr " print_strings (r);\n";
524 List.iter (generate_test_command_call test_name) seq;
525 generate_test_command_call ~test test_name last
526 | TestOutputListOfDevices (seq, expected) ->
527 pr " /* TestOutputListOfDevices for %s (%d) */\n" name i;
528 let seq, last = get_seq_last seq in
532 pr " if (!r[%d]) {\n" i;
533 pr " fprintf (stderr, \"%s: short list returned from command\\n\");\n" test_name;
534 pr " print_strings (r);\n";
538 pr " const char *expected = \"%s\";\n" (c_quote str);
539 pr " r[%d][5] = 's';\n" i;
540 pr " if (STRNEQ (r[%d], expected)) {\n" i;
541 pr " fprintf (stderr, \"%s: expected \\\"%%s\\\" but got \\\"%%s\\\"\\n\", expected, r[%d]);\n" test_name i;
546 pr " if (r[%d] != NULL) {\n" (List.length expected);
547 pr " fprintf (stderr, \"%s: extra elements returned from command\\n\");\n"
549 pr " print_strings (r);\n";
553 List.iter (generate_test_command_call test_name) seq;
554 generate_test_command_call ~test test_name last
555 | TestOutputInt (seq, expected) ->
556 pr " /* TestOutputInt for %s (%d) */\n" name i;
557 let seq, last = get_seq_last seq in
559 pr " if (r != %d) {\n" expected;
560 pr " fprintf (stderr, \"%s: expected %d but got %%d\\n\","
566 List.iter (generate_test_command_call test_name) seq;
567 generate_test_command_call ~test test_name last
568 | TestOutputIntOp (seq, op, expected) ->
569 pr " /* TestOutputIntOp for %s (%d) */\n" name i;
570 let seq, last = get_seq_last seq in
572 pr " if (! (r %s %d)) {\n" op expected;
573 pr " fprintf (stderr, \"%s: expected %s %d but got %%d\\n\","
574 test_name op expected;
579 List.iter (generate_test_command_call test_name) seq;
580 generate_test_command_call ~test test_name last
581 | TestOutputTrue seq ->
582 pr " /* TestOutputTrue for %s (%d) */\n" name i;
583 let seq, last = get_seq_last seq in
586 pr " fprintf (stderr, \"%s: expected true, got false\\n\");\n"
591 List.iter (generate_test_command_call test_name) seq;
592 generate_test_command_call ~test test_name last
593 | TestOutputFalse seq ->
594 pr " /* TestOutputFalse for %s (%d) */\n" name i;
595 let seq, last = get_seq_last seq in
598 pr " fprintf (stderr, \"%s: expected false, got true\\n\");\n"
603 List.iter (generate_test_command_call test_name) seq;
604 generate_test_command_call ~test test_name last
605 | TestOutputLength (seq, expected) ->
606 pr " /* TestOutputLength for %s (%d) */\n" name i;
607 let seq, last = get_seq_last seq in
610 pr " for (j = 0; j < %d; ++j)\n" expected;
611 pr " if (r[j] == NULL) {\n";
612 pr " fprintf (stderr, \"%s: short list returned\\n\");\n"
614 pr " print_strings (r);\n";
617 pr " if (r[j] != NULL) {\n";
618 pr " fprintf (stderr, \"%s: long list returned\\n\");\n"
620 pr " print_strings (r);\n";
624 List.iter (generate_test_command_call test_name) seq;
625 generate_test_command_call ~test test_name last
626 | TestOutputBuffer (seq, expected) ->
627 pr " /* TestOutputBuffer for %s (%d) */\n" name i;
628 pr " const char *expected = \"%s\";\n" (c_quote expected);
629 let seq, last = get_seq_last seq in
630 let len = String.length expected in
632 pr " if (size != %d) {\n" len;
633 pr " fprintf (stderr, \"%s: returned size of buffer wrong, expected %d but got %%zu\\n\", size);\n" test_name len;
636 pr " if (STRNEQLEN (r, expected, size)) {\n";
637 pr " fprintf (stderr, \"%s: expected \\\"%%s\\\" but got \\\"%%s\\\"\\n\", expected, r);\n" test_name;
641 List.iter (generate_test_command_call test_name) seq;
642 generate_test_command_call ~test test_name last
643 | TestOutputStruct (seq, checks) ->
644 pr " /* TestOutputStruct for %s (%d) */\n" name i;
645 let seq, last = get_seq_last seq in
649 | CompareWithInt (field, expected) ->
650 pr " if (r->%s != %d) {\n" field expected;
651 pr " fprintf (stderr, \"%s: %s was %%d, expected %d\\n\",\n"
652 test_name field expected;
653 pr " (int) r->%s);\n" field;
656 | CompareWithIntOp (field, op, expected) ->
657 pr " if (!(r->%s %s %d)) {\n" field op expected;
658 pr " fprintf (stderr, \"%s: %s was %%d, expected %s %d\\n\",\n"
659 test_name field op expected;
660 pr " (int) r->%s);\n" field;
663 | CompareWithString (field, expected) ->
664 pr " if (STRNEQ (r->%s, \"%s\")) {\n" field expected;
665 pr " fprintf (stderr, \"%s: %s was \"%%s\", expected \"%s\"\\n\",\n"
666 test_name field expected;
667 pr " r->%s);\n" field;
670 | CompareFieldsIntEq (field1, field2) ->
671 pr " if (r->%s != r->%s) {\n" field1 field2;
672 pr " fprintf (stderr, \"%s: %s (%%d) <> %s (%%d)\\n\",\n"
673 test_name field1 field2;
674 pr " (int) r->%s, (int) r->%s);\n" field1 field2;
677 | CompareFieldsStrEq (field1, field2) ->
678 pr " if (STRNEQ (r->%s, r->%s)) {\n" field1 field2;
679 pr " fprintf (stderr, \"%s: %s (\"%%s\") <> %s (\"%%s\")\\n\",\n"
680 test_name field1 field2;
681 pr " r->%s, r->%s);\n" field1 field2;
686 List.iter (generate_test_command_call test_name) seq;
687 generate_test_command_call ~test test_name last
688 | TestOutputFileMD5 (seq, filename) ->
689 pr " /* TestOutputFileMD5 for %s (%d) */\n" name i;
690 pr " char expected[33];\n";
691 pr " md5sum (\"%s\", expected);\n" filename;
692 let seq, last = get_seq_last seq in
694 pr " if (STRNEQ (r, expected)) {\n";
695 pr " fprintf (stderr, \"%s: expected \\\"%%s\\\" but got \\\"%%s\\\"\\n\", expected, r);\n" test_name;
699 List.iter (generate_test_command_call test_name) seq;
700 generate_test_command_call ~test test_name last
701 | TestOutputDevice (seq, expected) ->
702 pr " /* TestOutputDevice for %s (%d) */\n" name i;
703 pr " const char *expected = \"%s\";\n" (c_quote expected);
704 let seq, last = get_seq_last seq in
707 pr " if (STRNEQ (r, expected)) {\n";
708 pr " fprintf (stderr, \"%s: expected \\\"%%s\\\" but got \\\"%%s\\\"\\n\", expected, r);\n" test_name;
712 List.iter (generate_test_command_call test_name) seq;
713 generate_test_command_call ~test test_name last
714 | TestOutputHashtable (seq, fields) ->
715 pr " /* TestOutputHashtable for %s (%d) */\n" name i;
716 pr " const char *key, *expected, *value;\n";
717 let seq, last = get_seq_last seq in
721 pr " key = \"%s\";\n" (c_quote key);
722 pr " expected = \"%s\";\n" (c_quote value);
723 pr " value = get_key (r, key);\n";
724 pr " if (value == NULL) {\n";
725 pr " fprintf (stderr, \"%s: key \\\"%%s\\\" not found in hash: expecting \\\"%%s\\\"\\n\", key, expected);\n" test_name;
728 pr " if (STRNEQ (value, expected)) {\n";
729 pr " fprintf (stderr, \"%s: key \\\"%%s\\\": expected \\\"%%s\\\" but got \\\"%%s\\\"\\n\", key, expected, value);\n" test_name;
734 List.iter (generate_test_command_call test_name) seq;
735 generate_test_command_call ~test test_name last
736 | TestLastFail seq ->
737 pr " /* TestLastFail for %s (%d) */\n" name i;
738 let seq, last = get_seq_last seq in
739 List.iter (generate_test_command_call test_name) seq;
740 generate_test_command_call test_name ~expect_error:true last
742 (* Generate the code to run a command, leaving the result in 'r'.
743 * If you expect to get an error then you should set expect_error:true.
745 and generate_test_command_call ?(expect_error = false) ?test test_name cmd =
749 (* Look up the command to find out what args/ret it has. *)
750 let style_ret, style_args, style_optargs =
752 let _, style, _, _, _, _, _ =
753 List.find (fun (n, _, _, _, _, _, _) -> n = name) all_functions in
756 failwithf "%s: in test, command %s was not found" test_name name in
758 (* Match up the arguments strings and argument types. *)
760 let rec loop argts args =
761 match argts, args with
762 | (t::ts), (s::ss) ->
763 let args, rest = loop ts ss in
764 ((t, s) :: args), rest
767 failwithf "%s: in test, too few args given to function %s"
770 let args, optargs = loop style_args args in
771 let optargs, rest = loop style_optargs optargs in
773 failwithf "%s: in test, too many args given to function %s"
781 | OptString n, "NULL" -> ()
788 pr " const char *%s = \"%s\";\n" n (c_quote arg);
790 pr " const char *%s = \"%s\";\n" n (c_quote arg);
791 pr " size_t %s_size = %d;\n" n (String.length arg)
795 | FileIn _, _ | FileOut _, _ -> ()
796 | StringList n, "" | DeviceList n, "" ->
797 pr " const char *const %s[1] = { NULL };\n" n
798 | StringList n, arg | DeviceList n, arg ->
799 let strs = string_split " " arg in
802 pr " const char *%s_%d = \"%s\";\n" n i (c_quote str);
804 pr " const char *const %s[] = {\n" n;
806 fun i _ -> pr " %s_%d,\n" n i
811 (* Difficult to make these pointers in order to run a test. *)
815 if optargs <> [] then (
816 pr " struct guestfs_%s_argv optargs;\n" name;
817 let _, bitmask = List.fold_left (
818 fun (shift, bitmask) optarg ->
821 | Bool n, "" -> false
823 pr " optargs.%s = 1;\n" n; true
825 pr " optargs.%s = 0;\n" n; true
827 failwithf "boolean optional arg '%s' should be empty string or \"true\" or \"false\"" n
832 with Failure _ -> failwithf "integer optional arg '%s' should be empty string or number" n in
833 pr " optargs.%s = %d;\n" n i; true
834 | Int64 n, "" -> false
837 try Int64.of_string i
838 with Failure _ -> failwithf "int64 optional arg '%s' should be empty string or number" n in
839 pr " optargs.%s = %Ld;\n" n i; true
840 | String n, "NOARG" -> false
842 pr " optargs.%s = \"%s\";\n" n (c_quote arg); true
843 | _ -> assert false in
844 let bit = if is_set then Int64.shift_left 1L shift else 0L in
845 let bitmask = Int64.logor bitmask bit in
846 let shift = shift + 1 in
849 pr " optargs.bitmask = UINT64_C(0x%Lx);\n" bitmask;
852 (match style_ret with
853 | RErr | RInt _ | RBool _ -> pr " int r;\n"
854 | RInt64 _ -> pr " int64_t r;\n"
855 | RConstString _ | RConstOptString _ ->
856 pr " const char *r;\n"
857 | RString _ -> pr " char *r;\n"
858 | RStringList _ | RHashtable _ ->
861 | RStruct (_, typ) ->
862 pr " struct guestfs_%s *r;\n" typ
863 | RStructList (_, typ) ->
864 pr " struct guestfs_%s_list *r;\n" typ
870 pr " suppress_error = %d;\n" (if expect_error then 1 else 0);
872 pr " r = guestfs_%s (g" name
874 pr " r = guestfs_%s_argv (g" name;
876 (* Generate the parameters. *)
879 | OptString _, "NULL" -> pr ", NULL"
881 | Device n, _ | Dev_or_Path n, _
887 pr ", %s, %s_size" n n
888 | FileIn _, arg | FileOut _, arg ->
889 pr ", \"%s\"" (c_quote arg)
890 | StringList n, _ | DeviceList n, _ ->
891 pr ", (char **) %s" n
894 try int_of_string arg
895 with Failure "int_of_string" ->
896 failwithf "%s: expecting an int, but got '%s'" test_name arg in
900 try Int64.of_string arg
901 with Failure "int_of_string" ->
902 failwithf "%s: expecting an int64, but got '%s'" test_name arg in
905 let b = bool_of_string arg in pr ", %d" (if b then 1 else 0)
906 | Pointer _, _ -> assert false
909 (match style_ret with
910 | RBufferOut _ -> pr ", &size"
914 if optargs <> [] then
919 (match errcode_of_ret style_ret, expect_error with
920 | `CannotReturnError, _ -> ()
921 | `ErrorIsMinusOne, false ->
922 pr " if (r == -1)\n";
924 | `ErrorIsMinusOne, true ->
925 pr " if (r != -1)\n";
927 | `ErrorIsNULL, false ->
928 pr " if (r == NULL)\n";
930 | `ErrorIsNULL, true ->
931 pr " if (r != NULL)\n";
935 (* Insert the test code. *)
941 (match style_ret with
942 | RErr | RInt _ | RInt64 _ | RBool _
943 | RConstString _ | RConstOptString _ -> ()
944 | RString _ | RBufferOut _ -> pr " free (r);\n"
945 | RStringList _ | RHashtable _ ->
946 pr " for (i = 0; r[i] != NULL; ++i)\n";
947 pr " free (r[i]);\n";
949 | RStruct (_, typ) ->
950 pr " guestfs_free_%s (r);\n" typ
951 | RStructList (_, typ) ->
952 pr " guestfs_free_%s_list (r);\n" typ