CTS/GTS 问题分析11
gts case是不放出源码的,这很不方便我们进行问题的分析,如果测试是apk,我们可以通过修改smali重新编译的方式赋值我们分析,那么,如果测试文件是一个jar包,那么应该如何处理呢?以本文为例记录分析方法
问题初探
测试命令:
run gts -m GtsUnofficialApisUsageTestCases -t com.android.gts.api.UnofficialApisUsageTest#testNonApiReferences
报错堆栈:
device log:
12-07 10:44:54.094 29217 29217 D TradefedEventsTag: ==================== com.android.gts.api.UnofficialApisUsageTest#testNonApiReferences STARTED: Fri Dec 07 10:44:53 CST 2018 ====================
12-07 10:44:54.710 29226 29226 D TradefedEventsTag: ==================== com.android.gts.api.UnofficialApisUsageTest#testNonApiReferences ENDED: Fri Dec 07 10:44:53 CST 2018 ====================
host log:
12-07 10:44:53 D/ModuleListener: ModuleListener.testStarted(com.android.gts.api.UnofficialApisUsageTest#testNonApiReferences)
12-07 10:44:53 I/ModuleListener: [1/1] com.android.gts.api.UnofficialApisUsageTest#testNonApiReferences fail:
java.lang.NullPointerException
at com.google.doclava.apicheck.ApiFile.parseApi(ApiFile.java:43)
at com.android.gts.api.ApprovedApis.parse(ApprovedApis.java:96)
at com.android.gts.api.ApprovedApis.lambda$new$0(ApprovedApis.java:50)
初始看除了报错堆栈外,毫无有效log,这条case写的不好;对于无源码的case而言,我认为google应该有义务将log写的详细点,不过这不是本文的重点;下面就要看看如何借这个问题
问题分析
首先通过jd-gui查看逻辑,结果关键地方均出现//INTERN ERROR//,属于jd-gui本身的问题,推荐一款看jar包代码的开源软件:Luyten
则可以看到代码:
@Test
public void testNonApiReferences() throws Exception {
if (!this.isTestRequired()) {
return;
}
Stream<PulledFile> pulledFiles;
try {
pulledFiles = Stream.concat((Stream<?>)this.getPackages(), (Stream<?>)this.getLibraries()).filter(module -> this.getRealPath(module.path).startsWith("/vendor")).map(module -> this.filePuller.pullFromDevice(module.path, module.name)).filter(file -> this.hasCode(file.fileInHost));
}
catch (DeviceNotAvailableException e) {
throw new RuntimeException("Cannot connect to device", (Throwable)e);
}
final Stream<File> apiFiles = Arrays.stream(new String[] { "/current.txt", "/system-current.txt", "/android-test-base-current.txt", "/android-test-runner-current.txt", "/android-test-mock-current.txt", "/android27.txt", "/org-apache-http-legacy-current.txt", "/org-apache-http-legacy-system-current.txt" }).map(name -> new File(name));
final ApprovedApis approvedApis = new ApprovedApis(apiFiles);
final DexAnalyzer extractedApis = new DexAnalyzer(pulledFiles, approvedApis);
final StringBuilder sb = new StringBuilder(10000);
extractedApis.collectUndefinedTypeReferences().sorted().forEach(ref -> sb.append("Undefined type ref: " + ref.getName() + " from: " + ref.printReferencedFrom() + "\n"));
extractedApis.collectUndefinedMethodReferences().sorted().forEach(ref -> sb.append("Undefined method ref: " + ref.getFullSignature() + " from: " + ref.printReferencedFrom() + "\n"));
extractedApis.collectUndefinedFieldReferences().sorted().forEach(ref -> sb.append("Undefined field ref: " + ref.getFullSignature() + " from: " + ref.printReferencedFrom() + "\n"));
if (sb.length() != 0) {
fail(sb.toString());
}
}
从报错堆栈上看,有一个文件解析不出来,因此case fail;
我们将jar包解压,jar -xvf GtsUnofficialApisUsageTestCases.jar,可以看到其包含的文件中没有org-apache-http-legacy-system-current.txt
手动创建一个空白文件org-apache-http-legacy-system-current.txt,重新编译jar包jar -cvf GtsUnofficialApisUsageTestCases.jar ./
再次测试,case可以pass,说明分析无误,属于case的问题</pre>
问题拓展
以上改的是jar包中的资源文件或者说是配置文件,如何在jar包中修改其代码,起码可以加log
我们在ApprovedApis.java中加log,将jar包中的class文件Save As ApprovedApis.java
将其放在刚刚解压的文件夹下(依赖路径),然后javac -classpath ./ ApprovedApis.java
报错
ApprovedApis1.java:10: error: class ApprovedApis is public, should be declared in a file named ApprovedApis.java
public class ApprovedApis
^
ApprovedApis1.java:21: error: incompatible types: invalid method reference
this.definedApiMethods.putAll(this.definedApiClasses.values().stream().flatMap(c -> c.getMethods().stream()).collect(Collectors.toMap((Function<? super Object, ? extends String>)DefinedMethod::getFullSignature, (Function<? super Object, ? extends DefinedMethod>)Function.identity())));
^
method getFullSignature in class DefinedMethod cannot be applied to given types
required: no arguments
found: Object
reason: actual and formal argument lists differ in length
将不正确的类型转换都去掉(注意,似乎不能加注释,只能直接改)
ApprovedApis.java:70: error: variable ex might not have been initialized
final Exception e = ex;
ex未赋初始值
ApprovedApis.java:51: error: local variables referenced from a lambda expression must be final or effectively final
c = this.definedApiClasses.get(classInfo.qualifiedName());
final对象先定义后赋值了,应该改为初次使用时直接赋值
依次将上面的报错修改完毕后,javac -classpath ./ ApprovedApis.java
在当前路径下生成ApprovedApis.class
替换解压出来的class文件,重新压缩,生成新的jar
可以看打出的log
gts-tf >
-------------------parse apiFile------------------path = /current.txt, name = current.txt
-------------------parse apiFile------------------path = /system-current.txt, name = system-current.txt
-------------------parse apiFile------------------path = /android-test-base-current.txt, name = android-test-base-current.txt
-------------------parse apiFile------------------path = /android-test-runner-current.txt, name = android-test-runner-current.txt
-------------------parse apiFile------------------path = /android-test-mock-current.txt, name = android-test-mock-current.txt
-------------------parse apiFile------------------path = /android27.txt, name = android27.txt
-------------------parse apiFile------------------path = /org-apache-http-legacy-current.txt, name = org-apache-http-legacy-current.txt
-------------------parse apiFile------------------path = /org-apache-http-legacy-system-current.txt, name = org-apache-http-legacy-system-current.txt
12-11 11:26:50 I/ModuleListener: [1/1] com.android.gts.api.UnofficialApisUsageTest#testNonApiReferences fail:
java.lang.NullPointerException
at com.google.doclava.apicheck.ApiFile.parseApi(ApiFile.java:43)
at com.android.gts.api.ApprovedApis.parse(ApprovedApis.java:54)
at com.android.gts.api.ApprovedApis.lambda$new$0(ApprovedApis.java:20)
at java.base/java.util.stream.ForEachOps$ForEachOp$OfRef.accept(ForEachOps.java:183)
可以看出是哪个文件出错了
问题总结
如何修改无源码,只有jar包的case;这个问题属于google的错误