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