1 // Copyright 2010 Google Inc.
2 // All rights reserved.
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are
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.
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.
29 #include "utils/process/systembuf.hpp"
40 #include <atf-c++.hpp>
42 using utils::process::systembuf
;
46 check_data(std::istream
& is
, std::size_t length
)
51 ATF_REQUIRE_EQ(ch
, chr
);
58 ATF_REQUIRE_EQ(cnt
, length
);
63 write_data(std::ostream
& os
, std::size_t length
)
66 for (std::size_t i
= 0; i
< length
; i
++) {
78 test_read(std::size_t length
, std::size_t bufsize
)
80 std::ofstream
f("test_read.txt");
81 write_data(f
, length
);
84 int fd
= ::open("test_read.txt", O_RDONLY
);
85 ATF_REQUIRE(fd
!= -1);
86 systembuf
sb(fd
, bufsize
);
88 check_data(is
, length
);
90 ::unlink("test_read.txt");
95 test_write(std::size_t length
, std::size_t bufsize
)
97 int fd
= ::open("test_write.txt", O_WRONLY
| O_CREAT
| O_TRUNC
,
98 S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IROTH
);
99 ATF_REQUIRE(fd
!= -1);
100 systembuf
sb(fd
, bufsize
);
101 std::ostream
os(&sb
);
102 write_data(os
, length
);
105 std::ifstream
is("test_write.txt");
106 check_data(is
, length
);
108 ::unlink("test_write.txt");
112 ATF_TEST_CASE(short_read
);
113 ATF_TEST_CASE_HEAD(short_read
)
115 set_md_var("descr", "Tests that a short read (one that fits in the "
116 "internal buffer) works when using systembuf");
118 ATF_TEST_CASE_BODY(short_read
)
124 ATF_TEST_CASE(long_read
);
125 ATF_TEST_CASE_HEAD(long_read
)
127 set_md_var("descr", "Tests that a long read (one that does not fit in "
128 "the internal buffer) works when using systembuf");
130 ATF_TEST_CASE_BODY(long_read
)
132 test_read(64 * 1024, 1024);
136 ATF_TEST_CASE(short_write
);
137 ATF_TEST_CASE_HEAD(short_write
)
139 set_md_var("descr", "Tests that a short write (one that fits in the "
140 "internal buffer) works when using systembuf");
142 ATF_TEST_CASE_BODY(short_write
)
144 test_write(64, 1024);
148 ATF_TEST_CASE(long_write
);
149 ATF_TEST_CASE_HEAD(long_write
)
151 set_md_var("descr", "Tests that a long write (one that does not fit "
152 "in the internal buffer) works when using systembuf");
154 ATF_TEST_CASE_BODY(long_write
)
156 test_write(64 * 1024, 1024);
160 ATF_INIT_TEST_CASES(tcs
)
162 ATF_ADD_TEST_CASE(tcs
, short_read
);
163 ATF_ADD_TEST_CASE(tcs
, long_read
);
164 ATF_ADD_TEST_CASE(tcs
, short_write
);
165 ATF_ADD_TEST_CASE(tcs
, long_write
);