utils_test.cpp revision a747113422afaa29ce72d2c5ba7f0b7ea9ec2054
5d82424f5d3c77c092c111b935041fd3dc4b3e98Andreas Gustafsson//
499b34cea04a46823d003d4c0520c8b03e8513cbBrian Wellington// Automated Testing Framework (atf)
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence//
5d82424f5d3c77c092c111b935041fd3dc4b3e98Andreas Gustafsson// Copyright (c) 2007 The NetBSD Foundation, Inc.
5d82424f5d3c77c092c111b935041fd3dc4b3e98Andreas Gustafsson// All rights reserved.
5d82424f5d3c77c092c111b935041fd3dc4b3e98Andreas Gustafsson//
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// are met:
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.
15a44745412679c30a6d022733925af70a38b715David Lawrence//
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//
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence
364a82f7c25b62967678027043425201a5e5171aBob Halleyextern "C" {
5d82424f5d3c77c092c111b935041fd3dc4b3e98Andreas Gustafsson#include <sys/stat.h>
b356893425f18a2399771faa875e91318f204fb5Andreas Gustafsson#include <sys/wait.h>
b356893425f18a2399771faa875e91318f204fb5Andreas Gustafsson
b356893425f18a2399771faa875e91318f204fb5Andreas Gustafsson#include <fcntl.h>
b356893425f18a2399771faa875e91318f204fb5Andreas Gustafsson#include <unistd.h>
b356893425f18a2399771faa875e91318f204fb5Andreas Gustafsson}
b356893425f18a2399771faa875e91318f204fb5Andreas Gustafsson
b356893425f18a2399771faa875e91318f204fb5Andreas Gustafsson#include <cstdlib>
b356893425f18a2399771faa875e91318f204fb5Andreas Gustafsson#include <iostream>
b356893425f18a2399771faa875e91318f204fb5Andreas Gustafsson#include <set>
b356893425f18a2399771faa875e91318f204fb5Andreas Gustafsson#include <string>
b356893425f18a2399771faa875e91318f204fb5Andreas Gustafsson#include <vector>
b356893425f18a2399771faa875e91318f204fb5Andreas Gustafsson
b356893425f18a2399771faa875e91318f204fb5Andreas Gustafsson#include "macros.hpp"
ed019cabc1cc75d4412010c331876e4ae5080a4dDavid Lawrence#include "utils.hpp"
ed019cabc1cc75d4412010c331876e4ae5080a4dDavid Lawrence
b356893425f18a2399771faa875e91318f204fb5Andreas Gustafsson#include "detail/test_helpers.hpp"
b356893425f18a2399771faa875e91318f204fb5Andreas Gustafsson
b356893425f18a2399771faa875e91318f204fb5Andreas Gustafssonstatic std::string
b356893425f18a2399771faa875e91318f204fb5Andreas Gustafssonread_file(const char *path)
b356893425f18a2399771faa875e91318f204fb5Andreas Gustafsson{
b356893425f18a2399771faa875e91318f204fb5Andreas Gustafsson char buffer[1024];
b356893425f18a2399771faa875e91318f204fb5Andreas Gustafsson
b356893425f18a2399771faa875e91318f204fb5Andreas Gustafsson const int fd = open(path, O_RDONLY);
5d82424f5d3c77c092c111b935041fd3dc4b3e98Andreas Gustafsson if (fd == -1)
5d82424f5d3c77c092c111b935041fd3dc4b3e98Andreas Gustafsson ATF_FAIL("Cannot open " + std::string(path));
5d82424f5d3c77c092c111b935041fd3dc4b3e98Andreas Gustafsson const ssize_t length = read(fd, buffer, sizeof(buffer) - 1);
5d82424f5d3c77c092c111b935041fd3dc4b3e98Andreas Gustafsson close(fd);
87cafc5e70f79f2586d067fbdd64f61bbab069d2David Lawrence ATF_REQUIRE(length != -1);
5d82424f5d3c77c092c111b935041fd3dc4b3e98Andreas Gustafsson if (length == sizeof(buffer) - 1)
5d82424f5d3c77c092c111b935041fd3dc4b3e98Andreas Gustafsson ATF_FAIL("Internal buffer not long enough to read temporary file");
5d82424f5d3c77c092c111b935041fd3dc4b3e98Andreas Gustafsson ((char *)buffer)[length] = '\0';
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence
5d82424f5d3c77c092c111b935041fd3dc4b3e98Andreas Gustafsson return buffer;
5d82424f5d3c77c092c111b935041fd3dc4b3e98Andreas Gustafsson}
5d82424f5d3c77c092c111b935041fd3dc4b3e98Andreas Gustafsson
5d82424f5d3c77c092c111b935041fd3dc4b3e98Andreas Gustafsson// ------------------------------------------------------------------------
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence// Tests cases for the free functions.
5d82424f5d3c77c092c111b935041fd3dc4b3e98Andreas Gustafsson// ------------------------------------------------------------------------
5d82424f5d3c77c092c111b935041fd3dc4b3e98Andreas Gustafsson
87cafc5e70f79f2586d067fbdd64f61bbab069d2David LawrenceATF_TEST_CASE_WITHOUT_HEAD(cat_file__empty);
87cafc5e70f79f2586d067fbdd64f61bbab069d2David LawrenceATF_TEST_CASE_BODY(cat_file__empty)
5d82424f5d3c77c092c111b935041fd3dc4b3e98Andreas Gustafsson{
5d82424f5d3c77c092c111b935041fd3dc4b3e98Andreas Gustafsson atf::utils::create_file("file.txt", "");
5d82424f5d3c77c092c111b935041fd3dc4b3e98Andreas Gustafsson atf::utils::redirect(STDOUT_FILENO, "captured.txt");
87cafc5e70f79f2586d067fbdd64f61bbab069d2David Lawrence atf::utils::cat_file("file.txt", "PREFIX");
87cafc5e70f79f2586d067fbdd64f61bbab069d2David Lawrence std::cout.flush();
5d82424f5d3c77c092c111b935041fd3dc4b3e98Andreas Gustafsson close(STDOUT_FILENO);
5d82424f5d3c77c092c111b935041fd3dc4b3e98Andreas Gustafsson
5d82424f5d3c77c092c111b935041fd3dc4b3e98Andreas Gustafsson ATF_REQUIRE_EQ("", read_file("captured.txt"));
5d82424f5d3c77c092c111b935041fd3dc4b3e98Andreas Gustafsson}
5d82424f5d3c77c092c111b935041fd3dc4b3e98Andreas Gustafsson
5d82424f5d3c77c092c111b935041fd3dc4b3e98Andreas GustafssonATF_TEST_CASE_WITHOUT_HEAD(cat_file__one_line);
5d82424f5d3c77c092c111b935041fd3dc4b3e98Andreas GustafssonATF_TEST_CASE_BODY(cat_file__one_line)
5d82424f5d3c77c092c111b935041fd3dc4b3e98Andreas Gustafsson{
87cafc5e70f79f2586d067fbdd64f61bbab069d2David Lawrence atf::utils::create_file("file.txt", "This is a single line\n");
87cafc5e70f79f2586d067fbdd64f61bbab069d2David Lawrence atf::utils::redirect(STDOUT_FILENO, "captured.txt");
87cafc5e70f79f2586d067fbdd64f61bbab069d2David Lawrence atf::utils::cat_file("file.txt", "PREFIX");
5d82424f5d3c77c092c111b935041fd3dc4b3e98Andreas Gustafsson std::cout.flush();
5d82424f5d3c77c092c111b935041fd3dc4b3e98Andreas Gustafsson close(STDOUT_FILENO);
5d82424f5d3c77c092c111b935041fd3dc4b3e98Andreas Gustafsson
5d82424f5d3c77c092c111b935041fd3dc4b3e98Andreas Gustafsson ATF_REQUIRE_EQ("PREFIXThis is a single line\n", read_file("captured.txt"));
5d82424f5d3c77c092c111b935041fd3dc4b3e98Andreas Gustafsson}
5d82424f5d3c77c092c111b935041fd3dc4b3e98Andreas Gustafsson
5d82424f5d3c77c092c111b935041fd3dc4b3e98Andreas GustafssonATF_TEST_CASE_WITHOUT_HEAD(cat_file__several_lines);
5d82424f5d3c77c092c111b935041fd3dc4b3e98Andreas GustafssonATF_TEST_CASE_BODY(cat_file__several_lines)
5d82424f5d3c77c092c111b935041fd3dc4b3e98Andreas Gustafsson{
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::utils::cat_file("file.txt", ">");
5d82424f5d3c77c092c111b935041fd3dc4b3e98Andreas Gustafsson std::cout.flush();
5d82424f5d3c77c092c111b935041fd3dc4b3e98Andreas Gustafsson close(STDOUT_FILENO);
5d82424f5d3c77c092c111b935041fd3dc4b3e98Andreas Gustafsson
5d82424f5d3c77c092c111b935041fd3dc4b3e98Andreas Gustafsson ATF_REQUIRE_EQ(">First\n>Second line\n>And third\n",
5d82424f5d3c77c092c111b935041fd3dc4b3e98Andreas Gustafsson read_file("captured.txt"));
5d82424f5d3c77c092c111b935041fd3dc4b3e98Andreas Gustafsson}
5d82424f5d3c77c092c111b935041fd3dc4b3e98Andreas Gustafsson
5d82424f5d3c77c092c111b935041fd3dc4b3e98Andreas GustafssonATF_TEST_CASE_WITHOUT_HEAD(cat_file__no_newline_eof);
5d82424f5d3c77c092c111b935041fd3dc4b3e98Andreas GustafssonATF_TEST_CASE_BODY(cat_file__no_newline_eof)
5d82424f5d3c77c092c111b935041fd3dc4b3e98Andreas Gustafsson{
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 std::cout.flush();
8eaa51a6991ea6bc6c9db0b907beacde8dd1fed2Andreas Gustafsson close(STDOUT_FILENO);
8eaa51a6991ea6bc6c9db0b907beacde8dd1fed2Andreas Gustafsson
8eaa51a6991ea6bc6c9db0b907beacde8dd1fed2Andreas Gustafsson ATF_REQUIRE_EQ("PREFIXFoo\nPREFIX bar baz", read_file("captured.txt"));
8eaa51a6991ea6bc6c9db0b907beacde8dd1fed2Andreas Gustafsson}
8eaa51a6991ea6bc6c9db0b907beacde8dd1fed2Andreas Gustafsson
8eaa51a6991ea6bc6c9db0b907beacde8dd1fed2Andreas GustafssonATF_TEST_CASE_WITHOUT_HEAD(compare_file__empty__match);
8eaa51a6991ea6bc6c9db0b907beacde8dd1fed2Andreas GustafssonATF_TEST_CASE_BODY(compare_file__empty__match)
8eaa51a6991ea6bc6c9db0b907beacde8dd1fed2Andreas Gustafsson{
8eaa51a6991ea6bc6c9db0b907beacde8dd1fed2Andreas Gustafsson atf::utils::create_file("test.txt", "");
8eaa51a6991ea6bc6c9db0b907beacde8dd1fed2Andreas Gustafsson ATF_REQUIRE(atf::utils::compare_file("test.txt", ""));
8eaa51a6991ea6bc6c9db0b907beacde8dd1fed2Andreas Gustafsson}
8eaa51a6991ea6bc6c9db0b907beacde8dd1fed2Andreas Gustafsson
8eaa51a6991ea6bc6c9db0b907beacde8dd1fed2Andreas GustafssonATF_TEST_CASE_WITHOUT_HEAD(compare_file__empty__not_match);
8eaa51a6991ea6bc6c9db0b907beacde8dd1fed2Andreas GustafssonATF_TEST_CASE_BODY(compare_file__empty__not_match)
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence{
8eaa51a6991ea6bc6c9db0b907beacde8dd1fed2Andreas Gustafsson atf::utils::create_file("test.txt", "");
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 Gustafsson}
8eaa51a6991ea6bc6c9db0b907beacde8dd1fed2Andreas Gustafsson
8eaa51a6991ea6bc6c9db0b907beacde8dd1fed2Andreas GustafssonATF_TEST_CASE_WITHOUT_HEAD(compare_file__short__match);
8eaa51a6991ea6bc6c9db0b907beacde8dd1fed2Andreas GustafssonATF_TEST_CASE_BODY(compare_file__short__match)
8eaa51a6991ea6bc6c9db0b907beacde8dd1fed2Andreas Gustafsson{
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 Gustafsson}
8eaa51a6991ea6bc6c9db0b907beacde8dd1fed2Andreas Gustafsson
8eaa51a6991ea6bc6c9db0b907beacde8dd1fed2Andreas GustafssonATF_TEST_CASE_WITHOUT_HEAD(compare_file__short__not_match);
8eaa51a6991ea6bc6c9db0b907beacde8dd1fed2Andreas GustafssonATF_TEST_CASE_BODY(compare_file__short__not_match)
8eaa51a6991ea6bc6c9db0b907beacde8dd1fed2Andreas Gustafsson{
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 "));
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence}
8eaa51a6991ea6bc6c9db0b907beacde8dd1fed2Andreas Gustafsson
8eaa51a6991ea6bc6c9db0b907beacde8dd1fed2Andreas GustafssonATF_TEST_CASE_WITHOUT_HEAD(compare_file__long__match);
9cd6d409b78a6f833b681c13a68fbdc7c024fe66David LawrenceATF_TEST_CASE_BODY(compare_file__long__match)
9cd6d409b78a6f833b681c13a68fbdc7c024fe66David Lawrence{
9cd6d409b78a6f833b681c13a68fbdc7c024fe66David Lawrence char long_contents[3456];
8eaa51a6991ea6bc6c9db0b907beacde8dd1fed2Andreas Gustafsson size_t i = 0;
8eaa51a6991ea6bc6c9db0b907beacde8dd1fed2Andreas Gustafsson for (; i < sizeof(long_contents) - 1; i++)
8eaa51a6991ea6bc6c9db0b907beacde8dd1fed2Andreas Gustafsson long_contents[i] = '0' + (i % 10);
8eaa51a6991ea6bc6c9db0b907beacde8dd1fed2Andreas Gustafsson long_contents[i] = '\0';
8eaa51a6991ea6bc6c9db0b907beacde8dd1fed2Andreas Gustafsson atf::utils::create_file("test.txt", long_contents);
5d82424f5d3c77c092c111b935041fd3dc4b3e98Andreas Gustafsson
87cafc5e70f79f2586d067fbdd64f61bbab069d2David Lawrence ATF_REQUIRE(atf::utils::compare_file("test.txt", long_contents));
5d82424f5d3c77c092c111b935041fd3dc4b3e98Andreas Gustafsson}
87cafc5e70f79f2586d067fbdd64f61bbab069d2David Lawrence
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David LawrenceATF_TEST_CASE_WITHOUT_HEAD(compare_file__long__not_match);
5d82424f5d3c77c092c111b935041fd3dc4b3e98Andreas GustafssonATF_TEST_CASE_BODY(compare_file__long__not_match)
5d82424f5d3c77c092c111b935041fd3dc4b3e98Andreas Gustafsson{
87cafc5e70f79f2586d067fbdd64f61bbab069d2David Lawrence char long_contents[3456];
5d82424f5d3c77c092c111b935041fd3dc4b3e98Andreas Gustafsson size_t i = 0;
5d82424f5d3c77c092c111b935041fd3dc4b3e98Andreas Gustafsson for (; i < sizeof(long_contents) - 1; i++)
5d82424f5d3c77c092c111b935041fd3dc4b3e98Andreas Gustafsson long_contents[i] = '0' + (i % 10);
87cafc5e70f79f2586d067fbdd64f61bbab069d2David Lawrence long_contents[i] = '\0';
5d82424f5d3c77c092c111b935041fd3dc4b3e98Andreas Gustafsson atf::utils::create_file("test.txt", long_contents);
5d82424f5d3c77c092c111b935041fd3dc4b3e98Andreas Gustafsson
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 long_contents[i - 1] = 'Z';
5d82424f5d3c77c092c111b935041fd3dc4b3e98Andreas Gustafsson ATF_REQUIRE(!atf::utils::compare_file("test.txt", long_contents));
5d82424f5d3c77c092c111b935041fd3dc4b3e98Andreas Gustafsson}
5d82424f5d3c77c092c111b935041fd3dc4b3e98Andreas Gustafsson
5d82424f5d3c77c092c111b935041fd3dc4b3e98Andreas GustafssonATF_TEST_CASE_WITHOUT_HEAD(copy_file__empty);
5d82424f5d3c77c092c111b935041fd3dc4b3e98Andreas GustafssonATF_TEST_CASE_BODY(copy_file__empty)
5d82424f5d3c77c092c111b935041fd3dc4b3e98Andreas Gustafsson{
5d82424f5d3c77c092c111b935041fd3dc4b3e98Andreas Gustafsson atf::utils::create_file("src.txt", "");
5d82424f5d3c77c092c111b935041fd3dc4b3e98Andreas Gustafsson ATF_REQUIRE(chmod("src.txt", 0520) != -1);
5d82424f5d3c77c092c111b935041fd3dc4b3e98Andreas Gustafsson
5d82424f5d3c77c092c111b935041fd3dc4b3e98Andreas Gustafsson atf::utils::copy_file("src.txt", "dest.txt");
5d82424f5d3c77c092c111b935041fd3dc4b3e98Andreas Gustafsson ATF_REQUIRE(atf::utils::compare_file("dest.txt", ""));
5d82424f5d3c77c092c111b935041fd3dc4b3e98Andreas Gustafsson struct stat sb;
5d82424f5d3c77c092c111b935041fd3dc4b3e98Andreas Gustafsson ATF_REQUIRE(stat("dest.txt", &sb) != -1);
5d82424f5d3c77c092c111b935041fd3dc4b3e98Andreas Gustafsson ATF_REQUIRE_EQ(0520, sb.st_mode & 0xfff);
5d82424f5d3c77c092c111b935041fd3dc4b3e98Andreas Gustafsson}
5d82424f5d3c77c092c111b935041fd3dc4b3e98Andreas Gustafsson
5d82424f5d3c77c092c111b935041fd3dc4b3e98Andreas GustafssonATF_TEST_CASE_WITHOUT_HEAD(copy_file__some_contents);
5d82424f5d3c77c092c111b935041fd3dc4b3e98Andreas GustafssonATF_TEST_CASE_BODY(copy_file__some_contents)
5d82424f5d3c77c092c111b935041fd3dc4b3e98Andreas Gustafsson{
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}
5d82424f5d3c77c092c111b935041fd3dc4b3e98Andreas Gustafsson
5d82424f5d3c77c092c111b935041fd3dc4b3e98Andreas GustafssonATF_TEST_CASE_WITHOUT_HEAD(create_file);
87cafc5e70f79f2586d067fbdd64f61bbab069d2David LawrenceATF_TEST_CASE_BODY(create_file)
5d82424f5d3c77c092c111b935041fd3dc4b3e98Andreas Gustafsson{
5d82424f5d3c77c092c111b935041fd3dc4b3e98Andreas Gustafsson atf::utils::create_file("test.txt", "This is a %d test");
5d82424f5d3c77c092c111b935041fd3dc4b3e98Andreas Gustafsson
5d82424f5d3c77c092c111b935041fd3dc4b3e98Andreas Gustafsson ATF_REQUIRE_EQ("This is a %d test", read_file("test.txt"));
5d82424f5d3c77c092c111b935041fd3dc4b3e98Andreas Gustafsson}
5d82424f5d3c77c092c111b935041fd3dc4b3e98Andreas Gustafsson
87cafc5e70f79f2586d067fbdd64f61bbab069d2David LawrenceATF_TEST_CASE_WITHOUT_HEAD(file_exists);
5d82424f5d3c77c092c111b935041fd3dc4b3e98Andreas GustafssonATF_TEST_CASE_BODY(file_exists)
5d82424f5d3c77c092c111b935041fd3dc4b3e98Andreas Gustafsson{
5d82424f5d3c77c092c111b935041fd3dc4b3e98Andreas Gustafsson atf::utils::create_file("test.txt", "foo");
5d82424f5d3c77c092c111b935041fd3dc4b3e98Andreas Gustafsson
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}
5d82424f5d3c77c092c111b935041fd3dc4b3e98Andreas Gustafsson
5d82424f5d3c77c092c111b935041fd3dc4b3e98Andreas GustafssonATF_TEST_CASE_WITHOUT_HEAD(fork);
5d82424f5d3c77c092c111b935041fd3dc4b3e98Andreas GustafssonATF_TEST_CASE_BODY(fork)
5d82424f5d3c77c092c111b935041fd3dc4b3e98Andreas Gustafsson{
5d82424f5d3c77c092c111b935041fd3dc4b3e98Andreas Gustafsson std::cout << "Should not get into child\n";
5d82424f5d3c77c092c111b935041fd3dc4b3e98Andreas Gustafsson std::cerr << "Should not get into child\n";
5d82424f5d3c77c092c111b935041fd3dc4b3e98Andreas Gustafsson pid_t pid = atf::utils::fork();
5d82424f5d3c77c092c111b935041fd3dc4b3e98Andreas Gustafsson if (pid == 0) {
5d82424f5d3c77c092c111b935041fd3dc4b3e98Andreas Gustafsson std::cout << "Child stdout\n";
5d82424f5d3c77c092c111b935041fd3dc4b3e98Andreas Gustafsson std::cerr << "Child stderr\n";
5d82424f5d3c77c092c111b935041fd3dc4b3e98Andreas Gustafsson exit(EXIT_SUCCESS);
5d82424f5d3c77c092c111b935041fd3dc4b3e98Andreas Gustafsson }
7e361074bc8a2df7a0891a7040eea02ca3a5e286Andreas Gustafsson
7e361074bc8a2df7a0891a7040eea02ca3a5e286Andreas Gustafsson int status;
7e361074bc8a2df7a0891a7040eea02ca3a5e286Andreas Gustafsson ATF_REQUIRE(waitpid(pid, &status, 0) != -1);
7e361074bc8a2df7a0891a7040eea02ca3a5e286Andreas Gustafsson ATF_REQUIRE(WIFEXITED(status));
7e361074bc8a2df7a0891a7040eea02ca3a5e286Andreas Gustafsson ATF_REQUIRE_EQ(EXIT_SUCCESS, WEXITSTATUS(status));
7e361074bc8a2df7a0891a7040eea02ca3a5e286Andreas Gustafsson
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 Gustafsson}
7e361074bc8a2df7a0891a7040eea02ca3a5e286Andreas Gustafsson
7e361074bc8a2df7a0891a7040eea02ca3a5e286Andreas GustafssonATF_TEST_CASE_WITHOUT_HEAD(grep_collection__set);
7e361074bc8a2df7a0891a7040eea02ca3a5e286Andreas GustafssonATF_TEST_CASE_BODY(grep_collection__set)
7e361074bc8a2df7a0891a7040eea02ca3a5e286Andreas Gustafsson{
7e361074bc8a2df7a0891a7040eea02ca3a5e286Andreas Gustafsson std::set< std::string > strings;
d80da258e377f02f0dcd703b89a1044cc58e949fMichael Graff strings.insert("First");
d80da258e377f02f0dcd703b89a1044cc58e949fMichael Graff strings.insert("Second");
d80da258e377f02f0dcd703b89a1044cc58e949fMichael Graff
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 Graff}
d80da258e377f02f0dcd703b89a1044cc58e949fMichael Graff
d80da258e377f02f0dcd703b89a1044cc58e949fMichael GraffATF_TEST_CASE_WITHOUT_HEAD(grep_collection__vector);
d80da258e377f02f0dcd703b89a1044cc58e949fMichael GraffATF_TEST_CASE_BODY(grep_collection__vector)
d80da258e377f02f0dcd703b89a1044cc58e949fMichael Graff{
6eccf5bd07eb9abf65cc08fec4a8fc97b62c0e1bBrian Wellington std::vector< std::string > strings;
6eccf5bd07eb9abf65cc08fec4a8fc97b62c0e1bBrian Wellington strings.push_back("First");
6eccf5bd07eb9abf65cc08fec4a8fc97b62c0e1bBrian Wellington strings.push_back("Second");
de6fe5ebe289d8e922b2fd8e8d642b6dd4f2a8efDavid Lawrence
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));
6eccf5bd07eb9abf65cc08fec4a8fc97b62c0e1bBrian Wellington}
900d7d9c9bc443110f9a2b93c94def44093d64b0David Lawrence
6eccf5bd07eb9abf65cc08fec4a8fc97b62c0e1bBrian WellingtonATF_TEST_CASE_WITHOUT_HEAD(grep_file);
de6fe5ebe289d8e922b2fd8e8d642b6dd4f2a8efDavid LawrenceATF_TEST_CASE_BODY(grep_file)
6eccf5bd07eb9abf65cc08fec4a8fc97b62c0e1bBrian Wellington{
de6fe5ebe289d8e922b2fd8e8d642b6dd4f2a8efDavid Lawrence atf::utils::create_file("test.txt", "line1\nthe second line\naaaabbbb\n");
6eccf5bd07eb9abf65cc08fec4a8fc97b62c0e1bBrian Wellington
6eccf5bd07eb9abf65cc08fec4a8fc97b62c0e1bBrian Wellington ATF_REQUIRE(atf::utils::grep_file("line1", "test.txt"));
ATF_REQUIRE(atf::utils::grep_file("second line", "test.txt"));
ATF_REQUIRE(atf::utils::grep_file("aa.*bb", "test.txt"));
ATF_REQUIRE(!atf::utils::grep_file("foo", "test.txt"));
ATF_REQUIRE(!atf::utils::grep_file("bar", "test.txt"));
ATF_REQUIRE(!atf::utils::grep_file("aaaaa", "test.txt"));
}
ATF_TEST_CASE_WITHOUT_HEAD(grep_string);
ATF_TEST_CASE_BODY(grep_string)
{
const char *str = "a string - aaaabbbb";
ATF_REQUIRE(atf::utils::grep_string("a string", str));
ATF_REQUIRE(atf::utils::grep_string("^a string", str));
ATF_REQUIRE(atf::utils::grep_string("aaaabbbb$", str));
ATF_REQUIRE(atf::utils::grep_string("aa.*bb", str));
ATF_REQUIRE(!atf::utils::grep_string("foo", str));
ATF_REQUIRE(!atf::utils::grep_string("bar", str));
ATF_REQUIRE(!atf::utils::grep_string("aaaaa", str));
}
ATF_TEST_CASE_WITHOUT_HEAD(redirect__stdout);
ATF_TEST_CASE_BODY(redirect__stdout)
{
std::cout << "Buffer this";
atf::utils::redirect(STDOUT_FILENO, "captured.txt");
std::cout << "The printed message";
std::cout.flush();
ATF_REQUIRE_EQ("The printed message", read_file("captured.txt"));
}
ATF_TEST_CASE_WITHOUT_HEAD(redirect__stderr);
ATF_TEST_CASE_BODY(redirect__stderr)
{
std::cerr << "Buffer this";
atf::utils::redirect(STDERR_FILENO, "captured.txt");
std::cerr << "The printed message";
std::cerr.flush();
ATF_REQUIRE_EQ("The printed message", read_file("captured.txt"));
}
ATF_TEST_CASE_WITHOUT_HEAD(redirect__other);
ATF_TEST_CASE_BODY(redirect__other)
{
const std::string message = "Foo bar\nbaz\n";
atf::utils::redirect(15, "captured.txt");
ATF_REQUIRE(write(15, message.c_str(), message.length()) != -1);
close(15);
ATF_REQUIRE_EQ(message, read_file("captured.txt"));
}
static void
fork_and_wait(const int exitstatus, const char* expout, const char* experr)
{
const pid_t pid = atf::utils::fork();
if (pid == 0) {
std::cout << "Some output\n";
std::cerr << "Some error\n";
exit(123);
}
atf::utils::wait(pid, exitstatus, expout, experr);
exit(EXIT_SUCCESS);
}
ATF_TEST_CASE_WITHOUT_HEAD(wait__ok);
ATF_TEST_CASE_BODY(wait__ok)
{
const pid_t control = fork();
ATF_REQUIRE(control != -1);
if (control == 0)
fork_and_wait(123, "Some output\n", "Some error\n");
else {
int status;
ATF_REQUIRE(waitpid(control, &status, 0) != -1);
ATF_REQUIRE(WIFEXITED(status));
ATF_REQUIRE_EQ(EXIT_SUCCESS, WEXITSTATUS(status));
}
}
ATF_TEST_CASE_WITHOUT_HEAD(wait__invalid_exitstatus);
ATF_TEST_CASE_BODY(wait__invalid_exitstatus)
{
const pid_t control = fork();
ATF_REQUIRE(control != -1);
if (control == 0)
fork_and_wait(120, "Some output\n", "Some error\n");
else {
int status;
ATF_REQUIRE(waitpid(control, &status, 0) != -1);
ATF_REQUIRE(WIFEXITED(status));
ATF_REQUIRE_EQ(EXIT_FAILURE, WEXITSTATUS(status));
}
}
ATF_TEST_CASE_WITHOUT_HEAD(wait__invalid_stdout);
ATF_TEST_CASE_BODY(wait__invalid_stdout)
{
const pid_t control = fork();
ATF_REQUIRE(control != -1);
if (control == 0)
fork_and_wait(123, "Some output foo\n", "Some error\n");
else {
int status;
ATF_REQUIRE(waitpid(control, &status, 0) != -1);
ATF_REQUIRE(WIFEXITED(status));
ATF_REQUIRE_EQ(EXIT_FAILURE, WEXITSTATUS(status));
}
}
ATF_TEST_CASE_WITHOUT_HEAD(wait__invalid_stderr);
ATF_TEST_CASE_BODY(wait__invalid_stderr)
{
const pid_t control = fork();
ATF_REQUIRE(control != -1);
if (control == 0)
fork_and_wait(123, "Some output\n", "Some error foo\n");
else {
int status;
ATF_REQUIRE(waitpid(control, &status, 0) != -1);
ATF_REQUIRE(WIFEXITED(status));
ATF_REQUIRE_EQ(EXIT_FAILURE, WEXITSTATUS(status));
}
}
ATF_TEST_CASE_WITHOUT_HEAD(wait__save_stdout);
ATF_TEST_CASE_BODY(wait__save_stdout)
{
const pid_t control = fork();
ATF_REQUIRE(control != -1);
if (control == 0)
fork_and_wait(123, "save:my-output.txt", "Some error\n");
else {
int status;
ATF_REQUIRE(waitpid(control, &status, 0) != -1);
ATF_REQUIRE(WIFEXITED(status));
ATF_REQUIRE_EQ(EXIT_SUCCESS, WEXITSTATUS(status));
ATF_REQUIRE(atf::utils::compare_file("my-output.txt", "Some output\n"));
}
}
ATF_TEST_CASE_WITHOUT_HEAD(wait__save_stderr);
ATF_TEST_CASE_BODY(wait__save_stderr)
{
const pid_t control = fork();
ATF_REQUIRE(control != -1);
if (control == 0)
fork_and_wait(123, "Some output\n", "save:my-output.txt");
else {
int status;
ATF_REQUIRE(waitpid(control, &status, 0) != -1);
ATF_REQUIRE(WIFEXITED(status));
ATF_REQUIRE_EQ(EXIT_SUCCESS, WEXITSTATUS(status));
ATF_REQUIRE(atf::utils::compare_file("my-output.txt", "Some error\n"));
}
}
// ------------------------------------------------------------------------
// Tests cases for the header file.
// ------------------------------------------------------------------------
HEADER_TC(include, "atf-c++/utils.hpp");
// ------------------------------------------------------------------------
// Main.
// ------------------------------------------------------------------------
ATF_INIT_TEST_CASES(tcs)
{
// Add the test for the free functions.
ATF_ADD_TEST_CASE(tcs, cat_file__empty);
ATF_ADD_TEST_CASE(tcs, cat_file__one_line);
ATF_ADD_TEST_CASE(tcs, cat_file__several_lines);
ATF_ADD_TEST_CASE(tcs, cat_file__no_newline_eof);
ATF_ADD_TEST_CASE(tcs, compare_file__empty__match);
ATF_ADD_TEST_CASE(tcs, compare_file__empty__not_match);
ATF_ADD_TEST_CASE(tcs, compare_file__short__match);
ATF_ADD_TEST_CASE(tcs, compare_file__short__not_match);
ATF_ADD_TEST_CASE(tcs, compare_file__long__match);
ATF_ADD_TEST_CASE(tcs, compare_file__long__not_match);
ATF_ADD_TEST_CASE(tcs, copy_file__empty);
ATF_ADD_TEST_CASE(tcs, copy_file__some_contents);
ATF_ADD_TEST_CASE(tcs, create_file);
ATF_ADD_TEST_CASE(tcs, file_exists);
ATF_ADD_TEST_CASE(tcs, fork);
ATF_ADD_TEST_CASE(tcs, grep_collection__set);
ATF_ADD_TEST_CASE(tcs, grep_collection__vector);
ATF_ADD_TEST_CASE(tcs, grep_file);
ATF_ADD_TEST_CASE(tcs, grep_string);
ATF_ADD_TEST_CASE(tcs, redirect__stdout);
ATF_ADD_TEST_CASE(tcs, redirect__stderr);
ATF_ADD_TEST_CASE(tcs, redirect__other);
ATF_ADD_TEST_CASE(tcs, wait__ok);
ATF_ADD_TEST_CASE(tcs, wait__invalid_exitstatus);
ATF_ADD_TEST_CASE(tcs, wait__invalid_stdout);
ATF_ADD_TEST_CASE(tcs, wait__invalid_stderr);
ATF_ADD_TEST_CASE(tcs, wait__save_stdout);
ATF_ADD_TEST_CASE(tcs, wait__save_stderr);
// Add the test cases for the header file.
ATF_ADD_TEST_CASE(tcs, include);
}