1 #include "../../test.h"
2 #include "../../builders/build.h"
3 #include "../../prng.h"
4 #include "../../../src/alloc.h"
5 #include "../../../src/asfd.h"
6 #include "../../../src/async.h"
7 #include "../../../src/attribs.h"
8 #include "../../../src/base64.h"
9 #include "../../../src/hexmap.h"
10 #include "../../../src/fsops.h"
11 #include "../../../src/iobuf.h"
12 #include "../../../src/protocol2/blk.h"
13 #include "../../../src/server/protocol2/backup_phase2.h"
14 #include "../../../src/server/sdirs.h"
15 #include "../../../src/slist.h"
16 #include "../../builders/build_asfd_mock.h"
17 
18 #define BASE	"utest_server_protocol2_backup_phase2"
19 
20 static struct ioevent_list areads;
21 static struct ioevent_list awrites;
22 static struct ioevent_list creads;
23 static struct ioevent_list cwrites;
24 
do_sdirs_init(struct sdirs * sdirs)25 static void do_sdirs_init(struct sdirs *sdirs)
26 {
27 	fail_unless(!sdirs_init(sdirs, PROTO_2,
28 		BASE, // directory
29 		"utestclient", // cname
30 		NULL, // client_lockdir
31 		"a_group", // dedup_group
32 		NULL // manual_delete
33 	));
34 }
35 
setup_sdirs(void)36 static struct sdirs *setup_sdirs(void)
37 {
38 	struct sdirs *sdirs;
39 	fail_unless((sdirs=sdirs_alloc())!=NULL);
40 	do_sdirs_init(sdirs);
41 	return sdirs;
42 }
43 
setup_conf(void)44 static struct conf **setup_conf(void)
45 {
46 	struct conf **confs=NULL;
47 	fail_unless((confs=confs_alloc())!=NULL);
48 	fail_unless(!confs_init(confs));
49 	return confs;
50 }
51 
setup_async(void)52 static struct async *setup_async(void)
53 {
54 	struct async *as;
55 	fail_unless((as=async_alloc())!=NULL);
56 	as->init(as, 0 /* estimate */);
57 	return as;
58 }
59 
setup(struct async ** as,struct sdirs ** sdirs,struct conf *** confs)60 static void setup(struct async **as,
61 	struct sdirs **sdirs, struct conf ***confs)
62 {
63 	if(as) *as=setup_async();
64 	if(sdirs) *sdirs=setup_sdirs();
65 	if(confs) *confs=setup_conf();
66 	fail_unless(!recursive_delete(BASE));
67 }
68 
tear_down(struct async ** as,struct sdirs ** sdirs,struct conf *** confs)69 static void tear_down(struct async **as,
70 	struct sdirs **sdirs, struct conf ***confs)
71 {
72 	async_free(as);
73 	sdirs_free(sdirs);
74 	confs_free(confs);
75 	fail_unless(!recursive_delete(BASE));
76 //printf("%d %d\n", alloc_count, free_count);
77 	alloc_check();
78 }
79 
START_TEST(test_phase2_unset_as_sdirs_confs)80 START_TEST(test_phase2_unset_as_sdirs_confs)
81 {
82 	setup(NULL, NULL, NULL);
83 	fail_unless(do_backup_phase2_server_protocol2(
84 		NULL, // as
85 		NULL, // chfd
86 		NULL, // sdirs
87 		0, // resume
88 		NULL // confs
89 	)==-1);
90 	tear_down(NULL, NULL, NULL);
91 }
92 END_TEST
93 
START_TEST(test_phase2_unset_sdirs_confs)94 START_TEST(test_phase2_unset_sdirs_confs)
95 {
96 	struct async *as;
97 	setup(&as, NULL, NULL);
98 	fail_unless(do_backup_phase2_server_protocol2(
99 		as,
100 		NULL, // chfd
101 		NULL, // sdirs
102 		0, // resume
103 		NULL // confs
104 	)==-1);
105 	tear_down(&as, NULL, NULL);
106 }
107 END_TEST
108 
START_TEST(test_phase2_unset_confs)109 START_TEST(test_phase2_unset_confs)
110 {
111 	struct async *as;
112 	struct sdirs *sdirs;
113 	setup(&as, &sdirs, NULL);
114 	fail_unless(do_backup_phase2_server_protocol2(
115 		as,
116 		NULL, // chfd
117 		sdirs,
118 		0, // resume
119 		NULL // confs
120 	)==-1);
121 	tear_down(&as, &sdirs, NULL);
122 }
123 END_TEST
124 
START_TEST(test_phase2_unset_sdirs)125 START_TEST(test_phase2_unset_sdirs)
126 {
127 	struct async *as;
128 	struct conf **confs;
129 	setup(&as, NULL, &confs);
130 	fail_unless(do_backup_phase2_server_protocol2(
131 		as,
132 		NULL, // chfd
133 		NULL, // sdirs
134 		0, // resume
135 		confs
136 	)==-1);
137 	tear_down(&as, NULL, &confs);
138 }
139 END_TEST
140 
START_TEST(test_phase2_unset_asfd)141 START_TEST(test_phase2_unset_asfd)
142 {
143 	struct async *as;
144 	struct sdirs *sdirs;
145 	struct conf **confs;
146 	setup(&as, &sdirs, &confs);
147 	fail_unless(do_backup_phase2_server_protocol2(
148 		as,
149 		NULL, // chfd
150 		sdirs,
151 		0, // resume
152 		confs
153 	)==-1);
154 	tear_down(&as, &sdirs, &confs);
155 }
156 END_TEST
157 
START_TEST(test_phase2_unset_chfd)158 START_TEST(test_phase2_unset_chfd)
159 {
160 	struct asfd *asfd;
161 	struct async *as;
162 	struct sdirs *sdirs;
163 	struct conf **confs;
164 	setup(&as, &sdirs, &confs);
165 	asfd=asfd_mock_setup(&areads, &awrites);
166 	as->asfd_add(as, asfd);
167 
168 	fail_unless(do_backup_phase2_server_protocol2(
169 		as,
170 		NULL, // chfd
171 		sdirs,
172 		0, // resume
173 		confs
174 	)==-1);
175 	asfd_free(&asfd);
176 	asfd_mock_teardown(&areads, &awrites);
177 	tear_down(&as, &sdirs, &confs);
178 }
179 END_TEST
180 
setup_asfds_empty(struct asfd * asfd,struct asfd * chfd,struct slist * slist)181 static void setup_asfds_empty(struct asfd *asfd, struct asfd *chfd,
182 	struct slist *slist)
183 {
184 	int ar=0, aw=0, cw=0;
185 	asfd_assert_write(asfd, &aw, 0, CMD_GEN, "requests_end");
186 	asfd_mock_read(asfd, &ar, 0, CMD_GEN, "sigs_end");
187 	asfd_assert_write(asfd, &aw, 0, CMD_GEN, "blk_requests_end");
188 	asfd_mock_read(asfd, &ar, 0, CMD_GEN, "backup_end");
189 
190 	asfd_assert_write(chfd, &cw, 0, CMD_GEN, "sigs_end");
191 }
192 
setup_asfds_empty_and_messages(struct asfd * asfd,struct asfd * chfd,struct slist * slist)193 static void setup_asfds_empty_and_messages(struct asfd *asfd, struct asfd *chfd,
194 	struct slist *slist)
195 {
196 	int ar=0, aw=0, cw=0;
197 	asfd_mock_read(asfd, &ar, 0, CMD_MESSAGE, "a message");
198 	asfd_assert_write(asfd, &aw, 0, CMD_GEN, "requests_end");
199 	asfd_mock_read(asfd, &ar, 0, CMD_MESSAGE, "a message");
200 	asfd_mock_read(asfd, &ar, 0, CMD_GEN, "sigs_end");
201 	asfd_mock_read(asfd, &ar, 0, CMD_WARNING, "a warning");
202 	asfd_mock_read(asfd, &ar, 0, CMD_WARNING, "another warning");
203 	asfd_assert_write(asfd, &aw, 0, CMD_GEN, "blk_requests_end");
204 	asfd_mock_read(asfd, &ar, 0, CMD_MESSAGE, "a message");
205 	asfd_mock_read(asfd, &ar, 0, CMD_GEN, "backup_end");
206 
207 	asfd_assert_write(chfd, &cw, 0, CMD_GEN, "sigs_end");
208 }
209 
setup_asfds_data_too_soon(struct asfd * asfd,struct asfd * chfd,struct slist * slist)210 static void setup_asfds_data_too_soon(struct asfd *asfd, struct asfd *chfd,
211 	struct slist *slist)
212 {
213 	int ar=0, aw=0;
214 	asfd_assert_write(asfd, &aw, 0, CMD_GEN, "requests_end");
215 	asfd_mock_read(asfd, &ar, 0, CMD_DATA, "some data");
216 }
217 
setup_asfds_write_error(struct asfd * asfd,struct asfd * chfd,struct slist * slist)218 static void setup_asfds_write_error(struct asfd *asfd, struct asfd *chfd,
219 	struct slist *slist)
220 {
221 	int aw=0;
222 	asfd_assert_write(asfd, &aw, -1, CMD_GEN, "requests_end");
223 }
224 
setup_asfds_write_error_chfd(struct asfd * asfd,struct asfd * chfd,struct slist * slist)225 static void setup_asfds_write_error_chfd(struct asfd *asfd, struct asfd *chfd,
226 	struct slist *slist)
227 {
228 	int ar=0, aw=0, cw=0;
229 	asfd_assert_write(asfd, &aw, 0, CMD_GEN, "requests_end");
230 	asfd_mock_read(asfd, &ar, 0, CMD_GEN, "sigs_end");
231 	asfd_assert_write(asfd, &aw, 0, CMD_GEN, "blk_requests_end");
232 
233 	asfd_assert_write(chfd, &cw, -1, CMD_GEN, "sigs_end");
234 }
235 
setup_asfds_read_error(struct asfd * asfd,struct asfd * chfd,struct slist * slist)236 static void setup_asfds_read_error(struct asfd *asfd, struct asfd *chfd,
237 	struct slist *slist)
238 {
239 	int ar=0, aw=0;
240 	asfd_assert_write(asfd, &aw,  0, CMD_GEN, "requests_end");
241 	asfd_mock_read(asfd, &ar, -1, CMD_DATA, "some data");
242 }
243 
setup_asfds_read_error_chfd(struct asfd * asfd,struct asfd * chfd,struct slist * slist)244 static void setup_asfds_read_error_chfd(struct asfd *asfd, struct asfd *chfd,
245 	struct slist *slist)
246 {
247 	int ar=0, aw=0, cr=0;
248 	asfd_assert_write(asfd, &aw,  0, CMD_GEN, "requests_end");
249 	asfd_mock_read_no_op(asfd, &ar, 1);
250 	asfd_mock_read(chfd, &cr, -1, CMD_SIG, "some sig");
251 }
252 
setup_asfds_chfd_bad_cmd(struct asfd * asfd,struct asfd * chfd,struct slist * slist)253 static void setup_asfds_chfd_bad_cmd(struct asfd *asfd, struct asfd *chfd,
254 	struct slist *slist)
255 {
256 	int ar=0, aw=0, cr=0;
257 	asfd_assert_write(asfd, &aw,  0, CMD_GEN, "requests_end");
258 	asfd_mock_read_no_op(asfd, &ar, 1);
259 	asfd_mock_read(chfd, &cr,  0, CMD_MESSAGE, "some message");
260 }
261 
setup_writes_from_slist(struct asfd * asfd,int * aw,struct slist * slist)262 static void setup_writes_from_slist(struct asfd *asfd,
263 	int *aw, struct slist *slist)
264 {
265 	struct sbuf *s;
266 	if(!slist) return;
267 	for(s=slist->head; s; s=s->next)
268 	{
269 		if(sbuf_is_filedata(s)
270 		  && !sbuf_is_encrypted(s)) // Not working for proto2 yet.
271 		{
272 			asfd_assert_write(asfd,
273 				aw, 0, s->path.cmd, s->path.buf);
274 		}
275 	}
276 }
277 
setup_asfds_no_sigs_from_client(struct asfd * asfd,struct asfd * chfd,struct slist * slist)278 static void setup_asfds_no_sigs_from_client(struct asfd *asfd,
279 	struct asfd *chfd, struct slist *slist)
280 {
281 	int ar=0, aw=0, cw=0;
282 
283 	setup_writes_from_slist(asfd, &aw, slist);
284 	asfd_assert_write(asfd, &aw, 0, CMD_GEN, "requests_end");
285 	asfd_mock_read_no_op(asfd, &ar, 60);
286 	asfd_mock_read(asfd, &ar, 0, CMD_GEN, "sigs_end");
287 	asfd_assert_write(asfd, &aw, 0, CMD_GEN, "blk_requests_end");
288 	asfd_mock_read(asfd, &ar, 0, CMD_GEN, "backup_end");
289 
290 	asfd_assert_write(chfd, &cw, 0, CMD_GEN, "sigs_end");
291 }
292 
setup_reads_from_slist(struct asfd * asfd,int * ar,struct slist * slist,int number_of_blks,uint64_t interrupt)293 static void setup_reads_from_slist(struct asfd *asfd,
294 	int *ar, struct slist *slist, int number_of_blks, uint64_t interrupt)
295 {
296 	int file_no=1;
297 	struct sbuf *s;
298 	struct blk blk;
299 	struct iobuf iobuf;
300 	if(!slist) return;
301 	for(s=slist->head; s; s=s->next)
302 	{
303 		if(sbuf_is_filedata(s)
304 		  && !sbuf_is_encrypted(s)) // Not working for proto2 yet.
305 		{
306 			int b;
307 			s->protocol2->index=file_no++;
308 			if(interrupt==s->protocol2->index)
309 			{
310 				char buf[32]="";
311 				asfd_mock_read(asfd,
312 					ar, 0, CMD_WARNING, "path vanished\n");
313 				base64_from_uint64(interrupt, buf);
314 				asfd_mock_read(asfd, ar, 0, CMD_INTERRUPT,
315 					buf);
316 				continue;
317 			}
318 			iobuf_free_content(&s->attr);
319 			attribs_encode(s);
320 			asfd_mock_read(asfd,
321 				ar, 0, CMD_ATTRIBS_SIGS, s->attr.buf);
322 			blk.fingerprint=file_no;
323 			memset(&blk.md5sum, file_no, MD5_DIGEST_LENGTH);
324 			blk_to_iobuf_sig(&blk, &iobuf);
325 			for(b=0; b<number_of_blks; b++)
326 				asfd_mock_read_iobuf(asfd, ar, 0, &iobuf);
327 		}
328 	}
329 }
330 
setup_chfd_writes_from_slist(struct asfd * chfd,int * cw,struct slist * slist,int number_of_blks,uint64_t interrupt)331 static void setup_chfd_writes_from_slist(struct asfd *chfd,
332 	int *cw, struct slist *slist, int number_of_blks, uint64_t interrupt)
333 {
334 	struct sbuf *s;
335 	struct blk blk;
336 	struct iobuf iobuf;
337 	uint64_t file_no=1;
338 	if(!slist) return;
339 	for(s=slist->head; s; s=s->next)
340 	{
341 		if(sbuf_is_filedata(s)
342 		  && !sbuf_is_encrypted(s)) // Not working for proto2 yet.
343 		{
344 			int b;
345 			if(interrupt==file_no++)
346 				continue;
347 			blk.fingerprint=file_no;
348 			memset(&blk.md5sum, file_no, MD5_DIGEST_LENGTH);
349 			blk_to_iobuf_sig(&blk, &iobuf);
350 			for(b=0; b<number_of_blks; b++)
351 				asfd_assert_write_iobuf(chfd, cw, 0, &iobuf);
352 		}
353 	}
354 }
355 
setup_chfd_reads_from_slist_blks_got(struct asfd * chfd,int * cr,struct slist * slist,int number_of_blks,uint64_t interrupt)356 static void setup_chfd_reads_from_slist_blks_got(struct asfd *chfd,
357 	int *cr, struct slist *slist, int number_of_blks, uint64_t interrupt)
358 {
359 	uint64_t file_no=1;
360 	int blk_index=1;
361 	struct blk blk;
362 	struct iobuf iobuf;
363 	struct sbuf *s;
364 	if(!slist) return;
365 	for(s=slist->head; s; s=s->next)
366 	{
367 		if(sbuf_is_filedata(s)
368 		  && !sbuf_is_encrypted(s)) // Not working for proto2 yet.
369 		{
370 			int b;
371 			if(interrupt==file_no++)
372 				continue;
373 			for(b=0; b<number_of_blks; b++)
374 			{
375 				blk.index=blk_index++;
376 				blk.savepath=0;
377 				blk_to_iobuf_index_and_savepath(&blk, &iobuf);
378 				asfd_mock_read_iobuf(chfd, cr, 0, &iobuf);
379 			}
380 		}
381 	}
382 }
383 
setup_chfd_reads_from_slist_blks_not_got(struct asfd * chfd,int * cr,struct slist * slist,int number_of_blks,uint64_t interrupt)384 static void setup_chfd_reads_from_slist_blks_not_got(struct asfd *chfd,
385 	int *cr, struct slist *slist, int number_of_blks, uint64_t interrupt)
386 {
387 	int blk_index=1;
388 	uint64_t file_no=1;
389 	struct blk blk;
390 	struct iobuf iobuf;
391 	struct sbuf *s;
392 	if(!slist) return;
393 	for(s=slist->head; s; s=s->next)
394 	{
395 		if(sbuf_is_filedata(s)
396 		  && !sbuf_is_encrypted(s)) // Not working for proto2 yet.
397 		{
398 			int b;
399 			if(interrupt==file_no++)
400 				continue;
401 			for(b=0; b<number_of_blks; b++)
402 			{
403 				blk.index=blk_index++;
404 				blk_to_iobuf_wrap_up(&blk, &iobuf);
405 				asfd_mock_read_iobuf(chfd, cr, 0, &iobuf);
406 			}
407 		}
408 	}
409 }
410 
setup_writes_from_slist_blk_requests(struct asfd * asfd,int * aw,struct slist * slist,int number_of_blks,uint64_t interrupt)411 static void setup_writes_from_slist_blk_requests(struct asfd *asfd,
412 	int *aw, struct slist *slist, int number_of_blks, uint64_t interrupt)
413 {
414 	struct sbuf *s;
415 	struct iobuf iobuf;
416 	char req[32]="";
417 	int blk_index=1;
418 	uint64_t file_no=1;
419 	if(!slist) return;
420 	for(s=slist->head; s; s=s->next)
421 	{
422 		if(sbuf_is_filedata(s)
423 		  && !sbuf_is_encrypted(s)) // Not working for proto2 yet.
424 		{
425 			int b;
426 			if(interrupt==file_no++)
427 				continue;
428 
429                         base64_from_uint64(blk_index, req);
430                         iobuf_from_str(&iobuf, CMD_DATA_REQ, req);
431                         asfd_assert_write_iobuf(asfd, aw, 0, &iobuf);
432                         for(b=0; b<number_of_blks; b++)
433                                 blk_index++;
434 		}
435 	}
436 }
437 
setup_reads_from_slist_blks(struct asfd * asfd,int * ar,struct slist * slist,int number_of_blks,uint64_t interrupt)438 static void setup_reads_from_slist_blks(struct asfd *asfd,
439 	int *ar, struct slist *slist, int number_of_blks, uint64_t interrupt)
440 {
441 	struct sbuf *s;
442 	struct blk blk;
443 	struct iobuf iobuf;
444 	uint64_t file_no=1;
445 	if(!slist) return;
446 	for(s=slist->head; s; s=s->next)
447 	{
448 		if(sbuf_is_filedata(s)
449 		  && !sbuf_is_encrypted(s)) // Not working for proto2 yet.
450 		{
451 			if(interrupt==file_no++)
452 				continue;
453 			blk_to_iobuf_sig(&blk, &iobuf);
454 			iobuf_from_str(&iobuf, CMD_DATA, (char *)"some data");
455 			asfd_mock_read_iobuf(asfd, ar, 0, &iobuf);
456 		}
457 	}
458 }
459 
setup_asfds_happy_path_one_blk_per_file_full_dedup(struct asfd * asfd,struct asfd * chfd,struct slist * slist)460 static void setup_asfds_happy_path_one_blk_per_file_full_dedup(
461 	struct asfd *asfd, struct asfd *chfd, struct slist *slist)
462 {
463 	int ar=0, aw=0, cr=0, cw=0;
464 
465 	setup_writes_from_slist(asfd, &aw, slist);
466 	asfd_assert_write(asfd, &aw, 0, CMD_GEN, "requests_end");
467 	asfd_mock_read_no_op(asfd, &ar, 30);
468 	setup_reads_from_slist(asfd, &ar, slist, 1, 0);
469 	asfd_mock_read(asfd, &ar, 0, CMD_GEN, "sigs_end");
470 
471 	asfd_assert_write(asfd, &aw, 0, CMD_GEN, "blk_requests_end");
472 	asfd_mock_read_no_op(asfd, &ar, 60);
473 	asfd_mock_read(asfd, &ar, 0, CMD_GEN, "backup_end");
474 
475 	setup_chfd_writes_from_slist(chfd, &cw, slist, 1, 0);
476 	asfd_mock_read_no_op(chfd, &cr, 60);
477 	setup_chfd_reads_from_slist_blks_got(chfd, &cr, slist, 1, 0);
478 	asfd_mock_read_no_op(chfd, &cr, 60);
479 	asfd_assert_write(chfd, &cw, 0, CMD_GEN, "sigs_end");
480 }
481 
setup_asfds_happy_path_one_blk_per_file_full_dedup_big(struct asfd * asfd,struct asfd * chfd,struct slist * slist)482 static void setup_asfds_happy_path_one_blk_per_file_full_dedup_big(
483 	struct asfd *asfd,
484 	struct asfd *chfd, struct slist *slist)
485 {
486 	int ar=0, aw=0, cr=0, cw=0;
487 
488 	setup_writes_from_slist(asfd, &aw, slist);
489 	asfd_assert_write(asfd, &aw, 0, CMD_GEN, "requests_end");
490 	asfd_mock_read_no_op(asfd, &ar, 12800);
491 	setup_reads_from_slist(asfd, &ar, slist, 1, 0);
492 	asfd_mock_read(asfd, &ar, 0, CMD_GEN, "sigs_end");
493 
494 	asfd_assert_write(asfd, &aw, 0, CMD_WRAP_UP, "BOJ");
495 	asfd_assert_write(asfd, &aw, 0, CMD_GEN, "blk_requests_end");
496 	asfd_mock_read_no_op(asfd, &ar, 25600);
497 	asfd_mock_read(asfd, &ar, 0, CMD_GEN, "backup_end");
498 
499 	setup_chfd_writes_from_slist(chfd, &cw, slist, 1, 0);
500 	asfd_mock_read_no_op(chfd, &cr, 25600);
501 	setup_chfd_reads_from_slist_blks_got(chfd, &cr, slist, 1, 0);
502 	asfd_mock_read_no_op(chfd, &cr, 25600);
503 	asfd_assert_write(chfd, &cw, 0, CMD_GEN, "sigs_end");
504 	asfd_assert_write(chfd, &cw, 0, CMD_MANIFEST, "utest_server_protocol2_backup_phase2/a_group/clients/utestclient/working/changed/00000000");
505 }
506 
setup_asfds_happy_path_three_blks_per_file_full_dedup(struct asfd * asfd,struct asfd * chfd,struct slist * slist)507 static void setup_asfds_happy_path_three_blks_per_file_full_dedup(
508 	struct asfd *asfd, struct asfd *chfd, struct slist *slist)
509 {
510 	int ar=0, aw=0, cr=0, cw=0;
511 
512 	setup_writes_from_slist(asfd, &aw, slist);
513 	asfd_assert_write(asfd, &aw, 0, CMD_GEN, "requests_end");
514 	asfd_mock_read_no_op(asfd, &ar, 200);
515 	setup_reads_from_slist(asfd, &ar, slist, 3, 0);
516 	asfd_mock_read(asfd, &ar, 0, CMD_GEN, "sigs_end");
517 
518 	asfd_assert_write(asfd, &aw, 0, CMD_GEN, "blk_requests_end");
519 	asfd_mock_read_no_op(asfd, &ar, 1000);
520 	asfd_mock_read(asfd, &ar, 0, CMD_GEN, "backup_end");
521 
522 	setup_chfd_writes_from_slist(chfd, &cw, slist, 3, 0);
523 	asfd_mock_read_no_op(chfd, &cr, 1000);
524 	setup_chfd_reads_from_slist_blks_got(chfd, &cr, slist, 3, 0);
525 	asfd_mock_read_no_op(chfd, &cr, 1000);
526 	asfd_assert_write(chfd, &cw, 0, CMD_GEN, "sigs_end");
527 }
528 
setup_asfds_happy_path_three_blks_per_file_full_dedup_big(struct asfd * asfd,struct asfd * chfd,struct slist * slist)529 static void setup_asfds_happy_path_three_blks_per_file_full_dedup_big(
530 	struct asfd *asfd,
531 	struct asfd *chfd, struct slist *slist)
532 {
533 	int ar=0, aw=0, cr=0, cw=0;
534 
535 	setup_writes_from_slist(asfd, &aw, slist);
536 	asfd_assert_write(asfd, &aw, 0, CMD_GEN, "requests_end");
537 	asfd_mock_read_no_op(asfd, &ar, 12800);
538 	setup_reads_from_slist(asfd, &ar, slist, 3, 0);
539 	asfd_mock_read(asfd, &ar, 0, CMD_GEN, "sigs_end");
540 
541 	asfd_assert_write(asfd, &aw, 0, CMD_WRAP_UP, "BOJ");
542 	asfd_assert_write(asfd, &aw, 0, CMD_GEN, "blk_requests_end");
543 	asfd_mock_read_no_op(asfd, &ar, 25600);
544 	asfd_mock_read(asfd, &ar, 0, CMD_GEN, "backup_end");
545 
546 	setup_chfd_writes_from_slist(chfd, &cw, slist, 3, 0);
547 	asfd_mock_read_no_op(chfd, &cr, 25600);
548 	setup_chfd_reads_from_slist_blks_got(chfd, &cr, slist, 3, 0);
549 	asfd_mock_read_no_op(chfd, &cr, 25600);
550 	asfd_assert_write(chfd, &cw, 0, CMD_GEN, "sigs_end");
551 	asfd_assert_write(chfd, &cw, 0, CMD_MANIFEST, "utest_server_protocol2_backup_phase2/a_group/clients/utestclient/working/changed/00000000");
552 }
553 
setup_asfds_happy_path_one_blk_per_file_no_dedup(struct asfd * asfd,struct asfd * chfd,struct slist * slist)554 static void setup_asfds_happy_path_one_blk_per_file_no_dedup(
555 	struct asfd *asfd, struct asfd *chfd, struct slist *slist)
556 {
557 	int ar=0, aw=0, cr=0, cw=0;
558 
559 	setup_writes_from_slist(asfd, &aw, slist);
560 	asfd_assert_write(asfd, &aw, 0, CMD_GEN, "requests_end");
561 	asfd_mock_read_no_op(asfd, &ar, 30);
562 	setup_reads_from_slist(asfd, &ar, slist, 1, 0);
563 	asfd_mock_read(asfd, &ar, 0, CMD_GEN, "sigs_end");
564 
565 	setup_writes_from_slist_blk_requests(asfd, &aw, slist, 1, 0);
566 	asfd_assert_write(asfd, &aw, 0, CMD_GEN, "blk_requests_end");
567 	asfd_mock_read_no_op(asfd, &ar, 60);
568 	setup_reads_from_slist_blks(asfd, &ar, slist, 1, 0);
569 	asfd_mock_read(asfd, &ar, 0, CMD_GEN, "backup_end");
570 
571 	setup_chfd_writes_from_slist(chfd, &cw, slist, 1, 0);
572 	asfd_mock_read_no_op(chfd, &cr, 60);
573 	setup_chfd_reads_from_slist_blks_not_got(chfd, &cr, slist, 1, 0);
574 	asfd_mock_read_no_op(chfd, &cr, 60);
575 	asfd_assert_write(chfd, &cw, 0, CMD_GEN, "sigs_end");
576 }
577 
setup_asfds_happy_path_one_blk_per_file_no_dedup_big(struct asfd * asfd,struct asfd * chfd,struct slist * slist)578 static void setup_asfds_happy_path_one_blk_per_file_no_dedup_big(
579 	struct asfd *asfd, struct asfd *chfd, struct slist *slist)
580 {
581 	int ar=0, aw=0, cr=0, cw=0;
582 
583 	setup_writes_from_slist(asfd, &aw, slist);
584 	asfd_assert_write(asfd, &aw, 0, CMD_GEN, "requests_end");
585 	asfd_mock_read_no_op(asfd, &ar, 12800);
586 	setup_reads_from_slist(asfd, &ar, slist, 1, 0);
587 	asfd_mock_read(asfd, &ar, 0, CMD_GEN, "sigs_end");
588 
589 	setup_writes_from_slist_blk_requests(asfd, &aw, slist, 1, 0);
590 	asfd_assert_write(asfd, &aw, 0, CMD_GEN, "blk_requests_end");
591 	asfd_mock_read_no_op(asfd, &ar, 35600);
592 	setup_reads_from_slist_blks(asfd, &ar, slist, 1, 0);
593 	asfd_mock_read(asfd, &ar, 0, CMD_GEN, "backup_end");
594 
595 	setup_chfd_writes_from_slist(chfd, &cw, slist, 1, 0);
596 	asfd_mock_read_no_op(chfd, &cr, 35600);
597 	setup_chfd_reads_from_slist_blks_not_got(chfd, &cr, slist, 1, 0);
598 	asfd_mock_read_no_op(chfd, &cr, 35600);
599 	asfd_assert_write(chfd, &cw, 0, CMD_GEN, "sigs_end");
600 	asfd_assert_write(chfd, &cw, 0, CMD_MANIFEST, "utest_server_protocol2_backup_phase2/a_group/clients/utestclient/working/changed/00000000");
601 }
602 
setup_asfds_happy_path_three_blks_per_file_no_dedup(struct asfd * asfd,struct asfd * chfd,struct slist * slist)603 static void setup_asfds_happy_path_three_blks_per_file_no_dedup(
604 	struct asfd *asfd, struct asfd *chfd, struct slist *slist)
605 {
606 	int ar=0, aw=0, cr=0, cw=0;
607 
608 	setup_writes_from_slist(asfd, &aw, slist);
609 	asfd_assert_write(asfd, &aw, 0, CMD_GEN, "requests_end");
610 	asfd_mock_read_no_op(asfd, &ar, 200);
611 	setup_reads_from_slist(asfd, &ar, slist, 3, 0);
612 	asfd_mock_read(asfd, &ar, 0, CMD_GEN, "sigs_end");
613 
614 	setup_writes_from_slist_blk_requests(asfd, &aw, slist, 3, 0);
615 	asfd_assert_write(asfd, &aw, 0, CMD_GEN, "blk_requests_end");
616 	asfd_mock_read_no_op(asfd, &ar, 1000);
617 	setup_reads_from_slist_blks(asfd, &ar, slist, 3, 0);
618 	asfd_mock_read(asfd, &ar, 0, CMD_GEN, "backup_end");
619 
620 	setup_chfd_writes_from_slist(chfd, &cw, slist, 3, 0);
621 	asfd_mock_read_no_op(chfd, &cr, 1000);
622 	setup_chfd_reads_from_slist_blks_not_got(chfd, &cr, slist, 3, 0);
623 	asfd_mock_read_no_op(chfd, &cr, 1000);
624 	asfd_assert_write(chfd, &cw, 0, CMD_GEN, "sigs_end");
625 }
626 
setup_asfds_happy_path_three_blks_per_file_no_dedup_big(struct asfd * asfd,struct asfd * chfd,struct slist * slist)627 static void setup_asfds_happy_path_three_blks_per_file_no_dedup_big(
628 	struct asfd *asfd, struct asfd *chfd, struct slist *slist)
629 {
630 	int ar=0, aw=0, cr=0, cw=0;
631 
632 	setup_writes_from_slist(asfd, &aw, slist);
633 	asfd_assert_write(asfd, &aw, 0, CMD_GEN, "requests_end");
634 	asfd_mock_read_no_op(asfd, &ar, 12800);
635 	setup_reads_from_slist(asfd, &ar, slist, 3, 0);
636 	asfd_mock_read(asfd, &ar, 0, CMD_GEN, "sigs_end");
637 
638 	setup_writes_from_slist_blk_requests(asfd, &aw, slist, 3, 0);
639 	asfd_assert_write(asfd, &aw, 0, CMD_GEN, "blk_requests_end");
640 	asfd_mock_read_no_op(asfd, &ar, 25600);
641 	setup_reads_from_slist_blks(asfd, &ar, slist, 3, 0);
642 	asfd_mock_read(asfd, &ar, 0, CMD_GEN, "backup_end");
643 
644 	setup_chfd_writes_from_slist(chfd, &cw, slist, 3, 0);
645 	asfd_mock_read_no_op(chfd, &cr, 25600);
646 	setup_chfd_reads_from_slist_blks_not_got(chfd, &cr, slist, 3, 0);
647 	asfd_mock_read_no_op(chfd, &cr, 25600);
648 	asfd_assert_write(chfd, &cw, 0, CMD_GEN, "sigs_end");
649 	asfd_assert_write(chfd, &cw, 0, CMD_MANIFEST, "utest_server_protocol2_backup_phase2/a_group/clients/utestclient/working/changed/00000000");
650 }
651 
setup_asfds_happy_path_one_blk_per_file_no_dedup_interrupt(struct asfd * asfd,struct asfd * chfd,struct slist * slist)652 static void setup_asfds_happy_path_one_blk_per_file_no_dedup_interrupt(
653 	struct asfd *asfd, struct asfd *chfd, struct slist *slist)
654 {
655 	int ar=0, aw=0, cr=0, cw=0;
656 
657 	setup_writes_from_slist(asfd, &aw, slist);
658 	asfd_assert_write(asfd, &aw, 0, CMD_GEN, "requests_end");
659 	asfd_mock_read_no_op(asfd, &ar, 30);
660 	setup_reads_from_slist(asfd, &ar, slist, 1, 2 /* interrupt */);
661 	asfd_mock_read(asfd, &ar, 0, CMD_GEN, "sigs_end");
662 
663 	setup_writes_from_slist_blk_requests(asfd,
664 		&aw, slist, 1, 2 /* interrupt */);
665 	asfd_assert_write(asfd, &aw, 0, CMD_GEN, "blk_requests_end");
666 	asfd_mock_read_no_op(asfd, &ar, 60);
667 	setup_reads_from_slist_blks(asfd, &ar, slist, 1, 2 /* interrupt */);
668 	asfd_mock_read(asfd, &ar, 0, CMD_GEN, "backup_end");
669 
670 	setup_chfd_writes_from_slist(chfd, &cw, slist, 1, 2 /* interrupt */);
671 	asfd_mock_read_no_op(chfd, &cr, 60);
672 	setup_chfd_reads_from_slist_blks_not_got(chfd,
673 		&cr, slist, 1, 2 /* interrupt */);
674 	asfd_mock_read_no_op(chfd, &cr, 60);
675 	asfd_assert_write(chfd, &cw, 0, CMD_GEN, "sigs_end");
676 }
677 
async_rw_simple(struct async * as)678 static int async_rw_simple(struct async *as)
679 {
680 	return as->asfd->read(as->asfd);
681 }
682 
async_rw_both(struct async * as)683 static int async_rw_both(struct async *as)
684 {
685 	int ret=0;
686 	struct asfd *asfd=as->asfd;
687 	struct asfd *chfd=asfd->next;
688 	ret|=asfd->read(asfd);
689 	ret|=chfd->read(chfd);
690 	return ret;
691 }
692 
run_test(int expected_ret,int manio_entries,int async_read_write_callback (struct async * as),void setup_asfds_callback (struct asfd * asfd,struct asfd * chfd,struct slist * slist))693 static void run_test(int expected_ret,
694 	int manio_entries,
695 	int async_read_write_callback(struct async *as),
696 	void setup_asfds_callback(struct asfd *asfd, struct asfd *chfd,
697 		struct slist *slist))
698 {
699 	struct asfd *asfd;
700 	struct asfd *chfd;
701 	struct async *as;
702 	struct sdirs *sdirs;
703 	struct conf **confs;
704 	struct slist *slist=NULL;
705 	prng_init(0);
706 	base64_init();
707 	hexmap_init();
708 	setup(&as, &sdirs, &confs);
709 	asfd=asfd_mock_setup(&areads, &awrites);
710 	chfd=asfd_mock_setup(&creads, &cwrites);
711 	fail_unless((asfd->desc=strdup_w("a", __func__))!=NULL);
712 	fail_unless((chfd->desc=strdup_w("c", __func__))!=NULL);
713 	as->asfd_add(as, asfd);
714 	as->asfd_add(as, chfd);
715 	as->read_write=async_read_write_callback;
716 
717 	if(manio_entries)
718 		slist=build_manifest(sdirs->phase1data,
719 			PROTO_2, manio_entries, 1 /*phase*/);
720 	setup_asfds_callback(asfd, chfd, slist);
721 
722 	fail_unless(do_backup_phase2_server_protocol2(
723 		as,
724 		chfd,
725 		sdirs,
726 		0, // resume
727 		confs
728 	)==expected_ret);
729 
730 	if(!expected_ret)
731 	{
732 		// FIX THIS: Should check for the presence and correctness of
733 		// changed and unchanged manios.
734 	}
735 	asfd_free(&asfd);
736 	asfd_free(&chfd);
737 	asfd_mock_teardown(&areads, &awrites);
738 	asfd_mock_teardown(&creads, &cwrites);
739 	slist_free(&slist);
740 	tear_down(&as, &sdirs, &confs);
741 }
742 
START_TEST(asfds_empty)743 START_TEST(asfds_empty)
744 {
745 	run_test(0, 0, async_rw_simple, setup_asfds_empty);
746 }
747 END_TEST
748 
START_TEST(asfds_empty_and_messages)749 START_TEST(asfds_empty_and_messages)
750 {
751 	run_test(0, 0, async_rw_simple, setup_asfds_empty_and_messages);
752 }
753 END_TEST
754 
START_TEST(asfds_data_too_soon)755 START_TEST(asfds_data_too_soon)
756 {
757 	run_test(-1, 0, async_rw_simple, setup_asfds_data_too_soon);
758 }
759 END_TEST
760 
START_TEST(asfds_write_error)761 START_TEST(asfds_write_error)
762 {
763 	run_test(-1, 0, async_rw_simple, setup_asfds_write_error);
764 }
765 END_TEST
766 
START_TEST(asfds_write_error_chfd)767 START_TEST(asfds_write_error_chfd)
768 {
769 	run_test(-1, 0, async_rw_simple, setup_asfds_write_error_chfd);
770 }
771 END_TEST
772 
START_TEST(asfds_read_error)773 START_TEST(asfds_read_error)
774 {
775 	run_test(-1, 0, async_rw_simple, setup_asfds_read_error);
776 }
777 END_TEST
778 
START_TEST(asfds_read_error_chfd)779 START_TEST(asfds_read_error_chfd)
780 {
781 	run_test(-1, 0, async_rw_both, setup_asfds_read_error_chfd);
782 }
783 END_TEST
784 
START_TEST(asfds_chfd_bad_cmd)785 START_TEST(asfds_chfd_bad_cmd)
786 {
787 	run_test(-1, 0, async_rw_both, setup_asfds_chfd_bad_cmd);
788 }
789 END_TEST
790 
START_TEST(asfds_no_sigs_from_client)791 START_TEST(asfds_no_sigs_from_client)
792 {
793 	run_test(-1, 20, async_rw_simple, setup_asfds_no_sigs_from_client);
794 }
795 END_TEST
796 
START_TEST(asfds_happy_path_one_blk_per_file_full_dedup)797 START_TEST(asfds_happy_path_one_blk_per_file_full_dedup)
798 {
799 	run_test(0, 20, async_rw_both,
800 		setup_asfds_happy_path_one_blk_per_file_full_dedup);
801 }
802 END_TEST
803 
START_TEST(asfds_happy_path_one_blk_per_file_full_dedup_big)804 START_TEST(asfds_happy_path_one_blk_per_file_full_dedup_big)
805 {
806 	run_test(0, 12800, async_rw_both,
807 		setup_asfds_happy_path_one_blk_per_file_full_dedup_big);
808 }
809 END_TEST
810 
START_TEST(asfds_happy_path_three_blks_per_file_full_dedup)811 START_TEST(asfds_happy_path_three_blks_per_file_full_dedup)
812 {
813 	run_test(0, 20, async_rw_both,
814 		setup_asfds_happy_path_three_blks_per_file_full_dedup);
815 }
816 END_TEST
817 
START_TEST(asfds_happy_path_three_blks_per_file_full_dedup_big)818 START_TEST(asfds_happy_path_three_blks_per_file_full_dedup_big)
819 {
820 	run_test(0, 4000, async_rw_both,
821 		setup_asfds_happy_path_three_blks_per_file_full_dedup_big);
822 }
823 END_TEST
824 
START_TEST(asfds_happy_path_one_blk_per_file_no_dedup)825 START_TEST(asfds_happy_path_one_blk_per_file_no_dedup)
826 {
827 	run_test(0, 20, async_rw_both,
828 		setup_asfds_happy_path_one_blk_per_file_no_dedup);
829 }
830 END_TEST
831 
START_TEST(asfds_happy_path_one_blk_per_file_no_dedup_big)832 START_TEST(asfds_happy_path_one_blk_per_file_no_dedup_big)
833 {
834 	run_test(0, 12800, async_rw_both,
835 		setup_asfds_happy_path_one_blk_per_file_no_dedup_big);
836 }
837 END_TEST
838 
START_TEST(asfds_happy_path_three_blks_per_file_no_dedup)839 START_TEST(asfds_happy_path_three_blks_per_file_no_dedup)
840 {
841 	run_test(0, 20, async_rw_both,
842 		setup_asfds_happy_path_three_blks_per_file_no_dedup);
843 }
844 END_TEST
845 
START_TEST(asfds_happy_path_three_blks_per_file_no_dedup_big)846 START_TEST(asfds_happy_path_three_blks_per_file_no_dedup_big)
847 {
848 	run_test(0, 4000, async_rw_both,
849 		setup_asfds_happy_path_three_blks_per_file_no_dedup_big);
850 }
851 END_TEST
852 
START_TEST(asfds_happy_path_one_blk_per_file_no_dedup_interrupt)853 START_TEST(asfds_happy_path_one_blk_per_file_no_dedup_interrupt)
854 {
855 	run_test(0, 20, async_rw_both,
856 		setup_asfds_happy_path_one_blk_per_file_no_dedup_interrupt);
857 }
858 END_TEST
859 
suite_server_protocol2_backup_phase2(void)860 Suite *suite_server_protocol2_backup_phase2(void)
861 {
862 	Suite *s;
863 	TCase *tc_core;
864 
865 	s=suite_create("server_protocol2_backup_phase2");
866 
867 	tc_core=tcase_create("Core");
868 	tcase_set_timeout(tc_core, 60);
869 
870 	tcase_add_test(tc_core, test_phase2_unset_as_sdirs_confs);
871 	tcase_add_test(tc_core, test_phase2_unset_sdirs_confs);
872 	tcase_add_test(tc_core, test_phase2_unset_confs);
873 	tcase_add_test(tc_core, test_phase2_unset_sdirs);
874 	tcase_add_test(tc_core, test_phase2_unset_asfd);
875 	tcase_add_test(tc_core, test_phase2_unset_chfd);
876 
877 	tcase_add_test(tc_core, asfds_empty);
878 	tcase_add_test(tc_core, asfds_empty_and_messages);
879 	tcase_add_test(tc_core, asfds_data_too_soon);
880 	tcase_add_test(tc_core, asfds_write_error);
881 	tcase_add_test(tc_core, asfds_write_error_chfd);
882 	tcase_add_test(tc_core, asfds_read_error);
883 	tcase_add_test(tc_core, asfds_read_error_chfd);
884 	tcase_add_test(tc_core, asfds_chfd_bad_cmd);
885 	tcase_add_test(tc_core, asfds_no_sigs_from_client);
886 
887 	tcase_add_test(tc_core,
888 		asfds_happy_path_one_blk_per_file_full_dedup);
889 	tcase_add_test(tc_core,
890 		asfds_happy_path_one_blk_per_file_full_dedup_big);
891 	tcase_add_test(tc_core,
892 		asfds_happy_path_three_blks_per_file_full_dedup);
893 	tcase_add_test(tc_core,
894 		asfds_happy_path_three_blks_per_file_full_dedup_big);
895 
896 	tcase_add_test(tc_core,
897 		asfds_happy_path_one_blk_per_file_no_dedup);
898 	tcase_add_test(tc_core,
899 		asfds_happy_path_one_blk_per_file_no_dedup_big);
900 	tcase_add_test(tc_core,
901 		asfds_happy_path_three_blks_per_file_no_dedup);
902 	tcase_add_test(tc_core,
903 		asfds_happy_path_three_blks_per_file_no_dedup_big);
904 
905 	tcase_add_test(tc_core,
906 		asfds_happy_path_one_blk_per_file_no_dedup_interrupt);
907 
908 	suite_add_tcase(s, tc_core);
909 
910 	return s;
911 }
912