forked from btcsuite/btcd
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdisableauth_test.go
More file actions
202 lines (177 loc) · 5.29 KB
/
Copy pathdisableauth_test.go
File metadata and controls
202 lines (177 loc) · 5.29 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
package rpcclient
import (
"context"
"encoding/base64"
"io"
"net/http"
"net/http/httptest"
"path/filepath"
"strings"
"testing"
"time"
"github.com/gorilla/websocket"
"github.com/stretchr/testify/require"
)
const (
testRPCUser = "testuser"
testRPCPass = "testpass"
testCallerAuth = "Bearer test-api-key"
testExtraHeader = "X-Test-API-Key"
testExtraValue = "test-api-key"
)
// disableAuthTestCase describes one authentication header configuration that
// must behave the same for HTTP POST and WebSocket transports.
type disableAuthTestCase struct {
name string
configure func(*ConnConfig)
wantAuthorization string
}
// disableAuthTestCases returns the shared transport authentication cases.
func disableAuthTestCases(missingCookie string) []disableAuthTestCase {
basicAuth := "Basic " + base64.StdEncoding.EncodeToString(
[]byte(testRPCUser+":"+testRPCPass),
)
return []disableAuthTestCase{
{
name: "disabled omits generated authorization",
configure: func(config *ConnConfig) {
config.User = ""
config.Pass = ""
config.CookiePath = missingCookie
config.DisableAuth = true
},
},
{
name: "disabled preserves caller authorization",
configure: func(config *ConnConfig) {
config.User = ""
config.Pass = ""
config.CookiePath = missingCookie
config.DisableAuth = true
config.ExtraHeaders["Authorization"] =
testCallerAuth
},
wantAuthorization: testCallerAuth,
},
{
name: "explicit false includes basic authorization",
configure: func(config *ConnConfig) {
config.DisableAuth = false
},
wantAuthorization: basicAuth,
},
{
name: "zero value includes basic authorization",
configure: func(*ConnConfig) {
// Leave DisableAuth at its zero value.
},
wantAuthorization: basicAuth,
},
}
}
// newDisableAuthConfig creates the common configuration for the transport
// authentication cases.
func newDisableAuthConfig() *ConnConfig {
return &ConnConfig{
User: testRPCUser,
Pass: testRPCPass,
ExtraHeaders: map[string]string{
testExtraHeader: testExtraValue,
},
}
}
// assertAuthHeaders verifies both generated or caller-supplied authorization
// and the independent extra header.
func assertAuthHeaders(t *testing.T, header http.Header,
wantAuthorization string) {
t.Helper()
require.Equal(t, wantAuthorization, header.Get("Authorization"))
require.Equal(t, testExtraValue, header.Get(testExtraHeader))
}
// TestDisableAuthHTTPPost verifies that DisableAuth controls generated Basic
// Auth headers on HTTP POST requests without suppressing caller headers.
func TestDisableAuthHTTPPost(t *testing.T) {
missingCookie := filepath.Join(t.TempDir(), "missing-cookie")
for _, tc := range disableAuthTestCases(missingCookie) {
t.Run(tc.name, func(t *testing.T) {
requestHeader := make(chan http.Header, 1)
client := newPostModeTestClient(postRoundTripFunc(
func(req *http.Request) (*http.Response, error) {
requestHeader <- req.Header.Clone()
return &http.Response{
StatusCode: http.StatusOK,
Header: make(http.Header),
Body: io.NopCloser(strings.NewReader(
`{"result":1,"error":null,"id":1}`,
)),
}, nil
},
))
client.config = newDisableAuthConfig()
client.config.Host = "127.0.0.1:8332"
client.config.DisableTLS = true
client.config.HTTPPostMode = true
tc.configure(client.config)
result, err := sendPostRequestWithRetry(
context.Background(), newPostTestRequest(), 1,
client.httpClient, client.config, client.httpURL,
false,
)
require.NoError(t, err)
require.Equal(t, []byte("1"), result)
select {
case header := <-requestHeader:
assertAuthHeaders(t, header, tc.wantAuthorization)
case <-time.After(time.Second):
t.Fatal("timed out waiting for HTTP POST request")
}
})
}
}
// newWebsocketAuthServer creates a server that records the WebSocket handshake
// headers before upgrading the connection.
func newWebsocketAuthServer(t *testing.T) (string, <-chan http.Header) {
t.Helper()
requestHeader := make(chan http.Header, 1)
upgrader := websocket.Upgrader{}
handler := http.HandlerFunc(
func(w http.ResponseWriter, req *http.Request) {
requestHeader <- req.Header.Clone()
conn, err := upgrader.Upgrade(w, req, nil)
if err != nil {
return
}
defer func() {
_ = conn.Close()
}()
},
)
server := httptest.NewServer(handler)
t.Cleanup(server.Close)
return strings.TrimPrefix(server.URL, "http://"), requestHeader
}
// TestDisableAuthWebsocket verifies that DisableAuth controls generated Basic
// Auth headers on WebSocket handshakes without suppressing caller headers.
func TestDisableAuthWebsocket(t *testing.T) {
missingCookie := filepath.Join(t.TempDir(), "missing-cookie")
for _, tc := range disableAuthTestCases(missingCookie) {
t.Run(tc.name, func(t *testing.T) {
host, requestHeader := newWebsocketAuthServer(t)
config := newDisableAuthConfig()
config.Host = host
config.DisableTLS = true
tc.configure(config)
conn, err := dial(config)
require.NoError(t, err)
t.Cleanup(func() {
require.NoError(t, conn.Close())
})
select {
case header := <-requestHeader:
assertAuthHeaders(t, header, tc.wantAuthorization)
case <-time.After(time.Second):
t.Fatal("timed out waiting for WebSocket handshake")
}
})
}
}