|
20 | 20 | package org.apache.cloudstack.ca.provider; |
21 | 21 |
|
22 | 22 | import java.lang.reflect.Field; |
| 23 | +import java.net.NetworkInterface; |
| 24 | +import java.net.SocketException; |
23 | 25 | import java.security.InvalidKeyException; |
24 | 26 | import java.security.KeyPair; |
25 | 27 | import java.security.NoSuchAlgorithmException; |
|
38 | 40 |
|
39 | 41 | import org.apache.cloudstack.framework.ca.Certificate; |
40 | 42 | import org.apache.cloudstack.framework.config.ConfigKey; |
| 43 | +import org.apache.cloudstack.framework.config.dao.ConfigurationDao; |
41 | 44 | import org.apache.cloudstack.utils.security.CertUtils; |
42 | 45 | import org.apache.cloudstack.utils.security.SSLUtils; |
43 | 46 | import org.bouncycastle.asn1.x509.GeneralName; |
|
47 | 50 | import org.junit.Before; |
48 | 51 | import org.junit.Test; |
49 | 52 | import org.junit.runner.RunWith; |
| 53 | +import org.mockito.MockedStatic; |
50 | 54 | import org.mockito.Mockito; |
51 | 55 | import org.mockito.junit.MockitoJUnitRunner; |
52 | 56 | import org.springframework.test.util.ReflectionTestUtils; |
53 | 57 |
|
| 58 | +import com.cloud.configuration.Config; |
| 59 | +import com.cloud.utils.exception.CloudRuntimeException; |
| 60 | + |
54 | 61 |
|
55 | 62 | @RunWith(MockitoJUnitRunner.class) |
56 | 63 | public class RootCAProviderTest { |
@@ -208,4 +215,76 @@ public void testIsManagementCertificateMatch() { |
208 | 215 | Assert.fail(String.format("Exception occurred: %s", e.getMessage())); |
209 | 216 | } |
210 | 217 | } |
| 218 | + |
| 219 | + // --------------------------------------------------------------- |
| 220 | + // Tests for addConfiguredManagementIp |
| 221 | + // --------------------------------------------------------------- |
| 222 | + |
| 223 | + private ConfigurationDao mockConfigDao(String cidr) throws Exception { |
| 224 | + ConfigurationDao mockDao = Mockito.mock(ConfigurationDao.class); |
| 225 | + Mockito.when(mockDao.getValue(Config.ManagementNetwork.key())).thenReturn(cidr); |
| 226 | + addField(provider, "configDao", mockDao); |
| 227 | + return mockDao; |
| 228 | + } |
| 229 | + |
| 230 | + @Test |
| 231 | + public void testAddConfiguredManagementIpWithMatchingCidr() throws Exception { |
| 232 | + // 127.0.0.0/8 covers the loopback address (127.0.0.1) that is always |
| 233 | + // present on a Linux host, so the method must add it to the list. |
| 234 | + mockConfigDao("127.0.0.0/8"); |
| 235 | + |
| 236 | + List<String> ipList = new ArrayList<>(); |
| 237 | + provider.addConfiguredManagementIp(ipList); |
| 238 | + |
| 239 | + Assert.assertTrue("127.0.0.1 should be included for CIDR 127.0.0.0/8", |
| 240 | + ipList.contains("127.0.0.1")); |
| 241 | + } |
| 242 | + |
| 243 | + @Test |
| 244 | + public void testAddConfiguredManagementIpWithNonMatchingCidr() throws Exception { |
| 245 | + // 192.0.2.0/24 is TEST-NET-1 (RFC 5737) and is never assigned to a real |
| 246 | + // interface, so nothing should be added to the list. |
| 247 | + mockConfigDao("192.0.2.0/24"); |
| 248 | + |
| 249 | + List<String> ipList = new ArrayList<>(); |
| 250 | + provider.addConfiguredManagementIp(ipList); |
| 251 | + |
| 252 | + Assert.assertTrue("No IP should be added when no interface matches the CIDR", |
| 253 | + ipList.isEmpty()); |
| 254 | + } |
| 255 | + |
| 256 | + @Test |
| 257 | + public void testAddConfiguredManagementIpWithMultipleCidrs() throws Exception { |
| 258 | + // First CIDR is a non-matching TEST-NET; second covers loopback. |
| 259 | + // The method splits on "," and checks each CIDR individually, so |
| 260 | + // 127.0.0.1 must still be found via the second CIDR. |
| 261 | + mockConfigDao("192.0.2.0/24,127.0.0.0/8"); |
| 262 | + |
| 263 | + List<String> ipList = new ArrayList<>(); |
| 264 | + provider.addConfiguredManagementIp(ipList); |
| 265 | + |
| 266 | + Assert.assertTrue("127.0.0.1 should be included when the second comma-separated CIDR matches", |
| 267 | + ipList.contains("127.0.0.1")); |
| 268 | + } |
| 269 | + |
| 270 | + @Test |
| 271 | + public void testAddConfiguredManagementIpSocketException() throws Exception { |
| 272 | + mockConfigDao("127.0.0.0/8"); |
| 273 | + |
| 274 | + try (MockedStatic<NetworkInterface> networkInterfaceMock = |
| 275 | + Mockito.mockStatic(NetworkInterface.class)) { |
| 276 | + networkInterfaceMock.when(NetworkInterface::getNetworkInterfaces) |
| 277 | + .thenThrow(new SocketException("simulated network error")); |
| 278 | + |
| 279 | + try { |
| 280 | + provider.addConfiguredManagementIp(new ArrayList<>()); |
| 281 | + Assert.fail("Expected CloudRuntimeException to be thrown on SocketException"); |
| 282 | + } catch (CloudRuntimeException e) { |
| 283 | + Assert.assertTrue("Exception message should describe the failure", |
| 284 | + e.getMessage().contains("Exception while trying to gather the management server's network interfaces.")); |
| 285 | + Assert.assertTrue("Cause should be the original SocketException", |
| 286 | + e.getCause() instanceof SocketException); |
| 287 | + } |
| 288 | + } |
| 289 | + } |
211 | 290 | } |
0 commit comments