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();
}
}
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);
}
}
Aggregations