11package thread_cache ;
22
3- import dev .aikido .agent_api .background .ServiceConfiguration ;
43import dev .aikido .agent_api .background .cloud .api .ReportingApi ;
54import dev .aikido .agent_api .thread_cache .ThreadCacheObject ;
65import org .junit .jupiter .api .Test ;
7- import utils .EmtpyThreadCacheObject ;
86
97import java .util .List ;
108import java .util .Optional ;
@@ -24,7 +22,7 @@ public void update() {
2422 "fd00:3234:5678:9abc::1/64" ,
2523 "5.6.7.8/32"
2624 ))
27- ), "Test|One" )));
25+ ), null , "Test|One" )));
2826
2927 assertEquals (new ThreadCacheObject .BlockedResult (true , "description" ), tCache .isIpBlocked ("1.2.3.4" ));
3028 assertEquals (new ThreadCacheObject .BlockedResult (false , null ), tCache .isIpBlocked ("2.3.4.5" ));
@@ -59,9 +57,9 @@ public void updateEmpty() {
5957 "fd00:3234:5678:9abc::1/64" ,
6058 "5.6.7.8/32"
6159 ))
62- ), "Test|One" )));
60+ ), null , "Test|One" )));
6361
64- tCache .updateBlockedLists (Optional .of (new ReportingApi .APIListsResponse (null , null )));
62+ tCache .updateBlockedLists (Optional .of (new ReportingApi .APIListsResponse (null , null , null )));
6563
6664 assertEquals (new ThreadCacheObject .BlockedResult (true , "description" ), tCache .isIpBlocked ("1.2.3.4" ));
6765 assertEquals (new ThreadCacheObject .BlockedResult (false , null ), tCache .isIpBlocked ("2.3.4.5" ));
@@ -96,16 +94,16 @@ public void updateRegexes() {
9694 "fd00:3234:5678:9abc::1/64" ,
9795 "5.6.7.8/32"
9896 ))
99- ), "Test|One" )));
97+ ), null , "Test|One" )));
10098
101- tCache .updateBlockedLists (Optional .of (new ReportingApi .APIListsResponse (null , "" )));
99+ tCache .updateBlockedLists (Optional .of (new ReportingApi .APIListsResponse (null , null , "" )));
102100
103101 assertTrue (tCache .isBlockedUserAgent ("This is my TEST user agent" ));
104102 assertTrue (tCache .isBlockedUserAgent ("Test" ));
105103 assertTrue (tCache .isBlockedUserAgent ("TEst and ONE" ));
106104 assertFalse (tCache .isBlockedUserAgent ("Est|On" ));
107105 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" )));
109107
110108 assertFalse (tCache .isBlockedUserAgent ("This is my TEST user agent" ));
111109 assertFalse (tCache .isBlockedUserAgent ("Test" ));
@@ -155,4 +153,93 @@ public void testThreadCacheBypassedIPsSubnet() {
155153 assertFalse (tCache .isBypassedIP ("10.0.1.1" ));
156154 assertFalse (tCache .isBypassedIP ("1.2.3.4" ));
157155 }
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+
158245}
0 commit comments