@@ -8,18 +8,20 @@ import com.gh0u1l5.wechatmagician.spellbook.base.Version
8
8
import com.gh0u1l5.wechatmagician.spellbook.mirror.MirrorClasses
9
9
import com.gh0u1l5.wechatmagician.spellbook.mirror.MirrorFields
10
10
import com.gh0u1l5.wechatmagician.spellbook.mirror.MirrorMethods
11
+ import com.gh0u1l5.wechatmagician.spellbook.parser.ApkFile
11
12
import com.gh0u1l5.wechatmagician.spellbook.util.FileUtil
12
13
import com.gh0u1l5.wechatmagician.spellbook.util.MirrorUtil
13
14
import com.gh0u1l5.wechatmagician.spellbook.util.ReflectionUtil
14
15
import dalvik.system.PathClassLoader
15
- import net.dongliu.apk.parser.ApkFile
16
16
import org.junit.Assert.*
17
17
import org.junit.Before
18
18
import org.junit.Test
19
19
import org.junit.runner.RunWith
20
20
import java.io.File
21
21
import java.lang.ClassLoader.getSystemClassLoader
22
+ import kotlin.system.measureTimeMillis
22
23
24
+ @ExperimentalUnsignedTypes
23
25
@RunWith(AndroidJUnit4 ::class )
24
26
class MirrorUnitTest {
25
27
companion object {
@@ -34,8 +36,13 @@ class MirrorUnitTest {
34
36
}
35
37
36
38
private fun verifyPackage (apkPath : String ) {
37
- val cacheDir = context!! .cacheDir
39
+ // Parse the version of the apk
40
+ val regex = Regex (" wechat-v(.*)\\ .apk" )
41
+ val match = regex.find(apkPath) ? : throw Exception (" Unexpected path format" )
42
+ val version = match.groupValues[1 ]
38
43
44
+ // Store APK file to cache directory.
45
+ val cacheDir = context!! .cacheDir
39
46
val apkFile = File (cacheDir, apkPath)
40
47
try {
41
48
javaClass.classLoader!! .getResourceAsStream(apkPath).use {
@@ -46,25 +53,36 @@ class MirrorUnitTest {
46
53
return // ignore if the apk isn't accessible
47
54
}
48
55
56
+ // Ensure the apk is presented, and start the test
49
57
assertTrue(apkFile.exists())
50
58
ApkFile (apkFile).use {
59
+ // Benchmark the APK parser
60
+ val timeParseDex = measureTimeMillis { it.classTypes }
61
+ Log .d(" MirrorUnitTest" , " Benchmark: Parse DexClasses takes $timeParseDex ms." )
62
+
63
+ // Initialize WechatGlobal
51
64
WechatGlobal .wxUnitTestMode = true
52
- WechatGlobal .wxVersion = Version (it.apkMeta.versionName )
53
- WechatGlobal .wxPackageName = it.apkMeta.packageName
65
+ WechatGlobal .wxVersion = Version (version )
66
+ WechatGlobal .wxPackageName = " com.tencent.mm "
54
67
WechatGlobal .wxLoader = PathClassLoader (apkFile.absolutePath, getSystemClassLoader())
55
- WechatGlobal .wxClasses = it.dexClasses.map { clazz ->
56
- ReflectionUtil .ClassName (clazz.classType)
57
- }
68
+ WechatGlobal .wxClasses = it.classTypes
58
69
70
+ // Clear cached lazy evaluations
59
71
val objects = MirrorClasses + MirrorMethods + MirrorFields
60
72
ReflectionUtil .clearClassCache()
61
73
ReflectionUtil .clearMethodCache()
62
74
objects.forEach { instance ->
63
75
MirrorUtil .clearUnitTestLazyFields(instance)
64
76
}
65
77
66
- MirrorUtil .generateReportWithForceEval(objects).forEach {
67
- Log .d(" MirrorUnitTest" , " Verified ${it.first} -> ${it.second} " )
78
+ // Test each lazy evaluation and generate result.
79
+ var result: List <Pair <String , String >>? = null
80
+ val timeSearch = measureTimeMillis {
81
+ result = MirrorUtil .generateReportWithForceEval(objects)
82
+ }
83
+ Log .d(" MirrorUnitTest" , " Benchmark: Search over classes takes $timeSearch ms." )
84
+ result?.forEach { entry ->
85
+ Log .d(" MirrorUnitTest" , " Verified: ${entry.first} -> ${entry.second} " )
68
86
}
69
87
}
70
88
0 commit comments