-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver_test.go
319 lines (271 loc) · 10.9 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
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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
// Copyright 2015 The go-ethereum Authors
// This file is part of the go-ethereum library.
//
// The go-ethereum library is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// The go-ethereum library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
package rpc
import (
"bufio"
"bytes"
"context"
"errors"
"fmt"
"io"
"net"
"net/http"
"os"
"path/filepath"
"strings"
"testing"
"time"
"google.golang.org/protobuf/types/known/timestamppb"
"github.com/ethereum/go-ethereum/core/types"
"github.com/jpmorganchase/quorum-security-plugin-sdk-go/proto"
"github.com/stretchr/testify/assert"
)
func TestServerRegisterName(t *testing.T) {
server := NewServer()
service := new(testService)
if err := server.RegisterName("test", service); err != nil {
t.Fatalf("%v", err)
}
if len(server.services.services) != 2 {
t.Fatalf("Expected 2 service entries, got %d", len(server.services.services))
}
svc, ok := server.services.services["test"]
if !ok {
t.Fatalf("Expected service calc to be registered")
}
wantCallbacks := 9
// Quorum - Add 2 extra callbacks for the function added by us EchoCtxId and EchoCtxPSI
wantCallbacks += 2
// End Quorum
if len(svc.callbacks) != wantCallbacks {
t.Errorf("Expected %d callbacks for service 'service', got %d", wantCallbacks, len(svc.callbacks))
}
}
func TestServer(t *testing.T) {
files, err := os.ReadDir("testdata")
if err != nil {
t.Fatal("where'd my testdata go?")
}
for _, f := range files {
if f.IsDir() || strings.HasPrefix(f.Name(), ".") {
continue
}
path := filepath.Join("testdata", f.Name())
name := strings.TrimSuffix(f.Name(), filepath.Ext(f.Name()))
t.Run(name, func(t *testing.T) {
runTestScript(t, path)
})
}
}
func runTestScript(t *testing.T, file string) {
server := newTestServer()
content, err := os.ReadFile(file)
if err != nil {
t.Fatal(err)
}
clientConn, serverConn := net.Pipe()
defer clientConn.Close()
go server.ServeCodec(NewCodec(serverConn), 0)
readbuf := bufio.NewReader(clientConn)
for _, line := range strings.Split(string(content), "\n") {
line = strings.TrimSpace(line)
switch {
case len(line) == 0 || strings.HasPrefix(line, "//"):
// skip comments, blank lines
continue
case strings.HasPrefix(line, "--> "):
t.Log(line)
// write to connection
clientConn.SetWriteDeadline(time.Now().Add(5 * time.Second))
if _, err := io.WriteString(clientConn, line[4:]+"\n"); err != nil {
t.Fatalf("write error: %v", err)
}
case strings.HasPrefix(line, "<-- "):
t.Log(line)
want := line[4:]
// read line from connection and compare text
clientConn.SetReadDeadline(time.Now().Add(5 * time.Second))
sent, err := readbuf.ReadString('\n')
if err != nil {
t.Fatalf("read error: %v", err)
}
sent = strings.TrimRight(sent, "\r\n")
if sent != want {
t.Errorf("wrong line from server\ngot: %s\nwant: %s", sent, want)
}
default:
panic("invalid line in test script: " + line)
}
}
}
// This test checks that responses are delivered for very short-lived connections that
// only carry a single request.
func TestServerShortLivedConn(t *testing.T) {
server := newTestServer()
defer server.Stop()
listener, err := net.Listen("tcp", "127.0.0.1:0")
if err != nil {
t.Fatal("can't listen:", err)
}
defer listener.Close()
go server.ServeListener(listener)
var (
request = `{"jsonrpc":"2.0","id":1,"method":"rpc_modules"}` + "\n"
wantResp = `{"jsonrpc":"2.0","id":1,"result":{"nftest":"1.0","rpc":"1.0","test":"1.0"}}` + "\n"
deadline = time.Now().Add(10 * time.Second)
)
for i := 0; i < 20; i++ {
conn, err := net.Dial("tcp", listener.Addr().String())
if err != nil {
t.Fatal("can't dial:", err)
}
defer conn.Close()
conn.SetDeadline(deadline)
// Write the request, then half-close the connection so the server stops reading.
conn.Write([]byte(request))
conn.(*net.TCPConn).CloseWrite()
// Now try to get the response.
buf := make([]byte, 2000)
n, err := conn.Read(buf)
if err != nil {
t.Fatal("read error:", err)
}
if !bytes.Equal(buf[:n], []byte(wantResp)) {
t.Fatalf("wrong response: %s", buf[:n])
}
}
}
func TestAuthenticateHttpRequest_whenAuthenticationManagerFails(t *testing.T) {
protectedServer := NewProtectedServer(&stubAuthenticationManager{false, errors.New("arbitrary error")}, false)
arbitraryRequest, _ := http.NewRequest("POST", "https://arbitraryUrl", nil)
captor := &securityContextConfigurerCaptor{}
protectedServer.authenticateHttpRequest(arbitraryRequest, captor)
actualErr, hasError := captor.context.Value(ctxAuthenticationError).(error)
assert.True(t, hasError, "must have error")
assert.EqualError(t, actualErr, "internal error")
assert.Nil(t, PreauthenticatedTokenFromContext(captor.context), "must not be preauthenticated")
}
func TestAuthenticateHttpRequest_whenTypical(t *testing.T) {
protectedServer := NewProtectedServer(&stubAuthenticationManager{true, nil}, false)
arbitraryRequest, _ := http.NewRequest("POST", "https://arbitraryUrl", nil)
arbitraryRequest.Header.Set(HttpAuthorizationHeader, "arbitrary value")
captor := &securityContextConfigurerCaptor{}
protectedServer.authenticateHttpRequest(arbitraryRequest, captor)
_, hasError := captor.context.Value(ctxAuthenticationError).(error)
assert.False(t, hasError, "must not have error")
assert.NotNil(t, PreauthenticatedTokenFromContext(captor.context), "must be preauthenticated")
}
func TestAuthenticateHttpRequest_whenAuthenticationManagerIsDisabled(t *testing.T) {
protectedServer := NewProtectedServer(&stubAuthenticationManager{false, nil}, false)
arbitraryRequest, _ := http.NewRequest("POST", "https://arbitraryUrl", nil)
captor := &securityContextConfigurerCaptor{}
protectedServer.authenticateHttpRequest(arbitraryRequest, captor)
_, hasError := captor.context.Value(ctxAuthenticationError).(error)
assert.False(t, hasError, "must not have error")
assert.Nil(t, PreauthenticatedTokenFromContext(captor.context), "must not be preauthenticated")
}
func TestAuthenticateHttpRequest_whenMissingAccessToken(t *testing.T) {
protectedServer := NewProtectedServer(&stubAuthenticationManager{true, nil}, false)
arbitraryRequest, _ := http.NewRequest("POST", "https://arbitraryUrl", nil)
captor := &securityContextConfigurerCaptor{}
protectedServer.authenticateHttpRequest(arbitraryRequest, captor)
actualErr, hasError := captor.context.Value(ctxAuthenticationError).(error)
assert.True(t, hasError, "must have error")
assert.EqualError(t, actualErr, "missing access token")
assert.Nil(t, PreauthenticatedTokenFromContext(captor.context), "must not be preauthenticated")
}
func TestAuthenticateHttpRequest_Multitenancy_whenUserNotProvidePSI(t *testing.T) {
protectedServer := NewProtectedServer(&stubAuthenticationManager{true, nil}, false)
arbitraryRequest, _ := http.NewRequest("POST", "https://arbitraryUrl", nil)
captor := &securityContextConfigurerCaptor{}
protectedServer.authenticateHttpRequest(arbitraryRequest, captor)
assert.Nil(t, captor.context.Value(ctxRequestPrivateStateIdentifier))
assert.Nil(t, captor.context.Value(ctxPrivateStateIdentifier))
}
func TestAuthenticateHttpRequest_Multitenancy_whenPSIInURL(t *testing.T) {
arbitraryPSI := types.ToPrivateStateIdentifier("arbitrary")
protectedServer := NewProtectedServer(&stubAuthenticationManager{true, nil}, false)
arbitraryRequest, _ := http.NewRequest("POST", fmt.Sprintf("https://arbitraryUrl?%s=%s", QueryPrivateStateIdentifierParamName, arbitraryPSI.String()), nil)
captor := &securityContextConfigurerCaptor{}
protectedServer.authenticateHttpRequest(arbitraryRequest, captor)
assert.Equal(t, arbitraryPSI, captor.context.Value(ctxRequestPrivateStateIdentifier))
assert.Nil(t, captor.context.Value(ctxPrivateStateIdentifier))
}
func TestAuthenticateHttpRequest_Multitenancy_whenPSIInHTTPHeader(t *testing.T) {
arbitraryPSI := types.ToPrivateStateIdentifier("arbitrary")
protectedServer := NewProtectedServer(&stubAuthenticationManager{true, nil}, false)
arbitraryRequest, _ := http.NewRequest("POST", "https://arbitraryUrl", nil)
arbitraryRequest.Header.Set(HttpPrivateStateIdentifierHeader, arbitraryPSI.String())
captor := &securityContextConfigurerCaptor{}
protectedServer.authenticateHttpRequest(arbitraryRequest, captor)
assert.Equal(t, arbitraryPSI, captor.context.Value(ctxRequestPrivateStateIdentifier))
assert.Nil(t, captor.context.Value(ctxPrivateStateIdentifier))
}
func TestAuthenticateHttpRequest_MPS_whenTypical(t *testing.T) {
singleTenantServer := NewProtectedServer(&stubAuthenticationManager{false, nil}, false)
arbitraryRequest, _ := http.NewRequest("POST", "https://arbitraryUrl", nil)
captor := &securityContextConfigurerCaptor{}
singleTenantServer.authenticateHttpRequest(arbitraryRequest, captor)
assert.Nil(t, captor.context.Value(ctxRequestPrivateStateIdentifier))
assert.Equal(t, types.DefaultPrivateStateIdentifier, captor.context.Value(ctxPrivateStateIdentifier))
}
type securityContextConfigurerCaptor struct {
context SecurityContext
}
func (sc *securityContextConfigurerCaptor) Configure(secCtx SecurityContext) {
sc.context = secCtx
}
type stubAuthenticationManager struct {
isEnabled bool
stubErr error
}
func (s *stubAuthenticationManager) Authenticate(_ context.Context, _ string) (*proto.PreAuthenticatedAuthenticationToken, error) {
expiredAt := timestamppb.New(time.Now().Add(1 * time.Hour))
return &proto.PreAuthenticatedAuthenticationToken{
ExpiredAt: expiredAt,
}, nil
}
func (s *stubAuthenticationManager) IsEnabled(_ context.Context) (bool, error) {
return s.isEnabled, s.stubErr
}
// Quorum - This test checks that the `ID` from the RPC call is passed to the handler method
func TestServerContextIdCaptured(t *testing.T) {
var (
request = `{"jsonrpc":"2.0","id":1,"method":"test_echoCtxId"}` + "\n"
wantResp = `{"jsonrpc":"2.0","id":1,"result":1}` + "\n"
)
server := newTestServer()
defer server.Stop()
listener, err := net.Listen("tcp", "127.0.0.1:0")
if err != nil {
t.Fatal("can't listen:", err)
}
defer listener.Close()
go server.ServeListener(listener)
conn, err := net.Dial("tcp", listener.Addr().String())
if err != nil {
t.Fatal("can't dial:", err)
}
defer conn.Close()
// Write the request, then half-close the connection so the server stops reading.
conn.Write([]byte(request))
conn.(*net.TCPConn).CloseWrite()
// Now try to get the response.
buf := make([]byte, 2000)
n, err := conn.Read(buf)
assert.NoErrorf(t, err, "read error:", err)
assert.Equalf(t, buf[:n], []byte(wantResp), "wrong response: %s", buf[:n])
}