blob: d977c307e3e3f043f16aa7fb77c03a3f8b47e573 [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
15package android
16
17import (
18 "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"
23)
24
Patrice Arruda300cef92019-02-22 15:47:57 -080025func testPrebuiltEtc(t *testing.T, bp string) (*TestContext, Config) {
Tao Bao0ba5c942018-08-14 22:20:22 -070026 config, buildDir := setUp(t)
27 defer tearDown(buildDir)
28 ctx := NewTestArchContext()
29 ctx.RegisterModuleType("prebuilt_etc", ModuleFactoryAdaptor(PrebuiltEtcFactory))
Jaewoong Jung4b44fcd2019-02-07 08:28:03 -080030 ctx.RegisterModuleType("prebuilt_etc_host", ModuleFactoryAdaptor(PrebuiltEtcHostFactory))
Jaewoong Jungc3fcdb42019-02-13 05:50:33 -080031 ctx.RegisterModuleType("prebuilt_usr_share", ModuleFactoryAdaptor(PrebuiltUserShareFactory))
Patrice Arruda300cef92019-02-22 15:47:57 -080032 ctx.RegisterModuleType("prebuilt_usr_share_host", ModuleFactoryAdaptor(PrebuiltUserShareHostFactory))
Patrice Arruda61583eb2019-05-14 08:20:45 -070033 ctx.RegisterModuleType("prebuilt_font", ModuleFactoryAdaptor(PrebuiltFontFactory))
Patrice Arruda057a8b12019-06-03 15:29:27 -070034 ctx.RegisterModuleType("prebuilt_firmware", ModuleFactoryAdaptor(PrebuiltFirmwareFactory))
Tao Bao0ba5c942018-08-14 22:20:22 -070035 ctx.PreDepsMutators(func(ctx RegisterMutatorsContext) {
36 ctx.BottomUp("prebuilt_etc", prebuiltEtcMutator).Parallel()
37 })
38 ctx.Register()
39 mockFiles := map[string][]byte{
40 "Android.bp": []byte(bp),
41 "foo.conf": nil,
42 "bar.conf": nil,
43 "baz.conf": nil,
44 }
45 ctx.MockFileSystem(mockFiles)
46 _, errs := ctx.ParseFileList(".", []string{"Android.bp"})
47 FailIfErrored(t, errs)
48 _, errs = ctx.PrepareBuildActions(config)
49 FailIfErrored(t, errs)
50
Patrice Arruda300cef92019-02-22 15:47:57 -080051 return ctx, config
Tao Bao0ba5c942018-08-14 22:20:22 -070052}
53
54func setUp(t *testing.T) (config Config, buildDir string) {
55 buildDir, err := ioutil.TempDir("", "soong_prebuilt_etc_test")
56 if err != nil {
57 t.Fatal(err)
58 }
59
60 config = TestArchConfig(buildDir, nil)
61 return
62}
63
64func tearDown(buildDir string) {
65 os.RemoveAll(buildDir)
66}
67
68func TestPrebuiltEtcVariants(t *testing.T) {
Patrice Arruda300cef92019-02-22 15:47:57 -080069 ctx, _ := testPrebuiltEtc(t, `
Tao Bao0ba5c942018-08-14 22:20:22 -070070 prebuilt_etc {
71 name: "foo.conf",
72 src: "foo.conf",
73 }
74 prebuilt_etc {
75 name: "bar.conf",
76 src: "bar.conf",
77 recovery_available: true,
78 }
79 prebuilt_etc {
80 name: "baz.conf",
81 src: "baz.conf",
82 recovery: true,
83 }
84 `)
85
86 foo_variants := ctx.ModuleVariantsForTests("foo.conf")
87 if len(foo_variants) != 1 {
88 t.Errorf("expected 1, got %#v", foo_variants)
89 }
90
91 bar_variants := ctx.ModuleVariantsForTests("bar.conf")
92 if len(bar_variants) != 2 {
93 t.Errorf("expected 2, got %#v", bar_variants)
94 }
95
96 baz_variants := ctx.ModuleVariantsForTests("baz.conf")
97 if len(baz_variants) != 1 {
98 t.Errorf("expected 1, got %#v", bar_variants)
99 }
100}
Jiyong Park139a2e62018-10-26 21:49:39 +0900101
102func TestPrebuiltEtcOutputPath(t *testing.T) {
Patrice Arruda300cef92019-02-22 15:47:57 -0800103 ctx, _ := testPrebuiltEtc(t, `
Jiyong Park139a2e62018-10-26 21:49:39 +0900104 prebuilt_etc {
105 name: "foo.conf",
106 src: "foo.conf",
107 filename: "foo.installed.conf",
108 }
109 `)
110
Jaewoong Jungb9a11512019-01-15 10:47:05 -0800111 p := ctx.ModuleForTests("foo.conf", "android_arm64_armv8-a_core").Module().(*PrebuiltEtc)
Jiyong Park139a2e62018-10-26 21:49:39 +0900112 if p.outputFilePath.Base() != "foo.installed.conf" {
113 t.Errorf("expected foo.installed.conf, got %q", p.outputFilePath.Base())
114 }
115}
Jiyong Park1a7cf082018-11-13 11:59:12 +0900116
117func TestPrebuiltEtcGlob(t *testing.T) {
Patrice Arruda300cef92019-02-22 15:47:57 -0800118 ctx, _ := testPrebuiltEtc(t, `
Jiyong Park1a7cf082018-11-13 11:59:12 +0900119 prebuilt_etc {
120 name: "my_foo",
121 src: "foo.*",
122 }
123 prebuilt_etc {
124 name: "my_bar",
125 src: "bar.*",
126 filename_from_src: true,
127 }
128 `)
129
Jaewoong Jungb9a11512019-01-15 10:47:05 -0800130 p := ctx.ModuleForTests("my_foo", "android_arm64_armv8-a_core").Module().(*PrebuiltEtc)
Jiyong Park1a7cf082018-11-13 11:59:12 +0900131 if p.outputFilePath.Base() != "my_foo" {
132 t.Errorf("expected my_foo, got %q", p.outputFilePath.Base())
133 }
134
Jaewoong Jungb9a11512019-01-15 10:47:05 -0800135 p = ctx.ModuleForTests("my_bar", "android_arm64_armv8-a_core").Module().(*PrebuiltEtc)
Jiyong Park1a7cf082018-11-13 11:59:12 +0900136 if p.outputFilePath.Base() != "bar.conf" {
137 t.Errorf("expected bar.conf, got %q", p.outputFilePath.Base())
138 }
139}
Anton Hanssonce0e2582019-02-04 14:19:27 +0000140
141func TestPrebuiltEtcAndroidMk(t *testing.T) {
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -0700142 ctx, config := testPrebuiltEtc(t, `
Anton Hanssonce0e2582019-02-04 14:19:27 +0000143 prebuilt_etc {
144 name: "foo",
145 src: "foo.conf",
146 owner: "abc",
147 filename_from_src: true,
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -0700148 required: ["modA", "moduleB"],
149 host_required: ["hostModA", "hostModB"],
150 target_required: ["targetModA"],
Anton Hanssonce0e2582019-02-04 14:19:27 +0000151 }
152 `)
153
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -0700154 expected := map[string][]string{
155 "LOCAL_MODULE": {"foo"},
156 "LOCAL_MODULE_CLASS": {"ETC"},
157 "LOCAL_MODULE_OWNER": {"abc"},
158 "LOCAL_INSTALLED_MODULE_STEM": {"foo.conf"},
159 "LOCAL_REQUIRED_MODULES": {"modA", "moduleB"},
160 "LOCAL_HOST_REQUIRED_MODULES": {"hostModA", "hostModB"},
161 "LOCAL_TARGET_REQUIRED_MODULES": {"targetModA"},
Anton Hanssonce0e2582019-02-04 14:19:27 +0000162 }
163
164 mod := ctx.ModuleForTests("foo", "android_arm64_armv8-a_core").Module().(*PrebuiltEtc)
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -0700165 entries := AndroidMkEntriesForTest(t, config, "", mod)
166 for k, expectedValue := range expected {
167 if value, ok := entries.EntryMap[k]; ok {
168 if !reflect.DeepEqual(value, expectedValue) {
169 t.Errorf("Incorrect %s '%s', expected '%s'", k, value, expectedValue)
Anton Hanssonce0e2582019-02-04 14:19:27 +0000170 }
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -0700171 } else {
172 t.Errorf("No %s defined, saw %q", k, entries.EntryMap)
Anton Hanssonce0e2582019-02-04 14:19:27 +0000173 }
174 }
175}
Jaewoong Jung24788182019-02-04 14:34:10 -0800176
177func TestPrebuiltEtcHost(t *testing.T) {
Patrice Arruda300cef92019-02-22 15:47:57 -0800178 ctx, _ := testPrebuiltEtc(t, `
Jaewoong Jung24788182019-02-04 14:34:10 -0800179 prebuilt_etc_host {
180 name: "foo.conf",
181 src: "foo.conf",
182 }
183 `)
184
185 buildOS := BuildOs.String()
186 p := ctx.ModuleForTests("foo.conf", buildOS+"_common").Module().(*PrebuiltEtc)
187 if !p.Host() {
188 t.Errorf("host bit is not set for a prebuilt_etc_host module.")
189 }
190}
Jaewoong Jungc3fcdb42019-02-13 05:50:33 -0800191
192func TestPrebuiltUserShareInstallDirPath(t *testing.T) {
Patrice Arruda300cef92019-02-22 15:47:57 -0800193 ctx, _ := testPrebuiltEtc(t, `
Jaewoong Jungc3fcdb42019-02-13 05:50:33 -0800194 prebuilt_usr_share {
195 name: "foo.conf",
196 src: "foo.conf",
197 sub_dir: "bar",
198 }
199 `)
200
201 p := ctx.ModuleForTests("foo.conf", "android_arm64_armv8-a_core").Module().(*PrebuiltEtc)
202 expected := "target/product/test_device/system/usr/share/bar"
203 if p.installDirPath.RelPathString() != expected {
204 t.Errorf("expected %q, got %q", expected, p.installDirPath.RelPathString())
205 }
206}
Patrice Arruda300cef92019-02-22 15:47:57 -0800207
208func TestPrebuiltUserShareHostInstallDirPath(t *testing.T) {
209 ctx, config := testPrebuiltEtc(t, `
210 prebuilt_usr_share_host {
211 name: "foo.conf",
212 src: "foo.conf",
213 sub_dir: "bar",
214 }
215 `)
216
217 buildOS := BuildOs.String()
218 p := ctx.ModuleForTests("foo.conf", buildOS+"_common").Module().(*PrebuiltEtc)
219 expected := filepath.Join("host", config.PrebuiltOS(), "usr", "share", "bar")
220 if p.installDirPath.RelPathString() != expected {
221 t.Errorf("expected %q, got %q", expected, p.installDirPath.RelPathString())
222 }
223}
Patrice Arruda61583eb2019-05-14 08:20:45 -0700224
225func TestPrebuiltFontInstallDirPath(t *testing.T) {
226 ctx, _ := testPrebuiltEtc(t, `
227 prebuilt_font {
228 name: "foo.conf",
229 src: "foo.conf",
230 }
231 `)
232
233 p := ctx.ModuleForTests("foo.conf", "android_arm64_armv8-a_core").Module().(*PrebuiltEtc)
234 expected := "target/product/test_device/system/fonts"
235 if p.installDirPath.RelPathString() != expected {
236 t.Errorf("expected %q, got %q", expected, p.installDirPath.RelPathString())
237 }
238}
Patrice Arruda057a8b12019-06-03 15:29:27 -0700239
240func TestPrebuiltFirmwareDirPath(t *testing.T) {
241 targetPath := "target/product/test_device"
242 tests := []struct {
243 description string
244 config string
245 expectedPath string
246 }{{
247 description: "prebuilt: system firmware",
248 config: `
249 prebuilt_firmware {
250 name: "foo.conf",
251 src: "foo.conf",
252 }`,
253 expectedPath: filepath.Join(targetPath, "system/etc/firmware"),
254 }, {
255 description: "prebuilt: vendor firmware",
256 config: `
257 prebuilt_firmware {
258 name: "foo.conf",
259 src: "foo.conf",
260 soc_specific: true,
261 sub_dir: "sub_dir",
262 }`,
263 expectedPath: filepath.Join(targetPath, "vendor/firmware/sub_dir"),
264 }}
265 for _, tt := range tests {
266 t.Run(tt.description, func(t *testing.T) {
267 ctx, _ := testPrebuiltEtc(t, tt.config)
268 p := ctx.ModuleForTests("foo.conf", "android_arm64_armv8-a_core").Module().(*PrebuiltEtc)
269 if p.installDirPath.RelPathString() != tt.expectedPath {
270 t.Errorf("expected %q, got %q", tt.expectedPath, p.installDirPath)
271 }
272 })
273 }
274}