summaryrefslogtreecommitdiff
path: root/hostsidetests/edi/src/android/edi/cts/ClasspathDeviceInfo.java
blob: 2785ab2b3987f63ceeda0fc212f41f1efaac69ea (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
/*
 * Copyright (C) 2017 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 android.edi.cts;

import static android.compat.testing.Classpaths.ClasspathType.BOOTCLASSPATH;
import static android.compat.testing.Classpaths.ClasspathType.SYSTEMSERVERCLASSPATH;

import static com.google.common.truth.Truth.assertThat;
import static com.google.common.truth.Truth.assertWithMessage;

import android.compat.testing.Classpaths;
import android.compat.testing.Classpaths.ClasspathType;

import com.android.compatibility.common.util.DeviceInfo;
import com.android.compatibility.common.util.HostInfoStore;
import com.android.tradefed.device.ITestDevice;
import com.android.tradefed.log.LogUtil.CLog;

import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableSet;

import org.jf.dexlib2.iface.ClassDef;

/**
 * Collects information about Java classes present in *CLASSPATH variables and Java shared libraries
 * from the device.
 */
public class ClasspathDeviceInfo extends DeviceInfo {

    private static final String HELPER_APP_PACKAGE = "android.edi.cts.app";
    private static final String HELPER_APP_CLASS = HELPER_APP_PACKAGE + ".ClasspathDeviceTest";

    private ITestDevice mDevice;

    @Override
    protected void collectDeviceInfo(HostInfoStore store) throws Exception {
        mDevice = getDevice();

        store.startArray("jars");
        collectClasspathsJars(store);
        collectSharedLibraryJars(store);
        store.endArray();
    }

    private void collectClasspathsJars(HostInfoStore store) throws Exception {
        collectClasspathJarInfo(store, BOOTCLASSPATH);
        collectClasspathJarInfo(store, SYSTEMSERVERCLASSPATH);
        // No need to collect DEX2OATBOOTCLASSPATH, as it is just a subset of BOOTCLASSPATH
    }

    private void collectClasspathJarInfo(HostInfoStore store, ClasspathType classpath)
            throws Exception {
        ImmutableList<String> paths = Classpaths.getJarsOnClasspath(mDevice, classpath);
        for (int i = 0; i < paths.size(); i++) {
            store.startGroup();
            store.addResult("classpath", classpath.name());
            store.addResult("path", paths.get(i));
            store.addResult("index", i);
            collectClassInfo(store, paths.get(i));
            store.endGroup();
        }
    }

    private void collectSharedLibraryJars(HostInfoStore store) throws Exception {
        // Trigger helper app to collect and write info about shared libraries on the device.
        assertThat(runDeviceTests(HELPER_APP_PACKAGE, HELPER_APP_CLASS)).isTrue();

        String remoteFile = "/sdcard/shared-libs.txt";
        String content;
        try {
            content = mDevice.pullFileContents(remoteFile);
        } finally {
            mDevice.deleteFile(remoteFile);
        }

        for (String line : content.split("\n")) {
            String[] words = line.split(" ");
            assertWithMessage(
                    "expected each line to be in the format: <name> <type> <version> <path>...")
                    .that(words.length)
                    .isAtLeast(4);
            String libraryName = words[0];
            String libraryType = words[1];
            String libraryVersion = words[2];
            for (int i = 3; i < words.length; i++) {
                String path = words[i];
                if (!mDevice.doesFileExist(path)) {
                    CLog.w("Shared library is not present on device " + path);
                    continue;
                }

                store.startGroup();
                store.startGroup("shared_library");
                store.addResult("name", libraryName);
                store.addResult("type", libraryType);
                store.addResult("version", libraryVersion);
                store.endGroup(); // shared_library
                store.addResult("path", path);
                store.addResult("index", i - 3); // minus <name> <type> <version>
                collectClassInfo(store, path);
                store.endGroup();
            }
        }
    }

    private void collectClassInfo(HostInfoStore store, String path) throws Exception {
        store.startArray("classes");
        for (ClassDef classDef : Classpaths.getClassDefsFromJar(mDevice, path)) {
            store.startGroup();
            store.addResult("name", classDef.getType());
            store.endGroup();
        }
        store.endArray();
    }
}