git » gofer » next » tree

[next] / server / server_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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
package server

import (
	"fmt"
	"io"
	"net"
	"net/http"
	"net/http/httptest"
	"os"
	"path/filepath"
	"strings"
	"testing"
	"time"

	"blitiri.com.ar/go/gofer/config"
	"blitiri.com.ar/go/gofer/ipratelimit"
	"blitiri.com.ar/go/gofer/ratelimit"
	"blitiri.com.ar/go/gofer/reqlog"
	"blitiri.com.ar/go/log"
)

// WaitForHTTPServer waits 5 seconds for an HTTP server to start, and returns
// an error if it fails to do so.
// It does this by repeatedly querying the server until it either replies or
// times out.
func waitForHTTPServer(addr string) error {
	c := http.Client{
		Timeout: 100 * time.Millisecond,
	}

	deadline := time.Now().Add(5 * time.Second)
	tick := time.Tick(100 * time.Millisecond)

	for (<-tick).Before(deadline) {
		_, err := c.Get("http://" + addr + "/testpoke")
		if err == nil {
			return nil
		}
	}

	return fmt.Errorf("timed out")
}

// Get a free (TCP) port. This is hacky and not race-free, but it works well
// enough for testing purposes.
func getFreePort() string {
	l, _ := net.Listen("tcp", "localhost:0")
	defer l.Close()
	return l.Addr().String()
}

const backendResponse = "backend response\n"

// Addresses of the proxy under test (created by TestMain).
var (
	httpAddr string
	rawAddr  string
)

// startServer for testing. Returns raw addr, http addr, and the backend test
// server (which should be closed afterwards).
// Note it leaks goroutines, we're ok with this for testing.
func TestMain(m *testing.M) {
	backend := httptest.NewServer(
		http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
			fmt.Fprint(w, backendResponse)
		}))
	defer backend.Close()

	// We have two frontends: one raw and one http.
	rawAddr = getFreePort()
	httpAddr = getFreePort()

	log.Default.Level = log.Error

	pwd, _ := os.Getwd()

	const configTemplate = `
raw:
  "$RAW_ADDR":
    to: "$BACKEND_ADDR"

http:
  "$HTTP_ADDR":
    routes:
      "/be/": { proxy: "$BACKEND_URL" }
      "localhost/xy/": { proxy: "$BACKEND_URL" }
      "/static/hola": { file: "$PWD/testdata/hola" }
      "/dir/": { dir: "$PWD/testdata/" }
      "/redir/": { redirect: "http://$HTTP_ADDR/dir/" }
`
	configStr := strings.NewReplacer(
		"$RAW_ADDR", rawAddr,
		"$HTTP_ADDR", httpAddr,
		"$BACKEND_URL", backend.URL,
		"$BACKEND_ADDR", backend.Listener.Addr().String(),
		"$PWD", pwd,
	).Replace(configTemplate)

	conf, err := config.LoadString(configStr)
	if err != nil {
		log.Fatalf("error loading test config: %v", err)
	}

	go Raw(rawAddr, conf.Raw[rawAddr])
	go HTTP(httpAddr, conf.HTTP[httpAddr])

	waitForHTTPServer(httpAddr)
	waitForHTTPServer(rawAddr)

	os.Exit(m.Run())
}

func TestSimple(t *testing.T) {
	// Test the raw proxy.
	testGet(t, "http://"+rawAddr+"/be", 200)

	// Test the HTTP proxy. Try a combination of URLs and error responses just
	// to exercise a bit more of the path handling and error checking code.
	testGet(t, "http://"+httpAddr+"/be", 200)
	testGet(t, "http://"+httpAddr+"/be/", 200)
	testGet(t, "http://"+httpAddr+"/be/2", 200)
	testGet(t, "http://"+httpAddr+"/be/3", 200)
	testGet(t, "http://"+httpAddr+"/x", 404)
	testGet(t, "http://"+httpAddr+"/xy/1", 404)

	// Test the domain-based routing.
	_, httpPort, _ := net.SplitHostPort(httpAddr)
	testGet(t, "http://localhost:"+httpPort+"/be/", 200)
	testGet(t, "http://localhost:"+httpPort+"/xy/1", 200)

	// Test dir and file schemes.
	testGet(t, "http://"+httpAddr+"/static/hola", 200)
	testGet(t, "http://"+httpAddr+"/dir/hola", 200)
	testGet(t, "http://"+httpAddr+"/redir/hola", 200)
}

func testGet(t *testing.T, url string, expectedStatus int) {
	t.Helper()
	t.Logf("URL: %s", url)
	resp, err := http.Get(url)
	if err != nil {
		t.Fatal(err)
	}
	t.Logf("status %v", resp.Status)

	if resp.StatusCode != expectedStatus {
		t.Errorf("expected status %d, got %v", expectedStatus, resp.Status)
		t.Errorf("response: %#v", resp)
	}

	// We don't care about the body for non-200 responses.
	if resp.StatusCode != http.StatusOK {
		return
	}

	b, err := io.ReadAll(resp.Body)
	if err != nil {
		t.Fatal(err)
	}
	if string(b) != backendResponse {
		t.Errorf("expected body = %q, got %q", backendResponse, string(b))
	}

	t.Logf("response body: %q", b)
}

func TestJoinPath(t *testing.T) {
	cases := []struct{ a, b, expected string }{
		{"/a/", "", "/a/"},
		{"/a/", "b", "/a/b"},
		{"/a/", "b/", "/a/b/"},
		{"a/", "", "a/"},
		{"a/", "b", "a/b"},
		{"a/", "b/", "a/b/"},
		{"a/", "/b/", "a/b/"},
		{"/", "", "/"},
		{"", "", "/"},
		{"/", "/", "/"},
	}
	for _, c := range cases {
		got := joinPath(c.a, c.b)
		if got != c.expected {
			t.Errorf("join %q, %q = %q, expected %q", c.a, c.b, got, c.expected)
		}
	}
}

func TestAdjustPath(t *testing.T) {
	cases := []struct{ from, to, req, expected string }{
		{"/", "/", "/", "/"},
		{"/", "/", "/a", "/a"},
		{"/", "/", "/a/x", "/a/x"},
		{"/a", "/", "/a", "/"},
		{"/a", "/", "/a/", "/"},
		{"/a", "/", "/a/x", "/x"},
		{"/a/", "/", "/a/", "/"},
		{"/a/", "/", "/a/x", "/x"},
		{"/a/", "/b", "/a/", "/b"},
		{"/a/", "/b", "/a/x", "/b/x"},
		{"/p/q", "/r/s", "/p/q", "/r/s"},
		{"/p/q", "/r/s", "/p/q", "/r/s"},
		{"/p/q", "/r/s", "/p/q/x", "/r/s/x"},
	}
	for _, c := range cases {
		got := adjustPath(c.req, c.from, c.to)
		if got != c.expected {
			t.Errorf("adjustPath(%q, %q, %q) = %q, expected %q",
				c.req, c.from, c.to, got, c.expected)
		}
	}
}

func TestAdjustPathPanic(t *testing.T) {
	// We expect req to have from as prefix; otherwise, the function should
	// panic.
	defer func() {
		if r := recover(); r == nil {
			t.Errorf("adjustPath did not panic as expected")
		}
	}()
	adjustPath("/req", "/from", "/to")
}

func TestRateLimitRequestErrors(t *testing.T) {
	// WithRateLimit needs to split host and port, and parse the host IP
	// address. We don't expect either to fail, due to the nature of the
	// handlers, but we want to make sure we fail-open if they do.
	// To test this, we set up some artificial requests.

	// First, set up a rate limiter which rejects everything.
	ratelimit.FromConfig("TestRateLimitRequestErrors", config.RateLimit{
		Rate: config.Rate{Requests: 0, Period: 1 * time.Second}})
	rl := ratelimit.FromName("TestRateLimitRequestErrors")
	if rl == nil {
		t.Fatalf("rate limiter not found")
	}

	// Set up a trivial root handler, and then wrap it with the rate limiter.
	rootHCalled := 0
	rootH := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		rootHCalled++
		w.WriteHeader(200)
	})

	h := WithRateLimit(rootH, rl)

	// Test that a well-formed request is rejected.
	w := httptest.NewRecorder()
	r := httptest.NewRequest("GET", "http://localhost:1234/", nil)
	h.ServeHTTP(w, r)
	if w.Code != http.StatusTooManyRequests {
		t.Errorf("expected status 429 (too many requests), got %v", w.Code)
	}

	// Test that a request with a remote address that fails to be split
	// fails-open and is allowed.
	r = httptest.NewRequest("GET", "http://[::1]:1234/", nil)
	r.RemoteAddr = "invalid"
	w = httptest.NewRecorder()
	h.ServeHTTP(w, r)
	if w.Code != http.StatusOK {
		t.Errorf("expected status 200 (OK), got %v", w.Code)
	}
	if rootHCalled != 1 {
		t.Errorf("expected root handler to be called 1, got %v", rootHCalled)
	}

	// Test that a request with a remote address that has a non-IP as host
	// fails-open and is allowed.
	r = httptest.NewRequest("GET", "http://[::1]:1234/", nil)
	r.RemoteAddr = "localhost:1234"
	w = httptest.NewRecorder()
	h.ServeHTTP(w, r)
	if w.Code != http.StatusOK {
		t.Errorf("expected status 200 (OK), got %v", w.Code)
	}
	if rootHCalled != 2 {
		t.Errorf("expected root handler to be called 2, got %v", rootHCalled)
	}

	// Test that a request with a link-local address, which includes a zone, is
	// rate limited like any other.
	r = httptest.NewRequest("GET", "http://[::1]:1234/", nil)
	r.RemoteAddr = "[fe80::1%eth0]:1234"
	w = httptest.NewRecorder()
	h.ServeHTTP(w, r)
	if w.Code != http.StatusTooManyRequests {
		t.Errorf("expected status 429 (too many requests), got %v", w.Code)
	}
	if rootHCalled != 2 {
		t.Errorf("expected root handler to be called 2, got %v", rootHCalled)
	}
}

// TestLoggingStatus checks that the request log records the status the client
// gets, in particular when the handler does not call WriteHeader explicitly.
func TestLoggingStatus(t *testing.T) {
	cases := []struct {
		name    string
		handler http.HandlerFunc
		status  int
		length  int
	}{
		{"empty", func(w http.ResponseWriter, r *http.Request) {},
			200, 0},
		{"write", func(w http.ResponseWriter, r *http.Request) {
			w.Write([]byte("hello"))
		}, 200, 5},
		{"readfrom", func(w http.ResponseWriter, r *http.Request) {
			w.(io.ReaderFrom).ReadFrom(strings.NewReader("hello"))
		}, 200, 5},
		{"explicit", func(w http.ResponseWriter, r *http.Request) {
			w.WriteHeader(http.StatusCreated)
		}, 201, 0},
		{"error", func(w http.ResponseWriter, r *http.Request) {
			w.WriteHeader(http.StatusNotFound)
			w.Write([]byte("hello"))
		}, 404, 5},
	}
	for _, c := range cases {
		t.Run(c.name, func(t *testing.T) {
			path := filepath.Join(t.TempDir(), "log")
			rl, err := reqlog.New(path, 1, "{{.Status}} {{.Length}}\n")
			if err != nil {
				t.Fatal(err)
			}

			h := WithTrace("test", WithReqLog(WithLogging(c.handler), rl))
			w := httptest.NewRecorder()
			h.ServeHTTP(w, httptest.NewRequest("GET", "http://unused/", nil))
			if w.Code != c.status {
				t.Errorf("client got status %d, want %d", w.Code, c.status)
			}

			// The log is written asynchronously, so wait for the entry.
			want := fmt.Sprintf("%d %d\n", c.status, c.length)
			got := ""
			deadline := time.Now().Add(5 * time.Second)
			for got == "" && time.Now().Before(deadline) {
				time.Sleep(time.Millisecond)
				buf, _ := os.ReadFile(path)
				if strings.HasSuffix(string(buf), "\n") {
					got = string(buf)
				}
			}
			if got != want {
				t.Errorf("logged %q, want %q", got, want)
			}
		})
	}
}

func Benchmark(b *testing.B) {
	makeBench := func(url string) func(b *testing.B) {
		return func(b *testing.B) {
			var resp *http.Response
			var err error
			for b.Loop() {
				resp, err = http.Get(url)
				if err != nil {
					b.Fatal(err)
				}
				resp.Body.Close()
				if resp.StatusCode != 200 {
					b.Errorf("expected status 200, got %v", resp.Status)
					b.Fatalf("response: %#v", resp)
				}
			}
		}
	}

	b.Run("HTTP", makeBench("http://"+httpAddr+"/be"))
	b.Run("Raw", makeBench("http://"+rawAddr+"/be"))
}

func BenchmarkParallel(b *testing.B) {
	makeP := func(url string) func(pb *testing.PB) {
		return func(pb *testing.PB) {
			var resp *http.Response
			var err error
			for pb.Next() {
				resp, err = http.Get(url)
				if err != nil {
					b.Fatal(err)
				}
				resp.Body.Close()
				if resp.StatusCode != 200 {
					b.Errorf("expected status 200, got %v", resp.Status)
					b.Fatalf("response: %#v", resp)
				}
			}
		}
	}

	b.Run("HTTP", func(b *testing.B) {
		b.RunParallel(makeP("http://" + httpAddr + "/be"))
	})
	b.Run("Raw", func(b *testing.B) {
		b.RunParallel(makeP("http://" + rawAddr + "/be"))
	})
}

// nopResponseWriter is a minimal http.ResponseWriter for benchmarks.
type nopResponseWriter struct {
	hdr http.Header
}

func (w *nopResponseWriter) Header() http.Header         { return w.hdr }
func (w *nopResponseWriter) Write(b []byte) (int, error) { return len(b), nil }
func (w *nopResponseWriter) WriteHeader(int)             {}

func benchLimiter(b *testing.B, requests uint64) *ipratelimit.Limiter {
	name := b.Name()
	ratelimit.FromConfig(name, config.RateLimit{
		Rate: config.Rate{Requests: requests, Period: time.Hour},
		Size: 1024,
	})
	return ratelimit.FromName(name)
}

var benchRemoteAddrs = []struct{ name, addr string }{
	{"IPv4", "192.0.2.1:1234"},
	{"IPv6", "[2001:db8:1:2:3:4:5:6]:1234"},
}

// BenchmarkWithRateLimit measures the rate limiter as seen by HTTP requests,
// including parsing the remote address. The denied path matters because it is
// what a client that is being abusive will exercise.
func BenchmarkWithRateLimit(b *testing.B) {
	h := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {})
	for _, c := range []struct {
		name     string
		requests uint64
	}{{"Allowed", 1 << 62}, {"Denied", 1}} {
		for _, a := range benchRemoteAddrs {
			b.Run(c.name+"/"+a.name, func(b *testing.B) {
				wrapped := WithRateLimit(h, benchLimiter(b, c.requests))
				r := httptest.NewRequest("GET", "http://unused/", nil)
				r.RemoteAddr = a.addr
				w := &nopResponseWriter{hdr: http.Header{}}
				for b.Loop() {
					wrapped.ServeHTTP(w, r)
				}
			})
		}
	}
}

// BenchmarkRawAllowed measures the rate limiter as seen by raw proxying.
// Note net.TCPAddr holds IPv4 addresses in their 16-byte form when they come
// from a dual-stack socket.
func BenchmarkRawAllowed(b *testing.B) {
	for _, c := range []struct {
		name string
		ip   net.IP
	}{
		{"IPv4", net.ParseIP("192.0.2.1")},
		{"IPv6", net.ParseIP("2001:db8:1:2:3:4:5:6")},
	} {
		b.Run(c.name, func(b *testing.B) {
			lim := benchLimiter(b, 1<<62)
			addr := &net.TCPAddr{IP: c.ip, Port: 1234}
			for b.Loop() {
				allowed(addr, lim)
			}
		})
	}
}