4a53e3c2b83c476a93148eaee0272649beb221caMark Andrews/* Copyright (c) 2010 The NetBSD Foundation, Inc.
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt * All rights reserved.
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt * Redistribution and use in source and binary forms, with or without
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt * modification, are permitted provided that the following conditions
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt * 1. Redistributions of source code must retain the above copyright
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt * notice, this list of conditions and the following disclaimer.
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt * 2. Redistributions in binary form must reproduce the above copyright
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt * notice, this list of conditions and the following disclaimer in the
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt * documentation and/or other materials provided with the distribution.
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt * CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt * IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
4a53e3c2b83c476a93148eaee0272649beb221caMark Andrews * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt/** Reads the contents of a file into a buffer.
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt * Up to buflen-1 characters are read into buffer. If this function returns,
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt * the contents read into the buffer are guaranteed to be nul-terminated.
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt * Note, however, that if the file contains any nul characters itself,
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt * comparing it "as a string" will not work.
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt * \param path The file to be read, which must exist.
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt * \param buffer Buffer into which to store the file contents.
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt * \param buflen Size of the target buffer.
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt * \return The count of bytes read. */
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Huntread_file(const char *path, void *const buffer, const size_t buflen)
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt ATF_REQUIRE_MSG(fd != -1, "Cannot open %s", path);
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt const ssize_t length = read(fd, buffer, buflen - 1);
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt atf_utils_redirect(STDOUT_FILENO, "captured.txt");
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt read_file("captured.txt", buffer, sizeof(buffer));
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt atf_utils_create_file("file.txt", "This is a single line\n");
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt atf_utils_redirect(STDOUT_FILENO, "captured.txt");
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt read_file("captured.txt", buffer, sizeof(buffer));
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt ATF_REQUIRE_STREQ("PREFIXThis is a single line\n", buffer);
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt atf_utils_create_file("file.txt", "First\nSecond line\nAnd third\n");
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt atf_utils_redirect(STDOUT_FILENO, "captured.txt");
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt read_file("captured.txt", buffer, sizeof(buffer));
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt ATF_REQUIRE_STREQ(">First\n>Second line\n>And third\n", buffer);
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt atf_utils_create_file("file.txt", "Foo\n bar baz");
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt atf_utils_redirect(STDOUT_FILENO, "captured.txt");
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt read_file("captured.txt", buffer, sizeof(buffer));
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt ATF_REQUIRE_STREQ("PREFIXFoo\nPREFIX bar baz", buffer);
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt ATF_REQUIRE(atf_utils_compare_file("test.txt", ""));
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan HuntATF_TC_WITHOUT_HEAD(compare_file__empty__not_match);
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt ATF_REQUIRE(!atf_utils_compare_file("test.txt", "\n"));
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt ATF_REQUIRE(!atf_utils_compare_file("test.txt", "foo"));
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt ATF_REQUIRE(!atf_utils_compare_file("test.txt", " "));
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt atf_utils_create_file("test.txt", "this is a short file");
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt ATF_REQUIRE(atf_utils_compare_file("test.txt", "this is a short file"));
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan HuntATF_TC_WITHOUT_HEAD(compare_file__short__not_match);
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt atf_utils_create_file("test.txt", "this is a short file");
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt ATF_REQUIRE(!atf_utils_compare_file("test.txt", ""));
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt ATF_REQUIRE(!atf_utils_compare_file("test.txt", "\n"));
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt ATF_REQUIRE(!atf_utils_compare_file("test.txt", "this is a Short file"));
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt ATF_REQUIRE(!atf_utils_compare_file("test.txt", "this is a short fil"));
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt ATF_REQUIRE(!atf_utils_compare_file("test.txt", "this is a short file "));
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt atf_utils_create_file("test.txt", "%s", long_contents);
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt ATF_REQUIRE(atf_utils_compare_file("test.txt", long_contents));
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan HuntATF_TC_WITHOUT_HEAD(compare_file__long__not_match);
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt atf_utils_create_file("test.txt", "%s", long_contents);
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt ATF_REQUIRE(!atf_utils_compare_file("test.txt", ""));
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt ATF_REQUIRE(!atf_utils_compare_file("test.txt", "\n"));
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt ATF_REQUIRE(!atf_utils_compare_file("test.txt", "0123456789"));
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt ATF_REQUIRE(!atf_utils_compare_file("test.txt", long_contents));
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt ATF_REQUIRE(atf_utils_compare_file("dest.txt", ""));
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt atf_utils_create_file("src.txt", "This is a\ntest file\n");
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt ATF_REQUIRE(atf_utils_compare_file("dest.txt", "This is a\ntest file\n"));
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt atf_utils_create_file("test.txt", "This is a test with %d", 12345);
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt ATF_REQUIRE_STREQ("This is a test with 12345", buffer);
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt ATF_REQUIRE( atf_utils_file_exists("./test.txt"));
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt if (pid == 0) {
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt ATF_REQUIRE_EQ(EXIT_SUCCESS, WEXITSTATUS(status));
4a53e3c2b83c476a93148eaee0272649beb221caMark Andrews RE(atf_dynstr_init_fmt(&out_name, "atf_utils_fork_%d_out.txt", (int)pid));
4a53e3c2b83c476a93148eaee0272649beb221caMark Andrews RE(atf_dynstr_init_fmt(&err_name, "atf_utils_fork_%d_err.txt", (int)pid));
4a53e3c2b83c476a93148eaee0272649beb221caMark Andrews read_file(atf_dynstr_cstring(&out_name), buffer, sizeof(buffer));
4a53e3c2b83c476a93148eaee0272649beb221caMark Andrews read_file(atf_dynstr_cstring(&err_name), buffer, sizeof(buffer));
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt atf_utils_create_file("test.txt", "line1\nthe second line\naaaabbbb\n");
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt ATF_CHECK(atf_utils_grep_file("line1", "test.txt"));
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt ATF_CHECK(atf_utils_grep_file("line%d", "test.txt", 1));
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt ATF_CHECK(atf_utils_grep_file("second line", "test.txt"));
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt ATF_CHECK(atf_utils_grep_file("aa.*bb", "test.txt"));
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt ATF_CHECK(!atf_utils_grep_file("foo", "test.txt"));
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt ATF_CHECK(!atf_utils_grep_file("bar", "test.txt"));
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt ATF_CHECK(!atf_utils_grep_file("aaaaa", "test.txt"));
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt ATF_CHECK(atf_utils_grep_string("a string", str));
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt ATF_CHECK(atf_utils_grep_string("^a string", str));
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt ATF_CHECK(atf_utils_grep_string("aaaabbbb$", str));
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt ATF_CHECK(atf_utils_grep_string("a%s*bb", str, "a."));
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt const char *l1 = "First line with % formatting % characters %";
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt const char *l2 = "Second line; much longer than the first one";
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt atf_utils_create_file("test.txt", "%s\n%s\n%s", l1, l2, l3);
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt atf_utils_redirect(STDOUT_FILENO, "captured.txt");
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt read_file("captured.txt", buffer, sizeof(buffer));
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt atf_utils_redirect(STDERR_FILENO, "captured.txt");
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt read_file("captured.txt", buffer, sizeof(buffer));
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt ATF_REQUIRE(write(15, message, strlen(message)) != -1);
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt read_file("captured.txt", buffer, sizeof(buffer));
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Huntfork_and_wait(const int exitstatus, const char* expout, const char* experr)
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt if (pid == 0) {
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt fork_and_wait(123, "Some output\n", "Some error\n");
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt ATF_REQUIRE_EQ(EXIT_SUCCESS, WEXITSTATUS(status));
4a53e3c2b83c476a93148eaee0272649beb221caMark Andrews atf_utils_wait(child, 50, "Child output\n", "Child error\n");
4a53e3c2b83c476a93148eaee0272649beb221caMark Andrews "Parent output\n"
4a53e3c2b83c476a93148eaee0272649beb221caMark Andrews "subprocess stdout: Child output\n"
4a53e3c2b83c476a93148eaee0272649beb221caMark Andrews "subprocess stderr: Child error\n",
4a53e3c2b83c476a93148eaee0272649beb221caMark Andrews "Parent error\n");
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt fork_and_wait(120, "Some output\n", "Some error\n");
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt ATF_REQUIRE_EQ(EXIT_FAILURE, WEXITSTATUS(status));
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt fork_and_wait(123, "Some output foo\n", "Some error\n");
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt ATF_REQUIRE_EQ(EXIT_FAILURE, WEXITSTATUS(status));
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt fork_and_wait(123, "Some output\n", "Some error foo\n");
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt ATF_REQUIRE_EQ(EXIT_FAILURE, WEXITSTATUS(status));
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt fork_and_wait(123, "save:my-output.txt", "Some error\n");
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt ATF_REQUIRE_EQ(EXIT_SUCCESS, WEXITSTATUS(status));
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt ATF_REQUIRE(atf_utils_compare_file("my-output.txt", "Some output\n"));
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt fork_and_wait(123, "Some output\n", "save:my-output.txt");
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt ATF_REQUIRE_EQ(EXIT_SUCCESS, WEXITSTATUS(status));
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt ATF_REQUIRE(atf_utils_compare_file("my-output.txt", "Some error\n"));
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt ATF_TP_ADD_TC(tp, compare_file__empty__not_match);