| 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) | 
| Ulya Trafimovich | 50c4a4b | 2020-04-21 15:36:33 +0100 | [diff] [blame] | 51 | dexpreoptConfig.BootJars = []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 | 98be1bb | 2019-12-13 20:41:13 -0800 | [diff] [blame] | 54 | ctx := testContext() | 
| Ulya Trafimovich | b28cc37 | 2020-01-13 15:18:16 +0000 | [diff] [blame] | 55 | RegisterDexpreoptBootJarsComponents(ctx) | 
| Colin Cross | 7622867 | 2019-02-25 16:40:34 -0800 | [diff] [blame] | 56 | run(t, ctx, config) | 
|  | 57 |  | 
|  | 58 | dexpreoptBootJars := ctx.SingletonForTests("dex_bootjars") | 
| Ulya Trafimovich | 86d9e3a | 2020-05-19 11:15:44 +0100 | [diff] [blame] | 59 | rule := dexpreoptBootJars.Output(ruleFile) | 
| Colin Cross | 7622867 | 2019-02-25 16:40:34 -0800 | [diff] [blame] | 60 |  | 
| Ulya Trafimovich | 86d9e3a | 2020-05-19 11:15:44 +0100 | [diff] [blame] | 61 | 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 |  | 
|  | 86 | func TestDexpreoptBootJars(t *testing.T) { | 
|  | 87 | ruleFile := "boot-foo.art" | 
| Colin Cross | 7622867 | 2019-02-25 16:40:34 -0800 | [diff] [blame] | 88 |  | 
|  | 89 | expectedInputs := []string{ | 
| David Srbecky | 7f8dac1 | 2020-02-13 16:00:45 +0000 | [diff] [blame] | 90 | "dex_artjars/android/apex/com.android.art/javalib/arm64/boot.art", | 
| Colin Cross | 7622867 | 2019-02-25 16:40:34 -0800 | [diff] [blame] | 91 | "dex_bootjars_input/foo.jar", | 
|  | 92 | "dex_bootjars_input/bar.jar", | 
| Colin Cross | 42be761 | 2019-02-21 18:12:14 -0800 | [diff] [blame] | 93 | "dex_bootjars_input/baz.jar", | 
| Colin Cross | 7622867 | 2019-02-25 16:40:34 -0800 | [diff] [blame] | 94 | } | 
|  | 95 |  | 
| Colin Cross | 7622867 | 2019-02-25 16:40:34 -0800 | [diff] [blame] | 96 | expectedOutputs := []string{ | 
| David Srbecky | 7f8dac1 | 2020-02-13 16:00:45 +0000 | [diff] [blame] | 97 | "dex_bootjars/android/system/framework/arm64/boot.invocation", | 
| David Srbecky | 7f8dac1 | 2020-02-13 16:00:45 +0000 | [diff] [blame] | 98 | "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 Srbecky | 7f8dac1 | 2020-02-13 16:00:45 +0000 | [diff] [blame] | 101 | "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 Srbecky | 7f8dac1 | 2020-02-13 16:00:45 +0000 | [diff] [blame] | 104 | "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 Srbecky | 7f8dac1 | 2020-02-13 16:00:45 +0000 | [diff] [blame] | 107 | "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 Cross | 7622867 | 2019-02-25 16:40:34 -0800 | [diff] [blame] | 110 | } | 
|  | 111 |  | 
| Ulya Trafimovich | 86d9e3a | 2020-05-19 11:15:44 +0100 | [diff] [blame] | 112 | testDexpreoptBoot(t, ruleFile, expectedInputs, expectedOutputs) | 
|  | 113 | } | 
|  | 114 |  | 
|  | 115 | // Changes to the boot.zip structure may break the ART APK scanner. | 
|  | 116 | func TestDexpreoptBootZip(t *testing.T) { | 
|  | 117 | ruleFile := "boot.zip" | 
|  | 118 |  | 
| Ulya Trafimovich | 5006d8d | 2020-05-20 13:47:13 +0100 | [diff] [blame] | 119 | ctx := android.PathContextForTesting(testConfig(nil, "", nil)) | 
|  | 120 | expectedInputs := []string{} | 
|  | 121 | for _, target := range dexpreoptTargets(ctx) { | 
|  | 122 | 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 Cross | 7622867 | 2019-02-25 16:40:34 -0800 | [diff] [blame] | 128 | } | 
|  | 129 |  | 
| Ulya Trafimovich | 86d9e3a | 2020-05-19 11:15:44 +0100 | [diff] [blame] | 130 | expectedOutputs := []string{ | 
|  | 131 | "dex_bootjars/boot.zip", | 
| Colin Cross | 7622867 | 2019-02-25 16:40:34 -0800 | [diff] [blame] | 132 | } | 
| Ulya Trafimovich | 86d9e3a | 2020-05-19 11:15:44 +0100 | [diff] [blame] | 133 |  | 
|  | 134 | testDexpreoptBoot(t, ruleFile, expectedInputs, expectedOutputs) | 
| Colin Cross | 7622867 | 2019-02-25 16:40:34 -0800 | [diff] [blame] | 135 | } |