Halt pollLoop when PollingPacketConn is closed.
[champa.git] / champa-server / main_test.go
blob6be3d5cc62b12f9de1dad192825ef36a44f502f6
1 package main
3 import (
4 "bytes"
5 "encoding/base64"
6 "net/http"
7 "testing"
9 "www.bamsoftware.com/git/champa.git/turbotunnel"
12 func mustNewRequest(method, path string) *http.Request {
13 req, err := http.NewRequest(method, path, nil)
14 if err != nil {
15 panic(err)
17 return req
20 func TestDecodeRequest(t *testing.T) {
21 defClientID := turbotunnel.ClientID{'C', 'L', 'I', 'E', 'N', 'T', 'I', 'D'}
22 defPayload := []byte("abcde")
23 defEncoded := string(base64.RawURLEncoding.EncodeToString(bytes.Join([][]byte{defClientID[:], defPayload}, nil)))
25 for _, test := range []struct {
26 path string
27 expectedClientID turbotunnel.ClientID
28 expectedPayload []byte
30 // Good tests.
31 {"/0/XXXX/" + defEncoded, defClientID, defPayload},
32 {"/0/@@@@/" + defEncoded, defClientID, defPayload}, // non-base64 padding
33 {"/0/XXXX/YYYY/" + defEncoded, defClientID, defPayload},
34 {"/0" + defEncoded, defClientID, defPayload}, // no slash necessary after version
36 // Bad tests.
37 {"/0", turbotunnel.ClientID{}, nil},
38 {"//0/XXXX/" + defEncoded, turbotunnel.ClientID{}, nil}, // extra slash at start
39 {"0/XXXX/" + defEncoded, turbotunnel.ClientID{}, nil}, // no slash at start
40 {"/0/@@@@" + defEncoded, turbotunnel.ClientID{}, nil}, // non-base64 in payload
41 {"/0/XXXX/" + defEncoded + "/", turbotunnel.ClientID{}, nil},
42 {"/1/XXXX/" + defEncoded, turbotunnel.ClientID{}, nil}, // unknown version
43 {"/0/XXXX/Q0xJRU5USURhYmNkZQ===", turbotunnel.ClientID{}, nil}, // with padding characters
44 } {
45 req := mustNewRequest("GET", test.path)
46 clientID, payload := decodeRequest(req)
47 if !bytes.Equal(payload, test.expectedPayload) || clientID != test.expectedClientID {
48 t.Errorf("%+q → (%v, %v), expected (%v, %v)",
49 test.path, clientID, payload, test.expectedClientID, test.expectedPayload)