blob: f17d436aed717cefa0379541851fee8babafc827 [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"
Paul Duffin175947f2021-03-12 21:44:02 +000019 "path/filepath"
Liz Kammer5ca3a622020-08-05 15:40:41 -070020 "testing"
Anton Hanssona2adc372020-07-03 15:31:32 +010021
Paul Duffin01289a22021-02-04 17:49:33 +000022 "android/soong/android"
Anton Hanssona2adc372020-07-03 15:31:32 +010023 "github.com/google/blueprint/proptools"
Liz Kammer5ca3a622020-08-05 15:40:41 -070024)
25
Paul Duffin175947f2021-03-12 21:44:02 +000026func fixtureSetBootJarsProductVariable(bootJars ...string) android.FixturePreparer {
27 return android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
28 variables.BootJars = android.CreateTestConfiguredJarList(bootJars)
29 })
Liz Kammer5ca3a622020-08-05 15:40:41 -070030}
31
Paul Duffin175947f2021-03-12 21:44:02 +000032func fixtureSetPrebuiltHiddenApiDirProductVariable(prebuiltHiddenApiDir *string) android.FixturePreparer {
33 return android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
34 variables.PrebuiltHiddenApiDir = prebuiltHiddenApiDir
35 })
Liz Kammer5ca3a622020-08-05 15:40:41 -070036}
37
Paul Duffin175947f2021-03-12 21:44:02 +000038var hiddenApiFixtureFactory = javaFixtureFactory.Extend(PrepareForTestWithHiddenApiBuildComponents)
Liz Kammer5ca3a622020-08-05 15:40:41 -070039
40func TestHiddenAPISingleton(t *testing.T) {
Paul Duffin175947f2021-03-12 21:44:02 +000041 result := hiddenApiFixtureFactory.Extend(
42 fixtureSetBootJarsProductVariable("platform:foo"),
43 ).RunTestWithBp(t, `
Liz Kammer5ca3a622020-08-05 15:40:41 -070044 java_library {
45 name: "foo",
46 srcs: ["a.java"],
47 compile_dex: true,
Paul Duffin01289a22021-02-04 17:49:33 +000048 }
Paul Duffin175947f2021-03-12 21:44:02 +000049 `)
Liz Kammer5ca3a622020-08-05 15:40:41 -070050
Paul Duffin175947f2021-03-12 21:44:02 +000051 hiddenAPI := result.SingletonForTests("hiddenapi")
Liz Kammer5ca3a622020-08-05 15:40:41 -070052 hiddenapiRule := hiddenAPI.Rule("hiddenapi")
53 want := "--boot-dex=" + buildDir + "/.intermediates/foo/android_common/aligned/foo.jar"
Paul Duffin175947f2021-03-12 21:44:02 +000054 android.AssertStringDoesContain(t, "hiddenapi command", hiddenapiRule.RuleParams.Command, want)
Liz Kammer5ca3a622020-08-05 15:40:41 -070055}
56
Paul Duffin01289a22021-02-04 17:49:33 +000057func TestHiddenAPIIndexSingleton(t *testing.T) {
Paul Duffin175947f2021-03-12 21:44:02 +000058 result := hiddenApiFixtureFactory.Extend(
Paul Duffindc92abb2021-03-13 08:28:35 +000059 PrepareForTestWithJavaSdkLibraryFiles,
60 FixtureWithLastReleaseApis("bar"),
Paul Duffin175947f2021-03-12 21:44:02 +000061 fixtureSetBootJarsProductVariable("platform:foo", "platform:bar"),
62 ).RunTestWithBp(t, `
Paul Duffin01289a22021-02-04 17:49:33 +000063 java_library {
64 name: "foo",
65 srcs: ["a.java"],
66 compile_dex: true,
Paul Duffin031d8692021-02-12 11:46:42 +000067
68 hiddenapi_additional_annotations: [
69 "foo-hiddenapi-annotations",
70 ],
Paul Duffin01289a22021-02-04 17:49:33 +000071 }
72
Paul Duffine7975ff2021-02-11 15:33:37 +000073 java_library {
Paul Duffin031d8692021-02-12 11:46:42 +000074 name: "foo-hiddenapi-annotations",
75 srcs: ["a.java"],
76 compile_dex: true,
77 }
78
Paul Duffin01289a22021-02-04 17:49:33 +000079 java_import {
80 name: "foo",
81 jars: ["a.jar"],
82 compile_dex: true,
83 prefer: false,
84 }
85
86 java_sdk_library {
87 name: "bar",
88 srcs: ["a.java"],
89 compile_dex: true,
90 }
Paul Duffin175947f2021-03-12 21:44:02 +000091 `)
Paul Duffin01289a22021-02-04 17:49:33 +000092
Paul Duffin175947f2021-03-12 21:44:02 +000093 hiddenAPIIndex := result.SingletonForTests("hiddenapi_index")
Paul Duffin01289a22021-02-04 17:49:33 +000094 indexRule := hiddenAPIIndex.Rule("singleton-merged-hiddenapi-index")
Paul Duffin4fd997b2021-02-03 20:06:33 +000095 CheckHiddenAPIRuleInputs(t, `
Paul Duffin01289a22021-02-04 17:49:33 +000096.intermediates/bar/android_common/hiddenapi/index.csv
97.intermediates/foo/android_common/hiddenapi/index.csv
Paul Duffin01289a22021-02-04 17:49:33 +000098`,
99 indexRule)
Paul Duffin031d8692021-02-12 11:46:42 +0000100
101 // Make sure that the foo-hiddenapi-annotations.jar is included in the inputs to the rules that
102 // creates the index.csv file.
Paul Duffin175947f2021-03-12 21:44:02 +0000103 foo := result.ModuleForTests("foo", "android_common")
Paul Duffin031d8692021-02-12 11:46:42 +0000104 indexParams := foo.Output("hiddenapi/index.csv")
105 CheckHiddenAPIRuleInputs(t, `
106.intermediates/foo-hiddenapi-annotations/android_common/javac/foo-hiddenapi-annotations.jar
107.intermediates/foo/android_common/javac/foo.jar
108`, indexParams)
Paul Duffin01289a22021-02-04 17:49:33 +0000109}
110
Paul Duffinec0fe172021-02-25 15:34:13 +0000111func TestHiddenAPISingletonWithSourceAndPrebuiltPreferredButNoDex(t *testing.T) {
Paul Duffin175947f2021-03-12 21:44:02 +0000112 expectedErrorMessage :=
113 "hiddenapi has determined that the source module \"foo\" should be ignored as it has been" +
114 " replaced by the prebuilt module \"prebuilt_foo\" but unfortunately it does not provide a" +
115 " suitable boot dex jar"
116
117 hiddenApiFixtureFactory.Extend(
118 fixtureSetBootJarsProductVariable("platform:foo"),
119 ).ExtendWithErrorHandler(android.FixtureExpectsAtLeastOneErrorMatchingPattern(expectedErrorMessage)).
120 RunTestWithBp(t, `
Paul Duffinec0fe172021-02-25 15:34:13 +0000121 java_library {
122 name: "foo",
123 srcs: ["a.java"],
124 compile_dex: true,
125 }
126
127 java_import {
128 name: "foo",
129 jars: ["a.jar"],
130 prefer: true,
131 }
Paul Duffin175947f2021-03-12 21:44:02 +0000132 `)
Paul Duffinec0fe172021-02-25 15:34:13 +0000133}
134
Liz Kammer5ca3a622020-08-05 15:40:41 -0700135func TestHiddenAPISingletonWithPrebuilt(t *testing.T) {
Paul Duffin175947f2021-03-12 21:44:02 +0000136 result := hiddenApiFixtureFactory.Extend(
137 fixtureSetBootJarsProductVariable("platform:foo"),
138 ).RunTestWithBp(t, `
Liz Kammer5ca3a622020-08-05 15:40:41 -0700139 java_import {
140 name: "foo",
141 jars: ["a.jar"],
142 compile_dex: true,
143 }
Paul Duffin175947f2021-03-12 21:44:02 +0000144 `)
Liz Kammer5ca3a622020-08-05 15:40:41 -0700145
Paul Duffin175947f2021-03-12 21:44:02 +0000146 hiddenAPI := result.SingletonForTests("hiddenapi")
Liz Kammer5ca3a622020-08-05 15:40:41 -0700147 hiddenapiRule := hiddenAPI.Rule("hiddenapi")
Bill Peckhamff89ffa2020-12-23 16:13:04 -0800148 want := "--boot-dex=" + buildDir + "/.intermediates/foo/android_common/aligned/foo.jar"
Paul Duffin175947f2021-03-12 21:44:02 +0000149 android.AssertStringDoesContain(t, "hiddenapi command", hiddenapiRule.RuleParams.Command, want)
Liz Kammer5ca3a622020-08-05 15:40:41 -0700150}
151
152func TestHiddenAPISingletonWithPrebuiltUseSource(t *testing.T) {
Paul Duffin175947f2021-03-12 21:44:02 +0000153 result := hiddenApiFixtureFactory.Extend(
154 fixtureSetBootJarsProductVariable("platform:foo"),
155 ).RunTestWithBp(t, `
Liz Kammer5ca3a622020-08-05 15:40:41 -0700156 java_library {
157 name: "foo",
158 srcs: ["a.java"],
159 compile_dex: true,
Paul Duffin01289a22021-02-04 17:49:33 +0000160 }
Liz Kammer5ca3a622020-08-05 15:40:41 -0700161
162 java_import {
163 name: "foo",
164 jars: ["a.jar"],
165 compile_dex: true,
166 prefer: false,
Paul Duffin01289a22021-02-04 17:49:33 +0000167 }
Paul Duffin175947f2021-03-12 21:44:02 +0000168 `)
Liz Kammer5ca3a622020-08-05 15:40:41 -0700169
Paul Duffin175947f2021-03-12 21:44:02 +0000170 hiddenAPI := result.SingletonForTests("hiddenapi")
Liz Kammer5ca3a622020-08-05 15:40:41 -0700171 hiddenapiRule := hiddenAPI.Rule("hiddenapi")
172 fromSourceJarArg := "--boot-dex=" + buildDir + "/.intermediates/foo/android_common/aligned/foo.jar"
Paul Duffin175947f2021-03-12 21:44:02 +0000173 android.AssertStringDoesContain(t, "hiddenapi command", hiddenapiRule.RuleParams.Command, fromSourceJarArg)
Liz Kammer5ca3a622020-08-05 15:40:41 -0700174
175 prebuiltJarArg := "--boot-dex=" + buildDir + "/.intermediates/foo/android_common/dex/foo.jar"
Paul Duffin175947f2021-03-12 21:44:02 +0000176 android.AssertStringDoesNotContain(t, "hiddenapi command", hiddenapiRule.RuleParams.Command, prebuiltJarArg)
Liz Kammer5ca3a622020-08-05 15:40:41 -0700177}
178
179func TestHiddenAPISingletonWithPrebuiltOverrideSource(t *testing.T) {
Paul Duffin175947f2021-03-12 21:44:02 +0000180 result := hiddenApiFixtureFactory.Extend(
181 fixtureSetBootJarsProductVariable("platform:foo"),
182 ).RunTestWithBp(t, `
Liz Kammer5ca3a622020-08-05 15:40:41 -0700183 java_library {
184 name: "foo",
185 srcs: ["a.java"],
186 compile_dex: true,
Paul Duffin01289a22021-02-04 17:49:33 +0000187 }
Liz Kammer5ca3a622020-08-05 15:40:41 -0700188
189 java_import {
190 name: "foo",
191 jars: ["a.jar"],
192 compile_dex: true,
193 prefer: true,
Paul Duffin01289a22021-02-04 17:49:33 +0000194 }
Paul Duffin175947f2021-03-12 21:44:02 +0000195 `)
Liz Kammer5ca3a622020-08-05 15:40:41 -0700196
Paul Duffin175947f2021-03-12 21:44:02 +0000197 hiddenAPI := result.SingletonForTests("hiddenapi")
Liz Kammer5ca3a622020-08-05 15:40:41 -0700198 hiddenapiRule := hiddenAPI.Rule("hiddenapi")
199 prebuiltJarArg := "--boot-dex=" + buildDir + "/.intermediates/prebuilt_foo/android_common/dex/foo.jar"
Paul Duffin175947f2021-03-12 21:44:02 +0000200 android.AssertStringDoesContain(t, "hiddenapi command", hiddenapiRule.RuleParams.Command, prebuiltJarArg)
Liz Kammer5ca3a622020-08-05 15:40:41 -0700201
202 fromSourceJarArg := "--boot-dex=" + buildDir + "/.intermediates/foo/android_common/aligned/foo.jar"
Paul Duffin175947f2021-03-12 21:44:02 +0000203 android.AssertStringDoesNotContain(t, "hiddenapi command", hiddenapiRule.RuleParams.Command, fromSourceJarArg)
Liz Kammer5ca3a622020-08-05 15:40:41 -0700204}
Anton Hanssona2adc372020-07-03 15:31:32 +0100205
206func TestHiddenAPISingletonSdks(t *testing.T) {
207 testCases := []struct {
208 name string
209 unbundledBuild bool
210 publicStub string
211 systemStub string
212 testStub string
213 corePlatformStub string
Paul Duffindc92abb2021-03-13 08:28:35 +0000214
215 // Additional test preparer
216 preparer android.FixturePreparer
Anton Hanssona2adc372020-07-03 15:31:32 +0100217 }{
218 {
219 name: "testBundled",
220 unbundledBuild: false,
221 publicStub: "android_stubs_current",
222 systemStub: "android_system_stubs_current",
223 testStub: "android_test_stubs_current",
224 corePlatformStub: "legacy.core.platform.api.stubs",
Paul Duffindc92abb2021-03-13 08:28:35 +0000225 preparer: android.GroupFixturePreparers(),
Anton Hanssona2adc372020-07-03 15:31:32 +0100226 }, {
227 name: "testUnbundled",
228 unbundledBuild: true,
229 publicStub: "sdk_public_current_android",
230 systemStub: "sdk_system_current_android",
231 testStub: "sdk_test_current_android",
232 corePlatformStub: "legacy.core.platform.api.stubs",
Paul Duffindc92abb2021-03-13 08:28:35 +0000233 preparer: PrepareForTestWithPrebuiltsOfCurrentApi,
Anton Hanssona2adc372020-07-03 15:31:32 +0100234 },
235 }
236 for _, tc := range testCases {
237 t.Run(tc.name, func(t *testing.T) {
Paul Duffin175947f2021-03-12 21:44:02 +0000238 result := hiddenApiFixtureFactory.Extend(
Paul Duffindc92abb2021-03-13 08:28:35 +0000239 tc.preparer,
Paul Duffin175947f2021-03-12 21:44:02 +0000240 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
241 variables.Always_use_prebuilt_sdks = proptools.BoolPtr(tc.unbundledBuild)
242 }),
243 ).RunTest(t)
Anton Hanssona2adc372020-07-03 15:31:32 +0100244
Paul Duffin175947f2021-03-12 21:44:02 +0000245 hiddenAPI := result.SingletonForTests("hiddenapi")
Anton Hanssona2adc372020-07-03 15:31:32 +0100246 hiddenapiRule := hiddenAPI.Rule("hiddenapi")
247 wantPublicStubs := "--public-stub-classpath=" + generateSdkDexPath(tc.publicStub, tc.unbundledBuild)
Paul Duffin175947f2021-03-12 21:44:02 +0000248 android.AssertStringDoesContain(t, "hiddenapi command", hiddenapiRule.RuleParams.Command, wantPublicStubs)
Anton Hanssona2adc372020-07-03 15:31:32 +0100249
250 wantSystemStubs := "--system-stub-classpath=" + generateSdkDexPath(tc.systemStub, tc.unbundledBuild)
Paul Duffin175947f2021-03-12 21:44:02 +0000251 android.AssertStringDoesContain(t, "hiddenapi command", hiddenapiRule.RuleParams.Command, wantSystemStubs)
Anton Hanssona2adc372020-07-03 15:31:32 +0100252
253 wantTestStubs := "--test-stub-classpath=" + generateSdkDexPath(tc.testStub, tc.unbundledBuild)
Paul Duffin175947f2021-03-12 21:44:02 +0000254 android.AssertStringDoesContain(t, "hiddenapi command", hiddenapiRule.RuleParams.Command, wantTestStubs)
Anton Hanssona2adc372020-07-03 15:31:32 +0100255
Paul Duffin175947f2021-03-12 21:44:02 +0000256 wantCorePlatformStubs := "--core-platform-stub-classpath=" + generateDexPath(defaultJavaDir, tc.corePlatformStub)
257 android.AssertStringDoesContain(t, "hiddenapi command", hiddenapiRule.RuleParams.Command, wantCorePlatformStubs)
Anton Hanssona2adc372020-07-03 15:31:32 +0100258 })
259 }
260}
261
262func generateDexedPath(subDir, dex, module string) string {
263 return fmt.Sprintf("%s/.intermediates/%s/android_common/%s/%s.jar", buildDir, subDir, dex, module)
264}
265
Paul Duffin175947f2021-03-12 21:44:02 +0000266func generateDexPath(moduleDir string, module string) string {
267 return generateDexedPath(filepath.Join(moduleDir, module), "dex", module)
Anton Hanssona2adc372020-07-03 15:31:32 +0100268}
269
270func generateSdkDexPath(module string, unbundled bool) string {
271 if unbundled {
272 return generateDexedPath("prebuilts/sdk/"+module, "dex", module)
273 }
Paul Duffin175947f2021-03-12 21:44:02 +0000274 return generateDexPath(defaultJavaDir, module)
Anton Hanssona2adc372020-07-03 15:31:32 +0100275}
Bill Peckhambae47492021-01-08 09:34:44 -0800276
277func TestHiddenAPISingletonWithPrebuiltCsvFile(t *testing.T) {
278
279 // The idea behind this test is to ensure that when the build is
280 // confugured with a PrebuiltHiddenApiDir that the rules for the
281 // hiddenapi singleton copy the prebuilts to the typical output
282 // location, and then use that output location for the hiddenapi encode
283 // dex step.
284
285 // Where to find the prebuilt hiddenapi files:
286 prebuiltHiddenApiDir := "path/to/prebuilt/hiddenapi"
287
Paul Duffin175947f2021-03-12 21:44:02 +0000288 result := hiddenApiFixtureFactory.Extend(
289 fixtureSetBootJarsProductVariable("platform:foo"),
290 fixtureSetPrebuiltHiddenApiDirProductVariable(&prebuiltHiddenApiDir),
291 ).RunTestWithBp(t, `
Bill Peckhambae47492021-01-08 09:34:44 -0800292 java_import {
293 name: "foo",
294 jars: ["a.jar"],
295 compile_dex: true,
296 }
Paul Duffin175947f2021-03-12 21:44:02 +0000297 `)
Bill Peckhambae47492021-01-08 09:34:44 -0800298
299 expectedCpInput := prebuiltHiddenApiDir + "/hiddenapi-flags.csv"
300 expectedCpOutput := buildDir + "/hiddenapi/hiddenapi-flags.csv"
301 expectedFlagsCsv := buildDir + "/hiddenapi/hiddenapi-flags.csv"
302
Paul Duffin175947f2021-03-12 21:44:02 +0000303 foo := result.ModuleForTests("foo", "android_common")
Bill Peckhambae47492021-01-08 09:34:44 -0800304
Paul Duffin175947f2021-03-12 21:44:02 +0000305 hiddenAPI := result.SingletonForTests("hiddenapi")
Bill Peckhambae47492021-01-08 09:34:44 -0800306 cpRule := hiddenAPI.Rule("Cp")
307 actualCpInput := cpRule.BuildParams.Input
308 actualCpOutput := cpRule.BuildParams.Output
309 encodeDexRule := foo.Rule("hiddenAPIEncodeDex")
310 actualFlagsCsv := encodeDexRule.BuildParams.Args["flagsCsv"]
311
Paul Duffin175947f2021-03-12 21:44:02 +0000312 android.AssertStringEquals(t, "hiddenapi cp rule input", expectedCpInput, actualCpInput.String())
Bill Peckhambae47492021-01-08 09:34:44 -0800313
Paul Duffin175947f2021-03-12 21:44:02 +0000314 android.AssertStringEquals(t, "hiddenapi cp rule output", expectedCpOutput, actualCpOutput.String())
Bill Peckhambae47492021-01-08 09:34:44 -0800315
Paul Duffin175947f2021-03-12 21:44:02 +0000316 android.AssertStringEquals(t, "hiddenapi encode dex rule flags csv", expectedFlagsCsv, actualFlagsCsv)
Bill Peckhambae47492021-01-08 09:34:44 -0800317}