Search in sources :

Example 1 with RecordingLogger

use of com.android.ide.common.res2.RecordingLogger in project android by JetBrains.

the class RenderSecurityManagerTest method testExec.

@Test
public void testExec() throws Exception {
    assertNull(RenderSecurityManager.getCurrent());
    RenderSecurityManager manager = new RenderSecurityManager(null, null);
    RecordingLogger logger = new RecordingLogger();
    manager.setLogger(logger);
    try {
        assertNull(RenderSecurityManager.getCurrent());
        manager.setActive(true, myCredential);
        assertSame(manager, RenderSecurityManager.getCurrent());
        if (new File("/bin/ls").exists()) {
            Runtime.getRuntime().exec("/bin/ls");
        } else {
            manager.checkExec("/bin/ls");
        }
        fail("Should have thrown security exception");
    } catch (SecurityException exception) {
        //noinspection ConstantConditions
        assertEquals(RenderSecurityManager.RESTRICT_READS ? "Read access not allowed during rendering (/bin/ls)" : "Exec access not allowed during rendering (/bin/ls)", exception.toString());
    // pass
    } finally {
        manager.dispose(myCredential);
        assertNull(RenderSecurityManager.getCurrent());
        assertNull(System.getSecurityManager());
        assertThat(logger.getWarningMsgs()).isEmpty();
    }
}
Also used : RecordingLogger(com.android.ide.common.res2.RecordingLogger) File(java.io.File) Test(org.junit.Test)

Example 2 with RecordingLogger

use of com.android.ide.common.res2.RecordingLogger in project android by JetBrains.

the class RenderSecurityManagerTest method testLogger.

@Test
public void testLogger() throws Exception {
    assertNull(RenderSecurityManager.getCurrent());
    final CyclicBarrier barrier1 = new CyclicBarrier(2);
    final CyclicBarrier barrier2 = new CyclicBarrier(2);
    final CyclicBarrier barrier3 = new CyclicBarrier(2);
    Thread thread = new Thread() {

        @Override
        public void run() {
            try {
                barrier1.await();
                barrier2.await();
                System.setSecurityManager(new SecurityManager() {

                    @Override
                    public String toString() {
                        return "MyTestSecurityManager";
                    }

                    @Override
                    public void checkPermission(Permission permission) {
                    }
                });
                barrier3.await();
                assertNull(RenderSecurityManager.getCurrent());
                assertNotNull(System.getSecurityManager());
                assertEquals("MyTestSecurityManager", System.getSecurityManager().toString());
            } catch (InterruptedException e) {
                fail(e.toString());
            } catch (BrokenBarrierException e) {
                fail(e.toString());
            }
        }
    };
    thread.start();
    RenderSecurityManager manager = new RenderSecurityManager(null, null);
    RecordingLogger logger = new RecordingLogger();
    manager.setLogger(logger);
    try {
        barrier1.await();
        assertNull(RenderSecurityManager.getCurrent());
        manager.setActive(true, myCredential);
        assertSame(manager, RenderSecurityManager.getCurrent());
        barrier2.await();
        barrier3.await();
        assertNull(RenderSecurityManager.getCurrent());
        manager.setActive(false, myCredential);
        assertNull(RenderSecurityManager.getCurrent());
        assertEquals(Collections.singletonList("RenderSecurityManager being replaced by another thread"), logger.getWarningMsgs());
    } catch (InterruptedException e) {
        fail(e.toString());
    } catch (BrokenBarrierException e) {
        fail(e.toString());
    } finally {
        manager.dispose(myCredential);
        assertNull(RenderSecurityManager.getCurrent());
        assertNotNull(System.getSecurityManager());
        assertEquals("MyTestSecurityManager", System.getSecurityManager().toString());
        System.setSecurityManager(null);
    }
}
Also used : BrokenBarrierException(java.util.concurrent.BrokenBarrierException) RecordingLogger(com.android.ide.common.res2.RecordingLogger) Permission(java.security.Permission) FilePermission(java.io.FilePermission) CyclicBarrier(java.util.concurrent.CyclicBarrier) Test(org.junit.Test)

Aggregations

RecordingLogger (com.android.ide.common.res2.RecordingLogger)2 Test (org.junit.Test)2 File (java.io.File)1 FilePermission (java.io.FilePermission)1 Permission (java.security.Permission)1 BrokenBarrierException (java.util.concurrent.BrokenBarrierException)1 CyclicBarrier (java.util.concurrent.CyclicBarrier)1