-
Notifications
You must be signed in to change notification settings - Fork 10
/
data_test.go
384 lines (300 loc) · 14.2 KB
/
data_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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
package main
import (
"log"
"net"
"net/http"
"testing"
dhcp "github.com/krolaw/dhcp4"
"github.com/stretchr/testify/assert"
)
func newSubnet(dt *DataTracker, name, subnet string) (s *Subnet) {
_, theNet, _ := net.ParseCIDR(subnet)
s = NewSubnet()
s.Name = name
s.Subnet = &MyIPNet{theNet}
s.ActiveStart = dhcp.IPAdd(theNet.IP, 5)
s.ActiveEnd = dhcp.IPAdd(theNet.IP, 25)
return
}
func addNewSubnet(dt *DataTracker, name, subnet string) (s *Subnet, err error, code int) {
s = newSubnet(dt, name, subnet)
err, code = dt.AddSubnet(s)
return
}
func simpleSetup() (dt *DataTracker, s *Subnet) {
store, err := NewFileStore("./database.test.json")
if err != nil {
log.Panic(err)
}
dt = NewDataTracker(store)
s, _, _ = addNewSubnet(dt, "fred", "192.168.128.0/24")
dt.AddSubnet(s)
return
}
func TestAddSubnet(t *testing.T) {
dt, s := simpleSetup()
assert.Equal(t, len(dt.Subnets), 1, "Subnet should be 1")
assert.Equal(t, dt.Subnets["fred"], s, "Subnet should be fred")
}
func TestAddSubnetExisting(t *testing.T) {
dt, _ := simpleSetup()
_, err, code := addNewSubnet(dt, "fred", "192.168.124.0/24")
assert.Equal(t, len(dt.Subnets), 1, "subnets should be len 1")
assert.Equal(t, code, http.StatusConflict, "Return code should be conflict, but is %d", code)
assert.NotNil(t, err, "Error should not be nil")
assert.Equal(t, err.Error(), "Already exists", "Error message should be 'Already exists', but was %s", err.Error())
}
func TestAddSubnetOverlap(t *testing.T) {
dt, _ := simpleSetup()
_, err, code := addNewSubnet(dt, "fred2", "192.168.128.0/24")
assert.Equal(t, len(dt.Subnets), 1, "subnets should be len 1")
assert.Equal(t, code, http.StatusBadRequest, "Return code should be bad status, but is %d", code)
assert.NotNil(t, err, "Error should not be nil")
assert.Equal(t, err.Error(), "Subnet overlaps with existing subnet", "Error message should be 'Subnet overlaps with existing subnet', but was %s", err.Error())
}
func TestRemoveSubnetMissing(t *testing.T) {
dt, _ := simpleSetup()
err, code := dt.RemoveSubnet("missing")
assert.Equal(t, code, http.StatusNotFound, "Code should be not found, but is %d", code)
assert.NotNil(t, err, "Error should not be nil")
assert.Equal(t, err.Error(), "Not Found", "Error message should be 'Not Found', but was %s", err.Error())
}
func TestRemoveSubnet(t *testing.T) {
dt, s := simpleSetup()
err, code := dt.RemoveSubnet(s.Name)
assert.Nil(t, err, "Error should be nil")
assert.Equal(t, code, http.StatusOK, "Code should be 200, but is: %d", code)
assert.Equal(t, len(dt.Subnets), 0, "Subnets length should be 0, but is %d", len(dt.Subnets))
}
func TestReplaceSubnetNotFound(t *testing.T) {
dt, _ := simpleSetup()
ns := newSubnet(dt, "fred", "192.168.124.0/24")
err, code := dt.ReplaceSubnet("fred2", ns)
assert.Equal(t, code, http.StatusNotFound, "Return code should be not found, but is %d", code)
assert.NotNil(t, err, "Error should not be nil")
assert.Equal(t, err.Error(), "Not Found", "Error message should be 'Not Found', but was %s", err.Error())
}
func TestReplaceSubnetReplace(t *testing.T) {
dt, _ := simpleSetup()
ns := newSubnet(dt, "fred", "192.168.124.0/24")
err, code := dt.ReplaceSubnet("fred", ns)
assert.Equal(t, code, http.StatusOK, "Return code should be ok, but is %d", code)
assert.Nil(t, err, "Error should be nil")
assert.Equal(t, ns, dt.Subnets["fred"], "Replaced new subnet should the new one")
assert.Equal(t, dt.Subnets["fred"].Subnet.String(), "192.168.124.0/24", "Subnet should be 192.168.124.0/24, but is %s", dt.Subnets["fred"].Subnet.String())
}
func TestReplaceSubnetRename(t *testing.T) {
dt, _ := simpleSetup()
ns := newSubnet(dt, "fred2", "192.168.128.0/24")
err, code := dt.ReplaceSubnet("fred", ns)
assert.Equal(t, code, http.StatusOK, "Return code should be ok, but is %d", code)
assert.Nil(t, err, "Error should be nil")
assert.Nil(t, dt.Subnets["fred"], "Old slot should be nil")
assert.Equal(t, dt.Subnets["fred2"], ns, "Replaced new subnet should the new one")
assert.Equal(t, dt.Subnets["fred2"].Subnet.String(), "192.168.128.0/24", "Subnet should be 192.168.128.0/24, but is %s", dt.Subnets["fred2"].Subnet.String())
}
func TestReplaceSubnetPreserveInfo(t *testing.T) {
dt, s := simpleSetup()
s.Leases["greg"] = &Lease{
Mac: "macit",
}
s.Bindings["greg"] = &Binding{
Mac: "macit",
}
ns := newSubnet(dt, "fred2", "192.168.128.0/24")
err, code := dt.ReplaceSubnet("fred", ns)
assert.Equal(t, code, http.StatusOK, "Return code should be ok, but is %d", code)
assert.Nil(t, err, "Error should be nil")
assert.Nil(t, dt.Subnets["fred"], "Old slot should be nil")
assert.Equal(t, dt.Subnets["fred2"], ns, "Replaced new subnet should the new one")
assert.Equal(t, dt.Subnets["fred2"].Subnet.String(), "192.168.128.0/24", "Subnet should be 192.168.128.0/24, but is %s", dt.Subnets["fred2"].Subnet.String())
assert.NotNil(t, dt.Subnets["fred2"].Leases["greg"], "Leases['greg'] should not be nil")
assert.NotNil(t, dt.Subnets["fred2"].Bindings["greg"], "Bindings['greg'] should not be nil")
assert.Equal(t, dt.Subnets["fred2"].Leases["greg"].Mac, "macit", "Leases['greg'].Mac should be 'macit', but is %s", dt.Subnets["fred2"].Leases["greg"].Mac)
assert.Equal(t, dt.Subnets["fred2"].Bindings["greg"].Mac, "macit", "Bindings['greg'].Mac should be 'macit', but is %s", dt.Subnets["fred2"].Bindings["greg"].Mac)
}
func TestReplaceSubnetMustNotOverlap(t *testing.T) {
dt, _ := simpleSetup()
os, _, _ := addNewSubnet(dt, "fred2", "192.168.124.0/24")
ns := newSubnet(dt, "fred2", "192.168.128.0/24")
err, code := dt.ReplaceSubnet("fred2", ns)
assert.Equal(t, code, http.StatusBadRequest, "Return code should be bad request, but is %d", code)
assert.NotNil(t, err, "Error should be not nil")
assert.Equal(t, err.Error(), "Subnet overlaps with existing subnet", "Error should be 'Subnet overlaps with existing subnet', but is %s", err.Error())
assert.NotNil(t, dt.Subnets["fred2"], "fred2 slot should not be nil")
assert.Equal(t, dt.Subnets["fred2"], os, "Should not replace the old subnet")
}
func TestFindSubnetEmpty(t *testing.T) {
store, err := NewFileStore("./database.test.json")
if err != nil {
log.Panic(err)
}
dt := NewDataTracker(store)
s := dt.FindSubnet(net.ParseIP("0.0.0.0"))
assert.Nil(t, s, "Expected nil subnets")
}
func TestFindSubnetWithSubnetNotMatched(t *testing.T) {
dt, _ := simpleSetup()
s := dt.FindSubnet(net.ParseIP("0.0.0.0"))
assert.Nil(t, s, "Expected nil subnets")
}
func TestFindSubnetWithSubnetMatches(t *testing.T) {
dt, s := simpleSetup()
ns := dt.FindSubnet(net.ParseIP("192.168.128.0"))
assert.Equal(t, s, ns, "Expected to find subnet")
ns = dt.FindSubnet(net.ParseIP("192.168.128.255"))
assert.Equal(t, s, ns, "Expected to find subnet")
ns = dt.FindSubnet(net.ParseIP("192.168.128.124"))
assert.Equal(t, s, ns, "Expected to find subnet")
}
func TestMarshallingMyIPNet(t *testing.T) {
_, netdata, _ := net.ParseCIDR("192.168.124.0/24")
myipnet := &MyIPNet{netdata}
b, err := myipnet.MarshalText()
assert.Equal(t, string(b[:len(b)]), "192.168.124.0/24", "Should be 192.168.124.0/24, but is %s", string(b[:len(b)]))
assert.Nil(t, err, "Err should be nil")
myunipnet := &MyIPNet{}
err = myunipnet.UnmarshalText(b)
assert.Equal(t, myunipnet.String(), "192.168.124.0/24", "Should be 192.168.124.0/24, but is %s", myunipnet.String())
myunipnet2 := &MyIPNet{}
b = make([]byte, 0)
err = myunipnet2.UnmarshalText(b)
assert.NotNil(t, err, "Err should be nil")
assert.Equal(t, err.Error(), "Empty MyIPNet", "Error message should be 'Empty MyIPNet', but was %s", err.Error())
b = []byte("fredrocks")
err = myunipnet.UnmarshalText(b)
assert.NotNil(t, err, "Error should not be nil")
assert.Equal(t, err.Error(), "invalid NetIP address: fredrocks", "Error message should be 'invalid NetIP address: fredrocks', but was %s", err.Error())
}
// GREG: Test load/save data
func TestSubnetOverlapInside(t *testing.T) {
dt, _ := simpleSetup()
_, err, code := addNewSubnet(dt, "fred2", "192.168.128.128/25")
assert.Equal(t, code, http.StatusBadRequest, "Return code should be bad status, but is %d", code)
assert.NotNil(t, err, "Error should not be nil")
assert.Equal(t, err.Error(), "Subnet overlaps with existing subnet", "Error message should be 'Subnet overlaps with existing subnet', but was %s", err.Error())
}
func TestSubnetOverlapOutside(t *testing.T) {
dt, _ := simpleSetup()
_, err, code := addNewSubnet(dt, "fred2", "192.168.0.0/16")
assert.Equal(t, code, http.StatusBadRequest, "Return code should be bad status, but is %d", code)
assert.NotNil(t, err, "Error should not be nil")
assert.Equal(t, err.Error(), "Subnet overlaps with existing subnet", "Error message should be 'Subnet overlaps with existing subnet', but was %s", err.Error())
}
func TestAddBindingMissing(t *testing.T) {
dt, s := simpleSetup()
b := NewBinding()
b.Mac = "macit"
b.Ip = net.ParseIP("192.168.128.10")
err, code := dt.AddBinding("fred2", *b)
assert.Equal(t, code, http.StatusNotFound, "Return code should be not found, but is %d", code)
assert.NotNil(t, err, "Error should not be nil")
assert.Equal(t, err.Error(), "Not Found", "Error message should be 'Not Found', but was %s", err.Error())
assert.Equal(t, len(s.Bindings), 0, "There should not be any bindings")
}
func TestAddBinding(t *testing.T) {
dt, s := simpleSetup()
assert.Equal(t, s.ActiveBits.Any(), false, "No bits should be set")
b := NewBinding()
b.Mac = "macit"
b.Ip = net.ParseIP("192.168.128.10")
err, code := dt.AddBinding("fred", *b)
assert.Equal(t, code, http.StatusOK, "Return code should be ok, but is %d", code)
assert.Nil(t, err, "Error should be nil")
assert.Equal(t, len(s.Bindings), 1, "There should be one binding")
assert.NotNil(t, s.Bindings["macit"], "Binding should be not nil")
assert.Equal(t, s.Bindings["macit"], b, "Binding should be the same")
assert.Equal(t, s.Bindings["macit"].Ip.String(), "192.168.128.10", "Binding ip should be 192.168.128.10, but is %s", s.Bindings["macit"].Ip.String())
assert.Equal(t, s.ActiveBits.Count(), uint(1), "bit count should be 1, but is %d", s.ActiveBits.Count())
assert.Equal(t, s.ActiveBits.Test(5), true, "bit 5 should be set")
}
func TestAddBindingReplace(t *testing.T) {
dt, s := simpleSetup()
assert.Equal(t, s.ActiveBits.Any(), false, "No bits should be set")
b := NewBinding()
b.Mac = "macit"
b.Ip = net.ParseIP("192.168.128.10")
dt.AddBinding("fred", *b)
assert.Equal(t, s.ActiveBits.Test(5), true, "bit 5 should be set")
b2 := NewBinding()
b2.Mac = "macit"
b2.Ip = net.ParseIP("192.168.128.16")
err, code := dt.AddBinding("fred", *b2)
assert.Equal(t, code, http.StatusOK, "Return code should be ok, but is %d", code)
assert.Nil(t, err, "Error should be nil")
assert.Equal(t, len(s.Bindings), 1, "There should be one binding")
assert.NotNil(t, s.Bindings["macit"], "Binding should be not nil")
assert.Equal(t, s.Bindings["macit"], b2, "Binding should be the same")
assert.Equal(t, s.Bindings["macit"].Ip.String(), "192.168.128.16", "Binding ip should be 192.168.128.16, but is %s", s.Bindings["macit"].Ip.String())
assert.Equal(t, s.ActiveBits.Count(), uint(1), "bit count should be 1, but is %d", s.ActiveBits.Count())
assert.Equal(t, s.ActiveBits.Test(5), false, "bit 5 should be false")
assert.Equal(t, s.ActiveBits.Test(11), true, "bit 11 should be true")
}
func TestDeleteBindingMissingSubnet(t *testing.T) {
dt, s := simpleSetup()
b := NewBinding()
b.Mac = "macit"
b.Ip = net.ParseIP("192.168.128.10")
dt.AddBinding("fred", *b)
err, code := dt.DeleteBinding("fred2", "macit")
assert.Equal(t, code, http.StatusNotFound, "Return code should be not found, but is %d", code)
assert.NotNil(t, err, "Error should not be nil")
assert.Equal(t, err.Error(), "Subnet Not Found", "Error message should be 'Subnet Not Found', but was %s", err.Error())
assert.Equal(t, len(s.Bindings), 1, "There should be one binding")
assert.Equal(t, s.ActiveBits.Count(), uint(1), "bit count should be 1, but is %d", s.ActiveBits.Count())
assert.Equal(t, s.ActiveBits.Test(5), true, "bit 5 should be true")
}
func TestDeleteBindingMissingBinding(t *testing.T) {
dt, s := simpleSetup()
b := NewBinding()
b.Mac = "macit"
b.Ip = net.ParseIP("192.168.128.10")
dt.AddBinding("fred", *b)
err, code := dt.DeleteBinding("fred", "missing")
assert.Equal(t, code, http.StatusNotFound, "Return code should be not found, but is %d", code)
assert.NotNil(t, err, "Error should not be nil")
assert.Equal(t, err.Error(), "Binding Not Found", "Error message should be 'Binding Not Found', but was %s", err.Error())
assert.Equal(t, len(s.Bindings), 1, "There should one binding")
assert.Equal(t, s.ActiveBits.Count(), uint(1), "bit count should be 1, but is %d", s.ActiveBits.Count())
assert.Equal(t, s.ActiveBits.Test(5), true, "bit 5 should be true")
}
func TestDeleteBinding(t *testing.T) {
dt, s := simpleSetup()
b := NewBinding()
b.Mac = "macit"
b.Ip = net.ParseIP("192.168.128.10")
dt.AddBinding("fred", *b)
err, code := dt.DeleteBinding("fred", "macit")
assert.Equal(t, code, http.StatusOK, "Return code should be ok, but is %d", code)
assert.Nil(t, err, "Error should be nil")
assert.Equal(t, len(s.Bindings), 0, "There should not be any bindings")
assert.Equal(t, s.ActiveBits.Count(), uint(0), "bit count should be 0, but is %d", s.ActiveBits.Count())
assert.Equal(t, s.ActiveBits.Test(5), false, "bit 5 should be false")
}
func TestSetNextServerMissing(t *testing.T) {
dt, _ := simpleSetup()
ip := net.ParseIP("192.168.128.10")
nextServer := NextServer{
Server: "1.1.1.1",
}
err, code := dt.SetNextServer("fred2", ip, nextServer)
assert.Equal(t, code, http.StatusNotFound, "Return code should be not found, but is %d", code)
assert.NotNil(t, err, "Error should not be nil")
assert.Equal(t, err.Error(), "Not Found", "Error message should be 'Not Found', but was %s", err.Error())
}
func TestSetNextServer(t *testing.T) {
dt, s := simpleSetup()
b := NewBinding()
b.Mac = "macit"
b.Ip = net.ParseIP("192.168.128.10")
dt.AddBinding("fred", *b)
ip := net.ParseIP("192.168.128.10")
nextServer := NextServer{
Server: "1.1.1.1",
}
err, code := dt.SetNextServer("fred", ip, nextServer)
b = s.Bindings["macit"]
assert.Equal(t, code, http.StatusOK, "Return code should be ok, but is %d", code)
assert.Nil(t, err, "Error should be nil")
assert.Equal(t, *b.NextServer, "1.1.1.1", "Next server should be 1.1.1.1, but is %s", b.NextServer)
}