| 1 | // Copyright (c) 2013-2014 Sandstorm Development Group, Inc. and contributorscxx . . $ `<file>/src/kj/io-test.c++`/ |
| 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_SOURCEcxx . . $ `<built-in>:469:9`! |
| 23 | #define _GNU_SOURCE |
| 24 | #endif |
| 25 | |
| 26 | #include "io.h"cxx . . $ `<file>/src/kj/io.h`/ |
| 27 | #include "debug.h"cxx . . $ `<file>/src/kj/debug.h`/ |
| 28 | #include "miniposix.h"cxx . . $ `<file>/src/kj/miniposix.h`/ |
| 29 | #include <kj/compat/gtest.h>cxx . . $ `<file>/src/kj/compat/gtest.h`/ |
| 30 | |
| 31 | namespace kjcxx . . $ kj/ { |
| 32 | namespacecxx . . $ kj/`$anonymous_namespace_src/kj/io-test.c++`/ { |
| 33 | |
| 34 | TESTcxx . . $ `src/kj/compat/gtest.h:120:9`!(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_SYSCALLcxx . . $ `src/kj/debug.h:236:9`!(miniposix::pipe(fds)); |
| 40 | |
| 41 | FdInputStreamcxx . . $ kj/FdInputStream# incxx . . $ kj/FdInputStream#FdInputStream(9d886ccc22572476).((AutoCloseFdcxx . . $ kj/AutoCloseFd#(fds[0]))); |
| 42 | FdOutputStreamcxx . . $ kj/FdOutputStream# outcxx . . $ kj/FdOutputStream#FdOutputStream(9d886ccc22572476).((AutoCloseFdcxx . . $ kj/AutoCloseFd#(fds[1]))); |
| 43 | |
| 44 | ArrayPtrcxx . . $ kj/ArrayPtr#<const bytecxx . . $ kj/byte#> pieces[5] = { |
| 45 | arrayPtrcxx . . $ kj/arrayPtr(e55e65d9587a059a).(implicitCastcxx . . $ kj/implicitCast(6d8f76ed31d9267f).<const bytecxx . . $ kj/byte#*>(nullptr), 0), |
| 46 | arrayPtrcxx . . $ kj/arrayPtr(e55e65d9587a059a).(reinterpret_cast<const bytecxx . . $ kj/byte#*>("foo"), 3), |
| 47 | arrayPtrcxx . . $ kj/arrayPtr(e55e65d9587a059a).(implicitCastcxx . . $ kj/implicitCast(6d8f76ed31d9267f).<const bytecxx . . $ kj/byte#*>(nullptr), 0), |
| 48 | arrayPtrcxx . . $ kj/arrayPtr(e55e65d9587a059a).(reinterpret_cast<const bytecxx . . $ kj/byte#*>("bar"), 3), |
| 49 | arrayPtrcxx . . $ kj/arrayPtr(e55e65d9587a059a).(implicitCastcxx . . $ kj/implicitCast(6d8f76ed31d9267f).<const bytecxx . . $ kj/byte#*>(nullptr), 0) |
| 50 | }; |
| 51 | |
| 52 | out.writecxx . . $ kj/FdOutputStream#write(9a8c2cc5c087affd).(piecescxx . . $ kj/ArrayPtr#ArrayPtr(37a1ddb7d9d83419).); |
| 53 | |
| 54 | char buf[7]; |
| 55 | in.readcxx . . $ kj/InputStream#read(5500c4293b1bbef0).(buf, 6); |
| 56 | buf[6] = '\0'; |
| 57 | |
| 58 | EXPECT_STREQcxx . . $ `src/kj/compat/gtest.h:62:9`!("foobar", buf); |
| 59 | } |
| 60 | |
| 61 | KJ_TESTcxx . . $ `src/kj/test.h:72:9`!("stringify AutoCloseFd") { |
| 62 | int fds[2]; |
| 63 | KJ_SYSCALLcxx . . $ `src/kj/debug.h:236:9`!(miniposix::pipe(fds)); |
| 64 | AutoCloseFdcxx . . $ kj/AutoCloseFd# incxx . . $ kj/AutoCloseFd#AutoCloseFd(d4f767463ce0a6b3).(fds[0]), outcxx . . $ kj/AutoCloseFd#AutoCloseFd(d4f767463ce0a6b3).(fds[1]); |
| 65 | |
| 66 | KJ_EXPECTcxx . . $ `src/kj/test.h:92:9`!(kj::str(in) == kj::str(fds[0]), in, fds[0]); |
| 67 | } |
| 68 | |
| 69 | KJ_TESTcxx . . $ `src/kj/test.h:72:9`!("VectorOutputStream") { |
| 70 | VectorOutputStreamcxx . . $ kj/VectorOutputStream# outputcxx . . $ kj/VectorOutputStream#VectorOutputStream(2c44cd906ae4e829).(16); |
| 71 | auto buf = output.getWriteBuffercxx . . $ kj/VectorOutputStream#getWriteBuffer(fc5425f282210c4e).(); |
| 72 | KJ_ASSERTcxx . . $ `src/kj/debug.h:359:9`!(buf.size() == 16); |
| 73 | |
| 74 | for (auto i: kjcxx . . $ kj/::indicescxx . . $ kj/indices(5511956b41a96918).(buf)) { |
| 75 | buf[cxx . . $ kj/ArrayPtr#`operator[]`(ec43b8547ac5c60).i] = 'a' + i; |
| 76 | } |
| 77 | |
| 78 | output.writecxx . . $ kj/VectorOutputStream#write(8c7266571c1d65cc).(buf.begincxx . . $ kj/ArrayPtr#begin(155dc7f103cdb9db).(), 4); |
| 79 | KJ_ASSERTcxx . . $ `src/kj/debug.h:359:9`!(output.getArray().begin() == buf.begin()); |
| 80 | KJ_ASSERTcxx . . $ `src/kj/debug.h:359:9`!(output.getArray().size() == 4); |
| 81 | |
| 82 | auto buf2 = output.getWriteBuffercxx . . $ kj/VectorOutputStream#getWriteBuffer(fc5425f282210c4e).(); |
| 83 | KJ_ASSERTcxx . . $ `src/kj/debug.h:359:9`!(buf2.end() == buf.end()); |
| 84 | KJ_ASSERTcxx . . $ `src/kj/debug.h:359:9`!(buf2.size() == 12); |
| 85 | |
| 86 | output.writecxx . . $ kj/VectorOutputStream#write(8c7266571c1d65cc).(buf2.begincxx . . $ kj/ArrayPtr#begin(155dc7f103cdb9db).(), buf2.sizecxx . . $ kj/ArrayPtr#size(fe4d2d262d4164a8).()); |
| 87 | KJ_ASSERTcxx . . $ `src/kj/debug.h:359:9`!(output.getArray().begin() == buf.begin()); |
| 88 | KJ_ASSERTcxx . . $ `src/kj/debug.h:359:9`!(output.getArray().size() == 16); |
| 89 | |
| 90 | auto buf3 = output.getWriteBuffercxx . . $ kj/VectorOutputStream#getWriteBuffer(fc5425f282210c4e).(); |
| 91 | KJ_ASSERTcxx . . $ `src/kj/debug.h:359:9`!(buf3.size() == 16); |
| 92 | KJ_ASSERTcxx . . $ `src/kj/debug.h:359:9`!(output.getArray().begin() != buf.begin()); |
| 93 | KJ_ASSERTcxx . . $ `src/kj/debug.h:359:9`!(output.getArray().end() == buf3.begin()); |
| 94 | KJ_ASSERTcxx . . $ `src/kj/debug.h:359:9`!(kj::str(output.getArray().asChars()) == "abcdefghijklmnop"); |
| 95 | |
| 96 | bytecxx . . $ kj/byte# junk[24]; |
| 97 | for (auto i: kjcxx . . $ kj/::indicescxx . . $ kj/indices(5511956b41a96918).(junk)) { |
| 98 | junk[i] = 'A' + i; |
| 99 | } |
| 100 | |
| 101 | output.writecxx . . $ kj/VectorOutputStream#write(8c7266571c1d65cc).(junk, 4); |
| 102 | KJ_ASSERTcxx . . $ `src/kj/debug.h:359:9`!(output.getArray().begin() != buf.begin()); |
| 103 | KJ_ASSERTcxx . . $ `src/kj/debug.h:359:9`!(output.getArray().end() == buf3.begin() + 4); |
| 104 | KJ_ASSERTcxx . . $ `src/kj/debug.h:359:9`!(kj::str(output.getArray().asChars()) == "abcdefghijklmnopABCD"); |
| 105 | |
| 106 | output.writecxx . . $ kj/VectorOutputStream#write(8c7266571c1d65cc).(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_ASSERTcxx . . $ `src/kj/debug.h:359:9`!(output.getArray().end() != buf3.begin() + 24); |
| 110 | KJ_ASSERTcxx . . $ `src/kj/debug.h:359:9`!(kj::str(output.getArray().asChars()) == "abcdefghijklmnopABCDEFGHIJKLMNOPQRSTUVWX"); |
| 111 | |
| 112 | KJ_ASSERTcxx . . $ `src/kj/debug.h:359:9`!(output.getWriteBuffer().size() == 24); |
| 113 | KJ_ASSERTcxx . . $ `src/kj/debug.h:359:9`!(output.getWriteBuffer().begin() == output.getArray().begin() + 40); |
| 114 | |
| 115 | output.clearcxx . . $ kj/VectorOutputStream#clear(49f6e7a06ebc5aa8).(); |
| 116 | KJ_ASSERTcxx . . $ `src/kj/debug.h:359:9`!(output.getWriteBuffer().begin() == output.getArray().begin()); |
| 117 | KJ_ASSERTcxx . . $ `src/kj/debug.h:359:9`!(output.getWriteBuffer().size() == 64); |
| 118 | KJ_ASSERTcxx . . $ `src/kj/debug.h:359:9`!(output.getArray().size() == 0); |
| 119 | } |
| 120 | |
| 121 | class MockInputStreamcxx . . $ kj/`$anonymous_namespace_src/kj/io-test.c++`/MockInputStream#: public InputStreamcxx . . $ kj/InputStream# { |
| 122 | public: |
| 123 | MockInputStreamcxx . . $ kj/`$anonymous_namespace_src/kj/io-test.c++`/MockInputStream#MockInputStream(ad62ceb0e775be35).(kjcxx . . $ kj/::ArrayPtrcxx . . $ kj/ArrayPtr#<const bytecxx . . $ kj/byte#> bytes, size_t blockSize) |
| 124 | : bytescxx . . $ kj/`$anonymous_namespace_src/kj/io-test.c++`/MockInputStream#bytes.(bytes), blockSizecxx . . $ kj/`$anonymous_namespace_src/kj/io-test.c++`/MockInputStream#blockSize.(blockSize) {} |
| 125 | |
| 126 | size_t tryReadcxx . . $ kj/`$anonymous_namespace_src/kj/io-test.c++`/MockInputStream#tryRead(e2eaf0fd878a292f).(void* buffer, size_t minBytes, size_t maxBytes) override { |
| 127 | // Clamp max read to blockSize. |
| 128 | size_t n = kjcxx . . $ kj/::mincxx . . $ kj/min(4af833f8d939a8d2).(blockSizecxx . . $ kj/`$anonymous_namespace_src/kj/io-test.c++`/MockInputStream#blockSize., maxBytes); |
| 129 | |
| 130 | // Unless that's less than minBytes -- in which case, use minBytes. |
| 131 | n = kjcxx . . $ kj/::maxcxx . . $ kj/max(4af833f8d939a8d2).(n, minBytes); |
| 132 | |
| 133 | // But also don't read more data than we have. |
| 134 | n = kjcxx . . $ kj/::mincxx . . $ kj/min(4af833f8d939a8d2).(n, bytescxx . . $ kj/`$anonymous_namespace_src/kj/io-test.c++`/MockInputStream#bytes..sizecxx . . $ kj/ArrayPtr#size(fe4d2d262d4164a8).()); |
| 135 | |
| 136 | memcpy(buffer, bytescxx . . $ kj/`$anonymous_namespace_src/kj/io-test.c++`/MockInputStream#bytes..begincxx . . $ kj/ArrayPtr#begin(155dc7f103cdb9db).(), n); |
| 137 | bytescxx . . $ kj/`$anonymous_namespace_src/kj/io-test.c++`/MockInputStream#bytes. =cxx . . $ kj/ArrayPtr#`operator=`(596308cff75d5c06). bytescxx . . $ kj/`$anonymous_namespace_src/kj/io-test.c++`/MockInputStream#bytes..slicecxx . . $ kj/ArrayPtr#slice(2c7e4e1cf603d8fe).(n, bytescxx . . $ kj/`$anonymous_namespace_src/kj/io-test.c++`/MockInputStream#bytes..sizecxx . . $ kj/ArrayPtr#size(fe4d2d262d4164a8).()); |
| 138 | return n; |
| 139 | } |
| 140 | |
| 141 | private: |
| 142 | kjcxx . . $ kj/::ArrayPtrcxx . . $ kj/ArrayPtr#<const bytecxx . . $ kj/byte#> bytescxx . . $ kj/`$anonymous_namespace_src/kj/io-test.c++`/MockInputStream#bytes.; |
| 143 | size_t blockSizecxx . . $ kj/`$anonymous_namespace_src/kj/io-test.c++`/MockInputStream#blockSize.; |
| 144 | }; |
| 145 | |
| 146 | KJ_TESTcxx . . $ `src/kj/test.h:72:9`!("InputStream::readAllText() / readAllBytes()") { |
| 147 | auto bigText = strArraycxx . . $ kj/strArray(53035638ff13f71b).(kjcxx . . $ kj/::repeatcxx . . $ kj/repeat(3a17d17d9e859854).("foo bar baz"_kjcxx . . $ `operator""_kj`(f0c11caeac747)., 12345), ","); |
| 148 | size_t inputSizes[] = { 0, 1, 256, 4096, 8191, 8192, 8193, 10000, bigText.sizecxx . . $ kj/String#size(fe4d2d262d4164a8).() }; |
| 149 | size_t blockSizes[] = { 1, 4, 256, 4096, 8192, bigText.sizecxx . . $ kj/String#size(fe4d2d262d4164a8).() }; |
| 150 | uint64_tcxx . . $ uint64_t# limits[] = { |
| 151 | 0, 1, 256, |
| 152 | bigText.sizecxx . . $ kj/String#size(fe4d2d262d4164a8).() / 2, |
| 153 | bigText.sizecxx . . $ kj/String#size(fe4d2d262d4164a8).() - 1, |
| 154 | bigText.sizecxx . . $ kj/String#size(fe4d2d262d4164a8).(), |
| 155 | bigText.sizecxx . . $ kj/String#size(fe4d2d262d4164a8).() + 1, |
| 156 | kjcxx . . $ kj/::maxValuecxx . . $ kj/maxValue. |
| 157 | }; |
| 158 | |
| 159 | for (size_t inputSize: inputSizes) { |
| 160 | for (size_t blockSize: blockSizes) { |
| 161 | for (uint64_tcxx . . $ uint64_t# limit: limits) { |
| 162 | KJ_CONTEXTcxx . . $ `src/kj/debug.h:277:9`!(inputSize, blockSize, limit); |
| 163 | auto textSlice = bigText.asBytescxx . . $ kj/String#asBytes(fc5425f282210c4e).().slicecxx . . $ kj/ArrayPtr#slice(2c7e4e1cf603d8fe).(0, inputSize); |
| 164 | auto readAllText = [&]() { |
| 165 | MockInputStreamcxx . . $ kj/`$anonymous_namespace_src/kj/io-test.c++`/MockInputStream# inputcxx . . $ kj/`$anonymous_namespace_src/kj/io-test.c++`/MockInputStream#MockInputStream(ad62ceb0e775be35).(textSlice, blockSize); |
| 166 | return input.readAllTextcxx . . $ kj/InputStream#readAllText(1fe7ee1713dc9afb).(limit); |
| 167 | }; |
| 168 | auto readAllBytes = [&]() { |
| 169 | MockInputStreamcxx . . $ kj/`$anonymous_namespace_src/kj/io-test.c++`/MockInputStream# inputcxx . . $ kj/`$anonymous_namespace_src/kj/io-test.c++`/MockInputStream#MockInputStream(ad62ceb0e775be35).(textSlice, blockSize); |
| 170 | return input.readAllBytescxx . . $ kj/InputStream#readAllBytes(7a707b5bf3ddf9a9).(limit); |
| 171 | }; |
| 172 | if (limit > inputSize) { |
| 173 | KJ_EXPECTcxx . . $ `src/kj/test.h:92:9`!(readAllText().asBytes() == textSlice); |
| 174 | KJ_EXPECTcxx . . $ `src/kj/test.h:92:9`!(readAllBytes() == textSlice); |
| 175 | } else { |
| 176 | KJ_EXPECT_THROW_MESSAGEcxx . . $ `src/kj/test.h:150:9`!("Reached limit before EOF.", readAllText()); |
| 177 | KJ_EXPECT_THROW_MESSAGEcxx . . $ `src/kj/test.h:150:9`!("Reached limit before EOF.", readAllBytes()); |
| 178 | } |
| 179 | } |
| 180 | } |
| 181 | } |
| 182 | } |
| 183 | |
| 184 | KJ_TESTcxx . . $ `src/kj/test.h:72:9`!("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 | kjcxx . . $ kj/::bytecxx . . $ kj/byte# buffer[10] = { 0 }; |
| 194 | |
| 195 | ArrayOutputStreamcxx . . $ kj/ArrayOutputStream# outputcxx . . $ kj/ArrayOutputStream#ArrayOutputStream(263fd99fb500e72e).(arrayPtrcxx . . $ kj/arrayPtr(25656fe11b4c7c4b).(buffer, buffer + 5)); |
| 196 | |
| 197 | // Succeeds and fills the ArrayOutputStream. |
| 198 | output.writecxx . . $ kj/ArrayOutputStream#write(8c7266571c1d65cc).(buffer + 5, 5); |
| 199 | |
| 200 | // Previously this threw an inscrutable "size <= array.end() - fillPos" requirement failure. |
| 201 | KJ_EXPECT_THROW_MESSAGEcxx . . $ `src/kj/test.h:150:9`!( |
| 202 | "backing array was not large enough for the data written", |
| 203 | output.write(buffer + 5, 5)); |
| 204 | } |
| 205 | |
| 206 | } // namespace |
| 207 | } // namespace kj |