/
interface_test.go
213 lines (170 loc) · 4.1 KB
/
interface_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
package esl
import (
"bytes"
"strings"
"testing"
"time"
)
func TestBasicConnectionSendRecv(t *testing.T) {
if testing.Short() {
t.Skip()
}
socket, err := Dial(eslHost, eslPasword, 1, 30*time.Second)
if err != nil {
t.Errorf("Dial error: %s", err)
return
}
if socket == nil {
t.Errorf("Socket is nil")
return
}
defer socket.Close()
n, content, err := socket.Recv(AuthRequestBufferSize)
if err != nil {
t.Errorf("Unable to read content: %s", err)
}
if content == nil {
t.Error("Content is nil")
}
if len(content) == 0 {
t.Error("Content cannot be empty")
}
if int64(n) >= AuthRequestBufferSize {
t.Errorf("n cannot be 28")
}
found := bytes.HasPrefix(content, []byte("Content-Type: auth/request\n\n"))
if !found {
t.Errorf("Expected 'Content-Type: auth/request', got: '%s' | %+v | side: %t", content, content, found)
}
n, content, err = socket.SendRecv("auth " + eslPasword)
if err != nil {
t.Errorf("Login error: %s", err)
return
}
if n != 54 {
t.Errorf("Expected length of 54, got: %d", n)
}
expected := []byte("Content-Type: command/reply\nReply-Text: +OK accepted\n\n")
cmp := bytes.Compare(expected, content[0:n])
if cmp != 0 {
t.Errorf("Expected '%s' %+v \ngot: '%s' | %+v | side: %d\n",
expected, expected, content, content, cmp)
}
}
func TestSocketSendRecevError(t *testing.T) {
socket := &Socket{}
n, buf, err := socket.SendRecv("foo")
if err == nil {
t.Errorf("Expected error but got nil")
}
if buf != nil {
t.Errorf("Expected nil buf, but got: '%s'", buf)
}
if n != 0 {
t.Errorf("Expected n 0 but got '%d'", n)
}
}
func TestSocketAPI(t *testing.T) {
if testing.Short() {
t.Skip()
}
socket, err := Connect(eslHost, eslPasword, 1, 30*time.Second)
if err != nil {
t.Errorf("Unable to connect: %s", err)
return
}
if socket == nil {
t.Errorf("Socket is nil without an error")
return
}
defer socket.Close()
msg, err := socket.API("show", "api")
if err != nil {
t.Errorf("Unable to call API: %s", err)
return
}
if msg == nil {
t.Errorf("No message returned without an error")
return
}
ct := msg.ContentType()
if ct != ECTAPIResponse {
t.Errorf("Invalid content type, expected '%s' got '%s'", ECTAPIResponse, ct)
return
}
l := msg.Headers.GetInt("Content-Length")
if l <= 0 {
t.Errorf("l (%d) cannot be empty", l)
return
}
bodyLength := int64(len(msg.Body))
if bodyLength != l-1 && bodyLength != l {
t.Errorf("Expected body length to be %d but got %d", l, bodyLength)
return
}
if !bytes.HasSuffix(msg.Body, []byte(" total.")) {
t.Errorf("Unexpected end of API commands.")
return
}
}
func TestSocketAPIConnectError(t *testing.T) {
socket := &Socket{}
msg, err := socket.API("show", "")
if err == nil {
t.Errorf("Expected err, but nil returned")
}
if msg != nil {
t.Errorf("Expected msg to be nil, but got '%s'", msg)
}
}
func TestSocketBgAPI(t *testing.T) {
if testing.Short() {
t.Skip()
}
socket, err := Connect(eslHost, eslPasword, 1, 30*time.Second)
if err != nil {
t.Errorf("Unable to connect: %s", err)
return
}
if socket == nil {
t.Errorf("Socket is nil without an error")
return
}
defer socket.Close()
msg, err := socket.BgAPI("show", "api")
if err != nil {
t.Errorf("Unable to call API: %s", err)
return
}
if msg == nil {
t.Errorf("No message returned without an error")
return
}
ct := msg.ContentType()
if ct != ECTCommandReply {
t.Errorf("Invalid content type, expected '%s' got '%s'", ECTCommandReply, ct)
return
}
if !msg.Headers.Exists("Reply-Text") {
t.Errorf("Expected Header of 'Reply-Text', but go headers of: %v", msg.Headers.Keys())
return
}
if msg.HasError() {
t.Errorf("Expected UUI, but got error of: %s", msg.Error())
return
}
text := msg.Headers.GetString("Reply-Text")
if !strings.HasPrefix(text, "+OK Job-UUID:") {
t.Errorf("Expected '+OK Job-UUID:' prefix but got: %s", text)
}
}
func TestSocketBgAPIError(t *testing.T) {
socket := &Socket{}
msg, err := socket.BgAPI("show", "")
if err == nil {
t.Errorf("Expected err, but nil returned")
}
if msg != nil {
t.Errorf("Expected msg to be nil, but got '%s'", msg)
}
}