-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbitmap_test.go
More file actions
125 lines (103 loc) · 2.77 KB
/
bitmap_test.go
File metadata and controls
125 lines (103 loc) · 2.77 KB
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
package qtunnul
import (
"testing"
)
func TestBitmap_SlidingWindow(t *testing.T) {
b := &Bitmap{}
// 1. Initialize with 250
if !b.Add(250) {
t.Error("Failed to add initial value 250")
}
if b.current != 250 {
t.Errorf("Expected current to be 250, got %d", b.current)
}
// 2. Test Boundary: 186 (250 - 64) -> Should be allowed
if !b.Add(186) {
t.Error("Failed to add 186 (boundary value)")
}
// 3. Test Out of Boundary: 185 (250 - 65) -> Should be rejected
if b.Add(185) {
t.Error("Allowed 185, which should be too old")
}
// 4. Test Duplicate: 186 again -> Should be rejected
if b.Add(186) {
t.Error("Allowed duplicate 186")
}
// 5. Test Duplicate: 250 again -> Should be rejected
if b.Add(250) {
t.Error("Allowed duplicate 250 (current)")
}
// 6. Test Intermediate: 200 -> Should be allowed
if !b.Add(200) {
t.Error("Failed to add 200")
}
// Verify duplicate 200
if b.Add(200) {
t.Error("Allowed duplicate 200")
}
// 7. Advance Window: Add 251
if !b.Add(251) {
t.Error("Failed to add 251")
}
// Now current is 251.
// 186 is now diff = 251 - 186 = 65. Too old.
// 187 is diff = 64. Allowed.
if b.Add(186) {
t.Error("Allowed 186 after window advanced to 251")
}
// 187 was never added, so it should be allowed
if !b.Add(187) {
t.Error("Failed to add 187 (new boundary for 251)")
}
}
func TestBitmap_BigJump(t *testing.T) {
b := &Bitmap{}
b.Add(10)
// Jump to 100 (diff 90 > 64)
// Should clear history
if !b.Add(100) {
t.Error("Failed to add 100")
}
// Old history (10) is gone.
// Check 99 (diff 1) -> Should be allowed (empty slot)
if !b.Add(99) {
t.Error("Failed to add 99")
}
// Check 10 again (diff 90) -> Too old
if b.Add(10) {
t.Error("Allowed 10 which should be cleared")
}
}
func TestBitmap_Wrapping(t *testing.T) {
b := &Bitmap{}
// 1. Start near 0
b.Add(10)
// 2. Receive a packet that is "behind" 0 (wrapping)
// MaxUint64 - 5. Distance from 10 is 16.
// 10 - (Max-5) = 16.
// Should be accepted as "Old" packet.
oldSeq := ^uint64(0) - 5 // MaxUint64 - 5
if !b.Add(oldSeq) {
t.Errorf("Failed to add wrapped old packet %d when current is 10", oldSeq)
}
// 3. Receive a packet that is "ahead" of MaxUint64 (wrapping)
// Set current to MaxUint64 - 5
b = &Bitmap{}
startSeq := ^uint64(0) - 5
b.Add(startSeq)
// Add 5. Distance is: 5 - (Max-5) = 11.
// Should be accepted as "New" packet.
if !b.Add(5) {
t.Errorf("Failed to add wrapped new packet 5 when current is %d", startSeq)
}
if b.current != 5 {
t.Errorf("Expected current to be 5, got %d", b.current)
}
// 4. Verify history after wrap
// Previous current (Max-5) should be in history.
// Distance from 5 to Max-5 is 11.
// Try adding Max-5 again -> should be duplicate
if b.Add(startSeq) {
t.Error("Allowed duplicate wrapped packet")
}
}