utils_test.cpp revision a747113422afaa29ce72d2c5ba7f0b7ea9ec2054
499b34cea04a46823d003d4c0520c8b03e8513cbBrian Wellington// Automated Testing Framework (atf)
5d82424f5d3c77c092c111b935041fd3dc4b3e98Andreas Gustafsson// Copyright (c) 2007 The NetBSD Foundation, Inc.
5d82424f5d3c77c092c111b935041fd3dc4b3e98Andreas Gustafsson// All rights reserved.
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence// Redistribution and use in source and binary forms, with or without
15a44745412679c30a6d022733925af70a38b715David Lawrence// modification, are permitted provided that the following conditions
15a44745412679c30a6d022733925af70a38b715David Lawrence// 1. Redistributions of source code must retain the above copyright
15a44745412679c30a6d022733925af70a38b715David Lawrence// notice, this list of conditions and the following disclaimer.
15a44745412679c30a6d022733925af70a38b715David Lawrence// 2. Redistributions in binary form must reproduce the above copyright
15a44745412679c30a6d022733925af70a38b715David Lawrence// notice, this list of conditions and the following disclaimer in the
15a44745412679c30a6d022733925af70a38b715David Lawrence// documentation and/or other materials provided with the distribution.
5d82424f5d3c77c092c111b935041fd3dc4b3e98Andreas Gustafsson// THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND
5d82424f5d3c77c092c111b935041fd3dc4b3e98Andreas Gustafsson// CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
900d7d9c9bc443110f9a2b93c94def44093d64b0David Lawrence// INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
9c3531d72aeaad6c5f01efe6a1c82023e1379e4dDavid Lawrence// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
5d82424f5d3c77c092c111b935041fd3dc4b3e98Andreas Gustafsson// IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY
5d82424f5d3c77c092c111b935041fd3dc4b3e98Andreas Gustafsson// DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
8eaa51a6991ea6bc6c9db0b907beacde8dd1fed2Andreas Gustafsson// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
5d82424f5d3c77c092c111b935041fd3dc4b3e98Andreas Gustafsson// GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
8eaa51a6991ea6bc6c9db0b907beacde8dd1fed2Andreas Gustafsson// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
9cd6d409b78a6f833b681c13a68fbdc7c024fe66David Lawrence// IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
5d82424f5d3c77c092c111b935041fd3dc4b3e98Andreas Gustafsson// OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
8eaa51a6991ea6bc6c9db0b907beacde8dd1fed2Andreas Gustafsson// IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
5d82424f5d3c77c092c111b935041fd3dc4b3e98Andreas Gustafsson ATF_FAIL("Cannot open " + std::string(path));
5d82424f5d3c77c092c111b935041fd3dc4b3e98Andreas Gustafsson const ssize_t length = read(fd, buffer, sizeof(buffer) - 1);
5d82424f5d3c77c092c111b935041fd3dc4b3e98Andreas Gustafsson ATF_FAIL("Internal buffer not long enough to read temporary file");
5d82424f5d3c77c092c111b935041fd3dc4b3e98Andreas Gustafsson// ------------------------------------------------------------------------
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence// Tests cases for the free functions.
5d82424f5d3c77c092c111b935041fd3dc4b3e98Andreas Gustafsson// ------------------------------------------------------------------------
5d82424f5d3c77c092c111b935041fd3dc4b3e98Andreas Gustafsson atf::utils::redirect(STDOUT_FILENO, "captured.txt");
5d82424f5d3c77c092c111b935041fd3dc4b3e98Andreas Gustafsson ATF_REQUIRE_EQ("", read_file("captured.txt"));
5d82424f5d3c77c092c111b935041fd3dc4b3e98Andreas GustafssonATF_TEST_CASE_WITHOUT_HEAD(cat_file__one_line);
87cafc5e70f79f2586d067fbdd64f61bbab069d2David Lawrence atf::utils::create_file("file.txt", "This is a single line\n");
87cafc5e70f79f2586d067fbdd64f61bbab069d2David Lawrence atf::utils::redirect(STDOUT_FILENO, "captured.txt");
5d82424f5d3c77c092c111b935041fd3dc4b3e98Andreas Gustafsson ATF_REQUIRE_EQ("PREFIXThis is a single line\n", read_file("captured.txt"));
5d82424f5d3c77c092c111b935041fd3dc4b3e98Andreas GustafssonATF_TEST_CASE_WITHOUT_HEAD(cat_file__several_lines);
5d82424f5d3c77c092c111b935041fd3dc4b3e98Andreas GustafssonATF_TEST_CASE_BODY(cat_file__several_lines)
5d82424f5d3c77c092c111b935041fd3dc4b3e98Andreas Gustafsson atf::utils::create_file("file.txt", "First\nSecond line\nAnd third\n");
5d82424f5d3c77c092c111b935041fd3dc4b3e98Andreas Gustafsson atf::utils::redirect(STDOUT_FILENO, "captured.txt");
5d82424f5d3c77c092c111b935041fd3dc4b3e98Andreas Gustafsson ATF_REQUIRE_EQ(">First\n>Second line\n>And third\n",
5d82424f5d3c77c092c111b935041fd3dc4b3e98Andreas GustafssonATF_TEST_CASE_WITHOUT_HEAD(cat_file__no_newline_eof);
5d82424f5d3c77c092c111b935041fd3dc4b3e98Andreas GustafssonATF_TEST_CASE_BODY(cat_file__no_newline_eof)
5d82424f5d3c77c092c111b935041fd3dc4b3e98Andreas Gustafsson atf::utils::create_file("file.txt", "Foo\n bar baz");
8eaa51a6991ea6bc6c9db0b907beacde8dd1fed2Andreas Gustafsson atf::utils::redirect(STDOUT_FILENO, "captured.txt");
8eaa51a6991ea6bc6c9db0b907beacde8dd1fed2Andreas Gustafsson atf::utils::cat_file("file.txt", "PREFIX");
8eaa51a6991ea6bc6c9db0b907beacde8dd1fed2Andreas Gustafsson ATF_REQUIRE_EQ("PREFIXFoo\nPREFIX bar baz", read_file("captured.txt"));
8eaa51a6991ea6bc6c9db0b907beacde8dd1fed2Andreas GustafssonATF_TEST_CASE_WITHOUT_HEAD(compare_file__empty__match);
8eaa51a6991ea6bc6c9db0b907beacde8dd1fed2Andreas GustafssonATF_TEST_CASE_BODY(compare_file__empty__match)
8eaa51a6991ea6bc6c9db0b907beacde8dd1fed2Andreas Gustafsson ATF_REQUIRE(atf::utils::compare_file("test.txt", ""));
8eaa51a6991ea6bc6c9db0b907beacde8dd1fed2Andreas GustafssonATF_TEST_CASE_WITHOUT_HEAD(compare_file__empty__not_match);
8eaa51a6991ea6bc6c9db0b907beacde8dd1fed2Andreas GustafssonATF_TEST_CASE_BODY(compare_file__empty__not_match)
8eaa51a6991ea6bc6c9db0b907beacde8dd1fed2Andreas Gustafsson ATF_REQUIRE(!atf::utils::compare_file("test.txt", "\n"));
8eaa51a6991ea6bc6c9db0b907beacde8dd1fed2Andreas Gustafsson ATF_REQUIRE(!atf::utils::compare_file("test.txt", "foo"));
8eaa51a6991ea6bc6c9db0b907beacde8dd1fed2Andreas Gustafsson ATF_REQUIRE(!atf::utils::compare_file("test.txt", " "));
8eaa51a6991ea6bc6c9db0b907beacde8dd1fed2Andreas GustafssonATF_TEST_CASE_WITHOUT_HEAD(compare_file__short__match);
8eaa51a6991ea6bc6c9db0b907beacde8dd1fed2Andreas GustafssonATF_TEST_CASE_BODY(compare_file__short__match)
8eaa51a6991ea6bc6c9db0b907beacde8dd1fed2Andreas Gustafsson atf::utils::create_file("test.txt", "this is a short file");
8eaa51a6991ea6bc6c9db0b907beacde8dd1fed2Andreas Gustafsson ATF_REQUIRE(atf::utils::compare_file("test.txt", "this is a short file"));
8eaa51a6991ea6bc6c9db0b907beacde8dd1fed2Andreas GustafssonATF_TEST_CASE_WITHOUT_HEAD(compare_file__short__not_match);
8eaa51a6991ea6bc6c9db0b907beacde8dd1fed2Andreas GustafssonATF_TEST_CASE_BODY(compare_file__short__not_match)
8eaa51a6991ea6bc6c9db0b907beacde8dd1fed2Andreas Gustafsson atf::utils::create_file("test.txt", "this is a short file");
8eaa51a6991ea6bc6c9db0b907beacde8dd1fed2Andreas Gustafsson ATF_REQUIRE(!atf::utils::compare_file("test.txt", ""));
8eaa51a6991ea6bc6c9db0b907beacde8dd1fed2Andreas Gustafsson ATF_REQUIRE(!atf::utils::compare_file("test.txt", "\n"));
8eaa51a6991ea6bc6c9db0b907beacde8dd1fed2Andreas Gustafsson ATF_REQUIRE(!atf::utils::compare_file("test.txt", "this is a Short file"));
8eaa51a6991ea6bc6c9db0b907beacde8dd1fed2Andreas Gustafsson ATF_REQUIRE(!atf::utils::compare_file("test.txt", "this is a short fil"));
8eaa51a6991ea6bc6c9db0b907beacde8dd1fed2Andreas Gustafsson ATF_REQUIRE(!atf::utils::compare_file("test.txt", "this is a short file "));
8eaa51a6991ea6bc6c9db0b907beacde8dd1fed2Andreas GustafssonATF_TEST_CASE_WITHOUT_HEAD(compare_file__long__match);
8eaa51a6991ea6bc6c9db0b907beacde8dd1fed2Andreas Gustafsson for (; i < sizeof(long_contents) - 1; i++)
8eaa51a6991ea6bc6c9db0b907beacde8dd1fed2Andreas Gustafsson atf::utils::create_file("test.txt", long_contents);
87cafc5e70f79f2586d067fbdd64f61bbab069d2David Lawrence ATF_REQUIRE(atf::utils::compare_file("test.txt", long_contents));
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David LawrenceATF_TEST_CASE_WITHOUT_HEAD(compare_file__long__not_match);
5d82424f5d3c77c092c111b935041fd3dc4b3e98Andreas GustafssonATF_TEST_CASE_BODY(compare_file__long__not_match)
5d82424f5d3c77c092c111b935041fd3dc4b3e98Andreas Gustafsson for (; i < sizeof(long_contents) - 1; i++)
5d82424f5d3c77c092c111b935041fd3dc4b3e98Andreas Gustafsson atf::utils::create_file("test.txt", long_contents);
5d82424f5d3c77c092c111b935041fd3dc4b3e98Andreas Gustafsson ATF_REQUIRE(!atf::utils::compare_file("test.txt", ""));
5d82424f5d3c77c092c111b935041fd3dc4b3e98Andreas Gustafsson ATF_REQUIRE(!atf::utils::compare_file("test.txt", "\n"));
5d82424f5d3c77c092c111b935041fd3dc4b3e98Andreas Gustafsson ATF_REQUIRE(!atf::utils::compare_file("test.txt", "0123456789"));
5d82424f5d3c77c092c111b935041fd3dc4b3e98Andreas Gustafsson ATF_REQUIRE(!atf::utils::compare_file("test.txt", long_contents));
5d82424f5d3c77c092c111b935041fd3dc4b3e98Andreas GustafssonATF_TEST_CASE_WITHOUT_HEAD(copy_file__empty);
5d82424f5d3c77c092c111b935041fd3dc4b3e98Andreas Gustafsson ATF_REQUIRE(chmod("src.txt", 0520) != -1);
5d82424f5d3c77c092c111b935041fd3dc4b3e98Andreas Gustafsson atf::utils::copy_file("src.txt", "dest.txt");
5d82424f5d3c77c092c111b935041fd3dc4b3e98Andreas Gustafsson ATF_REQUIRE(atf::utils::compare_file("dest.txt", ""));
5d82424f5d3c77c092c111b935041fd3dc4b3e98Andreas Gustafsson ATF_REQUIRE(stat("dest.txt", &sb) != -1);
5d82424f5d3c77c092c111b935041fd3dc4b3e98Andreas Gustafsson ATF_REQUIRE_EQ(0520, sb.st_mode & 0xfff);
5d82424f5d3c77c092c111b935041fd3dc4b3e98Andreas GustafssonATF_TEST_CASE_WITHOUT_HEAD(copy_file__some_contents);
5d82424f5d3c77c092c111b935041fd3dc4b3e98Andreas GustafssonATF_TEST_CASE_BODY(copy_file__some_contents)
5d82424f5d3c77c092c111b935041fd3dc4b3e98Andreas Gustafsson atf::utils::create_file("src.txt", "This is a\ntest file\n");
5d82424f5d3c77c092c111b935041fd3dc4b3e98Andreas Gustafsson atf::utils::copy_file("src.txt", "dest.txt");
5d82424f5d3c77c092c111b935041fd3dc4b3e98Andreas Gustafsson ATF_REQUIRE(atf::utils::compare_file("dest.txt", "This is a\ntest file\n"));
5d82424f5d3c77c092c111b935041fd3dc4b3e98Andreas Gustafsson atf::utils::create_file("test.txt", "This is a %d test");
5d82424f5d3c77c092c111b935041fd3dc4b3e98Andreas Gustafsson ATF_REQUIRE_EQ("This is a %d test", read_file("test.txt"));
5d82424f5d3c77c092c111b935041fd3dc4b3e98Andreas Gustafsson atf::utils::create_file("test.txt", "foo");
5d82424f5d3c77c092c111b935041fd3dc4b3e98Andreas Gustafsson ATF_REQUIRE( atf::utils::file_exists("test.txt"));
5d82424f5d3c77c092c111b935041fd3dc4b3e98Andreas Gustafsson ATF_REQUIRE( atf::utils::file_exists("./test.txt"));
d80da258e377f02f0dcd703b89a1044cc58e949fMichael Graff ATF_REQUIRE(!atf::utils::file_exists("./test.tx"));
5d82424f5d3c77c092c111b935041fd3dc4b3e98Andreas Gustafsson ATF_REQUIRE(!atf::utils::file_exists("test.txt2"));
5d82424f5d3c77c092c111b935041fd3dc4b3e98Andreas Gustafsson std::cout << "Should not get into child\n";
5d82424f5d3c77c092c111b935041fd3dc4b3e98Andreas Gustafsson std::cerr << "Should not get into child\n";
7e361074bc8a2df7a0891a7040eea02ca3a5e286Andreas Gustafsson ATF_REQUIRE(waitpid(pid, &status, 0) != -1);
7e361074bc8a2df7a0891a7040eea02ca3a5e286Andreas Gustafsson ATF_REQUIRE_EQ(EXIT_SUCCESS, WEXITSTATUS(status));
7e361074bc8a2df7a0891a7040eea02ca3a5e286Andreas Gustafsson ATF_REQUIRE_EQ("Child stdout\n", read_file("atf_utils_fork_out.txt"));
7e361074bc8a2df7a0891a7040eea02ca3a5e286Andreas Gustafsson ATF_REQUIRE_EQ("Child stderr\n", read_file("atf_utils_fork_err.txt"));
7e361074bc8a2df7a0891a7040eea02ca3a5e286Andreas GustafssonATF_TEST_CASE_WITHOUT_HEAD(grep_collection__set);
d80da258e377f02f0dcd703b89a1044cc58e949fMichael Graff ATF_REQUIRE( atf::utils::grep_collection("irs", strings));
d80da258e377f02f0dcd703b89a1044cc58e949fMichael Graff ATF_REQUIRE( atf::utils::grep_collection("cond", strings));
d80da258e377f02f0dcd703b89a1044cc58e949fMichael Graff ATF_REQUIRE(!atf::utils::grep_collection("Third", strings));
d80da258e377f02f0dcd703b89a1044cc58e949fMichael GraffATF_TEST_CASE_WITHOUT_HEAD(grep_collection__vector);
de6fe5ebe289d8e922b2fd8e8d642b6dd4f2a8efDavid Lawrence ATF_REQUIRE( atf::utils::grep_collection("irs", strings));
de6fe5ebe289d8e922b2fd8e8d642b6dd4f2a8efDavid Lawrence ATF_REQUIRE( atf::utils::grep_collection("cond", strings));
de6fe5ebe289d8e922b2fd8e8d642b6dd4f2a8efDavid Lawrence ATF_REQUIRE(!atf::utils::grep_collection("Third", strings));
de6fe5ebe289d8e922b2fd8e8d642b6dd4f2a8efDavid Lawrence atf::utils::create_file("test.txt", "line1\nthe second line\naaaabbbb\n");
6eccf5bd07eb9abf65cc08fec4a8fc97b62c0e1bBrian Wellington ATF_REQUIRE(atf::utils::grep_file("line1", "test.txt"));
if (pid == 0) {
if (control == 0)
int status;
if (control == 0)
int status;
if (control == 0)
int status;
if (control == 0)
int status;
if (control == 0)
int status;
if (control == 0)
int status;