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 ( |
Paul Duffin | 9b381ef | 2021-04-08 23:01:37 +0100 | [diff] [blame] | 18 | "fmt" |
| 19 | "strings" |
Paul Duffin | bb7f1ac | 2021-03-29 22:18:45 +0100 | [diff] [blame] | 20 | "testing" |
| 21 | |
| 22 | "android/soong/android" |
| 23 | "android/soong/dexpreopt" |
| 24 | ) |
| 25 | |
| 26 | // Contains some simple tests for platform_bootclasspath. |
| 27 | |
| 28 | var prepareForTestWithPlatformBootclasspath = android.GroupFixturePreparers( |
| 29 | PrepareForTestWithJavaDefaultModules, |
| 30 | dexpreopt.PrepareForTestByEnablingDexpreopt, |
| 31 | ) |
| 32 | |
| 33 | func TestPlatformBootclasspath(t *testing.T) { |
Paul Duffin | b432df9 | 2021-03-22 22:09:42 +0000 | [diff] [blame] | 34 | preparer := android.GroupFixturePreparers( |
| 35 | prepareForTestWithPlatformBootclasspath, |
Paul Duffin | 110b0ad | 2021-04-27 14:36:08 +0100 | [diff] [blame] | 36 | FixtureConfigureBootJars("platform:foo", "system_ext:bar"), |
Paul Duffin | b432df9 | 2021-03-22 22:09:42 +0000 | [diff] [blame] | 37 | android.FixtureWithRootAndroidBp(` |
Paul Duffin | bb7f1ac | 2021-03-29 22:18:45 +0100 | [diff] [blame] | 38 | platform_bootclasspath { |
| 39 | name: "platform-bootclasspath", |
| 40 | } |
Paul Duffin | b432df9 | 2021-03-22 22:09:42 +0000 | [diff] [blame] | 41 | |
| 42 | java_library { |
| 43 | name: "bar", |
| 44 | srcs: ["a.java"], |
| 45 | system_modules: "none", |
| 46 | sdk_version: "none", |
| 47 | compile_dex: true, |
Paul Duffin | 110b0ad | 2021-04-27 14:36:08 +0100 | [diff] [blame] | 48 | system_ext_specific: true, |
Paul Duffin | b432df9 | 2021-03-22 22:09:42 +0000 | [diff] [blame] | 49 | } |
| 50 | `), |
| 51 | ) |
| 52 | |
| 53 | var addSourceBootclassPathModule = android.FixtureAddTextFile("source/Android.bp", ` |
| 54 | java_library { |
| 55 | name: "foo", |
| 56 | srcs: ["a.java"], |
| 57 | system_modules: "none", |
| 58 | sdk_version: "none", |
| 59 | compile_dex: true, |
| 60 | } |
| 61 | `) |
| 62 | |
| 63 | var addPrebuiltBootclassPathModule = android.FixtureAddTextFile("prebuilt/Android.bp", ` |
| 64 | java_import { |
| 65 | name: "foo", |
| 66 | jars: ["a.jar"], |
| 67 | compile_dex: true, |
| 68 | prefer: false, |
| 69 | } |
| 70 | `) |
| 71 | |
| 72 | var addPrebuiltPreferredBootclassPathModule = android.FixtureAddTextFile("prebuilt/Android.bp", ` |
| 73 | java_import { |
| 74 | name: "foo", |
| 75 | jars: ["a.jar"], |
| 76 | compile_dex: true, |
| 77 | prefer: true, |
| 78 | } |
| 79 | `) |
| 80 | |
| 81 | t.Run("missing", func(t *testing.T) { |
| 82 | preparer. |
| 83 | ExtendWithErrorHandler(android.FixtureExpectsAtLeastOneErrorMatchingPattern(`"platform-bootclasspath" depends on undefined module "foo"`)). |
| 84 | RunTest(t) |
| 85 | }) |
| 86 | |
| 87 | t.Run("source", func(t *testing.T) { |
| 88 | result := android.GroupFixturePreparers( |
| 89 | preparer, |
| 90 | addSourceBootclassPathModule, |
| 91 | ).RunTest(t) |
| 92 | |
| 93 | CheckPlatformBootclasspathModules(t, result, "platform-bootclasspath", []string{ |
| 94 | "platform:foo", |
| 95 | "platform:bar", |
| 96 | }) |
| 97 | }) |
| 98 | |
| 99 | t.Run("prebuilt", func(t *testing.T) { |
| 100 | result := android.GroupFixturePreparers( |
| 101 | preparer, |
| 102 | addPrebuiltBootclassPathModule, |
| 103 | ).RunTest(t) |
| 104 | |
| 105 | CheckPlatformBootclasspathModules(t, result, "platform-bootclasspath", []string{ |
| 106 | "platform:prebuilt_foo", |
| 107 | "platform:bar", |
| 108 | }) |
| 109 | }) |
| 110 | |
| 111 | t.Run("source+prebuilt - source preferred", func(t *testing.T) { |
| 112 | result := android.GroupFixturePreparers( |
| 113 | preparer, |
| 114 | addSourceBootclassPathModule, |
| 115 | addPrebuiltBootclassPathModule, |
| 116 | ).RunTest(t) |
| 117 | |
| 118 | CheckPlatformBootclasspathModules(t, result, "platform-bootclasspath", []string{ |
| 119 | "platform:foo", |
| 120 | "platform:bar", |
| 121 | }) |
| 122 | }) |
| 123 | |
| 124 | t.Run("source+prebuilt - prebuilt preferred", func(t *testing.T) { |
| 125 | result := android.GroupFixturePreparers( |
| 126 | preparer, |
| 127 | addSourceBootclassPathModule, |
| 128 | addPrebuiltPreferredBootclassPathModule, |
| 129 | ).RunTest(t) |
| 130 | |
| 131 | CheckPlatformBootclasspathModules(t, result, "platform-bootclasspath", []string{ |
| 132 | "platform:prebuilt_foo", |
| 133 | "platform:bar", |
| 134 | }) |
| 135 | }) |
Paul Duffin | 4977540 | 2021-04-30 08:58:12 +0100 | [diff] [blame] | 136 | |
| 137 | t.Run("dex import", func(t *testing.T) { |
| 138 | result := android.GroupFixturePreparers( |
| 139 | preparer, |
| 140 | android.FixtureAddTextFile("deximport/Android.bp", ` |
| 141 | dex_import { |
| 142 | name: "foo", |
| 143 | jars: ["a.jar"], |
| 144 | } |
| 145 | `), |
| 146 | ).RunTest(t) |
| 147 | |
| 148 | CheckPlatformBootclasspathModules(t, result, "platform-bootclasspath", []string{ |
| 149 | "platform:prebuilt_foo", |
| 150 | "platform:bar", |
| 151 | }) |
| 152 | }) |
Paul Duffin | bb7f1ac | 2021-03-29 22:18:45 +0100 | [diff] [blame] | 153 | } |
Paul Duffin | 6a76645 | 2021-04-12 14:15:22 +0100 | [diff] [blame] | 154 | |
Paul Duffin | 9b381ef | 2021-04-08 23:01:37 +0100 | [diff] [blame] | 155 | func TestPlatformBootclasspath_Fragments(t *testing.T) { |
| 156 | result := android.GroupFixturePreparers( |
| 157 | prepareForTestWithPlatformBootclasspath, |
Paul Duffin | 2fef136 | 2021-04-15 13:32:00 +0100 | [diff] [blame] | 158 | PrepareForTestWithJavaSdkLibraryFiles, |
| 159 | FixtureWithLastReleaseApis("foo"), |
Paul Duffin | 9b381ef | 2021-04-08 23:01:37 +0100 | [diff] [blame] | 160 | android.FixtureWithRootAndroidBp(` |
| 161 | platform_bootclasspath { |
| 162 | name: "platform-bootclasspath", |
| 163 | fragments: [ |
| 164 | {module:"bar-fragment"}, |
| 165 | ], |
| 166 | hidden_api: { |
| 167 | unsupported: [ |
| 168 | "unsupported.txt", |
| 169 | ], |
| 170 | removed: [ |
| 171 | "removed.txt", |
| 172 | ], |
| 173 | max_target_r_low_priority: [ |
| 174 | "max-target-r-low-priority.txt", |
| 175 | ], |
| 176 | max_target_q: [ |
| 177 | "max-target-q.txt", |
| 178 | ], |
| 179 | max_target_p: [ |
| 180 | "max-target-p.txt", |
| 181 | ], |
| 182 | max_target_o_low_priority: [ |
| 183 | "max-target-o-low-priority.txt", |
| 184 | ], |
| 185 | blocked: [ |
| 186 | "blocked.txt", |
| 187 | ], |
| 188 | unsupported_packages: [ |
| 189 | "unsupported-packages.txt", |
| 190 | ], |
| 191 | }, |
| 192 | } |
| 193 | |
| 194 | bootclasspath_fragment { |
| 195 | name: "bar-fragment", |
| 196 | contents: ["bar"], |
Paul Duffin | 2fef136 | 2021-04-15 13:32:00 +0100 | [diff] [blame] | 197 | api: { |
| 198 | stub_libs: ["foo"], |
| 199 | }, |
Paul Duffin | 9b381ef | 2021-04-08 23:01:37 +0100 | [diff] [blame] | 200 | hidden_api: { |
| 201 | unsupported: [ |
| 202 | "bar-unsupported.txt", |
| 203 | ], |
| 204 | removed: [ |
| 205 | "bar-removed.txt", |
| 206 | ], |
| 207 | max_target_r_low_priority: [ |
| 208 | "bar-max-target-r-low-priority.txt", |
| 209 | ], |
| 210 | max_target_q: [ |
| 211 | "bar-max-target-q.txt", |
| 212 | ], |
| 213 | max_target_p: [ |
| 214 | "bar-max-target-p.txt", |
| 215 | ], |
| 216 | max_target_o_low_priority: [ |
| 217 | "bar-max-target-o-low-priority.txt", |
| 218 | ], |
| 219 | blocked: [ |
| 220 | "bar-blocked.txt", |
| 221 | ], |
| 222 | unsupported_packages: [ |
| 223 | "bar-unsupported-packages.txt", |
| 224 | ], |
| 225 | }, |
| 226 | } |
| 227 | |
| 228 | java_library { |
| 229 | name: "bar", |
| 230 | srcs: ["a.java"], |
| 231 | system_modules: "none", |
| 232 | sdk_version: "none", |
| 233 | compile_dex: true, |
| 234 | } |
Paul Duffin | 2fef136 | 2021-04-15 13:32:00 +0100 | [diff] [blame] | 235 | |
| 236 | java_sdk_library { |
| 237 | name: "foo", |
| 238 | srcs: ["a.java"], |
| 239 | public: { |
| 240 | enabled: true, |
| 241 | }, |
| 242 | compile_dex: true, |
| 243 | } |
Paul Duffin | 9b381ef | 2021-04-08 23:01:37 +0100 | [diff] [blame] | 244 | `), |
| 245 | ).RunTest(t) |
| 246 | |
| 247 | pbcp := result.Module("platform-bootclasspath", "android_common") |
| 248 | info := result.ModuleProvider(pbcp, hiddenAPIFlagFileInfoProvider).(hiddenAPIFlagFileInfo) |
| 249 | |
| 250 | for _, category := range hiddenAPIFlagFileCategories { |
| 251 | name := category.propertyName |
| 252 | message := fmt.Sprintf("category %s", name) |
| 253 | filename := strings.ReplaceAll(name, "_", "-") |
| 254 | expected := []string{fmt.Sprintf("%s.txt", filename), fmt.Sprintf("bar-%s.txt", filename)} |
| 255 | android.AssertPathsRelativeToTopEquals(t, message, expected, info.categoryToPaths[category]) |
| 256 | } |
Paul Duffin | 2fef136 | 2021-04-15 13:32:00 +0100 | [diff] [blame] | 257 | |
| 258 | android.AssertPathsRelativeToTopEquals(t, "stub flags", []string{"out/soong/.intermediates/bar-fragment/android_common/modular-hiddenapi/stub-flags.csv"}, info.StubFlagsPaths) |
| 259 | android.AssertPathsRelativeToTopEquals(t, "annotation flags", []string{"out/soong/.intermediates/bar-fragment/android_common/modular-hiddenapi/annotation-flags.csv"}, info.AnnotationFlagsPaths) |
| 260 | android.AssertPathsRelativeToTopEquals(t, "metadata flags", []string{"out/soong/.intermediates/bar-fragment/android_common/modular-hiddenapi/metadata.csv"}, info.MetadataPaths) |
| 261 | android.AssertPathsRelativeToTopEquals(t, "index flags", []string{"out/soong/.intermediates/bar-fragment/android_common/modular-hiddenapi/index.csv"}, info.IndexPaths) |
| 262 | android.AssertPathsRelativeToTopEquals(t, "all flags", []string{"out/soong/.intermediates/bar-fragment/android_common/modular-hiddenapi/all-flags.csv"}, info.AllFlagsPaths) |
Paul Duffin | 9b381ef | 2021-04-08 23:01:37 +0100 | [diff] [blame] | 263 | } |
| 264 | |
Artur Satayev | 97259dc | 2021-04-07 15:17:14 +0100 | [diff] [blame] | 265 | func TestPlatformBootclasspathVariant(t *testing.T) { |
| 266 | result := android.GroupFixturePreparers( |
| 267 | prepareForTestWithPlatformBootclasspath, |
| 268 | android.FixtureWithRootAndroidBp(` |
| 269 | platform_bootclasspath { |
| 270 | name: "platform-bootclasspath", |
| 271 | } |
| 272 | `), |
| 273 | ).RunTest(t) |
| 274 | |
| 275 | variants := result.ModuleVariantsForTests("platform-bootclasspath") |
| 276 | android.AssertIntEquals(t, "expect 1 variant", 1, len(variants)) |
| 277 | } |
| 278 | |
| 279 | func TestPlatformBootclasspath_ClasspathFragmentPaths(t *testing.T) { |
| 280 | result := android.GroupFixturePreparers( |
| 281 | prepareForTestWithPlatformBootclasspath, |
| 282 | android.FixtureWithRootAndroidBp(` |
| 283 | platform_bootclasspath { |
| 284 | name: "platform-bootclasspath", |
| 285 | } |
| 286 | `), |
| 287 | ).RunTest(t) |
| 288 | |
| 289 | p := result.Module("platform-bootclasspath", "android_common").(*platformBootclasspathModule) |
| 290 | android.AssertStringEquals(t, "output filepath", p.Name()+".pb", p.ClasspathFragmentBase.outputFilepath.Base()) |
| 291 | android.AssertPathRelativeToTopEquals(t, "install filepath", "out/soong/target/product/test_device/system/etc/classpaths", p.ClasspathFragmentBase.installDirPath) |
| 292 | } |
| 293 | |
| 294 | func TestPlatformBootclasspathModule_AndroidMkEntries(t *testing.T) { |
| 295 | preparer := android.GroupFixturePreparers( |
| 296 | prepareForTestWithPlatformBootclasspath, |
| 297 | android.FixtureWithRootAndroidBp(` |
| 298 | platform_bootclasspath { |
| 299 | name: "platform-bootclasspath", |
| 300 | } |
| 301 | `), |
| 302 | ) |
| 303 | |
| 304 | t.Run("AndroidMkEntries", func(t *testing.T) { |
| 305 | result := preparer.RunTest(t) |
| 306 | |
| 307 | p := result.Module("platform-bootclasspath", "android_common").(*platformBootclasspathModule) |
| 308 | |
| 309 | entries := android.AndroidMkEntriesForTest(t, result.TestContext, p) |
| 310 | android.AssertIntEquals(t, "AndroidMkEntries count", 2, len(entries)) |
| 311 | }) |
| 312 | |
| 313 | t.Run("hiddenapi-flags-entry", func(t *testing.T) { |
| 314 | result := preparer.RunTest(t) |
| 315 | |
| 316 | p := result.Module("platform-bootclasspath", "android_common").(*platformBootclasspathModule) |
| 317 | |
| 318 | entries := android.AndroidMkEntriesForTest(t, result.TestContext, p) |
| 319 | got := entries[0].OutputFile |
| 320 | android.AssertBoolEquals(t, "valid output path", true, got.Valid()) |
| 321 | android.AssertSame(t, "output filepath", p.hiddenAPIFlagsCSV, got.Path()) |
| 322 | }) |
| 323 | |
| 324 | t.Run("classpath-fragment-entry", func(t *testing.T) { |
| 325 | result := preparer.RunTest(t) |
| 326 | |
| 327 | want := map[string][]string{ |
| 328 | "LOCAL_MODULE": {"platform-bootclasspath"}, |
| 329 | "LOCAL_MODULE_CLASS": {"ETC"}, |
| 330 | "LOCAL_INSTALLED_MODULE_STEM": {"platform-bootclasspath.pb"}, |
| 331 | // Output and Install paths are tested separately in TestPlatformBootclasspath_ClasspathFragmentPaths |
| 332 | } |
| 333 | |
| 334 | p := result.Module("platform-bootclasspath", "android_common").(*platformBootclasspathModule) |
| 335 | |
| 336 | entries := android.AndroidMkEntriesForTest(t, result.TestContext, p) |
| 337 | got := entries[1] |
| 338 | for k, expectedValue := range want { |
| 339 | if value, ok := got.EntryMap[k]; ok { |
| 340 | android.AssertDeepEquals(t, k, expectedValue, value) |
| 341 | } else { |
| 342 | t.Errorf("No %s defined, saw %q", k, got.EntryMap) |
| 343 | } |
| 344 | } |
| 345 | }) |
| 346 | } |
| 347 | |
Paul Duffin | 6a76645 | 2021-04-12 14:15:22 +0100 | [diff] [blame] | 348 | func TestPlatformBootclasspath_Dist(t *testing.T) { |
| 349 | result := android.GroupFixturePreparers( |
| 350 | prepareForTestWithPlatformBootclasspath, |
Paul Duffin | 60264a0 | 2021-04-12 20:02:36 +0100 | [diff] [blame] | 351 | FixtureConfigureBootJars("platform:foo", "platform:bar"), |
Paul Duffin | 6a76645 | 2021-04-12 14:15:22 +0100 | [diff] [blame] | 352 | android.PrepareForTestWithAndroidMk, |
| 353 | android.FixtureWithRootAndroidBp(` |
| 354 | platform_bootclasspath { |
| 355 | name: "platform-bootclasspath", |
| 356 | dists: [ |
| 357 | { |
| 358 | targets: ["droidcore"], |
| 359 | tag: "hiddenapi-flags.csv", |
| 360 | }, |
| 361 | ], |
| 362 | } |
| 363 | |
| 364 | java_library { |
| 365 | name: "bar", |
| 366 | srcs: ["a.java"], |
| 367 | system_modules: "none", |
| 368 | sdk_version: "none", |
| 369 | compile_dex: true, |
| 370 | } |
| 371 | |
| 372 | java_library { |
| 373 | name: "foo", |
| 374 | srcs: ["a.java"], |
| 375 | system_modules: "none", |
| 376 | sdk_version: "none", |
| 377 | compile_dex: true, |
| 378 | } |
| 379 | `), |
| 380 | ).RunTest(t) |
| 381 | |
| 382 | platformBootclasspath := result.Module("platform-bootclasspath", "android_common").(*platformBootclasspathModule) |
| 383 | entries := android.AndroidMkEntriesForTest(t, result.TestContext, platformBootclasspath) |
| 384 | goals := entries[0].GetDistForGoals(platformBootclasspath) |
| 385 | android.AssertStringEquals(t, "platform dist goals phony", ".PHONY: droidcore\n", goals[0]) |
| 386 | 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[1])) |
| 387 | } |
Paul Duffin | 00b2bfd | 2021-04-12 17:24:36 +0100 | [diff] [blame] | 388 | |
Paul Duffin | 85dee5d | 2021-04-13 00:14:38 +0100 | [diff] [blame] | 389 | func TestPlatformBootclasspath_HiddenAPIMonolithicFiles(t *testing.T) { |
Paul Duffin | 00b2bfd | 2021-04-12 17:24:36 +0100 | [diff] [blame] | 390 | result := android.GroupFixturePreparers( |
| 391 | hiddenApiFixtureFactory, |
| 392 | PrepareForTestWithJavaSdkLibraryFiles, |
| 393 | FixtureWithLastReleaseApis("bar"), |
| 394 | FixtureConfigureBootJars("platform:foo", "platform:bar"), |
| 395 | ).RunTestWithBp(t, ` |
| 396 | java_library { |
| 397 | name: "foo", |
| 398 | srcs: ["a.java"], |
| 399 | compile_dex: true, |
| 400 | |
| 401 | hiddenapi_additional_annotations: [ |
| 402 | "foo-hiddenapi-annotations", |
| 403 | ], |
| 404 | } |
| 405 | |
| 406 | java_library { |
| 407 | name: "foo-hiddenapi-annotations", |
| 408 | srcs: ["a.java"], |
| 409 | compile_dex: true, |
| 410 | } |
| 411 | |
| 412 | java_import { |
| 413 | name: "foo", |
| 414 | jars: ["a.jar"], |
| 415 | compile_dex: true, |
| 416 | prefer: false, |
| 417 | } |
| 418 | |
| 419 | java_sdk_library { |
| 420 | name: "bar", |
| 421 | srcs: ["a.java"], |
| 422 | compile_dex: true, |
| 423 | } |
| 424 | |
| 425 | platform_bootclasspath { |
| 426 | name: "myplatform-bootclasspath", |
| 427 | } |
| 428 | `) |
| 429 | |
Paul Duffin | 00b2bfd | 2021-04-12 17:24:36 +0100 | [diff] [blame] | 430 | // Make sure that the foo-hiddenapi-annotations.jar is included in the inputs to the rules that |
| 431 | // creates the index.csv file. |
Paul Duffin | 537ea3d | 2021-05-14 10:38:00 +0100 | [diff] [blame] | 432 | platformBootclasspath := result.ModuleForTests("myplatform-bootclasspath", "android_common") |
| 433 | indexRule := platformBootclasspath.Rule("monolithic_hidden_API_index") |
Paul Duffin | 00b2bfd | 2021-04-12 17:24:36 +0100 | [diff] [blame] | 434 | CheckHiddenAPIRuleInputs(t, ` |
Paul Duffin | 537ea3d | 2021-05-14 10:38:00 +0100 | [diff] [blame] | 435 | .intermediates/bar/android_common/javac/bar.jar |
Paul Duffin | 00b2bfd | 2021-04-12 17:24:36 +0100 | [diff] [blame] | 436 | .intermediates/foo-hiddenapi-annotations/android_common/javac/foo-hiddenapi-annotations.jar |
| 437 | .intermediates/foo/android_common/javac/foo.jar |
Paul Duffin | 537ea3d | 2021-05-14 10:38:00 +0100 | [diff] [blame] | 438 | `, |
| 439 | indexRule) |
Paul Duffin | 00b2bfd | 2021-04-12 17:24:36 +0100 | [diff] [blame] | 440 | } |