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) == -1) {
118 (* Generate a list of commands which are not tested anywhere. *)
119 pr "static void no_test_warnings (void)\n";
122 let hash : (string, bool) Hashtbl.t = Hashtbl.create 13 in
124 fun (_, _, _, _, tests, _, _) ->
125 let tests = filter_map (
127 | (_, (Always|If _|Unless _|IfAvailable _), test) -> Some test
128 | (_, Disabled, _) -> None
130 let seq = List.concat (List.map seq_of_test tests) in
131 let cmds_tested = List.map List.hd seq in
132 List.iter (fun cmd -> Hashtbl.replace hash cmd true) cmds_tested
136 fun (name, _, _, _, _, _, _) ->
137 if not (Hashtbl.mem hash name) then
138 pr " fprintf (stderr, \"warning: \\\"guestfs_%s\\\" has no tests\\n\");\n" name
144 (* Generate the actual tests. Note that we generate the tests
145 * in reverse order, deliberately, so that (in general) the
146 * newest tests run first. This makes it quicker and easier to
151 fun (name, _, _, flags, tests, _, _) ->
152 mapi (generate_one_test name flags) tests
153 ) (List.rev all_functions) in
154 let test_names = List.concat test_names in
155 let nr_tests = List.length test_names in
158 int main (int argc, char *argv[])
161 unsigned long int n_failed = 0;
162 const char *filename;
164 int nr_tests, test_num = 0;
166 setbuf (stdout, NULL);
170 g = guestfs_create ();
172 printf (\"guestfs_create FAILED\\n\");
176 guestfs_set_error_handler (g, print_error, NULL);
178 filename = \"test1.img\";
179 fd = open (filename, O_WRONLY|O_CREAT|O_NOCTTY|O_TRUNC, 0666);
184 if (ftruncate (fd, %d) == -1) {
185 perror (\"ftruncate\");
190 if (close (fd) == -1) {
195 if (guestfs_add_drive (g, filename) == -1) {
196 printf (\"guestfs_add_drive %%s FAILED\\n\", filename);
200 filename = \"test2.img\";
201 fd = open (filename, O_WRONLY|O_CREAT|O_NOCTTY|O_TRUNC, 0666);
206 if (ftruncate (fd, %d) == -1) {
207 perror (\"ftruncate\");
212 if (close (fd) == -1) {
217 if (guestfs_add_drive (g, filename) == -1) {
218 printf (\"guestfs_add_drive %%s FAILED\\n\", filename);
222 filename = \"test3.img\";
223 fd = open (filename, O_WRONLY|O_CREAT|O_NOCTTY|O_TRUNC, 0666);
228 if (ftruncate (fd, %d) == -1) {
229 perror (\"ftruncate\");
234 if (close (fd) == -1) {
239 if (guestfs_add_drive (g, filename) == -1) {
240 printf (\"guestfs_add_drive %%s FAILED\\n\", filename);
244 if (guestfs_add_drive_ro (g, \"../images/test.iso\") == -1) {
245 printf (\"guestfs_add_drive_ro ../images/test.iso FAILED\\n\");
249 /* Set a timeout in case qemu hangs during launch (RHBZ#505329). */
252 if (guestfs_launch (g) == -1) {
253 printf (\"guestfs_launch FAILED\\n\");
257 /* Cancel previous alarm. */
260 /* Create ext2 filesystem on /dev/sdb1 partition. */
261 if (guestfs_part_disk (g, \"/dev/sdb\", \"mbr\") == -1) {
262 printf (\"guestfs_part_disk FAILED\\n\");
265 if (guestfs_mkfs (g, \"ext2\", \"/dev/sdb1\") == -1) {
266 printf (\"guestfs_mkfs (/dev/sdb1) FAILED\\n\");
272 " (500 * 1024 * 1024) (50 * 1024 * 1024) (10 * 1024 * 1024) nr_tests;
277 pr " if (guestfs_get_verbose (g))\n";
278 pr " printf (\"-------------------------------------------------------------------------------\\n\");\n";
279 pr " printf (\"%%3d/%%3d %s\\n\", test_num, nr_tests);\n" test_name;
280 pr " if (%s () == -1) {\n" test_name;
281 pr " printf (\"%s FAILED\\n\");\n" test_name;
287 pr " /* Check close callback is called. */
288 int close_sentinel = 1;
289 guestfs_set_close_callback (g, incr, &close_sentinel);
293 if (close_sentinel != 2) {
294 fprintf (stderr, \"close callback was not called\\n\");
298 unlink (\"test1.img\");
299 unlink (\"test2.img\");
300 unlink (\"test3.img\");
304 pr " if (n_failed > 0) {\n";
305 pr " printf (\"***** %%lu / %%d tests FAILED *****\\n\", n_failed, nr_tests);\n";
306 pr " exit (EXIT_FAILURE);\n";
310 pr " exit (EXIT_SUCCESS);\n";
313 and generate_one_test name flags i (init, prereq, test) =
314 let test_name = sprintf "test_%s_%d" name i in
317 static int %s_skip (void)
321 str = getenv (\"TEST_ONLY\");
323 return strstr (str, \"%s\") == NULL;
324 str = getenv (\"SKIP_%s\");
325 if (str && STREQ (str, \"1\")) return 1;
326 str = getenv (\"SKIP_TEST_%s\");
327 if (str && STREQ (str, \"1\")) return 1;
331 " test_name name (String.uppercase test_name) (String.uppercase name);
334 | Disabled | Always | IfAvailable _ -> ()
335 | If code | Unless code ->
336 pr "static int %s_prereq (void)\n" test_name;
347 printf (\" %%s skipped (reason: environment variable set)\\n\", \"%s\");
351 " test_name test_name test_name;
353 (* Optional functions should only be tested if the relevant
354 * support is available in the daemon.
359 pr " if (!is_available (\"%s\")) {\n" group;
360 pr " printf (\" %%s skipped (reason: group %%s not available in daemon)\\n\", \"%s\", \"%s\");\n" test_name group;
368 pr " printf (\" %%s skipped (reason: test disabled in generator)\\n\", \"%s\");\n" test_name
370 pr " if (! %s_prereq ()) {\n" test_name;
371 pr " printf (\" %%s skipped (reason: test prerequisite)\\n\", \"%s\");\n" test_name;
375 generate_one_test_body name i test_name init test;
377 pr " if (%s_prereq ()) {\n" test_name;
378 pr " printf (\" %%s skipped (reason: test prerequisite)\\n\", \"%s\");\n" test_name;
382 generate_one_test_body name i test_name init test;
383 | IfAvailable group ->
384 pr " if (!is_available (\"%s\")) {\n" group;
385 pr " printf (\" %%s skipped (reason: %%s not available)\\n\", \"%s\", \"%s\");\n" test_name group;
389 generate_one_test_body name i test_name init test;
391 generate_one_test_body name i test_name init test
399 and generate_one_test_body name i test_name init test =
401 | InitNone (* XXX at some point, InitNone and InitEmpty became
402 * folded together as the same thing. Really we should
403 * make InitNone do nothing at all, but the tests may
404 * need to be checked to make sure this is OK.
407 pr " /* InitNone|InitEmpty for %s */\n" test_name;
408 List.iter (generate_test_command_call test_name)
409 [["blockdev_setrw"; "/dev/sda"];
413 pr " /* InitPartition for %s: create /dev/sda1 */\n" test_name;
414 List.iter (generate_test_command_call test_name)
415 [["blockdev_setrw"; "/dev/sda"];
418 ["part_disk"; "/dev/sda"; "mbr"]]
420 pr " /* InitBasicFS for %s: create ext2 on /dev/sda1 */\n" test_name;
421 List.iter (generate_test_command_call test_name)
422 [["blockdev_setrw"; "/dev/sda"];
425 ["part_disk"; "/dev/sda"; "mbr"];
426 ["mkfs"; "ext2"; "/dev/sda1"];
427 ["mount_options"; ""; "/dev/sda1"; "/"]]
428 | InitBasicFSonLVM ->
429 pr " /* InitBasicFSonLVM for %s: create ext2 on /dev/VG/LV */\n"
431 List.iter (generate_test_command_call test_name)
432 [["blockdev_setrw"; "/dev/sda"];
435 ["part_disk"; "/dev/sda"; "mbr"];
436 ["pvcreate"; "/dev/sda1"];
437 ["vgcreate"; "VG"; "/dev/sda1"];
438 ["lvcreate"; "LV"; "VG"; "8"];
439 ["mkfs"; "ext2"; "/dev/VG/LV"];
440 ["mount_options"; ""; "/dev/VG/LV"; "/"]]
442 pr " /* InitISOFS for %s */\n" test_name;
443 List.iter (generate_test_command_call test_name)
444 [["blockdev_setrw"; "/dev/sda"];
447 ["mount_ro"; "/dev/sdd"; "/"]]
449 pr " /* InitScratchFS for %s */\n" test_name;
450 List.iter (generate_test_command_call test_name)
451 [["blockdev_setrw"; "/dev/sda"];
454 ["mount_options"; ""; "/dev/sdb1"; "/"]]
457 let get_seq_last = function
459 failwithf "%s: you cannot use [] (empty list) when expecting a command"
462 let seq = List.rev seq in
463 List.rev (List.tl seq), List.hd seq
468 pr " /* TestRun for %s (%d) */\n" name i;
469 List.iter (generate_test_command_call test_name) seq
470 | TestOutput (seq, expected) ->
471 pr " /* TestOutput for %s (%d) */\n" name i;
472 pr " const char *expected = \"%s\";\n" (c_quote expected);
473 let seq, last = get_seq_last seq in
475 pr " if (STRNEQ (r, expected)) {\n";
476 pr " fprintf (stderr, \"%s: expected \\\"%%s\\\" but got \\\"%%s\\\"\\n\", expected, r);\n" test_name;
480 List.iter (generate_test_command_call test_name) seq;
481 generate_test_command_call ~test test_name last
482 | TestOutputList (seq, expected) ->
483 pr " /* TestOutputList for %s (%d) */\n" name i;
484 let seq, last = get_seq_last seq in
488 pr " if (!r[%d]) {\n" i;
489 pr " fprintf (stderr, \"%s: short list returned from command\\n\");\n" test_name;
490 pr " print_strings (r);\n";
494 pr " const char *expected = \"%s\";\n" (c_quote str);
495 pr " if (STRNEQ (r[%d], expected)) {\n" i;
496 pr " fprintf (stderr, \"%s: expected \\\"%%s\\\" but got \\\"%%s\\\"\\n\", expected, r[%d]);\n" test_name i;
501 pr " if (r[%d] != NULL) {\n" (List.length expected);
502 pr " fprintf (stderr, \"%s: extra elements returned from command\\n\");\n"
504 pr " print_strings (r);\n";
508 List.iter (generate_test_command_call test_name) seq;
509 generate_test_command_call ~test test_name last
510 | TestOutputListOfDevices (seq, expected) ->
511 pr " /* TestOutputListOfDevices for %s (%d) */\n" name i;
512 let seq, last = get_seq_last seq in
516 pr " if (!r[%d]) {\n" i;
517 pr " fprintf (stderr, \"%s: short list returned from command\\n\");\n" test_name;
518 pr " print_strings (r);\n";
522 pr " const char *expected = \"%s\";\n" (c_quote str);
523 pr " r[%d][5] = 's';\n" i;
524 pr " if (STRNEQ (r[%d], expected)) {\n" i;
525 pr " fprintf (stderr, \"%s: expected \\\"%%s\\\" but got \\\"%%s\\\"\\n\", expected, r[%d]);\n" test_name i;
530 pr " if (r[%d] != NULL) {\n" (List.length expected);
531 pr " fprintf (stderr, \"%s: extra elements returned from command\\n\");\n"
533 pr " print_strings (r);\n";
537 List.iter (generate_test_command_call test_name) seq;
538 generate_test_command_call ~test test_name last
539 | TestOutputInt (seq, expected) ->
540 pr " /* TestOutputInt for %s (%d) */\n" name i;
541 let seq, last = get_seq_last seq in
543 pr " if (r != %d) {\n" expected;
544 pr " fprintf (stderr, \"%s: expected %d but got %%d\\n\","
550 List.iter (generate_test_command_call test_name) seq;
551 generate_test_command_call ~test test_name last
552 | TestOutputIntOp (seq, op, expected) ->
553 pr " /* TestOutputIntOp for %s (%d) */\n" name i;
554 let seq, last = get_seq_last seq in
556 pr " if (! (r %s %d)) {\n" op expected;
557 pr " fprintf (stderr, \"%s: expected %s %d but got %%d\\n\","
558 test_name op expected;
563 List.iter (generate_test_command_call test_name) seq;
564 generate_test_command_call ~test test_name last
565 | TestOutputTrue seq ->
566 pr " /* TestOutputTrue for %s (%d) */\n" name i;
567 let seq, last = get_seq_last seq in
570 pr " fprintf (stderr, \"%s: expected true, got false\\n\");\n"
575 List.iter (generate_test_command_call test_name) seq;
576 generate_test_command_call ~test test_name last
577 | TestOutputFalse seq ->
578 pr " /* TestOutputFalse for %s (%d) */\n" name i;
579 let seq, last = get_seq_last seq in
582 pr " fprintf (stderr, \"%s: expected false, got true\\n\");\n"
587 List.iter (generate_test_command_call test_name) seq;
588 generate_test_command_call ~test test_name last
589 | TestOutputLength (seq, expected) ->
590 pr " /* TestOutputLength for %s (%d) */\n" name i;
591 let seq, last = get_seq_last seq in
594 pr " for (j = 0; j < %d; ++j)\n" expected;
595 pr " if (r[j] == NULL) {\n";
596 pr " fprintf (stderr, \"%s: short list returned\\n\");\n"
598 pr " print_strings (r);\n";
601 pr " if (r[j] != NULL) {\n";
602 pr " fprintf (stderr, \"%s: long list returned\\n\");\n"
604 pr " print_strings (r);\n";
608 List.iter (generate_test_command_call test_name) seq;
609 generate_test_command_call ~test test_name last
610 | TestOutputBuffer (seq, expected) ->
611 pr " /* TestOutputBuffer for %s (%d) */\n" name i;
612 pr " const char *expected = \"%s\";\n" (c_quote expected);
613 let seq, last = get_seq_last seq in
614 let len = String.length expected in
616 pr " if (size != %d) {\n" len;
617 pr " fprintf (stderr, \"%s: returned size of buffer wrong, expected %d but got %%zu\\n\", size);\n" test_name len;
620 pr " if (STRNEQLEN (r, expected, size)) {\n";
621 pr " fprintf (stderr, \"%s: expected \\\"%%s\\\" but got \\\"%%s\\\"\\n\", expected, r);\n" test_name;
625 List.iter (generate_test_command_call test_name) seq;
626 generate_test_command_call ~test test_name last
627 | TestOutputStruct (seq, checks) ->
628 pr " /* TestOutputStruct for %s (%d) */\n" name i;
629 let seq, last = get_seq_last seq in
633 | CompareWithInt (field, expected) ->
634 pr " if (r->%s != %d) {\n" field expected;
635 pr " fprintf (stderr, \"%s: %s was %%d, expected %d\\n\",\n"
636 test_name field expected;
637 pr " (int) r->%s);\n" field;
640 | CompareWithIntOp (field, op, expected) ->
641 pr " if (!(r->%s %s %d)) {\n" field op expected;
642 pr " fprintf (stderr, \"%s: %s was %%d, expected %s %d\\n\",\n"
643 test_name field op expected;
644 pr " (int) r->%s);\n" field;
647 | CompareWithString (field, expected) ->
648 pr " if (STRNEQ (r->%s, \"%s\")) {\n" field expected;
649 pr " fprintf (stderr, \"%s: %s was \"%%s\", expected \"%s\"\\n\",\n"
650 test_name field expected;
651 pr " r->%s);\n" field;
654 | CompareFieldsIntEq (field1, field2) ->
655 pr " if (r->%s != r->%s) {\n" field1 field2;
656 pr " fprintf (stderr, \"%s: %s (%%d) <> %s (%%d)\\n\",\n"
657 test_name field1 field2;
658 pr " (int) r->%s, (int) r->%s);\n" field1 field2;
661 | CompareFieldsStrEq (field1, field2) ->
662 pr " if (STRNEQ (r->%s, r->%s)) {\n" field1 field2;
663 pr " fprintf (stderr, \"%s: %s (\"%%s\") <> %s (\"%%s\")\\n\",\n"
664 test_name field1 field2;
665 pr " r->%s, r->%s);\n" field1 field2;
670 List.iter (generate_test_command_call test_name) seq;
671 generate_test_command_call ~test test_name last
672 | TestOutputFileMD5 (seq, filename) ->
673 pr " /* TestOutputFileMD5 for %s (%d) */\n" name i;
674 pr " char expected[33];\n";
675 pr " md5sum (\"%s\", expected);\n" filename;
676 let seq, last = get_seq_last seq in
678 pr " if (STRNEQ (r, expected)) {\n";
679 pr " fprintf (stderr, \"%s: expected \\\"%%s\\\" but got \\\"%%s\\\"\\n\", expected, r);\n" test_name;
683 List.iter (generate_test_command_call test_name) seq;
684 generate_test_command_call ~test test_name last
685 | TestOutputDevice (seq, expected) ->
686 pr " /* TestOutputDevice for %s (%d) */\n" name i;
687 pr " const char *expected = \"%s\";\n" (c_quote expected);
688 let seq, last = get_seq_last seq in
691 pr " if (STRNEQ (r, expected)) {\n";
692 pr " fprintf (stderr, \"%s: expected \\\"%%s\\\" but got \\\"%%s\\\"\\n\", expected, r);\n" test_name;
696 List.iter (generate_test_command_call test_name) seq;
697 generate_test_command_call ~test test_name last
698 | TestLastFail seq ->
699 pr " /* TestLastFail for %s (%d) */\n" name i;
700 let seq, last = get_seq_last seq in
701 List.iter (generate_test_command_call test_name) seq;
702 generate_test_command_call test_name ~expect_error:true last
704 (* Generate the code to run a command, leaving the result in 'r'.
705 * If you expect to get an error then you should set expect_error:true.
707 and generate_test_command_call ?(expect_error = false) ?test test_name cmd =
711 (* Look up the command to find out what args/ret it has. *)
712 let style_ret, style_args, style_optargs =
714 let _, style, _, _, _, _, _ =
715 List.find (fun (n, _, _, _, _, _, _) -> n = name) all_functions in
718 failwithf "%s: in test, command %s was not found" test_name name in
720 (* Match up the arguments strings and argument types. *)
722 let rec loop argts args =
723 match argts, args with
724 | (t::ts), (s::ss) ->
725 let args, rest = loop ts ss in
726 ((t, s) :: args), rest
729 failwithf "%s: in test, too few args given to function %s"
732 let args, optargs = loop style_args args in
733 let optargs, rest = loop style_optargs optargs in
735 failwithf "%s: in test, too many args given to function %s"
743 | OptString n, "NULL" -> ()
750 pr " const char *%s = \"%s\";\n" n (c_quote arg);
752 pr " const char *%s = \"%s\";\n" n (c_quote arg);
753 pr " size_t %s_size = %d;\n" n (String.length arg)
757 | FileIn _, _ | FileOut _, _ -> ()
758 | StringList n, "" | DeviceList n, "" ->
759 pr " const char *const %s[1] = { NULL };\n" n
760 | StringList n, arg | DeviceList n, arg ->
761 let strs = string_split " " arg in
764 pr " const char *%s_%d = \"%s\";\n" n i (c_quote str);
766 pr " const char *const %s[] = {\n" n;
768 fun i _ -> pr " %s_%d,\n" n i
773 (* Difficult to make these pointers in order to run a test. *)
777 if optargs <> [] then (
778 pr " struct guestfs_%s_argv optargs;\n" name;
779 let bitmask = List.fold_left (
780 fun bitmask optarg ->
783 | Bool n, "" -> false
785 pr " optargs.%s = 1;\n" n; true
787 pr " optargs.%s = 0;\n" n; true
789 failwithf "boolean optional arg '%s' should be empty string or \"true\" or \"false\"" n
794 with Failure _ -> failwithf "integer optional arg '%s' should be empty string or number" n in
795 pr " optargs.%s = %d;\n" n i; true
796 | Int64 n, "" -> false
799 try Int64.of_string i
800 with Failure _ -> failwithf "int64 optional arg '%s' should be empty string or number" n in
801 pr " optargs.%s = %Ld;\n" n i; true
802 | String n, "NOARG" -> false
804 pr " optargs.%s = \"%s\";\n" n (c_quote arg); true
805 | _ -> assert false in
806 let bitmask = Int64.shift_left bitmask 1 in
807 let bitmask = if is_set then Int64.succ bitmask else bitmask in
810 pr " optargs.bitmask = UINT64_C(0x%Lx);\n" bitmask;
813 (match style_ret with
814 | RErr | RInt _ | RBool _ -> pr " int r;\n"
815 | RInt64 _ -> pr " int64_t r;\n"
816 | RConstString _ | RConstOptString _ ->
817 pr " const char *r;\n"
818 | RString _ -> pr " char *r;\n"
819 | RStringList _ | RHashtable _ ->
822 | RStruct (_, typ) ->
823 pr " struct guestfs_%s *r;\n" typ
824 | RStructList (_, typ) ->
825 pr " struct guestfs_%s_list *r;\n" typ
831 pr " suppress_error = %d;\n" (if expect_error then 1 else 0);
833 pr " r = guestfs_%s (g" name
835 pr " r = guestfs_%s_argv (g" name;
837 (* Generate the parameters. *)
840 | OptString _, "NULL" -> pr ", NULL"
842 | Device n, _ | Dev_or_Path n, _
848 pr ", %s, %s_size" n n
849 | FileIn _, arg | FileOut _, arg ->
850 pr ", \"%s\"" (c_quote arg)
851 | StringList n, _ | DeviceList n, _ ->
852 pr ", (char **) %s" n
855 try int_of_string arg
856 with Failure "int_of_string" ->
857 failwithf "%s: expecting an int, but got '%s'" test_name arg in
861 try Int64.of_string arg
862 with Failure "int_of_string" ->
863 failwithf "%s: expecting an int64, but got '%s'" test_name arg in
866 let b = bool_of_string arg in pr ", %d" (if b then 1 else 0)
867 | Pointer _, _ -> assert false
870 (match style_ret with
871 | RBufferOut _ -> pr ", &size"
875 if optargs <> [] then
880 (match errcode_of_ret style_ret, expect_error with
881 | `CannotReturnError, _ -> ()
882 | `ErrorIsMinusOne, false ->
883 pr " if (r == -1)\n";
885 | `ErrorIsMinusOne, true ->
886 pr " if (r != -1)\n";
888 | `ErrorIsNULL, false ->
889 pr " if (r == NULL)\n";
891 | `ErrorIsNULL, true ->
892 pr " if (r != NULL)\n";
896 (* Insert the test code. *)
902 (match style_ret with
903 | RErr | RInt _ | RInt64 _ | RBool _
904 | RConstString _ | RConstOptString _ -> ()
905 | RString _ | RBufferOut _ -> pr " free (r);\n"
906 | RStringList _ | RHashtable _ ->
907 pr " for (i = 0; r[i] != NULL; ++i)\n";
908 pr " free (r[i]);\n";
910 | RStruct (_, typ) ->
911 pr " guestfs_free_%s (r);\n" typ
912 | RStructList (_, typ) ->
913 pr " guestfs_free_%s_list (r);\n" typ