forked from openshift/installer
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathipnet_test.go
More file actions
97 lines (86 loc) · 2.15 KB
/
ipnet_test.go
File metadata and controls
97 lines (86 loc) · 2.15 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
package ipnet
import (
"encoding/json"
"net"
"testing"
)
func assertJSON(t *testing.T, data interface{}, expected string) {
actualBytes, err := json.Marshal(data)
if err != nil {
t.Fatal(err)
}
actual := string(actualBytes)
if actual != expected {
t.Fatalf("%s != %s", actual, expected)
}
}
func TestMarshal(t *testing.T) {
stdlibIPNet := &net.IPNet{
IP: net.IP{192, 168, 0, 10},
Mask: net.IPv4Mask(255, 255, 255, 0),
}
assertJSON(t, stdlibIPNet, "{\"IP\":\"192.168.0.10\",\"Mask\":\"////AA==\"}")
wrappedIPNet := &IPNet{IPNet: *stdlibIPNet}
assertJSON(t, wrappedIPNet, "\"192.168.0.10/24\"")
assertJSON(t, &IPNet{}, "null")
assertJSON(t, nil, "null")
}
func TestUnmarshal(t *testing.T) {
for _, ipNetIn := range []*IPNet{
nil,
{IPNet: net.IPNet{
IP: net.IP{192, 168, 0, 10},
Mask: net.IPv4Mask(255, 255, 255, 0),
}},
} {
t.Run(ipNetIn.String(), func(t *testing.T) {
data, err := json.Marshal(ipNetIn)
if err != nil {
t.Fatal(err)
}
var ipNetOut *IPNet
err = json.Unmarshal(data, &ipNetOut)
if err != nil {
t.Fatal(err)
}
if ipNetOut.String() != ipNetIn.String() {
t.Fatalf("%v != %v", ipNetOut, ipNetIn)
}
})
}
}
func TestDeepCopy(t *testing.T) {
for _, ipNetIn := range []*IPNet{
{},
{IPNet: net.IPNet{
IP: net.IP{192, 168, 0, 10},
Mask: net.IPv4Mask(255, 255, 255, 0),
}},
} {
t.Run(ipNetIn.String(), func(t *testing.T) {
t.Run("DeepCopyInto", func(t *testing.T) {
ipNetOut := &IPNet{IPNet: net.IPNet{
IP: net.IP{10, 0, 0, 0},
Mask: net.IPv4Mask(255, 0, 0, 0),
}}
ipNetIn.DeepCopyInto(ipNetOut)
if ipNetOut.String() != ipNetIn.String() {
t.Fatalf("%v != %v", ipNetOut, ipNetIn)
}
})
t.Run("DeepCopy", func(t *testing.T) {
ipNetOut := ipNetIn.DeepCopy()
if ipNetOut.String() != ipNetIn.String() {
t.Fatalf("%v != %v", ipNetOut, ipNetIn)
}
ipNetIn.IPNet = net.IPNet{
IP: net.IP{192, 168, 10, 10},
Mask: net.IPv4Mask(255, 255, 255, 255),
}
if ipNetOut.String() == ipNetIn.String() {
t.Fatalf("%v (%q) == %v (%q)", ipNetOut, ipNetOut.String(), ipNetIn, ipNetIn.String())
}
})
})
}
}