Skip to content

Commit

Permalink
Merge pull request #488 from shovanmaity/fuz-2
Browse files Browse the repository at this point in the history
add fuzz test for remove string utility function
  • Loading branch information
Saranya-jena authored Apr 4, 2024
2 parents f1afe83 + fe0bf63 commit dc17ee1
Show file tree
Hide file tree
Showing 7 changed files with 69 additions and 3 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/push.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down
3 changes: 3 additions & 0 deletions pkg/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
60 changes: 60 additions & 0 deletions pkg/utils/utils_fuzz_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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)
})
}

0 comments on commit dc17ee1

Please sign in to comment.