...

Source file src/github.com/chenzhuoyu/base64x/fuzz_test.go

Documentation: github.com/chenzhuoyu/base64x

     1  package base64x
     2  
     3  import (
     4      `encoding/base64`
     5      `encoding/json`
     6      `testing`
     7      `github.com/stretchr/testify/require`
     8      `github.com/davecgh/go-spew/spew`
     9  )
    10  
    11  func FuzzMain(f *testing.F) {
    12      var corpus = []string {
    13          "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/",
    14          "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_",
    15          "=",
    16          `\/`,
    17          "\r\n",
    18          `\r\n`,
    19          `\u0036`, `\u0039`, `\u003d`,
    20          `"\u0036"`, `"\u003d\u003d"`,
    21      }
    22      for _, c := range(corpus) {
    23          f.Add([]byte(c))
    24      }
    25      f.Fuzz(fuzzBase64Impl)
    26  }
    27  
    28  func fuzzBase64Impl(t *testing.T, data []byte) {
    29      fuzzBase64CommonImpl(t, data)
    30      fuzzBase64JsonImpl(t, data)
    31  }
    32  
    33  type EncodeFuzzPairs struct {
    34      ours      Encoding
    35      stdlib    *base64.Encoding
    36  }
    37  
    38  var fuzzPairs = []EncodeFuzzPairs {
    39      {StdEncoding, base64.StdEncoding},
    40      {URLEncoding, base64.URLEncoding},
    41      {RawStdEncoding, base64.RawStdEncoding},
    42      {RawURLEncoding, base64.RawURLEncoding},
    43  }
    44  
    45  func fuzzBase64CommonImpl(t *testing.T, data []byte) {
    46      for _, fp := range(fuzzPairs) {
    47          // fuzz encode
    48          encoded0 := fp.ours.EncodeToString(data)
    49          encoded1 := fp.stdlib.EncodeToString(data)
    50          require.Equalf(t, encoded0, encoded1, "encode from %s", spew.Sdump(data))
    51          // fuzz decode
    52          encoded := encoded1
    53          dbuf0 := make([]byte, fp.ours.DecodedLen(len(encoded)))
    54          dbuf1 := make([]byte, fp.stdlib.DecodedLen(len(encoded)))
    55          count0, err0 := fp.ours.Decode(dbuf0, []byte(encoded))
    56          count1, err1 := fp.stdlib.Decode(dbuf1, []byte(encoded))
    57          require.Equalf(t, dbuf0, dbuf1, "decode from %s", spew.Sdump(encoded))
    58          require.Equalf(t, err0 != nil, err1 != nil, "decode from %s", spew.Sdump(encoded))
    59          require.Equalf(t, count0, count1, "decode from %s", spew.Sdump(encoded))
    60      }
    61  }
    62  
    63  func fuzzBase64JsonImpl(t *testing.T, data []byte) {
    64      // fuzz valid JSON-encoded base64
    65      jencoded, _ := json.Marshal(data)
    66      var dbuf0, dbuf1 []byte
    67      dbuf0 = make([]byte,  JSONStdEncoding.DecodedLen(len(jencoded)))
    68      count0, err0 := JSONStdEncoding.Decode(dbuf0, jencoded[1:len(jencoded) - 1])
    69      err1 := json.Unmarshal(jencoded, &dbuf1)
    70      require.Equalf(t, dbuf0[:count0], dbuf1,  "decode json from %s", spew.Sdump(jencoded))
    71      require.Equalf(t, err0 != nil, err1 != nil, "decode json from %s", spew.Sdump(jencoded))
    72  }

View as plain text