1// Copyright 2015 go-fuzz project authors. All rights reserved.
2// Use of this source code is governed by Apache 2 LICENSE.
3// Modified from original file https://github.com/dvyukov/go-fuzz-corpus/blob/master/json/json.go
4
5package jsoniter
6
7import (
8	"encoding/json"
9	"fmt"
10	"reflect"
11)
12
13func Fuzz(data []byte) int {
14	score := 0
15	for _, ctor := range []func() interface{}{
16		//func() interface{} { return nil },
17		func() interface{} { return new([]interface{}) },
18		func() interface{} { m := map[string]string{}; return &m },
19		func() interface{} { m := map[string]interface{}{}; return &m },
20		func() interface{} { return new(S) },
21	} {
22		v := ctor()
23		if ConfigCompatibleWithStandardLibrary.Unmarshal(data, v) != nil {
24			continue
25		}
26		score = 1
27		vj := ctor()
28		err := json.Unmarshal(data, vj)
29		if err != nil {
30			panic(err)
31		}
32		if !reflect.DeepEqual(v, vj) {
33			fmt.Printf("v0: %#v\n", v)
34			fmt.Printf("v1: %#v\n", vj)
35			panic("not equal")
36		}
37
38		data1, err := ConfigCompatibleWithStandardLibrary.Marshal(v)
39		if err != nil {
40			panic(err)
41		}
42		v1 := ctor()
43		if ConfigCompatibleWithStandardLibrary.Unmarshal(data1, v1) != nil {
44			continue
45		}
46		if !reflect.DeepEqual(v, v1) {
47			fmt.Printf("v0: %#v\n", v)
48			fmt.Printf("v1: %#v\n", v1)
49			panic("not equal")
50		}
51	}
52	return score
53}
54
55type S struct {
56	A int    `json:",omitempty"`
57	B string `json:"B1,omitempty"`
58	C float64
59	D bool
60	E uint8
61	F []byte
62	G interface{}
63	H map[string]interface{}
64	I map[string]string
65	J []interface{}
66	K []string
67	L S1
68	M *S1
69	N *int
70	O **int
71	//	P json.RawMessage
72	Q Marshaller
73	R int `json:"-"`
74	S int `json:",string"`
75}
76
77type S1 struct {
78	A int
79	B string
80}
81
82type Marshaller struct {
83	v string
84}
85
86func (m *Marshaller) MarshalJSON() ([]byte, error) {
87	return ConfigCompatibleWithStandardLibrary.Marshal(m.v)
88}
89
90func (m *Marshaller) UnmarshalJSON(data []byte) error {
91	return ConfigCompatibleWithStandardLibrary.Unmarshal(data, &m.v)
92}
93