1 31 32 package org.opencms.file; 33 34 import org.opencms.file.types.CmsResourceTypePlain; 35 import org.opencms.lock.CmsLock; 36 import org.opencms.lock.CmsLockException; 37 import org.opencms.main.OpenCms; 38 import org.opencms.security.CmsAccessControlEntry; 39 import org.opencms.security.CmsPermissionSet; 40 import org.opencms.security.CmsPermissionViolationException; 41 import org.opencms.security.I_CmsPrincipal; 42 import org.opencms.test.OpenCmsTestCase; 43 import org.opencms.test.OpenCmsTestProperties; 44 import org.opencms.test.OpenCmsTestResourceFilter; 45 46 import java.util.ArrayList ; 47 import java.util.Iterator ; 48 import java.util.List ; 49 50 import junit.extensions.TestSetup; 51 import junit.framework.Test; 52 import junit.framework.TestSuite; 53 54 61 public class TestLock extends OpenCmsTestCase { 62 63 68 public TestLock(String arg0) { 69 super(arg0); 70 } 71 72 77 public static Test suite() { 78 OpenCmsTestProperties.initialize(org.opencms.test.AllTests.TEST_PROPERTIES_PATH); 79 80 TestSuite suite = new TestSuite(); 81 suite.setName(TestLock.class.getName()); 82 83 suite.addTest(new TestLock("testLockWithDeletedNewFiles")); 84 suite.addTest(new TestLock("testLockForFile")); 85 suite.addTest(new TestLock("testLockForFolder")); 86 suite.addTest(new TestLock("testLockForFolderPrelockedShared")); 87 suite.addTest(new TestLock("testLockForFolderPrelockedExclusive")); 88 suite.addTest(new TestLock("testLockSteal")); 89 suite.addTest(new TestLock("testLockRequired")); 90 suite.addTest(new TestLock("testLockInherit")); 91 suite.addTest(new TestLock("testLockForSiblings")); 92 suite.addTest(new TestLock("testLockForBaseOperations")); 93 94 95 TestSetup wrapper = new TestSetup(suite) { 96 97 protected void setUp() { 98 setupOpenCms("simpletest", "/sites/default/"); 99 } 100 101 protected void tearDown() { 102 removeOpenCms(); 103 } 104 }; 105 106 return wrapper; 107 } 108 109 123 public void testLockWithDeletedNewFiles() throws Throwable { 124 125 CmsObject cms = getCmsObject(); 126 echo("Testing lock status of a deleted new file"); 127 128 String source = "/folder1/newfile.html"; 129 130 cms.createResource(source, CmsResourceTypePlain.getStaticTypeId()); 132 assertLock(cms, source, CmsLock.TYPE_EXCLUSIVE); 134 cms.deleteResource(source, CmsResource.DELETE_REMOVE_SIBLINGS); 136 137 cms.loginUser("test2", "test2"); 139 cms.getRequestContext().setCurrentProject(cms.readProject("Offline")); 140 141 cms.createResource(source, CmsResourceTypePlain.getStaticTypeId()); 143 144 assertLock(cms, source, CmsLock.TYPE_EXCLUSIVE); 146 } 147 148 153 public void testLockForBaseOperations() throws Throwable { 154 155 CmsObject cms = getCmsObject(); 156 echo("Testing lock state for basic operations"); 157 158 String source = "/types/text.txt"; 159 String destination1 = "/types/text_new1.txt"; 160 String destination2 = "/types/text_new2.txt"; 161 storeResources(cms, source); 162 163 cms.copyResource(source, destination1, CmsResource.COPY_AS_NEW); 165 166 assertLock(cms, source, CmsLock.TYPE_UNLOCKED); 169 assertLock(cms, destination1, CmsLock.TYPE_EXCLUSIVE); 170 171 cms.lockResource(source); 173 cms.copyResource(source, destination2, CmsResource.COPY_AS_NEW); 174 175 assertLock(cms, source, CmsLock.TYPE_EXCLUSIVE); 177 assertLock(cms, destination2, CmsLock.TYPE_EXCLUSIVE); 178 179 source = "/types/jsp.jsp"; 181 destination1 = "/types/jsp_new1.html"; 182 183 cms.lockResource(source); 185 cms.moveResource(source, destination1); 186 187 assertLock(cms, source, CmsLock.TYPE_SHARED_EXCLUSIVE); 189 assertLock(cms, destination1, CmsLock.TYPE_EXCLUSIVE); 190 } 191 192 197 public void testLockForFile() throws Throwable { 198 199 CmsObject cms = getCmsObject(); 200 echo("Testing locking of files"); 201 202 String source = "/folder1/subfolder11/page1.html"; 203 String sibling1 = "/folder1/subfolder12/page1.html"; 204 String sibling2 = "/folder2/subfolder22/page1.html"; 205 storeResources(cms, source); 206 207 cms.lockResource(source); 209 210 assertLock(cms, source, CmsLock.TYPE_EXCLUSIVE); 213 assertLock(cms, sibling1, CmsLock.TYPE_SHARED_EXCLUSIVE); 214 assertLock(cms, sibling2, CmsLock.TYPE_SHARED_EXCLUSIVE); 215 216 cms.unlockResource(source); 218 219 assertLock(cms, source, CmsLock.TYPE_UNLOCKED); 221 assertLock(cms, sibling1, CmsLock.TYPE_UNLOCKED); 222 assertLock(cms, sibling2, CmsLock.TYPE_UNLOCKED); 223 } 224 225 230 public void testLockForFolder() throws Throwable { 231 232 CmsObject cms = getCmsObject(); 233 echo("Testing locking of folders"); 234 235 String folder = "/folder1/subfolder12/"; 236 String sibling1 = "/folder1/subfolder12/page1.html"; 237 String sibling2 = "/folder2/subfolder22/page1.html"; 238 storeResources(cms, folder); 239 240 cms.lockResource(folder); 242 243 assertLock(cms, folder, CmsLock.TYPE_EXCLUSIVE); 245 246 List resources = cms.getResourcesInFolder(folder, CmsResourceFilter.DEFAULT); 248 Iterator i = resources.iterator(); 249 while (i.hasNext()) { 250 CmsResource res = (CmsResource)i.next(); 251 assertLock(cms, cms.getSitePath(res), CmsLock.TYPE_INHERITED); 252 } 253 254 assertLock(cms, sibling1, CmsLock.TYPE_INHERITED); 256 assertLock(cms, sibling2, CmsLock.TYPE_UNLOCKED); 258 259 cms.unlockResource(folder); 261 262 assertLock(cms, folder, CmsLock.TYPE_UNLOCKED); 264 265 resources = cms.getResourcesInFolder(folder, CmsResourceFilter.DEFAULT); 267 i = resources.iterator(); 268 while (i.hasNext()) { 269 CmsResource res = (CmsResource)i.next(); 270 assertLock(cms, cms.getSitePath(res), CmsLock.TYPE_UNLOCKED); 271 } 272 273 assertLock(cms, sibling1, CmsLock.TYPE_UNLOCKED); 275 assertLock(cms, sibling2, CmsLock.TYPE_UNLOCKED); 276 277 } 278 279 286 public void testLockForFolderPrelockedShared() throws Throwable { 287 288 CmsObject cms = getCmsObject(); 289 echo("Testing locking of folders with shared prelocks"); 290 291 String folder = "/folder1/subfolder12/"; 292 String source = "/folder1/subfolder11/page1.html"; 293 String sibling1 = "/folder1/subfolder12/page1.html"; 294 String sibling2 = "/folder2/subfolder22/page1.html"; 295 storeResources(cms, folder); 296 297 298 cms.lockResource(source); 300 301 assertLock(cms, source, CmsLock.TYPE_EXCLUSIVE); 304 assertLock(cms, sibling1, CmsLock.TYPE_SHARED_EXCLUSIVE); 305 assertLock(cms, sibling2, CmsLock.TYPE_SHARED_EXCLUSIVE); 306 307 cms.lockResource(folder); 309 310 List resources = cms.getResourcesInFolder(folder, CmsResourceFilter.DEFAULT); 312 Iterator i = resources.iterator(); 313 while (i.hasNext()) { 314 CmsResource res = (CmsResource)i.next(); 315 if (cms.getSitePath(res).equals(sibling1)) { 318 assertLock(cms, cms.getSitePath(res), CmsLock.TYPE_SHARED_INHERITED); 319 } else { 320 assertLock(cms, cms.getSitePath(res), CmsLock.TYPE_INHERITED); 321 } 322 } 323 324 assertLock(cms, source, CmsLock.TYPE_EXCLUSIVE); 326 assertLock(cms, sibling2, CmsLock.TYPE_SHARED_EXCLUSIVE); 327 328 cms.unlockResource(folder); 330 331 assertLock(cms, folder, CmsLock.TYPE_UNLOCKED); 333 334 resources = cms.getResourcesInFolder(folder, CmsResourceFilter.DEFAULT); 337 i = resources.iterator(); 338 while (i.hasNext()) { 339 CmsResource res = (CmsResource)i.next(); 340 if (cms.getSitePath(res).equals(sibling1)) { 341 assertLock(cms, cms.getSitePath(res), CmsLock.TYPE_SHARED_EXCLUSIVE); 342 } else { 343 assertLock(cms, cms.getSitePath(res), CmsLock.TYPE_UNLOCKED); 344 } 345 } 346 347 assertLock(cms, source, CmsLock.TYPE_EXCLUSIVE); 349 assertLock(cms, sibling2, CmsLock.TYPE_SHARED_EXCLUSIVE); 350 351 cms.unlockResource(source); 353 354 assertLock(cms, source, CmsLock.TYPE_UNLOCKED); 356 assertLock(cms, sibling1, CmsLock.TYPE_UNLOCKED); 357 assertLock(cms, sibling2, CmsLock.TYPE_UNLOCKED); 358 359 } 360 361 368 public void testLockForFolderPrelockedExclusive() throws Throwable { 369 370 CmsObject cms = getCmsObject(); 371 echo("Testing locking of folders with exclusive prelocks"); 372 373 String folder = "/folder1/subfolder12/"; 374 String source = "/folder1/subfolder12/page1.html"; 375 String sibling1 = "/folder1/subfolder11/page1.html"; 376 String sibling2 = "/folder2/subfolder22/page1.html"; 377 storeResources(cms, folder); 378 379 380 cms.lockResource(source); 382 383 assertLock(cms, source, CmsLock.TYPE_EXCLUSIVE); 386 assertLock(cms, sibling1, CmsLock.TYPE_SHARED_EXCLUSIVE); 387 assertLock(cms, sibling2, CmsLock.TYPE_SHARED_EXCLUSIVE); 388 389 cms.lockResource(folder); 391 392 List resources = cms.getResourcesInFolder(folder, CmsResourceFilter.DEFAULT); 394 Iterator i = resources.iterator(); 395 while (i.hasNext()) { 396 CmsResource res = (CmsResource)i.next(); 397 assertLock(cms, cms.getSitePath(res), CmsLock.TYPE_INHERITED); 398 } 399 400 assertLock(cms, sibling1, CmsLock.TYPE_UNLOCKED); 402 assertLock(cms, sibling2, CmsLock.TYPE_UNLOCKED); 403 404 405 cms.unlockResource(folder); 407 408 assertLock(cms, folder, CmsLock.TYPE_UNLOCKED); 410 411 resources = cms.getResourcesInFolder(folder, CmsResourceFilter.DEFAULT); 413 i = resources.iterator(); 414 while (i.hasNext()) { 415 CmsResource res = (CmsResource)i.next(); 416 assertLock(cms, cms.getSitePath(res), CmsLock.TYPE_UNLOCKED); 417 } 418 419 assertLock(cms, sibling1, CmsLock.TYPE_UNLOCKED); 421 assertLock(cms, sibling2, CmsLock.TYPE_UNLOCKED); 422 423 } 424 425 430 public void testLockSteal() throws Throwable { 431 432 CmsObject cms = getCmsObject(); 433 echo("Testing stealing a lock"); 434 435 String source = "/folder1/subfolder11/page1.html"; 436 String sibling1 = "/folder1/subfolder12/page1.html"; 437 String sibling2 = "/folder2/subfolder22/page1.html"; 438 storeResources(cms, source); 439 440 CmsProject offlineProject = cms.readProject("Offline"); 442 443 cms.loginUser("test1" , "test1"); 445 cms.getRequestContext().setCurrentProject(offlineProject); 446 447 cms.lockResource(source); 449 450 assertLock(cms, source, CmsLock.TYPE_EXCLUSIVE); 453 assertLock(cms, sibling1, CmsLock.TYPE_SHARED_EXCLUSIVE); 454 assertLock(cms, sibling2, CmsLock.TYPE_SHARED_EXCLUSIVE); 455 456 cms.loginUser("Admin" , "admin"); 458 cms.getRequestContext().setCurrentProject(offlineProject); 459 460 cms.changeLock(sibling1); 462 463 assertLock(cms, sibling1, CmsLock.TYPE_EXCLUSIVE); 466 assertLock(cms, source, CmsLock.TYPE_SHARED_EXCLUSIVE); 467 assertLock(cms, sibling2, CmsLock.TYPE_SHARED_EXCLUSIVE); 468 469 cms.chacc(source, I_CmsPrincipal.PRINCIPAL_USER, "test2", 0, CmsPermissionSet.PERMISSION_WRITE, CmsAccessControlEntry.ACCESS_FLAGS_OVERWRITE + CmsAccessControlEntry.ACCESS_FLAGS_INHERIT); 471 472 cms.loginUser("test2" , "test2"); 474 cms.getRequestContext().setCurrentProject(offlineProject); 475 476 Exception error = null; 477 try { 478 cms.changeLock(source); 480 } catch (CmsPermissionViolationException e) { 481 error = e; 482 } 483 assertNotNull(error); 484 try { 485 cms.changeLock(sibling1); 487 } catch (CmsPermissionViolationException e) { 488 error = e; 489 } 490 assertNotNull(error); 491 try { 492 cms.changeLock(sibling2); 494 } catch (CmsPermissionViolationException e) { 495 error = e; 496 } 497 assertNotNull(error); 498 499 cms.loginUser("Admin" , "admin"); 501 cms.getRequestContext().setCurrentProject(offlineProject); 502 503 assertLock(cms, sibling1, CmsLock.TYPE_EXCLUSIVE); 505 assertLock(cms, source, CmsLock.TYPE_SHARED_EXCLUSIVE); 506 assertLock(cms, sibling2, CmsLock.TYPE_SHARED_EXCLUSIVE); 507 508 cms.loginUser("test1" , "test1"); 510 cms.getRequestContext().setCurrentProject(offlineProject); 511 512 cms.changeLock(sibling2); 514 515 assertLock(cms, sibling2, CmsLock.TYPE_EXCLUSIVE); 517 assertLock(cms, source, CmsLock.TYPE_SHARED_EXCLUSIVE); 518 assertLock(cms, sibling1, CmsLock.TYPE_SHARED_EXCLUSIVE); 519 } 520 521 526 public void testLockForSiblings() throws Throwable { 527 528 CmsObject cms = getCmsObject(); 529 echo("Testing lock state after sibling creation"); 530 531 String source = "/folder2/index.html"; 532 String destination1 = "/folder2/index_sib1.html"; 533 String destination2 = "/folder2/index_sib2.html"; 534 storeResources(cms, source); 535 536 cms.copyResource(source, destination1, CmsResource.COPY_AS_SIBLING); 538 539 assertLock(cms, source, CmsLock.TYPE_SHARED_EXCLUSIVE); 542 assertLock(cms, destination1, CmsLock.TYPE_EXCLUSIVE); 543 544 cms.copyResource(source, destination2, CmsResource.COPY_AS_SIBLING); 546 547 assertLock(cms, source, CmsLock.TYPE_SHARED_EXCLUSIVE); 550 assertLock(cms, destination1, CmsLock.TYPE_EXCLUSIVE); 551 assertLock(cms, destination2, CmsLock.TYPE_SHARED_EXCLUSIVE); 552 553 source = "/folder2/page1.html"; 555 destination1 = "/folder2/page1_sib1.html"; 556 destination2 = "/folder2/page1_sib2.html"; 557 558 cms.lockResource(source); 560 cms.createSibling(source, destination1, null); 561 562 assertLock(cms, source, CmsLock.TYPE_EXCLUSIVE); 564 assertLock(cms, destination1, CmsLock.TYPE_SHARED_EXCLUSIVE); 565 566 cms.createSibling(destination1, destination2, null); 568 assertLock(cms, source, CmsLock.TYPE_EXCLUSIVE); 571 assertLock(cms, destination1, CmsLock.TYPE_SHARED_EXCLUSIVE); 572 assertLock(cms, destination2, CmsLock.TYPE_SHARED_EXCLUSIVE); 573 } 574 575 580 public void testLockInherit() throws Throwable { 581 582 CmsObject cms = getCmsObject(); 583 echo("Testing inherited lock delete scenario"); 584 585 String source = "/folder1/index.html"; 586 String folder = "/folder1/"; 587 storeResources(cms, source); 588 589 cms.lockResource(source); 591 cms.deleteResource(source, CmsResource.DELETE_PRESERVE_SIBLINGS); 592 593 cms.lockResource(folder); 595 596 assertLock(cms, source, CmsLock.TYPE_INHERITED); 598 } 599 600 605 public void testLockRequired() throws Throwable { 606 607 CmsObject cms = getCmsObject(); 608 echo("Testing if a lock is required for write/control operations"); 609 610 String source = "/index.html"; 611 storeResources(cms, source); 612 long timestamp = System.currentTimeMillis(); 613 614 assertLock(cms, source, CmsLock.TYPE_UNLOCKED); 616 617 CmsFile file = cms.readFile(source); 618 619 boolean needLock; 620 621 needLock = false; 622 try { 623 cms.setDateLastModified(source, timestamp, false); 624 } catch (CmsLockException e) { 625 needLock = true; 627 } 628 if (! needLock) { 629 fail("Touch operation on resource permitted without a lock on the current user!"); 630 } 631 632 needLock = false; 633 try { 634 cms.deleteResource(source, CmsResource.DELETE_PRESERVE_SIBLINGS); 635 } catch (CmsLockException e) { 636 needLock = true; 638 } 639 if (! needLock) { 640 fail("Delete operation on resource permitted without a lock on the current user!"); 641 } 642 643 needLock = false; 644 try { 645 cms.writeFile(file); 646 } catch (CmsLockException e) { 647 needLock = true; 649 } 650 if (! needLock) { 651 fail("Write operation on resource permitted without a lock on the current user!"); 652 } 653 654 needLock = false; 655 try { 656 cms.moveResource(source, "index_dest.html"); 657 } catch (CmsLockException e) { 658 needLock = true; 660 } 661 if (! needLock) { 662 fail("Move operation on resource permitted without a lock on the current user!"); 663 } 664 665 needLock = false; 666 try { 667 cms.writePropertyObject(source, new CmsProperty(CmsPropertyDefinition.PROPERTY_TITLE, "New title", null)); 668 } catch (CmsLockException e) { 669 needLock = true; 671 } 672 if (! needLock) { 673 fail("Write property operation on resource permitted without a lock on the current user!"); 674 } 675 676 needLock = false; 677 try { 678 List properties = new ArrayList (); 679 properties.add(new CmsProperty(CmsPropertyDefinition.PROPERTY_TITLE, "New title 2", null)); 680 cms.writePropertyObjects(source, properties); 681 } catch (CmsLockException e) { 682 needLock = true; 684 } 685 if (! needLock) { 686 fail("Write property list operation on resource permitted without a lock on the current user!"); 687 } 688 689 needLock = false; 690 try { 691 cms.chflags(source, 1234); 692 } catch (CmsLockException e) { 693 needLock = true; 695 } 696 if (! needLock) { 697 fail("Change flags operation on resource permitted without a lock on the current user!"); 698 } 699 700 needLock = false; 701 try { 702 cms.chtype(source, CmsResourceTypePlain.getStaticTypeId()); 703 } catch (CmsLockException e) { 704 needLock = true; 706 } 707 if (! needLock) { 708 fail("Change type operation on resource permitted without a lock on the current user!"); 709 } 710 711 needLock = false; 712 try { 713 cms.replaceResource(source, CmsResourceTypePlain.getStaticTypeId(), "Kaputt".getBytes(), null); 714 } catch (CmsLockException e) { 715 needLock = true; 717 } 718 if (! needLock) { 719 fail("Replace operation on resource permitted without a lock on the current user!"); 720 } 721 722 needLock = false; 723 try { 724 cms.changeLastModifiedProjectId(source); 725 } catch (CmsLockException e) { 726 needLock = true; 728 } 729 if (! needLock) { 730 fail("Change last modified in project operation on resource permitted without a lock on the current user!"); 731 } 732 733 needLock = false; 734 try { 735 CmsPermissionSet permissions = new CmsPermissionSet(CmsPermissionSet.PERMISSION_WRITE, CmsPermissionSet.PERMISSION_READ); 736 cms.chacc(source, I_CmsPrincipal.PRINCIPAL_GROUP, OpenCms.getDefaultUsers().getGroupAdministrators(), permissions.getAllowedPermissions(), permissions.getDeniedPermissions(), CmsAccessControlEntry.ACCESS_FLAGS_OVERWRITE); 737 } catch (CmsLockException e) { 738 needLock = true; 740 } 741 if (! needLock) { 742 fail("Change permissions operation on resource permitted without a lock on the current user!"); 743 } 744 745 needLock = false; 746 try { 747 cms.undeleteResource(source); 748 } catch (CmsLockException e) { 749 needLock = true; 751 } 752 if (! needLock) { 753 fail("Unlock operation on resource permitted without a lock on the current user!"); 754 } 755 756 assertFilter(cms, source, OpenCmsTestResourceFilter.FILTER_EQUAL); 758 759 cms.lockResource(source); 761 cms.deleteResource(source, CmsResource.DELETE_PRESERVE_SIBLINGS); 762 763 cms.lockResource(source); 765 cms.undeleteResource(source); 766 cms.unlockResource(source); 767 768 assertFilter(cms, source, OpenCmsTestResourceFilter.FILTER_UNDOCHANGES); 770 } 771 } 772 773 | Popular Tags |