|
| 1 | +package vulnerabilities.outbound_blocking; |
| 2 | + |
| 3 | +import dev.aikido.agent_api.storage.service_configuration.Domain; |
| 4 | +import dev.aikido.agent_api.vulnerabilities.outbound_blocking.OutboundDomains; |
| 5 | +import org.junit.jupiter.api.BeforeEach; |
| 6 | +import org.junit.jupiter.api.Test; |
| 7 | + |
| 8 | +import java.util.List; |
| 9 | + |
| 10 | +import static org.junit.jupiter.api.Assertions.*; |
| 11 | + |
| 12 | +public class OutboundDomainsTest { |
| 13 | + |
| 14 | + private OutboundDomains outboundDomains; |
| 15 | + |
| 16 | + @BeforeEach |
| 17 | + public void setUp() { |
| 18 | + outboundDomains = new OutboundDomains(); |
| 19 | + } |
| 20 | + |
| 21 | + // --- shouldBlockOutgoingRequest with blockNewOutgoingRequests=false (default) --- |
| 22 | + |
| 23 | + @Test |
| 24 | + public void testDefaultDoesNotBlockUnknownHostname() { |
| 25 | + assertFalse(outboundDomains.shouldBlockOutgoingRequest("unknown.com")); |
| 26 | + } |
| 27 | + |
| 28 | + @Test |
| 29 | + public void testBlockModeBlocksHostname() { |
| 30 | + outboundDomains.update(List.of(new Domain("blocked.com", "block")), false); |
| 31 | + assertTrue(outboundDomains.shouldBlockOutgoingRequest("blocked.com")); |
| 32 | + } |
| 33 | + |
| 34 | + @Test |
| 35 | + public void testAllowModeDoesNotBlockHostname() { |
| 36 | + outboundDomains.update(List.of(new Domain("allowed.com", "allow")), false); |
| 37 | + assertFalse(outboundDomains.shouldBlockOutgoingRequest("allowed.com")); |
| 38 | + } |
| 39 | + |
| 40 | + @Test |
| 41 | + public void testUnknownHostnameNotBlockedWhenBlockNewOutgoingRequestsFalse() { |
| 42 | + outboundDomains.update(List.of(new Domain("blocked.com", "block")), false); |
| 43 | + assertFalse(outboundDomains.shouldBlockOutgoingRequest("unknown.com")); |
| 44 | + } |
| 45 | + |
| 46 | + // --- shouldBlockOutgoingRequest with blockNewOutgoingRequests=true --- |
| 47 | + |
| 48 | + @Test |
| 49 | + public void testUnknownHostnameBlockedWhenBlockNewOutgoingRequestsTrue() { |
| 50 | + outboundDomains.update(List.of(), true); |
| 51 | + assertTrue(outboundDomains.shouldBlockOutgoingRequest("unknown.com")); |
| 52 | + } |
| 53 | + |
| 54 | + @Test |
| 55 | + public void testAllowModeNotBlockedWhenBlockNewOutgoingRequestsTrue() { |
| 56 | + outboundDomains.update(List.of(new Domain("allowed.com", "allow")), true); |
| 57 | + assertFalse(outboundDomains.shouldBlockOutgoingRequest("allowed.com")); |
| 58 | + } |
| 59 | + |
| 60 | + @Test |
| 61 | + public void testBlockModeBlockedWhenBlockNewOutgoingRequestsTrue() { |
| 62 | + outboundDomains.update(List.of(new Domain("blocked.com", "block")), true); |
| 63 | + assertTrue(outboundDomains.shouldBlockOutgoingRequest("blocked.com")); |
| 64 | + } |
| 65 | + |
| 66 | + // --- update() behaviour --- |
| 67 | + |
| 68 | + @Test |
| 69 | + public void testUpdateWithNullDomainsPreservesExistingDomains() { |
| 70 | + outboundDomains.update(List.of(new Domain("blocked.com", "block")), false); |
| 71 | + // null domains should not reset the map |
| 72 | + outboundDomains.update(null, false); |
| 73 | + assertTrue(outboundDomains.shouldBlockOutgoingRequest("blocked.com")); |
| 74 | + } |
| 75 | + |
| 76 | + @Test |
| 77 | + public void testUpdateWithNullDomainsUpdatesBlockFlag() { |
| 78 | + outboundDomains.update(null, true); |
| 79 | + // blockNewOutgoingRequests should now be true even though domains unchanged |
| 80 | + assertTrue(outboundDomains.shouldBlockOutgoingRequest("unknown.com")); |
| 81 | + } |
| 82 | + |
| 83 | + @Test |
| 84 | + public void testUpdateReplacesDomainsMap() { |
| 85 | + outboundDomains.update(List.of(new Domain("old.com", "block")), false); |
| 86 | + outboundDomains.update(List.of(new Domain("new.com", "block")), false); |
| 87 | + // old entry should be gone |
| 88 | + assertFalse(outboundDomains.shouldBlockOutgoingRequest("old.com")); |
| 89 | + assertTrue(outboundDomains.shouldBlockOutgoingRequest("new.com")); |
| 90 | + } |
| 91 | + |
| 92 | + @Test |
| 93 | + public void testUpdateWithEmptyListClearsDomainsMap() { |
| 94 | + outboundDomains.update(List.of(new Domain("blocked.com", "block")), false); |
| 95 | + outboundDomains.update(List.of(), false); |
| 96 | + assertFalse(outboundDomains.shouldBlockOutgoingRequest("blocked.com")); |
| 97 | + } |
| 98 | + |
| 99 | + @Test |
| 100 | + public void testMultipleDomainsWithMixedModes() { |
| 101 | + outboundDomains.update(List.of( |
| 102 | + new Domain("blocked.com", "block"), |
| 103 | + new Domain("allowed.com", "allow") |
| 104 | + ), false); |
| 105 | + assertTrue(outboundDomains.shouldBlockOutgoingRequest("blocked.com")); |
| 106 | + assertFalse(outboundDomains.shouldBlockOutgoingRequest("allowed.com")); |
| 107 | + assertFalse(outboundDomains.shouldBlockOutgoingRequest("other.com")); |
| 108 | + } |
| 109 | +} |
0 commit comments