-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy patharchiver_test.go
More file actions
1210 lines (1071 loc) · 35 KB
/
Copy patharchiver_test.go
File metadata and controls
1210 lines (1071 loc) · 35 KB
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
package zip
import (
"bytes"
"context"
"encoding/binary"
"errors"
"fmt"
"io"
"os"
"path/filepath"
"runtime"
"strings"
"testing"
"time"
"github.com/unxed/zip/internal/filepool"
)
func TestArchiverAndExtractor(t *testing.T) {
srcDir := t.TempDir()
dstDir := t.TempDir()
zipPath := filepath.Join(t.TempDir(), "fast.zip")
// 1. Prepare source structure
filesToCreate := map[string]string{
"file1.txt": "hello parallel world",
"dir1/file2.txt": "inside a directory",
}
for path, content := range filesToCreate {
fullPath := filepath.Join(srcDir, path)
mustMkdirAll(t, filepath.Dir(fullPath))
if err := os.WriteFile(fullPath, []byte(content), 0600); err != nil {
t.Fatalf("failed to create test file: %v", err)
}
}
// Create symlink (skip on Windows unless running with admin privileges)
if runtime.GOOS != "windows" {
symlinkTarget := "file1.txt"
symlinkPath := filepath.Join(srcDir, "symlink.txt")
if err := os.Symlink(symlinkTarget, symlinkPath); err != nil {
t.Fatalf("failed to create symlink: %v", err)
}
}
// 2. Gather files for Archiver
filesMap := make(map[string]os.FileInfo)
err := filepath.Walk(srcDir, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if path == srcDir {
return nil // skip root
}
filesMap[path] = info
return nil
})
if err != nil {
t.Fatalf("walk failed: %v", err)
}
// 3. Archive files (Testing multi-threading)
f, err := os.Create(zipPath)
if err != nil {
t.Fatalf("failed to create zip file: %v", err)
}
closeAt(t, f)
archiver, err := NewArchiver(f, srcDir, WithArchiverConcurrency(4), WithArchiverMethod(Deflate))
if err != nil {
t.Fatalf("failed to init archiver: %v", err)
}
closeAt(t, archiver)
if err := archiver.Archive(context.Background(), filesMap); err != nil {
t.Fatalf("archive failed: %v", err)
}
if err := archiver.Close(); err != nil {
t.Fatalf("failed to close archiver: %v", err)
}
if err := f.Close(); err != nil {
t.Fatalf("failed to close %s: %v", zipPath, err)
}
// 4. Extract files (Testing multi-threading)
extractor, err := NewExtractor(zipPath, dstDir, WithExtractorConcurrency(4))
if err != nil {
t.Fatalf("failed to init extractor: %v", err)
}
closeAt(t, extractor)
if err := extractor.Extract(context.Background()); err != nil {
t.Fatalf("extract failed: %v", err)
}
if err := extractor.Close(); err != nil {
t.Fatalf("failed to close extractor: %v", err)
}
// 5. Verify extracted content
for path, expectedContent := range filesToCreate {
fullPath := filepath.Join(dstDir, path)
content, err := os.ReadFile(fullPath)
if err != nil {
t.Errorf("extracted file %s is missing: %v", path, err)
continue
}
if !bytes.Equal(content, []byte(expectedContent)) {
t.Errorf("extracted file %s content mismatch. Expected %q, got %q", path, expectedContent, string(content))
}
}
if runtime.GOOS != "windows" {
symlinkPath := filepath.Join(dstDir, "symlink.txt")
target, err := os.Readlink(symlinkPath)
if err != nil {
t.Errorf("symlink was not extracted properly: %v", err)
} else if target != "file1.txt" {
t.Errorf("symlink target mismatch. Expected 'file1.txt', got %q", target)
}
}
}
func TestArchiver_OutsideChrootNormalization(t *testing.T) {
tmp := t.TempDir()
chroot := filepath.Join(tmp, "inside")
mustMkdir(t, chroot)
outsideFile := filepath.Join(tmp, "outside.txt")
mustWriteFile(t, outsideFile, []byte("safe"), 0644)
zipPath := filepath.Join(tmp, "test.zip")
f, err := os.Create(zipPath)
if err != nil {
t.Fatal(err)
}
closeAt(t, f)
a, err := NewArchiver(f, chroot)
if err != nil {
t.Fatal(err)
}
closeAt(t, a)
info, _ := os.Stat(outsideFile)
files := map[string]os.FileInfo{
outsideFile: info,
}
if err := a.Archive(context.Background(), files); err != nil {
t.Fatalf("expected successful archive with normalized path, got: %v", err)
}
if err := a.Close(); err != nil {
t.Fatalf("close archiver: %v", err)
}
if err := f.Close(); err != nil {
t.Fatalf("close %s: %v", zipPath, err)
}
zr, err := OpenReader(zipPath)
if err != nil {
t.Fatal(err)
}
closeAt(t, zr)
if len(zr.File) != 1 {
t.Fatalf("expected 1 file, got %d", len(zr.File))
}
if filepath.IsAbs(zr.File[0].Name) || strings.HasPrefix(zr.File[0].Name, "../") {
t.Errorf("path was not safely normalized: %s", zr.File[0].Name)
}
}
func TestArchiver_SkipIrregularFiles(t *testing.T) {
tmp := t.TempDir()
fPath := filepath.Join(tmp, "normal.txt")
mustWriteFile(t, fPath, []byte("data"), 0644)
zipPath := filepath.Join(tmp, "out.zip")
zipF, err := os.Create(zipPath)
if err != nil {
t.Fatal(err)
}
closeAt(t, zipF)
a, err := NewArchiver(zipF, tmp)
if err != nil {
t.Fatal(err)
}
closeAt(t, a)
// Simulate FileInfo for a socket (irregular file)
files := make(map[string]os.FileInfo)
info, _ := os.Stat(fPath)
files[fPath] = info
// Manually add a file with socket mode (FileInfo is an interface)
files["/tmp/socket"] = mockFileInfo{name: "socket", mode: os.ModeSocket}
if err := a.Archive(context.Background(), files); err != nil {
t.Fatalf("archiver failed: %v", err)
}
// Verify that the archive contains only 1 file (the socket was skipped)
if err := a.Close(); err != nil {
t.Fatalf("close archiver: %v", err)
}
if err := zipF.Close(); err != nil {
t.Fatalf("close %s: %v", zipPath, err)
}
zr, err := OpenReader(zipPath)
if err != nil {
t.Fatal(err)
}
closeAt(t, zr)
if len(zr.File) != 1 {
t.Errorf("expected 1 file (socket should be skipped), got %d", len(zr.File))
}
}
func TestArchiver_EmptyEntries(t *testing.T) {
tmp := t.TempDir()
// Create an empty directory and an empty file
mustMkdir(t, filepath.Join(tmp, "empty_dir"))
mustWriteFile(t, filepath.Join(tmp, "empty_file.txt"), []byte{}, 0644)
zipPath := filepath.Join(tmp, "empty.zip")
zipF, err := os.Create(zipPath)
if err != nil {
t.Fatal(err)
}
closeAt(t, zipF)
a, err := NewArchiver(zipF, tmp)
if err != nil {
t.Fatal(err)
}
closeAt(t, a)
files := make(map[string]os.FileInfo)
if err := filepath.Walk(tmp, func(p string, info os.FileInfo, err error) error {
if p != tmp && p != zipPath {
files[p] = info
}
return nil
}); err != nil {
t.Fatalf("walk failed: %v", err)
}
if err := a.Archive(context.Background(), files); err != nil {
t.Fatalf("failed to archive empty entries: %v", err)
}
if err := a.Close(); err != nil {
t.Fatalf("close archiver: %v", err)
}
if err := zipF.Close(); err != nil {
t.Fatalf("close %s: %v", zipPath, err)
}
zr, err := OpenReader(zipPath)
if err != nil {
t.Fatal(err)
}
closeAt(t, zr)
foundFile := false
foundDir := false
for _, f := range zr.File {
if f.Name == "empty_file.txt" && f.UncompressedSize64 == 0 {
foundFile = true
}
if f.Name == "empty_dir/" {
foundDir = true
}
}
if !foundFile || !foundDir {
t.Errorf("archiver missed empty entries: file=%v, dir=%v", foundFile, foundDir)
}
}
func TestArchiver_MetadataPreservation(t *testing.T) {
tmp := t.TempDir()
srcDir := filepath.Join(tmp, "src")
mustMkdir(t, srcDir)
filePath := filepath.Join(srcDir, "meta.txt")
mustWriteFile(t, filePath, []byte("metadata preservation"), 0644)
now := time.Now().Truncate(time.Second)
if err := os.Chtimes(filePath, now.Add(-time.Hour), now); err != nil {
t.Fatal(err)
}
zipPath := filepath.Join(tmp, "meta.zip")
f, err := os.Create(zipPath)
if err != nil {
t.Fatal(err)
}
closeAt(t, f)
// Enable metadata support via Archiver option
a, err := NewArchiver(f, srcDir, WithArchiverPlatformMetadata(true))
if err != nil {
t.Fatal(err)
}
closeAt(t, a)
info, _ := os.Stat(filePath)
// Manually add OwnerSet to simulate a successful pull (since tests might not run as root)
fh := &FileHeader{
Name: "meta.txt",
Uid: 123,
Gid: 456,
Uname: "testuser",
Gname: "testgroup",
OwnerSet: true,
Modified: now,
}
if err := a.createFile(context.Background(), filePath, info, fh, nil); err != nil {
t.Fatalf("createFile failed: %v", err)
}
if err := a.Close(); err != nil {
t.Fatalf("close archiver: %v", err)
}
if err := f.Close(); err != nil {
t.Fatalf("close %s: %v", zipPath, err)
}
zr, err := OpenReader(zipPath)
if err != nil {
t.Fatal(err)
}
closeAt(t, zr)
file := zr.File[0]
uid, gid, ok := parseUnixExtra(file.Extra)
if !ok || uid != 123 || gid != 456 {
t.Errorf("Unix metadata not preserved in Archiver: %d:%d (ok=%v)", uid, gid, ok)
}
if file.Uname != "testuser" || file.Gname != "testgroup" {
t.Errorf("Unix owner name strings not preserved in Archiver: %q:%q", file.Uname, file.Gname)
}
}
func TestArchiver_ZstdParallel(t *testing.T) {
tmp := t.TempDir()
srcDir := filepath.Join(tmp, "src")
mustMkdir(t, srcDir)
// Generate multiple files for parallel processing
filesMap := make(map[string]os.FileInfo)
for i := 0; i < 20; i++ {
p := filepath.Join(srcDir, fmt.Sprintf("file_%d.bin", i))
mustWriteFile(t, p, make([]byte, 1024), 0644)
info, _ := os.Stat(p)
filesMap[p] = info
}
zipPath := filepath.Join(tmp, "zstd_para.zip")
zipF, err := os.Create(zipPath)
if err != nil {
t.Fatal(err)
}
closeAt(t, zipF)
// Use ZSTD in parallel mode
a, err := NewArchiver(zipF, srcDir, WithArchiverMethod(ZSTD), WithArchiverConcurrency(4))
if err != nil {
t.Fatal(err)
}
closeAt(t, a)
if err := a.Archive(context.Background(), filesMap); err != nil {
t.Fatalf("parallel ZSTD archive failed: %v", err)
}
if err := a.Close(); err != nil {
t.Fatalf("close archiver: %v", err)
}
if err := zipF.Close(); err != nil {
t.Fatalf("close %s: %v", zipPath, err)
}
// Verify that files are readable
zr, err := OpenReader(zipPath)
if err != nil {
t.Fatal(err)
}
closeAt(t, zr)
if len(zr.File) != 20 || zr.File[0].Method != ZSTD {
t.Errorf("ZSTD parallel archive error: count=%d, method=%d", len(zr.File), zr.File[0].Method)
}
}
func TestArchiver_ContextCancellation(t *testing.T) {
tmp := t.TempDir()
srcDir := filepath.Join(tmp, "src")
mustMkdir(t, srcDir)
p := filepath.Join(srcDir, "large.bin")
mustWriteFile(t, p, make([]byte, 1024*1024), 0644)
info, _ := os.Stat(p)
zipF, err := os.Create(filepath.Join(tmp, "cancel.zip"))
if err != nil {
t.Fatal(err)
}
closeAt(t, zipF)
ctx, cancel := context.WithCancel(context.Background())
a, err := NewArchiver(zipF, srcDir)
if err != nil {
t.Fatal(err)
}
closeAt(t, a)
// Cancel immediately
cancel()
err = a.Archive(ctx, map[string]os.FileInfo{p: info})
if err == nil || !errors.Is(err, context.Canceled) {
t.Errorf("expected context.Canceled, got: %v", err)
}
}
func TestArchiver_InvalidStageDir(t *testing.T) {
tmp := t.TempDir()
srcDir := filepath.Join(tmp, "src")
mustMkdirAll(t, srcDir)
// Create two files to force the Archiver to use FilePool (since concurrency=2)
mustWriteFile(t, filepath.Join(srcDir, "test1.txt"), make([]byte, 5*1024*1024), 0644)
mustWriteFile(t, filepath.Join(srcDir, "test2.txt"), make([]byte, 5*1024*1024), 0644)
zipF, err := os.Create(filepath.Join(tmp, "test.zip"))
if err != nil {
t.Fatal(err)
}
closeAt(t, zipF)
// Provide a non-existent path as the directory for buffers.
// Set BufferSize(0) so that data does not stay in memory,
// but goes directly to the file system (triggering a path error).
a, err := NewArchiver(zipF, srcDir,
WithArchiverConcurrency(2),
WithArchiverBufferSize(0),
WithStageDirectory(filepath.Join(tmp, "non-existent-path")))
if err != nil {
t.Fatal(err)
}
closeAt(t, a)
files := make(map[string]os.FileInfo)
i1, _ := os.Stat(filepath.Join(srcDir, "test1.txt"))
i2, _ := os.Stat(filepath.Join(srcDir, "test2.txt"))
files[filepath.Join(srcDir, "test1.txt")] = i1
files[filepath.Join(srcDir, "test2.txt")] = i2
err = a.Archive(context.Background(), files)
if err == nil {
t.Error("expected error due to invalid stage directory, got nil")
}
}
func TestArchiver_WrittenStats(t *testing.T) {
srcDir := t.TempDir()
zipPath := filepath.Join(t.TempDir(), "stats.zip")
// Create multiple files
mustWriteFile(t, filepath.Join(srcDir, "f1.txt"), []byte("data1"), 0644)
mustWriteFile(t, filepath.Join(srcDir, "f2.txt"), []byte("data22"), 0644)
filesMap := make(map[string]os.FileInfo)
if err := filepath.Walk(srcDir, func(path string, info os.FileInfo, err error) error {
if path != srcDir {
filesMap[path] = info
}
return nil
}); err != nil {
t.Fatalf("walk failed: %v", err)
}
f, err := os.Create(zipPath)
if err != nil {
t.Fatal(err)
}
closeAt(t, f)
archiver, err := NewArchiver(f, srcDir, WithArchiverSolid(true), WithArchiverMethod(Deflate))
if err != nil {
t.Fatal(err)
}
closeAt(t, archiver)
if err := archiver.Archive(context.Background(), filesMap); err != nil {
t.Fatalf("solid archiving failed: %v", err)
}
if err := archiver.Close(); err != nil {
t.Fatalf("close archiver: %v", err)
}
if err := f.Close(); err != nil {
t.Fatalf("close %s: %v", zipPath, err)
}
bytes, entries := archiver.Written()
// 2 entries (f1.txt and f2.txt) + 1 outer Solid.zip entry = 3 entries total
if entries != 3 {
t.Errorf("expected 3 entries total (2 inner + 1 outer), got %d", entries)
}
if bytes <= 0 {
t.Errorf("expected positive bytes written, got %d", bytes)
}
}
type mockFileInfo struct {
name string
mode os.FileMode
}
func (m mockFileInfo) Name() string { return m.name }
func (m mockFileInfo) Size() int64 { return 0 }
func (m mockFileInfo) Mode() os.FileMode { return m.mode }
func (m mockFileInfo) ModTime() time.Time { return time.Now() }
func (m mockFileInfo) IsDir() bool { return m.mode.IsDir() }
func (m mockFileInfo) Sys() interface{} { return nil }
func TestArchiver_SolidSeekIndex(t *testing.T) {
tmpDir := t.TempDir()
srcDir := filepath.Join(tmpDir, "src")
mustMkdirAll(t, srcDir)
mustWriteFile(t, filepath.Join(srcDir, "test.txt"), []byte("solid seek index test data"), 0644)
archivePath := filepath.Join(tmpDir, "solid.zip")
f, err := os.Create(archivePath)
if err != nil {
t.Fatal(err)
}
closeAt(t, f)
a, err := NewArchiver(f, srcDir,
WithArchiverSolid(true),
WithArchiverMethod(Deflate),
WithArchiverSeekIndex(1024, true),
)
if err != nil {
t.Fatal(err)
}
closeAt(t, a)
files := make(map[string]os.FileInfo)
if err := filepath.Walk(srcDir, func(path string, info os.FileInfo, err error) error {
if path != srcDir {
files[path] = info
}
return nil
}); err != nil {
t.Fatalf("walk failed: %v", err)
}
if err := a.Archive(context.Background(), files); err != nil {
t.Fatal(err)
}
if err := a.Close(); err != nil {
t.Fatalf("close archiver: %v", err)
}
if err := f.Close(); err != nil {
t.Fatalf("close %s: %v", archivePath, err)
}
zr, err := OpenReader(archivePath)
if err != nil {
t.Fatal(err)
}
closeAt(t, zr)
if len(zr.File) != 1 || zr.File[0].Name != "Solid.zip" {
t.Fatalf("Expected Solid.zip, got %v", zr.File[0].Name)
}
rs, err := zr.File[0].OpenSeekable()
if err != nil {
t.Fatalf("OpenSeekable failed on Solid archive: %v", err)
}
buf := make([]byte, 5)
n, _ := rs.Read(buf)
if n == 0 {
t.Fatalf("Read from seekable stream failed")
}
}
func TestArchiver_DifferentDrivesWindows(t *testing.T) {
if runtime.GOOS != "windows" {
t.Skip("Windows-only test")
}
tmp := t.TempDir()
archivePath := filepath.Join(tmp, "different_drives.zip")
f, err := os.Create(archivePath)
if err != nil {
t.Fatal(err)
}
closeAt(t, f)
a, err := NewArchiver(f, tmp)
if err != nil {
t.Fatal(err)
}
closeAt(t, a)
currentDrive := filepath.VolumeName(tmp)
targetDrive := "D:"
if currentDrive == "D:" || currentDrive == "d:" {
targetDrive = "C:"
}
targetPath := targetDrive + `\dummy_dir`
files := map[string]os.FileInfo{
targetPath: mockFileInfo{name: "dummy_dir", mode: os.ModeDir | 0755},
}
err = a.Archive(context.Background(), files)
if err != nil {
t.Fatalf("expected success, got error: %v", err)
}
}
func TestArchiver_CompressionHeuristics(t *testing.T) {
tmpDir := t.TempDir()
srcDir := filepath.Join(tmpDir, "src")
mustMkdirAll(t, srcDir)
// 1. Тест на эффективность сжатия мелкого текста (раньше было 104%, теперь должно быть < 100%)
// Генерируем текст с высокой энтропией Хаффмана, но плохим LZ77 (короткий, без повторов строк)
textPath := filepath.Join(srcDir, "small_text.txt")
var textBuf bytes.Buffer
for i := 0; i < 500; i++ {
fmt.Fprintf(&textBuf, "line %d: some unique text content here\n", i)
}
mustWriteFile(t, textPath, textBuf.Bytes(), 0644)
// 2. Тест на защиту нулей (не должны сжиматься через HuffmanOnly, иначе ratio будет ~12%)
zeroPath := filepath.Join(srcDir, "zeros.bin")
mustWriteFile(t, zeroPath, make([]byte, 1024*10), 0644)
zipPath := filepath.Join(tmpDir, "heuristics.zip")
f, err := os.Create(zipPath)
if err != nil {
t.Fatal(err)
}
closeAt(t, f)
// Используем уровень 1 (BestSpeed), на котором раньше были аномалии
a, err := NewArchiver(f, srcDir, WithArchiverLevel(1), WithArchiverMethod(Deflate))
if err != nil {
t.Fatal(err)
}
closeAt(t, a)
files := make(map[string]os.FileInfo)
if err := filepath.Walk(srcDir, func(p string, info os.FileInfo, err error) error {
if p != srcDir {
files[p] = info
}
return nil
}); err != nil {
t.Fatalf("walk failed: %v", err)
}
if err := a.Archive(context.Background(), files); err != nil {
t.Fatalf("Archive failed: %v", err)
}
if err := a.Close(); err != nil {
t.Fatalf("close archiver: %v", err)
}
if err := f.Close(); err != nil {
t.Fatalf("close %s: %v", zipPath, err)
}
zr, err := OpenReader(zipPath)
if err != nil {
t.Fatal(err)
}
closeAt(t, zr)
for _, file := range zr.File {
ratio := float64(file.CompressedSize64) / float64(file.UncompressedSize64) * 100
if file.Name == "small_text.txt" {
if ratio >= 100 {
t.Errorf("Small text anomaly: ratio is %.2f%%, expected < 100%%", ratio)
}
t.Logf("Small text ratio: %.2f%% (OK)", ratio)
}
if file.Name == "zeros.bin" {
// LZ77 сожмет 10КБ нулей почти в ноль (десятки байт).
// Хаффман сожмет 10КБ нулей до ~1.2КБ (12.5%).
if ratio > 5 {
t.Errorf("Zeros bloat anomaly: ratio is %.2f%%, expected < 5%% (LZ77 should be used)", ratio)
}
t.Logf("Zeros ratio: %.2f%% (OK)", ratio)
}
}
}
func TestArchiver_TorrentZipConsistencyWithHeuristics(t *testing.T) {
tmpDir := t.TempDir()
srcDir := filepath.Join(tmpDir, "src")
mustMkdirAll(t, srcDir)
// Создаем "идеальный" файл для Хаффмана (много уникальных символов, мало повторов)
// Эвристика analyzeBlock захотела бы включить HuffmanOnly (Level -2)
path := filepath.Join(srcDir, "huffman_bait.txt")
data := make([]byte, 8192)
for i := range data {
data[i] = byte(i % 256)
}
mustWriteFile(t, path, data, 0644)
zipPath := filepath.Join(tmpDir, "tz_test.zip")
f, err := os.Create(zipPath)
if err != nil {
t.Fatal(err)
}
closeAt(t, f)
// В режиме TorrentZip уровень ВСЕГДА должен оставаться 9 (LZ77)
a, err := NewArchiver(f, srcDir, WithArchiverTorrentZip(true))
if err != nil {
t.Fatal(err)
}
closeAt(t, a)
info, _ := os.Stat(path)
if err := a.Archive(context.Background(), map[string]os.FileInfo{path: info}); err != nil {
t.Fatalf("archive failed: %v", err)
}
if err := a.Close(); err != nil {
t.Fatalf("close archiver: %v", err)
}
if err := f.Close(); err != nil {
t.Fatalf("close %s: %v", zipPath, err)
}
zr, err := OpenReader(zipPath)
if err != nil {
t.Fatal(err)
}
closeAt(t, zr)
// На уровне 9 LZ77 на таком паттерне (0..255 повторяется) сработает идеально.
// Если бы включился HuffmanOnly, размер был бы точно 8192 + оверхед Хаффмана.
if zr.File[0].CompressedSize64 > 500 {
t.Errorf("TorrentZip likely used HuffmanOnly instead of Level 9 LZ77: comp size %d", zr.File[0].CompressedSize64)
}
}
func TestArchiver_ParallelEncryption(t *testing.T) {
tmpDir := t.TempDir()
srcDir := filepath.Join(tmpDir, "src")
dstDir := filepath.Join(tmpDir, "dst")
mustMkdirAll(t, srcDir)
password := "parallel-password-123"
// Create multiple files to force concurrency > 1
filesToCreate := map[string]string{
"file1.txt": "parallel encrypted content 1",
"file2.txt": "parallel encrypted content 2",
"file3.txt": "parallel encrypted content 3",
"file4.txt": "parallel encrypted content 4",
"file5.txt": "parallel encrypted content 5",
}
for path, content := range filesToCreate {
fullPath := filepath.Join(srcDir, path)
mustMkdirAll(t, filepath.Dir(fullPath))
if err := os.WriteFile(fullPath, []byte(content), 0600); err != nil {
t.Fatalf("failed to create test file: %v", err)
}
}
zipPath := filepath.Join(tmpDir, "parallel_enc.zip")
f, err := os.Create(zipPath)
if err != nil {
t.Fatalf("failed to create zip file: %v", err)
}
closeAt(t, f)
// Use WithArchiverConcurrency(4) to ensure multiple goroutines process files in parallel
a, err := NewArchiver(f, srcDir, WithArchiverConcurrency(4), WithArchiverPassword(password))
if err != nil {
t.Fatalf("failed to init archiver: %v", err)
}
closeAt(t, a)
filesMap := make(map[string]os.FileInfo)
err = filepath.Walk(srcDir, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if path != srcDir {
filesMap[path] = info
}
return nil
})
if err != nil {
t.Fatalf("walk failed: %v", err)
}
if err := a.Archive(context.Background(), filesMap); err != nil {
t.Fatalf("archive failed: %v", err)
}
if err := a.Close(); err != nil {
t.Fatalf("failed to close archiver: %v", err)
}
if err := f.Close(); err != nil {
t.Fatalf("failed to close %s: %v", zipPath, err)
}
// Extract and verify using multiple goroutines as well
mustMkdirAll(t, dstDir)
e, err := NewExtractor(zipPath, dstDir, WithExtractorPassword(password), WithExtractorConcurrency(4))
if err != nil {
t.Fatalf("failed to init extractor: %v", err)
}
closeAt(t, e)
if err := e.Extract(context.Background()); err != nil {
t.Fatalf("extract failed: %v", err)
}
if err := e.Close(); err != nil {
t.Fatalf("failed to close extractor: %v", err)
}
// Verify the extracted files contents
for path, expectedContent := range filesToCreate {
fullPath := filepath.Join(dstDir, path)
content, err := os.ReadFile(fullPath)
if err != nil {
t.Errorf("extracted file %s is missing: %v", path, err)
continue
}
if !bytes.Equal(content, []byte(expectedContent)) {
t.Errorf("extracted file %s content mismatch. Expected %q, got %q", path, expectedContent, string(content))
}
}
}
// symlinkModeInfo is a real file's FileInfo with the symlink bit set on top.
//
// Archive is handed the FileInfo along with the path, so an entry can claim to
// be a symlink while the path names an ordinary file. That is exactly what a
// caller does when the tree changed under it between the walk and the archive,
// and it makes os.Readlink fail for a reason no platform disagrees about --
// EINVAL on Unix, "the file or directory is not a reparse point" on Windows --
// without needing the privilege that creating a real symlink on Windows wants.
type symlinkModeInfo struct{ os.FileInfo }
func (s symlinkModeInfo) Mode() os.FileMode { return s.FileInfo.Mode() | os.ModeSymlink }
// TestArchiver_ErrorWithIdleWorkersReturns pins every exit from Archive
// against the worker channel being left open.
//
// The workers sit in `for task := range taskCh`, so only the close lets them
// out, and the errgroup is waited on in a deferred call. Any return from the
// middle of the submit loop therefore hangs that wait on every worker that has
// nothing left to do, unless the channel is closed on the path actually taken.
//
// The window is forced rather than raced for. The eight regular entries sort
// ahead of the ninth and all succeed, so by the time the loop reaches the
// symlink entry the pool is idle by construction; the symlink is archived
// inline, on the loop's own goroutine, and its Readlink failure returns
// straight out of the loop -- without cancelling the context, so no worker
// notices anything and all four stay parked. Against a close that happens at
// the bottom of the function this hangs every time, not one run in four.
func TestArchiver_ErrorWithIdleWorkersReturns(t *testing.T) {
tmp := t.TempDir()
srcDir := filepath.Join(tmp, "src")
if err := os.MkdirAll(srcDir, 0755); err != nil {
t.Fatal(err)
}
files := make(map[string]os.FileInfo)
add := func(name string, wrap bool) {
p := filepath.Join(srcDir, name)
if err := os.WriteFile(p, []byte("payload"), 0600); err != nil {
t.Fatal(err)
}
fi, err := os.Stat(p)
if err != nil {
t.Fatal(err)
}
if wrap {
fi = symlinkModeInfo{fi}
}
files[p] = fi
}
for i := 0; i < 8; i++ {
add(fmt.Sprintf("a%02d.txt", i), false)
}
// Sorts last, so every worker has drained its task and gone idle before
// the loop gets here.
add("zz_link", true)
zipF, err := os.Create(filepath.Join(tmp, "out.zip"))
if err != nil {
t.Fatal(err)
}
closeAt(t, zipF)
a, err := NewArchiver(zipF, srcDir, WithArchiverConcurrency(4))
if err != nil {
t.Fatal(err)
}
closeAt(t, a)
done := make(chan error, 1)
go func() { done <- a.Archive(context.Background(), files) }()
select {
case err := <-done:
if err == nil {
t.Fatal("expected the unreadable link to be reported")
}
case <-time.After(30 * time.Second):
buf := make([]byte, 1<<16)
n := runtime.Stack(buf, true)
t.Fatalf("Archive did not return within 30s, workers are still waiting on the task channel:\n%s", buf[:n])
}
}
// seekFailReader is a ReadSeeker whose seeks stop working from the nth one on,
// the way a file whose disk has gone away does. The reads keep working, so the
// failure is the rewind and nothing else.
type seekFailReader struct {
r *bytes.Reader
failAt int
seeks int
err error
}
func (s *seekFailReader) Read(p []byte) (int, error) { return s.r.Read(p) }
func (s *seekFailReader) Seek(offset int64, whence int) (int64, error) {
s.seeks++
if s.seeks >= s.failAt {
return 0, s.err
}
return s.r.Seek(offset, whence)
}
// expandingCompressor is a compressor that makes an entry twice the size it
// was, which is the case the fall back to Store exists for. A real compressor
// expands incompressible data by a fraction of a percent, so a file large
// enough to cross the archiver's threshold that way would be tens of
// megabytes of test fixture.
type expandingCompressor struct{ w io.Writer }
func (e expandingCompressor) Write(p []byte) (int, error) {
for i := 0; i < 2; i++ {
if _, err := e.w.Write(p); err != nil {
return 0, err
}
}
return len(p), nil
}
func (e expandingCompressor) Close() error { return nil }
// mixedEntropyBytes returns n bytes over 200 distinct values, which is what
// the archiver's heuristic reads as "worth deflating": too many distinct bytes
// to be worth Huffman coding alone, too few to look like data that is already
// compressed.
func mixedEntropyBytes(n int) []byte {
data := make([]byte, n)
for i := range data {
data[i] = byte(i % 200)
}
return data
}
// highEntropyBytes returns n bytes over all 256 values, evenly enough spread
// that the archiver's heuristic reads them as data that is already compressed
// and not worth deflating a second time. The generator is a plain xorshift so
// that the fixture is the same on every run.
func highEntropyBytes(n int) []byte {
data := make([]byte, 0, n+4)
x := uint32(2463534242)
var word [4]byte
for len(data) < n {
x ^= x << 13
x ^= x >> 17
x ^= x << 5
binary.LittleEndian.PutUint32(word[:], x)
data = append(data, word[:]...)
}
return data[:n]
}
// TestArchiver_CompressFileStoresWhatIsAlreadyCompressed covers the other
// answer the peek can give: a file the heuristic reads as incompressible is
// stored, and the compressor is never asked about it.
func TestArchiver_CompressFileStoresWhatIsAlreadyCompressed(t *testing.T) {
var buf bytes.Buffer
a, err := NewArchiver(&buf, t.TempDir())
if err != nil {
t.Fatalf("building the archiver: %v", err)
}
closeAt(t, a)
fp, err := filepool.New(t.TempDir(), 1, -1)
if err != nil {
t.Fatalf("building the file pool: %v", err)