-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathip_cache_test.go
165 lines (129 loc) · 3.17 KB
/
ip_cache_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
package adapters
import (
"net"
"testing"
"time"
)
func TestIPCaching(t *testing.T) {
cache := NewIPCache[string]()
// Store a number of ranges
ranges := []struct {
Range string
Value string
}{
{
Range: "10.0.0.0/24",
Value: "super-local",
},
{
// Goes up to 10.0.63.255
Range: "10.0.0.0/18",
Value: "semi-local",
},
{
Range: "10.0.0.0/8",
Value: "local",
},
}
for _, r := range ranges {
_, network, err := net.ParseCIDR(r.Range)
if err != nil {
t.Fatal(err)
}
cache.Store(network, r.Value, 10*time.Minute)
}
expectations := []struct {
IP string
Value string
}{
{
IP: "10.0.0.1",
Value: "super-local",
},
{
IP: "10.0.20.20",
Value: "semi-local",
},
{
IP: "10.23.54.76",
Value: "local",
},
}
for _, e := range expectations {
ip := net.ParseIP(e.IP)
value, ok := cache.SearchIP(ip)
if !ok {
t.Fatal("Expected to find a value")
}
if value != e.Value {
t.Errorf("Expected to find %v, got %v", e.Value, value)
}
}
// Test for something that should not exist
ip := net.ParseIP("86.4.78.2")
_, ok := cache.SearchIP(ip)
if ok {
t.Error("Expected not to find a value for a public IP")
}
}
func TestIPCachePurge(t *testing.T) {
cache := NewIPCache[string]()
start := time.Now()
// Store a number of ranges
_, a, _ := net.ParseCIDR("10.0.0.0/24")
cache.Store(a, "super-local", 1*time.Second)
_, b, _ := net.ParseCIDR("10.0.0.0/18")
cache.Store(b, "semi-local", 2*time.Second)
_, c, _ := net.ParseCIDR("10.0.0.0/8")
cache.Store(c, "local", 3*time.Second)
// Lookup a local IP, this should be served from the most local cache
// entry
result, found := cache.SearchIP(net.ParseIP("10.0.0.1"))
if !found {
t.Fatal("Expected to find a value")
}
if result != "super-local" {
t.Errorf("Expected to find super-local, got %v", result)
}
// Expire the first (most specific) entry
numExpired := cache.Expire(start.Add(1100 * time.Millisecond))
if numExpired != 1 {
t.Errorf("Expected 1 entry to expire, got %v", numExpired)
}
// Lookup a local IP, this should be served from the next most local cache
// entry
result, found = cache.SearchIP(net.ParseIP("10.0.0.1"))
if !found {
t.Fatal("Expected to find a value")
}
if result != "semi-local" {
t.Errorf("Expected to find semi-local, got %v", result)
}
// Expire the second entry
numExpired = cache.Expire(start.Add(2100 * time.Millisecond))
if numExpired != 1 {
t.Errorf("Expected 1 entry to expire, got %v", numExpired)
}
// Lookup a local IP, this should be served from the local entry
result, found = cache.SearchIP(net.ParseIP("10.0.0.1"))
if !found {
t.Fatal("Expected to find a value")
}
if result != "local" {
t.Errorf("Expected to find local, got %v", result)
}
// Expire the third entry
numExpired = cache.Expire(start.Add(3100 * time.Millisecond))
if numExpired != 1 {
t.Errorf("Expected 1 entry to expire, got %v", numExpired)
}
// Lookup a local IP the cache should now be empty
_, found = cache.SearchIP(net.ParseIP("10.0.0.1"))
if found {
t.Fatal("Expected not to find a value")
}
}
func TestPlayground(t *testing.T) {
ip := net.ParseIP("10.0.0.1/32")
t.Log(ip)
}