Debian Codegraph @

src/kj/io-test.c++

raw symbols SCIP
1 // Copyright (c) 2013-2014 Sandstorm Development Group, Inc. and contributors
2 // Licensed under the MIT License:
3 //
4 // Permission is hereby granted, free of charge, to any person obtaining a copy
5 // of this software and associated documentation files (the "Software"), to deal
6 // in the Software without restriction, including without limitation the rights
7 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 // copies of the Software, and to permit persons to whom the Software is
9 // furnished to do so, subject to the following conditions:
10 //
11 // The above copyright notice and this permission notice shall be included in
12 // all copies or substantial portions of the Software.
13 //
14 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20 // THE SOFTWARE.
21
22 #ifndef _GNU_SOURCE
23 #define _GNU_SOURCE
24 #endif
25
26 #include "io.h"
27 #include "debug.h"
28 #include "miniposix.h"
29 #include <kj/compat/gtest.h>
30
31 namespace kj {
32 namespace {
33
34 TEST(Io, WriteVec) {
35 // Check that writing an array of arrays works even when some of the arrays are empty. (This
36 // used to not work in some cases.)
37
38 int fds[2];
39 KJ_SYSCALL(miniposix::pipe(fds));
40
41 FdInputStream in((AutoCloseFd(fds[0])));
42 FdOutputStream out((AutoCloseFd(fds[1])));
43
44 ArrayPtr<const byte> pieces[5] = {
45 arrayPtr(implicitCast<const byte*>(nullptr), 0),
46 arrayPtr(reinterpret_cast<const byte*>("foo"), 3),
47 arrayPtr(implicitCast<const byte*>(nullptr), 0),
48 arrayPtr(reinterpret_cast<const byte*>("bar"), 3),
49 arrayPtr(implicitCast<const byte*>(nullptr), 0)
50 };
51
52 out.write(pieces);
53
54 char buf[7];
55 in.read(buf, 6);
56 buf[6] = '\0';
57
58 EXPECT_STREQ("foobar", buf);
59 }
60
61 KJ_TEST("stringify AutoCloseFd") {
62 int fds[2];
63 KJ_SYSCALL(miniposix::pipe(fds));
64 AutoCloseFd in(fds[0]), out(fds[1]);
65
66 KJ_EXPECT(kj::str(in) == kj::str(fds[0]), in, fds[0]);
67 }
68
69 KJ_TEST("VectorOutputStream") {
70 VectorOutputStream output(16);
71 auto buf = output.getWriteBuffer();
72 KJ_ASSERT(buf.size() == 16);
73
74 for (auto i: kj::indices(buf)) {
75 buf[i] = 'a' + i;
76 }
77
78 output.write(buf.begin(), 4);
79 KJ_ASSERT(output.getArray().begin() == buf.begin());
80 KJ_ASSERT(output.getArray().size() == 4);
81
82 auto buf2 = output.getWriteBuffer();
83 KJ_ASSERT(buf2.end() == buf.end());
84 KJ_ASSERT(buf2.size() == 12);
85
86 output.write(buf2.begin(), buf2.size());
87 KJ_ASSERT(output.getArray().begin() == buf.begin());
88 KJ_ASSERT(output.getArray().size() == 16);
89
90 auto buf3 = output.getWriteBuffer();
91 KJ_ASSERT(buf3.size() == 16);
92 KJ_ASSERT(output.getArray().begin() != buf.begin());
93 KJ_ASSERT(output.getArray().end() == buf3.begin());
94 KJ_ASSERT(kj::str(output.getArray().asChars()) == "abcdefghijklmnop");
95
96 byte junk[24];
97 for (auto i: kj::indices(junk)) {
98 junk[i] = 'A' + i;
99 }
100
101 output.write(junk, 4);
102 KJ_ASSERT(output.getArray().begin() != buf.begin());
103 KJ_ASSERT(output.getArray().end() == buf3.begin() + 4);
104 KJ_ASSERT(kj::str(output.getArray().asChars()) == "abcdefghijklmnopABCD");
105
106 output.write(junk + 4, 20);
107 // (We can't assert output.getArray().begin() != buf.begin() because the memory allocator could
108 // legitimately have allocated a new array in the same space.)
109 KJ_ASSERT(output.getArray().end() != buf3.begin() + 24);
110 KJ_ASSERT(kj::str(output.getArray().asChars()) == "abcdefghijklmnopABCDEFGHIJKLMNOPQRSTUVWX");
111
112 KJ_ASSERT(output.getWriteBuffer().size() == 24);
113 KJ_ASSERT(output.getWriteBuffer().begin() == output.getArray().begin() + 40);
114
115 output.clear();
116 KJ_ASSERT(output.getWriteBuffer().begin() == output.getArray().begin());
117 KJ_ASSERT(output.getWriteBuffer().size() == 64);
118 KJ_ASSERT(output.getArray().size() == 0);
119 }
120
121 class MockInputStream: public InputStream {
122 public:
123 MockInputStream(kj::ArrayPtr<const byte> bytes, size_t blockSize)
124 : bytes(bytes), blockSize(blockSize) {}
125
126 size_t tryRead(void* buffer, size_t minBytes, size_t maxBytes) override {
127 // Clamp max read to blockSize.
128 size_t n = kj::min(blockSize, maxBytes);
129
130 // Unless that's less than minBytes -- in which case, use minBytes.
131 n = kj::max(n, minBytes);
132
133 // But also don't read more data than we have.
134 n = kj::min(n, bytes.size());
135
136 memcpy(buffer, bytes.begin(), n);
137 bytes = bytes.slice(n, bytes.size());
138 return n;
139 }
140
141 private:
142 kj::ArrayPtr<const byte> bytes;
143 size_t blockSize;
144 };
145
146 KJ_TEST("InputStream::readAllText() / readAllBytes()") {
147 auto bigText = strArray(kj::repeat("foo bar baz"_kj, 12345), ",");
148 size_t inputSizes[] = { 0, 1, 256, 4096, 8191, 8192, 8193, 10000, bigText.size() };
149 size_t blockSizes[] = { 1, 4, 256, 4096, 8192, bigText.size() };
150 uint64_t limits[] = {
151 0, 1, 256,
152 bigText.size() / 2,
153 bigText.size() - 1,
154 bigText.size(),
155 bigText.size() + 1,
156 kj::maxValue
157 };
158
159 for (size_t inputSize: inputSizes) {
160 for (size_t blockSize: blockSizes) {
161 for (uint64_t limit: limits) {
162 KJ_CONTEXT(inputSize, blockSize, limit);
163 auto textSlice = bigText.asBytes().slice(0, inputSize);
164 auto readAllText = [&]() {
165 MockInputStream input(textSlice, blockSize);
166 return input.readAllText(limit);
167 };
168 auto readAllBytes = [&]() {
169 MockInputStream input(textSlice, blockSize);
170 return input.readAllBytes(limit);
171 };
172 if (limit > inputSize) {
173 KJ_EXPECT(readAllText().asBytes() == textSlice);
174 KJ_EXPECT(readAllBytes() == textSlice);
175 } else {
176 KJ_EXPECT_THROW_MESSAGE("Reached limit before EOF.", readAllText());
177 KJ_EXPECT_THROW_MESSAGE("Reached limit before EOF.", readAllBytes());
178 }
179 }
180 }
181 }
182 }
183
184 KJ_TEST("ArrayOutputStream::write() does not assume adjacent write buffer is its own") {
185 // Previously, if ArrayOutputStream::write(src, size) saw that `src` equaled its fill position, it
186 // would assume that the write was already in its buffer. This assumption was buggy if the write
187 // buffer was directly adjacent in memory to the ArrayOutputStream's buffer, and the
188 // ArrayOutputStream was full (i.e., its fill position was one-past-the-end).
189 //
190 // VectorOutputStream also suffered a similar bug, but it is much harder to test, since it
191 // performs its own allocation.
192
193 kj::byte buffer[10] = { 0 };
194
195 ArrayOutputStream output(arrayPtr(buffer, buffer + 5));
196
197 // Succeeds and fills the ArrayOutputStream.
198 output.write(buffer + 5, 5);
199
200 // Previously this threw an inscrutable "size <= array.end() - fillPos" requirement failure.
201 KJ_EXPECT_THROW_MESSAGE(
202 "backing array was not large enough for the data written",
203 output.write(buffer + 5, 5));
204 }
205
206 } // namespace
207 } // namespace kj