Make tests more granular

This commit is contained in:
topjohnwu
2025-05-13 00:31:21 -07:00
committed by John Wu
parent 4f4b1ff885
commit 7831f40691
2 changed files with 74 additions and 37 deletions

View File

@@ -7,6 +7,11 @@ import androidx.test.uiautomator.By
import androidx.test.uiautomator.Until import androidx.test.uiautomator.Until
import com.topjohnwu.magisk.core.model.module.LocalModule import com.topjohnwu.magisk.core.model.module.LocalModule
import com.topjohnwu.magisk.core.utils.RootUtils import com.topjohnwu.magisk.core.utils.RootUtils
import com.topjohnwu.magisk.test.Environment.Companion.EMPTY_ZYGISK
import com.topjohnwu.magisk.test.Environment.Companion.INVALID_ZYGISK
import com.topjohnwu.magisk.test.Environment.Companion.MOUNT_TEST
import com.topjohnwu.magisk.test.Environment.Companion.REMOVE_TEST
import com.topjohnwu.magisk.test.Environment.Companion.SEPOLICY_RULE
import com.topjohnwu.superuser.Shell import com.topjohnwu.superuser.Shell
import kotlinx.coroutines.runBlocking import kotlinx.coroutines.runBlocking
import org.junit.After import org.junit.After
@@ -50,8 +55,9 @@ class AdditionalTest : BaseTest {
@Test @Test
fun testModuleCount() { fun testModuleCount() {
var expected = 0 var expected = 2
if (Environment.testModules()) expected +=2 if (Environment.mount()) expected++
if (Environment.preinit()) expected++
if (Environment.lsposed()) expected++ if (Environment.lsposed()) expected++
if (Environment.shamiko()) expected++ if (Environment.shamiko()) expected++
assertEquals("Module count incorrect", expected, modules.size) assertEquals("Module count incorrect", expected, modules.size)
@@ -79,11 +85,10 @@ class AdditionalTest : BaseTest {
} }
@Test @Test
fun testModule01() { fun testModuleMount() {
assumeTrue(Environment.testModules()) assumeTrue(Environment.mount())
val module = modules.find { it.id == "test_01" } assertNotNull("$MOUNT_TEST is not installed", modules.find { it.id == MOUNT_TEST })
assertNotNull("test_01 is not installed", module)
assertTrue( assertTrue(
"/system/etc/newfile should exist", "/system/etc/newfile should exist",
RootUtils.fs.getFile("/system/etc/newfile").exists() RootUtils.fs.getFile("/system/etc/newfile").exists()
@@ -97,28 +102,37 @@ class AdditionalTest : BaseTest {
"/system/app/EasterEgg should be empty", "/system/app/EasterEgg should be empty",
egg.isEmpty() egg.isEmpty()
) )
}
@Test
fun testSepolicyRule() {
assumeTrue(Environment.preinit())
assertNotNull("$SEPOLICY_RULE is not installed", modules.find { it.id == SEPOLICY_RULE })
assertTrue( assertTrue(
"Module sepolicy.rule is not applied", "Module sepolicy.rule is not applied",
Shell.cmd("magiskpolicy --print-rules | grep -q magisk_test").exec().isSuccess Shell.cmd("magiskpolicy --print-rules | grep -q magisk_test").exec().isSuccess
) )
module!!
assertTrue("test_01 should be zygisk unloaded", module.zygiskUnloaded)
} }
@Test @Test
fun testModule02() { fun testEmptyZygiskModule() {
assumeTrue(Environment.testModules()) val module = modules.find { it.id == EMPTY_ZYGISK }
assertNotNull("$EMPTY_ZYGISK is not installed", module)
val module = modules.find { it.id == "test_02" }
assertNotNull("test_02 is not installed", module)
module!! module!!
assertTrue("test_02 should be zygisk unloaded", module.zygiskUnloaded) assertTrue("$EMPTY_ZYGISK should be zygisk unloaded", module.zygiskUnloaded)
} }
@Test @Test
fun testModule03() { fun testInvalidZygiskModule() {
assumeTrue(Environment.testModules()) val module = modules.find { it.id == INVALID_ZYGISK }
assertNotNull("$INVALID_ZYGISK is not installed", module)
module!!
assertTrue("$INVALID_ZYGISK should be zygisk unloaded", module.zygiskUnloaded)
}
assertNull("test_03 should be removed", modules.find { it.id == "test_03" }) @Test
fun testRemoveModule() {
assertNull("$REMOVE_TEST should be removed", modules.find { it.id == REMOVE_TEST })
} }
} }

View File

@@ -40,11 +40,16 @@ class Environment : BaseTest {
fun before() = BaseTest.prerequisite() fun before() = BaseTest.prerequisite()
// The kernel running on emulators < API 26 does not play well with // The kernel running on emulators < API 26 does not play well with
// magic mount. Skip module tests on those legacy platforms. // magic mount. Skip mount_test on those legacy platforms.
fun testModules(): Boolean { fun mount(): Boolean {
return Build.VERSION.SDK_INT >= 26 return Build.VERSION.SDK_INT >= 26
} }
// It is possible that there are no suitable preinit partition to use
fun preinit(): Boolean {
return Shell.cmd("magisk --preinit-device").exec().isSuccess
}
fun lsposed(): Boolean { fun lsposed(): Boolean {
return Build.VERSION.SDK_INT in 27..34 return Build.VERSION.SDK_INT in 27..34
} }
@@ -54,6 +59,11 @@ class Environment : BaseTest {
} }
private const val MODULE_ERROR = "Module zip processing incorrect" private const val MODULE_ERROR = "Module zip processing incorrect"
const val MOUNT_TEST = "mount_test"
const val SEPOLICY_RULE = "sepolicy_rule"
const val INVALID_ZYGISK = "invalid_zygisk"
const val REMOVE_TEST = "remove_test"
const val EMPTY_ZYGISK = "empty_zygisk"
} }
object TimberLog : CallbackList<String>(Runnable::run) { object TimberLog : CallbackList<String>(Runnable::run) {
@@ -84,9 +94,9 @@ class Environment : BaseTest {
} }
} }
private fun setupModule01(root: ExtendedFile) { private fun setupMountTest(root: ExtendedFile) {
val error = "test_01 setup failed" val error = "$MOUNT_TEST setup failed"
val path = root.getChildFile("test_01") val path = root.getChildFile(MOUNT_TEST)
// Create /system/etc/newfile // Create /system/etc/newfile
val etc = path.getChildFile("system").getChildFile("etc") val etc = path.getChildFile("system").getChildFile("etc")
@@ -103,9 +113,13 @@ class Environment : BaseTest {
assertTrue(error, bin.mkdirs()) assertTrue(error, bin.mkdirs())
assertTrue(error, Shell.cmd("mknod $bin/screenrecord c 0 0").exec().isSuccess) assertTrue(error, Shell.cmd("mknod $bin/screenrecord c 0 0").exec().isSuccess)
// Create an empty zygisk folder assertTrue(error, Shell.cmd("set_default_perm $path").exec().isSuccess)
val module = LocalModule(path) }
assertTrue(error, module.zygiskFolder.mkdir())
private fun setupSepolicyRuleModule(root: ExtendedFile) {
val error = "$SEPOLICY_RULE setup failed"
val path = root.getChildFile(SEPOLICY_RULE)
assertTrue(error, path.mkdirs())
// Add sepolicy patch // Add sepolicy patch
PrintStream(path.getChildFile("sepolicy.rule").newOutputStream()).use { PrintStream(path.getChildFile("sepolicy.rule").newOutputStream()).use {
@@ -118,9 +132,18 @@ class Environment : BaseTest {
).exec().isSuccess) ).exec().isSuccess)
} }
private fun setupModule02(root: ExtendedFile) { private fun setupEmptyZygiskModule(root: ExtendedFile) {
val error = "test_02 setup failed" val error = "$EMPTY_ZYGISK setup failed"
val path = root.getChildFile("test_02") val path = root.getChildFile(EMPTY_ZYGISK)
// Create an empty zygisk folder
val module = LocalModule(path)
assertTrue(error, module.zygiskFolder.mkdirs())
}
private fun setupInvalidZygiskModule(root: ExtendedFile) {
val error = "$INVALID_ZYGISK setup failed"
val path = root.getChildFile(INVALID_ZYGISK)
// Create invalid zygisk libraries // Create invalid zygisk libraries
val module = LocalModule(path) val module = LocalModule(path)
@@ -133,9 +156,9 @@ class Environment : BaseTest {
assertTrue(error, Shell.cmd("set_default_perm $path").exec().isSuccess) assertTrue(error, Shell.cmd("set_default_perm $path").exec().isSuccess)
} }
private fun setupModule03(root: ExtendedFile) { private fun setupRemoveModule(root: ExtendedFile) {
val error = "test_03 setup failed" val error = "$REMOVE_TEST setup failed"
val path = root.getChildFile("test_03") val path = root.getChildFile(REMOVE_TEST)
// Create a new module but mark is as "remove" // Create a new module but mark is as "remove"
val module = LocalModule(path) val module = LocalModule(path)
@@ -189,12 +212,12 @@ class Environment : BaseTest {
} }
} }
if (testModules()) { val root = RootUtils.fs.getFile(Const.MODULE_PATH)
val root = RootUtils.fs.getFile(Const.MODULE_PATH) if (mount()) { setupMountTest(root) }
setupModule01(root) if (preinit()) { setupSepolicyRuleModule(root) }
setupModule02(root) setupEmptyZygiskModule(root)
setupModule03(root) setupInvalidZygiskModule(root)
} setupRemoveModule(root)
} }
@Test @Test