diff options
Diffstat (limited to 'libraries/sts-common-util/host-side/tests/src/com/android/sts/common')
4 files changed, 307 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); + } } diff --git a/libraries/sts-common-util/host-side/tests/src/com/android/sts/common/ProcessUtilTest.java b/libraries/sts-common-util/host-side/tests/src/com/android/sts/common/ProcessUtilTest.java new file mode 100644 index 000000000..c6f4c6963 --- /dev/null +++ b/libraries/sts-common-util/host-side/tests/src/com/android/sts/common/ProcessUtilTest.java @@ -0,0 +1,90 @@ +/* + * Copyright (C) 2023 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.android.sts.common; + +import static com.google.common.truth.Truth.assertWithMessage; + +import static org.junit.Assert.assertTrue; + +import com.android.tradefed.device.IFileEntry; +import com.android.tradefed.testtype.DeviceJUnit4ClassRunner; +import com.android.tradefed.testtype.junit4.BaseHostJUnit4Test; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; + +import java.util.Optional; +import java.util.regex.Pattern; + +/** Unit tests for {@link ProcessUtil}. */ +@RunWith(DeviceJUnit4ClassRunner.class) +public class ProcessUtilTest extends BaseHostJUnit4Test { + + @Before + public void setUp() throws Exception { + assertTrue("could not unroot", getDevice().disableAdbRoot()); + } + + @After + public void tearDown() throws Exception { + assertTrue("could not unroot", getDevice().disableAdbRoot()); + } + + @Test(expected = IllegalStateException.class) + public void testFindLoadedByProcessNonRoot() throws Exception { + // expect failure because the shell user has no permission to read process info of other + // users + ProcessUtil.findFileLoadedByProcess( + getDevice(), "system_server", Pattern.compile(Pattern.quote("libc.so"))); + } + + @Test(expected = IllegalArgumentException.class) + public void testFindLoadedByProcessMultipleProcesses() throws Exception { + // pattern 'android' has multiple (android.hardware.drm, android.hardware.gnss, etc) + ProcessUtil.findFileLoadedByProcess(getDevice(), "android", null); + } + + @Test + public void testFindLoadedByProcessUtilRoot() throws Exception { + assertTrue("must test with rootable device", getDevice().enableAdbRoot()); + Optional<IFileEntry> fileEntryOptional = + ProcessUtil.findFileLoadedByProcess( + getDevice(), "system_server", Pattern.compile(Pattern.quote("libc.so"))); + assertWithMessage("file entry should not be empty") + .that(fileEntryOptional.isPresent()) + .isTrue(); + IFileEntry fileEntry = fileEntryOptional.get(); + assertWithMessage("file entry should be a path to libc.so") + .that(fileEntry.getFullPath()) + .contains("libc.so"); + } + + @Test + public void testFindLoadedByProcessUtilNoMatch() throws Exception { + assertTrue("must test with rootable device", getDevice().enableAdbRoot()); + Optional<IFileEntry> fileEntryOptional = + ProcessUtil.findFileLoadedByProcess( + getDevice(), + "system_server", + Pattern.compile(Pattern.quote("doesnotexist.foobar"))); + assertWithMessage("file entry should be empty if no matches") + .that(fileEntryOptional.isPresent()) + .isFalse(); + } +} diff --git a/libraries/sts-common-util/host-side/tests/src/com/android/sts/common/UserUtilsTest.java b/libraries/sts-common-util/host-side/tests/src/com/android/sts/common/UserUtilsTest.java new file mode 100644 index 000000000..aa58f1022 --- /dev/null +++ b/libraries/sts-common-util/host-side/tests/src/com/android/sts/common/UserUtilsTest.java @@ -0,0 +1,99 @@ +/* + * Copyright (C) 2023 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +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.Test; +import org.junit.runner.RunWith; + +import java.util.Map; + +/** Unit tests for {@link UserUtils}. */ +@RunWith(DeviceJUnit4ClassRunner.class) +public class UserUtilsTest extends BaseHostJUnit4Test { + private static final String TEST_USER_NAME = "TestUserForUserUtils"; + private static final String CMD_PM_LIST_USERS = "pm list users"; + + @Before + public void setUp() throws Exception { + assertWithMessage("device already has the test user") + .that(CommandUtil.runAndCheck(getDevice(), CMD_PM_LIST_USERS).getStdout()) + .doesNotContain(TEST_USER_NAME); + } + + @After + public void tearDown() throws Exception { + assertWithMessage("did not clean up the test user") + .that(CommandUtil.runAndCheck(getDevice(), CMD_PM_LIST_USERS).getStdout()) + .doesNotContain(TEST_USER_NAME); + } + + @Test + public void testUserUtilsNonRoot() throws Exception { + assertTrue(getDevice().disableAdbRoot()); + try (AutoCloseable user = + new UserUtils.SecondaryUser(getDevice()).name(TEST_USER_NAME).withUser()) { + assertFalse( + "device should not implicitly root to create a user", getDevice().isAdbRoot()); + assertWithMessage("did not create the test user") + .that(CommandUtil.runAndCheck(getDevice(), CMD_PM_LIST_USERS).getStdout()) + .contains(TEST_USER_NAME); + } + assertFalse("device should not implicitly root to cleanup", getDevice().isAdbRoot()); + } + + @Test + public void testUserUtilsRoot() throws Exception { + assertTrue("must test with rootable device", getDevice().enableAdbRoot()); + try (AutoCloseable user = + new UserUtils.SecondaryUser(getDevice()).name(TEST_USER_NAME).withUser()) { + assertTrue( + "device should still be root after user creation if started with root", + getDevice().isAdbRoot()); + assertWithMessage("did not create the test user") + .that(CommandUtil.runAndCheck(getDevice(), CMD_PM_LIST_USERS).getStdout()) + .contains(TEST_USER_NAME); + } + assertTrue( + "device should still be root after cleanup if started with root", + getDevice().isAdbRoot()); + } + + @Test + public void testUserUtilsUserRestriction() throws Exception { + assertTrue("must test with rootable device", getDevice().enableAdbRoot()); + try (AutoCloseable user = + new UserUtils.SecondaryUser(getDevice()) + .name(TEST_USER_NAME) + .withUserRestrictions(Map.of("test_restriction", "1")) + .withUser()) { + // Exception is thrown if any error occurs while setting user restriction above + } + assertTrue( + "device should still be root after cleanup if started with root", + getDevice().isAdbRoot()); + } +} diff --git a/libraries/sts-common-util/host-side/tests/src/com/android/sts/common/util/KernelVersionHostTest.java b/libraries/sts-common-util/host-side/tests/src/com/android/sts/common/util/KernelVersionHostTest.java new file mode 100644 index 000000000..3a4603978 --- /dev/null +++ b/libraries/sts-common-util/host-side/tests/src/com/android/sts/common/util/KernelVersionHostTest.java @@ -0,0 +1,32 @@ +/* + * Copyright (C) 2023 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.android.sts.common.util; + +import com.android.tradefed.testtype.DeviceJUnit4ClassRunner; +import com.android.tradefed.testtype.junit4.BaseHostJUnit4Test; + +import org.junit.Test; +import org.junit.runner.RunWith; + +@RunWith(DeviceJUnit4ClassRunner.class) +public class KernelVersionHostTest extends BaseHostJUnit4Test { + + @Test + public final void testGetKernelVersion() throws Exception { + KernelVersionHost.getKernelVersion(getDevice()); + } +} |