forked from gcla/termshark
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils_extra_test.go
More file actions
1382 lines (1185 loc) · 38 KB
/
utils_extra_test.go
File metadata and controls
1382 lines (1185 loc) · 38 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
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// 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"
"context"
"io"
"os"
"path/filepath"
"regexp"
"strings"
"sync"
"testing"
"github.com/blang/semver"
"github.com/gcla/termshark/v2/pkg/lifecycle"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
//======================================================================
// TSharkVersionFromOutput - comprehensive version parsing
//======================================================================
func TestTSharkVersionFromOutput_v1x(t *testing.T) {
tests := []struct {
name string
input string
expected semver.Version
}{
{
name: "TShark 1.6.7",
input: "TShark 1.6.7\n\nCopyright 1998-2012",
expected: semver.MustParse("1.6.7"),
},
{
name: "TShark 1.10.2",
input: "TShark 1.10.2\n\nmore text",
expected: semver.MustParse("1.10.2"),
},
{
name: "TShark 1.0.0 minimal",
input: "TShark 1.0.0",
expected: semver.MustParse("1.0.0"),
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
v, err := TSharkVersionFromOutput(tt.input)
assert.NoError(t, err)
assert.Equal(t, tt.expected, v)
})
}
}
func TestTSharkVersionFromOutput_v2x(t *testing.T) {
tests := []struct {
name string
input string
expected semver.Version
}{
{
name: "TShark 2.6.6 with Git info",
input: "TShark (Wireshark) 2.6.6 (Git v2.6.6 packaged as 2.6.6-1~ubuntu18.04.0)\n\nCopyright 1998-2019 Gerald Combs",
expected: semver.MustParse("2.6.6"),
},
{
name: "TShark 2.0.0",
input: "TShark (Wireshark) 2.0.0\n",
expected: semver.MustParse("2.0.0"),
},
{
name: "TShark 2.4.10",
input: "TShark (Wireshark) 2.4.10 (v2.4.10)\n\nCopyright",
expected: semver.MustParse("2.4.10"),
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
v, err := TSharkVersionFromOutput(tt.input)
assert.NoError(t, err)
assert.Equal(t, tt.expected, v)
})
}
}
func TestTSharkVersionFromOutput_v3x(t *testing.T) {
tests := []struct {
name string
input string
expected semver.Version
}{
{
name: "TShark 3.2.3",
input: "TShark (Wireshark) 3.2.3 (Git v3.2.3 packaged as 3.2.3-1)\n\nCopyright 1998-2020",
expected: semver.MustParse("3.2.3"),
},
{
name: "TShark 3.6.14",
input: "TShark (Wireshark) 3.6.14 (wireshark-3.6.14)\n\nCopyright 1998-2023",
expected: semver.MustParse("3.6.14"),
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
v, err := TSharkVersionFromOutput(tt.input)
assert.NoError(t, err)
assert.Equal(t, tt.expected, v)
})
}
}
func TestTSharkVersionFromOutput_v4x(t *testing.T) {
tests := []struct {
name string
input string
expected semver.Version
}{
{
name: "TShark 4.0.1",
input: "TShark (Wireshark) 4.0.1 (Git v4.0.1 packaged as 4.0.1-1~ubuntu22.04.0)\n\nCopyright",
expected: semver.MustParse("4.0.1"),
},
{
name: "TShark 4.2.0",
input: "TShark (Wireshark) 4.2.0\nRunning on Linux 6.1",
expected: semver.MustParse("4.2.0"),
},
{
name: "TShark 4.6.0",
input: "TShark (Wireshark) 4.6.0 (v4.6.0)\n",
expected: semver.MustParse("4.6.0"),
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
v, err := TSharkVersionFromOutput(tt.input)
assert.NoError(t, err)
assert.Equal(t, tt.expected, v)
})
}
}
func TestTSharkVersionFromOutput_MalformedAndEdgeCases(t *testing.T) {
tests := []struct {
name string
input string
expectErr bool
}{
{"empty string", "", true},
{"just whitespace", " \n\t ", true},
{"no version number", "TShark (Wireshark) \n\nCopyright", true},
{"partial version", "TShark 2.6", true},
{"not tshark at all", "wireshark 3.2.1", true},
{"random text", "hello world", true},
{"only numbers", "1.2.3", true},
{"version without TShark prefix", "Version 2.6.6", true},
{"tshark lowercase", "tshark 2.6.6", true},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
_, err := TSharkVersionFromOutput(tt.input)
if tt.expectErr {
assert.Error(t, err)
} else {
assert.NoError(t, err)
}
})
}
}
//======================================================================
// Config path functions
//======================================================================
func TestConfFile(t *testing.T) {
result := ConfFile("termshark.toml")
assert.True(t, strings.HasSuffix(result, filepath.Join("termshark", "termshark.toml")))
assert.True(t, filepath.IsAbs(result))
}
func TestConfFile_EmptyString(t *testing.T) {
result := ConfFile("")
assert.True(t, strings.HasSuffix(result, "termshark"))
}
func TestConfFile_NestedPath(t *testing.T) {
result := ConfFile("subdir/file.toml")
assert.Contains(t, result, "termshark")
assert.True(t, strings.HasSuffix(result, filepath.Join("termshark", "subdir/file.toml")))
}
func TestCacheDir(t *testing.T) {
result := CacheDir()
assert.True(t, strings.HasSuffix(result, "termshark"))
assert.True(t, filepath.IsAbs(result))
}
func TestCacheFile(t *testing.T) {
result := CacheFile("empty.pcap")
assert.True(t, strings.HasSuffix(result, filepath.Join("termshark", "empty.pcap")))
assert.True(t, filepath.IsAbs(result))
}
func TestCacheFile_EmptyName(t *testing.T) {
result := CacheFile("")
// Should still return a valid path ending in termshark dir
assert.True(t, strings.HasSuffix(result, "termshark"))
}
func TestDefaultPcapDir(t *testing.T) {
result := DefaultPcapDir()
assert.True(t, strings.HasSuffix(result, filepath.Join("termshark", "pcaps")))
assert.True(t, filepath.IsAbs(result))
}
func TestDefaultPcapDir_ContainsCacheDir(t *testing.T) {
cacheDir := CacheDir()
pcapDir := DefaultPcapDir()
assert.True(t, strings.HasPrefix(pcapDir, cacheDir),
"PcapDir (%s) should be under CacheDir (%s)", pcapDir, cacheDir)
}
//======================================================================
// ApplyArguments - thorough edge cases
//======================================================================
func TestApplyArguments_EmptyCmd(t *testing.T) {
res, total := ApplyArguments([]string{}, []string{"a1"})
assert.Equal(t, []string{}, res)
assert.Equal(t, 0, total)
}
func TestApplyArguments_EmptyArgs(t *testing.T) {
cmd := []string{"echo", "$1", "$2"}
res, total := ApplyArguments(cmd, []string{})
assert.Equal(t, []string{"echo", "$1", "$2"}, res)
assert.Equal(t, 0, total)
}
func TestApplyArguments_NilArgs(t *testing.T) {
cmd := []string{"echo", "$1"}
res, total := ApplyArguments(cmd, nil)
assert.Equal(t, []string{"echo", "$1"}, res)
assert.Equal(t, 0, total)
}
func TestApplyArguments_NoPlaceholders(t *testing.T) {
cmd := []string{"echo", "hello", "world"}
res, total := ApplyArguments(cmd, []string{"a1", "a2"})
assert.Equal(t, []string{"echo", "hello", "world"}, res)
assert.Equal(t, 0, total)
}
func TestApplyArguments_AllSubstituted(t *testing.T) {
cmd := []string{"$1", "$2", "$3"}
args := []string{"a", "b", "c"}
res, total := ApplyArguments(cmd, args)
assert.Equal(t, []string{"a", "b", "c"}, res)
assert.Equal(t, 3, total)
}
func TestApplyArguments_HighIndex(t *testing.T) {
cmd := []string{"echo", "$99"}
args := make([]string, 100)
for i := range args {
args[i] = "x"
}
args[98] = "found"
res, total := ApplyArguments(cmd, args)
assert.Equal(t, "found", res[1])
assert.Equal(t, 1, total)
}
func TestApplyArguments_NotArgPattern(t *testing.T) {
// $0 is not valid (args are 1-indexed per regex)
cmd := []string{"$0", "$1"}
args := []string{"first"}
res, total := ApplyArguments(cmd, args)
assert.Equal(t, "$0", res[0])
assert.Equal(t, "first", res[1])
assert.Equal(t, 1, total)
}
func TestApplyArguments_DollarSignInText(t *testing.T) {
// Dollar sign embedded in other text is not a placeholder
cmd := []string{"price$1", "$1"}
args := []string{"val"}
res, total := ApplyArguments(cmd, args)
assert.Equal(t, "price$1", res[0]) // not a standalone $1
assert.Equal(t, "val", res[1])
assert.Equal(t, 1, total)
}
//======================================================================
// RunForExitCode and RunForStderr
//======================================================================
func TestRunForExitCode_Success(t *testing.T) {
exitCode, err := RunForExitCode("true", []string{}, nil)
assert.NoError(t, err)
assert.Equal(t, 0, exitCode)
}
func TestRunForExitCode_Failure(t *testing.T) {
exitCode, err := RunForExitCode("false", []string{}, nil)
assert.Error(t, err)
assert.Equal(t, 1, exitCode)
}
func TestRunForExitCode_NonExistentCommand(t *testing.T) {
_, err := RunForExitCode("definitely_not_a_command_xyz123", []string{}, nil)
assert.Error(t, err)
}
func TestRunForExitCode_WithEnv(t *testing.T) {
exitCode, err := RunForExitCode("sh", []string{"-c", "exit 0"}, []string{"FOO=bar"})
assert.NoError(t, err)
assert.Equal(t, 0, exitCode)
}
func TestRunForExitCode_SpecificExitCode(t *testing.T) {
exitCode, err := RunForExitCode("sh", []string{"-c", "exit 42"}, nil)
assert.Error(t, err)
assert.Equal(t, 42, exitCode)
}
func TestRunForStderr_CapturesStderr(t *testing.T) {
var stderr bytes.Buffer
exitCode, err := RunForStderr("sh", []string{"-c", "echo errormsg >&2; exit 1"}, nil, &stderr)
assert.Error(t, err)
assert.Equal(t, 1, exitCode)
assert.Contains(t, stderr.String(), "errormsg")
}
func TestRunForStderr_NoStderrOnSuccess(t *testing.T) {
var stderr bytes.Buffer
exitCode, err := RunForStderr("sh", []string{"-c", "echo stdout_only"}, nil, &stderr)
assert.NoError(t, err)
assert.Equal(t, 0, exitCode)
assert.Equal(t, "", stderr.String())
}
func TestRunForStderr_NonExistent(t *testing.T) {
_, err := RunForStderr("definitely_not_a_command_xyz123", []string{}, nil, io.Discard)
assert.Error(t, err)
}
//======================================================================
// SafePid and KillIfPossible
//======================================================================
type mockProcess struct {
pid int
killErr error
}
func (m *mockProcess) Kill() error { return m.killErr }
func (m *mockProcess) Pid() int { return m.pid }
func TestSafePid_NilProcess(t *testing.T) {
assert.Equal(t, -1, SafePid(nil))
}
func TestSafePid_ValidProcess(t *testing.T) {
p := &mockProcess{pid: 12345}
assert.Equal(t, 12345, SafePid(p))
}
func TestSafePid_ZeroPid(t *testing.T) {
p := &mockProcess{pid: 0}
assert.Equal(t, 0, SafePid(p))
}
func TestKillIfPossible_NilProcess(t *testing.T) {
err := KillIfPossible(nil)
assert.NoError(t, err)
}
func TestKillIfPossible_AlreadyFinished(t *testing.T) {
p := &mockProcess{killErr: os.ErrProcessDone}
err := KillIfPossible(p)
assert.NoError(t, err)
}
func TestKillIfPossible_OtherError(t *testing.T) {
p := &mockProcess{killErr: os.ErrPermission}
err := KillIfPossible(p)
assert.Error(t, err)
assert.ErrorIs(t, err, os.ErrPermission)
}
func TestKillIfPossible_Success(t *testing.T) {
p := &mockProcess{killErr: nil}
err := KillIfPossible(p)
assert.NoError(t, err)
}
//======================================================================
// ConvertArgToTShark - additional edge cases
//======================================================================
func TestConvertArgToTShark_TableDriven(t *testing.T) {
tests := []struct {
name string
arg string
expectFlag string
expectVal string
expectOk bool
}{
{"valid single char flag", "--tshark-d=foo", "d", "foo", true},
{"multi-char flag rejected", "--tshark-abc=foo", "", "", false},
{"boolean true", "--tshark-V=true", "V", "", true},
{"boolean false", "--tshark-V=false", "", "", false},
{"wrong prefix", "--ts-V=wow", "", "", false},
{"no equals", "--tshark-d", "", "", false},
{"numeric flag", "--tshark-2=bar", "2", "bar", true},
{"value with equals", "--tshark-o=key=val", "o", "key=val", true},
{"value with spaces", "--tshark-Y=tcp port 80", "Y", "tcp port 80", true},
{"empty string", "", "", "", false},
{"just dashes", "--", "", "", false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
f, v, ok := ConvertArgToTShark(tt.arg)
assert.Equal(t, tt.expectOk, ok, "ok mismatch")
if ok {
assert.Equal(t, tt.expectFlag, f, "flag mismatch")
assert.Equal(t, tt.expectVal, v, "val mismatch")
}
})
}
}
//======================================================================
// interfacesFrom parsing
//======================================================================
func TestInterfacesFrom_EmptyInput(t *testing.T) {
interfaces, err := interfacesFrom(strings.NewReader(""))
assert.NoError(t, err)
assert.Equal(t, 0, len(interfaces))
}
func TestInterfacesFrom_SingleSimpleInterface(t *testing.T) {
input := "1. eth0\n"
interfaces, err := interfacesFrom(strings.NewReader(input))
assert.NoError(t, err)
assert.Equal(t, 1, len(interfaces))
assert.Equal(t, "eth0", interfaces[1][0])
}
func TestInterfacesFrom_InterfaceWithDescription(t *testing.T) {
input := "1. lo (Loopback)\n"
interfaces, err := interfacesFrom(strings.NewReader(input))
assert.NoError(t, err)
assert.Equal(t, 1, len(interfaces))
assert.Equal(t, "Loopback", interfaces[1][0])
assert.Equal(t, "lo", interfaces[1][1])
}
func TestInterfacesFrom_MalformedLine(t *testing.T) {
input := "not a valid interface line\n"
_, err := interfacesFrom(strings.NewReader(input))
assert.Error(t, err)
}
func TestInterfacesFrom_MultipleInterfaces(t *testing.T) {
input := `1. eth0
2. wlan0
3. lo (Loopback)
`
interfaces, err := interfacesFrom(strings.NewReader(input))
assert.NoError(t, err)
assert.Equal(t, 3, len(interfaces))
assert.Equal(t, "eth0", interfaces[1][0])
assert.Equal(t, "wlan0", interfaces[2][0])
assert.Equal(t, "Loopback", interfaces[3][0])
}
func TestInterfacesFrom_ExtcapInterfaces(t *testing.T) {
input := `1. ciscodump (Cisco remote capture)
2. randpkt (Random packet generator)
3. sshdump (SSH remote capture)
`
interfaces, err := interfacesFrom(strings.NewReader(input))
assert.NoError(t, err)
assert.Equal(t, 3, len(interfaces))
assert.Equal(t, "Cisco remote capture", interfaces[1][0])
assert.Equal(t, "ciscodump", interfaces[1][1])
}
func TestInterfacesFrom_LargeIndex(t *testing.T) {
input := "100. eth99\n"
interfaces, err := interfacesFrom(strings.NewReader(input))
assert.NoError(t, err)
assert.Equal(t, 1, len(interfaces))
assert.Equal(t, "eth99", interfaces[100][0])
}
func TestInterfacesFrom_WindowsStyle(t *testing.T) {
input := `1. \Device\NPF_{BAC1CFBD-DE27-4023-B478-0C490B99DC5E} (Local Area Connection 2)
`
interfaces, err := interfacesFrom(strings.NewReader(input))
assert.NoError(t, err)
assert.Equal(t, 1, len(interfaces))
assert.Equal(t, "Local Area Connection 2", interfaces[1][0])
assert.Equal(t, `\Device\NPF_{BAC1CFBD-DE27-4023-B478-0C490B99DC5E}`, interfaces[1][1])
}
//======================================================================
// IPCompare
//======================================================================
func TestIPCompare_BothValid(t *testing.T) {
var ip IPCompare
tests := []struct {
name string
a, b string
expected bool
}{
{"a < b", "10.0.0.1", "10.0.0.2", true},
{"a > b", "10.0.0.2", "10.0.0.1", false},
{"equal", "10.0.0.1", "10.0.0.1", false},
{"different octets", "10.0.1.0", "10.0.0.255", false},
{"cross-boundary", "192.168.0.255", "192.168.1.0", true},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
assert.Equal(t, tt.expected, ip.Less(tt.a, tt.b))
})
}
}
func TestIPCompare_IPv6(t *testing.T) {
var ip IPCompare
tests := []struct {
name string
a, b string
expected bool
}{
{"v6 less", "::1", "::2", true},
{"v6 greater", "::2", "::1", false},
{"v6 equal", "::1", "::1", false},
{"v4 vs v6", "192.168.0.1", "2001:db8::1", true},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
assert.Equal(t, tt.expected, ip.Less(tt.a, tt.b))
})
}
}
func TestIPCompare_InvalidIPs(t *testing.T) {
var ip IPCompare
tests := []struct {
name string
a, b string
expected bool
}{
{"both invalid lexicographic", "abc", "def", true},
{"both invalid reverse", "def", "abc", false},
{"a valid b invalid", "192.168.0.1", "invalid", true},
{"a invalid b valid", "invalid", "192.168.0.1", false},
{"both invalid same", "xxx", "xxx", false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
assert.Equal(t, tt.expected, ip.Less(tt.a, tt.b))
})
}
}
//======================================================================
// MACCompare
//======================================================================
func TestMACCompare_Comprehensive(t *testing.T) {
var mac MACCompare
tests := []struct {
name string
a, b string
expected bool
}{
{"a < b", "00:00:00:00:00:01", "00:00:00:00:00:02", true},
{"a > b", "00:00:00:00:00:02", "00:00:00:00:00:01", false},
{"equal", "aa:bb:cc:dd:ee:ff", "aa:bb:cc:dd:ee:ff", false},
{"higher byte diff", "00:00:01:00:00:00", "00:00:00:ff:ff:ff", false},
{"both invalid", "xxx", "yyy", true},
{"a valid b invalid", "aa:bb:cc:dd:ee:ff", "invalid", true},
{"a invalid b valid", "invalid", "aa:bb:cc:dd:ee:ff", false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
assert.Equal(t, tt.expected, mac.Less(tt.a, tt.b))
})
}
}
//======================================================================
// ConvPktsCompare - additional edge cases
//======================================================================
func TestConvPktsCompare_AdditionalEdgeCases(t *testing.T) {
var c ConvPktsCompare
tests := []struct {
name string
a, b string
expected bool
}{
{"bytes explicit unit", "100 bytes", "200 bytes", true},
{"bytes reverse", "200 bytes", "100 bytes", false},
{"kB equal", "1 kB", "1 kB", false},
{"MB vs kB", "1 MB", "1 kB", false},
{"kB vs MB", "1 kB", "1 MB", true},
{"large comma number", "1,000,000 bytes", "2,000,000 bytes", true},
{"zero", "0 bytes", "1 bytes", true},
{"comma in kB", "1,024 kB", "2,048 kB", true},
{"plain number no unit", "500", "600", true},
{"empty string a", "", "100", false},
{"empty string b", "100", "", false},
{"both empty", "", "", false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
assert.Equal(t, tt.expected, c.Less(tt.a, tt.b))
})
}
}
//======================================================================
// ReverseStringSlice - additional edge cases
//======================================================================
func TestReverseStringSlice_NilSlice(t *testing.T) {
// nil slice should not panic
var s []string
// nil has len 0, so the loop won't execute
ReverseStringSlice(s)
assert.Nil(t, s)
}
func TestReverseStringSlice_LargeSlice(t *testing.T) {
s := make([]string, 1000)
for i := range s {
s[i] = strings.Repeat("a", i)
}
ReverseStringSlice(s)
assert.Equal(t, strings.Repeat("a", 999), s[0])
assert.Equal(t, "", s[999])
}
//======================================================================
// StringIsArgPrefixOf - additional edge cases
//======================================================================
func TestStringIsArgPrefixOf_EdgeCases(t *testing.T) {
tests := []struct {
name string
arg string
list []string
expected bool
}{
{"nil list", "--flag=val", nil, false},
{"empty arg empty list", "", []string{}, false},
{"equals only", "=value", []string{""}, true},
{"list with empty string", "--flag=val", []string{""}, false},
{"partial match", "--fla=value", []string{"--flag"}, false},
{"multiple matches first", "--flag=value", []string{"--flag", "--other"}, true},
{"multiple matches second", "--other=value", []string{"--flag", "--other"}, true},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
assert.Equal(t, tt.expected, StringIsArgPrefixOf(tt.arg, tt.list))
})
}
}
//======================================================================
// RemoveFromStringSlice
//======================================================================
func TestRemoveFromStringSlice_NilSlice(t *testing.T) {
result := RemoveFromStringSlice("foo", nil)
assert.Equal(t, []string{"foo"}, result)
}
func TestRemoveFromStringSlice_EmptyElement(t *testing.T) {
result := RemoveFromStringSlice("", []string{"a", "", "b"})
assert.Equal(t, "", result[0])
assert.Contains(t, result, "a")
assert.Contains(t, result, "b")
}
//======================================================================
// IndentPdml
//======================================================================
func TestIndentPdml_NestedElements(t *testing.T) {
input := `<proto name="eth"><field name="src" show="00:11:22:33:44:55"><field name="addr" show="00:11:22"/></field></proto>`
var out bytes.Buffer
err := IndentPdml(bytes.NewReader([]byte(input)), &out)
assert.NoError(t, err)
assert.Contains(t, out.String(), "eth")
assert.Contains(t, out.String(), "src")
assert.Contains(t, out.String(), "addr")
}
func TestIndentPdml_EmptyElement(t *testing.T) {
input := `<proto name="empty"/>`
var out bytes.Buffer
err := IndentPdml(bytes.NewReader([]byte(input)), &out)
assert.NoError(t, err)
assert.Contains(t, out.String(), "empty")
}
func TestIndentPdml_EmptyInput(t *testing.T) {
var out bytes.Buffer
err := IndentPdml(bytes.NewReader([]byte("")), &out)
assert.Error(t, err)
}
func TestIndentPdml_BrokenXML(t *testing.T) {
input := `<proto name="broken"><field`
var out bytes.Buffer
err := IndentPdml(bytes.NewReader([]byte(input)), &out)
assert.Error(t, err)
}
//======================================================================
// RootCause
//======================================================================
type wrappedError struct {
inner error
}
func (w wrappedError) Error() string { return "wrapped: " + w.inner.Error() }
func (w wrappedError) Cause() error { return w.inner }
type doubleWrappedError struct {
inner error
}
func (d doubleWrappedError) Error() string { return "double: " + d.inner.Error() }
func (d doubleWrappedError) Cause() error { return d.inner }
func TestRootCause_SingleWrap(t *testing.T) {
root := BadState
wrapped := wrappedError{inner: root}
assert.Equal(t, root, RootCause(wrapped))
}
func TestRootCause_DoubleWrap(t *testing.T) {
root := BadCommand
inner := wrappedError{inner: root}
outer := doubleWrappedError{inner: inner}
assert.Equal(t, root, RootCause(outer))
}
func TestRootCause_NoWrap(t *testing.T) {
assert.Equal(t, BadState, RootCause(BadState))
}
func TestRootCause_Nil(t *testing.T) {
assert.Nil(t, RootCause(nil))
}
//======================================================================
// Tracker and Go/GoWithContext/Context
//======================================================================
func TestSetTrackerAndGo(t *testing.T) {
tracker := lifecycle.New()
oldTracker := Tracker()
defer SetTracker(oldTracker) // restore
SetTracker(tracker)
assert.Equal(t, tracker, Tracker())
var ran bool
var mu sync.Mutex
Go(func() {
mu.Lock()
defer mu.Unlock()
ran = true
})
tracker.Wait()
mu.Lock()
assert.True(t, ran)
mu.Unlock()
}
func TestGoWithContext_ReceivesContext(t *testing.T) {
tracker := lifecycle.New()
oldTracker := Tracker()
defer SetTracker(oldTracker)
SetTracker(tracker)
var received context.Context
var mu sync.Mutex
GoWithContext(func(ctx context.Context) {
mu.Lock()
defer mu.Unlock()
received = ctx
})
tracker.Wait()
mu.Lock()
assert.NotNil(t, received)
mu.Unlock()
}
func TestContext_WithTracker(t *testing.T) {
tracker := lifecycle.New()
oldTracker := Tracker()
defer SetTracker(oldTracker)
SetTracker(tracker)
ctx := Context()
assert.NotNil(t, ctx)
// Should be the tracker's context
assert.Equal(t, tracker.Context(), ctx)
}
func TestContext_WithoutTracker(t *testing.T) {
oldTracker := Tracker()
defer SetTracker(oldTracker)
SetTracker(nil)
ctx := Context()
assert.NotNil(t, ctx)
// Should be a background context
}
func TestGo_WithoutTracker(t *testing.T) {
oldTracker := Tracker()
defer SetTracker(oldTracker)
SetTracker(nil)
ch := make(chan bool, 1)
Go(func() {
ch <- true
})
assert.True(t, <-ch)
}
func TestGoWithContext_WithoutTracker(t *testing.T) {
oldTracker := Tracker()
defer SetTracker(oldTracker)
SetTracker(nil)
ch := make(chan bool, 1)
GoWithContext(func(ctx context.Context) {
ch <- true
})
assert.True(t, <-ch)
}
//======================================================================
// TrackedGo
//======================================================================
func TestTrackedGo_SingleWaitGroup(t *testing.T) {
var wg sync.WaitGroup
var ran bool
var mu sync.Mutex
TrackedGo(func() {
mu.Lock()
defer mu.Unlock()
ran = true
}, &wg)
wg.Wait()
mu.Lock()
assert.True(t, ran)
mu.Unlock()
}
func TestTrackedGo_MultipleWaitGroups(t *testing.T) {
var wg1, wg2 sync.WaitGroup
var ran bool
var mu sync.Mutex
TrackedGo(func() {
mu.Lock()
defer mu.Unlock()
ran = true
}, &wg1, &wg2)
wg1.Wait()
wg2.Wait()
mu.Lock()
assert.True(t, ran)
mu.Unlock()
}
func TestTrackedGo_NoWaitGroups(t *testing.T) {
ch := make(chan bool, 1)
TrackedGo(func() {
ch <- true
})
assert.True(t, <-ch)
}
//======================================================================
// WriteEmptyPcap
//======================================================================
func TestWriteEmptyPcap_ValidHeader(t *testing.T) {
tmpfile, err := os.CreateTemp("", "test*.pcap")
require.NoError(t, err)
tmpfile.Close()
defer os.Remove(tmpfile.Name())
err = WriteEmptyPcap(tmpfile.Name())
assert.NoError(t, err)
data, err := os.ReadFile(tmpfile.Name())
assert.NoError(t, err)
assert.Equal(t, 24, len(data))
// Check pcap magic number (little-endian 0xA1B2C3D4)
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])
// Check version (2.4)
assert.Equal(t, byte(2), data[4])
assert.Equal(t, byte(0), data[5])
assert.Equal(t, byte(4), data[6])
assert.Equal(t, byte(0), data[7])
}
func TestWriteEmptyPcap_InvalidPath(t *testing.T) {
err := WriteEmptyPcap("/nonexistent/dir/test.pcap")
assert.Error(t, err)
}
//======================================================================
// FileSizeDifferentTo
//======================================================================
func TestFileSizeDifferentTo_Comprehensive(t *testing.T) {
tmpfile, err := os.CreateTemp("", "test*.txt")
require.NoError(t, err)
defer os.Remove(tmpfile.Name())
_, err = tmpfile.WriteString("12345")
require.NoError(t, err)
tmpfile.Close()
tests := []struct {
name string
size int64
wantDiff bool
wantSize int64
}{
{"same size", 5, false, 5},
{"different size smaller", 3, true, 5},
{"different size larger", 10, true, 5},
{"zero", 0, true, 5},
{"negative", -1, true, 5},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
newSize, diff := FileSizeDifferentTo(tmpfile.Name(), tt.size)
assert.Equal(t, tt.wantDiff, diff)
assert.Equal(t, tt.wantSize, newSize)
})
}
}
func TestFileSizeDifferentTo_NonexistentFile(t *testing.T) {
_, diff := FileSizeDifferentTo("/nonexistent/file.txt", 5)
assert.True(t, diff)
}
func TestFileSizeDifferentTo_EmptyFile(t *testing.T) {
tmpfile, err := os.CreateTemp("", "test*.txt")
require.NoError(t, err)
tmpfile.Close()
defer os.Remove(tmpfile.Name())
newSize, diff := FileSizeDifferentTo(tmpfile.Name(), 0)
assert.False(t, diff)
assert.Equal(t, int64(0), newSize)
_, diff = FileSizeDifferentTo(tmpfile.Name(), 1)
assert.True(t, diff)
}
//======================================================================