-
Notifications
You must be signed in to change notification settings - Fork 1
/
server_client_test.go
77 lines (65 loc) · 1.54 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
package votifier
import (
"crypto/rsa"
"errors"
"net"
"strings"
"testing"
)
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 := 1; i <= 2; i++ {
vl := func(rv Vote, ver int) {
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
if i == 1 {
pk := key.PublicKey
client = NewV1Client(listener.Addr().String(), &pk)
} else {
client = NewV2Client(listener.Addr().String(), "abcxyz")
}
server := NewServer(key, vl, StaticServiceTokenIdentifier("abcxyz"))
go server.Serve(listener)
err = client.SendVote(v)
if err != nil {
t.Error(err)
}
}
}
func TestServerv2Panic(t *testing.T) {
vl := func(rv Vote, ver int) {
panic(errors.New("boom"))
}
listener, err := net.Listen("tcp", "127.0.0.1:0")
if err != nil {
t.Error(err)
}
defer listener.Close()
server := NewServer(nil, vl, StaticServiceTokenIdentifier("abcxyz"))
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")
}
}