quic_lcidm_test.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. /*
  2. * Copyright 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/quic_lcidm.h"
  10. #include "testutil.h"
  11. static char ptrs[8];
  12. static const QUIC_CONN_ID cid8_1 = { 8, { 1 } };
  13. static const QUIC_CONN_ID cid8_2 = { 8, { 2 } };
  14. static const QUIC_CONN_ID cid8_3 = { 8, { 3 } };
  15. static const QUIC_CONN_ID cid8_4 = { 8, { 4 } };
  16. static const QUIC_CONN_ID cid8_5 = { 8, { 5 } };
  17. static int test_lcidm(void)
  18. {
  19. int testresult = 0;
  20. QUIC_LCIDM *lcidm;
  21. size_t lcid_len = 10; /* != ODCID len */
  22. QUIC_CONN_ID lcid_1, lcid_dummy, lcid_init = {0};
  23. OSSL_QUIC_FRAME_NEW_CONN_ID ncid_frame_1, ncid_frame_2, ncid_frame_3;
  24. void *opaque = NULL;
  25. uint64_t seq_num = UINT64_MAX;
  26. int did_retire = 0;
  27. if (!TEST_ptr(lcidm = ossl_quic_lcidm_new(NULL, lcid_len)))
  28. goto err;
  29. if (!TEST_size_t_eq(ossl_quic_lcidm_get_lcid_len(lcidm), lcid_len))
  30. goto err;
  31. if (!TEST_true(ossl_quic_lcidm_enrol_odcid(lcidm, ptrs + 0, &cid8_1))
  32. || !TEST_false(ossl_quic_lcidm_enrol_odcid(lcidm, ptrs + 0, &cid8_2))
  33. || !TEST_false(ossl_quic_lcidm_enrol_odcid(lcidm, ptrs + 1, &cid8_1))
  34. || !TEST_size_t_eq(ossl_quic_lcidm_get_num_active_lcid(lcidm, ptrs + 1), 0)
  35. || !TEST_true(ossl_quic_lcidm_enrol_odcid(lcidm, ptrs + 1, &cid8_3))
  36. || !TEST_false(ossl_quic_lcidm_enrol_odcid(lcidm, ptrs + 1, &cid8_4))
  37. || !TEST_size_t_eq(ossl_quic_lcidm_get_num_active_lcid(lcidm, ptrs + 0), 1)
  38. || !TEST_size_t_eq(ossl_quic_lcidm_get_num_active_lcid(lcidm, ptrs + 1), 1)
  39. || !TEST_true(ossl_quic_lcidm_retire_odcid(lcidm, ptrs + 0))
  40. || !TEST_size_t_eq(ossl_quic_lcidm_get_num_active_lcid(lcidm, ptrs + 0), 0)
  41. || !TEST_false(ossl_quic_lcidm_enrol_odcid(lcidm, ptrs + 0, &cid8_1))
  42. || !TEST_false(ossl_quic_lcidm_enrol_odcid(lcidm, ptrs + 0, &cid8_5))
  43. || !TEST_size_t_eq(ossl_quic_lcidm_get_num_active_lcid(lcidm, ptrs + 0), 0)
  44. || !TEST_true(ossl_quic_lcidm_generate_initial(lcidm, ptrs + 2, &lcid_1))
  45. || !TEST_size_t_eq(ossl_quic_lcidm_get_num_active_lcid(lcidm, ptrs + 2), 1)
  46. || !TEST_false(ossl_quic_lcidm_generate_initial(lcidm, ptrs + 2, &lcid_init))
  47. || !TEST_true(ossl_quic_lcidm_generate(lcidm, ptrs + 2, &ncid_frame_1))
  48. || !TEST_true(ossl_quic_lcidm_generate(lcidm, ptrs + 2, &ncid_frame_2))
  49. || !TEST_true(ossl_quic_lcidm_generate(lcidm, ptrs + 2, &ncid_frame_3))
  50. || !TEST_size_t_eq(ossl_quic_lcidm_get_num_active_lcid(lcidm, ptrs + 2), 4)
  51. || !TEST_true(ossl_quic_lcidm_lookup(lcidm, &lcid_1, &seq_num, &opaque))
  52. || !TEST_ptr_eq(opaque, ptrs + 2)
  53. || !TEST_uint64_t_eq(seq_num, 0)
  54. || !TEST_true(ossl_quic_lcidm_lookup(lcidm, &ncid_frame_1.conn_id,
  55. &seq_num, &opaque))
  56. || !TEST_ptr_eq(opaque, ptrs + 2)
  57. || !TEST_uint64_t_eq(seq_num, 1)
  58. || !TEST_true(ossl_quic_lcidm_lookup(lcidm, &ncid_frame_2.conn_id,
  59. &seq_num, &opaque))
  60. || !TEST_ptr_eq(opaque, ptrs + 2)
  61. || !TEST_uint64_t_eq(seq_num, 2)
  62. || !TEST_true(ossl_quic_lcidm_lookup(lcidm, &ncid_frame_3.conn_id,
  63. &seq_num, &opaque))
  64. || !TEST_ptr_eq(opaque, ptrs + 2)
  65. || !TEST_uint64_t_eq(seq_num, 3)
  66. || !TEST_true(ossl_quic_lcidm_retire(lcidm, ptrs + 2, 2, NULL,
  67. &lcid_dummy, &seq_num, &did_retire))
  68. || !TEST_true(did_retire)
  69. || !TEST_size_t_eq(ossl_quic_lcidm_get_num_active_lcid(lcidm, ptrs + 2), 3)
  70. || !TEST_true(ossl_quic_lcidm_retire(lcidm, ptrs + 2, 2, NULL,
  71. &lcid_dummy, &seq_num, &did_retire))
  72. || !TEST_true(did_retire)
  73. || !TEST_size_t_eq(ossl_quic_lcidm_get_num_active_lcid(lcidm, ptrs + 2), 2)
  74. || !TEST_true(ossl_quic_lcidm_retire(lcidm, ptrs + 2, 2, NULL,
  75. &lcid_dummy, &seq_num, &did_retire))
  76. || !TEST_false(did_retire)
  77. || !TEST_size_t_eq(ossl_quic_lcidm_get_num_active_lcid(lcidm, ptrs + 2), 2)
  78. || !TEST_false(ossl_quic_lcidm_lookup(lcidm, &lcid_init,
  79. &seq_num, &opaque))
  80. || !TEST_false(ossl_quic_lcidm_lookup(lcidm, &ncid_frame_1.conn_id,
  81. &seq_num, &opaque))
  82. || !TEST_true(ossl_quic_lcidm_lookup(lcidm, &ncid_frame_2.conn_id,
  83. &seq_num, &opaque))
  84. || !TEST_true(ossl_quic_lcidm_cull(lcidm, ptrs + 2))
  85. || !TEST_size_t_eq(ossl_quic_lcidm_get_num_active_lcid(lcidm, ptrs + 2), 0))
  86. goto err;
  87. testresult = 1;
  88. err:
  89. ossl_quic_lcidm_free(lcidm);
  90. return testresult;
  91. }
  92. int setup_tests(void)
  93. {
  94. ADD_TEST(test_lcidm);
  95. return 1;
  96. }