Tao Bao | 0ba5c94 | 2018-08-14 22:20:22 -0700 | [diff] [blame] | 1 | // 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 | |
| 15 | package android |
| 16 | |
| 17 | import ( |
Patrice Arruda | 300cef9 | 2019-02-22 15:47:57 -0800 | [diff] [blame] | 18 | "path/filepath" |
Jaewoong Jung | 9aa3ab1 | 2019-04-03 15:47:29 -0700 | [diff] [blame] | 19 | "reflect" |
Tao Bao | 0ba5c94 | 2018-08-14 22:20:22 -0700 | [diff] [blame] | 20 | "testing" |
| 21 | ) |
| 22 | |
Patrice Arruda | 300cef9 | 2019-02-22 15:47:57 -0800 | [diff] [blame] | 23 | func testPrebuiltEtc(t *testing.T, bp string) (*TestContext, Config) { |
Colin Cross | 2ffb9a8 | 2019-06-10 14:32:38 -0700 | [diff] [blame] | 24 | config := TestArchConfig(buildDir, nil) |
Tao Bao | 0ba5c94 | 2018-08-14 22:20:22 -0700 | [diff] [blame] | 25 | ctx := NewTestArchContext() |
Colin Cross | 4b49b76 | 2019-11-22 15:25:03 -0800 | [diff] [blame] | 26 | ctx.RegisterModuleType("prebuilt_etc", PrebuiltEtcFactory) |
| 27 | ctx.RegisterModuleType("prebuilt_etc_host", PrebuiltEtcHostFactory) |
| 28 | ctx.RegisterModuleType("prebuilt_usr_share", PrebuiltUserShareFactory) |
| 29 | ctx.RegisterModuleType("prebuilt_usr_share_host", PrebuiltUserShareHostFactory) |
| 30 | ctx.RegisterModuleType("prebuilt_font", PrebuiltFontFactory) |
| 31 | ctx.RegisterModuleType("prebuilt_firmware", PrebuiltFirmwareFactory) |
Tao Bao | 0ba5c94 | 2018-08-14 22:20:22 -0700 | [diff] [blame] | 32 | ctx.Register() |
| 33 | mockFiles := map[string][]byte{ |
| 34 | "Android.bp": []byte(bp), |
| 35 | "foo.conf": nil, |
| 36 | "bar.conf": nil, |
| 37 | "baz.conf": nil, |
| 38 | } |
| 39 | ctx.MockFileSystem(mockFiles) |
| 40 | _, errs := ctx.ParseFileList(".", []string{"Android.bp"}) |
| 41 | FailIfErrored(t, errs) |
| 42 | _, errs = ctx.PrepareBuildActions(config) |
| 43 | FailIfErrored(t, errs) |
| 44 | |
Patrice Arruda | 300cef9 | 2019-02-22 15:47:57 -0800 | [diff] [blame] | 45 | return ctx, config |
Tao Bao | 0ba5c94 | 2018-08-14 22:20:22 -0700 | [diff] [blame] | 46 | } |
| 47 | |
Tao Bao | 0ba5c94 | 2018-08-14 22:20:22 -0700 | [diff] [blame] | 48 | func TestPrebuiltEtcVariants(t *testing.T) { |
Patrice Arruda | 300cef9 | 2019-02-22 15:47:57 -0800 | [diff] [blame] | 49 | ctx, _ := testPrebuiltEtc(t, ` |
Tao Bao | 0ba5c94 | 2018-08-14 22:20:22 -0700 | [diff] [blame] | 50 | prebuilt_etc { |
| 51 | name: "foo.conf", |
| 52 | src: "foo.conf", |
| 53 | } |
| 54 | prebuilt_etc { |
| 55 | name: "bar.conf", |
| 56 | src: "bar.conf", |
| 57 | recovery_available: true, |
| 58 | } |
| 59 | prebuilt_etc { |
| 60 | name: "baz.conf", |
| 61 | src: "baz.conf", |
| 62 | recovery: true, |
| 63 | } |
| 64 | `) |
| 65 | |
| 66 | foo_variants := ctx.ModuleVariantsForTests("foo.conf") |
| 67 | if len(foo_variants) != 1 { |
| 68 | t.Errorf("expected 1, got %#v", foo_variants) |
| 69 | } |
| 70 | |
| 71 | bar_variants := ctx.ModuleVariantsForTests("bar.conf") |
| 72 | if len(bar_variants) != 2 { |
| 73 | t.Errorf("expected 2, got %#v", bar_variants) |
| 74 | } |
| 75 | |
| 76 | baz_variants := ctx.ModuleVariantsForTests("baz.conf") |
| 77 | if len(baz_variants) != 1 { |
| 78 | t.Errorf("expected 1, got %#v", bar_variants) |
| 79 | } |
| 80 | } |
Jiyong Park | 139a2e6 | 2018-10-26 21:49:39 +0900 | [diff] [blame] | 81 | |
| 82 | func TestPrebuiltEtcOutputPath(t *testing.T) { |
Patrice Arruda | 300cef9 | 2019-02-22 15:47:57 -0800 | [diff] [blame] | 83 | ctx, _ := testPrebuiltEtc(t, ` |
Jiyong Park | 139a2e6 | 2018-10-26 21:49:39 +0900 | [diff] [blame] | 84 | prebuilt_etc { |
| 85 | name: "foo.conf", |
| 86 | src: "foo.conf", |
| 87 | filename: "foo.installed.conf", |
| 88 | } |
| 89 | `) |
| 90 | |
Colin Cross | 7113d20 | 2019-11-20 16:39:12 -0800 | [diff] [blame] | 91 | p := ctx.ModuleForTests("foo.conf", "android_arm64_armv8-a").Module().(*PrebuiltEtc) |
Jiyong Park | 139a2e6 | 2018-10-26 21:49:39 +0900 | [diff] [blame] | 92 | if p.outputFilePath.Base() != "foo.installed.conf" { |
| 93 | t.Errorf("expected foo.installed.conf, got %q", p.outputFilePath.Base()) |
| 94 | } |
| 95 | } |
Jiyong Park | 1a7cf08 | 2018-11-13 11:59:12 +0900 | [diff] [blame] | 96 | |
| 97 | func TestPrebuiltEtcGlob(t *testing.T) { |
Patrice Arruda | 300cef9 | 2019-02-22 15:47:57 -0800 | [diff] [blame] | 98 | ctx, _ := testPrebuiltEtc(t, ` |
Jiyong Park | 1a7cf08 | 2018-11-13 11:59:12 +0900 | [diff] [blame] | 99 | prebuilt_etc { |
| 100 | name: "my_foo", |
| 101 | src: "foo.*", |
| 102 | } |
| 103 | prebuilt_etc { |
| 104 | name: "my_bar", |
| 105 | src: "bar.*", |
| 106 | filename_from_src: true, |
| 107 | } |
| 108 | `) |
| 109 | |
Colin Cross | 7113d20 | 2019-11-20 16:39:12 -0800 | [diff] [blame] | 110 | p := ctx.ModuleForTests("my_foo", "android_arm64_armv8-a").Module().(*PrebuiltEtc) |
Jiyong Park | 1a7cf08 | 2018-11-13 11:59:12 +0900 | [diff] [blame] | 111 | if p.outputFilePath.Base() != "my_foo" { |
| 112 | t.Errorf("expected my_foo, got %q", p.outputFilePath.Base()) |
| 113 | } |
| 114 | |
Colin Cross | 7113d20 | 2019-11-20 16:39:12 -0800 | [diff] [blame] | 115 | p = ctx.ModuleForTests("my_bar", "android_arm64_armv8-a").Module().(*PrebuiltEtc) |
Jiyong Park | 1a7cf08 | 2018-11-13 11:59:12 +0900 | [diff] [blame] | 116 | if p.outputFilePath.Base() != "bar.conf" { |
| 117 | t.Errorf("expected bar.conf, got %q", p.outputFilePath.Base()) |
| 118 | } |
| 119 | } |
Anton Hansson | ce0e258 | 2019-02-04 14:19:27 +0000 | [diff] [blame] | 120 | |
| 121 | func TestPrebuiltEtcAndroidMk(t *testing.T) { |
Jaewoong Jung | 9aa3ab1 | 2019-04-03 15:47:29 -0700 | [diff] [blame] | 122 | ctx, config := testPrebuiltEtc(t, ` |
Anton Hansson | ce0e258 | 2019-02-04 14:19:27 +0000 | [diff] [blame] | 123 | prebuilt_etc { |
| 124 | name: "foo", |
| 125 | src: "foo.conf", |
| 126 | owner: "abc", |
| 127 | filename_from_src: true, |
Jaewoong Jung | 9aa3ab1 | 2019-04-03 15:47:29 -0700 | [diff] [blame] | 128 | required: ["modA", "moduleB"], |
| 129 | host_required: ["hostModA", "hostModB"], |
| 130 | target_required: ["targetModA"], |
Anton Hansson | ce0e258 | 2019-02-04 14:19:27 +0000 | [diff] [blame] | 131 | } |
| 132 | `) |
| 133 | |
Jaewoong Jung | 9aa3ab1 | 2019-04-03 15:47:29 -0700 | [diff] [blame] | 134 | expected := map[string][]string{ |
| 135 | "LOCAL_MODULE": {"foo"}, |
| 136 | "LOCAL_MODULE_CLASS": {"ETC"}, |
| 137 | "LOCAL_MODULE_OWNER": {"abc"}, |
| 138 | "LOCAL_INSTALLED_MODULE_STEM": {"foo.conf"}, |
| 139 | "LOCAL_REQUIRED_MODULES": {"modA", "moduleB"}, |
| 140 | "LOCAL_HOST_REQUIRED_MODULES": {"hostModA", "hostModB"}, |
| 141 | "LOCAL_TARGET_REQUIRED_MODULES": {"targetModA"}, |
Anton Hansson | ce0e258 | 2019-02-04 14:19:27 +0000 | [diff] [blame] | 142 | } |
| 143 | |
Colin Cross | 7113d20 | 2019-11-20 16:39:12 -0800 | [diff] [blame] | 144 | mod := ctx.ModuleForTests("foo", "android_arm64_armv8-a").Module().(*PrebuiltEtc) |
Jiyong Park | 0b0e1b9 | 2019-12-03 13:24:29 +0900 | [diff] [blame] | 145 | entries := AndroidMkEntriesForTest(t, config, "", mod)[0] |
Jaewoong Jung | 9aa3ab1 | 2019-04-03 15:47:29 -0700 | [diff] [blame] | 146 | for k, expectedValue := range expected { |
| 147 | if value, ok := entries.EntryMap[k]; ok { |
| 148 | if !reflect.DeepEqual(value, expectedValue) { |
| 149 | t.Errorf("Incorrect %s '%s', expected '%s'", k, value, expectedValue) |
Anton Hansson | ce0e258 | 2019-02-04 14:19:27 +0000 | [diff] [blame] | 150 | } |
Jaewoong Jung | 9aa3ab1 | 2019-04-03 15:47:29 -0700 | [diff] [blame] | 151 | } else { |
| 152 | t.Errorf("No %s defined, saw %q", k, entries.EntryMap) |
Anton Hansson | ce0e258 | 2019-02-04 14:19:27 +0000 | [diff] [blame] | 153 | } |
| 154 | } |
| 155 | } |
Jaewoong Jung | 2478818 | 2019-02-04 14:34:10 -0800 | [diff] [blame] | 156 | |
| 157 | func TestPrebuiltEtcHost(t *testing.T) { |
Patrice Arruda | 300cef9 | 2019-02-22 15:47:57 -0800 | [diff] [blame] | 158 | ctx, _ := testPrebuiltEtc(t, ` |
Jaewoong Jung | 2478818 | 2019-02-04 14:34:10 -0800 | [diff] [blame] | 159 | prebuilt_etc_host { |
| 160 | name: "foo.conf", |
| 161 | src: "foo.conf", |
| 162 | } |
| 163 | `) |
| 164 | |
| 165 | buildOS := BuildOs.String() |
| 166 | p := ctx.ModuleForTests("foo.conf", buildOS+"_common").Module().(*PrebuiltEtc) |
| 167 | if !p.Host() { |
| 168 | t.Errorf("host bit is not set for a prebuilt_etc_host module.") |
| 169 | } |
| 170 | } |
Jaewoong Jung | c3fcdb4 | 2019-02-13 05:50:33 -0800 | [diff] [blame] | 171 | |
| 172 | func TestPrebuiltUserShareInstallDirPath(t *testing.T) { |
Patrice Arruda | 300cef9 | 2019-02-22 15:47:57 -0800 | [diff] [blame] | 173 | ctx, _ := testPrebuiltEtc(t, ` |
Jaewoong Jung | c3fcdb4 | 2019-02-13 05:50:33 -0800 | [diff] [blame] | 174 | prebuilt_usr_share { |
| 175 | name: "foo.conf", |
| 176 | src: "foo.conf", |
| 177 | sub_dir: "bar", |
| 178 | } |
| 179 | `) |
| 180 | |
Colin Cross | 7113d20 | 2019-11-20 16:39:12 -0800 | [diff] [blame] | 181 | p := ctx.ModuleForTests("foo.conf", "android_arm64_armv8-a").Module().(*PrebuiltEtc) |
Colin Cross | ff6c33d | 2019-10-02 16:01:35 -0700 | [diff] [blame] | 182 | expected := buildDir + "/target/product/test_device/system/usr/share/bar" |
| 183 | if p.installDirPath.String() != expected { |
| 184 | t.Errorf("expected %q, got %q", expected, p.installDirPath.String()) |
Jaewoong Jung | c3fcdb4 | 2019-02-13 05:50:33 -0800 | [diff] [blame] | 185 | } |
| 186 | } |
Patrice Arruda | 300cef9 | 2019-02-22 15:47:57 -0800 | [diff] [blame] | 187 | |
| 188 | func TestPrebuiltUserShareHostInstallDirPath(t *testing.T) { |
| 189 | ctx, config := testPrebuiltEtc(t, ` |
| 190 | prebuilt_usr_share_host { |
| 191 | name: "foo.conf", |
| 192 | src: "foo.conf", |
| 193 | sub_dir: "bar", |
| 194 | } |
| 195 | `) |
| 196 | |
| 197 | buildOS := BuildOs.String() |
| 198 | p := ctx.ModuleForTests("foo.conf", buildOS+"_common").Module().(*PrebuiltEtc) |
Colin Cross | ff6c33d | 2019-10-02 16:01:35 -0700 | [diff] [blame] | 199 | expected := filepath.Join(buildDir, "host", config.PrebuiltOS(), "usr", "share", "bar") |
| 200 | if p.installDirPath.String() != expected { |
| 201 | t.Errorf("expected %q, got %q", expected, p.installDirPath.String()) |
Patrice Arruda | 300cef9 | 2019-02-22 15:47:57 -0800 | [diff] [blame] | 202 | } |
| 203 | } |
Patrice Arruda | 61583eb | 2019-05-14 08:20:45 -0700 | [diff] [blame] | 204 | |
| 205 | func TestPrebuiltFontInstallDirPath(t *testing.T) { |
| 206 | ctx, _ := testPrebuiltEtc(t, ` |
| 207 | prebuilt_font { |
| 208 | name: "foo.conf", |
| 209 | src: "foo.conf", |
| 210 | } |
| 211 | `) |
| 212 | |
Colin Cross | 7113d20 | 2019-11-20 16:39:12 -0800 | [diff] [blame] | 213 | p := ctx.ModuleForTests("foo.conf", "android_arm64_armv8-a").Module().(*PrebuiltEtc) |
Colin Cross | ff6c33d | 2019-10-02 16:01:35 -0700 | [diff] [blame] | 214 | expected := buildDir + "/target/product/test_device/system/fonts" |
| 215 | if p.installDirPath.String() != expected { |
| 216 | t.Errorf("expected %q, got %q", expected, p.installDirPath.String()) |
Patrice Arruda | 61583eb | 2019-05-14 08:20:45 -0700 | [diff] [blame] | 217 | } |
| 218 | } |
Patrice Arruda | 057a8b1 | 2019-06-03 15:29:27 -0700 | [diff] [blame] | 219 | |
| 220 | func TestPrebuiltFirmwareDirPath(t *testing.T) { |
Colin Cross | ff6c33d | 2019-10-02 16:01:35 -0700 | [diff] [blame] | 221 | targetPath := buildDir + "/target/product/test_device" |
Patrice Arruda | 057a8b1 | 2019-06-03 15:29:27 -0700 | [diff] [blame] | 222 | tests := []struct { |
| 223 | description string |
| 224 | config string |
| 225 | expectedPath string |
| 226 | }{{ |
| 227 | description: "prebuilt: system firmware", |
| 228 | config: ` |
| 229 | prebuilt_firmware { |
| 230 | name: "foo.conf", |
| 231 | src: "foo.conf", |
| 232 | }`, |
| 233 | expectedPath: filepath.Join(targetPath, "system/etc/firmware"), |
| 234 | }, { |
| 235 | description: "prebuilt: vendor firmware", |
| 236 | config: ` |
| 237 | prebuilt_firmware { |
| 238 | name: "foo.conf", |
| 239 | src: "foo.conf", |
| 240 | soc_specific: true, |
| 241 | sub_dir: "sub_dir", |
| 242 | }`, |
| 243 | expectedPath: filepath.Join(targetPath, "vendor/firmware/sub_dir"), |
| 244 | }} |
| 245 | for _, tt := range tests { |
| 246 | t.Run(tt.description, func(t *testing.T) { |
| 247 | ctx, _ := testPrebuiltEtc(t, tt.config) |
Colin Cross | 7113d20 | 2019-11-20 16:39:12 -0800 | [diff] [blame] | 248 | p := ctx.ModuleForTests("foo.conf", "android_arm64_armv8-a").Module().(*PrebuiltEtc) |
Colin Cross | ff6c33d | 2019-10-02 16:01:35 -0700 | [diff] [blame] | 249 | if p.installDirPath.String() != tt.expectedPath { |
Patrice Arruda | 057a8b1 | 2019-06-03 15:29:27 -0700 | [diff] [blame] | 250 | t.Errorf("expected %q, got %q", tt.expectedPath, p.installDirPath) |
| 251 | } |
| 252 | }) |
| 253 | } |
| 254 | } |