Skip to content

Commit 9832418

Browse files
authored
Merge pull request kubernetes#84168 from BrendanSChang/fuzz
Add fuzz targets for Duration, MicroTime, and Time
2 parents e528c2f + 9ef94b2 commit 9832418

File tree

2 files changed

+64
-0
lines changed

2 files changed

+64
-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: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,53 @@ 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+
// FuzzDurationStrict is a fuzz target for strict-unmarshaling Duration defined
31+
// in "k8s.io/apimachinery/pkg/apis/meta/v1". This target also checks that the
32+
// unmarshaled result can be marshaled back to the input.
33+
func FuzzDurationStrict(b []byte) int {
34+
var durationHolder struct {
35+
D metav1.Duration `json:"d"`
36+
}
37+
if err := sigyaml.UnmarshalStrict(b, &durationHolder); err != nil {
38+
return 0
39+
}
40+
result, err := sigyaml.Marshal(&durationHolder)
41+
if err != nil {
42+
panic(err)
43+
}
44+
if !bytes.Equal(result, b) {
45+
panic("result != input")
46+
}
47+
return 1
48+
}
49+
50+
// FuzzMicroTimeStrict is a fuzz target for strict-unmarshaling MicroTime
51+
// defined in "k8s.io/apimachinery/pkg/apis/meta/v1". This target also checks
52+
// that the unmarshaled result can be marshaled back to the input.
53+
func FuzzMicroTimeStrict(b []byte) int {
54+
var microTimeHolder struct {
55+
T metav1.MicroTime `json:"t"`
56+
}
57+
if err := sigyaml.UnmarshalStrict(b, &microTimeHolder); err != nil {
58+
return 0
59+
}
60+
result, err := sigyaml.Marshal(&microTimeHolder)
61+
if err != nil {
62+
panic(err)
63+
}
64+
if !bytes.Equal(result, b) {
65+
panic("result != input")
66+
}
67+
return 1
68+
}
69+
2770
// FuzzSigYaml is a fuzz target for "sigs.k8s.io/yaml" unmarshaling.
2871
func FuzzSigYaml(b []byte) int {
2972
t := struct{}{}
@@ -38,6 +81,26 @@ func FuzzSigYaml(b []byte) int {
3881
return out
3982
}
4083

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

0 commit comments

Comments
 (0)