aboutsummaryrefslogtreecommitdiff
path: root/javatests/com/android/modules/conformanceframework/DuplicateClassesTest.java
blob: f10cc526325840b998f91014be93cb12840fa6e9 (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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
/*
 * Copyright (C) 2022 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.modules.conformanceframework;


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

import static org.junit.Assert.assertNotNull;
import static org.junit.Assume.assumeTrue;

import com.android.modules.proto.ClasspathClasses.ClasspathClassesDump;
import com.android.modules.proto.ClasspathClasses.ClasspathEntry;
import com.android.modules.proto.ClasspathClasses.Jar;
import com.android.modules.targetprep.ClasspathFetcher;
import com.android.modules.utils.build.testing.DeviceSdkLevel;
import com.android.tradefed.config.Option;
import com.android.tradefed.device.DeviceNotAvailableException;
import com.android.tradefed.device.ITestDevice;
import com.android.tradefed.invoker.TestInformation;
import com.android.tradefed.testtype.DeviceJUnit4ClassRunner;
import com.android.tradefed.testtype.junit4.BaseHostJUnit4Test;
import com.android.tradefed.testtype.junit4.BeforeClassWithInfo;
import com.android.tradefed.testtype.junit4.DeviceTestRunOptions;

import com.google.common.collect.HashMultimap;
import com.google.common.collect.ImmutableCollection;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableMultimap;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.ImmutableSetMultimap;
import com.google.common.collect.Multimap;
import com.google.common.collect.Multimaps;

import org.jf.dexlib2.iface.ClassDef;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Arrays;
import java.util.Collection;
import java.util.Objects;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.Stream;



/**
 * Tests for detecting no duplicate class files are present on BOOTCLASSPATH and
 * SYSTEMSERVERCLASSPATH.
 *
 * <p>Duplicate class files are not safe as some of the jars on *CLASSPATH are updated outside of
 * the main dessert release cycle; they also contribute to unnecessary disk space usage.
 */
@RunWith(DeviceJUnit4ClassRunner.class)
public class DuplicateClassesTest extends BaseHostJUnit4Test {
    private static ImmutableSet<String> sBootclasspathJars;
    private static ImmutableSet<String> sSystemserverclasspathJars;

    private static ImmutableMultimap<String, String> sJarsToClasses;
    private static String sApexPackage;

    private DeviceSdkLevel mDeviceSdkLevel;

    /**
     * Fetch all classpath info extracted by ClasspathFetcher.
     *
     */
    @BeforeClassWithInfo
    public static void setupOnce(TestInformation testInfo) throws Exception {
        final String dctArtifactsPath = Objects.requireNonNull(
                testInfo.properties().get(ClasspathFetcher.DEVICE_JAR_ARTIFACTS_TAG));
        sApexPackage = testInfo.properties().get(ClasspathFetcher.APEX_PKG_TAG);
        final ImmutableMultimap.Builder<String, String> jarsToClasses =
                new ImmutableMultimap.Builder<>();
        final File bcpDumpFile = new File(dctArtifactsPath, ClasspathFetcher.BCP_CLASSES_FILE);
        final ClasspathClassesDump bcpDump =
                ClasspathClassesDump.parseFrom(new FileInputStream(bcpDumpFile));
        sBootclasspathJars = bcpDump.getEntriesList().stream()
            .map(entry -> entry.getJar().getPath())
            .collect(ImmutableSet.toImmutableSet());
        bcpDump.getEntriesList().stream()
            .forEach(entry -> {
                jarsToClasses.putAll(entry.getJar().getPath(), entry.getClassesList());
            });
        final File sscpDumpFile = new File(dctArtifactsPath, ClasspathFetcher.SSCP_CLASSES_FILE);
        final ClasspathClassesDump sscpDump =
                ClasspathClassesDump.parseFrom(new FileInputStream(sscpDumpFile));
        sSystemserverclasspathJars = sscpDump.getEntriesList().stream()
            .map(entry -> entry.getJar().getPath())
            .collect(ImmutableSet.toImmutableSet());
            sscpDump.getEntriesList().stream()
            .forEach(entry -> {
                jarsToClasses.putAll(entry.getJar().getPath(), entry.getClassesList());
            });
        sJarsToClasses = jarsToClasses.build();
    }

    @Before
    public void setup() {
        mDeviceSdkLevel = new DeviceSdkLevel(getDevice());
    }

    /**
     * Ensure that there are no duplicate classes among jars listed in BOOTCLASSPATH.
     */
    @Test
    public void testBootclasspath_nonDuplicateClasses() throws Exception {
        assumeTrue(mDeviceSdkLevel.isDeviceAtLeastR());
        assertThat(getDuplicateClasses(sBootclasspathJars)).isEmpty();
    }

    /**
     * Ensure that there are no duplicate classes among jars listed in SYSTEMSERVERCLASSPATH.
     */
    @Test
    public void testSystemserverClasspath_nonDuplicateClasses() throws Exception {
        assumeTrue(mDeviceSdkLevel.isDeviceAtLeastR());
        assertThat(getDuplicateClasses(sSystemserverclasspathJars)).isEmpty();
    }

    /**
     * Ensure that there are no duplicate classes among jars listed in BOOTCLASSPATH and
     * SYSTEMSERVERCLASSPATH.
     */
    @Test
    public void testSystemserverAndBootClasspath_nonDuplicateClasses() throws Exception {
        assumeTrue(mDeviceSdkLevel.isDeviceAtLeastR());
        final ImmutableSet.Builder<String> jars = new ImmutableSet.Builder<>();
        jars.addAll(sBootclasspathJars);
        jars.addAll(sSystemserverclasspathJars);
        assertThat(getDuplicateClasses(jars.build())).isEmpty();
    }

    /**
     * Gets the duplicate classes within a list of jar files.
     *
     * @param jars a list of jar files.
     * @return a multimap with the class name as a key and the jar files as a value.
     */
    private Multimap<String, String> getDuplicateClasses(ImmutableCollection<String> jars) {
        final HashMultimap<String, String> allClasses = HashMultimap.create();
        Multimaps.invertFrom(Multimaps.filterKeys(sJarsToClasses, jars::contains), allClasses);
        return Multimaps.filterKeys(allClasses, key -> validDuplicates(allClasses.get(key)));
    }

    /**
     * Filtering function for excluding invalid / uninteresting duplicates.
     *
     * This will filter out classes that are in only 1 jar, or duplicates that
     * do not include jars in the apex under test.
     */

    private boolean validDuplicates(Collection<String> duplicateJars) {
        if (duplicateJars.size() <= 1) {
            return false;
        }
        if (sApexPackage.equals(ClasspathFetcher.PLATFORM_PACKAGE)) {
            return duplicateJars.stream()
                .anyMatch(jar -> !jar.startsWith("/apex"));
        }
        final String apexPrefix = "/apex/" + sApexPackage;
        return duplicateJars.stream()
            .anyMatch(jar -> jar.startsWith(apexPrefix));

    }
}