-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathserver_test.go
176 lines (144 loc) · 4.32 KB
/
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
package main
import (
"net/http"
"net/http/httptest"
"strings"
"testing"
)
type dummyAuth struct {
usernameSent, passwordSent string
called int
ret bool
}
func (a *dummyAuth) authenticate(username, password string) (bool, error) {
a.usernameSent = username
a.passwordSent = password
a.called += 1
return a.ret, nil
}
func TestRootHandlerServesLoaderScriptWithDefaultExpiry(t *testing.T) {
request, _ := http.NewRequest("GET", "/", nil)
response := httptest.NewRecorder()
server := newServer(
configuration{LoaderScript: "testdata/loader.sh"},
nil,
)
server.routes()
server.ServeHTTP(response, request)
code := response.Code
if code != 200 {
t.Error("response code was not 200:", code)
}
body := strings.TrimSpace(response.Body.String())
if body != "awesome loader script 32400" {
t.Error("wrong response from /:", body)
}
}
func TestRootHandlerServesLoaderScriptWithExpiry(t *testing.T) {
request, _ := http.NewRequest("GET", "/3", nil)
response := httptest.NewRecorder()
server := newServer(
configuration{LoaderScript: "testdata/loader.sh"},
nil,
)
server.routes()
server.ServeHTTP(response, request)
code := response.Code
if code != 200 {
t.Error("response code was not 200:", code)
}
body := strings.TrimSpace(response.Body.String())
if body != "awesome loader script 10800" {
t.Error("wrong response from /:", body)
}
}
func TestKeysHandlerRequiresAuthentication(t *testing.T) {
request, _ := http.NewRequest("GET", "/key", nil)
response := httptest.NewRecorder()
server := newServer(
configuration{PrivateKey: "testdata/id_rsa"},
&dummyAuth{},
)
server.routes()
server.ServeHTTP(response, request)
code := response.Code
if code != 401 {
t.Error("request should have been rejected with 401:", code)
}
header := response.Header().Get("Authenticate")
if header != "KeyGuard" {
t.Error("correct authenticate header was not in response:", header)
}
}
func TestKeysHandlerRequiresValidCredentials(t *testing.T) {
request, _ := http.NewRequest("GET", "/key", nil)
response := httptest.NewRecorder()
request.SetBasicAuth("cromega", "supersecurepassword")
server := newServer(
configuration{PrivateKey: "testdata/id_rsa"},
&dummyAuth{ret: true},
)
server.routes()
server.ServeHTTP(response, request)
code := response.Code
if code != 200 {
t.Error("http status should be 200:", code)
}
}
func TestKeysHandlerAuthenticatesTheRequest(t *testing.T) {
request, _ := http.NewRequest("GET", "/key", nil)
request.SetBasicAuth("keyguard", "supersecurepassword")
response := httptest.NewRecorder()
auth := &dummyAuth{ret: true}
server := newServer(
configuration{PrivateKey: "testdata/id_rsa"},
auth,
)
server.routes()
server.ServeHTTP(response, request)
if auth.called != 1 {
t.Error("the authenticator was not called")
}
if auth.usernameSent != "keyguard" {
t.Error("sent the wrong username to the authenticator:", auth.usernameSent)
}
if auth.passwordSent != "supersecurepassword" {
t.Error("sent the wrong password to the authenticator:", auth.passwordSent)
}
if response.Code != 200 {
t.Error("wrong respnse code:", response.Code)
}
}
func TestKeysHandlerRespondsWithKey(t *testing.T) {
request, _ := http.NewRequest("GET", "/key", nil)
request.SetBasicAuth("cromega", "supersecurepassword")
response := httptest.NewRecorder()
auth := &dummyAuth{ret: true}
server := newServer(
configuration{PrivateKey: "testdata/id_rsa"},
auth,
)
server.routes()
server.ServeHTTP(response, request)
body := response.Body.String()
if body != "awesome private key" {
t.Error("server should have responded with the correct ssh key:", body)
}
}
func TestPublicKeyHandlerSendsPublicKey(t *testing.T) {
request, _ := http.NewRequest("GET", "/pubkey", nil)
response := httptest.NewRecorder()
server := newServer(
configuration{PrivateKey: "testdata/real_id_rsa"},
nil,
)
server.routes()
server.ServeHTTP(response, request)
if response.Code != 200 {
t.Error("response should be OK", response.Code)
}
body := response.Body.String()
if body != "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAAAgQDBgps60FUrGlCFlH48/cSWaDuZYfCTvgRIZt60CiQ1fPj53SQ6xpcDpSCpt1pJt/Q1xZtHPaNZ+HWKAU3tOgspi/AJdrQAPC54CLzdBsMlL/+JxjMxtCf0bbG8dxoRijxIppXVyIuCLabA2oEhepf3U/H+Dvm3XST22f87FsQVrw==\n" {
t.Error("server should have responded with a public key:", body)
}
}