Paul Duffin | 3451e16 | 2021-01-20 15:16:56 +0000 | [diff] [blame] | 1 | // Copyright (C) 2021 The Android Open Source Project |
| 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 ( |
| 18 | "testing" |
Paul Duffin | 837486d | 2021-03-23 23:13:53 +0000 | [diff] [blame] | 19 | |
| 20 | "android/soong/android" |
| 21 | "android/soong/dexpreopt" |
Paul Duffin | 3451e16 | 2021-01-20 15:16:56 +0000 | [diff] [blame] | 22 | ) |
| 23 | |
Paul Duffin | 7771eba | 2021-04-23 14:25:28 +0100 | [diff] [blame] | 24 | // Contains some simple tests for bootclasspath_fragment logic, additional tests can be found in |
| 25 | // apex/bootclasspath_fragment_test.go as the ART boot image requires modules from the ART apex. |
Paul Duffin | 3451e16 | 2021-01-20 15:16:56 +0000 | [diff] [blame] | 26 | |
Paul Duffin | 7771eba | 2021-04-23 14:25:28 +0100 | [diff] [blame] | 27 | var prepareForTestWithBootclasspathFragment = android.GroupFixturePreparers( |
Paul Duffin | 837486d | 2021-03-23 23:13:53 +0000 | [diff] [blame] | 28 | PrepareForTestWithJavaDefaultModules, |
| 29 | dexpreopt.PrepareForTestByEnablingDexpreopt, |
| 30 | ) |
| 31 | |
Paul Duffin | 8018e50 | 2021-05-21 19:28:09 +0100 | [diff] [blame] | 32 | func TestBootclasspathFragment_UnknownImageName(t *testing.T) { |
Paul Duffin | 7771eba | 2021-04-23 14:25:28 +0100 | [diff] [blame] | 33 | prepareForTestWithBootclasspathFragment. |
Paul Duffin | 837486d | 2021-03-23 23:13:53 +0000 | [diff] [blame] | 34 | ExtendWithErrorHandler(android.FixtureExpectsAtLeastOneErrorMatchingPattern( |
Paul Duffin | 8018e50 | 2021-05-21 19:28:09 +0100 | [diff] [blame] | 35 | `\Qimage_name: unknown image name "unknown", expected "art"\E`)). |
Paul Duffin | 837486d | 2021-03-23 23:13:53 +0000 | [diff] [blame] | 36 | RunTestWithBp(t, ` |
Paul Duffin | 7771eba | 2021-04-23 14:25:28 +0100 | [diff] [blame] | 37 | bootclasspath_fragment { |
| 38 | name: "unknown-bootclasspath-fragment", |
Paul Duffin | 837486d | 2021-03-23 23:13:53 +0000 | [diff] [blame] | 39 | image_name: "unknown", |
Paul Duffin | 8018e50 | 2021-05-21 19:28:09 +0100 | [diff] [blame] | 40 | contents: ["foo"], |
Paul Duffin | 837486d | 2021-03-23 23:13:53 +0000 | [diff] [blame] | 41 | } |
| 42 | `) |
Paul Duffin | 3451e16 | 2021-01-20 15:16:56 +0000 | [diff] [blame] | 43 | } |
Paul Duffin | f7f65da | 2021-03-10 15:00:46 +0000 | [diff] [blame] | 44 | |
Paul Duffin | 8018e50 | 2021-05-21 19:28:09 +0100 | [diff] [blame] | 45 | func TestPrebuiltBootclasspathFragment_UnknownImageName(t *testing.T) { |
Paul Duffin | 7771eba | 2021-04-23 14:25:28 +0100 | [diff] [blame] | 46 | prepareForTestWithBootclasspathFragment. |
Paul Duffin | 4b64ba0 | 2021-03-29 11:02:53 +0100 | [diff] [blame] | 47 | ExtendWithErrorHandler(android.FixtureExpectsAtLeastOneErrorMatchingPattern( |
Paul Duffin | 8018e50 | 2021-05-21 19:28:09 +0100 | [diff] [blame] | 48 | `\Qimage_name: unknown image name "unknown", expected "art"\E`)). |
Paul Duffin | 837486d | 2021-03-23 23:13:53 +0000 | [diff] [blame] | 49 | RunTestWithBp(t, ` |
Paul Duffin | 7771eba | 2021-04-23 14:25:28 +0100 | [diff] [blame] | 50 | prebuilt_bootclasspath_fragment { |
| 51 | name: "unknown-bootclasspath-fragment", |
Paul Duffin | 837486d | 2021-03-23 23:13:53 +0000 | [diff] [blame] | 52 | image_name: "unknown", |
Paul Duffin | 8018e50 | 2021-05-21 19:28:09 +0100 | [diff] [blame] | 53 | contents: ["foo"], |
Paul Duffin | 837486d | 2021-03-23 23:13:53 +0000 | [diff] [blame] | 54 | } |
| 55 | `) |
Paul Duffin | f7f65da | 2021-03-10 15:00:46 +0000 | [diff] [blame] | 56 | } |
Paul Duffin | c7ef989 | 2021-03-23 23:21:59 +0000 | [diff] [blame] | 57 | |
Paul Duffin | 7771eba | 2021-04-23 14:25:28 +0100 | [diff] [blame] | 58 | func TestBootclasspathFragmentInconsistentArtConfiguration_Platform(t *testing.T) { |
Paul Duffin | c7ef989 | 2021-03-23 23:21:59 +0000 | [diff] [blame] | 59 | android.GroupFixturePreparers( |
Paul Duffin | 7771eba | 2021-04-23 14:25:28 +0100 | [diff] [blame] | 60 | prepareForTestWithBootclasspathFragment, |
Paul Duffin | c7ef989 | 2021-03-23 23:21:59 +0000 | [diff] [blame] | 61 | dexpreopt.FixtureSetArtBootJars("platform:foo", "apex:bar"), |
| 62 | ). |
| 63 | ExtendWithErrorHandler(android.FixtureExpectsAtLeastOneErrorMatchingPattern( |
| 64 | `\QArtApexJars is invalid as it requests a platform variant of "foo"\E`)). |
| 65 | RunTestWithBp(t, ` |
Paul Duffin | 7771eba | 2021-04-23 14:25:28 +0100 | [diff] [blame] | 66 | bootclasspath_fragment { |
| 67 | name: "bootclasspath-fragment", |
Paul Duffin | c7ef989 | 2021-03-23 23:21:59 +0000 | [diff] [blame] | 68 | image_name: "art", |
Paul Duffin | 8018e50 | 2021-05-21 19:28:09 +0100 | [diff] [blame] | 69 | contents: ["foo", "bar"], |
Paul Duffin | c7ef989 | 2021-03-23 23:21:59 +0000 | [diff] [blame] | 70 | apex_available: [ |
| 71 | "apex", |
| 72 | ], |
| 73 | } |
| 74 | `) |
| 75 | } |
| 76 | |
Paul Duffin | 7771eba | 2021-04-23 14:25:28 +0100 | [diff] [blame] | 77 | func TestBootclasspathFragmentInconsistentArtConfiguration_ApexMixture(t *testing.T) { |
Paul Duffin | c7ef989 | 2021-03-23 23:21:59 +0000 | [diff] [blame] | 78 | android.GroupFixturePreparers( |
Paul Duffin | 7771eba | 2021-04-23 14:25:28 +0100 | [diff] [blame] | 79 | prepareForTestWithBootclasspathFragment, |
Paul Duffin | c7ef989 | 2021-03-23 23:21:59 +0000 | [diff] [blame] | 80 | dexpreopt.FixtureSetArtBootJars("apex1:foo", "apex2:bar"), |
| 81 | ). |
| 82 | ExtendWithErrorHandler(android.FixtureExpectsAtLeastOneErrorMatchingPattern( |
| 83 | `\QArtApexJars configuration is inconsistent, expected all jars to be in the same apex but it specifies apex "apex1" and "apex2"\E`)). |
| 84 | RunTestWithBp(t, ` |
Paul Duffin | 7771eba | 2021-04-23 14:25:28 +0100 | [diff] [blame] | 85 | bootclasspath_fragment { |
| 86 | name: "bootclasspath-fragment", |
Paul Duffin | c7ef989 | 2021-03-23 23:21:59 +0000 | [diff] [blame] | 87 | image_name: "art", |
Paul Duffin | 8018e50 | 2021-05-21 19:28:09 +0100 | [diff] [blame] | 88 | contents: ["foo", "bar"], |
Paul Duffin | c7ef989 | 2021-03-23 23:21:59 +0000 | [diff] [blame] | 89 | apex_available: [ |
| 90 | "apex1", |
| 91 | "apex2", |
| 92 | ], |
| 93 | } |
| 94 | `) |
| 95 | } |
Paul Duffin | 82886d6 | 2021-03-24 01:34:57 +0000 | [diff] [blame] | 96 | |
Paul Duffin | c7d1644 | 2021-04-23 13:55:49 +0100 | [diff] [blame] | 97 | func TestBootclasspathFragment_Coverage(t *testing.T) { |
| 98 | prepareForTestWithFrameworkCoverage := android.FixtureMergeEnv(map[string]string{ |
| 99 | "EMMA_INSTRUMENT": "true", |
| 100 | "EMMA_INSTRUMENT_FRAMEWORK": "true", |
| 101 | }) |
| 102 | |
| 103 | prepareWithBp := android.FixtureWithRootAndroidBp(` |
| 104 | bootclasspath_fragment { |
| 105 | name: "myfragment", |
| 106 | contents: [ |
| 107 | "mybootlib", |
| 108 | ], |
Paul Duffin | 895c714 | 2021-04-25 13:40:15 +0100 | [diff] [blame] | 109 | api: { |
| 110 | stub_libs: [ |
| 111 | "mysdklibrary", |
| 112 | ], |
| 113 | }, |
Paul Duffin | c7d1644 | 2021-04-23 13:55:49 +0100 | [diff] [blame] | 114 | coverage: { |
| 115 | contents: [ |
| 116 | "coveragelib", |
| 117 | ], |
Paul Duffin | 895c714 | 2021-04-25 13:40:15 +0100 | [diff] [blame] | 118 | api: { |
| 119 | stub_libs: [ |
| 120 | "mycoveragestubs", |
| 121 | ], |
| 122 | }, |
Paul Duffin | c7d1644 | 2021-04-23 13:55:49 +0100 | [diff] [blame] | 123 | }, |
| 124 | } |
| 125 | |
| 126 | java_library { |
| 127 | name: "mybootlib", |
| 128 | srcs: ["Test.java"], |
| 129 | system_modules: "none", |
| 130 | sdk_version: "none", |
| 131 | compile_dex: true, |
| 132 | } |
| 133 | |
| 134 | java_library { |
| 135 | name: "coveragelib", |
| 136 | srcs: ["Test.java"], |
| 137 | system_modules: "none", |
| 138 | sdk_version: "none", |
| 139 | compile_dex: true, |
| 140 | } |
Paul Duffin | 895c714 | 2021-04-25 13:40:15 +0100 | [diff] [blame] | 141 | |
| 142 | java_sdk_library { |
| 143 | name: "mysdklibrary", |
| 144 | srcs: ["Test.java"], |
| 145 | compile_dex: true, |
| 146 | public: {enabled: true}, |
| 147 | system: {enabled: true}, |
| 148 | } |
| 149 | |
| 150 | java_sdk_library { |
| 151 | name: "mycoveragestubs", |
| 152 | srcs: ["Test.java"], |
| 153 | compile_dex: true, |
| 154 | public: {enabled: true}, |
| 155 | } |
Paul Duffin | c7d1644 | 2021-04-23 13:55:49 +0100 | [diff] [blame] | 156 | `) |
| 157 | |
| 158 | checkContents := func(t *testing.T, result *android.TestResult, expected ...string) { |
| 159 | module := result.Module("myfragment", "android_common").(*BootclasspathFragmentModule) |
| 160 | android.AssertArrayString(t, "contents property", expected, module.properties.Contents) |
| 161 | } |
| 162 | |
Paul Duffin | 895c714 | 2021-04-25 13:40:15 +0100 | [diff] [blame] | 163 | preparer := android.GroupFixturePreparers( |
| 164 | prepareForTestWithBootclasspathFragment, |
| 165 | PrepareForTestWithJavaSdkLibraryFiles, |
| 166 | FixtureWithLastReleaseApis("mysdklibrary", "mycoveragestubs"), |
satayev | abcd597 | 2021-08-06 17:49:46 +0100 | [diff] [blame^] | 167 | FixtureConfigureApexBootJars("someapex:mybootlib"), |
Paul Duffin | 895c714 | 2021-04-25 13:40:15 +0100 | [diff] [blame] | 168 | prepareWithBp, |
| 169 | ) |
| 170 | |
Paul Duffin | c7d1644 | 2021-04-23 13:55:49 +0100 | [diff] [blame] | 171 | t.Run("without coverage", func(t *testing.T) { |
Paul Duffin | 895c714 | 2021-04-25 13:40:15 +0100 | [diff] [blame] | 172 | result := preparer.RunTest(t) |
Paul Duffin | c7d1644 | 2021-04-23 13:55:49 +0100 | [diff] [blame] | 173 | checkContents(t, result, "mybootlib") |
| 174 | }) |
| 175 | |
| 176 | t.Run("with coverage", func(t *testing.T) { |
| 177 | result := android.GroupFixturePreparers( |
Paul Duffin | c7d1644 | 2021-04-23 13:55:49 +0100 | [diff] [blame] | 178 | prepareForTestWithFrameworkCoverage, |
Paul Duffin | 895c714 | 2021-04-25 13:40:15 +0100 | [diff] [blame] | 179 | preparer, |
Paul Duffin | c7d1644 | 2021-04-23 13:55:49 +0100 | [diff] [blame] | 180 | ).RunTest(t) |
| 181 | checkContents(t, result, "mybootlib", "coveragelib") |
| 182 | }) |
| 183 | } |
Paul Duffin | 1093158 | 2021-04-25 10:13:54 +0100 | [diff] [blame] | 184 | |
| 185 | func TestBootclasspathFragment_StubLibs(t *testing.T) { |
| 186 | result := android.GroupFixturePreparers( |
| 187 | prepareForTestWithBootclasspathFragment, |
| 188 | PrepareForTestWithJavaSdkLibraryFiles, |
Paul Duffin | 34827d4 | 2021-05-13 21:25:05 +0100 | [diff] [blame] | 189 | FixtureWithLastReleaseApis("mysdklibrary", "myothersdklibrary", "mycoreplatform"), |
satayev | abcd597 | 2021-08-06 17:49:46 +0100 | [diff] [blame^] | 190 | FixtureConfigureApexBootJars("someapex:mysdklibrary"), |
Paul Duffin | 1093158 | 2021-04-25 10:13:54 +0100 | [diff] [blame] | 191 | ).RunTestWithBp(t, ` |
| 192 | bootclasspath_fragment { |
| 193 | name: "myfragment", |
| 194 | contents: ["mysdklibrary"], |
| 195 | api: { |
| 196 | stub_libs: [ |
| 197 | "mystublib", |
Paul Duffin | 34827d4 | 2021-05-13 21:25:05 +0100 | [diff] [blame] | 198 | "myothersdklibrary", |
Paul Duffin | 1093158 | 2021-04-25 10:13:54 +0100 | [diff] [blame] | 199 | ], |
| 200 | }, |
| 201 | core_platform_api: { |
Paul Duffin | 3f0290e | 2021-06-30 18:25:36 +0100 | [diff] [blame] | 202 | stub_libs: ["mycoreplatform.stubs"], |
Paul Duffin | 1093158 | 2021-04-25 10:13:54 +0100 | [diff] [blame] | 203 | }, |
| 204 | } |
| 205 | |
| 206 | java_library { |
| 207 | name: "mystublib", |
| 208 | srcs: ["Test.java"], |
| 209 | system_modules: "none", |
| 210 | sdk_version: "none", |
| 211 | compile_dex: true, |
| 212 | } |
| 213 | |
| 214 | java_sdk_library { |
| 215 | name: "mysdklibrary", |
| 216 | srcs: ["a.java"], |
Paul Duffin | f4600f6 | 2021-05-13 22:34:45 +0100 | [diff] [blame] | 217 | shared_library: false, |
Paul Duffin | 1093158 | 2021-04-25 10:13:54 +0100 | [diff] [blame] | 218 | public: {enabled: true}, |
| 219 | system: {enabled: true}, |
| 220 | } |
| 221 | |
| 222 | java_sdk_library { |
Paul Duffin | 34827d4 | 2021-05-13 21:25:05 +0100 | [diff] [blame] | 223 | name: "myothersdklibrary", |
| 224 | srcs: ["a.java"], |
| 225 | shared_library: false, |
| 226 | public: {enabled: true}, |
| 227 | } |
| 228 | |
| 229 | java_sdk_library { |
Paul Duffin | 1093158 | 2021-04-25 10:13:54 +0100 | [diff] [blame] | 230 | name: "mycoreplatform", |
| 231 | srcs: ["a.java"], |
Paul Duffin | f4600f6 | 2021-05-13 22:34:45 +0100 | [diff] [blame] | 232 | shared_library: false, |
Paul Duffin | 1093158 | 2021-04-25 10:13:54 +0100 | [diff] [blame] | 233 | public: {enabled: true}, |
| 234 | } |
| 235 | `) |
| 236 | |
| 237 | fragment := result.Module("myfragment", "android_common") |
Paul Duffin | 18cf197 | 2021-05-21 22:46:59 +0100 | [diff] [blame] | 238 | info := result.ModuleProvider(fragment, HiddenAPIInfoProvider).(HiddenAPIInfo) |
Paul Duffin | 1093158 | 2021-04-25 10:13:54 +0100 | [diff] [blame] | 239 | |
| 240 | stubsJar := "out/soong/.intermediates/mystublib/android_common/dex/mystublib.jar" |
| 241 | |
Paul Duffin | 34827d4 | 2021-05-13 21:25:05 +0100 | [diff] [blame] | 242 | // Stubs jars for mysdklibrary |
Paul Duffin | 1093158 | 2021-04-25 10:13:54 +0100 | [diff] [blame] | 243 | publicStubsJar := "out/soong/.intermediates/mysdklibrary.stubs/android_common/dex/mysdklibrary.stubs.jar" |
Paul Duffin | 1093158 | 2021-04-25 10:13:54 +0100 | [diff] [blame] | 244 | systemStubsJar := "out/soong/.intermediates/mysdklibrary.stubs.system/android_common/dex/mysdklibrary.stubs.system.jar" |
Paul Duffin | 1093158 | 2021-04-25 10:13:54 +0100 | [diff] [blame] | 245 | |
Paul Duffin | 34827d4 | 2021-05-13 21:25:05 +0100 | [diff] [blame] | 246 | // Stubs jars for myothersdklibrary |
| 247 | otherPublicStubsJar := "out/soong/.intermediates/myothersdklibrary.stubs/android_common/dex/myothersdklibrary.stubs.jar" |
| 248 | |
| 249 | // Check that SdkPublic uses public stubs for all sdk libraries. |
Paul Duffin | 280a31a | 2021-06-27 20:28:29 +0100 | [diff] [blame] | 250 | android.AssertPathsRelativeToTopEquals(t, "public dex stubs jar", []string{otherPublicStubsJar, publicStubsJar, stubsJar}, info.TransitiveStubDexJarsByScope.StubDexJarsForScope(PublicHiddenAPIScope)) |
Paul Duffin | 34827d4 | 2021-05-13 21:25:05 +0100 | [diff] [blame] | 251 | |
| 252 | // Check that SdkSystem uses system stubs for mysdklibrary and public stubs for myothersdklibrary |
| 253 | // as it does not provide system stubs. |
Paul Duffin | 280a31a | 2021-06-27 20:28:29 +0100 | [diff] [blame] | 254 | android.AssertPathsRelativeToTopEquals(t, "system dex stubs jar", []string{otherPublicStubsJar, systemStubsJar, stubsJar}, info.TransitiveStubDexJarsByScope.StubDexJarsForScope(SystemHiddenAPIScope)) |
Paul Duffin | 34827d4 | 2021-05-13 21:25:05 +0100 | [diff] [blame] | 255 | |
| 256 | // Check that SdkTest also uses system stubs for mysdklibrary as it does not provide test stubs |
| 257 | // and public stubs for myothersdklibrary as it does not provide test stubs either. |
Paul Duffin | 280a31a | 2021-06-27 20:28:29 +0100 | [diff] [blame] | 258 | android.AssertPathsRelativeToTopEquals(t, "test dex stubs jar", []string{otherPublicStubsJar, systemStubsJar, stubsJar}, info.TransitiveStubDexJarsByScope.StubDexJarsForScope(TestHiddenAPIScope)) |
Paul Duffin | 1093158 | 2021-04-25 10:13:54 +0100 | [diff] [blame] | 259 | |
| 260 | // Check that SdkCorePlatform uses public stubs from the mycoreplatform library. |
| 261 | corePlatformStubsJar := "out/soong/.intermediates/mycoreplatform.stubs/android_common/dex/mycoreplatform.stubs.jar" |
Paul Duffin | 280a31a | 2021-06-27 20:28:29 +0100 | [diff] [blame] | 262 | android.AssertPathsRelativeToTopEquals(t, "core platform dex stubs jar", []string{corePlatformStubsJar}, info.TransitiveStubDexJarsByScope.StubDexJarsForScope(CorePlatformHiddenAPIScope)) |
Paul Duffin | d2b1e0c | 2021-06-27 20:53:39 +0100 | [diff] [blame] | 263 | |
Paul Duffin | 280a31a | 2021-06-27 20:28:29 +0100 | [diff] [blame] | 264 | // Check the widest stubs.. The list contains the widest stub dex jar provided by each module. |
| 265 | expectedWidestPaths := []string{ |
| 266 | // mycoreplatform's widest API is core platform. |
| 267 | corePlatformStubsJar, |
| 268 | |
| 269 | // myothersdklibrary's widest API is public. |
| 270 | otherPublicStubsJar, |
| 271 | |
| 272 | // sdklibrary's widest API is system. |
| 273 | systemStubsJar, |
| 274 | |
| 275 | // mystublib's only provides one API and so it must be the widest. |
| 276 | stubsJar, |
| 277 | } |
| 278 | |
| 279 | android.AssertPathsRelativeToTopEquals(t, "widest dex stubs jar", expectedWidestPaths, info.TransitiveStubDexJarsByScope.StubDexJarsForWidestAPIScope()) |
Paul Duffin | 1093158 | 2021-04-25 10:13:54 +0100 | [diff] [blame] | 280 | } |