@@ -17,8 +17,11 @@ limitations under the License.
1717package utils
1818
1919import (
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