blob: 0f9ef58414fc612069405cd2bf07b3f38053e115 [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 (
18 "android/soong/android"
Anton Hanssona2adc372020-07-03 15:31:32 +010019 "fmt"
Liz Kammer5ca3a622020-08-05 15:40:41 -070020 "strings"
21 "testing"
Anton Hanssona2adc372020-07-03 15:31:32 +010022
23 "github.com/google/blueprint/proptools"
Liz Kammer5ca3a622020-08-05 15:40:41 -070024)
25
Bill Peckhambae47492021-01-08 09:34:44 -080026func testConfigWithBootJars(bp string, bootJars []string, prebuiltHiddenApiDir *string) android.Config {
Liz Kammer5ca3a622020-08-05 15:40:41 -070027 config := testConfig(nil, bp, nil)
Paul Duffine10dfa42020-10-23 21:23:44 +010028 config.TestProductVariables.BootJars = android.CreateTestConfiguredJarList(bootJars)
Bill Peckhambae47492021-01-08 09:34:44 -080029 config.TestProductVariables.PrebuiltHiddenApiDir = prebuiltHiddenApiDir
Liz Kammer5ca3a622020-08-05 15:40:41 -070030 return config
31}
32
Colin Crossae8600b2020-10-29 17:09:13 -070033func testContextWithHiddenAPI(config android.Config) *android.TestContext {
34 ctx := testContext(config)
Liz Kammer5ca3a622020-08-05 15:40:41 -070035 ctx.RegisterSingletonType("hiddenapi", hiddenAPISingletonFactory)
36 return ctx
37}
38
Anton Hanssona2adc372020-07-03 15:31:32 +010039func testHiddenAPIWithConfig(t *testing.T, config android.Config) *android.TestContext {
Liz Kammer5ca3a622020-08-05 15:40:41 -070040 t.Helper()
41
Colin Crossae8600b2020-10-29 17:09:13 -070042 ctx := testContextWithHiddenAPI(config)
Liz Kammer5ca3a622020-08-05 15:40:41 -070043
44 run(t, ctx, config)
Anton Hanssona2adc372020-07-03 15:31:32 +010045 return ctx
46}
Liz Kammer5ca3a622020-08-05 15:40:41 -070047
Bill Peckhambae47492021-01-08 09:34:44 -080048func testHiddenAPIBootJars(t *testing.T, bp string, bootJars []string, prebuiltHiddenApiDir *string) (*android.TestContext, android.Config) {
49 config := testConfigWithBootJars(bp, bootJars, prebuiltHiddenApiDir)
Anton Hanssona2adc372020-07-03 15:31:32 +010050
51 return testHiddenAPIWithConfig(t, config), config
52}
53
54func testHiddenAPIUnbundled(t *testing.T, unbundled bool) (*android.TestContext, android.Config) {
55 config := testConfig(nil, ``, nil)
56 config.TestProductVariables.Always_use_prebuilt_sdks = proptools.BoolPtr(unbundled)
57
58 return testHiddenAPIWithConfig(t, config), config
Liz Kammer5ca3a622020-08-05 15:40:41 -070059}
60
61func TestHiddenAPISingleton(t *testing.T) {
Anton Hanssona2adc372020-07-03 15:31:32 +010062 ctx, _ := testHiddenAPIBootJars(t, `
Liz Kammer5ca3a622020-08-05 15:40:41 -070063 java_library {
64 name: "foo",
65 srcs: ["a.java"],
66 compile_dex: true,
67 }
Bill Peckhambae47492021-01-08 09:34:44 -080068 `, []string{":foo"}, nil)
Liz Kammer5ca3a622020-08-05 15:40:41 -070069
70 hiddenAPI := ctx.SingletonForTests("hiddenapi")
71 hiddenapiRule := hiddenAPI.Rule("hiddenapi")
72 want := "--boot-dex=" + buildDir + "/.intermediates/foo/android_common/aligned/foo.jar"
73 if !strings.Contains(hiddenapiRule.RuleParams.Command, want) {
74 t.Errorf("Expected %s in hiddenapi command, but it was not present: %s", want, hiddenapiRule.RuleParams.Command)
75 }
76}
77
78func TestHiddenAPISingletonWithPrebuilt(t *testing.T) {
Anton Hanssona2adc372020-07-03 15:31:32 +010079 ctx, _ := testHiddenAPIBootJars(t, `
Liz Kammer5ca3a622020-08-05 15:40:41 -070080 java_import {
81 name: "foo",
82 jars: ["a.jar"],
83 compile_dex: true,
84 }
Bill Peckhambae47492021-01-08 09:34:44 -080085 `, []string{":foo"}, nil)
Liz Kammer5ca3a622020-08-05 15:40:41 -070086
87 hiddenAPI := ctx.SingletonForTests("hiddenapi")
88 hiddenapiRule := hiddenAPI.Rule("hiddenapi")
Bill Peckhamff89ffa2020-12-23 16:13:04 -080089 want := "--boot-dex=" + buildDir + "/.intermediates/foo/android_common/aligned/foo.jar"
Liz Kammer5ca3a622020-08-05 15:40:41 -070090 if !strings.Contains(hiddenapiRule.RuleParams.Command, want) {
91 t.Errorf("Expected %s in hiddenapi command, but it was not present: %s", want, hiddenapiRule.RuleParams.Command)
92 }
93}
94
95func TestHiddenAPISingletonWithPrebuiltUseSource(t *testing.T) {
Anton Hanssona2adc372020-07-03 15:31:32 +010096 ctx, _ := testHiddenAPIBootJars(t, `
Liz Kammer5ca3a622020-08-05 15:40:41 -070097 java_library {
98 name: "foo",
99 srcs: ["a.java"],
100 compile_dex: true,
101 }
102
103 java_import {
104 name: "foo",
105 jars: ["a.jar"],
106 compile_dex: true,
107 prefer: false,
108 }
Bill Peckhambae47492021-01-08 09:34:44 -0800109 `, []string{":foo"}, nil)
Liz Kammer5ca3a622020-08-05 15:40:41 -0700110
111 hiddenAPI := ctx.SingletonForTests("hiddenapi")
112 hiddenapiRule := hiddenAPI.Rule("hiddenapi")
113 fromSourceJarArg := "--boot-dex=" + buildDir + "/.intermediates/foo/android_common/aligned/foo.jar"
114 if !strings.Contains(hiddenapiRule.RuleParams.Command, fromSourceJarArg) {
115 t.Errorf("Expected %s in hiddenapi command, but it was not present: %s", fromSourceJarArg, hiddenapiRule.RuleParams.Command)
116 }
117
118 prebuiltJarArg := "--boot-dex=" + buildDir + "/.intermediates/foo/android_common/dex/foo.jar"
119 if strings.Contains(hiddenapiRule.RuleParams.Command, prebuiltJarArg) {
120 t.Errorf("Did not expect %s in hiddenapi command, but it was present: %s", prebuiltJarArg, hiddenapiRule.RuleParams.Command)
121 }
122}
123
124func TestHiddenAPISingletonWithPrebuiltOverrideSource(t *testing.T) {
Anton Hanssona2adc372020-07-03 15:31:32 +0100125 ctx, _ := testHiddenAPIBootJars(t, `
Liz Kammer5ca3a622020-08-05 15:40:41 -0700126 java_library {
127 name: "foo",
128 srcs: ["a.java"],
129 compile_dex: true,
130 }
131
132 java_import {
133 name: "foo",
134 jars: ["a.jar"],
135 compile_dex: true,
136 prefer: true,
137 }
Bill Peckhambae47492021-01-08 09:34:44 -0800138 `, []string{":foo"}, nil)
Liz Kammer5ca3a622020-08-05 15:40:41 -0700139
140 hiddenAPI := ctx.SingletonForTests("hiddenapi")
141 hiddenapiRule := hiddenAPI.Rule("hiddenapi")
142 prebuiltJarArg := "--boot-dex=" + buildDir + "/.intermediates/prebuilt_foo/android_common/dex/foo.jar"
143 if !strings.Contains(hiddenapiRule.RuleParams.Command, prebuiltJarArg) {
144 t.Errorf("Expected %s in hiddenapi command, but it was not present: %s", prebuiltJarArg, hiddenapiRule.RuleParams.Command)
145 }
146
147 fromSourceJarArg := "--boot-dex=" + buildDir + "/.intermediates/foo/android_common/aligned/foo.jar"
148 if strings.Contains(hiddenapiRule.RuleParams.Command, fromSourceJarArg) {
149 t.Errorf("Did not expect %s in hiddenapi command, but it was present: %s", fromSourceJarArg, hiddenapiRule.RuleParams.Command)
150 }
151}
Anton Hanssona2adc372020-07-03 15:31:32 +0100152
153func TestHiddenAPISingletonSdks(t *testing.T) {
154 testCases := []struct {
155 name string
156 unbundledBuild bool
157 publicStub string
158 systemStub string
159 testStub string
160 corePlatformStub string
161 }{
162 {
163 name: "testBundled",
164 unbundledBuild: false,
165 publicStub: "android_stubs_current",
166 systemStub: "android_system_stubs_current",
167 testStub: "android_test_stubs_current",
168 corePlatformStub: "legacy.core.platform.api.stubs",
169 }, {
170 name: "testUnbundled",
171 unbundledBuild: true,
172 publicStub: "sdk_public_current_android",
173 systemStub: "sdk_system_current_android",
174 testStub: "sdk_test_current_android",
175 corePlatformStub: "legacy.core.platform.api.stubs",
176 },
177 }
178 for _, tc := range testCases {
179 t.Run(tc.name, func(t *testing.T) {
180 ctx, _ := testHiddenAPIUnbundled(t, tc.unbundledBuild)
181
182 hiddenAPI := ctx.SingletonForTests("hiddenapi")
183 hiddenapiRule := hiddenAPI.Rule("hiddenapi")
184 wantPublicStubs := "--public-stub-classpath=" + generateSdkDexPath(tc.publicStub, tc.unbundledBuild)
185 if !strings.Contains(hiddenapiRule.RuleParams.Command, wantPublicStubs) {
186 t.Errorf("Expected %s in hiddenapi command, but it was not present: %s", wantPublicStubs, hiddenapiRule.RuleParams.Command)
187 }
188
189 wantSystemStubs := "--system-stub-classpath=" + generateSdkDexPath(tc.systemStub, tc.unbundledBuild)
190 if !strings.Contains(hiddenapiRule.RuleParams.Command, wantSystemStubs) {
191 t.Errorf("Expected %s in hiddenapi command, but it was not present: %s", wantSystemStubs, hiddenapiRule.RuleParams.Command)
192 }
193
194 wantTestStubs := "--test-stub-classpath=" + generateSdkDexPath(tc.testStub, tc.unbundledBuild)
195 if !strings.Contains(hiddenapiRule.RuleParams.Command, wantTestStubs) {
196 t.Errorf("Expected %s in hiddenapi command, but it was not present: %s", wantTestStubs, hiddenapiRule.RuleParams.Command)
197 }
198
199 wantCorePlatformStubs := "--core-platform-stub-classpath=" + generateDexPath(tc.corePlatformStub)
200 if !strings.Contains(hiddenapiRule.RuleParams.Command, wantCorePlatformStubs) {
201 t.Errorf("Expected %s in hiddenapi command, but it was not present: %s", wantCorePlatformStubs, hiddenapiRule.RuleParams.Command)
202 }
203 })
204 }
205}
206
207func generateDexedPath(subDir, dex, module string) string {
208 return fmt.Sprintf("%s/.intermediates/%s/android_common/%s/%s.jar", buildDir, subDir, dex, module)
209}
210
211func generateDexPath(module string) string {
212 return generateDexedPath(module, "dex", module)
213}
214
215func generateSdkDexPath(module string, unbundled bool) string {
216 if unbundled {
217 return generateDexedPath("prebuilts/sdk/"+module, "dex", module)
218 }
219 return generateDexPath(module)
220}
Bill Peckhambae47492021-01-08 09:34:44 -0800221
222func TestHiddenAPISingletonWithPrebuiltCsvFile(t *testing.T) {
223
224 // The idea behind this test is to ensure that when the build is
225 // confugured with a PrebuiltHiddenApiDir that the rules for the
226 // hiddenapi singleton copy the prebuilts to the typical output
227 // location, and then use that output location for the hiddenapi encode
228 // dex step.
229
230 // Where to find the prebuilt hiddenapi files:
231 prebuiltHiddenApiDir := "path/to/prebuilt/hiddenapi"
232
233 ctx, _ := testHiddenAPIBootJars(t, `
234 java_import {
235 name: "foo",
236 jars: ["a.jar"],
237 compile_dex: true,
238 }
239 `, []string{":foo"}, &prebuiltHiddenApiDir)
240
241 expectedCpInput := prebuiltHiddenApiDir + "/hiddenapi-flags.csv"
242 expectedCpOutput := buildDir + "/hiddenapi/hiddenapi-flags.csv"
243 expectedFlagsCsv := buildDir + "/hiddenapi/hiddenapi-flags.csv"
244
245 foo := ctx.ModuleForTests("foo", "android_common")
246
247 hiddenAPI := ctx.SingletonForTests("hiddenapi")
248 cpRule := hiddenAPI.Rule("Cp")
249 actualCpInput := cpRule.BuildParams.Input
250 actualCpOutput := cpRule.BuildParams.Output
251 encodeDexRule := foo.Rule("hiddenAPIEncodeDex")
252 actualFlagsCsv := encodeDexRule.BuildParams.Args["flagsCsv"]
253
254 if actualCpInput.String() != expectedCpInput {
255 t.Errorf("Prebuilt hiddenapi cp rule input mismatch, actual: %s, expected: %s", actualCpInput, expectedCpInput)
256 }
257
258 if actualCpOutput.String() != expectedCpOutput {
259 t.Errorf("Prebuilt hiddenapi cp rule output mismatch, actual: %s, expected: %s", actualCpOutput, expectedCpOutput)
260 }
261
262 if actualFlagsCsv != expectedFlagsCsv {
263 t.Errorf("Prebuilt hiddenapi encode dex rule flags csv mismatch, actual: %s, expected: %s", actualFlagsCsv, expectedFlagsCsv)
264 }
265}