summaryrefslogtreecommitdiff
path: root/libraries/sts-common-util/host-side/tests/src/com/android/sts/common/MallocDebugTest.java
diff options
context:
space:
mode:
Diffstat (limited to 'libraries/sts-common-util/host-side/tests/src/com/android/sts/common/MallocDebugTest.java')
-rw-r--r--libraries/sts-common-util/host-side/tests/src/com/android/sts/common/MallocDebugTest.java86
1 files changed, 86 insertions, 0 deletions
diff --git a/libraries/sts-common-util/host-side/tests/src/com/android/sts/common/MallocDebugTest.java b/libraries/sts-common-util/host-side/tests/src/com/android/sts/common/MallocDebugTest.java
index 03b19341d..f82a90756 100644
--- a/libraries/sts-common-util/host-side/tests/src/com/android/sts/common/MallocDebugTest.java
+++ b/libraries/sts-common-util/host-side/tests/src/com/android/sts/common/MallocDebugTest.java
@@ -16,9 +16,16 @@
package com.android.sts.common;
+import static com.google.common.truth.Truth.assertWithMessage;
+
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertTrue;
+
import com.android.tradefed.testtype.DeviceJUnit4ClassRunner;
import com.android.tradefed.testtype.junit4.BaseHostJUnit4Test;
+import org.junit.After;
+import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;
@@ -45,6 +52,26 @@ public class MallocDebugTest extends BaseHostJUnit4Test {
}
}
+ @Before
+ public void setUp() throws Exception {
+ assertWithMessage("libc.debug.malloc.options not empty before test")
+ .that(getDevice().getProperty("libc.debug.malloc.options"))
+ .isNull();
+ assertWithMessage("libc.debug.malloc.programs not empty before test")
+ .that(getDevice().getProperty("libc.debug.malloc.programs"))
+ .isNull();
+ }
+
+ @After
+ public void tearDown() throws Exception {
+ assertWithMessage("libc.debug.malloc.options not empty after test")
+ .that(getDevice().getProperty("libc.debug.malloc.options"))
+ .isNull();
+ assertWithMessage("libc.debug.malloc.programs not empty after test")
+ .that(getDevice().getProperty("libc.debug.malloc.programs"))
+ .isNull();
+ }
+
@Test(expected = Test.None.class /* no exception expected */)
public void testMallocDebugNoErrors() throws Exception {
MallocDebug.assertNoMallocDebugErrors(logcatWithoutErrors);
@@ -54,4 +81,63 @@ public class MallocDebugTest extends BaseHostJUnit4Test {
public void testMallocDebugWithErrors() throws Exception {
MallocDebug.assertNoMallocDebugErrors(logcatWithErrors);
}
+
+ @Test(expected = IllegalStateException.class)
+ public void testMallocDebugAutocloseableNonRoot() throws Exception {
+ assertTrue(getDevice().disableAdbRoot());
+ try (AutoCloseable mallocDebug =
+ MallocDebug.withLibcMallocDebugOnNewProcess(
+ getDevice(), "backtrace guard", "native-poc")) {
+ // empty
+ }
+ }
+
+ @Test
+ public void testMallocDebugAutocloseableRoot() throws Exception {
+ assertTrue("must test with rootable device", getDevice().enableAdbRoot());
+ try (AutoCloseable mallocDebug =
+ MallocDebug.withLibcMallocDebugOnNewProcess(
+ getDevice(), "backtrace guard", "native-poc")) {
+ // empty
+ }
+ }
+
+ @Test
+ public void testMallocDebugAutocloseableNonRootCleanup() throws Exception {
+ assertTrue("must test with rootable device", getDevice().enableAdbRoot());
+ try (AutoCloseable mallocDebug =
+ MallocDebug.withLibcMallocDebugOnNewProcess(
+ getDevice(), "backtrace guard", "native-poc")) {
+ assertTrue("could not disable root", getDevice().disableAdbRoot());
+ }
+ assertFalse(
+ "device should not be root after autoclose if the body unrooted",
+ getDevice().isAdbRoot());
+ }
+
+ @Test
+ public void testMallocDebugAutoseablePriorValueNoException() throws Exception {
+ assertTrue("must test with rootable device", getDevice().enableAdbRoot());
+ final String oldValue = "TEST_VALUE_OLD";
+ final String newValue = "TEST_VALUE_NEW";
+ assertTrue(
+ "could not set libc.debug.malloc.options",
+ getDevice().setProperty("libc.debug.malloc.options", oldValue));
+ assertWithMessage("test property was not properly set on device")
+ .that(getDevice().getProperty("libc.debug.malloc.options"))
+ .isEqualTo(oldValue);
+ try (AutoCloseable mallocDebug =
+ MallocDebug.withLibcMallocDebugOnNewProcess(getDevice(), newValue, "native-poc")) {
+ assertWithMessage("new property was not set during malloc debug body")
+ .that(getDevice().getProperty("libc.debug.malloc.options"))
+ .isEqualTo(newValue);
+ }
+ String afterValue = getDevice().getProperty("libc.debug.malloc.options");
+ assertTrue(
+ "could not clear libc.debug.malloc.options",
+ getDevice().setProperty("libc.debug.malloc.options", ""));
+ assertWithMessage("prior property was not restored after test")
+ .that(afterValue)
+ .isEqualTo(oldValue);
+ }
}