@@ -20,7 +20,6 @@ import (
20
20
"bytes"
21
21
"fmt"
22
22
"net/http"
23
- "os"
24
23
"regexp"
25
24
"strconv"
26
25
"strings"
@@ -31,19 +30,19 @@ import (
31
30
"github.com/hashicorp/go-cleanhttp"
32
31
)
33
32
34
- func ProcessRequests (t * testing.T , data []byte , addr string , methods ... string ) {
35
- t .Helper ()
33
+ func ProcessRequests (tb * testing.T , data []byte , addr string , methods ... string ) {
34
+ tb .Helper ()
36
35
37
36
if len (methods ) == 0 {
38
- t .Fatalf ("no methods specified" )
37
+ tb .Fatalf ("no methods specified" )
39
38
}
40
39
for _ , method := range methods {
41
- ProcessRequest (t , data , addr , method )
40
+ ProcessRequest (tb , data , addr , method )
42
41
}
43
42
}
44
43
45
- func ProcessRequest (t testing.TB , data []byte , addr , method string ) {
46
- t .Helper ()
44
+ func ProcessRequest (tb testing.TB , data []byte , addr , method string ) {
45
+ tb .Helper ()
47
46
48
47
switch method {
49
48
case
@@ -57,26 +56,26 @@ func ProcessRequest(t testing.TB, data []byte, addr, method string) {
57
56
http .MethodOptions ,
58
57
http .MethodTrace :
59
58
60
- req := newFuzzRequest ().Fuzz (t , data , method , addr )
59
+ req := newFuzzRequest ().Fuzz (tb , data , method , addr )
61
60
defer req .Body .Close ()
62
61
63
- resp := fuzzHTTPRequest (t , req )
62
+ resp := fuzzHTTPRequest (tb , req )
64
63
if resp != nil {
65
64
if resp .StatusCode > 500 {
66
- t .Errorf ("resp: %v" , resp )
65
+ tb .Errorf ("resp: %v" , resp )
67
66
}
68
67
defer resp .Body .Close ()
69
68
}
70
69
default :
71
- t .Errorf ("Unsupported HTTP method: %s" , method )
70
+ tb .Errorf ("Unsupported HTTP method: %s" , method )
72
71
}
73
72
}
74
73
75
- func fuzzHTTPRequest (t testing.TB , fuzzReq * http.Request ) * http.Response {
76
- t .Helper ()
74
+ func fuzzHTTPRequest (tb testing.TB , fuzzReq * http.Request ) * http.Response {
75
+ tb .Helper ()
77
76
78
77
if fuzzReq == nil {
79
- t .Skip ("Skipping test because fuzzReq is nil" )
78
+ tb .Skip ("Skipping test because fuzzReq is nil" )
80
79
}
81
80
client := cleanhttp .DefaultClient ()
82
81
client .Timeout = time .Second
@@ -98,11 +97,11 @@ func fuzzHTTPRequest(t testing.TB, fuzzReq *http.Request) *http.Response {
98
97
return nil
99
98
}
100
99
101
- t .Logf ("fuzzing request, %s, %s" , fuzzReq .Method , fuzzReq .URL )
100
+ tb .Logf ("fuzzing request, %s, %s" , fuzzReq .Method , fuzzReq .URL )
102
101
103
102
resp , err := client .Do (fuzzReq )
104
103
if err != nil && ! strings .Contains (err .Error (), "checkRedirect disabled for test" ) {
105
- t .Logf ("err: %s" , err )
104
+ tb .Logf ("err: %s" , err )
106
105
}
107
106
108
107
return resp
@@ -114,14 +113,14 @@ func newFuzzRequest() *fuzzRequest {
114
113
return & fuzzRequest {}
115
114
}
116
115
117
- func (s * fuzzRequest ) Fuzz (t testing.TB , data []byte , method , addr string ) * http.Request {
118
- t .Helper ()
116
+ func (s * fuzzRequest ) Fuzz (tb testing.TB , data []byte , method , addr string ) * http.Request {
117
+ tb .Helper ()
119
118
120
119
bodyReader := bytes .NewBuffer (data )
121
120
122
121
req , err := http .NewRequest (method , addr , bodyReader )
123
122
if err != nil {
124
- t .Skipf ("Skipping test: not enough data for fuzzing: %s" , err .Error ())
123
+ tb .Skipf ("Skipping test: not enough data for fuzzing: %s" , err .Error ())
125
124
}
126
125
127
126
// Get the address of the local listener in order to attach it to an Origin header.
@@ -130,10 +129,13 @@ func (s *fuzzRequest) Fuzz(t testing.TB, data []byte, method, addr string) *http
130
129
131
130
fuzzConsumer := fuzz .NewConsumer (data )
132
131
var headersMap map [string ]string
133
- fuzzConsumer .FuzzMap (& headersMap )
132
+ err = fuzzConsumer .FuzzMap (& headersMap )
133
+ if err != nil {
134
+ tb .Skipf ("Skipping test: not enough data for fuzzing: %s" , err .Error ())
135
+ }
134
136
135
137
for k , v := range headersMap {
136
- for i := 0 ; i < len (v ); i ++ {
138
+ for range len (v ) {
137
139
req .Header .Add (k , v )
138
140
}
139
141
}
@@ -144,16 +146,3 @@ func (s *fuzzRequest) Fuzz(t testing.TB, data []byte, method, addr string) *http
144
146
145
147
return req
146
148
}
147
-
148
- func GetPortFromEnv (env string , defaultPort int ) (port int , err error ) {
149
- portEnv := os .Getenv (env )
150
- if portEnv == "" {
151
- return defaultPort , nil
152
- }
153
-
154
- port , err = strconv .Atoi (portEnv )
155
- if err != nil {
156
- return 0 , fmt .Errorf ("failed to parse port env var %s: %w" , env , err )
157
- }
158
- return port , nil
159
- }
0 commit comments