1 /* Copyright (c) 2014, Google Inc.
3 * Permission to use, copy, modify, and/or distribute this software for any
4 * purpose with or without fee is hereby granted, provided that the above
5 * copyright notice and this permission notice appear in all copies.
7 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
8 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
9 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
10 * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
11 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
12 * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
13 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */
18 #include <gtest/gtest.h>
20 #include <openssl/bio.h>
21 #include <openssl/crypto.h>
22 #include <openssl/err.h>
23 #include <openssl/mem.h>
25 #include "../internal.h"
26 #include "../test/test_util.h"
28 #if !defined(OPENSSL_WINDOWS)
29 #include <arpa/inet.h>
32 #include <netinet/in.h>
34 #include <sys/socket.h>
38 OPENSSL_MSVC_PRAGMA(warning(push, 3))
41 OPENSSL_MSVC_PRAGMA(warning(pop))
45 #if !defined(OPENSSL_WINDOWS)
46 static int closesocket(int sock) { return close(sock); }
47 static std::string LastSocketError() { return strerror(errno); }
49 static std::string LastSocketError() {
50 char buf[DECIMAL_SIZE(int) + 1];
51 BIO_snprintf(buf, sizeof(buf), "%d", WSAGetLastError());
58 explicit ScopedSocket(int sock) : sock_(sock) {}
67 TEST(BIOTest, SocketConnect) {
68 static const char kTestMessage[] = "test";
69 int listening_sock = -1;
72 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *) &ss;
73 struct sockaddr_in *sin = (struct sockaddr_in *) &ss;
74 OPENSSL_memset(&ss, 0, sizeof(ss));
76 ss.ss_family = AF_INET6;
77 listening_sock = socket(AF_INET6, SOCK_STREAM, 0);
78 ASSERT_NE(-1, listening_sock) << LastSocketError();
80 ASSERT_EQ(1, inet_pton(AF_INET6, "::1", &sin6->sin6_addr))
82 if (bind(listening_sock, (struct sockaddr *)sin6, sizeof(*sin6)) == -1) {
83 closesocket(listening_sock);
85 ss.ss_family = AF_INET;
86 listening_sock = socket(AF_INET, SOCK_STREAM, 0);
87 ASSERT_NE(-1, listening_sock) << LastSocketError();
89 ASSERT_EQ(1, inet_pton(AF_INET, "127.0.0.1", &sin->sin_addr))
91 ASSERT_EQ(0, bind(listening_sock, (struct sockaddr *)sin, sizeof(*sin)))
95 ScopedSocket listening_sock_closer(listening_sock);
96 ASSERT_EQ(0, listen(listening_sock, 1)) << LastSocketError();
97 ASSERT_EQ(0, getsockname(listening_sock, (struct sockaddr *)&ss, &len))
101 if (ss.ss_family == AF_INET6) {
102 BIO_snprintf(hostname, sizeof(hostname), "[::1]:%d",
103 ntohs(sin6->sin6_port));
104 } else if (ss.ss_family == AF_INET) {
105 BIO_snprintf(hostname, sizeof(hostname), "127.0.0.1:%d",
106 ntohs(sin->sin_port));
109 // Connect to it with a connect BIO.
110 bssl::UniquePtr<BIO> bio(BIO_new_connect(hostname));
113 // Write a test message to the BIO.
114 ASSERT_EQ(static_cast<int>(sizeof(kTestMessage)),
115 BIO_write(bio.get(), kTestMessage, sizeof(kTestMessage)));
117 // Accept the socket.
118 int sock = accept(listening_sock, (struct sockaddr *) &ss, &len);
119 ASSERT_NE(-1, sock) << LastSocketError();
120 ScopedSocket sock_closer(sock);
122 // Check the same message is read back out.
123 char buf[sizeof(kTestMessage)];
124 ASSERT_EQ(static_cast<int>(sizeof(kTestMessage)),
125 recv(sock, buf, sizeof(buf), 0))
126 << LastSocketError();
127 EXPECT_EQ(Bytes(kTestMessage, sizeof(kTestMessage)), Bytes(buf, sizeof(buf)));
130 TEST(BIOTest, Printf) {
131 // Test a short output, a very long one, and various sizes around
132 // 256 (the size of the buffer) to ensure edge cases are correct.
133 static const size_t kLengths[] = {5, 250, 251, 252, 253, 254, 1023};
135 bssl::UniquePtr<BIO> bio(BIO_new(BIO_s_mem()));
138 for (size_t length : kLengths) {
139 SCOPED_TRACE(length);
141 std::string in(length, 'a');
143 int ret = BIO_printf(bio.get(), "test %s", in.c_str());
145 EXPECT_EQ(5 + length, static_cast<size_t>(ret));
147 const uint8_t *contents;
149 ASSERT_TRUE(BIO_mem_contents(bio.get(), &contents, &len));
150 EXPECT_EQ("test " + in,
151 std::string(reinterpret_cast<const char *>(contents), len));
153 ASSERT_TRUE(BIO_reset(bio.get()));
157 static const size_t kLargeASN1PayloadLen = 8000;
159 struct ASN1TestParam {
161 std::vector<uint8_t> input;
162 // suffix_len is the number of zeros to append to |input|.
164 // expected_len, if |should_succeed| is true, is the expected length of the
168 } kASN1TestParams[] = {
169 {true, {0x30, 2, 1, 2, 0, 0}, 0, 4, 100},
170 {false /* truncated */, {0x30, 3, 1, 2}, 0, 0, 100},
171 {false /* should be short len */, {0x30, 0x81, 1, 1}, 0, 0, 100},
172 {false /* zero padded */, {0x30, 0x82, 0, 1, 1}, 0, 0, 100},
174 // Test a large payload.
176 {0x30, 0x82, kLargeASN1PayloadLen >> 8, kLargeASN1PayloadLen & 0xff},
177 kLargeASN1PayloadLen,
178 4 + kLargeASN1PayloadLen,
179 kLargeASN1PayloadLen * 2},
180 {false /* max_len too short */,
181 {0x30, 0x82, kLargeASN1PayloadLen >> 8, kLargeASN1PayloadLen & 0xff},
182 kLargeASN1PayloadLen,
183 4 + kLargeASN1PayloadLen,
184 3 + kLargeASN1PayloadLen},
186 // Test an indefinite-length input.
189 kLargeASN1PayloadLen + 2,
190 2 + kLargeASN1PayloadLen + 2,
191 kLargeASN1PayloadLen * 2},
192 {false /* max_len too short */,
194 kLargeASN1PayloadLen + 2,
195 2 + kLargeASN1PayloadLen + 2,
196 2 + kLargeASN1PayloadLen + 1},
199 class BIOASN1Test : public testing::TestWithParam<ASN1TestParam> {};
201 TEST_P(BIOASN1Test, ReadASN1) {
202 const ASN1TestParam& param = GetParam();
203 std::vector<uint8_t> input = param.input;
204 input.resize(input.size() + param.suffix_len, 0);
206 bssl::UniquePtr<BIO> bio(BIO_new_mem_buf(input.data(), input.size()));
211 int ok = BIO_read_asn1(bio.get(), &out, &out_len, param.max_len);
215 bssl::UniquePtr<uint8_t> out_storage(out);
217 ASSERT_EQ(param.should_succeed, (ok == 1));
218 if (param.should_succeed) {
219 EXPECT_EQ(Bytes(input.data(), param.expected_len), Bytes(out, out_len));
223 INSTANTIATE_TEST_CASE_P(, BIOASN1Test, testing::ValuesIn(kASN1TestParams));
225 // Run through the tests twice, swapping |bio1| and |bio2|, for symmetry.
226 class BIOPairTest : public testing::TestWithParam<bool> {};
228 TEST_P(BIOPairTest, TestPair) {
230 ASSERT_TRUE(BIO_new_bio_pair(&bio1, 10, &bio2, 10));
231 bssl::UniquePtr<BIO> free_bio1(bio1), free_bio2(bio2);
234 std::swap(bio1, bio2);
237 // Check initial states.
238 EXPECT_EQ(10u, BIO_ctrl_get_write_guarantee(bio1));
239 EXPECT_EQ(0u, BIO_ctrl_get_read_request(bio1));
241 // Data written in one end may be read out the other.
243 EXPECT_EQ(5, BIO_write(bio1, "12345", 5));
244 EXPECT_EQ(5u, BIO_ctrl_get_write_guarantee(bio1));
245 ASSERT_EQ(5, BIO_read(bio2, buf, sizeof(buf)));
246 EXPECT_EQ(Bytes("12345"), Bytes(buf, 5));
247 EXPECT_EQ(10u, BIO_ctrl_get_write_guarantee(bio1));
249 // Attempting to write more than 10 bytes will write partially.
250 EXPECT_EQ(10, BIO_write(bio1, "1234567890___", 13));
251 EXPECT_EQ(0u, BIO_ctrl_get_write_guarantee(bio1));
252 EXPECT_EQ(-1, BIO_write(bio1, "z", 1));
253 EXPECT_TRUE(BIO_should_write(bio1));
254 ASSERT_EQ(10, BIO_read(bio2, buf, sizeof(buf)));
255 EXPECT_EQ(Bytes("1234567890"), Bytes(buf, 10));
256 EXPECT_EQ(10u, BIO_ctrl_get_write_guarantee(bio1));
258 // Unsuccessful reads update the read request.
259 EXPECT_EQ(-1, BIO_read(bio2, buf, 5));
260 EXPECT_TRUE(BIO_should_read(bio2));
261 EXPECT_EQ(5u, BIO_ctrl_get_read_request(bio1));
263 // The read request is clamped to the size of the buffer.
264 EXPECT_EQ(-1, BIO_read(bio2, buf, 20));
265 EXPECT_TRUE(BIO_should_read(bio2));
266 EXPECT_EQ(10u, BIO_ctrl_get_read_request(bio1));
268 // Data may be written and read in chunks.
269 EXPECT_EQ(5, BIO_write(bio1, "12345", 5));
270 EXPECT_EQ(5u, BIO_ctrl_get_write_guarantee(bio1));
271 EXPECT_EQ(5, BIO_write(bio1, "67890___", 8));
272 EXPECT_EQ(0u, BIO_ctrl_get_write_guarantee(bio1));
273 ASSERT_EQ(3, BIO_read(bio2, buf, 3));
274 EXPECT_EQ(Bytes("123"), Bytes(buf, 3));
275 EXPECT_EQ(3u, BIO_ctrl_get_write_guarantee(bio1));
276 ASSERT_EQ(7, BIO_read(bio2, buf, sizeof(buf)));
277 EXPECT_EQ(Bytes("4567890"), Bytes(buf, 7));
278 EXPECT_EQ(10u, BIO_ctrl_get_write_guarantee(bio1));
280 // Successful reads reset the read request.
281 EXPECT_EQ(0u, BIO_ctrl_get_read_request(bio1));
283 // Test writes and reads starting in the middle of the ring buffer and
284 // wrapping to front.
285 EXPECT_EQ(8, BIO_write(bio1, "abcdefgh", 8));
286 EXPECT_EQ(2u, BIO_ctrl_get_write_guarantee(bio1));
287 ASSERT_EQ(3, BIO_read(bio2, buf, 3));
288 EXPECT_EQ(Bytes("abc"), Bytes(buf, 3));
289 EXPECT_EQ(5u, BIO_ctrl_get_write_guarantee(bio1));
290 EXPECT_EQ(5, BIO_write(bio1, "ijklm___", 8));
291 EXPECT_EQ(0u, BIO_ctrl_get_write_guarantee(bio1));
292 ASSERT_EQ(10, BIO_read(bio2, buf, sizeof(buf)));
293 EXPECT_EQ(Bytes("defghijklm"), Bytes(buf, 10));
294 EXPECT_EQ(10u, BIO_ctrl_get_write_guarantee(bio1));
296 // Data may flow from both ends in parallel.
297 EXPECT_EQ(5, BIO_write(bio1, "12345", 5));
298 EXPECT_EQ(5, BIO_write(bio2, "67890", 5));
299 ASSERT_EQ(5, BIO_read(bio2, buf, sizeof(buf)));
300 EXPECT_EQ(Bytes("12345"), Bytes(buf, 5));
301 ASSERT_EQ(5, BIO_read(bio1, buf, sizeof(buf)));
302 EXPECT_EQ(Bytes("67890"), Bytes(buf, 5));
304 // Closing the write end causes an EOF on the read half, after draining.
305 EXPECT_EQ(5, BIO_write(bio1, "12345", 5));
306 EXPECT_TRUE(BIO_shutdown_wr(bio1));
307 ASSERT_EQ(5, BIO_read(bio2, buf, sizeof(buf)));
308 EXPECT_EQ(Bytes("12345"), Bytes(buf, 5));
309 EXPECT_EQ(0, BIO_read(bio2, buf, sizeof(buf)));
311 // A closed write end may not be written to.
312 EXPECT_EQ(0u, BIO_ctrl_get_write_guarantee(bio1));
313 EXPECT_EQ(-1, BIO_write(bio1, "_____", 5));
315 uint32_t err = ERR_get_error();
316 EXPECT_EQ(ERR_LIB_BIO, ERR_GET_LIB(err));
317 EXPECT_EQ(BIO_R_BROKEN_PIPE, ERR_GET_REASON(err));
319 // The other end is still functional.
320 EXPECT_EQ(5, BIO_write(bio2, "12345", 5));
321 ASSERT_EQ(5, BIO_read(bio1, buf, sizeof(buf)));
322 EXPECT_EQ(Bytes("12345"), Bytes(buf, 5));
325 INSTANTIATE_TEST_CASE_P(, BIOPairTest, testing::Values(false, true));