-
Notifications
You must be signed in to change notification settings - Fork 128
/
shell2http_test.go
304 lines (265 loc) · 7.67 KB
/
shell2http_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
package main
import (
"fmt"
"io"
"io/ioutil"
"net"
"net/http"
"os"
"reflect"
"strings"
"testing"
"time"
)
func Test_parseCGIHeaders(t *testing.T) {
data := []struct {
in string
out string
headers map[string]string
}{
{
in: "Some text",
out: "Some text",
headers: map[string]string{},
},
{
in: "Location: url\n\nSome text",
out: "Some text",
headers: map[string]string{"Location": "url"},
},
{
in: "Location: url\n\n",
out: "",
headers: map[string]string{"Location": "url"},
},
{
in: "Location: url\nX-Name: x-value\n\nSome text",
out: "Some text",
headers: map[string]string{"Location": "url", "X-Name": "x-value"},
},
{
in: "Some text\nText\n\ntext",
out: "Some text\nText\n\ntext",
headers: map[string]string{},
},
{
in: "Some text\nText: value in text\n\ntext",
out: "Some text\nText: value in text\n\ntext",
headers: map[string]string{},
},
{
in: "Text::::\n\ntext",
out: "text",
headers: map[string]string{"Text": ":::"},
},
{
in: "Text: :::\n\ntext",
out: "text",
headers: map[string]string{"Text": ":::"},
},
{
in: "Text: \n\ntext",
out: "Text: \n\ntext",
headers: map[string]string{},
},
{
in: "Header: value\nText: \n\ntext",
out: "Header: value\nText: \n\ntext",
headers: map[string]string{},
},
}
for i, item := range data {
out, headers := parseCGIHeaders(item.in)
if !reflect.DeepEqual(item.headers, headers) || item.out != out {
t.Errorf("%d:\nexpected: %s / %#v\nreal : %s / %#v", i, item.out, item.headers, out, headers)
}
}
}
func Test_getShellAndParams(t *testing.T) {
shell, params, err := getShellAndParams("ls", Config{shell: "sh", defaultShell: "sh", defaultShOpt: "-c"})
if shell != "sh" || !reflect.DeepEqual(params, []string{"-c", "ls"}) || err != nil {
t.Errorf("1. getShellAndParams() failed")
}
shell, params, err = getShellAndParams("ls", Config{shell: "bash", defaultShell: "sh", defaultShOpt: "-c"})
if shell != "bash" || !reflect.DeepEqual(params, []string{"-c", "ls"}) || err != nil {
t.Errorf("3. getShellAndParams() failed")
}
shell, params, err = getShellAndParams("ls -l -a", Config{shell: "", defaultShell: "sh", defaultShOpt: "-c"})
if shell != "ls" || !reflect.DeepEqual(params, []string{"-l", "-a"}) || err != nil {
t.Errorf("4. getShellAndParams() failed")
}
shell, params, err = getShellAndParams("ls -l 'a b'", Config{shell: "", defaultShell: "sh", defaultShOpt: "-c"})
if shell != "ls" || !reflect.DeepEqual(params, []string{"-l", "a b"}) || err != nil {
t.Errorf("5. getShellAndParams() failed")
}
_, _, err = getShellAndParams("ls '-l", Config{shell: "", defaultShell: "sh", defaultShOpt: "-c"})
if err == nil {
t.Errorf("6. getShellAndParams() failed")
}
}
func Test_getShellAndParams_windows(t *testing.T) {
shell, params, err := getShellAndParams("ls", Config{shell: "cmd", defaultShell: "cmd", defaultShOpt: "/C"})
if shell != "cmd" || !reflect.DeepEqual(params, []string{"/C", "ls"}) || err != nil {
t.Errorf("2. getShellAndParams() failed")
}
}
func httpRequest(method string, url string, postData string) ([]byte, error) {
var postDataReader io.Reader
if method == "POST" && len(postData) > 0 {
postDataReader = strings.NewReader(postData)
}
request, err := http.NewRequest(method, url, postDataReader)
if err != nil {
return nil, err
}
request.Header.Set("X-Real-Ip", "127.0.0.1")
client := &http.Client{}
res, err := client.Do(request)
if err != nil {
return nil, err
}
body, err := ioutil.ReadAll(res.Body)
if err != nil {
return nil, err
}
err = res.Body.Close()
if err != nil {
return nil, err
}
return body, nil
}
func getFreePort(t *testing.T) string {
listen, _ := net.Listen("tcp", ":0")
parts := strings.Split(listen.Addr().String(), ":")
err := listen.Close()
if err != nil {
t.Errorf("getFreePort() failed")
}
return parts[len(parts)-1]
}
func testHTTP(t *testing.T, method, url, postData string, fn func(body string) bool, message string) {
res, err := httpRequest(method, url, postData)
if err != nil {
t.Errorf("%s, get %s failed: %s", message, url, err)
}
if !fn(string(res)) {
t.Errorf("%s failed", message)
}
}
func Test_main(t *testing.T) {
port := getFreePort(t)
os.Args = []string{"shell2http",
"-add-exit",
"-cache=1",
"-cgi",
// "-export-all-vars",
"-export-vars=HOME",
"-one-thread",
"-shell=",
"-log=/dev/null",
"-port=" + port,
"/echo", "echo 123",
"/form", "echo var=$v_var",
"/error", "/ not exists cmd",
"/post", "cat",
"/redirect", `echo "Location: /` + "\n" + `"`,
}
go main()
time.Sleep(100 * time.Millisecond) // wait for up http server
// hide stderr
oldStderr := os.Stderr // keep backup of the real stderr
newStderr, err := os.Open("/dev/null")
if err != nil {
t.Errorf("open /dev/null: %s", err)
}
os.Stderr = newStderr
defer func() {
os.Stderr = oldStderr
err := newStderr.Close()
if err != nil {
t.Errorf("Stderr Close failed: %s", err)
}
}()
testHTTP(t, "GET", "http://localhost:"+port+"/", "",
func(res string) bool { return len(res) > 0 && strings.HasPrefix(res, "<!DOCTYPE html>") },
"1. get /",
)
testHTTP(t, "GET", "http://localhost:"+port+"/echo", "",
func(res string) bool { return res == "123\n" },
"2. echo",
)
testHTTP(t, "GET", "http://localhost:"+port+"/echo", "",
func(res string) bool { return res == "123\n" },
"3. echo from cache",
)
testHTTP(t, "GET", "http://localhost:"+port+"/404", "",
func(res string) bool { return strings.HasPrefix(res, "404 page not found") },
"4. 404",
)
testHTTP(t, "GET", "http://localhost:"+port+"/error", "",
func(res string) bool { return strings.HasPrefix(res, "exec error:") },
"5. error",
)
testHTTP(t, "GET", "http://localhost:"+port+"/redirect", "",
func(res string) bool { return strings.HasPrefix(res, "<!DOCTYPE html>") },
"6. redirect",
)
testHTTP(t, "POST", "http://localhost:"+port+"/post", "X-header: value\n\ntext",
func(res string) bool { return strings.HasPrefix(res, "text") },
"7. POST",
)
}
func Test_errChain(t *testing.T) {
err := errChain()
if err != nil {
t.Errorf("1. errChain() empty failed")
}
err = errChain(func() error { return nil })
if err != nil {
t.Errorf("2. errChain() failed")
}
err = errChain(func() error { return nil }, func() error { return nil })
if err != nil {
t.Errorf("3. errChain() failed")
}
err = errChain(func() error { return fmt.Errorf("error") })
if err == nil {
t.Errorf("4. errChain() failed")
}
err = errChain(func() error { return nil }, func() error { return fmt.Errorf("error") })
if err == nil {
t.Errorf("5. errChain() failed")
}
var1 := false
err = errChain(func() error { return fmt.Errorf("error") }, func() error { var1 = true; return nil })
if err == nil || var1 {
t.Errorf("6. errChain() failed")
}
}
func Test_errChainAll(t *testing.T) {
err := errChainAll()
if err != nil {
t.Errorf("1. errChainAll() empty failed")
}
err = errChainAll(func() error { return nil })
if err != nil {
t.Errorf("2. errChainAll() failed")
}
err = errChainAll(func() error { return nil }, func() error { return nil })
if err != nil {
t.Errorf("3. errChainAll() failed")
}
err = errChainAll(func() error { return fmt.Errorf("error") })
if err == nil {
t.Errorf("4. errChainAll() failed")
}
err = errChainAll(func() error { return nil }, func() error { return fmt.Errorf("error") })
if err == nil {
t.Errorf("5. errChainAll() failed")
}
var1 := false
err = errChainAll(func() error { return fmt.Errorf("error") }, func() error { var1 = true; return nil })
if err == nil || !var1 {
t.Errorf("6. errChainAll() failed")
}
}