@@ -3,6 +3,7 @@ package scw
3
3
import (
4
4
"encoding/json"
5
5
"io/ioutil"
6
+ "net"
6
7
"testing"
7
8
"time"
8
9
@@ -67,7 +68,7 @@ func TestTimeSeries_MarshallJSON(t *testing.T) {
67
68
}
68
69
}
69
70
70
- func TestTimeSeries_UnmarshallJSON (t * testing.T ) {
71
+ func TestTimeSeries_UnmarshalJSON (t * testing.T ) {
71
72
cases := []struct {
72
73
name string
73
74
json string
@@ -166,3 +167,76 @@ func TestFile_UnmarshalJSON(t *testing.T) {
166
167
content : []byte ("\x00 \x00 \x00 \n " ),
167
168
}))
168
169
}
170
+
171
+ func TestIPNet_MarshallJSON (t * testing.T ) {
172
+ cases := []struct {
173
+ name string
174
+ ipRange IPNet
175
+ want string
176
+ err error
177
+ }{
178
+ {
179
+ name : "ip" ,
180
+ ipRange : IPNet {IPNet : net.IPNet {IP : net .IPv4 (42 , 42 , 42 , 42 ), Mask : net .CIDRMask (32 , 32 )}},
181
+ want : `"42.42.42.42/32"` ,
182
+ },
183
+ {
184
+ name : "network" ,
185
+ ipRange : IPNet {IPNet : net.IPNet {IP : net .IPv4 (42 , 42 , 42 , 42 ), Mask : net .CIDRMask (16 , 32 )}},
186
+ want : `"42.42.42.42/16"` ,
187
+ },
188
+ }
189
+
190
+ for _ , c := range cases {
191
+ t .Run (c .name , func (t * testing.T ) {
192
+ got , err := json .Marshal (c .ipRange )
193
+
194
+ testhelpers .Equals (t , c .err , err )
195
+ if c .err == nil {
196
+ testhelpers .Equals (t , c .want , string (got ))
197
+ }
198
+ })
199
+ }
200
+ }
201
+
202
+ func TestIPNet_UnmarshalJSON (t * testing.T ) {
203
+ cases := []struct {
204
+ name string
205
+ json string
206
+ want IPNet
207
+ err string
208
+ }{
209
+ {
210
+ name : "IP with CIDR" ,
211
+ json : `"42.42.42.42/32"` ,
212
+ want : IPNet {IPNet : net.IPNet {IP : net .IPv4 (42 , 42 , 42 , 42 ), Mask : net .CIDRMask (32 , 32 )}},
213
+ },
214
+ {
215
+ name : "IP with network" ,
216
+ json : `"192.0.2.1/24"` ,
217
+ want : IPNet {IPNet : net.IPNet {IP : net .IPv4 (192 , 0 , 2 , 0 ), Mask : net .CIDRMask (24 , 32 )}},
218
+ },
219
+ {
220
+ name : "IP alone" ,
221
+ json : `"42.42.42.42"` ,
222
+ want : IPNet {IPNet : net.IPNet {IP : net .IPv4 (42 , 42 , 42 , 42 ), Mask : net .CIDRMask (32 , 32 )}},
223
+ },
224
+ {
225
+ name : "with timestamp error" ,
226
+ json : `"name"` ,
227
+ err : "scaleway-sdk-go: cannot decode JSON: invalid CIDR address: name" ,
228
+ },
229
+ }
230
+
231
+ for _ , c := range cases {
232
+ t .Run (c .name , func (t * testing.T ) {
233
+ ipNet := & IPNet {}
234
+ err := json .Unmarshal ([]byte (c .json ), ipNet )
235
+ if err != nil {
236
+ testhelpers .Equals (t , c .err , err .Error ())
237
+ }
238
+
239
+ testhelpers .Equals (t , c .want .String (), ipNet .String ())
240
+ })
241
+ }
242
+ }
0 commit comments