summaryrefslogtreecommitdiff
path: root/lib/container_test.go
blob: d394b5805d0cc4bc9661839bedfffb83ce50aae7 (plain)
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
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
package lib

import (
	"testing"
)

func TestNewContainer(t *testing.T) {
	container := NewContainer()
	if container == nil {
		t.Fatal("NewContainer() returned nil")
	}
	if container.Len() != 0 {
		t.Errorf("NewContainer().Len() = %d, want 0", container.Len())
	}
}

func TestContainer_GetEntry(t *testing.T) {
	container := NewContainer()

	// Get non-existent entry
	_, found := container.GetEntry("notfound")
	if found {
		t.Error("Container.GetEntry() found non-existent entry")
	}

	// Add an entry
	entry := NewEntry("test")
	entry.AddPrefix("192.168.1.0/24")
	container.Add(entry)

	// Get existing entry (case insensitive, trimmed)
	tests := []string{"test", "TEST", "  test  ", "TeSt"}
	for _, name := range tests {
		got, found := container.GetEntry(name)
		if !found {
			t.Errorf("Container.GetEntry(%q) not found, want found", name)
			continue
		}
		if got.GetName() != "TEST" {
			t.Errorf("Container.GetEntry(%q).GetName() = %q, want %q", name, got.GetName(), "TEST")
		}
	}
}

func TestContainer_Len(t *testing.T) {
	container := NewContainer()

	if container.Len() != 0 {
		t.Errorf("Empty container.Len() = %d, want 0", container.Len())
	}

	// Add entries
	entry1 := NewEntry("entry1")
	entry1.AddPrefix("192.168.1.0/24")
	container.Add(entry1)

	if container.Len() != 1 {
		t.Errorf("Container.Len() after 1 add = %d, want 1", container.Len())
	}

	entry2 := NewEntry("entry2")
	entry2.AddPrefix("10.0.0.0/8")
	container.Add(entry2)

	if container.Len() != 2 {
		t.Errorf("Container.Len() after 2 adds = %d, want 2", container.Len())
	}
}

func TestContainer_Loop(t *testing.T) {
	container := NewContainer()

	// Add multiple entries
	entry1 := NewEntry("entry1")
	entry1.AddPrefix("192.168.1.0/24")
	container.Add(entry1)

	entry2 := NewEntry("entry2")
	entry2.AddPrefix("10.0.0.0/8")
	container.Add(entry2)

	entry3 := NewEntry("entry3")
	entry3.AddPrefix("2001:db8::/32")
	container.Add(entry3)

	// Loop through entries
	count := 0
	names := make(map[string]bool)
	for entry := range container.Loop() {
		count++
		names[entry.GetName()] = true
	}

	if count != 3 {
		t.Errorf("Container.Loop() iterated %d times, want 3", count)
	}

	expectedNames := map[string]bool{"ENTRY1": true, "ENTRY2": true, "ENTRY3": true}
	for name := range expectedNames {
		if !names[name] {
			t.Errorf("Container.Loop() missing entry %q", name)
		}
	}
}

func TestContainer_Add_NewEntry(t *testing.T) {
	container := NewContainer()

	entry := NewEntry("test")
	entry.AddPrefix("192.168.1.0/24")

	err := container.Add(entry)
	if err != nil {
		t.Errorf("Container.Add() error = %v, want nil", err)
	}

	if container.Len() != 1 {
		t.Errorf("Container.Len() = %d, want 1", container.Len())
	}

	// Verify entry exists
	got, found := container.GetEntry("test")
	if !found {
		t.Fatal("Container.GetEntry() not found after Add")
	}
	if got.GetName() != "TEST" {
		t.Errorf("Added entry name = %q, want %q", got.GetName(), "TEST")
	}
}

func TestContainer_Add_ExistingEntry(t *testing.T) {
	container := NewContainer()

	// Add first entry
	entry1 := NewEntry("test")
	entry1.AddPrefix("192.168.1.0/24")
	container.Add(entry1)

	// Add second entry with same name
	entry2 := NewEntry("test")
	entry2.AddPrefix("10.0.0.0/8")
	err := container.Add(entry2)
	if err != nil {
		t.Errorf("Container.Add() existing entry error = %v, want nil", err)
	}

	// Should still have only 1 entry
	if container.Len() != 1 {
		t.Errorf("Container.Len() = %d, want 1", container.Len())
	}

	// Verify both prefixes are in the entry
	got, _ := container.GetEntry("test")
	cidrs, err := got.MarshalText()
	if err != nil {
		t.Fatalf("MarshalText() error = %v", err)
	}
	if len(cidrs) != 2 {
		t.Errorf("Entry has %d prefixes, want 2", len(cidrs))
	}
}

func TestContainer_Add_WithIgnoreIPv4(t *testing.T) {
	container := NewContainer()

	// Add entry with both IPv4 and IPv6
	entry := NewEntry("test")
	entry.AddPrefix("192.168.1.0/24")
	entry.AddPrefix("2001:db8::/32")
	container.Add(entry, IgnoreIPv4)

	// Should only have IPv6
	got, _ := container.GetEntry("test")
	_, err := got.GetIPv4Set()
	if err == nil {
		t.Error("Entry should not have IPv4 set when added with IgnoreIPv4")
	}

	_, err = got.GetIPv6Set()
	if err != nil {
		t.Errorf("Entry.GetIPv6Set() error = %v, want nil", err)
	}
}

func TestContainer_Add_WithIgnoreIPv6(t *testing.T) {
	container := NewContainer()

	// Add entry with both IPv4 and IPv6
	entry := NewEntry("test")
	entry.AddPrefix("192.168.1.0/24")
	entry.AddPrefix("2001:db8::/32")
	container.Add(entry, IgnoreIPv6)

	// Should only have IPv4
	got, _ := container.GetEntry("test")
	_, err := got.GetIPv4Set()
	if err != nil {
		t.Errorf("Entry.GetIPv4Set() error = %v, want nil", err)
	}

	_, err = got.GetIPv6Set()
	if err == nil {
		t.Error("Entry should not have IPv6 set when added with IgnoreIPv6")
	}
}

func TestContainer_Add_ExistingWithIgnoreOptions(t *testing.T) {
	container := NewContainer()

	// Add first entry with IPv4
	entry1 := NewEntry("test")
	entry1.AddPrefix("192.168.1.0/24")
	container.Add(entry1)

	// Add second entry with IPv6, ignoring IPv4
	entry2 := NewEntry("test")
	entry2.AddPrefix("2001:db8::/32")
	container.Add(entry2, IgnoreIPv4)

	// Should have only IPv6 now
	got, _ := container.GetEntry("test")
	_, err := got.GetIPv6Set()
	if err != nil {
		t.Errorf("Entry.GetIPv6Set() error = %v, want nil", err)
	}
}

func TestContainer_Remove_NotFound(t *testing.T) {
	container := NewContainer()

	entry := NewEntry("notfound")
	err := container.Remove(entry, CaseRemoveEntry)
	if err == nil {
		t.Error("Container.Remove() on non-existent entry expected error, got nil")
	}
}

func TestContainer_Remove_CaseRemoveEntry(t *testing.T) {
	container := NewContainer()

	// Add entry
	entry := NewEntry("test")
	entry.AddPrefix("192.168.1.0/24")
	container.Add(entry)

	// Remove entire entry
	err := container.Remove(entry, CaseRemoveEntry)
	if err != nil {
		t.Errorf("Container.Remove() error = %v, want nil", err)
	}

	// Should not be found anymore
	_, found := container.GetEntry("test")
	if found {
		t.Error("Entry still found after CaseRemoveEntry")
	}

	if container.Len() != 0 {
		t.Errorf("Container.Len() = %d, want 0 after remove", container.Len())
	}
}

func TestContainer_Remove_CaseRemovePrefix(t *testing.T) {
	container := NewContainer()

	// Add entry with multiple prefixes
	entry := NewEntry("test")
	entry.AddPrefix("192.168.1.0/24")
	entry.AddPrefix("10.0.0.0/8")
	container.Add(entry)

	// Remove one prefix
	removeEntry := NewEntry("test")
	removeEntry.AddPrefix("192.168.1.0/24")
	err := container.Remove(removeEntry, CaseRemovePrefix)
	if err != nil {
		t.Errorf("Container.Remove() error = %v, want nil", err)
	}

	// Entry should still exist
	got, found := container.GetEntry("test")
	if !found {
		t.Fatal("Entry not found after CaseRemovePrefix")
	}

	// Should have only one prefix left
	cidrs, err := got.MarshalText()
	if err != nil {
		t.Fatalf("MarshalText() error = %v", err)
	}
	if len(cidrs) != 1 {
		t.Errorf("Entry has %d prefixes, want 1 after removal", len(cidrs))
	}
}

func TestContainer_Remove_WithIgnoreIPv4(t *testing.T) {
	container := NewContainer()

	// Add entry with both IPv4 and IPv6
	entry := NewEntry("test")
	entry.AddPrefix("192.168.1.0/24")
	entry.AddPrefix("2001:db8::/32")
	container.Add(entry)

	// Remove IPv6 only (ignoring IPv4)
	removeEntry := NewEntry("test")
	removeEntry.AddPrefix("2001:db8::/32")
	err := container.Remove(removeEntry, CaseRemovePrefix, IgnoreIPv4)
	if err != nil {
		t.Errorf("Container.Remove() error = %v, want nil", err)
	}

	// IPv4 should still exist
	got, _ := container.GetEntry("test")
	_, err = got.GetIPv4Set()
	if err != nil {
		t.Errorf("IPv4 set should still exist after removing IPv6 with IgnoreIPv4")
	}
}

func TestContainer_Remove_WithIgnoreIPv6(t *testing.T) {
	container := NewContainer()

	// Add entry with both IPv4 and IPv6
	entry := NewEntry("test")
	entry.AddPrefix("192.168.1.0/24")
	entry.AddPrefix("2001:db8::/32")
	container.Add(entry)

	// Remove IPv4 only (ignoring IPv6)
	removeEntry := NewEntry("test")
	removeEntry.AddPrefix("192.168.1.0/24")
	err := container.Remove(removeEntry, CaseRemovePrefix, IgnoreIPv6)
	if err != nil {
		t.Errorf("Container.Remove() error = %v, want nil", err)
	}

	// IPv6 should still exist
	got, _ := container.GetEntry("test")
	_, err = got.GetIPv6Set()
	if err != nil {
		t.Errorf("IPv6 set should still exist after removing IPv4 with IgnoreIPv6")
	}
}

func TestContainer_Remove_CaseRemoveEntry_WithIgnoreIPv4(t *testing.T) {
	container := NewContainer()

	// Add entry with both IPv4 and IPv6
	entry := NewEntry("test")
	entry.AddPrefix("192.168.1.0/24")
	entry.AddPrefix("2001:db8::/32")
	container.Add(entry)

	// Remove IPv6 only (CaseRemoveEntry with IgnoreIPv4)
	err := container.Remove(entry, CaseRemoveEntry, IgnoreIPv4)
	if err != nil {
		t.Errorf("Container.Remove() error = %v, want nil", err)
	}

	// Entry should still exist but only with IPv4
	got, found := container.GetEntry("test")
	if !found {
		t.Fatal("Entry should still exist")
	}

	_, err = got.GetIPv4Set()
	if err != nil {
		t.Errorf("IPv4 set should still exist")
	}

	_, err = got.GetIPv6Set()
	if err == nil {
		t.Error("IPv6 set should not exist")
	}
}

func TestContainer_Remove_CaseRemoveEntry_WithIgnoreIPv6(t *testing.T) {
	container := NewContainer()

	// Add entry with both IPv4 and IPv6
	entry := NewEntry("test")
	entry.AddPrefix("192.168.1.0/24")
	entry.AddPrefix("2001:db8::/32")
	container.Add(entry)

	// Remove IPv4 only (CaseRemoveEntry with IgnoreIPv6)
	err := container.Remove(entry, CaseRemoveEntry, IgnoreIPv6)
	if err != nil {
		t.Errorf("Container.Remove() error = %v, want nil", err)
	}

	// Entry should still exist but only with IPv6
	got, found := container.GetEntry("test")
	if !found {
		t.Fatal("Entry should still exist")
	}

	_, err = got.GetIPv6Set()
	if err != nil {
		t.Errorf("IPv6 set should still exist")
	}

	_, err = got.GetIPv4Set()
	if err == nil {
		t.Error("IPv4 set should not exist")
	}
}

func TestContainer_Remove_InvalidCase(t *testing.T) {
	container := NewContainer()

	// Add entry
	entry := NewEntry("test")
	entry.AddPrefix("192.168.1.0/24")
	container.Add(entry)

	// Try to remove with invalid case
	err := container.Remove(entry, CaseRemove(999))
	if err == nil {
		t.Error("Container.Remove() with invalid case expected error, got nil")
	}
}

func TestContainer_Lookup_IPv4(t *testing.T) {
	container := NewContainer()

	// Add entries
	entry1 := NewEntry("entry1")
	entry1.AddPrefix("192.168.1.0/24")
	container.Add(entry1)

	entry2 := NewEntry("entry2")
	entry2.AddPrefix("10.0.0.0/8")
	container.Add(entry2)

	// Lookup IPv4 address
	results, found, err := container.Lookup("192.168.1.1")
	if err != nil {
		t.Errorf("Container.Lookup() error = %v, want nil", err)
	}
	if !found {
		t.Error("Container.Lookup() found = false, want true")
	}
	if len(results) != 1 {
		t.Errorf("Container.Lookup() returned %d results, want 1", len(results))
	}
	if len(results) > 0 && results[0] != "ENTRY1" {
		t.Errorf("Container.Lookup() result = %q, want %q", results[0], "ENTRY1")
	}

	// Lookup IPv4 address in second entry
	results, found, err = container.Lookup("10.1.2.3")
	if err != nil {
		t.Errorf("Container.Lookup() error = %v, want nil", err)
	}
	if !found {
		t.Error("Container.Lookup() found = false, want true")
	}
	if len(results) != 1 {
		t.Errorf("Container.Lookup() returned %d results, want 1", len(results))
	}
	if len(results) > 0 && results[0] != "ENTRY2" {
		t.Errorf("Container.Lookup() result = %q, want %q", results[0], "ENTRY2")
	}
}

func TestContainer_Lookup_IPv6(t *testing.T) {
	container := NewContainer()

	// Add entry
	entry := NewEntry("entry1")
	entry.AddPrefix("2001:db8::/32")
	container.Add(entry)

	// Lookup IPv6 address
	results, found, err := container.Lookup("2001:db8::1")
	if err != nil {
		t.Errorf("Container.Lookup() error = %v, want nil", err)
	}
	if !found {
		t.Error("Container.Lookup() found = false, want true")
	}
	if len(results) != 1 {
		t.Errorf("Container.Lookup() returned %d results, want 1", len(results))
	}
	if len(results) > 0 && results[0] != "ENTRY1" {
		t.Errorf("Container.Lookup() result = %q, want %q", results[0], "ENTRY1")
	}
}

func TestContainer_Lookup_CIDR(t *testing.T) {
	container := NewContainer()

	// Add entry
	entry := NewEntry("entry1")
	entry.AddPrefix("192.168.0.0/16")
	container.Add(entry)

	// Lookup CIDR
	results, found, err := container.Lookup("192.168.1.0/24")
	if err != nil {
		t.Errorf("Container.Lookup() error = %v, want nil", err)
	}
	if !found {
		t.Error("Container.Lookup() found = false, want true")
	}
	if len(results) != 1 {
		t.Errorf("Container.Lookup() returned %d results, want 1", len(results))
	}
}

func TestContainer_Lookup_NotFound(t *testing.T) {
	container := NewContainer()

	// Add entry
	entry := NewEntry("entry1")
	entry.AddPrefix("192.168.1.0/24")
	container.Add(entry)

	// Lookup non-matching address
	results, found, err := container.Lookup("10.0.0.1")
	if err != nil {
		t.Errorf("Container.Lookup() error = %v, want nil", err)
	}
	if found {
		t.Error("Container.Lookup() found = true, want false")
	}
	if len(results) != 0 {
		t.Errorf("Container.Lookup() returned %d results, want 0", len(results))
	}
}

func TestContainer_Lookup_WithSearchList(t *testing.T) {
	container := NewContainer()

	// Add multiple entries
	entry1 := NewEntry("entry1")
	entry1.AddPrefix("192.168.1.0/24")
	container.Add(entry1)

	entry2 := NewEntry("entry2")
	entry2.AddPrefix("192.168.1.0/24")
	container.Add(entry2)

	entry3 := NewEntry("entry3")
	entry3.AddPrefix("192.168.1.0/24")
	container.Add(entry3)

	// Lookup with search list
	results, found, err := container.Lookup("192.168.1.1", "entry1", "entry3")
	if err != nil {
		t.Errorf("Container.Lookup() error = %v, want nil", err)
	}
	if !found {
		t.Error("Container.Lookup() found = false, want true")
	}
	if len(results) != 2 {
		t.Errorf("Container.Lookup() returned %d results, want 2", len(results))
	}

	// Verify results contain only searched entries
	resultMap := make(map[string]bool)
	for _, r := range results {
		resultMap[r] = true
	}
	if !resultMap["ENTRY1"] || !resultMap["ENTRY3"] {
		t.Errorf("Container.Lookup() results = %v, want ENTRY1 and ENTRY3", results)
	}
	if resultMap["ENTRY2"] {
		t.Error("Container.Lookup() should not include ENTRY2")
	}
}

func TestContainer_Lookup_InvalidIP(t *testing.T) {
	container := NewContainer()

	// Lookup invalid IP
	_, _, err := container.Lookup("invalid")
	if err == nil {
		t.Error("Container.Lookup() with invalid IP expected error, got nil")
	}
}

func TestContainer_Lookup_InvalidCIDR(t *testing.T) {
	container := NewContainer()

	// Lookup invalid CIDR
	_, _, err := container.Lookup("192.168.1.0/33")
	if err == nil {
		t.Error("Container.Lookup() with invalid CIDR expected error, got nil")
	}
}

func TestContainer_Lookup_SearchListCaseInsensitive(t *testing.T) {
	container := NewContainer()

	// Add entry
	entry := NewEntry("MyEntry")
	entry.AddPrefix("192.168.1.0/24")
	container.Add(entry)

	// Lookup with different case
	results, found, err := container.Lookup("192.168.1.1", "myentry", "MYENTRY", "  MyEntry  ")
	if err != nil {
		t.Errorf("Container.Lookup() error = %v, want nil", err)
	}
	if !found {
		t.Error("Container.Lookup() found = false, want true")
	}
	if len(results) != 1 {
		t.Errorf("Container.Lookup() returned %d results, want 1", len(results))
	}
}

func TestContainer_Lookup_EmptySearchListEntries(t *testing.T) {
	container := NewContainer()

	// Add entry
	entry := NewEntry("entry1")
	entry.AddPrefix("192.168.1.0/24")
	container.Add(entry)

	// Lookup with empty/whitespace search list entries (should be ignored)
	results, found, err := container.Lookup("192.168.1.1", "", "  ", "entry1")
	if err != nil {
		t.Errorf("Container.Lookup() error = %v, want nil", err)
	}
	if !found {
		t.Error("Container.Lookup() found = false, want true")
	}
	if len(results) != 1 {
		t.Errorf("Container.Lookup() returned %d results, want 1", len(results))
	}
}