mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-07-23 11:50:44 +00:00
Merge pull request #84168 from BrendanSChang/fuzz
Add fuzz targets for Duration, MicroTime, and Time
This commit is contained in:
commit
9832418870
@ -6,6 +6,7 @@ go_library(
|
||||
importpath = "k8s.io/kubernetes/test/fuzz/yaml",
|
||||
visibility = ["//visibility:private"],
|
||||
deps = [
|
||||
"//staging/src/k8s.io/apimachinery/pkg/apis/meta/v1:go_default_library",
|
||||
"//vendor/gopkg.in/yaml.v2:go_default_library",
|
||||
"//vendor/sigs.k8s.io/yaml:go_default_library",
|
||||
],
|
||||
|
@ -20,10 +20,53 @@ limitations under the License.
|
||||
package yaml
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
|
||||
"gopkg.in/yaml.v2"
|
||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||
sigyaml "sigs.k8s.io/yaml"
|
||||
)
|
||||
|
||||
// FuzzDurationStrict is a fuzz target for strict-unmarshaling Duration defined
|
||||
// in "k8s.io/apimachinery/pkg/apis/meta/v1". This target also checks that the
|
||||
// unmarshaled result can be marshaled back to the input.
|
||||
func FuzzDurationStrict(b []byte) int {
|
||||
var durationHolder struct {
|
||||
D metav1.Duration `json:"d"`
|
||||
}
|
||||
if err := sigyaml.UnmarshalStrict(b, &durationHolder); err != nil {
|
||||
return 0
|
||||
}
|
||||
result, err := sigyaml.Marshal(&durationHolder)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
if !bytes.Equal(result, b) {
|
||||
panic("result != input")
|
||||
}
|
||||
return 1
|
||||
}
|
||||
|
||||
// FuzzMicroTimeStrict is a fuzz target for strict-unmarshaling MicroTime
|
||||
// defined in "k8s.io/apimachinery/pkg/apis/meta/v1". This target also checks
|
||||
// that the unmarshaled result can be marshaled back to the input.
|
||||
func FuzzMicroTimeStrict(b []byte) int {
|
||||
var microTimeHolder struct {
|
||||
T metav1.MicroTime `json:"t"`
|
||||
}
|
||||
if err := sigyaml.UnmarshalStrict(b, µTimeHolder); err != nil {
|
||||
return 0
|
||||
}
|
||||
result, err := sigyaml.Marshal(µTimeHolder)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
if !bytes.Equal(result, b) {
|
||||
panic("result != input")
|
||||
}
|
||||
return 1
|
||||
}
|
||||
|
||||
// FuzzSigYaml is a fuzz target for "sigs.k8s.io/yaml" unmarshaling.
|
||||
func FuzzSigYaml(b []byte) int {
|
||||
t := struct{}{}
|
||||
@ -38,6 +81,26 @@ func FuzzSigYaml(b []byte) int {
|
||||
return out
|
||||
}
|
||||
|
||||
// FuzzTime is a fuzz target for strict-unmarshaling Time defined in
|
||||
// "k8s.io/apimachinery/pkg/apis/meta/v1". This target also checks that the
|
||||
// unmarshaled result can be marshaled back to the input.
|
||||
func FuzzTime(b []byte) int {
|
||||
var timeHolder struct {
|
||||
T metav1.Time `json:"t"`
|
||||
}
|
||||
if err := sigyaml.UnmarshalStrict(b, &timeHolder); err != nil {
|
||||
return 0
|
||||
}
|
||||
result, err := sigyaml.Marshal(&timeHolder)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
if !bytes.Equal(result, b) {
|
||||
panic("result != input")
|
||||
}
|
||||
return 1
|
||||
}
|
||||
|
||||
// FuzzYamlV2 is a fuzz target for "gopkg.in/yaml.v2" unmarshaling.
|
||||
func FuzzYamlV2(b []byte) int {
|
||||
t := struct{}{}
|
||||
|
Loading…
Reference in New Issue
Block a user