1// 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	"sort"
20	"testing"
21
22	"android/soong/android"
23)
24
25func testDexpreoptBoot(t *testing.T, ruleFile string, expectedInputs, expectedOutputs []string) {
26	bp := `
27		java_sdk_library {
28			name: "foo",
29			srcs: ["a.java"],
30			api_packages: ["foo"],
31		}
32
33		java_library {
34			name: "bar",
35			srcs: ["b.java"],
36			installable: true,
37			system_ext_specific: true,
38		}
39
40		dex_import {
41			name: "baz",
42			jars: ["a.jar"],
43		}
44
45		platform_bootclasspath {
46			name: "platform-bootclasspath",
47		}
48	`
49
50	result := android.GroupFixturePreparers(
51		prepareForJavaTest,
52		PrepareForTestWithJavaSdkLibraryFiles,
53		FixtureWithLastReleaseApis("foo"),
54		FixtureConfigureBootJars("platform:foo", "system_ext:bar", "platform:baz"),
55	).RunTestWithBp(t, bp)
56
57	platformBootclasspath := result.ModuleForTests("platform-bootclasspath", "android_common")
58	rule := platformBootclasspath.Output(ruleFile)
59
60	for i := range expectedInputs {
61		expectedInputs[i] = filepath.Join("out/soong/test_device", expectedInputs[i])
62	}
63
64	for i := range expectedOutputs {
65		expectedOutputs[i] = filepath.Join("out/soong/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	android.AssertStringPathsRelativeToTopEquals(t, "inputs", result.Config, expectedInputs, inputs)
77
78	android.AssertStringPathsRelativeToTopEquals(t, "outputs", result.Config, expectedOutputs, outputs)
79}
80
81func TestDexpreoptBootJars(t *testing.T) {
82	ruleFile := "boot-foo.art"
83
84	expectedInputs := []string{
85		"dex_artjars/android/apex/art_boot_images/javalib/arm64/boot.art",
86		"dex_bootjars_input/foo.jar",
87		"dex_bootjars_input/bar.jar",
88		"dex_bootjars_input/baz.jar",
89	}
90
91	expectedOutputs := []string{
92		"dex_bootjars/android/system/framework/arm64/boot.invocation",
93		"dex_bootjars/android/system/framework/arm64/boot-foo.art",
94		"dex_bootjars/android/system/framework/arm64/boot-bar.art",
95		"dex_bootjars/android/system/framework/arm64/boot-baz.art",
96		"dex_bootjars/android/system/framework/arm64/boot-foo.oat",
97		"dex_bootjars/android/system/framework/arm64/boot-bar.oat",
98		"dex_bootjars/android/system/framework/arm64/boot-baz.oat",
99		"dex_bootjars/android/system/framework/arm64/boot-foo.vdex",
100		"dex_bootjars/android/system/framework/arm64/boot-bar.vdex",
101		"dex_bootjars/android/system/framework/arm64/boot-baz.vdex",
102		"dex_bootjars_unstripped/android/system/framework/arm64/boot-foo.oat",
103		"dex_bootjars_unstripped/android/system/framework/arm64/boot-bar.oat",
104		"dex_bootjars_unstripped/android/system/framework/arm64/boot-baz.oat",
105	}
106
107	testDexpreoptBoot(t, ruleFile, expectedInputs, expectedOutputs)
108}
109
110// Changes to the boot.zip structure may break the ART APK scanner.
111func TestDexpreoptBootZip(t *testing.T) {
112	ruleFile := "boot.zip"
113
114	ctx := android.PathContextForTesting(android.TestArchConfig("", nil, "", nil))
115	expectedInputs := []string{}
116	for _, target := range ctx.Config().Targets[android.Android] {
117		for _, ext := range []string{".art", ".oat", ".vdex"} {
118			for _, jar := range []string{"foo", "bar", "baz"} {
119				expectedInputs = append(expectedInputs,
120					filepath.Join("dex_bootjars", target.Os.String(), "system/framework", target.Arch.ArchType.String(), "boot-"+jar+ext))
121			}
122		}
123	}
124
125	expectedOutputs := []string{
126		"dex_bootjars/boot.zip",
127	}
128
129	testDexpreoptBoot(t, ruleFile, expectedInputs, expectedOutputs)
130}
131