blob: 77cfff45c2513378922bcee02f717d0ae6e5a9ab [file] [log] [blame]
Liz Kammer5ca3a622020-08-05 15:40:41 -07001// Copyright 2020 Google Inc. All rights reserved.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15package java
16
17import (
Anton Hanssona2adc372020-07-03 15:31:32 +010018 "fmt"
Liz Kammer5ca3a622020-08-05 15:40:41 -070019 "strings"
20 "testing"
Anton Hanssona2adc372020-07-03 15:31:32 +010021
Paul Duffin01289a22021-02-04 17:49:33 +000022 "android/soong/android"
23
Anton Hanssona2adc372020-07-03 15:31:32 +010024 "github.com/google/blueprint/proptools"
Liz Kammer5ca3a622020-08-05 15:40:41 -070025)
26
Bill Peckhambae47492021-01-08 09:34:44 -080027func testConfigWithBootJars(bp string, bootJars []string, prebuiltHiddenApiDir *string) android.Config {
Liz Kammer5ca3a622020-08-05 15:40:41 -070028 config := testConfig(nil, bp, nil)
Paul Duffine10dfa42020-10-23 21:23:44 +010029 config.TestProductVariables.BootJars = android.CreateTestConfiguredJarList(bootJars)
Bill Peckhambae47492021-01-08 09:34:44 -080030 config.TestProductVariables.PrebuiltHiddenApiDir = prebuiltHiddenApiDir
Liz Kammer5ca3a622020-08-05 15:40:41 -070031 return config
32}
33
Colin Crossae8600b2020-10-29 17:09:13 -070034func testContextWithHiddenAPI(config android.Config) *android.TestContext {
35 ctx := testContext(config)
Paul Duffin01289a22021-02-04 17:49:33 +000036 RegisterHiddenApiSingletonComponents(ctx)
Liz Kammer5ca3a622020-08-05 15:40:41 -070037 return ctx
38}
39
Anton Hanssona2adc372020-07-03 15:31:32 +010040func testHiddenAPIWithConfig(t *testing.T, config android.Config) *android.TestContext {
Liz Kammer5ca3a622020-08-05 15:40:41 -070041 t.Helper()
42
Colin Crossae8600b2020-10-29 17:09:13 -070043 ctx := testContextWithHiddenAPI(config)
Liz Kammer5ca3a622020-08-05 15:40:41 -070044
45 run(t, ctx, config)
Anton Hanssona2adc372020-07-03 15:31:32 +010046 return ctx
47}
Liz Kammer5ca3a622020-08-05 15:40:41 -070048
Bill Peckhambae47492021-01-08 09:34:44 -080049func testHiddenAPIBootJars(t *testing.T, bp string, bootJars []string, prebuiltHiddenApiDir *string) (*android.TestContext, android.Config) {
50 config := testConfigWithBootJars(bp, bootJars, prebuiltHiddenApiDir)
Anton Hanssona2adc372020-07-03 15:31:32 +010051
52 return testHiddenAPIWithConfig(t, config), config
53}
54
55func testHiddenAPIUnbundled(t *testing.T, unbundled bool) (*android.TestContext, android.Config) {
56 config := testConfig(nil, ``, nil)
57 config.TestProductVariables.Always_use_prebuilt_sdks = proptools.BoolPtr(unbundled)
58
59 return testHiddenAPIWithConfig(t, config), config
Liz Kammer5ca3a622020-08-05 15:40:41 -070060}
61
62func TestHiddenAPISingleton(t *testing.T) {
Anton Hanssona2adc372020-07-03 15:31:32 +010063 ctx, _ := testHiddenAPIBootJars(t, `
Liz Kammer5ca3a622020-08-05 15:40:41 -070064 java_library {
65 name: "foo",
66 srcs: ["a.java"],
67 compile_dex: true,
Paul Duffin01289a22021-02-04 17:49:33 +000068 }
Paul Duffin9c3ac962021-02-03 14:11:27 +000069 `, []string{"platform:foo"}, nil)
Liz Kammer5ca3a622020-08-05 15:40:41 -070070
71 hiddenAPI := ctx.SingletonForTests("hiddenapi")
72 hiddenapiRule := hiddenAPI.Rule("hiddenapi")
73 want := "--boot-dex=" + buildDir + "/.intermediates/foo/android_common/aligned/foo.jar"
74 if !strings.Contains(hiddenapiRule.RuleParams.Command, want) {
75 t.Errorf("Expected %s in hiddenapi command, but it was not present: %s", want, hiddenapiRule.RuleParams.Command)
76 }
77}
78
Paul Duffin01289a22021-02-04 17:49:33 +000079func TestHiddenAPIIndexSingleton(t *testing.T) {
80 ctx, _ := testHiddenAPIBootJars(t, `
81 java_library {
82 name: "foo",
83 srcs: ["a.java"],
84 compile_dex: true,
85 }
86
87 java_import {
88 name: "foo",
89 jars: ["a.jar"],
90 compile_dex: true,
91 prefer: false,
92 }
93
94 java_sdk_library {
95 name: "bar",
96 srcs: ["a.java"],
97 compile_dex: true,
98 }
99 `, []string{"platform:foo", "platform:bar"}, nil)
100
101 hiddenAPIIndex := ctx.SingletonForTests("hiddenapi_index")
102 indexRule := hiddenAPIIndex.Rule("singleton-merged-hiddenapi-index")
Paul Duffin4fd997b2021-02-03 20:06:33 +0000103 CheckHiddenAPIRuleInputs(t, `
Paul Duffin01289a22021-02-04 17:49:33 +0000104.intermediates/bar/android_common/hiddenapi/index.csv
105.intermediates/foo/android_common/hiddenapi/index.csv
Paul Duffin01289a22021-02-04 17:49:33 +0000106`,
107 indexRule)
108}
109
Liz Kammer5ca3a622020-08-05 15:40:41 -0700110func TestHiddenAPISingletonWithPrebuilt(t *testing.T) {
Anton Hanssona2adc372020-07-03 15:31:32 +0100111 ctx, _ := testHiddenAPIBootJars(t, `
Liz Kammer5ca3a622020-08-05 15:40:41 -0700112 java_import {
113 name: "foo",
114 jars: ["a.jar"],
115 compile_dex: true,
116 }
Paul Duffin9c3ac962021-02-03 14:11:27 +0000117 `, []string{"platform:foo"}, nil)
Liz Kammer5ca3a622020-08-05 15:40:41 -0700118
119 hiddenAPI := ctx.SingletonForTests("hiddenapi")
120 hiddenapiRule := hiddenAPI.Rule("hiddenapi")
Bill Peckhamff89ffa2020-12-23 16:13:04 -0800121 want := "--boot-dex=" + buildDir + "/.intermediates/foo/android_common/aligned/foo.jar"
Liz Kammer5ca3a622020-08-05 15:40:41 -0700122 if !strings.Contains(hiddenapiRule.RuleParams.Command, want) {
123 t.Errorf("Expected %s in hiddenapi command, but it was not present: %s", want, hiddenapiRule.RuleParams.Command)
124 }
125}
126
127func TestHiddenAPISingletonWithPrebuiltUseSource(t *testing.T) {
Anton Hanssona2adc372020-07-03 15:31:32 +0100128 ctx, _ := testHiddenAPIBootJars(t, `
Liz Kammer5ca3a622020-08-05 15:40:41 -0700129 java_library {
130 name: "foo",
131 srcs: ["a.java"],
132 compile_dex: true,
Paul Duffin01289a22021-02-04 17:49:33 +0000133 }
Liz Kammer5ca3a622020-08-05 15:40:41 -0700134
135 java_import {
136 name: "foo",
137 jars: ["a.jar"],
138 compile_dex: true,
139 prefer: false,
Paul Duffin01289a22021-02-04 17:49:33 +0000140 }
Paul Duffin9c3ac962021-02-03 14:11:27 +0000141 `, []string{"platform:foo"}, nil)
Liz Kammer5ca3a622020-08-05 15:40:41 -0700142
143 hiddenAPI := ctx.SingletonForTests("hiddenapi")
144 hiddenapiRule := hiddenAPI.Rule("hiddenapi")
145 fromSourceJarArg := "--boot-dex=" + buildDir + "/.intermediates/foo/android_common/aligned/foo.jar"
146 if !strings.Contains(hiddenapiRule.RuleParams.Command, fromSourceJarArg) {
147 t.Errorf("Expected %s in hiddenapi command, but it was not present: %s", fromSourceJarArg, hiddenapiRule.RuleParams.Command)
148 }
149
150 prebuiltJarArg := "--boot-dex=" + buildDir + "/.intermediates/foo/android_common/dex/foo.jar"
151 if strings.Contains(hiddenapiRule.RuleParams.Command, prebuiltJarArg) {
152 t.Errorf("Did not expect %s in hiddenapi command, but it was present: %s", prebuiltJarArg, hiddenapiRule.RuleParams.Command)
153 }
154}
155
156func TestHiddenAPISingletonWithPrebuiltOverrideSource(t *testing.T) {
Anton Hanssona2adc372020-07-03 15:31:32 +0100157 ctx, _ := testHiddenAPIBootJars(t, `
Liz Kammer5ca3a622020-08-05 15:40:41 -0700158 java_library {
159 name: "foo",
160 srcs: ["a.java"],
161 compile_dex: true,
Paul Duffin01289a22021-02-04 17:49:33 +0000162 }
Liz Kammer5ca3a622020-08-05 15:40:41 -0700163
164 java_import {
165 name: "foo",
166 jars: ["a.jar"],
167 compile_dex: true,
168 prefer: true,
Paul Duffin01289a22021-02-04 17:49:33 +0000169 }
Paul Duffin9c3ac962021-02-03 14:11:27 +0000170 `, []string{"platform:foo"}, nil)
Liz Kammer5ca3a622020-08-05 15:40:41 -0700171
172 hiddenAPI := ctx.SingletonForTests("hiddenapi")
173 hiddenapiRule := hiddenAPI.Rule("hiddenapi")
174 prebuiltJarArg := "--boot-dex=" + buildDir + "/.intermediates/prebuilt_foo/android_common/dex/foo.jar"
175 if !strings.Contains(hiddenapiRule.RuleParams.Command, prebuiltJarArg) {
176 t.Errorf("Expected %s in hiddenapi command, but it was not present: %s", prebuiltJarArg, hiddenapiRule.RuleParams.Command)
177 }
178
179 fromSourceJarArg := "--boot-dex=" + buildDir + "/.intermediates/foo/android_common/aligned/foo.jar"
180 if strings.Contains(hiddenapiRule.RuleParams.Command, fromSourceJarArg) {
181 t.Errorf("Did not expect %s in hiddenapi command, but it was present: %s", fromSourceJarArg, hiddenapiRule.RuleParams.Command)
182 }
183}
Anton Hanssona2adc372020-07-03 15:31:32 +0100184
185func TestHiddenAPISingletonSdks(t *testing.T) {
186 testCases := []struct {
187 name string
188 unbundledBuild bool
189 publicStub string
190 systemStub string
191 testStub string
192 corePlatformStub string
193 }{
194 {
195 name: "testBundled",
196 unbundledBuild: false,
197 publicStub: "android_stubs_current",
198 systemStub: "android_system_stubs_current",
199 testStub: "android_test_stubs_current",
200 corePlatformStub: "legacy.core.platform.api.stubs",
201 }, {
202 name: "testUnbundled",
203 unbundledBuild: true,
204 publicStub: "sdk_public_current_android",
205 systemStub: "sdk_system_current_android",
206 testStub: "sdk_test_current_android",
207 corePlatformStub: "legacy.core.platform.api.stubs",
208 },
209 }
210 for _, tc := range testCases {
211 t.Run(tc.name, func(t *testing.T) {
212 ctx, _ := testHiddenAPIUnbundled(t, tc.unbundledBuild)
213
214 hiddenAPI := ctx.SingletonForTests("hiddenapi")
215 hiddenapiRule := hiddenAPI.Rule("hiddenapi")
216 wantPublicStubs := "--public-stub-classpath=" + generateSdkDexPath(tc.publicStub, tc.unbundledBuild)
217 if !strings.Contains(hiddenapiRule.RuleParams.Command, wantPublicStubs) {
218 t.Errorf("Expected %s in hiddenapi command, but it was not present: %s", wantPublicStubs, hiddenapiRule.RuleParams.Command)
219 }
220
221 wantSystemStubs := "--system-stub-classpath=" + generateSdkDexPath(tc.systemStub, tc.unbundledBuild)
222 if !strings.Contains(hiddenapiRule.RuleParams.Command, wantSystemStubs) {
223 t.Errorf("Expected %s in hiddenapi command, but it was not present: %s", wantSystemStubs, hiddenapiRule.RuleParams.Command)
224 }
225
226 wantTestStubs := "--test-stub-classpath=" + generateSdkDexPath(tc.testStub, tc.unbundledBuild)
227 if !strings.Contains(hiddenapiRule.RuleParams.Command, wantTestStubs) {
228 t.Errorf("Expected %s in hiddenapi command, but it was not present: %s", wantTestStubs, hiddenapiRule.RuleParams.Command)
229 }
230
231 wantCorePlatformStubs := "--core-platform-stub-classpath=" + generateDexPath(tc.corePlatformStub)
232 if !strings.Contains(hiddenapiRule.RuleParams.Command, wantCorePlatformStubs) {
233 t.Errorf("Expected %s in hiddenapi command, but it was not present: %s", wantCorePlatformStubs, hiddenapiRule.RuleParams.Command)
234 }
235 })
236 }
237}
238
239func generateDexedPath(subDir, dex, module string) string {
240 return fmt.Sprintf("%s/.intermediates/%s/android_common/%s/%s.jar", buildDir, subDir, dex, module)
241}
242
243func generateDexPath(module string) string {
244 return generateDexedPath(module, "dex", module)
245}
246
247func generateSdkDexPath(module string, unbundled bool) string {
248 if unbundled {
249 return generateDexedPath("prebuilts/sdk/"+module, "dex", module)
250 }
251 return generateDexPath(module)
252}
Bill Peckhambae47492021-01-08 09:34:44 -0800253
254func TestHiddenAPISingletonWithPrebuiltCsvFile(t *testing.T) {
255
256 // The idea behind this test is to ensure that when the build is
257 // confugured with a PrebuiltHiddenApiDir that the rules for the
258 // hiddenapi singleton copy the prebuilts to the typical output
259 // location, and then use that output location for the hiddenapi encode
260 // dex step.
261
262 // Where to find the prebuilt hiddenapi files:
263 prebuiltHiddenApiDir := "path/to/prebuilt/hiddenapi"
264
265 ctx, _ := testHiddenAPIBootJars(t, `
266 java_import {
267 name: "foo",
268 jars: ["a.jar"],
269 compile_dex: true,
270 }
Paul Duffin9c3ac962021-02-03 14:11:27 +0000271 `, []string{"platform:foo"}, &prebuiltHiddenApiDir)
Bill Peckhambae47492021-01-08 09:34:44 -0800272
273 expectedCpInput := prebuiltHiddenApiDir + "/hiddenapi-flags.csv"
274 expectedCpOutput := buildDir + "/hiddenapi/hiddenapi-flags.csv"
275 expectedFlagsCsv := buildDir + "/hiddenapi/hiddenapi-flags.csv"
276
277 foo := ctx.ModuleForTests("foo", "android_common")
278
279 hiddenAPI := ctx.SingletonForTests("hiddenapi")
280 cpRule := hiddenAPI.Rule("Cp")
281 actualCpInput := cpRule.BuildParams.Input
282 actualCpOutput := cpRule.BuildParams.Output
283 encodeDexRule := foo.Rule("hiddenAPIEncodeDex")
284 actualFlagsCsv := encodeDexRule.BuildParams.Args["flagsCsv"]
285
286 if actualCpInput.String() != expectedCpInput {
287 t.Errorf("Prebuilt hiddenapi cp rule input mismatch, actual: %s, expected: %s", actualCpInput, expectedCpInput)
288 }
289
290 if actualCpOutput.String() != expectedCpOutput {
291 t.Errorf("Prebuilt hiddenapi cp rule output mismatch, actual: %s, expected: %s", actualCpOutput, expectedCpOutput)
292 }
293
294 if actualFlagsCsv != expectedFlagsCsv {
295 t.Errorf("Prebuilt hiddenapi encode dex rule flags csv mismatch, actual: %s, expected: %s", actualFlagsCsv, expectedFlagsCsv)
296 }
297}