blob: 97dd125fbd7e25227efb8ae9ef1563416b88b6af [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
26func testConfigWithBootJars(bp string, bootJars []string) android.Config {
27 config := testConfig(nil, bp, nil)
28 config.TestProductVariables.BootJars = bootJars
29 return config
30}
31
32func testContextWithHiddenAPI() *android.TestContext {
33 ctx := testContext()
34 ctx.RegisterSingletonType("hiddenapi", hiddenAPISingletonFactory)
35 return ctx
36}
37
Anton Hanssona2adc372020-07-03 15:31:32 +010038func testHiddenAPIWithConfig(t *testing.T, config android.Config) *android.TestContext {
Liz Kammer5ca3a622020-08-05 15:40:41 -070039 t.Helper()
40
Liz Kammer5ca3a622020-08-05 15:40:41 -070041 ctx := testContextWithHiddenAPI()
42
43 run(t, ctx, config)
Anton Hanssona2adc372020-07-03 15:31:32 +010044 return ctx
45}
Liz Kammer5ca3a622020-08-05 15:40:41 -070046
Anton Hanssona2adc372020-07-03 15:31:32 +010047func testHiddenAPIBootJars(t *testing.T, bp string, bootJars []string) (*android.TestContext, android.Config) {
48 config := testConfigWithBootJars(bp, bootJars)
49
50 return testHiddenAPIWithConfig(t, config), config
51}
52
53func testHiddenAPIUnbundled(t *testing.T, unbundled bool) (*android.TestContext, android.Config) {
54 config := testConfig(nil, ``, nil)
55 config.TestProductVariables.Always_use_prebuilt_sdks = proptools.BoolPtr(unbundled)
56
57 return testHiddenAPIWithConfig(t, config), config
Liz Kammer5ca3a622020-08-05 15:40:41 -070058}
59
60func TestHiddenAPISingleton(t *testing.T) {
Colin Cross323dc602020-09-18 14:25:31 -070061 t.Parallel()
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 }
68 `, []string{":foo"})
69
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) {
Colin Cross323dc602020-09-18 14:25:31 -070079 t.Parallel()
Anton Hanssona2adc372020-07-03 15:31:32 +010080 ctx, _ := testHiddenAPIBootJars(t, `
Liz Kammer5ca3a622020-08-05 15:40:41 -070081 java_import {
82 name: "foo",
83 jars: ["a.jar"],
84 compile_dex: true,
85 }
86 `, []string{":foo"})
87
88 hiddenAPI := ctx.SingletonForTests("hiddenapi")
89 hiddenapiRule := hiddenAPI.Rule("hiddenapi")
90 want := "--boot-dex=" + buildDir + "/.intermediates/foo/android_common/dex/foo.jar"
91 if !strings.Contains(hiddenapiRule.RuleParams.Command, want) {
92 t.Errorf("Expected %s in hiddenapi command, but it was not present: %s", want, hiddenapiRule.RuleParams.Command)
93 }
94}
95
96func TestHiddenAPISingletonWithPrebuiltUseSource(t *testing.T) {
Colin Cross323dc602020-09-18 14:25:31 -070097 t.Parallel()
Anton Hanssona2adc372020-07-03 15:31:32 +010098 ctx, _ := testHiddenAPIBootJars(t, `
Liz Kammer5ca3a622020-08-05 15:40:41 -070099 java_library {
100 name: "foo",
101 srcs: ["a.java"],
102 compile_dex: true,
103 }
104
105 java_import {
106 name: "foo",
107 jars: ["a.jar"],
108 compile_dex: true,
109 prefer: false,
110 }
111 `, []string{":foo"})
112
113 hiddenAPI := ctx.SingletonForTests("hiddenapi")
114 hiddenapiRule := hiddenAPI.Rule("hiddenapi")
115 fromSourceJarArg := "--boot-dex=" + buildDir + "/.intermediates/foo/android_common/aligned/foo.jar"
116 if !strings.Contains(hiddenapiRule.RuleParams.Command, fromSourceJarArg) {
117 t.Errorf("Expected %s in hiddenapi command, but it was not present: %s", fromSourceJarArg, hiddenapiRule.RuleParams.Command)
118 }
119
120 prebuiltJarArg := "--boot-dex=" + buildDir + "/.intermediates/foo/android_common/dex/foo.jar"
121 if strings.Contains(hiddenapiRule.RuleParams.Command, prebuiltJarArg) {
122 t.Errorf("Did not expect %s in hiddenapi command, but it was present: %s", prebuiltJarArg, hiddenapiRule.RuleParams.Command)
123 }
124}
125
126func TestHiddenAPISingletonWithPrebuiltOverrideSource(t *testing.T) {
Colin Cross323dc602020-09-18 14:25:31 -0700127 t.Parallel()
Anton Hanssona2adc372020-07-03 15:31:32 +0100128 ctx, _ := testHiddenAPIBootJars(t, `
Liz Kammer5ca3a622020-08-05 15:40:41 -0700129 java_library {
130 name: "foo",
131 srcs: ["a.java"],
132 compile_dex: true,
133 }
134
135 java_import {
136 name: "foo",
137 jars: ["a.jar"],
138 compile_dex: true,
139 prefer: true,
140 }
141 `, []string{":foo"})
142
143 hiddenAPI := ctx.SingletonForTests("hiddenapi")
144 hiddenapiRule := hiddenAPI.Rule("hiddenapi")
145 prebuiltJarArg := "--boot-dex=" + buildDir + "/.intermediates/prebuilt_foo/android_common/dex/foo.jar"
146 if !strings.Contains(hiddenapiRule.RuleParams.Command, prebuiltJarArg) {
147 t.Errorf("Expected %s in hiddenapi command, but it was not present: %s", prebuiltJarArg, hiddenapiRule.RuleParams.Command)
148 }
149
150 fromSourceJarArg := "--boot-dex=" + buildDir + "/.intermediates/foo/android_common/aligned/foo.jar"
151 if strings.Contains(hiddenapiRule.RuleParams.Command, fromSourceJarArg) {
152 t.Errorf("Did not expect %s in hiddenapi command, but it was present: %s", fromSourceJarArg, hiddenapiRule.RuleParams.Command)
153 }
154}
Anton Hanssona2adc372020-07-03 15:31:32 +0100155
156func TestHiddenAPISingletonSdks(t *testing.T) {
Colin Cross323dc602020-09-18 14:25:31 -0700157 t.Parallel()
Anton Hanssona2adc372020-07-03 15:31:32 +0100158 testCases := []struct {
159 name string
160 unbundledBuild bool
161 publicStub string
162 systemStub string
163 testStub string
164 corePlatformStub string
165 }{
166 {
167 name: "testBundled",
168 unbundledBuild: false,
169 publicStub: "android_stubs_current",
170 systemStub: "android_system_stubs_current",
171 testStub: "android_test_stubs_current",
172 corePlatformStub: "legacy.core.platform.api.stubs",
173 }, {
174 name: "testUnbundled",
175 unbundledBuild: true,
176 publicStub: "sdk_public_current_android",
177 systemStub: "sdk_system_current_android",
178 testStub: "sdk_test_current_android",
179 corePlatformStub: "legacy.core.platform.api.stubs",
180 },
181 }
182 for _, tc := range testCases {
183 t.Run(tc.name, func(t *testing.T) {
184 ctx, _ := testHiddenAPIUnbundled(t, tc.unbundledBuild)
185
186 hiddenAPI := ctx.SingletonForTests("hiddenapi")
187 hiddenapiRule := hiddenAPI.Rule("hiddenapi")
188 wantPublicStubs := "--public-stub-classpath=" + generateSdkDexPath(tc.publicStub, tc.unbundledBuild)
189 if !strings.Contains(hiddenapiRule.RuleParams.Command, wantPublicStubs) {
190 t.Errorf("Expected %s in hiddenapi command, but it was not present: %s", wantPublicStubs, hiddenapiRule.RuleParams.Command)
191 }
192
193 wantSystemStubs := "--system-stub-classpath=" + generateSdkDexPath(tc.systemStub, tc.unbundledBuild)
194 if !strings.Contains(hiddenapiRule.RuleParams.Command, wantSystemStubs) {
195 t.Errorf("Expected %s in hiddenapi command, but it was not present: %s", wantSystemStubs, hiddenapiRule.RuleParams.Command)
196 }
197
198 wantTestStubs := "--test-stub-classpath=" + generateSdkDexPath(tc.testStub, tc.unbundledBuild)
199 if !strings.Contains(hiddenapiRule.RuleParams.Command, wantTestStubs) {
200 t.Errorf("Expected %s in hiddenapi command, but it was not present: %s", wantTestStubs, hiddenapiRule.RuleParams.Command)
201 }
202
203 wantCorePlatformStubs := "--core-platform-stub-classpath=" + generateDexPath(tc.corePlatformStub)
204 if !strings.Contains(hiddenapiRule.RuleParams.Command, wantCorePlatformStubs) {
205 t.Errorf("Expected %s in hiddenapi command, but it was not present: %s", wantCorePlatformStubs, hiddenapiRule.RuleParams.Command)
206 }
207 })
208 }
209}
210
211func generateDexedPath(subDir, dex, module string) string {
212 return fmt.Sprintf("%s/.intermediates/%s/android_common/%s/%s.jar", buildDir, subDir, dex, module)
213}
214
215func generateDexPath(module string) string {
216 return generateDexedPath(module, "dex", module)
217}
218
219func generateSdkDexPath(module string, unbundled bool) string {
220 if unbundled {
221 return generateDexedPath("prebuilts/sdk/"+module, "dex", module)
222 }
223 return generateDexPath(module)
224}