Skip to content

Commit

Permalink
support/fuzz: Add xdr.ClaimPredicate JSON fuzzing (#3181)
Browse files Browse the repository at this point in the history
A bug in `xdr.ClaimPredicate.MarshalJSON` (#3179) stopped ingestion in
testnet. I wrote a fuzzer to ensure there are no more bugs. Added files
to separate `xdr/fuzz` package to add more fuzzers in the future.
  • Loading branch information
bartekn authored Nov 5, 2020
1 parent 2cfb267 commit 9bf59df
Show file tree
Hide file tree
Showing 104 changed files with 78 additions and 0 deletions.
Binary file added xdr/fuzz/jsonclaimpredicate/corpus/0
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
78 changes: 78 additions & 0 deletions xdr/fuzz/jsonclaimpredicate/fuzz.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
// +build gofuzz

package jsonclaimpredicate

import (
"bytes"
"encoding/json"

"github.com/stellar/go/xdr"
)

// Fuzz is go-fuzz function for fuzzing xdr.ClaimPredicate JSON
// marshaller and unmarshaller.
func Fuzz(data []byte) int {
// Ignore malformed ClaimPredicate
var p xdr.ClaimPredicate
err := xdr.SafeUnmarshal(data, &p)
if err != nil {
return -1
}

// Ignore invalid predicates: (and/or length != 2, nil not)
if !validate(p) {
return -1
}

j, err := json.Marshal(p)
if err != nil {
panic(err)
}

var p2 xdr.ClaimPredicate
err = json.Unmarshal(j, &p2)
if err != nil {
panic(err)
}

j2, err := json.Marshal(p2)
if err != nil {
panic(err)
}

if !bytes.Equal(j, j2) {
panic("not equal " + string(j) + " " + string(j2))
}

return 1
}

func validate(p xdr.ClaimPredicate) bool {
switch p.Type {
case xdr.ClaimPredicateTypeClaimPredicateUnconditional:
return true
case xdr.ClaimPredicateTypeClaimPredicateAnd:
and := *p.AndPredicates
if len(and) != 2 {
return false
}
return validate(and[0]) && validate(and[1])
case xdr.ClaimPredicateTypeClaimPredicateOr:
or := *p.OrPredicates
if len(or) != 2 {
return false
}
return validate(or[0]) && validate(or[1])
case xdr.ClaimPredicateTypeClaimPredicateNot:
if *p.NotPredicate == nil {
return false
}
return validate(**p.NotPredicate)
case xdr.ClaimPredicateTypeClaimPredicateBeforeAbsoluteTime:
return *p.AbsBefore >= 0
case xdr.ClaimPredicateTypeClaimPredicateBeforeRelativeTime:
return *p.RelBefore >= 0
}

panic("Invalid type")
}

0 comments on commit 9bf59df

Please sign in to comment.