testing.go 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508
  1. // Copyright 2019 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. package java
  15. import (
  16. "fmt"
  17. "reflect"
  18. "regexp"
  19. "sort"
  20. "strings"
  21. "testing"
  22. "android/soong/android"
  23. "android/soong/cc"
  24. "android/soong/dexpreopt"
  25. "github.com/google/blueprint"
  26. )
  27. const defaultJavaDir = "default/java"
  28. // Test fixture preparer that will register most java build components.
  29. //
  30. // Singletons and mutators should only be added here if they are needed for a majority of java
  31. // module types, otherwise they should be added under a separate preparer to allow them to be
  32. // selected only when needed to reduce test execution time.
  33. //
  34. // Module types do not have much of an overhead unless they are used so this should include as many
  35. // module types as possible. The exceptions are those module types that require mutators and/or
  36. // singletons in order to function in which case they should be kept together in a separate
  37. // preparer.
  38. var PrepareForTestWithJavaBuildComponents = android.GroupFixturePreparers(
  39. // Make sure that mutators and module types, e.g. prebuilt mutators available.
  40. android.PrepareForTestWithAndroidBuildComponents,
  41. // Make java build components available to the test.
  42. android.FixtureRegisterWithContext(registerRequiredBuildComponentsForTest),
  43. android.FixtureRegisterWithContext(registerJavaPluginBuildComponents),
  44. // Additional files needed in tests that disallow non-existent source files.
  45. // This includes files that are needed by all, or at least most, instances of a java module type.
  46. android.MockFS{
  47. // Needed for linter used by java_library.
  48. "build/soong/java/lint_defaults.txt": nil,
  49. // Needed for apps that do not provide their own.
  50. "build/make/target/product/security": nil,
  51. }.AddToFixture(),
  52. )
  53. // Test fixture preparer that will define all default java modules except the
  54. // fake_tool_binary for dex2oatd.
  55. var PrepareForTestWithJavaDefaultModulesWithoutFakeDex2oatd = android.GroupFixturePreparers(
  56. // Make sure that all the module types used in the defaults are registered.
  57. PrepareForTestWithJavaBuildComponents,
  58. // Additional files needed when test disallows non-existent source.
  59. android.MockFS{
  60. // Needed for framework-res
  61. defaultJavaDir + "/AndroidManifest.xml": nil,
  62. // Needed for framework
  63. defaultJavaDir + "/framework/aidl": nil,
  64. // Needed for various deps defined in GatherRequiredDepsForTest()
  65. defaultJavaDir + "/a.java": nil,
  66. }.AddToFixture(),
  67. // The java default module definitions.
  68. android.FixtureAddTextFile(defaultJavaDir+"/Android.bp", gatherRequiredDepsForTest()),
  69. // Add dexpreopt compat libs (android.test.base, etc.) and a fake dex2oatd module.
  70. dexpreopt.PrepareForTestWithDexpreoptCompatLibs,
  71. )
  72. // Test fixture preparer that will define default java modules, e.g. standard prebuilt modules.
  73. var PrepareForTestWithJavaDefaultModules = android.GroupFixturePreparers(
  74. PrepareForTestWithJavaDefaultModulesWithoutFakeDex2oatd,
  75. dexpreopt.PrepareForTestWithFakeDex2oatd,
  76. )
  77. // Provides everything needed by dexpreopt.
  78. var PrepareForTestWithDexpreopt = android.GroupFixturePreparers(
  79. PrepareForTestWithJavaDefaultModules,
  80. dexpreopt.PrepareForTestByEnablingDexpreopt,
  81. )
  82. var PrepareForTestWithOverlayBuildComponents = android.FixtureRegisterWithContext(registerOverlayBuildComponents)
  83. // Prepare a fixture to use all java module types, mutators and singletons fully.
  84. //
  85. // This should only be used by tests that want to run with as much of the build enabled as possible.
  86. var PrepareForIntegrationTestWithJava = android.GroupFixturePreparers(
  87. cc.PrepareForIntegrationTestWithCc,
  88. PrepareForTestWithJavaDefaultModules,
  89. )
  90. // Prepare a fixture with the standard files required by a java_sdk_library module.
  91. var PrepareForTestWithJavaSdkLibraryFiles = android.FixtureMergeMockFs(android.MockFS{
  92. "api/current.txt": nil,
  93. "api/removed.txt": nil,
  94. "api/system-current.txt": nil,
  95. "api/system-removed.txt": nil,
  96. "api/test-current.txt": nil,
  97. "api/test-removed.txt": nil,
  98. "api/module-lib-current.txt": nil,
  99. "api/module-lib-removed.txt": nil,
  100. "api/system-server-current.txt": nil,
  101. "api/system-server-removed.txt": nil,
  102. })
  103. // FixtureWithLastReleaseApis creates a preparer that creates prebuilt versions of the specified
  104. // modules for the `last` API release. By `last` it just means last in the list of supplied versions
  105. // and as this only provides one version it can be any value.
  106. //
  107. // This uses FixtureWithPrebuiltApis under the covers so the limitations of that apply to this.
  108. func FixtureWithLastReleaseApis(moduleNames ...string) android.FixturePreparer {
  109. return FixtureWithPrebuiltApis(map[string][]string{
  110. "30": moduleNames,
  111. })
  112. }
  113. // PrepareForTestWithPrebuiltsOfCurrentApi is a preparer that creates prebuilt versions of the
  114. // standard modules for the current version.
  115. //
  116. // This uses FixtureWithPrebuiltApis under the covers so the limitations of that apply to this.
  117. var PrepareForTestWithPrebuiltsOfCurrentApi = FixtureWithPrebuiltApis(map[string][]string{
  118. "current": {},
  119. // Can't have current on its own as it adds a prebuilt_apis module but doesn't add any
  120. // .txt files which causes the prebuilt_apis module to fail.
  121. "30": {},
  122. })
  123. // FixtureWithPrebuiltApis creates a preparer that will define prebuilt api modules for the
  124. // specified releases and modules.
  125. //
  126. // The supplied map keys are the releases, e.g. current, 29, 30, etc. The values are a list of
  127. // modules for that release. Due to limitations in the prebuilt_apis module which this preparer
  128. // uses the set of releases must include at least one numbered release, i.e. it cannot just include
  129. // "current".
  130. //
  131. // This defines a file in the mock file system in a predefined location (prebuilts/sdk/Android.bp)
  132. // and so only one instance of this can be used in each fixture.
  133. func FixtureWithPrebuiltApis(release2Modules map[string][]string) android.FixturePreparer {
  134. mockFS := android.MockFS{}
  135. path := "prebuilts/sdk/Android.bp"
  136. bp := fmt.Sprintf(`
  137. prebuilt_apis {
  138. name: "sdk",
  139. api_dirs: ["%s"],
  140. imports_sdk_version: "none",
  141. imports_compile_dex: true,
  142. }
  143. `, strings.Join(android.SortedStringKeys(release2Modules), `", "`))
  144. for release, modules := range release2Modules {
  145. mockFS.Merge(prebuiltApisFilesForModules([]string{release}, modules))
  146. }
  147. return android.GroupFixturePreparers(
  148. android.FixtureAddTextFile(path, bp),
  149. android.FixtureMergeMockFs(mockFS),
  150. )
  151. }
  152. func prebuiltApisFilesForModules(apiLevels []string, modules []string) map[string][]byte {
  153. libs := append([]string{"android"}, modules...)
  154. fs := make(map[string][]byte)
  155. for _, level := range apiLevels {
  156. apiLevel := android.ApiLevelForTest(level)
  157. for _, sdkKind := range []android.SdkKind{android.SdkPublic, android.SdkSystem, android.SdkModule, android.SdkSystemServer, android.SdkTest} {
  158. // A core-for-system-modules file must only be created for the sdk kind that supports it.
  159. if sdkKind == systemModuleKind(sdkKind, apiLevel) {
  160. fs[fmt.Sprintf("prebuilts/sdk/%s/%s/core-for-system-modules.jar", level, sdkKind)] = nil
  161. }
  162. for _, lib := range libs {
  163. // Create a jar file for every library.
  164. fs[fmt.Sprintf("prebuilts/sdk/%s/%s/%s.jar", level, sdkKind, lib)] = nil
  165. // No finalized API files for "current"
  166. if level != "current" {
  167. fs[fmt.Sprintf("prebuilts/sdk/%s/%s/api/%s.txt", level, sdkKind, lib)] = nil
  168. fs[fmt.Sprintf("prebuilts/sdk/%s/%s/api/%s-removed.txt", level, sdkKind, lib)] = nil
  169. }
  170. }
  171. }
  172. if level == "current" {
  173. fs["prebuilts/sdk/current/core/android.jar"] = nil
  174. }
  175. fs[fmt.Sprintf("prebuilts/sdk/%s/public/framework.aidl", level)] = nil
  176. }
  177. return fs
  178. }
  179. // FixtureConfigureBootJars configures the boot jars in both the dexpreopt.GlobalConfig and
  180. // Config.productVariables structs. As a side effect that enables dexpreopt.
  181. func FixtureConfigureBootJars(bootJars ...string) android.FixturePreparer {
  182. artBootJars := []string{}
  183. for _, j := range bootJars {
  184. artApex := false
  185. for _, artApexName := range artApexNames {
  186. if strings.HasPrefix(j, artApexName+":") {
  187. artApex = true
  188. break
  189. }
  190. }
  191. if artApex {
  192. artBootJars = append(artBootJars, j)
  193. }
  194. }
  195. return android.GroupFixturePreparers(
  196. android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
  197. variables.BootJars = android.CreateTestConfiguredJarList(bootJars)
  198. }),
  199. dexpreopt.FixtureSetBootJars(bootJars...),
  200. dexpreopt.FixtureSetArtBootJars(artBootJars...),
  201. // Add a fake dex2oatd module.
  202. dexpreopt.PrepareForTestWithFakeDex2oatd,
  203. )
  204. }
  205. // FixtureConfigureApexBootJars configures the apex boot jars in both the
  206. // dexpreopt.GlobalConfig and Config.productVariables structs. As a side effect that enables
  207. // dexpreopt.
  208. func FixtureConfigureApexBootJars(bootJars ...string) android.FixturePreparer {
  209. return android.GroupFixturePreparers(
  210. android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
  211. variables.ApexBootJars = android.CreateTestConfiguredJarList(bootJars)
  212. }),
  213. dexpreopt.FixtureSetApexBootJars(bootJars...),
  214. // Add a fake dex2oatd module.
  215. dexpreopt.PrepareForTestWithFakeDex2oatd,
  216. )
  217. }
  218. // FixtureUseLegacyCorePlatformApi prepares the fixture by setting the exception list of those
  219. // modules that are allowed to use the legacy core platform API to be the ones supplied.
  220. func FixtureUseLegacyCorePlatformApi(moduleNames ...string) android.FixturePreparer {
  221. lookup := make(map[string]struct{})
  222. for _, moduleName := range moduleNames {
  223. lookup[moduleName] = struct{}{}
  224. }
  225. return android.FixtureModifyConfig(func(config android.Config) {
  226. // Try and set the legacyCorePlatformApiLookup in the config, the returned value will be the
  227. // actual value that is set.
  228. cached := config.Once(legacyCorePlatformApiLookupKey, func() interface{} {
  229. return lookup
  230. })
  231. // Make sure that the cached value is the one we need.
  232. if !reflect.DeepEqual(cached, lookup) {
  233. panic(fmt.Errorf("attempting to set legacyCorePlatformApiLookupKey to %q but it has already been set to %q", lookup, cached))
  234. }
  235. })
  236. }
  237. // registerRequiredBuildComponentsForTest registers the build components used by
  238. // PrepareForTestWithJavaDefaultModules.
  239. //
  240. // As functionality is moved out of here into separate FixturePreparer instances they should also
  241. // be moved into GatherRequiredDepsForTest for use by tests that have not yet switched to use test
  242. // fixtures.
  243. func registerRequiredBuildComponentsForTest(ctx android.RegistrationContext) {
  244. RegisterAARBuildComponents(ctx)
  245. RegisterAppBuildComponents(ctx)
  246. RegisterAppImportBuildComponents(ctx)
  247. RegisterAppSetBuildComponents(ctx)
  248. registerBootclasspathBuildComponents(ctx)
  249. registerBootclasspathFragmentBuildComponents(ctx)
  250. RegisterDexpreoptBootJarsComponents(ctx)
  251. RegisterDocsBuildComponents(ctx)
  252. RegisterGenRuleBuildComponents(ctx)
  253. registerJavaBuildComponents(ctx)
  254. registerPlatformBootclasspathBuildComponents(ctx)
  255. RegisterPrebuiltApisBuildComponents(ctx)
  256. RegisterRuntimeResourceOverlayBuildComponents(ctx)
  257. RegisterSdkLibraryBuildComponents(ctx)
  258. RegisterStubsBuildComponents(ctx)
  259. RegisterSystemModulesBuildComponents(ctx)
  260. registerSystemserverClasspathBuildComponents(ctx)
  261. registerLintBuildComponents(ctx)
  262. }
  263. // gatherRequiredDepsForTest gathers the module definitions used by
  264. // PrepareForTestWithJavaDefaultModules.
  265. //
  266. // As functionality is moved out of here into separate FixturePreparer instances they should also
  267. // be moved into GatherRequiredDepsForTest for use by tests that have not yet switched to use test
  268. // fixtures.
  269. func gatherRequiredDepsForTest() string {
  270. var bp string
  271. extraModules := []string{
  272. "core-lambda-stubs",
  273. "ext",
  274. "android_stubs_current",
  275. "android_system_stubs_current",
  276. "android_test_stubs_current",
  277. "android_module_lib_stubs_current",
  278. "android_system_server_stubs_current",
  279. "core.current.stubs",
  280. "legacy.core.platform.api.stubs",
  281. "stable.core.platform.api.stubs",
  282. "kotlin-stdlib",
  283. "kotlin-stdlib-jdk7",
  284. "kotlin-stdlib-jdk8",
  285. "kotlin-annotations",
  286. "stub-annotations",
  287. }
  288. for _, extra := range extraModules {
  289. bp += fmt.Sprintf(`
  290. java_library {
  291. name: "%s",
  292. srcs: ["a.java"],
  293. sdk_version: "none",
  294. system_modules: "stable-core-platform-api-stubs-system-modules",
  295. compile_dex: true,
  296. }
  297. `, extra)
  298. }
  299. bp += `
  300. java_library {
  301. name: "framework",
  302. srcs: ["a.java"],
  303. sdk_version: "none",
  304. system_modules: "stable-core-platform-api-stubs-system-modules",
  305. aidl: {
  306. export_include_dirs: ["framework/aidl"],
  307. },
  308. }
  309. android_app {
  310. name: "framework-res",
  311. sdk_version: "core_platform",
  312. }`
  313. systemModules := []string{
  314. "core-public-stubs-system-modules",
  315. "core-module-lib-stubs-system-modules",
  316. "legacy-core-platform-api-stubs-system-modules",
  317. "stable-core-platform-api-stubs-system-modules",
  318. }
  319. for _, extra := range systemModules {
  320. bp += fmt.Sprintf(`
  321. java_system_modules {
  322. name: "%[1]s",
  323. libs: ["%[1]s-lib"],
  324. }
  325. java_library {
  326. name: "%[1]s-lib",
  327. sdk_version: "none",
  328. system_modules: "none",
  329. }
  330. `, extra)
  331. }
  332. // Make sure that the dex_bootjars singleton module is instantiated for the tests.
  333. bp += `
  334. dex_bootjars {
  335. name: "dex_bootjars",
  336. }
  337. `
  338. return bp
  339. }
  340. func CheckModuleDependencies(t *testing.T, ctx *android.TestContext, name, variant string, expected []string) {
  341. t.Helper()
  342. module := ctx.ModuleForTests(name, variant).Module()
  343. deps := []string{}
  344. ctx.VisitDirectDeps(module, func(m blueprint.Module) {
  345. deps = append(deps, m.Name())
  346. })
  347. sort.Strings(deps)
  348. if actual := deps; !reflect.DeepEqual(expected, actual) {
  349. t.Errorf("expected %#q, found %#q", expected, actual)
  350. }
  351. }
  352. // CheckPlatformBootclasspathModules returns the apex:module pair for the modules depended upon by
  353. // the platform-bootclasspath module.
  354. func CheckPlatformBootclasspathModules(t *testing.T, result *android.TestResult, name string, expected []string) {
  355. t.Helper()
  356. platformBootclasspath := result.Module(name, "android_common").(*platformBootclasspathModule)
  357. pairs := ApexNamePairsFromModules(result.TestContext, platformBootclasspath.configuredModules)
  358. android.AssertDeepEquals(t, fmt.Sprintf("%s modules", "platform-bootclasspath"), expected, pairs)
  359. }
  360. func CheckClasspathFragmentProtoContentInfoProvider(t *testing.T, result *android.TestResult, generated bool, contents, outputFilename, installDir string) {
  361. t.Helper()
  362. p := result.Module("platform-bootclasspath", "android_common").(*platformBootclasspathModule)
  363. info := result.ModuleProvider(p, ClasspathFragmentProtoContentInfoProvider).(ClasspathFragmentProtoContentInfo)
  364. android.AssertBoolEquals(t, "classpath proto generated", generated, info.ClasspathFragmentProtoGenerated)
  365. android.AssertStringEquals(t, "classpath proto contents", contents, info.ClasspathFragmentProtoContents.String())
  366. android.AssertStringEquals(t, "output filepath", outputFilename, info.ClasspathFragmentProtoOutput.Base())
  367. android.AssertPathRelativeToTopEquals(t, "install filepath", installDir, info.ClasspathFragmentProtoInstallDir)
  368. }
  369. // ApexNamePairsFromModules returns the apex:module pair for the supplied modules.
  370. func ApexNamePairsFromModules(ctx *android.TestContext, modules []android.Module) []string {
  371. pairs := []string{}
  372. for _, module := range modules {
  373. pairs = append(pairs, apexNamePairFromModule(ctx, module))
  374. }
  375. return pairs
  376. }
  377. func apexNamePairFromModule(ctx *android.TestContext, module android.Module) string {
  378. name := module.Name()
  379. var apex string
  380. apexInfo := ctx.ModuleProvider(module, android.ApexInfoProvider).(android.ApexInfo)
  381. if apexInfo.IsForPlatform() {
  382. apex = "platform"
  383. } else {
  384. apex = apexInfo.InApexVariants[0]
  385. }
  386. return fmt.Sprintf("%s:%s", apex, name)
  387. }
  388. // CheckPlatformBootclasspathFragments returns the apex:module pair for the fragments depended upon
  389. // by the platform-bootclasspath module.
  390. func CheckPlatformBootclasspathFragments(t *testing.T, result *android.TestResult, name string, expected []string) {
  391. t.Helper()
  392. platformBootclasspath := result.Module(name, "android_common").(*platformBootclasspathModule)
  393. pairs := ApexNamePairsFromModules(result.TestContext, platformBootclasspath.fragments)
  394. android.AssertDeepEquals(t, fmt.Sprintf("%s fragments", "platform-bootclasspath"), expected, pairs)
  395. }
  396. func CheckHiddenAPIRuleInputs(t *testing.T, message string, expected string, hiddenAPIRule android.TestingBuildParams) {
  397. t.Helper()
  398. inputs := android.Paths{}
  399. if hiddenAPIRule.Input != nil {
  400. inputs = append(inputs, hiddenAPIRule.Input)
  401. }
  402. inputs = append(inputs, hiddenAPIRule.Inputs...)
  403. inputs = append(inputs, hiddenAPIRule.Implicits...)
  404. inputs = android.SortedUniquePaths(inputs)
  405. actual := strings.TrimSpace(strings.Join(inputs.RelativeToTop().Strings(), "\n"))
  406. re := regexp.MustCompile(`\n\s+`)
  407. expected = strings.TrimSpace(re.ReplaceAllString(expected, "\n"))
  408. if actual != expected {
  409. t.Errorf("Expected hiddenapi rule inputs - %s:\n%s\nactual inputs:\n%s", message, expected, actual)
  410. }
  411. }
  412. // Check that the merged file create by platform_compat_config_singleton has the correct inputs.
  413. func CheckMergedCompatConfigInputs(t *testing.T, result *android.TestResult, message string, expectedPaths ...string) {
  414. sourceGlobalCompatConfig := result.SingletonForTests("platform_compat_config_singleton")
  415. allOutputs := sourceGlobalCompatConfig.AllOutputs()
  416. android.AssertIntEquals(t, message+": output len", 1, len(allOutputs))
  417. output := sourceGlobalCompatConfig.Output(allOutputs[0])
  418. android.AssertPathsRelativeToTopEquals(t, message+": inputs", expectedPaths, output.Implicits)
  419. }
  420. // Register the fake APEX mutator to `android.InitRegistrationContext` as if the real mutator exists
  421. // at runtime. This must be called in `init()` of a test if the test is going to use the fake APEX
  422. // mutator. Otherwise, we will be missing the runtime mutator because "soong-apex" is not a
  423. // dependency, which will cause an inconsistency between testing and runtime mutators.
  424. func RegisterFakeRuntimeApexMutator() {
  425. registerFakeApexMutator(android.InitRegistrationContext)
  426. }
  427. var PrepareForTestWithFakeApexMutator = android.GroupFixturePreparers(
  428. android.FixtureRegisterWithContext(registerFakeApexMutator),
  429. )
  430. func registerFakeApexMutator(ctx android.RegistrationContext) {
  431. ctx.PostDepsMutators(func(ctx android.RegisterMutatorsContext) {
  432. ctx.BottomUp("apex", fakeApexMutator).Parallel()
  433. })
  434. }
  435. type apexModuleBase interface {
  436. ApexAvailable() []string
  437. }
  438. var _ apexModuleBase = (*Library)(nil)
  439. var _ apexModuleBase = (*SdkLibrary)(nil)
  440. // A fake APEX mutator that creates a platform variant and an APEX variant for modules with
  441. // `apex_available`. It helps us avoid a dependency on the real mutator defined in "soong-apex",
  442. // which will cause a cyclic dependency, and it provides an easy way to create an APEX variant for
  443. // testing without dealing with all the complexities in the real mutator.
  444. func fakeApexMutator(mctx android.BottomUpMutatorContext) {
  445. switch mctx.Module().(type) {
  446. case *Library, *SdkLibrary:
  447. if len(mctx.Module().(apexModuleBase).ApexAvailable()) > 0 {
  448. modules := mctx.CreateVariations("", "apex1000")
  449. apexInfo := android.ApexInfo{
  450. ApexVariationName: "apex1000",
  451. }
  452. mctx.SetVariationProvider(modules[1], android.ApexInfoProvider, apexInfo)
  453. }
  454. }
  455. }