blob: 100e73d0cb34efdcc2179b6ca14e1bd874ce21ba [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 (
Patrice Arruda300cef92019-02-22 15:47:57 -080018 "path/filepath"
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -070019 "reflect"
Tao Bao0ba5c942018-08-14 22:20:22 -070020 "testing"
21)
22
Patrice Arruda300cef92019-02-22 15:47:57 -080023func testPrebuiltEtc(t *testing.T, bp string) (*TestContext, Config) {
Colin Cross2ffb9a82019-06-10 14:32:38 -070024 config := TestArchConfig(buildDir, nil)
Tao Bao0ba5c942018-08-14 22:20:22 -070025 ctx := NewTestArchContext()
Colin Cross4b49b762019-11-22 15:25:03 -080026 ctx.RegisterModuleType("prebuilt_etc", PrebuiltEtcFactory)
27 ctx.RegisterModuleType("prebuilt_etc_host", PrebuiltEtcHostFactory)
28 ctx.RegisterModuleType("prebuilt_usr_share", PrebuiltUserShareFactory)
29 ctx.RegisterModuleType("prebuilt_usr_share_host", PrebuiltUserShareHostFactory)
30 ctx.RegisterModuleType("prebuilt_font", PrebuiltFontFactory)
31 ctx.RegisterModuleType("prebuilt_firmware", PrebuiltFirmwareFactory)
Tao Bao0ba5c942018-08-14 22:20:22 -070032 ctx.PreDepsMutators(func(ctx RegisterMutatorsContext) {
33 ctx.BottomUp("prebuilt_etc", prebuiltEtcMutator).Parallel()
34 })
35 ctx.Register()
36 mockFiles := map[string][]byte{
37 "Android.bp": []byte(bp),
38 "foo.conf": nil,
39 "bar.conf": nil,
40 "baz.conf": nil,
41 }
42 ctx.MockFileSystem(mockFiles)
43 _, errs := ctx.ParseFileList(".", []string{"Android.bp"})
44 FailIfErrored(t, errs)
45 _, errs = ctx.PrepareBuildActions(config)
46 FailIfErrored(t, errs)
47
Patrice Arruda300cef92019-02-22 15:47:57 -080048 return ctx, config
Tao Bao0ba5c942018-08-14 22:20:22 -070049}
50
Tao Bao0ba5c942018-08-14 22:20:22 -070051func TestPrebuiltEtcVariants(t *testing.T) {
Patrice Arruda300cef92019-02-22 15:47:57 -080052 ctx, _ := testPrebuiltEtc(t, `
Tao Bao0ba5c942018-08-14 22:20:22 -070053 prebuilt_etc {
54 name: "foo.conf",
55 src: "foo.conf",
56 }
57 prebuilt_etc {
58 name: "bar.conf",
59 src: "bar.conf",
60 recovery_available: true,
61 }
62 prebuilt_etc {
63 name: "baz.conf",
64 src: "baz.conf",
65 recovery: true,
66 }
67 `)
68
69 foo_variants := ctx.ModuleVariantsForTests("foo.conf")
70 if len(foo_variants) != 1 {
71 t.Errorf("expected 1, got %#v", foo_variants)
72 }
73
74 bar_variants := ctx.ModuleVariantsForTests("bar.conf")
75 if len(bar_variants) != 2 {
76 t.Errorf("expected 2, got %#v", bar_variants)
77 }
78
79 baz_variants := ctx.ModuleVariantsForTests("baz.conf")
80 if len(baz_variants) != 1 {
81 t.Errorf("expected 1, got %#v", bar_variants)
82 }
83}
Jiyong Park139a2e62018-10-26 21:49:39 +090084
85func TestPrebuiltEtcOutputPath(t *testing.T) {
Patrice Arruda300cef92019-02-22 15:47:57 -080086 ctx, _ := testPrebuiltEtc(t, `
Jiyong Park139a2e62018-10-26 21:49:39 +090087 prebuilt_etc {
88 name: "foo.conf",
89 src: "foo.conf",
90 filename: "foo.installed.conf",
91 }
92 `)
93
Jaewoong Jungb9a11512019-01-15 10:47:05 -080094 p := ctx.ModuleForTests("foo.conf", "android_arm64_armv8-a_core").Module().(*PrebuiltEtc)
Jiyong Park139a2e62018-10-26 21:49:39 +090095 if p.outputFilePath.Base() != "foo.installed.conf" {
96 t.Errorf("expected foo.installed.conf, got %q", p.outputFilePath.Base())
97 }
98}
Jiyong Park1a7cf082018-11-13 11:59:12 +090099
100func TestPrebuiltEtcGlob(t *testing.T) {
Patrice Arruda300cef92019-02-22 15:47:57 -0800101 ctx, _ := testPrebuiltEtc(t, `
Jiyong Park1a7cf082018-11-13 11:59:12 +0900102 prebuilt_etc {
103 name: "my_foo",
104 src: "foo.*",
105 }
106 prebuilt_etc {
107 name: "my_bar",
108 src: "bar.*",
109 filename_from_src: true,
110 }
111 `)
112
Jaewoong Jungb9a11512019-01-15 10:47:05 -0800113 p := ctx.ModuleForTests("my_foo", "android_arm64_armv8-a_core").Module().(*PrebuiltEtc)
Jiyong Park1a7cf082018-11-13 11:59:12 +0900114 if p.outputFilePath.Base() != "my_foo" {
115 t.Errorf("expected my_foo, got %q", p.outputFilePath.Base())
116 }
117
Jaewoong Jungb9a11512019-01-15 10:47:05 -0800118 p = ctx.ModuleForTests("my_bar", "android_arm64_armv8-a_core").Module().(*PrebuiltEtc)
Jiyong Park1a7cf082018-11-13 11:59:12 +0900119 if p.outputFilePath.Base() != "bar.conf" {
120 t.Errorf("expected bar.conf, got %q", p.outputFilePath.Base())
121 }
122}
Anton Hanssonce0e2582019-02-04 14:19:27 +0000123
124func TestPrebuiltEtcAndroidMk(t *testing.T) {
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -0700125 ctx, config := testPrebuiltEtc(t, `
Anton Hanssonce0e2582019-02-04 14:19:27 +0000126 prebuilt_etc {
127 name: "foo",
128 src: "foo.conf",
129 owner: "abc",
130 filename_from_src: true,
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -0700131 required: ["modA", "moduleB"],
132 host_required: ["hostModA", "hostModB"],
133 target_required: ["targetModA"],
Anton Hanssonce0e2582019-02-04 14:19:27 +0000134 }
135 `)
136
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -0700137 expected := map[string][]string{
138 "LOCAL_MODULE": {"foo"},
139 "LOCAL_MODULE_CLASS": {"ETC"},
140 "LOCAL_MODULE_OWNER": {"abc"},
141 "LOCAL_INSTALLED_MODULE_STEM": {"foo.conf"},
142 "LOCAL_REQUIRED_MODULES": {"modA", "moduleB"},
143 "LOCAL_HOST_REQUIRED_MODULES": {"hostModA", "hostModB"},
144 "LOCAL_TARGET_REQUIRED_MODULES": {"targetModA"},
Anton Hanssonce0e2582019-02-04 14:19:27 +0000145 }
146
147 mod := ctx.ModuleForTests("foo", "android_arm64_armv8-a_core").Module().(*PrebuiltEtc)
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -0700148 entries := AndroidMkEntriesForTest(t, config, "", mod)
149 for k, expectedValue := range expected {
150 if value, ok := entries.EntryMap[k]; ok {
151 if !reflect.DeepEqual(value, expectedValue) {
152 t.Errorf("Incorrect %s '%s', expected '%s'", k, value, expectedValue)
Anton Hanssonce0e2582019-02-04 14:19:27 +0000153 }
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -0700154 } else {
155 t.Errorf("No %s defined, saw %q", k, entries.EntryMap)
Anton Hanssonce0e2582019-02-04 14:19:27 +0000156 }
157 }
158}
Jaewoong Jung24788182019-02-04 14:34:10 -0800159
160func TestPrebuiltEtcHost(t *testing.T) {
Patrice Arruda300cef92019-02-22 15:47:57 -0800161 ctx, _ := testPrebuiltEtc(t, `
Jaewoong Jung24788182019-02-04 14:34:10 -0800162 prebuilt_etc_host {
163 name: "foo.conf",
164 src: "foo.conf",
165 }
166 `)
167
168 buildOS := BuildOs.String()
169 p := ctx.ModuleForTests("foo.conf", buildOS+"_common").Module().(*PrebuiltEtc)
170 if !p.Host() {
171 t.Errorf("host bit is not set for a prebuilt_etc_host module.")
172 }
173}
Jaewoong Jungc3fcdb42019-02-13 05:50:33 -0800174
175func TestPrebuiltUserShareInstallDirPath(t *testing.T) {
Patrice Arruda300cef92019-02-22 15:47:57 -0800176 ctx, _ := testPrebuiltEtc(t, `
Jaewoong Jungc3fcdb42019-02-13 05:50:33 -0800177 prebuilt_usr_share {
178 name: "foo.conf",
179 src: "foo.conf",
180 sub_dir: "bar",
181 }
182 `)
183
184 p := ctx.ModuleForTests("foo.conf", "android_arm64_armv8-a_core").Module().(*PrebuiltEtc)
Colin Crossff6c33d2019-10-02 16:01:35 -0700185 expected := buildDir + "/target/product/test_device/system/usr/share/bar"
186 if p.installDirPath.String() != expected {
187 t.Errorf("expected %q, got %q", expected, p.installDirPath.String())
Jaewoong Jungc3fcdb42019-02-13 05:50:33 -0800188 }
189}
Patrice Arruda300cef92019-02-22 15:47:57 -0800190
191func TestPrebuiltUserShareHostInstallDirPath(t *testing.T) {
192 ctx, config := testPrebuiltEtc(t, `
193 prebuilt_usr_share_host {
194 name: "foo.conf",
195 src: "foo.conf",
196 sub_dir: "bar",
197 }
198 `)
199
200 buildOS := BuildOs.String()
201 p := ctx.ModuleForTests("foo.conf", buildOS+"_common").Module().(*PrebuiltEtc)
Colin Crossff6c33d2019-10-02 16:01:35 -0700202 expected := filepath.Join(buildDir, "host", config.PrebuiltOS(), "usr", "share", "bar")
203 if p.installDirPath.String() != expected {
204 t.Errorf("expected %q, got %q", expected, p.installDirPath.String())
Patrice Arruda300cef92019-02-22 15:47:57 -0800205 }
206}
Patrice Arruda61583eb2019-05-14 08:20:45 -0700207
208func TestPrebuiltFontInstallDirPath(t *testing.T) {
209 ctx, _ := testPrebuiltEtc(t, `
210 prebuilt_font {
211 name: "foo.conf",
212 src: "foo.conf",
213 }
214 `)
215
216 p := ctx.ModuleForTests("foo.conf", "android_arm64_armv8-a_core").Module().(*PrebuiltEtc)
Colin Crossff6c33d2019-10-02 16:01:35 -0700217 expected := buildDir + "/target/product/test_device/system/fonts"
218 if p.installDirPath.String() != expected {
219 t.Errorf("expected %q, got %q", expected, p.installDirPath.String())
Patrice Arruda61583eb2019-05-14 08:20:45 -0700220 }
221}
Patrice Arruda057a8b12019-06-03 15:29:27 -0700222
223func TestPrebuiltFirmwareDirPath(t *testing.T) {
Colin Crossff6c33d2019-10-02 16:01:35 -0700224 targetPath := buildDir + "/target/product/test_device"
Patrice Arruda057a8b12019-06-03 15:29:27 -0700225 tests := []struct {
226 description string
227 config string
228 expectedPath string
229 }{{
230 description: "prebuilt: system firmware",
231 config: `
232 prebuilt_firmware {
233 name: "foo.conf",
234 src: "foo.conf",
235 }`,
236 expectedPath: filepath.Join(targetPath, "system/etc/firmware"),
237 }, {
238 description: "prebuilt: vendor firmware",
239 config: `
240 prebuilt_firmware {
241 name: "foo.conf",
242 src: "foo.conf",
243 soc_specific: true,
244 sub_dir: "sub_dir",
245 }`,
246 expectedPath: filepath.Join(targetPath, "vendor/firmware/sub_dir"),
247 }}
248 for _, tt := range tests {
249 t.Run(tt.description, func(t *testing.T) {
250 ctx, _ := testPrebuiltEtc(t, tt.config)
251 p := ctx.ModuleForTests("foo.conf", "android_arm64_armv8-a_core").Module().(*PrebuiltEtc)
Colin Crossff6c33d2019-10-02 16:01:35 -0700252 if p.installDirPath.String() != tt.expectedPath {
Patrice Arruda057a8b12019-06-03 15:29:27 -0700253 t.Errorf("expected %q, got %q", tt.expectedPath, p.installDirPath)
254 }
255 })
256 }
257}