blob: 95fe5e10ee07006294351e3916ceba56f5df77a0 [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)
Ulya Trafimovichb28cc372020-01-13 15:18:16 +000055 RegisterDexpreoptBootJarsComponents(ctx)
Colin Cross76228672019-02-25 16:40:34 -080056 run(t, ctx, config)
57
58 dexpreoptBootJars := ctx.SingletonForTests("dex_bootjars")
Ulya Trafimovich86d9e3a2020-05-19 11:15:44 +010059 rule := dexpreoptBootJars.Output(ruleFile)
Colin Cross76228672019-02-25 16:40:34 -080060
Ulya Trafimovich86d9e3a2020-05-19 11:15:44 +010061 for i := range expectedInputs {
62 expectedInputs[i] = filepath.Join(buildDir, "test_device", expectedInputs[i])
63 }
64
65 for i := range expectedOutputs {
66 expectedOutputs[i] = filepath.Join(buildDir, "test_device", expectedOutputs[i])
67 }
68
69 inputs := rule.Implicits.Strings()
70 sort.Strings(inputs)
71 sort.Strings(expectedInputs)
72
73 outputs := append(android.WritablePaths{rule.Output}, rule.ImplicitOutputs...).Strings()
74 sort.Strings(outputs)
75 sort.Strings(expectedOutputs)
76
77 if !reflect.DeepEqual(inputs, expectedInputs) {
78 t.Errorf("want inputs %q\n got inputs %q", expectedInputs, inputs)
79 }
80
81 if !reflect.DeepEqual(outputs, expectedOutputs) {
82 t.Errorf("want outputs %q\n got outputs %q", expectedOutputs, outputs)
83 }
84}
85
86func TestDexpreoptBootJars(t *testing.T) {
87 ruleFile := "boot-foo.art"
Colin Cross76228672019-02-25 16:40:34 -080088
89 expectedInputs := []string{
Martin Stjernholmea581fc2020-10-14 23:29:49 +010090 "dex_artjars/android/apex/art_boot_images/javalib/arm64/boot.art",
Colin Cross76228672019-02-25 16:40:34 -080091 "dex_bootjars_input/foo.jar",
92 "dex_bootjars_input/bar.jar",
Colin Cross42be7612019-02-21 18:12:14 -080093 "dex_bootjars_input/baz.jar",
Colin Cross76228672019-02-25 16:40:34 -080094 }
95
Colin Cross76228672019-02-25 16:40:34 -080096 expectedOutputs := []string{
David Srbecky7f8dac12020-02-13 16:00:45 +000097 "dex_bootjars/android/system/framework/arm64/boot.invocation",
David Srbecky7f8dac12020-02-13 16:00:45 +000098 "dex_bootjars/android/system/framework/arm64/boot-foo.art",
99 "dex_bootjars/android/system/framework/arm64/boot-bar.art",
100 "dex_bootjars/android/system/framework/arm64/boot-baz.art",
David Srbecky7f8dac12020-02-13 16:00:45 +0000101 "dex_bootjars/android/system/framework/arm64/boot-foo.oat",
102 "dex_bootjars/android/system/framework/arm64/boot-bar.oat",
103 "dex_bootjars/android/system/framework/arm64/boot-baz.oat",
David Srbecky7f8dac12020-02-13 16:00:45 +0000104 "dex_bootjars/android/system/framework/arm64/boot-foo.vdex",
105 "dex_bootjars/android/system/framework/arm64/boot-bar.vdex",
106 "dex_bootjars/android/system/framework/arm64/boot-baz.vdex",
David Srbecky7f8dac12020-02-13 16:00:45 +0000107 "dex_bootjars_unstripped/android/system/framework/arm64/boot-foo.oat",
108 "dex_bootjars_unstripped/android/system/framework/arm64/boot-bar.oat",
109 "dex_bootjars_unstripped/android/system/framework/arm64/boot-baz.oat",
Colin Cross76228672019-02-25 16:40:34 -0800110 }
111
Ulya Trafimovich86d9e3a2020-05-19 11:15:44 +0100112 testDexpreoptBoot(t, ruleFile, expectedInputs, expectedOutputs)
113}
114
115// Changes to the boot.zip structure may break the ART APK scanner.
116func TestDexpreoptBootZip(t *testing.T) {
117 ruleFile := "boot.zip"
118
Ulya Trafimovich5006d8d2020-05-20 13:47:13 +0100119 ctx := android.PathContextForTesting(testConfig(nil, "", nil))
120 expectedInputs := []string{}
Ulya Trafimovich9ab49332020-06-10 15:44:25 +0100121 for _, target := range ctx.Config().Targets[android.Android] {
Ulya Trafimovich5006d8d2020-05-20 13:47:13 +0100122 for _, ext := range []string{".art", ".oat", ".vdex"} {
123 for _, jar := range []string{"foo", "bar", "baz"} {
124 expectedInputs = append(expectedInputs,
125 filepath.Join("dex_bootjars", target.Os.String(), "system/framework", target.Arch.ArchType.String(), "boot-"+jar+ext))
126 }
127 }
Colin Cross76228672019-02-25 16:40:34 -0800128 }
129
Ulya Trafimovich86d9e3a2020-05-19 11:15:44 +0100130 expectedOutputs := []string{
131 "dex_bootjars/boot.zip",
Colin Cross76228672019-02-25 16:40:34 -0800132 }
Ulya Trafimovich86d9e3a2020-05-19 11:15:44 +0100133
134 testDexpreoptBoot(t, ruleFile, expectedInputs, expectedOutputs)
Colin Cross76228672019-02-25 16:40:34 -0800135}