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