blob: fde41d67e32fd3a9f88408ffd3ccaaa453202dcc [file] [log] [blame]
Inseob Kimc0907f12019-02-08 21:00:45 +09001// 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
15package sysprop
16
17import (
Paul Duffin7b3de8f2020-03-30 18:00:25 +010018 "reflect"
19
Inseob Kimc0907f12019-02-08 21:00:45 +090020 "android/soong/android"
21 "android/soong/cc"
22 "android/soong/java"
23
Inseob Kimc0907f12019-02-08 21:00:45 +090024 "io/ioutil"
25 "os"
26 "strings"
27 "testing"
28
29 "github.com/google/blueprint/proptools"
30)
31
32var buildDir string
33
34func setUp() {
35 var err error
36 buildDir, err = ioutil.TempDir("", "soong_sysprop_test")
37 if err != nil {
38 panic(err)
39 }
40}
41
42func tearDown() {
43 os.RemoveAll(buildDir)
44}
45
46func TestMain(m *testing.M) {
47 run := func() int {
48 setUp()
49 defer tearDown()
50
51 return m.Run()
52 }
53
54 os.Exit(run())
55}
56
Colin Cross98be1bb2019-12-13 20:41:13 -080057func testContext(config android.Config) *android.TestContext {
Inseob Kimc0907f12019-02-08 21:00:45 +090058
Colin Crossae8600b2020-10-29 17:09:13 -070059 ctx := android.NewTestArchContext(config)
Paul Duffinc059c8c2021-01-20 17:13:52 +000060 java.RegisterRequiredBuildComponentsForTest(ctx)
Paul Duffin43dc1cc2019-12-19 11:18:54 +000061
Inseob Kimc0907f12019-02-08 21:00:45 +090062 ctx.PreArchMutators(android.RegisterDefaultsPreArchMutators)
Inseob Kimc0907f12019-02-08 21:00:45 +090063
Paul Duffin021f4e52020-07-30 16:04:17 +010064 android.RegisterPrebuiltMutators(ctx)
65
Paul Duffin77980a82019-12-19 16:01:36 +000066 cc.RegisterRequiredBuildComponentsForTest(ctx)
Inseob Kimc0907f12019-02-08 21:00:45 +090067
Colin Cross4b49b762019-11-22 15:25:03 -080068 ctx.RegisterModuleType("sysprop_library", syspropLibraryFactory)
Inseob Kimc0907f12019-02-08 21:00:45 +090069
Colin Crossae8600b2020-10-29 17:09:13 -070070 ctx.Register()
Inseob Kimc0907f12019-02-08 21:00:45 +090071
Colin Cross98be1bb2019-12-13 20:41:13 -080072 return ctx
73}
74
75func run(t *testing.T, ctx *android.TestContext, config android.Config) {
76 t.Helper()
77 _, errs := ctx.ParseFileList(".", []string{"Android.bp"})
78 android.FailIfErrored(t, errs)
79 _, errs = ctx.PrepareBuildActions(config)
80 android.FailIfErrored(t, errs)
81}
82
83func testConfig(env map[string]string, bp string, fs map[string][]byte) android.Config {
Jiyong Park5e914b22021-03-08 10:09:52 +090084 bp += `
85 cc_library {
86 name: "libbase",
87 host_supported: true,
88 }
89
90 cc_library_headers {
91 name: "libbase_headers",
92 vendor_available: true,
93 recovery_available: true,
94 }
95
96 cc_library {
97 name: "liblog",
98 no_libcrt: true,
99 nocrt: true,
100 system_shared_libs: [],
101 recovery_available: true,
102 host_supported: true,
103 llndk_stubs: "liblog.llndk",
104 }
105
106 llndk_library {
107 name: "liblog.llndk",
108 symbol_file: "",
109 }
110
111 java_library {
112 name: "sysprop-library-stub-platform",
113 sdk_version: "core_current",
114 }
115
116 java_library {
117 name: "sysprop-library-stub-vendor",
118 soc_specific: true,
119 sdk_version: "core_current",
120 }
121
122 java_library {
123 name: "sysprop-library-stub-product",
124 product_specific: true,
125 sdk_version: "core_current",
126 }
127 `
128
Inseob Kimc0907f12019-02-08 21:00:45 +0900129 bp += cc.GatherRequiredDepsForTest(android.Android)
130
131 mockFS := map[string][]byte{
Inseob Kim42882742019-07-30 17:55:33 +0900132 "a.java": nil,
133 "b.java": nil,
134 "c.java": nil,
135 "d.cpp": nil,
136 "api/sysprop-platform-current.txt": nil,
137 "api/sysprop-platform-latest.txt": nil,
138 "api/sysprop-platform-on-product-current.txt": nil,
139 "api/sysprop-platform-on-product-latest.txt": nil,
140 "api/sysprop-vendor-current.txt": nil,
141 "api/sysprop-vendor-latest.txt": nil,
Inseob Kimfe612182020-10-20 16:29:55 +0900142 "api/sysprop-vendor-on-product-current.txt": nil,
143 "api/sysprop-vendor-on-product-latest.txt": nil,
Inseob Kim42882742019-07-30 17:55:33 +0900144 "api/sysprop-odm-current.txt": nil,
145 "api/sysprop-odm-latest.txt": nil,
146 "framework/aidl/a.aidl": nil,
Inseob Kimc0907f12019-02-08 21:00:45 +0900147
148 // For framework-res, which is an implicit dependency for framework
Dan Willemsen412160e2019-04-09 21:36:26 -0700149 "AndroidManifest.xml": nil,
150 "build/make/target/product/security/testkey": nil,
Inseob Kimc0907f12019-02-08 21:00:45 +0900151
152 "build/soong/scripts/jar-wrapper.sh": nil,
153
154 "build/make/core/proguard.flags": nil,
155 "build/make/core/proguard_basic_keeps.flags": nil,
156
157 "jdk8/jre/lib/jce.jar": nil,
158 "jdk8/jre/lib/rt.jar": nil,
159 "jdk8/lib/tools.jar": nil,
160
161 "bar-doc/a.java": nil,
162 "bar-doc/b.java": nil,
163 "bar-doc/IFoo.aidl": nil,
164 "bar-doc/known_oj_tags.txt": nil,
165 "external/doclava/templates-sdk": nil,
166
167 "cert/new_cert.x509.pem": nil,
168 "cert/new_cert.pk8": nil,
169
170 "android/sysprop/PlatformProperties.sysprop": nil,
171 "com/android/VendorProperties.sysprop": nil,
Inseob Kim42882742019-07-30 17:55:33 +0900172 "com/android2/OdmProperties.sysprop": nil,
Inseob Kimc0907f12019-02-08 21:00:45 +0900173 }
174
175 for k, v := range fs {
176 mockFS[k] = v
177 }
178
Colin Cross98be1bb2019-12-13 20:41:13 -0800179 config := java.TestConfig(buildDir, env, bp, mockFS)
Colin Crosse4759b92019-02-15 10:37:39 -0800180
Inseob Kimc0907f12019-02-08 21:00:45 +0900181 config.TestProductVariables.DeviceSystemSdkVersions = []string{"28"}
182 config.TestProductVariables.DeviceVndkVersion = proptools.StringPtr("current")
183 config.TestProductVariables.Platform_vndk_version = proptools.StringPtr("VER")
Colin Crosse4759b92019-02-15 10:37:39 -0800184
Inseob Kimc0907f12019-02-08 21:00:45 +0900185 return config
186
187}
188
189func test(t *testing.T, bp string) *android.TestContext {
190 t.Helper()
Colin Cross98be1bb2019-12-13 20:41:13 -0800191 config := testConfig(nil, bp, nil)
192 ctx := testContext(config)
Inseob Kimc0907f12019-02-08 21:00:45 +0900193 run(t, ctx, config)
194
195 return ctx
196}
197
198func TestSyspropLibrary(t *testing.T) {
199 ctx := test(t, `
200 sysprop_library {
201 name: "sysprop-platform",
Paul Duffin7b3de8f2020-03-30 18:00:25 +0100202 apex_available: ["//apex_available:platform"],
Inseob Kimc0907f12019-02-08 21:00:45 +0900203 srcs: ["android/sysprop/PlatformProperties.sysprop"],
204 api_packages: ["android.sysprop"],
205 property_owner: "Platform",
206 vendor_available: true,
Inseob Kim89db15d2020-02-03 18:06:46 +0900207 host_supported: true,
Inseob Kimc0907f12019-02-08 21:00:45 +0900208 }
209
210 sysprop_library {
211 name: "sysprop-platform-on-product",
212 srcs: ["android/sysprop/PlatformProperties.sysprop"],
213 api_packages: ["android.sysprop"],
214 property_owner: "Platform",
215 product_specific: true,
216 }
217
218 sysprop_library {
219 name: "sysprop-vendor",
220 srcs: ["com/android/VendorProperties.sysprop"],
221 api_packages: ["com.android"],
222 property_owner: "Vendor",
Inseob Kimfe612182020-10-20 16:29:55 +0900223 vendor: true,
224 }
225
226 sysprop_library {
227 name: "sysprop-vendor-on-product",
228 srcs: ["com/android/VendorProperties.sysprop"],
229 api_packages: ["com.android"],
230 property_owner: "Vendor",
Inseob Kimc0907f12019-02-08 21:00:45 +0900231 product_specific: true,
Inseob Kimc0907f12019-02-08 21:00:45 +0900232 }
233
Inseob Kim42882742019-07-30 17:55:33 +0900234 sysprop_library {
235 name: "sysprop-odm",
236 srcs: ["com/android2/OdmProperties.sysprop"],
237 api_packages: ["com.android2"],
238 property_owner: "Odm",
239 device_specific: true,
240 }
241
Inseob Kimc0907f12019-02-08 21:00:45 +0900242 java_library {
243 name: "java-platform",
244 srcs: ["c.java"],
245 sdk_version: "system_current",
246 libs: ["sysprop-platform"],
247 }
248
249 java_library {
Inseob Kimac1e9862019-12-09 18:15:47 +0900250 name: "java-platform-private",
251 srcs: ["c.java"],
252 platform_apis: true,
253 libs: ["sysprop-platform"],
254 }
255
256 java_library {
Inseob Kimc0907f12019-02-08 21:00:45 +0900257 name: "java-product",
258 srcs: ["c.java"],
259 sdk_version: "system_current",
260 product_specific: true,
Inseob Kimfe612182020-10-20 16:29:55 +0900261 libs: ["sysprop-platform", "sysprop-vendor-on-product"],
Inseob Kimc0907f12019-02-08 21:00:45 +0900262 }
263
264 java_library {
265 name: "java-vendor",
266 srcs: ["c.java"],
267 sdk_version: "system_current",
268 soc_specific: true,
269 libs: ["sysprop-platform", "sysprop-vendor"],
270 }
271
272 cc_library {
273 name: "cc-client-platform",
274 srcs: ["d.cpp"],
275 static_libs: ["sysprop-platform"],
276 }
277
Jiyong Park5d1598f2019-02-25 22:14:17 +0900278 cc_library_static {
279 name: "cc-client-platform-static",
280 srcs: ["d.cpp"],
281 whole_static_libs: ["sysprop-platform"],
282 }
283
Inseob Kimc0907f12019-02-08 21:00:45 +0900284 cc_library {
285 name: "cc-client-product",
286 srcs: ["d.cpp"],
287 product_specific: true,
Inseob Kimfe612182020-10-20 16:29:55 +0900288 static_libs: ["sysprop-platform-on-product", "sysprop-vendor-on-product"],
Inseob Kimc0907f12019-02-08 21:00:45 +0900289 }
290
291 cc_library {
292 name: "cc-client-vendor",
293 srcs: ["d.cpp"],
294 soc_specific: true,
295 static_libs: ["sysprop-platform", "sysprop-vendor"],
296 }
Inseob Kim1f959762019-03-27 17:20:37 +0900297
Inseob Kim89db15d2020-02-03 18:06:46 +0900298 cc_binary_host {
299 name: "hostbin",
300 static_libs: ["sysprop-platform"],
Inseob Kim1f959762019-03-27 17:20:37 +0900301 }
Jiyong Park5e914b22021-03-08 10:09:52 +0900302 `)
Inseob Kimc0907f12019-02-08 21:00:45 +0900303
Inseob Kim42882742019-07-30 17:55:33 +0900304 // Check for generated cc_library
305 for _, variant := range []string{
Colin Crossfb0c16e2019-11-20 17:12:35 -0800306 "android_vendor.VER_arm_armv7-a-neon_shared",
307 "android_vendor.VER_arm_armv7-a-neon_static",
308 "android_vendor.VER_arm64_armv8-a_shared",
309 "android_vendor.VER_arm64_armv8-a_static",
Inseob Kim42882742019-07-30 17:55:33 +0900310 } {
311 ctx.ModuleForTests("libsysprop-platform", variant)
312 ctx.ModuleForTests("libsysprop-vendor", variant)
313 ctx.ModuleForTests("libsysprop-odm", variant)
314 }
315
Inseob Kimc0907f12019-02-08 21:00:45 +0900316 for _, variant := range []string{
Colin Cross7113d202019-11-20 16:39:12 -0800317 "android_arm_armv7-a-neon_shared",
318 "android_arm_armv7-a-neon_static",
319 "android_arm64_armv8-a_shared",
320 "android_arm64_armv8-a_static",
Inseob Kimc0907f12019-02-08 21:00:45 +0900321 } {
Paul Duffin7b3de8f2020-03-30 18:00:25 +0100322 library := ctx.ModuleForTests("libsysprop-platform", variant).Module().(*cc.Module)
323 expectedApexAvailableOnLibrary := []string{"//apex_available:platform"}
324 if !reflect.DeepEqual(library.ApexProperties.Apex_available, expectedApexAvailableOnLibrary) {
325 t.Errorf("apex available property on libsysprop-platform must be %#v, but was %#v.",
326 expectedApexAvailableOnLibrary, library.ApexProperties.Apex_available)
327 }
Inseob Kim42882742019-07-30 17:55:33 +0900328
Inseob Kimfe612182020-10-20 16:29:55 +0900329 // product variant of vendor-owned sysprop_library
330 ctx.ModuleForTests("libsysprop-vendor-on-product", variant)
Inseob Kimc0907f12019-02-08 21:00:45 +0900331 }
332
333 ctx.ModuleForTests("sysprop-platform", "android_common")
Inseob Kimac1e9862019-12-09 18:15:47 +0900334 ctx.ModuleForTests("sysprop-platform_public", "android_common")
Inseob Kimc0907f12019-02-08 21:00:45 +0900335 ctx.ModuleForTests("sysprop-vendor", "android_common")
Inseob Kimfe612182020-10-20 16:29:55 +0900336 ctx.ModuleForTests("sysprop-vendor-on-product", "android_common")
Inseob Kimc0907f12019-02-08 21:00:45 +0900337
338 // Check for exported includes
Colin Cross7113d202019-11-20 16:39:12 -0800339 coreVariant := "android_arm64_armv8-a_static"
Colin Crossfb0c16e2019-11-20 17:12:35 -0800340 vendorVariant := "android_vendor.VER_arm64_armv8-a_static"
Inseob Kimc0907f12019-02-08 21:00:45 +0900341
Colin Cross7113d202019-11-20 16:39:12 -0800342 platformInternalPath := "libsysprop-platform/android_arm64_armv8-a_static/gen/sysprop/include"
343 platformPublicCorePath := "libsysprop-platform/android_arm64_armv8-a_static/gen/sysprop/public/include"
Colin Crossfb0c16e2019-11-20 17:12:35 -0800344 platformPublicVendorPath := "libsysprop-platform/android_vendor.VER_arm64_armv8-a_static/gen/sysprop/public/include"
Inseob Kimc0907f12019-02-08 21:00:45 +0900345
Colin Cross7113d202019-11-20 16:39:12 -0800346 platformOnProductPath := "libsysprop-platform-on-product/android_arm64_armv8-a_static/gen/sysprop/public/include"
Inseob Kimc0907f12019-02-08 21:00:45 +0900347
Colin Crossfb0c16e2019-11-20 17:12:35 -0800348 vendorInternalPath := "libsysprop-vendor/android_vendor.VER_arm64_armv8-a_static/gen/sysprop/include"
Inseob Kimfe612182020-10-20 16:29:55 +0900349 vendorPublicPath := "libsysprop-vendor-on-product/android_arm64_armv8-a_static/gen/sysprop/public/include"
Inseob Kimc0907f12019-02-08 21:00:45 +0900350
351 platformClient := ctx.ModuleForTests("cc-client-platform", coreVariant)
352 platformFlags := platformClient.Rule("cc").Args["cFlags"]
353
Jiyong Park5d1598f2019-02-25 22:14:17 +0900354 // platform should use platform's internal header
Inseob Kimc0907f12019-02-08 21:00:45 +0900355 if !strings.Contains(platformFlags, platformInternalPath) {
356 t.Errorf("flags for platform must contain %#v, but was %#v.",
357 platformInternalPath, platformFlags)
358 }
359
Jiyong Park5d1598f2019-02-25 22:14:17 +0900360 platformStaticClient := ctx.ModuleForTests("cc-client-platform-static", coreVariant)
361 platformStaticFlags := platformStaticClient.Rule("cc").Args["cFlags"]
362
363 // platform-static should use platform's internal header
364 if !strings.Contains(platformStaticFlags, platformInternalPath) {
365 t.Errorf("flags for platform-static must contain %#v, but was %#v.",
366 platformInternalPath, platformStaticFlags)
367 }
368
Inseob Kimc0907f12019-02-08 21:00:45 +0900369 productClient := ctx.ModuleForTests("cc-client-product", coreVariant)
370 productFlags := productClient.Rule("cc").Args["cFlags"]
371
Inseob Kim5cefbd22019-06-08 20:36:59 +0900372 // Product should use platform's and vendor's public headers
Inseob Kimc0907f12019-02-08 21:00:45 +0900373 if !strings.Contains(productFlags, platformOnProductPath) ||
Inseob Kim5cefbd22019-06-08 20:36:59 +0900374 !strings.Contains(productFlags, vendorPublicPath) {
Inseob Kimc0907f12019-02-08 21:00:45 +0900375 t.Errorf("flags for product must contain %#v and %#v, but was %#v.",
Inseob Kim5cefbd22019-06-08 20:36:59 +0900376 platformPublicCorePath, vendorPublicPath, productFlags)
Inseob Kimc0907f12019-02-08 21:00:45 +0900377 }
378
379 vendorClient := ctx.ModuleForTests("cc-client-vendor", vendorVariant)
380 vendorFlags := vendorClient.Rule("cc").Args["cFlags"]
381
Inseob Kim5cefbd22019-06-08 20:36:59 +0900382 // Vendor should use platform's public header and vendor's internal header
383 if !strings.Contains(vendorFlags, platformPublicVendorPath) ||
Inseob Kimc0907f12019-02-08 21:00:45 +0900384 !strings.Contains(vendorFlags, vendorInternalPath) {
385 t.Errorf("flags for vendor must contain %#v and %#v, but was %#v.",
Inseob Kim5cefbd22019-06-08 20:36:59 +0900386 platformPublicVendorPath, vendorInternalPath, vendorFlags)
Inseob Kimc0907f12019-02-08 21:00:45 +0900387 }
Inseob Kimac1e9862019-12-09 18:15:47 +0900388
389 // Java modules linking against system API should use public stub
Colin Cross75ce9ec2021-02-26 16:20:32 -0800390 javaSystemApiClient := ctx.ModuleForTests("java-platform", "android_common").Rule("javac")
391 syspropPlatformPublic := ctx.ModuleForTests("sysprop-platform_public", "android_common").Description("for turbine")
392 if g, w := javaSystemApiClient.Implicits.Strings(), syspropPlatformPublic.Output.String(); !android.InList(w, g) {
393 t.Errorf("system api client should use public stub %q, got %q", w, g)
Inseob Kimac1e9862019-12-09 18:15:47 +0900394 }
Inseob Kimc0907f12019-02-08 21:00:45 +0900395}
Jiyong Park5e914b22021-03-08 10:09:52 +0900396
397func TestApexAvailabilityIsForwarded(t *testing.T) {
398 ctx := test(t, `
399 sysprop_library {
400 name: "sysprop-platform",
401 apex_available: ["//apex_available:platform"],
402 srcs: ["android/sysprop/PlatformProperties.sysprop"],
403 api_packages: ["android.sysprop"],
404 property_owner: "Platform",
405 }
406 `)
407
408 expected := []string{"//apex_available:platform"}
409
410 ccModule := ctx.ModuleForTests("libsysprop-platform", "android_arm64_armv8-a_shared").Module().(*cc.Module)
411 propFromCc := ccModule.ApexProperties.Apex_available
412 if !reflect.DeepEqual(propFromCc, expected) {
413 t.Errorf("apex_available not forwarded to cc module. expected %#v, got %#v",
414 expected, propFromCc)
415 }
416
417 javaModule := ctx.ModuleForTests("sysprop-platform", "android_common").Module().(*java.Library)
418 propFromJava := javaModule.ApexProperties.Apex_available
419 if !reflect.DeepEqual(propFromJava, expected) {
420 t.Errorf("apex_available not forwarded to java module. expected %#v, got %#v",
421 expected, propFromJava)
422 }
423}
424
425func TestMinSdkVersionIsForwarded(t *testing.T) {
426 ctx := test(t, `
427 sysprop_library {
428 name: "sysprop-platform",
429 srcs: ["android/sysprop/PlatformProperties.sysprop"],
430 api_packages: ["android.sysprop"],
431 property_owner: "Platform",
432 cpp: {
433 min_sdk_version: "29",
434 },
435 java: {
436 min_sdk_version: "30",
437 },
438 }
439 `)
440
441 ccModule := ctx.ModuleForTests("libsysprop-platform", "android_arm64_armv8-a_shared").Module().(*cc.Module)
442 propFromCc := proptools.String(ccModule.Properties.Min_sdk_version)
443 if propFromCc != "29" {
444 t.Errorf("min_sdk_version not forwarded to cc module. expected %#v, got %#v",
445 "29", propFromCc)
446 }
447
448 javaModule := ctx.ModuleForTests("sysprop-platform", "android_common").Module().(*java.Library)
449 propFromJava := javaModule.MinSdkVersion()
450 if propFromJava != "30" {
451 t.Errorf("min_sdk_version not forwarded to java module. expected %#v, got %#v",
452 "30", propFromJava)
453 }
454}