blob: 4ce1984c0f2a3c687bd6c127cd5b98832e6f55af [file] [log] [blame]
Tao Bao0ba5c942018-08-14 22:20:22 -07001// 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 Jung4b79e982020-06-01 10:45:49 -070015package etc
Tao Bao0ba5c942018-08-14 22:20:22 -070016
17import (
Jaewoong Jung4b79e982020-06-01 10:45:49 -070018 "io/ioutil"
19 "os"
Patrice Arruda300cef92019-02-22 15:47:57 -080020 "path/filepath"
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -070021 "reflect"
Tao Bao0ba5c942018-08-14 22:20:22 -070022 "testing"
Jaewoong Jung4b79e982020-06-01 10:45:49 -070023
24 "android/soong/android"
Tao Bao0ba5c942018-08-14 22:20:22 -070025)
26
Jaewoong Jung4b79e982020-06-01 10:45:49 -070027var buildDir string
28
29func setUp() {
30 var err error
31 buildDir, err = ioutil.TempDir("", "soong_etc_test")
32 if err != nil {
33 panic(err)
34 }
35}
36
37func tearDown() {
38 os.RemoveAll(buildDir)
39}
40
41func 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
52func testPrebuiltEtc(t *testing.T, bp string) (*android.TestContext, android.Config) {
Colin Cross98be1bb2019-12-13 20:41:13 -080053 fs := map[string][]byte{
54 "foo.conf": nil,
55 "bar.conf": nil,
56 "baz.conf": nil,
57 }
58
Jaewoong Jung4b79e982020-06-01 10:45:49 -070059 config := android.TestArchConfig(buildDir, nil, bp, fs)
Colin Cross98be1bb2019-12-13 20:41:13 -080060
Jaewoong Jung4b79e982020-06-01 10:45:49 -070061 ctx := android.NewTestArchContext()
Colin Cross4b49b762019-11-22 15:25:03 -080062 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 Arruda0f688002020-06-08 21:40:25 +000068 ctx.RegisterModuleType("prebuilt_dsp", PrebuiltDSPFactory)
Colin Cross98be1bb2019-12-13 20:41:13 -080069 ctx.Register(config)
Tao Bao0ba5c942018-08-14 22:20:22 -070070 _, errs := ctx.ParseFileList(".", []string{"Android.bp"})
Jaewoong Jung4b79e982020-06-01 10:45:49 -070071 android.FailIfErrored(t, errs)
Tao Bao0ba5c942018-08-14 22:20:22 -070072 _, errs = ctx.PrepareBuildActions(config)
Jaewoong Jung4b79e982020-06-01 10:45:49 -070073 android.FailIfErrored(t, errs)
Tao Bao0ba5c942018-08-14 22:20:22 -070074
Patrice Arruda300cef92019-02-22 15:47:57 -080075 return ctx, config
Tao Bao0ba5c942018-08-14 22:20:22 -070076}
77
Tao Bao0ba5c942018-08-14 22:20:22 -070078func TestPrebuiltEtcVariants(t *testing.T) {
Patrice Arruda300cef92019-02-22 15:47:57 -080079 ctx, _ := testPrebuiltEtc(t, `
Tao Bao0ba5c942018-08-14 22:20:22 -070080 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 Park139a2e62018-10-26 21:49:39 +0900111
112func TestPrebuiltEtcOutputPath(t *testing.T) {
Patrice Arruda300cef92019-02-22 15:47:57 -0800113 ctx, _ := testPrebuiltEtc(t, `
Jiyong Park139a2e62018-10-26 21:49:39 +0900114 prebuilt_etc {
115 name: "foo.conf",
116 src: "foo.conf",
117 filename: "foo.installed.conf",
118 }
119 `)
120
Colin Cross7113d202019-11-20 16:39:12 -0800121 p := ctx.ModuleForTests("foo.conf", "android_arm64_armv8-a").Module().(*PrebuiltEtc)
Jiyong Park139a2e62018-10-26 21:49:39 +0900122 if p.outputFilePath.Base() != "foo.installed.conf" {
123 t.Errorf("expected foo.installed.conf, got %q", p.outputFilePath.Base())
124 }
125}
Jiyong Park1a7cf082018-11-13 11:59:12 +0900126
127func TestPrebuiltEtcGlob(t *testing.T) {
Patrice Arruda300cef92019-02-22 15:47:57 -0800128 ctx, _ := testPrebuiltEtc(t, `
Jiyong Park1a7cf082018-11-13 11:59:12 +0900129 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 Cross7113d202019-11-20 16:39:12 -0800140 p := ctx.ModuleForTests("my_foo", "android_arm64_armv8-a").Module().(*PrebuiltEtc)
Jiyong Park1a7cf082018-11-13 11:59:12 +0900141 if p.outputFilePath.Base() != "my_foo" {
142 t.Errorf("expected my_foo, got %q", p.outputFilePath.Base())
143 }
144
Colin Cross7113d202019-11-20 16:39:12 -0800145 p = ctx.ModuleForTests("my_bar", "android_arm64_armv8-a").Module().(*PrebuiltEtc)
Jiyong Park1a7cf082018-11-13 11:59:12 +0900146 if p.outputFilePath.Base() != "bar.conf" {
147 t.Errorf("expected bar.conf, got %q", p.outputFilePath.Base())
148 }
149}
Anton Hanssonce0e2582019-02-04 14:19:27 +0000150
151func TestPrebuiltEtcAndroidMk(t *testing.T) {
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -0700152 ctx, config := testPrebuiltEtc(t, `
Anton Hanssonce0e2582019-02-04 14:19:27 +0000153 prebuilt_etc {
154 name: "foo",
155 src: "foo.conf",
156 owner: "abc",
157 filename_from_src: true,
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -0700158 required: ["modA", "moduleB"],
159 host_required: ["hostModA", "hostModB"],
160 target_required: ["targetModA"],
Anton Hanssonce0e2582019-02-04 14:19:27 +0000161 }
162 `)
163
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -0700164 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 Hanssonce0e2582019-02-04 14:19:27 +0000172 }
173
Colin Cross7113d202019-11-20 16:39:12 -0800174 mod := ctx.ModuleForTests("foo", "android_arm64_armv8-a").Module().(*PrebuiltEtc)
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700175 entries := android.AndroidMkEntriesForTest(t, config, "", mod)[0]
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -0700176 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 Hanssonce0e2582019-02-04 14:19:27 +0000180 }
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -0700181 } else {
182 t.Errorf("No %s defined, saw %q", k, entries.EntryMap)
Anton Hanssonce0e2582019-02-04 14:19:27 +0000183 }
184 }
185}
Jaewoong Jung24788182019-02-04 14:34:10 -0800186
187func TestPrebuiltEtcHost(t *testing.T) {
Patrice Arruda300cef92019-02-22 15:47:57 -0800188 ctx, _ := testPrebuiltEtc(t, `
Jaewoong Jung24788182019-02-04 14:34:10 -0800189 prebuilt_etc_host {
190 name: "foo.conf",
191 src: "foo.conf",
192 }
193 `)
194
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700195 buildOS := android.BuildOs.String()
Jaewoong Jung24788182019-02-04 14:34:10 -0800196 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 Jungc3fcdb42019-02-13 05:50:33 -0800201
202func TestPrebuiltUserShareInstallDirPath(t *testing.T) {
Patrice Arruda300cef92019-02-22 15:47:57 -0800203 ctx, _ := testPrebuiltEtc(t, `
Jaewoong Jungc3fcdb42019-02-13 05:50:33 -0800204 prebuilt_usr_share {
205 name: "foo.conf",
206 src: "foo.conf",
207 sub_dir: "bar",
208 }
209 `)
210
Colin Cross7113d202019-11-20 16:39:12 -0800211 p := ctx.ModuleForTests("foo.conf", "android_arm64_armv8-a").Module().(*PrebuiltEtc)
Colin Crossff6c33d2019-10-02 16:01:35 -0700212 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 Jungc3fcdb42019-02-13 05:50:33 -0800215 }
216}
Patrice Arruda300cef92019-02-22 15:47:57 -0800217
218func 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 Jung4b79e982020-06-01 10:45:49 -0700227 buildOS := android.BuildOs.String()
Patrice Arruda300cef92019-02-22 15:47:57 -0800228 p := ctx.ModuleForTests("foo.conf", buildOS+"_common").Module().(*PrebuiltEtc)
Colin Crossff6c33d2019-10-02 16:01:35 -0700229 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 Arruda300cef92019-02-22 15:47:57 -0800232 }
233}
Patrice Arruda61583eb2019-05-14 08:20:45 -0700234
235func TestPrebuiltFontInstallDirPath(t *testing.T) {
236 ctx, _ := testPrebuiltEtc(t, `
237 prebuilt_font {
238 name: "foo.conf",
239 src: "foo.conf",
240 }
241 `)
242
Colin Cross7113d202019-11-20 16:39:12 -0800243 p := ctx.ModuleForTests("foo.conf", "android_arm64_armv8-a").Module().(*PrebuiltEtc)
Colin Crossff6c33d2019-10-02 16:01:35 -0700244 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 Arruda61583eb2019-05-14 08:20:45 -0700247 }
248}
Patrice Arruda057a8b12019-06-03 15:29:27 -0700249
250func TestPrebuiltFirmwareDirPath(t *testing.T) {
Colin Crossff6c33d2019-10-02 16:01:35 -0700251 targetPath := buildDir + "/target/product/test_device"
Patrice Arruda057a8b12019-06-03 15:29:27 -0700252 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 Cross7113d202019-11-20 16:39:12 -0800278 p := ctx.ModuleForTests("foo.conf", "android_arm64_armv8-a").Module().(*PrebuiltEtc)
Colin Crossff6c33d2019-10-02 16:01:35 -0700279 if p.installDirPath.String() != tt.expectedPath {
Patrice Arruda057a8b12019-06-03 15:29:27 -0700280 t.Errorf("expected %q, got %q", tt.expectedPath, p.installDirPath)
281 }
282 })
283 }
284}
Patrice Arruda0f688002020-06-08 21:40:25 +0000285
286func 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}