This repository has been archived by the owner on Oct 20, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathOptionsHelper_test.go
215 lines (176 loc) · 6.34 KB
/
OptionsHelper_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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
// Simple tests for reflect helpers
package reflectDHCP
import (
"github.com/krolaw/dhcp4"
"testing"
"encoding/json"
"bytes"
"reflect"
)
func TestJSONUnmarshal_optionsAll_byte_SubnetMask(t *testing.T){
var opt optionsAll_byte
optT, strO, byteV := &opt.SubnetMask, []byte(`{"SubnetMask":"255.255.255.0"}`), []byte{255,255,255,0}
if err := json.Unmarshal(strO, &opt); err != nil {
t.Fatal("Error in Unmarshal: ", err)
}
if !bytes.Equal(*optT, byteV) {
t.Fatal(*optT, " != ", byteV)
}
}
func TestJSONUnmarshal_optionsAll_byte_TimeOffset(t *testing.T){
var opt optionsAll_byte
optT, strO, byteV := &opt.TimeOffset, []byte(`{"TimeOffset":-124}`), []byte{255,255,255,132}
if err := json.Unmarshal(strO, &opt); err != nil {
t.Fatal("Error in Unmarshal: ", err)
}
if !bytes.Equal(*optT, byteV) {
t.Fatal(*optT, " != ", byteV)
}
}
func TestJSONUnmarshal_optionsAll_byte_Router(t *testing.T){
var opt optionsAll_byte
optT, strO, byteV := &opt.Router, []byte(`{"Router":["1.2.3.4","10.20.30.40"]}`), []byte{1,2,3,4,10,20,30,40}
if err := json.Unmarshal(strO, &opt); err != nil {
t.Fatal("Error in Unmarshal: ", err)
}
if !bytes.Equal(*optT, byteV) {
t.Fatal(*optT, " != ", byteV)
}
}
func TestJSONUnmarshal_optionsAll_byte_HostName(t *testing.T){
var opt optionsAll_byte
optT, strO, byteV := &opt.HostName, []byte(`{"HostName":"localhost"}`), []byte{'l','o','c','a','l','h','o','s','t'}
if err := json.Unmarshal(strO, &opt); err != nil {
t.Fatal("Error in Unmarshal: ", err)
}
if !bytes.Equal(*optT, byteV) {
t.Fatal(*optT, " != ", byteV)
}
}
func TestJSONUnmarshal_optionsAll_byte_BootFileSize(t *testing.T){
var opt optionsAll_byte
optT, strO, byteV := &opt.BootFileSize, []byte(`{"BootFileSize":124}`), []byte{0,124}
if err := json.Unmarshal(strO, &opt); err != nil {
t.Fatal("Error in Unmarshal: ", err)
}
if !bytes.Equal(*optT, byteV) {
t.Fatal(*optT, " != ", byteV)
}
}
func TestJSONUnmarshal_optionsAll_byte_IPForwardingEnableDisable(t *testing.T){
var opt optionsAll_byte
optT, strO, byteV := &opt.IPForwardingEnableDisable, []byte(`{"IPForwardingEnableDisable":true}`), []byte{1}
if err := json.Unmarshal(strO, &opt); err != nil {
t.Fatal("Error in Unmarshal: ", err)
}
if !bytes.Equal(*optT, byteV) {
t.Fatal(*optT, " != ", byteV)
}
}
func TestJSONUnmarshal_optionsAll_byte_PolicyFilter(t *testing.T){
var opt optionsAll_byte
optT, strO, byteV := &opt.PolicyFilter, []byte(`{"PolicyFilter":["1.2.3.4 255.255.255.0", "10.20.30.40 255.255.0.0"]}`),
[]byte{1,2,3,4,255,255,255,0,10,20,30,40,255,255,0,0}
if err := json.Unmarshal(strO, &opt); err != nil {
t.Fatal("Error in Unmarshal: ", err)
}
if !bytes.Equal(*optT, byteV) {
t.Fatal(*optT, " != ", byteV)
}
}
func TestJSONUnmarshal_optionsAll_byte_DefaultIPTimeToLive(t *testing.T){
var opt optionsAll_byte
optT, strO, byteV := &opt.DefaultIPTimeToLive, []byte(`{"DefaultIPTimeToLive":124}`), []byte{124}
if err := json.Unmarshal(strO, &opt); err != nil {
t.Fatal("Error in Unmarshal: ", err)
}
if !bytes.Equal(*optT, byteV) {
t.Fatal(*optT, " != ", byteV)
}
}
func TestJSONUnmarshal_optionsAll_byte_PathMTUAgingTimeout(t *testing.T){
var opt optionsAll_byte
optT, strO, byteV := &opt.PathMTUAgingTimeout, []byte(`{"PathMTUAgingTimeout":124}`), []byte{0,0,0,124}
if err := json.Unmarshal(strO, &opt); err != nil {
t.Fatal("Error in Unmarshal: ", err)
}
if !bytes.Equal(*optT, byteV) {
t.Fatal(*optT, " != ", byteV)
}
}
func TestJSONUnmarshal_optionsAll_byte_PathMTUPlateauTable(t *testing.T){
var opt optionsAll_byte
optT, strO, byteV := &opt.PathMTUPlateauTable, []byte(`{"PathMTUPlateauTable":[1,2,4]}`), []byte{0,1,0,2,0,4}
if err := json.Unmarshal(strO, &opt); err != nil {
t.Fatal("Error in Unmarshal: ", err)
}
if !bytes.Equal(*optT, byteV) {
t.Fatal(*optT, " != ", byteV)
}
}
func TestJSONUnmarshal_Options(t *testing.T){
var strO = []byte(`{
"SubnetMask": "255.255.255.0",
"TimeOffset": -124,
"Router": ["1.2.3.4","10.20.30.40"],
"HostName": "localhost",
"BootFileSize": 124,
"IPForwardingEnableDisable": true,
"PolicyFilter": ["1.2.3.4 255.255.255.0", "10.20.30.40 255.255.0.0"],
"DefaultIPTimeToLive": 124,
"PathMTUAgingTimeout": 124,
"PathMTUPlateauTable": [1,2,4]
}`)
var mapV = Options{
dhcp4.OptionSubnetMask: []byte{255,255,255,0},
dhcp4.OptionTimeOffset: []byte{255,255,255,132},
dhcp4.OptionRouter: []byte{1,2,3,4,10,20,30,40},
dhcp4.OptionHostName: []byte{'l','o','c','a','l','h','o','s','t'},
dhcp4.OptionBootFileSize: []byte{0,124},
dhcp4.OptionIPForwardingEnableDisable: []byte{1},
dhcp4.OptionPolicyFilter: []byte{1,2,3,4,255,255,255,0,10,20,30,40,255,255,0,0},
dhcp4.OptionDefaultIPTimeToLive: []byte{124},
dhcp4.OptionPathMTUAgingTimeout: []byte{0,0,0,124},
dhcp4.OptionPathMTUPlateauTable: []byte{0,1,0,2,0,4},
}
var opt = Options{}
if err := json.Unmarshal(strO, &opt); err != nil {
t.Fatal("Error in Unmarshal: ", err)
}
if !reflect.DeepEqual(opt, mapV) {
t.Fatal(opt, " != ", mapV)
}
}
func TestJSONUnmarshal_OptionsCast(t *testing.T){
var strO = []byte(`{
"SubnetMask": "255.255.255.0",
"TimeOffset": -124,
"Router": ["1.2.3.4","10.20.30.40"],
"HostName": "localhost",
"BootFileSize": 124,
"IPForwardingEnableDisable": true,
"PolicyFilter": ["1.2.3.4 255.255.255.0", "10.20.30.40 255.255.0.0"],
"DefaultIPTimeToLive": 124,
"PathMTUAgingTimeout": 124,
"PathMTUPlateauTable": [1,2,4]
}`)
var mapV = dhcp4.Options{
dhcp4.OptionSubnetMask: []byte{255,255,255,0},
dhcp4.OptionTimeOffset: []byte{255,255,255,132},
dhcp4.OptionRouter: []byte{1,2,3,4,10,20,30,40},
dhcp4.OptionHostName: []byte{'l','o','c','a','l','h','o','s','t'},
dhcp4.OptionBootFileSize: []byte{0,124},
dhcp4.OptionIPForwardingEnableDisable: []byte{1},
dhcp4.OptionPolicyFilter: []byte{1,2,3,4,255,255,255,0,10,20,30,40,255,255,0,0},
dhcp4.OptionDefaultIPTimeToLive: []byte{124},
dhcp4.OptionPathMTUAgingTimeout: []byte{0,0,0,124},
dhcp4.OptionPathMTUPlateauTable: []byte{0,1,0,2,0,4},
}
var opt = dhcp4.Options{}
if err := json.Unmarshal(strO, (*Options)(&opt)); err != nil {
t.Fatal("Error in Unmarshal: ", err)
}
if !reflect.DeepEqual(opt, mapV) {
t.Fatal(opt, " != ", mapV)
}
}