Skip to content

Commit

Permalink
Merge pull request #19 from fenollp/fuzzing-marshalling
Browse files Browse the repository at this point in the history
Fuzz un/marshall-ing methods after #16
  • Loading branch information
wk8 authored Dec 11, 2022
2 parents fe4d501 + 2e43df0 commit c95df00
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 3 deletions.
77 changes: 77 additions & 0 deletions fuzz_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package orderedmap

// Adapted from https://github.com/dvyukov/go-fuzz-corpus/blob/c42c1b2/json/json.go

import (
"encoding/json"
"fmt"
"reflect"
"testing"
)

func FuzzMarshalling(f *testing.F) {
f.Fuzz(func(t *testing.T, data []byte) {
for _, ctor := range []func() any{
func() any { return &OrderedMap[string, string]{} },
func() any { return &OrderedMap[string, any]{} },
func() any { return new(S) },
} {
v := ctor()
if json.Unmarshal(data, v) != nil {
continue
}
data1, err := json.Marshal(v)
if err != nil {
panic(err)
}

if s, ok := v.(*S); ok {
if len(s.P) == 0 {
s.P = []byte(`""`)
}
}

v1 := ctor()
if json.Unmarshal(data1, v1) != nil {
continue
}

if s, ok := v.(*S); ok {
// Some additional escaping happens with P.
s.P = nil
v1.(*S).P = nil
}

if !reflect.DeepEqual(v, v1) {
// Skip false positives due to linked list initialization
switch v := v.(type) {
case *OrderedMap[string, string]:
if l := v1.(*OrderedMap[string, string]).Len(); l == 0 && l == v.Len() {
continue
}
case *OrderedMap[string, any]:
if l := v1.(*OrderedMap[string, any]).Len(); l == 0 && l == v.Len() {
continue
}
case *S:
if l := v1.(*S).H.Len(); l == 0 && l == v.H.Len() {
if ll := v1.(*S).I.Len(); ll == 0 && ll == v.I.Len() {
continue
}
}
default:
panic(fmt.Sprintf("unhandled %T", v))
}
fmt.Printf("v0: %#v\n", v)
fmt.Printf("v1: %#v\n", v1)
panic("not equal")
}
}
})
}

type S struct {
H OrderedMap[int, any]
I OrderedMap[int, string]
P json.RawMessage
}
3 changes: 1 addition & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,12 @@ go 1.18
require (
github.com/bahlo/generic-list-go v0.2.0
github.com/buger/jsonparser v1.1.1
github.com/davecgh/go-spew v1.1.0
github.com/mailru/easyjson v0.7.7
github.com/stretchr/testify v1.6.1
)

require (
github.com/josharian/intern v1.0.0 // indirect
github.com/davecgh/go-spew v1.1.0 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c // indirect
)
1 change: 0 additions & 1 deletion go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ github.com/buger/jsonparser v1.1.1 h1:2PnMjfWD7wBILjqQbt530v576A/cAbQvEW9gGIpYMU
github.com/buger/jsonparser v1.1.1/go.mod h1:6RYKKt7H4d4+iWqouImQ9R2FZql3VbhNgx27UK13J/0=
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/josharian/intern v1.0.0 h1:vlS4z54oSdjm0bgjRigI+G1HpF+tI+9rE5LLzOg8HmY=
github.com/josharian/intern v1.0.0/go.mod h1:5DoeVV0s6jJacbCEi61lwdGj/aVlrQvzHFFd8Hwg//Y=
github.com/mailru/easyjson v0.7.7 h1:UGYAvKxe3sBsEDzO8ZeWOSlIQfWFlxbzLZe7hwFURr0=
github.com/mailru/easyjson v0.7.7/go.mod h1:xzfreul335JAWq5oZzymOObrkdz5UnU4kGfJJLY9Nlc=
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
go test fuzz v1
[]byte("{}")

0 comments on commit c95df00

Please sign in to comment.