@@ -255,4 +255,48 @@ func TestCreateNetworkpolicyInstance_AllowDenyFlags(t *testing.T) {
255255 // Should allow IP outside denied range
256256 allowed = np .Validate ("8.8.8.8" )
257257 require .True (t , allowed , "IP outside denied range should be allowed" )
258+
259+ // Test combined Allow and Deny flags
260+ options = & Options {}
261+ options .Allow = []string {"192.168.0.0/16" } // Allow 192.168.x.x
262+ options .Deny = []string {"192.168.1.0/24" } // But deny 192.168.1.x
263+
264+ np , err = runner .createNetworkpolicyInstance (options )
265+ require .Nil (t , err , "could not create networkpolicy instance" )
266+
267+ // Should block IP outside allowed range (even if not in deny list)
268+ allowed = np .Validate ("10.0.0.1" )
269+ require .False (t , allowed , "IP outside allowed range should be blocked" )
270+
271+ // Should block IP in denied range (even if in allowed range)
272+ allowed = np .Validate ("192.168.1.100" )
273+ require .False (t , allowed , "IP in denied range should be blocked even if in allowed range" )
274+
275+ // Should allow IP in allowed range but not in denied range
276+ allowed = np .Validate ("192.168.2.50" )
277+ require .True (t , allowed , "IP in allowed range but not in denied range should be allowed" )
278+
279+ // Test with multiple Allow and Deny ranges
280+ options = & Options {}
281+ options .Allow = []string {"10.0.0.0/8" , "172.16.0.0/12" } // Allow 10.x.x.x and 172.16-31.x.x
282+ options .Deny = []string {"10.1.0.0/16" , "172.20.0.0/16" } // Deny 10.1.x.x and 172.20.x.x
283+
284+ np , err = runner .createNetworkpolicyInstance (options )
285+ require .Nil (t , err , "could not create networkpolicy instance" )
286+
287+ // Test various scenarios
288+ allowed = np .Validate ("10.0.1.1" )
289+ require .True (t , allowed , "10.0.1.1 should be allowed (in allow range, not in deny)" )
290+
291+ allowed = np .Validate ("10.1.1.1" )
292+ require .False (t , allowed , "10.1.1.1 should be blocked (in deny range)" )
293+
294+ allowed = np .Validate ("172.16.1.1" )
295+ require .True (t , allowed , "172.16.1.1 should be allowed (in allow range, not in deny)" )
296+
297+ allowed = np .Validate ("172.20.1.1" )
298+ require .False (t , allowed , "172.20.1.1 should be blocked (in deny range)" )
299+
300+ allowed = np .Validate ("192.168.1.1" )
301+ require .False (t , allowed , "192.168.1.1 should be blocked (not in any allow range)" )
258302}
0 commit comments