forked from bazelbuild/bazel
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCompatDexBuilderTest.java
More file actions
197 lines (181 loc) · 8.43 KB
/
CompatDexBuilderTest.java
File metadata and controls
197 lines (181 loc) · 8.43 KB
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
187
188
189
190
191
192
193
194
195
196
197
// Copyright 2020 The Bazel Authors. All rights reserved.
//
// 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.google.devtools.build.android.r8;
import static com.google.common.truth.Truth.assertThat;
import static java.nio.charset.StandardCharsets.UTF_8;
import com.android.tools.r8.D8;
import com.android.tools.r8.D8Command;
import com.android.tools.r8.DiagnosticsHandler;
import com.android.tools.r8.OutputMode;
import com.google.common.cache.Cache;
import com.google.common.cache.CacheBuilder;
import com.google.common.collect.ImmutableList;
import com.google.devtools.common.options.OptionsParsingException;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;
import java.util.concurrent.ExecutionException;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
/** Test for CompatDexBuilder. */
@RunWith(JUnit4.class)
public class CompatDexBuilderTest {
@Rule public TemporaryFolder temp = new TemporaryFolder();
@Test
public void compileManyClasses()
throws IOException, InterruptedException, ExecutionException, OptionsParsingException {
// Random set of classes from the R8 example test directory naming001.
final String inputJar = System.getProperty("CompatDexBuilderTests.naming001");
final ImmutableList<String> classNames =
ImmutableList.of(
"A",
"B",
"C",
"D",
"E",
"F",
"G",
"H",
"I",
"J",
"K",
"L",
"Reflect2$A",
"Reflect2$B",
"Reflect2",
"Reflect");
// Run CompatDexBuilder on naming001.jar
Path outputZip = temp.getRoot().toPath().resolve("out.zip");
CompatDexBuilder.main(
new String[] {"--input_jar", inputJar, "--output_zip", outputZip.toString()});
assertThat(Files.exists(outputZip)).isTrue();
// Verify if all the classes have their corresponding ".class.dex" files in the zip.
Set<String> expectedNames = new HashSet<>();
for (String className : classNames) {
expectedNames.add(
"com/google/devtools/build/android/r8/testdata/naming001/" + className + ".class.dex");
}
try (ZipFile zipFile = new ZipFile(outputZip.toFile(), UTF_8)) {
zipFile.stream()
.forEach(
ze -> {
expectedNames.remove(ze.getName());
});
}
assertThat(expectedNames).isEmpty();
}
@Test
public void compileWithSyntheticLambdas() throws Exception {
final String contextName = "com/google/devtools/build/android/r8/testdata/lambda/Lambda";
final String inputJar = System.getProperty("CompatDexBuilderTests.lambda");
final Path outputZip = temp.getRoot().toPath().resolve("out.zip");
CompatDexBuilder.main(
new String[] {"--input_jar", inputJar, "--output_zip", outputZip.toString()});
assertThat(Files.exists(outputZip)).isTrue();
try (ZipFile zipFile = new ZipFile(outputZip.toFile(), UTF_8)) {
assertThat(zipFile.getEntry(contextName + ".class.dex")).isNotNull();
ZipEntry entry = zipFile.getEntry("META-INF/synthetic-contexts.map");
assertThat(entry).isNotNull();
try (BufferedReader reader =
new BufferedReader(new InputStreamReader(zipFile.getInputStream(entry), UTF_8))) {
String line = reader.readLine();
assertThat(line).isNotNull();
// Format of mapping is: <synthetic-binary-name>;<context-binary-name>\n
int sep = line.indexOf(';');
String syntheticNameInMap = line.substring(0, sep);
String contextNameInMap = line.substring(sep + 1);
// The synthetic will be prefixed by the context type. This checks the synthetic name
// is larger than the context to avoid hardcoding the synthetic names, which may change.
assertThat(syntheticNameInMap).startsWith(contextName);
assertThat(syntheticNameInMap).isNotEqualTo(contextName);
// Check expected context.
assertThat(contextNameInMap).isEqualTo(contextName);
// Only one synthetic and its context should be present.
line = reader.readLine();
assertThat(line).isNull();
}
}
}
@Test
public void compileWithCachedSyntheticLambdas() throws Exception {
// Test synthetic context information is cached alongside dexed classes
CompatDexBuilder compatDexBuilder = new CompatDexBuilder();
Cache<CompatDexBuilder.DexingKeyR8, CompatDexBuilder.DexingEntryR8> dexCache =
CacheBuilder.newBuilder().build();
DiagnosticsHandler diagnostics = new DiagnosticsHandler() {};
PrintWriter pw = new PrintWriter(System.err);
final String contextName = "com/google/devtools/build/android/r8/testdata/lambda/Lambda";
final String inputJar = System.getProperty("CompatDexBuilderTests.lambda");
final Path outputZipA = temp.getRoot().toPath().resolve("outA.zip");
final Path outputZipB = temp.getRoot().toPath().resolve("outB.zip");
String[] args = new String[] {"--input_jar", inputJar, "--output_zip", outputZipA.toString()};
compatDexBuilder.processRequest(dexCache, diagnostics, Arrays.asList(args), pw);
args = new String[] {"--input_jar", inputJar, "--output_zip", outputZipB.toString()};
compatDexBuilder.processRequest(dexCache, diagnostics, Arrays.asList(args), pw);
Path[] outputZips = new Path[] {outputZipA, outputZipB};
for (Path outputZip: outputZips) {
try (ZipFile zipFile = new ZipFile(outputZip.toFile(), UTF_8)) {
assertThat(zipFile.getEntry(contextName + ".class.dex")).isNotNull();
ZipEntry entry = zipFile.getEntry("META-INF/synthetic-contexts.map");
assertThat(entry).isNotNull();
try (BufferedReader reader =
new BufferedReader(new InputStreamReader(zipFile.getInputStream(entry), UTF_8))) {
String line = reader.readLine();
assertThat(line).isNotNull();
// Format of mapping is: <synthetic-binary-name>;<context-binary-name>\n
int sep = line.indexOf(';');
String syntheticNameInMap = line.substring(0, sep);
String contextNameInMap = line.substring(sep + 1);
// The synthetic will be prefixed by the context type. This checks the synthetic name
// is larger than the context to avoid hardcoding the synthetic names, which may change.
assertThat(syntheticNameInMap).startsWith(contextName);
assertThat(syntheticNameInMap).isNotEqualTo(contextName);
// Check expected context.
assertThat(contextNameInMap).isEqualTo(contextName);
// Only one synthetic and its context should be present.
line = reader.readLine();
assertThat(line).isNull();
}
}
}
}
@Test
public void compileTwoClassesAndRun() throws Exception {
// Run CompatDexBuilder on dexMergeSample.jar
final String inputJar = System.getProperty("CompatDexBuilderTests.twosimpleclasses");
Path outputZip = temp.getRoot().toPath().resolve("out.zip");
CompatDexBuilder.main(
new String[] {"--input_jar", inputJar, "--output_zip", outputZip.toString()});
// Merge zip content into a single dex file.
Path d8OutDir = temp.newFolder().toPath();
D8.run(
D8Command.builder()
.setOutput(d8OutDir, OutputMode.DexIndexed)
.addProgramFiles(outputZip)
.build());
// TODO(sgjesse): Validate by running methods of Class1 and Class2.
// https://r8.googlesource.com/r8/+/5ee92486c896b918efb62e69bff5dfa79f30e7c2/src/test/java/com/android/tools/r8/compatdexbuilder/CompatDexBuilderTests.java#95
}
}