Skip to content

Commit dc17ee1

Browse files
authored
Merge pull request #488 from shovanmaity/fuz-2
add fuzz test for remove string utility function
2 parents f1afe83 + fe0bf63 commit dc17ee1

File tree

7 files changed

+69
-3
lines changed

7 files changed

+69
-3
lines changed

.github/workflows/build.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ jobs:
2222
run: make gofmt-check
2323

2424
- name: golangci-lint
25-
uses: reviewdog/action-golangci-lint@v1
25+
uses: reviewdog/action-golangci-lint@v2
2626

2727
- name: unused-package check
2828
run: make unused-package-check

.github/workflows/push.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ jobs:
2424
run: make gofmt-check
2525

2626
- name: golangci-lint
27-
uses: reviewdog/action-golangci-lint@v1
27+
uses: reviewdog/action-golangci-lint@v2
2828

2929
- name: unused-package check
3030
run: make unused-package-check

.github/workflows/release.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ jobs:
2222
run: make gofmt-check
2323

2424
- name: golangci-lint
25-
uses: reviewdog/action-golangci-lint@v1
25+
uses: reviewdog/action-golangci-lint@v2
2626

2727
- name: unused-package check
2828
run: make unused-package-check

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ require (
2020
)
2121

2222
require (
23+
github.com/AdaLogics/go-fuzz-headers v0.0.0-20230811130428-ced1acdcaa24
2324
github.com/google/martian v2.1.0+incompatible
2425
github.com/onsi/ginkgo v1.16.4
2526
github.com/onsi/gomega v1.15.0

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@ cloud.google.com/go/storage v1.8.0/go.mod h1:Wv1Oy7z6Yz3DshWRJFhqM/UCfaWIRTdp0RX
4343
cloud.google.com/go/storage v1.10.0/go.mod h1:FLPqc6j+Ki4BU591ie1oL6qBQGu2Bl/tZ9ullr3+Kg0=
4444
contrib.go.opencensus.io/exporter/ocagent v0.6.0/go.mod h1:zmKjrJcdo0aYcVS7bmEeSEBLPA9YJp5bjrofdU3pIXs=
4545
dmitri.shuralyov.com/gpu/mtl v0.0.0-20190408044501-666a987793e9/go.mod h1:H6x//7gZCb22OMCxBHrMx7a5I7Hp++hsVxbQ4BYO7hU=
46+
github.com/AdaLogics/go-fuzz-headers v0.0.0-20230811130428-ced1acdcaa24 h1:bvDV9vkmnHYOMsOr4WLk+Vo07yKIzd94sVoIqshQ4bU=
47+
github.com/AdaLogics/go-fuzz-headers v0.0.0-20230811130428-ced1acdcaa24/go.mod h1:8o94RPi1/7XTJvwPpRSzSUedZrtlirdB3r9Z20bi2f8=
4648
github.com/Azure/azure-pipeline-go v0.2.1/go.mod h1:UGSo8XybXnIGZ3epmeBw7Jdz+HiUVpqIlpz/HKHylF4=
4749
github.com/Azure/azure-pipeline-go v0.2.2/go.mod h1:4rQ/NZncSvGqNkkOsNpOU1tgoNuIlp9AfUH5G1tvCHc=
4850
github.com/Azure/azure-sdk-for-go v16.2.1+incompatible/go.mod h1:9XXNKU+eRnpl9moKnB4QOLf1HestfXbmab5FXxiDBjc=

pkg/utils/utils.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@ import (
2222

2323
// RemoveString removes a particular string from a slice of strings
2424
func RemoveString(slice []string, s string) (result []string) {
25+
if len(slice) == 0 {
26+
return
27+
}
2528
for _, item := range slice {
2629
if item == s {
2730
continue

pkg/utils/utils_fuzz_test.go

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,11 @@ limitations under the License.
1717
package utils
1818

1919
import (
20+
"math/rand"
2021
"testing"
22+
"unicode"
2123

24+
fuzzheaders "github.com/AdaLogics/go-fuzz-headers"
2225
"github.com/stretchr/testify/assert"
2326
v1 "k8s.io/api/core/v1"
2427
)
@@ -55,3 +58,60 @@ func FuzzSetEnv(f *testing.F) {
5558
}
5659
})
5760
}
61+
62+
func FuzzRemoveString(f *testing.F) {
63+
f.Fuzz(func(t *testing.T, extra string, data []byte) {
64+
consumer := fuzzheaders.NewConsumer(data)
65+
testInput := &struct {
66+
Data map[string]int
67+
}{}
68+
err := consumer.GenerateStruct(testInput)
69+
if err != nil {
70+
return
71+
}
72+
max := len(testInput.Data) - 1
73+
if max < 0 {
74+
max = 0
75+
}
76+
randomNumber := func(min, max int) int {
77+
if max == 0 {
78+
return 0
79+
}
80+
return rand.Intn(max-min) + min
81+
}(0, max)
82+
index := 0
83+
full := make([]string, 0)
84+
exclude := ""
85+
result := make([]string, 0)
86+
for k := range testInput.Data {
87+
if k == "" {
88+
continue
89+
}
90+
if !func() bool {
91+
for _, r := range k {
92+
if !unicode.IsLetter(r) {
93+
return false
94+
}
95+
}
96+
return true
97+
}() {
98+
continue
99+
}
100+
full = append(full, k)
101+
if index == randomNumber {
102+
exclude = k
103+
}
104+
if index != randomNumber {
105+
result = append(result, k)
106+
}
107+
}
108+
if exclude != "" {
109+
return
110+
}
111+
got := RemoveString(full, exclude)
112+
if got == nil {
113+
got = make([]string, 0)
114+
}
115+
assert.Equal(t, result, got)
116+
})
117+
}

0 commit comments

Comments
 (0)