forked from pebbe/zmq4
-
Notifications
You must be signed in to change notification settings - Fork 0
/
auth_test.go
144 lines (128 loc) · 3.54 KB
/
auth_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
package zmq4_test
import (
zmq "github.com/pebbe/zmq4"
"testing"
)
func TestAuthCurvePublic(t *testing.T) {
if _, minor, _ := zmq.Version(); minor < 2 {
t.Skip("CurvePublic not available in ZeroMQ versions prior to 4.2.0")
}
expected := "Yne@$w-vo<fVvi]a<NY6T1ed:M$fCG*[IaLV{hID"
public, err := zmq.AuthCurvePublic("D:)Q[IlAW!ahhC2ac:9*A}h:p?([4%wOTJ%JR%cs")
if err != nil {
t.Fatal(err)
}
if public != expected {
t.Fatalf("Expected: %s, got: %s", expected, public)
}
public, err = zmq.AuthCurvePublic("blabla")
if err == nil {
t.Fatal("Error expected")
}
}
func TestAuthStart(t *testing.T) {
if _, minor, _ := zmq.Version(); minor >= 1 && !zmq.HasCurve() {
t.Skip("Curve not available")
}
type Meta struct {
key string
value string
ok bool
}
zmq.AuthSetVerbose(false)
// Start authentication engine
err := zmq.AuthStart()
if err != nil {
t.Fatal("AuthStart:", err)
}
defer zmq.AuthStop()
zmq.AuthSetMetadataHandler(
func(version, request_id, domain, address, identity, mechanism string, credentials ...string) (metadata map[string]string) {
return map[string]string{
"Identity": identity,
"User-Id": "anonymous",
"Hello": "World!",
"Foo": "Bar",
}
})
zmq.AuthAllow("domain1", "127.0.0.1")
// We need two certificates, one for the client and one for
// the server. The client must know the server's public key
// to make a CURVE connection.
client_public, client_secret, err := zmq.NewCurveKeypair()
if err != nil {
t.Fatal("NewCurveKeypair:", err)
}
server_public, server_secret, err := zmq.NewCurveKeypair()
if err != nil {
t.Fatal("NewCurveKeypair:", err)
}
// Tell authenticator to use this public client key
zmq.AuthCurveAdd("domain1", client_public)
// Create and bind server socket
server, err := zmq.NewSocket(zmq.DEALER)
if err != nil {
t.Fatal("NewSocket:", err)
}
defer func() {
server.SetLinger(0)
server.Close()
}()
server.SetIdentity("Server1")
server.ServerAuthCurve("domain1", server_secret)
err = server.Bind("tcp://*:9000")
if err != nil {
t.Fatal("server.Bind:", err)
}
// Create and connect client socket
client, err := zmq.NewSocket(zmq.DEALER)
if err != nil {
t.Fatal("NewSocket:", err)
}
defer func() {
client.SetLinger(0)
client.Close()
}()
server.SetIdentity("Client1")
client.ClientAuthCurve(server_public, client_public, client_secret)
err = client.Connect("tcp://127.0.0.1:9000")
if err != nil {
t.Fatal("client.Connect:", err)
}
// Send a message from client to server
msg := []string{"Greetings", "Earthlings!"}
_, err = client.SendMessage(msg[0], msg[1])
if err != nil {
t.Fatal("client.SendMessage:", err)
}
// Receive message and metadata on the server
tests := []Meta{
{"Identity", "Server1", true},
{"User-Id", "anonymous", true},
{"Socket-Type", "DEALER", true},
{"Hello", "World!", true},
{"Foo", "Bar", true},
{"Fuz", "", false},
}
keys := make([]string, len(tests))
for i, test := range tests {
keys[i] = test.key
}
message, metadata, err := server.RecvMessageWithMetadata(0, keys...)
if err != nil {
t.Fatal("server.RecvMessageWithMetadata:", err)
}
if !arrayEqual(message, msg) {
t.Errorf("Received message was %q, expected %q", message, msg)
}
if _, minor, _ := zmq.Version(); minor < 1 {
t.Log("Metadata not avalable in ZeroMQ versions prior to 4.1.0")
} else {
for _, test := range tests {
value, ok := metadata[test.key]
if value != test.value || ok != test.ok {
t.Errorf("Metadata %s, expected %q %v, got %q %v", test.key, test.value, test.ok, value, ok)
}
}
}
}