@@ -20,10 +20,51 @@ limitations under the License.
20
20
package yaml
21
21
22
22
import (
23
+ "bytes"
24
+
23
25
"gopkg.in/yaml.v2"
26
+ metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
24
27
sigyaml "sigs.k8s.io/yaml"
25
28
)
26
29
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
+
27
68
// FuzzSigYaml is a fuzz target for "sigs.k8s.io/yaml" unmarshaling.
28
69
func FuzzSigYaml (b []byte ) int {
29
70
t := struct {}{}
@@ -38,6 +79,25 @@ func FuzzSigYaml(b []byte) int {
38
79
return out
39
80
}
40
81
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
+
41
101
// FuzzYamlV2 is a fuzz target for "gopkg.in/yaml.v2" unmarshaling.
42
102
func FuzzYamlV2 (b []byte ) int {
43
103
t := struct {}{}
0 commit comments