blob: 48bc244d9ca558072258911f90cfb1cdce2916a2 [file] [log] [blame]
Colin Cross76228672019-02-25 16:40:34 -08001// Copyright 2019 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 "path/filepath"
19 "reflect"
20 "sort"
21 "testing"
22
23 "android/soong/android"
24 "android/soong/dexpreopt"
25)
26
Ulya Trafimovich86d9e3a2020-05-19 11:15:44 +010027func testDexpreoptBoot(t *testing.T, ruleFile string, expectedInputs, expectedOutputs []string) {
Colin Cross76228672019-02-25 16:40:34 -080028 bp := `
29 java_sdk_library {
30 name: "foo",
31 srcs: ["a.java"],
32 api_packages: ["foo"],
33 }
34
35 java_library {
36 name: "bar",
37 srcs: ["b.java"],
38 installable: true,
39 }
Colin Cross42be7612019-02-21 18:12:14 -080040
41 dex_import {
42 name: "baz",
43 jars: ["a.jar"],
44 }
Colin Cross76228672019-02-25 16:40:34 -080045 `
46
Colin Cross98be1bb2019-12-13 20:41:13 -080047 config := testConfig(nil, bp, nil)
Colin Cross76228672019-02-25 16:40:34 -080048
Colin Cross98be1bb2019-12-13 20:41:13 -080049 pathCtx := android.PathContextForTesting(config)
Colin Cross76228672019-02-25 16:40:34 -080050 dexpreoptConfig := dexpreopt.GlobalConfigForTests(pathCtx)
Paul Duffine10dfa42020-10-23 21:23:44 +010051 dexpreoptConfig.BootJars = android.CreateTestConfiguredJarList([]string{"platform:foo", "platform:bar", "platform:baz"})
Martin Stjernholm40f9f3c2020-01-20 18:12:23 +000052 dexpreopt.SetTestGlobalConfig(config, dexpreoptConfig)
Colin Cross76228672019-02-25 16:40:34 -080053
Colin Crossae8600b2020-10-29 17:09:13 -070054 ctx := testContext(config)
Colin Cross76228672019-02-25 16:40:34 -080055 run(t, ctx, config)
56
57 dexpreoptBootJars := ctx.SingletonForTests("dex_bootjars")
Ulya Trafimovich86d9e3a2020-05-19 11:15:44 +010058 rule := dexpreoptBootJars.Output(ruleFile)
Colin Cross76228672019-02-25 16:40:34 -080059
Ulya Trafimovich86d9e3a2020-05-19 11:15:44 +010060 for i := range expectedInputs {
61 expectedInputs[i] = filepath.Join(buildDir, "test_device", expectedInputs[i])
62 }
63
64 for i := range expectedOutputs {
65 expectedOutputs[i] = filepath.Join(buildDir, "test_device", expectedOutputs[i])
66 }
67
68 inputs := rule.Implicits.Strings()
69 sort.Strings(inputs)
70 sort.Strings(expectedInputs)
71
72 outputs := append(android.WritablePaths{rule.Output}, rule.ImplicitOutputs...).Strings()
73 sort.Strings(outputs)
74 sort.Strings(expectedOutputs)
75
76 if !reflect.DeepEqual(inputs, expectedInputs) {
77 t.Errorf("want inputs %q\n got inputs %q", expectedInputs, inputs)
78 }
79
80 if !reflect.DeepEqual(outputs, expectedOutputs) {
81 t.Errorf("want outputs %q\n got outputs %q", expectedOutputs, outputs)
82 }
83}
84
85func TestDexpreoptBootJars(t *testing.T) {
86 ruleFile := "boot-foo.art"
Colin Cross76228672019-02-25 16:40:34 -080087
88 expectedInputs := []string{
Martin Stjernholmea581fc2020-10-14 23:29:49 +010089 "dex_artjars/android/apex/art_boot_images/javalib/arm64/boot.art",
Colin Cross76228672019-02-25 16:40:34 -080090 "dex_bootjars_input/foo.jar",
91 "dex_bootjars_input/bar.jar",
Colin Cross42be7612019-02-21 18:12:14 -080092 "dex_bootjars_input/baz.jar",
Colin Cross76228672019-02-25 16:40:34 -080093 }
94
Colin Cross76228672019-02-25 16:40:34 -080095 expectedOutputs := []string{
David Srbecky7f8dac12020-02-13 16:00:45 +000096 "dex_bootjars/android/system/framework/arm64/boot.invocation",
David Srbecky7f8dac12020-02-13 16:00:45 +000097 "dex_bootjars/android/system/framework/arm64/boot-foo.art",
98 "dex_bootjars/android/system/framework/arm64/boot-bar.art",
99 "dex_bootjars/android/system/framework/arm64/boot-baz.art",
David Srbecky7f8dac12020-02-13 16:00:45 +0000100 "dex_bootjars/android/system/framework/arm64/boot-foo.oat",
101 "dex_bootjars/android/system/framework/arm64/boot-bar.oat",
102 "dex_bootjars/android/system/framework/arm64/boot-baz.oat",
David Srbecky7f8dac12020-02-13 16:00:45 +0000103 "dex_bootjars/android/system/framework/arm64/boot-foo.vdex",
104 "dex_bootjars/android/system/framework/arm64/boot-bar.vdex",
105 "dex_bootjars/android/system/framework/arm64/boot-baz.vdex",
David Srbecky7f8dac12020-02-13 16:00:45 +0000106 "dex_bootjars_unstripped/android/system/framework/arm64/boot-foo.oat",
107 "dex_bootjars_unstripped/android/system/framework/arm64/boot-bar.oat",
108 "dex_bootjars_unstripped/android/system/framework/arm64/boot-baz.oat",
Colin Cross76228672019-02-25 16:40:34 -0800109 }
110
Ulya Trafimovich86d9e3a2020-05-19 11:15:44 +0100111 testDexpreoptBoot(t, ruleFile, expectedInputs, expectedOutputs)
112}
113
114// Changes to the boot.zip structure may break the ART APK scanner.
115func TestDexpreoptBootZip(t *testing.T) {
116 ruleFile := "boot.zip"
117
Ulya Trafimovich5006d8d2020-05-20 13:47:13 +0100118 ctx := android.PathContextForTesting(testConfig(nil, "", nil))
119 expectedInputs := []string{}
Ulya Trafimovich9ab49332020-06-10 15:44:25 +0100120 for _, target := range ctx.Config().Targets[android.Android] {
Ulya Trafimovich5006d8d2020-05-20 13:47:13 +0100121 for _, ext := range []string{".art", ".oat", ".vdex"} {
122 for _, jar := range []string{"foo", "bar", "baz"} {
123 expectedInputs = append(expectedInputs,
124 filepath.Join("dex_bootjars", target.Os.String(), "system/framework", target.Arch.ArchType.String(), "boot-"+jar+ext))
125 }
126 }
Colin Cross76228672019-02-25 16:40:34 -0800127 }
128
Ulya Trafimovich86d9e3a2020-05-19 11:15:44 +0100129 expectedOutputs := []string{
130 "dex_bootjars/boot.zip",
Colin Cross76228672019-02-25 16:40:34 -0800131 }
Ulya Trafimovich86d9e3a2020-05-19 11:15:44 +0100132
133 testDexpreoptBoot(t, ruleFile, expectedInputs, expectedOutputs)
Colin Cross76228672019-02-25 16:40:34 -0800134}