1*11be35a1SLionel Sambuc // Copyright 2010 Google Inc.
2*11be35a1SLionel Sambuc // All rights reserved.
3*11be35a1SLionel Sambuc //
4*11be35a1SLionel Sambuc // Redistribution and use in source and binary forms, with or without
5*11be35a1SLionel Sambuc // modification, are permitted provided that the following conditions are
6*11be35a1SLionel Sambuc // met:
7*11be35a1SLionel Sambuc //
8*11be35a1SLionel Sambuc // * Redistributions of source code must retain the above copyright
9*11be35a1SLionel Sambuc // notice, this list of conditions and the following disclaimer.
10*11be35a1SLionel Sambuc // * Redistributions in binary form must reproduce the above copyright
11*11be35a1SLionel Sambuc // notice, this list of conditions and the following disclaimer in the
12*11be35a1SLionel Sambuc // documentation and/or other materials provided with the distribution.
13*11be35a1SLionel Sambuc // * Neither the name of Google Inc. nor the names of its contributors
14*11be35a1SLionel Sambuc // may be used to endorse or promote products derived from this software
15*11be35a1SLionel Sambuc // without specific prior written permission.
16*11be35a1SLionel Sambuc //
17*11be35a1SLionel Sambuc // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18*11be35a1SLionel Sambuc // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19*11be35a1SLionel Sambuc // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20*11be35a1SLionel Sambuc // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21*11be35a1SLionel Sambuc // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22*11be35a1SLionel Sambuc // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23*11be35a1SLionel Sambuc // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24*11be35a1SLionel Sambuc // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25*11be35a1SLionel Sambuc // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26*11be35a1SLionel Sambuc // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27*11be35a1SLionel Sambuc // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28*11be35a1SLionel Sambuc
29*11be35a1SLionel Sambuc #include "utils/process/systembuf.hpp"
30*11be35a1SLionel Sambuc
31*11be35a1SLionel Sambuc extern "C" {
32*11be35a1SLionel Sambuc #include <sys/stat.h>
33*11be35a1SLionel Sambuc
34*11be35a1SLionel Sambuc #include <fcntl.h>
35*11be35a1SLionel Sambuc #include <unistd.h>
36*11be35a1SLionel Sambuc }
37*11be35a1SLionel Sambuc
38*11be35a1SLionel Sambuc #include <fstream>
39*11be35a1SLionel Sambuc
40*11be35a1SLionel Sambuc #include <atf-c++.hpp>
41*11be35a1SLionel Sambuc
42*11be35a1SLionel Sambuc using utils::process::systembuf;
43*11be35a1SLionel Sambuc
44*11be35a1SLionel Sambuc
45*11be35a1SLionel Sambuc static void
check_data(std::istream & is,std::size_t length)46*11be35a1SLionel Sambuc check_data(std::istream& is, std::size_t length)
47*11be35a1SLionel Sambuc {
48*11be35a1SLionel Sambuc char ch = 'A', chr;
49*11be35a1SLionel Sambuc std::size_t cnt = 0;
50*11be35a1SLionel Sambuc while (is >> chr) {
51*11be35a1SLionel Sambuc ATF_REQUIRE_EQ(ch, chr);
52*11be35a1SLionel Sambuc if (ch == 'Z')
53*11be35a1SLionel Sambuc ch = 'A';
54*11be35a1SLionel Sambuc else
55*11be35a1SLionel Sambuc ch++;
56*11be35a1SLionel Sambuc cnt++;
57*11be35a1SLionel Sambuc }
58*11be35a1SLionel Sambuc ATF_REQUIRE_EQ(cnt, length);
59*11be35a1SLionel Sambuc }
60*11be35a1SLionel Sambuc
61*11be35a1SLionel Sambuc
62*11be35a1SLionel Sambuc static void
write_data(std::ostream & os,std::size_t length)63*11be35a1SLionel Sambuc write_data(std::ostream& os, std::size_t length)
64*11be35a1SLionel Sambuc {
65*11be35a1SLionel Sambuc char ch = 'A';
66*11be35a1SLionel Sambuc for (std::size_t i = 0; i < length; i++) {
67*11be35a1SLionel Sambuc os << ch;
68*11be35a1SLionel Sambuc if (ch == 'Z')
69*11be35a1SLionel Sambuc ch = 'A';
70*11be35a1SLionel Sambuc else
71*11be35a1SLionel Sambuc ch++;
72*11be35a1SLionel Sambuc }
73*11be35a1SLionel Sambuc os.flush();
74*11be35a1SLionel Sambuc }
75*11be35a1SLionel Sambuc
76*11be35a1SLionel Sambuc
77*11be35a1SLionel Sambuc static void
test_read(std::size_t length,std::size_t bufsize)78*11be35a1SLionel Sambuc test_read(std::size_t length, std::size_t bufsize)
79*11be35a1SLionel Sambuc {
80*11be35a1SLionel Sambuc std::ofstream f("test_read.txt");
81*11be35a1SLionel Sambuc write_data(f, length);
82*11be35a1SLionel Sambuc f.close();
83*11be35a1SLionel Sambuc
84*11be35a1SLionel Sambuc int fd = ::open("test_read.txt", O_RDONLY);
85*11be35a1SLionel Sambuc ATF_REQUIRE(fd != -1);
86*11be35a1SLionel Sambuc systembuf sb(fd, bufsize);
87*11be35a1SLionel Sambuc std::istream is(&sb);
88*11be35a1SLionel Sambuc check_data(is, length);
89*11be35a1SLionel Sambuc ::close(fd);
90*11be35a1SLionel Sambuc ::unlink("test_read.txt");
91*11be35a1SLionel Sambuc }
92*11be35a1SLionel Sambuc
93*11be35a1SLionel Sambuc
94*11be35a1SLionel Sambuc static void
test_write(std::size_t length,std::size_t bufsize)95*11be35a1SLionel Sambuc test_write(std::size_t length, std::size_t bufsize)
96*11be35a1SLionel Sambuc {
97*11be35a1SLionel Sambuc int fd = ::open("test_write.txt", O_WRONLY | O_CREAT | O_TRUNC,
98*11be35a1SLionel Sambuc S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
99*11be35a1SLionel Sambuc ATF_REQUIRE(fd != -1);
100*11be35a1SLionel Sambuc systembuf sb(fd, bufsize);
101*11be35a1SLionel Sambuc std::ostream os(&sb);
102*11be35a1SLionel Sambuc write_data(os, length);
103*11be35a1SLionel Sambuc ::close(fd);
104*11be35a1SLionel Sambuc
105*11be35a1SLionel Sambuc std::ifstream is("test_write.txt");
106*11be35a1SLionel Sambuc check_data(is, length);
107*11be35a1SLionel Sambuc is.close();
108*11be35a1SLionel Sambuc ::unlink("test_write.txt");
109*11be35a1SLionel Sambuc }
110*11be35a1SLionel Sambuc
111*11be35a1SLionel Sambuc
112*11be35a1SLionel Sambuc ATF_TEST_CASE(short_read);
ATF_TEST_CASE_HEAD(short_read)113*11be35a1SLionel Sambuc ATF_TEST_CASE_HEAD(short_read)
114*11be35a1SLionel Sambuc {
115*11be35a1SLionel Sambuc set_md_var("descr", "Tests that a short read (one that fits in the "
116*11be35a1SLionel Sambuc "internal buffer) works when using systembuf");
117*11be35a1SLionel Sambuc }
ATF_TEST_CASE_BODY(short_read)118*11be35a1SLionel Sambuc ATF_TEST_CASE_BODY(short_read)
119*11be35a1SLionel Sambuc {
120*11be35a1SLionel Sambuc test_read(64, 1024);
121*11be35a1SLionel Sambuc }
122*11be35a1SLionel Sambuc
123*11be35a1SLionel Sambuc
124*11be35a1SLionel Sambuc ATF_TEST_CASE(long_read);
ATF_TEST_CASE_HEAD(long_read)125*11be35a1SLionel Sambuc ATF_TEST_CASE_HEAD(long_read)
126*11be35a1SLionel Sambuc {
127*11be35a1SLionel Sambuc set_md_var("descr", "Tests that a long read (one that does not fit in "
128*11be35a1SLionel Sambuc "the internal buffer) works when using systembuf");
129*11be35a1SLionel Sambuc }
ATF_TEST_CASE_BODY(long_read)130*11be35a1SLionel Sambuc ATF_TEST_CASE_BODY(long_read)
131*11be35a1SLionel Sambuc {
132*11be35a1SLionel Sambuc test_read(64 * 1024, 1024);
133*11be35a1SLionel Sambuc }
134*11be35a1SLionel Sambuc
135*11be35a1SLionel Sambuc
136*11be35a1SLionel Sambuc ATF_TEST_CASE(short_write);
ATF_TEST_CASE_HEAD(short_write)137*11be35a1SLionel Sambuc ATF_TEST_CASE_HEAD(short_write)
138*11be35a1SLionel Sambuc {
139*11be35a1SLionel Sambuc set_md_var("descr", "Tests that a short write (one that fits in the "
140*11be35a1SLionel Sambuc "internal buffer) works when using systembuf");
141*11be35a1SLionel Sambuc }
ATF_TEST_CASE_BODY(short_write)142*11be35a1SLionel Sambuc ATF_TEST_CASE_BODY(short_write)
143*11be35a1SLionel Sambuc {
144*11be35a1SLionel Sambuc test_write(64, 1024);
145*11be35a1SLionel Sambuc }
146*11be35a1SLionel Sambuc
147*11be35a1SLionel Sambuc
148*11be35a1SLionel Sambuc ATF_TEST_CASE(long_write);
ATF_TEST_CASE_HEAD(long_write)149*11be35a1SLionel Sambuc ATF_TEST_CASE_HEAD(long_write)
150*11be35a1SLionel Sambuc {
151*11be35a1SLionel Sambuc set_md_var("descr", "Tests that a long write (one that does not fit "
152*11be35a1SLionel Sambuc "in the internal buffer) works when using systembuf");
153*11be35a1SLionel Sambuc }
ATF_TEST_CASE_BODY(long_write)154*11be35a1SLionel Sambuc ATF_TEST_CASE_BODY(long_write)
155*11be35a1SLionel Sambuc {
156*11be35a1SLionel Sambuc test_write(64 * 1024, 1024);
157*11be35a1SLionel Sambuc }
158*11be35a1SLionel Sambuc
159*11be35a1SLionel Sambuc
ATF_INIT_TEST_CASES(tcs)160*11be35a1SLionel Sambuc ATF_INIT_TEST_CASES(tcs)
161*11be35a1SLionel Sambuc {
162*11be35a1SLionel Sambuc ATF_ADD_TEST_CASE(tcs, short_read);
163*11be35a1SLionel Sambuc ATF_ADD_TEST_CASE(tcs, long_read);
164*11be35a1SLionel Sambuc ATF_ADD_TEST_CASE(tcs, short_write);
165*11be35a1SLionel Sambuc ATF_ADD_TEST_CASE(tcs, long_write);
166*11be35a1SLionel Sambuc }
167