head	1.1;
branch	1.1.1;
access;
symbols
	netbsd-11-0-RC5:1.1.1.1
	netbsd-11-0-RC4:1.1.1.1
	netbsd-11-0-RC3:1.1.1.1
	netbsd-11-0-RC2:1.1.1.1
	netbsd-11-0-RC1:1.1.1.1
	perseant-exfatfs-base-20250801:1.1.1.1
	netbsd-11:1.1.1.1.0.44
	netbsd-11-base:1.1.1.1
	netbsd-10-1-RELEASE:1.1.1.1
	perseant-exfatfs-base-20240630:1.1.1.1
	perseant-exfatfs:1.1.1.1.0.42
	perseant-exfatfs-base:1.1.1.1
	netbsd-8-3-RELEASE:1.1.1.1
	netbsd-9-4-RELEASE:1.1.1.1
	netbsd-10-0-RELEASE:1.1.1.1
	netbsd-10-0-RC6:1.1.1.1
	netbsd-10-0-RC5:1.1.1.1
	netbsd-10-0-RC4:1.1.1.1
	netbsd-10-0-RC3:1.1.1.1
	netbsd-10-0-RC2:1.1.1.1
	netbsd-10-0-RC1:1.1.1.1
	netbsd-10:1.1.1.1.0.40
	netbsd-10-base:1.1.1.1
	netbsd-9-3-RELEASE:1.1.1.1
	cjep_sun2x-base1:1.1.1.1
	cjep_sun2x:1.1.1.1.0.38
	cjep_sun2x-base:1.1.1.1
	cjep_staticlib_x-base1:1.1.1.1
	netbsd-9-2-RELEASE:1.1.1.1
	cjep_staticlib_x:1.1.1.1.0.36
	cjep_staticlib_x-base:1.1.1.1
	netbsd-9-1-RELEASE:1.1.1.1
	phil-wifi-20200421:1.1.1.1
	phil-wifi-20200411:1.1.1.1
	is-mlppp:1.1.1.1.0.34
	is-mlppp-base:1.1.1.1
	phil-wifi-20200406:1.1.1.1
	netbsd-8-2-RELEASE:1.1.1.1
	netbsd-9-0-RELEASE:1.1.1.1
	netbsd-9-0-RC2:1.1.1.1
	netbsd-9-0-RC1:1.1.1.1
	phil-wifi-20191119:1.1.1.1
	netbsd-9:1.1.1.1.0.32
	netbsd-9-base:1.1.1.1
	phil-wifi-20190609:1.1.1.1
	netbsd-8-1-RELEASE:1.1.1.1
	netbsd-8-1-RC1:1.1.1.1
	pgoyette-compat-merge-20190127:1.1.1.1
	pgoyette-compat-20190127:1.1.1.1
	pgoyette-compat-20190118:1.1.1.1
	pgoyette-compat-1226:1.1.1.1
	pgoyette-compat-1126:1.1.1.1
	pgoyette-compat-1020:1.1.1.1
	pgoyette-compat-0930:1.1.1.1
	pgoyette-compat-0906:1.1.1.1
	netbsd-7-2-RELEASE:1.1.1.1
	pgoyette-compat-0728:1.1.1.1
	netbsd-8-0-RELEASE:1.1.1.1
	phil-wifi:1.1.1.1.0.30
	phil-wifi-base:1.1.1.1
	pgoyette-compat-0625:1.1.1.1
	netbsd-8-0-RC2:1.1.1.1
	pgoyette-compat-0521:1.1.1.1
	pgoyette-compat-0502:1.1.1.1
	pgoyette-compat-0422:1.1.1.1
	netbsd-8-0-RC1:1.1.1.1
	pgoyette-compat-0415:1.1.1.1
	pgoyette-compat-0407:1.1.1.1
	pgoyette-compat-0330:1.1.1.1
	pgoyette-compat-0322:1.1.1.1
	pgoyette-compat-0315:1.1.1.1
	netbsd-7-1-2-RELEASE:1.1.1.1
	pgoyette-compat:1.1.1.1.0.28
	pgoyette-compat-base:1.1.1.1
	netbsd-7-1-1-RELEASE:1.1.1.1
	matt-nb8-mediatek:1.1.1.1.0.26
	matt-nb8-mediatek-base:1.1.1.1
	perseant-stdc-iso10646:1.1.1.1.0.24
	perseant-stdc-iso10646-base:1.1.1.1
	netbsd-8:1.1.1.1.0.22
	netbsd-8-base:1.1.1.1
	prg-localcount2-base3:1.1.1.1
	prg-localcount2-base2:1.1.1.1
	prg-localcount2-base1:1.1.1.1
	prg-localcount2:1.1.1.1.0.20
	prg-localcount2-base:1.1.1.1
	pgoyette-localcount-20170426:1.1.1.1
	bouyer-socketcan-base1:1.1.1.1
	pgoyette-localcount-20170320:1.1.1.1
	netbsd-7-1:1.1.1.1.0.18
	netbsd-7-1-RELEASE:1.1.1.1
	netbsd-7-1-RC2:1.1.1.1
	netbsd-7-nhusb-base-20170116:1.1.1.1
	bouyer-socketcan:1.1.1.1.0.16
	bouyer-socketcan-base:1.1.1.1
	pgoyette-localcount-20170107:1.1.1.1
	netbsd-7-1-RC1:1.1.1.1
	pgoyette-localcount-20161104:1.1.1.1
	netbsd-7-0-2-RELEASE:1.1.1.1
	localcount-20160914:1.1.1.1
	netbsd-7-nhusb:1.1.1.1.0.14
	netbsd-7-nhusb-base:1.1.1.1
	pgoyette-localcount-20160806:1.1.1.1
	pgoyette-localcount-20160726:1.1.1.1
	pgoyette-localcount:1.1.1.1.0.12
	pgoyette-localcount-base:1.1.1.1
	netbsd-7-0-1-RELEASE:1.1.1.1
	netbsd-7-0:1.1.1.1.0.10
	netbsd-7-0-RELEASE:1.1.1.1
	netbsd-7-0-RC3:1.1.1.1
	netbsd-7-0-RC2:1.1.1.1
	netbsd-7-0-RC1:1.1.1.1
	tls-maxphys-base:1.1.1.1
	tls-maxphys:1.1.1.1.0.8
	netbsd-7:1.1.1.1.0.6
	netbsd-7-base:1.1.1.1
	yamt-pagecache:1.1.1.1.0.4
	yamt-pagecache-base9:1.1.1.1
	tls-earlyentropy:1.1.1.1.0.2
	tls-earlyentropy-base:1.1.1.1
	riastradh-xf86-video-intel-2-7-1-pre-2-21-15:1.1.1.1
	riastradh-drm2-base3:1.1.1.1
	atf-0-19:1.1.1.1
	GITHUB_JMMV:1.1.1;
locks; strict;
comment	@// @;


1.1
date	2014.02.08.19.11.33;	author jmmv;	state Exp;
branches
	1.1.1.1;
next	;
commitid	CKljpjViL2DYykox;

1.1.1.1
date	2014.02.08.19.11.33;	author jmmv;	state Exp;
branches
	1.1.1.1.4.1
	1.1.1.1.8.1;
next	;
commitid	CKljpjViL2DYykox;

1.1.1.1.4.1
date	2014.02.08.19.11.33;	author yamt;	state dead;
branches;
next	1.1.1.1.4.2;
commitid	TzMwNuE6CLSD5yBx;

1.1.1.1.4.2
date	2014.05.22.15.40.12;	author yamt;	state Exp;
branches;
next	;
commitid	TzMwNuE6CLSD5yBx;

1.1.1.1.8.1
date	2014.02.08.19.11.33;	author tls;	state dead;
branches;
next	1.1.1.1.8.2;
commitid	jTnpym9Qu0o4R1Nx;

1.1.1.1.8.2
date	2014.08.19.23.45.54;	author tls;	state Exp;
branches;
next	;
commitid	jTnpym9Qu0o4R1Nx;


desc
@@


1.1
log
@Initial revision
@
text
@//
// Automated Testing Framework (atf)
//
// Copyright (c) 2007 The NetBSD Foundation, Inc.
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions
// are met:
// 1. Redistributions of source code must retain the above copyright
//    notice, this list of conditions and the following disclaimer.
// 2. Redistributions in binary form must reproduce the above copyright
//    notice, this list of conditions and the following disclaimer in the
//    documentation and/or other materials provided with the distribution.
//
// THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND
// CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
// INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
// IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY
// DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
// GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
// IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
// OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
// IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//

extern "C" {
#include <sys/stat.h>
#include <sys/wait.h>

#include <fcntl.h>
#include <unistd.h>
}

#include <cassert>
#include <cerrno>
#include <cstddef>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <iostream>
#include <istream>
#include <ostream>

#include <atf-c++.hpp>

#include "io.hpp"
#include "signals.hpp"

// ------------------------------------------------------------------------
// Auxiliary functions.
// ------------------------------------------------------------------------

static
void
systembuf_check_data(std::istream& is, std::size_t length)
{
    char ch = 'A', chr;
    std::size_t cnt = 0;
    while (is >> chr) {
        ATF_REQUIRE_EQ(ch, chr);
        if (ch == 'Z')
            ch = 'A';
        else
            ch++;
        cnt++;
    }
    ATF_REQUIRE_EQ(cnt, length);
}

static
void
systembuf_write_data(std::ostream& os, std::size_t length)
{
    char ch = 'A';
    for (std::size_t i = 0; i < length; i++) {
        os << ch;
        if (ch == 'Z')
            ch = 'A';
        else
            ch++;
    }
    os.flush();
}

static
void
systembuf_test_read(std::size_t length, std::size_t bufsize)
{
    using tools::io::systembuf;

    std::ofstream f("test_read.txt");
    systembuf_write_data(f, length);
    f.close();

    int fd = ::open("test_read.txt", O_RDONLY);
    ATF_REQUIRE(fd != -1);
    systembuf sb(fd, bufsize);
    std::istream is(&sb);
    systembuf_check_data(is, length);
    ::close(fd);
    ::unlink("test_read.txt");
}

static
void
systembuf_test_write(std::size_t length, std::size_t bufsize)
{
    using tools::io::systembuf;

    int fd = ::open("test_write.txt", O_WRONLY | O_CREAT | O_TRUNC,
                    S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
    ATF_REQUIRE(fd != -1);
    systembuf sb(fd, bufsize);
    std::ostream os(&sb);
    systembuf_write_data(os, length);
    ::close(fd);

    std::ifstream is("test_write.txt");
    systembuf_check_data(is, length);
    is.close();
    ::unlink("test_write.txt");
}

// ------------------------------------------------------------------------
// Test cases for the "file_handle" class.
// ------------------------------------------------------------------------

ATF_TEST_CASE(file_handle_ctor);
ATF_TEST_CASE_HEAD(file_handle_ctor)
{
    set_md_var("descr", "Tests file_handle's constructors");
}
ATF_TEST_CASE_BODY(file_handle_ctor)
{
    using tools::io::file_handle;

    file_handle fh1;
    ATF_REQUIRE(!fh1.is_valid());

    file_handle fh2(STDOUT_FILENO);
    ATF_REQUIRE(fh2.is_valid());
    fh2.disown();
}

ATF_TEST_CASE(file_handle_copy);
ATF_TEST_CASE_HEAD(file_handle_copy)
{
    set_md_var("descr", "Tests file_handle's copy constructor");
}
ATF_TEST_CASE_BODY(file_handle_copy)
{
    using tools::io::file_handle;

    file_handle fh1;
    file_handle fh2(STDOUT_FILENO);

    file_handle fh3(fh2);
    ATF_REQUIRE(!fh2.is_valid());
    ATF_REQUIRE(fh3.is_valid());

    fh1 = fh3;
    ATF_REQUIRE(!fh3.is_valid());
    ATF_REQUIRE(fh1.is_valid());

    fh1.disown();
}

ATF_TEST_CASE(file_handle_get);
ATF_TEST_CASE_HEAD(file_handle_get)
{
    set_md_var("descr", "Tests the file_handle::get method");
}
ATF_TEST_CASE_BODY(file_handle_get)
{
    using tools::io::file_handle;

    file_handle fh1(STDOUT_FILENO);
    ATF_REQUIRE_EQ(fh1.get(), STDOUT_FILENO);
}

ATF_TEST_CASE(file_handle_posix_remap);
ATF_TEST_CASE_HEAD(file_handle_posix_remap)
{
    set_md_var("descr", "Tests the file_handle::posix_remap method");
}
ATF_TEST_CASE_BODY(file_handle_posix_remap)
{
    using tools::io::file_handle;

    int pfd[2];

    ATF_REQUIRE(::pipe(pfd) != -1);
    file_handle rend(pfd[0]);
    file_handle wend(pfd[1]);

    ATF_REQUIRE(rend.get() != 10);
    ATF_REQUIRE(wend.get() != 10);
    wend.posix_remap(10);
    ATF_REQUIRE_EQ(wend.get(), 10);
    ATF_REQUIRE(::write(wend.get(), "test-posix-remap", 16) != -1);
    {
        char buf[17];
        ATF_REQUIRE_EQ(::read(rend.get(), buf, sizeof(buf)), 16);
        buf[16] = '\0';
        ATF_REQUIRE(std::strcmp(buf, "test-posix-remap") == 0);
    }

    // Redo previous to ensure that remapping over the same descriptor
    // has no side-effects.
    ATF_REQUIRE_EQ(wend.get(), 10);
    wend.posix_remap(10);
    ATF_REQUIRE_EQ(wend.get(), 10);
    ATF_REQUIRE(::write(wend.get(), "test-posix-remap", 16) != -1);
    {
        char buf[17];
        ATF_REQUIRE_EQ(::read(rend.get(), buf, sizeof(buf)), 16);
        buf[16] = '\0';
        ATF_REQUIRE(std::strcmp(buf, "test-posix-remap") == 0);
    }
}

// ------------------------------------------------------------------------
// Test cases for the "systembuf" class.
// ------------------------------------------------------------------------

ATF_TEST_CASE(systembuf_short_read);
ATF_TEST_CASE_HEAD(systembuf_short_read)
{
    set_md_var("descr", "Tests that a short read (one that fits in the "
               "internal buffer) works when using systembuf");
}
ATF_TEST_CASE_BODY(systembuf_short_read)
{
    systembuf_test_read(64, 1024);
}

ATF_TEST_CASE(systembuf_long_read);
ATF_TEST_CASE_HEAD(systembuf_long_read)
{
    set_md_var("descr", "Tests that a long read (one that does not fit in "
               "the internal buffer) works when using systembuf");
}
ATF_TEST_CASE_BODY(systembuf_long_read)
{
    systembuf_test_read(64 * 1024, 1024);
}

ATF_TEST_CASE(systembuf_short_write);
ATF_TEST_CASE_HEAD(systembuf_short_write)
{
    set_md_var("descr", "Tests that a short write (one that fits in the "
               "internal buffer) works when using systembuf");
}
ATF_TEST_CASE_BODY(systembuf_short_write)
{
    systembuf_test_write(64, 1024);
}

ATF_TEST_CASE(systembuf_long_write);
ATF_TEST_CASE_HEAD(systembuf_long_write)
{
    set_md_var("descr", "Tests that a long write (one that does not fit "
               "in the internal buffer) works when using systembuf");
}
ATF_TEST_CASE_BODY(systembuf_long_write)
{
    systembuf_test_write(64 * 1024, 1024);
}

// ------------------------------------------------------------------------
// Test cases for the "pistream" class.
// ------------------------------------------------------------------------

ATF_TEST_CASE(pistream);
ATF_TEST_CASE_HEAD(pistream)
{
    set_md_var("descr", "Tests the pistream class");
}
ATF_TEST_CASE_BODY(pistream)
{
    using tools::io::file_handle;
    using tools::io::pistream;
    using tools::io::systembuf;

    int fds[2];
    ATF_REQUIRE(::pipe(fds) != -1);

    pistream rend(fds[0]);

    systembuf wbuf(fds[1]);
    std::ostream wend(&wbuf);

    // XXX This assumes that the pipe's buffer is big enough to accept
    // the data written without blocking!
    wend << "1Test 1message\n";
    wend.flush();
    std::string tmp;
    rend >> tmp;
    ATF_REQUIRE_EQ(tmp, "1Test");
    rend >> tmp;
    ATF_REQUIRE_EQ(tmp, "1message");
}

// ------------------------------------------------------------------------
// Tests for the "muxer" class.
// ------------------------------------------------------------------------

namespace {

static void
check_stream(std::ostream& os)
{
    // If we receive a signal while writing to the stream, the bad bit gets set.
    // Things seem to behave fine afterwards if we clear such error condition.
    // However, I'm not sure if it's safe to query errno at this point.
    ATF_REQUIRE(os.good() || (os.bad() && errno == EINTR));
    os.clear();
}

class mock_muxer : public tools::io::muxer {
    void line_callback(const size_t index, const std::string& line)
    {
        // The following should be enabled but causes the output to be so big
        // that it is annoying.  Reenable at some point if we make atf store
        // the output of the test cases in some other way (e.g. only if a test
        // failes), because this message is the only help in seeing how the
        // test fails.
        //std::cout << "line_callback(" << index << ", " << line << ")\n";
        check_stream(std::cout);
        switch (index) {
        case 0: lines0.push_back(line); break;
        case 1: lines1.push_back(line); break;
        default: ATF_REQUIRE(false);
        }
    }

public:
    mock_muxer(const int* fds, const size_t nfds, const size_t bufsize) :
        muxer(fds, nfds, bufsize) {}

    std::vector< std::string > lines0;
    std::vector< std::string > lines1;
};

static bool child_finished = false;
static void sigchld_handler(int signo)
{
    assert(signo == SIGCHLD);
    child_finished = true;
}

static void
child_printer(const int pipeout[2], const int pipeerr[2],
              const size_t iterations)
{
    ::close(pipeout[0]);
    ::close(pipeerr[0]);
    ATF_REQUIRE(::dup2(pipeout[1], STDOUT_FILENO) != -1);
    ATF_REQUIRE(::dup2(pipeerr[1], STDERR_FILENO) != -1);
    ::close(pipeout[1]);
    ::close(pipeerr[1]);

    for (size_t i = 0; i < iterations; i++) {
        std::cout << "stdout " << i << "\n";
        std::cerr << "stderr " << i << "\n";
    }

    std::cout << "stdout eof\n";
    std::cerr << "stderr eof\n";
    std::exit(EXIT_SUCCESS);
}

static void
muxer_test(const size_t bufsize, const size_t iterations)
{
    int pipeout[2], pipeerr[2];
    ATF_REQUIRE(pipe(pipeout) != -1);
    ATF_REQUIRE(pipe(pipeerr) != -1);

    tools::signals::signal_programmer sigchld(SIGCHLD, sigchld_handler);

    std::cout.flush();
    std::cerr.flush();

    pid_t pid = ::fork();
    ATF_REQUIRE(pid != -1);
    if (pid == 0) {
        sigchld.unprogram();
        child_printer(pipeout, pipeerr, iterations);
        std::abort();
    }
    ::close(pipeout[1]);
    ::close(pipeerr[1]);

    int fds[2] = {pipeout[0], pipeerr[0]};
    mock_muxer mux(fds, 2, bufsize);

    mux.mux(child_finished);
    check_stream(std::cout);
    std::cout << "mux done\n";

    mux.flush();
    std::cout << "flush done\n";
    check_stream(std::cout);

    sigchld.unprogram();
    int status;
    ATF_REQUIRE(::waitpid(pid, &status, 0) != -1);
    ATF_REQUIRE(WIFEXITED(status));
    ATF_REQUIRE(WEXITSTATUS(status) == EXIT_SUCCESS);

    ATF_REQUIRE(std::cout.good());
    ATF_REQUIRE(std::cerr.good());
    for (size_t i = 0; i < iterations; i++) {
        std::ostringstream exp0, exp1;
        exp0 << "stdout " << i;
        exp1 << "stderr " << i;

        ATF_REQUIRE(mux.lines0.size() > i);
        ATF_REQUIRE_EQ(exp0.str(), mux.lines0[i]);
        ATF_REQUIRE(mux.lines1.size() > i);
        ATF_REQUIRE_EQ(exp1.str(), mux.lines1[i]);
    }
    ATF_REQUIRE_EQ("stdout eof", mux.lines0[iterations]);
    ATF_REQUIRE_EQ("stderr eof", mux.lines1[iterations]);
    std::cout << "all done\n";
}

} // anonymous namespace

ATF_TEST_CASE_WITHOUT_HEAD(muxer_small_buffer);
ATF_TEST_CASE_BODY(muxer_small_buffer)
{
    muxer_test(4, 20000);
}

ATF_TEST_CASE_WITHOUT_HEAD(muxer_large_buffer);
ATF_TEST_CASE_BODY(muxer_large_buffer)
{
    muxer_test(1024, 50000);
}

// ------------------------------------------------------------------------
// Main.
// ------------------------------------------------------------------------

ATF_INIT_TEST_CASES(tcs)
{
    // Add the tests for the "file_handle" class.
    ATF_ADD_TEST_CASE(tcs, file_handle_ctor);
    ATF_ADD_TEST_CASE(tcs, file_handle_copy);
    ATF_ADD_TEST_CASE(tcs, file_handle_get);
    ATF_ADD_TEST_CASE(tcs, file_handle_posix_remap);

    // Add the tests for the "systembuf" class.
    ATF_ADD_TEST_CASE(tcs, systembuf_short_read);
    ATF_ADD_TEST_CASE(tcs, systembuf_long_read);
    ATF_ADD_TEST_CASE(tcs, systembuf_short_write);
    ATF_ADD_TEST_CASE(tcs, systembuf_long_write);

    // Add the tests for the "pistream" class.
    ATF_ADD_TEST_CASE(tcs, pistream);

    // Add the tests for the "muxer" class.
    ATF_ADD_TEST_CASE(tcs, muxer_small_buffer);
    ATF_ADD_TEST_CASE(tcs, muxer_large_buffer);
}
@


1.1.1.1
log
@Import atf 0.19:

Changes in version 0.19
***********************

Experimental version released on February 7th, 2014.

This is the last release to bundle the code for the deprecated tools.
The next release will drop their code and will stop worrying about
backwards compatibility between the ATF libraries and what the old tools
may or may not support.

If you still require the old tools for some reason, grab a copy of the
'tools' directory now.  The code in this directory is standalone and
does not depend on any internal details of atf-c++ any longer.

* Various fixes and improvements to support running as part of the FreeBSD
  test suite.

* Project hosting moved from Google Code (as a subproject of Kyua) to
  GitHub (as a first-class project).  The main reason for the change is
  the suppression of binary downloads in Google Code on Jan 15th, 2014.
  See https://github.com/jmmv/atf/

* Removed builtin help from atf-sh(1) and atf-check(1) for simplicity
  reasons.  In other words, their -h option is gone.

* Moved the code of the deprecated tools into a 'tools' directory and
  completely decoupled their code from the internals of atf-c++.  The
  reason for this is to painlessly allow a third-party to maintain a
  copy of these tools after we delete them because upcoming changes to
  atf-c++ would break the stale tools.


Changes in version 0.18
***********************

Experimental version released on November 16th, 2013.

* Issue 45: Added require.memory support in atf-run for FreeBSD.

* Fixed an issue with the handling of cin with libc++.

* Issue 64: Fixed various mandoc formatting warnings.

* NetBSD PR bin/48284: Made atf-check flush its progress message to
  stdout so that an interrupted test case always shows the last message
  being executed.

* NetBSD PR bin/48285: Fixed atf_check examples in atf-sh-api(3).
@
text
@@


1.1.1.1.8.1
log
@file io_test.cpp was added on branch tls-maxphys on 2014-08-19 23:45:54 +0000
@
text
@d1 471
@


1.1.1.1.8.2
log
@Rebase to HEAD as of a few days ago.
@
text
@a0 471
//
// Automated Testing Framework (atf)
//
// Copyright (c) 2007 The NetBSD Foundation, Inc.
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions
// are met:
// 1. Redistributions of source code must retain the above copyright
//    notice, this list of conditions and the following disclaimer.
// 2. Redistributions in binary form must reproduce the above copyright
//    notice, this list of conditions and the following disclaimer in the
//    documentation and/or other materials provided with the distribution.
//
// THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND
// CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
// INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
// IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY
// DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
// GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
// IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
// OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
// IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//

extern "C" {
#include <sys/stat.h>
#include <sys/wait.h>

#include <fcntl.h>
#include <unistd.h>
}

#include <cassert>
#include <cerrno>
#include <cstddef>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <iostream>
#include <istream>
#include <ostream>

#include <atf-c++.hpp>

#include "io.hpp"
#include "signals.hpp"

// ------------------------------------------------------------------------
// Auxiliary functions.
// ------------------------------------------------------------------------

static
void
systembuf_check_data(std::istream& is, std::size_t length)
{
    char ch = 'A', chr;
    std::size_t cnt = 0;
    while (is >> chr) {
        ATF_REQUIRE_EQ(ch, chr);
        if (ch == 'Z')
            ch = 'A';
        else
            ch++;
        cnt++;
    }
    ATF_REQUIRE_EQ(cnt, length);
}

static
void
systembuf_write_data(std::ostream& os, std::size_t length)
{
    char ch = 'A';
    for (std::size_t i = 0; i < length; i++) {
        os << ch;
        if (ch == 'Z')
            ch = 'A';
        else
            ch++;
    }
    os.flush();
}

static
void
systembuf_test_read(std::size_t length, std::size_t bufsize)
{
    using tools::io::systembuf;

    std::ofstream f("test_read.txt");
    systembuf_write_data(f, length);
    f.close();

    int fd = ::open("test_read.txt", O_RDONLY);
    ATF_REQUIRE(fd != -1);
    systembuf sb(fd, bufsize);
    std::istream is(&sb);
    systembuf_check_data(is, length);
    ::close(fd);
    ::unlink("test_read.txt");
}

static
void
systembuf_test_write(std::size_t length, std::size_t bufsize)
{
    using tools::io::systembuf;

    int fd = ::open("test_write.txt", O_WRONLY | O_CREAT | O_TRUNC,
                    S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
    ATF_REQUIRE(fd != -1);
    systembuf sb(fd, bufsize);
    std::ostream os(&sb);
    systembuf_write_data(os, length);
    ::close(fd);

    std::ifstream is("test_write.txt");
    systembuf_check_data(is, length);
    is.close();
    ::unlink("test_write.txt");
}

// ------------------------------------------------------------------------
// Test cases for the "file_handle" class.
// ------------------------------------------------------------------------

ATF_TEST_CASE(file_handle_ctor);
ATF_TEST_CASE_HEAD(file_handle_ctor)
{
    set_md_var("descr", "Tests file_handle's constructors");
}
ATF_TEST_CASE_BODY(file_handle_ctor)
{
    using tools::io::file_handle;

    file_handle fh1;
    ATF_REQUIRE(!fh1.is_valid());

    file_handle fh2(STDOUT_FILENO);
    ATF_REQUIRE(fh2.is_valid());
    fh2.disown();
}

ATF_TEST_CASE(file_handle_copy);
ATF_TEST_CASE_HEAD(file_handle_copy)
{
    set_md_var("descr", "Tests file_handle's copy constructor");
}
ATF_TEST_CASE_BODY(file_handle_copy)
{
    using tools::io::file_handle;

    file_handle fh1;
    file_handle fh2(STDOUT_FILENO);

    file_handle fh3(fh2);
    ATF_REQUIRE(!fh2.is_valid());
    ATF_REQUIRE(fh3.is_valid());

    fh1 = fh3;
    ATF_REQUIRE(!fh3.is_valid());
    ATF_REQUIRE(fh1.is_valid());

    fh1.disown();
}

ATF_TEST_CASE(file_handle_get);
ATF_TEST_CASE_HEAD(file_handle_get)
{
    set_md_var("descr", "Tests the file_handle::get method");
}
ATF_TEST_CASE_BODY(file_handle_get)
{
    using tools::io::file_handle;

    file_handle fh1(STDOUT_FILENO);
    ATF_REQUIRE_EQ(fh1.get(), STDOUT_FILENO);
}

ATF_TEST_CASE(file_handle_posix_remap);
ATF_TEST_CASE_HEAD(file_handle_posix_remap)
{
    set_md_var("descr", "Tests the file_handle::posix_remap method");
}
ATF_TEST_CASE_BODY(file_handle_posix_remap)
{
    using tools::io::file_handle;

    int pfd[2];

    ATF_REQUIRE(::pipe(pfd) != -1);
    file_handle rend(pfd[0]);
    file_handle wend(pfd[1]);

    ATF_REQUIRE(rend.get() != 10);
    ATF_REQUIRE(wend.get() != 10);
    wend.posix_remap(10);
    ATF_REQUIRE_EQ(wend.get(), 10);
    ATF_REQUIRE(::write(wend.get(), "test-posix-remap", 16) != -1);
    {
        char buf[17];
        ATF_REQUIRE_EQ(::read(rend.get(), buf, sizeof(buf)), 16);
        buf[16] = '\0';
        ATF_REQUIRE(std::strcmp(buf, "test-posix-remap") == 0);
    }

    // Redo previous to ensure that remapping over the same descriptor
    // has no side-effects.
    ATF_REQUIRE_EQ(wend.get(), 10);
    wend.posix_remap(10);
    ATF_REQUIRE_EQ(wend.get(), 10);
    ATF_REQUIRE(::write(wend.get(), "test-posix-remap", 16) != -1);
    {
        char buf[17];
        ATF_REQUIRE_EQ(::read(rend.get(), buf, sizeof(buf)), 16);
        buf[16] = '\0';
        ATF_REQUIRE(std::strcmp(buf, "test-posix-remap") == 0);
    }
}

// ------------------------------------------------------------------------
// Test cases for the "systembuf" class.
// ------------------------------------------------------------------------

ATF_TEST_CASE(systembuf_short_read);
ATF_TEST_CASE_HEAD(systembuf_short_read)
{
    set_md_var("descr", "Tests that a short read (one that fits in the "
               "internal buffer) works when using systembuf");
}
ATF_TEST_CASE_BODY(systembuf_short_read)
{
    systembuf_test_read(64, 1024);
}

ATF_TEST_CASE(systembuf_long_read);
ATF_TEST_CASE_HEAD(systembuf_long_read)
{
    set_md_var("descr", "Tests that a long read (one that does not fit in "
               "the internal buffer) works when using systembuf");
}
ATF_TEST_CASE_BODY(systembuf_long_read)
{
    systembuf_test_read(64 * 1024, 1024);
}

ATF_TEST_CASE(systembuf_short_write);
ATF_TEST_CASE_HEAD(systembuf_short_write)
{
    set_md_var("descr", "Tests that a short write (one that fits in the "
               "internal buffer) works when using systembuf");
}
ATF_TEST_CASE_BODY(systembuf_short_write)
{
    systembuf_test_write(64, 1024);
}

ATF_TEST_CASE(systembuf_long_write);
ATF_TEST_CASE_HEAD(systembuf_long_write)
{
    set_md_var("descr", "Tests that a long write (one that does not fit "
               "in the internal buffer) works when using systembuf");
}
ATF_TEST_CASE_BODY(systembuf_long_write)
{
    systembuf_test_write(64 * 1024, 1024);
}

// ------------------------------------------------------------------------
// Test cases for the "pistream" class.
// ------------------------------------------------------------------------

ATF_TEST_CASE(pistream);
ATF_TEST_CASE_HEAD(pistream)
{
    set_md_var("descr", "Tests the pistream class");
}
ATF_TEST_CASE_BODY(pistream)
{
    using tools::io::file_handle;
    using tools::io::pistream;
    using tools::io::systembuf;

    int fds[2];
    ATF_REQUIRE(::pipe(fds) != -1);

    pistream rend(fds[0]);

    systembuf wbuf(fds[1]);
    std::ostream wend(&wbuf);

    // XXX This assumes that the pipe's buffer is big enough to accept
    // the data written without blocking!
    wend << "1Test 1message\n";
    wend.flush();
    std::string tmp;
    rend >> tmp;
    ATF_REQUIRE_EQ(tmp, "1Test");
    rend >> tmp;
    ATF_REQUIRE_EQ(tmp, "1message");
}

// ------------------------------------------------------------------------
// Tests for the "muxer" class.
// ------------------------------------------------------------------------

namespace {

static void
check_stream(std::ostream& os)
{
    // If we receive a signal while writing to the stream, the bad bit gets set.
    // Things seem to behave fine afterwards if we clear such error condition.
    // However, I'm not sure if it's safe to query errno at this point.
    ATF_REQUIRE(os.good() || (os.bad() && errno == EINTR));
    os.clear();
}

class mock_muxer : public tools::io::muxer {
    void line_callback(const size_t index, const std::string& line)
    {
        // The following should be enabled but causes the output to be so big
        // that it is annoying.  Reenable at some point if we make atf store
        // the output of the test cases in some other way (e.g. only if a test
        // failes), because this message is the only help in seeing how the
        // test fails.
        //std::cout << "line_callback(" << index << ", " << line << ")\n";
        check_stream(std::cout);
        switch (index) {
        case 0: lines0.push_back(line); break;
        case 1: lines1.push_back(line); break;
        default: ATF_REQUIRE(false);
        }
    }

public:
    mock_muxer(const int* fds, const size_t nfds, const size_t bufsize) :
        muxer(fds, nfds, bufsize) {}

    std::vector< std::string > lines0;
    std::vector< std::string > lines1;
};

static bool child_finished = false;
static void sigchld_handler(int signo)
{
    assert(signo == SIGCHLD);
    child_finished = true;
}

static void
child_printer(const int pipeout[2], const int pipeerr[2],
              const size_t iterations)
{
    ::close(pipeout[0]);
    ::close(pipeerr[0]);
    ATF_REQUIRE(::dup2(pipeout[1], STDOUT_FILENO) != -1);
    ATF_REQUIRE(::dup2(pipeerr[1], STDERR_FILENO) != -1);
    ::close(pipeout[1]);
    ::close(pipeerr[1]);

    for (size_t i = 0; i < iterations; i++) {
        std::cout << "stdout " << i << "\n";
        std::cerr << "stderr " << i << "\n";
    }

    std::cout << "stdout eof\n";
    std::cerr << "stderr eof\n";
    std::exit(EXIT_SUCCESS);
}

static void
muxer_test(const size_t bufsize, const size_t iterations)
{
    int pipeout[2], pipeerr[2];
    ATF_REQUIRE(pipe(pipeout) != -1);
    ATF_REQUIRE(pipe(pipeerr) != -1);

    tools::signals::signal_programmer sigchld(SIGCHLD, sigchld_handler);

    std::cout.flush();
    std::cerr.flush();

    pid_t pid = ::fork();
    ATF_REQUIRE(pid != -1);
    if (pid == 0) {
        sigchld.unprogram();
        child_printer(pipeout, pipeerr, iterations);
        std::abort();
    }
    ::close(pipeout[1]);
    ::close(pipeerr[1]);

    int fds[2] = {pipeout[0], pipeerr[0]};
    mock_muxer mux(fds, 2, bufsize);

    mux.mux(child_finished);
    check_stream(std::cout);
    std::cout << "mux done\n";

    mux.flush();
    std::cout << "flush done\n";
    check_stream(std::cout);

    sigchld.unprogram();
    int status;
    ATF_REQUIRE(::waitpid(pid, &status, 0) != -1);
    ATF_REQUIRE(WIFEXITED(status));
    ATF_REQUIRE(WEXITSTATUS(status) == EXIT_SUCCESS);

    ATF_REQUIRE(std::cout.good());
    ATF_REQUIRE(std::cerr.good());
    for (size_t i = 0; i < iterations; i++) {
        std::ostringstream exp0, exp1;
        exp0 << "stdout " << i;
        exp1 << "stderr " << i;

        ATF_REQUIRE(mux.lines0.size() > i);
        ATF_REQUIRE_EQ(exp0.str(), mux.lines0[i]);
        ATF_REQUIRE(mux.lines1.size() > i);
        ATF_REQUIRE_EQ(exp1.str(), mux.lines1[i]);
    }
    ATF_REQUIRE_EQ("stdout eof", mux.lines0[iterations]);
    ATF_REQUIRE_EQ("stderr eof", mux.lines1[iterations]);
    std::cout << "all done\n";
}

} // anonymous namespace

ATF_TEST_CASE_WITHOUT_HEAD(muxer_small_buffer);
ATF_TEST_CASE_BODY(muxer_small_buffer)
{
    muxer_test(4, 20000);
}

ATF_TEST_CASE_WITHOUT_HEAD(muxer_large_buffer);
ATF_TEST_CASE_BODY(muxer_large_buffer)
{
    muxer_test(1024, 50000);
}

// ------------------------------------------------------------------------
// Main.
// ------------------------------------------------------------------------

ATF_INIT_TEST_CASES(tcs)
{
    // Add the tests for the "file_handle" class.
    ATF_ADD_TEST_CASE(tcs, file_handle_ctor);
    ATF_ADD_TEST_CASE(tcs, file_handle_copy);
    ATF_ADD_TEST_CASE(tcs, file_handle_get);
    ATF_ADD_TEST_CASE(tcs, file_handle_posix_remap);

    // Add the tests for the "systembuf" class.
    ATF_ADD_TEST_CASE(tcs, systembuf_short_read);
    ATF_ADD_TEST_CASE(tcs, systembuf_long_read);
    ATF_ADD_TEST_CASE(tcs, systembuf_short_write);
    ATF_ADD_TEST_CASE(tcs, systembuf_long_write);

    // Add the tests for the "pistream" class.
    ATF_ADD_TEST_CASE(tcs, pistream);

    // Add the tests for the "muxer" class.
    ATF_ADD_TEST_CASE(tcs, muxer_small_buffer);
    ATF_ADD_TEST_CASE(tcs, muxer_large_buffer);
}
@


1.1.1.1.4.1
log
@file io_test.cpp was added on branch yamt-pagecache on 2014-05-22 15:40:12 +0000
@
text
@d1 471
@


1.1.1.1.4.2
log
@sync with head.

for a reference, the tree before this commit was tagged
as yamt-pagecache-tag8.

this commit was splitted into small chunks to avoid
a limitation of cvs.  ("Protocol error: too many arguments")
@
text
@a0 471
//
// Automated Testing Framework (atf)
//
// Copyright (c) 2007 The NetBSD Foundation, Inc.
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions
// are met:
// 1. Redistributions of source code must retain the above copyright
//    notice, this list of conditions and the following disclaimer.
// 2. Redistributions in binary form must reproduce the above copyright
//    notice, this list of conditions and the following disclaimer in the
//    documentation and/or other materials provided with the distribution.
//
// THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND
// CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
// INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
// IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY
// DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
// GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
// IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
// OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
// IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//

extern "C" {
#include <sys/stat.h>
#include <sys/wait.h>

#include <fcntl.h>
#include <unistd.h>
}

#include <cassert>
#include <cerrno>
#include <cstddef>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <iostream>
#include <istream>
#include <ostream>

#include <atf-c++.hpp>

#include "io.hpp"
#include "signals.hpp"

// ------------------------------------------------------------------------
// Auxiliary functions.
// ------------------------------------------------------------------------

static
void
systembuf_check_data(std::istream& is, std::size_t length)
{
    char ch = 'A', chr;
    std::size_t cnt = 0;
    while (is >> chr) {
        ATF_REQUIRE_EQ(ch, chr);
        if (ch == 'Z')
            ch = 'A';
        else
            ch++;
        cnt++;
    }
    ATF_REQUIRE_EQ(cnt, length);
}

static
void
systembuf_write_data(std::ostream& os, std::size_t length)
{
    char ch = 'A';
    for (std::size_t i = 0; i < length; i++) {
        os << ch;
        if (ch == 'Z')
            ch = 'A';
        else
            ch++;
    }
    os.flush();
}

static
void
systembuf_test_read(std::size_t length, std::size_t bufsize)
{
    using tools::io::systembuf;

    std::ofstream f("test_read.txt");
    systembuf_write_data(f, length);
    f.close();

    int fd = ::open("test_read.txt", O_RDONLY);
    ATF_REQUIRE(fd != -1);
    systembuf sb(fd, bufsize);
    std::istream is(&sb);
    systembuf_check_data(is, length);
    ::close(fd);
    ::unlink("test_read.txt");
}

static
void
systembuf_test_write(std::size_t length, std::size_t bufsize)
{
    using tools::io::systembuf;

    int fd = ::open("test_write.txt", O_WRONLY | O_CREAT | O_TRUNC,
                    S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
    ATF_REQUIRE(fd != -1);
    systembuf sb(fd, bufsize);
    std::ostream os(&sb);
    systembuf_write_data(os, length);
    ::close(fd);

    std::ifstream is("test_write.txt");
    systembuf_check_data(is, length);
    is.close();
    ::unlink("test_write.txt");
}

// ------------------------------------------------------------------------
// Test cases for the "file_handle" class.
// ------------------------------------------------------------------------

ATF_TEST_CASE(file_handle_ctor);
ATF_TEST_CASE_HEAD(file_handle_ctor)
{
    set_md_var("descr", "Tests file_handle's constructors");
}
ATF_TEST_CASE_BODY(file_handle_ctor)
{
    using tools::io::file_handle;

    file_handle fh1;
    ATF_REQUIRE(!fh1.is_valid());

    file_handle fh2(STDOUT_FILENO);
    ATF_REQUIRE(fh2.is_valid());
    fh2.disown();
}

ATF_TEST_CASE(file_handle_copy);
ATF_TEST_CASE_HEAD(file_handle_copy)
{
    set_md_var("descr", "Tests file_handle's copy constructor");
}
ATF_TEST_CASE_BODY(file_handle_copy)
{
    using tools::io::file_handle;

    file_handle fh1;
    file_handle fh2(STDOUT_FILENO);

    file_handle fh3(fh2);
    ATF_REQUIRE(!fh2.is_valid());
    ATF_REQUIRE(fh3.is_valid());

    fh1 = fh3;
    ATF_REQUIRE(!fh3.is_valid());
    ATF_REQUIRE(fh1.is_valid());

    fh1.disown();
}

ATF_TEST_CASE(file_handle_get);
ATF_TEST_CASE_HEAD(file_handle_get)
{
    set_md_var("descr", "Tests the file_handle::get method");
}
ATF_TEST_CASE_BODY(file_handle_get)
{
    using tools::io::file_handle;

    file_handle fh1(STDOUT_FILENO);
    ATF_REQUIRE_EQ(fh1.get(), STDOUT_FILENO);
}

ATF_TEST_CASE(file_handle_posix_remap);
ATF_TEST_CASE_HEAD(file_handle_posix_remap)
{
    set_md_var("descr", "Tests the file_handle::posix_remap method");
}
ATF_TEST_CASE_BODY(file_handle_posix_remap)
{
    using tools::io::file_handle;

    int pfd[2];

    ATF_REQUIRE(::pipe(pfd) != -1);
    file_handle rend(pfd[0]);
    file_handle wend(pfd[1]);

    ATF_REQUIRE(rend.get() != 10);
    ATF_REQUIRE(wend.get() != 10);
    wend.posix_remap(10);
    ATF_REQUIRE_EQ(wend.get(), 10);
    ATF_REQUIRE(::write(wend.get(), "test-posix-remap", 16) != -1);
    {
        char buf[17];
        ATF_REQUIRE_EQ(::read(rend.get(), buf, sizeof(buf)), 16);
        buf[16] = '\0';
        ATF_REQUIRE(std::strcmp(buf, "test-posix-remap") == 0);
    }

    // Redo previous to ensure that remapping over the same descriptor
    // has no side-effects.
    ATF_REQUIRE_EQ(wend.get(), 10);
    wend.posix_remap(10);
    ATF_REQUIRE_EQ(wend.get(), 10);
    ATF_REQUIRE(::write(wend.get(), "test-posix-remap", 16) != -1);
    {
        char buf[17];
        ATF_REQUIRE_EQ(::read(rend.get(), buf, sizeof(buf)), 16);
        buf[16] = '\0';
        ATF_REQUIRE(std::strcmp(buf, "test-posix-remap") == 0);
    }
}

// ------------------------------------------------------------------------
// Test cases for the "systembuf" class.
// ------------------------------------------------------------------------

ATF_TEST_CASE(systembuf_short_read);
ATF_TEST_CASE_HEAD(systembuf_short_read)
{
    set_md_var("descr", "Tests that a short read (one that fits in the "
               "internal buffer) works when using systembuf");
}
ATF_TEST_CASE_BODY(systembuf_short_read)
{
    systembuf_test_read(64, 1024);
}

ATF_TEST_CASE(systembuf_long_read);
ATF_TEST_CASE_HEAD(systembuf_long_read)
{
    set_md_var("descr", "Tests that a long read (one that does not fit in "
               "the internal buffer) works when using systembuf");
}
ATF_TEST_CASE_BODY(systembuf_long_read)
{
    systembuf_test_read(64 * 1024, 1024);
}

ATF_TEST_CASE(systembuf_short_write);
ATF_TEST_CASE_HEAD(systembuf_short_write)
{
    set_md_var("descr", "Tests that a short write (one that fits in the "
               "internal buffer) works when using systembuf");
}
ATF_TEST_CASE_BODY(systembuf_short_write)
{
    systembuf_test_write(64, 1024);
}

ATF_TEST_CASE(systembuf_long_write);
ATF_TEST_CASE_HEAD(systembuf_long_write)
{
    set_md_var("descr", "Tests that a long write (one that does not fit "
               "in the internal buffer) works when using systembuf");
}
ATF_TEST_CASE_BODY(systembuf_long_write)
{
    systembuf_test_write(64 * 1024, 1024);
}

// ------------------------------------------------------------------------
// Test cases for the "pistream" class.
// ------------------------------------------------------------------------

ATF_TEST_CASE(pistream);
ATF_TEST_CASE_HEAD(pistream)
{
    set_md_var("descr", "Tests the pistream class");
}
ATF_TEST_CASE_BODY(pistream)
{
    using tools::io::file_handle;
    using tools::io::pistream;
    using tools::io::systembuf;

    int fds[2];
    ATF_REQUIRE(::pipe(fds) != -1);

    pistream rend(fds[0]);

    systembuf wbuf(fds[1]);
    std::ostream wend(&wbuf);

    // XXX This assumes that the pipe's buffer is big enough to accept
    // the data written without blocking!
    wend << "1Test 1message\n";
    wend.flush();
    std::string tmp;
    rend >> tmp;
    ATF_REQUIRE_EQ(tmp, "1Test");
    rend >> tmp;
    ATF_REQUIRE_EQ(tmp, "1message");
}

// ------------------------------------------------------------------------
// Tests for the "muxer" class.
// ------------------------------------------------------------------------

namespace {

static void
check_stream(std::ostream& os)
{
    // If we receive a signal while writing to the stream, the bad bit gets set.
    // Things seem to behave fine afterwards if we clear such error condition.
    // However, I'm not sure if it's safe to query errno at this point.
    ATF_REQUIRE(os.good() || (os.bad() && errno == EINTR));
    os.clear();
}

class mock_muxer : public tools::io::muxer {
    void line_callback(const size_t index, const std::string& line)
    {
        // The following should be enabled but causes the output to be so big
        // that it is annoying.  Reenable at some point if we make atf store
        // the output of the test cases in some other way (e.g. only if a test
        // failes), because this message is the only help in seeing how the
        // test fails.
        //std::cout << "line_callback(" << index << ", " << line << ")\n";
        check_stream(std::cout);
        switch (index) {
        case 0: lines0.push_back(line); break;
        case 1: lines1.push_back(line); break;
        default: ATF_REQUIRE(false);
        }
    }

public:
    mock_muxer(const int* fds, const size_t nfds, const size_t bufsize) :
        muxer(fds, nfds, bufsize) {}

    std::vector< std::string > lines0;
    std::vector< std::string > lines1;
};

static bool child_finished = false;
static void sigchld_handler(int signo)
{
    assert(signo == SIGCHLD);
    child_finished = true;
}

static void
child_printer(const int pipeout[2], const int pipeerr[2],
              const size_t iterations)
{
    ::close(pipeout[0]);
    ::close(pipeerr[0]);
    ATF_REQUIRE(::dup2(pipeout[1], STDOUT_FILENO) != -1);
    ATF_REQUIRE(::dup2(pipeerr[1], STDERR_FILENO) != -1);
    ::close(pipeout[1]);
    ::close(pipeerr[1]);

    for (size_t i = 0; i < iterations; i++) {
        std::cout << "stdout " << i << "\n";
        std::cerr << "stderr " << i << "\n";
    }

    std::cout << "stdout eof\n";
    std::cerr << "stderr eof\n";
    std::exit(EXIT_SUCCESS);
}

static void
muxer_test(const size_t bufsize, const size_t iterations)
{
    int pipeout[2], pipeerr[2];
    ATF_REQUIRE(pipe(pipeout) != -1);
    ATF_REQUIRE(pipe(pipeerr) != -1);

    tools::signals::signal_programmer sigchld(SIGCHLD, sigchld_handler);

    std::cout.flush();
    std::cerr.flush();

    pid_t pid = ::fork();
    ATF_REQUIRE(pid != -1);
    if (pid == 0) {
        sigchld.unprogram();
        child_printer(pipeout, pipeerr, iterations);
        std::abort();
    }
    ::close(pipeout[1]);
    ::close(pipeerr[1]);

    int fds[2] = {pipeout[0], pipeerr[0]};
    mock_muxer mux(fds, 2, bufsize);

    mux.mux(child_finished);
    check_stream(std::cout);
    std::cout << "mux done\n";

    mux.flush();
    std::cout << "flush done\n";
    check_stream(std::cout);

    sigchld.unprogram();
    int status;
    ATF_REQUIRE(::waitpid(pid, &status, 0) != -1);
    ATF_REQUIRE(WIFEXITED(status));
    ATF_REQUIRE(WEXITSTATUS(status) == EXIT_SUCCESS);

    ATF_REQUIRE(std::cout.good());
    ATF_REQUIRE(std::cerr.good());
    for (size_t i = 0; i < iterations; i++) {
        std::ostringstream exp0, exp1;
        exp0 << "stdout " << i;
        exp1 << "stderr " << i;

        ATF_REQUIRE(mux.lines0.size() > i);
        ATF_REQUIRE_EQ(exp0.str(), mux.lines0[i]);
        ATF_REQUIRE(mux.lines1.size() > i);
        ATF_REQUIRE_EQ(exp1.str(), mux.lines1[i]);
    }
    ATF_REQUIRE_EQ("stdout eof", mux.lines0[iterations]);
    ATF_REQUIRE_EQ("stderr eof", mux.lines1[iterations]);
    std::cout << "all done\n";
}

} // anonymous namespace

ATF_TEST_CASE_WITHOUT_HEAD(muxer_small_buffer);
ATF_TEST_CASE_BODY(muxer_small_buffer)
{
    muxer_test(4, 20000);
}

ATF_TEST_CASE_WITHOUT_HEAD(muxer_large_buffer);
ATF_TEST_CASE_BODY(muxer_large_buffer)
{
    muxer_test(1024, 50000);
}

// ------------------------------------------------------------------------
// Main.
// ------------------------------------------------------------------------

ATF_INIT_TEST_CASES(tcs)
{
    // Add the tests for the "file_handle" class.
    ATF_ADD_TEST_CASE(tcs, file_handle_ctor);
    ATF_ADD_TEST_CASE(tcs, file_handle_copy);
    ATF_ADD_TEST_CASE(tcs, file_handle_get);
    ATF_ADD_TEST_CASE(tcs, file_handle_posix_remap);

    // Add the tests for the "systembuf" class.
    ATF_ADD_TEST_CASE(tcs, systembuf_short_read);
    ATF_ADD_TEST_CASE(tcs, systembuf_long_read);
    ATF_ADD_TEST_CASE(tcs, systembuf_short_write);
    ATF_ADD_TEST_CASE(tcs, systembuf_long_write);

    // Add the tests for the "pistream" class.
    ATF_ADD_TEST_CASE(tcs, pistream);

    // Add the tests for the "muxer" class.
    ATF_ADD_TEST_CASE(tcs, muxer_small_buffer);
    ATF_ADD_TEST_CASE(tcs, muxer_large_buffer);
}
@


