blob: cd7551c8d6364d39fe2b156d8926ac32e9afc537 [file] [log] [blame]
Colin Cross43f08db2018-11-12 10:13:39 -08001// Copyright 2018 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 dexpreopt
16
17import (
Colin Crossfeec25b2019-01-30 17:32:39 -080018 "android/soong/android"
Anton Hansson4ec91dd2019-10-02 17:42:44 +010019 "fmt"
Colin Cross43f08db2018-11-12 10:13:39 -080020 "testing"
21)
22
Martin Stjernholm8d80cee2020-01-31 17:44:54 +000023func testSystemModuleConfig(ctx android.PathContext, name string) *ModuleConfig {
Anton Hansson4ec91dd2019-10-02 17:42:44 +010024 return testModuleConfig(ctx, name, "system")
25}
26
Martin Stjernholm8d80cee2020-01-31 17:44:54 +000027func testSystemProductModuleConfig(ctx android.PathContext, name string) *ModuleConfig {
Anton Hansson4ec91dd2019-10-02 17:42:44 +010028 return testModuleConfig(ctx, name, "system/product")
29}
30
Martin Stjernholm8d80cee2020-01-31 17:44:54 +000031func testProductModuleConfig(ctx android.PathContext, name string) *ModuleConfig {
Anton Hansson4ec91dd2019-10-02 17:42:44 +010032 return testModuleConfig(ctx, name, "product")
33}
34
Martin Stjernholm8d80cee2020-01-31 17:44:54 +000035func testModuleConfig(ctx android.PathContext, name, partition string) *ModuleConfig {
36 return &ModuleConfig{
Anton Hansson4ec91dd2019-10-02 17:42:44 +010037 Name: name,
38 DexLocation: fmt.Sprintf("/%s/app/test/%s.apk", partition, name),
39 BuildPath: android.PathForOutput(ctx, fmt.Sprintf("%s/%s.apk", name, name)),
40 DexPath: android.PathForOutput(ctx, fmt.Sprintf("%s/dex/%s.jar", name, name)),
Colin Cross69f59a32019-02-15 10:39:37 -080041 UncompressedDex: false,
42 HasApkLibraries: false,
43 PreoptFlags: nil,
44 ProfileClassListing: android.OptionalPath{},
45 ProfileIsTextListing: false,
Ulya Trafimovich8c35fcf2021-02-17 16:23:28 +000046 EnforceUsesLibrariesStatusFile: android.PathForOutput(ctx, fmt.Sprintf("%s/enforce_uses_libraries.status", name)),
Colin Cross69f59a32019-02-15 10:39:37 -080047 EnforceUsesLibraries: false,
Ulya Trafimovich8cbc5d22020-11-03 15:15:46 +000048 ClassLoaderContexts: nil,
Colin Cross69f59a32019-02-15 10:39:37 -080049 Archs: []android.ArchType{android.Arm},
50 DexPreoptImages: android.Paths{android.PathForTesting("system/framework/arm/boot.art")},
Ulya Trafimovich4d2eeed2019-11-08 10:54:21 +000051 DexPreoptImagesDeps: []android.OutputPaths{android.OutputPaths{}},
52 DexPreoptImageLocations: []string{},
Colin Cross69f59a32019-02-15 10:39:37 -080053 PreoptBootClassPathDexFiles: nil,
54 PreoptBootClassPathDexLocations: nil,
55 PreoptExtractedApk: false,
56 NoCreateAppImage: false,
57 ForceCreateAppImage: false,
58 PresignedPrebuilt: false,
Colin Cross69f59a32019-02-15 10:39:37 -080059 }
Colin Cross43f08db2018-11-12 10:13:39 -080060}
61
62func TestDexPreopt(t *testing.T) {
Martin Stjernholm75a48d82020-01-10 20:32:59 +000063 config := android.TestConfig("out", nil, "", nil)
Colin Crossf1a035e2020-11-16 17:32:30 -080064 ctx := android.BuilderContextForTesting(config)
Paul Duffin9f045242021-01-21 15:05:11 +000065 globalSoong := globalSoongConfigForTests()
Martin Stjernholm75a48d82020-01-10 20:32:59 +000066 global := GlobalConfigForTests(ctx)
67 module := testSystemModuleConfig(ctx, "test")
Colin Cross43f08db2018-11-12 10:13:39 -080068
Martin Stjernholm75a48d82020-01-10 20:32:59 +000069 rule, err := GenerateDexpreoptRule(ctx, globalSoong, global, module)
Colin Cross43f08db2018-11-12 10:13:39 -080070 if err != nil {
Colin Cross69f59a32019-02-15 10:39:37 -080071 t.Fatal(err)
Colin Cross43f08db2018-11-12 10:13:39 -080072 }
73
Colin Crossdeabb942019-02-11 14:11:09 -080074 wantInstalls := android.RuleBuilderInstalls{
Colin Cross69f59a32019-02-15 10:39:37 -080075 {android.PathForOutput(ctx, "test/oat/arm/package.odex"), "/system/app/test/oat/arm/test.odex"},
76 {android.PathForOutput(ctx, "test/oat/arm/package.vdex"), "/system/app/test/oat/arm/test.vdex"},
Colin Cross43f08db2018-11-12 10:13:39 -080077 }
78
Colin Cross2cdd5df2019-02-25 10:25:24 -080079 if rule.Installs().String() != wantInstalls.String() {
Colin Cross43f08db2018-11-12 10:13:39 -080080 t.Errorf("\nwant installs:\n %v\ngot:\n %v", wantInstalls, rule.Installs())
81 }
82}
83
84func TestDexPreoptSystemOther(t *testing.T) {
Martin Stjernholm75a48d82020-01-10 20:32:59 +000085 config := android.TestConfig("out", nil, "", nil)
Colin Crossf1a035e2020-11-16 17:32:30 -080086 ctx := android.BuilderContextForTesting(config)
Paul Duffin9f045242021-01-21 15:05:11 +000087 globalSoong := globalSoongConfigForTests()
Anton Hansson4ec91dd2019-10-02 17:42:44 +010088 global := GlobalConfigForTests(ctx)
89 systemModule := testSystemModuleConfig(ctx, "Stest")
90 systemProductModule := testSystemProductModuleConfig(ctx, "SPtest")
91 productModule := testProductModuleConfig(ctx, "Ptest")
Colin Cross43f08db2018-11-12 10:13:39 -080092
93 global.HasSystemOther = true
Colin Cross43f08db2018-11-12 10:13:39 -080094
Anton Hansson4ec91dd2019-10-02 17:42:44 +010095 type moduleTest struct {
Martin Stjernholm8d80cee2020-01-31 17:44:54 +000096 module *ModuleConfig
Anton Hansson4ec91dd2019-10-02 17:42:44 +010097 expectedPartition string
98 }
99 tests := []struct {
100 patterns []string
101 moduleTests []moduleTest
102 }{
103 {
104 patterns: []string{"app/%"},
105 moduleTests: []moduleTest{
Anton Hansson43ab0bc2019-10-03 14:18:45 +0100106 {module: systemModule, expectedPartition: "system_other/system"},
Anton Hansson4ec91dd2019-10-02 17:42:44 +0100107 {module: systemProductModule, expectedPartition: "system/product"},
108 {module: productModule, expectedPartition: "product"},
109 },
110 },
Anton Hanssonda4d9d92020-09-15 09:28:55 +0000111 // product/app/% only applies to product apps inside the system partition
Anton Hansson4ec91dd2019-10-02 17:42:44 +0100112 {
113 patterns: []string{"app/%", "product/app/%"},
114 moduleTests: []moduleTest{
Anton Hansson43ab0bc2019-10-03 14:18:45 +0100115 {module: systemModule, expectedPartition: "system_other/system"},
116 {module: systemProductModule, expectedPartition: "system_other/system/product"},
Anton Hanssonda4d9d92020-09-15 09:28:55 +0000117 {module: productModule, expectedPartition: "product"},
Anton Hansson4ec91dd2019-10-02 17:42:44 +0100118 },
119 },
Colin Cross43f08db2018-11-12 10:13:39 -0800120 }
121
Anton Hansson4ec91dd2019-10-02 17:42:44 +0100122 for _, test := range tests {
123 global.PatternsOnSystemOther = test.patterns
124 for _, mt := range test.moduleTests {
Martin Stjernholm75a48d82020-01-10 20:32:59 +0000125 rule, err := GenerateDexpreoptRule(ctx, globalSoong, global, mt.module)
Anton Hansson4ec91dd2019-10-02 17:42:44 +0100126 if err != nil {
127 t.Fatal(err)
128 }
129
130 name := mt.module.Name
131 wantInstalls := android.RuleBuilderInstalls{
132 {android.PathForOutput(ctx, name+"/oat/arm/package.odex"), fmt.Sprintf("/%s/app/test/oat/arm/%s.odex", mt.expectedPartition, name)},
133 {android.PathForOutput(ctx, name+"/oat/arm/package.vdex"), fmt.Sprintf("/%s/app/test/oat/arm/%s.vdex", mt.expectedPartition, name)},
134 }
135
136 if rule.Installs().String() != wantInstalls.String() {
Anton Hanssonda4d9d92020-09-15 09:28:55 +0000137 t.Errorf("\nwant installs:\n %v\ngot:\n %v", wantInstalls, rule.Installs())
Anton Hansson4ec91dd2019-10-02 17:42:44 +0100138 }
139 }
Colin Cross43f08db2018-11-12 10:13:39 -0800140 }
141
Colin Cross43f08db2018-11-12 10:13:39 -0800142}
143
144func TestDexPreoptProfile(t *testing.T) {
Martin Stjernholm75a48d82020-01-10 20:32:59 +0000145 config := android.TestConfig("out", nil, "", nil)
Colin Crossf1a035e2020-11-16 17:32:30 -0800146 ctx := android.BuilderContextForTesting(config)
Paul Duffin9f045242021-01-21 15:05:11 +0000147 globalSoong := globalSoongConfigForTests()
Martin Stjernholm75a48d82020-01-10 20:32:59 +0000148 global := GlobalConfigForTests(ctx)
149 module := testSystemModuleConfig(ctx, "test")
Colin Cross43f08db2018-11-12 10:13:39 -0800150
Colin Cross69f59a32019-02-15 10:39:37 -0800151 module.ProfileClassListing = android.OptionalPathForPath(android.PathForTesting("profile"))
Colin Cross43f08db2018-11-12 10:13:39 -0800152
Martin Stjernholm75a48d82020-01-10 20:32:59 +0000153 rule, err := GenerateDexpreoptRule(ctx, globalSoong, global, module)
Colin Cross43f08db2018-11-12 10:13:39 -0800154 if err != nil {
Colin Cross69f59a32019-02-15 10:39:37 -0800155 t.Fatal(err)
Colin Cross43f08db2018-11-12 10:13:39 -0800156 }
157
Colin Crossdeabb942019-02-11 14:11:09 -0800158 wantInstalls := android.RuleBuilderInstalls{
Colin Cross69f59a32019-02-15 10:39:37 -0800159 {android.PathForOutput(ctx, "test/profile.prof"), "/system/app/test/test.apk.prof"},
160 {android.PathForOutput(ctx, "test/oat/arm/package.art"), "/system/app/test/oat/arm/test.art"},
161 {android.PathForOutput(ctx, "test/oat/arm/package.odex"), "/system/app/test/oat/arm/test.odex"},
162 {android.PathForOutput(ctx, "test/oat/arm/package.vdex"), "/system/app/test/oat/arm/test.vdex"},
Colin Cross43f08db2018-11-12 10:13:39 -0800163 }
164
Colin Cross2cdd5df2019-02-25 10:25:24 -0800165 if rule.Installs().String() != wantInstalls.String() {
Colin Cross43f08db2018-11-12 10:13:39 -0800166 t.Errorf("\nwant installs:\n %v\ngot:\n %v", wantInstalls, rule.Installs())
167 }
168}
Jeongik Chac6246672021-04-08 00:00:19 +0900169
170func TestDexPreoptConfigToJson(t *testing.T) {
171 config := android.TestConfig("out", nil, "", nil)
172 ctx := android.BuilderContextForTesting(config)
173 module := testSystemModuleConfig(ctx, "test")
174 data, err := moduleConfigToJSON(module)
175 if err != nil {
176 t.Errorf("Failed to convert module config data to JSON, %v", err)
177 }
178 parsed, err := ParseModuleConfig(ctx, data)
179 if err != nil {
180 t.Errorf("Failed to parse JSON, %v", err)
181 }
182 before := fmt.Sprintf("%v", module)
183 after := fmt.Sprintf("%v", parsed)
184 android.AssertStringEquals(t, "The result must be the same as the original after marshalling and unmarshalling it.", before, after)
185}