Use strict unmarshaling for metav1 fuzz targets

This commit is contained in:
Brendan Chang 2019-10-23 19:16:50 -04:00
parent 862e814f82
commit 7aa9434811

View File

@ -27,40 +27,42 @@ import (
sigyaml "sigs.k8s.io/yaml" sigyaml "sigs.k8s.io/yaml"
) )
// FuzzDuration is a fuzz target for unmarshaling Duration defined in "k8s.io/apimachinery/pkg/apis/meta/v1". // FuzzDuration is a fuzz target for strict-unmarshaling Duration defined in
// This target also checks that the unmarshaled result can be marshaled back to the input. // "k8s.io/apimachinery/pkg/apis/meta/v1". This target also checks that the
func FuzzDuration(b []byte) int { // unmarshaled result can be marshaled back to the input.
var unmarshalResult struct { func FuzzDurationStrict(b []byte) int {
var durationHolder struct {
D metav1.Duration `json:"d"` D metav1.Duration `json:"d"`
} }
if err := yaml.Unmarshal(b, &unmarshalResult); err != nil { if err := sigyaml.UnmarshalStrict(b, &durationHolder); err != nil {
return 0 return 0
} }
marshalResult, err := sigyaml.Marshal(&unmarshalResult) result, err := sigyaml.Marshal(&durationHolder)
if err != nil { if err != nil {
panic(err) panic(err)
} }
if !bytes.Equal(marshalResult, b) { if !bytes.Equal(result, b) {
panic("marshalResult != input") panic("result != input")
} }
return 1 return 1
} }
// FuzzMicroTime is a fuzz target for unmarshaling MicroTime defined in "k8s.io/apimachinery/pkg/apis/meta/v1". // FuzzMicroTime is a fuzz target for strict-unmarshaling MicroTime defined in
// This target also checks that the unmarshaled result can be marshaled back to the input. // "k8s.io/apimachinery/pkg/apis/meta/v1". This target also checks that the
func FuzzMicroTime(b []byte) int { // unmarshaled result can be marshaled back to the input.
var unmarshalResult struct { func FuzzMicroTimeStrict(b []byte) int {
var microTimeHolder struct {
T metav1.MicroTime `json:"t"` T metav1.MicroTime `json:"t"`
} }
if err := yaml.Unmarshal(b, &unmarshalResult); err != nil { if err := sigyaml.UnmarshalStrict(b, &microTimeHolder); err != nil {
return 0 return 0
} }
marshalResult, err := sigyaml.Marshal(&unmarshalResult) result, err := sigyaml.Marshal(&microTimeHolder)
if err != nil { if err != nil {
panic(err) panic(err)
} }
if !bytes.Equal(marshalResult, b) { if !bytes.Equal(result, b) {
panic("marshalResult != input") panic("result != input")
} }
return 1 return 1
} }
@ -79,21 +81,22 @@ func FuzzSigYaml(b []byte) int {
return out return out
} }
// FuzzTime is a fuzz target for unmarshaling Time defined in "k8s.io/apimachinery/pkg/apis/meta/v1". // FuzzTime is a fuzz target for strict-unmarshaling Time defined in
// This target also checks that the unmarshaled result can be marshaled back to the input. // "k8s.io/apimachinery/pkg/apis/meta/v1". This target also checks that the
// unmarshaled result can be marshaled back to the input.
func FuzzTime(b []byte) int { func FuzzTime(b []byte) int {
var unmarshalResult struct { var timeHolder struct {
T metav1.Time `json:"t"` T metav1.Time `json:"t"`
} }
if err := sigyaml.Unmarshal(b, &unmarshalResult); err != nil { if err := sigyaml.UnmarshalStrict(b, &timeHolder); err != nil {
return 0 return 0
} }
marshalResult, err := yaml.Marshal(&unmarshalResult) result, err := sigyaml.Marshal(&timeHolder)
if err != nil { if err != nil {
panic(err) panic(err)
} }
if !bytes.Equal(marshalResult, b) { if !bytes.Equal(result, b) {
panic("marshalResult != input") panic("result != input")
} }
return 1 return 1
} }