quic_fc_test.c 22 KB

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