summaryrefslogtreecommitdiffstats
path: root/ctdb/tests/src/tmon_test.c
blob: fb1f5eb9d3049140c51b259b672402ee142960df (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
/*
   Test trivial FD monitoring

   Copyright (C) Martin Schwenke, DataDirect Networks  2022

   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 "system/filesys.h"
#include "system/wait.h"

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

#include "lib/util/tevent_unix.h"

#include "common/tmon.h"

#include "tests/src/test_backtrace.h"

struct test_write_state {
	const char *write_data;
	size_t write_data_len;
	unsigned int offset;
	struct tevent_req *req;
};

static int test_write_callback(void *private_data, struct tmon_pkt *pkt)
{
	struct test_write_state *state = talloc_get_type_abort(
		private_data, struct test_write_state);
	bool status;
	size_t len;
	char *end;
	int err;
	char c;
	const char *t;

	assert(state->write_data != NULL);

	len = strlen(state->write_data);
	if (state->offset >= len) {
		return TMON_STATUS_EXIT;
	}

	c = state->write_data[state->offset];
	state->offset++;

	if (isdigit(c)) {
		err = c - '0';

		if (err == 0) {
			status = tmon_set_exit(pkt);
		} else {
			status = tmon_set_errno(pkt, err);
		}
	} else if (ispunct(c)) {
		switch (c) {
		case '.':
			return TMON_STATUS_SKIP;
			break;
		case '!':
			status = tmon_set_ping(pkt);
			break;
		case '#':
			/* Additional errno syntax: #nnn[;] */
			t = &state->write_data[state->offset];
			err = (int)strtol(t, &end, 10);
			state->offset += (end - t);
			if (state->write_data[state->offset] == ';') {
				state->offset++;
			}
			status = tmon_set_errno(pkt, err);
			break;
		default:
			status = false;
		}
	} else if (isascii(c) && !isspace(c)) {
		status = tmon_set_ascii(pkt, c);
	} else {
		status = tmon_set_custom(pkt, (uint16_t)c);
	}

	if (!status) {
		return EDOM;
	}

	t = getenv("CTDB_TEST_TMON_WRITE_SKIP_MODE");
	if (t == NULL) {
		return 0;
	}

	/*
	 * This is write-skip mode: tmon_write() is called directly
	 * here in the callback and TMON_WRITE_SKIP is returned.  This
	 * allows tmon_write() to be exercised by reusing test cases
	 * rather than writing extra test code and test cases.
	 */

	status = tmon_write(state->req, pkt);
	if (!status) {
		return EIO;
	}

	return TMON_STATUS_SKIP;
}

static void test_tmon_done(struct tevent_req *subreq);

static struct tevent_req *test_write_send(TALLOC_CTX *mem_ctx,
					  struct tevent_context *ev,
					  int fd,
					  const char *write_data)
{
	struct tevent_req *req, *subreq;
	struct test_write_state *state;
	struct tmon_actions actions = {
		.write_callback = test_write_callback,
	};

	req = tevent_req_create(mem_ctx, &state, struct test_write_state);
	if (req == NULL) {
		return NULL;
	}

	state->write_data = write_data;
	state->offset = 0;

	subreq = tmon_send(state,
			   ev,
			   fd,
			   TMON_FD_WRITE,
			   0,
			   1,
			   &actions,
			   state);
	if (tevent_req_nomem(subreq, req)) {
		return tevent_req_post(req, ev);
	}
	tevent_req_set_callback(subreq, test_tmon_done, req);

	/* Nasty hack, but OK to cheapen testing - see test_write_callback() */
	state->req = subreq;

	return req;
}

static void test_tmon_done(struct tevent_req *subreq)
{
	struct tevent_req *req = tevent_req_callback_data(
		subreq, struct tevent_req);
	bool status;
	int err;

	status = tmon_recv(subreq, &err);
	TALLOC_FREE(subreq);
	if (!status) {
		tevent_req_error(req, err);
		return;
	}

	tevent_req_done(req);
}

static bool test_write_recv(struct tevent_req *req, int *perr)
{
	if (tevent_req_is_unix_error(req, perr)) {
		return false;
	}

	return true;
}

static int test_timeout_ok_callback(void *private_data)
{
	return 0;
}

static int test_read_callback(void *private_data, struct tmon_pkt *pkt)
{
	bool status;
	char c;
	uint16_t val;

	status = tmon_parse_ping(pkt);
	if (status) {
		printf("PING\n");
		fflush(stdout);
		return 0;
	}

	status = tmon_parse_ascii(pkt, &c);
	if (status) {
		printf("ASCII %c\n", c);
		fflush(stdout);
		return 0;
	}

	status = tmon_parse_custom(pkt, &val);
	if (status) {
		printf("CUSTOM 0x%"PRIx16"\n", val);
		fflush(stdout);
		return 0;
	}

	return 0;
}

static int test_close_ok_callback(void *private_data)
{
	return 0;
}

struct test_read_state {
};

static struct tevent_req *test_read_send(TALLOC_CTX *mem_ctx,
					 struct tevent_context *ev,
					 int fd,
					 bool close_ok,
					 unsigned long timeout,
					 bool timeout_ok)
{
	struct tevent_req *req, *subreq;
	struct test_read_state *state;
	struct tmon_actions actions = {
		.read_callback = test_read_callback,
	};

	req = tevent_req_create(mem_ctx, &state, struct test_read_state);
	if (req == NULL) {
		return NULL;
	}

	if (timeout_ok) {
		actions.timeout_callback = test_timeout_ok_callback;
	}
	if (close_ok) {
		actions.close_callback = test_close_ok_callback;
	}

	subreq = tmon_send(state,
			   ev,
			   fd,
			   TMON_FD_READ,
			   timeout,
			   0,
			   &actions,
			   state);
	if (tevent_req_nomem(subreq, req)) {
		return tevent_req_post(req, ev);
	}
	tevent_req_set_callback(subreq, test_tmon_done, req);

	return req;
}

static bool test_read_recv(struct tevent_req *req, int *perr)
{
	if (tevent_req_is_unix_error(req, perr)) {
		return false;
	}

	return true;
}

static void test(const char *write_data,
		 bool close_ok,
		 unsigned long timeout,
		 bool timeout_ok)
{
	TALLOC_CTX *mem_ctx;
	struct tevent_context *ev;
	struct tevent_req *req;
	int fd[2];
	pid_t pid;
	int wstatus;
	bool status;
	int err;
	int ret;

	mem_ctx = talloc_new(NULL);
	assert(mem_ctx != NULL);

	ev = tevent_context_init(mem_ctx);
	assert(ev != NULL);

	ret = pipe(fd);
	assert(ret == 0);

	pid = fork();
	assert(pid != -1);

	if (pid == 0) {
		/* child */
		close(fd[1]);

		req = test_read_send(mem_ctx,
				     ev,
				     fd[0],
				     close_ok,
				     timeout,
				     timeout_ok);
		assert(req != NULL);

		status = tevent_req_poll(req, ev);
		assert(status);

		status = test_read_recv(req, &err);
		if (status) {
			err = 0;
			printf("READER OK\n");
		} else {
			printf("READER ERR=%d\n", err);
		}
		fflush(stdout);

		_exit(ret);
	}

	/* Parent */
	close(fd[0]);

	req = test_write_send(mem_ctx,
			      ev,
			      fd[1],
			      write_data);
	assert(req != NULL);

	status = tevent_req_poll(req, ev);
	assert(status);

	status = test_write_recv(req, &err);
	if (status) {
		err = 0;
		printf("WRITER OK\n");
	} else {
		printf("WRITER ERR=%d\n", err);
	}
	fflush(stdout);

	/* Close to mimick exit, so child status can be checked below */
	close(fd[1]);

	waitpid(pid, &wstatus, 0);
}

static void usage(const char *prog)
{
	fprintf(stderr,
		"usage: %s <write_data> <close_ok> <timeout> <timeout_ok>\n\n"
		"  <write_data> is processed by test_write_callback(), "
		"1 character per second:\n"
		"                0: write EXIT\n"
		"              1-9: write ERRNO 1-9\n"
		"                .: skip write\n"
		"          <space>: write CUSTOM containing <space>\n"
		"    other <ascii>: write ASCII containing <ascii>\n"
		"            other: write CUSTOM\n"
		"  See test_write_callback() for more details\n"
		,
		prog);
	exit(1);
}

int main(int argc, const char **argv)
{
	bool close_ok, timeout_ok;
	unsigned long timeout;

	if (argc != 5) {
		usage(argv[0]);
	}

	test_backtrace_setup();

	close_ok = (strcmp(argv[2], "true") == 0);
	timeout = strtoul(argv[3], NULL, 0);
	if (timeout == 0) {
		/*
		 * Default timeout that should not come into play but
		 * will cause tests to fail after a reasonable amount
		 * of time, if something unexpected happens.
		 */
		timeout = 20;
	}
	timeout_ok = (strcmp(argv[4], "true") == 0);

	test(argv[1], close_ok, timeout, timeout_ok);

	return 0;
}