1
1
package common
2
2
3
3
import (
4
- "github.com/stretchr/testify/assert"
5
4
"regexp"
6
5
"strconv"
7
6
"strings"
8
7
"testing"
8
+
9
+ fuzz "github.com/AdaLogics/go-fuzz-headers"
10
+ "github.com/litmuschaos/litmus-go/pkg/types"
11
+ "github.com/stretchr/testify/assert"
12
+ "github.com/stretchr/testify/require"
9
13
)
10
14
11
15
func FuzzRandomInterval (f * testing.F ) {
@@ -24,17 +28,144 @@ func FuzzRandomInterval(f *testing.F) {
24
28
f .Fuzz (func (t * testing.T , interval string ) {
25
29
re := regexp .MustCompile (`^\d+(-\d+)?$` )
26
30
intervals := strings .Split (interval , "-" )
27
- err := RandomInterval (interval )
31
+ skip := false
32
+ if re .MatchString (interval ) {
33
+ a , _ := strconv .Atoi (intervals [0 ])
34
+ if len (intervals ) == 2 {
35
+ b , _ := strconv .Atoi (intervals [1 ])
36
+ if a > 5 || (b - a ) > 5 || (b - a ) < 1 {
37
+ skip = true
38
+ }
39
+ } else if len (intervals ) == 1 {
40
+ if a > 5 || a < 1 {
41
+ skip = true
42
+ }
43
+ }
44
+ }
45
+
46
+ if ! skip {
47
+ err := RandomInterval (interval )
48
+ if re .MatchString (interval ) == false {
49
+ assert .Error (t , err , "{\" errorCode\" :\" GENERIC_ERROR\" ,\" reason\" :\" could not parse CHAOS_INTERVAL env, bad input\" }" )
50
+ return
51
+ }
52
+
53
+ num , _ := strconv .Atoi (intervals [0 ])
54
+ if num < 1 && err != nil {
55
+ assert .Error (t , err , "{\" errorCode\" :\" GENERIC_ERROR\" ,\" reason\" :\" invalid CHAOS_INTERVAL env value, value below lower limit\" }" )
56
+ return
57
+ } else if num > 1 && err != nil {
58
+ t .Errorf ("Unexpected Error: %v" , err )
59
+ }
60
+ }
61
+ })
62
+ }
63
+
64
+ func FuzzGetContainerNames (f * testing.F ) {
65
+
66
+ f .Fuzz (func (t * testing.T , data []byte ) {
67
+ fuzzConsumer := fuzz .NewConsumer (data )
68
+ targetStruct := & struct {
69
+ chaosDetails types.ChaosDetails
70
+ }{}
71
+ err := fuzzConsumer .GenerateStruct (targetStruct )
72
+ if err != nil {
73
+ return
74
+ }
75
+ names := GetContainerNames (& targetStruct .chaosDetails )
76
+ require .Equal (t , len (names ), len (targetStruct .chaosDetails .SideCar )+ 1 )
77
+ })
78
+ }
79
+
80
+ func FuzzGetSidecarVolumes (f * testing.F ) {
81
+
82
+ f .Fuzz (func (t * testing.T , data []byte ) {
83
+ fuzzConsumer := fuzz .NewConsumer (data )
84
+ targetStruct := & struct {
85
+ chaosDetails types.ChaosDetails
86
+ }{}
87
+ err := fuzzConsumer .GenerateStruct (targetStruct )
88
+ if err != nil {
89
+ return
90
+ }
91
+ volumes := GetSidecarVolumes (& targetStruct .chaosDetails )
92
+ var volCounts = 0
93
+ for _ , s := range targetStruct .chaosDetails .SideCar {
94
+ volCounts += len (s .Secrets )
95
+ }
96
+ require .Equal (t , len (volumes ), len (volumes ))
97
+ })
98
+ }
28
99
29
- if re .MatchString (interval ) == false {
30
- assert .Error (t , err , "{\" errorCode\" :\" GENERIC_ERROR\" ,\" reason\" :\" could not parse CHAOS_INTERVAL env, bad input\" }" )
100
+ func FuzzBuildSidecar (f * testing.F ) {
101
+
102
+ f .Fuzz (func (t * testing.T , data []byte ) {
103
+ fuzzConsumer := fuzz .NewConsumer (data )
104
+ targetStruct := & struct {
105
+ chaosDetails types.ChaosDetails
106
+ }{}
107
+ err := fuzzConsumer .GenerateStruct (targetStruct )
108
+ if err != nil {
109
+ return
110
+ }
111
+ containers := BuildSidecar (& targetStruct .chaosDetails )
112
+ require .Equal (t , len (containers ), len (targetStruct .chaosDetails .SideCar ))
113
+ })
114
+ }
115
+
116
+ func FuzzContains (f * testing.F ) {
117
+ f .Fuzz (func (t * testing.T , data []byte ) {
118
+ fuzzConsumer := fuzz .NewConsumer (data )
119
+ targetStruct := & struct {
120
+ val string
121
+ slice []string
122
+ }{}
123
+ err := fuzzConsumer .GenerateStruct (targetStruct )
124
+ if err != nil {
125
+ return
126
+ }
127
+ contains := Contains (targetStruct .val , targetStruct .slice )
128
+ for _ , s := range targetStruct .slice {
129
+ if s == targetStruct .val {
130
+ require .True (t , contains )
131
+ return
132
+ }
31
133
}
134
+ require .False (t , contains )
135
+ })
136
+ }
137
+
138
+ func FuzzSubStringExistsInSlice (f * testing.F ) {
139
+ f .Fuzz (func (t * testing.T , data []byte ) {
140
+ fuzzConsumer := fuzz .NewConsumer (data )
141
+ targetStruct := & struct {
142
+ val string
143
+ slice []string
144
+ }{}
145
+ err := fuzzConsumer .GenerateStruct (targetStruct )
146
+ if err != nil {
147
+ return
148
+ }
149
+ contains := SubStringExistsInSlice (targetStruct .val , targetStruct .slice )
150
+ for _ , s := range targetStruct .slice {
151
+ if strings .Contains (s , targetStruct .val ) {
152
+ require .True (t , contains )
153
+ return
154
+ }
155
+ }
156
+ require .False (t , contains )
157
+ })
158
+ }
159
+
160
+ func FuzzGetRandomSequence (f * testing.F ) {
161
+ f .Add ("random" )
32
162
33
- num , _ := strconv . Atoi ( intervals [ 0 ])
34
- if num < 1 && err != nil {
35
- assert . Error ( t , err , "{ \" errorCode \" : \" GENERIC_ERROR \" , \" reason \" : \" invalid CHAOS_INTERVAL env value, value below lower limit \" }" )
36
- } else if num > 1 && err != nil {
37
- t . Errorf ( "Unexpected Error: %v" , err )
163
+ f . Fuzz ( func ( t * testing. T , sequence string ) {
164
+ val := GetRandomSequence ( sequence )
165
+ if strings . ToLower ( sequence ) == "random" {
166
+ require . Contains ( t , [] string { "serial" , "parallel" }, val )
167
+ return
38
168
}
169
+ require .Equal (t , sequence , val )
39
170
})
40
171
}
0 commit comments