]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/kyua/utils/stream_test.cpp
Import the kyua test framework.
[FreeBSD/FreeBSD.git] / contrib / kyua / utils / stream_test.cpp
1 // Copyright 2011 The Kyua Authors.
2 // All rights reserved.
3 //
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are
6 // met:
7 //
8 // * Redistributions of source code must retain the above copyright
9 //   notice, this list of conditions and the following disclaimer.
10 // * Redistributions in binary form must reproduce the above copyright
11 //   notice, this list of conditions and the following disclaimer in the
12 //   documentation and/or other materials provided with the distribution.
13 // * Neither the name of Google Inc. nor the names of its contributors
14 //   may be used to endorse or promote products derived from this software
15 //   without specific prior written permission.
16 //
17 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28
29 #include "utils/stream.hpp"
30
31 #include <cstdlib>
32 #include <sstream>
33
34 #include <atf-c++.hpp>
35
36 #include "utils/fs/path.hpp"
37
38 namespace fs = utils::fs;
39
40
41 ATF_TEST_CASE_WITHOUT_HEAD(open_ostream__stdout);
42 ATF_TEST_CASE_BODY(open_ostream__stdout)
43 {
44     const pid_t pid = atf::utils::fork();
45     if (pid == 0) {
46         std::auto_ptr< std::ostream > output = utils::open_ostream(
47             fs::path("/dev/stdout"));
48         (*output) << "Message to stdout\n";
49         output.reset();
50         std::exit(EXIT_SUCCESS);
51     }
52     atf::utils::wait(pid, EXIT_SUCCESS, "Message to stdout\n", "");
53 }
54
55
56 ATF_TEST_CASE_WITHOUT_HEAD(open_ostream__stderr);
57 ATF_TEST_CASE_BODY(open_ostream__stderr)
58 {
59     const pid_t pid = atf::utils::fork();
60     if (pid == 0) {
61         std::auto_ptr< std::ostream > output = utils::open_ostream(
62             fs::path("/dev/stderr"));
63         (*output) << "Message to stderr\n";
64         output.reset();
65         std::exit(EXIT_SUCCESS);
66     }
67     atf::utils::wait(pid, EXIT_SUCCESS, "", "Message to stderr\n");
68 }
69
70
71 ATF_TEST_CASE_WITHOUT_HEAD(open_ostream__other);
72 ATF_TEST_CASE_BODY(open_ostream__other)
73 {
74     const pid_t pid = atf::utils::fork();
75     if (pid == 0) {
76         std::auto_ptr< std::ostream > output = utils::open_ostream(
77             fs::path("some-file.txt"));
78         (*output) << "Message to other file\n";
79         output.reset();
80         std::exit(EXIT_SUCCESS);
81     }
82     atf::utils::wait(pid, EXIT_SUCCESS, "", "");
83     atf::utils::compare_file("some-file.txt", "Message to other file\n");
84 }
85
86
87 ATF_TEST_CASE_WITHOUT_HEAD(stream_length__empty);
88 ATF_TEST_CASE_BODY(stream_length__empty)
89 {
90     std::istringstream input("");
91     ATF_REQUIRE_EQ(0, utils::stream_length(input));
92 }
93
94
95 ATF_TEST_CASE_WITHOUT_HEAD(stream_length__some);
96 ATF_TEST_CASE_BODY(stream_length__some)
97 {
98     const std::string contents(8192, 'x');
99     std::istringstream input(contents);
100     ATF_REQUIRE_EQ(
101         contents.length(),
102         static_cast< std::string::size_type >(utils::stream_length(input)));
103 }
104
105
106 ATF_TEST_CASE_WITHOUT_HEAD(read_file__ok);
107 ATF_TEST_CASE_BODY(read_file__ok)
108 {
109     const char* contents = "These are\nsome file contents";
110     atf::utils::create_file("input.txt", contents);
111     ATF_REQUIRE_EQ(contents, utils::read_file(fs::path("input.txt")));
112 }
113
114
115 ATF_TEST_CASE_WITHOUT_HEAD(read_file__missing_file);
116 ATF_TEST_CASE_BODY(read_file__missing_file)
117 {
118     ATF_REQUIRE_THROW_RE(std::runtime_error,
119                          "Failed to open 'foo.txt' for read",
120                          utils::read_file(fs::path("foo.txt")));
121 }
122
123
124 ATF_TEST_CASE_WITHOUT_HEAD(read_stream__empty);
125 ATF_TEST_CASE_BODY(read_stream__empty)
126 {
127     std::istringstream input("");
128     ATF_REQUIRE_EQ("", utils::read_stream(input));
129 }
130
131
132 ATF_TEST_CASE_WITHOUT_HEAD(read_stream__some);
133 ATF_TEST_CASE_BODY(read_stream__some)
134 {
135     std::string contents;
136     for (int i = 0; i < 1000; i++)
137         contents += "abcdef";
138     std::istringstream input(contents);
139     ATF_REQUIRE_EQ(contents, utils::read_stream(input));
140 }
141
142
143 ATF_INIT_TEST_CASES(tcs)
144 {
145     ATF_ADD_TEST_CASE(tcs, open_ostream__stdout);
146     ATF_ADD_TEST_CASE(tcs, open_ostream__stderr);
147     ATF_ADD_TEST_CASE(tcs, open_ostream__other);
148
149     ATF_ADD_TEST_CASE(tcs, stream_length__empty);
150     ATF_ADD_TEST_CASE(tcs, stream_length__some);
151
152     ATF_ADD_TEST_CASE(tcs, read_file__ok);
153     ATF_ADD_TEST_CASE(tcs, read_file__missing_file);
154
155     ATF_ADD_TEST_CASE(tcs, read_stream__empty);
156     ATF_ADD_TEST_CASE(tcs, read_stream__some);
157 }