Source file src/compress/gzip/fuzz_test.go

     1  // Copyright 2021 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package gzip
     6  
     7  import (
     8  	"bytes"
     9  	"encoding/base64"
    10  	"io"
    11  	"os"
    12  	"path/filepath"
    13  	"strings"
    14  	"testing"
    15  )
    16  
    17  func FuzzReader(f *testing.F) {
    18  	inp := []byte("Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.")
    19  	for _, level := range []int{BestSpeed, BestCompression, DefaultCompression, HuffmanOnly} {
    20  		b := bytes.NewBuffer(nil)
    21  		w, err := NewWriterLevel(b, level)
    22  		if err != nil {
    23  			f.Fatalf("failed to construct writer: %s", err)
    24  		}
    25  		_, err = w.Write(inp)
    26  		if err != nil {
    27  			f.Fatalf("failed to write: %s", err)
    28  		}
    29  		f.Add(b.Bytes())
    30  	}
    31  
    32  	testdata, err := os.ReadDir("testdata")
    33  	if err != nil {
    34  		f.Fatalf("failed to read testdata directory: %s", err)
    35  	}
    36  	for _, de := range testdata {
    37  		if de.IsDir() {
    38  			continue
    39  		}
    40  		b, err := os.ReadFile(filepath.Join("testdata", de.Name()))
    41  		if err != nil {
    42  			f.Fatalf("failed to read testdata: %s", err)
    43  		}
    44  
    45  		// decode any base64 encoded test files
    46  		if strings.HasPrefix(de.Name(), ".base64") {
    47  			b, err = base64.StdEncoding.DecodeString(string(b))
    48  			if err != nil {
    49  				f.Fatalf("failed to decode base64 testdata: %s", err)
    50  			}
    51  		}
    52  
    53  		f.Add(b)
    54  	}
    55  
    56  	f.Fuzz(func(t *testing.T, b []byte) {
    57  		for _, multistream := range []bool{true, false} {
    58  			r, err := NewReader(bytes.NewBuffer(b))
    59  			if err != nil {
    60  				continue
    61  			}
    62  
    63  			r.Multistream(multistream)
    64  
    65  			decompressed := bytes.NewBuffer(nil)
    66  			if _, err := io.Copy(decompressed, r); err != nil {
    67  				continue
    68  			}
    69  
    70  			if err := r.Close(); err != nil {
    71  				continue
    72  			}
    73  
    74  			for _, level := range []int{NoCompression, BestSpeed, BestCompression, DefaultCompression, HuffmanOnly} {
    75  				w, err := NewWriterLevel(io.Discard, level)
    76  				if err != nil {
    77  					t.Fatalf("failed to construct writer: %s", err)
    78  				}
    79  				_, err = w.Write(decompressed.Bytes())
    80  				if err != nil {
    81  					t.Fatalf("failed to write: %s", err)
    82  				}
    83  				if err := w.Flush(); err != nil {
    84  					t.Fatalf("failed to flush: %s", err)
    85  				}
    86  				if err := w.Close(); err != nil {
    87  					t.Fatalf("failed to close: %s", err)
    88  				}
    89  			}
    90  		}
    91  	})
    92  }
    93  

View as plain text