Fix up path generation to use OPENSSL_MODULES
[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,
35                                                        ossl_ticks2time(1100),
36                                                        "ctx 1",
37                                                        PAYLOAD(payload)))
38             || !TEST_ptr(e2 = ossl_event_queue_add_new(q, 2, 5,
39                                                        ossl_ticks2time(1100),
40                                                        "ctx 2",
41                                                        PAYLOAD("data")))
42             || !TEST_true(ossl_event_queue_add(q, &e3, 3, 20,
43                                                ossl_ticks2time(1200), "ctx 3",
44                                                PAYLOAD("more data")))
45             || !TEST_ptr(e4 = ossl_event_queue_add_new(q, 2, 5,
46                                                        ossl_ticks2time(1150),
47                                                        "ctx 2",
48                                                        PAYLOAD("data")))
49
50             /* Verify some event details */
51             || !TEST_uint_eq(ossl_event_get_type(e1), 1)
52             || !TEST_uint_eq(ossl_event_get_priority(e1), 10)
53             || !TEST_uint64_t_eq(ossl_time2ticks(ossl_event_get_when(e1))
54                                  , 1100)
55             || !TEST_str_eq(ossl_event_get0_ctx(e1), "ctx 1")
56             || !TEST_ptr(p = ossl_event_get0_payload(e1, &len))
57             || !TEST_str_eq((char *)p, payload)
58             || !TEST_uint64_t_eq(ossl_time2ticks(ossl_event_time_until(&e3)),
59                                  1100)
60             || !TEST_uint64_t_eq(ossl_time2ticks(ossl_event_queue_time_until_next(q)),
61                                  1000)
62
63             /* Modify an event's time */
64             || !TEST_true(ossl_event_queue_postpone_until(q, e1,
65                                                           ossl_ticks2time(1200)))
66             || !TEST_uint64_t_eq(ossl_time2ticks(ossl_event_get_when(e1)), 1200)
67             || !TEST_true(ossl_event_queue_remove(q, e4)))
68         goto err;
69     ossl_event_free(e4);
70
71     /* Execute the queue */
72     cur_time = ossl_ticks2time(1000);
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(1100);
77     if (!TEST_true(ossl_event_queue_get1_next_event(q, &ep))
78             || !TEST_ptr_eq(ep, e2))
79         goto err;
80     ossl_event_free(ep);
81     ep = e2 = NULL;
82     if (!TEST_true(ossl_event_queue_get1_next_event(q, &ep))
83             || !TEST_ptr_null(ep))
84         goto err;
85
86     cur_time = ossl_ticks2time(1250);
87     if (!TEST_true(ossl_event_queue_get1_next_event(q, &ep))
88             || !TEST_ptr_eq(ep, &e3))
89         goto err;
90     ossl_event_free(ep);
91     ep = NULL;
92     if (!TEST_true(ossl_event_queue_get1_next_event(q, &ep))
93             || !TEST_ptr_eq(ep, e1))
94         goto err;
95     ossl_event_free(ep);
96     ep = e1 = NULL;
97     if (!TEST_true(ossl_event_queue_get1_next_event(q, &ep))
98             || !TEST_ptr_null(ep))
99         goto err;
100
101     res = 1;
102  err:
103     ossl_event_free(ep);
104     ossl_event_queue_free(q);
105     return res;
106 }
107
108 int setup_tests(void)
109 {
110     ADD_TEST(event_test);
111     return 1;
112 }