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