Search in sources :

Example 1 with LocalSystemOperations

use of com.axway.ats.core.system.LocalSystemOperations in project ats-framework by Axway.

the class Test_FileSystemSnapshot method checkPermissions.

@Test
public void checkPermissions() {
    if (new LocalSystemOperations().getOperatingSystemType().isWindows()) {
        Logger.getLogger(Test_FileSystemSnapshot.class).warn("We skip this test as it is not applicable for Windows OS");
        return;
    }
    // disable all checks
    configurator.setFileSnapshotCheckFileSize(false);
    configurator.setFileSnapshotCheckModificationTime(false);
    configurator.setFileSnapshotCheckFileMd5(false);
    configurator.setFileSnapshotCheckFilePermissions(false);
    // we work with 2 files only
    String firstFile = FILES_ROOT + "permissions/sub-dir1/file3.xml";
    String secondFile = FILES_ROOT + "permissions/sub-dir2/file3.xml";
    // remember the current permissions
    String firstPermissions = new LocalFileSystemOperations().getFilePermissions(firstFile);
    String secondPermissions = new LocalFileSystemOperations().getFilePermissions(secondFile);
    // make permissions different
    new LocalFileSystemOperations().setFilePermissions(firstFile, "333");
    new LocalFileSystemOperations().setFilePermissions(secondFile, "777");
    try {
        // TEST1: The pair of files have different permissions, but we have disabled the check, so no
        // error will be thrown when comparing
        FileSystemSnapshot snapshot1 = new FileSystemSnapshot("snap1");
        snapshot1.addDirectory("F1", FILES_ROOT + "permissions/sub-dir1");
        snapshot1.takeSnapshot();
        FileSystemSnapshot snapshot2 = new FileSystemSnapshot("snap2");
        snapshot2.addDirectory("F1", FILES_ROOT + "permissions/sub-dir2");
        snapshot2.takeSnapshot();
        snapshot1.compare(snapshot2);
        // globally enable checking the file permissions
        configurator.setFileSnapshotCheckFilePermissions(true);
        // TEST2: Now we will do the check and error must be registered
        FileSystemSnapshot snapshot3 = new FileSystemSnapshot("snap1");
        snapshot3.addDirectory("F1", FILES_ROOT + "permissions/sub-dir1");
        snapshot3.takeSnapshot();
        FileSystemSnapshot snapshot4 = new FileSystemSnapshot("snap2");
        snapshot4.addDirectory("F1", FILES_ROOT + "permissions/sub-dir2");
        snapshot4.takeSnapshot();
        try {
            // expected exception - file permissions difference
            snapshot3.compare(snapshot4);
            // log permissions
            LocalFileSystemOperations lfs = new LocalFileSystemOperations();
            System.err.println("Snapshots compare is expected to fail. Permissions dump:");
            System.err.println("Permissions for " + firstFile + ": " + lfs.getFilePermissions(firstFile));
            System.err.println("Permissions for " + secondFile + ": " + lfs.getFilePermissions(secondFile));
            thisShouldNotBeReached();
        } catch (FileSystemSnapshotException se) {
            verifyError(se, ".*Permissions: .*");
        }
    } finally {
        // restore the original permissions
        new LocalFileSystemOperations().setFilePermissions(firstFile, firstPermissions);
        new LocalFileSystemOperations().setFilePermissions(secondFile, secondPermissions);
    }
}
Also used : LocalFileSystemOperations(com.axway.ats.core.filesystem.LocalFileSystemOperations) LocalSystemOperations(com.axway.ats.core.system.LocalSystemOperations) FileSystemSnapshotException(com.axway.ats.common.filesystem.snapshot.FileSystemSnapshotException) BaseTest(com.axway.ats.action.BaseTest) Test(org.junit.Test)

Aggregations

BaseTest (com.axway.ats.action.BaseTest)1 FileSystemSnapshotException (com.axway.ats.common.filesystem.snapshot.FileSystemSnapshotException)1 LocalFileSystemOperations (com.axway.ats.core.filesystem.LocalFileSystemOperations)1 LocalSystemOperations (com.axway.ats.core.system.LocalSystemOperations)1 Test (org.junit.Test)1