33
33
// Mock logger for testing
34
34
var testLogger = zap .NewNop ()
35
35
36
- // TestRawIPInRanges tests the rawIPInRanges function.
37
36
func TestRawIPInRanges (t * testing.T ) {
37
+ // Mock predefined CIDRs
38
+ originalIPRanges := data .IPRanges
39
+
40
+ // Restore the original data.IPRanges map after the test
41
+ defer func () {
42
+ data .IPRanges = originalIPRanges
43
+ }()
44
+ data .IPRanges = map [string ][]string {
45
+ "openai" : {
46
+ "203.0.113.0/24" ,
47
+ "2001:db8:1::/48" ,
48
+ },
49
+ }
50
+
38
51
tests := []struct {
39
52
name string
40
53
ip string
@@ -90,9 +103,6 @@ func TestRawIPInRanges(t *testing.T) {
90
103
clientIP := net .ParseIP (tt .ip )
91
104
assert .NotNil (t , clientIP , "Failed to parse IP" )
92
105
93
- // Mock predefined CIDRs
94
- data .IPRanges = predefinedCIDRs
95
-
96
106
result := rawIPInRanges (clientIP , tt .cidrRanges , testLogger )
97
107
assert .Equal (t , tt .expected , result , "Unexpected result for IP %s" , tt .ip )
98
108
})
@@ -126,9 +136,6 @@ func TestIPInRanges(t *testing.T) {
126
136
clientIP := net .ParseIP (tt .ip )
127
137
assert .NotNil (t , clientIP , "Failed to parse IP" )
128
138
129
- // Mock predefined CIDRs
130
- data .IPRanges = predefinedCIDRs
131
-
132
139
// First call (not cached)
133
140
result := IPInRanges (clientIP , tt .cidrRanges , testLogger )
134
141
assert .Equal (t , tt .expected , result , "Unexpected result for IP %s (first call)" , tt .ip )
@@ -148,9 +155,6 @@ func TestIPInRangesCacheExpiration(t *testing.T) {
148
155
clientIP := net .ParseIP ("192.168.1.100" )
149
156
assert .NotNil (t , clientIP , "Failed to parse IP" )
150
157
151
- // Mock predefined CIDRs
152
- data .IPRanges = predefinedCIDRs
153
-
154
158
// First call (not cached)
155
159
result := IPInRanges (clientIP , validCIDRs , testLogger )
156
160
assert .True (t , result , "Expected IP to be in range (first call)" )
@@ -162,3 +166,57 @@ func TestIPInRangesCacheExpiration(t *testing.T) {
162
166
result = IPInRanges (clientIP , validCIDRs , testLogger )
163
167
assert .True (t , result , "Expected IP to be in range (second call, cache expired)" )
164
168
}
169
+
170
+ func TestLocalhostRanges (t * testing.T ) {
171
+ // Mock the data.IPRanges map to include the "localhost" range
172
+ originalIPRanges := data .IPRanges
173
+
174
+ // Restore the original data.IPRanges map after the test
175
+ defer func () {
176
+ data .IPRanges = originalIPRanges
177
+ }()
178
+ data .IPRanges = map [string ][]string {
179
+ "localhost" : {
180
+ "127.0.0.0/8" , // IPv4 localhost range
181
+ "::1/128" , // IPv6 localhost range
182
+ },
183
+ }
184
+
185
+ tests := []struct {
186
+ name string
187
+ ip string
188
+ expected bool
189
+ }{
190
+ {
191
+ name : "IPv4 localhost" ,
192
+ ip : "127.0.0.1" ,
193
+ expected : true ,
194
+ },
195
+ {
196
+ name : "IPv4 non-localhost" ,
197
+ ip : "192.168.1.1" ,
198
+ expected : false ,
199
+ },
200
+ {
201
+ name : "IPv6 localhost" ,
202
+ ip : "::1" ,
203
+ expected : true ,
204
+ },
205
+ {
206
+ name : "IPv6 non-localhost" ,
207
+ ip : "2001:db8::1" ,
208
+ expected : false ,
209
+ },
210
+ }
211
+
212
+ for _ , tt := range tests {
213
+ t .Run (tt .name , func (t * testing.T ) {
214
+ clientIP := net .ParseIP (tt .ip )
215
+ assert .NotNil (t , clientIP , "Failed to parse IP" )
216
+
217
+ // Pass "localhost" as the CIDR range to check
218
+ result := rawIPInRanges (clientIP , []string {"localhost" }, testLogger )
219
+ assert .Equal (t , tt .expected , result , "Unexpected result for IP %s" , tt .ip )
220
+ })
221
+ }
222
+ }
0 commit comments