quic_stream_test.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605
  1. /*
  2. * Copyright 2022-2023 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  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
  8. */
  9. #include "internal/packet.h"
  10. #include "internal/quic_stream.h"
  11. #include "testutil.h"
  12. static int compare_iov(const unsigned char *ref, size_t ref_len,
  13. const OSSL_QTX_IOVEC *iov, size_t iov_len)
  14. {
  15. size_t i, total_len = 0;
  16. const unsigned char *cur = ref;
  17. for (i = 0; i < iov_len; ++i)
  18. total_len += iov[i].buf_len;
  19. if (ref_len != total_len)
  20. return 0;
  21. for (i = 0; i < iov_len; ++i) {
  22. if (memcmp(cur, iov[i].buf, iov[i].buf_len))
  23. return 0;
  24. cur += iov[i].buf_len;
  25. }
  26. return 1;
  27. }
  28. static const unsigned char data_1[] = {
  29. 0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59,
  30. 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f
  31. };
  32. static int test_sstream_simple(void)
  33. {
  34. int testresult = 0;
  35. QUIC_SSTREAM *sstream = NULL;
  36. OSSL_QUIC_FRAME_STREAM hdr;
  37. OSSL_QTX_IOVEC iov[2];
  38. size_t num_iov = 0, wr = 0, i, init_size = 8192;
  39. if (!TEST_ptr(sstream = ossl_quic_sstream_new(init_size)))
  40. goto err;
  41. /* A stream with nothing yet appended is totally acked */
  42. if (!TEST_true(ossl_quic_sstream_is_totally_acked(sstream)))
  43. goto err;
  44. /* Should not have any data yet */
  45. num_iov = OSSL_NELEM(iov);
  46. if (!TEST_false(ossl_quic_sstream_get_stream_frame(sstream, 0, &hdr, iov,
  47. &num_iov)))
  48. goto err;
  49. /* Append data */
  50. if (!TEST_true(ossl_quic_sstream_append(sstream, data_1, sizeof(data_1),
  51. &wr))
  52. || !TEST_size_t_eq(wr, sizeof(data_1)))
  53. goto err;
  54. /* No longer totally acked */
  55. if (!TEST_false(ossl_quic_sstream_is_totally_acked(sstream)))
  56. goto err;
  57. /* Read data */
  58. num_iov = OSSL_NELEM(iov);
  59. if (!TEST_true(ossl_quic_sstream_get_stream_frame(sstream, 0, &hdr, iov,
  60. &num_iov))
  61. || !TEST_size_t_gt(num_iov, 0)
  62. || !TEST_uint64_t_eq(hdr.offset, 0)
  63. || !TEST_uint64_t_eq(hdr.len, sizeof(data_1))
  64. || !TEST_false(hdr.is_fin))
  65. goto err;
  66. if (!TEST_true(compare_iov(data_1, sizeof(data_1), iov, num_iov)))
  67. goto err;
  68. /* Mark data as half transmitted */
  69. if (!TEST_true(ossl_quic_sstream_mark_transmitted(sstream, 0, 7)))
  70. goto err;
  71. /* Read data */
  72. num_iov = OSSL_NELEM(iov);
  73. if (!TEST_true(ossl_quic_sstream_get_stream_frame(sstream, 0, &hdr, iov,
  74. &num_iov))
  75. || !TEST_size_t_gt(num_iov, 0)
  76. || !TEST_uint64_t_eq(hdr.offset, 8)
  77. || !TEST_uint64_t_eq(hdr.len, sizeof(data_1) - 8)
  78. || !TEST_false(hdr.is_fin))
  79. goto err;
  80. if (!TEST_true(compare_iov(data_1 + 8, sizeof(data_1) - 8, iov, num_iov)))
  81. goto err;
  82. if (!TEST_true(ossl_quic_sstream_mark_transmitted(sstream, 8, 15)))
  83. goto err;
  84. /* Read more data; should not be any more */
  85. num_iov = OSSL_NELEM(iov);
  86. if (!TEST_false(ossl_quic_sstream_get_stream_frame(sstream, 0, &hdr, iov,
  87. &num_iov)))
  88. goto err;
  89. /* Now we have lost bytes 4-6 */
  90. if (!TEST_true(ossl_quic_sstream_mark_lost(sstream, 4, 6)))
  91. goto err;
  92. /* Should be able to read them */
  93. num_iov = OSSL_NELEM(iov);
  94. if (!TEST_true(ossl_quic_sstream_get_stream_frame(sstream, 0, &hdr, iov,
  95. &num_iov))
  96. || !TEST_size_t_gt(num_iov, 0)
  97. || !TEST_uint64_t_eq(hdr.offset, 4)
  98. || !TEST_uint64_t_eq(hdr.len, 3)
  99. || !TEST_false(hdr.is_fin))
  100. goto err;
  101. if (!TEST_true(compare_iov(data_1 + 4, 3, iov, num_iov)))
  102. goto err;
  103. /* Retransmit */
  104. if (!TEST_true(ossl_quic_sstream_mark_transmitted(sstream, 4, 6)))
  105. goto err;
  106. /* Read more data; should not be any more */
  107. num_iov = OSSL_NELEM(iov);
  108. if (!TEST_false(ossl_quic_sstream_get_stream_frame(sstream, 0, &hdr, iov,
  109. &num_iov)))
  110. goto err;
  111. if (!TEST_size_t_eq(ossl_quic_sstream_get_buffer_used(sstream), 16))
  112. goto err;
  113. /* Data has been acknowledged, space should be not be freed yet */
  114. if (!TEST_true(ossl_quic_sstream_mark_acked(sstream, 1, 7))
  115. || !TEST_size_t_eq(ossl_quic_sstream_get_buffer_used(sstream), 16))
  116. goto err;
  117. /* Now data should be freed */
  118. if (!TEST_true(ossl_quic_sstream_mark_acked(sstream, 0, 0))
  119. || !TEST_size_t_eq(ossl_quic_sstream_get_buffer_used(sstream), 8))
  120. goto err;
  121. if (!TEST_true(ossl_quic_sstream_mark_acked(sstream, 0, 15))
  122. || !TEST_size_t_eq(ossl_quic_sstream_get_buffer_used(sstream), 0))
  123. goto err;
  124. /* Now FIN */
  125. ossl_quic_sstream_fin(sstream);
  126. /* Get FIN frame */
  127. for (i = 0; i < 2; ++i) {
  128. num_iov = OSSL_NELEM(iov);
  129. if (!TEST_true(ossl_quic_sstream_get_stream_frame(sstream, 0, &hdr, iov,
  130. &num_iov))
  131. || !TEST_uint64_t_eq(hdr.offset, 16)
  132. || !TEST_uint64_t_eq(hdr.len, 0)
  133. || !TEST_true(hdr.is_fin)
  134. || !TEST_size_t_eq(num_iov, 0))
  135. goto err;
  136. }
  137. if (!TEST_true(ossl_quic_sstream_mark_transmitted_fin(sstream, 16)))
  138. goto err;
  139. /* Read more data; FIN should not be returned any more */
  140. num_iov = OSSL_NELEM(iov);
  141. if (!TEST_false(ossl_quic_sstream_get_stream_frame(sstream, 0, &hdr, iov,
  142. &num_iov)))
  143. goto err;
  144. /* Lose FIN frame */
  145. if (!TEST_true(ossl_quic_sstream_mark_lost_fin(sstream)))
  146. goto err;
  147. /* Get FIN frame */
  148. for (i = 0; i < 2; ++i) {
  149. num_iov = OSSL_NELEM(iov);
  150. if (!TEST_true(ossl_quic_sstream_get_stream_frame(sstream, 0, &hdr, iov,
  151. &num_iov))
  152. || !TEST_uint64_t_eq(hdr.offset, 16)
  153. || !TEST_uint64_t_eq(hdr.len, 0)
  154. || !TEST_true(hdr.is_fin)
  155. || !TEST_size_t_eq(num_iov, 0))
  156. goto err;
  157. }
  158. if (!TEST_true(ossl_quic_sstream_mark_transmitted_fin(sstream, 16)))
  159. goto err;
  160. /* Read more data; FIN should not be returned any more */
  161. num_iov = OSSL_NELEM(iov);
  162. if (!TEST_false(ossl_quic_sstream_get_stream_frame(sstream, 0, &hdr, iov,
  163. &num_iov)))
  164. goto err;
  165. /* Acknowledge fin. */
  166. if (!TEST_true(ossl_quic_sstream_mark_acked_fin(sstream)))
  167. goto err;
  168. if (!TEST_true(ossl_quic_sstream_is_totally_acked(sstream)))
  169. goto err;
  170. testresult = 1;
  171. err:
  172. ossl_quic_sstream_free(sstream);
  173. return testresult;
  174. }
  175. static int test_sstream_bulk(int idx)
  176. {
  177. int testresult = 0;
  178. QUIC_SSTREAM *sstream = NULL;
  179. OSSL_QUIC_FRAME_STREAM hdr;
  180. OSSL_QTX_IOVEC iov[2];
  181. size_t i, j, num_iov = 0, init_size = 8192, l;
  182. size_t consumed = 0, total_written = 0, rd, cur_rd, expected = 0, start_at;
  183. unsigned char *src_buf = NULL, *dst_buf = NULL;
  184. unsigned char *ref_src_buf = NULL, *ref_dst_buf = NULL;
  185. unsigned char *ref_dst_cur, *ref_src_cur, *dst_cur;
  186. if (!TEST_ptr(sstream = ossl_quic_sstream_new(init_size)))
  187. goto err;
  188. if (!TEST_size_t_eq(ossl_quic_sstream_get_buffer_size(sstream), init_size))
  189. goto err;
  190. if (!TEST_ptr(src_buf = OPENSSL_zalloc(init_size)))
  191. goto err;
  192. if (!TEST_ptr(dst_buf = OPENSSL_malloc(init_size)))
  193. goto err;
  194. if (!TEST_ptr(ref_src_buf = OPENSSL_malloc(init_size)))
  195. goto err;
  196. if (!TEST_ptr(ref_dst_buf = OPENSSL_malloc(init_size)))
  197. goto err;
  198. /*
  199. * Append a preliminary buffer to allow later code to exercise wraparound.
  200. */
  201. if (!TEST_true(ossl_quic_sstream_append(sstream, src_buf, init_size / 2,
  202. &consumed))
  203. || !TEST_size_t_eq(consumed, init_size / 2)
  204. || !TEST_true(ossl_quic_sstream_mark_transmitted(sstream, 0,
  205. init_size / 2 - 1))
  206. || !TEST_true(ossl_quic_sstream_mark_acked(sstream, 0,
  207. init_size / 2 - 1)))
  208. goto err;
  209. start_at = init_size / 2;
  210. /* Generate a random buffer. */
  211. for (i = 0; i < init_size; ++i)
  212. src_buf[i] = (unsigned char)(test_random() & 0xFF);
  213. /* Append bytes into the buffer in chunks of random length. */
  214. ref_src_cur = ref_src_buf;
  215. do {
  216. l = (test_random() % init_size) + 1;
  217. if (!TEST_true(ossl_quic_sstream_append(sstream, src_buf, l, &consumed)))
  218. goto err;
  219. memcpy(ref_src_cur, src_buf, consumed);
  220. ref_src_cur += consumed;
  221. total_written += consumed;
  222. } while (consumed > 0);
  223. if (!TEST_size_t_eq(ossl_quic_sstream_get_buffer_used(sstream), init_size)
  224. || !TEST_size_t_eq(ossl_quic_sstream_get_buffer_avail(sstream), 0))
  225. goto err;
  226. /*
  227. * Randomly select bytes out of the buffer by marking them as transmitted.
  228. * Record the remaining bytes, which should be the sequence of bytes
  229. * returned.
  230. */
  231. ref_src_cur = ref_src_buf;
  232. ref_dst_cur = ref_dst_buf;
  233. for (i = 0; i < total_written; ++i) {
  234. if ((test_random() & 1) != 0) {
  235. *ref_dst_cur++ = *ref_src_cur;
  236. ++expected;
  237. } else if (!TEST_true(ossl_quic_sstream_mark_transmitted(sstream,
  238. start_at + i,
  239. start_at + i)))
  240. goto err;
  241. ++ref_src_cur;
  242. }
  243. /* Exercise resize. */
  244. if (!TEST_true(ossl_quic_sstream_set_buffer_size(sstream, init_size * 2))
  245. || !TEST_true(ossl_quic_sstream_set_buffer_size(sstream, init_size)))
  246. goto err;
  247. /* Readout and verification. */
  248. dst_cur = dst_buf;
  249. for (i = 0, rd = 0; rd < expected; ++i) {
  250. num_iov = OSSL_NELEM(iov);
  251. if (!TEST_true(ossl_quic_sstream_get_stream_frame(sstream, i, &hdr, iov,
  252. &num_iov)))
  253. goto err;
  254. cur_rd = 0;
  255. for (j = 0; j < num_iov; ++j) {
  256. if (!TEST_size_t_le(iov[j].buf_len + rd, expected))
  257. goto err;
  258. memcpy(dst_cur, iov[j].buf, iov[j].buf_len);
  259. dst_cur += iov[j].buf_len;
  260. cur_rd += iov[j].buf_len;
  261. }
  262. if (!TEST_uint64_t_eq(cur_rd, hdr.len))
  263. goto err;
  264. rd += cur_rd;
  265. }
  266. if (!TEST_mem_eq(dst_buf, rd, ref_dst_buf, expected))
  267. goto err;
  268. testresult = 1;
  269. err:
  270. OPENSSL_free(src_buf);
  271. OPENSSL_free(dst_buf);
  272. OPENSSL_free(ref_src_buf);
  273. OPENSSL_free(ref_dst_buf);
  274. ossl_quic_sstream_free(sstream);
  275. return testresult;
  276. }
  277. static int test_single_copy_read(QUIC_RSTREAM *qrs,
  278. unsigned char *buf, size_t size,
  279. size_t *readbytes, int *fin)
  280. {
  281. const unsigned char *record;
  282. size_t rec_len;
  283. *readbytes = 0;
  284. for (;;) {
  285. if (!ossl_quic_rstream_get_record(qrs, &record, &rec_len, fin))
  286. return 0;
  287. if (rec_len == 0)
  288. break;
  289. if (rec_len > size) {
  290. rec_len = size;
  291. *fin = 0;
  292. }
  293. memcpy(buf, record, rec_len);
  294. size -= rec_len;
  295. *readbytes += rec_len;
  296. buf += rec_len;
  297. if (!ossl_quic_rstream_release_record(qrs, rec_len))
  298. return 0;
  299. if (*fin || size == 0)
  300. break;
  301. }
  302. return 1;
  303. }
  304. static const unsigned char simple_data[] =
  305. "Hello world! And thank you for all the fish!";
  306. static int test_rstream_simple(int idx)
  307. {
  308. QUIC_RSTREAM *rstream = NULL;
  309. int ret = 0;
  310. unsigned char buf[sizeof(simple_data)];
  311. size_t readbytes = 0, avail = 0;
  312. int fin = 0;
  313. int use_rbuf = idx > 1;
  314. int use_sc = idx % 2;
  315. int (* read_fn)(QUIC_RSTREAM *, unsigned char *, size_t, size_t *,
  316. int *) = use_sc ? test_single_copy_read
  317. : ossl_quic_rstream_read;
  318. if (!TEST_ptr(rstream = ossl_quic_rstream_new(NULL, NULL, 0)))
  319. goto err;
  320. if (!TEST_true(ossl_quic_rstream_queue_data(rstream, NULL, 5,
  321. simple_data + 5, 10, 0))
  322. || !TEST_true(ossl_quic_rstream_queue_data(rstream, NULL,
  323. sizeof(simple_data) - 1,
  324. simple_data + sizeof(simple_data) - 1,
  325. 1, 1))
  326. || !TEST_true(ossl_quic_rstream_peek(rstream, buf, sizeof(buf),
  327. &readbytes, &fin))
  328. || !TEST_false(fin)
  329. || !TEST_size_t_eq(readbytes, 0)
  330. || !TEST_true(ossl_quic_rstream_queue_data(rstream, NULL,
  331. sizeof(simple_data) - 10,
  332. simple_data + sizeof(simple_data) - 10,
  333. 10, 1))
  334. || !TEST_true(ossl_quic_rstream_queue_data(rstream, NULL, 0,
  335. simple_data, 1, 0))
  336. || !TEST_true(ossl_quic_rstream_peek(rstream, buf, sizeof(buf),
  337. &readbytes, &fin))
  338. || !TEST_false(fin)
  339. || !TEST_size_t_eq(readbytes, 1)
  340. || !TEST_mem_eq(buf, 1, simple_data, 1)
  341. || (use_rbuf && !TEST_false(ossl_quic_rstream_move_to_rbuf(rstream)))
  342. || (use_rbuf
  343. && !TEST_true(ossl_quic_rstream_resize_rbuf(rstream,
  344. sizeof(simple_data))))
  345. || (use_rbuf && !TEST_true(ossl_quic_rstream_move_to_rbuf(rstream)))
  346. || !TEST_true(ossl_quic_rstream_queue_data(rstream, NULL,
  347. 0, simple_data,
  348. 10, 0))
  349. || !TEST_true(ossl_quic_rstream_queue_data(rstream, NULL,
  350. sizeof(simple_data),
  351. NULL,
  352. 0, 1))
  353. || !TEST_true(ossl_quic_rstream_peek(rstream, buf, sizeof(buf),
  354. &readbytes, &fin))
  355. || !TEST_false(fin)
  356. || !TEST_size_t_eq(readbytes, 15)
  357. || !TEST_mem_eq(buf, 15, simple_data, 15)
  358. || !TEST_true(ossl_quic_rstream_queue_data(rstream, NULL,
  359. 15,
  360. simple_data + 15,
  361. sizeof(simple_data) - 15, 1))
  362. || !TEST_true(ossl_quic_rstream_available(rstream, &avail, &fin))
  363. || !TEST_true(fin)
  364. || !TEST_size_t_eq(avail, sizeof(simple_data))
  365. || !TEST_true(read_fn(rstream, buf, 2, &readbytes, &fin))
  366. || !TEST_false(fin)
  367. || !TEST_size_t_eq(readbytes, 2)
  368. || !TEST_mem_eq(buf, 2, simple_data, 2)
  369. || !TEST_true(read_fn(rstream, buf + 2, 12, &readbytes, &fin))
  370. || !TEST_false(fin)
  371. || !TEST_size_t_eq(readbytes, 12)
  372. || !TEST_mem_eq(buf + 2, 12, simple_data + 2, 12)
  373. || !TEST_true(ossl_quic_rstream_queue_data(rstream, NULL,
  374. sizeof(simple_data),
  375. NULL,
  376. 0, 1))
  377. || (use_rbuf
  378. && !TEST_true(ossl_quic_rstream_resize_rbuf(rstream,
  379. 2 * sizeof(simple_data))))
  380. || (use_rbuf && !TEST_true(ossl_quic_rstream_move_to_rbuf(rstream)))
  381. || !TEST_true(read_fn(rstream, buf + 14, 5, &readbytes, &fin))
  382. || !TEST_false(fin)
  383. || !TEST_size_t_eq(readbytes, 5)
  384. || !TEST_mem_eq(buf, 14 + 5, simple_data, 14 + 5)
  385. || !TEST_true(read_fn(rstream, buf + 14 + 5, sizeof(buf) - 14 - 5,
  386. &readbytes, &fin))
  387. || !TEST_true(fin)
  388. || !TEST_size_t_eq(readbytes, sizeof(buf) - 14 - 5)
  389. || !TEST_mem_eq(buf, sizeof(buf), simple_data, sizeof(simple_data))
  390. || (use_rbuf && !TEST_true(ossl_quic_rstream_move_to_rbuf(rstream)))
  391. || !TEST_true(read_fn(rstream, buf, sizeof(buf), &readbytes, &fin))
  392. || !TEST_true(fin)
  393. || !TEST_size_t_eq(readbytes, 0))
  394. goto err;
  395. ret = 1;
  396. err:
  397. ossl_quic_rstream_free(rstream);
  398. return ret;
  399. }
  400. static int test_rstream_random(int idx)
  401. {
  402. unsigned char *bulk_data = NULL;
  403. unsigned char *read_buf = NULL;
  404. QUIC_RSTREAM *rstream = NULL;
  405. size_t i, read_off, queued_min, queued_max;
  406. const size_t data_size = 10000;
  407. int r, s, fin = 0, fin_set = 0;
  408. int ret = 0;
  409. size_t readbytes = 0;
  410. if (!TEST_ptr(bulk_data = OPENSSL_malloc(data_size))
  411. || !TEST_ptr(read_buf = OPENSSL_malloc(data_size))
  412. || !TEST_ptr(rstream = ossl_quic_rstream_new(NULL, NULL, 0)))
  413. goto err;
  414. if (idx % 3 == 0)
  415. ossl_quic_rstream_set_cleanse(rstream, 1);
  416. for (i = 0; i < data_size; ++i)
  417. bulk_data[i] = (unsigned char)(test_random() & 0xFF);
  418. read_off = queued_min = queued_max = 0;
  419. for (r = 0; r < 100; ++r) {
  420. for (s = 0; s < 10; ++s) {
  421. size_t off = (r * 10 + s) * 10, size = 10;
  422. if (test_random() % 10 == 0)
  423. /* drop packet */
  424. continue;
  425. if (off <= queued_min && off + size > queued_min)
  426. queued_min = off + size;
  427. if (!TEST_true(ossl_quic_rstream_queue_data(rstream, NULL, off,
  428. bulk_data + off,
  429. size, 0)))
  430. goto err;
  431. if (queued_max < off + size)
  432. queued_max = off + size;
  433. if (test_random() % 5 != 0)
  434. continue;
  435. /* random overlapping retransmit */
  436. off = read_off + test_random() % 50;
  437. if (off > 50)
  438. off -= 50;
  439. size = test_random() % 100 + 1;
  440. if (off + size > data_size)
  441. off = data_size - size;
  442. if (off <= queued_min && off + size > queued_min)
  443. queued_min = off + size;
  444. if (!TEST_true(ossl_quic_rstream_queue_data(rstream, NULL, off,
  445. bulk_data + off,
  446. size, 0)))
  447. goto err;
  448. if (queued_max < off + size)
  449. queued_max = off + size;
  450. }
  451. if (idx % 2 == 0) {
  452. if (!TEST_true(test_single_copy_read(rstream, read_buf, data_size,
  453. &readbytes, &fin)))
  454. goto err;
  455. } else if (!TEST_true(ossl_quic_rstream_read(rstream, read_buf,
  456. data_size,
  457. &readbytes, &fin))) {
  458. goto err;
  459. }
  460. if (!TEST_size_t_ge(readbytes, queued_min - read_off)
  461. || !TEST_size_t_le(readbytes + read_off, data_size)
  462. || (idx % 3 != 0
  463. && !TEST_mem_eq(read_buf, readbytes, bulk_data + read_off,
  464. readbytes)))
  465. goto err;
  466. read_off += readbytes;
  467. queued_min = read_off;
  468. if (test_random() % 50 == 0)
  469. if (!TEST_true(ossl_quic_rstream_resize_rbuf(rstream,
  470. queued_max - read_off + 1))
  471. || !TEST_true(ossl_quic_rstream_move_to_rbuf(rstream)))
  472. goto err;
  473. if (!fin_set && queued_max >= data_size - test_random() % 200) {
  474. fin_set = 1;
  475. /* Queue empty fin frame */
  476. if (!TEST_true(ossl_quic_rstream_queue_data(rstream, NULL, data_size,
  477. NULL, 0, 1)))
  478. goto err;
  479. }
  480. }
  481. TEST_info("Total read bytes: %zu Fin rcvd: %d", read_off, fin);
  482. if (idx % 3 == 0)
  483. for (i = 0; i < read_off; i++)
  484. if (!TEST_uchar_eq(bulk_data[i], 0))
  485. goto err;
  486. if (read_off == data_size && fin_set && !fin) {
  487. /* We might still receive the final empty frame */
  488. if (idx % 2 == 0) {
  489. if (!TEST_true(test_single_copy_read(rstream, read_buf, data_size,
  490. &readbytes, &fin)))
  491. goto err;
  492. } else if (!TEST_true(ossl_quic_rstream_read(rstream, read_buf,
  493. data_size,
  494. &readbytes, &fin))) {
  495. goto err;
  496. }
  497. if (!TEST_size_t_eq(readbytes, 0) || !TEST_true(fin))
  498. goto err;
  499. }
  500. ret = 1;
  501. err:
  502. ossl_quic_rstream_free(rstream);
  503. OPENSSL_free(bulk_data);
  504. OPENSSL_free(read_buf);
  505. return ret;
  506. }
  507. int setup_tests(void)
  508. {
  509. ADD_TEST(test_sstream_simple);
  510. ADD_ALL_TESTS(test_sstream_bulk, 100);
  511. ADD_ALL_TESTS(test_rstream_simple, 4);
  512. ADD_ALL_TESTS(test_rstream_random, 100);
  513. return 1;
  514. }