From 36fb41695201e51de5676fc606687f547e3c7263 Mon Sep 17 00:00:00 2001 From: Kent Gruber Date: Tue, 1 May 2018 10:53:09 -0400 Subject: [PATCH 1/2] add basic findInterfaceByName test --- network/net_test.go | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/network/net_test.go b/network/net_test.go index 7604d4c8..32d7fc4d 100644 --- a/network/net_test.go +++ b/network/net_test.go @@ -60,3 +60,29 @@ func TestBuildEndpointFromInterface(t *testing.T) { t.Error(err) } } + +func TestFindInterfaceByName(t *testing.T) { + ifaces, err := net.Interfaces() + if err != nil { + t.Error(err) + } + if len(ifaces) <= 0 { + t.Error("Unable to find any network interfaces to run test with.") + } + var exampleIface net.Interface + // emulate libpcap's pcap_lookupdev function to find + // default interface to test with ( maybe could use loopback ? ) + for _, iface := range ifaces { + if iface.HardwareAddr != nil { + exampleIface = iface + break + } + } + foundEndpoint, err := findInterfaceByName(exampleIface.Name, ifaces) + if err != nil { + t.Error("unable to find a given interface by name to build endpoint", err) + } + if foundEndpoint.Name() != exampleIface.Name { + t.Error("unable to find a given interface by name to build endpoint") + } +} From dcd5001357c2c3db56ffc8ca563a515a1b2bd194 Mon Sep 17 00:00:00 2001 From: Kent Gruber Date: Tue, 1 May 2018 11:01:31 -0400 Subject: [PATCH 2/2] add basic matchByAddress test checks both mac address and ip address cases --- network/net_test.go | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/network/net_test.go b/network/net_test.go index 32d7fc4d..b20b4588 100644 --- a/network/net_test.go +++ b/network/net_test.go @@ -86,3 +86,35 @@ func TestFindInterfaceByName(t *testing.T) { t.Error("unable to find a given interface by name to build endpoint") } } + +func TestMatchByAddress(t *testing.T) { + ifaces, err := net.Interfaces() + if err != nil { + t.Error(err) + } + if len(ifaces) <= 0 { + t.Error("Unable to find any network interfaces to run test with.") + } + var exampleIface net.Interface + // emulate libpcap's pcap_lookupdev function to find + // default interface to test with ( maybe could use loopback ? ) + for _, iface := range ifaces { + if iface.HardwareAddr != nil { + exampleIface = iface + break + } + } + // check match by mac address + if !matchByAddress(exampleIface, exampleIface.HardwareAddr.String()) { + t.Error("unable to verify a match with a given interface and mac address") + } + addrs, err := exampleIface.Addrs() + if err != nil || len(addrs) <= 0 { + t.Error("Unable to find any ip addresses to run test with interface:", exampleIface.Name) + + } + // check match by ip address + if !matchByAddress(exampleIface, addrs[0].String()) { + t.Error("unable to verify a match with a given interface and ip address") + } +}