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
32 let rec generate_bindtests () =
33 generate_header CStyle LGPLv2plus;
41 #include \"guestfs.h\"
42 #include \"guestfs-internal.h\"
43 #include \"guestfs-internal-actions.h\"
44 #include \"guestfs_protocol.h\"
47 print_strings (char *const *argv)
52 for (argc = 0; argv[argc] != NULL; ++argc) {
53 if (argc > 0) printf (\", \");
54 printf (\"\\\"%%s\\\"\", argv[argc]);
59 /* The test0 function prints its parameters to stdout. */
63 match test_functions with
65 | test0 :: tests -> test0, tests in
68 let (name, (ret, args, _ as style), _, _, _, _, _) = test0 in
69 generate_prototype ~extern:false ~semicolon:false ~newline:true
70 ~handle:"g" ~prefix:"guestfs__" name style;
75 | Device n | Dev_or_Path n
79 | Key n -> pr " printf (\"%%s\\n\", %s);\n" n
83 pr " for (i = 0; i < %s_size; ++i)\n" n;
84 pr " printf (\"<%%02x>\", %s[i]);\n" n;
85 pr " printf (\"\\n\");\n";
87 | OptString n -> pr " printf (\"%%s\\n\", %s ? %s : \"null\");\n" n n
88 | StringList n | DeviceList n -> pr " print_strings (%s);\n" n
89 | Bool n -> pr " printf (\"%%s\\n\", %s ? \"true\" : \"false\");\n" n
90 | Int n -> pr " printf (\"%%d\\n\", %s);\n" n
91 | Int64 n -> pr " printf (\"%%\" PRIi64 \"\\n\", %s);\n" n
92 | Pointer _ -> assert false
94 pr " /* Java changes stdout line buffering so we need this: */\n";
95 pr " fflush (stdout);\n";
101 fun (name, (ret, args, _ as style), _, _, _, _, _) ->
102 if String.sub name (String.length name - 3) 3 <> "err" then (
103 pr "/* Test normal return. */\n";
104 generate_prototype ~extern:false ~semicolon:false ~newline:true
105 ~handle:"g" ~prefix:"guestfs__" name style;
112 pr " sscanf (val, \"%%d\", &r);\n";
116 pr " sscanf (val, \"%%\" SCNi64, &r);\n";
119 pr " return STREQ (val, \"true\");\n"
121 | RConstOptString _ ->
122 (* Can't return the input string here. Return a static
123 * string so we ensure we get a segfault if the caller
126 pr " return \"static string\";\n"
128 pr " return strdup (val);\n"
130 pr " char **strs;\n";
132 pr " sscanf (val, \"%%d\", &n);\n";
133 pr " strs = safe_malloc (g, (n+1) * sizeof (char *));\n";
134 pr " for (i = 0; i < n; ++i) {\n";
135 pr " strs[i] = safe_malloc (g, 16);\n";
136 pr " snprintf (strs[i], 16, \"%%d\", i);\n";
138 pr " strs[n] = NULL;\n";
140 | RStruct (_, typ) ->
141 pr " struct guestfs_%s *r;\n" typ;
142 pr " r = safe_calloc (g, sizeof *r, 1);\n";
144 | RStructList (_, typ) ->
145 pr " struct guestfs_%s_list *r;\n" typ;
146 pr " r = safe_calloc (g, sizeof *r, 1);\n";
147 pr " sscanf (val, \"%%d\", &r->len);\n";
148 pr " r->val = safe_calloc (g, r->len, sizeof *r->val);\n";
151 pr " char **strs;\n";
153 pr " sscanf (val, \"%%d\", &n);\n";
154 pr " strs = safe_malloc (g, (n*2+1) * sizeof (*strs));\n";
155 pr " for (i = 0; i < n; ++i) {\n";
156 pr " strs[i*2] = safe_malloc (g, 16);\n";
157 pr " strs[i*2+1] = safe_malloc (g, 16);\n";
158 pr " snprintf (strs[i*2], 16, \"%%d\", i);\n";
159 pr " snprintf (strs[i*2+1], 16, \"%%d\", i);\n";
161 pr " strs[n*2] = NULL;\n";
164 pr " return strdup (val);\n"
169 pr "/* Test error return. */\n";
170 generate_prototype ~extern:false ~semicolon:false ~newline:true
171 ~handle:"g" ~prefix:"guestfs__" name style;
173 pr " error (g, \"error\");\n";
175 | RErr | RInt _ | RInt64 _ | RBool _ ->
177 | RConstString _ | RConstOptString _
178 | RString _ | RStringList _ | RStruct _
189 and generate_ocaml_bindtests () =
190 generate_header OCamlStyle GPLv2plus;
194 let g = Guestfs.create () in
201 | CallString s -> "\"" ^ s ^ "\""
202 | CallOptString None -> "None"
203 | CallOptString (Some s) -> sprintf "(Some \"%s\")" s
204 | CallStringList xs ->
205 "[|" ^ String.concat ";" (List.map (sprintf "\"%s\"") xs) ^ "|]"
206 | CallInt i when i >= 0 -> string_of_int i
207 | CallInt i (* when i < 0 *) -> "(" ^ string_of_int i ^ ")"
208 | CallInt64 i when i >= 0L -> Int64.to_string i ^ "L"
209 | CallInt64 i (* when i < 0L *) -> "(" ^ Int64.to_string i ^ "L)"
210 | CallBool b -> string_of_bool b
211 | CallBuffer s -> sprintf "%S" s
216 generate_lang_bindtests (
217 fun f args -> pr " Guestfs.%s g %s;\n" f (mkargs args)
220 pr "print_endline \"EOF\"\n"
222 and generate_perl_bindtests () =
223 pr "#!/usr/bin/perl -w\n";
224 generate_header HashStyle GPLv2plus;
231 my $g = Sys::Guestfs->new ();
238 | CallString s -> "\"" ^ s ^ "\""
239 | CallOptString None -> "undef"
240 | CallOptString (Some s) -> sprintf "\"%s\"" s
241 | CallStringList xs ->
242 "[" ^ String.concat "," (List.map (sprintf "\"%s\"") xs) ^ "]"
243 | CallInt i -> string_of_int i
244 | CallInt64 i -> Int64.to_string i
245 | CallBool b -> if b then "1" else "0"
246 | CallBuffer s -> "\"" ^ c_quote s ^ "\""
251 generate_lang_bindtests (
252 fun f args -> pr "$g->%s (%s);\n" f (mkargs args)
255 pr "print \"EOF\\n\"\n"
257 and generate_python_bindtests () =
258 generate_header HashStyle GPLv2plus;
263 g = guestfs.GuestFS ()
270 | CallString s -> "\"" ^ s ^ "\""
271 | CallOptString None -> "None"
272 | CallOptString (Some s) -> sprintf "\"%s\"" s
273 | CallStringList xs ->
274 "[" ^ String.concat "," (List.map (sprintf "\"%s\"") xs) ^ "]"
275 | CallInt i -> string_of_int i
276 | CallInt64 i -> Int64.to_string i
277 | CallBool b -> if b then "1" else "0"
278 | CallBuffer s -> "\"" ^ c_quote s ^ "\""
283 generate_lang_bindtests (
284 fun f args -> pr "g.%s (%s)\n" f (mkargs args)
289 and generate_ruby_bindtests () =
290 generate_header HashStyle GPLv2plus;
295 g = Guestfs::create()
302 | CallString s -> "\"" ^ s ^ "\""
303 | CallOptString None -> "nil"
304 | CallOptString (Some s) -> sprintf "\"%s\"" s
305 | CallStringList xs ->
306 "[" ^ String.concat "," (List.map (sprintf "\"%s\"") xs) ^ "]"
307 | CallInt i -> string_of_int i
308 | CallInt64 i -> Int64.to_string i
309 | CallBool b -> string_of_bool b
310 | CallBuffer s -> "\"" ^ c_quote s ^ "\""
315 generate_lang_bindtests (
316 fun f args -> pr "g.%s(%s)\n" f (mkargs args)
319 pr "print \"EOF\\n\"\n"
321 and generate_java_bindtests () =
322 generate_header CStyle GPLv2plus;
325 import com.redhat.et.libguestfs.*;
327 public class Bindtests {
328 public static void main (String[] argv)
331 GuestFS g = new GuestFS ();
338 | CallString s -> "\"" ^ s ^ "\""
339 | CallOptString None -> "null"
340 | CallOptString (Some s) -> sprintf "\"%s\"" s
341 | CallStringList xs ->
343 String.concat "," (List.map (sprintf "\"%s\"") xs) ^ "}"
344 | CallInt i -> string_of_int i
345 | CallInt64 i -> Int64.to_string i
346 | CallBool b -> string_of_bool b
348 "new byte[] { " ^ String.concat "," (
349 map_chars (fun c -> string_of_int (Char.code c)) s
355 generate_lang_bindtests (
356 fun f args -> pr " g.%s (%s);\n" f (mkargs args)
360 System.out.println (\"EOF\");
362 catch (Exception exn) {
363 System.err.println (exn);
370 and generate_haskell_bindtests () =
371 generate_header HaskellStyle GPLv2plus;
374 module Bindtests where
375 import qualified Guestfs
385 | CallString s -> "\"" ^ s ^ "\""
386 | CallOptString None -> "Nothing"
387 | CallOptString (Some s) -> sprintf "(Just \"%s\")" s
388 | CallStringList xs ->
389 "[" ^ String.concat "," (List.map (sprintf "\"%s\"") xs) ^ "]"
390 | CallInt i when i < 0 -> "(" ^ string_of_int i ^ ")"
391 | CallInt i -> string_of_int i
392 | CallInt64 i when i < 0L -> "(" ^ Int64.to_string i ^ ")"
393 | CallInt64 i -> Int64.to_string i
394 | CallBool true -> "True"
395 | CallBool false -> "False"
396 | CallBuffer s -> "\"" ^ c_quote s ^ "\""
401 generate_lang_bindtests (
402 fun f args -> pr " Guestfs.%s g %s\n" f (mkargs args)
405 pr " putStrLn \"EOF\"\n"
407 (* Language-independent bindings tests - we do it this way to
408 * ensure there is parity in testing bindings across all languages.
410 and generate_lang_bindtests call =
411 call "test0" [CallString "abc"; CallOptString (Some "def");
412 CallStringList []; CallBool false;
413 CallInt 0; CallInt64 0L; CallString "123"; CallString "456";
414 CallBuffer "abc\000abc"];
415 call "test0" [CallString "abc"; CallOptString None;
416 CallStringList []; CallBool false;
417 CallInt 0; CallInt64 0L; CallString "123"; CallString "456";
418 CallBuffer "abc\000abc"];
419 call "test0" [CallString ""; CallOptString (Some "def");
420 CallStringList []; CallBool false;
421 CallInt 0; CallInt64 0L; CallString "123"; CallString "456";
422 CallBuffer "abc\000abc"];
423 call "test0" [CallString ""; CallOptString (Some "");
424 CallStringList []; CallBool false;
425 CallInt 0; CallInt64 0L; CallString "123"; CallString "456";
426 CallBuffer "abc\000abc"];
427 call "test0" [CallString "abc"; CallOptString (Some "def");
428 CallStringList ["1"]; CallBool false;
429 CallInt 0; CallInt64 0L; CallString "123"; CallString "456";
430 CallBuffer "abc\000abc"];
431 call "test0" [CallString "abc"; CallOptString (Some "def");
432 CallStringList ["1"; "2"]; CallBool false;
433 CallInt 0; CallInt64 0L; CallString "123"; CallString "456";
434 CallBuffer "abc\000abc"];
435 call "test0" [CallString "abc"; CallOptString (Some "def");
436 CallStringList ["1"]; CallBool true;
437 CallInt 0; CallInt64 0L; CallString "123"; CallString "456";
438 CallBuffer "abc\000abc"];
439 call "test0" [CallString "abc"; CallOptString (Some "def");
440 CallStringList ["1"]; CallBool false;
441 CallInt (-1); CallInt64 (-1L); CallString "123"; CallString "456";
442 CallBuffer "abc\000abc"];
443 call "test0" [CallString "abc"; CallOptString (Some "def");
444 CallStringList ["1"]; CallBool false;
445 CallInt (-2); CallInt64 (-2L); CallString "123"; CallString "456";
446 CallBuffer "abc\000abc"];
447 call "test0" [CallString "abc"; CallOptString (Some "def");
448 CallStringList ["1"]; CallBool false;
449 CallInt 1; CallInt64 1L; CallString "123"; CallString "456";
450 CallBuffer "abc\000abc"];
451 call "test0" [CallString "abc"; CallOptString (Some "def");
452 CallStringList ["1"]; CallBool false;
453 CallInt 2; CallInt64 2L; CallString "123"; CallString "456";
454 CallBuffer "abc\000abc"];
455 call "test0" [CallString "abc"; CallOptString (Some "def");
456 CallStringList ["1"]; CallBool false;
457 CallInt 4095; CallInt64 4095L; CallString "123"; CallString "456";
458 CallBuffer "abc\000abc"];
459 call "test0" [CallString "abc"; CallOptString (Some "def");
460 CallStringList ["1"]; CallBool false;
461 CallInt 0; CallInt64 0L; CallString ""; CallString "";
462 CallBuffer "abc\000abc"]
464 (* XXX Add here tests of the return and error functions. *)