3131import java .util .Arrays ;
3232import java .util .Collections ;
3333import java .util .List ;
34+ import java .util .Set ;
35+
36+ import org .apache .cloudstack .network .NetworkPermissionVO ;
37+ import org .apache .cloudstack .network .dao .NetworkPermissionDao ;
38+ import org .junit .Before ;
39+ import org .junit .Test ;
40+ import org .mockito .InjectMocks ;
41+ import org .mockito .Mock ;
42+ import org .mockito .MockitoAnnotations ;
43+ import org .mockito .Spy ;
3444
3545import com .cloud .dc .DataCenter ;
3646import com .cloud .dc .DataCenterVO ;
47+ import com .cloud .dc .VlanVO ;
3748import com .cloud .dc .dao .DataCenterDao ;
49+ import com .cloud .dc .dao .VlanDao ;
50+ import com .cloud .domain .DomainVO ;
51+ import com .cloud .domain .dao .DomainDao ;
3852import com .cloud .exception .InvalidParameterValueException ;
53+ import com .cloud .exception .PermissionDeniedException ;
54+ import com .cloud .network .Network .Provider ;
55+ import com .cloud .network .dao .IPAddressDao ;
56+ import com .cloud .network .dao .IPAddressVO ;
57+ import com .cloud .network .dao .NetworkDao ;
58+ import com .cloud .network .dao .NetworkDomainDao ;
59+ import com .cloud .network .dao .NetworkDomainVO ;
60+ import com .cloud .network .dao .NetworkVO ;
3961import com .cloud .network .dao .PhysicalNetworkDao ;
4062import com .cloud .network .dao .PhysicalNetworkServiceProviderDao ;
4163import com .cloud .network .dao .PhysicalNetworkServiceProviderVO ;
4264import com .cloud .network .dao .PhysicalNetworkVO ;
43- import junit .framework .Assert ;
44-
45- import org .junit .Before ;
46- import org .junit .Test ;
47-
48- import com .cloud .dc .VlanVO ;
49- import com .cloud .dc .dao .VlanDao ;
50- import com .cloud .network .dao .IPAddressDao ;
51- import com .cloud .network .dao .IPAddressVO ;
65+ import com .cloud .projects .dao .ProjectDao ;
5266import com .cloud .user .Account ;
67+ import com .cloud .user .AccountVO ;
68+ import com .cloud .user .DomainManager ;
69+ import com .cloud .user .dao .AccountDao ;
5370import com .cloud .utils .db .Filter ;
5471import com .cloud .utils .db .SearchBuilder ;
5572import com .cloud .utils .db .SearchCriteria ;
73+ import com .cloud .utils .exception .CloudRuntimeException ;
5674import com .cloud .utils .net .Ip ;
57- import com .cloud .network .Network .Provider ;
58- import org .mockito .InjectMocks ;
59- import org .mockito .Mock ;
60- import org .mockito .MockitoAnnotations ;
61- import org .mockito .Spy ;
75+
76+ import junit .framework .Assert ;
6277
6378public class NetworkModelTest {
6479
@@ -85,6 +100,20 @@ public class NetworkModelTest {
85100 private PhysicalNetworkVO physicalNetworkZone2 ;
86101 @ Mock
87102 private PhysicalNetworkServiceProviderVO providerVO ;
103+ @ Mock
104+ private AccountDao accountDao ;
105+ @ Mock
106+ private NetworkDao networkDao ;
107+ @ Mock
108+ private NetworkPermissionDao networkPermissionDao ;
109+ @ Mock
110+ private NetworkDomainDao networkDomainDao ;
111+ @ Mock
112+ private DomainManager domainManager ;
113+ @ Mock
114+ private DomainDao domainDao ;
115+ @ Mock
116+ private ProjectDao projectDao ;
88117
89118 private static final long ZONE_1_ID = 1L ;
90119 private static final long ZONE_2_ID = 2L ;
@@ -263,4 +292,116 @@ public void checkIp6ParametersTestNullStartAndEndIpv6() {
263292 networkModel .checkIp6Parameters (null , null , IPV6_GATEWAY ,IPV6_CIDR );
264293 }
265294
295+ @ Test
296+ public void testCheckNetworkPermissions () {
297+ long accountId = 1L ;
298+ AccountVO caller = mock (AccountVO .class );
299+ when (caller .getId ()).thenReturn (accountId );
300+ when (caller .getType ()).thenReturn (Account .Type .NORMAL );
301+ NetworkVO network = mock (NetworkVO .class );
302+ when (network .getGuestType ()).thenReturn (Network .GuestType .Isolated );
303+ when (network .getAccountId ()).thenReturn (accountId );
304+ when (accountDao .findById (accountId )).thenReturn (caller );
305+ when (networkDao .listBy (caller .getId (), network .getId ())).thenReturn (List .of (network ));
306+ when (networkPermissionDao .findByNetworkAndAccount (network .getId (), caller .getId ())).thenReturn (mock (NetworkPermissionVO .class ));
307+ networkModel .checkNetworkPermissions (caller , network );
308+ }
309+
310+ @ Test (expected = CloudRuntimeException .class )
311+ public void testCheckNetworkPermissionsNullNetwork () {
312+ AccountVO caller = mock (AccountVO .class );
313+ NetworkVO network = null ;
314+ networkModel .checkNetworkPermissions (caller , network );
315+ }
316+
317+ @ Test (expected = PermissionDeniedException .class )
318+ public void testCheckNetworkPermissionsNoOwner () {
319+ long accountId = 1L ;
320+ AccountVO caller = mock (AccountVO .class );
321+ when (caller .getId ()).thenReturn (accountId );
322+ when (caller .getType ()).thenReturn (Account .Type .NORMAL );
323+ NetworkVO network = mock (NetworkVO .class );
324+ when (network .getGuestType ()).thenReturn (Network .GuestType .Isolated );
325+ when (network .getAccountId ()).thenReturn (accountId );
326+ when (accountDao .findById (accountId )).thenReturn (null );
327+ networkModel .checkNetworkPermissions (caller , network );
328+ }
329+
330+ @ Test (expected = PermissionDeniedException .class )
331+ public void testCheckNetworkPermissionsNoPermission () {
332+ long accountId = 1L ;
333+ AccountVO caller = mock (AccountVO .class );
334+ when (caller .getId ()).thenReturn (accountId );
335+ when (caller .getType ()).thenReturn (Account .Type .NORMAL );
336+ NetworkVO network = mock (NetworkVO .class );
337+ when (network .getGuestType ()).thenReturn (Network .GuestType .Isolated );
338+ when (network .getAccountId ()).thenReturn (accountId );
339+ when (accountDao .findById (accountId )).thenReturn (caller );
340+ when (networkDao .listBy (caller .getId (), network .getId ())).thenReturn (null );
341+ when (networkPermissionDao .findByNetworkAndAccount (network .getId (), caller .getId ())).thenReturn (null );
342+ networkModel .checkNetworkPermissions (caller , network );
343+ }
344+
345+ @ Test
346+ public void testCheckNetworkPermissionsSharedNetwork () {
347+ long id = 1L ;
348+ long subDomainId = 2L ;
349+ AccountVO caller = mock (AccountVO .class );
350+ when (caller .getId ()).thenReturn (id );
351+ when (caller .getDomainId ()).thenReturn (id );
352+ when (caller .getType ()).thenReturn (Account .Type .NORMAL );
353+ NetworkVO network = mock (NetworkVO .class );
354+ when (network .getGuestType ()).thenReturn (Network .GuestType .Shared );
355+ when (network .getId ()).thenReturn (id );
356+ when (networkDao .findById (network .getId ())).thenReturn (network );
357+ NetworkDomainVO networkDomainVO = mock (NetworkDomainVO .class );
358+ when (networkDomainVO .getDomainId ()).thenReturn (id );
359+ when (networkDomainDao .getDomainNetworkMapByNetworkId (id )).thenReturn (networkDomainVO );
360+ networkModel .checkNetworkPermissions (caller , network );
361+ when (caller .getDomainId ()).thenReturn (subDomainId );
362+ networkDomainVO .subdomainAccess = Boolean .TRUE ;
363+ when (domainManager .getDomainParentIds (subDomainId )).thenReturn (Set .of (id ));
364+ networkModel .checkNetworkPermissions (caller , network );
365+ }
366+
367+ @ Test (expected = PermissionDeniedException .class )
368+ public void testCheckNetworkPermissionsSharedNetworkNoSubDomainAccess () {
369+ long id = 1L ;
370+ long subDomainId = 2L ;
371+ AccountVO caller = mock (AccountVO .class );
372+ when (caller .getId ()).thenReturn (id );
373+ when (caller .getDomainId ()).thenReturn (subDomainId );
374+ when (caller .getType ()).thenReturn (Account .Type .NORMAL );
375+ NetworkVO network = mock (NetworkVO .class );
376+ when (network .getGuestType ()).thenReturn (Network .GuestType .Shared );
377+ when (network .getId ()).thenReturn (id );
378+ when (networkDao .findById (network .getId ())).thenReturn (network );
379+ when (domainDao .findById (caller .getDomainId ())).thenReturn (mock (DomainVO .class ));
380+ NetworkDomainVO networkDomainVO = mock (NetworkDomainVO .class );
381+ when (networkDomainVO .getDomainId ()).thenReturn (id );
382+ networkDomainVO .subdomainAccess = Boolean .FALSE ;
383+ when (networkDomainDao .getDomainNetworkMapByNetworkId (id )).thenReturn (networkDomainVO );
384+ networkModel .checkNetworkPermissions (caller , network );
385+ }
386+
387+ @ Test (expected = PermissionDeniedException .class )
388+ public void testCheckNetworkPermissionsSharedNetworkNotSubDomain () {
389+ long id = 1L ;
390+ long subDomainId = 2L ;
391+ AccountVO caller = mock (AccountVO .class );
392+ when (caller .getId ()).thenReturn (id );
393+ when (caller .getDomainId ()).thenReturn (subDomainId );
394+ when (caller .getType ()).thenReturn (Account .Type .NORMAL );
395+ NetworkVO network = mock (NetworkVO .class );
396+ when (network .getGuestType ()).thenReturn (Network .GuestType .Shared );
397+ when (network .getId ()).thenReturn (id );
398+ when (networkDao .findById (network .getId ())).thenReturn (network );
399+ when (domainDao .findById (caller .getDomainId ())).thenReturn (mock (DomainVO .class ));
400+ NetworkDomainVO networkDomainVO = mock (NetworkDomainVO .class );
401+ when (networkDomainVO .getDomainId ()).thenReturn (id );
402+ networkDomainVO .subdomainAccess = Boolean .TRUE ;
403+ when (networkDomainDao .getDomainNetworkMapByNetworkId (id )).thenReturn (networkDomainVO );
404+ when (domainManager .getDomainParentIds (subDomainId )).thenReturn (Set .of (0L ));
405+ networkModel .checkNetworkPermissions (caller , network );
406+ }
266407}
0 commit comments