(t *testing.T)
| 31 | ) |
| 32 | |
| 33 | func 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() |
nothing calls this directly
no test coverage detected
searching dependent graphs…