summaryrefslogtreecommitdiffstats
path: root/ctdb/event/event_protocol_test.c
blob: 8f34fa5b68d3625350e3a07f01131b688d322aa7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
/*
   CTDB event daemon - protocol test

   Copyright (C) Amitay Isaacs  2018

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 3 of the License, or
   (at your option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program; if not, see <http://www.gnu.org/licenses/>.
*/

#include "replace.h"

#include <talloc.h>
#include <assert.h>

#define EVENT_PROTOCOL_TEST
#include "event/event_protocol.c"

#include "tests/src/protocol_common_basic.h"

/*
 * Functions to fill and verify event protocol structures
 */

static void fill_ctdb_event_script(TALLOC_CTX *mem_ctx,
				   struct ctdb_event_script *p)
{
	fill_ctdb_stringn(mem_ctx, &p->name);
	fill_ctdb_timeval(&p->begin);
	fill_ctdb_timeval(&p->end);
	p->result = rand32i();
	fill_ctdb_stringn(mem_ctx, &p->output);
}

static void verify_ctdb_event_script(struct ctdb_event_script *p1,
				     struct ctdb_event_script *p2)
{
	verify_ctdb_stringn(&p1->name, &p2->name);
	verify_ctdb_timeval(&p1->begin, &p2->begin);
	verify_ctdb_timeval(&p1->end, &p2->end);
	assert(p1->result == p2->result);
	verify_ctdb_stringn(&p1->output, &p2->output);
}

static void fill_ctdb_event_script_list(TALLOC_CTX *mem_ctx,
					struct ctdb_event_script_list *p)
{
	int i;

	p->num_scripts = rand_int(32);
	if (p->num_scripts > 0) {
		p->script = talloc_array(mem_ctx,
					 struct ctdb_event_script,
					 p->num_scripts);
		assert(p->script != NULL);

		for (i=0; i<p->num_scripts; i++) {
			fill_ctdb_event_script(mem_ctx, &p->script[i]);
		}
	} else {
		p->script = NULL;
	}
}

static void verify_ctdb_event_script_list(struct ctdb_event_script_list *p1,
					  struct ctdb_event_script_list *p2)
{
	int i;

	assert(p1->num_scripts == p2->num_scripts);
	for (i=0; i<p1->num_scripts; i++) {
		verify_ctdb_event_script(&p1->script[i], &p2->script[i]);
	}
}

static void fill_ctdb_event_request_run(TALLOC_CTX *mem_ctx,
					struct ctdb_event_request_run *p)
{
	fill_ctdb_stringn(mem_ctx, &p->component);
	fill_ctdb_stringn(mem_ctx, &p->event);
	fill_ctdb_stringn(mem_ctx, &p->args);
	p->timeout = rand32();
	p->flags = rand32();
}

static void verify_ctdb_event_request_run(struct ctdb_event_request_run *p1,
					  struct ctdb_event_request_run *p2)
{
	verify_ctdb_stringn(&p1->component, &p2->component);
	verify_ctdb_stringn(&p1->event, &p2->event);
	verify_ctdb_stringn(&p1->args, &p2->args);
	assert(p1->timeout == p2->timeout);
	assert(p1->flags == p2->flags);
}

static void fill_ctdb_event_request_status(TALLOC_CTX *mem_ctx,
					   struct ctdb_event_request_status *p)
{
	fill_ctdb_stringn(mem_ctx, &p->component);
	fill_ctdb_stringn(mem_ctx, &p->event);
}

static void verify_ctdb_event_request_status(
					struct ctdb_event_request_status *p1,
					struct ctdb_event_request_status *p2)
{
	verify_ctdb_stringn(&p1->component, &p2->component);
	verify_ctdb_stringn(&p1->event, &p2->event);
}

static void fill_ctdb_event_request_script(TALLOC_CTX *mem_ctx,
					   struct ctdb_event_request_script *p)
{
	fill_ctdb_stringn(mem_ctx, &p->component);
	fill_ctdb_stringn(mem_ctx, &p->script);
	if (rand_int(1) == 0) {
		p->action = CTDB_EVENT_SCRIPT_DISABLE;
	} else {
		p->action = CTDB_EVENT_SCRIPT_ENABLE;
	}
}

static void fill_ctdb_event_reply_status(TALLOC_CTX *mem_ctx,
					 struct ctdb_event_reply_status *p)
{
	p->summary = rand32i();
	p->script_list = talloc(mem_ctx, struct ctdb_event_script_list);
	assert(p->script_list != NULL);

	fill_ctdb_event_script_list(mem_ctx, p->script_list);
}

static void verify_ctdb_event_reply_status(struct ctdb_event_reply_status *p1,
					   struct ctdb_event_reply_status *p2)
{
	assert(p1->summary == p2->summary);
	verify_ctdb_event_script_list(p1->script_list, p2->script_list);
}

static void verify_ctdb_event_request_script(
					struct ctdb_event_request_script *p1,
					struct ctdb_event_request_script *p2)
{
	verify_ctdb_stringn(&p1->component, &p2->component);
	verify_ctdb_stringn(&p1->script, &p2->script);
	assert(p1->action == p2->action);
}

static void fill_ctdb_event_request_data(TALLOC_CTX *mem_ctx,
					 struct ctdb_event_request *p,
					 uint32_t cmd)
{
	p->cmd = cmd;

	switch (cmd) {
	case CTDB_EVENT_CMD_RUN:
		p->data.run = talloc(mem_ctx, struct ctdb_event_request_run);
		assert(p->data.run != NULL);

		fill_ctdb_event_request_run(mem_ctx, p->data.run);
		break;

	case CTDB_EVENT_CMD_STATUS:
		p->data.status = talloc(mem_ctx,
					struct ctdb_event_request_status);
		assert(p->data.status != NULL);

		fill_ctdb_event_request_status(mem_ctx, p->data.status);
		break;

	case CTDB_EVENT_CMD_SCRIPT:
		p->data.script = talloc(mem_ctx,
					struct ctdb_event_request_script);
		assert(p->data.script != NULL);

		fill_ctdb_event_request_script(mem_ctx, p->data.script);
		break;

	default:
		assert(cmd > 0 && cmd < CTDB_EVENT_CMD_MAX);
	}
}

static void verify_ctdb_event_request_data(struct ctdb_event_request *p1,
					   struct ctdb_event_request *p2)
{
	assert(p1->cmd == p2->cmd);

	switch (p1->cmd) {
	case CTDB_EVENT_CMD_RUN:
		verify_ctdb_event_request_run(p1->data.run, p2->data.run);
		break;

	case CTDB_EVENT_CMD_STATUS:
		verify_ctdb_event_request_status(p1->data.status,
						 p2->data.status);
		break;

	case CTDB_EVENT_CMD_SCRIPT:
		verify_ctdb_event_request_script(p1->data.script,
						 p2->data.script);
		break;

	default:
		assert(p1->cmd > 0 && p1->cmd < CTDB_EVENT_CMD_MAX);
	}
}

static void fill_ctdb_event_reply_data(TALLOC_CTX *mem_ctx,
				       struct ctdb_event_reply *p,
				       uint32_t cmd)
{
	p->cmd = cmd;
	p->result = rand32i();

	if (p->result != 0) {
		return;
	}

	switch (cmd) {
	case CTDB_EVENT_CMD_STATUS:
		p->data.status = talloc(mem_ctx,
					struct ctdb_event_reply_status);
		assert(p->data.status != NULL);

		fill_ctdb_event_reply_status(mem_ctx, p->data.status);
		break;

	default:
		assert(cmd > 0 && cmd < CTDB_EVENT_CMD_MAX);
	}
}

static void verify_ctdb_event_reply_data(struct ctdb_event_reply *p1,
					 struct ctdb_event_reply *p2)
{
	assert(p1->cmd == p2->cmd);
	assert(p1->result == p2->result);

	if (p1->result != 0) {
		return;
	}

	switch (p1->cmd) {
	case CTDB_EVENT_CMD_STATUS:
		verify_ctdb_event_reply_status(p1->data.status,
					       p2->data.status);
		break;

	default:
		assert(p1->cmd > 0 && p1->cmd < CTDB_EVENT_CMD_MAX);
	}
}

static void fill_ctdb_event_header(struct ctdb_event_header *p)
{
	p->length = 0; /* updated by push functions */
	p->version = 0; /* updated by push functions */
	p->reqid = rand32();
}

static void verify_ctdb_event_header(struct ctdb_event_header *p1,
				     struct ctdb_event_header *p2)
{
	assert(p1->length == p2->length);
	assert(p1->version == p2->version);
	assert(p1->reqid == p2->reqid);
}

static void fill_ctdb_event_request(TALLOC_CTX *mem_ctx,
				    struct ctdb_event_request *p,
				    uint32_t cmd)
{
	fill_ctdb_event_request_data(mem_ctx, p, cmd);
}

static void verify_ctdb_event_request(struct ctdb_event_request *p1,
				      struct ctdb_event_request *p2)
{
	verify_ctdb_event_request_data(p1, p2);
}

static void fill_ctdb_event_reply(TALLOC_CTX *mem_ctx,
				  struct ctdb_event_reply *p,
				  uint32_t cmd)
{
	fill_ctdb_event_reply_data(mem_ctx, p, cmd);
}

static void verify_ctdb_event_reply(struct ctdb_event_reply *p1,
				    struct ctdb_event_reply *p2)
{
	verify_ctdb_event_reply_data(p1, p2);
}

#define EVENT_PROTOCOL1_TEST(TYPE, NAME) \
static void TEST_FUNC(NAME)(uint32_t cmd) \
{ \
	TALLOC_CTX *mem_ctx; \
	TYPE c1, *c2; \
	uint8_t *buf; \
	size_t buflen, np; \
	int ret; \
\
	protocol_test_iterate_tag("%s %u\n", #NAME, cmd); \
	mem_ctx = talloc_new(NULL); \
	assert(mem_ctx != NULL); \
	FILL_FUNC(NAME)(mem_ctx, &c1, cmd); \
	buflen = LEN_FUNC(NAME)(&c1); \
	buf = talloc_size(mem_ctx, buflen); \
	assert(buf != NULL); \
	np = 0; \
	PUSH_FUNC(NAME)(&c1, buf, &np); \
	assert(np == buflen); \
	np = 0; \
	ret = PULL_FUNC(NAME)(buf, buflen, mem_ctx, &c2, &np); \
	assert(ret == 0); \
	assert(np == buflen); \
	VERIFY_FUNC(NAME)(&c1, c2); \
	talloc_free(mem_ctx); \
}

#define EVENT_PROTOCOL2_TEST(TYPE, NAME) \
static void TEST_FUNC(NAME)(uint32_t cmd) \
{ \
	TALLOC_CTX *mem_ctx; \
	struct ctdb_event_header h1, h2; \
	TYPE c1, *c2; \
	uint8_t *buf; \
	size_t buflen, len; \
	int ret; \
\
	protocol_test_iterate_tag("%s %u\n", #NAME, cmd); \
	mem_ctx = talloc_new(NULL); \
	assert(mem_ctx != NULL); \
	fill_ctdb_event_header(&h1); \
	FILL_FUNC(NAME)(mem_ctx, &c1, cmd); \
	buflen = LEN_FUNC(NAME)(&h1, &c1); \
	buf = talloc_size(mem_ctx, buflen); \
	assert(buf != NULL); \
	len = 0; \
	ret = PUSH_FUNC(NAME)(&h1, &c1, buf, &len); \
	assert(ret == EMSGSIZE); \
	assert(len == buflen); \
	ret = PUSH_FUNC(NAME)(&h1, &c1, buf, &buflen); \
	assert(ret == 0); \
	ret = PULL_FUNC(NAME)(buf, buflen, &h2, mem_ctx, &c2); \
	assert(ret == 0); \
	verify_ctdb_event_header(&h1, &h2); \
	VERIFY_FUNC(NAME)(&c1, c2); \
	talloc_free(mem_ctx); \
}

PROTOCOL_TYPE3_TEST(struct ctdb_event_script, ctdb_event_script);
PROTOCOL_TYPE3_TEST(struct ctdb_event_script_list, ctdb_event_script_list);

PROTOCOL_TYPE3_TEST(struct ctdb_event_request_run, ctdb_event_request_run);
PROTOCOL_TYPE3_TEST(struct ctdb_event_request_status,
		    ctdb_event_request_status);
PROTOCOL_TYPE3_TEST(struct ctdb_event_request_script,
		    ctdb_event_request_script);

PROTOCOL_TYPE3_TEST(struct ctdb_event_reply_status, ctdb_event_reply_status);

EVENT_PROTOCOL1_TEST(struct ctdb_event_request, ctdb_event_request_data);
EVENT_PROTOCOL1_TEST(struct ctdb_event_reply, ctdb_event_reply_data);

EVENT_PROTOCOL2_TEST(struct ctdb_event_request, ctdb_event_request);
EVENT_PROTOCOL2_TEST(struct ctdb_event_reply, ctdb_event_reply);

static void event_protocol_test(void)
{
	uint32_t cmd;

	TEST_FUNC(ctdb_event_script)();
	TEST_FUNC(ctdb_event_script_list)();

	TEST_FUNC(ctdb_event_request_run)();
	TEST_FUNC(ctdb_event_request_status)();
	TEST_FUNC(ctdb_event_request_script)();

	TEST_FUNC(ctdb_event_reply_status)();

	for (cmd=1; cmd<CTDB_EVENT_CMD_MAX; cmd++) {
		TEST_FUNC(ctdb_event_request_data)(cmd);
	}
	for (cmd=1; cmd<CTDB_EVENT_CMD_MAX; cmd++) {
		TEST_FUNC(ctdb_event_reply_data)(cmd);
	}

	for (cmd=1; cmd<CTDB_EVENT_CMD_MAX; cmd++) {
		TEST_FUNC(ctdb_event_request)(cmd);
	}
	for (cmd=1; cmd<CTDB_EVENT_CMD_MAX; cmd++) {
		TEST_FUNC(ctdb_event_reply)(cmd);
	}
}

int main(int argc, const char **argv)
{
	protocol_test_iterate(argc, argv, event_protocol_test);
	return 0;
}