blob: e5e1c254745826b1d88e8e71c23a83859afc3463 [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 fixtureSetPrebuiltHiddenApiDirProductVariable(prebuiltHiddenApiDir *string) android.FixturePreparer {
27 return android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
28 variables.PrebuiltHiddenApiDir = prebuiltHiddenApiDir
29 })
Liz Kammer5ca3a622020-08-05 15:40:41 -070030}
31
Paul Duffin71ae5942021-03-22 15:36:52 +000032var hiddenApiFixtureFactory = android.GroupFixturePreparers(
33 prepareForJavaTest, PrepareForTestWithHiddenApiBuildComponents)
Liz Kammer5ca3a622020-08-05 15:40:41 -070034
35func TestHiddenAPISingleton(t *testing.T) {
Paul Duffin79abe572021-03-29 02:16:14 +010036 result := android.GroupFixturePreparers(
37 hiddenApiFixtureFactory,
Paul Duffin60264a02021-04-12 20:02:36 +010038 FixtureConfigureBootJars("platform:foo"),
Paul Duffin175947f2021-03-12 21:44:02 +000039 ).RunTestWithBp(t, `
Liz Kammer5ca3a622020-08-05 15:40:41 -070040 java_library {
41 name: "foo",
42 srcs: ["a.java"],
43 compile_dex: true,
Paul Duffin01289a22021-02-04 17:49:33 +000044 }
Paul Duffin175947f2021-03-12 21:44:02 +000045 `)
Liz Kammer5ca3a622020-08-05 15:40:41 -070046
Paul Duffin175947f2021-03-12 21:44:02 +000047 hiddenAPI := result.SingletonForTests("hiddenapi")
Paul Duffina71a67a2021-03-29 00:42:57 +010048 hiddenapiRule := hiddenAPI.Rule("hiddenapi")
Paul Duffin2f9e71e2021-03-22 16:24:49 +000049 want := "--boot-dex=out/soong/.intermediates/foo/android_common/aligned/foo.jar"
Paul Duffin175947f2021-03-12 21:44:02 +000050 android.AssertStringDoesContain(t, "hiddenapi command", hiddenapiRule.RuleParams.Command, want)
Liz Kammer5ca3a622020-08-05 15:40:41 -070051}
52
Paul Duffin01289a22021-02-04 17:49:33 +000053func TestHiddenAPIIndexSingleton(t *testing.T) {
Paul Duffin79abe572021-03-29 02:16:14 +010054 result := android.GroupFixturePreparers(
55 hiddenApiFixtureFactory,
Paul Duffindc92abb2021-03-13 08:28:35 +000056 PrepareForTestWithJavaSdkLibraryFiles,
57 FixtureWithLastReleaseApis("bar"),
Paul Duffin60264a02021-04-12 20:02:36 +010058 FixtureConfigureBootJars("platform:foo", "platform:bar"),
Paul Duffin175947f2021-03-12 21:44:02 +000059 ).RunTestWithBp(t, `
Paul Duffin01289a22021-02-04 17:49:33 +000060 java_library {
61 name: "foo",
62 srcs: ["a.java"],
63 compile_dex: true,
Paul Duffin031d8692021-02-12 11:46:42 +000064
65 hiddenapi_additional_annotations: [
66 "foo-hiddenapi-annotations",
67 ],
Paul Duffin01289a22021-02-04 17:49:33 +000068 }
69
Paul Duffine7975ff2021-02-11 15:33:37 +000070 java_library {
Paul Duffin031d8692021-02-12 11:46:42 +000071 name: "foo-hiddenapi-annotations",
72 srcs: ["a.java"],
73 compile_dex: true,
74 }
75
Paul Duffin01289a22021-02-04 17:49:33 +000076 java_import {
77 name: "foo",
78 jars: ["a.jar"],
79 compile_dex: true,
80 prefer: false,
81 }
82
83 java_sdk_library {
84 name: "bar",
85 srcs: ["a.java"],
86 compile_dex: true,
87 }
Paul Duffin175947f2021-03-12 21:44:02 +000088 `)
Paul Duffin01289a22021-02-04 17:49:33 +000089
Paul Duffin175947f2021-03-12 21:44:02 +000090 hiddenAPIIndex := result.SingletonForTests("hiddenapi_index")
Paul Duffin01289a22021-02-04 17:49:33 +000091 indexRule := hiddenAPIIndex.Rule("singleton-merged-hiddenapi-index")
Paul Duffin4fd997b2021-02-03 20:06:33 +000092 CheckHiddenAPIRuleInputs(t, `
Paul Duffin01289a22021-02-04 17:49:33 +000093.intermediates/bar/android_common/hiddenapi/index.csv
94.intermediates/foo/android_common/hiddenapi/index.csv
Paul Duffin01289a22021-02-04 17:49:33 +000095`,
96 indexRule)
Paul Duffin031d8692021-02-12 11:46:42 +000097
98 // Make sure that the foo-hiddenapi-annotations.jar is included in the inputs to the rules that
99 // creates the index.csv file.
Paul Duffin175947f2021-03-12 21:44:02 +0000100 foo := result.ModuleForTests("foo", "android_common")
Paul Duffin031d8692021-02-12 11:46:42 +0000101 indexParams := foo.Output("hiddenapi/index.csv")
102 CheckHiddenAPIRuleInputs(t, `
103.intermediates/foo-hiddenapi-annotations/android_common/javac/foo-hiddenapi-annotations.jar
104.intermediates/foo/android_common/javac/foo.jar
105`, indexParams)
Paul Duffin01289a22021-02-04 17:49:33 +0000106}
107
Paul Duffinec0fe172021-02-25 15:34:13 +0000108func TestHiddenAPISingletonWithSourceAndPrebuiltPreferredButNoDex(t *testing.T) {
Paul Duffin175947f2021-03-12 21:44:02 +0000109 expectedErrorMessage :=
110 "hiddenapi has determined that the source module \"foo\" should be ignored as it has been" +
111 " replaced by the prebuilt module \"prebuilt_foo\" but unfortunately it does not provide a" +
112 " suitable boot dex jar"
113
Paul Duffin79abe572021-03-29 02:16:14 +0100114 android.GroupFixturePreparers(
115 hiddenApiFixtureFactory,
Paul Duffin60264a02021-04-12 20:02:36 +0100116 FixtureConfigureBootJars("platform:foo"),
Paul Duffin175947f2021-03-12 21:44:02 +0000117 ).ExtendWithErrorHandler(android.FixtureExpectsAtLeastOneErrorMatchingPattern(expectedErrorMessage)).
118 RunTestWithBp(t, `
Paul Duffinec0fe172021-02-25 15:34:13 +0000119 java_library {
120 name: "foo",
121 srcs: ["a.java"],
122 compile_dex: true,
123 }
124
125 java_import {
126 name: "foo",
127 jars: ["a.jar"],
128 prefer: true,
129 }
Paul Duffin175947f2021-03-12 21:44:02 +0000130 `)
Paul Duffinec0fe172021-02-25 15:34:13 +0000131}
132
Liz Kammer5ca3a622020-08-05 15:40:41 -0700133func TestHiddenAPISingletonWithPrebuilt(t *testing.T) {
Paul Duffin79abe572021-03-29 02:16:14 +0100134 result := android.GroupFixturePreparers(
135 hiddenApiFixtureFactory,
Paul Duffin60264a02021-04-12 20:02:36 +0100136 FixtureConfigureBootJars("platform:foo"),
Paul Duffin175947f2021-03-12 21:44:02 +0000137 ).RunTestWithBp(t, `
Liz Kammer5ca3a622020-08-05 15:40:41 -0700138 java_import {
139 name: "foo",
140 jars: ["a.jar"],
141 compile_dex: true,
142 }
Paul Duffin175947f2021-03-12 21:44:02 +0000143 `)
Liz Kammer5ca3a622020-08-05 15:40:41 -0700144
Paul Duffin175947f2021-03-12 21:44:02 +0000145 hiddenAPI := result.SingletonForTests("hiddenapi")
Paul Duffina71a67a2021-03-29 00:42:57 +0100146 hiddenapiRule := hiddenAPI.Rule("hiddenapi")
Paul Duffin2f9e71e2021-03-22 16:24:49 +0000147 want := "--boot-dex=out/soong/.intermediates/foo/android_common/aligned/foo.jar"
Paul Duffin175947f2021-03-12 21:44:02 +0000148 android.AssertStringDoesContain(t, "hiddenapi command", hiddenapiRule.RuleParams.Command, want)
Liz Kammer5ca3a622020-08-05 15:40:41 -0700149}
150
151func TestHiddenAPISingletonWithPrebuiltUseSource(t *testing.T) {
Paul Duffin79abe572021-03-29 02:16:14 +0100152 result := android.GroupFixturePreparers(
153 hiddenApiFixtureFactory,
Paul Duffin60264a02021-04-12 20:02:36 +0100154 FixtureConfigureBootJars("platform:foo"),
Paul Duffin175947f2021-03-12 21:44:02 +0000155 ).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")
Paul Duffina71a67a2021-03-29 00:42:57 +0100171 hiddenapiRule := hiddenAPI.Rule("hiddenapi")
Paul Duffin2f9e71e2021-03-22 16:24:49 +0000172 fromSourceJarArg := "--boot-dex=out/soong/.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
Paul Duffin2f9e71e2021-03-22 16:24:49 +0000175 prebuiltJarArg := "--boot-dex=out/soong/.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 Duffin79abe572021-03-29 02:16:14 +0100180 result := android.GroupFixturePreparers(
181 hiddenApiFixtureFactory,
Paul Duffin60264a02021-04-12 20:02:36 +0100182 FixtureConfigureBootJars("platform:foo"),
Paul Duffin175947f2021-03-12 21:44:02 +0000183 ).RunTestWithBp(t, `
Liz Kammer5ca3a622020-08-05 15:40:41 -0700184 java_library {
185 name: "foo",
186 srcs: ["a.java"],
187 compile_dex: true,
Paul Duffin01289a22021-02-04 17:49:33 +0000188 }
Liz Kammer5ca3a622020-08-05 15:40:41 -0700189
190 java_import {
191 name: "foo",
192 jars: ["a.jar"],
193 compile_dex: true,
194 prefer: true,
Paul Duffin01289a22021-02-04 17:49:33 +0000195 }
Paul Duffin175947f2021-03-12 21:44:02 +0000196 `)
Liz Kammer5ca3a622020-08-05 15:40:41 -0700197
Paul Duffin175947f2021-03-12 21:44:02 +0000198 hiddenAPI := result.SingletonForTests("hiddenapi")
Paul Duffina71a67a2021-03-29 00:42:57 +0100199 hiddenapiRule := hiddenAPI.Rule("hiddenapi")
Paul Duffin2f9e71e2021-03-22 16:24:49 +0000200 prebuiltJarArg := "--boot-dex=out/soong/.intermediates/prebuilt_foo/android_common/dex/foo.jar"
Paul Duffin175947f2021-03-12 21:44:02 +0000201 android.AssertStringDoesContain(t, "hiddenapi command", hiddenapiRule.RuleParams.Command, prebuiltJarArg)
Liz Kammer5ca3a622020-08-05 15:40:41 -0700202
Paul Duffin2f9e71e2021-03-22 16:24:49 +0000203 fromSourceJarArg := "--boot-dex=out/soong/.intermediates/foo/android_common/aligned/foo.jar"
Paul Duffin175947f2021-03-12 21:44:02 +0000204 android.AssertStringDoesNotContain(t, "hiddenapi command", hiddenapiRule.RuleParams.Command, fromSourceJarArg)
Liz Kammer5ca3a622020-08-05 15:40:41 -0700205}
Anton Hanssona2adc372020-07-03 15:31:32 +0100206
207func TestHiddenAPISingletonSdks(t *testing.T) {
208 testCases := []struct {
209 name string
210 unbundledBuild bool
211 publicStub string
212 systemStub string
213 testStub string
214 corePlatformStub string
Paul Duffindc92abb2021-03-13 08:28:35 +0000215
216 // Additional test preparer
217 preparer android.FixturePreparer
Anton Hanssona2adc372020-07-03 15:31:32 +0100218 }{
219 {
220 name: "testBundled",
221 unbundledBuild: false,
222 publicStub: "android_stubs_current",
223 systemStub: "android_system_stubs_current",
224 testStub: "android_test_stubs_current",
225 corePlatformStub: "legacy.core.platform.api.stubs",
Paul Duffindc92abb2021-03-13 08:28:35 +0000226 preparer: android.GroupFixturePreparers(),
Anton Hanssona2adc372020-07-03 15:31:32 +0100227 }, {
228 name: "testUnbundled",
229 unbundledBuild: true,
230 publicStub: "sdk_public_current_android",
231 systemStub: "sdk_system_current_android",
232 testStub: "sdk_test_current_android",
233 corePlatformStub: "legacy.core.platform.api.stubs",
Paul Duffindc92abb2021-03-13 08:28:35 +0000234 preparer: PrepareForTestWithPrebuiltsOfCurrentApi,
Anton Hanssona2adc372020-07-03 15:31:32 +0100235 },
236 }
237 for _, tc := range testCases {
238 t.Run(tc.name, func(t *testing.T) {
Paul Duffin79abe572021-03-29 02:16:14 +0100239 result := android.GroupFixturePreparers(
240 hiddenApiFixtureFactory,
Paul Duffindc92abb2021-03-13 08:28:35 +0000241 tc.preparer,
Paul Duffin175947f2021-03-12 21:44:02 +0000242 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
243 variables.Always_use_prebuilt_sdks = proptools.BoolPtr(tc.unbundledBuild)
244 }),
245 ).RunTest(t)
Anton Hanssona2adc372020-07-03 15:31:32 +0100246
Paul Duffin175947f2021-03-12 21:44:02 +0000247 hiddenAPI := result.SingletonForTests("hiddenapi")
Paul Duffina71a67a2021-03-29 00:42:57 +0100248 hiddenapiRule := hiddenAPI.Rule("hiddenapi")
Anton Hanssona2adc372020-07-03 15:31:32 +0100249 wantPublicStubs := "--public-stub-classpath=" + generateSdkDexPath(tc.publicStub, tc.unbundledBuild)
Paul Duffin175947f2021-03-12 21:44:02 +0000250 android.AssertStringDoesContain(t, "hiddenapi command", hiddenapiRule.RuleParams.Command, wantPublicStubs)
Anton Hanssona2adc372020-07-03 15:31:32 +0100251
252 wantSystemStubs := "--system-stub-classpath=" + generateSdkDexPath(tc.systemStub, tc.unbundledBuild)
Paul Duffin175947f2021-03-12 21:44:02 +0000253 android.AssertStringDoesContain(t, "hiddenapi command", hiddenapiRule.RuleParams.Command, wantSystemStubs)
Anton Hanssona2adc372020-07-03 15:31:32 +0100254
255 wantTestStubs := "--test-stub-classpath=" + generateSdkDexPath(tc.testStub, tc.unbundledBuild)
Paul Duffin175947f2021-03-12 21:44:02 +0000256 android.AssertStringDoesContain(t, "hiddenapi command", hiddenapiRule.RuleParams.Command, wantTestStubs)
Anton Hanssona2adc372020-07-03 15:31:32 +0100257
Paul Duffin175947f2021-03-12 21:44:02 +0000258 wantCorePlatformStubs := "--core-platform-stub-classpath=" + generateDexPath(defaultJavaDir, tc.corePlatformStub)
259 android.AssertStringDoesContain(t, "hiddenapi command", hiddenapiRule.RuleParams.Command, wantCorePlatformStubs)
Anton Hanssona2adc372020-07-03 15:31:32 +0100260 })
261 }
262}
263
264func generateDexedPath(subDir, dex, module string) string {
Paul Duffin2f9e71e2021-03-22 16:24:49 +0000265 return fmt.Sprintf("out/soong/.intermediates/%s/android_common/%s/%s.jar", subDir, dex, module)
Anton Hanssona2adc372020-07-03 15:31:32 +0100266}
267
Paul Duffin175947f2021-03-12 21:44:02 +0000268func generateDexPath(moduleDir string, module string) string {
269 return generateDexedPath(filepath.Join(moduleDir, module), "dex", module)
Anton Hanssona2adc372020-07-03 15:31:32 +0100270}
271
272func generateSdkDexPath(module string, unbundled bool) string {
273 if unbundled {
274 return generateDexedPath("prebuilts/sdk/"+module, "dex", module)
275 }
Paul Duffin175947f2021-03-12 21:44:02 +0000276 return generateDexPath(defaultJavaDir, module)
Anton Hanssona2adc372020-07-03 15:31:32 +0100277}
Bill Peckhambae47492021-01-08 09:34:44 -0800278
279func TestHiddenAPISingletonWithPrebuiltCsvFile(t *testing.T) {
280
281 // The idea behind this test is to ensure that when the build is
282 // confugured with a PrebuiltHiddenApiDir that the rules for the
283 // hiddenapi singleton copy the prebuilts to the typical output
284 // location, and then use that output location for the hiddenapi encode
285 // dex step.
286
287 // Where to find the prebuilt hiddenapi files:
288 prebuiltHiddenApiDir := "path/to/prebuilt/hiddenapi"
289
Paul Duffin79abe572021-03-29 02:16:14 +0100290 result := android.GroupFixturePreparers(
291 hiddenApiFixtureFactory,
Paul Duffin60264a02021-04-12 20:02:36 +0100292 FixtureConfigureBootJars("platform:foo"),
Paul Duffin175947f2021-03-12 21:44:02 +0000293 fixtureSetPrebuiltHiddenApiDirProductVariable(&prebuiltHiddenApiDir),
294 ).RunTestWithBp(t, `
Bill Peckhambae47492021-01-08 09:34:44 -0800295 java_import {
296 name: "foo",
297 jars: ["a.jar"],
298 compile_dex: true,
299 }
Paul Duffin175947f2021-03-12 21:44:02 +0000300 `)
Bill Peckhambae47492021-01-08 09:34:44 -0800301
302 expectedCpInput := prebuiltHiddenApiDir + "/hiddenapi-flags.csv"
Paul Duffin2f9e71e2021-03-22 16:24:49 +0000303 expectedCpOutput := "out/soong/hiddenapi/hiddenapi-flags.csv"
304 expectedFlagsCsv := "out/soong/hiddenapi/hiddenapi-flags.csv"
Bill Peckhambae47492021-01-08 09:34:44 -0800305
Paul Duffin175947f2021-03-12 21:44:02 +0000306 foo := result.ModuleForTests("foo", "android_common")
Bill Peckhambae47492021-01-08 09:34:44 -0800307
Paul Duffin175947f2021-03-12 21:44:02 +0000308 hiddenAPI := result.SingletonForTests("hiddenapi")
Bill Peckhambae47492021-01-08 09:34:44 -0800309 cpRule := hiddenAPI.Rule("Cp")
310 actualCpInput := cpRule.BuildParams.Input
311 actualCpOutput := cpRule.BuildParams.Output
Paul Duffina71a67a2021-03-29 00:42:57 +0100312 encodeDexRule := foo.Rule("hiddenAPIEncodeDex")
Bill Peckhambae47492021-01-08 09:34:44 -0800313 actualFlagsCsv := encodeDexRule.BuildParams.Args["flagsCsv"]
314
Paul Duffin2f9e71e2021-03-22 16:24:49 +0000315 android.AssertPathRelativeToTopEquals(t, "hiddenapi cp rule input", expectedCpInput, actualCpInput)
Bill Peckhambae47492021-01-08 09:34:44 -0800316
Paul Duffin2f9e71e2021-03-22 16:24:49 +0000317 android.AssertPathRelativeToTopEquals(t, "hiddenapi cp rule output", expectedCpOutput, actualCpOutput)
Bill Peckhambae47492021-01-08 09:34:44 -0800318
Paul Duffin175947f2021-03-12 21:44:02 +0000319 android.AssertStringEquals(t, "hiddenapi encode dex rule flags csv", expectedFlagsCsv, actualFlagsCsv)
Bill Peckhambae47492021-01-08 09:34:44 -0800320}