xref: /minix3/external/bsd/kyua-testers/dist/atf_inttest.c (revision 11be35a165022172ed3cea20f2b5df0307540b0e)
1*11be35a1SLionel Sambuc // Copyright 2012 Google Inc.
2*11be35a1SLionel Sambuc // All rights reserved.
3*11be35a1SLionel Sambuc //
4*11be35a1SLionel Sambuc // Redistribution and use in source and binary forms, with or without
5*11be35a1SLionel Sambuc // modification, are permitted provided that the following conditions are
6*11be35a1SLionel Sambuc // met:
7*11be35a1SLionel Sambuc //
8*11be35a1SLionel Sambuc // * Redistributions of source code must retain the above copyright
9*11be35a1SLionel Sambuc //   notice, this list of conditions and the following disclaimer.
10*11be35a1SLionel Sambuc // * Redistributions in binary form must reproduce the above copyright
11*11be35a1SLionel Sambuc //   notice, this list of conditions and the following disclaimer in the
12*11be35a1SLionel Sambuc //   documentation and/or other materials provided with the distribution.
13*11be35a1SLionel Sambuc // * Neither the name of Google Inc. nor the names of its contributors
14*11be35a1SLionel Sambuc //   may be used to endorse or promote products derived from this software
15*11be35a1SLionel Sambuc //   without specific prior written permission.
16*11be35a1SLionel Sambuc //
17*11be35a1SLionel Sambuc // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18*11be35a1SLionel Sambuc // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19*11be35a1SLionel Sambuc // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20*11be35a1SLionel Sambuc // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21*11be35a1SLionel Sambuc // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22*11be35a1SLionel Sambuc // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23*11be35a1SLionel Sambuc // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24*11be35a1SLionel Sambuc // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25*11be35a1SLionel Sambuc // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26*11be35a1SLionel Sambuc // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27*11be35a1SLionel Sambuc // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28*11be35a1SLionel Sambuc 
29*11be35a1SLionel Sambuc #include <stdlib.h>
30*11be35a1SLionel Sambuc 
31*11be35a1SLionel Sambuc #include <atf-c.h>
32*11be35a1SLionel Sambuc 
33*11be35a1SLionel Sambuc #define INTERFACE "atf"
34*11be35a1SLionel Sambuc #include "common_inttest.h"
35*11be35a1SLionel Sambuc 
36*11be35a1SLionel Sambuc 
37*11be35a1SLionel Sambuc ATF_TC(list__ok);
ATF_TC_HEAD(list__ok,tc)38*11be35a1SLionel Sambuc ATF_TC_HEAD(list__ok, tc) { setup(tc, true); }
ATF_TC_BODY(list__ok,tc)39*11be35a1SLionel Sambuc ATF_TC_BODY(list__ok, tc)
40*11be35a1SLionel Sambuc {
41*11be35a1SLionel Sambuc     char* helpers = helpers_path(tc);
42*11be35a1SLionel Sambuc     check(EXIT_SUCCESS,
43*11be35a1SLionel Sambuc           "test_case{name='fail'}\n"
44*11be35a1SLionel Sambuc           "test_case{name='pass'}\n"
45*11be35a1SLionel Sambuc           "test_case{name='signal'}\n"
46*11be35a1SLionel Sambuc           "test_case{name='sleep'}\n"
47*11be35a1SLionel Sambuc           "test_case{name='cleanup_check_work_directory', has_cleanup='true'}\n"
48*11be35a1SLionel Sambuc           "test_case{name='cleanup_fail', has_cleanup='true'}\n"
49*11be35a1SLionel Sambuc           "test_case{name='cleanup_signal', has_cleanup='true'}\n"
50*11be35a1SLionel Sambuc           "test_case{name='cleanup_sleep', has_cleanup='true'}\n"
51*11be35a1SLionel Sambuc           "test_case{name='body_and_cleanup_fail', has_cleanup='true'}\n"
52*11be35a1SLionel Sambuc           "test_case{name='print_config', has_cleanup='true'}\n",
53*11be35a1SLionel Sambuc           "",
54*11be35a1SLionel Sambuc           "list", helpers, NULL);
55*11be35a1SLionel Sambuc     free(helpers);
56*11be35a1SLionel Sambuc }
57*11be35a1SLionel Sambuc 
58*11be35a1SLionel Sambuc 
59*11be35a1SLionel Sambuc ATF_TC(test__pass);
ATF_TC_HEAD(test__pass,tc)60*11be35a1SLionel Sambuc ATF_TC_HEAD(test__pass, tc) { setup(tc, true); }
ATF_TC_BODY(test__pass,tc)61*11be35a1SLionel Sambuc ATF_TC_BODY(test__pass, tc)
62*11be35a1SLionel Sambuc {
63*11be35a1SLionel Sambuc     char* helpers = helpers_path(tc);
64*11be35a1SLionel Sambuc     check(EXIT_SUCCESS,
65*11be35a1SLionel Sambuc           "First line to stdout\nSecond line to stdout\n",
66*11be35a1SLionel Sambuc           "First line to stderr\nSecond line to stderr\n",
67*11be35a1SLionel Sambuc           "test", helpers, "pass", "test-result", NULL);
68*11be35a1SLionel Sambuc     free(helpers);
69*11be35a1SLionel Sambuc 
70*11be35a1SLionel Sambuc     ATF_REQUIRE(atf_utils_compare_file("test-result", "passed\n"));
71*11be35a1SLionel Sambuc }
72*11be35a1SLionel Sambuc 
73*11be35a1SLionel Sambuc 
74*11be35a1SLionel Sambuc ATF_TC(test__fail);
ATF_TC_HEAD(test__fail,tc)75*11be35a1SLionel Sambuc ATF_TC_HEAD(test__fail, tc) { setup(tc, true); }
ATF_TC_BODY(test__fail,tc)76*11be35a1SLionel Sambuc ATF_TC_BODY(test__fail, tc)
77*11be35a1SLionel Sambuc {
78*11be35a1SLionel Sambuc     char* helpers = helpers_path(tc);
79*11be35a1SLionel Sambuc     check(EXIT_FAILURE, "First line to stdout\n", "First line to stderr\n",
80*11be35a1SLionel Sambuc           "test", helpers, "fail", "test-result", NULL);
81*11be35a1SLionel Sambuc     free(helpers);
82*11be35a1SLionel Sambuc 
83*11be35a1SLionel Sambuc     ATF_REQUIRE(atf_utils_compare_file("test-result",
84*11be35a1SLionel Sambuc         "failed: This is the failure message\n"));
85*11be35a1SLionel Sambuc }
86*11be35a1SLionel Sambuc 
87*11be35a1SLionel Sambuc 
88*11be35a1SLionel Sambuc ATF_TC(test__crash);
ATF_TC_HEAD(test__crash,tc)89*11be35a1SLionel Sambuc ATF_TC_HEAD(test__crash, tc) { setup(tc, true); }
ATF_TC_BODY(test__crash,tc)90*11be35a1SLionel Sambuc ATF_TC_BODY(test__crash, tc)
91*11be35a1SLionel Sambuc {
92*11be35a1SLionel Sambuc     char* helpers = helpers_path(tc);
93*11be35a1SLionel Sambuc     check(EXIT_FAILURE, "", "save:crash.err",
94*11be35a1SLionel Sambuc           "test", helpers, "signal", "test-result", NULL);
95*11be35a1SLionel Sambuc     free(helpers);
96*11be35a1SLionel Sambuc 
97*11be35a1SLionel Sambuc     ATF_REQUIRE(atf_utils_compare_file("test-result",
98*11be35a1SLionel Sambuc         "broken: Premature exit; test case received signal 6 (core dumped)\n"));
99*11be35a1SLionel Sambuc 
100*11be35a1SLionel Sambuc     ATF_REQUIRE(atf_utils_grep_file("About to die due to SIGABRT!",
101*11be35a1SLionel Sambuc                                     "crash.err"));
102*11be35a1SLionel Sambuc     ATF_REQUIRE(atf_utils_grep_file("attempting to gather stack trace",
103*11be35a1SLionel Sambuc                                     "crash.err"));
104*11be35a1SLionel Sambuc }
105*11be35a1SLionel Sambuc 
106*11be35a1SLionel Sambuc 
107*11be35a1SLionel Sambuc ATF_TC(test__timeout);
ATF_TC_HEAD(test__timeout,tc)108*11be35a1SLionel Sambuc ATF_TC_HEAD(test__timeout, tc) { setup(tc, true); }
ATF_TC_BODY(test__timeout,tc)109*11be35a1SLionel Sambuc ATF_TC_BODY(test__timeout, tc)
110*11be35a1SLionel Sambuc {
111*11be35a1SLionel Sambuc     char* helpers = helpers_path(tc);
112*11be35a1SLionel Sambuc     check(EXIT_FAILURE, "", "Subprocess timed out; sending KILL signal...\n",
113*11be35a1SLionel Sambuc           "-t1", "test", helpers, "sleep", "test-result", NULL);
114*11be35a1SLionel Sambuc     free(helpers);
115*11be35a1SLionel Sambuc 
116*11be35a1SLionel Sambuc     ATF_REQUIRE(atf_utils_compare_file("test-result",
117*11be35a1SLionel Sambuc                                        "broken: Test case body timed out\n"));
118*11be35a1SLionel Sambuc }
119*11be35a1SLionel Sambuc 
120*11be35a1SLionel Sambuc 
121*11be35a1SLionel Sambuc ATF_TC(test__result_priority);
ATF_TC_HEAD(test__result_priority,tc)122*11be35a1SLionel Sambuc ATF_TC_HEAD(test__result_priority, tc) { setup(tc, true); }
ATF_TC_BODY(test__result_priority,tc)123*11be35a1SLionel Sambuc ATF_TC_BODY(test__result_priority, tc)
124*11be35a1SLionel Sambuc {
125*11be35a1SLionel Sambuc     char* helpers = helpers_path(tc);
126*11be35a1SLionel Sambuc     check(EXIT_FAILURE, "Killing cleanup\n", "",
127*11be35a1SLionel Sambuc           "test", "-vhas.cleanup=true", helpers, "body_and_cleanup_fail",
128*11be35a1SLionel Sambuc           "test-result", NULL);
129*11be35a1SLionel Sambuc     free(helpers);
130*11be35a1SLionel Sambuc 
131*11be35a1SLionel Sambuc     ATF_REQUIRE(atf_utils_compare_file("test-result", "failed: Body fails\n"));
132*11be35a1SLionel Sambuc }
133*11be35a1SLionel Sambuc 
134*11be35a1SLionel Sambuc 
135*11be35a1SLionel Sambuc ATF_TC(test__cleanup__ok);
ATF_TC_HEAD(test__cleanup__ok,tc)136*11be35a1SLionel Sambuc ATF_TC_HEAD(test__cleanup__ok, tc) { setup(tc, true); }
ATF_TC_BODY(test__cleanup__ok,tc)137*11be35a1SLionel Sambuc ATF_TC_BODY(test__cleanup__ok, tc)
138*11be35a1SLionel Sambuc {
139*11be35a1SLionel Sambuc     char* helpers = helpers_path(tc);
140*11be35a1SLionel Sambuc     check(EXIT_SUCCESS,
141*11be35a1SLionel Sambuc           "Body stdout\nCleanup stdout\n"
142*11be35a1SLionel Sambuc               "Cleanup properly ran in the same directory as the body\n",
143*11be35a1SLionel Sambuc           "Body stderr\nCleanup stderr\n",
144*11be35a1SLionel Sambuc           "test", "-vhas.cleanup=true", helpers, "cleanup_check_work_directory",
145*11be35a1SLionel Sambuc           "test-result", NULL);
146*11be35a1SLionel Sambuc     free(helpers);
147*11be35a1SLionel Sambuc 
148*11be35a1SLionel Sambuc     ATF_REQUIRE(atf_utils_compare_file("test-result", "passed\n"));
149*11be35a1SLionel Sambuc }
150*11be35a1SLionel Sambuc 
151*11be35a1SLionel Sambuc 
152*11be35a1SLionel Sambuc ATF_TC(test__cleanup__fail);
ATF_TC_HEAD(test__cleanup__fail,tc)153*11be35a1SLionel Sambuc ATF_TC_HEAD(test__cleanup__fail, tc) { setup(tc, true); }
ATF_TC_BODY(test__cleanup__fail,tc)154*11be35a1SLionel Sambuc ATF_TC_BODY(test__cleanup__fail, tc)
155*11be35a1SLionel Sambuc {
156*11be35a1SLionel Sambuc     char* helpers = helpers_path(tc);
157*11be35a1SLionel Sambuc     check(EXIT_FAILURE, "", "",
158*11be35a1SLionel Sambuc           "test", "-vhas.cleanup=true", helpers, "cleanup_fail", "test-result",
159*11be35a1SLionel Sambuc           NULL);
160*11be35a1SLionel Sambuc     free(helpers);
161*11be35a1SLionel Sambuc 
162*11be35a1SLionel Sambuc     ATF_REQUIRE(atf_utils_compare_file("test-result", "broken: Test case "
163*11be35a1SLionel Sambuc         "cleanup exited with code 1\n"));
164*11be35a1SLionel Sambuc }
165*11be35a1SLionel Sambuc 
166*11be35a1SLionel Sambuc 
167*11be35a1SLionel Sambuc ATF_TC(test__cleanup__crash);
ATF_TC_HEAD(test__cleanup__crash,tc)168*11be35a1SLionel Sambuc ATF_TC_HEAD(test__cleanup__crash, tc) { setup(tc, true); }
ATF_TC_BODY(test__cleanup__crash,tc)169*11be35a1SLionel Sambuc ATF_TC_BODY(test__cleanup__crash, tc)
170*11be35a1SLionel Sambuc {
171*11be35a1SLionel Sambuc     char* helpers = helpers_path(tc);
172*11be35a1SLionel Sambuc     check(EXIT_FAILURE, "", "save:crash.err",
173*11be35a1SLionel Sambuc           "test", "-vhas.cleanup=true", helpers, "cleanup_signal",
174*11be35a1SLionel Sambuc           "test-result", NULL);
175*11be35a1SLionel Sambuc     free(helpers);
176*11be35a1SLionel Sambuc 
177*11be35a1SLionel Sambuc     ATF_REQUIRE(atf_utils_compare_file("test-result",
178*11be35a1SLionel Sambuc         "broken: Test case cleanup received signal 6 (core dumped)\n"));
179*11be35a1SLionel Sambuc 
180*11be35a1SLionel Sambuc     ATF_REQUIRE(atf_utils_grep_file("About to die due to SIGABRT!",
181*11be35a1SLionel Sambuc                                     "crash.err"));
182*11be35a1SLionel Sambuc     ATF_REQUIRE(atf_utils_grep_file("attempting to gather stack trace",
183*11be35a1SLionel Sambuc                                     "crash.err"));
184*11be35a1SLionel Sambuc }
185*11be35a1SLionel Sambuc 
186*11be35a1SLionel Sambuc 
187*11be35a1SLionel Sambuc ATF_TC(test__cleanup__timeout);
ATF_TC_HEAD(test__cleanup__timeout,tc)188*11be35a1SLionel Sambuc ATF_TC_HEAD(test__cleanup__timeout, tc) { setup(tc, true); }
ATF_TC_BODY(test__cleanup__timeout,tc)189*11be35a1SLionel Sambuc ATF_TC_BODY(test__cleanup__timeout, tc)
190*11be35a1SLionel Sambuc {
191*11be35a1SLionel Sambuc     char* helpers = helpers_path(tc);
192*11be35a1SLionel Sambuc     check(EXIT_FAILURE, "", "Subprocess timed out; sending KILL signal...\n",
193*11be35a1SLionel Sambuc           "-t1", "test", "-vhas.cleanup=true", helpers, "cleanup_sleep",
194*11be35a1SLionel Sambuc           "test-result", NULL);
195*11be35a1SLionel Sambuc     free(helpers);
196*11be35a1SLionel Sambuc 
197*11be35a1SLionel Sambuc     ATF_REQUIRE(atf_utils_compare_file("test-result", "broken: Test case "
198*11be35a1SLionel Sambuc         "cleanup timed out\n"));
199*11be35a1SLionel Sambuc }
200*11be35a1SLionel Sambuc 
201*11be35a1SLionel Sambuc 
202*11be35a1SLionel Sambuc ATF_TC(test__config__builtin);
ATF_TC_HEAD(test__config__builtin,tc)203*11be35a1SLionel Sambuc ATF_TC_HEAD(test__config__builtin, tc) { setup(tc, true); }
ATF_TC_BODY(test__config__builtin,tc)204*11be35a1SLionel Sambuc ATF_TC_BODY(test__config__builtin, tc)
205*11be35a1SLionel Sambuc {
206*11be35a1SLionel Sambuc     char* helpers = helpers_path(tc);
207*11be35a1SLionel Sambuc     check(EXIT_SUCCESS, "", "",
208*11be35a1SLionel Sambuc           "test", helpers, "print_config", "test-result", NULL);
209*11be35a1SLionel Sambuc     free(helpers);
210*11be35a1SLionel Sambuc 
211*11be35a1SLionel Sambuc     ATF_REQUIRE(atf_utils_compare_file("test-result", "passed\n"));
212*11be35a1SLionel Sambuc }
213*11be35a1SLionel Sambuc 
214*11be35a1SLionel Sambuc 
215*11be35a1SLionel Sambuc ATF_TC(test__config__custom);
ATF_TC_HEAD(test__config__custom,tc)216*11be35a1SLionel Sambuc ATF_TC_HEAD(test__config__custom, tc) { setup(tc, true); }
ATF_TC_BODY(test__config__custom,tc)217*11be35a1SLionel Sambuc ATF_TC_BODY(test__config__custom, tc)
218*11be35a1SLionel Sambuc {
219*11be35a1SLionel Sambuc     char* helpers = helpers_path(tc);
220*11be35a1SLionel Sambuc     check(EXIT_SUCCESS,
221*11be35a1SLionel Sambuc           "body my-var1 value1\n"
222*11be35a1SLionel Sambuc           "body v2 a b c foo\n"
223*11be35a1SLionel Sambuc           "cleanup my-var1 value1\n"
224*11be35a1SLionel Sambuc           "cleanup v2 a b c foo\n",
225*11be35a1SLionel Sambuc           "",
226*11be35a1SLionel Sambuc           "test", "-vmy-var1=value1", "-vv2=a b c foo", helpers,
227*11be35a1SLionel Sambuc           "print_config", "test-result", NULL);
228*11be35a1SLionel Sambuc     free(helpers);
229*11be35a1SLionel Sambuc 
230*11be35a1SLionel Sambuc     ATF_REQUIRE(atf_utils_compare_file("test-result", "passed\n"));
231*11be35a1SLionel Sambuc }
232*11be35a1SLionel Sambuc 
233*11be35a1SLionel Sambuc 
234*11be35a1SLionel Sambuc ATF_TC(test__invalid_test_case_name);
ATF_TC_HEAD(test__invalid_test_case_name,tc)235*11be35a1SLionel Sambuc ATF_TC_HEAD(test__invalid_test_case_name, tc) { setup(tc, false); }
ATF_TC_BODY(test__invalid_test_case_name,tc)236*11be35a1SLionel Sambuc ATF_TC_BODY(test__invalid_test_case_name, tc)
237*11be35a1SLionel Sambuc {
238*11be35a1SLionel Sambuc     char* helpers = helpers_path(tc);
239*11be35a1SLionel Sambuc     check(EXIT_FAILURE, "",  // TODO(jmmv): Should be EXIT_INTERNAL_ERROR.
240*11be35a1SLionel Sambuc           "atf_helpers: ERROR: Unknown test case `foo'\n"
241*11be35a1SLionel Sambuc           "atf_helpers: See atf-test-program(1) for usage details.\n",
242*11be35a1SLionel Sambuc           "test", "-vhas.cleanup=false", helpers, "foo", "test-result", NULL);
243*11be35a1SLionel Sambuc     free(helpers);
244*11be35a1SLionel Sambuc 
245*11be35a1SLionel Sambuc     ATF_REQUIRE(atf_utils_compare_file("test-result",
246*11be35a1SLionel Sambuc         "broken: Premature exit; test case exited with code 1\n"));
247*11be35a1SLionel Sambuc }
248*11be35a1SLionel Sambuc 
249*11be35a1SLionel Sambuc 
250*11be35a1SLionel Sambuc ATF_TC(test__missing_test_program);
ATF_TC_HEAD(test__missing_test_program,tc)251*11be35a1SLionel Sambuc ATF_TC_HEAD(test__missing_test_program, tc) { setup(tc, false); }
ATF_TC_BODY(test__missing_test_program,tc)252*11be35a1SLionel Sambuc ATF_TC_BODY(test__missing_test_program, tc)
253*11be35a1SLionel Sambuc {
254*11be35a1SLionel Sambuc     check(EXIT_INTERNAL_ERROR, "",
255*11be35a1SLionel Sambuc           "kyua-atf-tester: execvp failed: No such file or directory\n",
256*11be35a1SLionel Sambuc           "test", "./non-existent", "pass", "test-result", NULL);
257*11be35a1SLionel Sambuc 
258*11be35a1SLionel Sambuc     ATF_REQUIRE(!atf_utils_file_exists("test-result"));
259*11be35a1SLionel Sambuc }
260*11be35a1SLionel Sambuc 
261*11be35a1SLionel Sambuc 
ATF_TP_ADD_TCS(tp)262*11be35a1SLionel Sambuc ATF_TP_ADD_TCS(tp)
263*11be35a1SLionel Sambuc {
264*11be35a1SLionel Sambuc     ATF_TP_ADD_TC(tp, top__missing_command);
265*11be35a1SLionel Sambuc     ATF_TP_ADD_TC(tp, top__unknown_command);
266*11be35a1SLionel Sambuc 
267*11be35a1SLionel Sambuc     ATF_TP_ADD_TC(tp, list__ok);
268*11be35a1SLionel Sambuc 
269*11be35a1SLionel Sambuc     ATF_TP_ADD_TC(tp, test__pass);
270*11be35a1SLionel Sambuc     ATF_TP_ADD_TC(tp, test__fail);
271*11be35a1SLionel Sambuc     ATF_TP_ADD_TC(tp, test__crash);
272*11be35a1SLionel Sambuc     ATF_TP_ADD_TC(tp, test__timeout);
273*11be35a1SLionel Sambuc     ATF_TP_ADD_TC(tp, test__result_priority);
274*11be35a1SLionel Sambuc     ATF_TP_ADD_TC(tp, test__cleanup__ok);
275*11be35a1SLionel Sambuc     ATF_TP_ADD_TC(tp, test__cleanup__fail);
276*11be35a1SLionel Sambuc     ATF_TP_ADD_TC(tp, test__cleanup__crash);
277*11be35a1SLionel Sambuc     ATF_TP_ADD_TC(tp, test__cleanup__timeout);
278*11be35a1SLionel Sambuc     ATF_TP_ADD_TC(tp, test__config__builtin);
279*11be35a1SLionel Sambuc     ATF_TP_ADD_TC(tp, test__config__custom);
280*11be35a1SLionel Sambuc     ATF_TP_ADD_TC(tp, test__missing_test_program);
281*11be35a1SLionel Sambuc     ATF_TP_ADD_TC(tp, test__invalid_test_case_name);
282*11be35a1SLionel Sambuc 
283*11be35a1SLionel Sambuc     return atf_no_error();
284*11be35a1SLionel Sambuc }
285