2 * Automated Testing Framework (atf)
4 * Copyright (c) 2010 The NetBSD Foundation, Inc.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND
17 * CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
18 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
19 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY
21 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
23 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
25 * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
26 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
27 * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 #include "atf-c/utils.h"
46 #include "detail/dynstr.h"
48 /** Searches for a regexp in a string.
50 * \param regex The regexp to look for.
51 * \param str The string in which to look for the expression.
53 * \return True if there is a match; false otherwise. */
56 grep_string(const char *regex, const char *str)
61 printf("Looking for '%s' in '%s'\n", regex, str);
62 ATF_REQUIRE(regcomp(&preg, regex, REG_EXTENDED) == 0);
64 res = regexec(&preg, str, 0, NULL, 0);
65 ATF_REQUIRE(res == 0 || res == REG_NOMATCH);
72 /** Prints the contents of a file to stdout.
74 * \param name The name of the file to be printed.
75 * \param prefix An string to be prepended to every line of the printed
78 atf_utils_cat_file(const char *name, const char *prefix)
80 const int fd = open(name, O_RDONLY);
81 ATF_REQUIRE_MSG(fd != -1, "Cannot open %s", name);
85 bool continued = false;
86 while ((count = read(fd, buffer, sizeof(buffer) - 1)) > 0) {
94 while ((end = strchr(iter, '\n')) != NULL) {
99 if (iter != buffer + count)
100 printf("%s", prefix);
104 if (iter < buffer + count) {
109 ATF_REQUIRE(count == 0);
112 /** Compares a file against the given golden contents.
114 * \param name Name of the file to be compared.
115 * \param contents Expected contents of the file.
117 * \return True if the file matches the contents; false otherwise. */
119 atf_utils_compare_file(const char *name, const char *contents)
121 const int fd = open(name, O_RDONLY);
122 ATF_REQUIRE_MSG(fd != -1, "Cannot open %s", name);
124 const char *pos = contents;
125 ssize_t remaining = strlen(contents);
129 while ((count = read(fd, buffer, sizeof(buffer))) > 0 &&
130 count <= remaining) {
131 if (memcmp(pos, buffer, count) != 0) {
139 return count == 0 && remaining == 0;
144 * \param source Path to the source file.
145 * \param destination Path to the destination file. */
147 atf_utils_copy_file(const char *source, const char *destination)
149 const int input = open(source, O_RDONLY);
150 ATF_REQUIRE_MSG(input != -1, "Failed to open source file during "
151 "copy (%s)", source);
153 const int output = open(destination, O_WRONLY | O_CREAT | O_TRUNC, 0777);
154 ATF_REQUIRE_MSG(output != -1, "Failed to open destination file during "
155 "copy (%s)", destination);
159 while ((length = read(input, buffer, sizeof(buffer))) > 0)
160 ATF_REQUIRE_MSG(write(output, buffer, length) == length,
161 "Failed to write to %s during copy", destination);
162 ATF_REQUIRE_MSG(length != -1, "Failed to read from %s during copy", source);
165 ATF_REQUIRE_MSG(fstat(input, &sb) != -1,
166 "Failed to stat source file %s during copy", source);
167 ATF_REQUIRE_MSG(fchmod(output, sb.st_mode) != -1,
168 "Failed to chmod destination file %s during copy",
177 * \param name Name of the file to create.
178 * \param contents Text to write into the created file.
179 * \param ... Positional parameters to the contents. */
181 atf_utils_create_file(const char *name, const char *contents, ...)
184 atf_dynstr_t formatted;
187 va_start(ap, contents);
188 error = atf_dynstr_init_ap(&formatted, contents, ap);
190 ATF_REQUIRE(!atf_is_error(error));
192 const int fd = open(name, O_WRONLY | O_CREAT | O_TRUNC, 0644);
193 ATF_REQUIRE_MSG(fd != -1, "Cannot create file %s", name);
194 ATF_REQUIRE(write(fd, atf_dynstr_cstring(&formatted),
195 atf_dynstr_length(&formatted)) != -1);
198 atf_dynstr_fini(&formatted);
201 /** Checks if a file exists.
203 * \param path Location of the file to check for.
205 * \return True if the file exists, false otherwise. */
207 atf_utils_file_exists(const char *path)
209 const int ret = access(path, F_OK);
212 atf_tc_fail("Failed to check the existence of %s: %s", path,
220 /** Spawns a subprocess and redirects its output to files.
222 * Use the atf_utils_wait() function to wait for the completion of the spawned
223 * subprocess and validate its exit conditions.
225 * \return 0 in the new child; the PID of the new child in the parent. Does
226 * not return in error conditions. */
230 const pid_t pid = fork();
232 atf_tc_fail("fork failed");
235 atf_utils_redirect(STDOUT_FILENO, "atf_utils_fork_out.txt");
236 atf_utils_redirect(STDERR_FILENO, "atf_utils_fork_err.txt");
241 /** Frees an dynamically-allocated "argv" array.
243 * \param argv A dynamically-allocated array of dynamically-allocated
246 atf_utils_free_charpp(char **argv)
250 for (ptr = argv; *ptr != NULL; ptr++)
256 /** Searches for a regexp in a file.
258 * \param regex The regexp to look for.
259 * \param file The file in which to look for the expression.
260 * \param ... Positional parameters to the regex.
262 * \return True if there is a match; false otherwise. */
264 atf_utils_grep_file(const char *regex, const char *file, ...)
268 atf_dynstr_t formatted;
272 error = atf_dynstr_init_ap(&formatted, regex, ap);
274 ATF_REQUIRE(!atf_is_error(error));
276 ATF_REQUIRE((fd = open(file, O_RDONLY)) != -1);
279 while (!found && (line = atf_utils_readline(fd)) != NULL) {
280 found = grep_string(atf_dynstr_cstring(&formatted), line);
285 atf_dynstr_fini(&formatted);
290 /** Searches for a regexp in a string.
292 * \param regex The regexp to look for.
293 * \param str The string in which to look for the expression.
294 * \param ... Positional parameters to the regex.
296 * \return True if there is a match; false otherwise. */
298 atf_utils_grep_string(const char *regex, const char *str, ...)
302 atf_dynstr_t formatted;
306 error = atf_dynstr_init_ap(&formatted, regex, ap);
308 ATF_REQUIRE(!atf_is_error(error));
310 res = grep_string(atf_dynstr_cstring(&formatted), str);
312 atf_dynstr_fini(&formatted);
317 /** Reads a line of arbitrary length.
319 * \param fd The descriptor from which to read the line.
321 * \return A pointer to the read line, which must be released with free(), or
322 * NULL if there was nothing to read from the file. */
324 atf_utils_readline(const int fd)
331 error = atf_dynstr_init(&temp);
332 ATF_REQUIRE(!atf_is_error(error));
334 while ((cnt = read(fd, &ch, sizeof(ch))) == sizeof(ch) &&
336 error = atf_dynstr_append_fmt(&temp, "%c", ch);
337 ATF_REQUIRE(!atf_is_error(error));
339 ATF_REQUIRE(cnt != -1);
341 if (cnt == 0 && atf_dynstr_length(&temp) == 0) {
342 atf_dynstr_fini(&temp);
345 return atf_dynstr_fini_disown(&temp);
348 /** Redirects a file descriptor to a file.
350 * \param target_fd The file descriptor to be replaced.
351 * \param name The name of the file to direct the descriptor to.
353 * \pre Should only be called from the process spawned by fork_for_testing
354 * because this exits uncontrolledly.
355 * \post Terminates execution if the redirection fails. */
357 atf_utils_redirect(const int target_fd, const char *name)
359 if (target_fd == STDOUT_FILENO)
361 else if (target_fd == STDERR_FILENO)
364 const int new_fd = open(name, O_WRONLY | O_CREAT | O_TRUNC, 0644);
366 err(EXIT_FAILURE, "Cannot create %s", name);
367 if (new_fd != target_fd) {
368 if (dup2(new_fd, target_fd) == -1)
369 err(EXIT_FAILURE, "Cannot redirect to fd %d", target_fd);
374 /** Waits for a subprocess and validates its exit condition.
376 * \param pid The process to be waited for. Must have been started by
378 * \param exitstatus Expected exit status.
379 * \param expout Expected contents of stdout.
380 * \param experr Expected contents of stderr. */
382 atf_utils_wait(const pid_t pid, const int exitstatus, const char *expout,
386 ATF_REQUIRE(waitpid(pid, &status, 0) != -1);
388 atf_utils_cat_file("atf_utils_fork_out.txt", "subprocess stdout: ");
389 atf_utils_cat_file("atf_utils_fork_err.txt", "subprocess stderr: ");
391 ATF_REQUIRE(WIFEXITED(status));
392 ATF_REQUIRE_EQ(exitstatus, WEXITSTATUS(status));
394 const char *save_prefix = "save:";
395 const size_t save_prefix_length = strlen(save_prefix);
397 if (strlen(expout) > save_prefix_length &&
398 strncmp(expout, save_prefix, save_prefix_length) == 0) {
399 atf_utils_copy_file("atf_utils_fork_out.txt",
400 expout + save_prefix_length);
402 ATF_REQUIRE(atf_utils_compare_file("atf_utils_fork_out.txt", expout));
405 if (strlen(experr) > save_prefix_length &&
406 strncmp(experr, save_prefix, save_prefix_length) == 0) {
407 atf_utils_copy_file("atf_utils_fork_err.txt",
408 experr + save_prefix_length);
410 ATF_REQUIRE(atf_utils_compare_file("atf_utils_fork_err.txt", experr));
413 ATF_REQUIRE(unlink("atf_utils_fork_out.txt") != -1);
414 ATF_REQUIRE(unlink("atf_utils_fork_err.txt") != -1);