File: test-dir-watch.c

package info (click to toggle)
ell 0.82-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 4,388 kB
  • sloc: ansic: 61,555; sh: 5,450; makefile: 575
file content (590 lines) | stat: -rw-r--r-- 13,225 bytes parent folder | download | duplicates (3)
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
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
/*
 * Embedded Linux library
 * Copyright (C) 2017  Intel Corporation
 *
 * SPDX-License-Identifier: LGPL-2.1-or-later
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#define _GNU_SOURCE
#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
#include <assert.h>
#include <sys/stat.h>
#include <sys/types.h>

#include <ell/ell.h>

enum test_op {
	OP_NULL,
	OP_OPEN,
	OP_CREAT,
	OP_UNLINK,
	OP_TRUNCATE,
	OP_RENAME,
};

struct test_result {
	const char *dir;
	const char *file;
	bool ignore;
	enum l_dir_watch_event event;
};

typedef struct test_result test_result_t;

#define MAX_RESULTS 2

struct test_data {
	enum test_op op;
	const char *orig_dir;
	const char *orig_file;
	const char *dest_dir;
	const char *dest_file;
	unsigned int length;
	const char *content;
	const struct test_result *results[MAX_RESULTS + 1];
};

struct test_entry {
	const struct test_data *data;
	char **test_dirs;
	char **test_files;
	char **watch_dirs;
	unsigned int idx;
	unsigned int res_idx;
	struct l_queue *watch_list;
	bool failed;
};

struct test_watch {
	char *pathname;
	struct test_entry *entry;
};

#define TEST_FULL(_dir, _file, _op) \
			.orig_dir = _dir, .orig_file = _file, .op = _op

#define test_open(_dir, _file, _length) \
			TEST_FULL(_dir, _file, OP_OPEN), .length = _length
#define test_creat(_dir, _file, _content) \
			TEST_FULL(_dir, _file, OP_CREAT), .content = _content
#define test_unlink(_dir, _file) \
			TEST_FULL(_dir, _file, OP_UNLINK)
#define test_truncate(_dir, _file, _length) \
			TEST_FULL(_dir, _file, OP_TRUNCATE), .length = _length
#define test_rename(_olddir, _oldfile, _newdir, _newfile) \
			TEST_FULL(_olddir, _oldfile, OP_RENAME), \
				.dest_dir = _newdir, .dest_file = _newfile

#define RESULT_PTR(_dir, _file, _ignore, _event) \
			(&(test_result_t) { _dir, _file, _ignore, _event })

#define results(args...)	.results = { args, RESULT_PTR(NULL, NULL, true, 0) }

#define created(_dir, _file)	RESULT_PTR(_dir, _file, false, L_DIR_WATCH_EVENT_CREATED)
#define removed(_dir, _file)	RESULT_PTR(_dir, _file, false, L_DIR_WATCH_EVENT_REMOVED)
#define modified(_dir, _file)	RESULT_PTR(_dir, _file, false, L_DIR_WATCH_EVENT_MODIFIED)
#define accessed(_dir, _file)	RESULT_PTR(_dir, _file, false, L_DIR_WATCH_EVENT_ACCESSED)

#define result_ignore()			.results = { RESULT_PTR(NULL, NULL, true, 0) }
#define result_created(_dir, _file)	results(created(_dir, _file))
#define result_removed(_dir, _file)	results(removed(_dir, _file))
#define result_modified(_dir, _file)	results(modified(_dir, _file))
#define result_accessed(_dir, _file)	results(accessed(_dir, _file))

static void start_single_test(void *user_data);

static void event_callback(const char *pathname, enum l_dir_watch_event event,
								void *user_data)
{
	struct test_watch *watch_data = user_data;
	struct test_entry *entry = watch_data->entry;
	const struct test_data *data = &entry->data[entry->idx];
	const struct test_result *result = data->results[entry->res_idx];
	const char *str;

	switch (event) {
	case L_DIR_WATCH_EVENT_CREATED:
		str = "CREATED";
		break;
	case L_DIR_WATCH_EVENT_REMOVED:
		str = "REMOVED";
		break;
	case L_DIR_WATCH_EVENT_MODIFIED:
		str = "MODIFIED";
		break;
	case L_DIR_WATCH_EVENT_ACCESSED:
		str = "ACCESSED";
		break;
	case L_DIR_WATCH_EVENT_ATTRIB:
		str = "ATTRIB";
		break;
	default:
		str = "UNKNOWN";
		break;
	}

	l_debug("l_dir_watch event:%s pathname:%s [%s]", str, pathname,
							watch_data->pathname);

	/* This will result in waiting for the timeout */
	if (result->ignore)
		return;

	/* The watching directory needs to match the watch callback data */
	if (strcmp(result->dir, watch_data->pathname))
		return;

	/* The file inside the watch directory needs to match */
	if (strcmp(result->file, pathname)) {
		entry->failed = true;
		return;
	}

	/* The expected event needs to match as well */
	if (result->event != event) {
		entry->failed = true;
		return;
	}

	/* Successful match of the expected event data */
	l_debug("l_dir_watch ==> MATCH index %u", entry->res_idx);

	if (entry->res_idx < MAX_RESULTS) {
		entry->res_idx++;
		result = data->results[entry->res_idx];
		if (result->dir) {
			/* More results are required to match */
			return;
		}
	}

	/* Move to next test case */
	entry->idx++;
	entry->res_idx = 0;

	l_idle_oneshot(start_single_test, entry, NULL);
}

static void run_cleanup(char **dirs, char **files)
{
	int i;

	for (i = 0; files[i]; i++) {
		l_debug("unlink(\"%s\")", files[i]);
		unlink(files[i]);
	}

	for (i = 0; dirs[i]; i++) {
		l_debug("rmdir(\"%s\")", dirs[i]);
		rmdir(dirs[i]);
	}
}

static void dir_watch_free(void *data)
{
	struct l_dir_watch *watch = data;

	l_debug("free l_dir_watch [%p]", watch);

	l_dir_watch_destroy(watch);
}

static void free_test_entry(void *data)
{
	struct test_entry *entry = data;

	l_debug("free test_entry [%p]", entry);

	l_queue_destroy(entry->watch_list, dir_watch_free);

	/* Clean run should remove any leftovers */
	run_cleanup(entry->test_dirs, entry->test_files);

	l_strv_free(entry->test_dirs);
	l_strv_free(entry->test_files);
	l_strv_free(entry->watch_dirs);

	l_free(entry);
}

static void op_open(const char *dir, const char *file, unsigned int length)
{
	char *pathname;
	int fd, err;

	pathname = l_strdup_printf("%s/%s", dir, file);

	l_debug("open(\"%s\", O_RDONLY)", pathname);
	fd = open(pathname, O_RDONLY);
	l_debug("=> %d", fd);

	if (length > 0) {
		unsigned char *buf = l_malloc(length);
		ssize_t res;

		l_debug("read(%d, %p, %u)", fd, buf, length);
		res = read(fd, buf, length);
		l_debug("=> %zd", res);

		l_free(buf);
	}

	l_debug("close(%d)", fd);
	err = close(fd);
	l_debug("=> %d", err);

	l_free(pathname);
}

static void op_creat(const char *dir, const char *file, const char *content)
{
	char *pathname;
	int fd, err;

	pathname = l_strdup_printf("%s/%s", dir, file);

	l_debug("creat(\"%s\", 0600)", pathname);
	fd = creat(pathname, 0600);
	l_debug("=> %d", fd);

	if (content) {
		int len = strlen(content);
		ssize_t res;

		l_debug("write(%d, \"%s\", %d)", fd, content, len);
		res = write(fd, content, len);
		l_debug("=> %zd", res);
	}

	l_debug("close(%d)", fd);
	err = close(fd);
	l_debug("=> %d", err);

	l_free(pathname);
}

static void op_unlink(const char *dir, const char *file)
{
	char *pathname;
	int err;

	pathname = l_strdup_printf("%s/%s", dir, file);

	l_debug("unlink(\"%s\")", pathname);
	err = unlink(pathname);
	l_debug("=> %d", err);

	l_free(pathname);
}

static void op_truncate(const char *dir, const char *file, unsigned int length)
{
	char *pathname;
	int err;

	pathname = l_strdup_printf("%s/%s", dir, file);

	l_debug("truncate(\"%s\", %u)", pathname, length);
	err = truncate(pathname, length);
	l_debug("=> %d", err);

	l_free(pathname);
}

static void op_rename(const char *olddir, const char *oldfile,
				const char *newdir, const char *newfile)
{
	char *oldpath, *newpath;
	int err;

	oldpath = l_strdup_printf("%s/%s", olddir, oldfile);
	newpath = l_strdup_printf("%s/%s", newdir, newfile);

	l_debug("rename(\"%s\", \"%s\")", oldpath, newpath);
	err = rename(oldpath, newpath);
	l_debug("=> %d", err);

	l_free(oldpath);
	l_free(newpath);
}

static void start_single_test(void *user_data)
{
	struct test_entry *entry = user_data;
	const struct test_data *data = &entry->data[entry->idx];
	const struct test_result *result = data->results[entry->res_idx];
	bool failed = entry->failed;
	bool ignore = false;

	switch (data->op) {
	case OP_NULL:
		free_test_entry(entry);
		assert(!failed);
		l_main_quit();
		return;
	case OP_OPEN:
		op_open(data->orig_dir, data->orig_file, data->length);
		break;
	case OP_CREAT:
		op_creat(data->orig_dir, data->orig_file, data->content);
		break;
	case OP_UNLINK:
		op_unlink(data->orig_dir, data->orig_file);
		break;
	case OP_TRUNCATE:
		op_truncate(data->orig_dir, data->orig_file, data->length);
		break;
	case OP_RENAME:
		op_rename(data->orig_dir, data->orig_file,
					data->dest_dir, data->dest_file);
		break;
	default:
		ignore = true;
		break;
	}

	if (!result->ignore && !ignore)
		return;

	/* Move to next test case */
	entry->idx++;
	entry->res_idx = 0;

	l_idle_oneshot(start_single_test, entry, NULL);
}

static void watch_data_free(void *data)
{
	struct test_watch *watch_data = data;

	l_debug("free test_watch [%s]", watch_data->pathname);

	l_free(watch_data->pathname);
	l_free(watch_data);
}

static void process_test_entry(void *user_data)
{
	struct test_entry *entry = user_data;
	int i;

	/* In case there is any leftovers */
	run_cleanup(entry->test_dirs, entry->test_files);

	/* Create the directories in use */
	for (i = 0; entry->test_dirs[i]; i++) {
		l_debug("mkdir(%s, 0700)", entry->test_dirs[i]);
		mkdir(entry->test_dirs[i], 0700);
	}

	for (i = 0; entry->watch_dirs[i]; i++) {
		struct test_watch *watch_data;
		struct l_dir_watch *watch;

		watch_data = l_new(struct test_watch, 1);
		watch_data->pathname = l_strdup(entry->watch_dirs[i]);
		watch_data->entry = entry;

		l_debug("new test_watch [%s]", watch_data->pathname);

		watch = l_dir_watch_new(watch_data->pathname, event_callback,
						watch_data, watch_data_free);

		l_debug("new l_dir_watch [%p]", watch);

		l_queue_push_tail(entry->watch_list, watch);
	}

	l_idle_oneshot(start_single_test, entry, NULL);
}

static void process_test(const void *data)
{
	const struct test_data *test_data = (const struct test_data *) data;
	struct test_entry *entry;
	int i, exit_status;

	assert(l_main_init());

	l_log_set_stderr();

	entry = l_new(struct test_entry, 1);
	entry->data = test_data;

	for (i = 0; test_data[i].op; i++) {
		char *file;
		int n;

		if (!l_strv_contains(entry->test_dirs, test_data[i].orig_dir))
			entry->test_dirs = l_strv_append(entry->test_dirs,
							test_data[i].orig_dir);

		if (test_data[i].orig_dir) {
			file = l_strdup_printf("%s/%s", test_data[i].orig_dir,
							test_data[i].orig_file);
			if (!l_strv_contains(entry->test_files, file))
				entry->test_files = l_strv_append(entry->test_files,
									file);
			l_free(file);
		}

		if (test_data[i].dest_dir) {
			file = l_strdup_printf("%s/%s", test_data[i].dest_dir,
							test_data[i].dest_file);
			if (!l_strv_contains(entry->test_files, file))
				entry->test_files = l_strv_append(entry->test_files,
									file);
			l_free(file);
		}

		for (n = 0; n < MAX_RESULTS; n++) {
			if (!test_data[i].results[n])
				break;
			if (l_strv_contains(entry->watch_dirs,
						test_data[i].results[n]->dir))
				continue;
			entry->watch_dirs = l_strv_append(entry->watch_dirs,
						test_data[i].results[n]->dir);
		}

	}

	entry->watch_list = l_queue_new();
	entry->failed = false;

	l_idle_oneshot(process_test_entry, entry, NULL);

	exit_status = l_main_run();
	assert(exit_status == EXIT_SUCCESS);

	assert(l_main_exit());
}

#define DIR_1	"/tmp/ell-test-dir-1"
#define DIR_2	"/tmp/ell-test-dir-2"
#define FILE_1	"file-1"
#define FILE_2	"file-2"
#define FILE_3	"file-3"
#define FILE_4	"file-4"

static const struct test_data test_data_1[] = {
	{
		test_creat(DIR_1, FILE_1, NULL),
		result_created(DIR_1, FILE_1),
	},
	{
		test_unlink(DIR_1, FILE_1),
		result_removed(DIR_1, FILE_1),
	},
	{
		test_creat(DIR_1, FILE_2, "File content"),
		result_created(DIR_1, FILE_2),
	},
	{
		test_rename(DIR_1, FILE_2, DIR_1, FILE_3),
		results(removed(DIR_1, FILE_2), created(DIR_1, FILE_3)),
	},
	{
		test_open(DIR_1, FILE_3, 4),
		result_accessed(DIR_1, FILE_3),
	},
	{
		test_truncate(DIR_1, FILE_3, 4),
		result_modified(DIR_1, FILE_3),
	},
	{
		test_unlink(DIR_1, FILE_3),
		result_removed(DIR_1, FILE_3),
	},
	{
		test_creat(DIR_2, FILE_4, NULL),
		result_ignore(),
	},
	{
		test_rename(DIR_2, FILE_4, DIR_1, FILE_4),
		result_created(DIR_1, FILE_4),
	},
	{
		test_rename(DIR_1, FILE_4, DIR_2, FILE_4),
		result_removed(DIR_1, FILE_4),
	},
	{
		test_unlink(DIR_2, FILE_4),
		result_ignore(),
	},
	{ }
};

static const struct test_data test_data_2[] = {
	{
		test_creat(DIR_1, FILE_1, NULL),
		result_created(DIR_1, FILE_1),
	},
	{
		test_rename(DIR_1, FILE_1, DIR_2, FILE_1),
		results(removed(DIR_1, FILE_1), created(DIR_2, FILE_1)),
	},
	{
		test_unlink(DIR_2, FILE_1),
		result_removed(DIR_2, FILE_1),
	},
	{ }
};

static const struct test_data test_data_3[] = {
	{
		test_creat(DIR_1, FILE_1, "X"),
		result_created(DIR_1, FILE_1),
	},
	{
		test_open(DIR_1, FILE_1, 1),
		result_accessed(DIR_1, FILE_1),
	},
	{
		test_unlink(DIR_1, FILE_1),
		result_removed(DIR_1, FILE_1),
	},
	{ }
};

static const struct test_data test_data_4[] = {
	{
		test_creat(DIR_1, FILE_1, "ABC"),
		result_created(DIR_1, FILE_1),
	},
	{
		test_creat(DIR_1, FILE_2, "XYZ"),
		result_created(DIR_1, FILE_2),
	},
	{
		test_rename(DIR_1, FILE_2, DIR_1, FILE_1),
		results(removed(DIR_1, FILE_2), created(DIR_1, FILE_1)),
	},
	{
		test_unlink(DIR_1, FILE_1),
		result_removed(DIR_1, FILE_1),
	},
	{ }
};

int main(int argc, char *argv[])
{
	l_test_init(&argc, &argv);

	l_test_add_data_func("Single directory test",
					test_data_1, process_test, 0);
	l_test_add_data_func("Move between directories",
					test_data_2, process_test, 0);
	l_test_add_data_func("Create and open file",
					test_data_3, process_test, 0);
	l_test_add_data_func("Replace existing file",
					test_data_4, process_test, 0);

	return l_test_run();
}