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