event_queue_test.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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/event_queue.h"
  10. #include "internal/nelem.h"
  11. #include "testutil.h"
  12. static OSSL_TIME cur_time = { 100 };
  13. OSSL_TIME ossl_time_now(void)
  14. {
  15. return cur_time;
  16. }
  17. #define PAYLOAD(s) s, strlen(s) + 1
  18. static int event_test(void)
  19. {
  20. int res = 0;
  21. size_t len = 0;
  22. OSSL_EVENT *e1, *e2, e3, *e4 = NULL, *ep = NULL;
  23. OSSL_EVENT_QUEUE *q = NULL;
  24. void *p;
  25. static char payload[] = "payload";
  26. /* Create an event queue and add some events */
  27. if (!TEST_ptr(q = ossl_event_queue_new())
  28. || !TEST_ptr(e1 = ossl_event_queue_add_new(q, 1, 10,
  29. ossl_ticks2time(1100),
  30. "ctx 1",
  31. PAYLOAD(payload)))
  32. || !TEST_ptr(e2 = ossl_event_queue_add_new(q, 2, 5,
  33. ossl_ticks2time(1100),
  34. "ctx 2",
  35. PAYLOAD("data")))
  36. || !TEST_true(ossl_event_queue_add(q, &e3, 3, 20,
  37. ossl_ticks2time(1200), "ctx 3",
  38. PAYLOAD("more data")))
  39. || !TEST_ptr(e4 = ossl_event_queue_add_new(q, 2, 5,
  40. ossl_ticks2time(1150),
  41. "ctx 2",
  42. PAYLOAD("data")))
  43. /* Verify some event details */
  44. || !TEST_uint_eq(ossl_event_get_type(e1), 1)
  45. || !TEST_uint_eq(ossl_event_get_priority(e1), 10)
  46. || !TEST_uint64_t_eq(ossl_time2ticks(ossl_event_get_when(e1))
  47. , 1100)
  48. || !TEST_str_eq(ossl_event_get0_ctx(e1), "ctx 1")
  49. || !TEST_ptr(p = ossl_event_get0_payload(e1, &len))
  50. || !TEST_str_eq((char *)p, payload)
  51. || !TEST_uint64_t_eq(ossl_time2ticks(ossl_event_time_until(&e3)),
  52. 1100)
  53. || !TEST_uint64_t_eq(ossl_time2ticks(ossl_event_queue_time_until_next(q)),
  54. 1000)
  55. /* Modify an event's time */
  56. || !TEST_true(ossl_event_queue_postpone_until(q, e1,
  57. ossl_ticks2time(1200)))
  58. || !TEST_uint64_t_eq(ossl_time2ticks(ossl_event_get_when(e1)), 1200)
  59. || !TEST_true(ossl_event_queue_remove(q, e4)))
  60. goto err;
  61. ossl_event_free(e4);
  62. /* Execute the queue */
  63. cur_time = ossl_ticks2time(1000);
  64. if (!TEST_true(ossl_event_queue_get1_next_event(q, &ep))
  65. || !TEST_ptr_null(ep))
  66. goto err;
  67. cur_time = ossl_ticks2time(1100);
  68. if (!TEST_true(ossl_event_queue_get1_next_event(q, &ep))
  69. || !TEST_ptr_eq(ep, e2))
  70. goto err;
  71. ossl_event_free(ep);
  72. ep = e2 = NULL;
  73. if (!TEST_true(ossl_event_queue_get1_next_event(q, &ep))
  74. || !TEST_ptr_null(ep))
  75. goto err;
  76. cur_time = ossl_ticks2time(1250);
  77. if (!TEST_true(ossl_event_queue_get1_next_event(q, &ep))
  78. || !TEST_ptr_eq(ep, &e3))
  79. goto err;
  80. ossl_event_free(ep);
  81. ep = NULL;
  82. if (!TEST_true(ossl_event_queue_get1_next_event(q, &ep))
  83. || !TEST_ptr_eq(ep, e1))
  84. goto err;
  85. ossl_event_free(ep);
  86. ep = e1 = NULL;
  87. if (!TEST_true(ossl_event_queue_get1_next_event(q, &ep))
  88. || !TEST_ptr_null(ep))
  89. goto err;
  90. res = 1;
  91. err:
  92. ossl_event_free(ep);
  93. ossl_event_queue_free(q);
  94. return res;
  95. }
  96. int setup_tests(void)
  97. {
  98. ADD_TEST(event_test);
  99. return 1;
  100. }