-
Notifications
You must be signed in to change notification settings - Fork 49
/
helper_test.go
160 lines (147 loc) · 3.81 KB
/
helper_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
package tc
import (
"errors"
"net"
"testing"
"github.com/google/go-cmp/cmp"
)
func TestLegacyIPHelper(t *testing.T) {
tests := map[string]struct {
input net.IP
err error
}{
"googleDNSIPv4": {input: net.ParseIP("8.8.8.8")},
"googleDNSIPv6": {input: net.ParseIP("2001:4860:4860::8888"), err: ErrInvalidArg},
"invalidIP": {input: net.ParseIP("foobar"), err: ErrInvalidArg},
}
for name, testcase := range tests {
name := name
testcase := testcase
t.Run(name, func(t *testing.T) {
var num uint32
var err error
if num, err = ipToUint32(testcase.input); err != nil {
if errors.Is(err, testcase.err) {
t.Log("Received expected error")
return
}
t.Fatalf("Received unexpected error: %v", err)
}
if testcase.err != nil {
t.Fatalf("Expected error but got none")
}
got := uint32ToIP(num)
if diff := cmp.Diff(testcase.input, got); diff != "" {
t.Fatalf("TestLegacyIPHelper() mismatch (-want +got):\n%s", diff)
}
})
}
}
func TestRealIPHelper(t *testing.T) {
tests := map[string]struct {
input net.IP
err error
}{
"googleDNSIPv4": {input: net.ParseIP("8.8.8.8")},
"googleDNSIPv6": {input: net.ParseIP("2001:4860:4860::8888")},
"invalidIP": {input: net.ParseIP("foobar"), err: ErrInvalidArg},
}
for name, testcase := range tests {
name := name
testcase := testcase
t.Run(name, func(t *testing.T) {
var ip net.IP
var err error
slice := ipToBytes(testcase.input)
if ip, err = bytesToIP(slice); err != nil {
if errors.Is(err, testcase.err) {
t.Log("Received expected error")
return
}
t.Fatalf("Received unexpected error: %v", err)
}
if testcase.err != nil {
t.Fatalf("Expected error but got none")
}
if diff := cmp.Diff(testcase.input, ip); diff != "" {
t.Fatalf("TestRealIPHelper() mismatch (-want +got):\n%s", diff)
}
})
}
t.Run("invalid byte length", func(t *testing.T) {
for i := 0; i < 20; i++ {
var slice []byte
for j := 0; j < i; j++ {
slice = append(slice, byte(j))
}
_, err := bytesToIP(slice)
if err != nil {
if errors.Is(err, ErrInvalidArg) && (i != net.IPv4len && i != net.IPv6len) {
t.Logf("Received expected error for byte length of %d", i)
continue
}
t.Fatalf("Received unexpected error for slice length of %d: %v", i, err)
}
}
})
}
func TestMacHelper(t *testing.T) {
for _, macStr := range []string{
"00:00:5e:00:53:01",
"02:00:5e:10:00:00:00:01",
"00:00:00:00:fe:80:00:00:00:00:00:00:02:00:5e:10:00:00:00:01",
"00-00-5e-00-53-01",
"02-00-5e-10-00-00-00-01",
"00-00-00-00-fe-80-00-00-00-00-00-00-02-00-5e-10-00-00-00-01",
"0000.5e00.5301",
"0200.5e10.0000.0001",
"0000.0000.fe80.0000.0000.0000.0200.5e10.0000.0001",
} {
macStr := macStr
t.Run(macStr, func(t *testing.T) {
mac, err := net.ParseMAC(macStr)
if err != nil {
t.Fatalf("failed to parse mac string: %v", err)
}
tmp := hardwareAddrToBytes(mac)
mac2 := bytesToHardwareAddr(tmp)
if diff := cmp.Diff(mac, mac2); diff != "" {
t.Fatalf("HardwareAddr missmatch (-want +got):\n%s", diff)
}
})
}
}
func TestEndianSwapUint16(t *testing.T) {
for _, in := range []uint16{2, 32, 96, 254, 65534} {
tmp := endianSwapUint16(in)
out := endianSwapUint16(tmp)
if in != out {
t.Fatalf("%d != %d", in, out)
}
}
}
func TestEndianSwapUint32(t *testing.T) {
for _, in := range []uint32{2, 32, 96, 254, 65534, 65535, 4294967294} {
tmp := endianSwapUint32(in)
out := endianSwapUint32(tmp)
if in != out {
t.Fatalf("%d != %d", in, out)
}
}
}
func TestBytesToInt32(t *testing.T) {
tests := []struct {
in []byte
out int32
}{
{in: []byte{0x1}, out: 0},
{in: []byte{0x0, 0x0, 0x0, 0x2a}, out: 42},
}
for _, test := range tests {
test := test
out := bytesToInt32(test.in)
if test.out != out {
t.Fatalf("%d != %d", test.out, out)
}
}
}