blob: 3894ed36bdd320ac1c2f22bbac5993d3c5b1ae74 [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(
59 fixtureSetBootJarsProductVariable("platform:foo", "platform:bar"),
60 ).RunTestWithBp(t, `
Paul Duffin01289a22021-02-04 17:49:33 +000061 java_library {
62 name: "foo",
63 srcs: ["a.java"],
64 compile_dex: true,
Paul Duffin031d8692021-02-12 11:46:42 +000065
66 hiddenapi_additional_annotations: [
67 "foo-hiddenapi-annotations",
68 ],
Paul Duffin01289a22021-02-04 17:49:33 +000069 }
70
Paul Duffine7975ff2021-02-11 15:33:37 +000071 java_library {
Paul Duffin031d8692021-02-12 11:46:42 +000072 name: "foo-hiddenapi-annotations",
73 srcs: ["a.java"],
74 compile_dex: true,
75 }
76
Paul Duffin01289a22021-02-04 17:49:33 +000077 java_import {
78 name: "foo",
79 jars: ["a.jar"],
80 compile_dex: true,
81 prefer: false,
82 }
83
84 java_sdk_library {
85 name: "bar",
86 srcs: ["a.java"],
87 compile_dex: true,
88 }
Paul Duffin175947f2021-03-12 21:44:02 +000089 `)
Paul Duffin01289a22021-02-04 17:49:33 +000090
Paul Duffin175947f2021-03-12 21:44:02 +000091 hiddenAPIIndex := result.SingletonForTests("hiddenapi_index")
Paul Duffin01289a22021-02-04 17:49:33 +000092 indexRule := hiddenAPIIndex.Rule("singleton-merged-hiddenapi-index")
Paul Duffin4fd997b2021-02-03 20:06:33 +000093 CheckHiddenAPIRuleInputs(t, `
Paul Duffin01289a22021-02-04 17:49:33 +000094.intermediates/bar/android_common/hiddenapi/index.csv
95.intermediates/foo/android_common/hiddenapi/index.csv
Paul Duffin01289a22021-02-04 17:49:33 +000096`,
97 indexRule)
Paul Duffin031d8692021-02-12 11:46:42 +000098
99 // Make sure that the foo-hiddenapi-annotations.jar is included in the inputs to the rules that
100 // creates the index.csv file.
Paul Duffin175947f2021-03-12 21:44:02 +0000101 foo := result.ModuleForTests("foo", "android_common")
Paul Duffin031d8692021-02-12 11:46:42 +0000102 indexParams := foo.Output("hiddenapi/index.csv")
103 CheckHiddenAPIRuleInputs(t, `
104.intermediates/foo-hiddenapi-annotations/android_common/javac/foo-hiddenapi-annotations.jar
105.intermediates/foo/android_common/javac/foo.jar
106`, indexParams)
Paul Duffin01289a22021-02-04 17:49:33 +0000107}
108
Paul Duffinec0fe172021-02-25 15:34:13 +0000109func TestHiddenAPISingletonWithSourceAndPrebuiltPreferredButNoDex(t *testing.T) {
Paul Duffin175947f2021-03-12 21:44:02 +0000110 expectedErrorMessage :=
111 "hiddenapi has determined that the source module \"foo\" should be ignored as it has been" +
112 " replaced by the prebuilt module \"prebuilt_foo\" but unfortunately it does not provide a" +
113 " suitable boot dex jar"
114
115 hiddenApiFixtureFactory.Extend(
116 fixtureSetBootJarsProductVariable("platform:foo"),
117 ).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 Duffin175947f2021-03-12 21:44:02 +0000134 result := hiddenApiFixtureFactory.Extend(
135 fixtureSetBootJarsProductVariable("platform:foo"),
136 ).RunTestWithBp(t, `
Liz Kammer5ca3a622020-08-05 15:40:41 -0700137 java_import {
138 name: "foo",
139 jars: ["a.jar"],
140 compile_dex: true,
141 }
Paul Duffin175947f2021-03-12 21:44:02 +0000142 `)
Liz Kammer5ca3a622020-08-05 15:40:41 -0700143
Paul Duffin175947f2021-03-12 21:44:02 +0000144 hiddenAPI := result.SingletonForTests("hiddenapi")
Liz Kammer5ca3a622020-08-05 15:40:41 -0700145 hiddenapiRule := hiddenAPI.Rule("hiddenapi")
Bill Peckhamff89ffa2020-12-23 16:13:04 -0800146 want := "--boot-dex=" + buildDir + "/.intermediates/foo/android_common/aligned/foo.jar"
Paul Duffin175947f2021-03-12 21:44:02 +0000147 android.AssertStringDoesContain(t, "hiddenapi command", hiddenapiRule.RuleParams.Command, want)
Liz Kammer5ca3a622020-08-05 15:40:41 -0700148}
149
150func TestHiddenAPISingletonWithPrebuiltUseSource(t *testing.T) {
Paul Duffin175947f2021-03-12 21:44:02 +0000151 result := hiddenApiFixtureFactory.Extend(
152 fixtureSetBootJarsProductVariable("platform:foo"),
153 ).RunTestWithBp(t, `
Liz Kammer5ca3a622020-08-05 15:40:41 -0700154 java_library {
155 name: "foo",
156 srcs: ["a.java"],
157 compile_dex: true,
Paul Duffin01289a22021-02-04 17:49:33 +0000158 }
Liz Kammer5ca3a622020-08-05 15:40:41 -0700159
160 java_import {
161 name: "foo",
162 jars: ["a.jar"],
163 compile_dex: true,
164 prefer: false,
Paul Duffin01289a22021-02-04 17:49:33 +0000165 }
Paul Duffin175947f2021-03-12 21:44:02 +0000166 `)
Liz Kammer5ca3a622020-08-05 15:40:41 -0700167
Paul Duffin175947f2021-03-12 21:44:02 +0000168 hiddenAPI := result.SingletonForTests("hiddenapi")
Liz Kammer5ca3a622020-08-05 15:40:41 -0700169 hiddenapiRule := hiddenAPI.Rule("hiddenapi")
170 fromSourceJarArg := "--boot-dex=" + buildDir + "/.intermediates/foo/android_common/aligned/foo.jar"
Paul Duffin175947f2021-03-12 21:44:02 +0000171 android.AssertStringDoesContain(t, "hiddenapi command", hiddenapiRule.RuleParams.Command, fromSourceJarArg)
Liz Kammer5ca3a622020-08-05 15:40:41 -0700172
173 prebuiltJarArg := "--boot-dex=" + buildDir + "/.intermediates/foo/android_common/dex/foo.jar"
Paul Duffin175947f2021-03-12 21:44:02 +0000174 android.AssertStringDoesNotContain(t, "hiddenapi command", hiddenapiRule.RuleParams.Command, prebuiltJarArg)
Liz Kammer5ca3a622020-08-05 15:40:41 -0700175}
176
177func TestHiddenAPISingletonWithPrebuiltOverrideSource(t *testing.T) {
Paul Duffin175947f2021-03-12 21:44:02 +0000178 result := hiddenApiFixtureFactory.Extend(
179 fixtureSetBootJarsProductVariable("platform:foo"),
180 ).RunTestWithBp(t, `
Liz Kammer5ca3a622020-08-05 15:40:41 -0700181 java_library {
182 name: "foo",
183 srcs: ["a.java"],
184 compile_dex: true,
Paul Duffin01289a22021-02-04 17:49:33 +0000185 }
Liz Kammer5ca3a622020-08-05 15:40:41 -0700186
187 java_import {
188 name: "foo",
189 jars: ["a.jar"],
190 compile_dex: true,
191 prefer: true,
Paul Duffin01289a22021-02-04 17:49:33 +0000192 }
Paul Duffin175947f2021-03-12 21:44:02 +0000193 `)
Liz Kammer5ca3a622020-08-05 15:40:41 -0700194
Paul Duffin175947f2021-03-12 21:44:02 +0000195 hiddenAPI := result.SingletonForTests("hiddenapi")
Liz Kammer5ca3a622020-08-05 15:40:41 -0700196 hiddenapiRule := hiddenAPI.Rule("hiddenapi")
197 prebuiltJarArg := "--boot-dex=" + buildDir + "/.intermediates/prebuilt_foo/android_common/dex/foo.jar"
Paul Duffin175947f2021-03-12 21:44:02 +0000198 android.AssertStringDoesContain(t, "hiddenapi command", hiddenapiRule.RuleParams.Command, prebuiltJarArg)
Liz Kammer5ca3a622020-08-05 15:40:41 -0700199
200 fromSourceJarArg := "--boot-dex=" + buildDir + "/.intermediates/foo/android_common/aligned/foo.jar"
Paul Duffin175947f2021-03-12 21:44:02 +0000201 android.AssertStringDoesNotContain(t, "hiddenapi command", hiddenapiRule.RuleParams.Command, fromSourceJarArg)
Liz Kammer5ca3a622020-08-05 15:40:41 -0700202}
Anton Hanssona2adc372020-07-03 15:31:32 +0100203
204func TestHiddenAPISingletonSdks(t *testing.T) {
205 testCases := []struct {
206 name string
207 unbundledBuild bool
208 publicStub string
209 systemStub string
210 testStub string
211 corePlatformStub string
212 }{
213 {
214 name: "testBundled",
215 unbundledBuild: false,
216 publicStub: "android_stubs_current",
217 systemStub: "android_system_stubs_current",
218 testStub: "android_test_stubs_current",
219 corePlatformStub: "legacy.core.platform.api.stubs",
220 }, {
221 name: "testUnbundled",
222 unbundledBuild: true,
223 publicStub: "sdk_public_current_android",
224 systemStub: "sdk_system_current_android",
225 testStub: "sdk_test_current_android",
226 corePlatformStub: "legacy.core.platform.api.stubs",
227 },
228 }
229 for _, tc := range testCases {
230 t.Run(tc.name, func(t *testing.T) {
Paul Duffin175947f2021-03-12 21:44:02 +0000231 result := hiddenApiFixtureFactory.Extend(
232 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
233 variables.Always_use_prebuilt_sdks = proptools.BoolPtr(tc.unbundledBuild)
234 }),
235 ).RunTest(t)
Anton Hanssona2adc372020-07-03 15:31:32 +0100236
Paul Duffin175947f2021-03-12 21:44:02 +0000237 hiddenAPI := result.SingletonForTests("hiddenapi")
Anton Hanssona2adc372020-07-03 15:31:32 +0100238 hiddenapiRule := hiddenAPI.Rule("hiddenapi")
239 wantPublicStubs := "--public-stub-classpath=" + generateSdkDexPath(tc.publicStub, tc.unbundledBuild)
Paul Duffin175947f2021-03-12 21:44:02 +0000240 android.AssertStringDoesContain(t, "hiddenapi command", hiddenapiRule.RuleParams.Command, wantPublicStubs)
Anton Hanssona2adc372020-07-03 15:31:32 +0100241
242 wantSystemStubs := "--system-stub-classpath=" + generateSdkDexPath(tc.systemStub, tc.unbundledBuild)
Paul Duffin175947f2021-03-12 21:44:02 +0000243 android.AssertStringDoesContain(t, "hiddenapi command", hiddenapiRule.RuleParams.Command, wantSystemStubs)
Anton Hanssona2adc372020-07-03 15:31:32 +0100244
245 wantTestStubs := "--test-stub-classpath=" + generateSdkDexPath(tc.testStub, tc.unbundledBuild)
Paul Duffin175947f2021-03-12 21:44:02 +0000246 android.AssertStringDoesContain(t, "hiddenapi command", hiddenapiRule.RuleParams.Command, wantTestStubs)
Anton Hanssona2adc372020-07-03 15:31:32 +0100247
Paul Duffin175947f2021-03-12 21:44:02 +0000248 wantCorePlatformStubs := "--core-platform-stub-classpath=" + generateDexPath(defaultJavaDir, tc.corePlatformStub)
249 android.AssertStringDoesContain(t, "hiddenapi command", hiddenapiRule.RuleParams.Command, wantCorePlatformStubs)
Anton Hanssona2adc372020-07-03 15:31:32 +0100250 })
251 }
252}
253
254func generateDexedPath(subDir, dex, module string) string {
255 return fmt.Sprintf("%s/.intermediates/%s/android_common/%s/%s.jar", buildDir, subDir, dex, module)
256}
257
Paul Duffin175947f2021-03-12 21:44:02 +0000258func generateDexPath(moduleDir string, module string) string {
259 return generateDexedPath(filepath.Join(moduleDir, module), "dex", module)
Anton Hanssona2adc372020-07-03 15:31:32 +0100260}
261
262func generateSdkDexPath(module string, unbundled bool) string {
263 if unbundled {
264 return generateDexedPath("prebuilts/sdk/"+module, "dex", module)
265 }
Paul Duffin175947f2021-03-12 21:44:02 +0000266 return generateDexPath(defaultJavaDir, module)
Anton Hanssona2adc372020-07-03 15:31:32 +0100267}
Bill Peckhambae47492021-01-08 09:34:44 -0800268
269func TestHiddenAPISingletonWithPrebuiltCsvFile(t *testing.T) {
270
271 // The idea behind this test is to ensure that when the build is
272 // confugured with a PrebuiltHiddenApiDir that the rules for the
273 // hiddenapi singleton copy the prebuilts to the typical output
274 // location, and then use that output location for the hiddenapi encode
275 // dex step.
276
277 // Where to find the prebuilt hiddenapi files:
278 prebuiltHiddenApiDir := "path/to/prebuilt/hiddenapi"
279
Paul Duffin175947f2021-03-12 21:44:02 +0000280 result := hiddenApiFixtureFactory.Extend(
281 fixtureSetBootJarsProductVariable("platform:foo"),
282 fixtureSetPrebuiltHiddenApiDirProductVariable(&prebuiltHiddenApiDir),
283 ).RunTestWithBp(t, `
Bill Peckhambae47492021-01-08 09:34:44 -0800284 java_import {
285 name: "foo",
286 jars: ["a.jar"],
287 compile_dex: true,
288 }
Paul Duffin175947f2021-03-12 21:44:02 +0000289 `)
Bill Peckhambae47492021-01-08 09:34:44 -0800290
291 expectedCpInput := prebuiltHiddenApiDir + "/hiddenapi-flags.csv"
292 expectedCpOutput := buildDir + "/hiddenapi/hiddenapi-flags.csv"
293 expectedFlagsCsv := buildDir + "/hiddenapi/hiddenapi-flags.csv"
294
Paul Duffin175947f2021-03-12 21:44:02 +0000295 foo := result.ModuleForTests("foo", "android_common")
Bill Peckhambae47492021-01-08 09:34:44 -0800296
Paul Duffin175947f2021-03-12 21:44:02 +0000297 hiddenAPI := result.SingletonForTests("hiddenapi")
Bill Peckhambae47492021-01-08 09:34:44 -0800298 cpRule := hiddenAPI.Rule("Cp")
299 actualCpInput := cpRule.BuildParams.Input
300 actualCpOutput := cpRule.BuildParams.Output
301 encodeDexRule := foo.Rule("hiddenAPIEncodeDex")
302 actualFlagsCsv := encodeDexRule.BuildParams.Args["flagsCsv"]
303
Paul Duffin175947f2021-03-12 21:44:02 +0000304 android.AssertStringEquals(t, "hiddenapi cp rule input", expectedCpInput, actualCpInput.String())
Bill Peckhambae47492021-01-08 09:34:44 -0800305
Paul Duffin175947f2021-03-12 21:44:02 +0000306 android.AssertStringEquals(t, "hiddenapi cp rule output", expectedCpOutput, actualCpOutput.String())
Bill Peckhambae47492021-01-08 09:34:44 -0800307
Paul Duffin175947f2021-03-12 21:44:02 +0000308 android.AssertStringEquals(t, "hiddenapi encode dex rule flags csv", expectedFlagsCsv, actualFlagsCsv)
Bill Peckhambae47492021-01-08 09:34:44 -0800309}