-
Notifications
You must be signed in to change notification settings - Fork 0
/
ipv4_test.go
177 lines (142 loc) · 3.82 KB
/
ipv4_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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
package schemer
import (
"bytes"
"net"
"testing"
)
// uses a net.IP directly...
func testIPv41(useJSON bool, t *testing.T) {
var srcIP net.IP = net.IPv4(192, 168, 0, 2)
s, err := SchemaOf(&srcIP)
if err != nil {
t.Error(err)
return
}
writerSchema := s.(*ipv4Schema)
var encodedData bytes.Buffer
err = writerSchema.Encode(&encodedData, srcIP)
if err != nil {
t.Error(err)
return
}
var binarywriterSchema []byte
var readerSchema Schema
if useJSON {
// write out our schema as JSON
binarywriterSchema, err = writerSchema.MarshalJSON()
if err != nil {
t.Error("writerSchema.MarshalJSON() failed", err)
}
// recreate the schema from the JSON
readerSchema, err = DecodeSchemaJSON(bytes.NewReader(binarywriterSchema))
if err != nil {
t.Error("cannot create writerSchema from raw JSON data", err)
return
}
} else {
// write out our schema as binary
binarywriterSchema, err = writerSchema.MarshalSchemer()
if err != nil {
t.Error("writerSchema.MarshalSchemer() failed", err)
}
// recreate the schema from the binary data
readerSchema, err = DecodeSchema(bytes.NewReader(binarywriterSchema))
if err != nil {
t.Error("cannot create writerSchema from raw binary data", err)
return
}
}
var destIP net.IP
r := bytes.NewReader(encodedData.Bytes())
err = readerSchema.Decode(r, &destIP)
if err != nil {
t.Error(err)
return
}
decodeOK := srcIP[0] == destIP[0] && srcIP[1] == destIP[1] && srcIP[2] == destIP[2] && srcIP[3] == destIP[3]
if !decodeOK {
t.Error("unexpected custom data type (IP) decode...")
}
}
// uses a struct
func testIPv42(useJSON bool, t *testing.T) {
type SourceStruct struct {
IntField1 int
IP net.IP
Str string
}
var structToEncode = SourceStruct{IntField1: 42, IP: net.IPv4(192, 168, 0, 1), Str: "test"}
s, err := SchemaOf(&structToEncode)
if err != nil {
t.Error(err)
return
}
writerSchema, ok := s.(*FixedObjectSchema)
if !ok {
t.Error(err)
return
}
var encodedData bytes.Buffer
err = writerSchema.Encode(&encodedData, structToEncode)
if err != nil {
t.Error(err)
return
}
var binarywriterSchema []byte
var readerSchema Schema
if useJSON {
// write out our schema as JSON
binarywriterSchema, err = writerSchema.MarshalJSON()
if err != nil {
t.Error("writerSchema.MarshalJSON() failed", err)
}
// recreate the schema from the JSON
readerSchema, err = DecodeSchemaJSON(bytes.NewReader(binarywriterSchema))
if err != nil {
t.Error("cannot create writerSchema from raw JSON data", err)
return
}
} else {
// write out our schema as binary
binarywriterSchema, err = writerSchema.MarshalSchemer()
if err != nil {
t.Error("writerSchema.MarshalSchemer() failed", err)
}
// recreate the schema from the binary data
readerSchema, err = DecodeSchema(bytes.NewReader(binarywriterSchema))
if err != nil {
t.Error("cannot create writerSchema from raw binary data", err)
return
}
}
type DestinationStruct struct {
IntField1 int
IP net.IP
Str string
}
var structToDecode = DestinationStruct{}
r := bytes.NewReader(encodedData.Bytes())
err = readerSchema.Decode(r, &structToDecode)
if err != nil {
t.Error(err)
return
}
// and now make sure that the structs match!
decodeOK := true
decodeOK = decodeOK && (structToDecode.IntField1 == structToEncode.IntField1)
decodeOK = decodeOK && (structToDecode.Str == structToEncode.Str)
decodeOK = decodeOK &&
structToEncode.IP[0] == structToDecode.IP[0] &&
structToEncode.IP[1] == structToDecode.IP[1] &&
structToEncode.IP[2] == structToDecode.IP[2] &&
structToEncode.IP[3] == structToDecode.IP[3]
if !decodeOK {
t.Error("unexpected struct to struct decode, using custom data type (net.IP)")
}
}
func TestIPv41(t *testing.T) {
testIPv41(true, t)
testIPv41(false, t)
testIPv42(true, t)
testIPv42(false, t)
}