Repository navigation
Expand file tree
/
Copy pathutils_test.go
More file actions
587 lines (506 loc) · 15.3 KB
/
Copy pathutils_test.go
File metadata and controls
587 lines (506 loc) · 15.3 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
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
// Copyright 2019-2022 Graham Clark. All rights reserved. Use of this source
// code is governed by the MIT license that can be found in the LICENSE
// file.
package termshark
import (
"bytes"
"os"
"regexp"
"testing"
"time"
"github.com/blang/semver"
"github.com/gcla/termshark/v2/pkg/format"
"github.com/stretchr/testify/assert"
)
//======================================================================
func TestApplyArgs(t *testing.T) {
cmd := []string{"echo", "something", "$3", "else", "$1", "$3"}
args := []string{"a1", "a2"}
eres := []string{"echo", "something", "$3", "else", "a1", "$3"}
res, total := ApplyArguments(cmd, args)
assert.Equal(t, eres, res)
assert.Equal(t, total, 1)
args = []string{"a1", "a2", "a3"}
eres = []string{"echo", "something", "a3", "else", "a1", "a3"}
res, total = ApplyArguments(cmd, args)
assert.Equal(t, eres, res)
assert.Equal(t, total, 3)
}
func TestArgConv(t *testing.T) {
var tests = []struct {
arg string
flag string
val string
res bool
}{
{"--tshark-d=foo", "d", "foo", true},
{"--tshark-abc=foo", "", "", false},
{"--tshark-V=true", "V", "", true},
{"--tshark-V=false", "", "", false},
{"--ts-V=wow", "", "", false},
}
for _, test := range tests {
f, v, ok := ConvertArgToTShark(test.arg)
assert.Equal(t, test.res, ok)
if test.res {
assert.Equal(t, test.flag, f)
assert.Equal(t, test.val, v)
}
}
}
func TestVer1(t *testing.T) {
out1 := `TShark (Wireshark) 2.6.6 (Git v2.6.6 packaged as 2.6.6-1~ubuntu18.04.0)
Copyright 1998-2019 Gerald Combs <gerald@wireshark.org> and contributors.`
v1, err := TSharkVersionFromOutput(out1)
assert.NoError(t, err)
res, _ := semver.Make("2.6.6")
assert.Equal(t, res, v1)
}
func TestVer2(t *testing.T) {
out1 := `TShark 1.6.7
Copyright 1998-2012 Gerald Combs <gerald@wireshark.org> and contributors.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
Compiled (64-bit) with GLib 2.32.0, with libpcap (version unknown), with libz
1.2.3.4, with POSIX capabilities (Linux), without libpcre, with SMI 0.4.8, with
c-ares 1.7.5, with Lua 5.1, without Python, with GnuTLS 2.12.14, with Gcrypt
1.5.0, with MIT Kerberos, with GeoIP.
Running on Linux 3.2.0-126-generic, with libpcap version 1.1.1, with libz
1.2.3.4.
`
v1, err := TSharkVersionFromOutput(out1)
assert.NoError(t, err)
res, _ := semver.Make("1.6.7")
assert.Equal(t, res, v1)
}
func TestInterfaces1(t *testing.T) {
out1 := `
1. \Device\NPF_{BAC1CFBD-DE27-4023-B478-0C490B99DC5E} (Local Area Connection 2)
2. \Device\NPF_{78032B7E-4968-42D3-9F37-287EA86C0AAA} (Local Area Connection* 10)
3. \Device\NPF_{84E7CAE6-E96F-4F31-96FD-170B0F514AB2} (Npcap Loopback Adapter)
4. \Device\NPF_NdisWanIpv6 (NdisWan Adapter)
5. \Device\NPF_{503E1F71-C57C-438D-B004-EA5563723C16} (Local Area Connection 5)
6. \Device\NPF_{15DDE443-C208-4328-8919-9666682EE804} (Local Area Connection* 11)
`[1:]
interfaces, err := interfacesFrom(bytes.NewReader([]byte(out1)))
assert.NoError(t, err)
assert.Equal(t, 6, len(interfaces))
v := interfaces[2]
assert.Equal(t, `\Device\NPF_{78032B7E-4968-42D3-9F37-287EA86C0AAA}`, v[1])
assert.Equal(t, `Local Area Connection* 10`, v[0])
}
func TestInterfaces2(t *testing.T) {
out1 := `
1. eth0
2. ham0
3. docker0
4. vethd45103d
5. lo (Loopback)
6. mpqemubr0-dummy
7. nflog
8. nfqueue
9. bluetooth0
10. virbr0-nic
11. vboxnet0
12. ciscodump (Cisco remote capture)
13. dpauxmon (DisplayPort AUX channel monitor capture)
14. randpkt (Random packet generator)
15. sdjournal (systemd Journal Export)
16. sshdump (SSH remote capture)
17. udpdump (UDP Listener remote capture)
`[1:]
interfaces, err := interfacesFrom(bytes.NewReader([]byte(out1)))
assert.NoError(t, err)
assert.Equal(t, 17, len(interfaces))
v := interfaces[3]
assert.Equal(t, `docker0`, v[0])
v = interfaces[12]
assert.Equal(t, `Cisco remote capture`, v[0])
assert.Equal(t, `ciscodump`, v[1])
}
func TestConv1(t *testing.T) {
var tests = []struct {
arg string
res string
}{
{"hello\x41world\x42", "helloAworldB"},
{"80 \xe2\x86\x92 53347", "80 → 53347"},
{"hello\x41world\x42 foo \\000 bar", "helloAworldB foo \\000 bar"},
}
for _, test := range tests {
outs := format.TranslateHexCodes([]byte(test.arg))
assert.Equal(t, string(outs), test.res)
}
}
func TestIPComp1(t *testing.T) {
var ip IPCompare
assert.True(t, ip.Less("x", "y"))
assert.True(t, ip.Less("192.168.0.4", "y"))
assert.False(t, ip.Less("y", "192.168.0.4"))
assert.True(t, ip.Less("192.168.0.253", "192.168.1.4"))
assert.False(t, ip.Less("192.168.1.4", "192.168.0.253"))
assert.True(t, ip.Less("192.168.0.253", "::ffff:192.168.1.4"))
assert.True(t, ip.Less("::ffff:192.168.0.253", "192.168.1.4"))
assert.True(t, ip.Less("192.168.0.253", "2001:db8::68"))
assert.False(t, ip.Less("2001:db8::68", "192.168.0.253"))
}
func TestMACComp1(t *testing.T) {
var mac MACCompare
assert.True(t, mac.Less("x", "y"))
assert.True(t, mac.Less("11:22:33:44:55:66", "y"))
assert.True(t, mac.Less("xx:22:33:44:55:66", "y"))
assert.False(t, mac.Less("xx:22:33:44:55:66", "11:22:33:44:55:66"))
assert.True(t, mac.Less("11:22:33:44:55:66", "11:22:33:44:55:67"))
assert.False(t, mac.Less("11:22:33:44:55:66", "11:22:33:44:54:66"))
}
func TestFolders(t *testing.T) {
tmp := os.Getenv("TMPDIR")
os.Setenv("TMPDIR", "/foo")
defer os.Setenv("TMPDIR", tmp)
val, err := TsharkSetting("Temp")
assert.NoError(t, err)
assert.Equal(t, "/foo", val)
val, err = TsharkSetting("Deliberately missing")
assert.Error(t, err)
}
func TestReverseStringSlice(t *testing.T) {
tests := []struct {
input []string
expected []string
}{
{[]string{}, []string{}},
{[]string{"a"}, []string{"a"}},
{[]string{"a", "b"}, []string{"b", "a"}},
{[]string{"a", "b", "c"}, []string{"c", "b", "a"}},
{[]string{"1", "2", "3", "4"}, []string{"4", "3", "2", "1"}},
}
for _, test := range tests {
s := make([]string, len(test.input))
copy(s, test.input)
ReverseStringSlice(s)
assert.Equal(t, test.expected, s)
}
}
func TestIsCommandInPath(t *testing.T) {
// "ls" should be in path on most systems
assert.True(t, IsCommandInPath("ls"))
assert.True(t, IsCommandInPath("sh"))
// A nonexistent command
assert.False(t, IsCommandInPath("definitely_not_a_real_command_12345"))
}
func TestTSharkVersionFromOutputInvalid(t *testing.T) {
// Test with invalid output
_, err := TSharkVersionFromOutput("not a valid version string")
assert.Error(t, err)
assert.Equal(t, TSharkVersionUnknown, err)
// Test with empty string
_, err = TSharkVersionFromOutput("")
assert.Error(t, err)
}
func TestRemoveFromStringSlice(t *testing.T) {
tests := []struct {
name string
pcap string
comps []string
expected []string
}{
{
name: "empty slice",
pcap: "foo",
comps: []string{},
expected: []string{"foo"},
},
{
name: "item not in slice",
pcap: "foo",
comps: []string{"a", "b", "c"},
expected: []string{"foo", "a", "b", "c"},
},
{
name: "item at beginning",
pcap: "a",
comps: []string{"a", "b", "c"},
expected: []string{"a", "b", "c"},
},
{
name: "item in middle",
pcap: "b",
comps: []string{"a", "b", "c"},
expected: []string{"b", "a", "c"},
},
{
name: "item at end",
pcap: "c",
comps: []string{"a", "b", "c"},
expected: []string{"c", "a", "b"},
},
{
name: "multiple occurrences",
pcap: "b",
comps: []string{"a", "b", "c", "b", "d"},
expected: []string{"b", "a", "c", "d"},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result := RemoveFromStringSlice(tt.pcap, tt.comps)
assert.Equal(t, tt.expected, result)
})
}
}
func TestWriteEmptyPcap(t *testing.T) {
// Create a temp file
tmpfile, err := os.CreateTemp("", "test*.pcap")
assert.NoError(t, err)
tmpfile.Close()
defer os.Remove(tmpfile.Name())
// Write empty pcap
err = WriteEmptyPcap(tmpfile.Name())
assert.NoError(t, err)
// Read the file back and verify the magic number
data, err := os.ReadFile(tmpfile.Name())
assert.NoError(t, err)
assert.Equal(t, 24, len(data)) // pcap header is 24 bytes
// Check magic number (0xA1B2C3D4 in little endian)
assert.Equal(t, byte(0xD4), data[0])
assert.Equal(t, byte(0xC3), data[1])
assert.Equal(t, byte(0xB2), data[2])
assert.Equal(t, byte(0xA1), data[3])
}
func TestFileNewerThan(t *testing.T) {
// Create two temp files
file1, err := os.CreateTemp("", "test1*.txt")
assert.NoError(t, err)
file1.Close()
defer os.Remove(file1.Name())
file2, err := os.CreateTemp("", "test2*.txt")
assert.NoError(t, err)
file2.Close()
defer os.Remove(file2.Name())
// Test with non-existent file
_, err = FileNewerThan("/nonexistent/file1", file2.Name())
assert.Error(t, err)
_, err = FileNewerThan(file1.Name(), "/nonexistent/file2")
assert.Error(t, err)
// file2 was created after file1, so file2 should be newer
// But they might be created in the same instant, so we touch file2
os.Chtimes(file2.Name(), testTimeNow(), testTimeNow())
newer, err := FileNewerThan(file2.Name(), file1.Name())
assert.NoError(t, err)
// Note: on fast systems both files might have same timestamp
// so we just check there's no error
_ = newer
}
func testTimeNow() time.Time {
return time.Now().Add(time.Second)
}
func TestConvPktsCompare(t *testing.T) {
var c ConvPktsCompare
tests := []struct {
a, b string
expected bool
}{
{"100", "200", true},
{"200", "100", false},
{"100", "100", false},
{"1,234", "2,345", true}, // comma-separated numbers
{"100 kB", "200 kB", true}, // with units
{"1 kB", "500", false}, // 1024 vs 500
{"500", "1 kB", true}, // 500 vs 1024
{"1 MB", "1,000 kB", false}, // 1MB (1048576) > 1000kB (1024000)
{"abc", "def", false}, // non-numeric returns false
{"def", "abc", false}, // non-numeric returns false
}
for _, tt := range tests {
result := c.Less(tt.a, tt.b)
assert.Equal(t, tt.expected, result, "Less(%q, %q)", tt.a, tt.b)
}
}
func TestReadWriteGob(t *testing.T) {
// Create a temp file
tmpfile, err := os.CreateTemp("", "test*.gob.gz")
assert.NoError(t, err)
tmpfile.Close()
defer os.Remove(tmpfile.Name())
// Test writing and reading a simple struct
type TestData struct {
Name string
Values []int
}
original := TestData{
Name: "test",
Values: []int{1, 2, 3, 4, 5},
}
// Write
err = WriteGob(tmpfile.Name(), original)
assert.NoError(t, err)
// Read
var loaded TestData
err = ReadGob(tmpfile.Name(), &loaded)
assert.NoError(t, err)
assert.Equal(t, original.Name, loaded.Name)
assert.Equal(t, original.Values, loaded.Values)
}
func TestReadGob_NonexistentFile(t *testing.T) {
var data string
err := ReadGob("/nonexistent/file.gob.gz", &data)
assert.Error(t, err)
}
func TestWriteGob_InvalidPath(t *testing.T) {
err := WriteGob("/nonexistent/dir/file.gob.gz", "test")
assert.Error(t, err)
}
func TestDirOfPathCommand(t *testing.T) {
// Test with a known command
path, err := DirOfPathCommand("ls")
assert.NoError(t, err)
assert.NotEmpty(t, path)
// Test with non-existent command
_, err = DirOfPathCommand("definitely_not_a_command_12345")
assert.Error(t, err)
}
func TestDirOfPathCommandUnsafe(t *testing.T) {
// Test with a known command
path := DirOfPathCommandUnsafe("ls")
assert.NotEmpty(t, path)
// Test with non-existent command should panic
assert.Panics(t, func() {
DirOfPathCommandUnsafe("definitely_not_a_command_12345")
})
}
func TestStringIsArgPrefixOf(t *testing.T) {
tests := []struct {
arg string
list []string
expected bool
}{
{"--flag=value", []string{"--flag"}, true},
{"--flag=value", []string{"--other", "--flag"}, true},
{"--flag=value", []string{"--other"}, false},
{"--flag", []string{"--flag"}, false}, // no = sign
{"--flag=", []string{"--flag"}, true},
{"", []string{"--flag"}, false},
{"--flag=value", []string{}, false},
}
for _, tt := range tests {
result := StringIsArgPrefixOf(tt.arg, tt.list)
assert.Equal(t, tt.expected, result, "StringIsArgPrefixOf(%q, %v)", tt.arg, tt.list)
}
}
func TestRootCause(t *testing.T) {
// Simple error (no wrapping)
err := BadState
assert.Equal(t, BadState, RootCause(err))
// Nil error
assert.Nil(t, RootCause(nil))
}
func TestErrorTypes(t *testing.T) {
assert.Equal(t, "Bad state", BadState.Error())
assert.Equal(t, "Error running command", BadCommand.Error())
assert.Equal(t, "Configuration error", ConfigErr.Error())
assert.Equal(t, "Internal error", InternalErr.Error())
}
func TestIndentPdml(t *testing.T) {
input := `<proto name="tcp"><field name="port" show="80"/></proto>`
var out bytes.Buffer
err := IndentPdml(bytes.NewReader([]byte(input)), &out)
assert.NoError(t, err)
assert.Contains(t, out.String(), "tcp")
assert.Contains(t, out.String(), "port")
}
func TestIndentPdml_InvalidXML(t *testing.T) {
input := `<not valid xml`
var out bytes.Buffer
err := IndentPdml(bytes.NewReader([]byte(input)), &out)
assert.Error(t, err)
}
func TestFixNewlines(t *testing.T) {
input := []byte("line1\nline2\nline3")
result := fixNewlines(input)
// On non-Windows, should return input unchanged
assert.Equal(t, input, result)
}
func TestRunningRemotely(t *testing.T) {
orig := os.Getenv("SSH_TTY")
defer os.Setenv("SSH_TTY", orig)
os.Setenv("SSH_TTY", "/dev/pts/0")
assert.True(t, RunningRemotely())
os.Unsetenv("SSH_TTY")
assert.False(t, RunningRemotely())
}
func TestGlobalJumpPos_Base(t *testing.T) {
g := GlobalJumpPos{
JumpPos: JumpPos{Summary: "test", Pos: 42},
Filename: "/path/to/capture.pcap",
}
assert.Equal(t, "capture.pcap", g.Base())
g.Filename = "simple.pcap"
assert.Equal(t, "simple.pcap", g.Base())
}
func TestDateStringForFilename(t *testing.T) {
s := DateStringForFilename()
// Format is 2006-01-02--15-04-05
assert.Regexp(t, `^\d{4}-\d{2}-\d{2}--\d{2}-\d{2}-\d{2}$`, s)
}
func TestErrLogger(t *testing.T) {
w := ErrLogger("component", "test")
assert.NotNil(t, w)
w.Close()
}
func TestTsregexp_FindStringSubmatchMap(t *testing.T) {
tests := []struct {
name string
pattern string
input string
expected map[string]string
}{
{
name: "named groups",
pattern: `(?P<first>\w+)\s+(?P<second>\w+)`,
input: "hello world",
expected: map[string]string{
"first": "hello",
"second": "world",
},
},
{
name: "no match",
pattern: `(?P<num>\d+)`,
input: "abc",
expected: map[string]string{},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
re := tsregexp{regexp.MustCompile(tt.pattern)}
result := re.FindStringSubmatchMap(tt.input)
for k, v := range tt.expected {
assert.Equal(t, v, result[k], "key=%s", k)
}
})
}
}
func TestFileSizeDifferentTo(t *testing.T) {
// Create a temp file with known content
tmpfile, err := os.CreateTemp("", "test*.txt")
assert.NoError(t, err)
defer os.Remove(tmpfile.Name())
tmpfile.WriteString("hello")
tmpfile.Close()
// Same size should return false (not different)
newSize, diff := FileSizeDifferentTo(tmpfile.Name(), 5)
assert.False(t, diff)
assert.Equal(t, int64(5), newSize)
// Different size should return true
newSize, diff = FileSizeDifferentTo(tmpfile.Name(), 10)
assert.True(t, diff)
assert.Equal(t, int64(5), newSize)
// Non-existent file still returns diff=true
_, diff = FileSizeDifferentTo("/nonexistent/file.txt", 5)
assert.True(t, diff)
}
//======================================================================
// Local Variables:
// mode: Go
// fill-column: 110
// End: