quic_srtm_test.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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_srtm.h"
  10. #include "testutil.h"
  11. static char ptrs[8];
  12. static const QUIC_STATELESS_RESET_TOKEN token_1 = {{
  13. 0x01, 0x02, 0x03, 0x04
  14. }};
  15. static const QUIC_STATELESS_RESET_TOKEN token_2 = {{
  16. 0x01, 0x02, 0x03, 0x05
  17. }};
  18. static int test_srtm(void)
  19. {
  20. int testresult = 0;
  21. QUIC_SRTM *srtm;
  22. void *opaque = NULL;
  23. uint64_t seq_num = 0;
  24. if (!TEST_ptr(srtm = ossl_quic_srtm_new(NULL, NULL)))
  25. goto err;
  26. if (!TEST_true(ossl_quic_srtm_add(srtm, ptrs + 0, 0, &token_1))
  27. || !TEST_false(ossl_quic_srtm_add(srtm, ptrs + 0, 0, &token_1))
  28. || !TEST_false(ossl_quic_srtm_remove(srtm, ptrs + 0, 1))
  29. || !TEST_false(ossl_quic_srtm_remove(srtm, ptrs + 3, 0))
  30. || !TEST_true(ossl_quic_srtm_cull(srtm, ptrs + 3))
  31. || !TEST_true(ossl_quic_srtm_cull(srtm, ptrs + 3))
  32. || !TEST_true(ossl_quic_srtm_add(srtm, ptrs + 0, 1, &token_1))
  33. || !TEST_true(ossl_quic_srtm_add(srtm, ptrs + 0, 2, &token_1))
  34. || !TEST_true(ossl_quic_srtm_add(srtm, ptrs + 0, 3, &token_1))
  35. || !TEST_true(ossl_quic_srtm_add(srtm, ptrs + 1, 0, &token_1))
  36. || !TEST_true(ossl_quic_srtm_add(srtm, ptrs + 2, 0, &token_2))
  37. || !TEST_true(ossl_quic_srtm_add(srtm, ptrs + 3, 3, &token_2))
  38. || !TEST_true(ossl_quic_srtm_remove(srtm, ptrs + 3, 3))
  39. || !TEST_true(ossl_quic_srtm_lookup(srtm, &token_1, 0, &opaque, &seq_num))
  40. || !TEST_ptr_eq(opaque, ptrs + 1)
  41. || !TEST_uint64_t_eq(seq_num, 0)
  42. || !TEST_true(ossl_quic_srtm_lookup(srtm, &token_1, 1, &opaque, &seq_num))
  43. || !TEST_ptr_eq(opaque, ptrs + 0)
  44. || !TEST_uint64_t_eq(seq_num, 3)
  45. || !TEST_true(ossl_quic_srtm_lookup(srtm, &token_1, 2, &opaque, &seq_num))
  46. || !TEST_ptr_eq(opaque, ptrs + 0)
  47. || !TEST_uint64_t_eq(seq_num, 2)
  48. || !TEST_true(ossl_quic_srtm_lookup(srtm, &token_1, 3, &opaque, &seq_num))
  49. || !TEST_ptr_eq(opaque, ptrs + 0)
  50. || !TEST_uint64_t_eq(seq_num, 1)
  51. || !TEST_true(ossl_quic_srtm_lookup(srtm, &token_1, 4, &opaque, &seq_num))
  52. || !TEST_ptr_eq(opaque, ptrs + 0)
  53. || !TEST_uint64_t_eq(seq_num, 0)
  54. || !TEST_false(ossl_quic_srtm_lookup(srtm, &token_1, 5, &opaque, &seq_num))
  55. || !TEST_true(ossl_quic_srtm_cull(srtm, ptrs + 0))
  56. || !TEST_true(ossl_quic_srtm_lookup(srtm, &token_1, 0, &opaque, &seq_num))
  57. || !TEST_ptr_eq(opaque, ptrs + 1)
  58. || !TEST_uint64_t_eq(seq_num, 0)
  59. || !TEST_true(ossl_quic_srtm_lookup(srtm, &token_2, 0, &opaque, &seq_num))
  60. || !TEST_ptr_eq(opaque, ptrs + 2)
  61. || !TEST_uint64_t_eq(seq_num, 0)
  62. || !TEST_true(ossl_quic_srtm_remove(srtm, ptrs + 2, 0))
  63. || !TEST_false(ossl_quic_srtm_lookup(srtm, &token_2, 0, &opaque, &seq_num))
  64. )
  65. goto err;
  66. testresult = 1;
  67. err:
  68. ossl_quic_srtm_free(srtm);
  69. return testresult;
  70. }
  71. int setup_tests(void)
  72. {
  73. ADD_TEST(test_srtm);
  74. return 1;
  75. }