Colin Cross | 7622867 | 2019-02-25 16:40:34 -0800 | [diff] [blame] | 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 | |
| 15 | package java |
| 16 | |
| 17 | import ( |
| 18 | "path/filepath" |
| 19 | "reflect" |
| 20 | "sort" |
| 21 | "testing" |
| 22 | |
| 23 | "android/soong/android" |
| 24 | "android/soong/dexpreopt" |
| 25 | ) |
| 26 | |
Ulya Trafimovich | 86d9e3a | 2020-05-19 11:15:44 +0100 | [diff] [blame] | 27 | func testDexpreoptBoot(t *testing.T, ruleFile string, expectedInputs, expectedOutputs []string) { |
Colin Cross | 7622867 | 2019-02-25 16:40:34 -0800 | [diff] [blame] | 28 | 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 Cross | 42be761 | 2019-02-21 18:12:14 -0800 | [diff] [blame] | 40 | |
| 41 | dex_import { |
| 42 | name: "baz", |
| 43 | jars: ["a.jar"], |
| 44 | } |
Colin Cross | 7622867 | 2019-02-25 16:40:34 -0800 | [diff] [blame] | 45 | ` |
| 46 | |
Colin Cross | 98be1bb | 2019-12-13 20:41:13 -0800 | [diff] [blame] | 47 | config := testConfig(nil, bp, nil) |
Colin Cross | 7622867 | 2019-02-25 16:40:34 -0800 | [diff] [blame] | 48 | |
Colin Cross | 98be1bb | 2019-12-13 20:41:13 -0800 | [diff] [blame] | 49 | pathCtx := android.PathContextForTesting(config) |
Colin Cross | 7622867 | 2019-02-25 16:40:34 -0800 | [diff] [blame] | 50 | dexpreoptConfig := dexpreopt.GlobalConfigForTests(pathCtx) |
Paul Duffin | e10dfa4 | 2020-10-23 21:23:44 +0100 | [diff] [blame] | 51 | dexpreoptConfig.BootJars = android.CreateTestConfiguredJarList([]string{"platform:foo", "platform:bar", "platform:baz"}) |
Martin Stjernholm | 40f9f3c | 2020-01-20 18:12:23 +0000 | [diff] [blame] | 52 | dexpreopt.SetTestGlobalConfig(config, dexpreoptConfig) |
Colin Cross | 7622867 | 2019-02-25 16:40:34 -0800 | [diff] [blame] | 53 | |
Colin Cross | ae8600b | 2020-10-29 17:09:13 -0700 | [diff] [blame] | 54 | ctx := testContext(config) |
Colin Cross | 7622867 | 2019-02-25 16:40:34 -0800 | [diff] [blame] | 55 | run(t, ctx, config) |
| 56 | |
| 57 | dexpreoptBootJars := ctx.SingletonForTests("dex_bootjars") |
Ulya Trafimovich | 86d9e3a | 2020-05-19 11:15:44 +0100 | [diff] [blame] | 58 | rule := dexpreoptBootJars.Output(ruleFile) |
Colin Cross | 7622867 | 2019-02-25 16:40:34 -0800 | [diff] [blame] | 59 | |
Ulya Trafimovich | 86d9e3a | 2020-05-19 11:15:44 +0100 | [diff] [blame] | 60 | 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 | |
| 85 | func TestDexpreoptBootJars(t *testing.T) { |
| 86 | ruleFile := "boot-foo.art" |
Colin Cross | 7622867 | 2019-02-25 16:40:34 -0800 | [diff] [blame] | 87 | |
| 88 | expectedInputs := []string{ |
Martin Stjernholm | ea581fc | 2020-10-14 23:29:49 +0100 | [diff] [blame] | 89 | "dex_artjars/android/apex/art_boot_images/javalib/arm64/boot.art", |
Colin Cross | 7622867 | 2019-02-25 16:40:34 -0800 | [diff] [blame] | 90 | "dex_bootjars_input/foo.jar", |
| 91 | "dex_bootjars_input/bar.jar", |
Colin Cross | 42be761 | 2019-02-21 18:12:14 -0800 | [diff] [blame] | 92 | "dex_bootjars_input/baz.jar", |
Colin Cross | 7622867 | 2019-02-25 16:40:34 -0800 | [diff] [blame] | 93 | } |
| 94 | |
Colin Cross | 7622867 | 2019-02-25 16:40:34 -0800 | [diff] [blame] | 95 | expectedOutputs := []string{ |
David Srbecky | 7f8dac1 | 2020-02-13 16:00:45 +0000 | [diff] [blame] | 96 | "dex_bootjars/android/system/framework/arm64/boot.invocation", |
David Srbecky | 7f8dac1 | 2020-02-13 16:00:45 +0000 | [diff] [blame] | 97 | "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 Srbecky | 7f8dac1 | 2020-02-13 16:00:45 +0000 | [diff] [blame] | 100 | "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 Srbecky | 7f8dac1 | 2020-02-13 16:00:45 +0000 | [diff] [blame] | 103 | "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 Srbecky | 7f8dac1 | 2020-02-13 16:00:45 +0000 | [diff] [blame] | 106 | "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 Cross | 7622867 | 2019-02-25 16:40:34 -0800 | [diff] [blame] | 109 | } |
| 110 | |
Ulya Trafimovich | 86d9e3a | 2020-05-19 11:15:44 +0100 | [diff] [blame] | 111 | testDexpreoptBoot(t, ruleFile, expectedInputs, expectedOutputs) |
| 112 | } |
| 113 | |
| 114 | // Changes to the boot.zip structure may break the ART APK scanner. |
| 115 | func TestDexpreoptBootZip(t *testing.T) { |
| 116 | ruleFile := "boot.zip" |
| 117 | |
Ulya Trafimovich | 5006d8d | 2020-05-20 13:47:13 +0100 | [diff] [blame] | 118 | ctx := android.PathContextForTesting(testConfig(nil, "", nil)) |
| 119 | expectedInputs := []string{} |
Ulya Trafimovich | 9ab4933 | 2020-06-10 15:44:25 +0100 | [diff] [blame] | 120 | for _, target := range ctx.Config().Targets[android.Android] { |
Ulya Trafimovich | 5006d8d | 2020-05-20 13:47:13 +0100 | [diff] [blame] | 121 | 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 Cross | 7622867 | 2019-02-25 16:40:34 -0800 | [diff] [blame] | 127 | } |
| 128 | |
Ulya Trafimovich | 86d9e3a | 2020-05-19 11:15:44 +0100 | [diff] [blame] | 129 | expectedOutputs := []string{ |
| 130 | "dex_bootjars/boot.zip", |
Colin Cross | 7622867 | 2019-02-25 16:40:34 -0800 | [diff] [blame] | 131 | } |
Ulya Trafimovich | 86d9e3a | 2020-05-19 11:15:44 +0100 | [diff] [blame] | 132 | |
| 133 | testDexpreoptBoot(t, ruleFile, expectedInputs, expectedOutputs) |
Colin Cross | 7622867 | 2019-02-25 16:40:34 -0800 | [diff] [blame] | 134 | } |