Debian Codegraph @

debian/rust-vendor/aws-lc-sys/aws-lc/crypto/bio/bio_test.cc

raw
1 // Copyright (c) 2014, Google Inc.
2 // SPDX-License-Identifier: ISC
3
4 #include <algorithm>
5 #include <string>
6 #include <utility>
7
8 #include <gtest/gtest.h>
9
10 #include <openssl/bio.h>
11 #include <openssl/err.h>
12 #include <openssl/mem.h>
13
14 #include <array>
15
16 #include "../internal.h"
17 #include "../test/file_util.h"
18 #include "../test/test_util.h"
19
20 #include <fcntl.h>
21
22 #if !defined(OPENSSL_WINDOWS)
23 static const int kOpenReadOnlyBinary = O_RDONLY;
24 static const int kOpenReadOnlyText = O_RDONLY;
25 #else
26 static const int kOpenReadOnlyBinary = _O_RDONLY | _O_BINARY;
27 static const int kOpenReadOnlyText = O_RDONLY | _O_TEXT;
28 #endif
29
30 TEST(BIOTest, Printf) {
31 // Test a short output, a very long one, and various sizes around
32 // 256 (the size of the buffer) to ensure edge cases are correct.
33 static const size_t kLengths[] = {5, 250, 251, 252, 253, 254, 1023};
34
35 bssl::UniquePtr<BIO> bio(BIO_new(BIO_s_mem()));
36 ASSERT_TRUE(bio);
37 ASSERT_EQ(strcmp(BIO_method_name(bio.get()), "memory buffer"), 0);
38
39 for (size_t length : kLengths) {
40 SCOPED_TRACE(length);
41
42 std::string in(length, 'a');
43
44 int ret = BIO_printf(bio.get(), "test %s", in.c_str());
45 ASSERT_GE(ret, 0);
46 EXPECT_EQ(5 + length, static_cast<size_t>(ret));
47
48 const uint8_t *contents;
49 size_t len;
50 ASSERT_TRUE(BIO_mem_contents(bio.get(), &contents, &len));
51 EXPECT_EQ("test " + in,
52 std::string(reinterpret_cast<const char *>(contents), len));
53
54 ASSERT_TRUE(BIO_reset(bio.get()));
55 }
56 }
57
58 TEST(BIOTest, CloseFlags) {
59 #if defined(OPENSSL_ANDROID)
60 // On Android, when running from an APK, |tmpfile| does not work. See
61 // b/36991167#comment8.
62 GTEST_SKIP();
63 #endif
64
65 const char *test_str = "test\ntest\ntest\n";
66
67 // Assert that CRLF line endings get inserted on write and translated back out
68 // on read for text mode.
69 TempFILE text_bio_file = createTempFILE();
70 ASSERT_TRUE(text_bio_file);
71 bssl::UniquePtr<BIO> text_bio(
72 BIO_new_fp(text_bio_file.get(), BIO_NOCLOSE | BIO_FP_TEXT));
73 int bytes_written = BIO_write(text_bio.get(), test_str, strlen(test_str));
74 EXPECT_GE(bytes_written, 0);
75 ASSERT_TRUE(BIO_flush(text_bio.get()));
76 ASSERT_EQ(0, BIO_seek(text_bio.get(), 0)); // 0 indicates success here
77 char contents[256];
78 OPENSSL_memset(contents, 0, sizeof(contents));
79 int bytes_read = BIO_read(text_bio.get(), contents, sizeof(contents));
80 EXPECT_GE(bytes_read, bytes_written);
81 EXPECT_EQ(test_str, std::string(contents));
82
83 // Windows should have translated '\n' to '\r\n' on write, so validate that
84 // by opening the file in raw binary mode (i.e. no BIO_FP_TEXT).
85 bssl::UniquePtr<BIO> text_bio_raw(
86 BIO_new_fp(text_bio_file.get(), BIO_NOCLOSE));
87 ASSERT_EQ(0, BIO_seek(text_bio.get(), 0)); // 0 indicates success here
88 OPENSSL_memset(contents, 0, sizeof(contents));
89 bytes_read = BIO_read(text_bio_raw.get(), contents, sizeof(contents));
90 EXPECT_GT(bytes_read, 0);
91 #if defined(OPENSSL_WINDOWS)
92 EXPECT_EQ("test\r\ntest\r\ntest\r\n", std::string(contents));
93 #else
94 EXPECT_EQ(test_str, std::string(contents));
95 #endif
96
97 // Assert that CRLF line endings don't get inserted on write for
98 // (default) binary mode.
99 TempFILE binary_bio_file = createTempFILE();
100 ASSERT_TRUE(binary_bio_file);
101 bssl::UniquePtr<BIO> binary_bio(
102 BIO_new_fp(binary_bio_file.get(), BIO_NOCLOSE));
103 bytes_written = BIO_write(binary_bio.get(), test_str, strlen(test_str));
104 EXPECT_EQ((int)strlen(test_str), bytes_written);
105 ASSERT_TRUE(BIO_flush(binary_bio.get()));
106 ASSERT_EQ(0, BIO_seek(binary_bio.get(), 0)); // 0 indicates success here
107 OPENSSL_memset(contents, 0, sizeof(contents));
108 bytes_read = BIO_read(binary_bio.get(), contents, sizeof(contents));
109 EXPECT_GE(bytes_read, bytes_written);
110 EXPECT_EQ(test_str, std::string(contents));
111
112 // This test is meant to ensure that we're correctly handling a ftell/fseek
113 // bug on Windows documented and reproduced here:
114 // https://developercommunity.visualstudio.com/t/fseek-ftell-fail-in-text-mode-for-unix-style-text/425878
115 long pos;
116 char b1[256], b2[256];
117 binary_bio.reset(BIO_new_fp(binary_bio_file.get(), BIO_NOCLOSE));
118 ASSERT_EQ(0, BIO_seek(binary_bio.get(), 0)); // 0 indicates success here
119 BIO_gets(binary_bio.get(), b1, sizeof(b1));
120 pos = BIO_tell(binary_bio.get());
121 ASSERT_GT(BIO_gets(binary_bio.get(), b1, sizeof(b1)), 0);
122 BIO_seek(binary_bio.get(), pos);
123 BIO_gets(binary_bio.get(), b2, sizeof(b2));
124 EXPECT_EQ(std::string(b1), std::string(b2));
125
126 // Assert that BIO_CLOSE causes the underlying file to be closed on BIO free
127 // (ftell will return < 0)
128 FILE *tmp = createRawTempFILE();
129 ASSERT_TRUE(tmp);
130 BIO *bio = BIO_new_fp(tmp, BIO_CLOSE);
131 EXPECT_EQ(0, BIO_tell(bio));
132 // save off fd to avoid referencing |tmp| after free and angering valgrind
133 int tmp_fd = fileno(tmp);
134 EXPECT_LT(0, tmp_fd);
135 EXPECT_TRUE(BIO_free(bio));
136 // Windows CRT hits an assertion error and stack overflow (exception code
137 // 0xc0000409) when calling _tell or lseek on an already-closed file
138 // descriptor, so only consider the non-Windows case here.
139 #if !defined(OPENSSL_WINDOWS)
140 EXPECT_EQ(-1, lseek(tmp_fd, 0, SEEK_CUR));
141 EXPECT_EQ(EBADF, errno); // EBADF indicates that |BIO_free| closed the file
142 #endif
143
144 // Assert that BIO_NOCLOSE does not close the underlying file on BIO free
145 tmp = createRawTempFILE();
146 ASSERT_TRUE(tmp);
147 bio = BIO_new_fp(tmp, BIO_NOCLOSE);
148 EXPECT_EQ(0, BIO_tell(bio));
149 EXPECT_TRUE(BIO_free(bio));
150 EXPECT_TRUE(tmp);
151 EXPECT_EQ(0, ftell(tmp)); // 0 indicates file is still open
152 EXPECT_EQ(0, fclose(tmp)); // 0 indicates success for fclose
153 }
154
155 TEST(BIOTest, ReadASN1) {
156 static const size_t kLargeASN1PayloadLen = 8000;
157
158 struct ASN1Test {
159 bool should_succeed;
160 std::vector<uint8_t> input;
161 // suffix_len is the number of zeros to append to |input|.
162 size_t suffix_len;
163 // expected_len, if |should_succeed| is true, is the expected length of the
164 // ASN.1 element.
165 size_t expected_len;
166 size_t max_len;
167 } kASN1Tests[] = {
168 {true, {0x30, 2, 1, 2, 0, 0}, 0, 4, 100},
169 {false /* truncated */, {0x30, 3, 1, 2}, 0, 0, 100},
170 {false /* should be short len */, {0x30, 0x81, 1, 1}, 0, 0, 100},
171 {false /* zero padded */, {0x30, 0x82, 0, 1, 1}, 0, 0, 100},
172
173 // Test a large payload.
174 {true,
175 {0x30, 0x82, kLargeASN1PayloadLen >> 8, kLargeASN1PayloadLen & 0xff},
176 kLargeASN1PayloadLen,
177 4 + kLargeASN1PayloadLen,
178 kLargeASN1PayloadLen * 2},
179 {false /* max_len too short */,
180 {0x30, 0x82, kLargeASN1PayloadLen >> 8, kLargeASN1PayloadLen & 0xff},
181 kLargeASN1PayloadLen,
182 4 + kLargeASN1PayloadLen,
183 3 + kLargeASN1PayloadLen},
184
185 // Test an indefinite-length input.
186 {true,
187 {0x30, 0x80},
188 kLargeASN1PayloadLen + 2,
189 2 + kLargeASN1PayloadLen + 2,
190 kLargeASN1PayloadLen * 2},
191 {false /* max_len too short */,
192 {0x30, 0x80},
193 kLargeASN1PayloadLen + 2,
194 2 + kLargeASN1PayloadLen + 2,
195 2 + kLargeASN1PayloadLen + 1},
196 };
197
198 for (const auto &t : kASN1Tests) {
199 std::vector<uint8_t> input = t.input;
200 input.resize(input.size() + t.suffix_len, 0);
201
202 bssl::UniquePtr<BIO> bio(BIO_new_mem_buf(input.data(), input.size()));
203 ASSERT_TRUE(bio);
204
205 uint8_t *out;
206 size_t out_len;
207 int ok = BIO_read_asn1(bio.get(), &out, &out_len, t.max_len);
208 if (!ok) {
209 out = nullptr;
210 }
211 bssl::UniquePtr<uint8_t> out_storage(out);
212
213 ASSERT_EQ(t.should_succeed, (ok == 1));
214 if (t.should_succeed) {
215 EXPECT_EQ(Bytes(input.data(), t.expected_len), Bytes(out, out_len));
216 }
217 }
218 }
219
220 TEST(BIOTest, MemReadOnly) {
221 // A memory BIO created from |BIO_new_mem_buf| is a read-only buffer.
222 static const char kData[] = "abcdefghijklmno\npqrs";
223 bssl::UniquePtr<BIO> bio(BIO_new_mem_buf(kData, strlen(kData)));
224 ASSERT_TRUE(bio);
225
226 auto check_bio_contents = [&](Bytes b) {
227 char *contents = nullptr;
228 long len_l = BIO_get_mem_data(bio.get(), &contents);
229 ASSERT_GE(len_l, 0);
230 EXPECT_EQ(Bytes(contents, len_l), b);
231
232 const uint8_t *contents_c = nullptr;
233 size_t len = 0;
234 ASSERT_TRUE(BIO_mem_contents(bio.get(), &contents_c, &len));
235 EXPECT_EQ(Bytes(contents_c, len), b);
236
237 BUF_MEM *buf = nullptr;
238 ASSERT_EQ(BIO_get_mem_ptr(bio.get(), &buf), 1);
239 EXPECT_EQ(Bytes(buf->data, buf->length), b);
240 };
241
242 // Writing to read-only buffers should fail.
243 EXPECT_EQ(BIO_write(bio.get(), kData, strlen(kData)), -1);
244
245 check_bio_contents(Bytes(kData));
246 EXPECT_EQ(BIO_eof(bio.get()), 0);
247
248 // Read less than the whole buffer.
249 char buf[6];
250 int ret = BIO_read(bio.get(), buf, sizeof(buf));
251 ASSERT_GT(ret, 0);
252 EXPECT_EQ(Bytes(buf, ret), Bytes("abcdef"));
253 check_bio_contents(Bytes("ghijklmno\npqrs"));
254 EXPECT_EQ(BIO_eof(bio.get()), 0);
255
256 ret = BIO_read(bio.get(), buf, sizeof(buf));
257 ASSERT_GT(ret, 0);
258 EXPECT_EQ(Bytes(buf, ret), Bytes("ghijkl"));
259 check_bio_contents(Bytes("mno\npqrs"));
260 EXPECT_EQ(BIO_eof(bio.get()), 0);
261
262 // Read stops at newline
263 ret = BIO_gets(bio.get(), buf, sizeof(buf));
264 ASSERT_GT(ret, 0);
265 EXPECT_EQ(Bytes(buf, ret), Bytes("mno\n"));
266 check_bio_contents(Bytes("pqrs"));
267 EXPECT_EQ(BIO_eof(bio.get()), 0);
268
269 // Read the remainder of the buffer.
270 ret = BIO_read(bio.get(), buf, sizeof(buf));
271 ASSERT_GT(ret, 0);
272 EXPECT_EQ(Bytes(buf, ret), Bytes("pqrs"));
273 EXPECT_EQ(BIO_eof(bio.get()), 1);
274 check_bio_contents(Bytes(""));
275 EXPECT_EQ(BIO_eof(bio.get()), 1);
276
277 // By default, reading from a consumed read-only buffer returns EOF.
278 EXPECT_EQ(BIO_read(bio.get(), buf, sizeof(buf)), 0);
279 EXPECT_FALSE(BIO_should_read(bio.get()));
280
281 // A memory BIO can be configured to return an error instead of EOF. This is
282 // error is returned as retryable. (This is not especially useful here. It
283 // makes more sense for a writable BIO.)
284 EXPECT_EQ(BIO_set_mem_eof_return(bio.get(), -1), 1);
285 EXPECT_EQ(BIO_read(bio.get(), buf, sizeof(buf)), -1);
286 EXPECT_TRUE(BIO_should_read(bio.get()));
287
288 // Rewind buffer when buffer pointer aligns with read pointer
289 ret = BIO_seek(bio.get(), 3);
290 ASSERT_EQ(ret, 3);
291 check_bio_contents(Bytes("defghijklmno\npqrs"));
292 EXPECT_EQ(BIO_eof(bio.get()), 0);
293
294 // Rewind buffer when buffer pointer does not align with read pointer
295 EXPECT_GT(BIO_read(bio.get(), buf, sizeof(buf)), 0);
296 ret = BIO_seek(bio.get(), 4);
297 ASSERT_EQ(ret, 4);
298 check_bio_contents(Bytes("efghijklmno\npqrs"));
299 EXPECT_EQ(BIO_eof(bio.get()), 0);
300
301 // Reset buffer when buffer pointer aligns with read pointer
302 ret = BIO_reset(bio.get());
303 ASSERT_EQ(ret, 1);
304 check_bio_contents(Bytes(kData));
305 EXPECT_EQ(BIO_eof(bio.get()), 0);
306
307 // Reset buffer when buffer pointer does not align with read pointer
308 EXPECT_GT(BIO_read(bio.get(), buf, sizeof(buf)), 0);
309 ret = BIO_reset(bio.get());
310 ASSERT_EQ(ret, 1);
311 check_bio_contents(Bytes(kData));
312 EXPECT_EQ(BIO_eof(bio.get()), 0);
313
314 // Read exactly the right number of bytes, to test the boundary condition is
315 // correct.
316 bio.reset(BIO_new_mem_buf("abc", 3));
317 ASSERT_TRUE(bio);
318 ret = BIO_read(bio.get(), buf, 3);
319 ASSERT_GT(ret, 0);
320 EXPECT_EQ(Bytes(buf, ret), Bytes("abc"));
321 EXPECT_EQ(BIO_eof(bio.get()), 1);
322 }
323
324 TEST(BIOTest, MemWritable) {
325 // A memory BIO created from |BIO_new| is writable.
326 bssl::UniquePtr<BIO> bio(BIO_new(BIO_s_mem()));
327 ASSERT_TRUE(bio);
328
329 auto check_bio_contents = [&](Bytes b) {
330 char *contents = nullptr;
331 long len_l = BIO_get_mem_data(bio.get(), &contents);
332 ASSERT_GE(len_l, 0);
333 EXPECT_EQ(Bytes(contents, len_l), b);
334
335 const uint8_t *contents_c = nullptr;
336 size_t len = 0;
337 ASSERT_TRUE(BIO_mem_contents(bio.get(), &contents_c, &len));
338 EXPECT_EQ(Bytes(contents_c, len), b);
339
340 BUF_MEM *buf = nullptr;
341 ASSERT_EQ(BIO_get_mem_ptr(bio.get(), &buf), 1);
342 EXPECT_EQ(Bytes(buf->data, buf->length), b);
343 };
344
345 // It is initially empty.
346 check_bio_contents(Bytes(""));
347 EXPECT_EQ(BIO_eof(bio.get()), 1);
348
349 // Reading from it should default to returning a retryable error.
350 char buf[32];
351 EXPECT_EQ(BIO_read(bio.get(), buf, sizeof(buf)), -1);
352 EXPECT_TRUE(BIO_should_read(bio.get()));
353
354 // This can be configured to return an EOF.
355 EXPECT_EQ(BIO_set_mem_eof_return(bio.get(), 0), 1);
356 EXPECT_EQ(BIO_read(bio.get(), buf, sizeof(buf)), 0);
357 EXPECT_FALSE(BIO_should_read(bio.get()));
358
359 // Restore the default. A writable memory |BIO| is typically used in this mode
360 // so additional data can be written when exhausted.
361 EXPECT_EQ(BIO_set_mem_eof_return(bio.get(), -1), 1);
362
363 // Writes append to the buffer.
364 ASSERT_EQ(BIO_write(bio.get(), "abcdef", 6), 6);
365 check_bio_contents(Bytes("abcdef"));
366 EXPECT_EQ(BIO_eof(bio.get()), 0);
367
368 // Reset clears the buffer
369 int ret = BIO_reset(bio.get());
370 ASSERT_EQ(ret, 1);
371 EXPECT_EQ(BIO_eof(bio.get()), 1);
372 EXPECT_EQ(BIO_read(bio.get(), buf, sizeof(buf)), -1);
373 EXPECT_TRUE(BIO_should_read(bio.get()));
374
375 // Writes can include embedded NULs.
376 ASSERT_EQ(BIO_write(bio.get(), "abcdef\0ghijk", 12), 12);
377 check_bio_contents(Bytes("abcdef\0ghijk", 12));
378 EXPECT_EQ(BIO_eof(bio.get()), 0);
379
380 // Do a partial read.
381 ret = BIO_read(bio.get(), buf, 4);
382 ASSERT_GT(ret, 0);
383 EXPECT_EQ(Bytes(buf, ret), Bytes("abcd"));
384 check_bio_contents(Bytes("ef\0ghijk", 8));
385 EXPECT_EQ(BIO_eof(bio.get()), 0);
386
387 // Reads and writes may alternate.
388 ASSERT_EQ(BIO_write(bio.get(), "\nlmnopq", 7), 7);
389 check_bio_contents(Bytes("ef\0ghijk\nlmnopq", 15));
390 EXPECT_EQ(BIO_eof(bio.get()), 0);
391
392 // Reads may consume embedded NULs.
393 ret = BIO_read(bio.get(), buf, 4);
394 ASSERT_GT(ret, 0);
395 EXPECT_EQ(Bytes(buf, ret), Bytes("ef\0g", 4));
396 check_bio_contents(Bytes("hijk\nlmnopq"));
397 EXPECT_EQ(BIO_eof(bio.get()), 0);
398
399 // Read then rewind.
400 ret = BIO_read(bio.get(), buf, 4);
401 ASSERT_GT(ret, 0);
402 EXPECT_EQ(Bytes(buf, ret), Bytes("hijk", 4));
403 ret = BIO_seek(bio.get(), 0);
404 ASSERT_EQ(ret, 0);
405 check_bio_contents(Bytes("hijk\nlmnopq"));
406 EXPECT_EQ(BIO_eof(bio.get()), 0);
407
408 // Gets stop at newline
409 ret = BIO_gets(bio.get(), buf, sizeof(buf));
410 ASSERT_GT(ret, 0);
411 EXPECT_EQ(Bytes(buf, ret), Bytes("hijk\n", 5));
412 check_bio_contents(Bytes("lmnopq"));
413 EXPECT_EQ(BIO_eof(bio.get()), 0);
414
415 // The read buffer exceeds the |BIO|, so we consume everything.
416 ret = BIO_read(bio.get(), buf, sizeof(buf));
417 ASSERT_GT(ret, 0);
418 EXPECT_EQ(Bytes(buf, ret), Bytes("lmnopq"));
419 check_bio_contents(Bytes(""));
420 EXPECT_EQ(BIO_eof(bio.get()), 1);
421
422 // The |BIO| is now empty.
423 EXPECT_EQ(BIO_read(bio.get(), buf, sizeof(buf)), -1);
424 EXPECT_TRUE(BIO_should_read(bio.get()));
425
426 // Repeat the above, reading exactly the right number of bytes, to test the
427 // boundary condition is correct.
428 ASSERT_EQ(BIO_write(bio.get(), "abc", 3), 3);
429 ret = BIO_read(bio.get(), buf, 3);
430 EXPECT_EQ(Bytes(buf, ret), Bytes("abc"));
431 EXPECT_EQ(BIO_read(bio.get(), buf, sizeof(buf)), -1);
432 EXPECT_TRUE(BIO_should_read(bio.get()));
433 EXPECT_EQ(BIO_eof(bio.get()), 1);
434 }
435
436 TEST(BIOTest, BioGetMemLeak) {
437 bssl::UniquePtr<BIO> bio(BIO_new(BIO_s_mem()));
438 bssl::UniquePtr<BUF_MEM> bufmem;
439 BUF_MEM *buf_mem = nullptr;
440 ASSERT_TRUE(bio);
441 ASSERT_EQ(BIO_puts(bio.get(), "Hello World\n"), 12);
442 ASSERT_TRUE(BIO_get_mem_ptr(bio.get(), &buf_mem));
443 // Take ownership of the pointer so it will free on function exit
444 bufmem.reset(buf_mem);
445 ASSERT_GT(BIO_set_close(bio.get(), BIO_NOCLOSE), 0);
446 bio.reset();
447 ASSERT_EQ(Bytes(buf_mem->data, buf_mem->length), Bytes("Hello World\n"));
448 }
449
450 // SetMemBufNoClose, SetMemBufClose, and SetMemBufReplacesState exercise
451 // a number of code-paths to ensure |BIO_set_mem_buf| functions correctly.
452 TEST(BIOTest, SetMemBufNoClose) {
453 bssl::UniquePtr<BIO> bio(BIO_new(BIO_s_mem()));
454 ASSERT_TRUE(bio);
455
456 BUF_MEM *m = BUF_MEM_new();
457 ASSERT_TRUE(m);
458 ASSERT_TRUE(BUF_MEM_grow(m, 5));
459 OPENSSL_memcpy(m->data, "hello", 5);
460
461 ASSERT_TRUE(BIO_set_mem_buf(bio.get(), m, BIO_NOCLOSE));
462
463 char out[8] = {0};
464 EXPECT_EQ(5, BIO_read(bio.get(), out, sizeof(out)));
465 EXPECT_EQ(Bytes(out, 5), Bytes("hello", 5));
466
467 // Exercise to surface memory violations.
468 bio.reset();
469 BUF_MEM_free(m);
470 }
471
472 TEST(BIOTest, SetMemBufClose) {
473 bssl::UniquePtr<BIO> bio(BIO_new(BIO_s_mem()));
474 ASSERT_TRUE(bio);
475
476 BUF_MEM *m = BUF_MEM_new();
477 ASSERT_TRUE(m);
478 ASSERT_TRUE(BUF_MEM_grow(m, 5));
479 OPENSSL_memcpy(m->data, "world", 5);
480
481 ASSERT_TRUE(BIO_set_mem_buf(bio.get(), m, BIO_CLOSE));
482
483 char out[8] = {0};
484 EXPECT_EQ(5, BIO_read(bio.get(), out, sizeof(out)));
485 EXPECT_EQ(Bytes(out, 5), Bytes("world", 5));
486
487 // Exercise to surface memory violations.
488 bio.reset();
489 }
490
491 TEST(BIOTest, SetMemBufReplacesState) {
492 // After BIO_set_mem_buf, reads must see the new buffer, not the
493 // zero-initialized BUF_MEM allocated by mem_new().
494 bssl::UniquePtr<BIO> bio(BIO_new(BIO_s_mem()));
495 ASSERT_EQ(BIO_puts(bio.get(), "initial"), 7);
496
497 bssl::UniquePtr<BUF_MEM> m(BUF_MEM_new());
498 ASSERT_TRUE(BUF_MEM_grow(m.get(), 3));
499 OPENSSL_memcpy(m->data, "abc", 3);
500 ASSERT_TRUE(BIO_set_mem_buf(bio.get(), m.get(), BIO_NOCLOSE));
501
502 char out[8] = {0};
503 EXPECT_EQ(3, BIO_read(bio.get(), out, sizeof(out)));
504 EXPECT_EQ(Bytes(out, 3), Bytes("abc", 3));
505 }
506
507 TEST(BIOTest, Gets) {
508 const struct {
509 std::string bio;
510 int gets_len;
511 std::string gets_result;
512 } kGetsTests[] = {
513 // BIO_gets should stop at the first newline. If the buffer is too small,
514 // stop there instead. Note the buffer size
515 // includes a trailing NUL.
516 {"123456789\n123456789", 5, "1234"},
517 {"123456789\n123456789", 9, "12345678"},
518 {"123456789\n123456789", 10, "123456789"},
519 {"123456789\n123456789", 11, "123456789\n"},
520 {"123456789\n123456789", 12, "123456789\n"},
521 {"123456789\n123456789", 256, "123456789\n"},
522
523 // If we run out of buffer, read the whole buffer.
524 {"12345", 5, "1234"},
525 {"12345", 6, "12345"},
526 {"12345", 10, "12345"},
527
528 // NUL bytes do not terminate gets.
529 {std::string("abc\0def\nghi", 11), 256, std::string("abc\0def\n", 8)},
530
531 // An output size of one means we cannot read any bytes. Only the trailing
532 // NUL is included.
533 {"12345", 1, ""},
534
535 // Empty line.
536 {"\nabcdef", 256, "\n"},
537 // Empty BIO.
538 {"", 256, ""},
539 };
540 for (const auto &t : kGetsTests) {
541 SCOPED_TRACE(t.bio);
542 SCOPED_TRACE(t.gets_len);
543
544 auto check_bio_gets = [&](BIO *bio) {
545 std::vector<char> buf(t.gets_len, 'a');
546 int ret = BIO_gets(bio, buf.data(), t.gets_len);
547 ASSERT_GE(ret, 0);
548 // |BIO_gets| should write a NUL terminator, not counted in the return
549 // value.
550 EXPECT_EQ(Bytes(buf.data(), ret + 1),
551 Bytes(t.gets_result.data(), t.gets_result.size() + 1));
552
553 // The remaining data should still be in the BIO.
554 buf.resize(t.bio.size() + 1);
555 ret = BIO_read(bio, buf.data(), static_cast<int>(buf.size()));
556 ASSERT_GE(ret, 0);
557 EXPECT_EQ(Bytes(buf.data(), ret),
558 Bytes(t.bio.substr(t.gets_result.size())));
559 };
560
561 {
562 SCOPED_TRACE("memory");
563 bssl::UniquePtr<BIO> bio(BIO_new_mem_buf(t.bio.data(), t.bio.size()));
564 ASSERT_TRUE(bio);
565 check_bio_gets(bio.get());
566 }
567
568 if (!SkipTempFileTests()) {
569 TemporaryFile file;
570 ASSERT_TRUE(file.Init(t.bio));
571
572 // TODO(crbug.com/boringssl/585): If the line has an embedded NUL, file
573 // BIOs do not currently report the answer correctly.
574 if (t.bio.find('\0') == std::string::npos) {
575 SCOPED_TRACE("file");
576
577 // Test |BIO_new_file|.
578 bssl::UniquePtr<BIO> bio(BIO_new_file(file.path().c_str(), "rb"));
579 ASSERT_TRUE(bio);
580 check_bio_gets(bio.get());
581
582 // Test |BIO_read_filename|.
583 bio.reset(BIO_new(BIO_s_file()));
584 ASSERT_TRUE(bio);
585 ASSERT_TRUE(BIO_read_filename(bio.get(), file.path().c_str()));
586 check_bio_gets(bio.get());
587
588 // Test |BIO_NOCLOSE|.
589 ScopedFILE file_obj = file.Open("rb");
590 ASSERT_TRUE(file_obj);
591 bio.reset(BIO_new_fp(file_obj.get(), BIO_NOCLOSE));
592 ASSERT_TRUE(bio);
593 check_bio_gets(bio.get());
594
595 // Test |BIO_CLOSE|.
596 file_obj = file.Open("rb");
597 ASSERT_TRUE(file_obj);
598 bio.reset(BIO_new_fp(file_obj.get(), BIO_CLOSE));
599 ASSERT_TRUE(bio);
600 file_obj.release(); // |BIO_new_fp| took ownership on success.
601 check_bio_gets(bio.get());
602 }
603
604 {
605 SCOPED_TRACE("fd");
606 #if defined(OPENSSL_WINDOWS)
607 int open_flags = _O_RDONLY | _O_BINARY;
608 #else
609 int open_flags = O_RDONLY;
610 #endif
611
612 // Test |BIO_NOCLOSE|.
613 ScopedFD fd = file.OpenFD(open_flags);
614 ASSERT_TRUE(fd.is_valid());
615 bssl::UniquePtr<BIO> bio(BIO_new_fd(fd.get(), BIO_NOCLOSE));
616 ASSERT_TRUE(bio);
617 check_bio_gets(bio.get());
618
619 // Test |BIO_CLOSE|.
620 fd = file.OpenFD(open_flags);
621 ASSERT_TRUE(fd.is_valid());
622 bio.reset(BIO_new_fd(fd.get(), BIO_CLOSE));
623 ASSERT_TRUE(bio);
624 fd.release(); // |BIO_new_fd| took ownership on success.
625 check_bio_gets(bio.get());
626 }
627 }
628 }
629
630 // Negative and zero lengths should not output anything, even a trailing NUL.
631 bssl::UniquePtr<BIO> bio(BIO_new_mem_buf("12345", -1));
632 ASSERT_TRUE(bio);
633 char c = 'a';
634 EXPECT_EQ(0, BIO_gets(bio.get(), &c, -1));
635 EXPECT_EQ(0, BIO_gets(bio.get(), &c, 0));
636 EXPECT_EQ(c, 'a');
637 }
638
639 TEST(BIOTest, ExternalData) {
640 // Create a |BIO| object
641 bssl::UniquePtr<BIO> bio(BIO_new(BIO_s_mem()));
642 int bio_index =
643 BIO_get_ex_new_index(0, nullptr, nullptr, nullptr, CustomDataFree);
644 ASSERT_GT(bio_index, 0);
645
646 // Associate custom data with the |BIO| using |BIO_set_ex_data| and set an
647 // arbitrary number.
648 auto *custom_data = static_cast<CustomData *>(malloc(sizeof(CustomData)));
649 ASSERT_TRUE(custom_data);
650 custom_data->custom_data = 123;
651 ASSERT_TRUE(BIO_set_ex_data(bio.get(), bio_index, custom_data));
652
653 // Retrieve the custom data using |BIO_get_ex_data|.
654 auto *retrieved_data =
655 static_cast<CustomData *>(BIO_get_ex_data(bio.get(), bio_index));
656 ASSERT_TRUE(retrieved_data);
657 EXPECT_EQ(retrieved_data->custom_data, 123);
658 }
659
660 // Test that, on Windows, |BIO_read_filename| opens files in binary mode.
661 TEST(BIOTest, FileMode) {
662 if (SkipTempFileTests()) {
663 GTEST_SKIP();
664 }
665
666 TemporaryFile temp;
667 ASSERT_TRUE(temp.Init("hello\r\nworld"));
668
669 auto expect_file_contents = [](BIO *bio, const std::string &str) {
670 // Read more than expected, to make sure we've reached the end of the file.
671 std::vector<char> buf(str.size() + 100);
672 int len = BIO_read(bio, buf.data(), static_cast<int>(buf.size()));
673 ASSERT_GT(len, 0);
674 EXPECT_EQ(Bytes(buf.data(), len), Bytes(str));
675 };
676 auto expect_binary_mode = [&](BIO *bio) {
677 expect_file_contents(bio, "hello\r\nworld");
678 };
679 auto expect_text_mode = [&](BIO *bio) {
680 #if defined(OPENSSL_WINDOWS)
681 expect_file_contents(bio, "hello\nworld");
682 #else
683 expect_file_contents(bio, "hello\r\nworld");
684 #endif
685 };
686
687 // |BIO_read_filename| should open in binary mode.
688 bssl::UniquePtr<BIO> bio(BIO_new(BIO_s_file()));
689 ASSERT_TRUE(bio);
690 ASSERT_TRUE(BIO_read_filename(bio.get(), temp.path().c_str()));
691 expect_binary_mode(bio.get());
692 // |BIO_new_file| does not set |BIO_FP_TEXT|, so expect it to set the file's
693 // mode to binary in all cases. This odd behavior, but it's what OpenSSL does.
694 bio.reset(BIO_new_file(temp.path().c_str(), "rb"));
695 ASSERT_TRUE(bio);
696 expect_binary_mode(bio.get());
697 bio.reset(BIO_new_file(temp.path().c_str(), "r"));
698 ASSERT_TRUE(bio);
699 // NOTE: Our behavior here aligns with OpenSSL which is to |_setmode| the file
700 // to binary. BoringSSL would |expect_text_mode| below because it respects
701 // default mode on Windows which is text and doesn't call |_setmode| (unless
702 // |BIO_FP_TEXT| is set, which is not the case here).
703 expect_binary_mode(bio.get());
704 // |BIO_new_fp| will always set |_O_BINARY| if |BIO_FP_TEXT| is not set in the
705 // call to |BIO_new_fp|.
706 ScopedFILE file = temp.Open("rb");
707 ASSERT_TRUE(file);
708 bio.reset(BIO_new_fp(file.get(), BIO_NOCLOSE));
709 ASSERT_TRUE(bio);
710 expect_binary_mode(bio.get());
711 file = temp.Open("r");
712 ASSERT_TRUE(file);
713 bio.reset(BIO_new_fp(file.get(), BIO_NOCLOSE));
714 ASSERT_TRUE(bio);
715 // NOTE: Our behavior here aligns with OpenSSL. BoringSSL would
716 // |expect_text_mode| below because they don't call |_setmode| unless
717 // |BIO_FP_TEXT| is set.
718 expect_binary_mode(bio.get());
719 // However, |BIO_FP_TEXT| changes the file to be text mode, no matter how it
720 // was opened.
721 file = temp.Open("rb");
722 ASSERT_TRUE(file);
723 bio.reset(BIO_new_fp(file.get(), BIO_NOCLOSE | BIO_FP_TEXT));
724 ASSERT_TRUE(bio);
725 expect_text_mode(bio.get());
726 file = temp.Open("r");
727 ASSERT_TRUE(file);
728 bio.reset(BIO_new_fp(file.get(), BIO_NOCLOSE | BIO_FP_TEXT));
729 ASSERT_TRUE(bio);
730 expect_text_mode(bio.get());
731 // |BIO_new_fd| inherits the FD's existing mode.
732 ScopedFD fd = temp.OpenFD(kOpenReadOnlyBinary);
733 ASSERT_TRUE(fd.is_valid());
734 bio.reset(BIO_new_fd(fd.get(), BIO_NOCLOSE));
735 ASSERT_TRUE(bio);
736 expect_binary_mode(bio.get());
737 fd = temp.OpenFD(kOpenReadOnlyText);
738 ASSERT_TRUE(fd.is_valid());
739 bio.reset(BIO_new_fd(fd.get(), BIO_NOCLOSE));
740 ASSERT_TRUE(bio);
741 expect_text_mode(bio.get());
742 }
743
744 // BIOPairTest: A parameterized test fixture for testing BIO pair operations
745 // This test class runs each test case with four different combinations:
746 // 1. Normal order (bio1->bio2) with legacy callback
747 // 2. Normal order (bio1->bio2) with extended callback
748 // 3. Swapped order (bio2->bio1) with legacy callback
749 // 4. Swapped order (bio2->bio1) with extended callback
750 //
751 // Parameters:
752 // std::tuple<bool, bool>:
753 // - First bool (get<0>): Controls BIO swapping
754 // * false = normal order (bio1->bio2)
755 // * true = swapped order (bio2->bio1)
756 // - Second bool (get<1>): Controls callback type
757 // * false = legacy callback (BIO_set_callback)
758 // * true = extended callback (BIO_set_callback_ex)
759 class BIOPairTest : public testing::TestWithParam<std::tuple<bool, bool>> {};
760
761 TEST_P(BIOPairTest, TestPair) {
762 BIO *bio1_raw, *bio2_raw;
763 ASSERT_TRUE(BIO_new_bio_pair(&bio1_raw, 10, &bio2_raw, 10));
764 bssl::UniquePtr<BIO> bio1(bio1_raw), bio2(bio2_raw);
765
766 const bool should_swap = std::get<0>(GetParam());
767 if (should_swap) {
768 std::swap(bio1, bio2);
769 }
770
771 // Check initial states.
772 EXPECT_EQ(10u, BIO_ctrl_get_write_guarantee(bio1.get()));
773 EXPECT_EQ(0u, BIO_ctrl_get_read_request(bio1.get()));
774 EXPECT_FALSE(BIO_eof(bio1.get()));
775 EXPECT_EQ(0u, BIO_pending(bio1.get()));
776 EXPECT_EQ(0u, BIO_wpending(bio1.get()));
777
778 // Data written in one end may be read out the other.
779 uint8_t buf[20];
780 EXPECT_EQ(5, BIO_write(bio1.get(), "12345", 5));
781 EXPECT_EQ(5u, BIO_ctrl_get_write_guarantee(bio1.get()));
782 EXPECT_EQ(5u, BIO_pending(bio2.get()));
783 EXPECT_EQ(5u, BIO_wpending(bio1.get()));
784 ASSERT_EQ(5, BIO_read(bio2.get(), buf, sizeof(buf)));
785 EXPECT_EQ(Bytes("12345"), Bytes(buf, 5));
786 EXPECT_EQ(10u, BIO_ctrl_get_write_guarantee(bio1.get()));
787 EXPECT_EQ(0u, BIO_pending(bio2.get()));
788 EXPECT_EQ(0u, BIO_wpending(bio1.get()));
789
790 // Attempting to write more than 10 bytes will write partially.
791 EXPECT_EQ(10, BIO_write(bio1.get(), "1234567890___", 13));
792 EXPECT_EQ(0u, BIO_ctrl_get_write_guarantee(bio1.get()));
793 EXPECT_EQ(-1, BIO_write(bio1.get(), "z", 1));
794 EXPECT_TRUE(BIO_should_write(bio1.get()));
795 ASSERT_EQ(10, BIO_read(bio2.get(), buf, sizeof(buf)));
796 EXPECT_EQ(Bytes("1234567890"), Bytes(buf, 10));
797 EXPECT_EQ(10u, BIO_ctrl_get_write_guarantee(bio1.get()));
798
799 // Unsuccessful reads update the read request.
800 EXPECT_EQ(-1, BIO_read(bio2.get(), buf, 5));
801 EXPECT_TRUE(BIO_should_read(bio2.get()));
802 EXPECT_EQ(5u, BIO_ctrl_get_read_request(bio1.get()));
803
804 // The read request is clamped to the size of the buffer.
805 EXPECT_EQ(-1, BIO_read(bio2.get(), buf, 20));
806 EXPECT_TRUE(BIO_should_read(bio2.get()));
807 EXPECT_EQ(10u, BIO_ctrl_get_read_request(bio1.get()));
808
809 // Data may be written and read in chunks.
810 EXPECT_EQ(5, BIO_write(bio1.get(), "12345", 5));
811 EXPECT_EQ(5u, BIO_ctrl_get_write_guarantee(bio1.get()));
812 EXPECT_EQ(5, BIO_write(bio1.get(), "67890___", 8));
813 EXPECT_EQ(0u, BIO_ctrl_get_write_guarantee(bio1.get()));
814 ASSERT_EQ(3, BIO_read(bio2.get(), buf, 3));
815 EXPECT_EQ(Bytes("123"), Bytes(buf, 3));
816 EXPECT_EQ(3u, BIO_ctrl_get_write_guarantee(bio1.get()));
817 ASSERT_EQ(7, BIO_read(bio2.get(), buf, sizeof(buf)));
818 EXPECT_EQ(Bytes("4567890"), Bytes(buf, 7));
819 EXPECT_EQ(10u, BIO_ctrl_get_write_guarantee(bio1.get()));
820
821 // Successful reads reset the read request.
822 EXPECT_EQ(0u, BIO_ctrl_get_read_request(bio1.get()));
823
824 // Test writes and reads starting in the middle of the ring buffer and
825 // wrapping to front.
826 EXPECT_EQ(8, BIO_write(bio1.get(), "abcdefgh", 8));
827 EXPECT_EQ(2u, BIO_ctrl_get_write_guarantee(bio1.get()));
828 ASSERT_EQ(3, BIO_read(bio2.get(), buf, 3));
829 EXPECT_EQ(Bytes("abc"), Bytes(buf, 3));
830 EXPECT_EQ(5u, BIO_ctrl_get_write_guarantee(bio1.get()));
831 EXPECT_EQ(5, BIO_write(bio1.get(), "ijklm___", 8));
832 EXPECT_EQ(0u, BIO_ctrl_get_write_guarantee(bio1.get()));
833 ASSERT_EQ(10, BIO_read(bio2.get(), buf, sizeof(buf)));
834 EXPECT_EQ(Bytes("defghijklm"), Bytes(buf, 10));
835 EXPECT_EQ(10u, BIO_ctrl_get_write_guarantee(bio1.get()));
836
837 // Data may flow from both ends in parallel.
838 EXPECT_EQ(5, BIO_write(bio1.get(), "12345", 5));
839 EXPECT_EQ(5, BIO_write(bio2.get(), "67890", 5));
840 ASSERT_EQ(5, BIO_read(bio2.get(), buf, sizeof(buf)));
841 EXPECT_EQ(Bytes("12345"), Bytes(buf, 5));
842 ASSERT_EQ(5, BIO_read(bio1.get(), buf, sizeof(buf)));
843 EXPECT_EQ(Bytes("67890"), Bytes(buf, 5));
844
845 // Closing the write end causes an EOF on the read half, after draining.
846 EXPECT_EQ(5, BIO_write(bio1.get(), "12345", 5));
847 EXPECT_TRUE(BIO_shutdown_wr(bio1.get()));
848 EXPECT_FALSE(BIO_eof(bio2.get()));
849 EXPECT_EQ(5u, BIO_pending(bio2.get()));
850 EXPECT_EQ(5u, BIO_wpending(bio1.get()));
851 ASSERT_EQ(5, BIO_read(bio2.get(), buf, sizeof(buf)));
852 EXPECT_EQ(Bytes("12345"), Bytes(buf, 5));
853 EXPECT_TRUE(BIO_eof(bio2.get()));
854 EXPECT_EQ(0u, BIO_pending(bio2.get()));
855 EXPECT_EQ(0u, BIO_wpending(bio1.get()));
856 EXPECT_EQ(0, BIO_read(bio2.get(), buf, sizeof(buf)));
857 EXPECT_TRUE(BIO_eof(bio2.get()));
858 EXPECT_EQ(0u, BIO_pending(bio2.get()));
859 EXPECT_EQ(0u, BIO_wpending(bio1.get()));
860
861 // A closed write end may not be written to.
862 EXPECT_EQ(0u, BIO_ctrl_get_write_guarantee(bio1.get()));
863 EXPECT_EQ(-1, BIO_write(bio1.get(), "_____", 5));
864 EXPECT_TRUE(ErrorEquals(ERR_get_error(), ERR_LIB_BIO, BIO_R_BROKEN_PIPE));
865
866 // The other end is still functional.
867 EXPECT_EQ(5, BIO_write(bio2.get(), "12345", 5));
868 ASSERT_EQ(5, BIO_read(bio1.get(), buf, sizeof(buf)));
869 EXPECT_EQ(Bytes("12345"), Bytes(buf, 5));
870 EXPECT_FALSE(BIO_eof(bio1.get()));
871
872 // Destroying |bio2| implicitly closes it, and discards unread data.
873 EXPECT_EQ(5, BIO_write(bio2.get(), "12345", 5));
874 bio2 = nullptr;
875
876 // |bio1| no longer has the "init" flag set, so reads and writes will fail at
877 // the BIO framework.
878 EXPECT_FALSE(BIO_get_init(bio1.get()));
879 EXPECT_EQ(-2, BIO_write(bio1.get(), "12345", 5));
880 EXPECT_TRUE(ErrorEquals(ERR_get_error(), ERR_LIB_BIO, BIO_R_UNINITIALIZED));
881 EXPECT_EQ(-2, BIO_read(bio1.get(), buf, sizeof(buf)));
882 EXPECT_TRUE(ErrorEquals(ERR_get_error(), ERR_LIB_BIO, BIO_R_UNINITIALIZED));
883
884 // Although in this disconnected state, |BIO_ctrl| works. |bio1| should
885 // report EOF when it has no peer.
886 EXPECT_TRUE(BIO_eof(bio1.get()));
887
888 // BIO_ctrl_get_write_guarantee should return 0 because there is no one to
889 // write to.
890 EXPECT_EQ(0u, BIO_ctrl_get_write_guarantee(bio1.get()));
891 }
892
893 #define CALL_BACK_FAILURE -1234567
894 #define CB_TEST_COUNT 2
895 static int test_count_ex;
896 static BIO *param_b_ex[CB_TEST_COUNT];
897 static int param_oper_ex[CB_TEST_COUNT];
898 static const char *param_argp_ex[CB_TEST_COUNT];
899 static int param_argi_ex[CB_TEST_COUNT];
900 static long param_argl_ex[CB_TEST_COUNT];
901 static long param_ret_ex[CB_TEST_COUNT];
902 static size_t param_len_ex[CB_TEST_COUNT];
903 static size_t param_processed_ex[CB_TEST_COUNT];
904
905 static long bio_cb_ex(BIO *b, int oper, const char *argp, size_t len, int argi,
906 long argl, int ret, size_t *processed) {
907 if (test_count_ex >= CB_TEST_COUNT) {
908 return CALL_BACK_FAILURE;
909 }
910 param_b_ex[test_count_ex] = b;
911 param_oper_ex[test_count_ex] = oper;
912 param_argp_ex[test_count_ex] = argp;
913 param_argi_ex[test_count_ex] = argi;
914 param_argl_ex[test_count_ex] = argl;
915 param_ret_ex[test_count_ex] = ret;
916 param_len_ex[test_count_ex] = len;
917 param_processed_ex[test_count_ex] = processed != NULL ? *processed : 0;
918 test_count_ex++;
919 return ret;
920 }
921
922 static long bio_cb(BIO *b, int oper, const char *argp, int argi,
923 long argl, long ret) {
924 if (test_count_ex >= CB_TEST_COUNT) {
925 return CALL_BACK_FAILURE;
926 }
927 param_b_ex[test_count_ex] = b;
928 param_oper_ex[test_count_ex] = oper;
929 param_argp_ex[test_count_ex] = argp;
930 param_argi_ex[test_count_ex] = argi;
931 param_argl_ex[test_count_ex] = argl;
932 param_ret_ex[test_count_ex] = ret;
933 test_count_ex++;
934 return ret;
935 }
936
937 static void bio_callback_cleanup() {
938 // These mocks are used in multiple tests and need to be reset
939 test_count_ex = 0;
940 OPENSSL_cleanse(param_b_ex, sizeof(param_b_ex));
941 OPENSSL_cleanse(param_oper_ex, sizeof(param_oper_ex));
942 OPENSSL_cleanse(param_argp_ex, sizeof(param_argp_ex));
943 OPENSSL_cleanse(param_argi_ex, sizeof(param_argi_ex));
944 OPENSSL_cleanse(param_argl_ex, sizeof(param_argl_ex));
945 OPENSSL_cleanse(param_ret_ex, sizeof(param_ret_ex));
946 OPENSSL_cleanse(param_len_ex, sizeof(param_len_ex));
947 OPENSSL_cleanse(param_processed_ex, sizeof(param_processed_ex));
948 }
949
950 #define TEST_BUF_LEN 20
951 #define TEST_DATA_WRITTEN 5
952 TEST_P(BIOPairTest, TestCallbacks) {
953 bio_callback_cleanup();
954
955 BIO *bio1, *bio2;
956 ASSERT_TRUE(BIO_new_bio_pair(&bio1, 10, &bio2, 10));
957
958 // Check the second parameter from the test tuple to determine which callback type to use
959 // params is a tuple<bool, bool> where:
960 // - get<0> controls bio swapping
961 // - get<1> controls callback type (true = extended, false = legacy)
962 const auto& params = GetParam();
963 if (std::get<0>(params)) { // swap_bios
964 std::swap(bio1, bio2);
965 }
966
967 if (std::get<1>(params)) {
968 // Use extended callback (BIO_callback_ex) which provides additional parameters:
969 // - len: size of the buffer for read/write operations
970 // - processed: pointer to store number of bytes actually processed
971 BIO_set_callback_ex(bio2, bio_cb_ex);
972 } else {
973 // Use legacy callback (BIO_callback) with basic parameters
974 BIO_set_callback(bio2, bio_cb);
975 }
976
977 // Data written in one end may be read out the other.
978 uint8_t buf[TEST_BUF_LEN];
979 EXPECT_EQ(TEST_DATA_WRITTEN, BIO_write(bio1, "12345", TEST_DATA_WRITTEN));
980 ASSERT_EQ(TEST_DATA_WRITTEN, BIO_read(bio2, buf, sizeof(buf)));
981 EXPECT_EQ(Bytes("12345"), Bytes(buf, TEST_DATA_WRITTEN));
982
983 // Check that read or write was called first, then the combo with
984 // BIO_CB_RETURN
985 ASSERT_EQ(param_oper_ex[0], BIO_CB_READ);
986 ASSERT_EQ(param_oper_ex[1], BIO_CB_READ | BIO_CB_RETURN);
987
988 // argp is a pointer to a buffer for read/write operations. We don't care
989 // where the buf is, but it should be the same before and after the BIO calls
990 ASSERT_EQ(param_argp_ex[0], param_argp_ex[1]);
991
992 // The calls before the BIO operation use 1 for the BIO's return value
993 ASSERT_EQ(param_ret_ex[0], 1);
994
995
996
997 if (!std::get<1>(params)) {
998 // Legacy callback uses ret to hold data processed
999 ASSERT_EQ(param_ret_ex[1], TEST_DATA_WRITTEN);
1000 // Legacy callback argi is used for len
1001 ASSERT_EQ(param_argi_ex[0], TEST_BUF_LEN);
1002 ASSERT_EQ(param_argi_ex[1], TEST_BUF_LEN);
1003 ASSERT_EQ(param_argl_ex[0], 0);
1004 ASSERT_EQ(param_argl_ex[1], 0);
1005 }
1006
1007 // Extended callback specific verifications
1008 if (std::get<1>(params)) {// If using extended callback
1009 // For callback_ex ret is set to 1 after setting processed
1010 ASSERT_EQ(param_ret_ex[1], 1);
1011 // For callback_ex argi and arl are unused
1012 ASSERT_EQ(param_argi_ex[0], 0);
1013 ASSERT_EQ(param_argi_ex[1], 0);
1014 ASSERT_EQ(param_argl_ex[0], 0);
1015 ASSERT_EQ(param_argl_ex[1], 0);
1016 // For callback_ex the |len| param is the requested number of bytes to
1017 // read/write
1018 ASSERT_EQ(param_len_ex[0], (size_t)TEST_BUF_LEN);
1019 ASSERT_EQ(param_len_ex[0], (size_t)TEST_BUF_LEN);
1020 // processed is null (0 in the array) the first call and the actual data the
1021 // second time
1022 ASSERT_EQ(param_processed_ex[0], 0u);
1023 ASSERT_EQ(param_processed_ex[1], 5u);
1024 }
1025
1026 // The mock should be "full" at this point
1027 ASSERT_EQ(test_count_ex, CB_TEST_COUNT);
1028
1029 // If we attempt to read or write more from either BIO the callback fails
1030 // and the callback return value is returned to the caller
1031 ASSERT_EQ(CALL_BACK_FAILURE, BIO_read(bio2, buf, sizeof(buf)));
1032
1033 // Run bio_callback_cleanup to reset the mock, without this when BIO_free
1034 // calls the callback it would fail before freeing the memory and be detected
1035 // as a memory leak.
1036 bio_callback_cleanup();
1037 ASSERT_EQ(BIO_free(bio1), 1);
1038 ASSERT_EQ(BIO_free(bio2), 1);
1039
1040 ASSERT_EQ(param_oper_ex[0], BIO_CB_FREE);
1041
1042 ASSERT_EQ(param_argp_ex[0], nullptr);
1043 ASSERT_EQ(param_argi_ex[0], 0);
1044 ASSERT_EQ(param_argl_ex[0], 0);
1045 ASSERT_EQ(param_ret_ex[0], 1);
1046 ASSERT_EQ(param_len_ex[0], 0u);
1047 }
1048
1049 namespace {
1050 #if !defined(OPENSSL_NO_SOCK)
1051 static int callback_invoked = 0;
1052
1053 static long callback(BIO *b, int state, int res) {
1054 callback_invoked = 1;
1055 EXPECT_EQ(state, 0);
1056 EXPECT_EQ(res, -1);
1057 return 0;
1058 }
1059
1060 TEST(BIOTest, InvokeConnectCallback) {
1061 #if defined(TARGET_OS_IPHONE) && TARGET_OS_IPHONE
1062 GTEST_SKIP() << "InvokeConnectCallback does not run on iOS";
1063 #endif
1064
1065 ASSERT_EQ(callback_invoked, 0);
1066 BIO *bio = BIO_new(BIO_s_connect());
1067 ASSERT_NE(bio, nullptr);
1068
1069 ASSERT_TRUE(BIO_set_conn_hostname(bio, "localhost"));
1070 ASSERT_TRUE(BIO_set_conn_port(bio, "5325"));
1071 ASSERT_TRUE(BIO_callback_ctrl(bio, BIO_CTRL_SET_CALLBACK, callback));
1072
1073 ASSERT_EQ(BIO_do_connect(bio), 0);
1074 ASSERT_EQ(callback_invoked, 1);
1075
1076 ASSERT_TRUE(BIO_free(bio));
1077 }
1078 #endif // !OPENSSL_NO_SOCK
1079 } // namespace
1080
1081
1082 // Instantiate the parameterized test suite for BIOPairTest
1083 // This creates test instances for all combinations of the boolean parameters
1084 //
1085 // Parameters:
1086 // First argument "All": Test suite name prefix
1087 // Second argument "BIOPairTest": The test fixture class
1088 // Third argument: Parameter generator using testing::Combine to create Cartesian product
1089 // - First Bool(): Controls BIO swapping
1090 // * false: Keep original BIO order (bio1->bio2)
1091 // * true: Swap BIOs (bio2->bio1)
1092 // - Second Bool(): Controls callback type
1093 // * false: Use legacy callback (BIO_set_callback)
1094 // * true: Use extended callback (BIO_set_callback_ex)
1095 //
1096 // This will generate four test combinations:
1097 // 1. (false, false) - Normal order, Legacy callback
1098 // 2. (false, true) - Normal order, Extended callback
1099 // 3. (true, false) - Swapped order, Legacy callback
1100 // 4. (true, true) - Swapped order, Extended callback
1101 INSTANTIATE_TEST_SUITE_P(
1102 All,
1103 BIOPairTest,
1104 testing::Combine(
1105 testing::Bool(), // swap_bios
1106 testing::Bool() // use_extended_callback
1107 )
1108 );
1109
1110 TEST(BIOTest, ReadWriteEx) {
1111 bssl::UniquePtr<BIO> bio(BIO_new(BIO_s_mem()));
1112 ASSERT_TRUE(bio);
1113
1114 // Reading from an initially empty bio should default to returning a error.
1115 // Check that both |BIO_read| and |BIO_read_ex| fail.
1116 char buf[32];
1117 size_t read = 1;
1118 EXPECT_EQ(BIO_read(bio.get(), buf, sizeof(buf)), -1);
1119 EXPECT_FALSE(BIO_read_ex(bio.get(), buf, sizeof(buf), &read));
1120 EXPECT_EQ(read, (size_t)0);
1121
1122 // Write and read normally from buffer.
1123 size_t written = 1;
1124 ASSERT_TRUE(BIO_write_ex(bio.get(), "abcdef", 6, &written));
1125 EXPECT_EQ(written, (size_t)6);
1126 ASSERT_TRUE(BIO_read_ex(bio.get(), buf, sizeof(buf), &read));
1127 EXPECT_EQ(read, (size_t)6);
1128 EXPECT_EQ(Bytes(buf, read), Bytes("abcdef"));
1129
1130 // Test NULL |written_bytes| behavior works.
1131 ASSERT_TRUE(BIO_write_ex(bio.get(), "ghilmnop", 8, nullptr));
1132 ASSERT_TRUE(BIO_read_ex(bio.get(), buf, sizeof(buf), &read));
1133 EXPECT_EQ(read, (size_t)8);
1134 EXPECT_EQ(Bytes(buf, read), Bytes("ghilmnop"));
1135
1136 // Test NULL |read_bytes| behavior fails.
1137 ASSERT_TRUE(BIO_write_ex(bio.get(), "ghilmnop", 8, nullptr));
1138 ASSERT_FALSE(BIO_read_ex(bio.get(), buf, sizeof(buf), nullptr));
1139
1140 // Test that |BIO_write/read_ex| align with their non-ex counterparts, when
1141 // encountering NULL data. EOF in |BIO_read| is indicated by returning 0.
1142 // In |BIO_read_ex| however, EOF returns a failure and sets |read| to 0.
1143 EXPECT_FALSE(BIO_write(bio.get(), nullptr, 0));
1144 EXPECT_FALSE(BIO_write_ex(bio.get(), nullptr, 0, &written));
1145 EXPECT_EQ(written, (size_t)0);
1146 EXPECT_EQ(BIO_read(bio.get(), nullptr, 0), 0);
1147 EXPECT_FALSE(BIO_read_ex(bio.get(), nullptr, 0, &read));
1148 EXPECT_EQ(read, (size_t)0);
1149 }
1150
1151 TEST(BIOTest, TestPutsAsWrite) {
1152 // By default, |BIO_puts| acts as |BIO_write|.
1153 bssl::UniquePtr<BIO> bio(BIO_new(BIO_s_mem()));
1154 ASSERT_TRUE(bio);
1155
1156 // Test basic puts and read
1157 uint8_t buf[32];
1158 EXPECT_EQ(12, BIO_puts(bio.get(), "hello world\n"));
1159 EXPECT_EQ(12, BIO_read(bio.get(), buf, sizeof(buf)));
1160 }
1161
1162 namespace {
1163 // Define custom BIO and BIO_METHODS to test BIO_puts without write
1164 static int customPuts(BIO *b, const char *in) {
1165 return 0;
1166 }
1167 static int customNew(BIO *b) {
1168 b->init=1;
1169 return 1;
1170 }
1171 static const BIO_METHOD custom_method = {
1172 BIO_TYPE_NONE, "CustomBioMethod", NULL /* write */,
1173 NULL, customPuts, NULL,
1174 NULL, customNew, NULL,
1175 NULL
1176 };
1177
1178 static const BIO_METHOD *BIO_cust(void) { return &custom_method; }
1179
1180 TEST(BIOTest, TestCustomPuts) {
1181 bssl::UniquePtr<BIO> bio(BIO_new(BIO_cust()));
1182 ASSERT_TRUE(bio);
1183
1184 ASSERT_EQ(0, BIO_puts(bio.get(), "hello world"));
1185
1186 // Test setting new puts method by creating a new BIO
1187 bssl::UniquePtr<BIO_METHOD> method(BIO_meth_new(0, nullptr));
1188 ASSERT_TRUE(method);
1189 ASSERT_TRUE(BIO_meth_set_create(
1190 method.get(), [](BIO *b) -> int {
1191 BIO_set_init(b, 1);
1192 return 1;
1193 }));
1194 ASSERT_TRUE(BIO_meth_set_puts(
1195 method.get(), [](BIO *b, const char *in) -> int {
1196 return 100;
1197 }
1198 ));
1199 bssl::UniquePtr<BIO> bio1(BIO_new(method.get()));
1200 ASSERT_TRUE(bio1);
1201 ASSERT_TRUE(bio1.get()->method->bputs);
1202 ASSERT_FALSE(bio1.get()->method->bwrite);
1203 // The new BIO_puts should only return 100
1204 ASSERT_EQ(100, BIO_puts(bio1.get(), "hello world"));
1205 }
1206
1207 TEST(BIOTest, TestPutsNullMethod) {
1208 // Create new BIO to test when neither puts nor write is set
1209 bssl::UniquePtr<BIO_METHOD> method(BIO_meth_new(0, nullptr));
1210 ASSERT_TRUE(method);
1211 ASSERT_TRUE(BIO_meth_set_create(
1212 method.get(), [](BIO *b) -> int {
1213 BIO_set_init(b, 1);
1214 return 1;
1215 }));
1216 bssl::UniquePtr<BIO> bio(BIO_new(method.get()));
1217 ASSERT_TRUE(bio);
1218
1219 ASSERT_FALSE(bio.get()->method->bputs);
1220 ASSERT_FALSE(bio.get()->method->bwrite);
1221 ASSERT_EQ(-2, BIO_puts(bio.get(), "hello world"));
1222 }
1223 } //namespace
1224
1225 TEST_P(BIOPairTest, TestPutsCallbacks) {
1226 bio_callback_cleanup();
1227 bssl::UniquePtr<BIO> bio(BIO_new(BIO_s_mem()));
1228 ASSERT_TRUE(bio);
1229
1230 // - get<1> controls callback type (true = extended, false = legacy)
1231 const bool use_extended_callback = std::get<1>(GetParam());
1232
1233 if (use_extended_callback) {
1234 // Use extended callback (BIO_callback_ex) which provides additional parameters:
1235 // - len: size of the buffer for read/write operations
1236 // - processed: pointer to store number of bytes actually processed
1237 BIO_set_callback_ex(bio.get(), bio_cb_ex);
1238 } else {
1239 // Use legacy callback (BIO_callback) with basic parameters
1240 BIO_set_callback(bio.get(), bio_cb);
1241 }
1242
1243 EXPECT_EQ(TEST_DATA_WRITTEN, BIO_puts(bio.get(), "12345"));
1244
1245 ASSERT_EQ(param_oper_ex[0], BIO_CB_PUTS);
1246 ASSERT_EQ(param_oper_ex[1], BIO_CB_PUTS | BIO_CB_RETURN);
1247
1248 ASSERT_EQ(param_argp_ex[0], param_argp_ex[1]);
1249 ASSERT_EQ(param_ret_ex[0], 1);
1250
1251 if (!use_extended_callback) {
1252 // Legacy callback uses ret for data processed
1253 ASSERT_EQ(param_ret_ex[1], TEST_DATA_WRITTEN);
1254 }
1255
1256
1257 ASSERT_EQ(param_argi_ex[0], 0);
1258 ASSERT_EQ(param_argi_ex[1], 0);
1259 ASSERT_EQ(param_argl_ex[0], 0);
1260 ASSERT_EQ(param_argl_ex[1], 0);
1261
1262 // Extended callback specific verifications
1263 if (use_extended_callback) {
1264 // ret is set to processed and ret is reset back to 1
1265 ASSERT_EQ(param_ret_ex[1], 1);
1266 // len unused in puts callback
1267 ASSERT_FALSE(param_len_ex[0]);
1268 ASSERT_FALSE(param_len_ex[1]);
1269
1270 // Verify processed bytes
1271 ASSERT_EQ(param_processed_ex[0], 0u);
1272 ASSERT_EQ(param_processed_ex[1], 5u);
1273 }
1274
1275 // The mock should be "full" at this point
1276 ASSERT_EQ(test_count_ex, CB_TEST_COUNT);
1277
1278 bio_callback_cleanup();
1279 }
1280
1281 TEST_P(BIOPairTest, TestGetsCallback) {
1282 bio_callback_cleanup();
1283
1284 bssl::UniquePtr<BIO> bio (BIO_new(BIO_s_mem()));
1285 ASSERT_TRUE(bio);
1286 // write data to BIO, then set callback
1287 EXPECT_EQ(TEST_DATA_WRITTEN, BIO_write(bio.get(), "12345", TEST_DATA_WRITTEN));
1288 char buf[TEST_BUF_LEN];
1289
1290 // - get<1> controls callback type (true = extended, false = legacy)
1291 const bool use_extended_callback = std::get<1>(GetParam());
1292
1293 if (use_extended_callback) {
1294 // Use extended callback (BIO_callback_ex) which provides additional parameters:
1295 // - len: size of the buffer for read/write operations
1296 // - processed: pointer to store number of bytes actually processed
1297 BIO_set_callback_ex(bio.get(), bio_cb_ex);
1298 } else {
1299 // Use legacy callback (BIO_callback) with basic parameters
1300 BIO_set_callback(bio.get(), bio_cb);
1301 }
1302
1303 ASSERT_EQ(TEST_DATA_WRITTEN, BIO_gets(bio.get(), buf, sizeof(buf)));
1304
1305 ASSERT_EQ(param_oper_ex[0], BIO_CB_GETS);
1306 ASSERT_EQ(param_oper_ex[1], BIO_CB_GETS | BIO_CB_RETURN);
1307
1308 ASSERT_EQ(param_argp_ex[0], param_argp_ex[1]);
1309 ASSERT_EQ(param_ret_ex[0], 1);
1310
1311
1312 ASSERT_EQ(param_argl_ex[0], 0);
1313 ASSERT_EQ(param_argl_ex[1], 0);
1314
1315 // Old-style callback uses argi for len
1316 if (!use_extended_callback) {
1317 // use ret for data processed
1318 ASSERT_EQ(param_ret_ex[1], TEST_DATA_WRITTEN);
1319 ASSERT_EQ(param_argi_ex[0], TEST_BUF_LEN);
1320 ASSERT_EQ(param_argi_ex[1], TEST_BUF_LEN);
1321 }
1322
1323 // Extended callback specific verifications
1324 if (use_extended_callback) {
1325 // ret is set to 1 after setting processed
1326 ASSERT_EQ(param_ret_ex[1], 1);
1327 ASSERT_EQ(param_argi_ex[0], 0);
1328 ASSERT_EQ(param_argi_ex[1], 0);
1329 ASSERT_EQ(param_len_ex[0], (size_t)TEST_BUF_LEN);
1330 ASSERT_EQ(param_len_ex[1], (size_t)TEST_BUF_LEN);
1331 ASSERT_EQ(param_processed_ex[0], 0u);
1332 ASSERT_EQ(param_processed_ex[1], 5u);
1333 }
1334
1335
1336 ASSERT_EQ(test_count_ex, CB_TEST_COUNT);
1337
1338 bio_callback_cleanup();
1339 }
1340
1341 TEST_P(BIOPairTest, TestCtrlCallback) {
1342 bio_callback_cleanup();
1343
1344 bssl::UniquePtr<BIO> bio(BIO_new(BIO_s_mem()));
1345 ASSERT_TRUE(bio);
1346
1347 // - get<1> controls callback type (true = extended, false = legacy)
1348 const bool use_extended_callback = std::get<1>(GetParam());
1349
1350 if (use_extended_callback) {
1351 // Use extended callback (BIO_callback_ex) which provides additional parameters:
1352 // - len: size of the buffer for read/write operations
1353 // - processed: pointer to store number of bytes actually processed
1354 BIO_set_callback_ex(bio.get(), bio_cb_ex);
1355 } else {
1356 // Use legacy callback (BIO_callback) with basic parameters
1357 BIO_set_callback(bio.get(), bio_cb);
1358 }
1359
1360 char buf[TEST_BUF_LEN];
1361 // Test BIO_ctrl. This is not normally called directly so we can use one of
1362 // the macros such as BIO_reset to test it
1363 ASSERT_EQ(BIO_reset(bio.get()), 1);
1364
1365 ASSERT_EQ(param_oper_ex[0], BIO_CB_CTRL);
1366 ASSERT_EQ(param_oper_ex[1], BIO_CB_CTRL | BIO_CB_RETURN);
1367
1368 // argi in this case in the cmd sent to the ctrl method
1369 ASSERT_EQ(param_argi_ex[0], BIO_CTRL_RESET);
1370 ASSERT_EQ(param_argi_ex[1], BIO_CTRL_RESET);
1371
1372 // BIO_ctrl of a memory bio sets ret to 1 when it calls the reset method
1373 ASSERT_EQ(param_ret_ex[0], 1);
1374 ASSERT_EQ(param_ret_ex[1], 1);
1375
1376 // processed is unused in ctrl
1377 ASSERT_EQ(param_processed_ex[0], 0u);
1378 ASSERT_EQ(param_processed_ex[1], 0u);
1379
1380 bio_callback_cleanup();
1381 // check that BIO_reset was called correctly
1382 ASSERT_EQ(BIO_gets(bio.get(), buf, sizeof(buf)), 0);
1383
1384 bio_callback_cleanup();
1385 }
1386
1387 TEST(BIOTest, GetMemDataBackwardsCompat) {
1388 bssl::UniquePtr<BIO> bio(BIO_new(BIO_s_mem()));
1389 ASSERT_TRUE(bio);
1390
1391 const uint8_t contents[] = {0x72, 0x61, 0x63, 0x63, 0x6f, 0x6f, 0x6e};
1392
1393 // Write some test data
1394 int write_len = BIO_write(bio.get(), contents, sizeof(contents));
1395 ASSERT_EQ(sizeof(contents), (size_t)write_len);
1396
1397 // Yes, this is something gRPC does
1398 const uint8_t *ptr = NULL;
1399 long data_len = BIO_get_mem_data(bio.get(), &ptr);
1400 ASSERT_EQ((size_t)data_len, sizeof(contents));
1401 EXPECT_EQ(Bytes(contents, sizeof(contents)), Bytes(ptr, data_len));
1402 }
1403
1404 TEST(BIOTest, Dump) {
1405 bssl::UniquePtr<BIO> bio(BIO_new(BIO_s_mem()));
1406 ASSERT_TRUE(bio);
1407
1408 static constexpr std::array<uint8_t, 12> data = {{
1409 0x00, 0x01, 0x02, 0x7f, 0x80, 0xff, 'A', 'B', 'C', '\n', '\r', '\t'
1410 }};
1411
1412 int ret = BIO_dump(bio.get(), data.data(), data.size());
1413
1414 static constexpr std::array<char, 75> kExpectedOutput = {{
1415 '0', '0', '0', '0', '0', '0', '0', '0', // offset
1416 ' ', ' ', // spacing
1417 '0', '0', ' ', '0', '1', ' ', '0', '2', ' ', '7', 'f', ' ',
1418 '8', '0', ' ', 'f', 'f', ' ', '4', '1', ' ', '4', '2', ' ', // first 8 bytes hex
1419 ' ', // group separator
1420 '4', '3', ' ', '0', 'a', ' ', '0', 'd', ' ', '0', '9', // remaining 4 bytes hex
1421 ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', // padding
1422 '|', // separator
1423 '.', '.', '.', '.', '.', '.', 'A', 'B', 'C', '.', '.', '.', // ASCII
1424 '|', '\n' // closing
1425 }};
1426
1427 const uint8_t *contents = nullptr;
1428 size_t len = 0;
1429 ASSERT_TRUE(BIO_mem_contents(bio.get(), &contents, &len));
1430 std::string output(reinterpret_cast<const char *>(contents), len);
1431
1432 EXPECT_EQ(output, std::string(kExpectedOutput.data(), kExpectedOutput.size()))
1433 << "BIO_dump output should exactly match expected format";
1434 EXPECT_EQ(ret, static_cast<int>(kExpectedOutput.size()))
1435 << "Return value should match expected length";
1436
1437 // Test edge cases
1438 bio.reset(BIO_new(BIO_s_mem()));
1439 ASSERT_TRUE(bio);
1440 EXPECT_EQ(BIO_dump(bio.get(), data.data(), 0), 0)
1441 << "Valid data with zero length should return 0";
1442
1443 EXPECT_EQ(BIO_dump(bio.get(), data.data(), -1), -1)
1444 << "Negative length should return -1";
1445
1446 EXPECT_EQ(BIO_dump(bio.get(), nullptr, 0), -1)
1447 << "BIO_dump with NULL data should return -1";
1448
1449 EXPECT_EQ(BIO_dump(nullptr, data.data(), data.size()), -1)
1450 << "BIO_dump with NULL BIO should return -1";
1451
1452 // Test with larger data
1453 static constexpr std::array<uint8_t, 32> large_data = {{
1454 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
1455 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
1456 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
1457 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f
1458 }};
1459
1460 bio.reset(BIO_new(BIO_s_mem()));
1461 ASSERT_TRUE(bio);
1462 ret = BIO_dump(bio.get(), large_data.data(), large_data.size());
1463
1464 static constexpr std::array<char, 158> kExpectedLargeOutput = {{
1465 // First line
1466 '0', '0', '0', '0', '0', '0', '0', '0', // offset
1467 ' ', ' ', // spacing
1468 '0', '0', ' ', '0', '1', ' ', '0', '2', ' ', '0', '3', ' ',
1469 '0', '4', ' ', '0', '5', ' ', '0', '6', ' ', '0', '7', ' ', // first 8 bytes hex
1470 ' ', // group separator
1471 '0', '8', ' ', '0', '9', ' ', '0', 'a', ' ', '0', 'b', ' ',
1472 '0', 'c', ' ', '0', 'd', ' ', '0', 'e', ' ', '0', 'f', ' ', // last 8 bytes hex
1473 ' ', '|', // separator
1474 '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', // ASCII
1475 '|', '\n', // closing
1476
1477 // Second line
1478 '0', '0', '0', '0', '0', '0', '1', '0', // offset
1479 ' ', ' ', // spacing
1480 '1', '0', ' ', '1', '1', ' ', '1', '2', ' ', '1', '3', ' ',
1481 '1', '4', ' ', '1', '5', ' ', '1', '6', ' ', '1', '7', ' ', // first 8 bytes hex
1482 ' ', // group separator
1483 '1', '8', ' ', '1', '9', ' ', '1', 'a', ' ', '1', 'b', ' ',
1484 '1', 'c', ' ', '1', 'd', ' ', '1', 'e', ' ', '1', 'f', ' ', // last 8 bytes hex
1485 ' ', '|', // separator
1486 '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', // ASCII
1487 '|', '\n' // closing
1488 }};
1489
1490 ASSERT_TRUE(BIO_mem_contents(bio.get(), &contents, &len));
1491 output = std::string(reinterpret_cast<const char *>(contents), len);
1492
1493 EXPECT_EQ(output, std::string(kExpectedLargeOutput.data(), kExpectedLargeOutput.size()))
1494 << "BIO_dump output should exactly match expected format";
1495 EXPECT_EQ(ret, static_cast<int>(kExpectedLargeOutput.size()))
1496 << "Return value should match expected length";
1497 }