Repository navigation
Expand file tree
/
Copy pathserver_test.go
More file actions
219 lines (199 loc) · 6.21 KB
/
Copy pathserver_test.go
File metadata and controls
219 lines (199 loc) · 6.21 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
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
package main
import (
"context"
"encoding/json"
"net"
"net/http"
"net/http/httptest"
"path/filepath"
"strings"
"sync"
"testing"
"time"
"tailscale.com/tailcfg"
"tailscale.com/types/key"
)
func TestAdmissionHandler(t *testing.T) {
store := &nodeStore{nodes: nodeSet{mustNodePublic(t, testNodeOne): {}}}
handler := newHandler(store, newTestLogger())
t.Run("allowed", func(t *testing.T) {
recorder := postAdmissionRequest(t, handler, "/", mustNodePublic(t, testNodeOne))
assertAdmissionResponse(t, recorder, http.StatusOK, true)
})
t.Run("denied", func(t *testing.T) {
recorder := postAdmissionRequest(t, handler, "/", mustNodePublic(t, testNodeTwo))
assertAdmissionResponse(t, recorder, http.StatusOK, false)
})
t.Run("existing catch-all path remains supported", func(t *testing.T) {
recorder := postAdmissionRequest(t, handler, "/verify", mustNodePublic(t, testNodeOne))
assertAdmissionResponse(t, recorder, http.StatusOK, true)
})
t.Run("unknown fields remain forward compatible", func(t *testing.T) {
body := `{"NodePublic":"` + testNodeOne + `","FutureField":true}`
recorder := httptest.NewRecorder()
handler.ServeHTTP(recorder, httptest.NewRequest(http.MethodPost, "/", strings.NewReader(body)))
assertAdmissionResponse(t, recorder, http.StatusOK, true)
})
t.Run("wrong method", func(t *testing.T) {
recorder := httptest.NewRecorder()
handler.ServeHTTP(recorder, httptest.NewRequest(http.MethodGet, "/", nil))
if recorder.Code != http.StatusMethodNotAllowed {
t.Fatalf("status = %d, want %d", recorder.Code, http.StatusMethodNotAllowed)
}
if allow := recorder.Header().Get("Allow"); allow != http.MethodPost {
t.Fatalf("Allow = %q, want %q", allow, http.MethodPost)
}
})
for _, tt := range []struct {
name string
body string
}{
{name: "malformed", body: `[`},
{name: "trailing JSON", body: `{"NodePublic":"` + testNodeOne + `"} {}`},
{
name: "oversized",
body: `{"NodePublic":"` + testNodeOne + `"}` + strings.Repeat(" ", maxRequestBodyBytes),
},
} {
t.Run(tt.name, func(t *testing.T) {
recorder := httptest.NewRecorder()
handler.ServeHTTP(recorder, httptest.NewRequest(http.MethodPost, "/", strings.NewReader(tt.body)))
if recorder.Code != http.StatusBadRequest {
t.Fatalf("status = %d, want %d", recorder.Code, http.StatusBadRequest)
}
})
}
}
func TestAdmissionHandlerConcurrentReload(t *testing.T) {
path := filepath.Join(t.TempDir(), "nodes.json")
writeTestFile(t, path, `["`+testNodeOne+`"]`)
store, err := newNodeStore(path, false)
if err != nil {
t.Fatal(err)
}
handler := newHandler(store, newTestLogger())
body := marshalAdmissionRequest(t, mustNodePublic(t, testNodeOne))
start := make(chan struct{})
var requests sync.WaitGroup
for range 4 {
requests.Add(1)
go func() {
defer requests.Done()
<-start
for range 50 {
recorder := httptest.NewRecorder()
handler.ServeHTTP(recorder, httptest.NewRequest(http.MethodPost, "/", strings.NewReader(body)))
if recorder.Code != http.StatusOK {
t.Errorf("status = %d, want %d", recorder.Code, http.StatusOK)
}
}
}()
}
close(start)
for i := range 20 {
node := testNodeOne
if i%2 == 0 {
node = testNodeTwo
}
writeTestFile(t, path, `["`+node+`"]`)
if _, _, err := store.reload(); err != nil {
t.Fatal(err)
}
}
requests.Wait()
}
func TestHTTPServerConfiguration(t *testing.T) {
server := newHTTPServer("127.0.0.1:0", http.NotFoundHandler())
if server.ReadHeaderTimeout != readHeaderTimeout {
t.Fatalf("ReadHeaderTimeout = %s, want %s", server.ReadHeaderTimeout, readHeaderTimeout)
}
if server.ReadTimeout != readTimeout {
t.Fatalf("ReadTimeout = %s, want %s", server.ReadTimeout, readTimeout)
}
if server.WriteTimeout != writeTimeout {
t.Fatalf("WriteTimeout = %s, want %s", server.WriteTimeout, writeTimeout)
}
if server.IdleTimeout != idleTimeout {
t.Fatalf("IdleTimeout = %s, want %s", server.IdleTimeout, idleTimeout)
}
}
func TestServeHTTPShutsDownOnCancellation(t *testing.T) {
listener, err := net.Listen("tcp", "127.0.0.1:0")
if err != nil {
t.Fatal(err)
}
server := newHTTPServer(listener.Addr().String(), http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
w.WriteHeader(http.StatusNoContent)
}))
ctx, cancel := context.WithCancel(context.Background())
serveDone := make(chan error, 1)
go func() {
serveDone <- serveHTTP(ctx, server, listener)
}()
client := &http.Client{
Transport: &http.Transport{Proxy: nil},
Timeout: time.Second,
}
defer client.CloseIdleConnections()
resp, err := client.Get("http://" + listener.Addr().String())
if err != nil {
cancel()
t.Fatal(err)
}
if err := resp.Body.Close(); err != nil {
cancel()
t.Fatal(err)
}
if resp.StatusCode != http.StatusNoContent {
cancel()
t.Fatalf("status = %d, want %d", resp.StatusCode, http.StatusNoContent)
}
cancel()
select {
case err := <-serveDone:
if err != nil {
t.Fatal(err)
}
case <-time.After(time.Second):
t.Fatal("HTTP server did not shut down after context cancellation")
}
}
func postAdmissionRequest(t *testing.T, handler http.Handler, path string, nodeKey key.NodePublic) *httptest.ResponseRecorder {
t.Helper()
body := marshalAdmissionRequest(t, nodeKey)
recorder := httptest.NewRecorder()
handler.ServeHTTP(recorder, httptest.NewRequest(http.MethodPost, path, strings.NewReader(body)))
return recorder
}
func marshalAdmissionRequest(t *testing.T, nodeKey key.NodePublic) string {
t.Helper()
text, err := nodeKey.MarshalText()
if err != nil {
t.Fatal(err)
}
body, err := json.Marshal(struct {
NodePublic string
}{
NodePublic: string(text),
})
if err != nil {
t.Fatal(err)
}
return string(body)
}
func assertAdmissionResponse(t *testing.T, recorder *httptest.ResponseRecorder, wantStatus int, wantAllow bool) {
t.Helper()
if recorder.Code != wantStatus {
t.Fatalf("status = %d, want %d", recorder.Code, wantStatus)
}
if contentType := recorder.Header().Get("Content-Type"); contentType != "application/json" {
t.Fatalf("Content-Type = %q, want application/json", contentType)
}
var response tailcfg.DERPAdmitClientResponse
if err := json.Unmarshal(recorder.Body.Bytes(), &response); err != nil {
t.Fatal(err)
}
if response.Allow != wantAllow {
t.Fatalf("Allow = %v, want %v", response.Allow, wantAllow)
}
}