forked from dspinhirne/netaddr-go
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIPv4_test.go
More file actions
106 lines (92 loc) · 2.13 KB
/
Copy pathIPv4_test.go
File metadata and controls
106 lines (92 loc) · 2.13 KB
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
package netaddr
import "testing"
import "fmt"
func ExampleParseIPv4() {
ip, _ := ParseIPv4("128.0.0.1")
fmt.Println(ip)
// Output: 128.0.0.1
}
func ExampleNewIPv4() {
ip := NewIPv4(0x80000001)
fmt.Println(ip)
// Output: 128.0.0.1
}
func Test_ParseIPv4(t *testing.T) {
cases := []struct {
given string
addr uint32
err bool
}{
{" 0.0.0.1 ", 1, false},
{"0.0.0.0", 0, false},
{"192.168.1.1", 0xc0a80101, false},
{"128.128.128.128", 0x80808080, false},
{"256.0.0.1", 0, true},
{"a.0.0.1", 0, true},
{"1. 1.1.1", 0, true},
{"1", 0, true},
}
for _, c := range cases {
ip, err := ParseIPv4(c.given)
if err != nil {
if !c.err {
t.Errorf("ParseIPv4(%s) unexpected parse error: %s", c.given, err.Error())
}
continue
}
if c.err {
t.Errorf("ParseIPv4(%s) expected error but none raised", c.given)
continue
}
if ip.addr != c.addr {
t.Errorf("ParseIPv4(%s).addr Expect: %x Result: %x", c.given, c.addr, ip.addr)
}
}
}
func Test_IPv4_Cmp(t *testing.T) {
cases := []struct {
ip1 string
ip2 string
res int
}{
{"1.1.1.0", "1.1.2.0", -1}, // numerically less
{"1.1.1.0", "1.1.0.0", 1}, // numerically greater
{"1.1.1.0", "1.1.1.0", 0}, // eq
}
for _, c := range cases {
ip1, _ := ParseIPv4(c.ip1)
ip2, _ := ParseIPv4(c.ip2)
if res, _ := ip1.Cmp(ip2); res != c.res {
t.Errorf("%s.Cmp(%s) Expect: %d Result: %d", ip1, ip2, c.res, res)
}
}
}
func Test_MulticastMac(t *testing.T) {
cases := []struct {
ip string
mac string
}{
{"223.255.255.255", ""},
{"224.0.0.0", "01-00-5e-00-00-00"},
{"230.2.3.5", "01-00-5e-02-03-05"},
{"235.147.18.23", "01-00-5e-13-12-17"},
{"239.255.255.255", "01-00-5e-7f-ff-ff"},
{"240.0.0.0", ""},
}
for _, c := range cases {
ip, _ := ParseIPv4(c.ip)
mac := ip.MulticastMac()
if mac.String() != c.mac {
t.Errorf("%s.MulticastMac() Expect: %s Result: %s", c.ip, c.mac, mac)
}
}
}
func Test_IPv4_String(t *testing.T) {
cases := []string{"0.0.0.0", "192.168.1.0", "1.2.3.4"}
for _, c := range cases {
ip, _ := ParseIPv4(c)
if ip.String() != c {
t.Errorf("%s.String() Expect: %s Result: %s", c, c, ip.String())
}
}
}