-
Notifications
You must be signed in to change notification settings - Fork 2
/
mac_test.go
68 lines (55 loc) · 1.35 KB
/
mac_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
package macgen
import (
"fmt"
"net"
"strconv"
"testing"
"github.com/stretchr/testify/assert"
)
type TestPrefixValues struct {
Input bool
Test bool
}
func TestGenerateRandomLocalPrefix(t *testing.T) {
TestValues := []TestPrefixValues{
{true, false},
{false, true},
}
for _, test := range TestValues {
prefix := GenerateRandomLocalMacPrefix(test.Input)
ui, err := strconv.ParseUint(prefix[0:2], 16, 8)
if err != nil {
t.Error(err)
}
bin := fmt.Sprintf("%08b", ui)
asciiRep := 48
if test.Test {
asciiRep = 49
}
if int(bin[7]) != asciiRep {
t.Errorf("uni/multicast bit not set correctly, got %v expected first bit to be %d", bin, asciiRep-48)
}
if int(bin[6]) != 49 {
t.Errorf("LAA bit not set correctly, got %v expected second bit to be 1", bin)
}
}
}
type TestSufixValues struct {
Input net.IP
Output string
Error error
}
func TestCalculateNICSufix(t *testing.T) {
TestValues := []TestSufixValues{
{net.ParseIP("10.0.0.0"), "00:00:00", nil},
{net.ParseIP("10.255.255.255"), "ff:ff:ff", nil},
{net.ParseIP("192.168.12.127"), "04:4a:7f", nil},
{net.ParseIP("::1"), "", fmt.Errorf("ip is not v4")},
{net.ParseIP("foo"), "", fmt.Errorf("ip is not v4")},
}
for _, test := range TestValues {
mac, err := CalculateNICSufix(test.Input)
assert.Equal(t, test.Output, mac)
assert.Equal(t, test.Error, err)
}
}