Skip to content

Commit 4c6bed6

Browse files
authored
Merge pull request kubernetes#84480 from BrendanSChang/fuzz
Fix validation for metav1 fuzz targets.
2 parents 9295fdb + fef67e4 commit 4c6bed6

File tree

1 file changed

+32
-7
lines changed

1 file changed

+32
-7
lines changed

test/fuzz/yaml/yaml.go

Lines changed: 32 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@ limitations under the License.
2020
package yaml
2121

2222
import (
23-
"bytes"
23+
"fmt"
24+
"strings"
2425

2526
"gopkg.in/yaml.v2"
2627
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
@@ -41,8 +42,12 @@ func FuzzDurationStrict(b []byte) int {
4142
if err != nil {
4243
panic(err)
4344
}
44-
if !bytes.Equal(result, b) {
45-
panic("result != input")
45+
// Result is in the format "d: <duration>\n", so strip off the trailing
46+
// newline and convert durationHolder.D to the expected format.
47+
resultStr := strings.TrimSpace(string(result[:]))
48+
inputStr := fmt.Sprintf("d: %s", durationHolder.D.Duration)
49+
if resultStr != inputStr {
50+
panic(fmt.Sprintf("result(%v) != input(%v)", resultStr, inputStr))
4651
}
4752
return 1
4853
}
@@ -61,8 +66,18 @@ func FuzzMicroTimeStrict(b []byte) int {
6166
if err != nil {
6267
panic(err)
6368
}
64-
if !bytes.Equal(result, b) {
65-
panic("result != input")
69+
// Result is in the format "t: <time>\n", so strip off the trailing
70+
// newline and convert microTimeHolder.T to the expected format. If
71+
// time is zero, the value is marshaled to "null".
72+
resultStr := strings.TrimSpace(string(result[:]))
73+
var inputStr string
74+
if microTimeHolder.T.Time.IsZero() {
75+
inputStr = "t: null"
76+
} else {
77+
inputStr = fmt.Sprintf("t: %s", microTimeHolder.T.Time)
78+
}
79+
if resultStr != inputStr {
80+
panic(fmt.Sprintf("result(%v) != input(%v)", resultStr, inputStr))
6681
}
6782
return 1
6883
}
@@ -95,8 +110,18 @@ func FuzzTimeStrict(b []byte) int {
95110
if err != nil {
96111
panic(err)
97112
}
98-
if !bytes.Equal(result, b) {
99-
panic("result != input")
113+
// Result is in the format "t: <time>\n", so strip off the trailing
114+
// newline and convert timeHolder.T to the expected format. If time is
115+
// zero, the value is marshaled to "null".
116+
resultStr := strings.TrimSpace(string(result[:]))
117+
var inputStr string
118+
if timeHolder.T.Time.IsZero() {
119+
inputStr = "t: null"
120+
} else {
121+
inputStr = fmt.Sprintf("t: %s", timeHolder.T.Time)
122+
}
123+
if resultStr != inputStr {
124+
panic(fmt.Sprintf("result(%v) != input(%v)", resultStr, inputStr))
100125
}
101126
return 1
102127
}

0 commit comments

Comments
 (0)