github.com/RomiChan/protobuf@v0.1.1-0.20230204044148-2ed269a2e54d/proto/fuzz_test.go (about)

     1  package proto_test
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/stretchr/testify/assert"
     7  
     8  	. "github.com/RomiChan/protobuf/proto"
     9  	"github.com/RomiChan/protobuf/proto/internal/testproto"
    10  )
    11  
    12  func FuzzMarshal1(f *testing.F) {
    13  	f.Fuzz(func(t *testing.T, i int32, j int64, s string) {
    14  		input := testproto.Proto2{
    15  			Nested: &testproto.Proto2_NestedMessage{
    16  				Int32Val:  Some(i),
    17  				Int64Val:  Some(j),
    18  				StringVal: Some(s),
    19  			},
    20  		}
    21  		b, err := Marshal(&input)
    22  		assert.NoError(t, err)
    23  
    24  		var output testproto.Proto2
    25  		assert.NoError(t, Unmarshal(b, &output))
    26  		assert.Equal(t, input, output)
    27  	})
    28  }
    29  
    30  func FuzzMarshalNested(f *testing.F) {
    31  	type message4 struct {
    32  		Value string `protobuf:"bytes,1,opt"`
    33  	}
    34  	type message3 struct {
    35  		Value  string    `protobuf:"bytes,1,opt"`
    36  		Nested *message4 `protobuf:"bytes,2,opt"`
    37  	}
    38  	type message2 struct {
    39  		Value  string    `protobuf:"bytes,1,opt"`
    40  		Nested *message3 `protobuf:"bytes,2,opt"`
    41  	}
    42  	type message1 struct {
    43  		Value  string    `protobuf:"bytes,1,opt"`
    44  		Nested *message2 `protobuf:"bytes,2,opt"`
    45  	}
    46  	type message struct {
    47  		Value  string    `protobuf:"bytes,1,opt"`
    48  		Nested *message1 `protobuf:"bytes,2,opt"`
    49  	}
    50  	f.Fuzz(func(t *testing.T, s, s1, s2, s3, s4 string) {
    51  		input := message{
    52  			Value: s,
    53  			Nested: &message1{
    54  				Value: s1,
    55  				Nested: &message2{
    56  					Value: s2,
    57  					Nested: &message3{
    58  						Value: s3,
    59  						Nested: &message4{
    60  							Value: s4,
    61  						},
    62  					},
    63  				},
    64  			},
    65  		}
    66  		b, err := Marshal(&input)
    67  		assert.NoError(t, err)
    68  
    69  		var output message
    70  		assert.NoError(t, Unmarshal(b, &output))
    71  		assert.Equal(t, input, output)
    72  	})
    73  }