From 361a7649cb1a938645ff507a743d43ef0ed04f74 Mon Sep 17 00:00:00 2001 From: Shovan Maity Date: Thu, 14 Mar 2024 12:02:36 +0530 Subject: [PATCH 1/2] add fuzz test for remove string utility function Signed-off-by: Shovan Maity --- go.mod | 1 + go.sum | 2 ++ pkg/utils/utils.go | 3 ++ pkg/utils/utils_fuzz_test.go | 60 ++++++++++++++++++++++++++++++++++++ 4 files changed, 66 insertions(+) diff --git a/go.mod b/go.mod index bba7e644..cff37877 100644 --- a/go.mod +++ b/go.mod @@ -20,6 +20,7 @@ require ( ) require ( + github.com/AdaLogics/go-fuzz-headers v0.0.0-20230811130428-ced1acdcaa24 github.com/google/martian v2.1.0+incompatible github.com/onsi/ginkgo v1.16.4 github.com/onsi/gomega v1.15.0 diff --git a/go.sum b/go.sum index ce22b414..feecb56a 100644 --- a/go.sum +++ b/go.sum @@ -43,6 +43,8 @@ cloud.google.com/go/storage v1.8.0/go.mod h1:Wv1Oy7z6Yz3DshWRJFhqM/UCfaWIRTdp0RX cloud.google.com/go/storage v1.10.0/go.mod h1:FLPqc6j+Ki4BU591ie1oL6qBQGu2Bl/tZ9ullr3+Kg0= contrib.go.opencensus.io/exporter/ocagent v0.6.0/go.mod h1:zmKjrJcdo0aYcVS7bmEeSEBLPA9YJp5bjrofdU3pIXs= dmitri.shuralyov.com/gpu/mtl v0.0.0-20190408044501-666a987793e9/go.mod h1:H6x//7gZCb22OMCxBHrMx7a5I7Hp++hsVxbQ4BYO7hU= +github.com/AdaLogics/go-fuzz-headers v0.0.0-20230811130428-ced1acdcaa24 h1:bvDV9vkmnHYOMsOr4WLk+Vo07yKIzd94sVoIqshQ4bU= +github.com/AdaLogics/go-fuzz-headers v0.0.0-20230811130428-ced1acdcaa24/go.mod h1:8o94RPi1/7XTJvwPpRSzSUedZrtlirdB3r9Z20bi2f8= github.com/Azure/azure-pipeline-go v0.2.1/go.mod h1:UGSo8XybXnIGZ3epmeBw7Jdz+HiUVpqIlpz/HKHylF4= github.com/Azure/azure-pipeline-go v0.2.2/go.mod h1:4rQ/NZncSvGqNkkOsNpOU1tgoNuIlp9AfUH5G1tvCHc= github.com/Azure/azure-sdk-for-go v16.2.1+incompatible/go.mod h1:9XXNKU+eRnpl9moKnB4QOLf1HestfXbmab5FXxiDBjc= diff --git a/pkg/utils/utils.go b/pkg/utils/utils.go index 29ba454a..b44de0ce 100644 --- a/pkg/utils/utils.go +++ b/pkg/utils/utils.go @@ -22,6 +22,9 @@ import ( // RemoveString removes a particular string from a slice of strings func RemoveString(slice []string, s string) (result []string) { + if len(slice) == 0 { + return + } for _, item := range slice { if item == s { continue diff --git a/pkg/utils/utils_fuzz_test.go b/pkg/utils/utils_fuzz_test.go index 8a5dc166..f7ebaa6b 100644 --- a/pkg/utils/utils_fuzz_test.go +++ b/pkg/utils/utils_fuzz_test.go @@ -17,8 +17,11 @@ limitations under the License. package utils import ( + "math/rand" "testing" + "unicode" + fuzzheaders "github.com/AdaLogics/go-fuzz-headers" "github.com/stretchr/testify/assert" v1 "k8s.io/api/core/v1" ) @@ -55,3 +58,60 @@ func FuzzSetEnv(f *testing.F) { } }) } + +func FuzzRemoveString(f *testing.F) { + f.Fuzz(func(t *testing.T, extra string, data []byte) { + consumer := fuzzheaders.NewConsumer(data) + testInput := &struct { + Data map[string]int + }{} + err := consumer.GenerateStruct(testInput) + if err != nil { + return + } + max := len(testInput.Data) - 1 + if max < 0 { + max = 0 + } + randomNumber := func(min, max int) int { + if max == 0 { + return 0 + } + return rand.Intn(max-min) + min + }(0, max) + index := 0 + full := make([]string, 0) + exclude := "" + result := make([]string, 0) + for k := range testInput.Data { + if k == "" { + continue + } + if !func() bool { + for _, r := range k { + if !unicode.IsLetter(r) { + return false + } + } + return true + }() { + continue + } + full = append(full, k) + if index == randomNumber { + exclude = k + } + if index != randomNumber { + result = append(result, k) + } + } + if exclude != "" { + return + } + got := RemoveString(full, exclude) + if got == nil { + got = make([]string, 0) + } + assert.Equal(t, result, got) + }) +} From fe0bf6332821a790b0000acfdf906adaa0ef4c80 Mon Sep 17 00:00:00 2001 From: Shovan Maity Date: Thu, 4 Apr 2024 11:36:07 +0530 Subject: [PATCH 2/2] Update golang ci version Signed-off-by: Shovan Maity --- .github/workflows/build.yml | 2 +- .github/workflows/push.yml | 2 +- .github/workflows/release.yml | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 37bbb913..46b5a3dc 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -22,7 +22,7 @@ jobs: run: make gofmt-check - name: golangci-lint - uses: reviewdog/action-golangci-lint@v1 + uses: reviewdog/action-golangci-lint@v2 - name: unused-package check run: make unused-package-check diff --git a/.github/workflows/push.yml b/.github/workflows/push.yml index adeb434f..626ac8de 100644 --- a/.github/workflows/push.yml +++ b/.github/workflows/push.yml @@ -24,7 +24,7 @@ jobs: run: make gofmt-check - name: golangci-lint - uses: reviewdog/action-golangci-lint@v1 + uses: reviewdog/action-golangci-lint@v2 - name: unused-package check run: make unused-package-check diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index e06b5832..be320ce1 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -22,7 +22,7 @@ jobs: run: make gofmt-check - name: golangci-lint - uses: reviewdog/action-golangci-lint@v1 + uses: reviewdog/action-golangci-lint@v2 - name: unused-package check run: make unused-package-check