Paul Duffin | bb7f1ac | 2021-03-29 22:18:45 +0100 | [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" |
| 19 | |
| 20 | "android/soong/android" |
| 21 | "android/soong/dexpreopt" |
| 22 | ) |
| 23 | |
| 24 | // Contains some simple tests for platform_bootclasspath. |
| 25 | |
| 26 | var prepareForTestWithPlatformBootclasspath = android.GroupFixturePreparers( |
| 27 | PrepareForTestWithJavaDefaultModules, |
| 28 | dexpreopt.PrepareForTestByEnablingDexpreopt, |
| 29 | ) |
| 30 | |
| 31 | func TestPlatformBootclasspath(t *testing.T) { |
Paul Duffin | b432df9 | 2021-03-22 22:09:42 +0000 | [diff] [blame] | 32 | preparer := android.GroupFixturePreparers( |
| 33 | prepareForTestWithPlatformBootclasspath, |
Paul Duffin | 110b0ad | 2021-04-27 14:36:08 +0100 | [diff] [blame] | 34 | FixtureConfigureBootJars("platform:foo", "system_ext:bar"), |
Paul Duffin | b432df9 | 2021-03-22 22:09:42 +0000 | [diff] [blame] | 35 | android.FixtureWithRootAndroidBp(` |
Paul Duffin | bb7f1ac | 2021-03-29 22:18:45 +0100 | [diff] [blame] | 36 | platform_bootclasspath { |
| 37 | name: "platform-bootclasspath", |
| 38 | } |
Paul Duffin | b432df9 | 2021-03-22 22:09:42 +0000 | [diff] [blame] | 39 | |
| 40 | java_library { |
| 41 | name: "bar", |
| 42 | srcs: ["a.java"], |
| 43 | system_modules: "none", |
| 44 | sdk_version: "none", |
| 45 | compile_dex: true, |
Paul Duffin | 110b0ad | 2021-04-27 14:36:08 +0100 | [diff] [blame] | 46 | system_ext_specific: true, |
Paul Duffin | b432df9 | 2021-03-22 22:09:42 +0000 | [diff] [blame] | 47 | } |
| 48 | `), |
| 49 | ) |
| 50 | |
| 51 | var addSourceBootclassPathModule = android.FixtureAddTextFile("source/Android.bp", ` |
| 52 | java_library { |
| 53 | name: "foo", |
Paul Duffin | b2c2173 | 2022-05-11 14:29:53 +0000 | [diff] [blame] | 54 | host_supported: true, // verify that b/232106778 is fixed |
Paul Duffin | b432df9 | 2021-03-22 22:09:42 +0000 | [diff] [blame] | 55 | srcs: ["a.java"], |
| 56 | system_modules: "none", |
| 57 | sdk_version: "none", |
| 58 | compile_dex: true, |
| 59 | } |
| 60 | `) |
| 61 | |
| 62 | var addPrebuiltBootclassPathModule = android.FixtureAddTextFile("prebuilt/Android.bp", ` |
| 63 | java_import { |
| 64 | name: "foo", |
| 65 | jars: ["a.jar"], |
| 66 | compile_dex: true, |
| 67 | prefer: false, |
| 68 | } |
| 69 | `) |
| 70 | |
| 71 | var addPrebuiltPreferredBootclassPathModule = android.FixtureAddTextFile("prebuilt/Android.bp", ` |
| 72 | java_import { |
| 73 | name: "foo", |
| 74 | jars: ["a.jar"], |
| 75 | compile_dex: true, |
| 76 | prefer: true, |
| 77 | } |
| 78 | `) |
| 79 | |
| 80 | t.Run("missing", func(t *testing.T) { |
| 81 | preparer. |
| 82 | ExtendWithErrorHandler(android.FixtureExpectsAtLeastOneErrorMatchingPattern(`"platform-bootclasspath" depends on undefined module "foo"`)). |
| 83 | RunTest(t) |
| 84 | }) |
| 85 | |
| 86 | t.Run("source", func(t *testing.T) { |
| 87 | result := android.GroupFixturePreparers( |
| 88 | preparer, |
| 89 | addSourceBootclassPathModule, |
| 90 | ).RunTest(t) |
| 91 | |
| 92 | CheckPlatformBootclasspathModules(t, result, "platform-bootclasspath", []string{ |
| 93 | "platform:foo", |
| 94 | "platform:bar", |
| 95 | }) |
| 96 | }) |
| 97 | |
| 98 | t.Run("prebuilt", func(t *testing.T) { |
| 99 | result := android.GroupFixturePreparers( |
| 100 | preparer, |
| 101 | addPrebuiltBootclassPathModule, |
| 102 | ).RunTest(t) |
| 103 | |
| 104 | CheckPlatformBootclasspathModules(t, result, "platform-bootclasspath", []string{ |
| 105 | "platform:prebuilt_foo", |
| 106 | "platform:bar", |
| 107 | }) |
| 108 | }) |
| 109 | |
| 110 | t.Run("source+prebuilt - source preferred", func(t *testing.T) { |
| 111 | result := android.GroupFixturePreparers( |
| 112 | preparer, |
| 113 | addSourceBootclassPathModule, |
| 114 | addPrebuiltBootclassPathModule, |
| 115 | ).RunTest(t) |
| 116 | |
| 117 | CheckPlatformBootclasspathModules(t, result, "platform-bootclasspath", []string{ |
| 118 | "platform:foo", |
| 119 | "platform:bar", |
| 120 | }) |
| 121 | }) |
| 122 | |
| 123 | t.Run("source+prebuilt - prebuilt preferred", func(t *testing.T) { |
| 124 | result := android.GroupFixturePreparers( |
| 125 | preparer, |
| 126 | addSourceBootclassPathModule, |
| 127 | addPrebuiltPreferredBootclassPathModule, |
| 128 | ).RunTest(t) |
| 129 | |
| 130 | CheckPlatformBootclasspathModules(t, result, "platform-bootclasspath", []string{ |
| 131 | "platform:prebuilt_foo", |
| 132 | "platform:bar", |
| 133 | }) |
| 134 | }) |
Paul Duffin | 4977540 | 2021-04-30 08:58:12 +0100 | [diff] [blame] | 135 | |
| 136 | t.Run("dex import", func(t *testing.T) { |
| 137 | result := android.GroupFixturePreparers( |
| 138 | preparer, |
| 139 | android.FixtureAddTextFile("deximport/Android.bp", ` |
| 140 | dex_import { |
| 141 | name: "foo", |
| 142 | jars: ["a.jar"], |
| 143 | } |
| 144 | `), |
| 145 | ).RunTest(t) |
| 146 | |
| 147 | CheckPlatformBootclasspathModules(t, result, "platform-bootclasspath", []string{ |
| 148 | "platform:prebuilt_foo", |
| 149 | "platform:bar", |
| 150 | }) |
| 151 | }) |
Paul Duffin | bb7f1ac | 2021-03-29 22:18:45 +0100 | [diff] [blame] | 152 | } |
Paul Duffin | 6a76645 | 2021-04-12 14:15:22 +0100 | [diff] [blame] | 153 | |
Artur Satayev | 97259dc | 2021-04-07 15:17:14 +0100 | [diff] [blame] | 154 | func TestPlatformBootclasspathVariant(t *testing.T) { |
| 155 | result := android.GroupFixturePreparers( |
| 156 | prepareForTestWithPlatformBootclasspath, |
| 157 | android.FixtureWithRootAndroidBp(` |
| 158 | platform_bootclasspath { |
| 159 | name: "platform-bootclasspath", |
| 160 | } |
| 161 | `), |
| 162 | ).RunTest(t) |
| 163 | |
| 164 | variants := result.ModuleVariantsForTests("platform-bootclasspath") |
| 165 | android.AssertIntEquals(t, "expect 1 variant", 1, len(variants)) |
| 166 | } |
| 167 | |
| 168 | func TestPlatformBootclasspath_ClasspathFragmentPaths(t *testing.T) { |
| 169 | result := android.GroupFixturePreparers( |
| 170 | prepareForTestWithPlatformBootclasspath, |
| 171 | android.FixtureWithRootAndroidBp(` |
| 172 | platform_bootclasspath { |
| 173 | name: "platform-bootclasspath", |
| 174 | } |
| 175 | `), |
| 176 | ).RunTest(t) |
| 177 | |
| 178 | p := result.Module("platform-bootclasspath", "android_common").(*platformBootclasspathModule) |
satayev | 227e745 | 2021-05-20 21:35:06 +0100 | [diff] [blame] | 179 | android.AssertStringEquals(t, "output filepath", "bootclasspath.pb", p.ClasspathFragmentBase.outputFilepath.Base()) |
Artur Satayev | 97259dc | 2021-04-07 15:17:14 +0100 | [diff] [blame] | 180 | android.AssertPathRelativeToTopEquals(t, "install filepath", "out/soong/target/product/test_device/system/etc/classpaths", p.ClasspathFragmentBase.installDirPath) |
| 181 | } |
| 182 | |
| 183 | func TestPlatformBootclasspathModule_AndroidMkEntries(t *testing.T) { |
| 184 | preparer := android.GroupFixturePreparers( |
| 185 | prepareForTestWithPlatformBootclasspath, |
| 186 | android.FixtureWithRootAndroidBp(` |
| 187 | platform_bootclasspath { |
| 188 | name: "platform-bootclasspath", |
| 189 | } |
| 190 | `), |
| 191 | ) |
| 192 | |
| 193 | t.Run("AndroidMkEntries", func(t *testing.T) { |
| 194 | result := preparer.RunTest(t) |
| 195 | |
| 196 | p := result.Module("platform-bootclasspath", "android_common").(*platformBootclasspathModule) |
| 197 | |
| 198 | entries := android.AndroidMkEntriesForTest(t, result.TestContext, p) |
| 199 | android.AssertIntEquals(t, "AndroidMkEntries count", 2, len(entries)) |
| 200 | }) |
| 201 | |
| 202 | t.Run("hiddenapi-flags-entry", func(t *testing.T) { |
| 203 | result := preparer.RunTest(t) |
| 204 | |
| 205 | p := result.Module("platform-bootclasspath", "android_common").(*platformBootclasspathModule) |
| 206 | |
| 207 | entries := android.AndroidMkEntriesForTest(t, result.TestContext, p) |
| 208 | got := entries[0].OutputFile |
| 209 | android.AssertBoolEquals(t, "valid output path", true, got.Valid()) |
| 210 | android.AssertSame(t, "output filepath", p.hiddenAPIFlagsCSV, got.Path()) |
| 211 | }) |
| 212 | |
| 213 | t.Run("classpath-fragment-entry", func(t *testing.T) { |
| 214 | result := preparer.RunTest(t) |
| 215 | |
| 216 | want := map[string][]string{ |
| 217 | "LOCAL_MODULE": {"platform-bootclasspath"}, |
| 218 | "LOCAL_MODULE_CLASS": {"ETC"}, |
satayev | 227e745 | 2021-05-20 21:35:06 +0100 | [diff] [blame] | 219 | "LOCAL_INSTALLED_MODULE_STEM": {"bootclasspath.pb"}, |
Artur Satayev | 97259dc | 2021-04-07 15:17:14 +0100 | [diff] [blame] | 220 | // Output and Install paths are tested separately in TestPlatformBootclasspath_ClasspathFragmentPaths |
| 221 | } |
| 222 | |
| 223 | p := result.Module("platform-bootclasspath", "android_common").(*platformBootclasspathModule) |
| 224 | |
| 225 | entries := android.AndroidMkEntriesForTest(t, result.TestContext, p) |
| 226 | got := entries[1] |
| 227 | for k, expectedValue := range want { |
| 228 | if value, ok := got.EntryMap[k]; ok { |
| 229 | android.AssertDeepEquals(t, k, expectedValue, value) |
| 230 | } else { |
| 231 | t.Errorf("No %s defined, saw %q", k, got.EntryMap) |
| 232 | } |
| 233 | } |
| 234 | }) |
| 235 | } |
| 236 | |
Paul Duffin | 6a76645 | 2021-04-12 14:15:22 +0100 | [diff] [blame] | 237 | func TestPlatformBootclasspath_Dist(t *testing.T) { |
| 238 | result := android.GroupFixturePreparers( |
| 239 | prepareForTestWithPlatformBootclasspath, |
Paul Duffin | 60264a0 | 2021-04-12 20:02:36 +0100 | [diff] [blame] | 240 | FixtureConfigureBootJars("platform:foo", "platform:bar"), |
Paul Duffin | 6a76645 | 2021-04-12 14:15:22 +0100 | [diff] [blame] | 241 | android.PrepareForTestWithAndroidMk, |
| 242 | android.FixtureWithRootAndroidBp(` |
| 243 | platform_bootclasspath { |
| 244 | name: "platform-bootclasspath", |
| 245 | dists: [ |
| 246 | { |
| 247 | targets: ["droidcore"], |
| 248 | tag: "hiddenapi-flags.csv", |
| 249 | }, |
| 250 | ], |
| 251 | } |
| 252 | |
| 253 | java_library { |
| 254 | name: "bar", |
| 255 | srcs: ["a.java"], |
| 256 | system_modules: "none", |
| 257 | sdk_version: "none", |
| 258 | compile_dex: true, |
| 259 | } |
| 260 | |
| 261 | java_library { |
| 262 | name: "foo", |
| 263 | srcs: ["a.java"], |
| 264 | system_modules: "none", |
| 265 | sdk_version: "none", |
| 266 | compile_dex: true, |
| 267 | } |
| 268 | `), |
| 269 | ).RunTest(t) |
| 270 | |
| 271 | platformBootclasspath := result.Module("platform-bootclasspath", "android_common").(*platformBootclasspathModule) |
| 272 | entries := android.AndroidMkEntriesForTest(t, result.TestContext, platformBootclasspath) |
| 273 | goals := entries[0].GetDistForGoals(platformBootclasspath) |
| 274 | android.AssertStringEquals(t, "platform dist goals phony", ".PHONY: droidcore\n", goals[0]) |
Bob Badour | 5180438 | 2022-04-13 11:27:19 -0700 | [diff] [blame^] | 275 | android.AssertStringDoesContain(t, "platform dist goals meta check", goals[1], "$(if $(strip $(ALL_TARGETS.") |
| 276 | android.AssertStringDoesContain(t, "platform dist goals meta assign", goals[1], "),,$(eval ALL_TARGETS.") |
| 277 | android.AssertStringEquals(t, "platform dist goals call", "$(call dist-for-goals,droidcore,out/soong/hiddenapi/hiddenapi-flags.csv:hiddenapi-flags.csv)\n", android.StringRelativeToTop(result.Config, goals[2])) |
Paul Duffin | 6a76645 | 2021-04-12 14:15:22 +0100 | [diff] [blame] | 278 | } |
Paul Duffin | 00b2bfd | 2021-04-12 17:24:36 +0100 | [diff] [blame] | 279 | |
Paul Duffin | 85dee5d | 2021-04-13 00:14:38 +0100 | [diff] [blame] | 280 | func TestPlatformBootclasspath_HiddenAPIMonolithicFiles(t *testing.T) { |
Paul Duffin | 00b2bfd | 2021-04-12 17:24:36 +0100 | [diff] [blame] | 281 | result := android.GroupFixturePreparers( |
| 282 | hiddenApiFixtureFactory, |
| 283 | PrepareForTestWithJavaSdkLibraryFiles, |
| 284 | FixtureWithLastReleaseApis("bar"), |
| 285 | FixtureConfigureBootJars("platform:foo", "platform:bar"), |
| 286 | ).RunTestWithBp(t, ` |
| 287 | java_library { |
| 288 | name: "foo", |
| 289 | srcs: ["a.java"], |
| 290 | compile_dex: true, |
| 291 | |
| 292 | hiddenapi_additional_annotations: [ |
| 293 | "foo-hiddenapi-annotations", |
| 294 | ], |
| 295 | } |
| 296 | |
| 297 | java_library { |
| 298 | name: "foo-hiddenapi-annotations", |
| 299 | srcs: ["a.java"], |
| 300 | compile_dex: true, |
| 301 | } |
| 302 | |
| 303 | java_import { |
| 304 | name: "foo", |
| 305 | jars: ["a.jar"], |
| 306 | compile_dex: true, |
| 307 | prefer: false, |
| 308 | } |
| 309 | |
| 310 | java_sdk_library { |
| 311 | name: "bar", |
| 312 | srcs: ["a.java"], |
| 313 | compile_dex: true, |
| 314 | } |
| 315 | |
| 316 | platform_bootclasspath { |
| 317 | name: "myplatform-bootclasspath", |
| 318 | } |
| 319 | `) |
| 320 | |
Paul Duffin | 00b2bfd | 2021-04-12 17:24:36 +0100 | [diff] [blame] | 321 | // Make sure that the foo-hiddenapi-annotations.jar is included in the inputs to the rules that |
| 322 | // creates the index.csv file. |
Paul Duffin | 537ea3d | 2021-05-14 10:38:00 +0100 | [diff] [blame] | 323 | platformBootclasspath := result.ModuleForTests("myplatform-bootclasspath", "android_common") |
Paul Duffin | d061d40 | 2021-06-07 21:36:01 +0100 | [diff] [blame] | 324 | |
| 325 | var rule android.TestingBuildParams |
| 326 | |
| 327 | // All the intermediate rules use the same inputs. |
| 328 | expectedIntermediateInputs := ` |
| 329 | out/soong/.intermediates/bar/android_common/javac/bar.jar |
| 330 | out/soong/.intermediates/foo-hiddenapi-annotations/android_common/javac/foo-hiddenapi-annotations.jar |
| 331 | out/soong/.intermediates/foo/android_common/javac/foo.jar |
| 332 | ` |
| 333 | |
| 334 | // Check flags output. |
| 335 | rule = platformBootclasspath.Output("hiddenapi-monolithic/annotation-flags-from-classes.csv") |
| 336 | CheckHiddenAPIRuleInputs(t, "intermediate flags", expectedIntermediateInputs, rule) |
| 337 | |
| 338 | rule = platformBootclasspath.Output("out/soong/hiddenapi/hiddenapi-flags.csv") |
| 339 | CheckHiddenAPIRuleInputs(t, "monolithic flags", ` |
| 340 | out/soong/.intermediates/myplatform-bootclasspath/android_common/hiddenapi-monolithic/annotation-flags-from-classes.csv |
| 341 | out/soong/hiddenapi/hiddenapi-stub-flags.txt |
| 342 | `, rule) |
| 343 | |
| 344 | // Check metadata output. |
| 345 | rule = platformBootclasspath.Output("hiddenapi-monolithic/metadata-from-classes.csv") |
| 346 | CheckHiddenAPIRuleInputs(t, "intermediate metadata", expectedIntermediateInputs, rule) |
| 347 | |
| 348 | rule = platformBootclasspath.Output("out/soong/hiddenapi/hiddenapi-unsupported.csv") |
| 349 | CheckHiddenAPIRuleInputs(t, "monolithic metadata", ` |
| 350 | out/soong/.intermediates/myplatform-bootclasspath/android_common/hiddenapi-monolithic/metadata-from-classes.csv |
| 351 | `, rule) |
| 352 | |
| 353 | // Check index output. |
| 354 | rule = platformBootclasspath.Output("hiddenapi-monolithic/index-from-classes.csv") |
| 355 | CheckHiddenAPIRuleInputs(t, "intermediate index", expectedIntermediateInputs, rule) |
| 356 | |
| 357 | rule = platformBootclasspath.Output("out/soong/hiddenapi/hiddenapi-index.csv") |
| 358 | CheckHiddenAPIRuleInputs(t, "monolithic index", ` |
| 359 | out/soong/.intermediates/myplatform-bootclasspath/android_common/hiddenapi-monolithic/index-from-classes.csv |
| 360 | `, rule) |
Paul Duffin | 00b2bfd | 2021-04-12 17:24:36 +0100 | [diff] [blame] | 361 | } |