hiddenapi_singleton_test.go 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. // Copyright 2020 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. "path/filepath"
  18. "testing"
  19. "android/soong/android"
  20. "github.com/google/blueprint/proptools"
  21. )
  22. func fixtureSetBootJarsProductVariable(bootJars ...string) android.FixturePreparer {
  23. return android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
  24. variables.BootJars = android.CreateTestConfiguredJarList(bootJars)
  25. })
  26. }
  27. func fixtureSetPrebuiltHiddenApiDirProductVariable(prebuiltHiddenApiDir *string) android.FixturePreparer {
  28. return android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
  29. variables.PrebuiltHiddenApiDir = prebuiltHiddenApiDir
  30. })
  31. }
  32. var hiddenApiFixtureFactory = android.GroupFixturePreparers(
  33. prepareForJavaTest, PrepareForTestWithHiddenApiBuildComponents)
  34. func TestHiddenAPISingleton(t *testing.T) {
  35. result := hiddenApiFixtureFactory.Extend(
  36. fixtureSetBootJarsProductVariable("platform:foo"),
  37. ).RunTestWithBp(t, `
  38. java_library {
  39. name: "foo",
  40. srcs: ["a.java"],
  41. compile_dex: true,
  42. }
  43. `)
  44. hiddenAPI := result.SingletonForTests("hiddenapi")
  45. hiddenapiRule := hiddenAPI.Rule("hiddenapi")
  46. want := "--boot-dex=out/soong/.intermediates/foo/android_common/aligned/foo.jar"
  47. android.AssertStringDoesContain(t, "hiddenapi command", hiddenapiRule.RuleParams.Command, want)
  48. }
  49. func TestHiddenAPIIndexSingleton(t *testing.T) {
  50. result := hiddenApiFixtureFactory.Extend(
  51. PrepareForTestWithJavaSdkLibraryFiles,
  52. FixtureWithLastReleaseApis("bar"),
  53. fixtureSetBootJarsProductVariable("platform:foo", "platform:bar"),
  54. ).RunTestWithBp(t, `
  55. java_library {
  56. name: "foo",
  57. srcs: ["a.java"],
  58. compile_dex: true,
  59. hiddenapi_additional_annotations: [
  60. "foo-hiddenapi-annotations",
  61. ],
  62. }
  63. java_library {
  64. name: "foo-hiddenapi-annotations",
  65. srcs: ["a.java"],
  66. compile_dex: true,
  67. }
  68. java_import {
  69. name: "foo",
  70. jars: ["a.jar"],
  71. compile_dex: true,
  72. prefer: false,
  73. }
  74. java_sdk_library {
  75. name: "bar",
  76. srcs: ["a.java"],
  77. compile_dex: true,
  78. }
  79. `)
  80. hiddenAPIIndex := result.SingletonForTests("hiddenapi_index")
  81. indexRule := hiddenAPIIndex.Rule("singleton-merged-hiddenapi-index")
  82. CheckHiddenAPIRuleInputs(t, `
  83. .intermediates/bar/android_common/hiddenapi/index.csv
  84. .intermediates/foo/android_common/hiddenapi/index.csv
  85. `,
  86. indexRule)
  87. // Make sure that the foo-hiddenapi-annotations.jar is included in the inputs to the rules that
  88. // creates the index.csv file.
  89. foo := result.ModuleForTests("foo", "android_common")
  90. indexParams := foo.Output("hiddenapi/index.csv")
  91. CheckHiddenAPIRuleInputs(t, `
  92. .intermediates/foo-hiddenapi-annotations/android_common/javac/foo-hiddenapi-annotations.jar
  93. .intermediates/foo/android_common/javac/foo.jar
  94. `, indexParams)
  95. }
  96. func TestHiddenAPISingletonWithSourceAndPrebuiltPreferredButNoDex(t *testing.T) {
  97. expectedErrorMessage :=
  98. "hiddenapi has determined that the source module \"foo\" should be ignored as it has been" +
  99. " replaced by the prebuilt module \"prebuilt_foo\" but unfortunately it does not provide a" +
  100. " suitable boot dex jar"
  101. hiddenApiFixtureFactory.Extend(
  102. fixtureSetBootJarsProductVariable("platform:foo"),
  103. ).ExtendWithErrorHandler(android.FixtureExpectsAtLeastOneErrorMatchingPattern(expectedErrorMessage)).
  104. RunTestWithBp(t, `
  105. java_library {
  106. name: "foo",
  107. srcs: ["a.java"],
  108. compile_dex: true,
  109. }
  110. java_import {
  111. name: "foo",
  112. jars: ["a.jar"],
  113. prefer: true,
  114. }
  115. `)
  116. }
  117. func TestHiddenAPISingletonWithPrebuilt(t *testing.T) {
  118. result := hiddenApiFixtureFactory.Extend(
  119. fixtureSetBootJarsProductVariable("platform:foo"),
  120. ).RunTestWithBp(t, `
  121. java_import {
  122. name: "foo",
  123. jars: ["a.jar"],
  124. compile_dex: true,
  125. }
  126. `)
  127. hiddenAPI := result.SingletonForTests("hiddenapi")
  128. hiddenapiRule := hiddenAPI.Rule("hiddenapi")
  129. want := "--boot-dex=out/soong/.intermediates/foo/android_common/aligned/foo.jar"
  130. android.AssertStringDoesContain(t, "hiddenapi command", hiddenapiRule.RuleParams.Command, want)
  131. }
  132. func TestHiddenAPISingletonWithPrebuiltUseSource(t *testing.T) {
  133. result := hiddenApiFixtureFactory.Extend(
  134. fixtureSetBootJarsProductVariable("platform:foo"),
  135. ).RunTestWithBp(t, `
  136. java_library {
  137. name: "foo",
  138. srcs: ["a.java"],
  139. compile_dex: true,
  140. }
  141. java_import {
  142. name: "foo",
  143. jars: ["a.jar"],
  144. compile_dex: true,
  145. prefer: false,
  146. }
  147. `)
  148. hiddenAPI := result.SingletonForTests("hiddenapi")
  149. hiddenapiRule := hiddenAPI.Rule("hiddenapi")
  150. fromSourceJarArg := "--boot-dex=out/soong/.intermediates/foo/android_common/aligned/foo.jar"
  151. android.AssertStringDoesContain(t, "hiddenapi command", hiddenapiRule.RuleParams.Command, fromSourceJarArg)
  152. prebuiltJarArg := "--boot-dex=out/soong/.intermediates/foo/android_common/dex/foo.jar"
  153. android.AssertStringDoesNotContain(t, "hiddenapi command", hiddenapiRule.RuleParams.Command, prebuiltJarArg)
  154. }
  155. func TestHiddenAPISingletonWithPrebuiltOverrideSource(t *testing.T) {
  156. result := hiddenApiFixtureFactory.Extend(
  157. fixtureSetBootJarsProductVariable("platform:foo"),
  158. ).RunTestWithBp(t, `
  159. java_library {
  160. name: "foo",
  161. srcs: ["a.java"],
  162. compile_dex: true,
  163. }
  164. java_import {
  165. name: "foo",
  166. jars: ["a.jar"],
  167. compile_dex: true,
  168. prefer: true,
  169. }
  170. `)
  171. hiddenAPI := result.SingletonForTests("hiddenapi")
  172. hiddenapiRule := hiddenAPI.Rule("hiddenapi")
  173. prebuiltJarArg := "--boot-dex=out/soong/.intermediates/prebuilt_foo/android_common/dex/foo.jar"
  174. android.AssertStringDoesContain(t, "hiddenapi command", hiddenapiRule.RuleParams.Command, prebuiltJarArg)
  175. fromSourceJarArg := "--boot-dex=out/soong/.intermediates/foo/android_common/aligned/foo.jar"
  176. android.AssertStringDoesNotContain(t, "hiddenapi command", hiddenapiRule.RuleParams.Command, fromSourceJarArg)
  177. }
  178. func TestHiddenAPISingletonSdks(t *testing.T) {
  179. testCases := []struct {
  180. name string
  181. unbundledBuild bool
  182. publicStub string
  183. systemStub string
  184. testStub string
  185. corePlatformStub string
  186. // Additional test preparer
  187. preparer android.FixturePreparer
  188. }{
  189. {
  190. name: "testBundled",
  191. unbundledBuild: false,
  192. publicStub: "android_stubs_current",
  193. systemStub: "android_system_stubs_current",
  194. testStub: "android_test_stubs_current",
  195. corePlatformStub: "legacy.core.platform.api.stubs",
  196. preparer: android.GroupFixturePreparers(),
  197. }, {
  198. name: "testUnbundled",
  199. unbundledBuild: true,
  200. publicStub: "sdk_public_current_android",
  201. systemStub: "sdk_system_current_android",
  202. testStub: "sdk_test_current_android",
  203. corePlatformStub: "legacy.core.platform.api.stubs",
  204. preparer: PrepareForTestWithPrebuiltsOfCurrentApi,
  205. },
  206. }
  207. for _, tc := range testCases {
  208. t.Run(tc.name, func(t *testing.T) {
  209. result := hiddenApiFixtureFactory.Extend(
  210. tc.preparer,
  211. android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
  212. variables.Always_use_prebuilt_sdks = proptools.BoolPtr(tc.unbundledBuild)
  213. }),
  214. ).RunTest(t)
  215. hiddenAPI := result.SingletonForTests("hiddenapi")
  216. hiddenapiRule := hiddenAPI.Rule("hiddenapi")
  217. wantPublicStubs := "--public-stub-classpath=" + generateSdkDexPath(tc.publicStub, tc.unbundledBuild)
  218. android.AssertStringDoesContain(t, "hiddenapi command", hiddenapiRule.RuleParams.Command, wantPublicStubs)
  219. wantSystemStubs := "--system-stub-classpath=" + generateSdkDexPath(tc.systemStub, tc.unbundledBuild)
  220. android.AssertStringDoesContain(t, "hiddenapi command", hiddenapiRule.RuleParams.Command, wantSystemStubs)
  221. wantTestStubs := "--test-stub-classpath=" + generateSdkDexPath(tc.testStub, tc.unbundledBuild)
  222. android.AssertStringDoesContain(t, "hiddenapi command", hiddenapiRule.RuleParams.Command, wantTestStubs)
  223. wantCorePlatformStubs := "--core-platform-stub-classpath=" + generateDexPath(defaultJavaDir, tc.corePlatformStub)
  224. android.AssertStringDoesContain(t, "hiddenapi command", hiddenapiRule.RuleParams.Command, wantCorePlatformStubs)
  225. })
  226. }
  227. }
  228. func generateDexedPath(subDir, dex, module string) string {
  229. return fmt.Sprintf("out/soong/.intermediates/%s/android_common/%s/%s.jar", subDir, dex, module)
  230. }
  231. func generateDexPath(moduleDir string, module string) string {
  232. return generateDexedPath(filepath.Join(moduleDir, module), "dex", module)
  233. }
  234. func generateSdkDexPath(module string, unbundled bool) string {
  235. if unbundled {
  236. return generateDexedPath("prebuilts/sdk/"+module, "dex", module)
  237. }
  238. return generateDexPath(defaultJavaDir, module)
  239. }
  240. func TestHiddenAPISingletonWithPrebuiltCsvFile(t *testing.T) {
  241. // The idea behind this test is to ensure that when the build is
  242. // confugured with a PrebuiltHiddenApiDir that the rules for the
  243. // hiddenapi singleton copy the prebuilts to the typical output
  244. // location, and then use that output location for the hiddenapi encode
  245. // dex step.
  246. // Where to find the prebuilt hiddenapi files:
  247. prebuiltHiddenApiDir := "path/to/prebuilt/hiddenapi"
  248. result := hiddenApiFixtureFactory.Extend(
  249. fixtureSetBootJarsProductVariable("platform:foo"),
  250. fixtureSetPrebuiltHiddenApiDirProductVariable(&prebuiltHiddenApiDir),
  251. ).RunTestWithBp(t, `
  252. java_import {
  253. name: "foo",
  254. jars: ["a.jar"],
  255. compile_dex: true,
  256. }
  257. `)
  258. expectedCpInput := prebuiltHiddenApiDir + "/hiddenapi-flags.csv"
  259. expectedCpOutput := "out/soong/hiddenapi/hiddenapi-flags.csv"
  260. expectedFlagsCsv := "out/soong/hiddenapi/hiddenapi-flags.csv"
  261. foo := result.ModuleForTests("foo", "android_common")
  262. hiddenAPI := result.SingletonForTests("hiddenapi")
  263. cpRule := hiddenAPI.Rule("Cp")
  264. actualCpInput := cpRule.BuildParams.Input
  265. actualCpOutput := cpRule.BuildParams.Output
  266. encodeDexRule := foo.Rule("hiddenAPIEncodeDex")
  267. actualFlagsCsv := encodeDexRule.BuildParams.Args["flagsCsv"]
  268. android.AssertPathRelativeToTopEquals(t, "hiddenapi cp rule input", expectedCpInput, actualCpInput)
  269. android.AssertPathRelativeToTopEquals(t, "hiddenapi cp rule output", expectedCpOutput, actualCpOutput)
  270. android.AssertStringEquals(t, "hiddenapi encode dex rule flags csv", expectedFlagsCsv, actualFlagsCsv)
  271. }