1// Copyright 2019-2020 Graham Clark. All rights reserved. Use of this source 2// code is governed by the MIT license that can be found in the LICENSE 3// file. 4 5package termshark 6 7import ( 8 "bytes" 9 "testing" 10 11 "github.com/blang/semver" 12 "github.com/gcla/termshark/v2/format" 13 "github.com/stretchr/testify/assert" 14) 15 16//====================================================================== 17 18func TestApplyArgs(t *testing.T) { 19 cmd := []string{"echo", "something", "$3", "else", "$1", "$3"} 20 args := []string{"a1", "a2"} 21 eres := []string{"echo", "something", "$3", "else", "a1", "$3"} 22 res, total := ApplyArguments(cmd, args) 23 assert.Equal(t, eres, res) 24 assert.Equal(t, total, 1) 25 26 args = []string{"a1", "a2", "a3"} 27 eres = []string{"echo", "something", "a3", "else", "a1", "a3"} 28 res, total = ApplyArguments(cmd, args) 29 assert.Equal(t, eres, res) 30 assert.Equal(t, total, 3) 31} 32 33func TestArgConv(t *testing.T) { 34 var tests = []struct { 35 arg string 36 flag string 37 val string 38 res bool 39 }{ 40 {"--tshark-d=foo", "d", "foo", true}, 41 {"--tshark-abc=foo", "", "", false}, 42 {"--tshark-V=true", "V", "", true}, 43 {"--tshark-V=false", "", "", false}, 44 {"--ts-V=wow", "", "", false}, 45 } 46 47 for _, test := range tests { 48 f, v, ok := ConvertArgToTShark(test.arg) 49 assert.Equal(t, test.res, ok) 50 if test.res { 51 assert.Equal(t, test.flag, f) 52 assert.Equal(t, test.val, v) 53 } 54 } 55} 56 57func TestVer1(t *testing.T) { 58 out1 := `TShark (Wireshark) 2.6.6 (Git v2.6.6 packaged as 2.6.6-1~ubuntu18.04.0) 59 60Copyright 1998-2019 Gerald Combs <gerald@wireshark.org> and contributors.` 61 62 v1, err := TSharkVersionFromOutput(out1) 63 assert.NoError(t, err) 64 res, _ := semver.Make("2.6.6") 65 assert.Equal(t, res, v1) 66} 67 68func TestVer2(t *testing.T) { 69 out1 := `TShark 1.6.7 70 71Copyright 1998-2012 Gerald Combs <gerald@wireshark.org> and contributors. 72This is free software; see the source for copying conditions. There is NO 73warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 74 75Compiled (64-bit) with GLib 2.32.0, with libpcap (version unknown), with libz 761.2.3.4, with POSIX capabilities (Linux), without libpcre, with SMI 0.4.8, with 77c-ares 1.7.5, with Lua 5.1, without Python, with GnuTLS 2.12.14, with Gcrypt 781.5.0, with MIT Kerberos, with GeoIP. 79 80Running on Linux 3.2.0-126-generic, with libpcap version 1.1.1, with libz 811.2.3.4. 82` 83 84 v1, err := TSharkVersionFromOutput(out1) 85 assert.NoError(t, err) 86 res, _ := semver.Make("1.6.7") 87 assert.Equal(t, res, v1) 88} 89 90func TestInterfaces1(t *testing.T) { 91 out1 := ` 921. \Device\NPF_{BAC1CFBD-DE27-4023-B478-0C490B99DC5E} (Local Area Connection 2) 932. \Device\NPF_{78032B7E-4968-42D3-9F37-287EA86C0AAA} (Local Area Connection* 10) 943. \Device\NPF_{84E7CAE6-E96F-4F31-96FD-170B0F514AB2} (Npcap Loopback Adapter) 954. \Device\NPF_NdisWanIpv6 (NdisWan Adapter) 965. \Device\NPF_{503E1F71-C57C-438D-B004-EA5563723C16} (Local Area Connection 5) 976. \Device\NPF_{15DDE443-C208-4328-8919-9666682EE804} (Local Area Connection* 11) 98`[1:] 99 interfaces, err := interfacesFrom(bytes.NewReader([]byte(out1))) 100 assert.NoError(t, err) 101 assert.Equal(t, 6, len(interfaces)) 102 v := interfaces[2] 103 assert.Equal(t, `\Device\NPF_{78032B7E-4968-42D3-9F37-287EA86C0AAA}`, v[1]) 104 assert.Equal(t, `Local Area Connection* 10`, v[0]) 105} 106 107func TestInterfaces2(t *testing.T) { 108 out1 := ` 1091. eth0 1102. ham0 1113. docker0 1124. vethd45103d 1135. lo (Loopback) 1146. mpqemubr0-dummy 1157. nflog 1168. nfqueue 1179. bluetooth0 11810. virbr0-nic 11911. vboxnet0 12012. ciscodump (Cisco remote capture) 12113. dpauxmon (DisplayPort AUX channel monitor capture) 12214. randpkt (Random packet generator) 12315. sdjournal (systemd Journal Export) 12416. sshdump (SSH remote capture) 12517. udpdump (UDP Listener remote capture) 126`[1:] 127 interfaces, err := interfacesFrom(bytes.NewReader([]byte(out1))) 128 assert.NoError(t, err) 129 assert.Equal(t, 17, len(interfaces)) 130 v := interfaces[3] 131 assert.Equal(t, `docker0`, v[0]) 132 v = interfaces[12] 133 assert.Equal(t, `Cisco remote capture`, v[0]) 134 assert.Equal(t, `ciscodump`, v[1]) 135} 136 137func TestConv1(t *testing.T) { 138 var tests = []struct { 139 arg string 140 res string 141 }{ 142 {"hello\x41world\x42", "helloAworldB"}, 143 {"80 \xe2\x86\x92 53347", "80 → 53347"}, 144 {"hello\x41world\x42 foo \\000 bar", "helloAworldB foo \\000 bar"}, 145 } 146 147 for _, test := range tests { 148 outs := format.TranslateHexCodes([]byte(test.arg)) 149 assert.Equal(t, string(outs), test.res) 150 } 151} 152 153//====================================================================== 154// Local Variables: 155// mode: Go 156// fill-column: 110 157// End: 158