blob: e13cb3cb737b3467c8c4a020037011e6da46babd [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)
Colin Cross98be1bb2019-12-13 20:41:13 -080068 ctx.Register(config)
Tao Bao0ba5c942018-08-14 22:20:22 -070069 _, errs := ctx.ParseFileList(".", []string{"Android.bp"})
Jaewoong Jung4b79e982020-06-01 10:45:49 -070070 android.FailIfErrored(t, errs)
Tao Bao0ba5c942018-08-14 22:20:22 -070071 _, errs = ctx.PrepareBuildActions(config)
Jaewoong Jung4b79e982020-06-01 10:45:49 -070072 android.FailIfErrored(t, errs)
Tao Bao0ba5c942018-08-14 22:20:22 -070073
Patrice Arruda300cef92019-02-22 15:47:57 -080074 return ctx, config
Tao Bao0ba5c942018-08-14 22:20:22 -070075}
76
Tao Bao0ba5c942018-08-14 22:20:22 -070077func TestPrebuiltEtcVariants(t *testing.T) {
Patrice Arruda300cef92019-02-22 15:47:57 -080078 ctx, _ := testPrebuiltEtc(t, `
Tao Bao0ba5c942018-08-14 22:20:22 -070079 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 Park139a2e62018-10-26 21:49:39 +0900110
111func TestPrebuiltEtcOutputPath(t *testing.T) {
Patrice Arruda300cef92019-02-22 15:47:57 -0800112 ctx, _ := testPrebuiltEtc(t, `
Jiyong Park139a2e62018-10-26 21:49:39 +0900113 prebuilt_etc {
114 name: "foo.conf",
115 src: "foo.conf",
116 filename: "foo.installed.conf",
117 }
118 `)
119
Colin Cross7113d202019-11-20 16:39:12 -0800120 p := ctx.ModuleForTests("foo.conf", "android_arm64_armv8-a").Module().(*PrebuiltEtc)
Jiyong Park139a2e62018-10-26 21:49:39 +0900121 if p.outputFilePath.Base() != "foo.installed.conf" {
122 t.Errorf("expected foo.installed.conf, got %q", p.outputFilePath.Base())
123 }
124}
Jiyong Park1a7cf082018-11-13 11:59:12 +0900125
126func TestPrebuiltEtcGlob(t *testing.T) {
Patrice Arruda300cef92019-02-22 15:47:57 -0800127 ctx, _ := testPrebuiltEtc(t, `
Jiyong Park1a7cf082018-11-13 11:59:12 +0900128 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 Cross7113d202019-11-20 16:39:12 -0800139 p := ctx.ModuleForTests("my_foo", "android_arm64_armv8-a").Module().(*PrebuiltEtc)
Jiyong Park1a7cf082018-11-13 11:59:12 +0900140 if p.outputFilePath.Base() != "my_foo" {
141 t.Errorf("expected my_foo, got %q", p.outputFilePath.Base())
142 }
143
Colin Cross7113d202019-11-20 16:39:12 -0800144 p = ctx.ModuleForTests("my_bar", "android_arm64_armv8-a").Module().(*PrebuiltEtc)
Jiyong Park1a7cf082018-11-13 11:59:12 +0900145 if p.outputFilePath.Base() != "bar.conf" {
146 t.Errorf("expected bar.conf, got %q", p.outputFilePath.Base())
147 }
148}
Anton Hanssonce0e2582019-02-04 14:19:27 +0000149
150func TestPrebuiltEtcAndroidMk(t *testing.T) {
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -0700151 ctx, config := testPrebuiltEtc(t, `
Anton Hanssonce0e2582019-02-04 14:19:27 +0000152 prebuilt_etc {
153 name: "foo",
154 src: "foo.conf",
155 owner: "abc",
156 filename_from_src: true,
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -0700157 required: ["modA", "moduleB"],
158 host_required: ["hostModA", "hostModB"],
159 target_required: ["targetModA"],
Anton Hanssonce0e2582019-02-04 14:19:27 +0000160 }
161 `)
162
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -0700163 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 Hanssonce0e2582019-02-04 14:19:27 +0000171 }
172
Colin Cross7113d202019-11-20 16:39:12 -0800173 mod := ctx.ModuleForTests("foo", "android_arm64_armv8-a").Module().(*PrebuiltEtc)
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700174 entries := android.AndroidMkEntriesForTest(t, config, "", mod)[0]
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -0700175 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 Hanssonce0e2582019-02-04 14:19:27 +0000179 }
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -0700180 } else {
181 t.Errorf("No %s defined, saw %q", k, entries.EntryMap)
Anton Hanssonce0e2582019-02-04 14:19:27 +0000182 }
183 }
184}
Jaewoong Jung24788182019-02-04 14:34:10 -0800185
186func TestPrebuiltEtcHost(t *testing.T) {
Patrice Arruda300cef92019-02-22 15:47:57 -0800187 ctx, _ := testPrebuiltEtc(t, `
Jaewoong Jung24788182019-02-04 14:34:10 -0800188 prebuilt_etc_host {
189 name: "foo.conf",
190 src: "foo.conf",
191 }
192 `)
193
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700194 buildOS := android.BuildOs.String()
Jaewoong Jung24788182019-02-04 14:34:10 -0800195 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 Jungc3fcdb42019-02-13 05:50:33 -0800200
201func TestPrebuiltUserShareInstallDirPath(t *testing.T) {
Patrice Arruda300cef92019-02-22 15:47:57 -0800202 ctx, _ := testPrebuiltEtc(t, `
Jaewoong Jungc3fcdb42019-02-13 05:50:33 -0800203 prebuilt_usr_share {
204 name: "foo.conf",
205 src: "foo.conf",
206 sub_dir: "bar",
207 }
208 `)
209
Colin Cross7113d202019-11-20 16:39:12 -0800210 p := ctx.ModuleForTests("foo.conf", "android_arm64_armv8-a").Module().(*PrebuiltEtc)
Colin Crossff6c33d2019-10-02 16:01:35 -0700211 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 Jungc3fcdb42019-02-13 05:50:33 -0800214 }
215}
Patrice Arruda300cef92019-02-22 15:47:57 -0800216
217func 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 Jung4b79e982020-06-01 10:45:49 -0700226 buildOS := android.BuildOs.String()
Patrice Arruda300cef92019-02-22 15:47:57 -0800227 p := ctx.ModuleForTests("foo.conf", buildOS+"_common").Module().(*PrebuiltEtc)
Colin Crossff6c33d2019-10-02 16:01:35 -0700228 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 Arruda300cef92019-02-22 15:47:57 -0800231 }
232}
Patrice Arruda61583eb2019-05-14 08:20:45 -0700233
234func TestPrebuiltFontInstallDirPath(t *testing.T) {
235 ctx, _ := testPrebuiltEtc(t, `
236 prebuilt_font {
237 name: "foo.conf",
238 src: "foo.conf",
239 }
240 `)
241
Colin Cross7113d202019-11-20 16:39:12 -0800242 p := ctx.ModuleForTests("foo.conf", "android_arm64_armv8-a").Module().(*PrebuiltEtc)
Colin Crossff6c33d2019-10-02 16:01:35 -0700243 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 Arruda61583eb2019-05-14 08:20:45 -0700246 }
247}
Patrice Arruda057a8b12019-06-03 15:29:27 -0700248
249func TestPrebuiltFirmwareDirPath(t *testing.T) {
Colin Crossff6c33d2019-10-02 16:01:35 -0700250 targetPath := buildDir + "/target/product/test_device"
Patrice Arruda057a8b12019-06-03 15:29:27 -0700251 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 Cross7113d202019-11-20 16:39:12 -0800277 p := ctx.ModuleForTests("foo.conf", "android_arm64_armv8-a").Module().(*PrebuiltEtc)
Colin Crossff6c33d2019-10-02 16:01:35 -0700278 if p.installDirPath.String() != tt.expectedPath {
Patrice Arruda057a8b12019-06-03 15:29:27 -0700279 t.Errorf("expected %q, got %q", tt.expectedPath, p.installDirPath)
280 }
281 })
282 }
283}