blob: f800c48f241f3069b18d34775798926d30d44cb6 [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 "os"
Patrice Arruda300cef92019-02-22 15:47:57 -080019 "path/filepath"
Tao Bao0ba5c942018-08-14 22:20:22 -070020 "testing"
Jaewoong Jung4b79e982020-06-01 10:45:49 -070021
22 "android/soong/android"
Tao Bao0ba5c942018-08-14 22:20:22 -070023)
24
Jaewoong Jung4b79e982020-06-01 10:45:49 -070025func TestMain(m *testing.M) {
Paul Duffin5f9f7712021-03-15 15:35:49 +000026 os.Exit(m.Run())
Jaewoong Jung4b79e982020-06-01 10:45:49 -070027}
28
Paul Duffin1172fed2021-03-08 11:28:18 +000029var prebuiltEtcFixtureFactory = android.NewFixtureFactory(
Paul Duffin5f9f7712021-03-15 15:35:49 +000030 nil,
Paul Duffin1172fed2021-03-08 11:28:18 +000031 android.PrepareForTestWithArchMutator,
32 PrepareForTestWithPrebuiltEtc,
33 android.FixtureMergeMockFs(android.MockFS{
Colin Cross98be1bb2019-12-13 20:41:13 -080034 "foo.conf": nil,
35 "bar.conf": nil,
36 "baz.conf": nil,
Paul Duffin1172fed2021-03-08 11:28:18 +000037 }),
38)
Colin Cross98be1bb2019-12-13 20:41:13 -080039
Tao Bao0ba5c942018-08-14 22:20:22 -070040func TestPrebuiltEtcVariants(t *testing.T) {
Paul Duffin921fac72021-03-10 09:00:58 +000041 result := prebuiltEtcFixtureFactory.RunTestWithBp(t, `
Tao Bao0ba5c942018-08-14 22:20:22 -070042 prebuilt_etc {
43 name: "foo.conf",
44 src: "foo.conf",
45 }
46 prebuilt_etc {
47 name: "bar.conf",
48 src: "bar.conf",
49 recovery_available: true,
50 }
51 prebuilt_etc {
52 name: "baz.conf",
53 src: "baz.conf",
54 recovery: true,
55 }
56 `)
57
Paul Duffin921fac72021-03-10 09:00:58 +000058 foo_variants := result.ModuleVariantsForTests("foo.conf")
Tao Bao0ba5c942018-08-14 22:20:22 -070059 if len(foo_variants) != 1 {
60 t.Errorf("expected 1, got %#v", foo_variants)
61 }
62
Paul Duffin921fac72021-03-10 09:00:58 +000063 bar_variants := result.ModuleVariantsForTests("bar.conf")
Tao Bao0ba5c942018-08-14 22:20:22 -070064 if len(bar_variants) != 2 {
65 t.Errorf("expected 2, got %#v", bar_variants)
66 }
67
Paul Duffin921fac72021-03-10 09:00:58 +000068 baz_variants := result.ModuleVariantsForTests("baz.conf")
Tao Bao0ba5c942018-08-14 22:20:22 -070069 if len(baz_variants) != 1 {
70 t.Errorf("expected 1, got %#v", bar_variants)
71 }
72}
Jiyong Park139a2e62018-10-26 21:49:39 +090073
74func TestPrebuiltEtcOutputPath(t *testing.T) {
Paul Duffin921fac72021-03-10 09:00:58 +000075 result := prebuiltEtcFixtureFactory.RunTestWithBp(t, `
Jiyong Park139a2e62018-10-26 21:49:39 +090076 prebuilt_etc {
77 name: "foo.conf",
78 src: "foo.conf",
79 filename: "foo.installed.conf",
80 }
81 `)
82
Paul Duffin921fac72021-03-10 09:00:58 +000083 p := result.Module("foo.conf", "android_arm64_armv8-a").(*PrebuiltEtc)
Paul Duffine84b1332021-03-12 11:59:43 +000084 android.AssertStringEquals(t, "output file path", "foo.installed.conf", p.outputFilePath.Base())
Jiyong Park139a2e62018-10-26 21:49:39 +090085}
Jiyong Park1a7cf082018-11-13 11:59:12 +090086
87func TestPrebuiltEtcGlob(t *testing.T) {
Paul Duffin921fac72021-03-10 09:00:58 +000088 result := prebuiltEtcFixtureFactory.RunTestWithBp(t, `
Jiyong Park1a7cf082018-11-13 11:59:12 +090089 prebuilt_etc {
90 name: "my_foo",
91 src: "foo.*",
92 }
93 prebuilt_etc {
94 name: "my_bar",
95 src: "bar.*",
96 filename_from_src: true,
97 }
98 `)
99
Paul Duffin921fac72021-03-10 09:00:58 +0000100 p := result.Module("my_foo", "android_arm64_armv8-a").(*PrebuiltEtc)
Paul Duffine84b1332021-03-12 11:59:43 +0000101 android.AssertStringEquals(t, "my_foo output file path", "my_foo", p.outputFilePath.Base())
Jiyong Park1a7cf082018-11-13 11:59:12 +0900102
Paul Duffin921fac72021-03-10 09:00:58 +0000103 p = result.Module("my_bar", "android_arm64_armv8-a").(*PrebuiltEtc)
Paul Duffine84b1332021-03-12 11:59:43 +0000104 android.AssertStringEquals(t, "my_bar output file path", "bar.conf", p.outputFilePath.Base())
Jiyong Park1a7cf082018-11-13 11:59:12 +0900105}
Anton Hanssonce0e2582019-02-04 14:19:27 +0000106
107func TestPrebuiltEtcAndroidMk(t *testing.T) {
Paul Duffin921fac72021-03-10 09:00:58 +0000108 result := prebuiltEtcFixtureFactory.RunTestWithBp(t, `
Anton Hanssonce0e2582019-02-04 14:19:27 +0000109 prebuilt_etc {
110 name: "foo",
111 src: "foo.conf",
112 owner: "abc",
113 filename_from_src: true,
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -0700114 required: ["modA", "moduleB"],
115 host_required: ["hostModA", "hostModB"],
116 target_required: ["targetModA"],
Anton Hanssonce0e2582019-02-04 14:19:27 +0000117 }
118 `)
119
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -0700120 expected := map[string][]string{
121 "LOCAL_MODULE": {"foo"},
122 "LOCAL_MODULE_CLASS": {"ETC"},
123 "LOCAL_MODULE_OWNER": {"abc"},
124 "LOCAL_INSTALLED_MODULE_STEM": {"foo.conf"},
125 "LOCAL_REQUIRED_MODULES": {"modA", "moduleB"},
126 "LOCAL_HOST_REQUIRED_MODULES": {"hostModA", "hostModB"},
127 "LOCAL_TARGET_REQUIRED_MODULES": {"targetModA"},
Anton Hanssonce0e2582019-02-04 14:19:27 +0000128 }
129
Paul Duffin921fac72021-03-10 09:00:58 +0000130 mod := result.Module("foo", "android_arm64_armv8-a").(*PrebuiltEtc)
131 entries := android.AndroidMkEntriesForTest(t, result.TestContext, mod)[0]
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -0700132 for k, expectedValue := range expected {
133 if value, ok := entries.EntryMap[k]; ok {
Paul Duffine84b1332021-03-12 11:59:43 +0000134 android.AssertDeepEquals(t, k, expectedValue, value)
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -0700135 } else {
136 t.Errorf("No %s defined, saw %q", k, entries.EntryMap)
Anton Hanssonce0e2582019-02-04 14:19:27 +0000137 }
138 }
139}
Jaewoong Jung24788182019-02-04 14:34:10 -0800140
Liz Kammer0449a632020-06-26 10:12:36 -0700141func TestPrebuiltEtcRelativeInstallPathInstallDirPath(t *testing.T) {
Paul Duffin921fac72021-03-10 09:00:58 +0000142 result := prebuiltEtcFixtureFactory.RunTestWithBp(t, `
Liz Kammer0449a632020-06-26 10:12:36 -0700143 prebuilt_etc {
144 name: "foo.conf",
145 src: "foo.conf",
146 relative_install_path: "bar",
147 }
148 `)
149
Paul Duffin921fac72021-03-10 09:00:58 +0000150 p := result.Module("foo.conf", "android_arm64_armv8-a").(*PrebuiltEtc)
Paul Duffin5f9f7712021-03-15 15:35:49 +0000151 expected := "out/soong/target/product/test_device/system/etc/bar"
152 android.AssertPathRelativeToTopEquals(t, "install dir", expected, p.installDirPath)
Liz Kammer0449a632020-06-26 10:12:36 -0700153}
154
155func TestPrebuiltEtcCannotSetRelativeInstallPathAndSubDir(t *testing.T) {
Paul Duffin1172fed2021-03-08 11:28:18 +0000156 prebuiltEtcFixtureFactory.
157 ExtendWithErrorHandler(android.FixtureExpectsAtLeastOneErrorMatchingPattern("relative_install_path is set. Cannot set sub_dir")).
158 RunTestWithBp(t, `
159 prebuilt_etc {
160 name: "foo.conf",
161 src: "foo.conf",
162 sub_dir: "bar",
163 relative_install_path: "bar",
164 }
165 `)
Liz Kammer0449a632020-06-26 10:12:36 -0700166}
167
Jaewoong Jung24788182019-02-04 14:34:10 -0800168func TestPrebuiltEtcHost(t *testing.T) {
Paul Duffin921fac72021-03-10 09:00:58 +0000169 result := prebuiltEtcFixtureFactory.RunTestWithBp(t, `
Jaewoong Jung24788182019-02-04 14:34:10 -0800170 prebuilt_etc_host {
171 name: "foo.conf",
172 src: "foo.conf",
173 }
174 `)
175
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700176 buildOS := android.BuildOs.String()
Paul Duffin921fac72021-03-10 09:00:58 +0000177 p := result.Module("foo.conf", buildOS+"_common").(*PrebuiltEtc)
Jaewoong Jung24788182019-02-04 14:34:10 -0800178 if !p.Host() {
179 t.Errorf("host bit is not set for a prebuilt_etc_host module.")
180 }
181}
Jaewoong Jungc3fcdb42019-02-13 05:50:33 -0800182
183func TestPrebuiltUserShareInstallDirPath(t *testing.T) {
Paul Duffin921fac72021-03-10 09:00:58 +0000184 result := prebuiltEtcFixtureFactory.RunTestWithBp(t, `
Jaewoong Jungc3fcdb42019-02-13 05:50:33 -0800185 prebuilt_usr_share {
186 name: "foo.conf",
187 src: "foo.conf",
188 sub_dir: "bar",
189 }
190 `)
191
Paul Duffin921fac72021-03-10 09:00:58 +0000192 p := result.Module("foo.conf", "android_arm64_armv8-a").(*PrebuiltEtc)
Paul Duffin5f9f7712021-03-15 15:35:49 +0000193 expected := "out/soong/target/product/test_device/system/usr/share/bar"
194 android.AssertPathRelativeToTopEquals(t, "install dir", expected, p.installDirPath)
Jaewoong Jungc3fcdb42019-02-13 05:50:33 -0800195}
Patrice Arruda300cef92019-02-22 15:47:57 -0800196
197func TestPrebuiltUserShareHostInstallDirPath(t *testing.T) {
Paul Duffin921fac72021-03-10 09:00:58 +0000198 result := prebuiltEtcFixtureFactory.RunTestWithBp(t, `
Patrice Arruda300cef92019-02-22 15:47:57 -0800199 prebuilt_usr_share_host {
200 name: "foo.conf",
201 src: "foo.conf",
202 sub_dir: "bar",
203 }
204 `)
205
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700206 buildOS := android.BuildOs.String()
Paul Duffin921fac72021-03-10 09:00:58 +0000207 p := result.Module("foo.conf", buildOS+"_common").(*PrebuiltEtc)
Paul Duffin5f9f7712021-03-15 15:35:49 +0000208 expected := filepath.Join("out/soong/host", result.Config.PrebuiltOS(), "usr", "share", "bar")
209 android.AssertPathRelativeToTopEquals(t, "install dir", expected, p.installDirPath)
Patrice Arruda300cef92019-02-22 15:47:57 -0800210}
Patrice Arruda61583eb2019-05-14 08:20:45 -0700211
212func TestPrebuiltFontInstallDirPath(t *testing.T) {
Paul Duffin921fac72021-03-10 09:00:58 +0000213 result := prebuiltEtcFixtureFactory.RunTestWithBp(t, `
Patrice Arruda61583eb2019-05-14 08:20:45 -0700214 prebuilt_font {
215 name: "foo.conf",
216 src: "foo.conf",
217 }
218 `)
219
Paul Duffin921fac72021-03-10 09:00:58 +0000220 p := result.Module("foo.conf", "android_arm64_armv8-a").(*PrebuiltEtc)
Paul Duffin5f9f7712021-03-15 15:35:49 +0000221 expected := "out/soong/target/product/test_device/system/fonts"
222 android.AssertPathRelativeToTopEquals(t, "install dir", expected, p.installDirPath)
Patrice Arruda61583eb2019-05-14 08:20:45 -0700223}
Patrice Arruda057a8b12019-06-03 15:29:27 -0700224
225func TestPrebuiltFirmwareDirPath(t *testing.T) {
Paul Duffin5f9f7712021-03-15 15:35:49 +0000226 targetPath := "out/soong/target/product/test_device"
Patrice Arruda057a8b12019-06-03 15:29:27 -0700227 tests := []struct {
228 description string
229 config string
230 expectedPath string
231 }{{
232 description: "prebuilt: system firmware",
233 config: `
234 prebuilt_firmware {
235 name: "foo.conf",
236 src: "foo.conf",
237 }`,
238 expectedPath: filepath.Join(targetPath, "system/etc/firmware"),
239 }, {
240 description: "prebuilt: vendor firmware",
241 config: `
242 prebuilt_firmware {
243 name: "foo.conf",
244 src: "foo.conf",
245 soc_specific: true,
246 sub_dir: "sub_dir",
247 }`,
248 expectedPath: filepath.Join(targetPath, "vendor/firmware/sub_dir"),
249 }}
250 for _, tt := range tests {
251 t.Run(tt.description, func(t *testing.T) {
Paul Duffin921fac72021-03-10 09:00:58 +0000252 result := prebuiltEtcFixtureFactory.RunTestWithBp(t, tt.config)
253 p := result.Module("foo.conf", "android_arm64_armv8-a").(*PrebuiltEtc)
Paul Duffin5f9f7712021-03-15 15:35:49 +0000254 android.AssertPathRelativeToTopEquals(t, "install dir", tt.expectedPath, p.installDirPath)
Patrice Arruda057a8b12019-06-03 15:29:27 -0700255 })
256 }
257}
Patrice Arruda0f688002020-06-08 21:40:25 +0000258
259func TestPrebuiltDSPDirPath(t *testing.T) {
Paul Duffin5f9f7712021-03-15 15:35:49 +0000260 targetPath := "out/soong/target/product/test_device"
Patrice Arruda0f688002020-06-08 21:40:25 +0000261 tests := []struct {
262 description string
263 config string
264 expectedPath string
265 }{{
266 description: "prebuilt: system dsp",
267 config: `
268 prebuilt_dsp {
269 name: "foo.conf",
270 src: "foo.conf",
271 }`,
272 expectedPath: filepath.Join(targetPath, "system/etc/dsp"),
273 }, {
274 description: "prebuilt: vendor dsp",
275 config: `
276 prebuilt_dsp {
277 name: "foo.conf",
278 src: "foo.conf",
279 soc_specific: true,
280 sub_dir: "sub_dir",
281 }`,
282 expectedPath: filepath.Join(targetPath, "vendor/dsp/sub_dir"),
283 }}
284 for _, tt := range tests {
285 t.Run(tt.description, func(t *testing.T) {
Paul Duffin921fac72021-03-10 09:00:58 +0000286 result := prebuiltEtcFixtureFactory.RunTestWithBp(t, tt.config)
287 p := result.Module("foo.conf", "android_arm64_armv8-a").(*PrebuiltEtc)
Paul Duffin5f9f7712021-03-15 15:35:49 +0000288 android.AssertPathRelativeToTopEquals(t, "install dir", tt.expectedPath, p.installDirPath)
Patrice Arruda0f688002020-06-08 21:40:25 +0000289 })
290 }
291}