CVE-2023-34968: mdssvc: introduce an allocating wrapper to sl_pack()
[Samba.git] / ctdb / event / event_protocol_test.c
blob8f34fa5b68d3625350e3a07f01131b688d322aa7
1 /*
2 CTDB event daemon - protocol test
4 Copyright (C) Amitay Isaacs 2018
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, see <http://www.gnu.org/licenses/>.
20 #include "replace.h"
22 #include <talloc.h>
23 #include <assert.h>
25 #define EVENT_PROTOCOL_TEST
26 #include "event/event_protocol.c"
28 #include "tests/src/protocol_common_basic.h"
31 * Functions to fill and verify event protocol structures
34 static void fill_ctdb_event_script(TALLOC_CTX *mem_ctx,
35 struct ctdb_event_script *p)
37 fill_ctdb_stringn(mem_ctx, &p->name);
38 fill_ctdb_timeval(&p->begin);
39 fill_ctdb_timeval(&p->end);
40 p->result = rand32i();
41 fill_ctdb_stringn(mem_ctx, &p->output);
44 static void verify_ctdb_event_script(struct ctdb_event_script *p1,
45 struct ctdb_event_script *p2)
47 verify_ctdb_stringn(&p1->name, &p2->name);
48 verify_ctdb_timeval(&p1->begin, &p2->begin);
49 verify_ctdb_timeval(&p1->end, &p2->end);
50 assert(p1->result == p2->result);
51 verify_ctdb_stringn(&p1->output, &p2->output);
54 static void fill_ctdb_event_script_list(TALLOC_CTX *mem_ctx,
55 struct ctdb_event_script_list *p)
57 int i;
59 p->num_scripts = rand_int(32);
60 if (p->num_scripts > 0) {
61 p->script = talloc_array(mem_ctx,
62 struct ctdb_event_script,
63 p->num_scripts);
64 assert(p->script != NULL);
66 for (i=0; i<p->num_scripts; i++) {
67 fill_ctdb_event_script(mem_ctx, &p->script[i]);
69 } else {
70 p->script = NULL;
74 static void verify_ctdb_event_script_list(struct ctdb_event_script_list *p1,
75 struct ctdb_event_script_list *p2)
77 int i;
79 assert(p1->num_scripts == p2->num_scripts);
80 for (i=0; i<p1->num_scripts; i++) {
81 verify_ctdb_event_script(&p1->script[i], &p2->script[i]);
85 static void fill_ctdb_event_request_run(TALLOC_CTX *mem_ctx,
86 struct ctdb_event_request_run *p)
88 fill_ctdb_stringn(mem_ctx, &p->component);
89 fill_ctdb_stringn(mem_ctx, &p->event);
90 fill_ctdb_stringn(mem_ctx, &p->args);
91 p->timeout = rand32();
92 p->flags = rand32();
95 static void verify_ctdb_event_request_run(struct ctdb_event_request_run *p1,
96 struct ctdb_event_request_run *p2)
98 verify_ctdb_stringn(&p1->component, &p2->component);
99 verify_ctdb_stringn(&p1->event, &p2->event);
100 verify_ctdb_stringn(&p1->args, &p2->args);
101 assert(p1->timeout == p2->timeout);
102 assert(p1->flags == p2->flags);
105 static void fill_ctdb_event_request_status(TALLOC_CTX *mem_ctx,
106 struct ctdb_event_request_status *p)
108 fill_ctdb_stringn(mem_ctx, &p->component);
109 fill_ctdb_stringn(mem_ctx, &p->event);
112 static void verify_ctdb_event_request_status(
113 struct ctdb_event_request_status *p1,
114 struct ctdb_event_request_status *p2)
116 verify_ctdb_stringn(&p1->component, &p2->component);
117 verify_ctdb_stringn(&p1->event, &p2->event);
120 static void fill_ctdb_event_request_script(TALLOC_CTX *mem_ctx,
121 struct ctdb_event_request_script *p)
123 fill_ctdb_stringn(mem_ctx, &p->component);
124 fill_ctdb_stringn(mem_ctx, &p->script);
125 if (rand_int(1) == 0) {
126 p->action = CTDB_EVENT_SCRIPT_DISABLE;
127 } else {
128 p->action = CTDB_EVENT_SCRIPT_ENABLE;
132 static void fill_ctdb_event_reply_status(TALLOC_CTX *mem_ctx,
133 struct ctdb_event_reply_status *p)
135 p->summary = rand32i();
136 p->script_list = talloc(mem_ctx, struct ctdb_event_script_list);
137 assert(p->script_list != NULL);
139 fill_ctdb_event_script_list(mem_ctx, p->script_list);
142 static void verify_ctdb_event_reply_status(struct ctdb_event_reply_status *p1,
143 struct ctdb_event_reply_status *p2)
145 assert(p1->summary == p2->summary);
146 verify_ctdb_event_script_list(p1->script_list, p2->script_list);
149 static void verify_ctdb_event_request_script(
150 struct ctdb_event_request_script *p1,
151 struct ctdb_event_request_script *p2)
153 verify_ctdb_stringn(&p1->component, &p2->component);
154 verify_ctdb_stringn(&p1->script, &p2->script);
155 assert(p1->action == p2->action);
158 static void fill_ctdb_event_request_data(TALLOC_CTX *mem_ctx,
159 struct ctdb_event_request *p,
160 uint32_t cmd)
162 p->cmd = cmd;
164 switch (cmd) {
165 case CTDB_EVENT_CMD_RUN:
166 p->data.run = talloc(mem_ctx, struct ctdb_event_request_run);
167 assert(p->data.run != NULL);
169 fill_ctdb_event_request_run(mem_ctx, p->data.run);
170 break;
172 case CTDB_EVENT_CMD_STATUS:
173 p->data.status = talloc(mem_ctx,
174 struct ctdb_event_request_status);
175 assert(p->data.status != NULL);
177 fill_ctdb_event_request_status(mem_ctx, p->data.status);
178 break;
180 case CTDB_EVENT_CMD_SCRIPT:
181 p->data.script = talloc(mem_ctx,
182 struct ctdb_event_request_script);
183 assert(p->data.script != NULL);
185 fill_ctdb_event_request_script(mem_ctx, p->data.script);
186 break;
188 default:
189 assert(cmd > 0 && cmd < CTDB_EVENT_CMD_MAX);
193 static void verify_ctdb_event_request_data(struct ctdb_event_request *p1,
194 struct ctdb_event_request *p2)
196 assert(p1->cmd == p2->cmd);
198 switch (p1->cmd) {
199 case CTDB_EVENT_CMD_RUN:
200 verify_ctdb_event_request_run(p1->data.run, p2->data.run);
201 break;
203 case CTDB_EVENT_CMD_STATUS:
204 verify_ctdb_event_request_status(p1->data.status,
205 p2->data.status);
206 break;
208 case CTDB_EVENT_CMD_SCRIPT:
209 verify_ctdb_event_request_script(p1->data.script,
210 p2->data.script);
211 break;
213 default:
214 assert(p1->cmd > 0 && p1->cmd < CTDB_EVENT_CMD_MAX);
218 static void fill_ctdb_event_reply_data(TALLOC_CTX *mem_ctx,
219 struct ctdb_event_reply *p,
220 uint32_t cmd)
222 p->cmd = cmd;
223 p->result = rand32i();
225 if (p->result != 0) {
226 return;
229 switch (cmd) {
230 case CTDB_EVENT_CMD_STATUS:
231 p->data.status = talloc(mem_ctx,
232 struct ctdb_event_reply_status);
233 assert(p->data.status != NULL);
235 fill_ctdb_event_reply_status(mem_ctx, p->data.status);
236 break;
238 default:
239 assert(cmd > 0 && cmd < CTDB_EVENT_CMD_MAX);
243 static void verify_ctdb_event_reply_data(struct ctdb_event_reply *p1,
244 struct ctdb_event_reply *p2)
246 assert(p1->cmd == p2->cmd);
247 assert(p1->result == p2->result);
249 if (p1->result != 0) {
250 return;
253 switch (p1->cmd) {
254 case CTDB_EVENT_CMD_STATUS:
255 verify_ctdb_event_reply_status(p1->data.status,
256 p2->data.status);
257 break;
259 default:
260 assert(p1->cmd > 0 && p1->cmd < CTDB_EVENT_CMD_MAX);
264 static void fill_ctdb_event_header(struct ctdb_event_header *p)
266 p->length = 0; /* updated by push functions */
267 p->version = 0; /* updated by push functions */
268 p->reqid = rand32();
271 static void verify_ctdb_event_header(struct ctdb_event_header *p1,
272 struct ctdb_event_header *p2)
274 assert(p1->length == p2->length);
275 assert(p1->version == p2->version);
276 assert(p1->reqid == p2->reqid);
279 static void fill_ctdb_event_request(TALLOC_CTX *mem_ctx,
280 struct ctdb_event_request *p,
281 uint32_t cmd)
283 fill_ctdb_event_request_data(mem_ctx, p, cmd);
286 static void verify_ctdb_event_request(struct ctdb_event_request *p1,
287 struct ctdb_event_request *p2)
289 verify_ctdb_event_request_data(p1, p2);
292 static void fill_ctdb_event_reply(TALLOC_CTX *mem_ctx,
293 struct ctdb_event_reply *p,
294 uint32_t cmd)
296 fill_ctdb_event_reply_data(mem_ctx, p, cmd);
299 static void verify_ctdb_event_reply(struct ctdb_event_reply *p1,
300 struct ctdb_event_reply *p2)
302 verify_ctdb_event_reply_data(p1, p2);
305 #define EVENT_PROTOCOL1_TEST(TYPE, NAME) \
306 static void TEST_FUNC(NAME)(uint32_t cmd) \
308 TALLOC_CTX *mem_ctx; \
309 TYPE c1, *c2; \
310 uint8_t *buf; \
311 size_t buflen, np; \
312 int ret; \
314 protocol_test_iterate_tag("%s %u\n", #NAME, cmd); \
315 mem_ctx = talloc_new(NULL); \
316 assert(mem_ctx != NULL); \
317 FILL_FUNC(NAME)(mem_ctx, &c1, cmd); \
318 buflen = LEN_FUNC(NAME)(&c1); \
319 buf = talloc_size(mem_ctx, buflen); \
320 assert(buf != NULL); \
321 np = 0; \
322 PUSH_FUNC(NAME)(&c1, buf, &np); \
323 assert(np == buflen); \
324 np = 0; \
325 ret = PULL_FUNC(NAME)(buf, buflen, mem_ctx, &c2, &np); \
326 assert(ret == 0); \
327 assert(np == buflen); \
328 VERIFY_FUNC(NAME)(&c1, c2); \
329 talloc_free(mem_ctx); \
332 #define EVENT_PROTOCOL2_TEST(TYPE, NAME) \
333 static void TEST_FUNC(NAME)(uint32_t cmd) \
335 TALLOC_CTX *mem_ctx; \
336 struct ctdb_event_header h1, h2; \
337 TYPE c1, *c2; \
338 uint8_t *buf; \
339 size_t buflen, len; \
340 int ret; \
342 protocol_test_iterate_tag("%s %u\n", #NAME, cmd); \
343 mem_ctx = talloc_new(NULL); \
344 assert(mem_ctx != NULL); \
345 fill_ctdb_event_header(&h1); \
346 FILL_FUNC(NAME)(mem_ctx, &c1, cmd); \
347 buflen = LEN_FUNC(NAME)(&h1, &c1); \
348 buf = talloc_size(mem_ctx, buflen); \
349 assert(buf != NULL); \
350 len = 0; \
351 ret = PUSH_FUNC(NAME)(&h1, &c1, buf, &len); \
352 assert(ret == EMSGSIZE); \
353 assert(len == buflen); \
354 ret = PUSH_FUNC(NAME)(&h1, &c1, buf, &buflen); \
355 assert(ret == 0); \
356 ret = PULL_FUNC(NAME)(buf, buflen, &h2, mem_ctx, &c2); \
357 assert(ret == 0); \
358 verify_ctdb_event_header(&h1, &h2); \
359 VERIFY_FUNC(NAME)(&c1, c2); \
360 talloc_free(mem_ctx); \
363 PROTOCOL_TYPE3_TEST(struct ctdb_event_script, ctdb_event_script);
364 PROTOCOL_TYPE3_TEST(struct ctdb_event_script_list, ctdb_event_script_list);
366 PROTOCOL_TYPE3_TEST(struct ctdb_event_request_run, ctdb_event_request_run);
367 PROTOCOL_TYPE3_TEST(struct ctdb_event_request_status,
368 ctdb_event_request_status);
369 PROTOCOL_TYPE3_TEST(struct ctdb_event_request_script,
370 ctdb_event_request_script);
372 PROTOCOL_TYPE3_TEST(struct ctdb_event_reply_status, ctdb_event_reply_status);
374 EVENT_PROTOCOL1_TEST(struct ctdb_event_request, ctdb_event_request_data);
375 EVENT_PROTOCOL1_TEST(struct ctdb_event_reply, ctdb_event_reply_data);
377 EVENT_PROTOCOL2_TEST(struct ctdb_event_request, ctdb_event_request);
378 EVENT_PROTOCOL2_TEST(struct ctdb_event_reply, ctdb_event_reply);
380 static void event_protocol_test(void)
382 uint32_t cmd;
384 TEST_FUNC(ctdb_event_script)();
385 TEST_FUNC(ctdb_event_script_list)();
387 TEST_FUNC(ctdb_event_request_run)();
388 TEST_FUNC(ctdb_event_request_status)();
389 TEST_FUNC(ctdb_event_request_script)();
391 TEST_FUNC(ctdb_event_reply_status)();
393 for (cmd=1; cmd<CTDB_EVENT_CMD_MAX; cmd++) {
394 TEST_FUNC(ctdb_event_request_data)(cmd);
396 for (cmd=1; cmd<CTDB_EVENT_CMD_MAX; cmd++) {
397 TEST_FUNC(ctdb_event_reply_data)(cmd);
400 for (cmd=1; cmd<CTDB_EVENT_CMD_MAX; cmd++) {
401 TEST_FUNC(ctdb_event_request)(cmd);
403 for (cmd=1; cmd<CTDB_EVENT_CMD_MAX; cmd++) {
404 TEST_FUNC(ctdb_event_reply)(cmd);
408 int main(int argc, const char **argv)
410 protocol_test_iterate(argc, argv, event_protocol_test);
411 return 0;