forked from jcelliott/turnpike
-
Notifications
You must be signed in to change notification settings - Fork 3
/
client_test.go
231 lines (197 loc) · 5.88 KB
/
client_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
package turnpike
import (
"fmt"
"testing"
. "github.com/smartystreets/goconvey/convey"
)
type testPeer struct {
messages chan Message
sentMessages []Message
}
func (t *testPeer) Send(msg Message) error {
t.sentMessages = append(t.sentMessages, msg)
switch msg := msg.(type) {
case *Hello:
if _, ok := msg.Details["authmethods"]; !ok {
t.messages <- &Welcome{
Id: NewID(),
Details: make(map[string]interface{}),
}
} else {
t.messages <- &Challenge{
AuthMethod: "testauth",
Extra: map[string]interface{}{"challenge": "password"},
}
}
case *Authenticate:
if msg.Signature == "passwordpassword" {
t.messages <- &Welcome{
Id: NewID(),
Details: make(map[string]interface{}),
}
} else {
t.messages <- &Abort{
Reason: URI("turnpike.error.invalid_auth_signature"),
}
}
case *Register:
// Only allow methods named "mymethod" to be registered.
if msg.Procedure == "mymethod" {
args := make([]interface{}, 0)
args = append(args, 1234)
t.messages <- &Registered{
Request: msg.Request,
Registration: 4567,
}
} else {
t.messages <- &Error{
Type: REGISTER,
Request: msg.Request,
Details: msg.Options,
Error: WAMP_ERROR_INVALID_URI,
Arguments: make([]interface{}, 0),
ArgumentsKw: make(map[string]interface{}),
}
}
case *Yield:
// Transform the yield into a result, and send it back to the client.
t.messages <- &Result{
Request: msg.Request,
Details: msg.Options,
Arguments: msg.Arguments,
ArgumentsKw: msg.ArgumentsKw,
}
case *Call:
// testmethod: A method called by the client test (fake)
// mymethod: A method called by the server test (does real work)
if msg.Procedure == "testmethod" {
args := make([]interface{}, 0)
args = append(args, 1234)
t.messages <- &Result{
Request: msg.Request,
Details: msg.Options,
Arguments: args,
ArgumentsKw: make(map[string]interface{}),
}
} else if msg.Procedure == "mymethod" {
t.messages <- &Invocation{
Request: msg.Request,
Registration: 4567, // Must match the registered message above.
Details: msg.Options,
Arguments: msg.Arguments,
ArgumentsKw: msg.ArgumentsKw,
}
} else {
t.messages <- &Error{
Type: CALL,
Request: msg.Request,
Details: msg.Options,
Error: "unknown method",
Arguments: make([]interface{}, 0),
ArgumentsKw: make(map[string]interface{}),
}
}
}
return nil
}
func (t *testPeer) Close() error {
return nil
}
func (t *testPeer) Receive() <-chan Message {
return t.messages
}
func newTestPeer() *testPeer {
return &testPeer{
messages: make(chan Message, 2),
}
}
func connectedTestClients() (*Client, *Client) {
peer := newTestPeer()
return newTestClient(peer), newTestClient(peer)
}
func newTestClient(p Peer) *Client {
client := NewClient(p)
_, err := client.JoinRealm("test.realm", ALLROLES, nil)
So(err, ShouldBeNil)
return client
}
func TestJoinRealm(t *testing.T) {
Convey("Given a server accepting client connections", t, func() {
server := newTestPeer()
Convey("A client should be able to succesfully join a realm", func() {
client := NewClient(server)
_, err := client.JoinRealm("test.realm", ALLROLES, nil)
So(err, ShouldBeNil)
})
})
}
func testAuthFunc(d map[string]interface{}, c map[string]interface{}) (string, map[string]interface{}, error) {
key := c["challenge"].(string)
if key == "fail" {
return "", map[string]interface{}{}, fmt.Errorf("authentication failed")
}
signature := key + key // it's super effective!
return signature, map[string]interface{}{}, nil
}
func TestJoinRealmCRA(t *testing.T) {
Convey("Given a server accepting client connections", t, func() {
server := newTestPeer()
Convey("A client should be able to successfully authenticate and join a realm", func() {
details := map[string]interface{}{"authmethods": []string{"testauth"}}
auth := map[string]AuthFunc{"testauth": testAuthFunc}
client := NewClient(server)
_, err := client.JoinRealmCRA("test.realm", ALLROLES, details, auth)
So(err, ShouldBeNil)
})
})
}
func TestRemoteCall(t *testing.T) {
Convey("Given two clients connected to the same server", t, func() {
callee, caller := connectedTestClients()
Convey("The callee registers an invalid method", func() {
handler := func(args []interface{}, kwargs map[string]interface{}) *CallResult {
return nil
}
err := callee.Register("invalidmethod", handler)
Convey("And expects an error", func() {
So(err, ShouldNotBeNil)
})
})
Convey("The callee registers a valid method", func() {
handler := func(args []interface{}, kwargs map[string]interface{}) *CallResult {
return &CallResult{Args: []interface{}{args[0].(int) * 2}}
}
err := callee.Register("mymethod", handler)
Convey("And expects no error", func() {
So(err, ShouldBeNil)
Convey("The caller calls the callee's remote method", func() {
callArgs := []interface{}{5100}
result, err := caller.Call("mymethod", callArgs, make(map[string]interface{}))
Convey("And succeeds at multiplying the number by 2", func() {
So(err, ShouldBeNil)
So(result.Arguments[0], ShouldEqual, 10200)
})
})
})
})
})
}
func TestClientCall(t *testing.T) {
Convey("Given a client connected to a server", t, func() {
server := newTestPeer()
client := newTestClient(server)
Convey("The client calls a valid method", func() {
result, err := client.Call("testmethod", []interface{}{}, map[string]interface{}{})
Convey("And expects a result", func() {
So(err, ShouldBeNil)
So(result.Arguments[0], ShouldEqual, 1234)
})
})
Convey("The client calls an invalid method", func() {
_, err := client.Call("invalidmethod", []interface{}{}, map[string]interface{}{})
Convey("And expects an error", func() {
So(err, ShouldNotBeNil)
})
})
})
}