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 | 60264a0 | 2021-04-12 20:02:36 +0100 | [diff] [blame] | 36 | FixtureConfigureBootJars("platform:foo", "platform: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, |
| 48 | } |
| 49 | `), |
| 50 | ) |
| 51 | |
| 52 | var addSourceBootclassPathModule = android.FixtureAddTextFile("source/Android.bp", ` |
| 53 | java_library { |
| 54 | name: "foo", |
| 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 | bb7f1ac | 2021-03-29 22:18:45 +0100 | [diff] [blame] | 135 | } |
Paul Duffin | 6a76645 | 2021-04-12 14:15:22 +0100 | [diff] [blame] | 136 | |
Paul Duffin | 9b381ef | 2021-04-08 23:01:37 +0100 | [diff] [blame^] | 137 | func TestPlatformBootclasspath_Fragments(t *testing.T) { |
| 138 | result := android.GroupFixturePreparers( |
| 139 | prepareForTestWithPlatformBootclasspath, |
| 140 | android.FixtureWithRootAndroidBp(` |
| 141 | platform_bootclasspath { |
| 142 | name: "platform-bootclasspath", |
| 143 | fragments: [ |
| 144 | {module:"bar-fragment"}, |
| 145 | ], |
| 146 | hidden_api: { |
| 147 | unsupported: [ |
| 148 | "unsupported.txt", |
| 149 | ], |
| 150 | removed: [ |
| 151 | "removed.txt", |
| 152 | ], |
| 153 | max_target_r_low_priority: [ |
| 154 | "max-target-r-low-priority.txt", |
| 155 | ], |
| 156 | max_target_q: [ |
| 157 | "max-target-q.txt", |
| 158 | ], |
| 159 | max_target_p: [ |
| 160 | "max-target-p.txt", |
| 161 | ], |
| 162 | max_target_o_low_priority: [ |
| 163 | "max-target-o-low-priority.txt", |
| 164 | ], |
| 165 | blocked: [ |
| 166 | "blocked.txt", |
| 167 | ], |
| 168 | unsupported_packages: [ |
| 169 | "unsupported-packages.txt", |
| 170 | ], |
| 171 | }, |
| 172 | } |
| 173 | |
| 174 | bootclasspath_fragment { |
| 175 | name: "bar-fragment", |
| 176 | contents: ["bar"], |
| 177 | hidden_api: { |
| 178 | unsupported: [ |
| 179 | "bar-unsupported.txt", |
| 180 | ], |
| 181 | removed: [ |
| 182 | "bar-removed.txt", |
| 183 | ], |
| 184 | max_target_r_low_priority: [ |
| 185 | "bar-max-target-r-low-priority.txt", |
| 186 | ], |
| 187 | max_target_q: [ |
| 188 | "bar-max-target-q.txt", |
| 189 | ], |
| 190 | max_target_p: [ |
| 191 | "bar-max-target-p.txt", |
| 192 | ], |
| 193 | max_target_o_low_priority: [ |
| 194 | "bar-max-target-o-low-priority.txt", |
| 195 | ], |
| 196 | blocked: [ |
| 197 | "bar-blocked.txt", |
| 198 | ], |
| 199 | unsupported_packages: [ |
| 200 | "bar-unsupported-packages.txt", |
| 201 | ], |
| 202 | }, |
| 203 | } |
| 204 | |
| 205 | java_library { |
| 206 | name: "bar", |
| 207 | srcs: ["a.java"], |
| 208 | system_modules: "none", |
| 209 | sdk_version: "none", |
| 210 | compile_dex: true, |
| 211 | } |
| 212 | `), |
| 213 | ).RunTest(t) |
| 214 | |
| 215 | pbcp := result.Module("platform-bootclasspath", "android_common") |
| 216 | info := result.ModuleProvider(pbcp, hiddenAPIFlagFileInfoProvider).(hiddenAPIFlagFileInfo) |
| 217 | |
| 218 | for _, category := range hiddenAPIFlagFileCategories { |
| 219 | name := category.propertyName |
| 220 | message := fmt.Sprintf("category %s", name) |
| 221 | filename := strings.ReplaceAll(name, "_", "-") |
| 222 | expected := []string{fmt.Sprintf("%s.txt", filename), fmt.Sprintf("bar-%s.txt", filename)} |
| 223 | android.AssertPathsRelativeToTopEquals(t, message, expected, info.categoryToPaths[category]) |
| 224 | } |
| 225 | } |
| 226 | |
Artur Satayev | 97259dc | 2021-04-07 15:17:14 +0100 | [diff] [blame] | 227 | func TestPlatformBootclasspathVariant(t *testing.T) { |
| 228 | result := android.GroupFixturePreparers( |
| 229 | prepareForTestWithPlatformBootclasspath, |
| 230 | android.FixtureWithRootAndroidBp(` |
| 231 | platform_bootclasspath { |
| 232 | name: "platform-bootclasspath", |
| 233 | } |
| 234 | `), |
| 235 | ).RunTest(t) |
| 236 | |
| 237 | variants := result.ModuleVariantsForTests("platform-bootclasspath") |
| 238 | android.AssertIntEquals(t, "expect 1 variant", 1, len(variants)) |
| 239 | } |
| 240 | |
| 241 | func TestPlatformBootclasspath_ClasspathFragmentPaths(t *testing.T) { |
| 242 | result := android.GroupFixturePreparers( |
| 243 | prepareForTestWithPlatformBootclasspath, |
| 244 | android.FixtureWithRootAndroidBp(` |
| 245 | platform_bootclasspath { |
| 246 | name: "platform-bootclasspath", |
| 247 | } |
| 248 | `), |
| 249 | ).RunTest(t) |
| 250 | |
| 251 | p := result.Module("platform-bootclasspath", "android_common").(*platformBootclasspathModule) |
| 252 | android.AssertStringEquals(t, "output filepath", p.Name()+".pb", p.ClasspathFragmentBase.outputFilepath.Base()) |
| 253 | android.AssertPathRelativeToTopEquals(t, "install filepath", "out/soong/target/product/test_device/system/etc/classpaths", p.ClasspathFragmentBase.installDirPath) |
| 254 | } |
| 255 | |
| 256 | func TestPlatformBootclasspathModule_AndroidMkEntries(t *testing.T) { |
| 257 | preparer := android.GroupFixturePreparers( |
| 258 | prepareForTestWithPlatformBootclasspath, |
| 259 | android.FixtureWithRootAndroidBp(` |
| 260 | platform_bootclasspath { |
| 261 | name: "platform-bootclasspath", |
| 262 | } |
| 263 | `), |
| 264 | ) |
| 265 | |
| 266 | t.Run("AndroidMkEntries", func(t *testing.T) { |
| 267 | result := preparer.RunTest(t) |
| 268 | |
| 269 | p := result.Module("platform-bootclasspath", "android_common").(*platformBootclasspathModule) |
| 270 | |
| 271 | entries := android.AndroidMkEntriesForTest(t, result.TestContext, p) |
| 272 | android.AssertIntEquals(t, "AndroidMkEntries count", 2, len(entries)) |
| 273 | }) |
| 274 | |
| 275 | t.Run("hiddenapi-flags-entry", func(t *testing.T) { |
| 276 | result := preparer.RunTest(t) |
| 277 | |
| 278 | p := result.Module("platform-bootclasspath", "android_common").(*platformBootclasspathModule) |
| 279 | |
| 280 | entries := android.AndroidMkEntriesForTest(t, result.TestContext, p) |
| 281 | got := entries[0].OutputFile |
| 282 | android.AssertBoolEquals(t, "valid output path", true, got.Valid()) |
| 283 | android.AssertSame(t, "output filepath", p.hiddenAPIFlagsCSV, got.Path()) |
| 284 | }) |
| 285 | |
| 286 | t.Run("classpath-fragment-entry", func(t *testing.T) { |
| 287 | result := preparer.RunTest(t) |
| 288 | |
| 289 | want := map[string][]string{ |
| 290 | "LOCAL_MODULE": {"platform-bootclasspath"}, |
| 291 | "LOCAL_MODULE_CLASS": {"ETC"}, |
| 292 | "LOCAL_INSTALLED_MODULE_STEM": {"platform-bootclasspath.pb"}, |
| 293 | // Output and Install paths are tested separately in TestPlatformBootclasspath_ClasspathFragmentPaths |
| 294 | } |
| 295 | |
| 296 | p := result.Module("platform-bootclasspath", "android_common").(*platformBootclasspathModule) |
| 297 | |
| 298 | entries := android.AndroidMkEntriesForTest(t, result.TestContext, p) |
| 299 | got := entries[1] |
| 300 | for k, expectedValue := range want { |
| 301 | if value, ok := got.EntryMap[k]; ok { |
| 302 | android.AssertDeepEquals(t, k, expectedValue, value) |
| 303 | } else { |
| 304 | t.Errorf("No %s defined, saw %q", k, got.EntryMap) |
| 305 | } |
| 306 | } |
| 307 | }) |
| 308 | } |
| 309 | |
Paul Duffin | 6a76645 | 2021-04-12 14:15:22 +0100 | [diff] [blame] | 310 | func TestPlatformBootclasspath_Dist(t *testing.T) { |
| 311 | result := android.GroupFixturePreparers( |
| 312 | prepareForTestWithPlatformBootclasspath, |
Paul Duffin | 60264a0 | 2021-04-12 20:02:36 +0100 | [diff] [blame] | 313 | FixtureConfigureBootJars("platform:foo", "platform:bar"), |
Paul Duffin | 6a76645 | 2021-04-12 14:15:22 +0100 | [diff] [blame] | 314 | android.PrepareForTestWithAndroidMk, |
| 315 | android.FixtureWithRootAndroidBp(` |
| 316 | platform_bootclasspath { |
| 317 | name: "platform-bootclasspath", |
| 318 | dists: [ |
| 319 | { |
| 320 | targets: ["droidcore"], |
| 321 | tag: "hiddenapi-flags.csv", |
| 322 | }, |
| 323 | ], |
| 324 | } |
| 325 | |
| 326 | java_library { |
| 327 | name: "bar", |
| 328 | srcs: ["a.java"], |
| 329 | system_modules: "none", |
| 330 | sdk_version: "none", |
| 331 | compile_dex: true, |
| 332 | } |
| 333 | |
| 334 | java_library { |
| 335 | name: "foo", |
| 336 | srcs: ["a.java"], |
| 337 | system_modules: "none", |
| 338 | sdk_version: "none", |
| 339 | compile_dex: true, |
| 340 | } |
| 341 | `), |
| 342 | ).RunTest(t) |
| 343 | |
| 344 | platformBootclasspath := result.Module("platform-bootclasspath", "android_common").(*platformBootclasspathModule) |
| 345 | entries := android.AndroidMkEntriesForTest(t, result.TestContext, platformBootclasspath) |
| 346 | goals := entries[0].GetDistForGoals(platformBootclasspath) |
| 347 | android.AssertStringEquals(t, "platform dist goals phony", ".PHONY: droidcore\n", goals[0]) |
| 348 | 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])) |
| 349 | } |
Paul Duffin | 00b2bfd | 2021-04-12 17:24:36 +0100 | [diff] [blame] | 350 | |
Paul Duffin | 85dee5d | 2021-04-13 00:14:38 +0100 | [diff] [blame] | 351 | func TestPlatformBootclasspath_HiddenAPIMonolithicFiles(t *testing.T) { |
Paul Duffin | 00b2bfd | 2021-04-12 17:24:36 +0100 | [diff] [blame] | 352 | result := android.GroupFixturePreparers( |
| 353 | hiddenApiFixtureFactory, |
| 354 | PrepareForTestWithJavaSdkLibraryFiles, |
| 355 | FixtureWithLastReleaseApis("bar"), |
| 356 | FixtureConfigureBootJars("platform:foo", "platform:bar"), |
| 357 | ).RunTestWithBp(t, ` |
| 358 | java_library { |
| 359 | name: "foo", |
| 360 | srcs: ["a.java"], |
| 361 | compile_dex: true, |
| 362 | |
| 363 | hiddenapi_additional_annotations: [ |
| 364 | "foo-hiddenapi-annotations", |
| 365 | ], |
| 366 | } |
| 367 | |
| 368 | java_library { |
| 369 | name: "foo-hiddenapi-annotations", |
| 370 | srcs: ["a.java"], |
| 371 | compile_dex: true, |
| 372 | } |
| 373 | |
| 374 | java_import { |
| 375 | name: "foo", |
| 376 | jars: ["a.jar"], |
| 377 | compile_dex: true, |
| 378 | prefer: false, |
| 379 | } |
| 380 | |
| 381 | java_sdk_library { |
| 382 | name: "bar", |
| 383 | srcs: ["a.java"], |
| 384 | compile_dex: true, |
| 385 | } |
| 386 | |
| 387 | platform_bootclasspath { |
| 388 | name: "myplatform-bootclasspath", |
| 389 | } |
| 390 | `) |
| 391 | |
| 392 | platformBootclasspath := result.ModuleForTests("myplatform-bootclasspath", "android_common") |
| 393 | indexRule := platformBootclasspath.Rule("platform-bootclasspath-monolithic-hiddenapi-index") |
| 394 | CheckHiddenAPIRuleInputs(t, ` |
| 395 | .intermediates/bar/android_common/hiddenapi/index.csv |
| 396 | .intermediates/foo/android_common/hiddenapi/index.csv |
| 397 | `, |
| 398 | indexRule) |
| 399 | |
| 400 | // Make sure that the foo-hiddenapi-annotations.jar is included in the inputs to the rules that |
| 401 | // creates the index.csv file. |
| 402 | foo := result.ModuleForTests("foo", "android_common") |
| 403 | indexParams := foo.Output("hiddenapi/index.csv") |
| 404 | CheckHiddenAPIRuleInputs(t, ` |
| 405 | .intermediates/foo-hiddenapi-annotations/android_common/javac/foo-hiddenapi-annotations.jar |
| 406 | .intermediates/foo/android_common/javac/foo.jar |
| 407 | `, indexParams) |
| 408 | } |