quic_fc_test.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643
  1. /*
  2. * Copyright 2022-2024 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/quic_fc.h"
  10. #include "internal/quic_error.h"
  11. #include "testutil.h"
  12. static int test_txfc(int is_stream)
  13. {
  14. int testresult = 0;
  15. QUIC_TXFC conn_txfc, stream_txfc, *txfc, *parent_txfc;
  16. if (!TEST_true(ossl_quic_txfc_init(&conn_txfc, 0)))
  17. goto err;
  18. if (is_stream && !TEST_true(ossl_quic_txfc_init(&stream_txfc, &conn_txfc)))
  19. goto err;
  20. txfc = is_stream ? &stream_txfc : &conn_txfc;
  21. parent_txfc = is_stream ? &conn_txfc : NULL;
  22. if (!TEST_true(ossl_quic_txfc_bump_cwm(txfc, 2000)))
  23. goto err;
  24. if (is_stream && !TEST_true(ossl_quic_txfc_bump_cwm(parent_txfc, 2000)))
  25. goto err;
  26. if (!TEST_uint64_t_eq(ossl_quic_txfc_get_swm(txfc), 0))
  27. goto err;
  28. if (!TEST_uint64_t_eq(ossl_quic_txfc_get_cwm(txfc), 2000))
  29. goto err;
  30. if (!TEST_uint64_t_eq(ossl_quic_txfc_get_credit_local(txfc, 0), 2000))
  31. goto err;
  32. if (!TEST_uint64_t_eq(ossl_quic_txfc_get_credit_local(txfc, 100), 1900))
  33. goto err;
  34. if (is_stream) {
  35. if ( !TEST_uint64_t_eq(ossl_quic_txfc_get_credit(txfc, 0), 2000))
  36. goto err;
  37. if ( !TEST_uint64_t_eq(ossl_quic_txfc_get_credit(txfc, 100), 1900))
  38. goto err;
  39. }
  40. if (!TEST_false(ossl_quic_txfc_has_become_blocked(txfc, 0)))
  41. goto err;
  42. if (!TEST_true(ossl_quic_txfc_consume_credit(txfc, 500)))
  43. goto err;
  44. if (!TEST_uint64_t_eq(ossl_quic_txfc_get_credit_local(txfc, 0), 1500))
  45. goto err;
  46. if (is_stream && !TEST_uint64_t_eq(ossl_quic_txfc_get_credit(txfc, 0),
  47. 1500))
  48. goto err;
  49. if (!TEST_false(ossl_quic_txfc_has_become_blocked(txfc, 0)))
  50. goto err;
  51. if (!TEST_uint64_t_eq(ossl_quic_txfc_get_swm(txfc), 500))
  52. goto err;
  53. if (!TEST_true(ossl_quic_txfc_consume_credit(txfc, 100)))
  54. goto err;
  55. if (!TEST_uint64_t_eq(ossl_quic_txfc_get_swm(txfc), 600))
  56. goto err;
  57. if (!TEST_uint64_t_eq(ossl_quic_txfc_get_credit_local(txfc, 0), 1400))
  58. goto err;
  59. if (is_stream && !TEST_uint64_t_eq(ossl_quic_txfc_get_credit(txfc, 0),
  60. 1400))
  61. goto err;
  62. if (!TEST_false(ossl_quic_txfc_has_become_blocked(txfc, 0)))
  63. goto err;
  64. if (!TEST_true(ossl_quic_txfc_consume_credit(txfc, 1400)))
  65. goto err;
  66. if (!TEST_uint64_t_eq(ossl_quic_txfc_get_credit_local(txfc, 0), 0))
  67. goto err;
  68. if (is_stream && !TEST_uint64_t_eq(ossl_quic_txfc_get_credit(txfc, 0),
  69. 0))
  70. goto err;
  71. if (!TEST_uint64_t_eq(ossl_quic_txfc_get_swm(txfc), 2000))
  72. goto err;
  73. if (!TEST_true(ossl_quic_txfc_has_become_blocked(txfc, 0)))
  74. goto err;
  75. if (!TEST_true(ossl_quic_txfc_has_become_blocked(txfc, 0)))
  76. goto err;
  77. if (!TEST_true(ossl_quic_txfc_has_become_blocked(txfc, 1)))
  78. goto err;
  79. if (!TEST_false(ossl_quic_txfc_has_become_blocked(txfc, 0)))
  80. goto err;
  81. if (!TEST_false(ossl_quic_txfc_has_become_blocked(txfc, 0)))
  82. goto err;
  83. if (!TEST_false(ossl_quic_txfc_consume_credit(txfc, 1)))
  84. goto err;
  85. if (!TEST_uint64_t_eq(ossl_quic_txfc_get_cwm(txfc), 2000))
  86. goto err;
  87. if (!TEST_uint64_t_eq(ossl_quic_txfc_get_swm(txfc), 2000))
  88. goto err;
  89. if (!TEST_false(ossl_quic_txfc_bump_cwm(txfc, 2000)))
  90. goto err;
  91. if (!TEST_true(ossl_quic_txfc_bump_cwm(txfc, 2500)))
  92. goto err;
  93. if (is_stream && !TEST_true(ossl_quic_txfc_bump_cwm(parent_txfc, 2400)))
  94. goto err;
  95. if (!TEST_uint64_t_eq(ossl_quic_txfc_get_cwm(txfc), 2500))
  96. goto err;
  97. if (!TEST_uint64_t_eq(ossl_quic_txfc_get_swm(txfc), 2000))
  98. goto err;
  99. if (!TEST_uint64_t_eq(ossl_quic_txfc_get_credit_local(txfc, 0), 500))
  100. goto err;
  101. if (is_stream)
  102. ossl_quic_txfc_has_become_blocked(parent_txfc, 1);
  103. if (is_stream) {
  104. if (!TEST_uint64_t_eq(ossl_quic_txfc_get_credit(txfc, 400), 0))
  105. goto err;
  106. if (!TEST_true(ossl_quic_txfc_consume_credit(txfc, 399)))
  107. goto err;
  108. if (!TEST_false(ossl_quic_txfc_has_become_blocked(txfc, 0)))
  109. goto err;
  110. if (!TEST_uint64_t_eq(ossl_quic_txfc_get_credit(txfc, 0), 1))
  111. goto err;
  112. if (!TEST_true(ossl_quic_txfc_consume_credit(txfc, 1)))
  113. goto err;
  114. if (!TEST_true(ossl_quic_txfc_has_become_blocked(parent_txfc, 0)))
  115. goto err;
  116. if (!TEST_true(ossl_quic_txfc_has_become_blocked(parent_txfc, 1)))
  117. goto err;
  118. if (!TEST_false(ossl_quic_txfc_has_become_blocked(parent_txfc, 0)))
  119. goto err;
  120. } else {
  121. if (!TEST_true(ossl_quic_txfc_consume_credit(txfc, 499)))
  122. goto err;
  123. if (!TEST_false(ossl_quic_txfc_has_become_blocked(txfc, 0)))
  124. goto err;
  125. if (is_stream && !TEST_false(ossl_quic_txfc_has_become_blocked(parent_txfc, 0)))
  126. goto err;
  127. if (!TEST_true(ossl_quic_txfc_consume_credit(txfc, 1)))
  128. goto err;
  129. if (!TEST_true(ossl_quic_txfc_has_become_blocked(txfc, 0)))
  130. goto err;
  131. if (!TEST_true(ossl_quic_txfc_has_become_blocked(txfc, 1)))
  132. goto err;
  133. if (!TEST_false(ossl_quic_txfc_has_become_blocked(txfc, 0)))
  134. goto err;
  135. }
  136. testresult = 1;
  137. err:
  138. return testresult;
  139. }
  140. static OSSL_TIME cur_time;
  141. static OSSL_TIME fake_now(void *arg)
  142. {
  143. return cur_time;
  144. }
  145. #define RX_OPC_END 0
  146. #define RX_OPC_INIT_CONN 1 /* arg0=initial window, arg1=max window */
  147. #define RX_OPC_INIT_STREAM 2 /* arg0=initial window, arg1=max window */
  148. #define RX_OPC_RX 3 /* arg0=end, arg1=is_fin */
  149. #define RX_OPC_RETIRE 4 /* arg0=num_bytes, arg1=rtt in OSSL_TIME ticks, expect_fail */
  150. #define RX_OPC_CHECK_CWM_CONN 5 /* arg0=expected */
  151. #define RX_OPC_CHECK_CWM_STREAM 6 /* arg0=expected */
  152. #define RX_OPC_CHECK_SWM_CONN 7 /* arg0=expected */
  153. #define RX_OPC_CHECK_SWM_STREAM 8 /* arg0=expected */
  154. #define RX_OPC_CHECK_RWM_CONN 9 /* arg0=expected */
  155. #define RX_OPC_CHECK_RWM_STREAM 10 /* arg0=expected */
  156. #define RX_OPC_CHECK_CHANGED_CONN 11 /* arg0=expected, arg1=clear */
  157. #define RX_OPC_CHECK_CHANGED_STREAM 12 /* arg0=expected, arg1=clear */
  158. #define RX_OPC_CHECK_ERROR_CONN 13 /* arg0=expected, arg1=clear */
  159. #define RX_OPC_CHECK_ERROR_STREAM 14 /* arg0=expected, arg1=clear */
  160. #define RX_OPC_STEP_TIME 15 /* arg0=OSSL_TIME ticks to advance */
  161. #define RX_OPC_MSG 16
  162. struct rx_test_op {
  163. unsigned char op;
  164. size_t stream_idx;
  165. uint64_t arg0, arg1;
  166. unsigned char expect_fail;
  167. const char *msg;
  168. };
  169. #define RX_OP_END \
  170. { RX_OPC_END }
  171. #define RX_OP_INIT_CONN(init_window_size, max_window_size) \
  172. { RX_OPC_INIT_CONN, 0, (init_window_size), (max_window_size) },
  173. #define RX_OP_INIT_STREAM(stream_idx, init_window_size, max_window_size) \
  174. { RX_OPC_INIT_STREAM, (stream_idx), (init_window_size), (max_window_size) },
  175. #define RX_OP_RX(stream_idx, end, is_fin) \
  176. { RX_OPC_RX, (stream_idx), (end), (is_fin) },
  177. #define RX_OP_RETIRE(stream_idx, num_bytes, rtt, expect_fail) \
  178. { RX_OPC_RETIRE, (stream_idx), (num_bytes), (rtt), (expect_fail) },
  179. #define RX_OP_CHECK_CWM_CONN(expected) \
  180. { RX_OPC_CHECK_CWM_CONN, 0, (expected) },
  181. #define RX_OP_CHECK_CWM_STREAM(stream_id, expected) \
  182. { RX_OPC_CHECK_CWM_STREAM, (stream_id), (expected) },
  183. #define RX_OP_CHECK_SWM_CONN(expected) \
  184. { RX_OPC_CHECK_SWM_CONN, 0, (expected) },
  185. #define RX_OP_CHECK_SWM_STREAM(stream_id, expected) \
  186. { RX_OPC_CHECK_SWM_STREAM, (stream_id), (expected) },
  187. #define RX_OP_CHECK_RWM_CONN(expected) \
  188. { RX_OPC_CHECK_RWM_CONN, 0, (expected) },
  189. #define RX_OP_CHECK_RWM_STREAM(stream_id, expected) \
  190. { RX_OPC_CHECK_RWM_STREAM, (stream_id), (expected) },
  191. #define RX_OP_CHECK_CHANGED_CONN(expected, clear) \
  192. { RX_OPC_CHECK_CHANGED_CONN, 0, (expected), (clear) },
  193. #define RX_OP_CHECK_CHANGED_STREAM(stream_id, expected, clear) \
  194. { RX_OPC_CHECK_CHANGED_STREAM, (stream_id), (expected), (clear) },
  195. #define RX_OP_CHECK_ERROR_CONN(expected, clear) \
  196. { RX_OPC_CHECK_ERROR_CONN, 0, (expected), (clear) },
  197. #define RX_OP_CHECK_ERROR_STREAM(stream_id, expected, clear) \
  198. { RX_OPC_CHECK_ERROR_STREAM, (stream_id), (expected), (clear) },
  199. #define RX_OP_STEP_TIME(t) \
  200. { RX_OPC_STEP_TIME, 0, (t) },
  201. #define RX_OP_MSG(msg) \
  202. { RX_OPC_MSG, 0, 0, 0, 0, (msg) },
  203. #define RX_OP_INIT(init_window_size, max_window_size) \
  204. RX_OP_INIT_CONN(init_window_size, max_window_size) \
  205. RX_OP_INIT_STREAM(0, init_window_size, max_window_size)
  206. #define RX_OP_CHECK_CWM(expected) \
  207. RX_OP_CHECK_CWM_CONN(expected) \
  208. RX_OP_CHECK_CWM_STREAM(0, expected)
  209. #define RX_OP_CHECK_SWM(expected) \
  210. RX_OP_CHECK_SWM_CONN(expected) \
  211. RX_OP_CHECK_SWM_STREAM(0, expected)
  212. #define RX_OP_CHECK_RWM(expected) \
  213. RX_OP_CHECK_RWM_CONN(expected) \
  214. RX_OP_CHECK_RWM_STREAM(0, expected)
  215. #define RX_OP_CHECK_CHANGED(expected, clear) \
  216. RX_OP_CHECK_CHANGED_CONN(expected, clear) \
  217. RX_OP_CHECK_CHANGED_STREAM(0, expected, clear)
  218. #define RX_OP_CHECK_ERROR(expected, clear) \
  219. RX_OP_CHECK_ERROR_CONN(expected, clear) \
  220. RX_OP_CHECK_ERROR_STREAM(0, expected, clear)
  221. #define INIT_WINDOW_SIZE (1 * 1024 * 1024)
  222. #define INIT_S_WINDOW_SIZE (384 * 1024)
  223. /* 1. Basic RXFC Tests (stream window == connection window) */
  224. static const struct rx_test_op rx_script_1[] = {
  225. RX_OP_STEP_TIME(1000 * OSSL_TIME_MS)
  226. RX_OP_INIT(INIT_WINDOW_SIZE, 10 * INIT_WINDOW_SIZE)
  227. /* Check initial state. */
  228. RX_OP_CHECK_CWM(INIT_WINDOW_SIZE)
  229. RX_OP_CHECK_ERROR(0, 0)
  230. RX_OP_CHECK_CHANGED(0, 0)
  231. /* We cannot retire what we have not received. */
  232. RX_OP_RETIRE(0, 1, 0, 1)
  233. /* Zero bytes is a no-op and always valid. */
  234. RX_OP_RETIRE(0, 0, 0, 0)
  235. /* Consume some window. */
  236. RX_OP_RX(0, 50, 0)
  237. /* CWM has not changed. */
  238. RX_OP_CHECK_CWM(INIT_WINDOW_SIZE)
  239. RX_OP_CHECK_SWM(50)
  240. /* RX, Partial retire */
  241. RX_OP_RX(0, 60, 0)
  242. RX_OP_CHECK_SWM(60)
  243. RX_OP_RETIRE(0, 20, 50 * OSSL_TIME_MS, 0)
  244. RX_OP_CHECK_RWM(20)
  245. RX_OP_CHECK_SWM(60)
  246. RX_OP_CHECK_CWM(INIT_WINDOW_SIZE)
  247. RX_OP_CHECK_CHANGED(0, 0)
  248. RX_OP_CHECK_ERROR(0, 0)
  249. /* Fully retired */
  250. RX_OP_RETIRE(0, 41, 0, 1)
  251. RX_OP_RETIRE(0, 40, 0, 0)
  252. RX_OP_CHECK_SWM(60)
  253. RX_OP_CHECK_RWM(60)
  254. RX_OP_CHECK_CWM(INIT_WINDOW_SIZE)
  255. RX_OP_CHECK_CHANGED(0, 0)
  256. RX_OP_CHECK_ERROR(0, 0)
  257. /* Exhaustion of window - we do not enlarge the window this epoch */
  258. RX_OP_STEP_TIME(201 * OSSL_TIME_MS)
  259. RX_OP_RX(0, INIT_WINDOW_SIZE, 0)
  260. RX_OP_RETIRE(0, INIT_WINDOW_SIZE - 60, 50 * OSSL_TIME_MS, 0)
  261. RX_OP_CHECK_SWM(INIT_WINDOW_SIZE)
  262. RX_OP_CHECK_CHANGED(1, 0)
  263. RX_OP_CHECK_CHANGED(1, 1)
  264. RX_OP_CHECK_CHANGED(0, 0)
  265. RX_OP_CHECK_ERROR(0, 0)
  266. RX_OP_CHECK_CWM(INIT_WINDOW_SIZE * 2)
  267. /* Second epoch - we still do not enlarge the window this epoch */
  268. RX_OP_RX(0, INIT_WINDOW_SIZE + 1, 0)
  269. RX_OP_STEP_TIME(201 * OSSL_TIME_MS)
  270. RX_OP_RX(0, INIT_WINDOW_SIZE * 2, 0)
  271. RX_OP_RETIRE(0, INIT_WINDOW_SIZE, 50 * OSSL_TIME_MS, 0)
  272. RX_OP_CHECK_SWM(INIT_WINDOW_SIZE * 2)
  273. RX_OP_CHECK_CHANGED(1, 0)
  274. RX_OP_CHECK_CHANGED(1, 1)
  275. RX_OP_CHECK_CHANGED(0, 0)
  276. RX_OP_CHECK_ERROR(0, 0)
  277. RX_OP_CHECK_CWM(INIT_WINDOW_SIZE * 3)
  278. /* Third epoch - we enlarge the window */
  279. RX_OP_RX(0, INIT_WINDOW_SIZE * 2 + 1, 0)
  280. RX_OP_STEP_TIME(199 * OSSL_TIME_MS)
  281. RX_OP_RX(0, INIT_WINDOW_SIZE * 3, 0)
  282. RX_OP_RETIRE(0, INIT_WINDOW_SIZE, 50 * OSSL_TIME_MS, 0)
  283. RX_OP_CHECK_SWM(INIT_WINDOW_SIZE * 3)
  284. RX_OP_CHECK_CHANGED(1, 0)
  285. RX_OP_CHECK_CHANGED(1, 1)
  286. RX_OP_CHECK_CHANGED(0, 0)
  287. RX_OP_CHECK_ERROR(0, 0)
  288. RX_OP_CHECK_CWM(INIT_WINDOW_SIZE * 5)
  289. /* Fourth epoch - peer violates flow control */
  290. RX_OP_RX(0, INIT_WINDOW_SIZE * 5 - 5, 0)
  291. RX_OP_STEP_TIME(250 * OSSL_TIME_MS)
  292. RX_OP_RX(0, INIT_WINDOW_SIZE * 5 + 1, 0)
  293. RX_OP_CHECK_SWM(INIT_WINDOW_SIZE * 5)
  294. RX_OP_CHECK_ERROR(OSSL_QUIC_ERR_FLOW_CONTROL_ERROR, 0)
  295. RX_OP_CHECK_ERROR(OSSL_QUIC_ERR_FLOW_CONTROL_ERROR, 1)
  296. RX_OP_CHECK_ERROR(0, 0)
  297. RX_OP_CHECK_CWM(INIT_WINDOW_SIZE * 5)
  298. /*
  299. * No window expansion due to flow control violation; window expansion is
  300. * triggered by retirement only.
  301. */
  302. RX_OP_CHECK_CHANGED(0, 0)
  303. RX_OP_END
  304. };
  305. /* 2. Interaction between connection and stream-level flow control */
  306. static const struct rx_test_op rx_script_2[] = {
  307. RX_OP_STEP_TIME(1000 * OSSL_TIME_MS)
  308. RX_OP_INIT_CONN(INIT_WINDOW_SIZE, 10 * INIT_WINDOW_SIZE)
  309. RX_OP_INIT_STREAM(0, INIT_S_WINDOW_SIZE, 30 * INIT_S_WINDOW_SIZE)
  310. RX_OP_INIT_STREAM(1, INIT_S_WINDOW_SIZE, 30 * INIT_S_WINDOW_SIZE)
  311. RX_OP_RX(0, 10, 0)
  312. RX_OP_CHECK_CWM_CONN(INIT_WINDOW_SIZE)
  313. RX_OP_CHECK_CWM_STREAM(0, INIT_S_WINDOW_SIZE)
  314. RX_OP_CHECK_CWM_STREAM(1, INIT_S_WINDOW_SIZE)
  315. RX_OP_CHECK_SWM_CONN(10)
  316. RX_OP_CHECK_SWM_STREAM(0, 10)
  317. RX_OP_CHECK_SWM_STREAM(1, 0)
  318. RX_OP_CHECK_RWM_CONN(0)
  319. RX_OP_CHECK_RWM_STREAM(0, 0)
  320. RX_OP_CHECK_RWM_STREAM(1, 0)
  321. RX_OP_RX(1, 42, 0)
  322. RX_OP_RX(1, 42, 0) /* monotonic; equal or lower values ignored */
  323. RX_OP_RX(1, 35, 0)
  324. RX_OP_CHECK_CWM_CONN(INIT_WINDOW_SIZE)
  325. RX_OP_CHECK_CWM_STREAM(0, INIT_S_WINDOW_SIZE)
  326. RX_OP_CHECK_CWM_STREAM(1, INIT_S_WINDOW_SIZE)
  327. RX_OP_CHECK_SWM_CONN(52)
  328. RX_OP_CHECK_SWM_STREAM(0, 10)
  329. RX_OP_CHECK_SWM_STREAM(1, 42)
  330. RX_OP_CHECK_RWM_CONN(0)
  331. RX_OP_CHECK_RWM_STREAM(0, 0)
  332. RX_OP_CHECK_RWM_STREAM(1, 0)
  333. RX_OP_RETIRE(0, 10, 50 * OSSL_TIME_MS, 0)
  334. RX_OP_CHECK_RWM_CONN(10)
  335. RX_OP_CHECK_RWM_STREAM(0, 10)
  336. RX_OP_CHECK_CWM_CONN(INIT_WINDOW_SIZE)
  337. RX_OP_CHECK_CWM_STREAM(0, INIT_S_WINDOW_SIZE)
  338. RX_OP_CHECK_CWM_STREAM(1, INIT_S_WINDOW_SIZE)
  339. RX_OP_RETIRE(1, 42, 50 * OSSL_TIME_MS, 0)
  340. RX_OP_CHECK_RWM_CONN(52)
  341. RX_OP_CHECK_RWM_STREAM(1, 42)
  342. RX_OP_CHECK_CWM_CONN(INIT_WINDOW_SIZE)
  343. RX_OP_CHECK_CWM_STREAM(0, INIT_S_WINDOW_SIZE)
  344. RX_OP_CHECK_CWM_STREAM(1, INIT_S_WINDOW_SIZE)
  345. RX_OP_CHECK_CHANGED_CONN(0, 0)
  346. /* FC limited by stream but not connection */
  347. RX_OP_STEP_TIME(1000 * OSSL_TIME_MS)
  348. RX_OP_RX(0, INIT_S_WINDOW_SIZE, 0)
  349. RX_OP_CHECK_SWM_CONN(INIT_S_WINDOW_SIZE + 42)
  350. RX_OP_CHECK_SWM_STREAM(0, INIT_S_WINDOW_SIZE)
  351. RX_OP_CHECK_SWM_STREAM(1, 42)
  352. RX_OP_CHECK_CWM_CONN(INIT_WINDOW_SIZE)
  353. RX_OP_CHECK_CWM_STREAM(0, INIT_S_WINDOW_SIZE)
  354. /* We bump CWM when more than 1/4 of the window has been retired */
  355. RX_OP_RETIRE(0, INIT_S_WINDOW_SIZE - 10, 50 * OSSL_TIME_MS, 0)
  356. RX_OP_CHECK_CWM_STREAM(0, INIT_S_WINDOW_SIZE * 2)
  357. RX_OP_CHECK_CHANGED_STREAM(0, 1, 0)
  358. RX_OP_CHECK_CHANGED_STREAM(0, 1, 1)
  359. RX_OP_CHECK_CHANGED_STREAM(0, 0, 0)
  360. /*
  361. * This is more than 1/4 of the connection window, so CWM will
  362. * be bumped here too.
  363. */
  364. RX_OP_CHECK_CWM_CONN(INIT_S_WINDOW_SIZE + INIT_WINDOW_SIZE + 42)
  365. RX_OP_CHECK_RWM_CONN(INIT_S_WINDOW_SIZE + 42)
  366. RX_OP_CHECK_RWM_STREAM(0, INIT_S_WINDOW_SIZE)
  367. RX_OP_CHECK_RWM_STREAM(1, 42)
  368. RX_OP_CHECK_CHANGED_CONN(1, 0)
  369. RX_OP_CHECK_CHANGED_CONN(1, 1)
  370. RX_OP_CHECK_CHANGED_CONN(0, 0)
  371. RX_OP_CHECK_ERROR_CONN(0, 0)
  372. RX_OP_CHECK_ERROR_STREAM(0, 0, 0)
  373. RX_OP_CHECK_ERROR_STREAM(1, 0, 0)
  374. /* Test exceeding limit at stream level. */
  375. RX_OP_RX(0, INIT_S_WINDOW_SIZE * 2 + 1, 0)
  376. RX_OP_CHECK_ERROR_STREAM(0, OSSL_QUIC_ERR_FLOW_CONTROL_ERROR, 0)
  377. RX_OP_CHECK_ERROR_STREAM(0, OSSL_QUIC_ERR_FLOW_CONTROL_ERROR, 1)
  378. RX_OP_CHECK_ERROR_STREAM(0, 0, 0)
  379. RX_OP_CHECK_ERROR_CONN(0, 0) /* doesn't affect conn */
  380. /* Test exceeding limit at connection level. */
  381. RX_OP_RX(0, INIT_WINDOW_SIZE * 2, 0)
  382. RX_OP_CHECK_ERROR_CONN(OSSL_QUIC_ERR_FLOW_CONTROL_ERROR, 0)
  383. RX_OP_CHECK_ERROR_CONN(OSSL_QUIC_ERR_FLOW_CONTROL_ERROR, 1)
  384. RX_OP_CHECK_ERROR_CONN(0, 0)
  385. RX_OP_END
  386. };
  387. static const struct rx_test_op *rx_scripts[] = {
  388. rx_script_1,
  389. rx_script_2
  390. };
  391. static int run_rxfc_script(const struct rx_test_op *script)
  392. {
  393. #define MAX_STREAMS 3
  394. int testresult = 0;
  395. const struct rx_test_op *op = script;
  396. QUIC_RXFC conn_rxfc = {0}, stream_rxfc[MAX_STREAMS] = {0}; /* coverity */
  397. char stream_init_done[MAX_STREAMS] = {0};
  398. int conn_init_done = 0;
  399. cur_time = ossl_time_zero();
  400. for (; op->op != RX_OPC_END; ++op) {
  401. switch (op->op) {
  402. case RX_OPC_INIT_CONN:
  403. if (!TEST_true(ossl_quic_rxfc_init(&conn_rxfc, 0,
  404. op->arg0, op->arg1,
  405. fake_now, NULL)))
  406. goto err;
  407. conn_init_done = 1;
  408. break;
  409. case RX_OPC_INIT_STREAM:
  410. if (!TEST_size_t_lt(op->stream_idx, OSSL_NELEM(stream_rxfc))
  411. || !TEST_true(conn_init_done))
  412. goto err;
  413. if (!TEST_true(ossl_quic_rxfc_init(&stream_rxfc[op->stream_idx],
  414. &conn_rxfc,
  415. op->arg0, op->arg1,
  416. fake_now, NULL)))
  417. goto err;
  418. stream_init_done[op->stream_idx] = 1;
  419. break;
  420. case RX_OPC_RX:
  421. if (!TEST_true(conn_init_done && op->stream_idx < OSSL_NELEM(stream_rxfc)
  422. && stream_init_done[op->stream_idx]))
  423. goto err;
  424. if (!TEST_true(ossl_quic_rxfc_on_rx_stream_frame(&stream_rxfc[op->stream_idx],
  425. op->arg0,
  426. (int)op->arg1)))
  427. goto err;
  428. break;
  429. case RX_OPC_RETIRE:
  430. if (!TEST_true(conn_init_done && op->stream_idx < OSSL_NELEM(stream_rxfc)
  431. && stream_init_done[op->stream_idx]))
  432. goto err;
  433. if (!TEST_int_eq(ossl_quic_rxfc_on_retire(&stream_rxfc[op->stream_idx],
  434. op->arg0,
  435. ossl_ticks2time(op->arg1)),
  436. !op->expect_fail))
  437. goto err;
  438. break;
  439. case RX_OPC_CHECK_CWM_CONN:
  440. if (!TEST_true(conn_init_done))
  441. goto err;
  442. if (!TEST_uint64_t_eq(ossl_quic_rxfc_get_cwm(&conn_rxfc),
  443. op->arg0))
  444. goto err;
  445. break;
  446. case RX_OPC_CHECK_CWM_STREAM:
  447. if (!TEST_true(op->stream_idx < OSSL_NELEM(stream_rxfc)
  448. && stream_init_done[op->stream_idx]))
  449. goto err;
  450. if (!TEST_uint64_t_eq(ossl_quic_rxfc_get_cwm(&stream_rxfc[op->stream_idx]),
  451. op->arg0))
  452. goto err;
  453. break;
  454. case RX_OPC_CHECK_SWM_CONN:
  455. if (!TEST_true(conn_init_done))
  456. goto err;
  457. if (!TEST_uint64_t_eq(ossl_quic_rxfc_get_swm(&conn_rxfc),
  458. op->arg0))
  459. goto err;
  460. break;
  461. case RX_OPC_CHECK_SWM_STREAM:
  462. if (!TEST_true(op->stream_idx < OSSL_NELEM(stream_rxfc)
  463. && stream_init_done[op->stream_idx]))
  464. goto err;
  465. if (!TEST_uint64_t_eq(ossl_quic_rxfc_get_swm(&stream_rxfc[op->stream_idx]),
  466. op->arg0))
  467. goto err;
  468. break;
  469. case RX_OPC_CHECK_RWM_CONN:
  470. if (!TEST_true(conn_init_done))
  471. goto err;
  472. if (!TEST_uint64_t_eq(ossl_quic_rxfc_get_rwm(&conn_rxfc),
  473. op->arg0))
  474. goto err;
  475. break;
  476. case RX_OPC_CHECK_RWM_STREAM:
  477. if (!TEST_true(op->stream_idx < OSSL_NELEM(stream_rxfc)
  478. && stream_init_done[op->stream_idx]))
  479. goto err;
  480. if (!TEST_uint64_t_eq(ossl_quic_rxfc_get_rwm(&stream_rxfc[op->stream_idx]),
  481. op->arg0))
  482. goto err;
  483. break;
  484. case RX_OPC_CHECK_CHANGED_CONN:
  485. if (!TEST_true(conn_init_done))
  486. goto err;
  487. if (!TEST_int_eq(ossl_quic_rxfc_has_cwm_changed(&conn_rxfc,
  488. (int)op->arg1),
  489. (int)op->arg0))
  490. goto err;
  491. break;
  492. case RX_OPC_CHECK_CHANGED_STREAM:
  493. if (!TEST_true(op->stream_idx < OSSL_NELEM(stream_rxfc)
  494. && stream_init_done[op->stream_idx]))
  495. goto err;
  496. if (!TEST_int_eq(ossl_quic_rxfc_has_cwm_changed(&stream_rxfc[op->stream_idx],
  497. (int)op->arg1),
  498. (int)op->arg0))
  499. goto err;
  500. break;
  501. case RX_OPC_CHECK_ERROR_CONN:
  502. if (!TEST_true(conn_init_done))
  503. goto err;
  504. if (!TEST_int_eq(ossl_quic_rxfc_get_error(&conn_rxfc,
  505. (int)op->arg1),
  506. (int)op->arg0))
  507. goto err;
  508. break;
  509. case RX_OPC_CHECK_ERROR_STREAM:
  510. if (!TEST_true(op->stream_idx < OSSL_NELEM(stream_rxfc)
  511. && stream_init_done[op->stream_idx]))
  512. goto err;
  513. if (!TEST_int_eq(ossl_quic_rxfc_get_error(&stream_rxfc[op->stream_idx],
  514. (int)op->arg1),
  515. (int)op->arg0))
  516. goto err;
  517. break;
  518. case RX_OPC_STEP_TIME:
  519. cur_time = ossl_time_add(cur_time, ossl_ticks2time(op->arg0));
  520. break;
  521. case RX_OPC_MSG:
  522. fprintf(stderr, "# %s\n", op->msg);
  523. break;
  524. default:
  525. goto err;
  526. }
  527. }
  528. testresult = 1;
  529. err:
  530. return testresult;
  531. }
  532. static int test_rxfc(int idx)
  533. {
  534. return run_rxfc_script(rx_scripts[idx]);
  535. }
  536. int setup_tests(void)
  537. {
  538. ADD_ALL_TESTS(test_txfc, 2);
  539. ADD_ALL_TESTS(test_rxfc, OSSL_NELEM(rx_scripts));
  540. return 1;
  541. }