-
Notifications
You must be signed in to change notification settings - Fork 0
/
srvcon_test.go
235 lines (210 loc) · 5.65 KB
/
srvcon_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
232
233
234
235
package tt
import (
"context"
"net"
"testing"
"github.com/gregoryv/mq"
)
// Server accepts subscribe followed by unsubscribe.
func TestServer_SubscribeThenUnsubscribe(t *testing.T) {
ctx := context.Background()
conn, _ := setupClientServer(ctx, t)
{ // initiate connect sequence
mq.NewConnect().WriteTo(conn)
// ignore ack
_, _ = mq.ReadPacket(conn)
}
{ // subscribe using malformed topic filter
p := mq.NewSubscribe()
p.SetPacketID(1)
p.SetSubscriptionID(1)
p.AddFilters(mq.NewTopicFilter("a/b/#", mq.OptQoS1))
p.WriteTo(conn)
}
{ // verify ack
p, _ := mq.ReadPacket(conn)
if _, ok := p.(*mq.SubAck); !ok {
t.Errorf("expected SubAck got %T", p)
}
}
{ // unsubscribe
p := mq.NewUnsubscribe()
p.SetPacketID(1)
p.AddFilter("a/b/#")
p.WriteTo(conn)
}
{ // verify ack
p, _ := mq.ReadPacket(conn)
if _, ok := p.(*mq.UnsubAck); !ok {
t.Errorf("expected UnsubAck got %T", p)
}
}
}
// Server sends a disconnect when a client sends a subscribe
// containing a malformed topic filter.
func TestServer_DisconnectsOnMalformedSubscribe(t *testing.T) {
ctx := context.Background()
conn, _ := setupClientServer(ctx, t)
{ // initiate connect sequence
mq.NewConnect().WriteTo(conn)
// ignore ack
_, _ = mq.ReadPacket(conn)
}
{ // subscribe using malformed topic filter
p := mq.NewSubscribe()
p.SetPacketID(1)
p.SetSubscriptionID(1)
p.AddFilters(mq.NewTopicFilter("a/#/c", mq.OptQoS1))
p.WriteTo(conn)
}
// verify we recieved a disconnect packet
p, _ := mq.ReadPacket(conn)
if p, ok := p.(*mq.Disconnect); !ok {
t.Error("expected Disconnect got", p)
}
}
// If a client connects without any id set the server should assign
// one in the returning ConnAck.
func TestServer_AssignsID(t *testing.T) {
ctx := context.Background()
conn, _ := setupClientServer(ctx, t)
// initiate connect sequence without client id
mq.NewConnect().WriteTo(conn)
// verify the acc contains assigned client id
p, err := mq.ReadPacket(conn)
if p := p.(*mq.ConnAck); p.AssignedClientID() == "" {
t.Error("missing assigned client id", err)
}
}
// If a client sends Disconnect, the server should close the network
// connection.
func TestServer_CloseConnectionOnDisconnect(t *testing.T) {
ctx := context.Background()
conn, _ := setupClientServer(ctx, t)
// initiate connect sequence
mq.NewConnect().WriteTo(conn)
// ignore ack
_, _ = mq.ReadPacket(conn)
// client sends disconnect
mq.NewDisconnect().WriteTo(conn)
// verify the connection is closed
if _, err := mq.NewPublish().WriteTo(conn); err == nil {
t.Error("network Connection still open")
}
}
// If server gets a malformed packet it should disconnect with the
// reason code MalformedPacket 0x81
func TestServer_DisconnectOnMalformed(t *testing.T) {
ctx := context.Background()
conn, _ := setupClientServer(ctx, t)
{ // initiate connect sequence
mq.NewConnect().WriteTo(conn)
// ignore ack
_, _ = mq.ReadPacket(conn)
}
{ // send malformed packet
p := mq.NewPublish()
p.SetQoS(mq.QoS3)
p.WriteTo(conn)
}
{ // check expected disconnect packet
p, _ := mq.ReadPacket(conn)
if p := p.(*mq.Disconnect); p.ReasonCode() != mq.MalformedPacket {
t.Error(p)
}
}
// verify the connection is closed
if _, err := mq.NewPublish().WriteTo(conn); err == nil {
t.Error("network Connection still open")
}
}
// Publish with exceeding QoS results in disconnect.
func TestServer_DisconnectOnQoSExceed(t *testing.T) {
ctx := context.Background()
conn, _ := setupClientServer(ctx, t)
{ // initiate connect sequence
mq.NewConnect().WriteTo(conn)
// ignore ack
_, _ = mq.ReadPacket(conn)
}
{ // send publish with exceeding QoS
p := mq.NewPublish()
p.SetPacketID(1)
p.SetQoS(2)
p.SetTopicName("hello")
p.WriteTo(conn)
}
{ // check expected disconnect packet
p, _ := mq.ReadPacket(conn)
if p := p.(*mq.Disconnect); p.ReasonCode() != mq.QoSNotSupported {
t.Error(p)
}
}
// verify the connection is closed
if _, err := mq.NewPublish().WriteTo(conn); err == nil {
t.Error("network Connection still open")
}
}
// Publish with malformed topic name results in disconnect.
func TestServer_DisconnectOnMalformedTopicName(t *testing.T) {
ctx := context.Background()
conn, _ := setupClientServer(ctx, t)
{ // initiate connect sequence
mq.NewConnect().WriteTo(conn)
// ignore ack
_, _ = mq.ReadPacket(conn)
}
{ // send publish with malformed topic name
p := mq.NewPublish()
p.SetPacketID(1)
p.SetQoS(1)
p.SetTopicName("+")
p.WriteTo(conn)
}
{ // check expected disconnect packet
p, _ := mq.ReadPacket(conn)
if p := p.(*mq.Disconnect); p.ReasonCode() != mq.MalformedPacket {
t.Error(p)
}
}
// verify the connection is closed
if _, err := mq.NewPublish().WriteTo(conn); err == nil {
t.Error("network Connection still open")
}
}
// Ping results in pong
func TestServer_AnswersPingWithPong(t *testing.T) {
ctx := context.Background()
conn, _ := setupClientServer(ctx, t)
{ // initiate connect sequence
mq.NewConnect().WriteTo(conn)
// ignore ack
_, _ = mq.ReadPacket(conn)
}
{ // send ping
mq.NewPingReq().WriteTo(conn)
}
{ // check expected disconnect packet
p, _ := mq.ReadPacket(conn)
_ = p.(*mq.PingResp)
}
}
func Test_includePort(t *testing.T) {
if got := includePort("x:123", true); got != "x:123" {
t.Errorf("got %q, expected x:123", got)
}
if got := includePort("x:123", false); got != "x" {
t.Errorf("got %q, expected x", got)
}
}
// ----------------------------------------
func setupClientServer(ctx context.Context, t *testing.T) (conn, srvconn net.Conn) {
s := NewServer()
ctx, cancel := context.WithCancel(ctx)
t.Cleanup(cancel)
go s.Run(ctx)
<-s.Events()
conn, srvconn = net.Pipe()
go s.serveConn(ctx, srvconn)
return
}