Skip to content

Commit

Permalink
Merge pull request #289 from BurntSushi/gofuzz
Browse files Browse the repository at this point in the history
Add support for "go test -fuzz"
  • Loading branch information
arp242 authored Jun 13, 2021
2 parents c39a372 + be3a045 commit 720994e
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 34 deletions.
8 changes: 0 additions & 8 deletions decode.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,6 @@ type Primitive struct {
context Key
}

// DEPRECATED!
//
// Use MetaData.PrimitiveDecode instead.
func PrimitiveDecode(primValue Primitive, v interface{}) error {
md := MetaData{decoded: make(map[string]bool)}
return md.unify(primValue.undecoded, rvalue(v))
}

// PrimitiveDecode is just like the other `Decode*` functions, except it
// decodes a TOML value that has already been parsed. Valid primitive values
// can *only* be obtained from values filled by the decoder functions,
Expand Down
23 changes: 23 additions & 0 deletions deprecated.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package toml

import "encoding"

// DEPRECATED!
//
// Use the identical encoding.TextMarshaler instead. It is defined here to
// support Go 1.1 and older.
type TextMarshaler encoding.TextMarshaler

// DEPRECATED!
//
// Use the identical encoding.TextUnmarshaler instead. It is defined here to
// support Go 1.1 and older.
type TextUnmarshaler encoding.TextUnmarshaler

// DEPRECATED!
//
// Use MetaData.PrimitiveDecode instead.
func PrimitiveDecode(primValue Primitive, v interface{}) error {
md := MetaData{decoded: make(map[string]bool)}
return md.unify(primValue.undecoded, rvalue(v))
}
26 changes: 0 additions & 26 deletions encoding_types.go

This file was deleted.

51 changes: 51 additions & 0 deletions fuzz_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
// +build gofuzzbeta
// +build go1.17

package toml

import (
"bytes"
"testing"
)

func FuzzDecode(f *testing.F) {
buf := make([]byte, 0, 2048)

f.Add(`
# This is a TOML document
title = "TOML Example"
[owner]
name = "Tom Preston-Werner"
dob = 1979-05-27T07:32:00-08:00
[database]
enabled = true
ports = [ 8000, 8001, 8002 ]
data = [ ["delta", "phi"], [3.14] ]
temp_targets = { cpu = 79.5, case = 72.0 }
[servers]
[servers.alpha]
ip = "10.0.0.1"
role = "frontend"
[servers.beta]
ip = "10.0.0.2"
role = "backend"
`)
f.Fuzz(func(t *testing.T, file string) {
var m map[string]interface{}
_, err := Decode(file, &m)
if err != nil {
t.Skip()
}

NewEncoder(bytes.NewBuffer(buf)).Encode(m)

// TODO: should check if the output is equal to the input, too, but some
// information is lost when encoding.
})
}

0 comments on commit 720994e

Please sign in to comment.