summaryrefslogtreecommitdiff
path: root/modules/caddyhttp/reverseproxy/fastcgi/client_test.go
blob: ef3474d40b8365d5cd2191370604c46b5b32054d (plain)
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
// Copyright 2015 Matthew Holt and The Caddy Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

// NOTE: These tests were adapted from the original
// repository from which this package was forked.
// The tests are slow (~10s) and in dire need of rewriting.
// As such, the tests have been disabled to speed up
// automated builds until they can be properly written.

package fastcgi

import (
	"bytes"
	"crypto/md5"
	"encoding/binary"
	"fmt"
	"io"
	"log"
	"math/rand"
	"net"
	"net/http"
	"net/http/fcgi"
	"net/url"
	"os"
	"path/filepath"
	"strconv"
	"strings"
	"testing"
	"time"
)

// test fcgi protocol includes:
// Get, Post, Post in multipart/form-data, and Post with files
// each key should be the md5 of the value or the file uploaded
// specify remote fcgi responder ip:port to test with php
// test failed if the remote fcgi(script) failed md5 verification
// and output "FAILED" in response
const (
	scriptFile = "/tank/www/fcgic_test.php"
	//ipPort = "remote-php-serv:59000"
	ipPort = "127.0.0.1:59000"
)

var globalt *testing.T

type FastCGIServer struct{}

func (s FastCGIServer) ServeHTTP(resp http.ResponseWriter, req *http.Request) {

	if err := req.ParseMultipartForm(100000000); err != nil {
		log.Printf("[ERROR] failed to parse: %v", err)
	}

	stat := "PASSED"
	fmt.Fprintln(resp, "-")
	fileNum := 0
	{
		length := 0
		for k0, v0 := range req.Form {
			h := md5.New()
			_, _ = io.WriteString(h, v0[0])
			_md5 := fmt.Sprintf("%x", h.Sum(nil))

			length += len(k0)
			length += len(v0[0])

			// echo error when key != _md5(val)
			if _md5 != k0 {
				fmt.Fprintln(resp, "server:err ", _md5, k0)
				stat = "FAILED"
			}
		}
		if req.MultipartForm != nil {
			fileNum = len(req.MultipartForm.File)
			for kn, fns := range req.MultipartForm.File {
				//fmt.Fprintln(resp, "server:filekey ", kn )
				length += len(kn)
				for _, f := range fns {
					fd, err := f.Open()
					if err != nil {
						log.Println("server:", err)
						return
					}
					h := md5.New()
					l0, err := io.Copy(h, fd)
					if err != nil {
						log.Println(err)
						return
					}
					length += int(l0)
					defer fd.Close()
					md5 := fmt.Sprintf("%x", h.Sum(nil))
					//fmt.Fprintln(resp, "server:filemd5 ", md5 )

					if kn != md5 {
						fmt.Fprintln(resp, "server:err ", md5, kn)
						stat = "FAILED"
					}
					//fmt.Fprintln(resp, "server:filename ", f.Filename )
				}
			}
		}

		fmt.Fprintln(resp, "server:got data length", length)
	}
	fmt.Fprintln(resp, "-"+stat+"-POST(", len(req.Form), ")-FILE(", fileNum, ")--")
}

func sendFcgi(reqType int, fcgiParams map[string]string, data []byte, posts map[string]string, files map[string]string) (content []byte) {
	fcgi, err := Dial("tcp", ipPort)
	if err != nil {
		log.Println("err:", err)
		return
	}

	length := 0

	var resp *http.Response
	switch reqType {
	case 0:
		if len(data) > 0 {
			length = len(data)
			rd := bytes.NewReader(data)
			resp, err = fcgi.Post(fcgiParams, "", "", rd, int64(rd.Len()))
		} else if len(posts) > 0 {
			values := url.Values{}
			for k, v := range posts {
				values.Set(k, v)
				length += len(k) + 2 + len(v)
			}
			resp, err = fcgi.PostForm(fcgiParams, values)
		} else {
			rd := bytes.NewReader(data)
			resp, err = fcgi.Get(fcgiParams, rd, int64(rd.Len()))
		}

	default:
		values := url.Values{}
		for k, v := range posts {
			values.Set(k, v)
			length += len(k) + 2 + len(v)
		}

		for k, v := range files {
			fi, _ := os.Lstat(v)
			length += len(k) + int(fi.Size())
		}
		resp, err = fcgi.PostFile(fcgiParams, values, files)
	}

	if err != nil {
		log.Println("err:", err)
		return
	}

	defer resp.Body.Close()
	content, _ = io.ReadAll(resp.Body)

	log.Println("c: send data length ≈", length, string(content))
	fcgi.Close()
	time.Sleep(1 * time.Second)

	if bytes.Contains(content, []byte("FAILED")) {
		globalt.Error("Server return failed message")
	}

	return
}

func generateRandFile(size int) (p string, m string) {

	p = filepath.Join(os.TempDir(), "fcgict"+strconv.Itoa(rand.Int()))

	// open output file
	fo, err := os.Create(p)
	if err != nil {
		panic(err)
	}
	// close fo on exit and check for its returned error
	defer func() {
		if err := fo.Close(); err != nil {
			panic(err)
		}
	}()

	h := md5.New()
	for i := 0; i < size/16; i++ {
		buf := make([]byte, 16)
		binary.PutVarint(buf, rand.Int63())
		if _, err := fo.Write(buf); err != nil {
			log.Printf("[ERROR] failed to write buffer: %v\n", err)
		}
		if _, err := h.Write(buf); err != nil {
			log.Printf("[ERROR] failed to write buffer: %v\n", err)
		}
	}
	m = fmt.Sprintf("%x", h.Sum(nil))
	return
}

func DisabledTest(t *testing.T) {
	// TODO: test chunked reader
	globalt = t

	rand.Seed(time.Now().UTC().UnixNano())

	// server
	go func() {
		listener, err := net.Listen("tcp", ipPort)
		if err != nil {
			log.Println("listener creation failed: ", err)
		}

		srv := new(FastCGIServer)
		if err := fcgi.Serve(listener, srv); err != nil {
			log.Print("[ERROR] failed to start server: ", err)
		}
	}()

	time.Sleep(1 * time.Second)

	// init
	fcgiParams := make(map[string]string)
	fcgiParams["REQUEST_METHOD"] = "GET"
	fcgiParams["SERVER_PROTOCOL"] = "HTTP/1.1"
	//fcgi_params["GATEWAY_INTERFACE"] = "CGI/1.1"
	fcgiParams["SCRIPT_FILENAME"] = scriptFile

	// simple GET
	log.Println("test:", "get")
	sendFcgi(0, fcgiParams, nil, nil, nil)

	// simple post data
	log.Println("test:", "post")
	sendFcgi(0, fcgiParams, []byte("c4ca4238a0b923820dcc509a6f75849b=1&7b8b965ad4bca0e41ab51de7b31363a1=n"), nil, nil)

	log.Println("test:", "post data (more than 60KB)")
	data := ""
	for i := 0x00; i < 0xff; i++ {
		v0 := strings.Repeat(fmt.Sprint(i), 256)
		h := md5.New()
		_, _ = io.WriteString(h, v0)
		k0 := fmt.Sprintf("%x", h.Sum(nil))
		data += k0 + "=" + url.QueryEscape(v0) + "&"
	}
	sendFcgi(0, fcgiParams, []byte(data), nil, nil)

	log.Println("test:", "post form (use url.Values)")
	p0 := make(map[string]string, 1)
	p0["c4ca4238a0b923820dcc509a6f75849b"] = "1"
	p0["7b8b965ad4bca0e41ab51de7b31363a1"] = "n"
	sendFcgi(1, fcgiParams, nil, p0, nil)

	log.Println("test:", "post forms (256 keys, more than 1MB)")
	p1 := make(map[string]string, 1)
	for i := 0x00; i < 0xff; i++ {
		v0 := strings.Repeat(fmt.Sprint(i), 4096)
		h := md5.New()
		_, _ = io.WriteString(h, v0)
		k0 := fmt.Sprintf("%x", h.Sum(nil))
		p1[k0] = v0
	}
	sendFcgi(1, fcgiParams, nil, p1, nil)

	log.Println("test:", "post file (1 file, 500KB)) ")
	f0 := make(map[string]string, 1)
	path0, m0 := generateRandFile(500000)
	f0[m0] = path0
	sendFcgi(1, fcgiParams, nil, p1, f0)

	log.Println("test:", "post multiple files (2 files, 5M each) and forms (256 keys, more than 1MB data")
	path1, m1 := generateRandFile(5000000)
	f0[m1] = path1
	sendFcgi(1, fcgiParams, nil, p1, f0)

	log.Println("test:", "post only files (2 files, 5M each)")
	sendFcgi(1, fcgiParams, nil, nil, f0)

	log.Println("test:", "post only 1 file")
	delete(f0, "m0")
	sendFcgi(1, fcgiParams, nil, nil, f0)

	if err := os.Remove(path0); err != nil {
		log.Println("[ERROR] failed to remove path: ", err)
	}
	if err := os.Remove(path1); err != nil {
		log.Println("[ERROR] failed to remove path: ", err)
	}
}