-
Notifications
You must be signed in to change notification settings - Fork 4
/
client_test.go
260 lines (219 loc) · 6.18 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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
package goami2
import (
"bufio"
"context"
"net"
"strings"
"testing"
"time"
"github.com/stretchr/testify/assert"
)
func connSrvSess(conn net.Conn, repl []string) {
go func() {
buf := make([]byte, 1024)
_, _ = conn.Write([]byte("Asterisk Call Manager/2.10.4\n"))
_, _ = conn.Read(buf)
for _, data := range repl {
_, _ = conn.Write([]byte(data))
}
}()
}
func TestClientLogin(t *testing.T) {
connClint, connSrv := net.Pipe()
cl := makeClient(connClint)
t.Run("timeout read prompt", func(t *testing.T) {
defer func() { cl.timeout = netTimeout }()
cl.timeout = time.Nanosecond
err := cl.login("admin", "pwd")
assert.ErrorContains(t, err, "i/o timeout")
})
t.Run("fail on invalid prompt", func(t *testing.T) {
go func() {
_, _ = connSrv.Write([]byte("foo bar"))
}()
err := cl.login("admin", "pwd")
assert.ErrorContains(t, err, "unexpected prompt")
})
t.Run("fail on invalid AMI message", func(t *testing.T) {
connSrvSess(connSrv, []string{"invalid message\r\n\r\n"})
err := cl.login("admin", "pwd")
assert.ErrorContains(t, err, "failed to read login response")
})
t.Run("fail on response status fail", func(t *testing.T) {
connSrvSess(connSrv,
[]string{"Response: Error\r\nMessage: Authentication failed\r\n\r\n"})
err := cl.login("admin", "pwd")
assert.ErrorContains(t, err, "Authentication failed")
})
t.Run("login successfully", func(t *testing.T) {
connSrvSess(connSrv,
[]string{"Response: Success\r\nMessage: Authentication accepted\r\n\r\n"})
err := cl.login("admin", "pwd")
assert.Nil(t, err)
})
t.Run("write to closed connection", func(t *testing.T) {
_ = connSrv.Close()
err := cl.login("admin", "pwd")
assert.ErrorContains(t, err, "failed setup read timeout")
})
}
func TestClientClose(t *testing.T) {
setup := func() *Client {
connClint, _ := net.Pipe()
return makeClient(connClint)
}
t.Run("close connection and channels", func(t *testing.T) {
cl := setup()
assert.NotNil(t, cl.conn)
assert.NotNil(t, cl.recv)
assert.NotNil(t, cl.err)
cl.Close()
assert.Nil(t, cl.conn)
assert.True(t, isClosedChan(cl.recv))
assert.True(t, isClosedChan(cl.err))
})
t.Run("not panic on multiple close call", func(t *testing.T) {
cl := setup()
cl.Close()
assert.Nil(t, cl.conn)
assert.True(t, isClosedChan(cl.recv))
assert.True(t, isClosedChan(cl.err))
assert.NotPanics(t, func() { cl.Close() })
})
t.Run("not panic on", func(t *testing.T) {
tests := map[string]func(*Client){
`close conn`: func(cl *Client) { cl.conn.Close() },
`close conn and recv chan`: func(cl *Client) {
cl.conn.Close()
close(cl.recv)
},
`close conn and recv and err chan`: func(cl *Client) {
cl.conn.Close()
close(cl.recv)
close(cl.err)
},
}
for name, init := range tests {
t.Run(name, func(t *testing.T) {
cl := setup()
init(cl)
assert.NotPanics(t, func() { cl.Close() })
})
}
})
}
func TestClientLoopRead(t *testing.T) {
setup := func() (net.Conn, net.Conn, *Client) {
connClint, connSrv := net.Pipe()
cl := makeClient(connClint)
return connClint, connSrv, cl
}
t.Run("stop on context done", func(t *testing.T) {
_, _, cl := setup()
chErr := cl.Err()
ctx, cancel := context.WithCancel(context.Background())
go cl.loop(ctx)
cancel()
err := <-chErr
assert.ErrorIs(t, err, ErrEOF)
cl.Close()
})
t.Run("stop on conn read error", func(t *testing.T) {
conn, _, cl := setup()
chErr := cl.Err()
go cl.loop(context.Background())
_ = conn.Close()
err := <-chErr
assert.ErrorIs(t, err, ErrEOF)
cl.Close()
})
t.Run("conn read invalid AMI package", func(t *testing.T) {
_, connSrv, cl := setup()
go cl.loop(context.Background())
_, _ = connSrv.Write([]byte("hello\r\nbye\r\n\r\n"))
err := <-cl.Err()
assert.ErrorIs(t, err, ErrAMI)
// loop is still running
_, _ = connSrv.Write([]byte("Response: Success\r\nMessage: Access granted\r\n\r\n"))
msg := <-cl.AllMessages()
assert.Equal(t, "Success", msg.Field("Response"))
assert.Equal(t, "Access granted", msg.Field("Message"))
})
}
func TestClientWriteToConnection(t *testing.T) {
connClient, connSrv := net.Pipe()
cl := makeClient(connClient)
buf := bufio.NewReader(connSrv)
t.Run("Send method", func(t *testing.T) {
cl.Send([]byte("Action: CoreStatus\n"))
s, err := buf.ReadString('\n')
assert.Nil(t, err)
assert.Equal(t, "Action: CoreStatus\n", s)
})
t.Run("MustSend success", func(t *testing.T) {
go func() {
err := cl.MustSend([]byte("must send\n"))
assert.Nil(t, err)
}()
s, err := buf.ReadString('\n')
assert.Nil(t, err)
assert.Equal(t, "must send\n", s)
})
t.Run("Action success", func(t *testing.T) {
go func() {
msg := NewAction("Uptime")
ok := cl.Action(msg)
assert.True(t, ok)
}()
s, err := buf.ReadString('\n')
assert.Nil(t, err)
assert.Equal(t, "Action: Uptime\r\n", s)
})
t.Run("MustSend and Action fail", func(t *testing.T) {
_ = connClient.Close()
msg := NewAction("Uptime")
ok := cl.Action(msg)
assert.False(t, ok)
err := cl.MustSend([]byte("must send\n"))
assert.ErrorContains(t, err, "io: read/write on closed")
})
t.Run("MustSend returns error when client is closed", func(t *testing.T) {
conn, _ := net.Pipe()
client := makeClient(conn)
client.Close()
err := client.MustSend([]byte("must send\n"))
assert.ErrorContains(t, err, "closed connection")
})
t.Run("MustSend timeout", func(t *testing.T) {
conn, _ := net.Pipe()
client := makeClient(conn)
client.timeout = 1 * time.Millisecond
err := client.MustSend([]byte("must send\n"))
assert.ErrorContains(t, err, "write pipe: i/o timeout")
})
}
func TestClientLoopStreamRead(t *testing.T) {
packets := getAmiFixtureCall()
input := strings.Join(packets, "")
connClient, connSrv := net.Pipe()
client := makeClient(connClient)
go func() {
_, err := connSrv.Write([]byte(input))
if err != nil {
t.Error(err.Error())
}
}()
go client.loop(context.Background())
for i := 0; i < len(packets); i++ {
msg := <-client.AllMessages()
assert.Equal(t, msg.String(), packets[i])
}
}
func TestIsClosedChan(t *testing.T) {
foo := make(chan struct{})
assert.False(t, isClosedChan(foo))
close(foo)
assert.True(t, isClosedChan(foo))
foo = nil
assert.True(t, isClosedChan(foo))
}