Yu Liu | 67a2842 | 2024-03-05 00:36:31 +0000 | [diff] [blame^] | 1 | // Copyright 2024 Google Inc. All rights reserved. |
| 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 apex |
| 16 | |
| 17 | import ( |
| 18 | "testing" |
| 19 | |
| 20 | "android/soong/aconfig/codegen" |
| 21 | "android/soong/android" |
| 22 | "android/soong/cc" |
| 23 | "android/soong/genrule" |
| 24 | "android/soong/java" |
| 25 | "android/soong/rust" |
| 26 | "github.com/google/blueprint/proptools" |
| 27 | ) |
| 28 | |
| 29 | var withAconfigValidationError = android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) { |
| 30 | variables.AconfigContainerValidation = "error" |
| 31 | variables.BuildId = proptools.StringPtr("TEST.BUILD_ID") |
| 32 | }) |
| 33 | |
| 34 | func TestValidationAcrossContainersExportedPass(t *testing.T) { |
| 35 | testCases := []struct { |
| 36 | name string |
| 37 | bp string |
| 38 | }{ |
| 39 | { |
| 40 | name: "Java lib passes for exported containers cross", |
| 41 | bp: apex_default_bp + ` |
| 42 | apex { |
| 43 | name: "myapex", |
| 44 | manifest: ":myapex.manifest", |
| 45 | androidManifest: ":myapex.androidmanifest", |
| 46 | key: "myapex.key", |
| 47 | java_libs: [ |
| 48 | "my_java_library_foo", |
| 49 | ], |
| 50 | updatable: false, |
| 51 | } |
| 52 | java_library { |
| 53 | name: "my_java_library_foo", |
| 54 | srcs: ["foo/bar/MyClass.java"], |
| 55 | sdk_version: "none", |
| 56 | system_modules: "none", |
| 57 | static_libs: ["my_java_aconfig_library_foo"], |
| 58 | apex_available: [ |
| 59 | "myapex", |
| 60 | ], |
| 61 | } |
| 62 | aconfig_declarations { |
| 63 | name: "my_aconfig_declarations_foo", |
| 64 | package: "com.example.package", |
| 65 | container: "otherapex", |
| 66 | srcs: ["foo.aconfig"], |
| 67 | exportable: true, |
| 68 | } |
| 69 | java_aconfig_library { |
| 70 | name: "my_java_aconfig_library_foo", |
| 71 | aconfig_declarations: "my_aconfig_declarations_foo", |
| 72 | mode: "exported", |
| 73 | apex_available: [ |
| 74 | "myapex", |
| 75 | ], |
| 76 | }`, |
| 77 | }, |
| 78 | { |
| 79 | name: "Android app passes for exported containers cross", |
| 80 | bp: apex_default_bp + ` |
| 81 | apex { |
| 82 | name: "myapex", |
| 83 | manifest: ":myapex.manifest", |
| 84 | androidManifest: ":myapex.androidmanifest", |
| 85 | key: "myapex.key", |
| 86 | apps: [ |
| 87 | "my_android_app_foo", |
| 88 | ], |
| 89 | updatable: false, |
| 90 | } |
| 91 | android_app { |
| 92 | name: "my_android_app_foo", |
| 93 | srcs: ["foo/MyClass.java"], |
| 94 | sdk_version: "none", |
| 95 | system_modules: "none", |
| 96 | stl: "none", |
| 97 | static_libs: ["my_java_library_bar"], |
| 98 | apex_available: [ "myapex" ], |
| 99 | } |
| 100 | java_library { |
| 101 | name: "my_java_library_bar", |
| 102 | srcs: ["foo/bar/MyClass.java"], |
| 103 | sdk_version: "none", |
| 104 | system_modules: "none", |
| 105 | static_libs: ["my_java_aconfig_library_bar"], |
| 106 | apex_available: [ |
| 107 | "myapex", |
| 108 | ], |
| 109 | } |
| 110 | aconfig_declarations { |
| 111 | name: "my_aconfig_declarations_bar", |
| 112 | package: "com.example.package", |
| 113 | container: "otherapex", |
| 114 | srcs: ["bar.aconfig"], |
| 115 | exportable: true, |
| 116 | } |
| 117 | java_aconfig_library { |
| 118 | name: "my_java_aconfig_library_bar", |
| 119 | aconfig_declarations: "my_aconfig_declarations_bar", |
| 120 | mode: "exported", |
| 121 | apex_available: [ |
| 122 | "myapex", |
| 123 | ], |
| 124 | }`, |
| 125 | }, |
| 126 | { |
| 127 | name: "Cc lib passes for exported containers cross", |
| 128 | bp: apex_default_bp + ` |
| 129 | apex { |
| 130 | name: "myapex", |
| 131 | manifest: ":myapex.manifest", |
| 132 | androidManifest: ":myapex.androidmanifest", |
| 133 | key: "myapex.key", |
| 134 | native_shared_libs: [ |
| 135 | "my_cc_library_bar", |
| 136 | ], |
| 137 | binaries: [ |
| 138 | "my_cc_binary_baz", |
| 139 | ], |
| 140 | updatable: false, |
| 141 | } |
| 142 | cc_library { |
| 143 | name: "my_cc_library_bar", |
| 144 | srcs: ["foo/bar/MyClass.cc"], |
| 145 | static_libs: [ |
| 146 | "my_cc_aconfig_library_bar", |
| 147 | "my_cc_aconfig_library_baz", |
| 148 | ], |
| 149 | apex_available: [ |
| 150 | "myapex", |
| 151 | ], |
| 152 | } |
| 153 | cc_binary { |
| 154 | name: "my_cc_binary_baz", |
| 155 | srcs: ["foo/bar/MyClass.cc"], |
| 156 | static_libs: ["my_cc_aconfig_library_baz"], |
| 157 | apex_available: [ |
| 158 | "myapex", |
| 159 | ], |
| 160 | } |
| 161 | cc_library { |
| 162 | name: "server_configurable_flags", |
| 163 | srcs: ["server_configurable_flags.cc"], |
| 164 | } |
| 165 | aconfig_declarations { |
| 166 | name: "my_aconfig_declarations_bar", |
| 167 | package: "com.example.package", |
| 168 | container: "otherapex", |
| 169 | srcs: ["bar.aconfig"], |
| 170 | exportable: true, |
| 171 | } |
| 172 | cc_aconfig_library { |
| 173 | name: "my_cc_aconfig_library_bar", |
| 174 | aconfig_declarations: "my_aconfig_declarations_bar", |
| 175 | apex_available: [ |
| 176 | "myapex", |
| 177 | ], |
| 178 | mode: "exported", |
| 179 | } |
| 180 | aconfig_declarations { |
| 181 | name: "my_aconfig_declarations_baz", |
| 182 | package: "com.example.package", |
| 183 | container: "otherapex", |
| 184 | srcs: ["baz.aconfig"], |
| 185 | exportable: true, |
| 186 | } |
| 187 | cc_aconfig_library { |
| 188 | name: "my_cc_aconfig_library_baz", |
| 189 | aconfig_declarations: "my_aconfig_declarations_baz", |
| 190 | apex_available: [ |
| 191 | "myapex", |
| 192 | ], |
| 193 | mode: "exported", |
| 194 | }`, |
| 195 | }, |
| 196 | } |
| 197 | for _, test := range testCases { |
| 198 | t.Run(test.name, func(t *testing.T) { |
| 199 | android.GroupFixturePreparers( |
| 200 | java.PrepareForTestWithJavaDefaultModules, |
| 201 | cc.PrepareForTestWithCcBuildComponents, |
| 202 | rust.PrepareForTestWithRustDefaultModules, |
| 203 | codegen.PrepareForTestWithAconfigBuildComponents, |
| 204 | PrepareForTestWithApexBuildComponents, |
| 205 | prepareForTestWithMyapex, |
| 206 | withAconfigValidationError, |
| 207 | ). |
| 208 | RunTestWithBp(t, test.bp) |
| 209 | }) |
| 210 | } |
| 211 | } |
| 212 | |
| 213 | func TestValidationAcrossContainersNotExportedFail(t *testing.T) { |
| 214 | testCases := []struct { |
| 215 | name string |
| 216 | expectedError string |
| 217 | bp string |
| 218 | }{ |
| 219 | { |
| 220 | name: "Java lib fails for non-exported containers cross", |
| 221 | bp: apex_default_bp + ` |
| 222 | apex { |
| 223 | name: "myapex", |
| 224 | manifest: ":myapex.manifest", |
| 225 | androidManifest: ":myapex.androidmanifest", |
| 226 | key: "myapex.key", |
| 227 | java_libs: [ |
| 228 | "my_java_library_foo", |
| 229 | ], |
| 230 | updatable: false, |
| 231 | } |
| 232 | java_library { |
| 233 | name: "my_java_library_foo", |
| 234 | srcs: ["foo/bar/MyClass.java"], |
| 235 | sdk_version: "none", |
| 236 | system_modules: "none", |
| 237 | static_libs: ["my_java_aconfig_library_foo"], |
| 238 | apex_available: [ |
| 239 | "myapex", |
| 240 | ], |
| 241 | } |
| 242 | aconfig_declarations { |
| 243 | name: "my_aconfig_declarations_foo", |
| 244 | package: "com.example.package", |
| 245 | container: "otherapex", |
| 246 | srcs: ["foo.aconfig"], |
| 247 | } |
| 248 | java_aconfig_library { |
| 249 | name: "my_java_aconfig_library_foo", |
| 250 | aconfig_declarations: "my_aconfig_declarations_foo", |
| 251 | apex_available: [ |
| 252 | "myapex", |
| 253 | ], |
| 254 | }`, |
| 255 | expectedError: `.*my_java_library_foo/myapex depends on my_java_aconfig_library_foo/otherapex/production across containers`, |
| 256 | }, |
| 257 | { |
| 258 | name: "Android app fails for non-exported containers cross", |
| 259 | bp: apex_default_bp + ` |
| 260 | apex { |
| 261 | name: "myapex", |
| 262 | manifest: ":myapex.manifest", |
| 263 | androidManifest: ":myapex.androidmanifest", |
| 264 | key: "myapex.key", |
| 265 | apps: [ |
| 266 | "my_android_app_foo", |
| 267 | ], |
| 268 | updatable: false, |
| 269 | } |
| 270 | android_app { |
| 271 | name: "my_android_app_foo", |
| 272 | srcs: ["foo/MyClass.java"], |
| 273 | sdk_version: "none", |
| 274 | system_modules: "none", |
| 275 | stl: "none", |
| 276 | static_libs: ["my_java_library_foo"], |
| 277 | apex_available: [ "myapex" ], |
| 278 | } |
| 279 | java_library { |
| 280 | name: "my_java_library_foo", |
| 281 | srcs: ["foo/bar/MyClass.java"], |
| 282 | sdk_version: "none", |
| 283 | system_modules: "none", |
| 284 | static_libs: ["my_java_aconfig_library_foo"], |
| 285 | apex_available: [ |
| 286 | "myapex", |
| 287 | ], |
| 288 | } |
| 289 | aconfig_declarations { |
| 290 | name: "my_aconfig_declarations_foo", |
| 291 | package: "com.example.package", |
| 292 | container: "otherapex", |
| 293 | srcs: ["bar.aconfig"], |
| 294 | } |
| 295 | java_aconfig_library { |
| 296 | name: "my_java_aconfig_library_foo", |
| 297 | aconfig_declarations: "my_aconfig_declarations_foo", |
| 298 | apex_available: [ |
| 299 | "myapex", |
| 300 | ], |
| 301 | }`, |
| 302 | expectedError: `.*my_android_app_foo/myapex depends on my_java_aconfig_library_foo/otherapex/production across containers`, |
| 303 | }, |
| 304 | { |
| 305 | name: "Cc lib fails for non-exported containers cross", |
| 306 | bp: apex_default_bp + ` |
| 307 | apex { |
| 308 | name: "myapex", |
| 309 | manifest: ":myapex.manifest", |
| 310 | androidManifest: ":myapex.androidmanifest", |
| 311 | key: "myapex.key", |
| 312 | native_shared_libs: [ |
| 313 | "my_cc_library_foo", |
| 314 | ], |
| 315 | updatable: false, |
| 316 | } |
| 317 | cc_library { |
| 318 | name: "my_cc_library_foo", |
| 319 | srcs: ["foo/bar/MyClass.cc"], |
| 320 | shared_libs: [ |
| 321 | "my_cc_aconfig_library_foo", |
| 322 | ], |
| 323 | apex_available: [ |
| 324 | "myapex", |
| 325 | ], |
| 326 | } |
| 327 | cc_library { |
| 328 | name: "server_configurable_flags", |
| 329 | srcs: ["server_configurable_flags.cc"], |
| 330 | } |
| 331 | aconfig_declarations { |
| 332 | name: "my_aconfig_declarations_foo", |
| 333 | package: "com.example.package", |
| 334 | container: "otherapex", |
| 335 | srcs: ["foo.aconfig"], |
| 336 | } |
| 337 | cc_aconfig_library { |
| 338 | name: "my_cc_aconfig_library_foo", |
| 339 | aconfig_declarations: "my_aconfig_declarations_foo", |
| 340 | apex_available: [ |
| 341 | "myapex", |
| 342 | ], |
| 343 | }`, |
| 344 | expectedError: `.*my_cc_library_foo/myapex depends on my_cc_aconfig_library_foo/otherapex/production across containers`, |
| 345 | }, |
| 346 | { |
| 347 | name: "Cc binary fails for non-exported containers cross", |
| 348 | bp: apex_default_bp + ` |
| 349 | apex { |
| 350 | name: "myapex", |
| 351 | manifest: ":myapex.manifest", |
| 352 | androidManifest: ":myapex.androidmanifest", |
| 353 | key: "myapex.key", |
| 354 | binaries: [ |
| 355 | "my_cc_binary_foo", |
| 356 | ], |
| 357 | updatable: false, |
| 358 | } |
| 359 | cc_library { |
| 360 | name: "my_cc_library_foo", |
| 361 | srcs: ["foo/bar/MyClass.cc"], |
| 362 | static_libs: [ |
| 363 | "my_cc_aconfig_library_foo", |
| 364 | ], |
| 365 | apex_available: [ |
| 366 | "myapex", |
| 367 | ], |
| 368 | } |
| 369 | cc_binary { |
| 370 | name: "my_cc_binary_foo", |
| 371 | srcs: ["foo/bar/MyClass.cc"], |
| 372 | static_libs: ["my_cc_library_foo"], |
| 373 | apex_available: [ |
| 374 | "myapex", |
| 375 | ], |
| 376 | } |
| 377 | cc_library { |
| 378 | name: "server_configurable_flags", |
| 379 | srcs: ["server_configurable_flags.cc"], |
| 380 | } |
| 381 | aconfig_declarations { |
| 382 | name: "my_aconfig_declarations_foo", |
| 383 | package: "com.example.package", |
| 384 | container: "otherapex", |
| 385 | srcs: ["foo.aconfig"], |
| 386 | } |
| 387 | cc_aconfig_library { |
| 388 | name: "my_cc_aconfig_library_foo", |
| 389 | aconfig_declarations: "my_aconfig_declarations_foo", |
| 390 | apex_available: [ |
| 391 | "myapex", |
| 392 | ], |
| 393 | }`, |
| 394 | expectedError: `.*my_cc_binary_foo/myapex depends on my_cc_aconfig_library_foo/otherapex/production across containers`, |
| 395 | }, |
| 396 | { |
| 397 | name: "Aconfig validation propagate along sourceOrOutputDependencyTag", |
| 398 | bp: apex_default_bp + ` |
| 399 | apex { |
| 400 | name: "myapex", |
| 401 | manifest: ":myapex.manifest", |
| 402 | androidManifest: ":myapex.androidmanifest", |
| 403 | key: "myapex.key", |
| 404 | apps: [ |
| 405 | "my_android_app_foo", |
| 406 | ], |
| 407 | updatable: false, |
| 408 | } |
| 409 | android_app { |
| 410 | name: "my_android_app_foo", |
| 411 | srcs: ["foo/MyClass.java"], |
| 412 | sdk_version: "none", |
| 413 | system_modules: "none", |
| 414 | stl: "none", |
| 415 | static_libs: ["my_java_library_foo"], |
| 416 | apex_available: [ "myapex" ], |
| 417 | } |
| 418 | java_library { |
| 419 | name: "my_java_library_foo", |
| 420 | srcs: [":my_genrule_foo"], |
| 421 | sdk_version: "none", |
| 422 | system_modules: "none", |
| 423 | apex_available: [ |
| 424 | "myapex", |
| 425 | ], |
| 426 | } |
| 427 | aconfig_declarations_group { |
| 428 | name: "my_aconfig_declarations_group_foo", |
| 429 | java_aconfig_libraries: [ |
| 430 | "my_java_aconfig_library_foo", |
| 431 | ], |
| 432 | } |
| 433 | filegroup { |
| 434 | name: "my_filegroup_foo_srcjars", |
| 435 | srcs: [ |
| 436 | ":my_aconfig_declarations_group_foo{.srcjars}", |
| 437 | ], |
| 438 | } |
| 439 | genrule { |
| 440 | name: "my_genrule_foo", |
| 441 | srcs: [":my_filegroup_foo_srcjars"], |
| 442 | cmd: "cp $(in) $(out)", |
| 443 | out: ["my_genrule_foo.srcjar"], |
| 444 | } |
| 445 | aconfig_declarations { |
| 446 | name: "my_aconfig_declarations_foo", |
| 447 | package: "com.example.package", |
| 448 | container: "otherapex", |
| 449 | srcs: ["bar.aconfig"], |
| 450 | } |
| 451 | java_aconfig_library { |
| 452 | name: "my_java_aconfig_library_foo", |
| 453 | aconfig_declarations: "my_aconfig_declarations_foo", |
| 454 | apex_available: [ |
| 455 | "myapex", |
| 456 | ], |
| 457 | }`, |
| 458 | expectedError: `.*my_android_app_foo/myapex depends on my_java_aconfig_library_foo/otherapex/production across containers`, |
| 459 | }, |
| 460 | } |
| 461 | for _, test := range testCases { |
| 462 | t.Run(test.name, func(t *testing.T) { |
| 463 | errorHandler := android.FixtureExpectsNoErrors |
| 464 | if test.expectedError != "" { |
| 465 | errorHandler = android.FixtureExpectsAtLeastOneErrorMatchingPattern(test.expectedError) |
| 466 | } |
| 467 | android.GroupFixturePreparers( |
| 468 | java.PrepareForTestWithJavaDefaultModules, |
| 469 | cc.PrepareForTestWithCcBuildComponents, |
| 470 | rust.PrepareForTestWithRustDefaultModules, |
| 471 | codegen.PrepareForTestWithAconfigBuildComponents, |
| 472 | genrule.PrepareForIntegrationTestWithGenrule, |
| 473 | PrepareForTestWithApexBuildComponents, |
| 474 | prepareForTestWithMyapex, |
| 475 | withAconfigValidationError, |
| 476 | ). |
| 477 | ExtendWithErrorHandler(errorHandler). |
| 478 | RunTestWithBp(t, test.bp) |
| 479 | }) |
| 480 | } |
| 481 | } |
| 482 | |
| 483 | func TestValidationNotPropagateAcrossShared(t *testing.T) { |
| 484 | testCases := []struct { |
| 485 | name string |
| 486 | bp string |
| 487 | }{ |
| 488 | { |
| 489 | name: "Java shared lib not propagate aconfig validation", |
| 490 | bp: apex_default_bp + ` |
| 491 | apex { |
| 492 | name: "myapex", |
| 493 | manifest: ":myapex.manifest", |
| 494 | androidManifest: ":myapex.androidmanifest", |
| 495 | key: "myapex.key", |
| 496 | java_libs: [ |
| 497 | "my_java_library_bar", |
| 498 | ], |
| 499 | updatable: false, |
| 500 | } |
| 501 | java_library { |
| 502 | name: "my_java_library_bar", |
| 503 | srcs: ["foo/bar/MyClass.java"], |
| 504 | sdk_version: "none", |
| 505 | system_modules: "none", |
| 506 | libs: ["my_java_library_foo"], |
| 507 | apex_available: [ |
| 508 | "myapex", |
| 509 | ], |
| 510 | } |
| 511 | java_library { |
| 512 | name: "my_java_library_foo", |
| 513 | srcs: ["foo/bar/MyClass.java"], |
| 514 | sdk_version: "none", |
| 515 | system_modules: "none", |
| 516 | static_libs: ["my_java_aconfig_library_foo"], |
| 517 | apex_available: [ |
| 518 | "myapex", |
| 519 | ], |
| 520 | } |
| 521 | aconfig_declarations { |
| 522 | name: "my_aconfig_declarations_foo", |
| 523 | package: "com.example.package", |
| 524 | container: "otherapex", |
| 525 | srcs: ["foo.aconfig"], |
| 526 | } |
| 527 | java_aconfig_library { |
| 528 | name: "my_java_aconfig_library_foo", |
| 529 | aconfig_declarations: "my_aconfig_declarations_foo", |
| 530 | apex_available: [ |
| 531 | "myapex", |
| 532 | ], |
| 533 | }`, |
| 534 | }, |
| 535 | } |
| 536 | for _, test := range testCases { |
| 537 | t.Run(test.name, func(t *testing.T) { |
| 538 | android.GroupFixturePreparers( |
| 539 | java.PrepareForTestWithJavaDefaultModules, |
| 540 | cc.PrepareForTestWithCcBuildComponents, |
| 541 | rust.PrepareForTestWithRustDefaultModules, |
| 542 | codegen.PrepareForTestWithAconfigBuildComponents, |
| 543 | PrepareForTestWithApexBuildComponents, |
| 544 | prepareForTestWithMyapex, |
| 545 | withAconfigValidationError, |
| 546 | ). |
| 547 | RunTestWithBp(t, test.bp) |
| 548 | }) |
| 549 | } |
| 550 | } |