1
2
3
4
5
6
7 package zip
8
9 import (
10 "bytes"
11 "cmp"
12 "errors"
13 "fmt"
14 "hash"
15 "internal/testenv"
16 "io"
17 "runtime"
18 "slices"
19 "strings"
20 "testing"
21 "time"
22 )
23
24 func TestOver65kFiles(t *testing.T) {
25 if testing.Short() && testenv.Builder() == "" {
26 t.Skip("skipping in short mode")
27 }
28 buf := new(strings.Builder)
29 w := NewWriter(buf)
30 const nFiles = (1 << 16) + 42
31 for i := 0; i < nFiles; i++ {
32 _, err := w.CreateHeader(&FileHeader{
33 Name: fmt.Sprintf("%d.dat", i),
34 Method: Store,
35 })
36 if err != nil {
37 t.Fatalf("creating file %d: %v", i, err)
38 }
39 }
40 if err := w.Close(); err != nil {
41 t.Fatalf("Writer.Close: %v", err)
42 }
43 s := buf.String()
44 zr, err := NewReader(strings.NewReader(s), int64(len(s)))
45 if err != nil {
46 t.Fatalf("NewReader: %v", err)
47 }
48 if got := len(zr.File); got != nFiles {
49 t.Fatalf("File contains %d files, want %d", got, nFiles)
50 }
51 for i := 0; i < nFiles; i++ {
52 want := fmt.Sprintf("%d.dat", i)
53 if zr.File[i].Name != want {
54 t.Fatalf("File(%d) = %q, want %q", i, zr.File[i].Name, want)
55 }
56 }
57 }
58
59 func TestModTime(t *testing.T) {
60 var testTime = time.Date(2009, time.November, 10, 23, 45, 58, 0, time.UTC)
61 fh := new(FileHeader)
62 fh.SetModTime(testTime)
63 outTime := fh.ModTime()
64 if !outTime.Equal(testTime) {
65 t.Errorf("times don't match: got %s, want %s", outTime, testTime)
66 }
67 }
68
69 func testHeaderRoundTrip(fh *FileHeader, wantUncompressedSize uint32, wantUncompressedSize64 uint64, t *testing.T) {
70 fi := fh.FileInfo()
71 fh2, err := FileInfoHeader(fi)
72 if err != nil {
73 t.Fatal(err)
74 }
75 if got, want := fh2.Name, fh.Name; got != want {
76 t.Errorf("Name: got %s, want %s\n", got, want)
77 }
78 if got, want := fh2.UncompressedSize, wantUncompressedSize; got != want {
79 t.Errorf("UncompressedSize: got %d, want %d\n", got, want)
80 }
81 if got, want := fh2.UncompressedSize64, wantUncompressedSize64; got != want {
82 t.Errorf("UncompressedSize64: got %d, want %d\n", got, want)
83 }
84 if got, want := fh2.ModifiedTime, fh.ModifiedTime; got != want {
85 t.Errorf("ModifiedTime: got %d, want %d\n", got, want)
86 }
87 if got, want := fh2.ModifiedDate, fh.ModifiedDate; got != want {
88 t.Errorf("ModifiedDate: got %d, want %d\n", got, want)
89 }
90
91 if sysfh, ok := fi.Sys().(*FileHeader); !ok && sysfh != fh {
92 t.Errorf("Sys didn't return original *FileHeader")
93 }
94 }
95
96 func TestFileHeaderRoundTrip(t *testing.T) {
97 fh := &FileHeader{
98 Name: "foo.txt",
99 UncompressedSize: 987654321,
100 ModifiedTime: 1234,
101 ModifiedDate: 5678,
102 }
103 testHeaderRoundTrip(fh, fh.UncompressedSize, uint64(fh.UncompressedSize), t)
104 }
105
106 func TestFileHeaderRoundTrip64(t *testing.T) {
107 fh := &FileHeader{
108 Name: "foo.txt",
109 UncompressedSize64: 9876543210,
110 ModifiedTime: 1234,
111 ModifiedDate: 5678,
112 }
113 testHeaderRoundTrip(fh, uint32max, fh.UncompressedSize64, t)
114 }
115
116 func TestFileHeaderRoundTripModified(t *testing.T) {
117 fh := &FileHeader{
118 Name: "foo.txt",
119 UncompressedSize: 987654321,
120 Modified: time.Now().Local(),
121 ModifiedTime: 1234,
122 ModifiedDate: 5678,
123 }
124 fi := fh.FileInfo()
125 fh2, err := FileInfoHeader(fi)
126 if err != nil {
127 t.Fatal(err)
128 }
129 if got, want := fh2.Modified, fh.Modified.UTC(); got != want {
130 t.Errorf("Modified: got %s, want %s\n", got, want)
131 }
132 if got, want := fi.ModTime(), fh.Modified.UTC(); got != want {
133 t.Errorf("Modified: got %s, want %s\n", got, want)
134 }
135 }
136
137 func TestFileHeaderRoundTripWithoutModified(t *testing.T) {
138 fh := &FileHeader{
139 Name: "foo.txt",
140 UncompressedSize: 987654321,
141 ModifiedTime: 1234,
142 ModifiedDate: 5678,
143 }
144 fi := fh.FileInfo()
145 fh2, err := FileInfoHeader(fi)
146 if err != nil {
147 t.Fatal(err)
148 }
149 if got, want := fh2.ModTime(), fh.ModTime(); got != want {
150 t.Errorf("Modified: got %s, want %s\n", got, want)
151 }
152 if got, want := fi.ModTime(), fh.ModTime(); got != want {
153 t.Errorf("Modified: got %s, want %s\n", got, want)
154 }
155 }
156
157 type repeatedByte struct {
158 off int64
159 b byte
160 n int64
161 }
162
163
164
165
166 type rleBuffer struct {
167 buf []repeatedByte
168 }
169
170 func (r *rleBuffer) Size() int64 {
171 if len(r.buf) == 0 {
172 return 0
173 }
174 last := &r.buf[len(r.buf)-1]
175 return last.off + last.n
176 }
177
178 func (r *rleBuffer) Write(p []byte) (n int, err error) {
179 var rp *repeatedByte
180 if len(r.buf) > 0 {
181 rp = &r.buf[len(r.buf)-1]
182
183 if lastByte := rp.b; len(p) > 0 && p[0] == lastByte {
184 if bytes.Count(p, []byte{lastByte}) == len(p) {
185 rp.n += int64(len(p))
186 return len(p), nil
187 }
188 }
189 }
190
191 for _, b := range p {
192 if rp == nil || rp.b != b {
193 r.buf = append(r.buf, repeatedByte{r.Size(), b, 1})
194 rp = &r.buf[len(r.buf)-1]
195 } else {
196 rp.n++
197 }
198 }
199 return len(p), nil
200 }
201
202 func memset(a []byte, b byte) {
203 if len(a) == 0 {
204 return
205 }
206
207
208 a[0] = b
209 for i, l := 1, len(a); i < l; i *= 2 {
210 copy(a[i:], a[:i])
211 }
212 }
213
214 func (r *rleBuffer) ReadAt(p []byte, off int64) (n int, err error) {
215 if len(p) == 0 {
216 return
217 }
218 skipParts, _ := slices.BinarySearchFunc(r.buf, off, func(rb repeatedByte, off int64) int {
219 return cmp.Compare(rb.off+rb.n, off)
220 })
221 parts := r.buf[skipParts:]
222 if len(parts) > 0 {
223 skipBytes := off - parts[0].off
224 for _, part := range parts {
225 repeat := int(min(part.n-skipBytes, int64(len(p)-n)))
226 memset(p[n:n+repeat], part.b)
227 n += repeat
228 if n == len(p) {
229 return
230 }
231 skipBytes = 0
232 }
233 }
234 if n != len(p) {
235 err = io.ErrUnexpectedEOF
236 }
237 return
238 }
239
240
241 func TestRLEBuffer(t *testing.T) {
242 b := new(rleBuffer)
243 var all []byte
244 writes := []string{"abcdeee", "eeeeeee", "eeeefghaaiii"}
245 for _, w := range writes {
246 b.Write([]byte(w))
247 all = append(all, w...)
248 }
249 if len(b.buf) != 10 {
250 t.Fatalf("len(b.buf) = %d; want 10", len(b.buf))
251 }
252
253 for i := 0; i < len(all); i++ {
254 for j := 0; j < len(all)-i; j++ {
255 buf := make([]byte, j)
256 n, err := b.ReadAt(buf, int64(i))
257 if err != nil || n != len(buf) {
258 t.Errorf("ReadAt(%d, %d) = %d, %v; want %d, nil", i, j, n, err, len(buf))
259 }
260 if !bytes.Equal(buf, all[i:i+j]) {
261 t.Errorf("ReadAt(%d, %d) = %q; want %q", i, j, buf, all[i:i+j])
262 }
263 }
264 }
265 }
266
267
268 type fakeHash32 struct {
269 hash.Hash32
270 }
271
272 func (fakeHash32) Write(p []byte) (int, error) { return len(p), nil }
273 func (fakeHash32) Sum32() uint32 { return 0 }
274
275 func TestZip64(t *testing.T) {
276 if testing.Short() {
277 t.Skip("slow test; skipping")
278 }
279 t.Parallel()
280 const size = 1 << 32
281 buf := testZip64(t, size)
282 testZip64DirectoryRecordLength(buf, t)
283 }
284
285 func TestZip64EdgeCase(t *testing.T) {
286 if testing.Short() {
287 t.Skip("slow test; skipping")
288 }
289 t.Parallel()
290
291
292
293
294
295 const size = 1<<32 - 1 - int64(len("END\n"))
296 buf := testZip64(t, size)
297 testZip64DirectoryRecordLength(buf, t)
298 }
299
300
301
302 func TestZip64DirectoryOffset(t *testing.T) {
303 if testing.Short() {
304 t.Skip("skipping in short mode")
305 }
306 t.Parallel()
307 const filename = "huge.txt"
308 gen := func(wantOff uint64) func(*Writer) {
309 return func(w *Writer) {
310 w.testHookCloseSizeOffset = func(size, off uint64) {
311 if off != wantOff {
312 t.Errorf("central directory offset = %d (%x); want %d", off, off, wantOff)
313 }
314 }
315 f, err := w.CreateHeader(&FileHeader{
316 Name: filename,
317 Method: Store,
318 })
319 if err != nil {
320 t.Fatal(err)
321 }
322 f.(*fileWriter).crc32 = fakeHash32{}
323 size := wantOff - fileHeaderLen - uint64(len(filename)) - dataDescriptorLen
324 if _, err := io.CopyN(f, zeros{}, int64(size)); err != nil {
325 t.Fatal(err)
326 }
327 if err := w.Close(); err != nil {
328 t.Fatal(err)
329 }
330 }
331 }
332 t.Run("uint32max-2_NoZip64", func(t *testing.T) {
333 t.Parallel()
334 if generatesZip64(t, gen(0xfffffffe)) {
335 t.Error("unexpected zip64")
336 }
337 })
338 t.Run("uint32max-1_Zip64", func(t *testing.T) {
339 t.Parallel()
340 if !generatesZip64(t, gen(0xffffffff)) {
341 t.Error("expected zip64")
342 }
343 })
344 }
345
346
347 func TestZip64ManyRecords(t *testing.T) {
348 if testing.Short() {
349 t.Skip("skipping in short mode")
350 }
351 t.Parallel()
352 gen := func(numRec int) func(*Writer) {
353 return func(w *Writer) {
354 for i := 0; i < numRec; i++ {
355 _, err := w.CreateHeader(&FileHeader{
356 Name: "a.txt",
357 Method: Store,
358 })
359 if err != nil {
360 t.Fatal(err)
361 }
362 }
363 if err := w.Close(); err != nil {
364 t.Fatal(err)
365 }
366 }
367 }
368
369 t.Run("uint16max-1_NoZip64", func(t *testing.T) {
370 t.Parallel()
371 if generatesZip64(t, gen(0xfffe)) {
372 t.Error("unexpected zip64")
373 }
374 })
375
376 t.Run("uint16max_Zip64", func(t *testing.T) {
377 t.Parallel()
378 if !generatesZip64(t, gen(0xffff)) {
379 t.Error("expected zip64")
380 }
381 })
382 }
383
384
385
386
387 type suffixSaver struct {
388 keep int
389 buf []byte
390 start int
391 size int64
392 }
393
394 func (ss *suffixSaver) Size() int64 { return ss.size }
395
396 var errDiscardedBytes = errors.New("ReadAt of discarded bytes")
397
398 func (ss *suffixSaver) ReadAt(p []byte, off int64) (n int, err error) {
399 back := ss.size - off
400 if back > int64(ss.keep) {
401 return 0, errDiscardedBytes
402 }
403 suf := ss.Suffix()
404 n = copy(p, suf[len(suf)-int(back):])
405 if n != len(p) {
406 err = io.EOF
407 }
408 return
409 }
410
411 func (ss *suffixSaver) Suffix() []byte {
412 if len(ss.buf) < ss.keep {
413 return ss.buf
414 }
415 buf := make([]byte, ss.keep)
416 n := copy(buf, ss.buf[ss.start:])
417 copy(buf[n:], ss.buf[:])
418 return buf
419 }
420
421 func (ss *suffixSaver) Write(p []byte) (n int, err error) {
422 n = len(p)
423 ss.size += int64(len(p))
424 if len(ss.buf) < ss.keep {
425 space := ss.keep - len(ss.buf)
426 add := len(p)
427 if add > space {
428 add = space
429 }
430 ss.buf = append(ss.buf, p[:add]...)
431 p = p[add:]
432 }
433 for len(p) > 0 {
434 n := copy(ss.buf[ss.start:], p)
435 p = p[n:]
436 ss.start += n
437 if ss.start == ss.keep {
438 ss.start = 0
439 }
440 }
441 return
442 }
443
444
445
446 func generatesZip64(t *testing.T, f func(w *Writer)) bool {
447 ss := &suffixSaver{keep: 10 << 20}
448 w := NewWriter(ss)
449 f(w)
450 return suffixIsZip64(t, ss)
451 }
452
453 type sizedReaderAt interface {
454 io.ReaderAt
455 Size() int64
456 }
457
458 func suffixIsZip64(t *testing.T, zip sizedReaderAt) bool {
459 d := make([]byte, 1024)
460 if _, err := zip.ReadAt(d, zip.Size()-int64(len(d))); err != nil {
461 t.Fatalf("ReadAt: %v", err)
462 }
463
464 sigOff := findSignatureInBlock(d)
465 if sigOff == -1 {
466 t.Errorf("failed to find signature in block")
467 return false
468 }
469
470 dirOff, err := findDirectory64End(zip, zip.Size()-int64(len(d))+int64(sigOff))
471 if err != nil {
472 t.Fatalf("findDirectory64End: %v", err)
473 }
474 if dirOff == -1 {
475 return false
476 }
477
478 d = make([]byte, directory64EndLen)
479 if _, err := zip.ReadAt(d, dirOff); err != nil {
480 t.Fatalf("ReadAt(off=%d): %v", dirOff, err)
481 }
482
483 b := readBuf(d)
484 if sig := b.uint32(); sig != directory64EndSignature {
485 return false
486 }
487
488 size := b.uint64()
489 if size != directory64EndLen-12 {
490 t.Errorf("expected length of %d, got %d", directory64EndLen-12, size)
491 }
492 return true
493 }
494
495
496 func TestZip64LargeDirectory(t *testing.T) {
497 if runtime.GOARCH == "wasm" {
498 t.Skip("too slow on wasm")
499 }
500 if testing.Short() {
501 t.Skip("skipping in short mode")
502 }
503 t.Parallel()
504
505
506 gen := func(wantLen int64) func(*Writer) {
507 return func(w *Writer) {
508 w.testHookCloseSizeOffset = func(size, off uint64) {
509 if size != uint64(wantLen) {
510 t.Errorf("Close central directory size = %d; want %d", size, wantLen)
511 }
512 }
513
514 uint16string := strings.Repeat(".", uint16max)
515 remain := wantLen
516 for remain > 0 {
517 commentLen := int(uint16max) - directoryHeaderLen - 1
518 thisRecLen := directoryHeaderLen + int(uint16max) + commentLen
519 if int64(thisRecLen) > remain {
520 remove := thisRecLen - int(remain)
521 commentLen -= remove
522 thisRecLen -= remove
523 }
524 remain -= int64(thisRecLen)
525 f, err := w.CreateHeader(&FileHeader{
526 Name: uint16string,
527 Comment: uint16string[:commentLen],
528 })
529 if err != nil {
530 t.Fatalf("CreateHeader: %v", err)
531 }
532 f.(*fileWriter).crc32 = fakeHash32{}
533 }
534 if err := w.Close(); err != nil {
535 t.Fatalf("Close: %v", err)
536 }
537 }
538 }
539 t.Run("uint32max-1_NoZip64", func(t *testing.T) {
540 t.Parallel()
541 if generatesZip64(t, gen(uint32max-1)) {
542 t.Error("unexpected zip64")
543 }
544 })
545 t.Run("uint32max_HasZip64", func(t *testing.T) {
546 t.Parallel()
547 if !generatesZip64(t, gen(uint32max)) {
548 t.Error("expected zip64")
549 }
550 })
551 }
552
553 func testZip64(t testing.TB, size int64) *rleBuffer {
554 const chunkSize = 1024
555 chunks := int(size / chunkSize)
556
557 buf := new(rleBuffer)
558 w := NewWriter(buf)
559 f, err := w.CreateHeader(&FileHeader{
560 Name: "huge.txt",
561 Method: Store,
562 })
563 if err != nil {
564 t.Fatal(err)
565 }
566 f.(*fileWriter).crc32 = fakeHash32{}
567 chunk := make([]byte, chunkSize)
568 for i := range chunk {
569 chunk[i] = '.'
570 }
571 for i := 0; i < chunks; i++ {
572 _, err := f.Write(chunk)
573 if err != nil {
574 t.Fatal("write chunk:", err)
575 }
576 }
577 if frag := int(size % chunkSize); frag > 0 {
578 _, err := f.Write(chunk[:frag])
579 if err != nil {
580 t.Fatal("write chunk:", err)
581 }
582 }
583 end := []byte("END\n")
584 _, err = f.Write(end)
585 if err != nil {
586 t.Fatal("write end:", err)
587 }
588 if err := w.Close(); err != nil {
589 t.Fatal(err)
590 }
591
592
593 r, err := NewReader(buf, buf.Size())
594 if err != nil {
595 t.Fatal("reader:", err)
596 }
597 f0 := r.File[0]
598 rc, err := f0.Open()
599 if err != nil {
600 t.Fatal("opening:", err)
601 }
602 rc.(*checksumReader).hash = fakeHash32{}
603 for i := 0; i < chunks; i++ {
604 _, err := io.ReadFull(rc, chunk)
605 if err != nil {
606 t.Fatal("read:", err)
607 }
608 }
609 if frag := int(size % chunkSize); frag > 0 {
610 _, err := io.ReadFull(rc, chunk[:frag])
611 if err != nil {
612 t.Fatal("read:", err)
613 }
614 }
615 gotEnd, err := io.ReadAll(rc)
616 if err != nil {
617 t.Fatal("read end:", err)
618 }
619 if !bytes.Equal(gotEnd, end) {
620 t.Errorf("End of zip64 archive %q, want %q", gotEnd, end)
621 }
622 err = rc.Close()
623 if err != nil {
624 t.Fatal("closing:", err)
625 }
626 if size+int64(len("END\n")) >= 1<<32-1 {
627 if got, want := f0.UncompressedSize, uint32(uint32max); got != want {
628 t.Errorf("UncompressedSize %#x, want %#x", got, want)
629 }
630 }
631
632 if got, want := f0.UncompressedSize64, uint64(size)+uint64(len(end)); got != want {
633 t.Errorf("UncompressedSize64 %#x, want %#x", got, want)
634 }
635
636 return buf
637 }
638
639
640 func testZip64DirectoryRecordLength(buf *rleBuffer, t *testing.T) {
641 if !suffixIsZip64(t, buf) {
642 t.Fatal("not a zip64")
643 }
644 }
645
646 func testValidHeader(h *FileHeader, t *testing.T) {
647 var buf bytes.Buffer
648 z := NewWriter(&buf)
649
650 f, err := z.CreateHeader(h)
651 if err != nil {
652 t.Fatalf("error creating header: %v", err)
653 }
654 if _, err := f.Write([]byte("hi")); err != nil {
655 t.Fatalf("error writing content: %v", err)
656 }
657 if err := z.Close(); err != nil {
658 t.Fatalf("error closing zip writer: %v", err)
659 }
660
661 b := buf.Bytes()
662 zf, err := NewReader(bytes.NewReader(b), int64(len(b)))
663 if err != nil {
664 t.Fatalf("got %v, expected nil", err)
665 }
666 zh := zf.File[0].FileHeader
667 if zh.Name != h.Name || zh.Method != h.Method || zh.UncompressedSize64 != uint64(len("hi")) {
668 t.Fatalf("got %q/%d/%d expected %q/%d/%d", zh.Name, zh.Method, zh.UncompressedSize64, h.Name, h.Method, len("hi"))
669 }
670 }
671
672
673 func TestHeaderInvalidTagAndSize(t *testing.T) {
674 const timeFormat = "20060102T150405.000.txt"
675
676 ts := time.Now()
677 filename := ts.Format(timeFormat)
678
679 h := FileHeader{
680 Name: filename,
681 Method: Deflate,
682 Extra: []byte(ts.Format(time.RFC3339Nano)),
683 }
684 h.SetModTime(ts)
685
686 testValidHeader(&h, t)
687 }
688
689 func TestHeaderTooShort(t *testing.T) {
690 h := FileHeader{
691 Name: "foo.txt",
692 Method: Deflate,
693 Extra: []byte{zip64ExtraID},
694 }
695 testValidHeader(&h, t)
696 }
697
698 func TestHeaderTooLongErr(t *testing.T) {
699 var headerTests = []struct {
700 name string
701 extra []byte
702 wanterr error
703 }{
704 {
705 name: strings.Repeat("x", 1<<16),
706 extra: []byte{},
707 wanterr: errLongName,
708 },
709 {
710 name: "long_extra",
711 extra: bytes.Repeat([]byte{0xff}, 1<<16),
712 wanterr: errLongExtra,
713 },
714 }
715
716
717 buf := new(bytes.Buffer)
718 w := NewWriter(buf)
719
720 for _, test := range headerTests {
721 h := &FileHeader{
722 Name: test.name,
723 Extra: test.extra,
724 }
725 _, err := w.CreateHeader(h)
726 if err != test.wanterr {
727 t.Errorf("error=%v, want %v", err, test.wanterr)
728 }
729 }
730
731 if err := w.Close(); err != nil {
732 t.Fatal(err)
733 }
734 }
735
736 func TestHeaderIgnoredSize(t *testing.T) {
737 h := FileHeader{
738 Name: "foo.txt",
739 Method: Deflate,
740 Extra: []byte{zip64ExtraID & 0xFF, zip64ExtraID >> 8, 24, 0, 1, 2, 3, 4, 5, 6, 7, 8, 1, 2, 3, 4, 5, 6, 7, 8, 1, 2, 3, 4, 5, 6, 7, 8, 1, 2, 3, 4, 5, 6, 7, 8},
741 }
742 testValidHeader(&h, t)
743 }
744
745
746
747 func TestZeroLengthHeader(t *testing.T) {
748 h := FileHeader{
749 Name: "extadata.txt",
750 Method: Deflate,
751 Extra: []byte{
752 85, 84, 5, 0, 3, 154, 144, 195, 77,
753 85, 120, 0, 0,
754 },
755 }
756 testValidHeader(&h, t)
757 }
758
759
760
761 func BenchmarkZip64Test(b *testing.B) {
762 for i := 0; i < b.N; i++ {
763 testZip64(b, 1<<26)
764 }
765 }
766
767 func BenchmarkZip64TestSizes(b *testing.B) {
768 for _, size := range []int64{1 << 12, 1 << 20, 1 << 26} {
769 b.Run(fmt.Sprint(size), func(b *testing.B) {
770 b.RunParallel(func(pb *testing.PB) {
771 for pb.Next() {
772 testZip64(b, size)
773 }
774 })
775 })
776 }
777 }
778
779 func TestSuffixSaver(t *testing.T) {
780 const keep = 10
781 ss := &suffixSaver{keep: keep}
782 ss.Write([]byte("abc"))
783 if got := string(ss.Suffix()); got != "abc" {
784 t.Errorf("got = %q; want abc", got)
785 }
786 ss.Write([]byte("defghijklmno"))
787 if got := string(ss.Suffix()); got != "fghijklmno" {
788 t.Errorf("got = %q; want fghijklmno", got)
789 }
790 if got, want := ss.Size(), int64(len("abc")+len("defghijklmno")); got != want {
791 t.Errorf("Size = %d; want %d", got, want)
792 }
793 buf := make([]byte, ss.Size())
794 for off := int64(0); off < ss.Size(); off++ {
795 for size := 1; size <= int(ss.Size()-off); size++ {
796 readBuf := buf[:size]
797 n, err := ss.ReadAt(readBuf, off)
798 if off < ss.Size()-keep {
799 if err != errDiscardedBytes {
800 t.Errorf("off %d, size %d = %v, %v (%q); want errDiscardedBytes", off, size, n, err, readBuf[:n])
801 }
802 continue
803 }
804 want := "abcdefghijklmno"[off : off+int64(size)]
805 got := string(readBuf[:n])
806 if err != nil || got != want {
807 t.Errorf("off %d, size %d = %v, %v (%q); want %q", off, size, n, err, got, want)
808 }
809 }
810 }
811
812 }
813
814 type zeros struct{}
815
816 func (zeros) Read(p []byte) (int, error) {
817 clear(p)
818 return len(p), nil
819 }
820
View as plain text