blob: 9d7ed0a4f0783fa39e81c911ab70a6317cb04515 [file] [log] [blame]
Colin Cross3bc7ffa2017-11-22 16:19:37 -08001// Copyright 2017 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
15package java
16
17import (
18 "android/soong/android"
Colin Crossd09b0b62018-04-18 11:06:47 -070019 "fmt"
Colin Crossa4f08812018-10-02 22:03:40 -070020 "path/filepath"
Colin Cross3bc7ffa2017-11-22 16:19:37 -080021 "reflect"
Colin Crossb69301e2017-12-01 10:48:26 -080022 "sort"
Colin Crossd09b0b62018-04-18 11:06:47 -070023 "strings"
Colin Cross3bc7ffa2017-11-22 16:19:37 -080024 "testing"
25)
26
27var (
28 resourceFiles = []string{
29 "res/layout/layout.xml",
30 "res/values/strings.xml",
31 "res/values-en-rUS/strings.xml",
32 }
33
34 compiledResourceFiles = []string{
35 "aapt2/res/layout_layout.xml.flat",
36 "aapt2/res/values_strings.arsc.flat",
37 "aapt2/res/values-en-rUS_strings.arsc.flat",
38 }
39)
40
Colin Cross527012a2017-11-30 22:56:16 -080041func testAppContext(config android.Config, bp string, fs map[string][]byte) *android.TestContext {
42 appFS := map[string][]byte{}
43 for k, v := range fs {
44 appFS[k] = v
Colin Cross3bc7ffa2017-11-22 16:19:37 -080045 }
46
Colin Cross527012a2017-11-30 22:56:16 -080047 for _, file := range resourceFiles {
48 appFS[file] = nil
49 }
50
51 return testContext(config, bp, appFS)
52}
53
54func testApp(t *testing.T, bp string) *android.TestContext {
55 config := testConfig(nil)
56
57 ctx := testAppContext(config, bp, nil)
58
59 run(t, ctx, config)
60
61 return ctx
Colin Cross3bc7ffa2017-11-22 16:19:37 -080062}
63
64func TestApp(t *testing.T) {
Colin Crossa97c5d32018-03-28 14:58:31 -070065 for _, moduleType := range []string{"android_app", "android_library"} {
66 t.Run(moduleType, func(t *testing.T) {
67 ctx := testApp(t, moduleType+` {
68 name: "foo",
69 srcs: ["a.java"],
70 }
71 `)
Colin Cross3bc7ffa2017-11-22 16:19:37 -080072
Colin Crossa97c5d32018-03-28 14:58:31 -070073 foo := ctx.ModuleForTests("foo", "android_common")
Colin Cross3bc7ffa2017-11-22 16:19:37 -080074
Colin Cross31656952018-05-24 16:11:20 -070075 var expectedLinkImplicits []string
76
77 manifestFixer := foo.Output("manifest_fixer/AndroidManifest.xml")
78 expectedLinkImplicits = append(expectedLinkImplicits, manifestFixer.Output.String())
Colin Cross3bc7ffa2017-11-22 16:19:37 -080079
Colin Crossa97c5d32018-03-28 14:58:31 -070080 frameworkRes := ctx.ModuleForTests("framework-res", "android_common")
81 expectedLinkImplicits = append(expectedLinkImplicits,
82 frameworkRes.Output("package-res.apk").Output.String())
Colin Cross3bc7ffa2017-11-22 16:19:37 -080083
Colin Crossa97c5d32018-03-28 14:58:31 -070084 // Test the mapping from input files to compiled output file names
85 compile := foo.Output(compiledResourceFiles[0])
86 if !reflect.DeepEqual(resourceFiles, compile.Inputs.Strings()) {
87 t.Errorf("expected aapt2 compile inputs expected:\n %#v\n got:\n %#v",
88 resourceFiles, compile.Inputs.Strings())
89 }
Colin Crossb69301e2017-12-01 10:48:26 -080090
Colin Crossa97c5d32018-03-28 14:58:31 -070091 compiledResourceOutputs := compile.Outputs.Strings()
92 sort.Strings(compiledResourceOutputs)
Colin Crossb69301e2017-12-01 10:48:26 -080093
Colin Crossa97c5d32018-03-28 14:58:31 -070094 expectedLinkImplicits = append(expectedLinkImplicits, compiledResourceOutputs...)
Colin Cross3bc7ffa2017-11-22 16:19:37 -080095
Colin Crossa97c5d32018-03-28 14:58:31 -070096 list := foo.Output("aapt2/res.list")
97 expectedLinkImplicits = append(expectedLinkImplicits, list.Output.String())
Colin Cross3bc7ffa2017-11-22 16:19:37 -080098
Colin Crossa97c5d32018-03-28 14:58:31 -070099 // Check that the link rule uses
100 res := ctx.ModuleForTests("foo", "android_common").Output("package-res.apk")
101 if !reflect.DeepEqual(expectedLinkImplicits, res.Implicits.Strings()) {
102 t.Errorf("expected aapt2 link implicits expected:\n %#v\n got:\n %#v",
103 expectedLinkImplicits, res.Implicits.Strings())
104 }
105 })
Colin Cross3bc7ffa2017-11-22 16:19:37 -0800106 }
107}
Colin Cross890ff552017-11-30 20:13:19 -0800108
109var testEnforceRROTests = []struct {
110 name string
111 enforceRROTargets []string
112 enforceRROExcludedOverlays []string
Anton Hansson0375a4f2019-01-24 14:39:19 +0000113 overlayFiles map[string][]string
114 rroDirs map[string][]string
Colin Cross890ff552017-11-30 20:13:19 -0800115}{
116 {
117 name: "no RRO",
118 enforceRROTargets: nil,
119 enforceRROExcludedOverlays: nil,
Anton Hansson0375a4f2019-01-24 14:39:19 +0000120 overlayFiles: map[string][]string{
121 "foo": []string{
122 "device/vendor/blah/static_overlay/foo/res/values/strings.xml",
123 "device/vendor/blah/overlay/foo/res/values/strings.xml",
124 },
125 "bar": []string{
126 "device/vendor/blah/static_overlay/bar/res/values/strings.xml",
127 "device/vendor/blah/overlay/bar/res/values/strings.xml",
128 },
Colin Cross890ff552017-11-30 20:13:19 -0800129 },
Anton Hansson0375a4f2019-01-24 14:39:19 +0000130 rroDirs: map[string][]string{
131 "foo": nil,
132 "bar": nil,
Colin Cross890ff552017-11-30 20:13:19 -0800133 },
Colin Cross890ff552017-11-30 20:13:19 -0800134 },
135 {
136 name: "enforce RRO on foo",
137 enforceRROTargets: []string{"foo"},
138 enforceRROExcludedOverlays: []string{"device/vendor/blah/static_overlay"},
Anton Hansson0375a4f2019-01-24 14:39:19 +0000139 overlayFiles: map[string][]string{
140 "foo": []string{"device/vendor/blah/static_overlay/foo/res/values/strings.xml"},
141 "bar": []string{
142 "device/vendor/blah/static_overlay/bar/res/values/strings.xml",
143 "device/vendor/blah/overlay/bar/res/values/strings.xml",
144 },
Colin Cross890ff552017-11-30 20:13:19 -0800145 },
Anton Hansson0375a4f2019-01-24 14:39:19 +0000146 rroDirs: map[string][]string{
147 "foo": []string{"device/vendor/blah/overlay/foo/res"},
148 "bar": nil,
Colin Cross890ff552017-11-30 20:13:19 -0800149 },
Colin Cross890ff552017-11-30 20:13:19 -0800150 },
151 {
152 name: "enforce RRO on all",
153 enforceRROTargets: []string{"*"},
154 enforceRROExcludedOverlays: []string{"device/vendor/blah/static_overlay"},
Anton Hansson0375a4f2019-01-24 14:39:19 +0000155 overlayFiles: map[string][]string{
156 "foo": []string{"device/vendor/blah/static_overlay/foo/res/values/strings.xml"},
157 "bar": []string{"device/vendor/blah/static_overlay/bar/res/values/strings.xml"},
Colin Cross890ff552017-11-30 20:13:19 -0800158 },
Anton Hansson0375a4f2019-01-24 14:39:19 +0000159 rroDirs: map[string][]string{
160 "foo": []string{"device/vendor/blah/overlay/foo/res"},
161 "bar": []string{"device/vendor/blah/overlay/bar/res"},
Colin Cross890ff552017-11-30 20:13:19 -0800162 },
163 },
164}
165
166func TestEnforceRRO(t *testing.T) {
167 resourceOverlays := []string{
168 "device/vendor/blah/overlay",
169 "device/vendor/blah/overlay2",
170 "device/vendor/blah/static_overlay",
171 }
172
173 fs := map[string][]byte{
174 "foo/res/res/values/strings.xml": nil,
175 "bar/res/res/values/strings.xml": nil,
176 "device/vendor/blah/overlay/foo/res/values/strings.xml": nil,
177 "device/vendor/blah/overlay/bar/res/values/strings.xml": nil,
178 "device/vendor/blah/static_overlay/foo/res/values/strings.xml": nil,
179 "device/vendor/blah/static_overlay/bar/res/values/strings.xml": nil,
180 "device/vendor/blah/overlay2/res/values/strings.xml": nil,
181 }
182
183 bp := `
184 android_app {
185 name: "foo",
186 resource_dirs: ["foo/res"],
187 }
188
189 android_app {
190 name: "bar",
191 resource_dirs: ["bar/res"],
192 }
193 `
194
195 for _, testCase := range testEnforceRROTests {
196 t.Run(testCase.name, func(t *testing.T) {
197 config := testConfig(nil)
Dan Willemsen674dc7f2018-03-12 18:06:05 -0700198 config.TestProductVariables.ResourceOverlays = &resourceOverlays
Colin Cross890ff552017-11-30 20:13:19 -0800199 if testCase.enforceRROTargets != nil {
Dan Willemsen674dc7f2018-03-12 18:06:05 -0700200 config.TestProductVariables.EnforceRROTargets = &testCase.enforceRROTargets
Colin Cross890ff552017-11-30 20:13:19 -0800201 }
202 if testCase.enforceRROExcludedOverlays != nil {
Dan Willemsen674dc7f2018-03-12 18:06:05 -0700203 config.TestProductVariables.EnforceRROExcludedOverlays = &testCase.enforceRROExcludedOverlays
Colin Cross890ff552017-11-30 20:13:19 -0800204 }
205
206 ctx := testAppContext(config, bp, fs)
207 run(t, ctx, config)
208
209 getOverlays := func(moduleName string) ([]string, []string) {
210 module := ctx.ModuleForTests(moduleName, "android_common")
211 overlayCompiledPaths := module.Output("aapt2/overlay.list").Inputs.Strings()
212
213 var overlayFiles []string
214 for _, o := range overlayCompiledPaths {
215 overlayFiles = append(overlayFiles, module.Output(o).Inputs.Strings()...)
216 }
217
218 rroDirs := module.Module().(*AndroidApp).rroDirs.Strings()
219
220 return overlayFiles, rroDirs
221 }
222
Anton Hansson0375a4f2019-01-24 14:39:19 +0000223 apps := []string{"foo", "bar"}
224 for _, app := range apps {
225 overlayFiles, rroDirs := getOverlays(app)
Colin Cross890ff552017-11-30 20:13:19 -0800226
Anton Hansson0375a4f2019-01-24 14:39:19 +0000227 if !reflect.DeepEqual(overlayFiles, testCase.overlayFiles[app]) {
228 t.Errorf("expected %s overlay files:\n %#v\n got:\n %#v",
229 app, testCase.overlayFiles[app], overlayFiles)
230 }
231 if !reflect.DeepEqual(rroDirs, testCase.rroDirs[app]) {
232 t.Errorf("expected %s rroDirs: %#v\n got:\n %#v",
233 app, testCase.rroDirs[app], rroDirs)
234 }
Colin Cross890ff552017-11-30 20:13:19 -0800235 }
Colin Cross890ff552017-11-30 20:13:19 -0800236 })
237 }
238}
Colin Crossd09b0b62018-04-18 11:06:47 -0700239
240func TestAppSdkVersion(t *testing.T) {
241 testCases := []struct {
242 name string
243 sdkVersion string
244 platformSdkInt int
245 platformSdkCodename string
246 platformSdkFinal bool
247 expectedMinSdkVersion string
248 }{
249 {
250 name: "current final SDK",
251 sdkVersion: "current",
252 platformSdkInt: 27,
253 platformSdkCodename: "REL",
254 platformSdkFinal: true,
255 expectedMinSdkVersion: "27",
256 },
257 {
258 name: "current non-final SDK",
259 sdkVersion: "current",
260 platformSdkInt: 27,
261 platformSdkCodename: "OMR1",
262 platformSdkFinal: false,
263 expectedMinSdkVersion: "OMR1",
264 },
265 {
266 name: "default final SDK",
267 sdkVersion: "",
268 platformSdkInt: 27,
269 platformSdkCodename: "REL",
270 platformSdkFinal: true,
271 expectedMinSdkVersion: "27",
272 },
273 {
274 name: "default non-final SDK",
275 sdkVersion: "",
276 platformSdkInt: 27,
277 platformSdkCodename: "OMR1",
278 platformSdkFinal: false,
279 expectedMinSdkVersion: "OMR1",
280 },
281 {
282 name: "14",
283 sdkVersion: "14",
284 expectedMinSdkVersion: "14",
285 },
286 }
287
288 for _, moduleType := range []string{"android_app", "android_library"} {
289 for _, test := range testCases {
290 t.Run(moduleType+" "+test.name, func(t *testing.T) {
291 bp := fmt.Sprintf(`%s {
292 name: "foo",
293 srcs: ["a.java"],
294 sdk_version: "%s",
295 }`, moduleType, test.sdkVersion)
296
297 config := testConfig(nil)
298 config.TestProductVariables.Platform_sdk_version = &test.platformSdkInt
299 config.TestProductVariables.Platform_sdk_codename = &test.platformSdkCodename
300 config.TestProductVariables.Platform_sdk_final = &test.platformSdkFinal
301
302 ctx := testAppContext(config, bp, nil)
303
304 run(t, ctx, config)
305
306 foo := ctx.ModuleForTests("foo", "android_common")
307 link := foo.Output("package-res.apk")
308 linkFlags := strings.Split(link.Args["flags"], " ")
309 min := android.IndexList("--min-sdk-version", linkFlags)
310 target := android.IndexList("--target-sdk-version", linkFlags)
311
312 if min == -1 || target == -1 || min == len(linkFlags)-1 || target == len(linkFlags)-1 {
313 t.Fatalf("missing --min-sdk-version or --target-sdk-version in link flags: %q", linkFlags)
314 }
315
316 gotMinSdkVersion := linkFlags[min+1]
317 gotTargetSdkVersion := linkFlags[target+1]
318
319 if gotMinSdkVersion != test.expectedMinSdkVersion {
320 t.Errorf("incorrect --min-sdk-version, expected %q got %q",
321 test.expectedMinSdkVersion, gotMinSdkVersion)
322 }
323
324 if gotTargetSdkVersion != test.expectedMinSdkVersion {
325 t.Errorf("incorrect --target-sdk-version, expected %q got %q",
326 test.expectedMinSdkVersion, gotTargetSdkVersion)
327 }
328 })
329 }
330 }
331}
Colin Crossa4f08812018-10-02 22:03:40 -0700332
333func TestJNI(t *testing.T) {
334 ctx := testJava(t, `
335 toolchain_library {
336 name: "libcompiler_rt-extras",
337 src: "",
338 }
339
340 toolchain_library {
341 name: "libatomic",
342 src: "",
343 }
344
345 toolchain_library {
346 name: "libgcc",
347 src: "",
348 }
349
350 toolchain_library {
351 name: "libclang_rt.builtins-aarch64-android",
352 src: "",
353 }
354
355 toolchain_library {
356 name: "libclang_rt.builtins-arm-android",
357 src: "",
358 }
359
360 cc_object {
361 name: "crtbegin_so",
362 stl: "none",
363 }
364
365 cc_object {
366 name: "crtend_so",
367 stl: "none",
368 }
369
370 cc_library {
371 name: "libjni",
372 system_shared_libs: [],
373 stl: "none",
374 }
375
376 android_test {
377 name: "test",
378 no_framework_libs: true,
379 jni_libs: ["libjni"],
380 }
381
382 android_test {
383 name: "test_first",
384 no_framework_libs: true,
385 compile_multilib: "first",
386 jni_libs: ["libjni"],
387 }
388
389 android_test {
390 name: "test_both",
391 no_framework_libs: true,
392 compile_multilib: "both",
393 jni_libs: ["libjni"],
394 }
395
396 android_test {
397 name: "test_32",
398 no_framework_libs: true,
399 compile_multilib: "32",
400 jni_libs: ["libjni"],
401 }
402
403 android_test {
404 name: "test_64",
405 no_framework_libs: true,
406 compile_multilib: "64",
407 jni_libs: ["libjni"],
408 }
409 `)
410
411 // check the existence of the internal modules
412 ctx.ModuleForTests("test", "android_common")
413 ctx.ModuleForTests("test_first", "android_common")
414 ctx.ModuleForTests("test_both", "android_common")
415 ctx.ModuleForTests("test_32", "android_common")
416 ctx.ModuleForTests("test_64", "android_common")
417
418 testCases := []struct {
419 name string
420 abis []string
421 }{
422 {"test", []string{"arm64-v8a"}},
423 {"test_first", []string{"arm64-v8a"}},
424 {"test_both", []string{"arm64-v8a", "armeabi-v7a"}},
425 {"test_32", []string{"armeabi-v7a"}},
426 {"test_64", []string{"arm64-v8a"}},
427 }
428
429 for _, test := range testCases {
430 t.Run(test.name, func(t *testing.T) {
431 app := ctx.ModuleForTests(test.name, "android_common")
432 jniLibZip := app.Output("jnilibs.zip")
433 var abis []string
434 args := strings.Fields(jniLibZip.Args["jarArgs"])
435 for i := 0; i < len(args); i++ {
436 if args[i] == "-P" {
437 abis = append(abis, filepath.Base(args[i+1]))
438 i++
439 }
440 }
441 if !reflect.DeepEqual(abis, test.abis) {
442 t.Errorf("want abis %v, got %v", test.abis, abis)
443 }
444 })
445 }
446}
Jaewoong Jung2ad817c2019-01-18 14:27:16 -0800447
448func TestCertificates(t *testing.T) {
449 testCases := []struct {
450 name string
451 bp string
452 certificateOverride string
453 expected string
454 }{
455 {
456 name: "default",
457 bp: `
458 android_app {
459 name: "foo",
460 srcs: ["a.java"],
461 }
462 `,
463 certificateOverride: "",
464 expected: "build/target/product/security/testkey.x509.pem build/target/product/security/testkey.pk8",
465 },
466 {
467 name: "module certificate property",
468 bp: `
469 android_app {
470 name: "foo",
471 srcs: ["a.java"],
472 certificate: ":new_certificate"
473 }
474
475 android_app_certificate {
476 name: "new_certificate",
477 certificate: "cert/new_cert",
478 }
479 `,
480 certificateOverride: "",
481 expected: "cert/new_cert.x509.pem cert/new_cert.pk8",
482 },
483 {
484 name: "path certificate property",
485 bp: `
486 android_app {
487 name: "foo",
488 srcs: ["a.java"],
489 certificate: "expiredkey"
490 }
491 `,
492 certificateOverride: "",
493 expected: "build/target/product/security/expiredkey.x509.pem build/target/product/security/expiredkey.pk8",
494 },
495 {
496 name: "certificate overrides",
497 bp: `
498 android_app {
499 name: "foo",
500 srcs: ["a.java"],
501 certificate: "expiredkey"
502 }
503
504 android_app_certificate {
505 name: "new_certificate",
506 certificate: "cert/new_cert",
507 }
508 `,
509 certificateOverride: "foo:new_certificate",
510 expected: "cert/new_cert.x509.pem cert/new_cert.pk8",
511 },
512 }
513
514 for _, test := range testCases {
515 t.Run(test.name, func(t *testing.T) {
516 config := testConfig(nil)
517 if test.certificateOverride != "" {
518 config.TestProductVariables.CertificateOverrides = []string{test.certificateOverride}
519 }
520 ctx := testAppContext(config, test.bp, nil)
521
522 run(t, ctx, config)
523 foo := ctx.ModuleForTests("foo", "android_common")
524
525 signapk := foo.Output("foo.apk")
526 signFlags := signapk.Args["certificates"]
527 if test.expected != signFlags {
528 t.Errorf("Incorrect signing flags, expected: %q, got: %q", test.expected, signFlags)
529 }
530 })
531 }
532}