...

Source file src/github.com/klauspost/compress/s2/fuzz_test.go

Documentation: github.com/klauspost/compress/s2

     1  //go:build go1.18
     2  // +build go1.18
     3  
     4  package s2
     5  
     6  import (
     7  	"bytes"
     8  	"fmt"
     9  	"io"
    10  	"testing"
    11  
    12  	"github.com/klauspost/compress/internal/fuzz"
    13  	"github.com/klauspost/compress/internal/snapref"
    14  )
    15  
    16  func FuzzEncodingBlocks(f *testing.F) {
    17  	fuzz.AddFromZip(f, "testdata/enc_regressions.zip", fuzz.TypeRaw, false)
    18  	fuzz.AddFromZip(f, "testdata/fuzz/block-corpus-raw.zip", fuzz.TypeRaw, testing.Short())
    19  	fuzz.AddFromZip(f, "testdata/fuzz/block-corpus-enc.zip", fuzz.TypeGoFuzz, testing.Short())
    20  
    21  	// Fuzzing tweaks:
    22  	const (
    23  		// Max input size:
    24  		maxSize = 8 << 20
    25  	)
    26  
    27  	f.Fuzz(func(t *testing.T, data []byte) {
    28  		if len(data) > maxSize {
    29  			return
    30  		}
    31  
    32  		writeDst := make([]byte, MaxEncodedLen(len(data)), MaxEncodedLen(len(data))+4)
    33  		writeDst = append(writeDst, 1, 2, 3, 4)
    34  		defer func() {
    35  			got := writeDst[MaxEncodedLen(len(data)):]
    36  			want := []byte{1, 2, 3, 4}
    37  			if !bytes.Equal(got, want) {
    38  				t.Fatalf("want %v, got %v - dest modified outside cap", want, got)
    39  			}
    40  		}()
    41  		compDst := writeDst[:MaxEncodedLen(len(data)):MaxEncodedLen(len(data))] // Hard cap
    42  		decDst := make([]byte, len(data))
    43  		comp := Encode(compDst, data)
    44  		decoded, err := Decode(decDst, comp)
    45  		if err != nil {
    46  			t.Error(err)
    47  			return
    48  		}
    49  		if !bytes.Equal(data, decoded) {
    50  			t.Error("block decoder mismatch")
    51  			return
    52  		}
    53  		if mel := MaxEncodedLen(len(data)); len(comp) > mel {
    54  			t.Error(fmt.Errorf("MaxEncodedLen Exceed: input: %d, mel: %d, got %d", len(data), mel, len(comp)))
    55  			return
    56  		}
    57  		comp = EncodeBetter(compDst, data)
    58  		decoded, err = Decode(decDst, comp)
    59  		if err != nil {
    60  			t.Error(err)
    61  			return
    62  		}
    63  		if !bytes.Equal(data, decoded) {
    64  			t.Error("block decoder mismatch")
    65  			return
    66  		}
    67  		if mel := MaxEncodedLen(len(data)); len(comp) > mel {
    68  			t.Error(fmt.Errorf("MaxEncodedLen Exceed: input: %d, mel: %d, got %d", len(data), mel, len(comp)))
    69  			return
    70  		}
    71  
    72  		comp = EncodeBest(compDst, data)
    73  		decoded, err = Decode(decDst, comp)
    74  		if err != nil {
    75  			t.Error(err)
    76  			return
    77  		}
    78  		if !bytes.Equal(data, decoded) {
    79  			t.Error("block decoder mismatch")
    80  			return
    81  		}
    82  		if mel := MaxEncodedLen(len(data)); len(comp) > mel {
    83  			t.Error(fmt.Errorf("MaxEncodedLen Exceed: input: %d, mel: %d, got %d", len(data), mel, len(comp)))
    84  			return
    85  		}
    86  
    87  		comp = EncodeSnappy(compDst, data)
    88  		decoded, err = snapref.Decode(decDst, comp)
    89  		if err != nil {
    90  			t.Error(err)
    91  			return
    92  		}
    93  		if !bytes.Equal(data, decoded) {
    94  			t.Error("block decoder mismatch")
    95  			return
    96  		}
    97  		if mel := MaxEncodedLen(len(data)); len(comp) > mel {
    98  			t.Error(fmt.Errorf("MaxEncodedLen Exceed: input: %d, mel: %d, got %d", len(data), mel, len(comp)))
    99  			return
   100  		}
   101  		comp = EncodeSnappyBetter(compDst, data)
   102  		decoded, err = snapref.Decode(decDst, comp)
   103  		if err != nil {
   104  			t.Error(err)
   105  			return
   106  		}
   107  		if !bytes.Equal(data, decoded) {
   108  			t.Error("block decoder mismatch")
   109  			return
   110  		}
   111  		if mel := MaxEncodedLen(len(data)); len(comp) > mel {
   112  			t.Error(fmt.Errorf("MaxEncodedLen Exceed: input: %d, mel: %d, got %d", len(data), mel, len(comp)))
   113  			return
   114  		}
   115  
   116  		comp = EncodeSnappyBest(compDst, data)
   117  		decoded, err = snapref.Decode(decDst, comp)
   118  		if err != nil {
   119  			t.Error(err)
   120  			return
   121  		}
   122  		if !bytes.Equal(data, decoded) {
   123  			t.Error("block decoder mismatch")
   124  			return
   125  		}
   126  		if mel := MaxEncodedLen(len(data)); len(comp) > mel {
   127  			t.Error(fmt.Errorf("MaxEncodedLen Exceed: input: %d, mel: %d, got %d", len(data), mel, len(comp)))
   128  			return
   129  		}
   130  
   131  		concat, err := ConcatBlocks(nil, data, []byte{0})
   132  		if err != nil || concat == nil {
   133  			return
   134  		}
   135  
   136  		EstimateBlockSize(data)
   137  		encoded := make([]byte, MaxEncodedLen(len(data)))
   138  		if len(encoded) < MaxEncodedLen(len(data)) || minNonLiteralBlockSize > len(data) || len(data) > maxBlockSize {
   139  			return
   140  		}
   141  
   142  		encodeBlockGo(encoded, data)
   143  		encodeBlockBetterGo(encoded, data)
   144  		encodeBlockSnappyGo(encoded, data)
   145  		encodeBlockBetterSnappyGo(encoded, data)
   146  		dst := encodeGo(encoded, data)
   147  		if dst == nil {
   148  			return
   149  		}
   150  	})
   151  }
   152  
   153  func FuzzStreamDecode(f *testing.F) {
   154  	enc := NewWriter(nil, WriterBlockSize(8<<10))
   155  	addCompressed := func(b []byte) {
   156  		var buf bytes.Buffer
   157  		enc.Reset(&buf)
   158  		enc.Write(b)
   159  		enc.Close()
   160  		f.Add(buf.Bytes())
   161  	}
   162  	fuzz.ReturnFromZip(f, "testdata/enc_regressions.zip", fuzz.TypeRaw, addCompressed)
   163  	fuzz.ReturnFromZip(f, "testdata/fuzz/block-corpus-raw.zip", fuzz.TypeRaw, addCompressed)
   164  	fuzz.ReturnFromZip(f, "testdata/fuzz/block-corpus-enc.zip", fuzz.TypeGoFuzz, addCompressed)
   165  	dec := NewReader(nil, ReaderIgnoreCRC())
   166  	f.Fuzz(func(t *testing.T, data []byte) {
   167  		// Using Read
   168  		dec.Reset(bytes.NewReader(data))
   169  		io.Copy(io.Discard, dec)
   170  
   171  		// Using DecodeConcurrent
   172  		dec.Reset(bytes.NewReader(data))
   173  		dec.DecodeConcurrent(io.Discard, 2)
   174  
   175  		// Use ByteReader.
   176  		dec.Reset(bytes.NewReader(data))
   177  		for {
   178  			_, err := dec.ReadByte()
   179  			if err != nil {
   180  				break
   181  			}
   182  		}
   183  	})
   184  }
   185  

View as plain text