...

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

Documentation: github.com/cloudwego/base64x

     1  /*
     2   * Copyright 2024 CloudWeGo Authors
     3   *
     4   * Licensed under the Apache License, Version 2.0 (the "License");
     5   * you may not use this file except in compliance with the License.
     6   * You may obtain a copy of the License at
     7   *
     8   *     http://www.apache.org/licenses/LICENSE-2.0
     9   *
    10   * Unless required by applicable law or agreed to in writing, software
    11   * distributed under the License is distributed on an "AS IS" BASIS,
    12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13   * See the License for the specific language governing permissions and
    14   * limitations under the License.
    15   */
    16  
    17  package base64x
    18  
    19  import (
    20      `encoding/base64`
    21      `encoding/json`
    22      `testing`
    23      `github.com/stretchr/testify/require`
    24      `github.com/davecgh/go-spew/spew`
    25  )
    26  
    27  func FuzzMain(f *testing.F) {
    28      var corpus = []string {
    29          "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/",
    30          "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_",
    31          "=",
    32          `\/`,
    33          "\r\n",
    34          `\r\n`,
    35          `\u0036`, `\u0039`, `\u003d`,
    36          `"\u0036"`, `"\u003d\u003d"`,
    37      }
    38      for _, c := range(corpus) {
    39          f.Add([]byte(c))
    40      }
    41      f.Fuzz(fuzzBase64Impl)
    42  }
    43  
    44  func fuzzBase64Impl(t *testing.T, data []byte) {
    45      fuzzBase64CommonImpl(t, data)
    46      fuzzBase64JsonImpl(t, data)
    47  }
    48  
    49  type EncodeFuzzPairs struct {
    50      ours      Encoding
    51      stdlib    *base64.Encoding
    52  }
    53  
    54  var fuzzPairs = []EncodeFuzzPairs {
    55      {StdEncoding, base64.StdEncoding},
    56      {URLEncoding, base64.URLEncoding},
    57      {RawStdEncoding, base64.RawStdEncoding},
    58      {RawURLEncoding, base64.RawURLEncoding},
    59  }
    60  
    61  func fuzzBase64CommonImpl(t *testing.T, data []byte) {
    62      for _, fp := range(fuzzPairs) {
    63          // fuzz encode
    64          encoded0 := fp.ours.EncodeToString(data)
    65          encoded1 := fp.stdlib.EncodeToString(data)
    66          require.Equalf(t, encoded0, encoded1, "encode from %s", spew.Sdump(data))
    67          // fuzz decode
    68          encoded := encoded1
    69          dbuf0 := make([]byte, fp.ours.DecodedLen(len(encoded)))
    70          dbuf1 := make([]byte, fp.stdlib.DecodedLen(len(encoded)))
    71          count0, err0 := fp.ours.Decode(dbuf0, []byte(encoded))
    72          count1, err1 := fp.stdlib.Decode(dbuf1, []byte(encoded))
    73          require.Equalf(t, dbuf0, dbuf1, "decode from %s", spew.Sdump(encoded))
    74          require.Equalf(t, err0 != nil, err1 != nil, "decode from %s", spew.Sdump(encoded))
    75          require.Equalf(t, count0, count1, "decode from %s", spew.Sdump(encoded))
    76      }
    77  }
    78  
    79  func fuzzBase64JsonImpl(t *testing.T, data []byte) {
    80      // fuzz valid JSON-encoded base64
    81      jencoded, _ := json.Marshal(data)
    82      var dbuf0, dbuf1 []byte
    83      dbuf0 = make([]byte,  JSONStdEncoding.DecodedLen(len(jencoded)))
    84      count0, err0 := JSONStdEncoding.Decode(dbuf0, jencoded[1:len(jencoded) - 1])
    85      err1 := json.Unmarshal(jencoded, &dbuf1)
    86      require.Equalf(t, dbuf0[:count0], dbuf1,  "decode json from %s", spew.Sdump(jencoded))
    87      require.Equalf(t, err0 != nil, err1 != nil, "decode json from %s", spew.Sdump(jencoded))
    88  }
    89  

View as plain text