quic_rcidm_test.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. /*
  2. * Copyright 2023-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_rcidm.h"
  10. #include "testutil.h"
  11. static const QUIC_CONN_ID cid8_1 = { 8, { 1 } };
  12. static const QUIC_CONN_ID cid8_2 = { 8, { 2 } };
  13. static const QUIC_CONN_ID cid8_3 = { 8, { 3 } };
  14. static const QUIC_CONN_ID cid8_4 = { 8, { 4 } };
  15. static const QUIC_CONN_ID cid8_5 = { 8, { 5 } };
  16. /*
  17. * 0: Client, Initial ODCID
  18. * 1: Client, Initial ODCID + Retry ODCID
  19. * 2: Server, doesn't start with Initial ODCID
  20. */
  21. static int test_rcidm(int idx)
  22. {
  23. int testresult = 0;
  24. QUIC_RCIDM *rcidm;
  25. OSSL_QUIC_FRAME_NEW_CONN_ID ncid_frame_1 = {0}, ncid_frame_2 = {0};
  26. QUIC_CONN_ID dcid_out;
  27. const QUIC_CONN_ID *odcid = NULL;
  28. uint64_t seq_num_out;
  29. ncid_frame_1.seq_num = 2;
  30. ncid_frame_1.conn_id.id_len = 8;
  31. ncid_frame_1.conn_id.id[0] = 3;
  32. ncid_frame_2.seq_num = 3;
  33. ncid_frame_2.conn_id.id_len = 8;
  34. ncid_frame_2.conn_id.id[0] = 4;
  35. odcid = ((idx == 2) ? NULL : &cid8_1);
  36. if (!TEST_ptr(rcidm = ossl_quic_rcidm_new(odcid)))
  37. goto err;
  38. if (idx != 2) {
  39. if (/* ODCID not counted */
  40. !TEST_true(ossl_quic_rcidm_get_preferred_tx_dcid_changed(rcidm, 1))
  41. || !TEST_false(ossl_quic_rcidm_get_preferred_tx_dcid_changed(rcidm, 0))
  42. || !TEST_true(ossl_quic_rcidm_get_preferred_tx_dcid(rcidm, &dcid_out))
  43. || !TEST_true(ossl_quic_conn_id_eq(&dcid_out, &cid8_1))
  44. || !TEST_size_t_eq(ossl_quic_rcidm_get_num_active(rcidm), 0))
  45. goto err;
  46. } else {
  47. if (!TEST_false(ossl_quic_rcidm_get_preferred_tx_dcid(rcidm, &dcid_out))
  48. || !TEST_size_t_eq(ossl_quic_rcidm_get_num_active(rcidm), 0))
  49. goto err;
  50. }
  51. if (idx == 1) {
  52. if (!TEST_true(ossl_quic_rcidm_add_from_server_retry(rcidm, &cid8_5))
  53. || !TEST_true(ossl_quic_rcidm_get_preferred_tx_dcid_changed(rcidm, 1))
  54. || !TEST_false(ossl_quic_rcidm_get_preferred_tx_dcid_changed(rcidm, 0))
  55. || !TEST_true(ossl_quic_rcidm_get_preferred_tx_dcid(rcidm, &dcid_out))
  56. || !TEST_true(ossl_quic_conn_id_eq(&dcid_out, &cid8_5))
  57. || !TEST_size_t_eq(ossl_quic_rcidm_get_num_active(rcidm), 0))
  58. goto err;
  59. }
  60. if (!TEST_true(ossl_quic_rcidm_add_from_initial(rcidm, &cid8_2))
  61. /* Initial SCID (seq=0) is counted */
  62. || !TEST_size_t_eq(ossl_quic_rcidm_get_num_active(rcidm), 1)
  63. || !TEST_true(ossl_quic_rcidm_get_preferred_tx_dcid_changed(rcidm, 1))
  64. || !TEST_false(ossl_quic_rcidm_get_preferred_tx_dcid_changed(rcidm, 0))
  65. || !TEST_true(ossl_quic_rcidm_get_preferred_tx_dcid(rcidm, &dcid_out))
  66. || !TEST_true(ossl_quic_conn_id_eq(&dcid_out, &cid8_2))
  67. || !TEST_true(ossl_quic_rcidm_add_from_ncid(rcidm, &ncid_frame_1))
  68. || !TEST_size_t_eq(ossl_quic_rcidm_get_num_active(rcidm), 2)
  69. /* Not changed over yet - handshake not confirmed */
  70. || !TEST_false(ossl_quic_rcidm_get_preferred_tx_dcid_changed(rcidm, 0))
  71. || !TEST_true(ossl_quic_rcidm_get_preferred_tx_dcid(rcidm, &dcid_out))
  72. || !TEST_true(ossl_quic_conn_id_eq(&dcid_out, &cid8_2))
  73. || !TEST_true(ossl_quic_rcidm_add_from_ncid(rcidm, &ncid_frame_2))
  74. || !TEST_size_t_eq(ossl_quic_rcidm_get_num_active(rcidm), 3)
  75. || !TEST_size_t_eq(ossl_quic_rcidm_get_num_retiring(rcidm), 0)
  76. || !TEST_false(ossl_quic_rcidm_pop_retire_seq_num(rcidm, &seq_num_out))
  77. /* Not changed over yet - handshake not confirmed */
  78. || !TEST_false(ossl_quic_rcidm_get_preferred_tx_dcid_changed(rcidm, 0))
  79. || !TEST_true(ossl_quic_rcidm_get_preferred_tx_dcid(rcidm, &dcid_out))
  80. || !TEST_true(ossl_quic_conn_id_eq(&dcid_out, &cid8_2)))
  81. goto err;
  82. ossl_quic_rcidm_on_handshake_complete(rcidm);
  83. if (!TEST_true(ossl_quic_rcidm_get_preferred_tx_dcid_changed(rcidm, 1))
  84. || !TEST_false(ossl_quic_rcidm_get_preferred_tx_dcid_changed(rcidm, 1))
  85. || !TEST_true(ossl_quic_rcidm_get_preferred_tx_dcid(rcidm, &dcid_out))
  86. || !TEST_true(ossl_quic_conn_id_eq(&dcid_out, &cid8_3))
  87. || !TEST_size_t_eq(ossl_quic_rcidm_get_num_retiring(rcidm), 1)
  88. || !TEST_true(ossl_quic_rcidm_peek_retire_seq_num(rcidm, &seq_num_out))
  89. || !TEST_uint64_t_eq(seq_num_out, 0))
  90. goto err;
  91. ossl_quic_rcidm_request_roll(rcidm);
  92. if (!TEST_true(ossl_quic_rcidm_get_preferred_tx_dcid_changed(rcidm, 1))
  93. || !TEST_false(ossl_quic_rcidm_get_preferred_tx_dcid_changed(rcidm, 1))
  94. || !TEST_true(ossl_quic_rcidm_get_preferred_tx_dcid(rcidm, &dcid_out))
  95. || !TEST_true(ossl_quic_conn_id_eq(&dcid_out, &cid8_4))
  96. || !TEST_size_t_eq(ossl_quic_rcidm_get_num_retiring(rcidm), 2)
  97. || !TEST_true(ossl_quic_rcidm_peek_retire_seq_num(rcidm, &seq_num_out))
  98. || !TEST_uint64_t_eq(seq_num_out, 0)
  99. || !TEST_true(ossl_quic_rcidm_pop_retire_seq_num(rcidm, &seq_num_out))
  100. || !TEST_uint64_t_eq(seq_num_out, 0)
  101. || !TEST_true(ossl_quic_rcidm_pop_retire_seq_num(rcidm, &seq_num_out))
  102. || !TEST_uint64_t_eq(seq_num_out, 2))
  103. goto err;
  104. testresult = 1;
  105. err:
  106. ossl_quic_rcidm_free(rcidm);
  107. return testresult;
  108. }
  109. int setup_tests(void)
  110. {
  111. ADD_ALL_TESTS(test_rcidm, 3);
  112. return 1;
  113. }