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