4a53e3c2b83c476a93148eaee0272649beb221caMark Andrews/* Copyright (c) 2010 The NetBSD Foundation, Inc.
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt * All rights reserved.
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt *
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 * are met:
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 *
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. */
4a53e3c2b83c476a93148eaee0272649beb221caMark Andrews
4a53e3c2b83c476a93148eaee0272649beb221caMark Andrews#include "atf-c/utils.h"
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt#include <sys/stat.h>
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt#include <sys/wait.h>
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt#include <fcntl.h>
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt#include <stddef.h>
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt#include <stdio.h>
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt#include <stdlib.h>
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt#include <string.h>
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt#include <unistd.h>
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt#include <atf-c.h>
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt
4a53e3c2b83c476a93148eaee0272649beb221caMark Andrews#include "atf-c/detail/dynstr.h"
4a53e3c2b83c476a93148eaee0272649beb221caMark Andrews#include "atf-c/detail/test_helpers.h"
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt/** Reads the contents of a file into a buffer.
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt *
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 *
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 *
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt * \return The count of bytes read. */
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Huntstatic ssize_t
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Huntread_file(const char *path, void *const buffer, const size_t buflen)
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt{
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt const int fd = open(path, O_RDONLY);
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt ATF_REQUIRE_MSG(fd != -1, "Cannot open %s", path);
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt const ssize_t length = read(fd, buffer, buflen - 1);
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt close(fd);
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt ATF_REQUIRE(length != -1);
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt ((char *)buffer)[length] = '\0';
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt return length;
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt}
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan HuntATF_TC_WITHOUT_HEAD(cat_file__empty);
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan HuntATF_TC_BODY(cat_file__empty, tc)
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt{
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt atf_utils_create_file("file.txt", "%s", "");
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt atf_utils_redirect(STDOUT_FILENO, "captured.txt");
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt atf_utils_cat_file("file.txt", "PREFIX");
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt fflush(stdout);
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt close(STDOUT_FILENO);
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt char buffer[1024];
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt read_file("captured.txt", buffer, sizeof(buffer));
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt ATF_REQUIRE_STREQ("", buffer);
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt}
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan HuntATF_TC_WITHOUT_HEAD(cat_file__one_line);
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan HuntATF_TC_BODY(cat_file__one_line, tc)
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt{
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 atf_utils_cat_file("file.txt", "PREFIX");
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt fflush(stdout);
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt close(STDOUT_FILENO);
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt char buffer[1024];
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt read_file("captured.txt", buffer, sizeof(buffer));
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt ATF_REQUIRE_STREQ("PREFIXThis is a single line\n", buffer);
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt}
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan HuntATF_TC_WITHOUT_HEAD(cat_file__several_lines);
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan HuntATF_TC_BODY(cat_file__several_lines, tc)
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt{
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 atf_utils_cat_file("file.txt", ">");
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt fflush(stdout);
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt close(STDOUT_FILENO);
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt char buffer[1024];
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}
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan HuntATF_TC_WITHOUT_HEAD(cat_file__no_newline_eof);
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan HuntATF_TC_BODY(cat_file__no_newline_eof, tc)
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt{
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt atf_utils_create_file("file.txt", "Foo\n bar baz");
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt atf_utils_redirect(STDOUT_FILENO, "captured.txt");
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt atf_utils_cat_file("file.txt", "PREFIX");
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt fflush(stdout);
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt close(STDOUT_FILENO);
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt char buffer[1024];
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt read_file("captured.txt", buffer, sizeof(buffer));
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt ATF_REQUIRE_STREQ("PREFIXFoo\nPREFIX bar baz", buffer);
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt}
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan HuntATF_TC_WITHOUT_HEAD(compare_file__empty__match);
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan HuntATF_TC_BODY(compare_file__empty__match, tc)
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt{
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt atf_utils_create_file("test.txt", "%s", "");
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt ATF_REQUIRE(atf_utils_compare_file("test.txt", ""));
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt}
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan HuntATF_TC_WITHOUT_HEAD(compare_file__empty__not_match);
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan HuntATF_TC_BODY(compare_file__empty__not_match, tc)
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt{
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt atf_utils_create_file("test.txt", "%s", "");
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}
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan HuntATF_TC_WITHOUT_HEAD(compare_file__short__match);
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan HuntATF_TC_BODY(compare_file__short__match, tc)
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt{
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 Hunt}
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan HuntATF_TC_WITHOUT_HEAD(compare_file__short__not_match);
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan HuntATF_TC_BODY(compare_file__short__not_match, tc)
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt{
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}
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan HuntATF_TC_WITHOUT_HEAD(compare_file__long__match);
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan HuntATF_TC_BODY(compare_file__long__match, tc)
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt{
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt char long_contents[3456];
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt size_t i = 0;
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt for (; i < sizeof(long_contents) - 1; i++)
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt long_contents[i] = '0' + (i % 10);
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt long_contents[i] = '\0';
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt atf_utils_create_file("test.txt", "%s", long_contents);
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt ATF_REQUIRE(atf_utils_compare_file("test.txt", long_contents));
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt}
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan HuntATF_TC_WITHOUT_HEAD(compare_file__long__not_match);
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan HuntATF_TC_BODY(compare_file__long__not_match, tc)
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt{
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt char long_contents[3456];
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt size_t i = 0;
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt for (; i < sizeof(long_contents) - 1; i++)
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt long_contents[i] = '0' + (i % 10);
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt long_contents[i] = '\0';
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt atf_utils_create_file("test.txt", "%s", long_contents);
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt
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 long_contents[i - 1] = 'Z';
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt ATF_REQUIRE(!atf_utils_compare_file("test.txt", long_contents));
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt}
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan HuntATF_TC_WITHOUT_HEAD(copy_file__empty);
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan HuntATF_TC_BODY(copy_file__empty, tc)
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt{
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt atf_utils_create_file("src.txt", "%s", "");
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt ATF_REQUIRE(chmod("src.txt", 0520) != -1);
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt atf_utils_copy_file("src.txt", "dest.txt");
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt ATF_REQUIRE(atf_utils_compare_file("dest.txt", ""));
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt struct stat sb;
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt ATF_REQUIRE(stat("dest.txt", &sb) != -1);
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt ATF_REQUIRE_EQ(0520, sb.st_mode & 0xfff);
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt}
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan HuntATF_TC_WITHOUT_HEAD(copy_file__some_contents);
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan HuntATF_TC_BODY(copy_file__some_contents, tc)
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt{
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt atf_utils_create_file("src.txt", "This is a\ntest file\n");
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt atf_utils_copy_file("src.txt", "dest.txt");
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt ATF_REQUIRE(atf_utils_compare_file("dest.txt", "This is a\ntest file\n"));
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt}
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan HuntATF_TC_WITHOUT_HEAD(create_file);
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan HuntATF_TC_BODY(create_file, tc)
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt{
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt atf_utils_create_file("test.txt", "This is a test with %d", 12345);
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt char buffer[128];
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt read_file("test.txt", buffer, sizeof(buffer));
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt ATF_REQUIRE_STREQ("This is a test with 12345", buffer);
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt}
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan HuntATF_TC_WITHOUT_HEAD(file_exists);
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan HuntATF_TC_BODY(file_exists, tc)
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt{
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt atf_utils_create_file("test.txt", "foo");
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt ATF_REQUIRE( atf_utils_file_exists("test.txt"));
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt ATF_REQUIRE( atf_utils_file_exists("./test.txt"));
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt ATF_REQUIRE(!atf_utils_file_exists("./test.tx"));
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt ATF_REQUIRE(!atf_utils_file_exists("test.txt2"));
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt}
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan HuntATF_TC_WITHOUT_HEAD(fork);
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan HuntATF_TC_BODY(fork, tc)
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt{
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt fprintf(stdout, "Should not get into child\n");
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt fprintf(stderr, "Should not get into child\n");
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt pid_t pid = atf_utils_fork();
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt if (pid == 0) {
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt fprintf(stdout, "Child stdout\n");
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt fprintf(stderr, "Child stderr\n");
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt exit(EXIT_SUCCESS);
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt }
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt int status;
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt ATF_REQUIRE(waitpid(pid, &status, 0) != -1);
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt ATF_REQUIRE(WIFEXITED(status));
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt ATF_REQUIRE_EQ(EXIT_SUCCESS, WEXITSTATUS(status));
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt
4a53e3c2b83c476a93148eaee0272649beb221caMark Andrews atf_dynstr_t out_name;
4a53e3c2b83c476a93148eaee0272649beb221caMark Andrews RE(atf_dynstr_init_fmt(&out_name, "atf_utils_fork_%d_out.txt", (int)pid));
4a53e3c2b83c476a93148eaee0272649beb221caMark Andrews atf_dynstr_t err_name;
4a53e3c2b83c476a93148eaee0272649beb221caMark Andrews RE(atf_dynstr_init_fmt(&err_name, "atf_utils_fork_%d_err.txt", (int)pid));
4a53e3c2b83c476a93148eaee0272649beb221caMark Andrews
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt char buffer[1024];
4a53e3c2b83c476a93148eaee0272649beb221caMark Andrews read_file(atf_dynstr_cstring(&out_name), buffer, sizeof(buffer));
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt ATF_REQUIRE_STREQ("Child stdout\n", buffer);
4a53e3c2b83c476a93148eaee0272649beb221caMark Andrews read_file(atf_dynstr_cstring(&err_name), buffer, sizeof(buffer));
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt ATF_REQUIRE_STREQ("Child stderr\n", buffer);
4a53e3c2b83c476a93148eaee0272649beb221caMark Andrews
4a53e3c2b83c476a93148eaee0272649beb221caMark Andrews atf_dynstr_fini(&err_name);
4a53e3c2b83c476a93148eaee0272649beb221caMark Andrews atf_dynstr_fini(&out_name);
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt}
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan HuntATF_TC_WITHOUT_HEAD(free_charpp__empty);
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan HuntATF_TC_BODY(free_charpp__empty, tc)
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt{
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt char **array = malloc(sizeof(char *) * 1);
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt array[0] = NULL;
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt atf_utils_free_charpp(array);
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt}
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan HuntATF_TC_WITHOUT_HEAD(free_charpp__some);
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan HuntATF_TC_BODY(free_charpp__some, tc)
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt{
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt char **array = malloc(sizeof(char *) * 4);
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt array[0] = strdup("first");
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt array[1] = strdup("second");
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt array[2] = strdup("third");
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt array[3] = NULL;
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt atf_utils_free_charpp(array);
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt}
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan HuntATF_TC_WITHOUT_HEAD(grep_file);
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan HuntATF_TC_BODY(grep_file, tc)
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt{
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt atf_utils_create_file("test.txt", "line1\nthe second line\naaaabbbb\n");
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt
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}
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan HuntATF_TC_WITHOUT_HEAD(grep_string);
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan HuntATF_TC_BODY(grep_string, tc)
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt{
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt const char *str = "a string - aaaabbbb";
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 ATF_CHECK(!atf_utils_grep_string("foo", str));
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt ATF_CHECK(!atf_utils_grep_string("bar", str));
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt ATF_CHECK(!atf_utils_grep_string("aaaaa", str));
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt}
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan HuntATF_TC_WITHOUT_HEAD(readline__none);
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan HuntATF_TC_BODY(readline__none, tc)
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt{
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt atf_utils_create_file("empty.txt", "%s", "");
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt const int fd = open("empty.txt", O_RDONLY);
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt ATF_REQUIRE(fd != -1);
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt ATF_REQUIRE(atf_utils_readline(fd) == NULL);
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt close(fd);
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt}
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan HuntATF_TC_WITHOUT_HEAD(readline__some);
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan HuntATF_TC_BODY(readline__some, tc)
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt{
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 const char *l3 = "Last line, without terminator";
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt atf_utils_create_file("test.txt", "%s\n%s\n%s", l1, l2, l3);
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt const int fd = open("test.txt", O_RDONLY);
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt ATF_REQUIRE(fd != -1);
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt char *line;
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt line = atf_utils_readline(fd);
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt ATF_REQUIRE_STREQ(l1, line);
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt free(line);
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt line = atf_utils_readline(fd);
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt ATF_REQUIRE_STREQ(l2, line);
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt free(line);
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt line = atf_utils_readline(fd);
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt ATF_REQUIRE_STREQ(l3, line);
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt free(line);
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt close(fd);
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt}
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan HuntATF_TC_WITHOUT_HEAD(redirect__stdout);
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan HuntATF_TC_BODY(redirect__stdout, tc)
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt{
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt printf("Buffer this");
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt atf_utils_redirect(STDOUT_FILENO, "captured.txt");
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt printf("The printed message");
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt fflush(stdout);
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt char buffer[1024];
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt read_file("captured.txt", buffer, sizeof(buffer));
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt ATF_REQUIRE_STREQ("The printed message", buffer);
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt}
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan HuntATF_TC_WITHOUT_HEAD(redirect__stderr);
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan HuntATF_TC_BODY(redirect__stderr, tc)
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt{
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt fprintf(stderr, "Buffer this");
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt atf_utils_redirect(STDERR_FILENO, "captured.txt");
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt fprintf(stderr, "The printed message");
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt fflush(stderr);
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt char buffer[1024];
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt read_file("captured.txt", buffer, sizeof(buffer));
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt ATF_REQUIRE_STREQ("The printed message", buffer);
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt}
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan HuntATF_TC_WITHOUT_HEAD(redirect__other);
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan HuntATF_TC_BODY(redirect__other, tc)
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt{
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt const char *message = "Foo bar\nbaz\n";
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt atf_utils_redirect(15, "captured.txt");
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt ATF_REQUIRE(write(15, message, strlen(message)) != -1);
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt close(15);
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt char buffer[1024];
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt read_file("captured.txt", buffer, sizeof(buffer));
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt ATF_REQUIRE_STREQ(message, buffer);
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt}
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Huntstatic void
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Huntfork_and_wait(const int exitstatus, const char* expout, const char* experr)
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt{
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt const pid_t pid = atf_utils_fork();
4a53e3c2b83c476a93148eaee0272649beb221caMark Andrews ATF_REQUIRE(pid != -1);
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt if (pid == 0) {
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt fprintf(stdout, "Some output\n");
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt fprintf(stderr, "Some error\n");
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt exit(123);
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt }
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt atf_utils_wait(pid, exitstatus, expout, experr);
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt exit(EXIT_SUCCESS);
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt}
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan HuntATF_TC_WITHOUT_HEAD(wait__ok);
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan HuntATF_TC_BODY(wait__ok, tc)
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt{
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt const pid_t control = fork();
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt ATF_REQUIRE(control != -1);
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt if (control == 0)
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt fork_and_wait(123, "Some output\n", "Some error\n");
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt else {
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt int status;
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt ATF_REQUIRE(waitpid(control, &status, 0) != -1);
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt ATF_REQUIRE(WIFEXITED(status));
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt ATF_REQUIRE_EQ(EXIT_SUCCESS, WEXITSTATUS(status));
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt }
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt}
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt
4a53e3c2b83c476a93148eaee0272649beb221caMark AndrewsATF_TC_WITHOUT_HEAD(wait__ok_nested);
4a53e3c2b83c476a93148eaee0272649beb221caMark AndrewsATF_TC_BODY(wait__ok_nested, tc)
4a53e3c2b83c476a93148eaee0272649beb221caMark Andrews{
4a53e3c2b83c476a93148eaee0272649beb221caMark Andrews const pid_t parent = atf_utils_fork();
4a53e3c2b83c476a93148eaee0272649beb221caMark Andrews ATF_REQUIRE(parent != -1);
4a53e3c2b83c476a93148eaee0272649beb221caMark Andrews if (parent == 0) {
4a53e3c2b83c476a93148eaee0272649beb221caMark Andrews const pid_t child = atf_utils_fork();
4a53e3c2b83c476a93148eaee0272649beb221caMark Andrews ATF_REQUIRE(child != -1);
4a53e3c2b83c476a93148eaee0272649beb221caMark Andrews if (child == 0) {
4a53e3c2b83c476a93148eaee0272649beb221caMark Andrews fflush(stderr);
4a53e3c2b83c476a93148eaee0272649beb221caMark Andrews fprintf(stdout, "Child output\n");
4a53e3c2b83c476a93148eaee0272649beb221caMark Andrews fflush(stdout);
4a53e3c2b83c476a93148eaee0272649beb221caMark Andrews fprintf(stderr, "Child error\n");
4a53e3c2b83c476a93148eaee0272649beb221caMark Andrews exit(50);
4a53e3c2b83c476a93148eaee0272649beb221caMark Andrews } else {
4a53e3c2b83c476a93148eaee0272649beb221caMark Andrews fprintf(stdout, "Parent output\n");
4a53e3c2b83c476a93148eaee0272649beb221caMark Andrews fprintf(stderr, "Parent error\n");
4a53e3c2b83c476a93148eaee0272649beb221caMark Andrews atf_utils_wait(child, 50, "Child output\n", "Child error\n");
4a53e3c2b83c476a93148eaee0272649beb221caMark Andrews exit(40);
4a53e3c2b83c476a93148eaee0272649beb221caMark Andrews }
4a53e3c2b83c476a93148eaee0272649beb221caMark Andrews } else {
4a53e3c2b83c476a93148eaee0272649beb221caMark Andrews atf_utils_wait(parent, 40,
4a53e3c2b83c476a93148eaee0272649beb221caMark Andrews "Parent output\n"
4a53e3c2b83c476a93148eaee0272649beb221caMark Andrews "subprocess stdout: Child output\n"
4a53e3c2b83c476a93148eaee0272649beb221caMark Andrews "subprocess stderr: Child error\n",
4a53e3c2b83c476a93148eaee0272649beb221caMark Andrews "Parent error\n");
4a53e3c2b83c476a93148eaee0272649beb221caMark Andrews }
4a53e3c2b83c476a93148eaee0272649beb221caMark Andrews}
4a53e3c2b83c476a93148eaee0272649beb221caMark Andrews
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan HuntATF_TC_WITHOUT_HEAD(wait__invalid_exitstatus);
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan HuntATF_TC_BODY(wait__invalid_exitstatus, tc)
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt{
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt const pid_t control = fork();
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt ATF_REQUIRE(control != -1);
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt if (control == 0)
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt fork_and_wait(120, "Some output\n", "Some error\n");
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt else {
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt int status;
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt ATF_REQUIRE(waitpid(control, &status, 0) != -1);
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt ATF_REQUIRE(WIFEXITED(status));
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt ATF_REQUIRE_EQ(EXIT_FAILURE, WEXITSTATUS(status));
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt }
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt}
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan HuntATF_TC_WITHOUT_HEAD(wait__invalid_stdout);
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan HuntATF_TC_BODY(wait__invalid_stdout, tc)
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt{
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt const pid_t control = fork();
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt ATF_REQUIRE(control != -1);
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt if (control == 0)
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt fork_and_wait(123, "Some output foo\n", "Some error\n");
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt else {
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt int status;
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt ATF_REQUIRE(waitpid(control, &status, 0) != -1);
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt ATF_REQUIRE(WIFEXITED(status));
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt ATF_REQUIRE_EQ(EXIT_FAILURE, WEXITSTATUS(status));
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt }
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt}
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan HuntATF_TC_WITHOUT_HEAD(wait__invalid_stderr);
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan HuntATF_TC_BODY(wait__invalid_stderr, tc)
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt{
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt const pid_t control = fork();
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt ATF_REQUIRE(control != -1);
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt if (control == 0)
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt fork_and_wait(123, "Some output\n", "Some error foo\n");
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt else {
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt int status;
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt ATF_REQUIRE(waitpid(control, &status, 0) != -1);
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt ATF_REQUIRE(WIFEXITED(status));
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt ATF_REQUIRE_EQ(EXIT_FAILURE, WEXITSTATUS(status));
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt }
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt}
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan HuntATF_TC_WITHOUT_HEAD(wait__save_stdout);
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan HuntATF_TC_BODY(wait__save_stdout, tc)
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt{
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt const pid_t control = fork();
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt ATF_REQUIRE(control != -1);
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt if (control == 0)
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt fork_and_wait(123, "save:my-output.txt", "Some error\n");
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt else {
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt int status;
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt ATF_REQUIRE(waitpid(control, &status, 0) != -1);
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt ATF_REQUIRE(WIFEXITED(status));
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt ATF_REQUIRE_EQ(EXIT_SUCCESS, WEXITSTATUS(status));
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt ATF_REQUIRE(atf_utils_compare_file("my-output.txt", "Some output\n"));
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt }
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt}
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan HuntATF_TC_WITHOUT_HEAD(wait__save_stderr);
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan HuntATF_TC_BODY(wait__save_stderr, tc)
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt{
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt const pid_t control = fork();
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt ATF_REQUIRE(control != -1);
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt if (control == 0)
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt fork_and_wait(123, "Some output\n", "save:my-output.txt");
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt else {
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt int status;
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt ATF_REQUIRE(waitpid(control, &status, 0) != -1);
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt ATF_REQUIRE(WIFEXITED(status));
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt ATF_REQUIRE_EQ(EXIT_SUCCESS, WEXITSTATUS(status));
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt ATF_REQUIRE(atf_utils_compare_file("my-output.txt", "Some error\n"));
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt }
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt}
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt
ef421f66f47224a42073deaf087378c5d0c9952eEvan HuntATF_TP_ADD_TCS(tp)
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt{
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt ATF_TP_ADD_TC(tp, cat_file__empty);
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt ATF_TP_ADD_TC(tp, cat_file__one_line);
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt ATF_TP_ADD_TC(tp, cat_file__several_lines);
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt ATF_TP_ADD_TC(tp, cat_file__no_newline_eof);
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt ATF_TP_ADD_TC(tp, compare_file__empty__match);
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt ATF_TP_ADD_TC(tp, compare_file__empty__not_match);
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt ATF_TP_ADD_TC(tp, compare_file__short__match);
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt ATF_TP_ADD_TC(tp, compare_file__short__not_match);
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt ATF_TP_ADD_TC(tp, compare_file__long__match);
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt ATF_TP_ADD_TC(tp, compare_file__long__not_match);
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt ATF_TP_ADD_TC(tp, copy_file__empty);
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt ATF_TP_ADD_TC(tp, copy_file__some_contents);
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt ATF_TP_ADD_TC(tp, create_file);
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt ATF_TP_ADD_TC(tp, file_exists);
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt ATF_TP_ADD_TC(tp, fork);
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt ATF_TP_ADD_TC(tp, free_charpp__empty);
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt ATF_TP_ADD_TC(tp, free_charpp__some);
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt ATF_TP_ADD_TC(tp, grep_file);
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt ATF_TP_ADD_TC(tp, grep_string);
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt ATF_TP_ADD_TC(tp, readline__none);
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt ATF_TP_ADD_TC(tp, readline__some);
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt ATF_TP_ADD_TC(tp, redirect__stdout);
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt ATF_TP_ADD_TC(tp, redirect__stderr);
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt ATF_TP_ADD_TC(tp, redirect__other);
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt ATF_TP_ADD_TC(tp, wait__ok);
4a53e3c2b83c476a93148eaee0272649beb221caMark Andrews ATF_TP_ADD_TC(tp, wait__ok_nested);
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt ATF_TP_ADD_TC(tp, wait__save_stdout);
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt ATF_TP_ADD_TC(tp, wait__save_stderr);
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt ATF_TP_ADD_TC(tp, wait__invalid_exitstatus);
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt ATF_TP_ADD_TC(tp, wait__invalid_stdout);
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt ATF_TP_ADD_TC(tp, wait__invalid_stderr);
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt return atf_no_error();
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt}