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