| 1 | package networkscip-go gomod github.com/bettercap/bettercap . `github.com/bettercap/bettercap/network`/ |
| 2 | |
| 3 | import ( |
| 4 | "netscip-go gomod github.com/golang/go/src go1.16 net/" |
| 5 | "testingscip-go gomod github.com/golang/go/src go1.16 testing/" |
| 6 | |
| 7 | "github.com/evilsocket/islazy/datascip-go gomod github.com/evilsocket/islazy v1.11.0 `github.com/evilsocket/islazy/data`/" |
| 8 | ) |
| 9 | |
| 10 | func TestIsZeroMacscip-go gomod github.com/bettercap/bettercap . `github.com/bettercap/bettercap/network`/TestIsZeroMac().(t *testingscip-go gomod github.com/golang/go/src go1.16 testing/.Tscip-go gomod github.com/golang/go/src go1.16 testing/T#) { |
| 11 | exampleMAC, _ := netscip-go gomod github.com/golang/go/src go1.16 net/.ParseMACscip-go gomod github.com/golang/go/src go1.16 net/ParseMAC().("00:00:00:00:00:00") |
| 12 | |
| 13 | exp := true |
| 14 | got := IsZeroMacscip-go gomod github.com/bettercap/bettercap . `github.com/bettercap/bettercap/network`/IsZeroMac().(exampleMAC) |
| 15 | if got != exp { |
| 16 | t.Fatalfscip-go gomod github.com/golang/go/src go1.16 testing/common#Fatalf().("expected '%t', got '%t'", exp, got) |
| 17 | } |
| 18 | } |
| 19 | |
| 20 | func TestIsBroadcastMacscip-go gomod github.com/bettercap/bettercap . `github.com/bettercap/bettercap/network`/TestIsBroadcastMac().(t *testingscip-go gomod github.com/golang/go/src go1.16 testing/.Tscip-go gomod github.com/golang/go/src go1.16 testing/T#) { |
| 21 | exampleMAC, _ := netscip-go gomod github.com/golang/go/src go1.16 net/.ParseMACscip-go gomod github.com/golang/go/src go1.16 net/ParseMAC().("ff:ff:ff:ff:ff:ff") |
| 22 | |
| 23 | exp := true |
| 24 | got := IsBroadcastMacscip-go gomod github.com/bettercap/bettercap . `github.com/bettercap/bettercap/network`/IsBroadcastMac().(exampleMAC) |
| 25 | if got != exp { |
| 26 | t.Fatalfscip-go gomod github.com/golang/go/src go1.16 testing/common#Fatalf().("expected '%t', got '%t'", exp, got) |
| 27 | } |
| 28 | } |
| 29 | |
| 30 | func TestNormalizeMacscip-go gomod github.com/bettercap/bettercap . `github.com/bettercap/bettercap/network`/TestNormalizeMac().(t *testingscip-go gomod github.com/golang/go/src go1.16 testing/.Tscip-go gomod github.com/golang/go/src go1.16 testing/T#) { |
| 31 | exp := "ff:ff:ff:ff:ff:ff" |
| 32 | got := NormalizeMacscip-go gomod github.com/bettercap/bettercap . `github.com/bettercap/bettercap/network`/NormalizeMac().("fF-fF-fF-fF-fF-fF") |
| 33 | if got != exp { |
| 34 | t.Fatalfscip-go gomod github.com/golang/go/src go1.16 testing/common#Fatalf().("expected '%s', got '%s'", exp, got) |
| 35 | } |
| 36 | } |
| 37 | |
| 38 | // TODO: refactor to parse targets with an actual alias map |
| 39 | func TestParseTargetsscip-go gomod github.com/bettercap/bettercap . `github.com/bettercap/bettercap/network`/TestParseTargets().(t *testingscip-go gomod github.com/golang/go/src go1.16 testing/.Tscip-go gomod github.com/golang/go/src go1.16 testing/T#) { |
| 40 | aliasMap, err := datascip-go gomod github.com/evilsocket/islazy v1.11.0 `github.com/evilsocket/islazy/data`/.NewMemUnsortedKVscip-go gomod github.com/evilsocket/islazy v1.11.0 `github.com/evilsocket/islazy/data`/NewMemUnsortedKV().() |
| 41 | if err != nil { |
| 42 | panic(err) |
| 43 | } |
| 44 | |
| 45 | aliasMap.Setscip-go gomod github.com/evilsocket/islazy v1.11.0 `github.com/evilsocket/islazy/data`/UnsortedKV#Set().("5c:00:0b:90:a9:f0", "test_alias") |
| 46 | aliasMap.Setscip-go gomod github.com/evilsocket/islazy v1.11.0 `github.com/evilsocket/islazy/data`/UnsortedKV#Set().("5c:00:0b:90:a9:f1", "Home_Laptop") |
| 47 | |
| 48 | cases := []struct { |
| 49 | Name string |
| 50 | InputTargets string |
| 51 | InputAliases *datascip-go gomod github.com/evilsocket/islazy v1.11.0 `github.com/evilsocket/islazy/data`/.UnsortedKVscip-go gomod github.com/evilsocket/islazy v1.11.0 `github.com/evilsocket/islazy/data`/UnsortedKV# |
| 52 | ExpectedIPCount int |
| 53 | ExpectedMACCount int |
| 54 | ExpectedError bool |
| 55 | }{ |
| 56 | // Not sure how to trigger sad path where macParser.FindAllString() |
| 57 | // finds a MAC but net.ParseMac() fails on the result. |
| 58 | { |
| 59 | "empty target string causes empty return", |
| 60 | "", |
| 61 | &datascip-go gomod github.com/evilsocket/islazy v1.11.0 `github.com/evilsocket/islazy/data`/.UnsortedKVscip-go gomod github.com/evilsocket/islazy v1.11.0 `github.com/evilsocket/islazy/data`/UnsortedKV#{}, |
| 62 | 0, |
| 63 | 0, |
| 64 | false, |
| 65 | }, |
| 66 | { |
| 67 | "MACs are parsed", |
| 68 | "192.168.1.2, 192.168.1.3, 5c:00:0b:90:a9:f0, 6c:00:0b:90:a9:f0, 6C:00:0B:90:A9:F0", |
| 69 | &datascip-go gomod github.com/evilsocket/islazy v1.11.0 `github.com/evilsocket/islazy/data`/.UnsortedKVscip-go gomod github.com/evilsocket/islazy v1.11.0 `github.com/evilsocket/islazy/data`/UnsortedKV#{}, |
| 70 | 2, |
| 71 | 3, |
| 72 | false, |
| 73 | }, |
| 74 | { |
| 75 | "Aliases are parsed", |
| 76 | "test_alias, Home_Laptop", |
| 77 | aliasMap, |
| 78 | 0, |
| 79 | 2, |
| 80 | false, |
| 81 | }, |
| 82 | } |
| 83 | for _, test := range cases { |
| 84 | t.Runscip-go gomod github.com/golang/go/src go1.16 testing/T#Run().(test.Name, func(t *testingscip-go gomod github.com/golang/go/src go1.16 testing/.Tscip-go gomod github.com/golang/go/src go1.16 testing/T#) { |
| 85 | ips, macs, err := ParseTargetsscip-go gomod github.com/bettercap/bettercap . `github.com/bettercap/bettercap/network`/ParseTargets().(test.InputTargets, test.InputAliases) |
| 86 | if err != nil && !test.ExpectedError { |
| 87 | t.Errorfscip-go gomod github.com/golang/go/src go1.16 testing/common#Errorf().("unexpected error: %s", err) |
| 88 | } |
| 89 | if err == nil && test.ExpectedError { |
| 90 | t.Errorscip-go gomod github.com/golang/go/src go1.16 testing/common#Error().("Expected error, but got none") |
| 91 | } |
| 92 | if test.ExpectedError { |
| 93 | return |
| 94 | } |
| 95 | if len(ips) != test.ExpectedIPCount { |
| 96 | t.Errorfscip-go gomod github.com/golang/go/src go1.16 testing/common#Errorf().("Wrong number of IPs. Got %v for targets %s", ips, test.InputTargets) |
| 97 | } |
| 98 | if len(macs) != test.ExpectedMACCount { |
| 99 | t.Errorfscip-go gomod github.com/golang/go/src go1.16 testing/common#Errorf().("Wrong number of MACs. Got %v for targets %s", macs, test.InputTargets) |
| 100 | } |
| 101 | }) |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | func TestBuildEndpointFromInterfacescip-go gomod github.com/bettercap/bettercap . `github.com/bettercap/bettercap/network`/TestBuildEndpointFromInterface().(t *testingscip-go gomod github.com/golang/go/src go1.16 testing/.Tscip-go gomod github.com/golang/go/src go1.16 testing/T#) { |
| 106 | ifaces, err := netscip-go gomod github.com/golang/go/src go1.16 net/.Interfacesscip-go gomod github.com/golang/go/src go1.16 net/Interfaces().() |
| 107 | if err != nil { |
| 108 | t.Errorscip-go gomod github.com/golang/go/src go1.16 testing/common#Error().(err) |
| 109 | } |
| 110 | if len(ifaces) <= 0 { |
| 111 | t.Errorscip-go gomod github.com/golang/go/src go1.16 testing/common#Error().("Unable to find any network interfaces to run test with.") |
| 112 | } |
| 113 | _, err = buildEndpointFromInterfacescip-go gomod github.com/bettercap/bettercap . `github.com/bettercap/bettercap/network`/buildEndpointFromInterface().(ifaces[0]) |
| 114 | if err != nil { |
| 115 | t.Errorscip-go gomod github.com/golang/go/src go1.16 testing/common#Error().(err) |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | func TestFindInterfaceByNamescip-go gomod github.com/bettercap/bettercap . `github.com/bettercap/bettercap/network`/TestFindInterfaceByName().(t *testingscip-go gomod github.com/golang/go/src go1.16 testing/.Tscip-go gomod github.com/golang/go/src go1.16 testing/T#) { |
| 120 | ifaces, err := netscip-go gomod github.com/golang/go/src go1.16 net/.Interfacesscip-go gomod github.com/golang/go/src go1.16 net/Interfaces().() |
| 121 | if err != nil { |
| 122 | t.Errorscip-go gomod github.com/golang/go/src go1.16 testing/common#Error().(err) |
| 123 | } |
| 124 | if len(ifaces) <= 0 { |
| 125 | t.Errorscip-go gomod github.com/golang/go/src go1.16 testing/common#Error().("Unable to find any network interfaces to run test with.") |
| 126 | } |
| 127 | var exampleIface netscip-go gomod github.com/golang/go/src go1.16 net/.Interfacescip-go gomod github.com/golang/go/src go1.16 net/Interface# |
| 128 | // emulate libpcap's pcap_lookupdev function to find |
| 129 | // default interface to test with ( maybe could use loopback ? ) |
| 130 | for _, iface := range ifaces { |
| 131 | if iface.HardwareAddrscip-go gomod github.com/golang/go/src go1.16 net/Interface#HardwareAddr. != nil { |
| 132 | exampleIface = iface |
| 133 | break |
| 134 | } |
| 135 | } |
| 136 | foundEndpoint, err := findInterfaceByNamescip-go gomod github.com/bettercap/bettercap . `github.com/bettercap/bettercap/network`/findInterfaceByName().(exampleIface.Namescip-go gomod github.com/golang/go/src go1.16 net/Interface#Name., ifaces) |
| 137 | if err != nil { |
| 138 | t.Errorscip-go gomod github.com/golang/go/src go1.16 testing/common#Error().("unable to find a given interface by name to build endpoint", err) |
| 139 | } |
| 140 | if foundEndpoint.Namescip-go gomod github.com/bettercap/bettercap . `github.com/bettercap/bettercap/network`/Endpoint#Name().() != exampleIface.Namescip-go gomod github.com/golang/go/src go1.16 net/Interface#Name. { |
| 141 | t.Errorscip-go gomod github.com/golang/go/src go1.16 testing/common#Error().("unable to find a given interface by name to build endpoint") |
| 142 | } |
| 143 | } |
| 144 | |
| 145 | func TestFindInterfacescip-go gomod github.com/bettercap/bettercap . `github.com/bettercap/bettercap/network`/TestFindInterface().(t *testingscip-go gomod github.com/golang/go/src go1.16 testing/.Tscip-go gomod github.com/golang/go/src go1.16 testing/T#) { |
| 146 | ifaces, err := netscip-go gomod github.com/golang/go/src go1.16 net/.Interfacesscip-go gomod github.com/golang/go/src go1.16 net/Interfaces().() |
| 147 | if err != nil { |
| 148 | t.Errorscip-go gomod github.com/golang/go/src go1.16 testing/common#Error().(err) |
| 149 | } |
| 150 | if len(ifaces) <= 0 { |
| 151 | t.Errorscip-go gomod github.com/golang/go/src go1.16 testing/common#Error().("Unable to find any network interfaces to run test with.") |
| 152 | } |
| 153 | var exampleIface netscip-go gomod github.com/golang/go/src go1.16 net/.Interfacescip-go gomod github.com/golang/go/src go1.16 net/Interface# |
| 154 | // emulate libpcap's pcap_lookupdev function to find |
| 155 | // default interface to test with ( maybe could use loopback ? ) |
| 156 | for _, iface := range ifaces { |
| 157 | if iface.HardwareAddrscip-go gomod github.com/golang/go/src go1.16 net/Interface#HardwareAddr. != nil { |
| 158 | exampleIface = iface |
| 159 | break |
| 160 | } |
| 161 | } |
| 162 | foundEndpoint, err := FindInterfacescip-go gomod github.com/bettercap/bettercap . `github.com/bettercap/bettercap/network`/FindInterface().(exampleIface.Namescip-go gomod github.com/golang/go/src go1.16 net/Interface#Name.) |
| 163 | if err != nil { |
| 164 | t.Errorscip-go gomod github.com/golang/go/src go1.16 testing/common#Error().("unable to find a given interface by name to build endpoint", err) |
| 165 | } |
| 166 | if foundEndpoint.Namescip-go gomod github.com/bettercap/bettercap . `github.com/bettercap/bettercap/network`/Endpoint#Name().() != exampleIface.Namescip-go gomod github.com/golang/go/src go1.16 net/Interface#Name. { |
| 167 | t.Errorscip-go gomod github.com/golang/go/src go1.16 testing/common#Error().("unable to find a given interface by name to build endpoint") |
| 168 | } |
| 169 | } |