MCPcopy
hub / github.com/klauspost/compress / TestNewReaderMismatch

Function TestNewReaderMismatch

zstd/decoder_test.go:33–140  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

31)
32
33func TestNewReaderMismatch(t *testing.T) {
34 // To identify a potential decoding error, do the following steps:
35 // 1) Place the compressed file in testdata, eg 'testdata/backup.bin.zst'
36 // 2) Decompress the file to using zstd, so it will be named 'testdata/backup.bin'
37 // 3) Run the test. A hash file will be generated 'testdata/backup.bin.hash'
38 // 4) The decoder will also run and decode the file. It will stop as soon as a mismatch is found.
39 // The hash file will be reused between runs if present.
40 const baseFile = "testdata/backup.bin"
41 const blockSize = 1024
42 hashes, err := os.ReadFile(baseFile + ".hash")
43 if os.IsNotExist(err) {
44 // Create the hash file.
45 f, err := os.Open(baseFile)
46 if os.IsNotExist(err) {
47 t.Skip("no decompressed file found")
48 return
49 }
50 defer f.Close()
51 br := bufio.NewReader(f)
52 var tmp [8]byte
53 xx := xxhash.New()
54 for {
55 xx.Reset()
56 buf := make([]byte, blockSize)
57 n, err := io.ReadFull(br, buf)
58 if err != nil {
59 if err != io.EOF && err != io.ErrUnexpectedEOF {
60 t.Fatal(err)
61 }
62 }
63 if n > 0 {
64 _, _ = xx.Write(buf[:n])
65 binary.LittleEndian.PutUint64(tmp[:], xx.Sum64())
66 hashes = append(hashes, tmp[4:]...)
67 }
68 if n != blockSize {
69 break
70 }
71 }
72 err = os.WriteFile(baseFile+".hash", hashes, os.ModePerm)
73 if err != nil {
74 // We can continue for now
75 t.Error(err)
76 }
77 t.Log("Saved", len(hashes)/4, "hashes as", baseFile+".hash")
78 }
79
80 f, err := os.Open(baseFile + ".zst")
81 if os.IsNotExist(err) {
82 t.Skip("no compressed file found")
83 return
84 }
85 defer f.Close()
86 dec, err := NewReader(f, WithDecoderConcurrency(1))
87 if err != nil {
88 t.Fatal(err)
89 }
90 defer dec.Close()

Callers

nothing calls this directly

Calls 14

CloseMethod · 0.95
NewFunction · 0.92
WithDecoderConcurrencyFunction · 0.85
SkipMethod · 0.80
FatalMethod · 0.80
Sum64Method · 0.80
CopyMethod · 0.80
NewReaderFunction · 0.70
CloseMethod · 0.65
ResetMethod · 0.65
WriteMethod · 0.65
OpenMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…