-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathserver_client_test.go
96 lines (83 loc) · 1.91 KB
/
server_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
package votifier
import (
"crypto/rsa"
"errors"
"net"
"strings"
"testing"
)
var (
Protocols = []VotifierProtocol{VotifierV1, VotifierV2}
)
func TestServer(t *testing.T) {
v := NewVote("golang", "golang", "127.0.0.1")
// Generate a set of keys for later use
key, err := rsa.GenerateKey(new(badRandomReader), 2048)
if err != nil {
t.Error(err)
return
}
for _, i := range Protocols {
vl := func(rv Vote, ver VotifierProtocol, meta interface{}) {
if rv != v {
t.Error("Vote received did not match original")
}
if ver != i {
t.Error("Vote is not v" + string(i))
}
}
listener, err := net.Listen("tcp", "127.0.0.1:0")
if err != nil {
t.Error(err)
}
defer listener.Close()
var client Client
switch i {
case VotifierV1:
pk := key.PublicKey
client = NewV1Client(listener.Addr().String(), &pk)
case VotifierV2:
client = NewV2Client(listener.Addr().String(), "abcxyz")
}
r := []ReceiverRecord{
ReceiverRecord{
PrivateKey: key,
TokenId: StaticServiceTokenIdentifier("abcxyz"),
Metadata: nil,
},
}
server := NewServer(vl, r)
go server.Serve(listener)
err = client.SendVote(v)
if err != nil {
t.Error(err)
}
}
}
func TestServerv2Panic(t *testing.T) {
vl := func(rv Vote, ver VotifierProtocol, meta interface{}) {
panic(errors.New("boom"))
}
listener, err := net.Listen("tcp", "127.0.0.1:0")
if err != nil {
t.Error(err)
}
defer listener.Close()
r := []ReceiverRecord{
ReceiverRecord{
PrivateKey: nil,
TokenId: StaticServiceTokenIdentifier("abcxyz"),
Metadata: nil,
},
}
server := NewServer(vl, r)
go server.Serve(listener)
client := NewV2Client(listener.Addr().String(), "abcxyz")
err = client.SendVote(NewVote("golang", "golang", "127.0.0.1"))
if err == nil {
t.Error("expected error, but didn't get any")
}
if !strings.HasSuffix(err.Error(), "panic: boom") {
t.Error("invalid error from error")
}
}