|
| 1 | +package types |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | + |
| 6 | + "google.golang.org/protobuf/proto" |
| 7 | + |
| 8 | + pb "github.com/evstack/ev-node/types/pb/evnode/v1" |
| 9 | +) |
| 10 | + |
| 11 | +func FuzzHeaderUnmarshalBinary(f *testing.F) { |
| 12 | + // Seed with a valid marshalled header |
| 13 | + h := Header{} |
| 14 | + h.BaseHeader.Height = 1 |
| 15 | + h.BaseHeader.ChainID = "test" |
| 16 | + h.BaseHeader.Time = 1000 |
| 17 | + if data, err := h.MarshalBinary(); err == nil { |
| 18 | + f.Add(data) |
| 19 | + } |
| 20 | + f.Add([]byte{}) |
| 21 | + f.Add([]byte{0xff, 0xff, 0xff}) |
| 22 | + |
| 23 | + f.Fuzz(func(t *testing.T, data []byte) { |
| 24 | + var h Header |
| 25 | + if err := h.UnmarshalBinary(data); err != nil { |
| 26 | + return |
| 27 | + } |
| 28 | + // Round-trip: if unmarshal succeeds, marshal should not panic |
| 29 | + _, _ = h.MarshalBinary() |
| 30 | + }) |
| 31 | +} |
| 32 | + |
| 33 | +func FuzzDataUnmarshalBinary(f *testing.F) { |
| 34 | + d := Data{Txs: Txs{[]byte("tx1"), []byte("tx2")}} |
| 35 | + if data, err := d.MarshalBinary(); err == nil { |
| 36 | + f.Add(data) |
| 37 | + } |
| 38 | + f.Add([]byte{}) |
| 39 | + |
| 40 | + f.Fuzz(func(t *testing.T, data []byte) { |
| 41 | + var d Data |
| 42 | + if err := d.UnmarshalBinary(data); err != nil { |
| 43 | + return |
| 44 | + } |
| 45 | + _, _ = d.MarshalBinary() |
| 46 | + }) |
| 47 | +} |
| 48 | + |
| 49 | +func FuzzSignedHeaderUnmarshalBinary(f *testing.F) { |
| 50 | + f.Add([]byte{}) |
| 51 | + f.Add([]byte{0x0a, 0x00}) |
| 52 | + |
| 53 | + f.Fuzz(func(t *testing.T, data []byte) { |
| 54 | + var sh SignedHeader |
| 55 | + if err := sh.UnmarshalBinary(data); err != nil { |
| 56 | + return |
| 57 | + } |
| 58 | + _, _ = sh.MarshalBinary() |
| 59 | + }) |
| 60 | +} |
| 61 | + |
| 62 | +func FuzzSignedDataUnmarshalBinary(f *testing.F) { |
| 63 | + f.Add([]byte{}) |
| 64 | + f.Add([]byte{0x0a, 0x00}) |
| 65 | + |
| 66 | + f.Fuzz(func(t *testing.T, data []byte) { |
| 67 | + var sd SignedData |
| 68 | + if err := sd.UnmarshalBinary(data); err != nil { |
| 69 | + return |
| 70 | + } |
| 71 | + _, _ = sd.MarshalBinary() |
| 72 | + }) |
| 73 | +} |
| 74 | + |
| 75 | +func FuzzDAEnvelopeUnmarshalBinary(f *testing.F) { |
| 76 | + f.Add([]byte{}) |
| 77 | + |
| 78 | + f.Fuzz(func(t *testing.T, data []byte) { |
| 79 | + var sh SignedHeader |
| 80 | + if _, err := sh.UnmarshalDAEnvelope(data); err != nil { |
| 81 | + return |
| 82 | + } |
| 83 | + _, _ = sh.MarshalDAEnvelope(nil) |
| 84 | + }) |
| 85 | +} |
| 86 | + |
| 87 | +func FuzzStateFromProto(f *testing.F) { |
| 88 | + s := State{ChainID: "test", LastBlockHeight: 1} |
| 89 | + if p, err := s.ToProto(); err == nil { |
| 90 | + if data, err := proto.Marshal(p); err == nil { |
| 91 | + f.Add(data) |
| 92 | + } |
| 93 | + } |
| 94 | + f.Add([]byte{}) |
| 95 | + |
| 96 | + f.Fuzz(func(t *testing.T, data []byte) { |
| 97 | + var ps pb.State |
| 98 | + if err := proto.Unmarshal(data, &ps); err != nil { |
| 99 | + return |
| 100 | + } |
| 101 | + var s State |
| 102 | + _ = s.FromProto(&ps) |
| 103 | + }) |
| 104 | +} |
0 commit comments