xref: /netbsd-src/external/bsd/kyua-testers/dist/atf_helpers.c (revision 82d56013d7b633d116a93943de88e08335357a7c)
1 // Copyright 2012 Google Inc.
2 // All rights reserved.
3 //
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are
6 // met:
7 //
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.
16 //
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.
28 
29 #include <signal.h>
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <unistd.h>
33 
34 #include <atf-c.h>
35 
36 
37 ATF_TC_WITHOUT_HEAD(fail);
38 ATF_TC_BODY(fail, tc)
39 {
40     fprintf(stdout, "First line to stdout\n");
41     fprintf(stderr, "First line to stderr\n");
42     atf_tc_fail("This is the failure message");
43 }
44 
45 
46 ATF_TC_WITHOUT_HEAD(pass);
47 ATF_TC_BODY(pass, tc)
48 {
49     fprintf(stdout, "First line to stdout\n");
50     fprintf(stdout, "Second line to stdout\n");
51     fprintf(stderr, "First line to stderr\n");
52     fprintf(stderr, "Second line to stderr\n");
53 }
54 
55 
56 ATF_TC_WITHOUT_HEAD(signal);
57 ATF_TC_BODY(signal, tc)
58 {
59     fprintf(stderr, "About to die due to SIGABRT!\n");
60     abort();
61 }
62 
63 
64 ATF_TC_WITHOUT_HEAD(sleep);
65 ATF_TC_BODY(sleep, tc)
66 {
67     sleep(300);
68 }
69 
70 
71 ATF_TC_WITH_CLEANUP(cleanup_check_work_directory);
72 ATF_TC_HEAD(cleanup_check_work_directory, tc) { }
73 ATF_TC_BODY(cleanup_check_work_directory, tc)
74 {
75     fprintf(stdout, "Body stdout\n");
76     fprintf(stderr, "Body stderr\n");
77     atf_utils_create_file("cookie-in-work-directory", "the value\n");
78 }
79 ATF_TC_CLEANUP(cleanup_check_work_directory, tc)
80 {
81     fprintf(stdout, "Cleanup stdout\n");
82     fprintf(stderr, "Cleanup stderr\n");
83     if (atf_utils_compare_file("cookie-in-work-directory", "the value\n")) {
84         printf("Cleanup properly ran in the same directory as the body\n");
85     } else {
86         printf("Cleanup did not run in the same directory as the body\n");
87     }
88 }
89 
90 
91 ATF_TC_WITH_CLEANUP(cleanup_fail);
92 ATF_TC_HEAD(cleanup_fail, tc) { }
93 ATF_TC_BODY(cleanup_fail, tc)
94 {
95 }
96 ATF_TC_CLEANUP(cleanup_fail, tc)
97 {
98     exit(EXIT_FAILURE);
99 }
100 
101 
102 ATF_TC_WITH_CLEANUP(cleanup_signal);
103 ATF_TC_HEAD(cleanup_signal, tc) { }
104 ATF_TC_BODY(cleanup_signal, tc) { }
105 ATF_TC_CLEANUP(cleanup_signal, tc)
106 {
107     fprintf(stderr, "About to die due to SIGABRT!\n");
108     abort();
109 }
110 
111 
112 ATF_TC_WITH_CLEANUP(cleanup_sleep);
113 ATF_TC_HEAD(cleanup_sleep, tc) { }
114 ATF_TC_BODY(cleanup_sleep, tc) { }
115 ATF_TC_CLEANUP(cleanup_sleep, tc)
116 {
117     sleep(300);
118 }
119 
120 
121 ATF_TC_WITH_CLEANUP(body_and_cleanup_fail);
122 ATF_TC_HEAD(body_and_cleanup_fail, tc) { }
123 ATF_TC_BODY(body_and_cleanup_fail, tc)
124 {
125     atf_tc_fail("Body fails");
126 }
127 ATF_TC_CLEANUP(body_and_cleanup_fail, tc)
128 {
129     printf("Killing cleanup\n");
130     fflush(stdout);
131     kill(getpid(), SIGKILL);
132 }
133 
134 
135 /// Prints a configuration variable if it exists.
136 ///
137 /// \param tc Caller test case.
138 /// \param part Identifier for the part of the test case.
139 /// \param name Name of the configuration variable.
140 static void
141 print_config_var(const atf_tc_t* tc, const char* part, const char* name)
142 {
143     if (atf_tc_has_config_var(tc, name))
144         printf("%s %s %s\n", part, name, atf_tc_get_config_var(tc, name));
145 }
146 
147 
148 /// Prints the configuration variables of the test case.
149 ///
150 /// Ideally we'd print all variables but the ATF interface does not have an API
151 /// to iterate over them all.  Instead, this just prints the variables we are
152 /// interested in for testing purposes.
153 ///
154 /// \param tc Caller test case.
155 /// \param part Identifier for the part of the test case.
156 static void
157 print_config_vars(const atf_tc_t* tc, const char* part)
158 {
159     print_config_var(tc, part, "my-var1");
160     print_config_var(tc, part, "v2");
161 }
162 
163 
164 ATF_TC_WITH_CLEANUP(print_config);
165 ATF_TC_HEAD(print_config, tc) {}
166 ATF_TC_BODY(print_config, tc)
167 {
168     print_config_vars(tc, "body");
169 }
170 ATF_TC_CLEANUP(print_config, tc)
171 {
172     print_config_vars(tc, "cleanup");
173 }
174 
175 
176 ATF_TP_ADD_TCS(tp)
177 {
178     ATF_TP_ADD_TC(tp, fail);
179     ATF_TP_ADD_TC(tp, pass);
180     ATF_TP_ADD_TC(tp, signal);
181     ATF_TP_ADD_TC(tp, sleep);
182 
183     ATF_TP_ADD_TC(tp, cleanup_check_work_directory);
184     ATF_TP_ADD_TC(tp, cleanup_fail);
185     ATF_TP_ADD_TC(tp, cleanup_signal);
186     ATF_TP_ADD_TC(tp, cleanup_sleep);
187 
188     ATF_TP_ADD_TC(tp, body_and_cleanup_fail);
189 
190     ATF_TP_ADD_TC(tp, print_config);
191 
192     return atf_no_error();
193 }
194