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.
37 ATF_TC_WITHOUT_HEAD(fail
);
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");
46 ATF_TC_WITHOUT_HEAD(pass
);
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");
56 ATF_TC_WITHOUT_HEAD(signal
);
57 ATF_TC_BODY(signal
, tc
)
59 fprintf(stderr
, "About to die due to SIGABRT!\n");
64 ATF_TC_WITHOUT_HEAD(sleep
);
65 ATF_TC_BODY(sleep
, tc
)
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
)
75 fprintf(stdout
, "Body stdout\n");
76 fprintf(stderr
, "Body stderr\n");
77 atf_utils_create_file("cookie-in-work-directory", "the value\n");
79 ATF_TC_CLEANUP(cleanup_check_work_directory
, tc
)
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");
86 printf("Cleanup did not run in the same directory as the body\n");
91 ATF_TC_WITH_CLEANUP(cleanup_fail
);
92 ATF_TC_HEAD(cleanup_fail
, tc
) { }
93 ATF_TC_BODY(cleanup_fail
, tc
)
96 ATF_TC_CLEANUP(cleanup_fail
, tc
)
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
)
107 fprintf(stderr
, "About to die due to SIGABRT!\n");
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
)
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
)
125 atf_tc_fail("Body fails");
127 ATF_TC_CLEANUP(body_and_cleanup_fail
, tc
)
129 printf("Killing cleanup\n");
131 kill(getpid(), SIGKILL
);
135 /// Prints a configuration variable if it exists.
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.
141 print_config_var(const atf_tc_t
* tc
, const char* part
, const char* name
)
143 if (atf_tc_has_config_var(tc
, name
))
144 printf("%s %s %s\n", part
, name
, atf_tc_get_config_var(tc
, name
));
148 /// Prints the configuration variables of the test case.
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.
154 /// \param tc Caller test case.
155 /// \param part Identifier for the part of the test case.
157 print_config_vars(const atf_tc_t
* tc
, const char* part
)
159 print_config_var(tc
, part
, "my-var1");
160 print_config_var(tc
, part
, "v2");
164 ATF_TC_WITH_CLEANUP(print_config
);
165 ATF_TC_HEAD(print_config
, tc
) {}
166 ATF_TC_BODY(print_config
, tc
)
168 print_config_vars(tc
, "body");
170 ATF_TC_CLEANUP(print_config
, tc
)
172 print_config_vars(tc
, "cleanup");
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
);
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
);
188 ATF_TP_ADD_TC(tp
, body_and_cleanup_fail
);
190 ATF_TP_ADD_TC(tp
, print_config
);
192 return atf_no_error();