1 // Copyright 2012 Google Inc.
2 // All rights reserved.
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are
8 // * Redistributions of source code must retain the above copyright
9 // notice, this list of conditions and the following disclaimer.
10 // * Redistributions in binary form must reproduce the above copyright
11 // notice, this list of conditions and the following disclaimer in the
12 // documentation and/or other materials provided with the distribution.
13 // * Neither the name of Google Inc. nor the names of its contributors
14 // may be used to endorse or promote products derived from this software
15 // without specific prior written permission.
17 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
38 /// Opens a file for reading and fails the test case if the open fails.
40 /// \param name The file to open.
42 /// \return A valid file descriptor.
44 safe_open(const char* name
)
46 const int fd
= open(name
, O_RDONLY
);
47 ATF_REQUIRE(fd
!= -1);
52 /// Test atf_list_parse for a valid case.
54 /// \param input_name Result file to parse.
55 /// \param exp_output Expected output of the parsing.
57 do_ok_test(const char* input_name
, const char* exp_output
)
59 FILE* output
= fopen("output.txt", "w");
60 kyua_error_t error
= atf_list_parse(safe_open(input_name
), output
);
63 if (!atf_utils_compare_file("output.txt", exp_output
)) {
64 atf_utils_create_file("expout.txt", "%s", exp_output
);
65 atf_utils_cat_file("expout.txt", "EXPECTED: ");
66 atf_utils_cat_file("output.txt", "ACTUAL: ");
67 atf_tc_fail_nonfatal("Output of atf_list_parse does not match expected "
71 if (kyua_error_is_set(error
)) {
73 kyua_error_format(error
, message
, sizeof(message
));
74 kyua_error_free(error
);
75 atf_tc_fail("%s", message
);
80 /// Test atf_list_parse for an error case.
82 /// \param input_name Invalid result file to parse.
83 /// \param exp_type Expected error type.
84 /// \param exp_message Expected error message.
86 do_fail_test(const char* input_name
, const char* exp_type
,
87 const char* exp_message
)
89 FILE* output
= fopen("output.txt", "w");
90 kyua_error_t error
= atf_list_parse(safe_open(input_name
), output
);
93 ATF_REQUIRE(kyua_error_is_set(error
));
94 ATF_REQUIRE(kyua_error_is_type(error
, exp_type
));
97 kyua_error_format(error
, message
, sizeof(message
));
98 kyua_error_free(error
);
99 ATF_REQUIRE_MATCH(exp_message
, message
);
103 ATF_TC_WITHOUT_HEAD(parse__ok__one
);
104 ATF_TC_BODY(parse__ok__one
, tc
)
106 atf_utils_create_file(
108 "Content-Type: application/X-atf-tp; version=\"1\"\n"
113 "test_case{name='first'}\n");
117 ATF_TC_WITHOUT_HEAD(parse__ok__several
);
118 ATF_TC_BODY(parse__ok__several
, tc
)
120 atf_utils_create_file(
122 "Content-Type: application/X-atf-tp; version=\"1\"\n"
125 "require.user: root\n"
130 "descr: A string with an embedded ' and \\' in it\n"
131 "has.cleanup: true\n"
136 "test_case{name='first', required_user='root'}\n"
137 "test_case{name='second'}\n"
138 "test_case{name='third', description='A string with an embedded \\' "
139 "and \\\\\\' in it', has_cleanup='true', ['custom.X-custom']='foo', "
140 "['custom.X-a\\'b']='bar'}\n");
144 ATF_TC_WITHOUT_HEAD(parse__error__bad_fd
);
145 ATF_TC_BODY(parse__error__bad_fd
, tc
)
148 kyua_error_t error
= atf_list_parse(10, stdout
);
150 ATF_REQUIRE(kyua_error_is_set(error
));
151 ATF_REQUIRE(kyua_error_is_type(error
, "libc"));
154 kyua_error_format(error
, message
, sizeof(message
));
155 kyua_error_free(error
);
156 ATF_REQUIRE_MATCH("fdopen\\(10\\) failed", message
);
160 ATF_TC_WITHOUT_HEAD(parse__error__bad_header
);
161 ATF_TC_BODY(parse__error__bad_header
, tc
)
163 atf_utils_create_file("input.txt", "%s", "");
164 do_fail_test("input.txt", "generic",
165 "fgets failed to read test cases list header");
167 atf_utils_create_file(
169 "Content-Type: application/X-atf-tp; version=\"1234\"");
170 do_fail_test("input.txt", "generic",
171 "Invalid test cases list header '.*X-atf-tp.*1234.*'");
173 atf_utils_create_file(
175 "Content-Type: application/X-atf-tp; version=\"1\"\n");
176 do_fail_test("input.txt", "generic",
177 "fgets failed to read test cases list header");
179 atf_utils_create_file(
181 "Content-Type: application/X-atf-tp; version=\"1\"\n"
184 do_fail_test("input.txt", "generic",
185 "Incomplete test cases list header");
189 ATF_TC_WITHOUT_HEAD(parse__error__empty
);
190 ATF_TC_BODY(parse__error__empty
, tc
)
192 atf_utils_create_file(
194 "Content-Type: application/X-atf-tp; version=\"1\"\n"
196 do_fail_test("input.txt", "generic",
197 "Empty test cases list: unexpected EOF");
201 ATF_TC_WITHOUT_HEAD(parse__error__bad_property
);
202 ATF_TC_BODY(parse__error__bad_property
, tc
)
204 atf_utils_create_file(
206 "Content-Type: application/X-atf-tp; version=\"1\"\n"
209 do_fail_test("input.txt", "generic", "Invalid property 'oh noes; 2'");
213 ATF_TC_WITHOUT_HEAD(parse__error__bad_order
);
214 ATF_TC_BODY(parse__error__bad_order
, tc
)
216 atf_utils_create_file(
218 "Content-Type: application/X-atf-tp; version=\"1\"\n"
222 do_fail_test("input.txt", "generic", "Expected ident property, got descr");
228 ATF_TP_ADD_TC(tp
, parse__ok__one
);
229 ATF_TP_ADD_TC(tp
, parse__ok__several
);
230 ATF_TP_ADD_TC(tp
, parse__error__bad_fd
);
231 ATF_TP_ADD_TC(tp
, parse__error__bad_header
);
232 ATF_TP_ADD_TC(tp
, parse__error__empty
);
233 ATF_TP_ADD_TC(tp
, parse__error__bad_property
);
234 ATF_TP_ADD_TC(tp
, parse__error__bad_order
);
236 return atf_no_error();