Skip to content

Commit d2074fc

Browse files
author
Brendan Chang
committed
Add fuzz targets for Duration, MicroTime, and Time
1 parent b717be8 commit d2074fc

File tree

2 files changed

+61
-0
lines changed

2 files changed

+61
-0
lines changed

test/fuzz/yaml/BUILD

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ go_library(
66
importpath = "k8s.io/kubernetes/test/fuzz/yaml",
77
visibility = ["//visibility:private"],
88
deps = [
9+
"//staging/src/k8s.io/apimachinery/pkg/apis/meta/v1:go_default_library",
910
"//vendor/gopkg.in/yaml.v2:go_default_library",
1011
"//vendor/sigs.k8s.io/yaml:go_default_library",
1112
],

test/fuzz/yaml/yaml.go

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,51 @@ limitations under the License.
2020
package yaml
2121

2222
import (
23+
"bytes"
24+
2325
"gopkg.in/yaml.v2"
26+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2427
sigyaml "sigs.k8s.io/yaml"
2528
)
2629

30+
// FuzzDuration is a fuzz target for unmarshaling Duration defined in "k8s.io/apimachinery/pkg/apis/meta/v1".
31+
// This target also checks that the unmarshaled result can be marshaled back to the input.
32+
func FuzzDuration(b []byte) int {
33+
var unmarshalResult struct {
34+
D metav1.Duration `json:"d"`
35+
}
36+
if err := yaml.Unmarshal(b, &unmarshalResult); err != nil {
37+
return 0
38+
}
39+
marshalResult, err := yaml.Marshal(&unmarshalResult)
40+
if err != nil {
41+
panic(err)
42+
}
43+
if !bytes.Equal(marshalResult, b) {
44+
panic("marshalResult != input")
45+
}
46+
return 1
47+
}
48+
49+
// FuzzMicroTime is a fuzz target for unmarshaling MicroTime defined in "k8s.io/apimachinery/pkg/apis/meta/v1".
50+
// This target also checks that the unmarshaled result can be marshaled back to the input.
51+
func FuzzMicroTime(b []byte) int {
52+
var unmarshalResult struct {
53+
T metav1.MicroTime `json:"t"`
54+
}
55+
if err := yaml.Unmarshal(b, &unmarshalResult); err != nil {
56+
return 0
57+
}
58+
marshalResult, err := yaml.Marshal(&unmarshalResult)
59+
if err != nil {
60+
panic(err)
61+
}
62+
if !bytes.Equal(marshalResult, b) {
63+
panic("marshalResult != input")
64+
}
65+
return 1
66+
}
67+
2768
// FuzzSigYaml is a fuzz target for "sigs.k8s.io/yaml" unmarshaling.
2869
func FuzzSigYaml(b []byte) int {
2970
t := struct{}{}
@@ -38,6 +79,25 @@ func FuzzSigYaml(b []byte) int {
3879
return out
3980
}
4081

82+
// FuzzTime is a fuzz target for unmarshaling Time defined in "k8s.io/apimachinery/pkg/apis/meta/v1".
83+
// This target also checks that the unmarshaled result can be marshaled back to the input.
84+
func FuzzTime(b []byte) int {
85+
var unmarshalResult struct {
86+
T metav1.Time `json:"t"`
87+
}
88+
if err := yaml.Unmarshal(b, &unmarshalResult); err != nil {
89+
return 0
90+
}
91+
marshalResult, err := yaml.Marshal(&unmarshalResult)
92+
if err != nil {
93+
panic(err)
94+
}
95+
if !bytes.Equal(marshalResult, b) {
96+
panic("marshalResult != input")
97+
}
98+
return 1
99+
}
100+
41101
// FuzzYamlV2 is a fuzz target for "gopkg.in/yaml.v2" unmarshaling.
42102
func FuzzYamlV2(b []byte) int {
43103
t := struct{}{}

0 commit comments

Comments
 (0)