torture: Test tevent_req_profile
[Samba.git] / lib / tevent / test_req.c
blob565ef31024fb91e2a47affccce8457c50c748583
1 /*
2 * Unix SMB/CIFS implementation.
4 * testing of some tevent_req aspects
6 * Copyright (C) Volker Lendecke 2018
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
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.
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.
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/>.
26 #include "includes.h"
27 #include "tevent.h"
28 #include "torture/torture.h"
29 #include "torture/local/proto.h"
30 #include "lib/util/tevent_unix.h"
31 #include "lib/util/tevent_req_profile.h"
32 #include "lib/util/time_basic.h"
34 struct tevent_req_create_state {
35 uint8_t val;
38 static bool test_tevent_req_create(struct torture_context *tctx,
39 const void *test_data)
41 struct tevent_req *req;
42 struct tevent_req_create_state *state;
44 req = tevent_req_create(tctx, &state,
45 struct tevent_req_create_state);
46 torture_assert_not_null(tctx, req, "tevent_req_create failed\n");
47 torture_assert_int_equal(tctx, state->val, 0, "state not initialized\n");
49 TALLOC_FREE(req);
51 return true;
54 struct profile1_state {
55 uint8_t dummy;
58 static bool test_tevent_req_profile1(struct torture_context *tctx,
59 const void *test_data)
61 struct tevent_req *req;
62 struct profile1_state *state;
63 const struct tevent_req_profile *p1;
64 struct tevent_req_profile *p2;
65 struct timeval start, stop;
66 bool ok;
67 int cmp;
69 req = tevent_req_create(tctx, &state, struct profile1_state);
70 torture_assert_not_null(tctx, req, "tevent_req_create failed\n");
72 p1 = tevent_req_get_profile(req);
73 torture_assert(tctx, p1 == NULL, "profile not initialized to NULL\n");
75 ok = tevent_req_set_profile(req);
76 torture_assert(tctx, ok, "set_profile failed\n");
78 tevent_req_done(req);
80 p2 = tevent_req_move_profile(req, tctx);
81 torture_assert_not_null(tctx, p2, "get_profile failed\n");
83 /* Demonstrate sure "p2" outlives req */
84 TALLOC_FREE(req);
86 tevent_req_profile_get_start(p2, NULL, &start);
87 tevent_req_profile_get_stop(p2, NULL, &stop);
89 cmp = tevent_timeval_compare(&start, &stop);
90 torture_assert(tctx, cmp <= 0, "stop before start\n");
92 TALLOC_FREE(p2);
94 return true;
97 struct profile2_state {
98 uint8_t dummy;
101 static void profile2_done(struct tevent_req *subreq);
103 static struct tevent_req *profile2_send(TALLOC_CTX *mem_ctx,
104 struct tevent_context *ev)
106 struct tevent_req *req, *subreq;
107 struct profile2_state *state;
108 bool ok;
110 req = tevent_req_create(mem_ctx, &state, struct profile2_state);
111 if (req == NULL) {
112 return NULL;
115 ok = tevent_req_set_profile(req);
116 if (!ok) {
117 return tevent_req_post(req, ev);
120 subreq = tevent_wakeup_send(
121 state,
123 tevent_timeval_current_ofs(0, 1));
124 if (tevent_req_nomem(subreq, req)) {
125 return tevent_req_post(req, ev);
127 tevent_req_set_callback(subreq, profile2_done, req);
129 return req;
132 static void profile2_done(struct tevent_req *subreq)
134 struct tevent_req *req = tevent_req_callback_data(
135 subreq, struct tevent_req);
136 bool ok;
138 ok = tevent_wakeup_recv(subreq);
139 if (!ok) {
140 tevent_req_oom(req);
141 return;
143 tevent_req_done(req);
146 static int profile2_recv(struct tevent_req *req,
147 TALLOC_CTX *mem_ctx,
148 struct tevent_req_profile **profile)
150 int err;
152 if (tevent_req_is_unix_error(req, &err)) {
153 return err;
156 *profile = tevent_req_move_profile(req, mem_ctx);
158 return 0;
161 static bool test_tevent_req_profile2(struct torture_context *tctx,
162 const void *test_data)
164 struct tevent_context *ev;
165 struct tevent_req *req;
166 struct tevent_req_profile *p1 = NULL;
167 struct tevent_req_profile *p2 = NULL;
168 const char *str1, *str2;
169 struct timeval tv1, tv2;
170 pid_t pid1, pid2;
171 enum tevent_req_state state1, state2;
172 uint64_t err1, err2;
173 ssize_t pack_len;
174 int err;
175 bool ok;
177 ev = samba_tevent_context_init(tctx);
178 torture_assert_not_null(tctx, ev, "samba_tevent_context_init failed\n");
180 req = profile2_send(tctx, ev);
181 torture_assert_not_null(tctx, req, "profile2_send failed\n");
183 ok = tevent_req_poll_unix(req, ev, &err);
184 torture_assert(tctx, ok, "tevent_req_poll_unix failed\n");
186 err = profile2_recv(req, tctx, &p1);
187 torture_assert_int_equal(tctx, err, 0, "profile2_recv failed\n");
189 TALLOC_FREE(req);
190 TALLOC_FREE(ev);
192 tevent_req_profile_print(p1, stdout, 0, UINT_MAX);
194 pack_len = tevent_req_profile_pack(p1, NULL, 0);
195 torture_assert(tctx, pack_len>0, "profile_pack failed\n");
198 uint8_t buf[pack_len];
199 ssize_t unpack_len;
201 tevent_req_profile_pack(p1, buf, sizeof(buf));
202 dump_data(10, buf, sizeof(buf));
204 unpack_len = tevent_req_profile_unpack(
205 buf,
206 pack_len,
207 tctx,
208 &p2);
209 torture_assert_int_equal(tctx,
210 pack_len,
211 unpack_len,
212 "profile_unpack failed\n");
215 tevent_req_profile_print(p2, stdout, 0, UINT_MAX);
217 tevent_req_profile_get_name(p1, &str1);
218 tevent_req_profile_get_name(p2, &str2);
219 torture_assert_str_equal(tctx, str1, str2, "names differ\n");
221 tevent_req_profile_get_start(p1, &str1, &tv1);
222 tevent_req_profile_get_start(p2, &str2, &tv2);
223 torture_assert_str_equal(tctx, str1, str2, "start strings differ\n");
224 torture_assert(tctx,
225 tevent_timeval_compare(&tv1, &tv2) == 0,
226 "start times differ\n");
228 tevent_req_profile_get_stop(p1, &str1, &tv1);
229 tevent_req_profile_get_stop(p2, &str2, &tv2);
230 torture_assert_str_equal(tctx, str1, str2, "stop strings differ\n");
231 torture_assert(tctx,
232 tevent_timeval_compare(&tv1, &tv2) == 0,
233 "stop times differ\n");
235 tevent_req_profile_get_status(p1, &pid1, &state1, &err1);
236 tevent_req_profile_get_status(p2, &pid2, &state2, &err2);
237 torture_assert_int_equal(tctx, pid1, pid2, "pids differ\n");
238 torture_assert_int_equal(tctx, state1, state2, "states differ\n");
239 torture_assert_int_equal(tctx, err1, err2, "user errors differ\n");
241 str1 = tevent_req_profile_string(p1, p1, 0, UINT_MAX);
242 torture_assert_not_null(tctx, str1, "profile_string failed\n");
243 str2 = tevent_req_profile_string(p2, p2, 0, UINT_MAX);
244 torture_assert_not_null(tctx, str2, "profile_string failed\n");
246 torture_assert_str_equal(tctx, str1, str2, "result strings differ\n");
248 TALLOC_FREE(p1);
249 TALLOC_FREE(p2);
251 return true;
254 struct torture_suite *torture_local_tevent_req(TALLOC_CTX *mem_ctx)
256 struct torture_suite *suite;
258 suite = torture_suite_create(mem_ctx, "tevent_req");
260 torture_suite_add_simple_tcase_const(
261 suite,
262 "create",
263 test_tevent_req_create,
264 NULL);
265 torture_suite_add_simple_tcase_const(
266 suite,
267 "profile1",
268 test_tevent_req_profile1,
269 NULL);
270 torture_suite_add_simple_tcase_const(
271 suite,
272 "profile2",
273 test_tevent_req_profile2,
274 NULL);
276 return suite;