quic_cfq_test.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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/packet.h"
  10. #include "internal/quic_cfq.h"
  11. #include "internal/quic_wire.h"
  12. #include "testutil.h"
  13. static const unsigned char ref_buf[] = {
  14. 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19
  15. };
  16. static const uint32_t ref_priority[] = {
  17. 90, 80, 70, 60, 95, 40, 94, 20, 10, 0
  18. };
  19. static const uint32_t ref_pn_space[] = {
  20. QUIC_PN_SPACE_INITIAL,
  21. QUIC_PN_SPACE_HANDSHAKE,
  22. QUIC_PN_SPACE_HANDSHAKE,
  23. QUIC_PN_SPACE_INITIAL,
  24. QUIC_PN_SPACE_INITIAL,
  25. QUIC_PN_SPACE_INITIAL,
  26. QUIC_PN_SPACE_INITIAL,
  27. QUIC_PN_SPACE_INITIAL,
  28. QUIC_PN_SPACE_APP,
  29. QUIC_PN_SPACE_APP,
  30. };
  31. static const uint64_t ref_frame_type[] = {
  32. OSSL_QUIC_FRAME_TYPE_NEW_CONN_ID,
  33. OSSL_QUIC_FRAME_TYPE_NEW_CONN_ID,
  34. OSSL_QUIC_FRAME_TYPE_NEW_CONN_ID,
  35. OSSL_QUIC_FRAME_TYPE_NEW_CONN_ID,
  36. OSSL_QUIC_FRAME_TYPE_NEW_CONN_ID,
  37. OSSL_QUIC_FRAME_TYPE_NEW_CONN_ID,
  38. OSSL_QUIC_FRAME_TYPE_NEW_CONN_ID,
  39. OSSL_QUIC_FRAME_TYPE_NEW_CONN_ID,
  40. OSSL_QUIC_FRAME_TYPE_NEW_CONN_ID,
  41. OSSL_QUIC_FRAME_TYPE_RETIRE_CONN_ID,
  42. };
  43. static const uint32_t expect[QUIC_PN_SPACE_NUM][11] = {
  44. { 4, 6, 0, 3, 5, 7, UINT32_MAX },
  45. { 1, 2, UINT32_MAX },
  46. { 8, 9, UINT32_MAX },
  47. };
  48. static QUIC_CFQ_ITEM *items[QUIC_PN_SPACE_NUM][10];
  49. static unsigned char *g_free;
  50. static size_t g_free_len;
  51. static void free_cb(unsigned char *buf, size_t buf_len, void *arg)
  52. {
  53. g_free = buf;
  54. g_free_len = buf_len;
  55. }
  56. static int check(QUIC_CFQ *cfq)
  57. {
  58. int testresult = 0;
  59. QUIC_CFQ_ITEM *item;
  60. size_t i;
  61. uint32_t pn_space;
  62. for (pn_space = QUIC_PN_SPACE_INITIAL; pn_space < QUIC_PN_SPACE_NUM; ++pn_space)
  63. for (i = 0, item = ossl_quic_cfq_get_priority_head(cfq, pn_space);;
  64. ++i, item = ossl_quic_cfq_item_get_priority_next(item, pn_space)) {
  65. if (expect[pn_space][i] == UINT32_MAX) {
  66. if (!TEST_ptr_null(item))
  67. goto err;
  68. break;
  69. }
  70. items[pn_space][i] = item;
  71. if (!TEST_ptr(item)
  72. || !TEST_ptr_eq(ossl_quic_cfq_item_get_encoded(item),
  73. ref_buf + expect[pn_space][i])
  74. || !TEST_int_eq(ossl_quic_cfq_item_get_pn_space(item), pn_space)
  75. || !TEST_int_eq(ossl_quic_cfq_item_get_state(item),
  76. QUIC_CFQ_STATE_NEW))
  77. goto err;
  78. }
  79. testresult = 1;
  80. err:
  81. return testresult;
  82. }
  83. static int test_cfq(void)
  84. {
  85. int testresult = 0;
  86. QUIC_CFQ *cfq = NULL;
  87. QUIC_CFQ_ITEM *item, *inext;
  88. size_t i;
  89. uint32_t pn_space;
  90. if (!TEST_ptr(cfq = ossl_quic_cfq_new()))
  91. goto err;
  92. g_free = NULL;
  93. g_free_len = 0;
  94. for (i = 0; i < OSSL_NELEM(ref_buf); ++i) {
  95. if (!TEST_ptr(item = ossl_quic_cfq_add_frame(cfq, ref_priority[i],
  96. ref_pn_space[i],
  97. ref_frame_type[i],
  98. ref_buf + i,
  99. 1,
  100. free_cb,
  101. NULL))
  102. || !TEST_int_eq(ossl_quic_cfq_item_get_state(item),
  103. QUIC_CFQ_STATE_NEW)
  104. || !TEST_uint_eq(ossl_quic_cfq_item_get_pn_space(item),
  105. ref_pn_space[i])
  106. || !TEST_uint64_t_eq(ossl_quic_cfq_item_get_frame_type(item),
  107. ref_frame_type[i])
  108. || !TEST_ptr_eq(ossl_quic_cfq_item_get_encoded(item),
  109. ref_buf + i)
  110. || !TEST_size_t_eq(ossl_quic_cfq_item_get_encoded_len(item),
  111. 1))
  112. goto err;
  113. }
  114. if (!check(cfq))
  115. goto err;
  116. for (pn_space = QUIC_PN_SPACE_INITIAL; pn_space < QUIC_PN_SPACE_NUM; ++pn_space)
  117. for (item = ossl_quic_cfq_get_priority_head(cfq, pn_space);
  118. item != NULL; item = inext) {
  119. inext = ossl_quic_cfq_item_get_priority_next(item, pn_space);
  120. ossl_quic_cfq_mark_tx(cfq, item);
  121. }
  122. for (pn_space = QUIC_PN_SPACE_INITIAL; pn_space < QUIC_PN_SPACE_NUM; ++pn_space)
  123. if (!TEST_ptr_null(ossl_quic_cfq_get_priority_head(cfq, pn_space)))
  124. goto err;
  125. for (pn_space = QUIC_PN_SPACE_INITIAL; pn_space < QUIC_PN_SPACE_NUM; ++pn_space)
  126. for (i = 0; i < OSSL_NELEM(items[0]); ++i)
  127. if (items[pn_space][i] != NULL)
  128. ossl_quic_cfq_mark_lost(cfq, items[pn_space][i], UINT32_MAX);
  129. if (!check(cfq))
  130. goto err;
  131. for (pn_space = QUIC_PN_SPACE_INITIAL; pn_space < QUIC_PN_SPACE_NUM; ++pn_space)
  132. for (i = 0; i < OSSL_NELEM(items[0]); ++i)
  133. if (items[pn_space][i] != NULL)
  134. ossl_quic_cfq_release(cfq, items[pn_space][i]);
  135. for (pn_space = QUIC_PN_SPACE_INITIAL; pn_space < QUIC_PN_SPACE_NUM; ++pn_space)
  136. if (!TEST_ptr_null(ossl_quic_cfq_get_priority_head(cfq, pn_space)))
  137. goto err;
  138. testresult = 1;
  139. err:
  140. ossl_quic_cfq_free(cfq);
  141. return testresult;
  142. }
  143. int setup_tests(void)
  144. {
  145. ADD_TEST(test_cfq);
  146. return 1;
  147. }