-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreader_test.go
More file actions
442 lines (404 loc) · 12.6 KB
/
Copy pathreader_test.go
File metadata and controls
442 lines (404 loc) · 12.6 KB
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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
package netargv
import (
"bufio"
"io"
"reflect"
"strings"
"testing"
)
// =====================================================================
// helpers
// =====================================================================
func newBufReader(s string) *bufio.Reader {
return bufio.NewReader(strings.NewReader(s))
}
func newReader(s string) *Reader {
return NewReader(strings.NewReader(s))
}
// =====================================================================
// Tests for: parseLine — handshake messages
// =====================================================================
func TestParseLine_Hello_SingleVersion(t *testing.T) {
msgType, args, flags, _, err := newReader("").parseLine("hello --version=0")
if err != nil {
t.Fatal(err)
}
if msgType != "hello" {
t.Fatalf("expected hello, got %q", msgType)
}
if len(args) != 0 {
t.Fatalf("expected no positional args, got %v", args)
}
if got := flags["version"]; !reflect.DeepEqual(got, []string{"0"}) {
t.Fatalf("expected version=[0], got %v", got)
}
}
func TestParseLine_Hello_MultipleVersions(t *testing.T) {
msgType, _, flags, _, err := newReader("").parseLine("hello --version=0 --version=1")
if err != nil {
t.Fatal(err)
}
if msgType != "hello" {
t.Fatalf("expected hello, got %q", msgType)
}
want := []string{"0", "1"}
if !reflect.DeepEqual(flags["version"], want) {
t.Fatalf("expected version=%v, got %v", want, flags["version"])
}
}
func TestParseLine_Auth(t *testing.T) {
msgType, args, flags, _, err := newReader("").parseLine("auth --token=7fo8sdafhsd89fasdj0sas")
if err != nil {
t.Fatal(err)
}
if msgType != "auth" {
t.Fatalf("expected auth, got %q", msgType)
}
if len(args) != 0 {
t.Fatalf("expected no positional args, got %v", args)
}
if got := flags["token"]; !reflect.DeepEqual(got, []string{"7fo8sdafhsd89fasdj0sas"}) {
t.Fatalf("unexpected token flag: %v", got)
}
}
func TestParseLine_About(t *testing.T) {
msgType, args, flags, _, err := newReader("").parseLine("about --host=worker2.insights.com --slots=1024 --os=linux --arch=amd64")
if err != nil {
t.Fatal(err)
}
if msgType != "about" {
t.Fatalf("expected about, got %q", msgType)
}
if len(args) != 0 {
t.Fatalf("expected no positional args, got %v", args)
}
checks := map[string]string{
"host": "worker2.insights.com",
"slots": "1024",
"os": "linux",
"arch": "amd64",
}
for k, v := range checks {
if got := flags[k]; len(got) != 1 || got[0] != v {
t.Fatalf("flag %q: expected [%s], got %v", k, v, got)
}
}
}
func TestParseLine_Support(t *testing.T) {
msgType, args, flags, _, err := newReader("").parseLine("support --name=Report:AnnualSomething --cost=5")
if err != nil {
t.Fatal(err)
}
if msgType != "support" {
t.Fatalf("expected support, got %q", msgType)
}
if len(args) != 0 {
t.Fatalf("expected no positional args, got %v", args)
}
if got := flags["name"]; !reflect.DeepEqual(got, []string{"Report:AnnualSomething"}) {
t.Fatalf("unexpected name flag: %v", got)
}
if got := flags["cost"]; !reflect.DeepEqual(got, []string{"5"}) {
t.Fatalf("unexpected cost flag: %v", got)
}
}
func TestParseLine_Ready(t *testing.T) {
msgType, args, flags, _, err := newReader("").parseLine("ready")
if err != nil {
t.Fatal(err)
}
if msgType != "ready" {
t.Fatalf("expected ready, got %q", msgType)
}
if len(args) != 0 {
t.Fatalf("expected no positional args, got %v", args)
}
if len(flags) != 0 {
t.Fatalf("expected no flags, got %v", flags)
}
}
// =====================================================================
// Tests for: parseLine — work-dispatch messages
// =====================================================================
func TestParseLine_Work(t *testing.T) {
msgType, args, flags, _, err := newReader("").parseLine("work --id=8d9fsajfidsa9f08dsa --job=Report:AnnualSomething --content-type=application/vnd.sqlite3")
if err != nil {
t.Fatal(err)
}
if msgType != "work" {
t.Fatalf("expected work, got %q", msgType)
}
if len(args) != 0 {
t.Fatalf("expected no positional args, got %v", args)
}
if got := flags["id"]; !reflect.DeepEqual(got, []string{"8d9fsajfidsa9f08dsa"}) {
t.Fatalf("unexpected id flag: %v", got)
}
if got := flags["job"]; !reflect.DeepEqual(got, []string{"Report:AnnualSomething"}) {
t.Fatalf("unexpected job flag: %v", got)
}
if got := flags["content-type"]; !reflect.DeepEqual(got, []string{"application/vnd.sqlite3"}) {
t.Fatalf("unexpected content-type flag: %v", got)
}
}
func TestParseLine_Accept(t *testing.T) {
msgType, args, flags, _, err := newReader("").parseLine("accept --id=8d9fsajfidsa9f08dsa --remaining-slots=23")
if err != nil {
t.Fatal(err)
}
if msgType != "accept" {
t.Fatalf("expected accept, got %q", msgType)
}
if len(args) != 0 {
t.Fatalf("expected no positional args, got %v", args)
}
if got := flags["remaining-slots"]; !reflect.DeepEqual(got, []string{"23"}) {
t.Fatalf("unexpected remaining-slots flag: %v", got)
}
}
func TestParseLine_Reject_QuotedMessage(t *testing.T) {
msgType, args, flags, _, err := newReader("").parseLine(`reject --message="Job not supported"`)
if err != nil {
t.Fatal(err)
}
if msgType != "reject" {
t.Fatalf("expected reject, got %q", msgType)
}
if len(args) != 0 {
t.Fatalf("expected no positional args, got %v", args)
}
if got := flags["message"]; !reflect.DeepEqual(got, []string{"Job not supported"}) {
t.Fatalf("unexpected message flag: %v", got)
}
}
func TestParseLine_Fail_QuotedMessage(t *testing.T) {
msgType, _, flags, _, err := newReader("").parseLine(`fail --message="Unsupported protocol version"`)
if err != nil {
t.Fatal(err)
}
if msgType != "fail" {
t.Fatalf("expected fail, got %q", msgType)
}
if got := flags["message"]; !reflect.DeepEqual(got, []string{"Unsupported protocol version"}) {
t.Fatalf("unexpected message flag: %v", got)
}
}
func TestParseLine_Progress(t *testing.T) {
msgType, args, flags, _, err := newReader("").parseLine(`progress 20% --id=8d9fsajfidsa9f08dsa --message="Hard at work"`)
if err != nil {
t.Fatal(err)
}
if msgType != "progress" {
t.Fatalf("expected progress, got %q", msgType)
}
if !reflect.DeepEqual(args, []string{"20%"}) {
t.Fatalf("expected args=[20%%], got %v", args)
}
if got := flags["message"]; !reflect.DeepEqual(got, []string{"Hard at work"}) {
t.Fatalf("unexpected message flag: %v", got)
}
}
func TestParseLine_Completed(t *testing.T) {
msgType, args, flags, _, err := newReader("").parseLine("completed success --id=8d9fsajfidsa9f08dsa --duration=1200")
if err != nil {
t.Fatal(err)
}
if msgType != "completed" {
t.Fatalf("expected completed, got %q", msgType)
}
if !reflect.DeepEqual(args, []string{"success"}) {
t.Fatalf("expected args=[success], got %v", args)
}
if got := flags["duration"]; !reflect.DeepEqual(got, []string{"1200"}) {
t.Fatalf("unexpected duration flag: %v", got)
}
}
func TestParseLine_Status(t *testing.T) {
msgType, args, flags, _, err := newReader("").parseLine("status --available-slots=12 --load=23% --uptime=12:43")
if err != nil {
t.Fatal(err)
}
if msgType != "status" {
t.Fatalf("expected status, got %q", msgType)
}
if len(args) != 0 {
t.Fatalf("expected no positional args, got %v", args)
}
if got := flags["available-slots"]; !reflect.DeepEqual(got, []string{"12"}) {
t.Fatalf("unexpected available-slots flag: %v", got)
}
if got := flags["load"]; !reflect.DeepEqual(got, []string{"23%"}) {
t.Fatalf("unexpected load flag: %v", got)
}
if got := flags["uptime"]; !reflect.DeepEqual(got, []string{"12:43"}) {
t.Fatalf("unexpected uptime flag: %v", got)
}
}
// =====================================================================
// Tests for: parseLine — edge cases
// =====================================================================
func TestParseLine_BooleanFlag(t *testing.T) {
_, _, flags, _, err := newReader("").parseLine("cmd --force")
if err != nil {
t.Fatal(err)
}
if got := flags["force"]; !reflect.DeepEqual(got, []string{"true"}) {
t.Fatalf("expected force=[true], got %v", got)
}
}
func TestParseLine_SingleQuotedFlagValue(t *testing.T) {
_, _, flags, _, err := newReader("").parseLine("cmd --message='hello world'")
if err != nil {
t.Fatal(err)
}
if got := flags["message"]; !reflect.DeepEqual(got, []string{"hello world"}) {
t.Fatalf("unexpected message flag: %v", got)
}
}
func TestParseLine_MessageTypePreservesCase(t *testing.T) {
msgType, _, _, _, err := newReader("").parseLine("Hello --version=0")
if err != nil {
t.Fatal(err)
}
if msgType != "Hello" {
t.Fatalf("expected Hello, got %q", msgType)
}
}
func TestParseLine_EmptyLine(t *testing.T) {
msgType, args, flags, payloadLen, err := newReader("").parseLine("")
if err != nil {
t.Fatal(err)
}
if msgType != "" || args != nil || flags != nil || payloadLen != 0 {
t.Fatalf("expected all-empty results for blank line, got %q %v %v", msgType, args, flags)
}
}
func TestParseLine_Authenticated(t *testing.T) {
msgType, args, flags, _, err := newReader("").parseLine("authenticated")
if err != nil {
t.Fatal(err)
}
if msgType != "authenticated" {
t.Fatalf("expected authenticated, got %q", msgType)
}
if len(args) != 0 {
t.Fatalf("expected no args, got %v", args)
}
if len(flags) != 0 {
t.Fatalf("expected no flags, got %v", flags)
}
}
// =====================================================================
// Tests for: parseLine — payload length declaration
// =====================================================================
func TestParseLine_PayloadLength(t *testing.T) {
msgType, args, flags, payloadLen, err := newReader("").parseLine("upload --name=script.sh -- 1024")
if err != nil {
t.Fatal(err)
}
if msgType != "upload" {
t.Fatalf("expected upload, got %q", msgType)
}
if len(args) != 0 {
t.Fatalf("expected no positional args, got %v", args)
}
if got := flags["name"]; !reflect.DeepEqual(got, []string{"script.sh"}) {
t.Fatalf("unexpected name flag: %v", got)
}
if payloadLen != 1024 {
t.Fatalf("expected payloadLen=1024, got %d", payloadLen)
}
}
func TestParseLine_PayloadLengthZero(t *testing.T) {
_, _, _, payloadLen, err := newReader("").parseLine("ping")
if err != nil {
t.Fatal(err)
}
if payloadLen != 0 {
t.Fatalf("expected payloadLen=0, got %d", payloadLen)
}
}
func TestParseLine_PayloadMissingCount(t *testing.T) {
_, _, _, _, err := newReader("").parseLine("upload --name=x --")
if err == nil {
t.Fatal("expected error for -- without byte count")
}
}
func TestParseLine_PayloadInvalidCount(t *testing.T) {
_, _, _, _, err := newReader("").parseLine("upload --name=x -- abc")
if err == nil {
t.Fatal("expected error for non-integer byte count")
}
}
// =====================================================================
// Tests for: readLogicalLine
// =====================================================================
func TestReadLogicalLine_SimpleLine(t *testing.T) {
r := &Reader{src: newBufReader("hello --version=0\n")}
got, err := r.readLogicalLine()
if err != nil {
t.Fatal(err)
}
if string(got) != "hello --version=0" {
t.Fatalf("unexpected line: %q", got)
}
}
func TestReadLogicalLine_StripsTrailingWhitespace(t *testing.T) {
r := &Reader{src: newBufReader(" ready \n")}
got, err := r.readLogicalLine()
if err != nil {
t.Fatal(err)
}
if string(got) != "ready" {
t.Fatalf("unexpected line: %q", got)
}
}
func TestReadLogicalLine_CRLFStripped(t *testing.T) {
r := &Reader{src: newBufReader("auth --token=abc\r\n")}
got, err := r.readLogicalLine()
if err != nil {
t.Fatal(err)
}
if string(got) != "auth --token=abc" {
t.Fatalf("unexpected line: %q", got)
}
}
func TestReadLogicalLine_SingleContinuation(t *testing.T) {
input := "hello --version=0 \\\n--version=1\n"
r := &Reader{src: newBufReader(input)}
got, err := r.readLogicalLine()
if err != nil {
t.Fatal(err)
}
want := "hello --version=0 --version=1"
if string(got) != want {
t.Fatalf("expected %q, got %q", want, got)
}
}
func TestReadLogicalLine_MultipleContinuations(t *testing.T) {
input := "work \\\n--id=abc \\\n--job=Foo\n"
r := &Reader{src: newBufReader(input)}
got, err := r.readLogicalLine()
if err != nil {
t.Fatal(err)
}
want := "work --id=abc --job=Foo"
if string(got) != want {
t.Fatalf("expected %q, got %q", want, got)
}
}
func TestReadLogicalLine_EOFOnEmptyBuffer(t *testing.T) {
r := &Reader{src: newBufReader("")}
_, err := r.readLogicalLine()
if err != io.EOF {
t.Fatalf("expected io.EOF, got %v", err)
}
}
func TestReadLogicalLine_EOFMidContinuation(t *testing.T) {
// Backslash at the very end with no following newline.
r := &Reader{src: newBufReader("hello \\\n")}
_, err := r.readLogicalLine()
if err != io.ErrUnexpectedEOF {
t.Fatalf("expected io.ErrUnexpectedEOF, got %v", err)
}
}