forked from DanielKrawisz/bmd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrpcserver_test.go
229 lines (191 loc) · 6.18 KB
/
rpcserver_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
// Copyright (c) 2015 Monetas.
// Copyright 2016 Daniel Krawisz.
// Use of this source code is governed by an ISC
// license that can be found in the LICENSE file.
package main
import (
"bytes"
"net"
"testing"
"github.com/DanielKrawisz/bmd/peer"
pb "github.com/DanielKrawisz/bmd/rpcproto"
"github.com/DanielKrawisz/bmutil/wire"
"golang.org/x/net/context"
"google.golang.org/grpc"
"google.golang.org/grpc/codes"
)
const (
rpcLoc = "localhost:8442"
rpcAdminUser = "admin"
rpcAdminPass = "admin"
rpcLimitUser = "limit"
rpcLimitPass = "limit"
)
var serv *server
func rpcTests(t *testing.T) {
// Order of the tests is important. Do not change.
testRPCAuth(t)
// Set up a connection to the server.
conn, err := grpc.Dial(rpcLoc, grpc.WithInsecure(),
grpc.WithPerRPCCredentials(pb.NewBasicAuthCredentials(rpcAdminUser, rpcAdminPass)))
if err != nil {
t.Fatalf("did not connect: %v", err)
}
defer conn.Close()
c := pb.NewBmdClient(conn)
testRPCSendObject(c, t)
testRPCGetObjects(c, t)
}
// testRPCAuth tests authentication failures for all RPC methods.
func testRPCAuth(t *testing.T) {
// Try accessing methods with no credentials.
conn, err := grpc.Dial(rpcLoc, grpc.WithInsecure())
if err != nil {
t.Fatalf("did not connect: %v", err)
}
c := pb.NewBmdClient(conn)
testRPCAuthFailure(c, t, codes.Unauthenticated)
conn.Close()
// Try accessing methods with invalid credentials.
conn, err = grpc.Dial(rpcLoc, grpc.WithInsecure(),
grpc.WithPerRPCCredentials(pb.NewBasicAuthCredentials("blahblah", "blah blah")))
if err != nil {
t.Fatalf("did not connect: %v", err)
}
c = pb.NewBmdClient(conn)
testRPCAuthFailure(c, t, codes.PermissionDenied)
conn.Close()
}
func testRPCAuthFailure(c pb.BmdClient, t *testing.T, expectedCode codes.Code) {
_, err := c.SendObject(context.Background(), &pb.Object{})
if grpc.Code(err) != expectedCode {
t.Errorf("Expected code %d, got unexpected error %v", expectedCode, err)
}
_, err = c.GetIdentity(context.Background(), &pb.GetIdentityRequest{})
if grpc.Code(err) != expectedCode {
t.Errorf("Expected code %d, got unexpected error %v", expectedCode, err)
}
stream, err := c.GetObjects(context.Background(), &pb.GetObjectsRequest{})
if err != nil {
t.Error(err)
}
_, err = stream.Recv()
if grpc.Code(err) != expectedCode {
t.Errorf("Expected code %d, got unexpected error %v", expectedCode, err)
}
}
// Test SendObject.
func testRPCSendObject(c pb.BmdClient, t *testing.T) {
errorTests := [][]byte{
[]byte{}, // empty
[]byte{0x00, 0x00, 0x00, 0x00}, // invalid object
[]byte{ // expired
0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0xe0, 0xf3, // Nonce
0x00, 0x00, 0x00, 0x00, 0x49, 0x5f, 0xab, 0x29, // 64-bit Timestamp
0x00, 0x00, 0x00, 0x00, // object type
0x03, // Version
0x01, // Stream Number
},
[]byte{ // invalid stream
0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0xe0, 0xf3, // Nonce
0x00, 0x00, 0x00, 0xff, 0x49, 0x5f, 0xab, 0x29, // 64-bit Timestamp
0x00, 0x00, 0x00, 0x00, // object type
0x03, // Version
0x05, // Stream Number
},
[]byte{ // invalid PoW
0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0xe0, 0xf3, // Nonce
0x00, 0x00, 0x00, 0xff, 0x49, 0x5f, 0xab, 0x29, // 64-bit Timestamp
0x00, 0x00, 0x00, 0x00, // object type
0x03, // Version
0x01, // Stream Number
},
}
for i, test := range errorTests {
_, err := c.SendObject(context.Background(), &pb.Object{Contents: test})
if grpc.Code(err) != codes.InvalidArgument {
t.Errorf("for case #%d got unexpected error %v", i, err)
}
}
// Insert valid object.
data := wire.EncodeMessage(testObj[0]) // getpubkey
ret, err := c.SendObject(context.Background(), &pb.Object{Contents: data})
if err != nil {
t.Errorf("for valid SendObject got error %v", err)
} else if 1 != ret.Counter {
t.Errorf("For counter expected %d got %d", 1, ret.Counter)
}
hash := toMsgObject(testObj[0]).InventoryHash()
// Check if advertised.
if ok, err := serv.objectManager.HaveInventory(wire.NewInvVect(hash)); !ok {
t.Error("server doesn't have new object in inventory, error:", err)
}
// Try inserting object again.
_, err = c.SendObject(context.Background(), &pb.Object{Contents: data})
if grpc.Code(err) != codes.AlreadyExists {
t.Errorf("got unexpected error %v", err)
}
}
func testRPCGetObjects(c pb.BmdClient, t *testing.T) {
// Receive Getpubkey inserted in previous test.
stream, err := c.GetObjects(context.Background(), &pb.GetObjectsRequest{
ObjectType: pb.ObjectType_GETPUBKEY,
FromCounter: 1,
})
if err != nil {
t.Fatal(err)
}
objMsg, err := stream.Recv()
if err != nil {
t.Fatal(err)
}
data := wire.EncodeMessage(testObj[0]) // getpubkey
if !bytes.Equal(data, objMsg.Contents) {
t.Errorf("invalid getpubkey bytes, expected %v got %v", data, objMsg.Contents)
}
// Test if objects inserted after subscribing are handled correctly.
data = wire.EncodeMessage(testObj[1]) // getpubkey
_, err = c.SendObject(context.Background(), &pb.Object{Contents: data})
if err != nil {
t.Errorf("for valid SendObject got error %v", err)
}
objMsg, err = stream.Recv()
if err != nil {
t.Fatal(err)
}
if !bytes.Equal(data, objMsg.Contents) {
t.Errorf("invalid getpubkey bytes, expected %v got %v", data, objMsg.Contents)
}
}
func TestRPCConnection(t *testing.T) {
// Address for mock listener to pass to server. The server
// needs at least one listener or it won't start so we mock it.
remoteAddr := &net.TCPAddr{IP: net.ParseIP("127.0.0.1"), Port: 8445}
// Set config.
cfg.MaxPeers = 0
cfg.RPCPass = rpcAdminUser
cfg.RPCUser = rpcAdminPass
cfg.RPCLimitUser = rpcLimitUser
cfg.RPCLimitPass = rpcLimitPass
cfg.DisableRPC = false
cfg.DisableTLS = true
cfg.RPCMaxClients = 1
defer resetCfg(cfg)()
// Set RPC listener.
cfg.RPCListeners = []string{net.JoinHostPort("", "8442")}
var err error
// Create a server.
listeners := []string{net.JoinHostPort("", "8445")}
serv, err = newServer(listeners, getMemDb([]*wire.MsgObject{}),
MockListen([]*MockListener{
NewMockListener(remoteAddr, make(chan peer.Connection), make(chan struct{}, 1))}), nil)
if err != nil {
t.Fatalf("Server creation failed: %s", err)
}
serv.Start()
// Run the actual tests.
rpcTests(t)
// Cleanup.
serv.Stop()
serv.WaitForShutdown()
}