forked from davidbb/ouidb
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathouitools_test.go
112 lines (93 loc) · 2.21 KB
/
ouitools_test.go
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
// Package go-oui provides functions to work with MAC and OUI's
package ouidb
import (
"fmt"
"math/rand"
"testing"
)
var db *OuiDB
func lookup(t *testing.T, mac, org string) {
if db == nil {
t.Fatal("database not initialized")
}
v, err := db.Lookup(mac)
if err != nil {
t.Fatalf("parse: %s: %s", mac, err.Error())
}
if v != org {
t.Fatalf("lookup: input %s, expect %q, got %q", mac, org, v)
}
//t.Logf("%s => %s\n", mac, v)
}
func string48(b [6]byte) string {
return fmt.Sprintf("%02x:%02x:%02x:%02x:%02x:%02x",
b[0], b[1], b[2], b[3], b[4], b[5])
}
func string24(b [3]byte) string {
return fmt.Sprintf("%02x:%02x:%02x:aa:bb:cc", b[0], b[1], b[2])
}
func invalid(t *testing.T, mac string) {
if db == nil {
t.Fatal("database not initialized")
}
v, err := db.Lookup(mac)
if err == nil {
t.Fatalf("didn't fail on invalid %s, got %s", mac, v)
}
}
func TestInitialization(t *testing.T) {
var err error
db, err = New("oui.txt")
if err != nil {
t.Fatalf("can't load database file oui.txt: %s", err)
}
}
func TestMissingDBFile(t *testing.T) {
_, err := New("bad-file")
if err == nil {
t.Fatal("didn't return err on missing file")
}
}
func TestInvalidDBFile(t *testing.T) {
_, err := New("ouidb_test.go")
if err == nil {
t.Fatal("didn't return err on bad file")
}
}
func TestLookup24(t *testing.T) {
lookup(t, "60:03:08:a0:ec:a6", "Apple")
}
func TestLookup36(t *testing.T) {
lookup(t, "00:1B:C5:00:E1:55", "VigorEle")
}
func TestLookup40(t *testing.T) {
lookup(t, "20-52-45-43-56-aa", "Receive")
}
func TestLookupUnknown(t *testing.T) {
lookup(t, "ff:ff:00:a0:ec:a6", "")
}
func TestFormatSingleZero(t *testing.T) {
lookup(t, "0:25:9c:42:0:62", "Cisco-Li")
}
func TestFormatUppercase(t *testing.T) {
lookup(t, "0:25:9C:42:C2:62", "Cisco-Li")
}
func TestInvalidMAC1(t *testing.T) {
invalid(t, "00:25-:9C:42:C2:62")
}
func TestLookupAll48(t *testing.T) {
for _, b := range db.blocks48 {
lookup(t, string48(b.oui), b.Organization())
}
}
func TestLookupAll24(t *testing.T) {
for _, b := range db.blocks24 {
lookup(t, string24(b.oui), b.Organization())
}
}
func BenchmarkAll(b *testing.B) {
for n := 0; n < b.N; n++ {
b := db.blocks24[rand.Intn(len(db.blocks24))]
db.Lookup(string24(b.oui))
}
}