2 * Copyright 2022 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the Apache License 2.0 (the "License"). You may not use
5 * this file except in compliance with the License. You can obtain a copy
6 * in the file LICENSE in the source distribution or at
7 * https://www.openssl.org/source/license.html
9 #include "internal/packet.h"
10 #include "internal/quic_stream.h"
13 static int compare_iov(const unsigned char *ref, size_t ref_len,
14 const OSSL_QTX_IOVEC *iov, size_t iov_len)
16 size_t i, total_len = 0;
17 const unsigned char *cur = ref;
19 for (i = 0; i < iov_len; ++i)
20 total_len += iov[i].buf_len;
22 if (ref_len != total_len)
25 for (i = 0; i < iov_len; ++i) {
26 if (memcmp(cur, iov[i].buf, iov[i].buf_len))
29 cur += iov[i].buf_len;
35 static const unsigned char data_1[] = {
36 0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59,
37 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f
40 static int test_sstream_simple(void)
43 QUIC_SSTREAM *sstream = NULL;
44 OSSL_QUIC_FRAME_STREAM hdr;
45 OSSL_QTX_IOVEC iov[2];
46 size_t num_iov = 0, wr = 0, i, init_size = 8192;
48 if (!TEST_ptr(sstream = ossl_quic_sstream_new(init_size)))
51 /* Should not have any data yet */
52 num_iov = OSSL_NELEM(iov);
53 if (!TEST_false(ossl_quic_sstream_get_stream_frame(sstream, 0, &hdr, iov,
58 if (!TEST_true(ossl_quic_sstream_append(sstream, data_1, sizeof(data_1),
60 || !TEST_size_t_eq(wr, sizeof(data_1)))
64 num_iov = OSSL_NELEM(iov);
65 if (!TEST_true(ossl_quic_sstream_get_stream_frame(sstream, 0, &hdr, iov,
67 || !TEST_size_t_gt(num_iov, 0)
68 || !TEST_uint64_t_eq(hdr.offset, 0)
69 || !TEST_uint64_t_eq(hdr.len, sizeof(data_1))
70 || !TEST_false(hdr.is_fin))
73 if (!TEST_true(compare_iov(data_1, sizeof(data_1), iov, num_iov)))
76 /* Mark data as half transmitted */
77 if (!TEST_true(ossl_quic_sstream_mark_transmitted(sstream, 0, 7)))
81 num_iov = OSSL_NELEM(iov);
82 if (!TEST_true(ossl_quic_sstream_get_stream_frame(sstream, 0, &hdr, iov,
84 || !TEST_size_t_gt(num_iov, 0)
85 || !TEST_uint64_t_eq(hdr.offset, 8)
86 || !TEST_uint64_t_eq(hdr.len, sizeof(data_1) - 8)
87 || !TEST_false(hdr.is_fin))
90 if (!TEST_true(compare_iov(data_1 + 8, sizeof(data_1) - 8, iov, num_iov)))
93 if (!TEST_true(ossl_quic_sstream_mark_transmitted(sstream, 8, 15)))
96 /* Read more data; should not be any more */
97 num_iov = OSSL_NELEM(iov);
98 if (!TEST_false(ossl_quic_sstream_get_stream_frame(sstream, 0, &hdr, iov,
102 /* Now we have lost bytes 4-6 */
103 if (!TEST_true(ossl_quic_sstream_mark_lost(sstream, 4, 6)))
106 /* Should be able to read them */
107 num_iov = OSSL_NELEM(iov);
108 if (!TEST_true(ossl_quic_sstream_get_stream_frame(sstream, 0, &hdr, iov,
110 || !TEST_size_t_gt(num_iov, 0)
111 || !TEST_uint64_t_eq(hdr.offset, 4)
112 || !TEST_uint64_t_eq(hdr.len, 3)
113 || !TEST_false(hdr.is_fin))
116 if (!TEST_true(compare_iov(data_1 + 4, 3, iov, num_iov)))
120 if (!TEST_true(ossl_quic_sstream_mark_transmitted(sstream, 4, 6)))
123 /* Read more data; should not be any more */
124 num_iov = OSSL_NELEM(iov);
125 if (!TEST_false(ossl_quic_sstream_get_stream_frame(sstream, 0, &hdr, iov,
129 if (!TEST_size_t_eq(ossl_quic_sstream_get_buffer_used(sstream), 16))
132 /* Data has been acknowledged, space should be not be freed yet */
133 if (!TEST_true(ossl_quic_sstream_mark_acked(sstream, 1, 7))
134 || !TEST_size_t_eq(ossl_quic_sstream_get_buffer_used(sstream), 16))
137 /* Now data should be freed */
138 if (!TEST_true(ossl_quic_sstream_mark_acked(sstream, 0, 0))
139 || !TEST_size_t_eq(ossl_quic_sstream_get_buffer_used(sstream), 8))
142 if (!TEST_true(ossl_quic_sstream_mark_acked(sstream, 0, 15))
143 || !TEST_size_t_eq(ossl_quic_sstream_get_buffer_used(sstream), 0))
147 ossl_quic_sstream_fin(sstream);
150 for (i = 0; i < 2; ++i) {
151 num_iov = OSSL_NELEM(iov);
152 if (!TEST_true(ossl_quic_sstream_get_stream_frame(sstream, 0, &hdr, iov,
154 || !TEST_uint64_t_eq(hdr.offset, 16)
155 || !TEST_uint64_t_eq(hdr.len, 0)
156 || !TEST_true(hdr.is_fin)
157 || !TEST_size_t_eq(num_iov, 0))
161 if (!TEST_true(ossl_quic_sstream_mark_transmitted_fin(sstream, 16)))
164 /* Read more data; FIN should not be returned any more */
165 num_iov = OSSL_NELEM(iov);
166 if (!TEST_false(ossl_quic_sstream_get_stream_frame(sstream, 0, &hdr, iov,
171 if (!TEST_true(ossl_quic_sstream_mark_lost_fin(sstream)))
175 for (i = 0; i < 2; ++i) {
176 num_iov = OSSL_NELEM(iov);
177 if (!TEST_true(ossl_quic_sstream_get_stream_frame(sstream, 0, &hdr, iov,
179 || !TEST_uint64_t_eq(hdr.offset, 16)
180 || !TEST_uint64_t_eq(hdr.len, 0)
181 || !TEST_true(hdr.is_fin)
182 || !TEST_size_t_eq(num_iov, 0))
186 if (!TEST_true(ossl_quic_sstream_mark_transmitted_fin(sstream, 16)))
189 /* Read more data; FIN should not be returned any more */
190 num_iov = OSSL_NELEM(iov);
191 if (!TEST_false(ossl_quic_sstream_get_stream_frame(sstream, 0, &hdr, iov,
195 /* Acknowledge fin. */
196 if (!TEST_true(ossl_quic_sstream_mark_acked_fin(sstream)))
201 ossl_quic_sstream_free(sstream);
205 static int test_sstream_bulk(int idx)
208 QUIC_SSTREAM *sstream = NULL;
209 OSSL_QUIC_FRAME_STREAM hdr;
210 OSSL_QTX_IOVEC iov[2];
211 size_t i, num_iov = 0, init_size = 8192, l;
212 size_t consumed = 0, rd, expected = 0;
213 unsigned char *src_buf = NULL, *dst_buf = NULL;
214 unsigned char *ref_src_buf = NULL, *ref_dst_buf = NULL;
215 unsigned char *ref_dst_cur, *ref_src_cur, *dst_cur;
217 if (!TEST_ptr(sstream = ossl_quic_sstream_new(init_size)))
220 if (!TEST_size_t_eq(ossl_quic_sstream_get_buffer_size(sstream), init_size))
223 if (!TEST_ptr(src_buf = OPENSSL_zalloc(init_size)))
226 if (!TEST_ptr(dst_buf = OPENSSL_malloc(init_size)))
229 if (!TEST_ptr(ref_src_buf = OPENSSL_malloc(init_size)))
232 if (!TEST_ptr(ref_dst_buf = OPENSSL_malloc(init_size)))
236 * Append a preliminary buffer to allow later code to exercise wraparound.
238 if (!TEST_true(ossl_quic_sstream_append(sstream, src_buf, init_size / 2,
240 || !TEST_size_t_eq(consumed, init_size / 2)
241 || !TEST_true(ossl_quic_sstream_mark_transmitted(sstream, 0,
243 || !TEST_true(ossl_quic_sstream_mark_acked(sstream, 0,
247 /* Generate a random buffer. */
248 for (i = 0; i < init_size; ++i)
249 src_buf[i] = (unsigned char)(test_random() & 0xFF);
251 /* Append bytes into the buffer in chunks of random length. */
252 ref_src_cur = ref_src_buf;
254 l = (test_random() % init_size) + 1;
255 if (!TEST_true(ossl_quic_sstream_append(sstream, src_buf, l, &consumed)))
258 memcpy(ref_src_cur, src_buf, consumed);
259 ref_src_cur += consumed;
260 } while (consumed > 0);
262 if (!TEST_size_t_eq(ossl_quic_sstream_get_buffer_used(sstream), init_size)
263 || !TEST_size_t_eq(ossl_quic_sstream_get_buffer_avail(sstream), 0))
267 * Randomly select bytes out of the buffer by marking them as transmitted.
268 * Record the remaining bytes, which should be the sequence of bytes
271 ref_src_cur = ref_src_buf;
272 ref_dst_cur = ref_dst_buf;
273 for (i = 0; i < consumed; ++i) {
274 if ((test_random() & 1) != 0) {
275 *ref_dst_cur++ = *ref_src_cur;
277 } else if (!TEST_true(ossl_quic_sstream_mark_transmitted(sstream, i, i)))
283 /* Exercise resize. */
284 if (!TEST_true(ossl_quic_sstream_set_buffer_size(sstream, init_size * 2))
285 || !TEST_true(ossl_quic_sstream_set_buffer_size(sstream, init_size)))
288 /* Readout and verification. */
290 for (i = 0, rd = 0; rd < expected; ++i) {
291 num_iov = OSSL_NELEM(iov);
292 if (!TEST_true(ossl_quic_sstream_get_stream_frame(sstream, i, &hdr, iov,
296 for (i = 0; i < num_iov; ++i) {
297 if (!TEST_size_t_le(iov[i].buf_len + rd, expected))
300 memcpy(dst_cur, iov[i].buf, iov[i].buf_len);
301 dst_cur += iov[i].buf_len;
302 rd += iov[i].buf_len;
305 if (!TEST_uint64_t_eq(rd, hdr.len))
309 if (!TEST_mem_eq(dst_buf, rd, ref_dst_buf, expected))
314 OPENSSL_free(src_buf);
315 OPENSSL_free(dst_buf);
316 OPENSSL_free(ref_src_buf);
317 OPENSSL_free(ref_dst_buf);
318 ossl_quic_sstream_free(sstream);
322 static const unsigned char simple_data[] =
323 "Hello world! And thank you for all the fish!";
325 static int test_rstream_simple(void)
327 QUIC_RSTREAM *rstream = NULL;
329 unsigned char buf[sizeof(simple_data)];
330 size_t readbytes = 0, avail = 0;
333 if (!TEST_ptr(rstream = ossl_quic_rstream_new(NULL, NULL)))
336 if (!TEST_true(ossl_quic_rstream_queue_data(rstream, NULL, 5,
337 simple_data + 5, 10, 0))
338 || !TEST_true(ossl_quic_rstream_queue_data(rstream, NULL,
339 sizeof(simple_data) - 1,
340 simple_data + sizeof(simple_data) - 1,
342 || !TEST_true(ossl_quic_rstream_peek(rstream, buf, sizeof(buf),
345 || !TEST_size_t_eq(readbytes, 0)
346 || !TEST_true(ossl_quic_rstream_queue_data(rstream, NULL,
347 sizeof(simple_data) - 10,
348 simple_data + sizeof(simple_data) - 10,
350 || !TEST_true(ossl_quic_rstream_queue_data(rstream, NULL, 0,
352 || !TEST_true(ossl_quic_rstream_peek(rstream, buf, sizeof(buf),
355 || !TEST_size_t_eq(readbytes, 1)
356 || !TEST_mem_eq(buf, 1, simple_data, 1)
357 || !TEST_true(ossl_quic_rstream_queue_data(rstream, NULL,
360 || !TEST_true(ossl_quic_rstream_peek(rstream, buf, sizeof(buf),
363 || !TEST_size_t_eq(readbytes, 15)
364 || !TEST_mem_eq(buf, 15, simple_data, 15)
365 || !TEST_true(ossl_quic_rstream_queue_data(rstream, NULL,
368 sizeof(simple_data) - 15, 1))
369 || !TEST_true(ossl_quic_rstream_available(rstream, &avail, &fin))
371 || !TEST_size_t_eq(avail, sizeof(simple_data))
372 || !TEST_true(ossl_quic_rstream_read(rstream, buf, 12,
375 || !TEST_size_t_eq(readbytes, 12)
376 || !TEST_mem_eq(buf, 12, simple_data, 12)
377 || !TEST_true(ossl_quic_rstream_read(rstream, buf + 12, sizeof(buf) - 12,
380 || !TEST_size_t_eq(readbytes, sizeof(buf) - 12)
381 || !TEST_mem_eq(buf, sizeof(buf), simple_data, sizeof(simple_data))
382 || !TEST_true(ossl_quic_rstream_read(rstream, buf, sizeof(buf),
385 || !TEST_size_t_eq(readbytes, 0))
391 ossl_quic_rstream_free(rstream);
395 static int test_rstream_random(int idx)
397 unsigned char *bulk_data = NULL;
398 unsigned char *read_buf = NULL;
399 QUIC_RSTREAM *rstream = NULL;
400 size_t i, read_off, queued_min;
401 const size_t data_size = 10000;
404 size_t readbytes = 0;
406 if (!TEST_ptr(bulk_data = OPENSSL_malloc(data_size))
407 || !TEST_ptr(read_buf = OPENSSL_malloc(data_size))
408 || !TEST_ptr(rstream = ossl_quic_rstream_new(NULL, NULL)))
411 for (i = 0; i < data_size; ++i)
412 bulk_data[i] = (unsigned char)(test_random() & 0xFF);
414 read_off = queued_min = 0;
415 for (r = 0; r < 100; ++r) {
416 for (s = 0; s < 10; ++s) {
417 size_t off = (r * 10 + s) * 10, size = 10;
419 if (test_random() % 5 == 0)
423 if (off <= queued_min && off + size > queued_min)
424 queued_min = off + size;
426 if (!TEST_true(ossl_quic_rstream_queue_data(rstream, NULL, off,
431 if (test_random() % 5 != 0)
434 /* random overlapping retransmit */
435 off = read_off + test_random() % 50;
438 size = test_random() % 100;
439 if (off + size > data_size)
440 off = data_size - size;
441 if (off <= queued_min && off + size > queued_min)
442 queued_min = off + size;
444 if (!TEST_true(ossl_quic_rstream_queue_data(rstream, NULL, off,
449 if (!TEST_true(ossl_quic_rstream_read(rstream, read_buf,
453 if (!TEST_size_t_ge(readbytes, queued_min - read_off)
454 || !TEST_size_t_le(readbytes + read_off, data_size)
455 || !TEST_mem_eq(read_buf, readbytes, bulk_data + read_off,
458 read_off += readbytes;
459 queued_min = read_off;
462 TEST_info("Total read bytes: %zu", read_off);
467 OPENSSL_free(bulk_data);
468 OPENSSL_free(read_buf);
469 ossl_quic_rstream_free(rstream);
474 int setup_tests(void)
476 ADD_TEST(test_sstream_simple);
477 ADD_ALL_TESTS(test_sstream_bulk, 100);
478 ADD_TEST(test_rstream_simple);
479 ADD_ALL_TESTS(test_rstream_random, 100);