1
1
package thread_cache ;
2
2
3
- import dev .aikido .agent_api .background .ServiceConfiguration ;
4
3
import dev .aikido .agent_api .background .cloud .api .ReportingApi ;
5
4
import dev .aikido .agent_api .thread_cache .ThreadCacheObject ;
6
5
import org .junit .jupiter .api .Test ;
7
- import utils .EmtpyThreadCacheObject ;
8
6
9
7
import java .util .List ;
10
8
import java .util .Optional ;
@@ -24,7 +22,7 @@ public void update() {
24
22
"fd00:3234:5678:9abc::1/64" ,
25
23
"5.6.7.8/32"
26
24
))
27
- ), "Test|One" )));
25
+ ), null , "Test|One" )));
28
26
29
27
assertEquals (new ThreadCacheObject .BlockedResult (true , "description" ), tCache .isIpBlocked ("1.2.3.4" ));
30
28
assertEquals (new ThreadCacheObject .BlockedResult (false , null ), tCache .isIpBlocked ("2.3.4.5" ));
@@ -59,9 +57,9 @@ public void updateEmpty() {
59
57
"fd00:3234:5678:9abc::1/64" ,
60
58
"5.6.7.8/32"
61
59
))
62
- ), "Test|One" )));
60
+ ), null , "Test|One" )));
63
61
64
- tCache .updateBlockedLists (Optional .of (new ReportingApi .APIListsResponse (null , null )));
62
+ tCache .updateBlockedLists (Optional .of (new ReportingApi .APIListsResponse (null , null , null )));
65
63
66
64
assertEquals (new ThreadCacheObject .BlockedResult (true , "description" ), tCache .isIpBlocked ("1.2.3.4" ));
67
65
assertEquals (new ThreadCacheObject .BlockedResult (false , null ), tCache .isIpBlocked ("2.3.4.5" ));
@@ -96,16 +94,16 @@ public void updateRegexes() {
96
94
"fd00:3234:5678:9abc::1/64" ,
97
95
"5.6.7.8/32"
98
96
))
99
- ), "Test|One" )));
97
+ ), null , "Test|One" )));
100
98
101
- tCache .updateBlockedLists (Optional .of (new ReportingApi .APIListsResponse (null , "" )));
99
+ tCache .updateBlockedLists (Optional .of (new ReportingApi .APIListsResponse (null , null , "" )));
102
100
103
101
assertTrue (tCache .isBlockedUserAgent ("This is my TEST user agent" ));
104
102
assertTrue (tCache .isBlockedUserAgent ("Test" ));
105
103
assertTrue (tCache .isBlockedUserAgent ("TEst and ONE" ));
106
104
assertFalse (tCache .isBlockedUserAgent ("Est|On" ));
107
105
assertFalse (tCache .isBlockedUserAgent ("Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36" ));
108
- tCache .updateBlockedLists (Optional .of (new ReportingApi .APIListsResponse (null , "Mozilla" )));
106
+ tCache .updateBlockedLists (Optional .of (new ReportingApi .APIListsResponse (null , null , "Mozilla" )));
109
107
110
108
assertFalse (tCache .isBlockedUserAgent ("This is my TEST user agent" ));
111
109
assertFalse (tCache .isBlockedUserAgent ("Test" ));
@@ -155,4 +153,93 @@ public void testThreadCacheBypassedIPsSubnet() {
155
153
assertFalse (tCache .isBypassedIP ("10.0.1.1" ));
156
154
assertFalse (tCache .isBypassedIP ("1.2.3.4" ));
157
155
}
156
+
157
+ @ Test
158
+ public void testIsIpBlockedWithAllowedAndBlockedIPs () {
159
+ // Create a ThreadCacheObject with both allowed and blocked IPs
160
+ ThreadCacheObject tCache = new ThreadCacheObject (null , null , null , null , Optional .of (new ReportingApi .APIListsResponse (List .of (
161
+ new ReportingApi .ListsResponseEntry ("geoip" , "description" , List .of (
162
+ "1.2.3.4" , // Blocked IP
163
+ "192.168.1.1" // Blocked IP
164
+ ))
165
+ ), List .of (
166
+ new ReportingApi .ListsResponseEntry ("geoip" , "description" , List .of (
167
+ "10.0.0.1" , // Allowed IP
168
+ "1.2.3.4"
169
+ ))
170
+ ), "Test|One" )));
171
+
172
+ // Test blocked IPs
173
+ assertEquals (new ThreadCacheObject .BlockedResult (true , "description" ), tCache .isIpBlocked ("1.2.3.4" ));
174
+ assertEquals (new ThreadCacheObject .BlockedResult (true , "description" ), tCache .isIpBlocked ("192.168.1.1" ));
175
+
176
+ // Test allowed IPs
177
+ /// Private IP :
178
+ assertEquals (new ThreadCacheObject .BlockedResult (false , null ), tCache .isIpBlocked ("10.0.0.2" ));
179
+ /// Not in allowlist
180
+ assertEquals (new ThreadCacheObject .BlockedResult (true , "allowlist" ), tCache .isIpBlocked ("1.2.3.3" ));
181
+ }
182
+
183
+ @ Test
184
+ public void testIsIpBlockedWithOnlyAllowedIPs () {
185
+ // Create a ThreadCacheObject with only allowed IPs
186
+ ThreadCacheObject tCache = new ThreadCacheObject (null , null , null , null , Optional .of (new ReportingApi .APIListsResponse (null , List .of (
187
+ new ReportingApi .ListsResponseEntry ("geoip" , "description" , List .of (
188
+ "10.0.0.1" // Allowed IP
189
+ ))
190
+ ), "Test|One" )));
191
+
192
+ // Test allowed IP
193
+ assertEquals (new ThreadCacheObject .BlockedResult (false , null ), tCache .isIpBlocked ("10.0.0.1" ));
194
+ // Test a non-allowed private-IP
195
+ assertEquals (new ThreadCacheObject .BlockedResult (false , null ), tCache .isIpBlocked ("10.0.0.2" ));
196
+ // Test a non-allowed IP
197
+ assertEquals (new ThreadCacheObject .BlockedResult (true , "allowlist" ), tCache .isIpBlocked ("1.2.3.4" ));
198
+ }
199
+
200
+ @ Test
201
+ public void testIsIpBlockedWithOnlyBlockedIPs () {
202
+ // Create a ThreadCacheObject with only blocked IPs
203
+ ThreadCacheObject tCache = new ThreadCacheObject (null , null , null , null , Optional .of (new ReportingApi .APIListsResponse (List .of (
204
+ new ReportingApi .ListsResponseEntry ("geoip" , "description" , List .of (
205
+ "1.2.3.4" , // Blocked IP
206
+ "192.168.1.1" // Blocked IP
207
+ ))
208
+ ), null , "Test|One" )));
209
+
210
+ // Test blocked IPs
211
+ assertEquals (new ThreadCacheObject .BlockedResult (true , "description" ), tCache .isIpBlocked ("1.2.3.4" ));
212
+ assertEquals (new ThreadCacheObject .BlockedResult (true , "description" ), tCache .isIpBlocked ("192.168.1.1" ));
213
+ // Test a non-blocked IP
214
+ assertEquals (new ThreadCacheObject .BlockedResult (false , null ), tCache .isIpBlocked ("10.0.0.1" ));
215
+ }
216
+
217
+ @ Test
218
+ public void testIsIpBlockedWithAllowedIPsAndBlockedIPs () {
219
+ // Create a ThreadCacheObject with multiple allowed and blocked IPs
220
+ ThreadCacheObject tCache = new ThreadCacheObject (null , null , null , null , Optional .of (new ReportingApi .APIListsResponse (null , List .of (
221
+ new ReportingApi .ListsResponseEntry ("geoip1" , "description" , List .of (
222
+ "1.2.3.4" // Blocked IP
223
+ )),
224
+ new ReportingApi .ListsResponseEntry ("geoip2" , "description" , List .of (
225
+ "8.8.8.0/24"
226
+ )),
227
+ new ReportingApi .ListsResponseEntry ("geoip3" , "description" , List .of (
228
+ "4.4.4.4" // Another allowed IP
229
+ ))
230
+ ), "Test|One" )));
231
+
232
+ // Test allowed IPs
233
+ assertEquals (new ThreadCacheObject .BlockedResult (false , null ), tCache .isIpBlocked ("10.0.0.1" ));
234
+ assertEquals (new ThreadCacheObject .BlockedResult (false , null ), tCache .isIpBlocked ("4.4.4.4" ));
235
+ assertEquals (new ThreadCacheObject .BlockedResult (false , null ), tCache .isIpBlocked ("1.2.3.4" ));
236
+ assertEquals (new ThreadCacheObject .BlockedResult (false , null ), tCache .isIpBlocked ("8.8.8.1" ));
237
+
238
+ // Test a non-allowed IP
239
+ assertEquals (new ThreadCacheObject .BlockedResult (true , "allowlist" ), tCache .isIpBlocked ("4.4.4.1" ));
240
+ assertEquals (new ThreadCacheObject .BlockedResult (true , "allowlist" ), tCache .isIpBlocked ("8.8.7.8" ));
241
+
242
+ }
243
+
244
+
158
245
}
0 commit comments