Merge pull request #84480 from BrendanSChang/fuzz

Fix validation for metav1 fuzz targets.
This commit is contained in:
Kubernetes Prow Robot 2019-12-19 12:40:10 -08:00 committed by GitHub
commit 4c6bed6e4e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -20,7 +20,8 @@ limitations under the License.
package yaml
import (
"bytes"
"fmt"
"strings"
"gopkg.in/yaml.v2"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
@ -41,8 +42,12 @@ func FuzzDurationStrict(b []byte) int {
if err != nil {
panic(err)
}
if !bytes.Equal(result, b) {
panic("result != input")
// Result is in the format "d: <duration>\n", so strip off the trailing
// newline and convert durationHolder.D to the expected format.
resultStr := strings.TrimSpace(string(result[:]))
inputStr := fmt.Sprintf("d: %s", durationHolder.D.Duration)
if resultStr != inputStr {
panic(fmt.Sprintf("result(%v) != input(%v)", resultStr, inputStr))
}
return 1
}
@ -61,8 +66,18 @@ func FuzzMicroTimeStrict(b []byte) int {
if err != nil {
panic(err)
}
if !bytes.Equal(result, b) {
panic("result != input")
// Result is in the format "t: <time>\n", so strip off the trailing
// newline and convert microTimeHolder.T to the expected format. If
// time is zero, the value is marshaled to "null".
resultStr := strings.TrimSpace(string(result[:]))
var inputStr string
if microTimeHolder.T.Time.IsZero() {
inputStr = "t: null"
} else {
inputStr = fmt.Sprintf("t: %s", microTimeHolder.T.Time)
}
if resultStr != inputStr {
panic(fmt.Sprintf("result(%v) != input(%v)", resultStr, inputStr))
}
return 1
}
@ -95,8 +110,18 @@ func FuzzTimeStrict(b []byte) int {
if err != nil {
panic(err)
}
if !bytes.Equal(result, b) {
panic("result != input")
// Result is in the format "t: <time>\n", so strip off the trailing
// newline and convert timeHolder.T to the expected format. If time is
// zero, the value is marshaled to "null".
resultStr := strings.TrimSpace(string(result[:]))
var inputStr string
if timeHolder.T.Time.IsZero() {
inputStr = "t: null"
} else {
inputStr = fmt.Sprintf("t: %s", timeHolder.T.Time)
}
if resultStr != inputStr {
panic(fmt.Sprintf("result(%v) != input(%v)", resultStr, inputStr))
}
return 1
}