forked from foxcpp/go-assuan
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server_test.go
223 lines (199 loc) · 5.6 KB
/
server_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
package server
import (
"bytes"
"io/ioutil"
"strings"
"testing"
"github.com/foxcpp/go-assuan/common"
)
func TestInquire(t *testing.T) {
sample := `D FOO
END
D BAR
END
D BAZ
END
`
pipe := common.NewPipe(strings.NewReader(sample), ioutil.Discard)
data, err := Inquire(&pipe, []string{"foo", "bar", "baz"})
if err != nil {
t.Error("Unexpected Inquire error:", err)
t.FailNow()
}
if string(data["foo"]) != "FOO" {
t.Error("Missing or incorrect data read:", data)
t.FailNow()
}
if string(data["bar"]) != "BAR" {
t.Error("Missing or incorrect data read:", data)
t.FailNow()
}
if string(data["baz"]) != "BAZ" {
t.Error("Missing or incorrect data read:", data)
t.FailNow()
}
}
func TestHandleCmd(t *testing.T) {
t.Run("BYE cmd", func(t *testing.T) {
buf := bytes.Buffer{}
pipe := common.NewPipe(nil, &buf)
if err := handleCmd(&pipe, "BYE", "", ProtoInfo{}, nil); err != nil {
t.Error("Unexpected handleCmd error:", err)
t.FailNow()
}
if buf.String() != "OK\n" {
t.Error("Response to BYE is not OK:", buf.String())
}
})
t.Run("RESET cmd (default handler)", func(t *testing.T) {
buf := bytes.Buffer{}
pipe := common.NewPipe(nil, &buf)
state := interface{}("foobar")
if err := handleCmd(&pipe, "RESET", "", ProtoInfo{}, &state); err != nil {
t.Error("Unexpected handleCmd error:", err)
t.FailNow()
}
if buf.String() != "OK\n" {
t.Error("Response to RESET is not OK:", buf.String())
}
})
t.Run("HELP cmd", helpTest)
t.Run("OPTION cmd", optionsTest)
t.Run("custom cmd", customCmdTest)
}
func helpTest(t *testing.T) {
t.Run("commands list", func(t *testing.T) {
buf := bytes.Buffer{}
pipe := common.NewPipe(nil, &buf)
if err := handleCmd(&pipe, "HELP", "", ProtoInfo{}, nil); err != nil {
t.Error("Unexpected handleCmd error:", err)
t.FailNow()
}
for _, line := range strings.Split(buf.String(), "\n") {
if line == "" {
// empty lines are ok in assuan actually
continue
}
if !strings.HasPrefix(line, "#") && line != "OK" {
t.Error("Response contains non-comment lines other than OK:", "'" + line + "'")
t.Error(buf.String())
t.FailNow()
}
}
})
t.Run("help for non-existent cmd", func(t *testing.T) {
buf := bytes.Buffer{}
pipe := common.NewPipe(nil, &buf)
if err := handleCmd(&pipe, "HELP", "CCMD", ProtoInfo{}, nil); err != nil {
t.Error("Unexpected handleCmd error:", err)
t.FailNow()
}
if !strings.HasPrefix(buf.String(), "ERR") {
t.Error("HELP command not failed")
t.Error(buf.String())
}
})
t.Run("help for cmd", func(t *testing.T) {
buf := bytes.Buffer{}
pipe := common.NewPipe(nil, &buf)
proto := ProtoInfo{}
proto.Help = make(map[string][]string)
proto.Help["CCMD"] = []string{"help string"}
if err := handleCmd(&pipe, "HELP", "CCMD", proto, nil); err != nil {
t.Error("Unexpected handleCmd error:", err)
t.FailNow()
}
if buf.String() != "# help string\nOK\n" {
t.Error("Mismatched output:")
t.Error(buf.String())
}
})
}
func customCmdTest(t *testing.T) {
t.Run("unknown cmd", func(t *testing.T) {
buf := bytes.Buffer{}
pipe := common.NewPipe(nil, &buf)
if err := handleCmd(&pipe, "CCMD", "test", ProtoInfo{}, nil); err != nil {
t.Error("Unexpected handleCmd error:", err)
t.FailNow()
}
if !strings.HasPrefix(buf.String(), "ERR") {
t.Error("CCMD command not failed")
t.Error(buf.String())
}
})
t.Run("common.Error from cmd handler", func(t *testing.T) {
buf := bytes.Buffer{}
pipe := common.NewPipe(nil, &buf)
proto := ProtoInfo{}
proto.Handlers = make(map[string]CommandHandler)
proto.Handlers["CCMD"] = func(_ *common.Pipe, _ interface{}, _ string) error {
return &common.Error{
Src: common.ErrSrcAssuan, Code: common.ErrAssUnknownCmd,
SrcName: "assuan", Message: "TEST ERROR",
}
}
if err := handleCmd(&pipe, "CCMD", "", proto, nil); err != nil {
t.Error("Unexpected handleCmd error:", err)
t.FailNow()
}
if !strings.HasPrefix(buf.String(), "ERR") {
t.Error("OPTION command not failed")
t.Error(buf.String())
}
})
}
func optionsTest(t *testing.T) {
t.Run("no OPTION support", func(t *testing.T) {
buf := bytes.Buffer{}
pipe := common.NewPipe(nil, &buf)
if err := handleCmd(&pipe, "OPTION", "a 2", ProtoInfo{}, nil); err != nil {
t.Error("Unexpected handleCmd error:", err)
t.FailNow()
}
if !strings.HasPrefix(buf.String(), "ERR") {
t.Error("OPTION command not failed")
t.Error(buf.String())
}
})
t.Run("common.Error from SetOption", func(t *testing.T) {
buf := bytes.Buffer{}
pipe := common.NewPipe(nil, &buf)
proto := ProtoInfo{}
proto.SetOption = func(_ interface{}, _, _ string) error {
return &common.Error{
Src: common.ErrSrcAssuan, Code: common.ErrAssUnknownCmd,
SrcName: "assuan", Message: "TEST ERROR",
}
}
if err := handleCmd(&pipe, "OPTION", "a 2", proto, nil); err != nil {
t.Error("Unexpected handleCmd error:", err)
t.FailNow()
}
if !strings.HasPrefix(buf.String(), "ERR") {
t.Error("OPTION command not failed")
t.Error(buf.String())
}
})
t.Run("correct value passed", func(t *testing.T) {
buf := bytes.Buffer{}
pipe := common.NewPipe(nil, &buf)
key, val := "", ""
proto := ProtoInfo{}
proto.SetOption = func(_ interface{}, k, v string) error {
key, val = k, v
return nil
}
if err := handleCmd(&pipe, "OPTION", "a 2", proto, nil); err != nil {
t.Error("Unexpected handleCmd error:", err)
t.FailNow()
}
if strings.HasPrefix(buf.String(), "ERR") {
t.Error("OPTION command failed")
t.Error(buf.String())
}
if key != "a" || val != "2" {
t.Errorf("Mismatched key-value: wanted %s/%s, got %s/%s", "a", "2", key, val)
}
})
}