summaryrefslogtreecommitdiff
path: root/adapters/arp/arp_test.go
blob: 9a215e0d1ca80cb8748a8de0df9e4723dc2f3b90 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
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{}