blob: 8fc36c2740ef9d6c9c98a7e1455c17754ef27432 [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
Liz Kammer0449a632020-06-26 10:12:36 -070052func testPrebuiltEtcContext(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)
Liz Kammer0449a632020-06-26 10:12:36 -070070
71 return ctx, config
72}
73
74func testPrebuiltEtc(t *testing.T, bp string) (*android.TestContext, android.Config) {
75 t.Helper()
76
77 ctx, config := testPrebuiltEtcContext(t, bp)
Tao Bao0ba5c942018-08-14 22:20:22 -070078 _, errs := ctx.ParseFileList(".", []string{"Android.bp"})
Jaewoong Jung4b79e982020-06-01 10:45:49 -070079 android.FailIfErrored(t, errs)
Tao Bao0ba5c942018-08-14 22:20:22 -070080 _, errs = ctx.PrepareBuildActions(config)
Jaewoong Jung4b79e982020-06-01 10:45:49 -070081 android.FailIfErrored(t, errs)
Tao Bao0ba5c942018-08-14 22:20:22 -070082
Patrice Arruda300cef92019-02-22 15:47:57 -080083 return ctx, config
Tao Bao0ba5c942018-08-14 22:20:22 -070084}
85
Liz Kammer0449a632020-06-26 10:12:36 -070086func testPrebuiltEtcError(t *testing.T, pattern, bp string) {
87 t.Helper()
88
89 ctx, config := testPrebuiltEtcContext(t, bp)
90 _, errs := ctx.ParseFileList(".", []string{"Android.bp"})
91 if len(errs) > 0 {
92 android.FailIfNoMatchingErrors(t, pattern, errs)
93 return
94 }
95
96 _, errs = ctx.PrepareBuildActions(config)
97 if len(errs) > 0 {
98 android.FailIfNoMatchingErrors(t, pattern, errs)
99 return
100 }
101
102 t.Fatalf("missing expected error %q (0 errors are returned)", pattern)
103}
Tao Bao0ba5c942018-08-14 22:20:22 -0700104func TestPrebuiltEtcVariants(t *testing.T) {
Patrice Arruda300cef92019-02-22 15:47:57 -0800105 ctx, _ := testPrebuiltEtc(t, `
Tao Bao0ba5c942018-08-14 22:20:22 -0700106 prebuilt_etc {
107 name: "foo.conf",
108 src: "foo.conf",
109 }
110 prebuilt_etc {
111 name: "bar.conf",
112 src: "bar.conf",
113 recovery_available: true,
114 }
115 prebuilt_etc {
116 name: "baz.conf",
117 src: "baz.conf",
118 recovery: true,
119 }
120 `)
121
122 foo_variants := ctx.ModuleVariantsForTests("foo.conf")
123 if len(foo_variants) != 1 {
124 t.Errorf("expected 1, got %#v", foo_variants)
125 }
126
127 bar_variants := ctx.ModuleVariantsForTests("bar.conf")
128 if len(bar_variants) != 2 {
129 t.Errorf("expected 2, got %#v", bar_variants)
130 }
131
132 baz_variants := ctx.ModuleVariantsForTests("baz.conf")
133 if len(baz_variants) != 1 {
134 t.Errorf("expected 1, got %#v", bar_variants)
135 }
136}
Jiyong Park139a2e62018-10-26 21:49:39 +0900137
138func TestPrebuiltEtcOutputPath(t *testing.T) {
Patrice Arruda300cef92019-02-22 15:47:57 -0800139 ctx, _ := testPrebuiltEtc(t, `
Jiyong Park139a2e62018-10-26 21:49:39 +0900140 prebuilt_etc {
141 name: "foo.conf",
142 src: "foo.conf",
143 filename: "foo.installed.conf",
144 }
145 `)
146
Colin Cross7113d202019-11-20 16:39:12 -0800147 p := ctx.ModuleForTests("foo.conf", "android_arm64_armv8-a").Module().(*PrebuiltEtc)
Jiyong Park139a2e62018-10-26 21:49:39 +0900148 if p.outputFilePath.Base() != "foo.installed.conf" {
149 t.Errorf("expected foo.installed.conf, got %q", p.outputFilePath.Base())
150 }
151}
Jiyong Park1a7cf082018-11-13 11:59:12 +0900152
153func TestPrebuiltEtcGlob(t *testing.T) {
Patrice Arruda300cef92019-02-22 15:47:57 -0800154 ctx, _ := testPrebuiltEtc(t, `
Jiyong Park1a7cf082018-11-13 11:59:12 +0900155 prebuilt_etc {
156 name: "my_foo",
157 src: "foo.*",
158 }
159 prebuilt_etc {
160 name: "my_bar",
161 src: "bar.*",
162 filename_from_src: true,
163 }
164 `)
165
Colin Cross7113d202019-11-20 16:39:12 -0800166 p := ctx.ModuleForTests("my_foo", "android_arm64_armv8-a").Module().(*PrebuiltEtc)
Jiyong Park1a7cf082018-11-13 11:59:12 +0900167 if p.outputFilePath.Base() != "my_foo" {
168 t.Errorf("expected my_foo, got %q", p.outputFilePath.Base())
169 }
170
Colin Cross7113d202019-11-20 16:39:12 -0800171 p = ctx.ModuleForTests("my_bar", "android_arm64_armv8-a").Module().(*PrebuiltEtc)
Jiyong Park1a7cf082018-11-13 11:59:12 +0900172 if p.outputFilePath.Base() != "bar.conf" {
173 t.Errorf("expected bar.conf, got %q", p.outputFilePath.Base())
174 }
175}
Anton Hanssonce0e2582019-02-04 14:19:27 +0000176
177func TestPrebuiltEtcAndroidMk(t *testing.T) {
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -0700178 ctx, config := testPrebuiltEtc(t, `
Anton Hanssonce0e2582019-02-04 14:19:27 +0000179 prebuilt_etc {
180 name: "foo",
181 src: "foo.conf",
182 owner: "abc",
183 filename_from_src: true,
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -0700184 required: ["modA", "moduleB"],
185 host_required: ["hostModA", "hostModB"],
186 target_required: ["targetModA"],
Anton Hanssonce0e2582019-02-04 14:19:27 +0000187 }
188 `)
189
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -0700190 expected := map[string][]string{
191 "LOCAL_MODULE": {"foo"},
192 "LOCAL_MODULE_CLASS": {"ETC"},
193 "LOCAL_MODULE_OWNER": {"abc"},
194 "LOCAL_INSTALLED_MODULE_STEM": {"foo.conf"},
195 "LOCAL_REQUIRED_MODULES": {"modA", "moduleB"},
196 "LOCAL_HOST_REQUIRED_MODULES": {"hostModA", "hostModB"},
197 "LOCAL_TARGET_REQUIRED_MODULES": {"targetModA"},
Anton Hanssonce0e2582019-02-04 14:19:27 +0000198 }
199
Colin Cross7113d202019-11-20 16:39:12 -0800200 mod := ctx.ModuleForTests("foo", "android_arm64_armv8-a").Module().(*PrebuiltEtc)
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700201 entries := android.AndroidMkEntriesForTest(t, config, "", mod)[0]
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -0700202 for k, expectedValue := range expected {
203 if value, ok := entries.EntryMap[k]; ok {
204 if !reflect.DeepEqual(value, expectedValue) {
205 t.Errorf("Incorrect %s '%s', expected '%s'", k, value, expectedValue)
Anton Hanssonce0e2582019-02-04 14:19:27 +0000206 }
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -0700207 } else {
208 t.Errorf("No %s defined, saw %q", k, entries.EntryMap)
Anton Hanssonce0e2582019-02-04 14:19:27 +0000209 }
210 }
211}
Jaewoong Jung24788182019-02-04 14:34:10 -0800212
Liz Kammer0449a632020-06-26 10:12:36 -0700213func TestPrebuiltEtcRelativeInstallPathInstallDirPath(t *testing.T) {
214 ctx, _ := testPrebuiltEtc(t, `
215 prebuilt_etc {
216 name: "foo.conf",
217 src: "foo.conf",
218 relative_install_path: "bar",
219 }
220 `)
221
222 p := ctx.ModuleForTests("foo.conf", "android_arm64_armv8-a").Module().(*PrebuiltEtc)
223 expected := buildDir + "/target/product/test_device/system/etc/bar"
224 if p.installDirPath.String() != expected {
225 t.Errorf("expected %q, got %q", expected, p.installDirPath.String())
226 }
227}
228
229func TestPrebuiltEtcCannotSetRelativeInstallPathAndSubDir(t *testing.T) {
230 testPrebuiltEtcError(t, "relative_install_path is set. Cannot set sub_dir", `
231 prebuilt_etc {
232 name: "foo.conf",
233 src: "foo.conf",
234 sub_dir: "bar",
235 relative_install_path: "bar",
236 }
237 `)
238}
239
Jaewoong Jung24788182019-02-04 14:34:10 -0800240func TestPrebuiltEtcHost(t *testing.T) {
Patrice Arruda300cef92019-02-22 15:47:57 -0800241 ctx, _ := testPrebuiltEtc(t, `
Jaewoong Jung24788182019-02-04 14:34:10 -0800242 prebuilt_etc_host {
243 name: "foo.conf",
244 src: "foo.conf",
245 }
246 `)
247
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700248 buildOS := android.BuildOs.String()
Jaewoong Jung24788182019-02-04 14:34:10 -0800249 p := ctx.ModuleForTests("foo.conf", buildOS+"_common").Module().(*PrebuiltEtc)
250 if !p.Host() {
251 t.Errorf("host bit is not set for a prebuilt_etc_host module.")
252 }
253}
Jaewoong Jungc3fcdb42019-02-13 05:50:33 -0800254
255func TestPrebuiltUserShareInstallDirPath(t *testing.T) {
Patrice Arruda300cef92019-02-22 15:47:57 -0800256 ctx, _ := testPrebuiltEtc(t, `
Jaewoong Jungc3fcdb42019-02-13 05:50:33 -0800257 prebuilt_usr_share {
258 name: "foo.conf",
259 src: "foo.conf",
260 sub_dir: "bar",
261 }
262 `)
263
Colin Cross7113d202019-11-20 16:39:12 -0800264 p := ctx.ModuleForTests("foo.conf", "android_arm64_armv8-a").Module().(*PrebuiltEtc)
Colin Crossff6c33d2019-10-02 16:01:35 -0700265 expected := buildDir + "/target/product/test_device/system/usr/share/bar"
266 if p.installDirPath.String() != expected {
267 t.Errorf("expected %q, got %q", expected, p.installDirPath.String())
Jaewoong Jungc3fcdb42019-02-13 05:50:33 -0800268 }
269}
Patrice Arruda300cef92019-02-22 15:47:57 -0800270
271func TestPrebuiltUserShareHostInstallDirPath(t *testing.T) {
272 ctx, config := testPrebuiltEtc(t, `
273 prebuilt_usr_share_host {
274 name: "foo.conf",
275 src: "foo.conf",
276 sub_dir: "bar",
277 }
278 `)
279
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700280 buildOS := android.BuildOs.String()
Patrice Arruda300cef92019-02-22 15:47:57 -0800281 p := ctx.ModuleForTests("foo.conf", buildOS+"_common").Module().(*PrebuiltEtc)
Colin Crossff6c33d2019-10-02 16:01:35 -0700282 expected := filepath.Join(buildDir, "host", config.PrebuiltOS(), "usr", "share", "bar")
283 if p.installDirPath.String() != expected {
284 t.Errorf("expected %q, got %q", expected, p.installDirPath.String())
Patrice Arruda300cef92019-02-22 15:47:57 -0800285 }
286}
Patrice Arruda61583eb2019-05-14 08:20:45 -0700287
288func TestPrebuiltFontInstallDirPath(t *testing.T) {
289 ctx, _ := testPrebuiltEtc(t, `
290 prebuilt_font {
291 name: "foo.conf",
292 src: "foo.conf",
293 }
294 `)
295
Colin Cross7113d202019-11-20 16:39:12 -0800296 p := ctx.ModuleForTests("foo.conf", "android_arm64_armv8-a").Module().(*PrebuiltEtc)
Colin Crossff6c33d2019-10-02 16:01:35 -0700297 expected := buildDir + "/target/product/test_device/system/fonts"
298 if p.installDirPath.String() != expected {
299 t.Errorf("expected %q, got %q", expected, p.installDirPath.String())
Patrice Arruda61583eb2019-05-14 08:20:45 -0700300 }
301}
Patrice Arruda057a8b12019-06-03 15:29:27 -0700302
303func TestPrebuiltFirmwareDirPath(t *testing.T) {
Colin Crossff6c33d2019-10-02 16:01:35 -0700304 targetPath := buildDir + "/target/product/test_device"
Patrice Arruda057a8b12019-06-03 15:29:27 -0700305 tests := []struct {
306 description string
307 config string
308 expectedPath string
309 }{{
310 description: "prebuilt: system firmware",
311 config: `
312 prebuilt_firmware {
313 name: "foo.conf",
314 src: "foo.conf",
315 }`,
316 expectedPath: filepath.Join(targetPath, "system/etc/firmware"),
317 }, {
318 description: "prebuilt: vendor firmware",
319 config: `
320 prebuilt_firmware {
321 name: "foo.conf",
322 src: "foo.conf",
323 soc_specific: true,
324 sub_dir: "sub_dir",
325 }`,
326 expectedPath: filepath.Join(targetPath, "vendor/firmware/sub_dir"),
327 }}
328 for _, tt := range tests {
329 t.Run(tt.description, func(t *testing.T) {
330 ctx, _ := testPrebuiltEtc(t, tt.config)
Colin Cross7113d202019-11-20 16:39:12 -0800331 p := ctx.ModuleForTests("foo.conf", "android_arm64_armv8-a").Module().(*PrebuiltEtc)
Colin Crossff6c33d2019-10-02 16:01:35 -0700332 if p.installDirPath.String() != tt.expectedPath {
Patrice Arruda057a8b12019-06-03 15:29:27 -0700333 t.Errorf("expected %q, got %q", tt.expectedPath, p.installDirPath)
334 }
335 })
336 }
337}
Patrice Arruda0f688002020-06-08 21:40:25 +0000338
339func TestPrebuiltDSPDirPath(t *testing.T) {
340 targetPath := filepath.Join(buildDir, "/target/product/test_device")
341 tests := []struct {
342 description string
343 config string
344 expectedPath string
345 }{{
346 description: "prebuilt: system dsp",
347 config: `
348 prebuilt_dsp {
349 name: "foo.conf",
350 src: "foo.conf",
351 }`,
352 expectedPath: filepath.Join(targetPath, "system/etc/dsp"),
353 }, {
354 description: "prebuilt: vendor dsp",
355 config: `
356 prebuilt_dsp {
357 name: "foo.conf",
358 src: "foo.conf",
359 soc_specific: true,
360 sub_dir: "sub_dir",
361 }`,
362 expectedPath: filepath.Join(targetPath, "vendor/dsp/sub_dir"),
363 }}
364 for _, tt := range tests {
365 t.Run(tt.description, func(t *testing.T) {
366 ctx, _ := testPrebuiltEtc(t, tt.config)
367 p := ctx.ModuleForTests("foo.conf", "android_arm64_armv8-a").Module().(*PrebuiltEtc)
368 if p.installDirPath.String() != tt.expectedPath {
369 t.Errorf("expected %q, got %q", tt.expectedPath, p.installDirPath)
370 }
371 })
372 }
373}