File: rsync_test.c

package info (click to toggle)
fort-validator 1.5.4-1%2Bdeb12u1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 3,936 kB
  • sloc: ansic: 41,247; makefile: 234; javascript: 30; sh: 18; xml: 7
file content (339 lines) | stat: -rw-r--r-- 7,339 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
#include <check.h>

#include "common.c"
#include "log.c"
#include "config/string_array.c"
#include "impersonator.c"
#include "rsync/rsync.c"
#include "types/uri.c"

static char const STR64[] = "abcdefghijklmnopqrstuvwxyz"
		"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
		"0123456789 \n";
static const size_t STR64LEN = sizeof(STR64) - 1;
static char content[1024];

struct validation *
state_retrieve(void)
{
	return NULL;
}

char *
config_get_rsync_program(void)
{
	return "rsync";
}

unsigned int
config_get_rsync_retry_count(void)
{
	return 0;
}

unsigned int
config_get_rsync_retry_interval(void)
{
	return 10;
}

size_t
token_count(struct string_tokenizer *t)
{
	ck_abort();
	return 0;
}

int
token_read(struct string_tokenizer *t, char **s)
{
	ck_abort();
	return 0;
}

void
string_tokenizer_init(struct string_tokenizer *t, char const *s, size_t sl, unsigned char sp)
{
	ck_abort();
}

bool
string_tokenizer_next(struct string_tokenizer *t)
{
	ck_abort();
	return false;
}

json_t *json_array_get(const json_t *array, size_t index)
{
	ck_abort();
	return 0;
}

size_t json_array_size(const json_t *array)
{
	ck_abort();
	return 0;
}

int
parse_json_string(json_t *json, char const *name, char const **result)
{
	ck_abort();
	return 0;
}


struct string_array const *
config_get_rsync_args(bool b)
{
	static char const *strs[] = {
	    /* Note, --bwlimit does not seem to exist in openrsync */
	    "--bwlimit=1K", "-vvv", "$REMOTE", "$LOCAL"
	};
	static struct string_array args;
	if (args.length == 0)
		string_array_init(&args, strs, ARRAY_LEN(strs));
	return &args;
}

/* Tests */

static void
disable_sigpipe(void)
{
	struct sigaction action = { .sa_handler = SIG_IGN };
	if (sigaction(SIGPIPE, &action, NULL) == -1)
		pr_crit("Cannot disable SIGPIPE: %s", strerror(errno));
}

static void
init_content(void)
{
	size_t i;

	if (sizeof(content) % STR64LEN != 0)
		pr_crit("content's length isn't divisible by str64's length");
	for (i = 0; i < (sizeof(content) / STR64LEN); i++)
		memcpy(content + 64 * i, STR64, STR64LEN);
}

static void
init_tmp(void)
{
	int res = mkdir("tmp/", 0700);
	if (res && errno != EEXIST)
		pr_crit("Could not create tmp/: %s", strerror(errno));
}

static void *
rsync_fast(void *arg)
{
	int fds[2][2];
	memcpy(fds, arg, sizeof(fds));

	ck_assert_int_eq(STR64LEN, write(STDERR_WRITE(fds), STR64, STR64LEN));
	ck_assert_int_eq(STR64LEN, write(STDOUT_WRITE(fds), STR64, STR64LEN));
	ck_assert_int_eq(STR64LEN, write(STDERR_WRITE(fds), STR64, STR64LEN));
	ck_assert_int_eq(STR64LEN, write(STDOUT_WRITE(fds), STR64, STR64LEN));

	close(STDERR_WRITE(fds));
	close(STDOUT_WRITE(fds));
	return NULL;
}

static void *
rsync_stalled(void *arg)
{
	int fds[2][2];
	memcpy(fds, arg, sizeof(fds));

	ck_assert_int_eq(STR64LEN, write(STDOUT_WRITE(fds), STR64, STR64LEN));

	sleep(5); /* The timeout is 4 seconds */

	ck_assert_int_ne(STR64LEN, write(STDOUT_WRITE(fds), STR64, STR64LEN));

	close(STDERR_WRITE(fds));
	close(STDOUT_WRITE(fds));
	return NULL;
}

static void *
rsync_drip_feed(void *arg)
{
	int fds[2][2];
	memcpy(fds, arg, sizeof(fds));

	ck_assert_int_eq(STR64LEN, write(STDOUT_WRITE(fds), STR64, STR64LEN));
	sleep(1);
	ck_assert_int_eq(STR64LEN, write(STDOUT_WRITE(fds), STR64, STR64LEN));
	ck_assert_int_eq(STR64LEN, write(STDERR_WRITE(fds), STR64, STR64LEN));
	sleep(1);
	ck_assert_int_eq(STR64LEN, write(STDOUT_WRITE(fds), STR64, STR64LEN));
	sleep(1);
	ck_assert_int_eq(STR64LEN, write(STDERR_WRITE(fds), STR64, STR64LEN));
	sleep(2);
	ck_assert_int_ne(STR64LEN, write(STDOUT_WRITE(fds), STR64, STR64LEN));

	close(STDERR_WRITE(fds));
	close(STDOUT_WRITE(fds));
	return NULL;
}

static void
prepare_exhaust(int fds[2][2], pthread_t *thread, void *(*rsync_simulator)(void *))
{
	ck_assert_int_eq(0, pipe(fds[0]));
	ck_assert_int_eq(0, pipe(fds[1]));
	ck_assert_int_eq(0, pthread_create(thread, NULL, rsync_simulator, fds));
}

static void
finish_exhaust(pthread_t thread)
{
	pthread_join(thread, NULL);
}

START_TEST(exhaust_read_fds_test_normal)
{
	int fds[2][2];
	pthread_t rsync_writer;

	printf("Normal transfer\n");
	prepare_exhaust(fds, &rsync_writer, rsync_fast);
	ck_assert_int_eq(0, exhaust_read_fds(STDERR_READ(fds), STDOUT_READ(fds), true));
	finish_exhaust(rsync_writer);
}
END_TEST

START_TEST(exhaust_read_fds_test_stalled)
{
	int fds[2][2];
	pthread_t rsync_writer;

	printf("Stalled transfer\n");
	prepare_exhaust(fds, &rsync_writer, rsync_stalled);
	ck_assert_int_eq(2, exhaust_read_fds(STDERR_READ(fds), STDOUT_READ(fds), true));
	finish_exhaust(rsync_writer);
}
END_TEST

START_TEST(exhaust_read_fds_test_drip)
{
	int fds[2][2];
	pthread_t rsync_writer;

	printf("Drip-feed\n");
	prepare_exhaust(fds, &rsync_writer, rsync_drip_feed);
	ck_assert_int_eq(2, exhaust_read_fds(STDERR_READ(fds), STDOUT_READ(fds), true));
	finish_exhaust(rsync_writer);
}
END_TEST

static void
create_file(char const *name, unsigned int kbs)
{
	FILE *file;
	unsigned int k;

	file = fopen(name, "wb");
	ck_assert_ptr_ne(NULL, file);
	for (k = 0; k < kbs; k++)
		ck_assert_int_eq(sizeof(content), fwrite(content, 1, sizeof(content), file));
	ck_assert_int_eq(0, fclose(file));
}

static void
ensure_file_deleted(char const *name)
{
	int ret;
	int error;

	errno = 0;
	ret = unlink(name);
	error = errno;

	ck_assert(ret == 0 || error == ENOENT);
}

START_TEST(full_rsync_timeout_test_1kb)
{
	printf("1kb\n");
	create_file("tmp/1kb", 1);
	ensure_file_deleted("tmp/1kb-copy");
	struct rpki_uri *uri = malloc(sizeof (struct rpki_uri));
	uri->global = strndup("tmp/1kb", 7);
	uri->global_len = 7;
	uri->local = strndup("tmp/1kb-copy", 12);
	ck_assert_int_eq(0, do_rsync(uri, false, false));
}
END_TEST

START_TEST(full_rsync_timeout_test_3kb)
{
	printf("3kb\n");
	create_file("tmp/3kb", 3);
	ensure_file_deleted("tmp/3kb-copy");
	struct rpki_uri *uri = malloc(sizeof (struct rpki_uri));
	uri->global = strndup("tmp/3kb", 7);
	uri->global_len = 7;
	uri->local = strndup("tmp/3kb-copy", 12);
	ck_assert_int_eq(0, do_rsync(uri, false, false));
}
END_TEST

START_TEST(full_rsync_timeout_test_5kb)
{
	printf("5kb\n");
	create_file("tmp/5kb", 5);
	ensure_file_deleted("tmp/5kb-copy");
	struct rpki_uri *uri = malloc(sizeof (struct rpki_uri));
	uri->global = strndup("tmp/5kb", 7);
	uri->global_len = 7;
	uri->local = strndup("tmp/5kb-copy", 12);
	/* Max speed is 1kbps, timeout is 4 seconds */
	ck_assert_int_eq(EREQFAILED, do_rsync(uri, false, false));
}
END_TEST

static Suite *xml_load_suite(void)
{
	Suite *suite;
	TCase *pipes;

	pipes = tcase_create("pipes");
	tcase_add_test(pipes, exhaust_read_fds_test_normal);
	tcase_add_test(pipes, exhaust_read_fds_test_stalled);
	tcase_add_test(pipes, exhaust_read_fds_test_drip);
	tcase_add_test(pipes, full_rsync_timeout_test_1kb);
	tcase_add_test(pipes, full_rsync_timeout_test_3kb);
	tcase_add_test(pipes, full_rsync_timeout_test_5kb);
	tcase_set_timeout(pipes, 6);

	suite = suite_create("rsync");
	suite_add_tcase(suite, pipes);

	return suite;
}

int main(void)
{
	Suite *suite;
	SRunner *runner;
	int tests_failed;

	printf("This test needs to exhaust some timeouts. Please be patient.\n");
	disable_sigpipe();
	init_content();
	init_tmp();

	suite = xml_load_suite();

	runner = srunner_create(suite);
	srunner_run_all(runner, CK_NORMAL);
	tests_failed = srunner_ntests_failed(runner);
	srunner_free(runner);

	return (tests_failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
}