summaryrefslogtreecommitdiff
path: root/adapters/arp/arp_test.go
diff options
context:
space:
mode:
Diffstat (limited to 'adapters/arp/arp_test.go')
-rw-r--r--adapters/arp/arp_test.go51
1 files changed, 51 insertions, 0 deletions
diff --git a/adapters/arp/arp_test.go b/adapters/arp/arp_test.go
new file mode 100644
index 0000000..9a215e0
--- /dev/null
+++ b/adapters/arp/arp_test.go
@@ -0,0 +1,51 @@
+package arp
+
+import "testing"
+
+const sample = `Neighbor Linklayer Address
+? (192.168.1.5) at 8:0:27:ab:cd:ef on en0 ifscope [ethernet]
+? (192.168.1.9) at AA:BB:CC:11:22:33 [ether] on br-lan
+? (10.0.0.2) at de:ad:be:ef:00:01 on eth0
+? (192.168.1.250) at (incomplete) on en0 [ethernet]
+
+`
+
+func TestParseTable(t *testing.T) {
+ table := parseTable(sample)
+
+ got := make(map[string]string, len(table.AddrToMac))
+ for ip, mac := range table.AddrToMac {
+ got[ip.Format()] = mac.Format()
+ }
+
+ want := map[string]string{
+ "192.168.1.5": "08:00:27:ab:cd:ef",
+ "192.168.1.9": "aa:bb:cc:11:22:33",
+ "10.0.0.2": "de:ad:be:ef:00:01",
+ }
+
+ if len(got) != len(want) {
+ t.Fatalf("parsed %d entries %v, want %d (header/incomplete lines should be skipped)", len(got), got, len(want))
+ }
+ for ip, mac := range want {
+ if got[ip] != mac {
+ t.Errorf("entry %q = %q, want %q", ip, got[ip], mac)
+ }
+ }
+}
+
+func TestTableLookup(t *testing.T) {
+ table := parseTable(sample)
+
+ if mac, ok := table.Lookup("192.168.1.5"); !ok || mac != "08:00:27:ab:cd:ef" {
+ t.Errorf("Lookup(192.168.1.5) = %q,%v", mac, ok)
+ }
+ if _, ok := table.Lookup("1.2.3.4"); ok {
+ t.Error("Lookup of a miss should be false")
+ }
+ if _, ok := table.Lookup("::1"); ok {
+ t.Error("Lookup of non-IPv4 should be false")
+ }
+}
+
+var _ TableProvider = SystemProvider{}