sysprop_test.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  1. // Copyright (C) 2019 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. package sysprop
  15. import (
  16. "reflect"
  17. "android/soong/android"
  18. "android/soong/cc"
  19. "android/soong/java"
  20. "io/ioutil"
  21. "os"
  22. "strings"
  23. "testing"
  24. "github.com/google/blueprint"
  25. "github.com/google/blueprint/proptools"
  26. )
  27. var buildDir string
  28. func setUp() {
  29. var err error
  30. buildDir, err = ioutil.TempDir("", "soong_sysprop_test")
  31. if err != nil {
  32. panic(err)
  33. }
  34. }
  35. func tearDown() {
  36. os.RemoveAll(buildDir)
  37. }
  38. func TestMain(m *testing.M) {
  39. run := func() int {
  40. setUp()
  41. defer tearDown()
  42. return m.Run()
  43. }
  44. os.Exit(run())
  45. }
  46. func testContext(config android.Config) *android.TestContext {
  47. ctx := android.NewTestArchContext()
  48. java.RegisterJavaBuildComponents(ctx)
  49. java.RegisterAppBuildComponents(ctx)
  50. java.RegisterSystemModulesBuildComponents(ctx)
  51. ctx.PreArchMutators(android.RegisterDefaultsPreArchMutators)
  52. ctx.PreArchMutators(func(ctx android.RegisterMutatorsContext) {
  53. ctx.BottomUp("sysprop_deps", syspropDepsMutator).Parallel()
  54. })
  55. android.RegisterPrebuiltMutators(ctx)
  56. cc.RegisterRequiredBuildComponentsForTest(ctx)
  57. ctx.PreDepsMutators(func(ctx android.RegisterMutatorsContext) {
  58. ctx.BottomUp("sysprop_java", java.SyspropMutator).Parallel()
  59. })
  60. ctx.RegisterModuleType("sysprop_library", syspropLibraryFactory)
  61. ctx.Register(config)
  62. return ctx
  63. }
  64. func run(t *testing.T, ctx *android.TestContext, config android.Config) {
  65. t.Helper()
  66. _, errs := ctx.ParseFileList(".", []string{"Android.bp"})
  67. android.FailIfErrored(t, errs)
  68. _, errs = ctx.PrepareBuildActions(config)
  69. android.FailIfErrored(t, errs)
  70. }
  71. func testConfig(env map[string]string, bp string, fs map[string][]byte) android.Config {
  72. bp += cc.GatherRequiredDepsForTest(android.Android)
  73. mockFS := map[string][]byte{
  74. "a.java": nil,
  75. "b.java": nil,
  76. "c.java": nil,
  77. "d.cpp": nil,
  78. "api/sysprop-platform-current.txt": nil,
  79. "api/sysprop-platform-latest.txt": nil,
  80. "api/sysprop-platform-on-product-current.txt": nil,
  81. "api/sysprop-platform-on-product-latest.txt": nil,
  82. "api/sysprop-vendor-current.txt": nil,
  83. "api/sysprop-vendor-latest.txt": nil,
  84. "api/sysprop-odm-current.txt": nil,
  85. "api/sysprop-odm-latest.txt": nil,
  86. "framework/aidl/a.aidl": nil,
  87. // For framework-res, which is an implicit dependency for framework
  88. "AndroidManifest.xml": nil,
  89. "build/make/target/product/security/testkey": nil,
  90. "build/soong/scripts/jar-wrapper.sh": nil,
  91. "build/make/core/proguard.flags": nil,
  92. "build/make/core/proguard_basic_keeps.flags": nil,
  93. "jdk8/jre/lib/jce.jar": nil,
  94. "jdk8/jre/lib/rt.jar": nil,
  95. "jdk8/lib/tools.jar": nil,
  96. "bar-doc/a.java": nil,
  97. "bar-doc/b.java": nil,
  98. "bar-doc/IFoo.aidl": nil,
  99. "bar-doc/known_oj_tags.txt": nil,
  100. "external/doclava/templates-sdk": nil,
  101. "cert/new_cert.x509.pem": nil,
  102. "cert/new_cert.pk8": nil,
  103. "android/sysprop/PlatformProperties.sysprop": nil,
  104. "com/android/VendorProperties.sysprop": nil,
  105. "com/android2/OdmProperties.sysprop": nil,
  106. }
  107. for k, v := range fs {
  108. mockFS[k] = v
  109. }
  110. config := java.TestConfig(buildDir, env, bp, mockFS)
  111. config.TestProductVariables.DeviceSystemSdkVersions = []string{"28"}
  112. config.TestProductVariables.DeviceVndkVersion = proptools.StringPtr("current")
  113. config.TestProductVariables.Platform_vndk_version = proptools.StringPtr("VER")
  114. return config
  115. }
  116. func test(t *testing.T, bp string) *android.TestContext {
  117. t.Helper()
  118. config := testConfig(nil, bp, nil)
  119. ctx := testContext(config)
  120. run(t, ctx, config)
  121. return ctx
  122. }
  123. func TestSyspropLibrary(t *testing.T) {
  124. ctx := test(t, `
  125. sysprop_library {
  126. name: "sysprop-platform",
  127. apex_available: ["//apex_available:platform"],
  128. srcs: ["android/sysprop/PlatformProperties.sysprop"],
  129. api_packages: ["android.sysprop"],
  130. property_owner: "Platform",
  131. vendor_available: true,
  132. host_supported: true,
  133. }
  134. sysprop_library {
  135. name: "sysprop-platform-on-product",
  136. srcs: ["android/sysprop/PlatformProperties.sysprop"],
  137. api_packages: ["android.sysprop"],
  138. property_owner: "Platform",
  139. product_specific: true,
  140. }
  141. sysprop_library {
  142. name: "sysprop-vendor",
  143. srcs: ["com/android/VendorProperties.sysprop"],
  144. api_packages: ["com.android"],
  145. property_owner: "Vendor",
  146. product_specific: true,
  147. vendor_available: true,
  148. }
  149. sysprop_library {
  150. name: "sysprop-odm",
  151. srcs: ["com/android2/OdmProperties.sysprop"],
  152. api_packages: ["com.android2"],
  153. property_owner: "Odm",
  154. device_specific: true,
  155. }
  156. java_library {
  157. name: "java-platform",
  158. srcs: ["c.java"],
  159. sdk_version: "system_current",
  160. libs: ["sysprop-platform"],
  161. }
  162. java_library {
  163. name: "java-platform-private",
  164. srcs: ["c.java"],
  165. platform_apis: true,
  166. libs: ["sysprop-platform"],
  167. }
  168. java_library {
  169. name: "java-product",
  170. srcs: ["c.java"],
  171. sdk_version: "system_current",
  172. product_specific: true,
  173. libs: ["sysprop-platform", "sysprop-vendor"],
  174. }
  175. java_library {
  176. name: "java-vendor",
  177. srcs: ["c.java"],
  178. sdk_version: "system_current",
  179. soc_specific: true,
  180. libs: ["sysprop-platform", "sysprop-vendor"],
  181. }
  182. cc_library {
  183. name: "cc-client-platform",
  184. srcs: ["d.cpp"],
  185. static_libs: ["sysprop-platform"],
  186. }
  187. cc_library_static {
  188. name: "cc-client-platform-static",
  189. srcs: ["d.cpp"],
  190. whole_static_libs: ["sysprop-platform"],
  191. }
  192. cc_library {
  193. name: "cc-client-product",
  194. srcs: ["d.cpp"],
  195. product_specific: true,
  196. static_libs: ["sysprop-platform-on-product", "sysprop-vendor"],
  197. }
  198. cc_library {
  199. name: "cc-client-vendor",
  200. srcs: ["d.cpp"],
  201. soc_specific: true,
  202. static_libs: ["sysprop-platform", "sysprop-vendor"],
  203. }
  204. cc_library {
  205. name: "libbase",
  206. host_supported: true,
  207. }
  208. cc_library_headers {
  209. name: "libbase_headers",
  210. vendor_available: true,
  211. recovery_available: true,
  212. }
  213. cc_library {
  214. name: "liblog",
  215. no_libcrt: true,
  216. nocrt: true,
  217. system_shared_libs: [],
  218. recovery_available: true,
  219. host_supported: true,
  220. }
  221. cc_binary_host {
  222. name: "hostbin",
  223. static_libs: ["sysprop-platform"],
  224. }
  225. llndk_library {
  226. name: "liblog",
  227. symbol_file: "",
  228. }
  229. java_library {
  230. name: "sysprop-library-stub-platform",
  231. sdk_version: "core_current",
  232. }
  233. java_library {
  234. name: "sysprop-library-stub-vendor",
  235. soc_specific: true,
  236. sdk_version: "core_current",
  237. }
  238. `)
  239. // Check for generated cc_library
  240. for _, variant := range []string{
  241. "android_vendor.VER_arm_armv7-a-neon_shared",
  242. "android_vendor.VER_arm_armv7-a-neon_static",
  243. "android_vendor.VER_arm64_armv8-a_shared",
  244. "android_vendor.VER_arm64_armv8-a_static",
  245. } {
  246. ctx.ModuleForTests("libsysprop-platform", variant)
  247. ctx.ModuleForTests("libsysprop-vendor", variant)
  248. ctx.ModuleForTests("libsysprop-odm", variant)
  249. }
  250. for _, variant := range []string{
  251. "android_arm_armv7-a-neon_shared",
  252. "android_arm_armv7-a-neon_static",
  253. "android_arm64_armv8-a_shared",
  254. "android_arm64_armv8-a_static",
  255. } {
  256. library := ctx.ModuleForTests("libsysprop-platform", variant).Module().(*cc.Module)
  257. expectedApexAvailableOnLibrary := []string{"//apex_available:platform"}
  258. if !reflect.DeepEqual(library.ApexProperties.Apex_available, expectedApexAvailableOnLibrary) {
  259. t.Errorf("apex available property on libsysprop-platform must be %#v, but was %#v.",
  260. expectedApexAvailableOnLibrary, library.ApexProperties.Apex_available)
  261. }
  262. // core variant of vendor-owned sysprop_library is for product
  263. ctx.ModuleForTests("libsysprop-vendor", variant)
  264. }
  265. ctx.ModuleForTests("sysprop-platform", "android_common")
  266. ctx.ModuleForTests("sysprop-platform_public", "android_common")
  267. ctx.ModuleForTests("sysprop-vendor", "android_common")
  268. // Check for exported includes
  269. coreVariant := "android_arm64_armv8-a_static"
  270. vendorVariant := "android_vendor.VER_arm64_armv8-a_static"
  271. platformInternalPath := "libsysprop-platform/android_arm64_armv8-a_static/gen/sysprop/include"
  272. platformPublicCorePath := "libsysprop-platform/android_arm64_armv8-a_static/gen/sysprop/public/include"
  273. platformPublicVendorPath := "libsysprop-platform/android_vendor.VER_arm64_armv8-a_static/gen/sysprop/public/include"
  274. platformOnProductPath := "libsysprop-platform-on-product/android_arm64_armv8-a_static/gen/sysprop/public/include"
  275. vendorInternalPath := "libsysprop-vendor/android_vendor.VER_arm64_armv8-a_static/gen/sysprop/include"
  276. vendorPublicPath := "libsysprop-vendor/android_arm64_armv8-a_static/gen/sysprop/public/include"
  277. platformClient := ctx.ModuleForTests("cc-client-platform", coreVariant)
  278. platformFlags := platformClient.Rule("cc").Args["cFlags"]
  279. // platform should use platform's internal header
  280. if !strings.Contains(platformFlags, platformInternalPath) {
  281. t.Errorf("flags for platform must contain %#v, but was %#v.",
  282. platformInternalPath, platformFlags)
  283. }
  284. platformStaticClient := ctx.ModuleForTests("cc-client-platform-static", coreVariant)
  285. platformStaticFlags := platformStaticClient.Rule("cc").Args["cFlags"]
  286. // platform-static should use platform's internal header
  287. if !strings.Contains(platformStaticFlags, platformInternalPath) {
  288. t.Errorf("flags for platform-static must contain %#v, but was %#v.",
  289. platformInternalPath, platformStaticFlags)
  290. }
  291. productClient := ctx.ModuleForTests("cc-client-product", coreVariant)
  292. productFlags := productClient.Rule("cc").Args["cFlags"]
  293. // Product should use platform's and vendor's public headers
  294. if !strings.Contains(productFlags, platformOnProductPath) ||
  295. !strings.Contains(productFlags, vendorPublicPath) {
  296. t.Errorf("flags for product must contain %#v and %#v, but was %#v.",
  297. platformPublicCorePath, vendorPublicPath, productFlags)
  298. }
  299. vendorClient := ctx.ModuleForTests("cc-client-vendor", vendorVariant)
  300. vendorFlags := vendorClient.Rule("cc").Args["cFlags"]
  301. // Vendor should use platform's public header and vendor's internal header
  302. if !strings.Contains(vendorFlags, platformPublicVendorPath) ||
  303. !strings.Contains(vendorFlags, vendorInternalPath) {
  304. t.Errorf("flags for vendor must contain %#v and %#v, but was %#v.",
  305. platformPublicVendorPath, vendorInternalPath, vendorFlags)
  306. }
  307. // Java modules linking against system API should use public stub
  308. javaSystemApiClient := ctx.ModuleForTests("java-platform", "android_common")
  309. publicStubFound := false
  310. ctx.VisitDirectDeps(javaSystemApiClient.Module(), func(dep blueprint.Module) {
  311. if dep.Name() == "sysprop-platform_public" {
  312. publicStubFound = true
  313. }
  314. })
  315. if !publicStubFound {
  316. t.Errorf("system api client should use public stub")
  317. }
  318. }