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
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
|
/* Copyright (c) 2014, Daniel Martí
* Copyright (c) 2014, The Tor Project, Inc. */
/* See LICENSE for licensing information */
#include "or.h"
#include "test.h"
#include "consdiff.c"
#ifndef OP_EQ
#define OP_EQ ==
#endif
#ifndef OP_NE
#define OP_NE !=
#endif
static void
test_consdiff_smartlist_slice(void *arg)
{
smartlist_t *sl = smartlist_new();
smartlist_slice_t *sls;
/* Create a regular smartlist. */
(void)arg;
smartlist_add(sl, (void*)1);
smartlist_add(sl, (void*)2);
smartlist_add(sl, (void*)3);
smartlist_add(sl, (void*)4);
smartlist_add(sl, (void*)5);
/* See if the slice was done correctly. */
sls = smartlist_slice(sl, 2, 5);
tt_ptr_op(sl, OP_EQ, sls->list);
tt_ptr_op((void*)3, OP_EQ, smartlist_get(sls->list, sls->offset));
tt_ptr_op((void*)5, OP_EQ,
smartlist_get(sls->list, sls->offset + (sls->len-1)));
tor_free(sls);
/* See that using -1 as the end does get to the last element. */
sls = smartlist_slice(sl, 2, -1);
tt_ptr_op(sl, OP_EQ, sls->list);
tt_ptr_op((void*)3, OP_EQ, smartlist_get(sls->list, sls->offset));
tt_ptr_op((void*)5, OP_EQ,
smartlist_get(sls->list, sls->offset + (sls->len-1)));
done:
tor_free(sls);
smartlist_free(sl);
}
static void
test_consdiff_smartlist_slice_string_pos(void *arg)
{
smartlist_t *sl = smartlist_new();
smartlist_slice_t *sls;
/* Create a regular smartlist. */
(void)arg;
smartlist_split_string(sl, "a:d:c:a:b", ":", 0, 0);
/* See that smartlist_slice_string_pos respects the bounds of the slice. */
sls = smartlist_slice(sl, 2, 5);
tt_int_op(3, OP_EQ, smartlist_slice_string_pos(sls, "a"));
tt_int_op(-1, OP_EQ, smartlist_slice_string_pos(sls, "d"));
done:
tor_free(sls);
if (sl) SMARTLIST_FOREACH(sl, char*, line, tor_free(line));
smartlist_free(sl);
}
static void
test_consdiff_lcs_lengths(void *arg)
{
smartlist_t *sl1 = smartlist_new();
smartlist_t *sl2 = smartlist_new();
smartlist_slice_t *sls1, *sls2;
int *lengths1, *lengths2;
/* Expected lcs lengths in regular and reverse order. */
int e_lengths1[] = { 0, 1, 2, 3, 3, 4 };
int e_lengths2[] = { 0, 1, 1, 2, 3, 4 };
(void)arg;
smartlist_split_string(sl1, "a:b:c:d:e", ":", 0, 0);
smartlist_split_string(sl2, "a:c:d:i:e", ":", 0, 0);
sls1 = smartlist_slice(sl1, 0, -1);
sls2 = smartlist_slice(sl2, 0, -1);
lengths1 = lcs_lengths(sls1, sls2, 1);
lengths2 = lcs_lengths(sls1, sls2, -1);
tt_mem_op(e_lengths1, OP_EQ, lengths1, sizeof(int) * 6);
tt_mem_op(e_lengths2, OP_EQ, lengths2, sizeof(int) * 6);
done:
tor_free(lengths1);
tor_free(lengths2);
tor_free(sls1);
tor_free(sls2);
if (sl1) SMARTLIST_FOREACH(sl1, char*, line, tor_free(line));
if (sl2) SMARTLIST_FOREACH(sl2, char*, line, tor_free(line));
smartlist_free(sl1);
smartlist_free(sl2);
}
static void
test_consdiff_trim_slices(void *arg)
{
smartlist_t *sl1 = smartlist_new();
smartlist_t *sl2 = smartlist_new();
smartlist_t *sl3 = smartlist_new();
smartlist_t *sl4 = smartlist_new();
smartlist_slice_t *sls1, *sls2, *sls3, *sls4;
(void)arg;
smartlist_split_string(sl1, "a:b:b:b:d", ":", 0, 0);
smartlist_split_string(sl2, "a:c:c:c:d", ":", 0, 0);
smartlist_split_string(sl3, "a:b:b:b:a", ":", 0, 0);
smartlist_split_string(sl4, "c:b:b:b:c", ":", 0, 0);
sls1 = smartlist_slice(sl1, 0, -1);
sls2 = smartlist_slice(sl2, 0, -1);
sls3 = smartlist_slice(sl3, 0, -1);
sls4 = smartlist_slice(sl4, 0, -1);
/* They should be trimmed by one line at each end. */
tt_int_op(5, OP_EQ, sls1->len);
tt_int_op(5, OP_EQ, sls2->len);
trim_slices(sls1, sls2);
tt_int_op(3, OP_EQ, sls1->len);
tt_int_op(3, OP_EQ, sls2->len);
/* They should not be trimmed at all. */
tt_int_op(5, OP_EQ, sls3->len);
tt_int_op(5, OP_EQ, sls4->len);
trim_slices(sls3, sls4);
tt_int_op(5, OP_EQ, sls3->len);
tt_int_op(5, OP_EQ, sls4->len);
done:
tor_free(sls1);
tor_free(sls2);
tor_free(sls3);
tor_free(sls4);
if (sl1) SMARTLIST_FOREACH(sl1, char*, line, tor_free(line));
if (sl2) SMARTLIST_FOREACH(sl2, char*, line, tor_free(line));
if (sl3) SMARTLIST_FOREACH(sl3, char*, line, tor_free(line));
if (sl4) SMARTLIST_FOREACH(sl4, char*, line, tor_free(line));
smartlist_free(sl1);
smartlist_free(sl2);
smartlist_free(sl3);
smartlist_free(sl4);
}
static void
test_consdiff_set_changed(void *arg)
{
smartlist_t *sl1 = smartlist_new();
smartlist_t *sl2 = smartlist_new();
bitarray_t *changed1 = bitarray_init_zero(4);
bitarray_t *changed2 = bitarray_init_zero(4);
smartlist_slice_t *sls1, *sls2;
(void)arg;
smartlist_split_string(sl1, "a:b:a:a", ":", 0, 0);
smartlist_split_string(sl2, "a:a:a:a", ":", 0, 0);
/* Length of sls1 is 0. */
sls1 = smartlist_slice(sl1, 0, 0);
sls2 = smartlist_slice(sl2, 1, 3);
set_changed(changed1, changed2, sls1, sls2);
/* The former is not changed, the latter changes all of its elements. */
tt_assert(!bitarray_is_set(changed1, 0));
tt_assert(!bitarray_is_set(changed1, 1));
tt_assert(!bitarray_is_set(changed1, 2));
tt_assert(!bitarray_is_set(changed1, 3));
tt_assert(!bitarray_is_set(changed2, 0));
tt_assert(bitarray_is_set(changed2, 1));
tt_assert(bitarray_is_set(changed2, 2));
tt_assert(!bitarray_is_set(changed2, 3));
bitarray_clear(changed2, 1);
bitarray_clear(changed2, 2);
/* Length of sls1 is 1 and its element is in sls2. */
tor_free(sls1);
sls1 = smartlist_slice(sl1, 0, 1);
set_changed(changed1, changed2, sls1, sls2);
/* The latter changes all elements but the (first) common one. */
tt_assert(!bitarray_is_set(changed1, 0));
tt_assert(!bitarray_is_set(changed1, 1));
tt_assert(!bitarray_is_set(changed1, 2));
tt_assert(!bitarray_is_set(changed1, 3));
tt_assert(!bitarray_is_set(changed2, 0));
tt_assert(!bitarray_is_set(changed2, 1));
tt_assert(bitarray_is_set(changed2, 2));
tt_assert(!bitarray_is_set(changed2, 3));
bitarray_clear(changed2, 2);
/* Length of sls1 is 1 and its element is not in sls2. */
tor_free(sls1);
sls1 = smartlist_slice(sl1, 1, 2);
set_changed(changed1, changed2, sls1, sls2);
/* The former changes its element, the latter changes all elements. */
tt_assert(!bitarray_is_set(changed1, 0));
tt_assert(bitarray_is_set(changed1, 1));
tt_assert(!bitarray_is_set(changed1, 2));
tt_assert(!bitarray_is_set(changed1, 3));
tt_assert(!bitarray_is_set(changed2, 0));
tt_assert(bitarray_is_set(changed2, 1));
tt_assert(bitarray_is_set(changed2, 2));
tt_assert(!bitarray_is_set(changed2, 3));
done:
bitarray_free(changed1);
bitarray_free(changed2);
if (sl1) SMARTLIST_FOREACH(sl1, char*, line, tor_free(line));
if (sl2) SMARTLIST_FOREACH(sl2, char*, line, tor_free(line));
smartlist_free(sl1);
smartlist_free(sl2);
tor_free(sls1);
tor_free(sls2);
}
static void
test_consdiff_calc_changes(void *arg)
{
smartlist_t *sl1 = smartlist_new();
smartlist_t *sl2 = smartlist_new();
smartlist_slice_t *sls1, *sls2;
bitarray_t *changed1 = bitarray_init_zero(4);
bitarray_t *changed2 = bitarray_init_zero(4);
(void)arg;
smartlist_split_string(sl1, "a:a:a:a", ":", 0, 0);
smartlist_split_string(sl2, "a:a:a:a", ":", 0, 0);
sls1 = smartlist_slice(sl1, 0, -1);
sls2 = smartlist_slice(sl2, 0, -1);
calc_changes(sls1, sls2, changed1, changed2);
/* Nothing should be set to changed. */
tt_assert(!bitarray_is_set(changed1, 0));
tt_assert(!bitarray_is_set(changed1, 1));
tt_assert(!bitarray_is_set(changed1, 2));
tt_assert(!bitarray_is_set(changed1, 3));
tt_assert(!bitarray_is_set(changed2, 0));
tt_assert(!bitarray_is_set(changed2, 1));
tt_assert(!bitarray_is_set(changed2, 2));
tt_assert(!bitarray_is_set(changed2, 3));
SMARTLIST_FOREACH(sl2, char*, line, tor_free(line));
smartlist_clear(sl2);
smartlist_split_string(sl2, "a:b:a:b", ":", 0, 0);
tor_free(sls1);
tor_free(sls2);
sls1 = smartlist_slice(sl1, 0, -1);
sls2 = smartlist_slice(sl2, 0, -1);
calc_changes(sls1, sls2, changed1, changed2);
/* Two elements are changed. */
tt_assert(!bitarray_is_set(changed1, 0));
tt_assert(bitarray_is_set(changed1, 1));
tt_assert(bitarray_is_set(changed1, 2));
tt_assert(!bitarray_is_set(changed1, 3));
bitarray_clear(changed1, 1);
bitarray_clear(changed1, 2);
tt_assert(!bitarray_is_set(changed2, 0));
tt_assert(bitarray_is_set(changed2, 1));
tt_assert(!bitarray_is_set(changed2, 2));
tt_assert(bitarray_is_set(changed2, 3));
bitarray_clear(changed1, 1);
bitarray_clear(changed1, 3);
SMARTLIST_FOREACH(sl2, char*, line, tor_free(line));
smartlist_clear(sl2);
smartlist_split_string(sl2, "b:b:b:b", ":", 0, 0);
tor_free(sls1);
tor_free(sls2);
sls1 = smartlist_slice(sl1, 0, -1);
sls2 = smartlist_slice(sl2, 0, -1);
calc_changes(sls1, sls2, changed1, changed2);
/* All elements are changed. */
tt_assert(bitarray_is_set(changed1, 0));
tt_assert(bitarray_is_set(changed1, 1));
tt_assert(bitarray_is_set(changed1, 2));
tt_assert(bitarray_is_set(changed1, 3));
tt_assert(bitarray_is_set(changed2, 0));
tt_assert(bitarray_is_set(changed2, 1));
tt_assert(bitarray_is_set(changed2, 2));
tt_assert(bitarray_is_set(changed2, 3));
done:
bitarray_free(changed1);
bitarray_free(changed2);
if (sl1) SMARTLIST_FOREACH(sl1, char*, line, tor_free(line));
if (sl2) SMARTLIST_FOREACH(sl2, char*, line, tor_free(line));
smartlist_free(sl1);
smartlist_free(sl2);
tor_free(sls1);
tor_free(sls2);
}
static void
test_consdiff_get_id_hash(void *arg)
{
const char *line, *e_hash;
/* No hash. */
(void)arg;
tt_ptr_op(NULL, OP_EQ, get_id_hash("r name"));
/* The hash contains characters that are not base64. */
tt_ptr_op(NULL, OP_EQ, get_id_hash( "r name _hash_isnt_base64 etc"));
line = "r name hash+valid+base64 etc";
e_hash = line+7;
tt_ptr_op(e_hash, OP_EQ, get_id_hash(line));
done:
;
}
static void
test_consdiff_is_valid_router_entry(void *arg)
{
/* Doesn't start with "r ". */
(void)arg;
tt_int_op(0, OP_EQ, is_valid_router_entry("foo"));
/* These are already tested with get_id_hash, but make sure it's run
* properly. */
tt_int_op(0, OP_EQ, is_valid_router_entry("r name"));
tt_int_op(0, OP_EQ, is_valid_router_entry("r name _hash_isnt_base64 etc"));
tt_int_op(1, OP_EQ, is_valid_router_entry("r name hash+valid+base64 etc"));
done:
;
}
static void
test_consdiff_next_router(void *arg)
{
smartlist_t *sl = smartlist_new();
(void)arg;
smartlist_add(sl, (char*)"foo");
smartlist_add(sl,
(char*)"r name hash+longer+than+27+chars+and+valid+base64 etc");
smartlist_add(sl, (char*)"foo");
smartlist_add(sl, (char*)"foo");
smartlist_add(sl,
(char*)"r name hash+longer+than+27+chars+and+valid+base64 etc");
smartlist_add(sl, (char*)"foo");
/* Not currently on a router entry line, finding the next one. */
tt_int_op(1, OP_EQ, next_router(sl, 0));
tt_int_op(4, OP_EQ, next_router(sl, 2));
/* Already at the beginning of a router entry line, ignore it. */
tt_int_op(4, OP_EQ, next_router(sl, 1));
/* There are no more router entries, so return the line after the last. */
tt_int_op(6, OP_EQ, next_router(sl, 4));
tt_int_op(6, OP_EQ, next_router(sl, 5));
done:
smartlist_free(sl);
}
static void
test_consdiff_base64cmp(void *arg)
{
/* NULL arguments. */
(void)arg;
tt_int_op(0, OP_EQ, base64cmp(NULL, NULL));
tt_int_op(-1, OP_EQ, base64cmp(NULL, "foo"));
tt_int_op(1, OP_EQ, base64cmp("bar", NULL));
/* Nil base64 values. */
tt_int_op(0, OP_EQ, base64cmp("", ""));
tt_int_op(0, OP_EQ, base64cmp("_", "&"));
/* Exact same valid strings. */
tt_int_op(0, OP_EQ, base64cmp("abcABC/+", "abcABC/+"));
/* Both end with an invalid base64 char other than '\0'. */
tt_int_op(0, OP_EQ, base64cmp("abcABC/+ ", "abcABC/+ "));
/* Only one ends with an invalid base64 char other than '\0'. */
tt_int_op(0, OP_EQ, base64cmp("abcABC/+ ", "abcABC/+"));
/* Comparisons that would return differently with strcmp(). */
tt_int_op(-1, OP_EQ, strcmp("/foo", "Afoo"));
tt_int_op(1, OP_EQ, base64cmp("/foo", "Afoo"));
tt_int_op(1, OP_EQ, strcmp("Afoo", "0foo"));
tt_int_op(-1, OP_EQ, base64cmp("Afoo", "0foo"));
/* Comparisons that would return the same as with strcmp(). */
tt_int_op(1, OP_EQ, strcmp("afoo", "Afoo"));
tt_int_op(1, OP_EQ, base64cmp("afoo", "Afoo"));
done:
;
}
static void
test_consdiff_gen_ed_diff(void *arg)
{
smartlist_t *cons1=NULL, *cons2=NULL, *diff=NULL;
int i;
(void)arg;
cons1 = smartlist_new();
cons2 = smartlist_new();
/* Identity hashes are not sorted properly, return NULL. */
smartlist_add(cons1, (char*)"r name bbbbbbbbbbbbbbbbbbbbbbbbbbb etc");
smartlist_add(cons1, (char*)"foo");
smartlist_add(cons1, (char*)"r name aaaaaaaaaaaaaaaaaaaaaaaaaaa etc");
smartlist_add(cons1, (char*)"bar");
smartlist_add(cons2, (char*)"r name aaaaaaaaaaaaaaaaaaaaaaaaaaa etc");
smartlist_add(cons2, (char*)"foo");
smartlist_add(cons2, (char*)"r name ccccccccccccccccccccccccccc etc");
smartlist_add(cons2, (char*)"bar");
diff = gen_ed_diff(cons1, cons2);
tt_ptr_op(NULL, OP_EQ, diff);
/* Same, but now with the second consensus. */
diff = gen_ed_diff(cons2, cons1);
tt_ptr_op(NULL, OP_EQ, diff);
/* Identity hashes are repeated, return NULL. */
smartlist_clear(cons1);
smartlist_add(cons1, (char*)"r name bbbbbbbbbbbbbbbbbbbbbbbbbbb etc");
smartlist_add(cons1, (char*)"foo");
smartlist_add(cons1, (char*)"r name bbbbbbbbbbbbbbbbbbbbbbbbbbb etc");
smartlist_add(cons1, (char*)"bar");
diff = gen_ed_diff(cons1, cons2);
tt_ptr_op(NULL, OP_EQ, diff);
/* We have to add a line that is just a dot, return NULL. */
smartlist_clear(cons1);
smartlist_clear(cons2);
smartlist_add(cons1, (char*)"foo1");
smartlist_add(cons1, (char*)"foo2");
smartlist_add(cons2, (char*)"foo1");
smartlist_add(cons2, (char*)".");
smartlist_add(cons2, (char*)"foo2");
diff = gen_ed_diff(cons1, cons2);
tt_ptr_op(NULL, OP_EQ, diff);
#define MAX_LINE_COUNT (10000)
/* Too many lines to be fed to the quadratic-time function. */
smartlist_clear(cons1);
smartlist_clear(cons2);
for (i=0; i < MAX_LINE_COUNT; ++i) smartlist_add(cons1, (char*)"a");
for (i=0; i < MAX_LINE_COUNT; ++i) smartlist_add(cons1, (char*)"b");
diff = gen_ed_diff(cons1, cons2);
tt_ptr_op(NULL, OP_EQ, diff);
/* We have dot lines, but they don't interfere with the script format. */
smartlist_clear(cons1);
smartlist_clear(cons2);
smartlist_add(cons1, (char*)"foo1");
smartlist_add(cons1, (char*)".");
smartlist_add(cons1, (char*)".");
smartlist_add(cons1, (char*)"foo2");
smartlist_add(cons2, (char*)"foo1");
smartlist_add(cons2, (char*)".");
smartlist_add(cons2, (char*)"foo2");
diff = gen_ed_diff(cons1, cons2);
tt_ptr_op(NULL, OP_NE, diff);
SMARTLIST_FOREACH(diff, char*, line, tor_free(line));
smartlist_free(diff);
/* Empty diff tests. */
smartlist_clear(cons1);
smartlist_clear(cons2);
diff = gen_ed_diff(cons1, cons2);
tt_ptr_op(NULL, !=, diff);
tt_int_op(0, OP_EQ, smartlist_len(diff));
smartlist_free(diff);
smartlist_add(cons1, (char*)"foo");
smartlist_add(cons1, (char*)"bar");
smartlist_add(cons2, (char*)"foo");
smartlist_add(cons2, (char*)"bar");
diff = gen_ed_diff(cons1, cons2);
tt_ptr_op(NULL, OP_NE, diff);
tt_int_op(0, OP_EQ, smartlist_len(diff));
smartlist_free(diff);
/* Everything is deleted. */
smartlist_clear(cons2);
diff = gen_ed_diff(cons1, cons2);
tt_ptr_op(NULL, OP_NE, diff);
tt_int_op(1, OP_EQ, smartlist_len(diff));
tt_str_op("1,2d", OP_EQ, smartlist_get(diff, 0));
SMARTLIST_FOREACH(diff, char*, line, tor_free(line));
smartlist_free(diff);
/* Everything is added. */
diff = gen_ed_diff(cons2, cons1);
tt_ptr_op(NULL, OP_NE, diff);
tt_int_op(4, OP_EQ, smartlist_len(diff));
tt_str_op("0a", OP_EQ, smartlist_get(diff, 0));
tt_str_op("foo", OP_EQ, smartlist_get(diff, 1));
tt_str_op("bar", OP_EQ, smartlist_get(diff, 2));
tt_str_op(".", OP_EQ, smartlist_get(diff, 3));
SMARTLIST_FOREACH(diff, char*, line, tor_free(line));
smartlist_free(diff);
/* Everything is changed. */
smartlist_add(cons2, (char*)"foo2");
smartlist_add(cons2, (char*)"bar2");
diff = gen_ed_diff(cons1, cons2);
tt_ptr_op(NULL, OP_NE, diff);
tt_int_op(4, OP_EQ, smartlist_len(diff));
tt_str_op("1,2c", OP_EQ, smartlist_get(diff, 0));
tt_str_op("foo2", OP_EQ, smartlist_get(diff, 1));
tt_str_op("bar2", OP_EQ, smartlist_get(diff, 2));
tt_str_op(".", OP_EQ, smartlist_get(diff, 3));
SMARTLIST_FOREACH(diff, char*, line, tor_free(line));
smartlist_free(diff);
/* Test 'a', 'c' and 'd' together. See that it is done in reverse order. */
smartlist_clear(cons1);
smartlist_clear(cons2);
smartlist_split_string(cons1, "A:B:C:D:E", ":", 0, 0);
smartlist_split_string(cons2, "A:C:O:E:U", ":", 0, 0);
diff = gen_ed_diff(cons1, cons2);
tt_ptr_op(NULL, OP_NE, diff);
tt_int_op(7, OP_EQ, smartlist_len(diff));
tt_str_op("5a", OP_EQ, smartlist_get(diff, 0));
tt_str_op("U", OP_EQ, smartlist_get(diff, 1));
tt_str_op(".", OP_EQ, smartlist_get(diff, 2));
tt_str_op("4c", OP_EQ, smartlist_get(diff, 3));
tt_str_op("O", OP_EQ, smartlist_get(diff, 4));
tt_str_op(".", OP_EQ, smartlist_get(diff, 5));
tt_str_op("2d", OP_EQ, smartlist_get(diff, 6));
/* TODO: small real use-cases, i.e. consensuses. */
done:
if (cons1) SMARTLIST_FOREACH(cons1, char*, line, tor_free(line));
if (cons2) SMARTLIST_FOREACH(cons2, char*, line, tor_free(line));
smartlist_free(cons1);
smartlist_free(cons2);
if (diff) SMARTLIST_FOREACH(diff, char*, line, tor_free(line));
smartlist_free(diff);
}
static void
test_consdiff_apply_ed_diff(void *arg)
{
smartlist_t *cons1=NULL, *cons2=NULL, *diff=NULL;
(void)arg;
cons1 = smartlist_new();
diff = smartlist_new();
smartlist_split_string(cons1, "A:B:C:D:E", ":", 0, 0);
/* Command without range. */
smartlist_add(diff, (char*)"a");
cons2 = apply_ed_diff(cons1, diff);
tt_ptr_op(NULL, OP_EQ, cons2);
smartlist_clear(diff);
/* Range without command. */
smartlist_add(diff, (char*)"1");
cons2 = apply_ed_diff(cons1, diff);
tt_ptr_op(NULL, OP_EQ, cons2);
smartlist_clear(diff);
/* Range without end. */
smartlist_add(diff, (char*)"1,");
cons2 = apply_ed_diff(cons1, diff);
tt_ptr_op(NULL, OP_EQ, cons2);
smartlist_clear(diff);
/* Incoherent ranges. */
smartlist_add(diff, (char*)"1,1");
cons2 = apply_ed_diff(cons1, diff);
tt_ptr_op(NULL, OP_EQ, cons2);
smartlist_clear(diff);
smartlist_add(diff, (char*)"3,2");
cons2 = apply_ed_diff(cons1, diff);
tt_ptr_op(NULL, OP_EQ, cons2);
smartlist_clear(diff);
/* Script is not in reverse order. */
smartlist_add(diff, (char*)"1d");
smartlist_add(diff, (char*)"3d");
cons2 = apply_ed_diff(cons1, diff);
tt_ptr_op(NULL, OP_EQ, cons2);
smartlist_clear(diff);
/* Script contains unrecognised commands longer than one char. */
smartlist_add(diff, (char*)"1foo");
cons2 = apply_ed_diff(cons1, diff);
tt_ptr_op(NULL, OP_EQ, cons2);
smartlist_clear(diff);
/* Script contains unrecognised commands. */
smartlist_add(diff, (char*)"1e");
cons2 = apply_ed_diff(cons1, diff);
tt_ptr_op(NULL, OP_EQ, cons2);
smartlist_clear(diff);
/* Command that should be followed by at least one line and a ".", but
* isn't. */
smartlist_add(diff, (char*)"0a");
cons2 = apply_ed_diff(cons1, diff);
tt_ptr_op(NULL, OP_EQ, cons2);
/* Now it is followed by a ".", but it inserts zero lines. */
smartlist_add(diff, (char*)".");
cons2 = apply_ed_diff(cons1, diff);
tt_ptr_op(NULL, OP_EQ, cons2);
smartlist_clear(diff);
/* Test appending text, 'a'. */
smartlist_split_string(diff, "3a:U:O:.:0a:V:.", ":", 0, 0);
cons2 = apply_ed_diff(cons1, diff);
tt_ptr_op(NULL, OP_NE, cons2);
tt_int_op(8, OP_EQ, smartlist_len(cons2));
tt_str_op("V", OP_EQ, smartlist_get(cons2, 0));
tt_str_op("A", OP_EQ, smartlist_get(cons2, 1));
tt_str_op("B", OP_EQ, smartlist_get(cons2, 2));
tt_str_op("C", OP_EQ, smartlist_get(cons2, 3));
tt_str_op("U", OP_EQ, smartlist_get(cons2, 4));
tt_str_op("O", OP_EQ, smartlist_get(cons2, 5));
tt_str_op("D", OP_EQ, smartlist_get(cons2, 6));
tt_str_op("E", OP_EQ, smartlist_get(cons2, 7));
SMARTLIST_FOREACH(diff, char*, line, tor_free(line));
smartlist_clear(diff);
SMARTLIST_FOREACH(cons2, char*, line, tor_free(line));
smartlist_free(cons2);
/* Test deleting text, 'd'. */
smartlist_split_string(diff, "4d:1,2d", ":", 0, 0);
cons2 = apply_ed_diff(cons1, diff);
tt_ptr_op(NULL, OP_NE, cons2);
tt_int_op(2, OP_EQ, smartlist_len(cons2));
tt_str_op("C", OP_EQ, smartlist_get(cons2, 0));
tt_str_op("E", OP_EQ, smartlist_get(cons2, 1));
SMARTLIST_FOREACH(diff, char*, line, tor_free(line));
smartlist_clear(diff);
SMARTLIST_FOREACH(cons2, char*, line, tor_free(line));
smartlist_free(cons2);
/* Test changing text, 'c'. */
smartlist_split_string(diff, "4c:T:X:.:1, 2c:M:.", ":", 0, 0);
cons2 = apply_ed_diff(cons1, diff);
tt_ptr_op(NULL, OP_NE, cons2);
tt_int_op(5, OP_EQ, smartlist_len(cons2));
tt_str_op("M", OP_EQ, smartlist_get(cons2, 0));
tt_str_op("C", OP_EQ, smartlist_get(cons2, 1));
tt_str_op("T", OP_EQ, smartlist_get(cons2, 2));
tt_str_op("X", OP_EQ, smartlist_get(cons2, 3));
tt_str_op("E", OP_EQ, smartlist_get(cons2, 4));
SMARTLIST_FOREACH(diff, char*, line, tor_free(line));
smartlist_clear(diff);
SMARTLIST_FOREACH(cons2, char*, line, tor_free(line));
smartlist_free(cons2);
/* Test 'a', 'd' and 'c' together. */
smartlist_split_string(diff, "4c:T:X:.:2d:0a:M:.", ":", 0, 0);
cons2 = apply_ed_diff(cons1, diff);
tt_ptr_op(NULL, OP_NE, cons2);
tt_int_op(6, OP_EQ, smartlist_len(cons2));
tt_str_op("M", OP_EQ, smartlist_get(cons2, 0));
tt_str_op("A", OP_EQ, smartlist_get(cons2, 1));
tt_str_op("C", OP_EQ, smartlist_get(cons2, 2));
tt_str_op("T", OP_EQ, smartlist_get(cons2, 3));
tt_str_op("X", OP_EQ, smartlist_get(cons2, 4));
tt_str_op("E", OP_EQ, smartlist_get(cons2, 5));
done:
if (cons1) SMARTLIST_FOREACH(cons1, char*, line, tor_free(line));
if (cons2) SMARTLIST_FOREACH(cons2, char*, line, tor_free(line));
smartlist_free(cons1);
smartlist_free(cons2);
if (diff) SMARTLIST_FOREACH(diff, char*, line, tor_free(line));
smartlist_free(diff);
}
static void
test_consdiff_gen_diff(void *arg)
{
char *cons1_str=NULL, *cons2_str=NULL;
smartlist_t *cons1=NULL, *cons2=NULL, *diff=NULL;
common_digests_t digests1, digests2;
(void)arg;
cons1 = smartlist_new();
cons2 = smartlist_new();
/* Identity hashes are not sorted properly, return NULL.
* Already tested in gen_ed_diff, but see that a NULL ed diff also makes
* gen_diff return NULL. */
cons1_str = tor_strdup(
"header\nnetwork-status-version foo\n"
"r name bbbbbbbbbbbbbbbbb etc\nfoo\n"
"r name aaaaaaaaaaaaaaaaa etc\nbar\n"
"directory-signature foo bar\nbar\n"
);
cons2_str = tor_strdup(
"header\nnetwork-status-version foo\n"
"r name aaaaaaaaaaaaaaaaa etc\nfoo\n"
"r name ccccccccccccccccc etc\nbar\n"
"directory-signature foo bar\nbar\n"
);
tt_int_op(0, OP_EQ,
router_get_networkstatus_v3_hashes(cons1_str, &digests1));
tt_int_op(0, OP_EQ,
router_get_networkstatus_v3_hashes(cons2_str, &digests2));
tor_split_lines(cons1, cons1_str, (int)strlen(cons1_str));
tor_split_lines(cons2, cons2_str, (int)strlen(cons2_str));
diff = consdiff_gen_diff(cons1, cons2, &digests1, &digests2);
tt_ptr_op(NULL, OP_EQ, diff);
/* Check that the headers are done properly. */
tor_free(cons1_str);
cons1_str = tor_strdup(
"header\nnetwork-status-version foo\n"
"r name ccccccccccccccccc etc\nfoo\n"
"r name eeeeeeeeeeeeeeeee etc\nbar\n"
"directory-signature foo bar\nbar\n"
);
tt_int_op(0, OP_EQ,
router_get_networkstatus_v3_hashes(cons1_str, &digests1));
smartlist_clear(cons1);
tor_split_lines(cons1, cons1_str, (int)strlen(cons1_str));
diff = consdiff_gen_diff(cons1, cons2, &digests1, &digests2);
tt_ptr_op(NULL, OP_NE, diff);
tt_int_op(7, OP_EQ, smartlist_len(diff));
tt_str_op("network-status-diff-version 1", OP_EQ, smartlist_get(diff, 0));
tt_str_op("hash "
"C2199B6827514F39ED9B3F2E2E73735C6C5468FD636240BB454C526220DE702A "
"B193E5FBFE5C009AEDE56F9218E6421A1AE5C19F43E091786A73F43F60409B60",
OP_EQ, smartlist_get(diff, 1));
tt_str_op("4,5d", OP_EQ, smartlist_get(diff, 2));
tt_str_op("2a", OP_EQ, smartlist_get(diff, 3));
tt_str_op("r name aaaaaaaaaaaaaaaaa etc", OP_EQ, smartlist_get(diff, 4));
tt_str_op("foo", OP_EQ, smartlist_get(diff, 5));
tt_str_op(".", OP_EQ, smartlist_get(diff, 6));
/* TODO: small real use-cases, i.e. consensuses. */
done:
tor_free(cons1_str);
tor_free(cons2_str);
smartlist_free(cons1);
smartlist_free(cons2);
if (diff) SMARTLIST_FOREACH(diff, char*, line, tor_free(line));
smartlist_free(diff);
}
static void
test_consdiff_apply_diff(void *arg)
{
smartlist_t *cons1=NULL, *diff=NULL;
char *cons1_str=NULL, *cons2 = NULL;
common_digests_t digests1;
(void)arg;
cons1 = smartlist_new();
diff = smartlist_new();
cons1_str = tor_strdup(
"header\nnetwork-status-version foo\n"
"r name ccccccccccccccccc etc\nfoo\n"
"r name eeeeeeeeeeeeeeeee etc\nbar\n"
"directory-signature foo bar\nbar\n"
);
tt_int_op(0, OP_EQ,
router_get_networkstatus_v3_hashes(cons1_str, &digests1));
tor_split_lines(cons1, cons1_str, (int)strlen(cons1_str));
/* diff doesn't have enough lines. */
cons2 = consdiff_apply_diff(cons1, diff, &digests1);
tt_ptr_op(NULL, OP_EQ, cons2);
/* first line doesn't match format-version string. */
smartlist_add(diff, (char*)"foo-bar");
smartlist_add(diff, (char*)"header-line");
cons2 = consdiff_apply_diff(cons1, diff, &digests1);
tt_ptr_op(NULL, OP_EQ, cons2);
/* The first word of the second header line is not "hash". */
smartlist_clear(diff);
smartlist_add(diff, (char*)"network-status-diff-version 1");
smartlist_add(diff, (char*)"word a b");
cons2 = consdiff_apply_diff(cons1, diff, &digests1);
tt_ptr_op(NULL, OP_EQ, cons2);
/* Wrong number of words after "hash". */
smartlist_clear(diff);
smartlist_add(diff, (char*)"network-status-diff-version 1");
smartlist_add(diff, (char*)"hash a b c");
cons2 = consdiff_apply_diff(cons1, diff, &digests1);
tt_ptr_op(NULL, OP_EQ, cons2);
/* base16 sha256 digests do not have the expected length. */
smartlist_clear(diff);
smartlist_add(diff, (char*)"network-status-diff-version 1");
smartlist_add(diff, (char*)"hash aaa bbb");
cons2 = consdiff_apply_diff(cons1, diff, &digests1);
tt_ptr_op(NULL, OP_EQ, cons2);
/* base16 sha256 digests contain non-base16 characters. */
smartlist_clear(diff);
smartlist_add(diff, (char*)"network-status-diff-version 1");
smartlist_add(diff, (char*)"hash"
" ????????????????????????????????????????????????????????????????"
" ----------------------------------------------------------------");
cons2 = consdiff_apply_diff(cons1, diff, &digests1);
tt_ptr_op(NULL, OP_EQ, cons2);
/* Invalid ed diff.
* As tested in apply_ed_diff, but check that apply_diff does return NULL if
* the ed diff can't be applied. */
smartlist_clear(diff);
smartlist_add(diff, (char*)"network-status-diff-version 1");
smartlist_add(diff, (char*)"hash"
/* sha256 of cons1. */
" C2199B6827514F39ED9B3F2E2E73735C6C5468FD636240BB454C526220DE702A"
/* sha256 of cons2. */
" 635D34593020C08E5ECD865F9986E29D50028EFA62843766A8197AD228A7F6AA");
smartlist_add(diff, (char*)"foobar");
cons2 = consdiff_apply_diff(cons1, diff, &digests1);
tt_ptr_op(NULL, OP_EQ, cons2);
/* Base consensus doesn't match its digest as found in the diff. */
smartlist_clear(diff);
smartlist_add(diff, (char*)"network-status-diff-version 1");
smartlist_add(diff, (char*)"hash"
/* bogus sha256. */
" 3333333333333333333333333333333333333333333333333333333333333333"
/* sha256 of cons2. */
" 635D34593020C08E5ECD865F9986E29D50028EFA62843766A8197AD228A7F6AA");
cons2 = consdiff_apply_diff(cons1, diff, &digests1);
tt_ptr_op(NULL, OP_EQ, cons2);
/* Resulting consensus doesn't match its digest as found in the diff. */
smartlist_clear(diff);
smartlist_add(diff, (char*)"network-status-diff-version 1");
smartlist_add(diff, (char*)"hash"
/* sha256 of cons1. */
" C2199B6827514F39ED9B3F2E2E73735C6C5468FD636240BB454C526220DE702A"
/* bogus sha256. */
" 3333333333333333333333333333333333333333333333333333333333333333");
cons2 = consdiff_apply_diff(cons1, diff, &digests1);
tt_ptr_op(NULL, OP_EQ, cons2);
/* Very simple test, only to see that nothing errors. */
smartlist_clear(diff);
smartlist_add(diff, (char*)"network-status-diff-version 1");
smartlist_add(diff, (char*)"hash"
/* sha256 of cons1. */
" C2199B6827514F39ED9B3F2E2E73735C6C5468FD636240BB454C526220DE702A"
/* sha256 of cons2. */
" 635D34593020C08E5ECD865F9986E29D50028EFA62843766A8197AD228A7F6AA");
smartlist_add(diff, (char*)"4c");
smartlist_add(diff, (char*)"sample");
smartlist_add(diff, (char*)".");
cons2 = consdiff_apply_diff(cons1, diff, &digests1);
tt_ptr_op(NULL, OP_NE, cons2);
tt_str_op(
"header\nnetwork-status-version foo\n"
"r name ccccccccccccccccc etc\nsample\n"
"r name eeeeeeeeeeeeeeeee etc\nbar\n"
"directory-signature foo bar\nbar\n", OP_EQ,
cons2);
tor_free(cons2);
/* Check that lowercase letters in base16-encoded digests work too. */
smartlist_clear(diff);
smartlist_add(diff, (char*)"network-status-diff-version 1");
smartlist_add(diff, (char*)"hash"
/* sha256 of cons1. */
" c2199b6827514f39ed9b3f2e2e73735c6c5468fd636240bb454c526220de702a"
/* sha256 of cons2. */
" 635d34593020c08e5ecd865f9986e29d50028efa62843766a8197ad228a7f6aa");
smartlist_add(diff, (char*)"4c");
smartlist_add(diff, (char*)"sample");
smartlist_add(diff, (char*)".");
cons2 = consdiff_apply_diff(cons1, diff, &digests1);
tt_ptr_op(NULL, OP_NE, cons2);
tt_str_op(
"header\nnetwork-status-version foo\n"
"r name ccccccccccccccccc etc\nsample\n"
"r name eeeeeeeeeeeeeeeee etc\nbar\n"
"directory-signature foo bar\nbar\n", OP_EQ,
cons2);
tor_free(cons2);
smartlist_clear(diff);
done:
tor_free(cons1_str);
smartlist_free(cons1);
smartlist_free(diff);
}
#define CONSDIFF_LEGACY(name) \
{ #name, test_consdiff_ ## name , 0, NULL, NULL }
struct testcase_t consdiff_tests[] = {
CONSDIFF_LEGACY(smartlist_slice),
CONSDIFF_LEGACY(smartlist_slice_string_pos),
CONSDIFF_LEGACY(lcs_lengths),
CONSDIFF_LEGACY(trim_slices),
CONSDIFF_LEGACY(set_changed),
CONSDIFF_LEGACY(calc_changes),
CONSDIFF_LEGACY(get_id_hash),
CONSDIFF_LEGACY(is_valid_router_entry),
CONSDIFF_LEGACY(next_router),
CONSDIFF_LEGACY(base64cmp),
CONSDIFF_LEGACY(gen_ed_diff),
CONSDIFF_LEGACY(apply_ed_diff),
CONSDIFF_LEGACY(gen_diff),
CONSDIFF_LEGACY(apply_diff),
END_OF_TESTCASES
};
|