codekingpro/portable-devtools
114k
1// Copyright 2021 The Go Authors. All rights reserved.2// Use of this source code is governed by a BSD-style3// license that can be found in the LICENSE file.4 5//go:build goexperiment.jsonv26 7package json8 9import (10 "bytes"11 "io"12 "testing"13)14 15func 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() any{34 func() any { return new(any) },35 func() any { return new(map[string]any) },36 func() any { return new([]any) },37 } {38 i := typ()39 if err := Unmarshal(b, i); err != nil {40 return41 }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 55func FuzzDecoderToken(f *testing.F) {56 f.Add([]byte(`{57"object": {58 "slice": [59 1,60 2.0,61 "3",62 [4],63 {5: {}}64 ]65},66"slice": [[]],67"string": ":)",68"int": 1e5,69"float": 3e-9"70}`))71 72 f.Fuzz(func(t *testing.T, b []byte) {73 r := bytes.NewReader(b)74 d := NewDecoder(r)75 for {76 _, err := d.Token()77 if err != nil {78 if err == io.EOF {79 break80 }81 return82 }83 }84 })85}86 