From: Richard Jones Date: Wed, 29 Jul 2009 17:44:46 +0000 (+0100) Subject: tests: Fix read_file test. X-Git-Tag: 1.0.66~62 X-Git-Url: http://git.annexia.org/?p=libguestfs.git;a=commitdiff_plain;h=bf2b08560f649c22152e4138531ad0b46b4ad1b3;hp=9847e0c07d5907887e26ec60192fa12aa46277ee tests: Fix read_file test. The test ignored the fact that this function returns a (char *, size_t) pair, and just treated the char * as a string. This of course would fail if the string didn't happen to be nul-terminated. The tests are updated to add a new TestOutputBuffer type which should be used with functions that return RBufferOut. --- diff --git a/images/Makefile.am b/images/Makefile.am index b6b7f72..424f5e4 100644 --- a/images/Makefile.am +++ b/images/Makefile.am @@ -22,7 +22,7 @@ EXTRA_DIST = \ helloworld.tar \ helloworld.tar.gz \ mbr-ext2-empty.img.gz \ - empty known-1 known-2 known-3 \ + empty known-1 known-2 known-3 known-4 known-5 \ bin-i586-dynamic \ bin-sparc-dynamic \ bin-win32.exe \ @@ -49,6 +49,8 @@ squash_files_src = \ $(srcdir)/known-1 \ $(srcdir)/known-2 \ $(srcdir)/known-3 \ + $(srcdir)/known-4 \ + $(srcdir)/known-5 \ $(srcdir)/bin-i586-dynamic \ $(srcdir)/bin-sparc-dynamic \ $(srcdir)/bin-win32.exe \ diff --git a/images/known-4 b/images/known-4 new file mode 100644 index 0000000..f5bc74f --- /dev/null +++ b/images/known-4 @@ -0,0 +1,3 @@ +abc +def +ghi \ No newline at end of file diff --git a/images/known-5 b/images/known-5 new file mode 100644 index 0000000..3122d94 --- /dev/null +++ b/images/known-5 @@ -0,0 +1,2 @@ +abcdefghi +jklmnopqr \ No newline at end of file diff --git a/src/generator.ml b/src/generator.ml index c098ec8..547acb7 100755 --- a/src/generator.ml +++ b/src/generator.ml @@ -243,6 +243,10 @@ and test = *) | TestOutputLength of seq * int (* Run the command sequence and expect the output of the final + * command to be a buffer (RBufferOut), ie. string + size. + *) + | TestOutputBuffer of seq * string + (* Run the command sequence and expect the output of the final * command to be a structure. *) | TestOutputStruct of seq * test_field_compare list @@ -3033,9 +3037,8 @@ with C. See C for full details."); ("read_file", (RBufferOut "content", [String "path"]), 150, [ProtocolLimitWarning], - [InitBasicFS, Always, TestOutput ( - [["write_file"; "/new"; "new file contents"; "0"]; - ["read_file"; "/new"]], "new file contents")], + [InitSquashFS, Always, TestOutputBuffer ( + [["read_file"; "/known-4"]], "abc\ndef\nghi")], "read a file", "\ This calls returns the contents of the file C as a @@ -3471,7 +3474,8 @@ let seq_of_test = function | TestOutputListOfDevices (s, _) | TestOutputInt (s, _) | TestOutputIntOp (s, _, _) | TestOutputTrue s | TestOutputFalse s - | TestOutputLength (s, _) | TestOutputStruct (s, _) + | TestOutputLength (s, _) | TestOutputBuffer (s, _) + | TestOutputStruct (s, _) | TestLastFail s -> s (* Handling for function flags. *) @@ -5219,6 +5223,23 @@ 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 + | TestOutputBuffer (seq, expected) -> + pr " /* TestOutputBuffer for %s (%d) */\n" name i; + pr " const char *expected = \"%s\";\n" (c_quote expected); + let seq, last = get_seq_last seq in + let len = String.length expected in + let test () = + pr " if (size != %d) {\n" len; + pr " fprintf (stderr, \"%s: returned size of buffer wrong, expected %d but got %%zu\\n\", size);\n" test_name len; + pr " return -1;\n"; + pr " }\n"; + pr " if (strncmp (r, expected, size) != 0) {\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 | TestOutputStruct (seq, checks) -> pr " /* TestOutputStruct for %s (%d) */\n" name i; let seq, last = get_seq_last seq in