git.lukeshu.com/go/lowmemjson@v0.3.9-0.20230723050957-72f6d13f6fb2/compat/json/borrowed_fuzz_test.go (about) 1 // Copyright 2021 The Go Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 // 5 // SPDX-License-Identifier: BSD-3-Clause 6 7 package json 8 9 import ( 10 "bytes" 11 "io" 12 "testing" 13 ) 14 15 func FuzzUnmarshalJSON(f *testing.F) { 16 f.Add([]byte(`{ 17 "object": { 18 "slice": [ 19 1, 20 2.0, 21 "3", 22 [4], 23 {5: {}} 24 ] 25 }, 26 "slice": [[]], 27 "string": ":)", 28 "int": 1e5, 29 "float": 3e-9" 30 }`)) 31 32 f.Fuzz(func(t *testing.T, b []byte) { 33 for _, typ := range []func() interface{}{ 34 func() interface{} { return new(interface{}) }, 35 func() interface{} { return new(map[string]interface{}) }, 36 func() interface{} { return new([]interface{}) }, 37 } { 38 i := typ() 39 if err := Unmarshal(b, i); err != nil { 40 return 41 } 42 43 encoded, err := Marshal(i) 44 if err != nil { 45 t.Fatalf("failed to marshal: %s", err) 46 } 47 48 if err := Unmarshal(encoded, i); err != nil { 49 t.Fatalf("failed to roundtrip: %s", err) 50 } 51 } 52 }) 53 } 54 55 func FuzzDecoderToken(f *testing.F) { 56 f.Skip("we don't have tokens") // MODIFIED: added 57 f.Add([]byte(`{ 58 "object": { 59 "slice": [ 60 1, 61 2.0, 62 "3", 63 [4], 64 {5: {}} 65 ] 66 }, 67 "slice": [[]], 68 "string": ":)", 69 "int": 1e5, 70 "float": 3e-9" 71 }`)) 72 73 f.Fuzz(func(t *testing.T, b []byte) { 74 r := bytes.NewReader(b) 75 d := NewDecoder(r) 76 for { 77 _, err := d.Token() 78 if err != nil { 79 if err == io.EOF { 80 break 81 } 82 return 83 } 84 } 85 }) 86 }