github.com/ethereum-optimism/optimism@v1.7.2/op-node/rollup/derive/batch_tob_test.go (about)

     1  package derive
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/ethereum-optimism/optimism/op-service/testutils/fuzzerutils"
     7  	fuzz "github.com/google/gofuzz"
     8  	"github.com/stretchr/testify/require"
     9  )
    10  
    11  // FuzzBatchRoundTrip executes a fuzz test similar to TestBatchRoundTrip, which tests that arbitrary BatchData will be
    12  // encoded and decoded without loss of its original values.
    13  // Does not test the span batch type because the fuzzer is not aware of the structure of a span batch.
    14  func FuzzBatchRoundTrip(f *testing.F) {
    15  	f.Fuzz(func(t *testing.T, fuzzedData []byte) {
    16  		// Create our fuzzer wrapper to generate complex values
    17  		typeProvider := fuzz.NewFromGoFuzz(fuzzedData).NilChance(0).MaxDepth(10000).NumElements(0, 0x100).AllowUnexportedFields(true)
    18  		fuzzerutils.AddFuzzerFunctions(typeProvider)
    19  
    20  		var singularBatch SingularBatch
    21  		typeProvider.Fuzz(&singularBatch)
    22  
    23  		// Create our batch data from fuzzed data
    24  		var batchData BatchData
    25  		// force batchdata to only contain singular batch
    26  		batchData.inner = &singularBatch
    27  
    28  		// Encode our batch data
    29  		enc, err := batchData.MarshalBinary()
    30  		require.NoError(t, err)
    31  
    32  		// Decode our encoded batch data
    33  		var dec BatchData
    34  		err = dec.UnmarshalBinary(enc)
    35  		require.NoError(t, err)
    36  
    37  		// Ensure the round trip encoding of batch data did not result in data loss
    38  		require.Equal(t, &batchData, &dec, "round trip batch encoding/decoding did not match original values")
    39  	})
    40  }