-
Notifications
You must be signed in to change notification settings - Fork 14
/
check.sh
executable file
·1161 lines (1002 loc) · 38.9 KB
/
check.sh
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
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/bin/sh
# Test suite for OneTime.
#
# How to write a new test:
#
# 1) start_new_test "some descriptive test string"
#
# 2) Do stuff. Your cwd is a temporary directory, tests/test-tmp/,
# and it already has its own tmp config directories in place, e.g.,
# "tests/test-tmp/dot-onetime" and others (see reset_config for
# the full list). So a typical invocation will look like this:
# "../../onetime --config=dot-onetime -e -p ../test-pad-1 etc etc"
#
# (All the permanent test data lives in tests/, so you'll use
# arguments like "../test-plaintext-b" and ../test-pad-1" a lot.)
#
# 3) If a tested condition fails, echo "ERROR: describe how & why",
# then set PASSED="no".
#
# 4) At the end of the test, call check_result.
#
# Note on why tests should *always* pass a config dir explicitly:
#
# Even when passing -n (--no-trace), you should always specify one of
# the test config directories explicitly (use "blank-dot-onetime" for
# a no-op config dir). Otherwise, if you happen to have some of the
# test pads recorded in your own ~/.onetime/pad-records (as can
# accidentally happen if you've been doing OneTime development and
# failed to pass '--config' every time you manually tested), you could
# write a test here that either inadvertently depends on something in
# your ~/.onetime/pad-records or is inadvertently sensitive to the
# ~/.onetime/pad-records of other people who have played around with
# the test pads and accidentally affected their ~/.onetime/pad-records.
# Either way, your test would not be portable. Even when an invocation
# couldn't possibly depend on nor affect any values in pad-records
# (e.g., it just runs --show-id or something), you should still pass
# "--config=blank-dot-onetime", because otherwise someone running the
# test who doesn't currently have a ~/.onetime directory will suddenly
# have one afterwards, which would be bad behavior for a test suite.
TEST_PAD_1_ID="6d0031fd04e927feb893aad9478b9e7e213b56e7fc766fdb57f12d3a55fa36e4"
TEST_PAD_1_V1_ID="6af6d0ac17081705cec30833da3cd436a400c429"
TEST_PAD_2_ID="7613667562635a22e62c55aabbb22d7a39bc368a8d2263e611db5caa215cc4cf"
TEST_PAD_2_V1_ID="de61f169bce003a1189b3e6ebb8ddfc0ef007ac2"
# See start_new_test() and check_result() for what these do.
THIS_TEST="(no test name initialized yet)"
PASSED="(uninitialized)"
cd tests
rm -rf test-tmp
mkdir test-tmp
cd test-tmp
start_new_test()
{
THIS_TEST="${1}"
PASSED="yes" # see check_result()
reset_config
}
check_result()
{
if [ "${1}x" = "XFAILx" ]; then
THIS_XFAIL="yes"
elif [ "${1}x" != "x" ]; then
echo "ERROR: unknown argument '${1}' to check_result()"
fi
if [ ${PASSED} = "yes" ]; then
if [ "${THIS_XFAIL}x" = "yesx" ]; then
echo "(XPASS): ${THIS_TEST}"
else
echo "PASS: ${THIS_TEST}"
fi
else
if [ "${THIS_XFAIL}x" = "yesx" ]; then
echo "(XFAIL): ${THIS_TEST}"
else
echo "FAIL: ${THIS_TEST}"
fi
# Print an extra blank line separating this "FAIL" line from the
# tests that come after it, so any already-printed errors related
# to this failure are visually grouped together with it.
echo ""
fi
unset THIS_XFAIL # Is this how to do local scope portably in shell?
}
# TODO: You'd think this function would be named 'reset_test_area' and
# would just do something like this:
#
# cd ..
# rm -rf test-tmp
# mkdir test-tmp
# cd test-tmp
#
# But apparently we've got some inter-test dependencies, because if
# you try that, lots of tests fail. None of this means the test suite
# is invalid, of course: it's testing what we think it's testing, it's
# just not designed as well as it could be, and that should be fixed.
reset_config()
{
for name in dot-onetime v1-dot-onetime blank-dot-onetime; do
rm -rf ${name}
cp -a ../${name} ./${name}
rm -rf ${name}/.svn
done
}
########################################################################
### Explain the test suite's output. ###
########################################################################
echo ''
echo ' Each passing test is indicated by exactly one "PASS" line'
echo ' that includes a brief description of the test.'
echo ''
echo ' A failing test will show "ERROR" details first -- which may span'
echo ' multiple lines and be very noisy -- followed by a "FAIL" line'
echo ' giving a brief description of that test. If a test is currently'
echo ' expected to fail, for example due to a known bug not yet fixed, '
echo ' then its description starts with "(XFAIL)".'
echo ''
echo " Note that some tests may take a while; don't be alarmed"
echo " if a minute or two goes by without output."
echo ''
########################################################################
start_new_test "basic encryption, decryption"
../../onetime --config=blank-dot-onetime -e -p ../test-pad-1 \
-o tmp-ciphertext-b-1 ../test-plaintext-b
../../onetime --config=blank-dot-onetime -d -p ../test-pad-1 \
-o tmp-plaintext-b tmp-ciphertext-b-1
if ! cmp tmp-plaintext-b ../test-plaintext-b
then
echo ""
echo "ERROR: decrypted plaintext does not match original plaintext"
PASSED="no"
fi
check_result
########################################################################
start_new_test "encryption, decryption of large plaintext"
# Assemble a pad and plaintext so large that not only are they many
# times larger than our 8k chunk size, but they're larger than the
# Python bzip2 compressor's maximum possible buffer. Experiments
# indicate that range(0, 2) would be enough here, but let's use 5.
for ignored in `python -c "for i in range(0, 5): print i"`; do
cat ../test-pad-1 >> large-pad
cat ../test-pad-2 >> large-plaintext
done
../../onetime --config=blank-dot-onetime -e -p large-pad \
-o tmp-large-ciphertext large-plaintext
../../onetime --config=blank-dot-onetime -d -p large-pad \
-o tmp-large-plaintext tmp-large-ciphertext
if ! cmp tmp-large-plaintext large-plaintext
then
echo ""
echo "ERROR: decrypted large plaintext does not match original plaintext"
PASSED="no"
fi
check_result
########################################################################
start_new_test "option parsing"
###
# In the tests of the various option parsing methods, "e.N" is
# encrypted text and "d.N" is decrypted text.
###
# mode 1
../../onetime -C dot-onetime -e -p ../test-pad-2 -o e.1 ../test-plaintext-a
../../onetime -C dot-onetime -d -p ../test-pad-2 -o d.1 e.1
# mode 2
../../onetime -C dot-onetime -e -p ../test-pad-2 ../test-plaintext-a
mv ../test-plaintext-a.onetime e.2.onetime
../../onetime -C dot-onetime -d -p ../test-pad-2 e.2.onetime
mv e.2 d.2
# mode 3
../../onetime -C dot-onetime -e -p ../test-pad-2 -o - ../test-plaintext-a > e.3
../../onetime -C dot-onetime -d -p ../test-pad-2 -o - e.3 > d.3
# mode 4
../../onetime -C dot-onetime -e -p ../test-pad-2 < ../test-plaintext-a > e.4
../../onetime -C dot-onetime -d -p ../test-pad-2 < e.4 > d.4
# mode 5
../../onetime -C dot-onetime -e -p ../test-pad-2 -o e.5 < ../test-plaintext-a
../../onetime -C dot-onetime -d -p ../test-pad-2 -o d.5 < e.5
for n in 1 2 3 4 5; do
if cmp ../test-plaintext-a d.${n}; then
true
else
echo ""
echo "ERROR: one or more of the usage modes failed"
PASSED="no"
fi
done
check_result
########################################################################
start_new_test "failed decryption should give an error and create no output"
../../onetime --config=blank-dot-onetime -e -p ../test-pad-1 \
-o tmp-ciphertext-b-1 < ../test-plaintext-b
../../onetime --config=blank-dot-onetime -d -p ../test-pad-2 \
-o tmp-plaintext-b-1 tmp-ciphertext-b-1 2>err.out
if ! grep -q "InnerFormat: unknown inner format version" err.out
then
echo ""
echo "ERROR: did not see expected error on failed decryption"
cat err.out
PASSED="no"
fi
if [ -f tmp-plaintext-b-1 ]
then
echo ""
echo "ERROR: output file left still created on failed decryption"
PASSED="no"
fi
check_result
########################################################################
start_new_test "decryption should not shrink pad usage"
# User sent in a report:
#
# $ onetime.py -e -p onetimepad.dat test1.txt
# ==> pad-records says used length now 27340
# $ onetime.py -e -p onetimepad.dat test2.txt
# ==> pad-records says used length now 54680
# $ onetime.py -e -p onetimepad.dat test3.txt
# ==> pad-records says used length now 82020
#
# (Now watch what happens on decryption...)
#
# $ onetime.py -d -p onetimepad.dat test1.txt.onetime
# ==> pad-records says length reverted to 27340!
../../onetime -C dot-onetime -e -p ../test-pad-1 \
-o tmp-ciphertext-b-1.onetime ../test-plaintext-b
if ! grep -q "<length>12555</length>" dot-onetime/pad-records; then
echo ""
echo "ERROR: Pad usage length is not 12555 after encryption iteration 1."
cat dot-onetime/pad-records
PASSED="no"
fi
../../onetime -C dot-onetime -e -p ../test-pad-1 \
-o tmp-ciphertext-b-2.onetime ../test-plaintext-b
if ! grep -q "<length>25250</length>" dot-onetime/pad-records; then
echo ""
echo "ERROR: Pad usage length is not 25250 after encryption iteration 2."
cat dot-onetime/pad-records
PASSED="no"
fi
../../onetime -C dot-onetime -e -p ../test-pad-1 \
-o tmp-ciphertext-b-3.onetime ../test-plaintext-b
if ! grep -q "<length>37873</length>" dot-onetime/pad-records; then
echo ""
echo "ERROR: Pad usage length is not 37873 after encryption iteration 3."
cat dot-onetime/pad-records
PASSED="no"
fi
../../onetime -C dot-onetime -d -p ../test-pad-1 \
-o tmp-plaintext-b-1 tmp-ciphertext-b-1.onetime
if ! grep -q "<length>37873</length>" dot-onetime/pad-records; then
cat dot-onetime/pad-records
if grep -q "<length>12555</length>" dot-onetime/pad-records; then
# Note that as long as everything is working, this case will not
# be triggered in normal test suite runs even if the length that
# *would* indicate the return of this bug has changed from 12523
# to something else due to normal development progress. So if
# we're inside the larger failure already, check carefully to see
# whether we should actually be in this case and were just
# expecting the wrong number.
echo ""
echo "ERROR: 'Decryption wrongly shrinks pad usage' bug is back."
else
echo ""
echo "ERROR: Pad usage length is not 37873 after decryption 1, but don't know why."
fi
PASSED="no"
fi
if ! cmp tmp-plaintext-b-1 ../test-plaintext-b; then
echo ""
echo "ERROR: Decryption failed to produce correct plaintext."
PASSED="no"
fi
check_result
########################################################################
start_new_test "decryption should record same pad usage as encryption"
# If Alice encrypts, resulting in pad usage range R for that pad, then
# if Bob decrypts starting from the same pad record state that Alice
# had started with, the Bob's post-decryption pad record state should
# be exactly the same as Alice's post-encryption pad record state.
cp -a dot-onetime e-dot-onetime # separate encryption copy
cp -a dot-onetime d-dot-onetime # separate decryption copy
../../onetime -C e-dot-onetime -e -p ../test-pad-1 \
-o tmp-ciphertext-b-1 ../test-plaintext-b
../../onetime -C d-dot-onetime -d -p ../test-pad-1 \
-o tmp-plaintext-b-1 tmp-ciphertext-b-1
if ! grep -q "<length>12555</length>" e-dot-onetime/pad-records; then
grep "<length>" e-dot-onetime/pad-records
echo ""
echo "ERROR: expected pad usage length of 12555 for encryption"
PASSED="no"
fi
if ! grep -q "<length>12555</length>" d-dot-onetime/pad-records; then
if grep -q "<length>12265</length>" d-dot-onetime/pad-records; then
# Note that as long as everything is working, this case will not
# be triggered in normal test suite runs even if the length that
# *would* indicate the return of this bug has changed from 12265
# to something else due to normal development progress. So if
# we're inside the larger failure already, check carefully to see
# whether we should actually be in this case and were just
# expecting the wrong number.
echo ""
echo "ERROR: tail fuzz authn isn't being counted in pad usage record"
else
grep "<length>" d-dot-onetime/pad-records
echo ""
echo "ERROR: pad usage length is not 12555 after decryption, for some new reason"
fi
PASSED="no"
fi
check_result
########################################################################
start_new_test "test reconsumption via repeated encoding and decoding"
# Debugging helper function. Deactivated by default -- change 'false'
# to 'true' to turn this on.
# Print the (string) first argument, then display all pad lengths.
maybe_show_lengths()
{
if false; then
echo ${1}
grep "/length" dot-onetime/pad-records
echo ""
fi
}
# Encode
../../onetime --config=dot-onetime -e -p ../test-pad-1 \
< ../test-plaintext-a > tmp-ciphertext-a.onetime
maybe_show_lengths "After encoding:"
# Decode twice, to make sure the pad can reconsume safely.
../../onetime --config=dot-onetime -d -p ../test-pad-1 \
< tmp-ciphertext-a.onetime > tmp-plaintext-a.decoded-1
maybe_show_lengths "After decoding once:"
if ! cmp ../test-plaintext-a tmp-plaintext-a.decoded-1; then
echo ""
echo "ERROR: tmp-plaintext-a.decoded-1 does not match test-plaintext-a input."
PASSED="no"
fi
../../onetime --config=dot-onetime -d -p ../test-pad-1 \
< tmp-ciphertext-a.onetime > tmp-plaintext-a.decoded-2
maybe_show_lengths "After decoding again:"
if ! cmp ../test-plaintext-a tmp-plaintext-a.decoded-2; then
echo ""
echo "ERROR: tmp-plaintext-a.decoded-2 does not match test-plaintext-a input."
PASSED="no"
fi
# Encode again with the same pad
../../onetime --config=dot-onetime -e -p ../test-pad-1 \
< ../test-plaintext-a > tmp-ciphertext-a.onetime
maybe_show_lengths "After encoding again:"
# Decode only once this time.
../../onetime --config=dot-onetime -d -p ../test-pad-1 \
< tmp-ciphertext-a.onetime > tmp-plaintext-a.decoded-3
maybe_show_lengths "After decoding:"
if ! cmp ../test-plaintext-a tmp-plaintext-a.decoded-3; then
echo ""
echo "ERROR: tmp-plaintext-a.decoded-3 does not match test-plaintext-a input."
PASSED="no"
fi
# Now do the entire thing again with the other pad.
# Encode
../../onetime --config=dot-onetime -e -p ../test-pad-2 \
< ../test-plaintext-a > tmp-ciphertext-a.onetime
maybe_show_lengths "After encoding:"
# Decode twice, to make sure the pad can reconsume safely.
../../onetime --config=dot-onetime -d -p ../test-pad-2 \
< tmp-ciphertext-a.onetime > tmp-plaintext-a.decoded-1
maybe_show_lengths "After decoding once:"
if ! cmp ../test-plaintext-a tmp-plaintext-a.decoded-1; then
echo ""
echo "ERROR: tmp-plaintext-a.decoded-1 (pad test-pad-2) does not match test-plaintext-a input."
PASSED="no"
fi
../../onetime --config=dot-onetime -d -p ../test-pad-2 \
< tmp-ciphertext-a.onetime > tmp-plaintext-a.decoded-2
maybe_show_lengths "After decoding again:"
if ! cmp ../test-plaintext-a tmp-plaintext-a.decoded-2; then
echo ""
echo "ERROR: tmp-plaintext-a.decoded-2 (pad test-pad-2) does not match test-plaintext-a input."
PASSED="no"
fi
# Encode again with the same pad
../../onetime --config=dot-onetime -e -p ../test-pad-2 \
< ../test-plaintext-a > tmp-ciphertext-a.onetime
maybe_show_lengths "After encoding again:"
# Decode only once this time.
../../onetime --config=dot-onetime -d -p ../test-pad-2 \
< tmp-ciphertext-a.onetime > tmp-plaintext-a.decoded-3
maybe_show_lengths "After decoding:"
if ! cmp ../test-plaintext-a tmp-plaintext-a.decoded-3; then
echo ""
echo "ERROR: tmp-plaintext-a.decoded-3 (pad test-pad-2) does not match test-plaintext-a input."
PASSED="no"
fi
check_result
########################################################################
start_new_test "make sure '--show-id' shows everything it should"
#####
## Check that both v2 and v1 pad IDs are displayed with --show-id.
if ! ../../onetime --config=blank-dot-onetime --show-id -p ../test-pad-1 \
| grep -q ${TEST_PAD_1_ID}
then
echo ""
echo "ERROR: --show-id -p test-pad-1 failed to display ID"
PASSED="no"
fi
if ! ../../onetime --config=blank-dot-onetime --show-id -p ../test-pad-1 \
| grep -q " ${TEST_PAD_1_V1_ID}"
then
echo ""
echo "ERROR: --show-id -p test-pad-1 failed to display v2 ID"
PASSED="no"
fi
check_result
########################################################################
start_new_test "same plaintext should encrypt smaller with v2+ than with v1"
## Encrypt message, compare against same message encrypted with v1.
## Result: the more recent ciphertext should be noticeably smaller.
../../onetime -n --config=blank-dot-onetime -e -p ../test-pad-1 \
-o test-ciphertext-b-1 ../test-plaintext-b
BYTES_NOW=`wc -c test-ciphertext-b-1 | cut -d " " -f1`
BYTES_V1=`wc -c ../test-v1-ciphertext-b-1 | cut -d " " -f1`
if [ ${BYTES_NOW} -ge ${BYTES_V1} ]
then
echo ""
echo "ERROR: new ciphertext is bigger than v1 encryption of same plaintext"
PASSED="no"
fi
check_result
########################################################################
start_new_test "decode v1 msg, where v1 entry has range already used"
## Receive v1 msg M, have v1 pad-records file with pad entry for M's
## pad, and that stretch of pad already marked as used. Decode msg.
## Result: upgraded pad ID, everything else stays same.
../../onetime --config=v1-dot-onetime -d -p ../test-pad-1 \
< ../test-v1-ciphertext-offset-0-a-1 > tmp-plaintext-a
if ! cmp ../test-plaintext-a tmp-plaintext-a; then
echo ""
echo "ERROR: tmp-plaintext-a does not match original plaintext."
PASSED="no"
fi
rm tmp-plaintext-a
if ! grep -q "<id>${TEST_PAD_1_ID}</id>" v1-dot-onetime/pad-records
then
echo ""
echo "ERROR: decoding v1 input failed to upgrade pad ID in pad-records"
PASSED="no"
fi
if grep -q "<id>${TEST_PAD_1_V1_ID}</id>" v1-dot-onetime/pad-records
then
echo ""
echo "ERROR: decoding v1 input failed to remove v1 pad ID from pad-records"
PASSED="no"
fi
if ! grep -q "<used><offset>0</offset>" v1-dot-onetime/pad-records
then
echo ""
echo "ERROR: decoding v1 input removed 0 offset from pad-records"
PASSED="no"
fi
if ! grep -q "<length>15</length></used>" v1-dot-onetime/pad-records
then
echo ""
echo "ERROR: decoding v1 input affected length 15 in pad-records"
PASSED="no"
fi
if [ `grep -c "</length></used>" v1-dot-onetime/pad-records` -gt 1 ]
then
echo ""
echo "ERROR: decoding v1 input inserted spurious length into pad-records"
PASSED="no"
fi
check_result
########################################################################
start_new_test "decode v1 msg, where v1 entry has range not already used"
## Receive v1 msg M, have v1 pad-records file with pad entry for M's
## pad, but this stretch of pad not marked as used.
## Result: upgraded pad ID, stretch marked as used.
../../onetime --config=v1-dot-onetime -d -p ../test-pad-1 \
< ../test-v1-ciphertext-offset-15-a-1 > tmp-plaintext-a
if ! cmp ../test-plaintext-a tmp-plaintext-a; then
echo ""
echo "ERROR: tmp-plaintext-a does not match original plaintext."
PASSED="no"
fi
rm tmp-plaintext-a
if ! grep -q "<id>${TEST_PAD_1_ID}</id>" v1-dot-onetime/pad-records
then
echo ""
echo "ERROR: decoding v1 input failed to upgrade pad ID in pad-records"
PASSED="no"
fi
if grep -q "<id>${TEST_PAD_1_V1_ID}</id>" v1-dot-onetime/pad-records
then
echo ""
echo "ERROR: decoding v1 input failed to remove v1 pad ID from pad-records"
PASSED="no"
fi
if ! grep -q "<used><offset>0</offset>" v1-dot-onetime/pad-records
then
echo ""
echo "ERROR: decoding v1 input removed 0 offset from pad-records"
PASSED="no"
fi
if grep -q "<length>15</length></used>" v1-dot-onetime/pad-records
then
echo ""
echo "ERROR: decoding v1 input left length 15 still in pad-records"
PASSED="no"
fi
if ! grep -q "<length>30</length></used>" v1-dot-onetime/pad-records
then
echo ""
echo "ERROR: decoding v1 input failed to put length 30 in pad-records"
PASSED="no"
fi
if [ `grep -c "</length></used>" v1-dot-onetime/pad-records` -gt 1 ]
then
echo ""
echo "ERROR: decoding v1 input inserted spurious length into pad-records"
PASSED="no"
fi
check_result
########################################################################
start_new_test "decode v2 msg, where v1 entry has range already used"
## Receive v2 msg M, have v1 pad-records file with pad entry for M's
## pad and that stretch of pad already marked as used.
## Result: upgraded pad ID, everything else stays same.
# Create the ciphertext, leaving no trace (this is test prep only).
../../onetime -n --config=blank-dot-onetime -e -p ../test-pad-1 \
< ../test-plaintext-a > tmp-ciphertext-a-1
# Manually tweak the v1 pad-records file to claim that range is
# already used, since otherwise it's a bit tough (using v2 onetime) to
# get that result, given that v2 would automatically upgrade the
# record's ID along with the range, and the whole point here is that
# we want to test that the ID can get upgraded without the range being
# affected.
sed -e 's|<length>15</length></used>|<length>512</length></used>|' \
< v1-dot-onetime/pad-records > v1-dot-onetime/TMP-pad-records
mv v1-dot-onetime/TMP-pad-records v1-dot-onetime/pad-records
# Decrypt the v2 file, updating the newly range-expanded v1 pad-records.
../../onetime --config=v1-dot-onetime -d -p ../test-pad-1 \
< tmp-ciphertext-a-1 > tmp-plaintext-a
if ! cmp ../test-plaintext-a tmp-plaintext-a; then
echo ""
echo "ERROR: tmp-plaintext-a does not match original plaintext."
PASSED="no"
fi
rm tmp-plaintext-a
rm tmp-ciphertext-a-1
if ! grep -q "<id>${TEST_PAD_1_ID}</id>" v1-dot-onetime/pad-records
then
echo ""
echo "ERROR: decoding v2 input failed to upgrade pad ID in pad-records"
PASSED="no"
fi
if grep -q "<id>${TEST_PAD_1_V1_ID}</id>" v1-dot-onetime/pad-records
then
echo ""
echo "ERROR: decoding v2 input failed to remove v1 pad ID from pad-records"
PASSED="no"
fi
if ! grep -q "<used><offset>0</offset>" v1-dot-onetime/pad-records
then
echo ""
echo "ERROR: decoding v2 input removed 0 offset from pad-records"
PASSED="no"
fi
if ! grep -q "<length>695</length></used>" v1-dot-onetime/pad-records
then
echo ""
cat v1-dot-onetime/pad-records
echo "ERROR: decoding v2 input affected length 695 in pad-records"
PASSED="no"
fi
if [ `grep -c "</length></used>" v1-dot-onetime/pad-records` -gt 1 ]
then
echo ""
echo "ERROR: decoding v2 input inserted spurious length into pad-records"
PASSED="no"
fi
check_result
########################################################################
start_new_test "decode v2 msg, where v1 entry range needs stretching"
## Receive v2 msg M, have v1 pad-records file with pad entry for M's
## pad, but this stretch of pad not marked as used and starting within
## the highest current used range.
## Result: upgraded pad ID, stretch marked as used.
# Create the ciphertext, leaving no trace (this is test prep only).
../../onetime -n --config=blank-dot-onetime -e -p ../test-pad-1 \
< ../test-plaintext-a > tmp-ciphertext-a-1
# Manually tweak the v1 pad-records file to put the already-used range
# just above 32, which is the offset where our v2 ciphertext starts
# using the pad. This gives us a chance to see if the range gets
# stretched and replaced when we decrypt.
sed -e 's|<length>15</length></used>|<length>33</length></used>|' \
< v1-dot-onetime/pad-records > v1-dot-onetime/TMP-pad-records
mv v1-dot-onetime/TMP-pad-records v1-dot-onetime/pad-records
# Decrypt the v2 file, updating the newly range-expanded v1 pad-records.
../../onetime --config=v1-dot-onetime -d -p ../test-pad-1 \
< tmp-ciphertext-a-1 > tmp-plaintext-a
if ! cmp ../test-plaintext-a tmp-plaintext-a; then
echo ""
echo "ERROR: tmp-plaintext-a does not match original plaintext."
PASSED="no"
fi
rm tmp-plaintext-a
rm tmp-ciphertext-a-1
if ! grep -q "<id>${TEST_PAD_1_ID}</id>" v1-dot-onetime/pad-records
then
echo ""
echo "ERROR: decoding v2 input failed to upgrade pad ID in pad-records"
PASSED="no"
fi
if grep -q "<id>${TEST_PAD_1_V1_ID}</id>" v1-dot-onetime/pad-records
then
echo ""
echo "ERROR: decoding v2 input failed to remove v1 pad ID from pad-records"
PASSED="no"
fi
if ! grep -q "<used><offset>0</offset>" v1-dot-onetime/pad-records
then
echo ""
echo "ERROR: decoding v2 input removed 0 offset from pad-records"
PASSED="no"
fi
if ! grep -q "<length>695</length></used>" v1-dot-onetime/pad-records
then
echo ""
echo "ERROR: decoding v2 input failed to use length 695 in pad-records"
cat v1-dot-onetime/pad-records
PASSED="no"
fi
if [ `grep -c "</length></used>" v1-dot-onetime/pad-records` -gt 1 ]
then
echo ""
echo "ERROR: decoding v2 input inserted spurious length into pad-records"
PASSED="no"
fi
check_result
########################################################################
start_new_test "decode v2 msg, where v1 entry needs new range"
## Receive v2 msg M, have v1 pad-records file with pad entry for M's
## pad, but this stretch of pad not marked as used and starting
## after the end of the highest current used range.
## Result: upgraded pad ID and new range added.
# Create the ciphertext, leaving no trace (this is test prep only).
../../onetime -n --config=blank-dot-onetime -e -p ../test-pad-1 \
< ../test-plaintext-a > tmp-ciphertext-a-1
# Decrypt the v2 file, updating the newly range-expanded v1 pad-records.
../../onetime --config=v1-dot-onetime -d -p ../test-pad-1 \
< tmp-ciphertext-a-1 > tmp-plaintext-a
if ! cmp ../test-plaintext-a tmp-plaintext-a; then
echo ""
echo "ERROR: tmp-plaintext-a does not match original plaintext."
PASSED="no"
fi
rm tmp-plaintext-a
rm tmp-ciphertext-a-1
if ! grep -q "<id>${TEST_PAD_1_ID}</id>" v1-dot-onetime/pad-records
then
echo ""
echo "ERROR: decoding v2 input failed to upgrade pad ID in pad-records"
PASSED="no"
fi
if grep -q "<id>${TEST_PAD_1_V1_ID}</id>" v1-dot-onetime/pad-records
then
echo ""
echo "ERROR: decoding v2 input failed to remove v1 pad ID from pad-records"
PASSED="no"
fi
if ! grep -q "<used><offset>0</offset>" v1-dot-onetime/pad-records
then
echo ""
echo "ERROR: decoding v2 input removed 0 offset from pad-records"
PASSED="no"
fi
if ! grep -q "<length>15</length></used>" v1-dot-onetime/pad-records
then
echo ""
echo "ERROR: decoding v2 input removed length 15 from pad-records"
PASSED="no"
fi
if ! grep -q "<used><offset>32</offset>" v1-dot-onetime/pad-records
then
echo ""
echo "ERROR: decoding v2 input failed to add offset 32 to pad-records"
PASSED="no"
fi
if ! grep -q "<length>663</length></used>" v1-dot-onetime/pad-records
then
echo ""
echo "ERROR: decoding v2 input failed to add length 663 to pad-records"
cat v1-dot-onetime/pad-records
PASSED="no"
fi
if [ `grep -c "</length></used>" v1-dot-onetime/pad-records` -gt 2 ]
then
echo ""
echo "ERROR: decoding v2 input inserted spurious length into pad-records"
PASSED="no"
fi
check_result
########################################################################
start_new_test "decode v1 msg, where no entry in pad-records at all"
## Receive v1 msg M, have no entry in pad-records file for M's pad.
## Result: new v2 entry
../../onetime --config=v1-dot-onetime -d -p ../test-pad-2 \
< ../test-v1-ciphertext-b-2 > tmp-plaintext-b
if ! cmp ../test-plaintext-b tmp-plaintext-b; then
echo ""
echo "ERROR: tmp-plaintext-b does not match original plaintext."
PASSED="no"
fi
rm tmp-plaintext-b
if ! grep -q "<id>${TEST_PAD_2_ID}</id>" v1-dot-onetime/pad-records
then
echo ""
echo "ERROR: decoding v1 input failed to insert pad ID into pad-records"
PASSED="no"
fi
if grep -q "<id>${TEST_PAD_2_V1_ID}</id>" v1-dot-onetime/pad-records
then
echo ""
echo "ERROR: decoding v1 input somehow inserted v1 pad ID into pad-records"
PASSED="no"
fi
if ! grep -q "<used><offset>0</offset>" v1-dot-onetime/pad-records
then
echo ""
echo "ERROR: decoding v1 input removed 0 offset from pad-records"
PASSED="no"
fi
# Expect the new length on the 10th line, which is in the second pad entry.
# But first test to make sure it's there at all; otherwise grep -n outputs
# nothing and the conditional gets harder to write.
if ! grep -q "<length>45541</length></used>" v1-dot-onetime/pad-records
then
echo ""
echo "ERROR: decoding v1 input failed to insert new 45541 record"
PASSED="no"
elif grep -q "<length>45541</length></used>" v1-dot-onetime/pad-records && \
[ `grep -n "<length>45541</length></used>" v1-dot-onetime/pad-records \
| cut -d ":" -f 1` -ne 10 ]
then
echo ""
echo "ERROR: decoding v1 input mis-inserted new 45541 record into pad-records"
PASSED="no"
fi
check_result
########################################################################
start_new_test "encode msg, where v1 pad entry has some range already used"
## Encrypt message, have v1 pad-records file with entry for pad used.
## Result: pad entry should be upgraded, with stretch now marked used.
../../onetime --config=v1-dot-onetime -e -p ../test-pad-1 \
-o tmp-ciphertext-b-1 < ../test-plaintext-b
# Toss the encryption, as it's not what we're testing here.
rm tmp-ciphertext-b-1
if ! grep -q "<id>${TEST_PAD_1_ID}</id>" v1-dot-onetime/pad-records
then
echo ""
echo "ERROR: encoding failed to upgrade v1 pad ID in pad-records"
PASSED="no"
fi
if grep -q "<id>${TEST_PAD_1_V1_ID}</id>" v1-dot-onetime/pad-records
then
echo ""
echo "ERROR: encoding failed to remove v1 pad ID from pad-records"
PASSED="no"
fi
if ! grep -q "<used><offset>0</offset>" v1-dot-onetime/pad-records
then
echo ""
echo "ERROR: encoding removed 0 offset from v1 entry in pad-records"
PASSED="no"
fi
# Expect the new offset on the 6th line, in second range of first entry.
# But first test to make sure it's there at all; otherwise grep -n outputs
# nothing and the conditional gets harder to write.
if ! grep -q "<used><offset>32</offset>" v1-dot-onetime/pad-records
then
echo ""
echo "ERROR: encoding failed to insert new offset 32 into pad-records"
PASSED="no"
elif grep -q "<used><offset>32</offset>" v1-dot-onetime/pad-records && \
[ `grep -n "<used><offset>32</offset>" v1-dot-onetime/pad-records \
| cut -d ":" -f 1` -ne 6 ]
then
echo ""
echo "ERROR: encoding mis-inserted new offset into pad-records"
PASSED="no"
fi
# Expect the new length on the 7th line, in second range of first entry.
if ! grep -q "<length>12659</length></used>" v1-dot-onetime/pad-records
then
echo ""
echo "ERROR: failed to insert new length 12659 into pad-records"
cat v1-dot-onetime/pad-records
PASSED="no"
elif grep -q "<length>12659</length></used>" v1-dot-onetime/pad-records && \
[ `grep -n "<length>12659</length></used>" v1-dot-onetime/pad-records \
| cut -d ":" -f 1` -ne 7 ]
then
echo ""
echo "ERROR: encoding mis-inserted new length 12659 into pad-records"
PASSED="no"
fi
check_result
########################################################################
start_new_test "decode msg, erroring because garbage after base64 data"
## Encrypt message
../../onetime --config=blank-dot-onetime -e -p ../test-pad-1 \
-o tmp-ciphertext-b-1 < ../test-plaintext-b
sed -e 's/-----END OneTime MESSAGE-----/ \n-----END OneTime MESSAGE-----/' \
< tmp-ciphertext-b-1 > tmp-ciphertext-b-1.damaged
../../onetime --config=blank-dot-onetime -d -p ../test-pad-1 \
-o tmp-plaintext-b-1 tmp-ciphertext-b-1.damaged 2>err.out
if ! grep -q "DecodingError: unexpected input" err.out
then
echo ""
echo "ERROR: decoder failed to detect trailing garbage in input stream"
PASSED="no"
fi
check_result
########################################################################
start_new_test "tampered head fuzz is detected, but decryption succeeds"
# This is actually a questionable "feature", really more an artifact
# of where the head fuzz digest is located in the stream than of
# deliberate UX design. Do we really want decryption to succeed if
# the head fuzz digest failed? But to do otherwise, we'd need to put
# the digest before the message text section.
## Encrypt message
../../onetime --test-mode \
--config=blank-dot-onetime -e -p ../test-pad-1 \
-o tmp-ciphertext-b-1 < ../test-plaintext-b 2>err.out
# In the base64-encoded ciphertext file, position 286 is 'v' (118).
../zap tmp-ciphertext-b-1 286 118 119 # tweak 'v' to 'w'
../../onetime --config=blank-dot-onetime -d -p ../test-pad-1 \
-o tmp-plaintext-b-1 < tmp-ciphertext-b-1 2>err.out
if ! grep -q "DigestMismatch: digest mismatch:" err.out || \
! grep -q " computed: fa258423e6c7559a529905a001d2e4c32ba322a73f5e867589025b1eeb91b220" err.out || \
! grep -q " received: 9581b039c3902390984fc98ef38f0ea1e44fb5a401cc0eb4a237b6777ffe0bb5" err.out
then
echo ""
echo "ERROR: did not see expected DigestMismatch error from tampered head fuzz"
cat err.out
PASSED="no"
fi
if ! cmp ../test-plaintext-b tmp-plaintext-b-1
then
echo ""
echo "ERROR: decryption unexpectedly failed when head fuzz tampered with"
head tmp-plaintext-b-1
echo "[...etc...]"
PASSED="no"
fi
check_result
########################################################################
start_new_test "tampering with ciphertext causes bzip decoder error"
## Encrypt message
../../onetime --config=blank-dot-onetime -e -p ../test-pad-1 \
-o tmp-ciphertext-b-1 < ../test-plaintext-b 2>err.out
# In the base64-encoded ciphertext file, position 8563 is 'h' (104).
../zap tmp-ciphertext-b-1 8563 104 105 # tweak 'h' to 'i'
../../onetime --config=blank-dot-onetime -d -p ../test-pad-1 \
< tmp-ciphertext-b-1 2>err.out
if ! grep -q "IOError: invalid data stream" err.out
then
echo ""