|
27 | 27 | import com.cloud.network.element.FirewallServiceProvider; |
28 | 28 | import com.cloud.network.element.VirtualRouterElement; |
29 | 29 | import com.cloud.network.element.VpcVirtualRouterElement; |
30 | | -import com.cloud.network.rules.FirewallManager; |
31 | 30 | import com.cloud.network.rules.FirewallRule; |
32 | 31 | import com.cloud.network.rules.FirewallRule.Purpose; |
33 | 32 | import com.cloud.network.rules.FirewallRuleVO; |
34 | 33 | import com.cloud.network.vpc.VpcManager; |
35 | 34 | import com.cloud.user.AccountManager; |
36 | 35 | import com.cloud.user.DomainManager; |
37 | 36 | import com.cloud.utils.component.ComponentContext; |
38 | | -import junit.framework.Assert; |
39 | 37 | import org.apache.cloudstack.engine.orchestration.service.NetworkOrchestrationService; |
40 | 38 | import org.apache.logging.log4j.Logger; |
41 | 39 | import org.apache.logging.log4j.LogManager; |
42 | 40 | import org.junit.After; |
| 41 | +import org.junit.Assert; |
43 | 42 | import org.junit.Before; |
44 | 43 | import org.junit.Ignore; |
45 | 44 | import org.junit.Test; |
46 | 45 | import org.junit.runner.RunWith; |
47 | 46 | import org.mockito.InjectMocks; |
48 | 47 | import org.mockito.Mock; |
49 | 48 | import org.mockito.MockitoAnnotations; |
| 49 | +import org.mockito.Spy; |
50 | 50 | import org.mockito.junit.MockitoJUnitRunner; |
51 | 51 |
|
52 | 52 | import java.util.ArrayList; |
@@ -111,19 +111,42 @@ public void testInjected() { |
111 | 111 | @Mock |
112 | 112 | FirewallRulesDao _firewallDao; |
113 | 113 |
|
| 114 | + @Spy |
114 | 115 | @InjectMocks |
115 | | - FirewallManager _firewallMgr = new FirewallManagerImpl(); |
| 116 | + FirewallManagerImpl _firewallMgr; |
| 117 | + |
| 118 | + FirewallRule fwRule50to150; |
| 119 | + FirewallRule fwRule100to200; |
| 120 | + FirewallRule fwRule151to200; |
| 121 | + |
| 122 | + FirewallRule pfRule50to150; |
| 123 | + FirewallRule pfRule100to200; |
| 124 | + FirewallRule pfRule151to200; |
| 125 | + |
116 | 126 |
|
117 | 127 | @Before |
118 | 128 | public void initMocks() { |
119 | 129 | closeable = MockitoAnnotations.openMocks(this); |
| 130 | + |
| 131 | + fwRule50to150 = createFirewallRule(50, 150, Purpose.Firewall); |
| 132 | + fwRule100to200 = createFirewallRule(100, 150, Purpose.Firewall); |
| 133 | + fwRule151to200 = createFirewallRule(151, 200, Purpose.Firewall); |
| 134 | + |
| 135 | + pfRule50to150 = createFirewallRule(50, 150, Purpose.PortForwarding); |
| 136 | + pfRule100to200 = createFirewallRule(100, 150, Purpose.PortForwarding); |
| 137 | + pfRule151to200 = createFirewallRule(151, 200, Purpose.PortForwarding); |
120 | 138 | } |
121 | 139 |
|
122 | 140 | @After |
123 | 141 | public void tearDown() throws Exception { |
124 | 142 | closeable.close(); |
125 | 143 | } |
126 | 144 |
|
| 145 | + private FirewallRule createFirewallRule(int startPort, int endPort, Purpose purpose) { |
| 146 | + return new FirewallRuleVO("xid", 1L, startPort, endPort, "TCP", 2, 3, 4, purpose, new ArrayList<>(), |
| 147 | + new ArrayList<>(), 5, 6, null, FirewallRule.TrafficType.Ingress); |
| 148 | + } |
| 149 | + |
127 | 150 | @Ignore("Requires database to be set up") |
128 | 151 | @Test |
129 | 152 | public void testApplyRules() { |
@@ -218,6 +241,75 @@ public void testDetectRulesConflict() { |
218 | 241 | } |
219 | 242 | } |
220 | 243 |
|
| 244 | + @Test |
| 245 | + public void checkIfRulesHaveConflictingPortRangesTestOnlyOneRuleIsFirewallReturnsFalse() |
| 246 | + { |
| 247 | + boolean result = _firewallMgr.checkIfRulesHaveConflictingPortRanges(fwRule50to150, pfRule50to150, true, false, false, true); |
| 248 | + |
| 249 | + Assert.assertFalse(result); |
| 250 | + } |
| 251 | + |
| 252 | + @Test |
| 253 | + public void checkIfRulesHaveConflictingPortRangesTestBothRulesAreFirewallButNoDuplicateCidrsReturnsFalse() |
| 254 | + { |
| 255 | + boolean result = _firewallMgr.checkIfRulesHaveConflictingPortRanges(fwRule50to150, fwRule50to150, false, true, false, false); |
| 256 | + |
| 257 | + Assert.assertFalse(result); |
| 258 | + } |
| 259 | + |
| 260 | + @Test |
| 261 | + public void checkIfRulesHaveConflictingPortRangesTestBothRulesArePortForwardingButNoDuplicateCidrsReturnsFalse() |
| 262 | + { |
| 263 | + boolean result = _firewallMgr.checkIfRulesHaveConflictingPortRanges(pfRule50to150, pfRule50to150, false, false, true, false); |
| 264 | + |
| 265 | + Assert.assertFalse(result); |
| 266 | + } |
| 267 | + |
| 268 | + @Test |
| 269 | + public void checkIfRulesHaveConflictingPortRangesTestBothRulesAreFirewallAndDuplicatedCidrsAndNewRuleSourceStartPortIsInsideExistingRangeReturnsTrue() |
| 270 | + { |
| 271 | + boolean result = _firewallMgr.checkIfRulesHaveConflictingPortRanges(fwRule100to200, fwRule50to150, false, true, false, true); |
| 272 | + |
| 273 | + Assert.assertTrue(result); |
| 274 | + } |
| 275 | + |
| 276 | + @Test |
| 277 | + public void checkIfRulesHaveConflictingPortRangesTestBothRulesAreFirewallAndDuplicatedCidrsAndNewRuleSourceEndPortIsInsideExistingRangeReturnsTrue() |
| 278 | + { |
| 279 | + boolean result = _firewallMgr.checkIfRulesHaveConflictingPortRanges(fwRule50to150, fwRule100to200, false, true, false, true); |
| 280 | + |
| 281 | + Assert.assertTrue(result); |
| 282 | + } |
| 283 | + |
| 284 | + @Test |
| 285 | + public void checkIfRulesHaveConflictingPortRangesTestBothRulesArePortForwardingAndDuplicatedCidrsAndNewRuleSourceStartPortIsInsideExistingRangeReturnsTrue() |
| 286 | + { |
| 287 | + boolean result = _firewallMgr.checkIfRulesHaveConflictingPortRanges(pfRule50to150, pfRule100to200, false, false, true, true); |
| 288 | + |
| 289 | + Assert.assertTrue(result); |
| 290 | + } |
| 291 | + |
| 292 | + @Test |
| 293 | + public void checkIfRulesHaveConflictingPortRangesTestBothRulesArePortForwardingAndDuplicatedCidrsAndNewRuleSourceEndPortIsInsideExistingRangeReturnsTrue() |
| 294 | + { |
| 295 | + boolean result = _firewallMgr.checkIfRulesHaveConflictingPortRanges(pfRule50to150, pfRule100to200, false, false, true, true); |
| 296 | + |
| 297 | + Assert.assertTrue(result); |
| 298 | + } |
| 299 | + |
| 300 | + @Test |
| 301 | + public void checkIfRulesHaveConflictingPortRangesTestBothRulesAreFirewallAndDuplicatedCidrsAndNoRangeConflictReturnsFalse() |
| 302 | + { |
| 303 | + boolean result = _firewallMgr.checkIfRulesHaveConflictingPortRanges(fwRule50to150, fwRule151to200, false, true, false, true); |
221 | 304 |
|
| 305 | + Assert.assertFalse(result); |
| 306 | + } |
222 | 307 |
|
| 308 | + @Test |
| 309 | + public void checkIfRulesHaveConflictingPortRangesTestBothRulesArePortForwardingAndDuplicatedCidrsAndNoRangeConflictReturnsFalse() |
| 310 | + { |
| 311 | + boolean result = _firewallMgr.checkIfRulesHaveConflictingPortRanges(pfRule50to150, pfRule151to200, false, false, true, true); |
| 312 | + |
| 313 | + Assert.assertFalse(result); |
| 314 | + } |
223 | 315 | } |
0 commit comments