forked from jmhodges/howsmyssl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index_test.go
191 lines (180 loc) · 6.13 KB
/
index_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
package main
import (
"bytes"
"encoding/json"
"expvar"
"net/http"
"net/http/httptest"
"testing"
)
func TestDumbNilishIndex(t *testing.T) {
tmpl := loadIndex()
buf := new(bytes.Buffer)
err := tmpl.Execute(buf, &clientInfo{})
if err != nil {
t.Errorf("index execution blew up with nilish clientInfo: %#v", err)
}
if len(buf.Bytes()) == 0 {
t.Errorf("index execution did not write anything")
}
}
type acmeTest struct {
challPath string
acmeRedirectURL string
expected string
code int
}
func TestACMERedirect(t *testing.T) {
staticVars := new(expvar.Map).Init()
staticHandler := makeStaticHandler("/static", staticVars)
webHandleFunc := http.NotFound
tests := []acmeTest{
// same domain redirect, acmeRedirectURL leads with "/"
{
challPath: "https://www.howsmyssl.com/.well-known/acme-challenge/foobar",
acmeRedirectURL: "/example.com",
expected: "/example.com/.well-known/acme-challenge/foobar",
},
{
challPath: "https://www.howsmyssl.com/.well-known/acme-challenge/foobar",
acmeRedirectURL: "http://example.com",
expected: "http://example.com/.well-known/acme-challenge/foobar",
},
{
challPath: "https://www.howsmyssl.com/.well-known/acme-challenge/foobar",
acmeRedirectURL: "http://example.com/",
expected: "http://example.com/.well-known/acme-challenge/foobar",
},
// Busted acmeRedirectURL. Meant to be a domain, but was not.
{
challPath: "https://www.howsmyssl.com/.well-known/acme-challenge/foobar",
acmeRedirectURL: "example.com",
expected: "/.well-known/acme-challenge/example.com/.well-known/acme-challenge/foobar",
},
// same domain redirect, acmeRedirectURL leads and trails with "/"
{
challPath: "https://www.howsmyssl.com/.well-known/acme-challenge/foobar",
acmeRedirectURL: "/okay/",
expected: "/okay/.well-known/acme-challenge/foobar",
},
// same domain redirect, acmeRedirectURL leads and trails with "/"
{
challPath: "https://www.howsmyssl.com/.well-known/acme-challenge/foobar",
acmeRedirectURL: "/okay/",
expected: "/okay/.well-known/acme-challenge/foobar",
},
// same domain redirect, acmeRedirectURL leads with "/"
{
challPath: "https://www.howsmyssl.com/.well-known/acme-challenge/foobar",
acmeRedirectURL: "/okay",
expected: "/okay/.well-known/acme-challenge/foobar",
},
{
challPath: "https://www.howsmyssl.com/.well-known/acme-challenge",
acmeRedirectURL: "http://example.com",
expected: "/.well-known/acme-challenge/",
code: http.StatusMovedPermanently,
},
{
challPath: "https://www.howsmyssl.com/.well-known/acme-challenge/",
acmeRedirectURL: "http://example.com",
expected: "",
code: http.StatusOK,
},
{
challPath: "https://www.howsmyssl.com/.well-known/acme-challenge/foobar",
acmeRedirectURL: "http://example.com",
expected: "http://example.com/.well-known/acme-challenge/foobar",
},
}
for i, tt := range tests {
tm := tlsMux("www.howsmyssl.com", "www.howsmyssl.com", tt.acmeRedirectURL, staticHandler, webHandleFunc, nil)
r, err := http.NewRequest("GET", tt.challPath, nil)
if err != nil {
t.Fatalf("borked request for %#v: %s", tt.challPath, err)
}
w := httptest.NewRecorder()
tm.ServeHTTP(w, r)
location := w.Header().Get("Location")
if tt.code == 0 {
tt.code = http.StatusFound
}
if w.Code != tt.code {
t.Errorf("#%d, want %d, got %d", i, tt.code, w.Code)
}
if location != tt.expected {
t.Errorf("#%d, %q, want %#v, got %#v", i, tt.acmeRedirectURL, tt.expected, location)
}
}
}
type vhostTest struct {
rawVHost string
httpsAddr string
expectedRouteHost string
expectedRedirectHost string
}
func TestVHostCalculation(t *testing.T) {
tests := []vhostTest{
vhostTest{
rawVHost: "www.howsmyssl.com",
httpsAddr: "0:10443",
expectedRouteHost: "www.howsmyssl.com",
expectedRedirectHost: "www.howsmyssl.com",
},
vhostTest{
rawVHost: "localhost:10443",
httpsAddr: "localhost:10443",
expectedRouteHost: "localhost",
expectedRedirectHost: "localhost:10443",
},
vhostTest{
rawVHost: "example.com:10443",
httpsAddr: "localhost:10443",
expectedRouteHost: "example.com",
expectedRedirectHost: "example.com:10443",
},
vhostTest{
rawVHost: "example.com:443",
httpsAddr: "0:443",
expectedRouteHost: "example.com",
expectedRedirectHost: "example.com",
},
}
staticVars := new(expvar.Map).Init()
staticHandler := makeStaticHandler("/static", staticVars)
webHandleFunc := http.NotFound
for i, vt := range tests {
routeHost, redirectHost := calculateDomains(vt.rawVHost, vt.httpsAddr)
if routeHost != vt.expectedRouteHost {
t.Errorf("#%d vhost %#v, httpsAddr %#v: want routeHost %#v, got %s", i, vt.rawVHost, vt.httpsAddr, vt.expectedRouteHost, routeHost)
}
if redirectHost != vt.expectedRedirectHost {
t.Errorf("#%d vhost %#v, httpsAddr %#v: want redirectHost %#v, got %#v", i, vt.rawVHost, vt.httpsAddr, vt.expectedRedirectHost, redirectHost)
}
tm := tlsMux(vt.expectedRouteHost, vt.expectedRedirectHost, "http://otherexample.com", staticHandler, webHandleFunc, nil)
r, err := http.NewRequest("GET", "https://howsmyssl.com/", nil)
if err != nil {
t.Fatalf("borked request")
}
w := httptest.NewRecorder()
tm.ServeHTTP(w, r)
expectedLocation := "https://" + vt.expectedRedirectHost + "/"
location := w.Header()["Location"][0]
if w.Code != http.StatusMovedPermanently {
t.Errorf("#%d vhost %#v, httpsAddr %#v: want Code %d, got %d", i, vt.rawVHost, vt.httpsAddr, http.StatusMovedPermanently, w.Code)
}
if location != expectedLocation {
t.Errorf("#%d vhost %#v, httpsAddr %#v: want Location %s, got %s", i, vt.rawVHost, vt.httpsAddr, expectedLocation, location)
}
}
}
func TestDisallowedBodyParses(t *testing.T) {
e := &struct {
Error string `json:"error"`
TLSVersion string `json:"tls_version"`
}{}
err := json.Unmarshal(disallowedOriginBody, e)
if err != nil {
t.Errorf("disallowedOriginBody did not parse: %s", err)
}
}