Skip to content

Commit 4b2d387

Browse files
server: null-check requested IP in assignSourceNatPublicIpAddress (#14025)
1 parent 5deb6a9 commit 4b2d387

2 files changed

Lines changed: 59 additions & 1 deletion

File tree

‎server/src/main/java/com/cloud/network/IpAddressManagerImpl.java‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -828,7 +828,7 @@ public PublicIp assignSourceNatPublicIpAddress(long dcId, Long podId, Account ow
828828
if(networkPublicIp != null)
829829
return null;
830830
IPAddressVO ipAddressVO = _ipAddressDao.findByIpAndDcId(dcId, requestedIp);
831-
if (ipAddressVO.getState() != State.Free) {
831+
if (ipAddressVO == null || ipAddressVO.getState() != State.Free) {
832832
throw new InsufficientAddressCapacityException("can not assign to this network", Network.class, networkId);
833833
}
834834
return fetchNewPublicIp(dcId, podId, null, owner, type, networkId, true, true, requestedIp, null, isSystem, null, null, forSystemVms);
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
// Licensed to the Apache Software Foundation (ASF) under one
2+
// or more contributor license agreements. See the NOTICE file
3+
// distributed with this work for additional information
4+
// regarding copyright ownership. The ASF licenses this file
5+
// to you under the Apache License, Version 2.0 (the
6+
// "License"); you may not use this file except in compliance
7+
// with the License. You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing,
12+
// software distributed under the License is distributed on an
13+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
// KIND, either express or implied. See the License for the
15+
// specific language governing permissions and limitations
16+
// under the License.
17+
package com.cloud.network;
18+
19+
import static org.mockito.Mockito.when;
20+
21+
import org.junit.Test;
22+
import org.junit.runner.RunWith;
23+
import org.mockito.InjectMocks;
24+
import org.mockito.Mock;
25+
import org.mockito.Mockito;
26+
import org.mockito.junit.MockitoJUnitRunner;
27+
28+
import com.cloud.dc.Vlan.VlanType;
29+
import com.cloud.exception.InsufficientAddressCapacityException;
30+
import com.cloud.network.dao.IPAddressDao;
31+
import com.cloud.user.Account;
32+
33+
@RunWith(MockitoJUnitRunner.class)
34+
public class IpAddressManagerImplSourceNatTest {
35+
36+
@Mock
37+
private IPAddressDao _ipAddressDao;
38+
39+
@InjectMocks
40+
private IpAddressManagerImpl ipAddressManagerImpl = new IpAddressManagerImpl();
41+
42+
@Test(expected = InsufficientAddressCapacityException.class)
43+
public void assignSourceNatPublicIpAddressThrowsWhenRequestedIpNotFound() throws Exception {
44+
// findByIpAndDcId returns null when the requested IP is not a known
45+
// public IP in the zone. The method must throw a capacity exception
46+
// rather than dereference the null IPAddressVO with getState().
47+
long dcId = 1L;
48+
long networkId = 2L;
49+
String requestedIp = "10.1.1.1";
50+
Account owner = Mockito.mock(Account.class);
51+
52+
when(_ipAddressDao.findByIpAndNetworkIdAndDcId(networkId, dcId, requestedIp)).thenReturn(null);
53+
when(_ipAddressDao.findByIpAndDcId(dcId, requestedIp)).thenReturn(null);
54+
55+
ipAddressManagerImpl.assignSourceNatPublicIpAddress(dcId, null, owner, VlanType.VirtualNetwork,
56+
networkId, requestedIp, false, false);
57+
}
58+
}

0 commit comments

Comments
 (0)