| 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 | 588e22a | 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 | 588e22a | 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 | 588e22a | 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 | 588e22a | 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 | 588e22a | 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 | 588e22a | 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 | 588e22a | 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 | 588e22a | 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"), | 
|  | 167 | prepareWithBp, | 
|  | 168 | ) | 
|  | 169 |  | 
| Paul Duffin | c7d1644 | 2021-04-23 13:55:49 +0100 | [diff] [blame] | 170 | t.Run("without coverage", func(t *testing.T) { | 
| Paul Duffin | 895c714 | 2021-04-25 13:40:15 +0100 | [diff] [blame] | 171 | result := preparer.RunTest(t) | 
| Paul Duffin | c7d1644 | 2021-04-23 13:55:49 +0100 | [diff] [blame] | 172 | checkContents(t, result, "mybootlib") | 
|  | 173 | }) | 
|  | 174 |  | 
|  | 175 | t.Run("with coverage", func(t *testing.T) { | 
|  | 176 | result := android.GroupFixturePreparers( | 
| Paul Duffin | c7d1644 | 2021-04-23 13:55:49 +0100 | [diff] [blame] | 177 | prepareForTestWithFrameworkCoverage, | 
| Paul Duffin | 895c714 | 2021-04-25 13:40:15 +0100 | [diff] [blame] | 178 | preparer, | 
| Paul Duffin | c7d1644 | 2021-04-23 13:55:49 +0100 | [diff] [blame] | 179 | ).RunTest(t) | 
|  | 180 | checkContents(t, result, "mybootlib", "coveragelib") | 
|  | 181 | }) | 
|  | 182 | } | 
| Paul Duffin | 1093158 | 2021-04-25 10:13:54 +0100 | [diff] [blame] | 183 |  | 
|  | 184 | func TestBootclasspathFragment_StubLibs(t *testing.T) { | 
|  | 185 | result := android.GroupFixturePreparers( | 
|  | 186 | prepareForTestWithBootclasspathFragment, | 
|  | 187 | PrepareForTestWithJavaSdkLibraryFiles, | 
| Paul Duffin | 34827d4 | 2021-05-13 21:25:05 +0100 | [diff] [blame] | 188 | FixtureWithLastReleaseApis("mysdklibrary", "myothersdklibrary", "mycoreplatform"), | 
| Paul Duffin | 1093158 | 2021-04-25 10:13:54 +0100 | [diff] [blame] | 189 | ).RunTestWithBp(t, ` | 
|  | 190 | bootclasspath_fragment { | 
|  | 191 | name: "myfragment", | 
|  | 192 | contents: ["mysdklibrary"], | 
|  | 193 | api: { | 
|  | 194 | stub_libs: [ | 
|  | 195 | "mystublib", | 
| Paul Duffin | 34827d4 | 2021-05-13 21:25:05 +0100 | [diff] [blame] | 196 | "myothersdklibrary", | 
| Paul Duffin | 1093158 | 2021-04-25 10:13:54 +0100 | [diff] [blame] | 197 | ], | 
|  | 198 | }, | 
|  | 199 | core_platform_api: { | 
| Paul Duffin | 5842387 | 2021-06-30 18:25:36 +0100 | [diff] [blame] | 200 | stub_libs: ["mycoreplatform.stubs"], | 
| Paul Duffin | 1093158 | 2021-04-25 10:13:54 +0100 | [diff] [blame] | 201 | }, | 
|  | 202 | } | 
|  | 203 |  | 
|  | 204 | java_library { | 
|  | 205 | name: "mystublib", | 
|  | 206 | srcs: ["Test.java"], | 
|  | 207 | system_modules: "none", | 
|  | 208 | sdk_version: "none", | 
|  | 209 | compile_dex: true, | 
|  | 210 | } | 
|  | 211 |  | 
|  | 212 | java_sdk_library { | 
|  | 213 | name: "mysdklibrary", | 
|  | 214 | srcs: ["a.java"], | 
| Paul Duffin | f4600f6 | 2021-05-13 22:34:45 +0100 | [diff] [blame] | 215 | shared_library: false, | 
| Paul Duffin | 1093158 | 2021-04-25 10:13:54 +0100 | [diff] [blame] | 216 | public: {enabled: true}, | 
|  | 217 | system: {enabled: true}, | 
|  | 218 | } | 
|  | 219 |  | 
|  | 220 | java_sdk_library { | 
| Paul Duffin | 34827d4 | 2021-05-13 21:25:05 +0100 | [diff] [blame] | 221 | name: "myothersdklibrary", | 
|  | 222 | srcs: ["a.java"], | 
|  | 223 | shared_library: false, | 
|  | 224 | public: {enabled: true}, | 
|  | 225 | } | 
|  | 226 |  | 
|  | 227 | java_sdk_library { | 
| Paul Duffin | 1093158 | 2021-04-25 10:13:54 +0100 | [diff] [blame] | 228 | name: "mycoreplatform", | 
|  | 229 | srcs: ["a.java"], | 
| Paul Duffin | f4600f6 | 2021-05-13 22:34:45 +0100 | [diff] [blame] | 230 | shared_library: false, | 
| Paul Duffin | 1093158 | 2021-04-25 10:13:54 +0100 | [diff] [blame] | 231 | public: {enabled: true}, | 
|  | 232 | } | 
|  | 233 | `) | 
|  | 234 |  | 
|  | 235 | fragment := result.Module("myfragment", "android_common") | 
| Paul Duffin | 6a58cc9 | 2021-05-21 22:46:59 +0100 | [diff] [blame] | 236 | info := result.ModuleProvider(fragment, HiddenAPIInfoProvider).(HiddenAPIInfo) | 
| Paul Duffin | 1093158 | 2021-04-25 10:13:54 +0100 | [diff] [blame] | 237 |  | 
|  | 238 | stubsJar := "out/soong/.intermediates/mystublib/android_common/dex/mystublib.jar" | 
|  | 239 |  | 
| Paul Duffin | 34827d4 | 2021-05-13 21:25:05 +0100 | [diff] [blame] | 240 | // Stubs jars for mysdklibrary | 
| Paul Duffin | 1093158 | 2021-04-25 10:13:54 +0100 | [diff] [blame] | 241 | publicStubsJar := "out/soong/.intermediates/mysdklibrary.stubs/android_common/dex/mysdklibrary.stubs.jar" | 
| Paul Duffin | 1093158 | 2021-04-25 10:13:54 +0100 | [diff] [blame] | 242 | 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] | 243 |  | 
| Paul Duffin | 34827d4 | 2021-05-13 21:25:05 +0100 | [diff] [blame] | 244 | // Stubs jars for myothersdklibrary | 
|  | 245 | otherPublicStubsJar := "out/soong/.intermediates/myothersdklibrary.stubs/android_common/dex/myothersdklibrary.stubs.jar" | 
|  | 246 |  | 
|  | 247 | // Check that SdkPublic uses public stubs for all sdk libraries. | 
| Paul Duffin | fb8f07b | 2021-06-27 20:28:29 +0100 | [diff] [blame] | 248 | 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] | 249 |  | 
|  | 250 | // Check that SdkSystem uses system stubs for mysdklibrary and public stubs for myothersdklibrary | 
|  | 251 | // as it does not provide system stubs. | 
| Paul Duffin | fb8f07b | 2021-06-27 20:28:29 +0100 | [diff] [blame] | 252 | 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] | 253 |  | 
|  | 254 | // Check that SdkTest also uses system stubs for mysdklibrary as it does not provide test stubs | 
|  | 255 | // and public stubs for myothersdklibrary as it does not provide test stubs either. | 
| Paul Duffin | fb8f07b | 2021-06-27 20:28:29 +0100 | [diff] [blame] | 256 | 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] | 257 |  | 
|  | 258 | // Check that SdkCorePlatform uses public stubs from the mycoreplatform library. | 
|  | 259 | corePlatformStubsJar := "out/soong/.intermediates/mycoreplatform.stubs/android_common/dex/mycoreplatform.stubs.jar" | 
| Paul Duffin | fb8f07b | 2021-06-27 20:28:29 +0100 | [diff] [blame] | 260 | android.AssertPathsRelativeToTopEquals(t, "core platform dex stubs jar", []string{corePlatformStubsJar}, info.TransitiveStubDexJarsByScope.StubDexJarsForScope(CorePlatformHiddenAPIScope)) | 
| Paul Duffin | d05bb8e | 2021-06-27 20:53:39 +0100 | [diff] [blame] | 261 |  | 
| Paul Duffin | fb8f07b | 2021-06-27 20:28:29 +0100 | [diff] [blame] | 262 | // Check the widest stubs.. The list contains the widest stub dex jar provided by each module. | 
|  | 263 | expectedWidestPaths := []string{ | 
|  | 264 | // mycoreplatform's widest API is core platform. | 
|  | 265 | corePlatformStubsJar, | 
|  | 266 |  | 
|  | 267 | // myothersdklibrary's widest API is public. | 
|  | 268 | otherPublicStubsJar, | 
|  | 269 |  | 
|  | 270 | // sdklibrary's widest API is system. | 
|  | 271 | systemStubsJar, | 
|  | 272 |  | 
|  | 273 | // mystublib's only provides one API and so it must be the widest. | 
|  | 274 | stubsJar, | 
|  | 275 | } | 
|  | 276 |  | 
|  | 277 | android.AssertPathsRelativeToTopEquals(t, "widest dex stubs jar", expectedWidestPaths, info.TransitiveStubDexJarsByScope.StubDexJarsForWidestAPIScope()) | 
| Paul Duffin | 1093158 | 2021-04-25 10:13:54 +0100 | [diff] [blame] | 278 | } |