sysprop_test.go 12 KB

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