-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreader_test.go
More file actions
566 lines (481 loc) · 14.5 KB
/
reader_test.go
File metadata and controls
566 lines (481 loc) · 14.5 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
// Copyright 2025 Lemon4ksan. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package gozip
import (
"bytes"
"context"
"encoding/binary"
"errors"
"hash/crc32"
"io"
"io/fs"
"testing"
"github.com/lemon4ksan/gozip/internal"
"github.com/lemon4ksan/gozip/internal/sys"
)
// writeOnlyBuffer wraps bytes.Buffer to hide Bytes(), ReadFrom and other methods,
// exposing only Write. This forces the ZipWriter to use Data Descriptors (Bit 3).
type writeOnlyBuffer struct {
buf *bytes.Buffer
}
func (w *writeOnlyBuffer) Write(p []byte) (int, error) {
return w.buf.Write(p)
}
func makeEOCD(entries uint16, cdSize, cdOffset uint32, comment string) []byte {
buf := new(bytes.Buffer)
binary.Write(buf, binary.LittleEndian, internal.EOCDSignature)
binary.Write(buf, binary.LittleEndian, uint16(0)) // Disk number
binary.Write(buf, binary.LittleEndian, uint16(0)) // Disk number with start
binary.Write(buf, binary.LittleEndian, entries) // Entries on disk
binary.Write(buf, binary.LittleEndian, entries) // Total entries
binary.Write(buf, binary.LittleEndian, cdSize) // Size of CD
binary.Write(buf, binary.LittleEndian, cdOffset) // Offset of CD
binary.Write(buf, binary.LittleEndian, uint16(len(comment))) // Comment len
buf.WriteString(comment)
return buf.Bytes()
}
func TestFindAndReadEndOfCentralDir(t *testing.T) {
tests := []struct {
name string
data []byte
wantFound bool
wantErr bool
}{
{
name: "Simple EOCD at end",
data: makeEOCD(5, 100, 200, ""),
wantFound: true,
},
{
name: "EOCD with comment",
data: makeEOCD(1, 50, 10, "This is a comment"),
wantFound: true,
},
{
name: "EOCD with comment preceded by garbage",
data: append([]byte("garbage data..."), makeEOCD(1, 50, 10, "Comment")...),
wantFound: true,
},
{
name: "Fake EOCD signature in comment (edge case)",
data: append([]byte("prefix"), makeEOCD(1, 50, 10, "Fake PK\x05\x06 signature")...),
wantFound: true,
},
{
name: "File too small",
data: []byte("too short"),
wantFound: false,
wantErr: true,
},
{
name: "No EOCD signature",
data: make([]byte, 100), // Just zeros
wantFound: false,
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
r := bytes.NewReader(tt.data)
zr := newZipReader(r, r.Size(), nil, ZipConfig{})
got, err := zr.FindAndReadEOCD(context.Background())
if (err != nil) != tt.wantErr {
t.Fatalf("findAndReadEndOfCentralDir() error = %v, wantErr %v", err, tt.wantErr)
}
if !tt.wantErr && tt.wantFound {
if got.EntriesNum == 0 && len(tt.data) > 30 {
// Basic check to see if we parsed something meaningful from our helper
// (assuming helper sets entries > 0 usually, except specific cases)
}
}
})
}
}
func TestFindEOCD_BufferBoundary(t *testing.T) {
comment := "short"
eocd := makeEOCD(1, 10, 10, comment)
// Ensure EOCD straddles the 1024-byte buffer boundary used in scanning
prefixLen := 1024 + 10
data := make([]byte, prefixLen)
data = append(data, eocd...)
r := bytes.NewReader(data)
zr := newZipReader(r, r.Size(), nil, ZipConfig{})
res, err := zr.FindAndReadEOCD(context.Background())
if err != nil {
t.Fatalf("Failed to find EOCD across buffer boundaries: %v", err)
}
if res.CommentLength != uint16(len(comment)) {
t.Errorf("Wrong comment length found: %d", res.CommentLength)
}
}
func TestParseZip64_Internal(t *testing.T) {
// Tag(2) + Size(2) + Uncomp(8) + Comp(8) + Offset(8)
data := internal.EncodeZip64ExtraField(nil, 5000000000, 5000000000, 5000000000)
f := &File{}
entry := internal.SharedEntry{
UncompressedSize: StandardSizeLimit,
CompressedSize: StandardSizeLimit,
LocalHeaderOffset: StandardSizeLimit,
}
parseZip64(f, data[4:], entry)
if f.uncompressedSize != 5000000000 {
t.Errorf("Uncompressed mismatch: %d", f.uncompressedSize)
}
if f.compressedSize != 5000000000 {
t.Errorf("Compressed mismatch: %d", f.compressedSize)
}
if f.localHeaderOffset != 5000000000 {
t.Errorf("LocalHeaderOffset mismatch: %d", f.localHeaderOffset)
}
}
func TestNewFileFromCentralDir_Zip64(t *testing.T) {
cd := internal.CentralDirectory{
UncompressedSize: StandardSizeLimit,
CompressedSize: StandardSizeLimit,
LocalHeaderOffset: StandardSizeLimit,
Filename: "large_file.dat",
ExtraField: make([]byte, 0),
}
// Construct the Zip64 Extra Field payload
// The map value in internal.CentralDirectory stores only the data payload.
extraPayload := new(bytes.Buffer)
binary.Write(extraPayload, binary.LittleEndian, []byte{0x01, 0x00, 0x18, 0x00})
binary.Write(extraPayload, binary.LittleEndian, uint64(5000000000)) // Real Uncompressed > 4GB
binary.Write(extraPayload, binary.LittleEndian, uint64(4000000000)) // Real Compressed
binary.Write(extraPayload, binary.LittleEndian, uint64(1000000000)) // Real Offset
cd.ExtraField = extraPayload.Bytes()
zr := &zipReader{}
f := zr.newFileFromCentralDir(cd)
if f.uncompressedSize != 5000000000 {
t.Errorf("Zip64 uncompressed size mismatch: got %d", f.uncompressedSize)
}
if f.compressedSize != 4000000000 {
t.Errorf("Zip64 compressed size mismatch: got %d", f.compressedSize)
}
if f.localHeaderOffset != 1000000000 {
t.Errorf("Zip64 offset mismatch: got %d", f.localHeaderOffset)
}
}
func TestParseFileExternalAttributes(t *testing.T) {
tests := []struct {
name string
entry internal.CentralDirectory
wantMode fs.FileMode
}{
{
name: "Unix Regular File (0644)",
entry: internal.CentralDirectory{
VersionMadeBy: uint16(sys.HostSystemUNIX) << 8,
ExternalFileAttributes: uint32(0644) << 16,
},
wantMode: 0644,
},
{
name: "Unix Directory (0755)",
entry: internal.CentralDirectory{
VersionMadeBy: uint16(sys.HostSystemUNIX) << 8,
ExternalFileAttributes: uint32(0040755) << 16, // IFDIR + 0755
},
wantMode: 0755 | fs.ModeDir,
},
{
name: "Windows/DOS ReadOnly",
entry: internal.CentralDirectory{
VersionMadeBy: uint16(sys.HostSystemFAT) << 8,
ExternalFileAttributes: 0x01, // ReadOnly bit
Filename: "file.txt",
},
// 0644 &^ 0222 = 0444
wantMode: 0444,
},
{
name: "Windows Directory",
entry: internal.CentralDirectory{
VersionMadeBy: uint16(sys.HostSystemFAT) << 8,
ExternalFileAttributes: 0x10, // Directory bit
Filename: "folder/",
},
wantMode: 0755 | fs.ModeDir,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := internal.ParseFileMode(tt.entry)
if got != tt.wantMode {
t.Errorf("parseFileExternalAttributes() = %v, want %v", got, tt.wantMode)
}
})
}
}
func TestChecksumReader(t *testing.T) {
data := []byte("hello world")
crc := crc32.ChecksumIEEE(data)
t.Run("Valid Checksum", func(t *testing.T) {
rc := io.NopCloser(bytes.NewReader(data))
cr := &checksumReader{
rc: rc,
hash: crc32.NewIEEE(),
wantCRC: crc,
wantSize: uint64(len(data)),
}
if _, err := io.Copy(io.Discard, cr); err != nil {
t.Fatalf("Read failed: %v", err)
}
if err := cr.Close(); err != nil {
t.Errorf("Close failed (checksum valid): %v", err)
}
})
t.Run("Partial Read (No Error)", func(t *testing.T) {
rc := io.NopCloser(bytes.NewReader(data))
cr := &checksumReader{
rc: rc,
hash: crc32.NewIEEE(),
wantCRC: crc,
wantSize: uint64(len(data)),
}
// Read only 1 byte
if _, err := io.ReadFull(cr, make([]byte, 1)); err != nil {
t.Fatal(err)
}
// Closing partial read should NOT return error (allows peeking)
if err := cr.Close(); err != nil {
t.Errorf("Expected nil error for partial read close, got: %v", err)
}
})
t.Run("Invalid Checksum", func(t *testing.T) {
rc := io.NopCloser(bytes.NewReader([]byte("wrong data")))
cr := &checksumReader{
rc: rc,
hash: crc32.NewIEEE(),
wantCRC: crc,
wantSize: uint64(len("wrong data")),
}
io.Copy(io.Discard, cr)
err := cr.Close()
if !errors.Is(err, ErrChecksum) {
t.Errorf("Expected ErrCheckSum, got: %v", err)
}
})
t.Run("Size Mismatch (Too long)", func(t *testing.T) {
longData := append(data, '!')
rc := io.NopCloser(bytes.NewReader(longData))
cr := &checksumReader{
rc: rc,
hash: crc32.NewIEEE(),
wantCRC: crc,
wantSize: uint64(len(data)),
}
_, err := io.Copy(io.Discard, cr)
if !errors.Is(err, ErrSizeMismatch) {
t.Errorf("Expected ErrSizeMismatch, got %v", err)
}
})
}
func TestZipReader_OpenFile_Integration(t *testing.T) {
buf := new(bytes.Buffer)
content := []byte("test content")
crc := crc32.ChecksumIEEE(content)
lhOffset := buf.Len()
binary.Write(buf, binary.LittleEndian, internal.LocalFileHeaderSignature)
binary.Write(buf, binary.LittleEndian, uint16(20)) // Version
binary.Write(buf, binary.LittleEndian, uint16(0)) // Flags
binary.Write(buf, binary.LittleEndian, uint16(Store)) // Method
binary.Write(buf, binary.LittleEndian, uint16(0)) // Time
binary.Write(buf, binary.LittleEndian, uint16(0)) // Date
binary.Write(buf, binary.LittleEndian, crc)
binary.Write(buf, binary.LittleEndian, uint32(len(content))) // Compressed
binary.Write(buf, binary.LittleEndian, uint32(len(content))) // Uncompressed
binary.Write(buf, binary.LittleEndian, uint16(4)) // Filename Len
binary.Write(buf, binary.LittleEndian, uint16(0)) // Extra Len
buf.WriteString("test")
buf.Write(content)
reader := bytes.NewReader(buf.Bytes())
zr := newZipReader(reader, reader.Size(), nil, ZipConfig{})
f := &File{
name: "test",
config: FileConfig{CompressionMethod: Store},
localHeaderOffset: int64(lhOffset),
compressedSize: int64(len(content)),
uncompressedSize: int64(len(content)),
crc32: crc,
}
rc, err := zr.openFile(f)
if err != nil {
t.Fatalf("openFile failed: %v", err)
}
defer rc.Close()
readBuf, err := io.ReadAll(rc)
if err != nil {
t.Fatalf("ReadAll failed: %v", err)
}
if !bytes.Equal(readBuf, content) {
t.Errorf("Content mismatch: got %s, want %s", readBuf, content)
}
if err := rc.Close(); err != nil {
t.Errorf("Close (checksum verification) failed: %v", err)
}
}
func TestStreamReader_DataDescriptor(t *testing.T) {
content := []byte("stream content")
crc := crc32.ChecksumIEEE(content)
buf := new(bytes.Buffer)
lh := internal.LocalFileHeader{
GeneralPurposeBitFlag: 0x08, // Bit 3
CompressionMethod: uint16(Store),
Filename: "stream.txt",
FilenameLength: 10,
}
buf.Write(lh.AppendBytes(nil))
buf.Write(content)
buf.Write(internal.EncodeDataDescriptor(crc, int64(len(content)), int64(len(content))))
nextLh := internal.LocalFileHeader{Filename: "next.txt", FilenameLength: 8}
buf.Write(nextLh.AppendBytes(nil))
sr := NewStreamReader(buf)
_, err := sr.Next()
if err != nil {
t.Fatalf("Next failed: %v", err)
}
rc, err := sr.Open()
if err != nil {
t.Fatalf("Open failed: %v", err)
}
got, _ := io.ReadAll(rc)
if !bytes.Equal(got, content) {
t.Errorf("Content mismatch: got %s", got)
}
rc.Close()
nextF, err := sr.Next()
if err != nil || nextF.Name() != "next.txt" {
t.Errorf("Failed to advance to next file after Data Descriptor: %v", err)
}
}
func TestStreamReader_RoundTrip(t *testing.T) {
buf := new(bytes.Buffer)
archive := NewZip()
testFiles := map[string]string{
"hello.txt": "Hello World",
"dir/nested.dat": "Nested Data",
"images/logo.png": string(make([]byte, 5000)),
}
for name, content := range testFiles {
if _, err := archive.AddString(content, name); err != nil {
t.Fatalf("AddString failed: %v", err)
}
}
if _, err := archive.WriteTo(buf); err != nil {
t.Fatalf("WriteTo failed: %v", err)
}
sr := NewStreamReader(bytes.NewReader(buf.Bytes()))
filesFound := 0
for {
f, err := sr.Next()
if err == io.EOF {
break
}
if err != nil {
t.Fatalf("Next() failed: %v", err)
}
expectedContent, ok := testFiles[f.Name()]
if !ok {
t.Errorf("Unexpected file found: %s", f.Name())
continue
}
rc, err := sr.Open()
if err != nil {
t.Fatalf("Open() failed for %s: %v", f.Name(), err)
}
content, err := io.ReadAll(rc)
if err != nil {
t.Fatalf("ReadAll failed: %v", err)
}
rc.Close()
if string(content) != expectedContent {
t.Errorf("Content mismatch for %s", f.Name())
}
filesFound++
}
if filesFound != len(testFiles) {
t.Errorf("Expected %d files, found %d", len(testFiles), filesFound)
}
}
func TestStreamReader_DataDescriptors(t *testing.T) {
buf := new(bytes.Buffer)
dest := &writeOnlyBuffer{buf: buf}
archive := NewZip()
archive.AddString("compressed data string repeated repeated", "deflate.txt",
WithCompression(Deflate, DeflateNormal))
archive.AddString("stored data string", "store.txt",
WithCompression(Store, 0))
if _, err := archive.WriteTo(dest); err != nil {
t.Fatalf("WriteTo failed: %v", err)
}
sr := NewStreamReader(buf)
f1, err := sr.Next()
if err != nil {
t.Fatal(err)
}
if f1.Name() != "deflate.txt" {
t.Fatalf("Expected deflate.txt, got %s", f1.Name())
}
if f1.flags&0x8 == 0 {
t.Fatal("Bit 3 (Data Descriptor) should be set for streaming write")
}
rc1, _ := sr.Open()
data1, _ := io.ReadAll(rc1)
if string(data1) != "compressed data string repeated repeated" {
t.Error("Deflate content mismatch")
}
f2, err := sr.Next()
if err != nil {
t.Fatal(err)
}
if f2.Name() != "store.txt" {
t.Fatalf("Expected store.txt, got %s", f2.Name())
}
rc2, _ := sr.Open()
data2, _ := io.ReadAll(rc2)
if string(data2) != "stored data string" {
t.Error("Store content mismatch")
}
if _, err := sr.Next(); err != io.EOF {
t.Error("Expected EOF")
}
}
func TestStreamReader_SkipAndPartial(t *testing.T) {
buf := new(bytes.Buffer)
archive := NewZip()
archive.AddString("file1 content", "1.txt")
archive.AddString("file2 content is longer", "2.txt")
archive.AddString("file3 content", "3.txt")
archive.WriteTo(buf)
sr := NewStreamReader(buf)
f1, _ := sr.Next()
if f1.Name() != "1.txt" {
t.Fatal("Order mismatch")
}
rc, _ := sr.Open()
io.ReadAll(rc)
rc.Close()
f2, err := sr.Next()
if err != nil {
t.Fatal(err)
}
if f2.Name() != "2.txt" {
t.Fatal("Order mismatch 2")
}
f3, err := sr.Next()
if err != nil {
t.Fatalf("Failed to skip file 2: %v", err)
}
if f3.Name() != "3.txt" {
t.Fatal("Order mismatch 3")
}
rc3, _ := sr.Open()
p := make([]byte, 5)
rc3.Read(p)
_, err = sr.Next()
if err != io.EOF {
t.Errorf("Expected EOF, got %v", err)
}
}