| viktoriia-lsg | 0e1030c | 2023-10-04 10:11:56 | [diff] [blame] | 1 | package huff0 |
| 2 | |
| 3 | import ( |
| 4 | "bytes" |
| 5 | "fmt" |
| 6 | "testing" |
| 7 | |
| 8 | "github.com/klauspost/compress/internal/fuzz" |
| 9 | ) |
| 10 | |
| 11 | func FuzzCompress(f *testing.F) { |
| 12 | fuzz.AddFromZip(f, "testdata/fse_compress.zip", fuzz.TypeRaw, false) |
| 13 | f.Fuzz(func(t *testing.T, buf0 []byte) { |
| 14 | //use of Compress1X |
| 15 | var s Scratch |
| 16 | if len(buf0) > BlockSizeMax { |
| 17 | buf0 = buf0[:BlockSizeMax] |
| 18 | } |
| 19 | EstimateSizes(buf0, &s) |
| 20 | b, re, err := Compress1X(buf0, &s) |
| 21 | s.validateTable(s.cTable) |
| 22 | s.canUseTable(s.cTable) |
| 23 | if err != nil || b == nil { |
| 24 | return |
| 25 | } |
| 26 | |
| 27 | min := s.minSize(len(buf0)) |
| 28 | |
| 29 | if len(s.OutData) < min { |
| 30 | t.Errorf("FuzzCompress: output data length (%d) below shannon limit (%d)", len(s.OutData), min) |
| 31 | } |
| 32 | if len(s.OutTable) == 0 { |
| 33 | t.Error("FuzzCompress: got no table definition") |
| 34 | } |
| 35 | if re { |
| 36 | t.Error("FuzzCompress: claimed to have re-used.") |
| 37 | } |
| 38 | if len(s.OutData) == 0 { |
| 39 | t.Error("FuzzCompress: got no data output") |
| 40 | } |
| 41 | |
| 42 | dec, remain, err := ReadTable(b, nil) |
| 43 | |
| 44 | //use of Decompress1X |
| 45 | out, err := dec.Decompress1X(remain) |
| 46 | if err != nil || len(out) == 0 { |
| 47 | return |
| 48 | } |
| 49 | if !bytes.Equal(out, buf0) { |
| 50 | t.Fatal(fmt.Sprintln("FuzzCompressX1 output mismatch\n", len(out), "org: \n", len(buf0))) |
| 51 | } |
| 52 | |
| 53 | //use of Compress4X |
| 54 | s.Reuse = ReusePolicyAllow |
| 55 | b, reUsed, err := Compress4X(buf0, &s) |
| 56 | if err != nil || b == nil { |
| 57 | return |
| 58 | } |
| 59 | remain = b |
| 60 | if !reUsed { |
| 61 | dec, remain, err = ReadTable(b, dec) |
| 62 | if err != nil { |
| 63 | return |
| 64 | } |
| 65 | } |
| 66 | //use of Decompress4X |
| 67 | out, err = dec.Decompress4X(remain, len(buf0)) |
| 68 | if err != nil || out == nil { |
| 69 | return |
| 70 | } |
| 71 | if !bytes.Equal(out, buf0) { |
| 72 | t.Fatal(fmt.Sprintln("FuzzCompressX4 output mismatch: ", len(out), ", org: ", len(buf0))) |
| 73 | } |
| 74 | }) |
| 75 | } |
| 76 | |
| 77 | func FuzzDecompress1x(f *testing.F) { |
| 78 | fuzz.AddFromZip(f, "testdata/huff0_decompress1x.zip", fuzz.TypeRaw, false) |
| 79 | f.Fuzz(func(t *testing.T, buf0 []byte) { |
| 80 | var s Scratch |
| 81 | _, remain, err := ReadTable(buf0, &s) |
| 82 | if err != nil { |
| 83 | return |
| 84 | } |
| 85 | out, err := s.Decompress1X(remain) |
| 86 | if err != nil || out == nil { |
| 87 | return |
| 88 | } |
| 89 | }) |
| 90 | } |