| 1 | package network |
| 2 | |
| 3 | import ( |
| 4 | "net" |
| 5 | "testing" |
| 6 | |
| 7 | "github.com/evilsocket/islazy/data" |
| 8 | ) |
| 9 | |
| 10 | func TestIsZeroMac(t *testing.T) { |
| 11 | exampleMAC, _ := net.ParseMAC("00:00:00:00:00:00") |
| 12 | |
| 13 | exp := true |
| 14 | got := IsZeroMac(exampleMAC) |
| 15 | if got != exp { |
| 16 | t.Fatalf("expected '%t', got '%t'", exp, got) |
| 17 | } |
| 18 | } |
| 19 | |
| 20 | func TestIsBroadcastMac(t *testing.T) { |
| 21 | exampleMAC, _ := net.ParseMAC("ff:ff:ff:ff:ff:ff") |
| 22 | |
| 23 | exp := true |
| 24 | got := IsBroadcastMac(exampleMAC) |
| 25 | if got != exp { |
| 26 | t.Fatalf("expected '%t', got '%t'", exp, got) |
| 27 | } |
| 28 | } |
| 29 | |
| 30 | func TestNormalizeMac(t *testing.T) { |
| 31 | exp := "ff:ff:ff:ff:ff:ff" |
| 32 | got := NormalizeMac("fF-fF-fF-fF-fF-fF") |
| 33 | if got != exp { |
| 34 | t.Fatalf("expected '%s', got '%s'", exp, got) |
| 35 | } |
| 36 | } |
| 37 | |
| 38 | // TODO: refactor to parse targets with an actual alias map |
| 39 | func TestParseTargets(t *testing.T) { |
| 40 | aliasMap, err := data.NewMemUnsortedKV() |
| 41 | if err != nil { |
| 42 | panic(err) |
| 43 | } |
| 44 | |
| 45 | aliasMap.Set("5c:00:0b:90:a9:f0", "test_alias") |
| 46 | aliasMap.Set("5c:00:0b:90:a9:f1", "Home_Laptop") |
| 47 | |
| 48 | cases := []struct { |
| 49 | Name string |
| 50 | InputTargets string |
| 51 | InputAliases *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 | &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 | &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.Run(test.Name, func(t *testing.T) { |
| 85 | ips, macs, err := ParseTargets(test.InputTargets, test.InputAliases) |
| 86 | if err != nil && !test.ExpectedError { |
| 87 | t.Errorf("unexpected error: %s", err) |
| 88 | } |
| 89 | if err == nil && test.ExpectedError { |
| 90 | t.Error("Expected error, but got none") |
| 91 | } |
| 92 | if test.ExpectedError { |
| 93 | return |
| 94 | } |
| 95 | if len(ips) != test.ExpectedIPCount { |
| 96 | t.Errorf("Wrong number of IPs. Got %v for targets %s", ips, test.InputTargets) |
| 97 | } |
| 98 | if len(macs) != test.ExpectedMACCount { |
| 99 | t.Errorf("Wrong number of MACs. Got %v for targets %s", macs, test.InputTargets) |
| 100 | } |
| 101 | }) |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | func TestBuildEndpointFromInterface(t *testing.T) { |
| 106 | ifaces, err := net.Interfaces() |
| 107 | if err != nil { |
| 108 | t.Error(err) |
| 109 | } |
| 110 | if len(ifaces) <= 0 { |
| 111 | t.Error("Unable to find any network interfaces to run test with.") |
| 112 | } |
| 113 | _, err = buildEndpointFromInterface(ifaces[0]) |
| 114 | if err != nil { |
| 115 | t.Error(err) |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | func TestFindInterfaceByName(t *testing.T) { |
| 120 | ifaces, err := net.Interfaces() |
| 121 | if err != nil { |
| 122 | t.Error(err) |
| 123 | } |
| 124 | if len(ifaces) <= 0 { |
| 125 | t.Error("Unable to find any network interfaces to run test with.") |
| 126 | } |
| 127 | var exampleIface 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.HardwareAddr != nil { |
| 132 | exampleIface = iface |
| 133 | break |
| 134 | } |
| 135 | } |
| 136 | foundEndpoint, err := findInterfaceByName(exampleIface.Name, ifaces) |
| 137 | if err != nil { |
| 138 | t.Error("unable to find a given interface by name to build endpoint", err) |
| 139 | } |
| 140 | if foundEndpoint.Name() != exampleIface.Name { |
| 141 | t.Error("unable to find a given interface by name to build endpoint") |
| 142 | } |
| 143 | } |
| 144 | |
| 145 | func TestFindInterface(t *testing.T) { |
| 146 | ifaces, err := net.Interfaces() |
| 147 | if err != nil { |
| 148 | t.Error(err) |
| 149 | } |
| 150 | if len(ifaces) <= 0 { |
| 151 | t.Error("Unable to find any network interfaces to run test with.") |
| 152 | } |
| 153 | var exampleIface 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.HardwareAddr != nil { |
| 158 | exampleIface = iface |
| 159 | break |
| 160 | } |
| 161 | } |
| 162 | foundEndpoint, err := FindInterface(exampleIface.Name) |
| 163 | if err != nil { |
| 164 | t.Error("unable to find a given interface by name to build endpoint", err) |
| 165 | } |
| 166 | if foundEndpoint.Name() != exampleIface.Name { |
| 167 | t.Error("unable to find a given interface by name to build endpoint") |
| 168 | } |
| 169 | } |