Line data Source code
1 : /*
2 : * Unix SMB/CIFS implementation.
3 : *
4 : * testing of some tevent_req aspects
5 : *
6 : * Copyright (C) Pavel Březina <pbrezina@redhat.com> 2021
7 : *
8 : * ** NOTE! The following LGPL license applies to the tevent
9 : * ** library. This does NOT imply that all of Samba is released
10 : * ** under the LGPL
11 : *
12 : * This library is free software; you can redistribute it and/or
13 : * modify it under the terms of the GNU Lesser General Public
14 : * License as published by the Free Software Foundation; either
15 : * version 3 of the License, or (at your option) any later version.
16 : *
17 : * This library is distributed in the hope that it will be useful,
18 : * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 : * Lesser General Public License for more details.
21 : *
22 : * You should have received a copy of the GNU Lesser General Public
23 : * License along with this library; if not, see <http://www.gnu.org/licenses/>.
24 : */
25 :
26 : #include <errno.h>
27 : #include <setjmp.h>
28 : #include <stdlib.h>
29 : #include <stdint.h>
30 : #include <signal.h>
31 :
32 : #include <talloc.h>
33 : #include <tevent.h>
34 : #include <cmocka.h>
35 :
36 0 : static void queue_trigger(struct tevent_req *req, void *private_data)
37 : {
38 : /* Dummy handler. Just return. */
39 0 : return;
40 : }
41 :
42 0 : static void fd_handler(struct tevent_context *ev,
43 : struct tevent_fd *fde,
44 : uint16_t flags,
45 : void *private_data)
46 : {
47 : /* Dummy handler. Just return. */
48 0 : return;
49 : }
50 :
51 0 : static void timer_handler(struct tevent_context *ev,
52 : struct tevent_timer *te,
53 : struct timeval current_time,
54 : void *private_data)
55 : {
56 : /* Dummy handler. Just return. */
57 0 : return;
58 : }
59 :
60 0 : static void signal_handler(struct tevent_context *ev,
61 : struct tevent_signal *se,
62 : int signum,
63 : int count,
64 : void *siginfo,
65 : void *private_data)
66 : {
67 : /* Dummy handler. Just return. */
68 0 : return;
69 : }
70 :
71 0 : static void immediate_handler(struct tevent_context *ctx,
72 : struct tevent_immediate *im,
73 : void *private_data)
74 : {
75 : /* Dummy handler. Just return. */
76 0 : return;
77 : }
78 :
79 5 : static int test_setup(void **state)
80 : {
81 : struct tevent_context *ev;
82 :
83 5 : ev = tevent_context_init(NULL);
84 5 : assert_non_null(ev);
85 :
86 5 : *state = ev;
87 5 : return 0;
88 : }
89 :
90 5 : static int test_teardown(void **state)
91 : {
92 5 : struct tevent_context *ev = (struct tevent_context *)(*state);
93 5 : talloc_free(ev);
94 5 : return 0;
95 : }
96 :
97 1 : static void test_fd_tag(void **state)
98 : {
99 1 : struct tevent_context *ev = (struct tevent_context *)(*state);
100 : struct tevent_fd *fde;
101 : uint64_t tag;
102 :
103 1 : fde = tevent_add_fd(ev, ev, 0, TEVENT_FD_READ, fd_handler, NULL);
104 1 : assert_non_null(fde);
105 :
106 1 : tag = tevent_fd_get_tag(fde);
107 1 : assert_int_equal(0, tag);
108 :
109 1 : tevent_fd_set_tag(fde, 1);
110 1 : tag = tevent_fd_get_tag(fde);
111 1 : assert_int_equal(1, tag);
112 :
113 1 : tevent_re_initialise(ev);
114 :
115 1 : tag = tevent_fd_get_tag(fde);
116 1 : assert_int_equal(1, tag);
117 :
118 1 : TALLOC_FREE(fde);
119 1 : }
120 :
121 1 : static void test_timer_tag(void **state)
122 : {
123 1 : struct tevent_context *ev = (struct tevent_context *)(*state);
124 : struct tevent_timer *te;
125 : struct timeval next;
126 : uint64_t tag;
127 :
128 1 : next = tevent_timeval_current();
129 1 : te = tevent_add_timer(ev, ev, next, timer_handler, NULL);
130 1 : assert_non_null(te);
131 :
132 1 : tag = tevent_timer_get_tag(te);
133 1 : assert_int_equal(0, tag);
134 :
135 1 : tevent_timer_set_tag(te, 1);
136 1 : tag = tevent_timer_get_tag(te);
137 1 : assert_int_equal(1, tag);
138 :
139 1 : next = tevent_timeval_current();
140 1 : tevent_update_timer(te, next);
141 :
142 1 : tag = tevent_timer_get_tag(te);
143 1 : assert_int_equal(1, tag);
144 :
145 1 : tevent_re_initialise(ev);
146 :
147 1 : tag = tevent_timer_get_tag(te);
148 1 : assert_int_equal(1, tag);
149 :
150 1 : TALLOC_FREE(te);
151 1 : }
152 :
153 1 : static void test_signal_tag(void **state)
154 : {
155 1 : struct tevent_context *ev = (struct tevent_context *)(*state);
156 : struct tevent_signal *se;
157 : uint64_t tag;
158 :
159 1 : se = tevent_add_signal(ev, ev, SIGUSR1, 0, signal_handler, NULL);
160 1 : assert_non_null(se);
161 :
162 1 : tag = tevent_signal_get_tag(se);
163 1 : assert_int_equal(0, tag);
164 :
165 1 : tevent_signal_set_tag(se, 1);
166 1 : tag = tevent_signal_get_tag(se);
167 1 : assert_int_equal(1, tag);
168 :
169 1 : tevent_re_initialise(ev);
170 :
171 1 : tag = tevent_signal_get_tag(se);
172 1 : assert_int_equal(1, tag);
173 :
174 1 : TALLOC_FREE(se);
175 1 : }
176 :
177 1 : static void test_immediate_tag(void **state)
178 : {
179 1 : struct tevent_context *ev = (struct tevent_context *)(*state);
180 : struct tevent_immediate *im;
181 : uint64_t tag;
182 :
183 1 : im = tevent_create_immediate(ev);
184 1 : assert_non_null(im);
185 :
186 1 : tag = tevent_immediate_get_tag(im);
187 1 : assert_int_equal(0, tag);
188 :
189 1 : tevent_immediate_set_tag(im, 1);
190 1 : tag = tevent_immediate_get_tag(im);
191 1 : assert_int_equal(1, tag);
192 :
193 1 : tevent_schedule_immediate(im, ev, immediate_handler, NULL);
194 :
195 1 : tag = tevent_immediate_get_tag(im);
196 1 : assert_int_equal(1, tag);
197 :
198 1 : tevent_re_initialise(ev);
199 :
200 1 : tag = tevent_immediate_get_tag(im);
201 1 : assert_int_equal(1, tag);
202 :
203 1 : TALLOC_FREE(im);
204 1 : }
205 :
206 1 : static void test_queue_entry_tag(void **state)
207 : {
208 1 : struct tevent_context *ev = (struct tevent_context *)(*state);
209 : struct tevent_queue *q;
210 : struct tevent_queue_entry *e1, *e2;
211 : struct tevent_req *r1, *r2;
212 : int *s1, *s2;
213 : uint64_t tag;
214 :
215 1 : q = tevent_queue_create(ev, "test_queue");
216 1 : assert_non_null(q);
217 :
218 1 : r1 = tevent_req_create(ev, &s1, int);
219 1 : r2 = tevent_req_create(ev, &s2, int);
220 1 : e1 = tevent_queue_add_entry(q, ev, r1, queue_trigger, NULL);
221 1 : e2 = tevent_queue_add_entry(q, ev, r2, queue_trigger, NULL);
222 :
223 1 : tag = tevent_queue_entry_get_tag(e1);
224 1 : assert_int_equal(0, tag);
225 1 : tag = tevent_queue_entry_get_tag(e2);
226 1 : assert_int_equal(0, tag);
227 :
228 1 : tevent_queue_entry_set_tag(e1, 1);
229 1 : tevent_queue_entry_set_tag(e2, 2);
230 :
231 1 : tag = tevent_queue_entry_get_tag(e1);
232 1 : assert_int_equal(1, tag);
233 :
234 1 : tag = tevent_queue_entry_get_tag(e2);
235 1 : assert_int_equal(2, tag);
236 :
237 1 : TALLOC_FREE(q);
238 1 : }
239 :
240 1 : int main(int argc, char **argv)
241 : {
242 1 : const struct CMUnitTest tests[] = {
243 : cmocka_unit_test_setup_teardown(test_fd_tag, test_setup, test_teardown),
244 : cmocka_unit_test_setup_teardown(test_timer_tag, test_setup, test_teardown),
245 : cmocka_unit_test_setup_teardown(test_signal_tag, test_setup, test_teardown),
246 : cmocka_unit_test_setup_teardown(test_immediate_tag, test_setup, test_teardown),
247 : cmocka_unit_test_setup_teardown(test_queue_entry_tag, test_setup, test_teardown),
248 : };
249 :
250 1 : cmocka_set_message_output(CM_OUTPUT_SUBUNIT);
251 :
252 1 : return cmocka_run_group_tests(tests, NULL, NULL);
253 : }
|