File: ipc-api-tests.c

package info (click to toggle)
mmlib 1.4.2-2.1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,716 kB
  • sloc: ansic: 18,071; makefile: 431; sh: 135; python: 63
file content (468 lines) | stat: -rw-r--r-- 10,900 bytes parent folder | download
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
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
/*
   @mindmaze_header@
 */
#if HAVE_CONFIG_H
# include <config.h>
#endif

#include <check.h>

#include <stdarg.h>
#include <stdbool.h>
#include <stdio.h>
#include <strings.h>

#include "mmerrno.h"
#include "mmlib.h"
#include "mmlog.h"
#include "mmpredefs.h"
#include "mmsysio.h"
#include "mmthread.h"
#include "mmtime.h"

#include "api-testcases.h"
#include "tests-child-proc.h"
#include "ipc-api-tests-exported.h"

#define MAX_NCLIENTS 32
static int nclients = 0;

static struct mm_ipc_srv * srv;

static
void test_teardown(void)
{
	int i;
	char filename[64];

	int flags = mm_error_set_flags(MM_ERROR_SET, MM_ERROR_IGNORE);

	mm_ipc_srv_destroy(srv);
	srv = NULL;

	for (i = 0 ; i < 5 ; i ++) {
		sprintf(filename, "%s-%d", IPC_TMPFILE, i);
		mm_unlink(filename);
	}

	mm_error_set_flags(flags, MM_ERROR_IGNORE);
}

START_TEST(ipc_create_simple)
{
	struct mm_ipc_srv * server = mm_ipc_srv_create(IPC_ADDR);
	ck_assert(server != NULL);
	mm_ipc_srv_destroy(server);
}
END_TEST

/* check what happens if you give a null-terminated string longer that the maximum */
START_TEST(ipc_create_invalid)
{
	char name[257];
	memset(name, 'a', sizeof(name) - 1);
	name[sizeof(name) - 1] = '\0';

	struct mm_ipc_srv *server = mm_ipc_srv_create(name);
	ck_assert(server == NULL);
	ck_assert(mm_get_lasterror_number() == ENAMETOOLONG);

	mm_ipc_srv_destroy(server);
}
END_TEST


START_TEST(ipc_create_double)
{
	struct mm_ipc_srv * srv1, * srv2;

	srv1 = mm_ipc_srv_create(IPC_ADDR);
	ck_assert(srv1 != NULL);

	srv2 = mm_ipc_srv_create(IPC_ADDR);
	ck_assert(srv2 == NULL);
	ck_assert(mm_get_lasterror_number() == EADDRINUSE);

	mm_ipc_srv_destroy(srv1);
	mm_ipc_srv_destroy(srv2);
}
END_TEST

static
void* test_handle_client(void * arg)
{
	struct ipc_test_ctx * ctx = arg;
	char buf[256];
	int recvfd = -1;
	int pipe[2] = {-1, -1}; /* 0:read, 1:write */
	int tmpfd;
	const char data[] = "ipc server test msg";
	char line[80];
	void * exit_value = (void*) -1;

	if (recv_msg_and_fd(ctx->fd, buf, sizeof(buf), &recvfd) < 0)
		goto cleanup;

	/* send the message with a file descriptor */
	tmpfd = open_shared_object_of_type(ctx, (int*) &pipe);
	if (tmpfd < 0)
		goto cleanup;

	if (mm_ipc_build_send_msg(ctx->fd, data, sizeof(data), tmpfd) < 0)
		goto cleanup;

	tmpfd = pipe[0];
	mm_close(pipe[1]);
	pipe[1] = -1;

	/* get another message from the client.
	 * (sent after the client finished writing to tmpfile)*/
	if (recv_msg_and_fd(ctx->fd, buf, sizeof(buf), &recvfd) < 0)
		goto cleanup;

	/* check the client message */
	if (ctx->shared_object == SHARED_FILE
	   || ctx->shared_object == SHARED_MEM)
		mm_seek(tmpfd, 0, SEEK_SET);

	if (mm_read(tmpfd, line, sizeof(line)) < 0
	   || strncmp(line, "client message in shared object\n",
	              sizeof("client message in shared object\n") - 1)) {
		ck_abort_msg("server failed to read the message written by the "
		             "client in the shared file");
		goto cleanup;
	}

	exit_value = NULL;

cleanup:
	if (exit_value != NULL) {
		exit_value = (void*)(intptr_t) mm_get_lasterror_number();
		fprintf(stderr, "%s() failed: %s",
		        __func__, mm_get_lasterror_desc());
	}

	mm_close(pipe[0]);
	mm_close(pipe[1]);
	mm_close(recvfd);

	return exit_value;
}

/*
 * accept up to nclients connections, then return
 */
static
void* test_server_process(void * arg)
{
	int i;
	struct ipc_test_ctx * global_ctx = arg;
	struct ipc_test_ctx ctx[MAX_NCLIENTS];
	mm_thread_t thid[MAX_NCLIENTS];

	for (i = 0; i < global_ctx->nclients; i++)
		ctx[i] = *global_ctx;

	for (i = 0; i < global_ctx->nclients; i++) {

		ctx[i].index = i;
		ctx[i].fd = mm_ipc_srv_accept(srv);
		if (ctx[i].fd == -1)
			goto cleanup;

		mm_thr_create(&thid[i], test_handle_client, &ctx[i]);
	}

	for (i = 0; i < global_ctx->nclients; i++) {
		intptr_t rv = 0;
		mm_thr_join(thid[i], (void**)rv);
		mm_close(ctx[i].fd);
		ctx[i].fd = 0;
	}

	test_teardown();
	return NULL;

cleanup:
	if (mm_get_lasterror_number() != 0)
		fprintf(stderr, "%s() failed: %s",
		        __func__, mm_get_lasterror_desc());

	while (--i > 0) {
		mm_thr_join(thid[i], NULL);
		mm_close(ctx[i].fd);
		ctx[i].fd = 0;
	}

	test_teardown();
	return (void*) -1;
}

static
intptr_t test_server_process_pending(void * arg)
{
	int i;
	struct ipc_test_ctx * global_ctx = arg;
	struct ipc_test_ctx ctx[MAX_NCLIENTS];
	mm_thread_t thid[MAX_NCLIENTS];

	srv = mm_ipc_srv_create(IPC_ADDR);
	if (srv == NULL)
		return -1;

	for (i = 0; i < global_ctx->nclients; i++) {
		ctx[i] = *global_ctx;
		ctx[i].index = i;
		ctx[i].fd = mm_ipc_srv_accept(srv);
		if (ctx[i].fd == -1)
			goto cleanup;

		mm_thr_create(&thid[i], test_handle_client, &ctx[i]);
	}

	for (i = 0; i < global_ctx->nclients; i++) {
		mm_thr_join(thid[i], NULL);
		mm_close(ctx[i].fd);
		ctx[i].fd = 0;
	}

	test_teardown();
	return 0;

cleanup:
	if (mm_get_lasterror_number() != 0)
		fprintf(stderr, "%s() failed: %s",
		        __func__, mm_get_lasterror_desc());

	while (--i > 0) {
		mm_thr_join(thid[i], NULL);
		mm_close(ctx[i].fd);
		ctx[i].fd = 0;
	}

	test_teardown();
	return -1;
}

/*
 * Create a server
 * Create nclients children, each child connect to ipc server
 * Check the server read the expected pattern (datagram boundaries and order)
 * Exchange file descriptor
 * Client writes, server checks.
 *
 * The ipc server handling is split in two; and shared amongst all threads.
 * (only works with threads)
 */
static void
run_test_core_connected_file(struct ipc_test_ctx * ctx)
{
	int i;
	thread_proc_id clt_id[MAX_NCLIENTS];

	srv = mm_ipc_srv_create(IPC_ADDR);
	ck_assert_msg(srv != NULL, "failed to create ipc server");

	/* prepare N clients waiting for the server to launch */
	for (i = 0; i < ctx->nclients; i++) {
		run_function(&clt_id[i], test_client_process,
		             ctx, RUN_AS_THREAD);
	}

	/* wait just a little to make sure the clients are all waiting
	 * (this seems to always be the case anyway) */
	mm_relative_sleep_ms(100);

	/*
	 * start and launch the server. It will:
	 * - handle the N pending clients immediately
	 * - clean and return
	 *
	 * test_server_process() makes uses of the global srv variable !
	 */
	if (test_server_process(ctx) != NULL) {
		ck_abort_msg("test_server_process failed");
	}

	/* wait for the clients to return */
	for (i = 0; i < ctx->nclients; i++)
		clean_function(clt_id[i], RUN_AS_THREAD);

	test_teardown();
}


static void
run_test_core_pending(struct ipc_test_ctx * ctx)
{
	int i;
	thread_proc_id srv_id;
	thread_proc_id clt_id[MAX_NCLIENTS];

	/* launch the server. It will:
	 * - enter a waiting state
	 * - handle N new clients
	 * - clean and return
	 */
	run_function(&srv_id, test_server_process_pending,
	             ctx, RUN_AS_THREAD);

	/* wait just a little to make sure the server is ready */
	mm_relative_sleep_ms(100);

	/* launch the clients to attack the server */
	for (i = 0; i < ctx->nclients; i++) {
		run_function(&clt_id[i], test_client_process, ctx,
		             ctx->run_mode);
	}

	/* wait for the clients to return */
	for (i = 0; i < ctx->nclients; i++)
		clean_function(clt_id[i], ctx->run_mode);

	/* wait for the server to return, then clean */
	clean_function(srv_id, RUN_AS_THREAD);
}


/*
 * Create IPC connected pair and ensure that data communication is really
 * full duplex
 */
#define TEST_STR1 "test string for pair"
#define TEST_STR2 "second test string for pair"
START_TEST(full_duplex)
{
	int fds[2];
	ssize_t rsz;
	char buffer[42];

	ck_assert(mm_ipc_connected_pair(fds) == 0);

	rsz = mm_write(fds[0], TEST_STR1, sizeof(TEST_STR1));
	ck_assert_int_eq(rsz, sizeof(TEST_STR1));
	rsz = mm_write(fds[1], TEST_STR2, sizeof(TEST_STR2));
	ck_assert_int_eq(rsz, sizeof(TEST_STR2));

	rsz = mm_read(fds[1], buffer, sizeof(buffer));
	ck_assert_int_eq(rsz, sizeof(TEST_STR1));
	ck_assert(memcmp(buffer, TEST_STR1, sizeof(TEST_STR1)) == 0);

	rsz = mm_read(fds[0], buffer, sizeof(buffer));
	ck_assert_int_eq(rsz, sizeof(TEST_STR2));
	ck_assert(memcmp(buffer, TEST_STR2, sizeof(TEST_STR2)) == 0);

	mm_close(fds[0]);
	mm_close(fds[1]);
}
END_TEST

START_TEST(broken_pipe)
{
	int fds[2];
	ssize_t rsz;
	char buffer[42];

	ck_assert(mm_ipc_connected_pair(fds) == 0);

	rsz = mm_write(fds[0], TEST_STR1, sizeof(TEST_STR1));
	ck_assert_int_eq(rsz, sizeof(TEST_STR1));

	rsz = mm_read(fds[1], buffer, sizeof(buffer));
	ck_assert_int_eq(rsz, sizeof(TEST_STR1));
	ck_assert(memcmp(buffer, TEST_STR1, sizeof(TEST_STR1)) == 0);

	mm_close(fds[1]);
	rsz = mm_write(fds[0], TEST_STR1, sizeof(TEST_STR1));
	ck_assert(rsz == -1);
	ck_assert(mm_get_lasterror_number() == EPIPE);
	mm_close(fds[0]);
}
END_TEST


/*
 * test to pass msg and file descriptors
 *
 * Create a server
 * Create nclients children, each child connect to ipc server
 * Check the server read the expected pattern (datagram boundaries and order)
 * Exchange file descriptor
 * Client writes, server checks.
 */
START_TEST(test_file_connected_thr)
{
	ck_assert(nclients > 0);

	struct ipc_test_ctx ctx = {
		.nclients = nclients,
		.run_mode = RUN_AS_THREAD,
		.shared_object = SHARED_FILE,
	};
	run_test_core_connected_file(&ctx);
}
END_TEST


static
const struct ipc_test_ctx test_pending_cases[] = {
	{.run_mode = RUN_AS_THREAD,  .shared_object = SHARED_PIPE},
	{.run_mode = RUN_AS_PROCESS, .shared_object = SHARED_PIPE},
	{.run_mode = RUN_AS_THREAD,  .shared_object = SHARED_FILE},
	{.run_mode = RUN_AS_PROCESS, .shared_object = SHARED_FILE},
	{.run_mode = RUN_AS_THREAD,  .shared_object = SHARED_MEM},
	{.run_mode = RUN_AS_PROCESS, .shared_object = SHARED_MEM},
	{.run_mode = RUN_AS_THREAD,  .shared_object = SHARED_IPC},
	{.run_mode = RUN_AS_PROCESS, .shared_object = SHARED_IPC},
};
#define NUM_PENDING_CASE (MM_NELEM(test_pending_cases))


/*
 * Create a server
 * Create nclients children, each child connect to ipc server
 * Check the server read the expected pattern (datagram boundaries and order)
 * Parent create a shared object and pass it to child
 * Children write a pattern, parent assert the pattern
 */
START_TEST(test_core_pending)
{
	ck_assert(nclients > 0);

	struct ipc_test_ctx ctx = {
		.nclients = nclients,
		.run_mode = test_pending_cases[_i].run_mode,
		.shared_object = test_pending_cases[_i].shared_object,
	};
	run_test_core_pending(&ctx);
}
END_TEST


LOCAL_SYMBOL
TCase* create_ipc_tcase(void)
{
	TCase * tc;

	nclients = atoi(mm_getenv("TC_IPC_NCLIENTS", "5"));

	tc = tcase_create("ipc");
	tcase_add_checked_fixture(tc, NULL, test_teardown);

	tcase_add_test(tc, ipc_create_simple);
	tcase_add_test(tc, ipc_create_invalid);
	tcase_add_test(tc, ipc_create_double);

	tcase_add_test(tc, full_duplex);
	tcase_add_test(tc, broken_pipe);

	/* test the ipc server with both
	 * connections pending before accept
	 * server ready before accepting connections */
	tcase_add_test(tc, test_file_connected_thr);

	/* server is up and running before starting the clients */
	tcase_add_loop_test(tc, test_core_pending, 0, NUM_PENDING_CASE);

	return tc;
}