blob: 34a485618b9422c0bc6d3cf85b6a163941f042ab [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)
Paul Duffine10dfa42020-10-23 21:23:44 +010028 config.TestProductVariables.BootJars = android.CreateTestConfiguredJarList(bootJars)
Liz Kammer5ca3a622020-08-05 15:40:41 -070029 return config
30}
31
Colin Crossae8600b2020-10-29 17:09:13 -070032func testContextWithHiddenAPI(config android.Config) *android.TestContext {
33 ctx := testContext(config)
Liz Kammer5ca3a622020-08-05 15:40:41 -070034 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
Colin Crossae8600b2020-10-29 17:09:13 -070041 ctx := testContextWithHiddenAPI(config)
Liz Kammer5ca3a622020-08-05 15:40:41 -070042
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) {
Anton Hanssona2adc372020-07-03 15:31:32 +010061 ctx, _ := testHiddenAPIBootJars(t, `
Liz Kammer5ca3a622020-08-05 15:40:41 -070062 java_library {
63 name: "foo",
64 srcs: ["a.java"],
65 compile_dex: true,
66 }
67 `, []string{":foo"})
68
69 hiddenAPI := ctx.SingletonForTests("hiddenapi")
70 hiddenapiRule := hiddenAPI.Rule("hiddenapi")
71 want := "--boot-dex=" + buildDir + "/.intermediates/foo/android_common/aligned/foo.jar"
72 if !strings.Contains(hiddenapiRule.RuleParams.Command, want) {
73 t.Errorf("Expected %s in hiddenapi command, but it was not present: %s", want, hiddenapiRule.RuleParams.Command)
74 }
75}
76
77func TestHiddenAPISingletonWithPrebuilt(t *testing.T) {
Anton Hanssona2adc372020-07-03 15:31:32 +010078 ctx, _ := testHiddenAPIBootJars(t, `
Liz Kammer5ca3a622020-08-05 15:40:41 -070079 java_import {
80 name: "foo",
81 jars: ["a.jar"],
82 compile_dex: true,
83 }
84 `, []string{":foo"})
85
86 hiddenAPI := ctx.SingletonForTests("hiddenapi")
87 hiddenapiRule := hiddenAPI.Rule("hiddenapi")
88 want := "--boot-dex=" + buildDir + "/.intermediates/foo/android_common/dex/foo.jar"
89 if !strings.Contains(hiddenapiRule.RuleParams.Command, want) {
90 t.Errorf("Expected %s in hiddenapi command, but it was not present: %s", want, hiddenapiRule.RuleParams.Command)
91 }
92}
93
94func TestHiddenAPISingletonWithPrebuiltUseSource(t *testing.T) {
Anton Hanssona2adc372020-07-03 15:31:32 +010095 ctx, _ := testHiddenAPIBootJars(t, `
Liz Kammer5ca3a622020-08-05 15:40:41 -070096 java_library {
97 name: "foo",
98 srcs: ["a.java"],
99 compile_dex: true,
100 }
101
102 java_import {
103 name: "foo",
104 jars: ["a.jar"],
105 compile_dex: true,
106 prefer: false,
107 }
108 `, []string{":foo"})
109
110 hiddenAPI := ctx.SingletonForTests("hiddenapi")
111 hiddenapiRule := hiddenAPI.Rule("hiddenapi")
112 fromSourceJarArg := "--boot-dex=" + buildDir + "/.intermediates/foo/android_common/aligned/foo.jar"
113 if !strings.Contains(hiddenapiRule.RuleParams.Command, fromSourceJarArg) {
114 t.Errorf("Expected %s in hiddenapi command, but it was not present: %s", fromSourceJarArg, hiddenapiRule.RuleParams.Command)
115 }
116
117 prebuiltJarArg := "--boot-dex=" + buildDir + "/.intermediates/foo/android_common/dex/foo.jar"
118 if strings.Contains(hiddenapiRule.RuleParams.Command, prebuiltJarArg) {
119 t.Errorf("Did not expect %s in hiddenapi command, but it was present: %s", prebuiltJarArg, hiddenapiRule.RuleParams.Command)
120 }
121}
122
123func TestHiddenAPISingletonWithPrebuiltOverrideSource(t *testing.T) {
Anton Hanssona2adc372020-07-03 15:31:32 +0100124 ctx, _ := testHiddenAPIBootJars(t, `
Liz Kammer5ca3a622020-08-05 15:40:41 -0700125 java_library {
126 name: "foo",
127 srcs: ["a.java"],
128 compile_dex: true,
129 }
130
131 java_import {
132 name: "foo",
133 jars: ["a.jar"],
134 compile_dex: true,
135 prefer: true,
136 }
137 `, []string{":foo"})
138
139 hiddenAPI := ctx.SingletonForTests("hiddenapi")
140 hiddenapiRule := hiddenAPI.Rule("hiddenapi")
141 prebuiltJarArg := "--boot-dex=" + buildDir + "/.intermediates/prebuilt_foo/android_common/dex/foo.jar"
142 if !strings.Contains(hiddenapiRule.RuleParams.Command, prebuiltJarArg) {
143 t.Errorf("Expected %s in hiddenapi command, but it was not present: %s", prebuiltJarArg, hiddenapiRule.RuleParams.Command)
144 }
145
146 fromSourceJarArg := "--boot-dex=" + buildDir + "/.intermediates/foo/android_common/aligned/foo.jar"
147 if strings.Contains(hiddenapiRule.RuleParams.Command, fromSourceJarArg) {
148 t.Errorf("Did not expect %s in hiddenapi command, but it was present: %s", fromSourceJarArg, hiddenapiRule.RuleParams.Command)
149 }
150}
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
160 }{
161 {
162 name: "testBundled",
163 unbundledBuild: false,
164 publicStub: "android_stubs_current",
165 systemStub: "android_system_stubs_current",
166 testStub: "android_test_stubs_current",
167 corePlatformStub: "legacy.core.platform.api.stubs",
168 }, {
169 name: "testUnbundled",
170 unbundledBuild: true,
171 publicStub: "sdk_public_current_android",
172 systemStub: "sdk_system_current_android",
173 testStub: "sdk_test_current_android",
174 corePlatformStub: "legacy.core.platform.api.stubs",
175 },
176 }
177 for _, tc := range testCases {
178 t.Run(tc.name, func(t *testing.T) {
179 ctx, _ := testHiddenAPIUnbundled(t, tc.unbundledBuild)
180
181 hiddenAPI := ctx.SingletonForTests("hiddenapi")
182 hiddenapiRule := hiddenAPI.Rule("hiddenapi")
183 wantPublicStubs := "--public-stub-classpath=" + generateSdkDexPath(tc.publicStub, tc.unbundledBuild)
184 if !strings.Contains(hiddenapiRule.RuleParams.Command, wantPublicStubs) {
185 t.Errorf("Expected %s in hiddenapi command, but it was not present: %s", wantPublicStubs, hiddenapiRule.RuleParams.Command)
186 }
187
188 wantSystemStubs := "--system-stub-classpath=" + generateSdkDexPath(tc.systemStub, tc.unbundledBuild)
189 if !strings.Contains(hiddenapiRule.RuleParams.Command, wantSystemStubs) {
190 t.Errorf("Expected %s in hiddenapi command, but it was not present: %s", wantSystemStubs, hiddenapiRule.RuleParams.Command)
191 }
192
193 wantTestStubs := "--test-stub-classpath=" + generateSdkDexPath(tc.testStub, tc.unbundledBuild)
194 if !strings.Contains(hiddenapiRule.RuleParams.Command, wantTestStubs) {
195 t.Errorf("Expected %s in hiddenapi command, but it was not present: %s", wantTestStubs, hiddenapiRule.RuleParams.Command)
196 }
197
198 wantCorePlatformStubs := "--core-platform-stub-classpath=" + generateDexPath(tc.corePlatformStub)
199 if !strings.Contains(hiddenapiRule.RuleParams.Command, wantCorePlatformStubs) {
200 t.Errorf("Expected %s in hiddenapi command, but it was not present: %s", wantCorePlatformStubs, hiddenapiRule.RuleParams.Command)
201 }
202 })
203 }
204}
205
206func generateDexedPath(subDir, dex, module string) string {
207 return fmt.Sprintf("%s/.intermediates/%s/android_common/%s/%s.jar", buildDir, subDir, dex, module)
208}
209
210func generateDexPath(module string) string {
211 return generateDexedPath(module, "dex", module)
212}
213
214func generateSdkDexPath(module string, unbundled bool) string {
215 if unbundled {
216 return generateDexedPath("prebuilts/sdk/"+module, "dex", module)
217 }
218 return generateDexPath(module)
219}