Liz Kammer | 5ca3a62 | 2020-08-05 15:40:41 -0700 | [diff] [blame] | 1 | // 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 | |
| 15 | package java |
| 16 | |
| 17 | import ( |
Anton Hansson | a2adc37 | 2020-07-03 15:31:32 +0100 | [diff] [blame] | 18 | "fmt" |
Paul Duffin | 175947f | 2021-03-12 21:44:02 +0000 | [diff] [blame] | 19 | "path/filepath" |
Liz Kammer | 5ca3a62 | 2020-08-05 15:40:41 -0700 | [diff] [blame] | 20 | "testing" |
Anton Hansson | a2adc37 | 2020-07-03 15:31:32 +0100 | [diff] [blame] | 21 | |
Paul Duffin | 01289a2 | 2021-02-04 17:49:33 +0000 | [diff] [blame] | 22 | "android/soong/android" |
Martin Stjernholm | 8be1e6d | 2021-09-15 03:34:04 +0100 | [diff] [blame] | 23 | |
Anton Hansson | a2adc37 | 2020-07-03 15:31:32 +0100 | [diff] [blame] | 24 | "github.com/google/blueprint/proptools" |
Liz Kammer | 5ca3a62 | 2020-08-05 15:40:41 -0700 | [diff] [blame] | 25 | ) |
| 26 | |
Paul Duffin | 74431d5 | 2021-04-21 14:10:42 +0100 | [diff] [blame] | 27 | // TODO(b/177892522): Move these tests into a more appropriate place. |
| 28 | |
Paul Duffin | 175947f | 2021-03-12 21:44:02 +0000 | [diff] [blame] | 29 | func fixtureSetPrebuiltHiddenApiDirProductVariable(prebuiltHiddenApiDir *string) android.FixturePreparer { |
| 30 | return android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) { |
| 31 | variables.PrebuiltHiddenApiDir = prebuiltHiddenApiDir |
| 32 | }) |
Liz Kammer | 5ca3a62 | 2020-08-05 15:40:41 -0700 | [diff] [blame] | 33 | } |
| 34 | |
Paul Duffin | 74431d5 | 2021-04-21 14:10:42 +0100 | [diff] [blame] | 35 | var prepareForTestWithDefaultPlatformBootclasspath = android.FixtureAddTextFile("frameworks/base/boot/Android.bp", ` |
| 36 | platform_bootclasspath { |
| 37 | name: "platform-bootclasspath", |
| 38 | } |
| 39 | `) |
| 40 | |
Paul Duffin | 71ae594 | 2021-03-22 15:36:52 +0000 | [diff] [blame] | 41 | var hiddenApiFixtureFactory = android.GroupFixturePreparers( |
Jiakai Zhang | b95998b | 2023-05-11 16:39:27 +0100 | [diff] [blame] | 42 | PrepareForTestWithJavaDefaultModules, |
| 43 | PrepareForTestWithHiddenApiBuildComponents, |
| 44 | ) |
Liz Kammer | 5ca3a62 | 2020-08-05 15:40:41 -0700 | [diff] [blame] | 45 | |
| 46 | func TestHiddenAPISingleton(t *testing.T) { |
Paul Duffin | 79abe57 | 2021-03-29 02:16:14 +0100 | [diff] [blame] | 47 | result := android.GroupFixturePreparers( |
| 48 | hiddenApiFixtureFactory, |
Paul Duffin | 60264a0 | 2021-04-12 20:02:36 +0100 | [diff] [blame] | 49 | FixtureConfigureBootJars("platform:foo"), |
Paul Duffin | 74431d5 | 2021-04-21 14:10:42 +0100 | [diff] [blame] | 50 | prepareForTestWithDefaultPlatformBootclasspath, |
Paul Duffin | 175947f | 2021-03-12 21:44:02 +0000 | [diff] [blame] | 51 | ).RunTestWithBp(t, ` |
Liz Kammer | 5ca3a62 | 2020-08-05 15:40:41 -0700 | [diff] [blame] | 52 | java_library { |
| 53 | name: "foo", |
| 54 | srcs: ["a.java"], |
| 55 | compile_dex: true, |
Paul Duffin | 01289a2 | 2021-02-04 17:49:33 +0000 | [diff] [blame] | 56 | } |
Paul Duffin | 175947f | 2021-03-12 21:44:02 +0000 | [diff] [blame] | 57 | `) |
Liz Kammer | 5ca3a62 | 2020-08-05 15:40:41 -0700 | [diff] [blame] | 58 | |
Paul Duffin | 74431d5 | 2021-04-21 14:10:42 +0100 | [diff] [blame] | 59 | hiddenAPI := result.ModuleForTests("platform-bootclasspath", "android_common") |
| 60 | hiddenapiRule := hiddenAPI.Rule("platform-bootclasspath-monolithic-hiddenapi-stub-flags") |
Paul Duffin | 2f9e71e | 2021-03-22 16:24:49 +0000 | [diff] [blame] | 61 | want := "--boot-dex=out/soong/.intermediates/foo/android_common/aligned/foo.jar" |
Paul Duffin | 175947f | 2021-03-12 21:44:02 +0000 | [diff] [blame] | 62 | android.AssertStringDoesContain(t, "hiddenapi command", hiddenapiRule.RuleParams.Command, want) |
Liz Kammer | 5ca3a62 | 2020-08-05 15:40:41 -0700 | [diff] [blame] | 63 | } |
| 64 | |
Paul Duffin | ec0fe17 | 2021-02-25 15:34:13 +0000 | [diff] [blame] | 65 | func TestHiddenAPISingletonWithSourceAndPrebuiltPreferredButNoDex(t *testing.T) { |
Paul Duffin | b6f53c0 | 2021-05-14 07:52:42 +0100 | [diff] [blame] | 66 | expectedErrorMessage := "module prebuilt_foo{os:android,arch:common} does not provide a dex jar" |
Paul Duffin | 175947f | 2021-03-12 21:44:02 +0000 | [diff] [blame] | 67 | |
Paul Duffin | 79abe57 | 2021-03-29 02:16:14 +0100 | [diff] [blame] | 68 | android.GroupFixturePreparers( |
| 69 | hiddenApiFixtureFactory, |
Paul Duffin | 60264a0 | 2021-04-12 20:02:36 +0100 | [diff] [blame] | 70 | FixtureConfigureBootJars("platform:foo"), |
Paul Duffin | 74431d5 | 2021-04-21 14:10:42 +0100 | [diff] [blame] | 71 | prepareForTestWithDefaultPlatformBootclasspath, |
Paul Duffin | 175947f | 2021-03-12 21:44:02 +0000 | [diff] [blame] | 72 | ).ExtendWithErrorHandler(android.FixtureExpectsAtLeastOneErrorMatchingPattern(expectedErrorMessage)). |
| 73 | RunTestWithBp(t, ` |
Paul Duffin | ec0fe17 | 2021-02-25 15:34:13 +0000 | [diff] [blame] | 74 | java_library { |
| 75 | name: "foo", |
| 76 | srcs: ["a.java"], |
| 77 | compile_dex: true, |
| 78 | } |
| 79 | |
| 80 | java_import { |
| 81 | name: "foo", |
| 82 | jars: ["a.jar"], |
| 83 | prefer: true, |
| 84 | } |
Paul Duffin | 175947f | 2021-03-12 21:44:02 +0000 | [diff] [blame] | 85 | `) |
Paul Duffin | ec0fe17 | 2021-02-25 15:34:13 +0000 | [diff] [blame] | 86 | } |
| 87 | |
Liz Kammer | 5ca3a62 | 2020-08-05 15:40:41 -0700 | [diff] [blame] | 88 | func TestHiddenAPISingletonWithPrebuilt(t *testing.T) { |
Paul Duffin | 79abe57 | 2021-03-29 02:16:14 +0100 | [diff] [blame] | 89 | result := android.GroupFixturePreparers( |
| 90 | hiddenApiFixtureFactory, |
Paul Duffin | 60264a0 | 2021-04-12 20:02:36 +0100 | [diff] [blame] | 91 | FixtureConfigureBootJars("platform:foo"), |
Paul Duffin | 74431d5 | 2021-04-21 14:10:42 +0100 | [diff] [blame] | 92 | prepareForTestWithDefaultPlatformBootclasspath, |
Paul Duffin | 175947f | 2021-03-12 21:44:02 +0000 | [diff] [blame] | 93 | ).RunTestWithBp(t, ` |
Liz Kammer | 5ca3a62 | 2020-08-05 15:40:41 -0700 | [diff] [blame] | 94 | java_import { |
| 95 | name: "foo", |
| 96 | jars: ["a.jar"], |
| 97 | compile_dex: true, |
| 98 | } |
Paul Duffin | 175947f | 2021-03-12 21:44:02 +0000 | [diff] [blame] | 99 | `) |
Liz Kammer | 5ca3a62 | 2020-08-05 15:40:41 -0700 | [diff] [blame] | 100 | |
Paul Duffin | 74431d5 | 2021-04-21 14:10:42 +0100 | [diff] [blame] | 101 | hiddenAPI := result.ModuleForTests("platform-bootclasspath", "android_common") |
| 102 | hiddenapiRule := hiddenAPI.Rule("platform-bootclasspath-monolithic-hiddenapi-stub-flags") |
Paul Duffin | 2f9e71e | 2021-03-22 16:24:49 +0000 | [diff] [blame] | 103 | want := "--boot-dex=out/soong/.intermediates/foo/android_common/aligned/foo.jar" |
Paul Duffin | 175947f | 2021-03-12 21:44:02 +0000 | [diff] [blame] | 104 | android.AssertStringDoesContain(t, "hiddenapi command", hiddenapiRule.RuleParams.Command, want) |
Liz Kammer | 5ca3a62 | 2020-08-05 15:40:41 -0700 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | func TestHiddenAPISingletonWithPrebuiltUseSource(t *testing.T) { |
Paul Duffin | 79abe57 | 2021-03-29 02:16:14 +0100 | [diff] [blame] | 108 | result := android.GroupFixturePreparers( |
| 109 | hiddenApiFixtureFactory, |
Paul Duffin | 60264a0 | 2021-04-12 20:02:36 +0100 | [diff] [blame] | 110 | FixtureConfigureBootJars("platform:foo"), |
Paul Duffin | 74431d5 | 2021-04-21 14:10:42 +0100 | [diff] [blame] | 111 | prepareForTestWithDefaultPlatformBootclasspath, |
Paul Duffin | 175947f | 2021-03-12 21:44:02 +0000 | [diff] [blame] | 112 | ).RunTestWithBp(t, ` |
Liz Kammer | 5ca3a62 | 2020-08-05 15:40:41 -0700 | [diff] [blame] | 113 | java_library { |
| 114 | name: "foo", |
| 115 | srcs: ["a.java"], |
| 116 | compile_dex: true, |
Paul Duffin | 01289a2 | 2021-02-04 17:49:33 +0000 | [diff] [blame] | 117 | } |
Liz Kammer | 5ca3a62 | 2020-08-05 15:40:41 -0700 | [diff] [blame] | 118 | |
| 119 | java_import { |
| 120 | name: "foo", |
| 121 | jars: ["a.jar"], |
| 122 | compile_dex: true, |
| 123 | prefer: false, |
Paul Duffin | 01289a2 | 2021-02-04 17:49:33 +0000 | [diff] [blame] | 124 | } |
Paul Duffin | 175947f | 2021-03-12 21:44:02 +0000 | [diff] [blame] | 125 | `) |
Liz Kammer | 5ca3a62 | 2020-08-05 15:40:41 -0700 | [diff] [blame] | 126 | |
Paul Duffin | 74431d5 | 2021-04-21 14:10:42 +0100 | [diff] [blame] | 127 | hiddenAPI := result.ModuleForTests("platform-bootclasspath", "android_common") |
| 128 | hiddenapiRule := hiddenAPI.Rule("platform-bootclasspath-monolithic-hiddenapi-stub-flags") |
Paul Duffin | 2f9e71e | 2021-03-22 16:24:49 +0000 | [diff] [blame] | 129 | fromSourceJarArg := "--boot-dex=out/soong/.intermediates/foo/android_common/aligned/foo.jar" |
Paul Duffin | 175947f | 2021-03-12 21:44:02 +0000 | [diff] [blame] | 130 | android.AssertStringDoesContain(t, "hiddenapi command", hiddenapiRule.RuleParams.Command, fromSourceJarArg) |
Liz Kammer | 5ca3a62 | 2020-08-05 15:40:41 -0700 | [diff] [blame] | 131 | |
Paul Duffin | 2f9e71e | 2021-03-22 16:24:49 +0000 | [diff] [blame] | 132 | prebuiltJarArg := "--boot-dex=out/soong/.intermediates/foo/android_common/dex/foo.jar" |
Paul Duffin | 175947f | 2021-03-12 21:44:02 +0000 | [diff] [blame] | 133 | android.AssertStringDoesNotContain(t, "hiddenapi command", hiddenapiRule.RuleParams.Command, prebuiltJarArg) |
Liz Kammer | 5ca3a62 | 2020-08-05 15:40:41 -0700 | [diff] [blame] | 134 | } |
| 135 | |
| 136 | func TestHiddenAPISingletonWithPrebuiltOverrideSource(t *testing.T) { |
Paul Duffin | 79abe57 | 2021-03-29 02:16:14 +0100 | [diff] [blame] | 137 | result := android.GroupFixturePreparers( |
| 138 | hiddenApiFixtureFactory, |
Paul Duffin | 60264a0 | 2021-04-12 20:02:36 +0100 | [diff] [blame] | 139 | FixtureConfigureBootJars("platform:foo"), |
Paul Duffin | 74431d5 | 2021-04-21 14:10:42 +0100 | [diff] [blame] | 140 | prepareForTestWithDefaultPlatformBootclasspath, |
Paul Duffin | 175947f | 2021-03-12 21:44:02 +0000 | [diff] [blame] | 141 | ).RunTestWithBp(t, ` |
Liz Kammer | 5ca3a62 | 2020-08-05 15:40:41 -0700 | [diff] [blame] | 142 | java_library { |
| 143 | name: "foo", |
| 144 | srcs: ["a.java"], |
| 145 | compile_dex: true, |
Paul Duffin | 01289a2 | 2021-02-04 17:49:33 +0000 | [diff] [blame] | 146 | } |
Liz Kammer | 5ca3a62 | 2020-08-05 15:40:41 -0700 | [diff] [blame] | 147 | |
| 148 | java_import { |
| 149 | name: "foo", |
| 150 | jars: ["a.jar"], |
| 151 | compile_dex: true, |
| 152 | prefer: true, |
Paul Duffin | 01289a2 | 2021-02-04 17:49:33 +0000 | [diff] [blame] | 153 | } |
Paul Duffin | 175947f | 2021-03-12 21:44:02 +0000 | [diff] [blame] | 154 | `) |
Liz Kammer | 5ca3a62 | 2020-08-05 15:40:41 -0700 | [diff] [blame] | 155 | |
Paul Duffin | 74431d5 | 2021-04-21 14:10:42 +0100 | [diff] [blame] | 156 | hiddenAPI := result.ModuleForTests("platform-bootclasspath", "android_common") |
| 157 | hiddenapiRule := hiddenAPI.Rule("platform-bootclasspath-monolithic-hiddenapi-stub-flags") |
Paul Duffin | 2f9e71e | 2021-03-22 16:24:49 +0000 | [diff] [blame] | 158 | prebuiltJarArg := "--boot-dex=out/soong/.intermediates/prebuilt_foo/android_common/dex/foo.jar" |
Paul Duffin | 175947f | 2021-03-12 21:44:02 +0000 | [diff] [blame] | 159 | android.AssertStringDoesContain(t, "hiddenapi command", hiddenapiRule.RuleParams.Command, prebuiltJarArg) |
Liz Kammer | 5ca3a62 | 2020-08-05 15:40:41 -0700 | [diff] [blame] | 160 | |
Paul Duffin | 2f9e71e | 2021-03-22 16:24:49 +0000 | [diff] [blame] | 161 | fromSourceJarArg := "--boot-dex=out/soong/.intermediates/foo/android_common/aligned/foo.jar" |
Paul Duffin | 175947f | 2021-03-12 21:44:02 +0000 | [diff] [blame] | 162 | android.AssertStringDoesNotContain(t, "hiddenapi command", hiddenapiRule.RuleParams.Command, fromSourceJarArg) |
Liz Kammer | 5ca3a62 | 2020-08-05 15:40:41 -0700 | [diff] [blame] | 163 | } |
Anton Hansson | a2adc37 | 2020-07-03 15:31:32 +0100 | [diff] [blame] | 164 | |
| 165 | func TestHiddenAPISingletonSdks(t *testing.T) { |
| 166 | testCases := []struct { |
| 167 | name string |
| 168 | unbundledBuild bool |
| 169 | publicStub string |
| 170 | systemStub string |
| 171 | testStub string |
| 172 | corePlatformStub string |
Paul Duffin | dc92abb | 2021-03-13 08:28:35 +0000 | [diff] [blame] | 173 | |
| 174 | // Additional test preparer |
| 175 | preparer android.FixturePreparer |
Anton Hansson | a2adc37 | 2020-07-03 15:31:32 +0100 | [diff] [blame] | 176 | }{ |
| 177 | { |
| 178 | name: "testBundled", |
| 179 | unbundledBuild: false, |
Jihoon Kang | bd09345 | 2023-12-26 19:08:01 +0000 | [diff] [blame] | 180 | publicStub: "android_stubs_current_exportable", |
| 181 | systemStub: "android_system_stubs_current_exportable", |
| 182 | testStub: "android_test_stubs_current_exportable", |
| 183 | corePlatformStub: "legacy.core.platform.api.stubs.exportable", |
Paul Duffin | dc92abb | 2021-03-13 08:28:35 +0000 | [diff] [blame] | 184 | preparer: android.GroupFixturePreparers(), |
Anton Hansson | a2adc37 | 2020-07-03 15:31:32 +0100 | [diff] [blame] | 185 | }, { |
| 186 | name: "testUnbundled", |
| 187 | unbundledBuild: true, |
| 188 | publicStub: "sdk_public_current_android", |
| 189 | systemStub: "sdk_system_current_android", |
| 190 | testStub: "sdk_test_current_android", |
Jihoon Kang | bd09345 | 2023-12-26 19:08:01 +0000 | [diff] [blame] | 191 | corePlatformStub: "legacy.core.platform.api.stubs.exportable", |
Paul Duffin | dc92abb | 2021-03-13 08:28:35 +0000 | [diff] [blame] | 192 | preparer: PrepareForTestWithPrebuiltsOfCurrentApi, |
Anton Hansson | a2adc37 | 2020-07-03 15:31:32 +0100 | [diff] [blame] | 193 | }, |
| 194 | } |
| 195 | for _, tc := range testCases { |
| 196 | t.Run(tc.name, func(t *testing.T) { |
Paul Duffin | 79abe57 | 2021-03-29 02:16:14 +0100 | [diff] [blame] | 197 | result := android.GroupFixturePreparers( |
| 198 | hiddenApiFixtureFactory, |
Paul Duffin | dc92abb | 2021-03-13 08:28:35 +0000 | [diff] [blame] | 199 | tc.preparer, |
Paul Duffin | 74431d5 | 2021-04-21 14:10:42 +0100 | [diff] [blame] | 200 | prepareForTestWithDefaultPlatformBootclasspath, |
Paul Duffin | 175947f | 2021-03-12 21:44:02 +0000 | [diff] [blame] | 201 | android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) { |
| 202 | variables.Always_use_prebuilt_sdks = proptools.BoolPtr(tc.unbundledBuild) |
Jihoon Kang | bd09345 | 2023-12-26 19:08:01 +0000 | [diff] [blame] | 203 | variables.BuildFlags = map[string]string{ |
| 204 | "RELEASE_HIDDEN_API_EXPORTABLE_STUBS": "true", |
| 205 | } |
Paul Duffin | 175947f | 2021-03-12 21:44:02 +0000 | [diff] [blame] | 206 | }), |
| 207 | ).RunTest(t) |
Anton Hansson | a2adc37 | 2020-07-03 15:31:32 +0100 | [diff] [blame] | 208 | |
Paul Duffin | 74431d5 | 2021-04-21 14:10:42 +0100 | [diff] [blame] | 209 | hiddenAPI := result.ModuleForTests("platform-bootclasspath", "android_common") |
| 210 | hiddenapiRule := hiddenAPI.Rule("platform-bootclasspath-monolithic-hiddenapi-stub-flags") |
Anton Hansson | a2adc37 | 2020-07-03 15:31:32 +0100 | [diff] [blame] | 211 | wantPublicStubs := "--public-stub-classpath=" + generateSdkDexPath(tc.publicStub, tc.unbundledBuild) |
Paul Duffin | 175947f | 2021-03-12 21:44:02 +0000 | [diff] [blame] | 212 | android.AssertStringDoesContain(t, "hiddenapi command", hiddenapiRule.RuleParams.Command, wantPublicStubs) |
Anton Hansson | a2adc37 | 2020-07-03 15:31:32 +0100 | [diff] [blame] | 213 | |
| 214 | wantSystemStubs := "--system-stub-classpath=" + generateSdkDexPath(tc.systemStub, tc.unbundledBuild) |
Paul Duffin | 175947f | 2021-03-12 21:44:02 +0000 | [diff] [blame] | 215 | android.AssertStringDoesContain(t, "hiddenapi command", hiddenapiRule.RuleParams.Command, wantSystemStubs) |
Anton Hansson | a2adc37 | 2020-07-03 15:31:32 +0100 | [diff] [blame] | 216 | |
| 217 | wantTestStubs := "--test-stub-classpath=" + generateSdkDexPath(tc.testStub, tc.unbundledBuild) |
Paul Duffin | 175947f | 2021-03-12 21:44:02 +0000 | [diff] [blame] | 218 | android.AssertStringDoesContain(t, "hiddenapi command", hiddenapiRule.RuleParams.Command, wantTestStubs) |
Anton Hansson | a2adc37 | 2020-07-03 15:31:32 +0100 | [diff] [blame] | 219 | |
Paul Duffin | 175947f | 2021-03-12 21:44:02 +0000 | [diff] [blame] | 220 | wantCorePlatformStubs := "--core-platform-stub-classpath=" + generateDexPath(defaultJavaDir, tc.corePlatformStub) |
| 221 | android.AssertStringDoesContain(t, "hiddenapi command", hiddenapiRule.RuleParams.Command, wantCorePlatformStubs) |
Anton Hansson | a2adc37 | 2020-07-03 15:31:32 +0100 | [diff] [blame] | 222 | }) |
| 223 | } |
| 224 | } |
| 225 | |
| 226 | func generateDexedPath(subDir, dex, module string) string { |
Paul Duffin | 2f9e71e | 2021-03-22 16:24:49 +0000 | [diff] [blame] | 227 | return fmt.Sprintf("out/soong/.intermediates/%s/android_common/%s/%s.jar", subDir, dex, module) |
Anton Hansson | a2adc37 | 2020-07-03 15:31:32 +0100 | [diff] [blame] | 228 | } |
| 229 | |
Paul Duffin | 175947f | 2021-03-12 21:44:02 +0000 | [diff] [blame] | 230 | func generateDexPath(moduleDir string, module string) string { |
| 231 | return generateDexedPath(filepath.Join(moduleDir, module), "dex", module) |
Anton Hansson | a2adc37 | 2020-07-03 15:31:32 +0100 | [diff] [blame] | 232 | } |
| 233 | |
| 234 | func generateSdkDexPath(module string, unbundled bool) string { |
| 235 | if unbundled { |
| 236 | return generateDexedPath("prebuilts/sdk/"+module, "dex", module) |
| 237 | } |
Paul Duffin | 175947f | 2021-03-12 21:44:02 +0000 | [diff] [blame] | 238 | return generateDexPath(defaultJavaDir, module) |
Anton Hansson | a2adc37 | 2020-07-03 15:31:32 +0100 | [diff] [blame] | 239 | } |
Bill Peckham | bae4749 | 2021-01-08 09:34:44 -0800 | [diff] [blame] | 240 | |
| 241 | func TestHiddenAPISingletonWithPrebuiltCsvFile(t *testing.T) { |
| 242 | |
| 243 | // The idea behind this test is to ensure that when the build is |
| 244 | // confugured with a PrebuiltHiddenApiDir that the rules for the |
| 245 | // hiddenapi singleton copy the prebuilts to the typical output |
| 246 | // location, and then use that output location for the hiddenapi encode |
| 247 | // dex step. |
| 248 | |
| 249 | // Where to find the prebuilt hiddenapi files: |
| 250 | prebuiltHiddenApiDir := "path/to/prebuilt/hiddenapi" |
| 251 | |
Paul Duffin | 79abe57 | 2021-03-29 02:16:14 +0100 | [diff] [blame] | 252 | result := android.GroupFixturePreparers( |
| 253 | hiddenApiFixtureFactory, |
Paul Duffin | 60264a0 | 2021-04-12 20:02:36 +0100 | [diff] [blame] | 254 | FixtureConfigureBootJars("platform:foo"), |
Paul Duffin | 175947f | 2021-03-12 21:44:02 +0000 | [diff] [blame] | 255 | fixtureSetPrebuiltHiddenApiDirProductVariable(&prebuiltHiddenApiDir), |
| 256 | ).RunTestWithBp(t, ` |
Bill Peckham | bae4749 | 2021-01-08 09:34:44 -0800 | [diff] [blame] | 257 | java_import { |
| 258 | name: "foo", |
| 259 | jars: ["a.jar"], |
| 260 | compile_dex: true, |
| 261 | } |
Paul Duffin | 175947f | 2021-03-12 21:44:02 +0000 | [diff] [blame] | 262 | `) |
Bill Peckham | bae4749 | 2021-01-08 09:34:44 -0800 | [diff] [blame] | 263 | |
| 264 | expectedCpInput := prebuiltHiddenApiDir + "/hiddenapi-flags.csv" |
Paul Duffin | 2f9e71e | 2021-03-22 16:24:49 +0000 | [diff] [blame] | 265 | expectedCpOutput := "out/soong/hiddenapi/hiddenapi-flags.csv" |
| 266 | expectedFlagsCsv := "out/soong/hiddenapi/hiddenapi-flags.csv" |
Bill Peckham | bae4749 | 2021-01-08 09:34:44 -0800 | [diff] [blame] | 267 | |
Paul Duffin | 175947f | 2021-03-12 21:44:02 +0000 | [diff] [blame] | 268 | foo := result.ModuleForTests("foo", "android_common") |
Bill Peckham | bae4749 | 2021-01-08 09:34:44 -0800 | [diff] [blame] | 269 | |
Paul Duffin | 175947f | 2021-03-12 21:44:02 +0000 | [diff] [blame] | 270 | hiddenAPI := result.SingletonForTests("hiddenapi") |
Bill Peckham | bae4749 | 2021-01-08 09:34:44 -0800 | [diff] [blame] | 271 | cpRule := hiddenAPI.Rule("Cp") |
| 272 | actualCpInput := cpRule.BuildParams.Input |
| 273 | actualCpOutput := cpRule.BuildParams.Output |
Paul Duffin | a71a67a | 2021-03-29 00:42:57 +0100 | [diff] [blame] | 274 | encodeDexRule := foo.Rule("hiddenAPIEncodeDex") |
Bill Peckham | bae4749 | 2021-01-08 09:34:44 -0800 | [diff] [blame] | 275 | actualFlagsCsv := encodeDexRule.BuildParams.Args["flagsCsv"] |
| 276 | |
Paul Duffin | 2f9e71e | 2021-03-22 16:24:49 +0000 | [diff] [blame] | 277 | android.AssertPathRelativeToTopEquals(t, "hiddenapi cp rule input", expectedCpInput, actualCpInput) |
Bill Peckham | bae4749 | 2021-01-08 09:34:44 -0800 | [diff] [blame] | 278 | |
Paul Duffin | 2f9e71e | 2021-03-22 16:24:49 +0000 | [diff] [blame] | 279 | android.AssertPathRelativeToTopEquals(t, "hiddenapi cp rule output", expectedCpOutput, actualCpOutput) |
Bill Peckham | bae4749 | 2021-01-08 09:34:44 -0800 | [diff] [blame] | 280 | |
Paul Duffin | 175947f | 2021-03-12 21:44:02 +0000 | [diff] [blame] | 281 | android.AssertStringEquals(t, "hiddenapi encode dex rule flags csv", expectedFlagsCsv, actualFlagsCsv) |
Bill Peckham | bae4749 | 2021-01-08 09:34:44 -0800 | [diff] [blame] | 282 | } |
Paul Duffin | 0d58658 | 2021-05-14 15:03:30 +0100 | [diff] [blame] | 283 | |
| 284 | func TestHiddenAPIEncoding_JavaSdkLibrary(t *testing.T) { |
| 285 | |
| 286 | result := android.GroupFixturePreparers( |
| 287 | hiddenApiFixtureFactory, |
| 288 | FixtureConfigureBootJars("platform:foo"), |
| 289 | PrepareForTestWithJavaSdkLibraryFiles, |
| 290 | FixtureWithLastReleaseApis("foo"), |
| 291 | |
| 292 | // Make sure that the frameworks/base/Android.bp file exists as otherwise hidden API encoding |
| 293 | // is disabled. |
| 294 | android.FixtureAddTextFile("frameworks/base/Android.bp", ""), |
| 295 | ).RunTestWithBp(t, ` |
| 296 | java_sdk_library { |
| 297 | name: "foo", |
| 298 | srcs: ["a.java"], |
| 299 | shared_library: false, |
| 300 | compile_dex: true, |
| 301 | public: {enabled: true}, |
| 302 | } |
| 303 | `) |
| 304 | |
| 305 | checkDexEncoded := func(t *testing.T, name, unencodedDexJar, encodedDexJar string) { |
Jihoon Kang | a3a0546 | 2024-04-05 00:36:44 +0000 | [diff] [blame] | 306 | moduleForTests := result.ModuleForTests(name+".impl", "android_common") |
Paul Duffin | 0d58658 | 2021-05-14 15:03:30 +0100 | [diff] [blame] | 307 | |
| 308 | encodeDexRule := moduleForTests.Rule("hiddenAPIEncodeDex") |
| 309 | actualUnencodedDexJar := encodeDexRule.Input |
| 310 | |
| 311 | // Make sure that the module has its dex jar encoded. |
| 312 | android.AssertStringEquals(t, "encode embedded java_library", unencodedDexJar, actualUnencodedDexJar.String()) |
| 313 | |
| 314 | // Make sure that the encoded dex jar is the exported one. |
Spandan Das | 59a4a2b | 2024-01-09 21:35:56 +0000 | [diff] [blame] | 315 | errCtx := moduleErrorfTestCtx{} |
| 316 | exportedDexJar := moduleForTests.Module().(UsesLibraryDependency).DexJarBuildPath(errCtx).Path() |
Paul Duffin | 0d58658 | 2021-05-14 15:03:30 +0100 | [diff] [blame] | 317 | android.AssertPathRelativeToTopEquals(t, "encode embedded java_library", encodedDexJar, exportedDexJar) |
| 318 | } |
| 319 | |
| 320 | // The java_library embedded with the java_sdk_library must be dex encoded. |
| 321 | t.Run("foo", func(t *testing.T) { |
Jihoon Kang | a3a0546 | 2024-04-05 00:36:44 +0000 | [diff] [blame] | 322 | expectedUnencodedDexJar := "out/soong/.intermediates/foo.impl/android_common/aligned/foo.jar" |
| 323 | expectedEncodedDexJar := "out/soong/.intermediates/foo.impl/android_common/hiddenapi/foo.jar" |
Paul Duffin | 0d58658 | 2021-05-14 15:03:30 +0100 | [diff] [blame] | 324 | checkDexEncoded(t, "foo", expectedUnencodedDexJar, expectedEncodedDexJar) |
| 325 | }) |
Paul Duffin | 0d58658 | 2021-05-14 15:03:30 +0100 | [diff] [blame] | 326 | } |