@@ -17,8 +17,11 @@ limitations under the License.
17
17
package utils
18
18
19
19
import (
20
+ "math/rand"
20
21
"testing"
22
+ "unicode"
21
23
24
+ fuzzheaders "github.com/AdaLogics/go-fuzz-headers"
22
25
"github.com/stretchr/testify/assert"
23
26
v1 "k8s.io/api/core/v1"
24
27
)
@@ -55,3 +58,60 @@ func FuzzSetEnv(f *testing.F) {
55
58
}
56
59
})
57
60
}
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