blob: 5ea9a5bca1ca9b04be46b6c9191edb8d15e656a4 [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 Duffinec0fe172021-02-25 15:34:13 +000053func TestHiddenAPISingletonWithSourceAndPrebuiltPreferredButNoDex(t *testing.T) {
Paul Duffin175947f2021-03-12 21:44:02 +000054 expectedErrorMessage :=
55 "hiddenapi has determined that the source module \"foo\" should be ignored as it has been" +
56 " replaced by the prebuilt module \"prebuilt_foo\" but unfortunately it does not provide a" +
57 " suitable boot dex jar"
58
Paul Duffin79abe572021-03-29 02:16:14 +010059 android.GroupFixturePreparers(
60 hiddenApiFixtureFactory,
Paul Duffin60264a02021-04-12 20:02:36 +010061 FixtureConfigureBootJars("platform:foo"),
Paul Duffin175947f2021-03-12 21:44:02 +000062 ).ExtendWithErrorHandler(android.FixtureExpectsAtLeastOneErrorMatchingPattern(expectedErrorMessage)).
63 RunTestWithBp(t, `
Paul Duffinec0fe172021-02-25 15:34:13 +000064 java_library {
65 name: "foo",
66 srcs: ["a.java"],
67 compile_dex: true,
68 }
69
70 java_import {
71 name: "foo",
72 jars: ["a.jar"],
73 prefer: true,
74 }
Paul Duffin175947f2021-03-12 21:44:02 +000075 `)
Paul Duffinec0fe172021-02-25 15:34:13 +000076}
77
Liz Kammer5ca3a622020-08-05 15:40:41 -070078func TestHiddenAPISingletonWithPrebuilt(t *testing.T) {
Paul Duffin79abe572021-03-29 02:16:14 +010079 result := android.GroupFixturePreparers(
80 hiddenApiFixtureFactory,
Paul Duffin60264a02021-04-12 20:02:36 +010081 FixtureConfigureBootJars("platform:foo"),
Paul Duffin175947f2021-03-12 21:44:02 +000082 ).RunTestWithBp(t, `
Liz Kammer5ca3a622020-08-05 15:40:41 -070083 java_import {
84 name: "foo",
85 jars: ["a.jar"],
86 compile_dex: true,
87 }
Paul Duffin175947f2021-03-12 21:44:02 +000088 `)
Liz Kammer5ca3a622020-08-05 15:40:41 -070089
Paul Duffin175947f2021-03-12 21:44:02 +000090 hiddenAPI := result.SingletonForTests("hiddenapi")
Paul Duffina71a67a2021-03-29 00:42:57 +010091 hiddenapiRule := hiddenAPI.Rule("hiddenapi")
Paul Duffin2f9e71e2021-03-22 16:24:49 +000092 want := "--boot-dex=out/soong/.intermediates/foo/android_common/aligned/foo.jar"
Paul Duffin175947f2021-03-12 21:44:02 +000093 android.AssertStringDoesContain(t, "hiddenapi command", hiddenapiRule.RuleParams.Command, want)
Liz Kammer5ca3a622020-08-05 15:40:41 -070094}
95
96func TestHiddenAPISingletonWithPrebuiltUseSource(t *testing.T) {
Paul Duffin79abe572021-03-29 02:16:14 +010097 result := android.GroupFixturePreparers(
98 hiddenApiFixtureFactory,
Paul Duffin60264a02021-04-12 20:02:36 +010099 FixtureConfigureBootJars("platform:foo"),
Paul Duffin175947f2021-03-12 21:44:02 +0000100 ).RunTestWithBp(t, `
Liz Kammer5ca3a622020-08-05 15:40:41 -0700101 java_library {
102 name: "foo",
103 srcs: ["a.java"],
104 compile_dex: true,
Paul Duffin01289a22021-02-04 17:49:33 +0000105 }
Liz Kammer5ca3a622020-08-05 15:40:41 -0700106
107 java_import {
108 name: "foo",
109 jars: ["a.jar"],
110 compile_dex: true,
111 prefer: false,
Paul Duffin01289a22021-02-04 17:49:33 +0000112 }
Paul Duffin175947f2021-03-12 21:44:02 +0000113 `)
Liz Kammer5ca3a622020-08-05 15:40:41 -0700114
Paul Duffin175947f2021-03-12 21:44:02 +0000115 hiddenAPI := result.SingletonForTests("hiddenapi")
Paul Duffina71a67a2021-03-29 00:42:57 +0100116 hiddenapiRule := hiddenAPI.Rule("hiddenapi")
Paul Duffin2f9e71e2021-03-22 16:24:49 +0000117 fromSourceJarArg := "--boot-dex=out/soong/.intermediates/foo/android_common/aligned/foo.jar"
Paul Duffin175947f2021-03-12 21:44:02 +0000118 android.AssertStringDoesContain(t, "hiddenapi command", hiddenapiRule.RuleParams.Command, fromSourceJarArg)
Liz Kammer5ca3a622020-08-05 15:40:41 -0700119
Paul Duffin2f9e71e2021-03-22 16:24:49 +0000120 prebuiltJarArg := "--boot-dex=out/soong/.intermediates/foo/android_common/dex/foo.jar"
Paul Duffin175947f2021-03-12 21:44:02 +0000121 android.AssertStringDoesNotContain(t, "hiddenapi command", hiddenapiRule.RuleParams.Command, prebuiltJarArg)
Liz Kammer5ca3a622020-08-05 15:40:41 -0700122}
123
124func TestHiddenAPISingletonWithPrebuiltOverrideSource(t *testing.T) {
Paul Duffin79abe572021-03-29 02:16:14 +0100125 result := android.GroupFixturePreparers(
126 hiddenApiFixtureFactory,
Paul Duffin60264a02021-04-12 20:02:36 +0100127 FixtureConfigureBootJars("platform:foo"),
Paul Duffin175947f2021-03-12 21:44:02 +0000128 ).RunTestWithBp(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: true,
Paul Duffin01289a22021-02-04 17:49:33 +0000140 }
Paul Duffin175947f2021-03-12 21:44:02 +0000141 `)
Liz Kammer5ca3a622020-08-05 15:40:41 -0700142
Paul Duffin175947f2021-03-12 21:44:02 +0000143 hiddenAPI := result.SingletonForTests("hiddenapi")
Paul Duffina71a67a2021-03-29 00:42:57 +0100144 hiddenapiRule := hiddenAPI.Rule("hiddenapi")
Paul Duffin2f9e71e2021-03-22 16:24:49 +0000145 prebuiltJarArg := "--boot-dex=out/soong/.intermediates/prebuilt_foo/android_common/dex/foo.jar"
Paul Duffin175947f2021-03-12 21:44:02 +0000146 android.AssertStringDoesContain(t, "hiddenapi command", hiddenapiRule.RuleParams.Command, prebuiltJarArg)
Liz Kammer5ca3a622020-08-05 15:40:41 -0700147
Paul Duffin2f9e71e2021-03-22 16:24:49 +0000148 fromSourceJarArg := "--boot-dex=out/soong/.intermediates/foo/android_common/aligned/foo.jar"
Paul Duffin175947f2021-03-12 21:44:02 +0000149 android.AssertStringDoesNotContain(t, "hiddenapi command", hiddenapiRule.RuleParams.Command, fromSourceJarArg)
Liz Kammer5ca3a622020-08-05 15:40:41 -0700150}
Anton Hanssona2adc372020-07-03 15:31:32 +0100151
152func TestHiddenAPISingletonSdks(t *testing.T) {
153 testCases := []struct {
154 name string
155 unbundledBuild bool
156 publicStub string
157 systemStub string
158 testStub string
159 corePlatformStub string
Paul Duffindc92abb2021-03-13 08:28:35 +0000160
161 // Additional test preparer
162 preparer android.FixturePreparer
Anton Hanssona2adc372020-07-03 15:31:32 +0100163 }{
164 {
165 name: "testBundled",
166 unbundledBuild: false,
167 publicStub: "android_stubs_current",
168 systemStub: "android_system_stubs_current",
169 testStub: "android_test_stubs_current",
170 corePlatformStub: "legacy.core.platform.api.stubs",
Paul Duffindc92abb2021-03-13 08:28:35 +0000171 preparer: android.GroupFixturePreparers(),
Anton Hanssona2adc372020-07-03 15:31:32 +0100172 }, {
173 name: "testUnbundled",
174 unbundledBuild: true,
175 publicStub: "sdk_public_current_android",
176 systemStub: "sdk_system_current_android",
177 testStub: "sdk_test_current_android",
178 corePlatformStub: "legacy.core.platform.api.stubs",
Paul Duffindc92abb2021-03-13 08:28:35 +0000179 preparer: PrepareForTestWithPrebuiltsOfCurrentApi,
Anton Hanssona2adc372020-07-03 15:31:32 +0100180 },
181 }
182 for _, tc := range testCases {
183 t.Run(tc.name, func(t *testing.T) {
Paul Duffin79abe572021-03-29 02:16:14 +0100184 result := android.GroupFixturePreparers(
185 hiddenApiFixtureFactory,
Paul Duffindc92abb2021-03-13 08:28:35 +0000186 tc.preparer,
Paul Duffin175947f2021-03-12 21:44:02 +0000187 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
188 variables.Always_use_prebuilt_sdks = proptools.BoolPtr(tc.unbundledBuild)
189 }),
190 ).RunTest(t)
Anton Hanssona2adc372020-07-03 15:31:32 +0100191
Paul Duffin175947f2021-03-12 21:44:02 +0000192 hiddenAPI := result.SingletonForTests("hiddenapi")
Paul Duffina71a67a2021-03-29 00:42:57 +0100193 hiddenapiRule := hiddenAPI.Rule("hiddenapi")
Anton Hanssona2adc372020-07-03 15:31:32 +0100194 wantPublicStubs := "--public-stub-classpath=" + generateSdkDexPath(tc.publicStub, tc.unbundledBuild)
Paul Duffin175947f2021-03-12 21:44:02 +0000195 android.AssertStringDoesContain(t, "hiddenapi command", hiddenapiRule.RuleParams.Command, wantPublicStubs)
Anton Hanssona2adc372020-07-03 15:31:32 +0100196
197 wantSystemStubs := "--system-stub-classpath=" + generateSdkDexPath(tc.systemStub, tc.unbundledBuild)
Paul Duffin175947f2021-03-12 21:44:02 +0000198 android.AssertStringDoesContain(t, "hiddenapi command", hiddenapiRule.RuleParams.Command, wantSystemStubs)
Anton Hanssona2adc372020-07-03 15:31:32 +0100199
200 wantTestStubs := "--test-stub-classpath=" + generateSdkDexPath(tc.testStub, tc.unbundledBuild)
Paul Duffin175947f2021-03-12 21:44:02 +0000201 android.AssertStringDoesContain(t, "hiddenapi command", hiddenapiRule.RuleParams.Command, wantTestStubs)
Anton Hanssona2adc372020-07-03 15:31:32 +0100202
Paul Duffin175947f2021-03-12 21:44:02 +0000203 wantCorePlatformStubs := "--core-platform-stub-classpath=" + generateDexPath(defaultJavaDir, tc.corePlatformStub)
204 android.AssertStringDoesContain(t, "hiddenapi command", hiddenapiRule.RuleParams.Command, wantCorePlatformStubs)
Anton Hanssona2adc372020-07-03 15:31:32 +0100205 })
206 }
207}
208
209func generateDexedPath(subDir, dex, module string) string {
Paul Duffin2f9e71e2021-03-22 16:24:49 +0000210 return fmt.Sprintf("out/soong/.intermediates/%s/android_common/%s/%s.jar", subDir, dex, module)
Anton Hanssona2adc372020-07-03 15:31:32 +0100211}
212
Paul Duffin175947f2021-03-12 21:44:02 +0000213func generateDexPath(moduleDir string, module string) string {
214 return generateDexedPath(filepath.Join(moduleDir, module), "dex", module)
Anton Hanssona2adc372020-07-03 15:31:32 +0100215}
216
217func generateSdkDexPath(module string, unbundled bool) string {
218 if unbundled {
219 return generateDexedPath("prebuilts/sdk/"+module, "dex", module)
220 }
Paul Duffin175947f2021-03-12 21:44:02 +0000221 return generateDexPath(defaultJavaDir, module)
Anton Hanssona2adc372020-07-03 15:31:32 +0100222}
Bill Peckhambae47492021-01-08 09:34:44 -0800223
224func TestHiddenAPISingletonWithPrebuiltCsvFile(t *testing.T) {
225
226 // The idea behind this test is to ensure that when the build is
227 // confugured with a PrebuiltHiddenApiDir that the rules for the
228 // hiddenapi singleton copy the prebuilts to the typical output
229 // location, and then use that output location for the hiddenapi encode
230 // dex step.
231
232 // Where to find the prebuilt hiddenapi files:
233 prebuiltHiddenApiDir := "path/to/prebuilt/hiddenapi"
234
Paul Duffin79abe572021-03-29 02:16:14 +0100235 result := android.GroupFixturePreparers(
236 hiddenApiFixtureFactory,
Paul Duffin60264a02021-04-12 20:02:36 +0100237 FixtureConfigureBootJars("platform:foo"),
Paul Duffin175947f2021-03-12 21:44:02 +0000238 fixtureSetPrebuiltHiddenApiDirProductVariable(&prebuiltHiddenApiDir),
239 ).RunTestWithBp(t, `
Bill Peckhambae47492021-01-08 09:34:44 -0800240 java_import {
241 name: "foo",
242 jars: ["a.jar"],
243 compile_dex: true,
244 }
Paul Duffin175947f2021-03-12 21:44:02 +0000245 `)
Bill Peckhambae47492021-01-08 09:34:44 -0800246
247 expectedCpInput := prebuiltHiddenApiDir + "/hiddenapi-flags.csv"
Paul Duffin2f9e71e2021-03-22 16:24:49 +0000248 expectedCpOutput := "out/soong/hiddenapi/hiddenapi-flags.csv"
249 expectedFlagsCsv := "out/soong/hiddenapi/hiddenapi-flags.csv"
Bill Peckhambae47492021-01-08 09:34:44 -0800250
Paul Duffin175947f2021-03-12 21:44:02 +0000251 foo := result.ModuleForTests("foo", "android_common")
Bill Peckhambae47492021-01-08 09:34:44 -0800252
Paul Duffin175947f2021-03-12 21:44:02 +0000253 hiddenAPI := result.SingletonForTests("hiddenapi")
Bill Peckhambae47492021-01-08 09:34:44 -0800254 cpRule := hiddenAPI.Rule("Cp")
255 actualCpInput := cpRule.BuildParams.Input
256 actualCpOutput := cpRule.BuildParams.Output
Paul Duffina71a67a2021-03-29 00:42:57 +0100257 encodeDexRule := foo.Rule("hiddenAPIEncodeDex")
Bill Peckhambae47492021-01-08 09:34:44 -0800258 actualFlagsCsv := encodeDexRule.BuildParams.Args["flagsCsv"]
259
Paul Duffin2f9e71e2021-03-22 16:24:49 +0000260 android.AssertPathRelativeToTopEquals(t, "hiddenapi cp rule input", expectedCpInput, actualCpInput)
Bill Peckhambae47492021-01-08 09:34:44 -0800261
Paul Duffin2f9e71e2021-03-22 16:24:49 +0000262 android.AssertPathRelativeToTopEquals(t, "hiddenapi cp rule output", expectedCpOutput, actualCpOutput)
Bill Peckhambae47492021-01-08 09:34:44 -0800263
Paul Duffin175947f2021-03-12 21:44:02 +0000264 android.AssertStringEquals(t, "hiddenapi encode dex rule flags csv", expectedFlagsCsv, actualFlagsCsv)
Bill Peckhambae47492021-01-08 09:34:44 -0800265}