/
integration.rs
1098 lines (922 loc) · 33.5 KB
/
integration.rs
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
use assert_cmd::Command;
use assert_fs::fixture::{FileWriteBin, NamedTempFile, TempDir};
use permutate::Permutator;
use rand::distributions::{Alphanumeric, Distribution, Standard};
use rand::rngs::StdRng;
use rand::SeedableRng;
use std::collections::{HashMap, HashSet};
use std::fs::{metadata, read_dir, File};
use std::io::{Read, Write};
use std::path::{Path, PathBuf};
use tar::Archive;
const SIZE_FILE1: usize = 10 * 1024 * 1024;
const SIZE_FILE2: usize = 10 * 1024 * 1024;
const UTIL: &str = "mlar";
struct TestFS {
// Files ordered by names
files: Vec<NamedTempFile>,
// Files ordered by their place in the archive
files_archive_order: Vec<PathBuf>,
}
fn setup() -> TestFS {
let tmp_file1 = NamedTempFile::new("file1.bin").unwrap();
let tmp_file2 = NamedTempFile::new("file2.bin").unwrap();
let tmp_file3 = NamedTempFile::new("file3.bin").unwrap();
// `file1.bin`: Use only alphanumeric charset to allow for compression
let mut rng: StdRng = SeedableRng::from_seed([0u8; 32]);
let data: Vec<u8> = Alphanumeric
.sample_iter(&mut rng)
.take(SIZE_FILE1)
.map(|c| c as u8)
.collect();
tmp_file1.write_binary(data.as_slice()).unwrap();
// `file2.bin`: Use full charset for bad compression
let mut rng: StdRng = SeedableRng::from_seed([0u8; 32]);
let data: Vec<u8> = Standard.sample_iter(&mut rng).take(SIZE_FILE2).collect();
tmp_file2.write_binary(data.as_slice()).unwrap();
// `file3.bin`: tiny file
tmp_file3.write_binary(b"ABCDEFGHIJ").unwrap();
let files_archive_order = vec![
tmp_file1.path().to_path_buf(),
tmp_file2.path().to_path_buf(),
tmp_file3.path().to_path_buf(),
];
let mut files = vec![tmp_file1, tmp_file2, tmp_file3];
files.sort_by(|i1, i2| Ord::cmp(&i1.path(), &i2.path()));
TestFS {
files,
files_archive_order,
}
}
fn ensure_tar_content(tar_file: &Path, files: &[NamedTempFile]) {
// Inspect the created TAR file
let mut arch = Archive::new(File::open(tar_file).unwrap());
// basename -> expected content
let mut fname2content = HashMap::new();
for file in files {
let mut content = Vec::new();
File::open(file.path())
.unwrap()
.read_to_end(&mut content)
.unwrap();
fname2content.insert(file.path().file_name().unwrap(), content);
}
for file in arch.entries().unwrap() {
// Detect I/O error (from `tar-rs` example)
let mut file = file.unwrap();
let pbuf = file.header().path().unwrap().to_path_buf();
let fname = pbuf.file_name().unwrap();
// Ensure the content is the expected one
let mut content = Vec::new();
file.read_to_end(&mut content).unwrap();
assert_eq!(&content, fname2content.get(fname).unwrap());
// Prepare for last check: correctness and completeness
fname2content.remove(fname);
}
// Ensure all files have been used
assert_eq!(fname2content.len(), 0);
}
fn ensure_directory_content(directory: &Path, files: &[NamedTempFile]) {
// basename -> expected content
let mut fname2content = HashMap::new();
for file in files {
let mut content = Vec::new();
File::open(file.path())
.unwrap()
.read_to_end(&mut content)
.unwrap();
fname2content.insert(file.path().file_name().unwrap(), content);
}
for entry in glob::glob(&(directory.to_string_lossy() + "/**/*")).unwrap() {
let entry = entry.unwrap();
if entry.metadata().unwrap().is_dir() {
// Ignore directories
continue;
}
let fname = entry.file_name().unwrap();
// Ensure the content is the expected one
let mut content = Vec::new();
File::open(&entry)
.unwrap()
.read_to_end(&mut content)
.unwrap();
assert_eq!(&content, fname2content.get(fname).unwrap());
// Prepare for last check: correctness and completeness
fname2content.remove(fname);
}
// Ensure all files have been used
assert_eq!(fname2content.len(), 0);
}
fn file_list_append_from_dir(dir: &Path, file_list: &mut Vec<String>) {
for entry in read_dir(dir).unwrap() {
let new_path = entry.unwrap().path();
if new_path.is_dir() {
file_list_append_from_dir(&new_path, file_list);
} else {
file_list.push(new_path.to_string_lossy().to_string());
}
}
}
#[test]
fn test_help() {
// `mlar --help`
let mut cmd = Command::cargo_bin(UTIL).unwrap();
cmd.arg("--help");
// Ensure the basic help display is working
println!("{:?}", cmd);
let assert = cmd.assert();
assert.success();
}
#[test]
fn test_create_from_dir() {
let mlar_file = NamedTempFile::new("output.mla").unwrap();
let ecc_public = Path::new("../samples/test_x25519_pub.pem");
let ecc_private = Path::new("../samples/test_x25519.pem");
// Temporary directory to test recursive file addition
let tmp_dir = TempDir::new().unwrap();
let mut subfile1_path = tmp_dir.path().join("subfile1");
let subdir_path = tmp_dir.path().join("subdir");
let mut subfile2_path = subdir_path.join("subfile2");
std::fs::write(&mut subfile1_path, "Test1").unwrap();
std::fs::create_dir(subdir_path).unwrap();
std::fs::write(&mut subfile2_path, "Test2").unwrap();
// `mlar create -o output.mla -p samples/test_x25519_pub.pem <tmp_dir>`
let mut cmd = Command::cargo_bin(UTIL).unwrap();
cmd.arg("create")
.arg("-o")
.arg(mlar_file.path())
.arg("-p")
.arg(ecc_public);
cmd.arg(tmp_dir.path());
let mut file_list: Vec<String> = Vec::new();
// The exact order of the files in the archive depends on the order of the
// result of `read_dir` which is plateform and filesystem dependent.
file_list_append_from_dir(tmp_dir.path(), &mut file_list);
println!("{:?}", cmd);
let assert = cmd.assert();
assert.success().stderr(file_list.join("\n") + "\n");
// `mlar list -i output.mla -k samples/test_x25519.pem`
let mut cmd = Command::cargo_bin(UTIL).unwrap();
cmd.arg("list")
.arg("-i")
.arg(mlar_file.path())
.arg("-k")
.arg(ecc_private);
println!("{:?}", cmd);
let assert = cmd.assert();
file_list.sort();
assert.success().stdout(file_list.join("\n") + "\n");
}
#[test]
fn test_create_filelist_stdin() {
let mlar_file = NamedTempFile::new("output.mla").unwrap();
let ecc_public = Path::new("../samples/test_x25519_pub.pem");
let ecc_private = Path::new("../samples/test_x25519.pem");
// Create files
let testfs = setup();
// `mlar create -o output.mla -p samples/test_x25519_pub.pem -`
let mut cmd = Command::cargo_bin(UTIL).unwrap();
cmd.arg("create")
.arg("-o")
.arg(mlar_file.path())
.arg("-p")
.arg(ecc_public);
cmd.arg("-");
println!("{:?}", cmd);
let mut file_list = String::new();
for file in &testfs.files {
file_list.push_str(format!("{}\n", file.path().to_string_lossy()).as_str());
}
cmd.write_stdin(String::from(&file_list));
println!("{:?}", file_list);
let assert = cmd.assert();
assert.success().stderr(String::from(&file_list));
// `mlar list -i output.mla -k samples/test_x25519.pem`
let mut cmd = Command::cargo_bin(UTIL).unwrap();
cmd.arg("list")
.arg("-i")
.arg(mlar_file.path())
.arg("-k")
.arg(ecc_private);
println!("{:?}", cmd);
let assert = cmd.assert();
assert.success().stdout(file_list);
}
#[test]
fn test_create_list_tar() {
let mlar_file = NamedTempFile::new("output.mla").unwrap();
let tar_file = NamedTempFile::new("output.tar").unwrap();
let ecc_public = Path::new("../samples/test_x25519_pub.pem");
let ecc_private = Path::new("../samples/test_x25519.pem");
// Create files
let testfs = setup();
// `mlar create -o output.mla -p samples/test_x25519_pub.pem file1.bin file2.bin file3.bin`
let mut cmd = Command::cargo_bin(UTIL).unwrap();
cmd.arg("create")
.arg("-o")
.arg(mlar_file.path())
.arg("-p")
.arg(ecc_public);
let mut file_list = String::new();
for file in &testfs.files {
cmd.arg(file.path());
file_list.push_str(format!("{}\n", file.path().to_string_lossy()).as_str());
}
println!("{:?}", cmd);
let assert = cmd.assert();
assert.success().stderr(String::from(&file_list));
// `mlar list -i output.mla -k samples/test_x25519.pem`
let mut cmd = Command::cargo_bin(UTIL).unwrap();
cmd.arg("list")
.arg("-i")
.arg(mlar_file.path())
.arg("-k")
.arg(ecc_private);
println!("{:?}", cmd);
let assert = cmd.assert();
assert.success().stdout(file_list);
// `mlar to-tar -i output.mla -k samples/test_x25519.pem -o output.tar`
let mut cmd = Command::cargo_bin(UTIL).unwrap();
cmd.arg("to-tar")
.arg("-i")
.arg(mlar_file.path())
.arg("-k")
.arg(ecc_private)
.arg("-o")
.arg(tar_file.path());
println!("{:?}", cmd);
let assert = cmd.assert();
assert.success();
// Inspect the created TAR file
ensure_tar_content(tar_file.path(), &testfs.files);
}
#[test]
fn test_truncated_repair_list_tar() {
let mlar_file = NamedTempFile::new("output.mla").unwrap();
let mlar_repaired_file = NamedTempFile::new("repaired.mla").unwrap();
let tar_file = NamedTempFile::new("output.tar").unwrap();
let ecc_public = Path::new("../samples/test_x25519_pub.pem");
let ecc_private = Path::new("../samples/test_x25519.pem");
// Create files
let testfs = setup();
// `mlar create -o output.mla -p samples/test_x25519_pub.pem file1.bin file2.bin file3.bin`
let mut cmd = Command::cargo_bin(UTIL).unwrap();
cmd.arg("create")
.arg("-o")
.arg(mlar_file.path())
.arg("-p")
.arg(ecc_public);
let mut file_list = String::new(); // Sorted by position in archive
let mut file_list_no_last = String::new(); // Sorted by name
for file in &testfs.files {
if file.path() != testfs.files_archive_order.last().unwrap() {
file_list_no_last.push_str(format!("{}\n", file.path().to_string_lossy()).as_str());
}
}
for path in &testfs.files_archive_order {
cmd.arg(path);
file_list.push_str(format!("{}\n", path.to_string_lossy()).as_str());
}
println!("{:?}", cmd);
let assert = cmd.assert();
assert.success().stderr(String::from(&file_list));
// Truncate output.mla
let mut data = Vec::new();
File::open(mlar_file.path())
.unwrap()
.read_to_end(&mut data)
.unwrap();
File::create(mlar_file.path())
.unwrap()
.write_all(&data[..data.len() * 6 / 7])
.unwrap();
// `mlar repair -i output.mla -k samples/test_x25519.pem -p samples/test_x25519_pub.pem -o repaired.mla`
let mut cmd = Command::cargo_bin(UTIL).unwrap();
cmd.arg("repair")
.arg("-i")
.arg(mlar_file.path())
.arg("-k")
.arg(ecc_private)
.arg("-p")
.arg(ecc_public)
.arg("-o")
.arg(mlar_repaired_file.path());
println!("{:?}", cmd);
let assert = cmd.assert();
assert.success();
// `mlar list -i repaired.mla -k samples/test_x25519.pem`
let mut cmd = Command::cargo_bin(UTIL).unwrap();
cmd.arg("list")
.arg("-i")
.arg(mlar_repaired_file.path())
.arg("-k")
.arg(ecc_private);
println!("{:?}", cmd);
let assert = cmd.assert();
// Do not consider the last file for test after trunc, as we truncate at
// 6 / 7 (last file being really small)
assert.success().stdout(file_list_no_last);
// `mlar to-tar -i output.mla -k samples/test_x25519.pem -o output.tar`
let mut cmd = Command::cargo_bin(UTIL).unwrap();
cmd.arg("to-tar")
.arg("-i")
.arg(mlar_repaired_file.path())
.arg("-k")
.arg(ecc_private)
.arg("-o")
.arg(tar_file.path());
println!("{:?}", cmd);
let assert = cmd.assert();
assert.success();
// Inspect the created TAR file
let mut arch = Archive::new(File::open(tar_file.path()).unwrap());
// basename -> expected content
let mut fname2content = HashMap::new();
// Do not consider the last file for test after trunc
for file in &testfs.files_archive_order[..testfs.files_archive_order.len() - 1] {
let mut content = Vec::new();
File::open(file).unwrap().read_to_end(&mut content).unwrap();
fname2content.insert(file.file_name().unwrap(), content);
}
for file in arch.entries().unwrap() {
// Detect I/O error (from `tar-rs` example)
let mut file = file.unwrap();
let pbuf = file.header().path().unwrap().to_path_buf();
let fname = pbuf.file_name().unwrap();
// Ensure the extracted content is the same as the expected one, even if
// truncated (ie, all the bytes must be correct, but the end can be missing)
let mut content = Vec::new();
file.read_to_end(&mut content).unwrap();
assert_eq!(
&content[..],
&fname2content.get(fname).unwrap()[..content.len()]
);
// Ensure we have at least one byte
assert_ne!(content.len(), 0);
// Prepare for last check: correctness and completeness
fname2content.remove(fname);
}
// Ensure all files have been used
assert_eq!(fname2content.len(), 0);
}
#[test]
fn test_multiple_keys() {
// Key parsing is common for each subcommands, so test only one: `list`
let mlar_file = NamedTempFile::new("output.mla").unwrap();
let ecc_publics = vec![
Path::new("../samples/test_x25519_pub.pem"),
Path::new("../samples/test_x25519_3_pub.pem"),
];
let ecc_privates = vec![
Path::new("../samples/test_x25519.pem"),
Path::new("../samples/test_x25519_2.pem"),
];
// Create files
let testfs = setup();
// `mlar create -o output.mla -p samples/test_x25519_pub.pem -p samples/test_x25519_3_pub.pem file1.bin file2.bin file3.bin`
let mut cmd = Command::cargo_bin(UTIL).unwrap();
cmd.arg("create")
.arg("-o")
.arg(mlar_file.path())
.arg("-p")
.arg(ecc_publics[0])
.arg("-p")
.arg(ecc_publics[1]);
let mut file_list = String::new();
for file in &testfs.files {
cmd.arg(file.path());
file_list.push_str(format!("{}\n", file.path().to_string_lossy()).as_str());
}
println!("{:?}", cmd);
let assert = cmd.assert();
assert.success().stderr(String::from(&file_list));
// Ensure:
// - we can read with one correct, one bad private key
// - we can read with only the second correct private key
// - we cannot read with only a bad private key
// `mlar list -i output.mla -k samples/test_x25519.pem -k samples/test_x25519_2.pem`
let mut cmd = Command::cargo_bin(UTIL).unwrap();
cmd.arg("list")
.arg("-i")
.arg(mlar_file.path())
.arg("-k")
.arg(&ecc_privates[0])
.arg("-k")
.arg(&ecc_privates[1]);
println!("{:?}", cmd);
let assert = cmd.assert();
assert.success().stdout(String::from(&file_list));
// `mlar list -i output.mla -k samples/test_x25519_3.pem`
let mut cmd = Command::cargo_bin(UTIL).unwrap();
cmd.arg("list")
.arg("-i")
.arg(mlar_file.path())
.arg("-k")
.arg(Path::new("../samples/test_x25519_3.pem"));
println!("{:?}", cmd);
let assert = cmd.assert();
assert.success().stdout(String::from(&file_list));
// `mlar list -i output.mla -k samples/test_x25519_2.pem`
let mut cmd = Command::cargo_bin(UTIL).unwrap();
cmd.arg("list")
.arg("-i")
.arg(mlar_file.path())
.arg("-k")
.arg(&ecc_privates[1]);
println!("{:?}", cmd);
let assert = cmd.assert();
assert.failure();
}
#[test]
fn test_multiple_compression_level() {
let mlar_file_q0 = NamedTempFile::new("output_q0.mla").unwrap();
let mlar_file_q5 = NamedTempFile::new("output_q5.mla").unwrap();
let tar_file_q0 = NamedTempFile::new("output_q0.tar").unwrap();
let tar_file_q5 = NamedTempFile::new("output_q5.tar").unwrap();
// Create files
let testfs = setup();
for (dest, compression_level) in &[(mlar_file_q0.path(), "0"), (mlar_file_q5.path(), "5")] {
// `mlar create -o {dest} -l compress -q {compression_level} file1.bin file2.bin file3.bin`
let mut cmd = Command::cargo_bin(UTIL).unwrap();
cmd.arg("create")
.arg("-o")
.arg(dest)
.arg("-l")
.arg("compress")
.arg("-q")
.arg(compression_level);
let mut file_list = String::new();
for file in &testfs.files {
cmd.arg(file.path());
file_list.push_str(format!("{}\n", file.path().to_string_lossy()).as_str());
}
println!("{:?}", cmd);
let assert = cmd.assert();
assert.success().stderr(String::from(&file_list));
}
// Hopefully, if compression works, q0 must be smaller than q5
let q0_size = metadata(mlar_file_q0.path()).unwrap().len();
let q5_size = metadata(mlar_file_q5.path()).unwrap().len();
assert!(q5_size < q0_size);
// Ensure files are correct
for (src, tar_name) in vec![(mlar_file_q0, &tar_file_q0), (mlar_file_q5, &tar_file_q5)] {
// `mlar to-tar -i {src} -o {tar_name}`
let mut cmd = Command::cargo_bin(UTIL).unwrap();
cmd.arg("to-tar")
.arg("-i")
.arg(src.path())
.arg("-o")
.arg(tar_name.path());
println!("{:?}", cmd);
let assert = cmd.assert();
assert.success();
}
ensure_tar_content(tar_file_q0.path(), &testfs.files);
ensure_tar_content(tar_file_q5.path(), &testfs.files);
}
#[test]
fn test_convert() {
// Create an archive with one public key, convert it to use only another key
// without compression, then verify the size and the content of the archive
let mlar_file = NamedTempFile::new("output.mla").unwrap();
let mlar_file_converted = NamedTempFile::new("convert.mla").unwrap();
let tar_file = NamedTempFile::new("output.tar").unwrap();
let ecc_public1 = Path::new("../samples/test_x25519_pub.pem");
let ecc_private1 = Path::new("../samples/test_x25519.pem");
let ecc_public2 = Path::new("../samples/test_x25519_2_pub.pem");
let ecc_private2 = Path::new("../samples/test_x25519_2.pem");
// Create files
let testfs = setup();
// `mlar create -o output.mla -p samples/public_1024.der file1.bin file2.bin file3.bin`
let mut cmd = Command::cargo_bin(UTIL).unwrap();
cmd.arg("create")
.arg("-o")
.arg(mlar_file.path())
.arg("-p")
.arg(ecc_public1);
let mut file_list = String::new();
for file in &testfs.files {
cmd.arg(file.path());
file_list.push_str(format!("{}\n", file.path().to_string_lossy()).as_str());
}
println!("{:?}", cmd);
let assert = cmd.assert();
assert.success().stderr(String::from(&file_list));
// `mlar convert -i output.mla -k samples/private_1024.der -l encrypt -o convert.mla -p samples/public_2048.der`
let mut cmd = Command::cargo_bin(UTIL).unwrap();
cmd.arg("convert")
.arg("-i")
.arg(mlar_file.path())
.arg("-k")
.arg(ecc_private1)
.arg("-l")
.arg("encrypt")
.arg("-o")
.arg(mlar_file_converted.path())
.arg("-p")
.arg(ecc_public2);
println!("{:?}", cmd);
let assert = cmd.assert();
assert.success().stderr(String::from(&file_list));
// Hopefully, compressed must be smaller than without compression
let size_output = metadata(mlar_file.path()).unwrap().len();
let size_convert = metadata(mlar_file_converted.path()).unwrap().len();
assert!(size_output < size_convert);
// `mlar to-tar -i convert.mla -k samples/private_2048.der -o output.tar`
let mut cmd = Command::cargo_bin(UTIL).unwrap();
cmd.arg("to-tar")
.arg("-i")
.arg(mlar_file_converted.path())
.arg("-k")
.arg(ecc_private2)
.arg("-o")
.arg(tar_file.path());
println!("{:?}", cmd);
let assert = cmd.assert();
assert.success();
// Inspect the created TAR file
ensure_tar_content(tar_file.path(), &testfs.files);
}
#[test]
fn test_stdio() {
// Create an archive on stdout, and check it
let mlar_file = NamedTempFile::new("output.mla").unwrap();
let tar_file = NamedTempFile::new("output.tar").unwrap();
let ecc_public = Path::new("../samples/test_x25519_pub.pem");
let ecc_private = Path::new("../samples/test_x25519.pem");
// Create files
let testfs = setup();
// `mlar create -o - -p samples/test_x25519_pub.pem file1.bin file2.bin file3.bin`
let mut cmd = Command::cargo_bin(UTIL).unwrap();
cmd.arg("create")
.arg("-o")
.arg("-")
.arg("-p")
.arg(ecc_public);
let mut file_list = String::new();
for file in &testfs.files {
cmd.arg(file.path());
file_list.push_str(format!("{}\n", file.path().to_string_lossy()).as_str());
}
println!("{:?}", cmd);
let assert = cmd.assert();
let archive_data = assert.get_output().stdout.clone();
assert.success().stderr(String::from(&file_list));
File::create(mlar_file.path())
.unwrap()
.write_all(&archive_data)
.unwrap();
// `mlar to-tar -i output.mla -k samples/test_x25519.pem -o output.tar`
let mut cmd = Command::cargo_bin(UTIL).unwrap();
cmd.arg("to-tar")
.arg("-i")
.arg(mlar_file.path())
.arg("-k")
.arg(ecc_private)
.arg("-o")
.arg(tar_file.path());
println!("{:?}", cmd);
let assert = cmd.assert();
assert.success();
// Inspect the created TAR file
ensure_tar_content(tar_file.path(), &testfs.files);
}
#[test]
fn test_multi_fileorders() {
// Create several archive with all possible file order. Result should be the same
let mlar_file = NamedTempFile::new("output.mla").unwrap();
let tar_file = NamedTempFile::new("output.tar").unwrap();
let ecc_public = Path::new("../samples/test_x25519_pub.pem");
let ecc_private = Path::new("../samples/test_x25519.pem");
// Create files
let testfs = setup();
let path_array: &[&Path] = &[
testfs.files[0].path(),
testfs.files[1].path(),
testfs.files[2].path(),
];
let path_array = [path_array];
let permutator = Permutator::new(&path_array[..]);
for list in permutator {
let set: HashSet<_> = list.iter().collect(); // dedup
if set.len() != list.len() {
// Duplicate, avoid
continue;
}
// `mlar create -o output.mla -p samples/test_x25519_pub.pem file1.bin file2.bin file3.bin`
let mut cmd = Command::cargo_bin(UTIL).unwrap();
cmd.arg("create")
.arg("-o")
.arg(mlar_file.path())
.arg("-p")
.arg(ecc_public);
let mut file_list = String::new();
for file in list {
cmd.arg(file);
file_list.push_str(format!("{}\n", file.to_string_lossy()).as_str());
}
println!("{:?}", cmd);
let assert = cmd.assert();
assert.success().stderr(String::from(&file_list));
// `mlar to-tar -i convert.mla -k samples/test_x25519.pem -o output.tar`
let mut cmd = Command::cargo_bin(UTIL).unwrap();
cmd.arg("to-tar")
.arg("-i")
.arg(mlar_file.path())
.arg("-k")
.arg(ecc_private)
.arg("-o")
.arg(tar_file.path());
println!("{:?}", cmd);
let assert = cmd.assert();
assert.success();
// Inspect the created TAR file
ensure_tar_content(tar_file.path(), &testfs.files);
}
}
#[test]
fn test_verbose_listing() {
let mlar_file = NamedTempFile::new("output.mla").unwrap();
let testfs = setup();
// `mlar create -l -o output.mla
let mut cmd = Command::cargo_bin(UTIL).unwrap();
cmd.arg("create").arg("-l").arg("-o").arg(mlar_file.path());
let mut file_list = String::new();
for file in &testfs.files {
cmd.arg(file.path());
file_list.push_str(format!("{}\n", file.path().to_string_lossy()).as_str());
}
println!("{:?}", cmd);
let assert = cmd.assert();
assert.success().stderr(String::from(&file_list));
// `mlar list -i output.mla`
let mut cmd = Command::cargo_bin(UTIL).unwrap();
cmd.arg("list").arg("-i").arg(mlar_file.path());
println!("{:?}", cmd);
let assert = cmd.assert();
assert.success().stdout(file_list);
// `mlar list -v -i output.mla`
let mut cmd = Command::cargo_bin(UTIL).unwrap();
cmd.arg("list").arg("-v").arg("-i").arg(mlar_file.path());
println!("{:?}", cmd);
let assert = cmd.assert();
assert.success();
// `mlar list -vv -i output.mla`
let mut cmd = Command::cargo_bin(UTIL).unwrap();
cmd.arg("list").arg("-vv").arg("-i").arg(mlar_file.path());
println!("{:?}", cmd);
let assert = cmd.assert();
assert.success();
}
#[test]
fn test_extract() {
let mlar_file = NamedTempFile::new("output.mla").unwrap();
let mut testfs = setup();
// `mlar create -l -o output.mla
let mut cmd = Command::cargo_bin(UTIL).unwrap();
cmd.arg("create").arg("-l").arg("-o").arg(mlar_file.path());
let mut file_list = String::new();
for file in &testfs.files {
cmd.arg(file.path());
file_list.push_str(format!("{}\n", file.path().to_string_lossy()).as_str());
}
println!("{:?}", cmd);
let assert = cmd.assert();
assert.success().stderr(String::from(&file_list));
let mut file_list = String::new();
for file in &testfs.files {
file_list.push_str(format!("{}\n", file.path().to_string_lossy()).as_str());
}
// Test global (with all files)
// `mlar extract -v -i output.mla -o ouput_dir -g '*'`
let output_dir = TempDir::new().unwrap();
let mut cmd = Command::cargo_bin(UTIL).unwrap();
cmd.arg("extract")
.arg("-v")
.arg("-i")
.arg(mlar_file.path())
.arg("-o")
.arg(output_dir.path())
.arg("-g")
.arg("*");
println!("{:?}", cmd);
let assert = cmd.assert();
assert.success().stdout(file_list);
ensure_directory_content(output_dir.path(), &testfs.files);
// Test linear extraction of all files
// `mlar extract -v -i output.mla -o ouput_dir`
let output_dir = TempDir::new().unwrap();
let mut cmd = Command::cargo_bin(UTIL).unwrap();
cmd.arg("extract")
.arg("-v")
.arg("-i")
.arg(mlar_file.path())
.arg("-o")
.arg(output_dir.path());
println!("{:?}", cmd);
let assert = cmd.assert();
assert
.success()
.stdout("Extracting the whole archive using a linear extraction\n");
ensure_directory_content(output_dir.path(), &testfs.files);
// Test extraction of one file explicitly
// `mlar extract -v -i output.mla -o ouput_dir file1`
let one_filename = &testfs.files_archive_order[0];
let mut one_file = Vec::new();
loop {
match testfs.files.pop() {
None => {
break;
}
Some(ntf) => {
if ntf.path() == one_filename {
one_file.push(ntf);
}
}
}
}
let output_dir = TempDir::new().unwrap();
let mut cmd = Command::cargo_bin(UTIL).unwrap();
cmd.arg("extract")
.arg("-v")
.arg("-i")
.arg(mlar_file.path())
.arg("-o")
.arg(output_dir.path())
.arg(one_filename);
println!("{:?}", cmd);
let assert = cmd.assert();
assert
.success()
.stdout(format!("{}\n", one_filename.to_string_lossy()));
ensure_directory_content(output_dir.path(), &one_file);
// Test extraction of one file through glob
// `mlar extract -v -i output.mla -o ouput_dir -g *1*`
let output_dir = TempDir::new().unwrap();
let mut cmd = Command::cargo_bin(UTIL).unwrap();
cmd.arg("extract")
.arg("-v")
.arg("-i")
.arg(mlar_file.path())
.arg("-o")
.arg(output_dir.path())
.arg("-g")
.arg("*file1*");
println!("{:?}", cmd);
let assert = cmd.assert();
assert
.success()
.stdout(format!("{}\n", one_filename.to_string_lossy()));
ensure_directory_content(output_dir.path(), &one_file);
}
#[test]
fn test_cat() {
let mlar_file = NamedTempFile::new("output.mla").unwrap();
let testfs = setup();
// `mlar create -l -o output.mla
let mut cmd = Command::cargo_bin(UTIL).unwrap();
cmd.arg("create").arg("-l").arg("-o").arg(mlar_file.path());
let mut file_list = String::new();
for file in &testfs.files {
cmd.arg(file.path());
file_list.push_str(format!("{}\n", file.path().to_string_lossy()).as_str());
}
println!("{:?}", cmd);
let assert = cmd.assert();
assert.success().stderr(String::from(&file_list));
// `mlar cat -i output.mla file1`
let mut cmd = Command::cargo_bin(UTIL).unwrap();
cmd.arg("cat")
.arg("-i")
.arg(mlar_file.path())
.arg(&testfs.files_archive_order[2]);
println!("{:?}", cmd);
let assert = cmd.assert();
let mut expected_content = Vec::new();
File::open(&testfs.files_archive_order[2])
.unwrap()
.read_to_end(&mut expected_content)
.unwrap();
assert_eq!(assert.success().get_output().stdout, expected_content);
}
#[test]
fn test_keygen() {
// Gen a keypair, create and list an archive using them
let mlar_file = NamedTempFile::new("output.mla").unwrap();
let output_dir = TempDir::new().unwrap();
let base_name = output_dir.path().join("key");
let testfs = setup();
// `mlar keygen tempdir/key`
let mut cmd = Command::cargo_bin(UTIL).unwrap();
cmd.arg("keygen").arg(&base_name);
cmd.assert().success();
// `mlar create -p tempdir/key.pub -o output.mla file1 file2 file3`
let mut cmd = Command::cargo_bin(UTIL).unwrap();
cmd.arg("create")
.arg("-p")
.arg(base_name.with_extension("pub"))
.arg("-o")
.arg(mlar_file.path());
let mut file_list = String::new();
for file in &testfs.files {
cmd.arg(file.path());
file_list.push_str(format!("{}\n", file.path().to_string_lossy()).as_str());
}
println!("{:?}", cmd);
let assert = cmd.assert();
assert.success().stderr(String::from(&file_list));
// `mlar list -k tempdir/key -i output.mla`
let mut cmd = Command::cargo_bin(UTIL).unwrap();
cmd.arg("list")
.arg("-k")
.arg(base_name)
.arg("-i")
.arg(mlar_file.path());
println!("{:?}", cmd);
let assert = cmd.assert();
assert.success().stdout(file_list);
}
#[test]
fn test_verbose_info() {