|
| 1 | +//go:build go1.18 |
| 2 | +// +build go1.18 |
| 3 | + |
| 4 | +package s2 |
| 5 | + |
| 6 | +import ( |
| 7 | + "bytes" |
| 8 | + "fmt" |
| 9 | + "testing" |
| 10 | + |
| 11 | + "github.com/klauspost/compress/internal/fuzz" |
| 12 | + "github.com/klauspost/compress/internal/snapref" |
| 13 | +) |
| 14 | + |
| 15 | +func FuzzEncodingBlocks(f *testing.F) { |
| 16 | + fuzz.AddFromZip(f, "testdata/enc_regressions.zip", true, false) |
| 17 | + fuzz.AddFromZip(f, "testdata/fuzz/block-corpus-raw.zip", true, testing.Short()) |
| 18 | + fuzz.AddFromZip(f, "testdata/fuzz/block-corpus-enc.zip", false, testing.Short()) |
| 19 | + |
| 20 | + // Fuzzing tweaks: |
| 21 | + const ( |
| 22 | + // Max input size: |
| 23 | + maxSize = 8 << 20 |
| 24 | + ) |
| 25 | + |
| 26 | + f.Fuzz(func(t *testing.T, data []byte) { |
| 27 | + if len(data) > maxSize { |
| 28 | + return |
| 29 | + } |
| 30 | + |
| 31 | + writeDst := make([]byte, MaxEncodedLen(len(data)), MaxEncodedLen(len(data))+4) |
| 32 | + writeDst = append(writeDst, 1, 2, 3, 4) |
| 33 | + defer func() { |
| 34 | + got := writeDst[MaxEncodedLen(len(data)):] |
| 35 | + want := []byte{1, 2, 3, 4} |
| 36 | + if !bytes.Equal(got, want) { |
| 37 | + t.Fatalf("want %v, got %v - dest modified outside cap", want, got) |
| 38 | + } |
| 39 | + }() |
| 40 | + compDst := writeDst[:MaxEncodedLen(len(data)):MaxEncodedLen(len(data))] // Hard cap |
| 41 | + decDst := make([]byte, len(data)) |
| 42 | + comp := Encode(compDst, data) |
| 43 | + decoded, err := Decode(decDst, comp) |
| 44 | + if err != nil { |
| 45 | + t.Error(err) |
| 46 | + return |
| 47 | + } |
| 48 | + if !bytes.Equal(data, decoded) { |
| 49 | + t.Error("block decoder mismatch") |
| 50 | + return |
| 51 | + } |
| 52 | + if mel := MaxEncodedLen(len(data)); len(comp) > mel { |
| 53 | + t.Error(fmt.Errorf("MaxEncodedLen Exceed: input: %d, mel: %d, got %d", len(data), mel, len(comp))) |
| 54 | + return |
| 55 | + } |
| 56 | + comp = EncodeBetter(compDst, data) |
| 57 | + decoded, err = Decode(decDst, comp) |
| 58 | + if err != nil { |
| 59 | + t.Error(err) |
| 60 | + return |
| 61 | + } |
| 62 | + if !bytes.Equal(data, decoded) { |
| 63 | + t.Error("block decoder mismatch") |
| 64 | + return |
| 65 | + } |
| 66 | + if mel := MaxEncodedLen(len(data)); len(comp) > mel { |
| 67 | + t.Error(fmt.Errorf("MaxEncodedLen Exceed: input: %d, mel: %d, got %d", len(data), mel, len(comp))) |
| 68 | + return |
| 69 | + } |
| 70 | + |
| 71 | + comp = EncodeBest(compDst, data) |
| 72 | + decoded, err = Decode(decDst, comp) |
| 73 | + if err != nil { |
| 74 | + t.Error(err) |
| 75 | + return |
| 76 | + } |
| 77 | + if !bytes.Equal(data, decoded) { |
| 78 | + t.Error("block decoder mismatch") |
| 79 | + return |
| 80 | + } |
| 81 | + if mel := MaxEncodedLen(len(data)); len(comp) > mel { |
| 82 | + t.Error(fmt.Errorf("MaxEncodedLen Exceed: input: %d, mel: %d, got %d", len(data), mel, len(comp))) |
| 83 | + return |
| 84 | + } |
| 85 | + |
| 86 | + comp = EncodeSnappy(compDst, data) |
| 87 | + decoded, err = snapref.Decode(decDst, comp) |
| 88 | + if err != nil { |
| 89 | + t.Error(err) |
| 90 | + return |
| 91 | + } |
| 92 | + if !bytes.Equal(data, decoded) { |
| 93 | + t.Error("block decoder mismatch") |
| 94 | + return |
| 95 | + } |
| 96 | + if mel := MaxEncodedLen(len(data)); len(comp) > mel { |
| 97 | + t.Error(fmt.Errorf("MaxEncodedLen Exceed: input: %d, mel: %d, got %d", len(data), mel, len(comp))) |
| 98 | + return |
| 99 | + } |
| 100 | + comp = EncodeSnappyBetter(compDst, data) |
| 101 | + decoded, err = snapref.Decode(decDst, comp) |
| 102 | + if err != nil { |
| 103 | + t.Error(err) |
| 104 | + return |
| 105 | + } |
| 106 | + if !bytes.Equal(data, decoded) { |
| 107 | + t.Error("block decoder mismatch") |
| 108 | + return |
| 109 | + } |
| 110 | + if mel := MaxEncodedLen(len(data)); len(comp) > mel { |
| 111 | + t.Error(fmt.Errorf("MaxEncodedLen Exceed: input: %d, mel: %d, got %d", len(data), mel, len(comp))) |
| 112 | + return |
| 113 | + } |
| 114 | + |
| 115 | + comp = EncodeSnappyBest(compDst, data) |
| 116 | + decoded, err = snapref.Decode(decDst, comp) |
| 117 | + if err != nil { |
| 118 | + t.Error(err) |
| 119 | + return |
| 120 | + } |
| 121 | + if !bytes.Equal(data, decoded) { |
| 122 | + t.Error("block decoder mismatch") |
| 123 | + return |
| 124 | + } |
| 125 | + if mel := MaxEncodedLen(len(data)); len(comp) > mel { |
| 126 | + t.Error(fmt.Errorf("MaxEncodedLen Exceed: input: %d, mel: %d, got %d", len(data), mel, len(comp))) |
| 127 | + return |
| 128 | + } |
| 129 | + }) |
| 130 | +} |
0 commit comments