forked from dspinhirne/netaddr-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Mask32_test.go
154 lines (135 loc) · 3.06 KB
/
Mask32_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
package netaddr
import "testing"
import "fmt"
func ExampleParseMask32() {
m32, _ := ParseMask32("/32")
fmt.Println(m32)
// Output: /32
}
func ExampleNewMask32() {
m32, _ := NewMask32(32)
fmt.Println(m32)
// Output: /32
}
func ExampleMask32_Extended() {
m32, _ := ParseMask32("/24")
fmt.Println(m32.Extended())
// Output: 255.255.255.0
}
func Test_ParseMask32(t *testing.T) {
cases := []struct {
given string
prefixLen uint
mask uint32
err bool
}{
{" 255.0.0.0 ", 8, 0xff000000, false},
{"0.0.0.0", 0, 0, false},
{"255.255.255.255", 32, 0xffffffff, false},
{" 8 ", 8, 0xff000000, false},
{"/32", 32, 0xffffffff, false},
{"//32", 0, 0, true},
{"256.0.0.0", 0, 0, true},
{"255.248.255.0", 0, 0, true},
{"255", 0, 0, true},
}
for _, c := range cases {
m32, err := ParseMask32(c.given)
if err != nil {
if !c.err {
t.Errorf("ParseMask32(%s) unexpected error: %s", c.given, err.Error())
}
continue
}
if c.err {
t.Errorf("ParseMask32(%s) expected error but none raised", c.given)
continue
}
if m32.mask != c.mask {
t.Errorf("ParseMask32(%s) mask. Expect: %08x Result: %08x", c.given, m32.mask, c.mask)
} else if m32.prefixLen != c.prefixLen {
t.Errorf("ParseMask32(%s) Expect: %d Result: %d", c.given, m32.prefixLen, c.prefixLen)
}
}
}
func Test_NewMask32(t *testing.T) {
cases := []struct {
given uint
prefixLen uint
mask uint32
err bool
}{
{8, 8, 0xff000000, false},
{17, 17, 0xffff8000, false},
{0, 0, 0, false},
{32, 32, 0xffffffff, false},
{33, 0, 0, true},
}
for _, c := range cases {
m32, err := NewMask32(c.given)
if err != nil {
if !c.err {
t.Errorf("Unexpected error: %s", err.Error())
}
continue
}
if c.err {
t.Errorf("Expected error when creating Netmask /%d, but none raised", c.given)
continue
}
if m32.mask != c.mask {
t.Errorf("NewMask(%d). Expect: %x Result: %x", c.given, m32.mask, c.mask)
} else if m32.prefixLen != c.prefixLen {
t.Errorf("NewMask(%d). Expect: %d Result: %d", c.given, m32.prefixLen, c.prefixLen)
}
}
}
func Test_Mask32_Extended(t *testing.T) {
cases := []struct {
given uint
extended string
}{
{32, "255.255.255.255"},
{8, "255.0.0.0"},
}
for _, c := range cases {
m32 := initMask32(c.given)
ext := m32.Extended()
if ext != c.extended {
t.Errorf("%d.Extended(). Expect: %s Result: %s", c.given, ext, c.extended)
}
}
}
func Test_Mask32_Cmp(t *testing.T) {
cases := []struct {
m1 uint
m2 uint
res int
}{
{25, 24, -1}, // mask less
{24, 25, 1}, // mask greater
{24, 24, 0}, // eq
}
for _, c := range cases {
m1 := initMask32(c.m1)
m2 := initMask32(c.m2)
if res := m1.Cmp(m2); res != c.res {
t.Errorf("%s.Cmp(%s). Expect: %d Result: %d", m1, m2, res, c.res)
}
}
}
func Test_Mask32_String(t *testing.T) {
cases := []struct {
given uint
expect string
}{
{32, "/32"},
{8, "/8"},
}
for _, c := range cases {
m32 := initMask32(c.given)
if m32.String() != c.expect {
t.Errorf("%d.String(). Expect: %s Result: %s", c.given, m32.String(), c.expect)
}
}
}