-
Notifications
You must be signed in to change notification settings - Fork 0
/
mac_test.go
162 lines (126 loc) · 4.63 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
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
package macpot_test
import (
"regexp"
"testing"
"github.com/stretchr/testify/assert"
"github.com/yitsushi/macpot"
)
func TestNew(t *testing.T) {
mac, err := macpot.New()
assert.NoError(t, err)
assert.Condition(t, func() bool {
re := regexp.MustCompile("^[a-z0-9]+(:[a-z0-9]{2}){5}$")
return re.Match([]byte(mac.ToString()))
}, "random generated MAC address is not valid: %s", mac.ToString())
}
func TestNewFromBytes(t *testing.T) {
mac := macpot.NewFromBytes([]byte{0x11, 0x22, 0x33, 0x44, 0x55, 0x66})
assert.Equal(t, "11:22:33:44:55:66", mac.ToString())
}
func TestNewFromBytes_longer(t *testing.T) {
mac := macpot.NewFromBytes([]byte{0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88})
assert.Equal(t, "11:22:33:44:55:66", mac.ToString())
}
func TestNewFromBytes_shorter(t *testing.T) {
mac := macpot.NewFromBytes([]byte{0x11, 0x22, 0x33, 0x44})
assert.Equal(t, "11:22:33:44:00:00", mac.ToString())
}
func TestNewFromUint64(t *testing.T) {
mac := macpot.NewFromUint64(18838586676582)
assert.Equal(t, "11:22:33:44:55:66", mac.ToString())
assert.Equal(t, uint64(18838586676582), mac.ToUint64())
}
func TestNewFromUint64_max(t *testing.T) {
mac := macpot.NewFromUint64(281474976710655)
assert.Equal(t, "ff:ff:ff:ff:ff:ff", mac.ToString())
}
func TestNewFromUint64_overflow(t *testing.T) {
mac := macpot.NewFromUint64(281474976710665)
assert.Equal(t, "00:00:00:00:00:09", mac.ToString())
}
func TestMac_Next(t *testing.T) {
mac := macpot.NewFromBytes([]byte{0x11, 0x22, 0x33, 0x44, 0x55, 0x66})
assert.Equal(t, "11:22:33:44:55:67", mac.Next().ToString())
}
func TestMac_Next_overflow(t *testing.T) {
mac := macpot.NewFromBytes([]byte{0x11, 0x22, 0x33, 0x44, 0xff, 0xff})
assert.Equal(t, "11:22:33:45:00:00", mac.Next().ToString())
}
func TestMac_SetLocal(t *testing.T) {
mac := macpot.NewFromBytes([]byte{0x11, 0x22, 0x33, 0x44, 0x55, 0x66})
assert.False(t, mac.IsLocal())
mac.SetLocal()
assert.Equal(t, "13:22:33:44:55:66", mac.ToString())
assert.True(t, mac.IsLocal())
}
func TestMac_SetLocal_noChanges(t *testing.T) {
mac := macpot.NewFromBytes([]byte{0x13, 0x22, 0x33, 0x44, 0x55, 0x66})
assert.True(t, mac.IsLocal())
mac.SetLocal()
assert.Equal(t, "13:22:33:44:55:66", mac.ToString())
assert.True(t, mac.IsLocal())
}
func TestMac_SetGlobal(t *testing.T) {
mac := macpot.NewFromBytes([]byte{0x13, 0x22, 0x33, 0x44, 0x55, 0x66})
assert.False(t, mac.IsGlobal())
mac.SetGlobal()
assert.Equal(t, "11:22:33:44:55:66", mac.ToString())
assert.True(t, mac.IsGlobal())
}
func TestMac_SetGlobal_noChanges(t *testing.T) {
mac := macpot.NewFromBytes([]byte{0x11, 0x22, 0x33, 0x44, 0x55, 0x66})
assert.True(t, mac.IsGlobal())
mac.SetGlobal()
assert.Equal(t, "11:22:33:44:55:66", mac.ToString())
assert.True(t, mac.IsGlobal())
}
func TestMac_SetUnicast(t *testing.T) {
mac := macpot.NewFromBytes([]byte{0x13, 0x22, 0x33, 0x44, 0x55, 0x66})
assert.False(t, mac.IsUnicast())
mac.SetUnicast()
assert.Equal(t, "12:22:33:44:55:66", mac.ToString())
assert.True(t, mac.IsUnicast())
}
func TestMac_SetUnicast_noChanges(t *testing.T) {
mac := macpot.NewFromBytes([]byte{0x12, 0x22, 0x33, 0x44, 0x55, 0x66})
assert.True(t, mac.IsUnicast())
mac.SetUnicast()
assert.Equal(t, "12:22:33:44:55:66", mac.ToString())
assert.True(t, mac.IsUnicast())
}
func TestMac_SetMulticast(t *testing.T) {
mac := macpot.NewFromBytes([]byte{0x12, 0x22, 0x33, 0x44, 0x55, 0x66})
assert.False(t, mac.IsMulticast())
mac.SetMulticast()
assert.Equal(t, "13:22:33:44:55:66", mac.ToString())
assert.True(t, mac.IsMulticast())
}
func TestMac_SetMulticast_noChanges(t *testing.T) {
mac := macpot.NewFromBytes([]byte{0x13, 0x22, 0x33, 0x44, 0x55, 0x66})
assert.True(t, mac.IsMulticast())
mac.SetMulticast()
assert.Equal(t, "13:22:33:44:55:66", mac.ToString())
assert.True(t, mac.IsMulticast())
}
func TestMac_SetOctet(t *testing.T) {
mac := macpot.NewFromBytes([]byte{0x11, 0x22, 0x33, 0x44, 0x55, 0x66})
err := mac.SetOctet(3, 0x99)
assert.NoError(t, err)
assert.Equal(t, "11:22:33:99:55:66", mac.ToString())
}
func TestMac_SetOctet_negativeIndex(t *testing.T) {
mac := macpot.NewFromBytes([]byte{0x11, 0x22, 0x33, 0x44, 0x55, 0x66})
err := mac.SetOctet(-3, 0x99)
assert.Error(t, err)
assert.ErrorAs(t, err, &macpot.OutOfBoundError{})
assert.Equal(t, "11:22:33:44:55:66", mac.ToString())
assert.Equal(t, "unable to set octet -3", err.Error())
}
func TestMac_SetOctet_largeIndex(t *testing.T) {
mac := macpot.NewFromBytes([]byte{0x11, 0x22, 0x33, 0x44, 0x55, 0x66})
err := mac.SetOctet(55, 0x99)
assert.Error(t, err)
assert.ErrorAs(t, err, &macpot.OutOfBoundError{})
assert.Equal(t, "11:22:33:44:55:66", mac.ToString())
assert.Equal(t, "unable to set octet 55", err.Error())
}