From 5a1f13d1238e0b712be24f77fb0ef6eb51458f30 Mon Sep 17 00:00:00 2001 From: AdamKorcz Date: Wed, 2 Dec 2020 15:08:03 +0000 Subject: [PATCH] Added fuzzer to integrate with OSS-fuzz --- hack/.packages | 1 + tests/fuzz/BUILD.bazel | 9 +++++++++ tests/fuzz/build.sh | 20 ++++++++++++++++++++ tests/fuzz/fuzz.go | 40 ++++++++++++++++++++++++++++++++++++++++ 4 files changed, 70 insertions(+) create mode 100644 tests/fuzz/BUILD.bazel create mode 100755 tests/fuzz/build.sh create mode 100644 tests/fuzz/fuzz.go diff --git a/hack/.packages b/hack/.packages index 4aaa61f5b1..e62a728b26 100644 --- a/hack/.packages +++ b/hack/.packages @@ -167,6 +167,7 @@ k8s.io/kops/protokube/pkg/hostmount k8s.io/kops/protokube/pkg/protokube k8s.io/kops/protokube/tests/integration/build_etcd_manifest k8s.io/kops/tests/codecs +k8s.io/kops/tests/fuzz k8s.io/kops/tests/integration/channel k8s.io/kops/tests/integration/conversion k8s.io/kops/upup/models diff --git a/tests/fuzz/BUILD.bazel b/tests/fuzz/BUILD.bazel new file mode 100644 index 0000000000..f4845cc90c --- /dev/null +++ b/tests/fuzz/BUILD.bazel @@ -0,0 +1,9 @@ +load("@io_bazel_rules_go//go:def.bzl", "go_library") + +go_library( + name = "go_default_library", + srcs = ["fuzz.go"], + importpath = "k8s.io/kops/tests/fuzz", + visibility = ["//visibility:public"], + deps = ["//pkg/jsonutils:go_default_library"], +) diff --git a/tests/fuzz/build.sh b/tests/fuzz/build.sh new file mode 100755 index 0000000000..69a6a66ba7 --- /dev/null +++ b/tests/fuzz/build.sh @@ -0,0 +1,20 @@ +#!/usr/bin/env bash + +# Copyright 2020 The Kubernetes Authors. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +cp "$SRC"/AFL/dictionaries/json.dict "$OUT"/fuzz_write_token.dict +zip "$OUT"/fuzz_write_token_seed_corpus.zip "$SRC"/go-fuzz-corpus/json/corpus/* + +compile_go_fuzzer k8s.io/kops/tests/fuzz FuzzWriteToken fuzz_write_token diff --git a/tests/fuzz/fuzz.go b/tests/fuzz/fuzz.go new file mode 100644 index 0000000000..74d29f69d1 --- /dev/null +++ b/tests/fuzz/fuzz.go @@ -0,0 +1,40 @@ +/* +Copyright 2020 The Kubernetes Authors. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package fuzz + +import ( + "bytes" + "encoding/json" + "strings" + + "k8s.io/kops/pkg/jsonutils" +) + +func FuzzWriteToken(data []byte) int { + var buf bytes.Buffer + out := jsonutils.NewJSONStreamWriter(&buf) + in := json.NewDecoder(strings.NewReader(string(data))) + token, err := in.Token() + if err != nil { + return -1 + } + err = out.WriteToken(token) + if err != nil { + return 0 + } + return 1 +}