@@ -262,6 +262,60 @@ public void testAllocateDnsZoneNonOwnerPrivateServer() {
262262 manager .allocateDnsZone (cmd );
263263 }
264264
265+ @ Test (expected = PermissionDeniedException .class )
266+ public void testAllocateDnsZoneNonOwnerShadowingOtherAccountZoneRejected () {
267+ CreateDnsZoneCmd cmd = mock (CreateDnsZoneCmd .class );
268+ when (cmd .getName ()).thenReturn ("www.tenant1.cloud.example" );
269+ when (cmd .getDnsServerId ()).thenReturn (SERVER_ID );
270+ when (dnsServerDao .findById (SERVER_ID )).thenReturn (serverVO );
271+ Mockito .doReturn (SERVER_ID ).when (serverVO ).getId ();
272+ Mockito .doReturn (ACCOUNT_ID + 99 ).when (serverVO ).getAccountId (); // different owner
273+ Mockito .doReturn (true ).when (serverVO ).getPublicServer ();
274+ Mockito .doReturn ("cloud.example" ).when (serverVO ).getPublicDomainSuffix ();
275+ DnsZoneVO victimZone = new DnsZoneVO ("tenant1.cloud.example" , DnsZone .ZoneType .Public , SERVER_ID ,
276+ ACCOUNT_ID + 50 , DOMAIN_ID , "victim zone" );
277+ when (dnsZoneDao .listByDnsServerId (SERVER_ID )).thenReturn (Collections .singletonList (victimZone ));
278+
279+ manager .allocateDnsZone (cmd );
280+ }
281+
282+ @ Test (expected = PermissionDeniedException .class )
283+ public void testAllocateDnsZoneNonOwnerParentOfOtherAccountZoneRejected () {
284+ CreateDnsZoneCmd cmd = mock (CreateDnsZoneCmd .class );
285+ when (cmd .getName ()).thenReturn ("tenant1.cloud.example" );
286+ when (cmd .getDnsServerId ()).thenReturn (SERVER_ID );
287+ when (dnsServerDao .findById (SERVER_ID )).thenReturn (serverVO );
288+ Mockito .doReturn (SERVER_ID ).when (serverVO ).getId ();
289+ Mockito .doReturn (ACCOUNT_ID + 99 ).when (serverVO ).getAccountId (); // different owner
290+ Mockito .doReturn (true ).when (serverVO ).getPublicServer ();
291+ Mockito .doReturn ("cloud.example" ).when (serverVO ).getPublicDomainSuffix ();
292+ DnsZoneVO victimZone = new DnsZoneVO ("www.tenant1.cloud.example" , DnsZone .ZoneType .Public , SERVER_ID ,
293+ ACCOUNT_ID + 50 , DOMAIN_ID , "victim zone" );
294+ when (dnsZoneDao .listByDnsServerId (SERVER_ID )).thenReturn (Collections .singletonList (victimZone ));
295+
296+ manager .allocateDnsZone (cmd );
297+ }
298+
299+ @ Test
300+ public void testAllocateDnsZoneNonOwnerPublicServerSuccess () {
301+ CreateDnsZoneCmd cmd = mock (CreateDnsZoneCmd .class );
302+ when (cmd .getName ()).thenReturn ("tenant2.cloud.example" );
303+ when (cmd .getDnsServerId ()).thenReturn (SERVER_ID );
304+ when (cmd .getType ()).thenReturn (DnsZone .ZoneType .Public );
305+ when (dnsServerDao .findById (SERVER_ID )).thenReturn (serverVO );
306+ Mockito .doReturn (SERVER_ID ).when (serverVO ).getId ();
307+ Mockito .doReturn (ACCOUNT_ID + 99 ).when (serverVO ).getAccountId (); // different owner
308+ Mockito .doReturn (true ).when (serverVO ).getPublicServer ();
309+ Mockito .doReturn ("cloud.example" ).when (serverVO ).getPublicDomainSuffix ();
310+ when (dnsZoneDao .listByDnsServerId (SERVER_ID )).thenReturn (Collections .emptyList ());
311+ when (dnsZoneDao .findByNameServerAndType (anyString (), anyLong (), any ())).thenReturn (null );
312+ when (dnsZoneDao .persist (any (DnsZoneVO .class ))).thenReturn (zoneVO );
313+
314+ DnsZone result = manager .allocateDnsZone (cmd );
315+ assertNotNull (result );
316+ verify (dnsZoneDao ).persist (Mockito .argThat (z -> "tenant2.cloud.example" .equals (((DnsZoneVO ) z ).getName ())));
317+ }
318+
265319 @ Test (expected = CloudRuntimeException .class )
266320 public void testProvisionDnsZoneNotFound () {
267321 when (dnsZoneDao .findById (ZONE_ID )).thenReturn (null );
@@ -806,6 +860,28 @@ public void testAddDnsServerNormalUser() throws Exception {
806860 s -> !((DnsServerVO ) s ).getPublicServer () && ((DnsServerVO ) s ).getPublicDomainSuffix () == null ));
807861 }
808862
863+ @ Test (expected = InvalidParameterValueException .class )
864+ public void testAddDnsServerPublicWithoutSuffixRejected () {
865+ org .apache .cloudstack .api .command .user .dns .AddDnsServerCmd cmd = mock (
866+ org .apache .cloudstack .api .command .user .dns .AddDnsServerCmd .class );
867+ when (accountMgr .isRootAdmin (callerMock .getId ())).thenReturn (true );
868+ when (cmd .getUrl ()).thenReturn ("http://newpdns:8081" );
869+ when (cmd .isPublic ()).thenReturn (true );
870+ when (dnsServerDao .findByUrlAndAccount (anyString (), anyLong ())).thenReturn (null );
871+ manager .addDnsServer (cmd );
872+ }
873+
874+ @ Test (expected = InvalidParameterValueException .class )
875+ public void testUpdateDnsServerPublicWithoutSuffixRejected () {
876+ org .apache .cloudstack .api .command .user .dns .UpdateDnsServerCmd cmd = mock (
877+ org .apache .cloudstack .api .command .user .dns .UpdateDnsServerCmd .class );
878+ when (cmd .getId ()).thenReturn (SERVER_ID );
879+ when (cmd .isPublic ()).thenReturn (true );
880+ when (accountMgr .isRootAdmin (callerMock .getId ())).thenReturn (true );
881+ when (dnsServerDao .findById (SERVER_ID )).thenReturn (serverVO );
882+ manager .updateDnsServer (cmd );
883+ }
884+
809885 @ Test (expected = CloudRuntimeException .class )
810886 public void testAddDnsServerValidationFailure () throws Exception {
811887 org .apache .cloudstack .api .command .user .dns .AddDnsServerCmd cmd = mock (
0 commit comments