-
-
Notifications
You must be signed in to change notification settings - Fork 39
Expand file tree
/
Copy pathserver_test.go
More file actions
194 lines (161 loc) · 5.64 KB
/
server_test.go
File metadata and controls
194 lines (161 loc) · 5.64 KB
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
package main
import (
"context"
"errors"
"net/http"
"net/http/httptest"
"testing"
)
func TestGetRedirectSimple(t *testing.T) {
var redirect *Redirect
var err error
dnsTXT := []string{
"Redirects from /test/* to https://github.com/holic/*",
}
redirect, err = getRedirect(dnsTXT, "/test/")
assertEqual(t, err, nil)
assertEqual(t, redirect.Location, "https://github.com/holic/")
redirect, err = getRedirect(dnsTXT, "/test/success")
assertEqual(t, err, nil)
assertEqual(t, redirect.Location, "https://github.com/holic/success")
redirect, err = getRedirect(dnsTXT, "/should/fail")
assertEqual(t, err.Error(), "No paths matched")
}
func TestGetRedirectComplex(t *testing.T) {
// Tests that catchalls (even interspersed in the TXT records) apply
// only after more specific matches
var redirect *Redirect
var err error
dnsTXT := []string{
"Redirects from /test/* to https://github.com/holic/*",
"Redirects to https://github.com/holic",
"Redirects from /noglob/ to https://github.com/holic/noglob",
}
redirect, err = getRedirect(dnsTXT, "/")
assertEqual(t, err, nil)
assertEqual(t, redirect.Location, "https://github.com/holic")
redirect, err = getRedirect(dnsTXT, "/test/somepath")
assertEqual(t, err, nil)
assertEqual(t, redirect.Location, "https://github.com/holic/somepath")
redirect, err = getRedirect(dnsTXT, "/noglob/")
assertEqual(t, err, nil)
assertEqual(t, redirect.Location, "https://github.com/holic/noglob")
redirect, err = getRedirect(dnsTXT, "/catch/all")
assertEqual(t, err, nil)
assertEqual(t, redirect.Location, "https://github.com/holic")
}
func TestRedirectHandler301CacheControl(t *testing.T) {
orig := lookupTXT
defer func() { lookupTXT = orig }()
lookupTXT = func(host string) ([]string, error) {
return []string{"Redirects permanently to https://example.com/"}, nil
}
req := httptest.NewRequest("GET", "http://go.example.com/", nil)
rr := httptest.NewRecorder()
redirectHandler(rr, req)
if rr.Code != http.StatusMovedPermanently {
t.Errorf("expected 301, got %d", rr.Code)
}
cc := rr.Header().Get("Cache-Control")
if cc == "" {
t.Error("expected Cache-Control header on 301, got none")
}
}
func TestRedirectHandler308CacheControl(t *testing.T) {
orig := lookupTXT
defer func() { lookupTXT = orig }()
lookupTXT = func(host string) ([]string, error) {
return []string{"Redirects to https://example.com/ with 308"}, nil
}
req := httptest.NewRequest("GET", "http://go.example.com/", nil)
rr := httptest.NewRecorder()
redirectHandler(rr, req)
if rr.Code != http.StatusPermanentRedirect {
t.Errorf("expected 308, got %d", rr.Code)
}
if cc := rr.Header().Get("Cache-Control"); cc == "" {
t.Error("expected Cache-Control header on 308, got none")
}
}
func TestRedirectHandler302NoCacheControl(t *testing.T) {
orig := lookupTXT
defer func() { lookupTXT = orig }()
lookupTXT = func(host string) ([]string, error) {
return []string{"Redirects to https://example.com/"}, nil
}
req := httptest.NewRequest("GET", "http://go.example.com/", nil)
rr := httptest.NewRecorder()
redirectHandler(rr, req)
if rr.Code != http.StatusFound {
t.Errorf("expected 302, got %d", rr.Code)
}
if cc := rr.Header().Get("Cache-Control"); cc != "" {
t.Errorf("expected no Cache-Control on 302, got %q", cc)
}
}
func TestRedirectHandler307NoCacheControl(t *testing.T) {
orig := lookupTXT
defer func() { lookupTXT = orig }()
lookupTXT = func(host string) ([]string, error) {
return []string{"Redirects to https://example.com/ with 307"}, nil
}
req := httptest.NewRequest("GET", "http://go.example.com/", nil)
rr := httptest.NewRecorder()
redirectHandler(rr, req)
if rr.Code != http.StatusTemporaryRedirect {
t.Errorf("expected 307, got %d", rr.Code)
}
if cc := rr.Header().Get("Cache-Control"); cc != "" {
t.Errorf("expected no Cache-Control on 307, got %q", cc)
}
}
func TestHostPolicy(t *testing.T) {
orig := lookupTXT
defer func() { lookupTXT = orig }()
// Valid: TXT record contains a parseable redirect config
lookupTXT = func(host string) ([]string, error) {
return []string{"Redirects to https://example.com"}, nil
}
if err := hostPolicy(context.Background(), "foo.example.com"); err != nil {
t.Errorf("expected no error, got %v", err)
}
// DNS error
lookupTXT = func(host string) ([]string, error) {
return nil, errors.New("no such host")
}
if err := hostPolicy(context.Background(), "foo.example.com"); err == nil {
t.Error("expected error for DNS failure")
}
// TXT records exist but none parse as redirect configs
lookupTXT = func(host string) ([]string, error) {
return []string{"v=spf1 include:example.com ~all"}, nil
}
if err := hostPolicy(context.Background(), "foo.example.com"); err == nil {
t.Error("expected error when no valid redirect config found")
}
}
func TestRateLimitedCache(t *testing.T) {
dir := t.TempDir()
cache := newRateLimitedCache(dir)
ctx := context.Background()
data := []byte("test-cert-data")
// First two certs for the same apex succeed
if err := cache.Put(ctx, "sub1.example.com", data); err != nil {
t.Fatalf("first put failed: %v", err)
}
if err := cache.Put(ctx, "sub2.example.com", data); err != nil {
t.Fatalf("second put failed: %v", err)
}
// Third cert for the same apex is rate limited
if err := cache.Put(ctx, "sub3.example.com", data); err == nil {
t.Error("expected rate limit error for third cert this week")
}
// Different apex is not affected
if err := cache.Put(ctx, "sub1.other.org", data); err != nil {
t.Fatalf("different apex put failed: %v", err)
}
// Non-domain keys (e.g., acme account key) bypass rate limiting
if err := cache.Put(ctx, "acme_account+key", data); err != nil {
t.Fatalf("acme account key put failed: %v", err)
}
}