blob: 3c466a176e0c5fe6acc34729b5d1c225be9de0e8 [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.Register()
33 mockFiles := map[string][]byte{
34 "Android.bp": []byte(bp),
35 "foo.conf": nil,
36 "bar.conf": nil,
37 "baz.conf": nil,
38 }
39 ctx.MockFileSystem(mockFiles)
40 _, errs := ctx.ParseFileList(".", []string{"Android.bp"})
41 FailIfErrored(t, errs)
42 _, errs = ctx.PrepareBuildActions(config)
43 FailIfErrored(t, errs)
44
Patrice Arruda300cef92019-02-22 15:47:57 -080045 return ctx, config
Tao Bao0ba5c942018-08-14 22:20:22 -070046}
47
Tao Bao0ba5c942018-08-14 22:20:22 -070048func TestPrebuiltEtcVariants(t *testing.T) {
Patrice Arruda300cef92019-02-22 15:47:57 -080049 ctx, _ := testPrebuiltEtc(t, `
Tao Bao0ba5c942018-08-14 22:20:22 -070050 prebuilt_etc {
51 name: "foo.conf",
52 src: "foo.conf",
53 }
54 prebuilt_etc {
55 name: "bar.conf",
56 src: "bar.conf",
57 recovery_available: true,
58 }
59 prebuilt_etc {
60 name: "baz.conf",
61 src: "baz.conf",
62 recovery: true,
63 }
64 `)
65
66 foo_variants := ctx.ModuleVariantsForTests("foo.conf")
67 if len(foo_variants) != 1 {
68 t.Errorf("expected 1, got %#v", foo_variants)
69 }
70
71 bar_variants := ctx.ModuleVariantsForTests("bar.conf")
72 if len(bar_variants) != 2 {
73 t.Errorf("expected 2, got %#v", bar_variants)
74 }
75
76 baz_variants := ctx.ModuleVariantsForTests("baz.conf")
77 if len(baz_variants) != 1 {
78 t.Errorf("expected 1, got %#v", bar_variants)
79 }
80}
Jiyong Park139a2e62018-10-26 21:49:39 +090081
82func TestPrebuiltEtcOutputPath(t *testing.T) {
Patrice Arruda300cef92019-02-22 15:47:57 -080083 ctx, _ := testPrebuiltEtc(t, `
Jiyong Park139a2e62018-10-26 21:49:39 +090084 prebuilt_etc {
85 name: "foo.conf",
86 src: "foo.conf",
87 filename: "foo.installed.conf",
88 }
89 `)
90
Colin Cross7113d202019-11-20 16:39:12 -080091 p := ctx.ModuleForTests("foo.conf", "android_arm64_armv8-a").Module().(*PrebuiltEtc)
Jiyong Park139a2e62018-10-26 21:49:39 +090092 if p.outputFilePath.Base() != "foo.installed.conf" {
93 t.Errorf("expected foo.installed.conf, got %q", p.outputFilePath.Base())
94 }
95}
Jiyong Park1a7cf082018-11-13 11:59:12 +090096
97func TestPrebuiltEtcGlob(t *testing.T) {
Patrice Arruda300cef92019-02-22 15:47:57 -080098 ctx, _ := testPrebuiltEtc(t, `
Jiyong Park1a7cf082018-11-13 11:59:12 +090099 prebuilt_etc {
100 name: "my_foo",
101 src: "foo.*",
102 }
103 prebuilt_etc {
104 name: "my_bar",
105 src: "bar.*",
106 filename_from_src: true,
107 }
108 `)
109
Colin Cross7113d202019-11-20 16:39:12 -0800110 p := ctx.ModuleForTests("my_foo", "android_arm64_armv8-a").Module().(*PrebuiltEtc)
Jiyong Park1a7cf082018-11-13 11:59:12 +0900111 if p.outputFilePath.Base() != "my_foo" {
112 t.Errorf("expected my_foo, got %q", p.outputFilePath.Base())
113 }
114
Colin Cross7113d202019-11-20 16:39:12 -0800115 p = ctx.ModuleForTests("my_bar", "android_arm64_armv8-a").Module().(*PrebuiltEtc)
Jiyong Park1a7cf082018-11-13 11:59:12 +0900116 if p.outputFilePath.Base() != "bar.conf" {
117 t.Errorf("expected bar.conf, got %q", p.outputFilePath.Base())
118 }
119}
Anton Hanssonce0e2582019-02-04 14:19:27 +0000120
121func TestPrebuiltEtcAndroidMk(t *testing.T) {
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -0700122 ctx, config := testPrebuiltEtc(t, `
Anton Hanssonce0e2582019-02-04 14:19:27 +0000123 prebuilt_etc {
124 name: "foo",
125 src: "foo.conf",
126 owner: "abc",
127 filename_from_src: true,
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -0700128 required: ["modA", "moduleB"],
129 host_required: ["hostModA", "hostModB"],
130 target_required: ["targetModA"],
Anton Hanssonce0e2582019-02-04 14:19:27 +0000131 }
132 `)
133
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -0700134 expected := map[string][]string{
135 "LOCAL_MODULE": {"foo"},
136 "LOCAL_MODULE_CLASS": {"ETC"},
137 "LOCAL_MODULE_OWNER": {"abc"},
138 "LOCAL_INSTALLED_MODULE_STEM": {"foo.conf"},
139 "LOCAL_REQUIRED_MODULES": {"modA", "moduleB"},
140 "LOCAL_HOST_REQUIRED_MODULES": {"hostModA", "hostModB"},
141 "LOCAL_TARGET_REQUIRED_MODULES": {"targetModA"},
Anton Hanssonce0e2582019-02-04 14:19:27 +0000142 }
143
Colin Cross7113d202019-11-20 16:39:12 -0800144 mod := ctx.ModuleForTests("foo", "android_arm64_armv8-a").Module().(*PrebuiltEtc)
Jiyong Park0b0e1b92019-12-03 13:24:29 +0900145 entries := AndroidMkEntriesForTest(t, config, "", mod)[0]
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -0700146 for k, expectedValue := range expected {
147 if value, ok := entries.EntryMap[k]; ok {
148 if !reflect.DeepEqual(value, expectedValue) {
149 t.Errorf("Incorrect %s '%s', expected '%s'", k, value, expectedValue)
Anton Hanssonce0e2582019-02-04 14:19:27 +0000150 }
Jaewoong Jung9aa3ab12019-04-03 15:47:29 -0700151 } else {
152 t.Errorf("No %s defined, saw %q", k, entries.EntryMap)
Anton Hanssonce0e2582019-02-04 14:19:27 +0000153 }
154 }
155}
Jaewoong Jung24788182019-02-04 14:34:10 -0800156
157func TestPrebuiltEtcHost(t *testing.T) {
Patrice Arruda300cef92019-02-22 15:47:57 -0800158 ctx, _ := testPrebuiltEtc(t, `
Jaewoong Jung24788182019-02-04 14:34:10 -0800159 prebuilt_etc_host {
160 name: "foo.conf",
161 src: "foo.conf",
162 }
163 `)
164
165 buildOS := BuildOs.String()
166 p := ctx.ModuleForTests("foo.conf", buildOS+"_common").Module().(*PrebuiltEtc)
167 if !p.Host() {
168 t.Errorf("host bit is not set for a prebuilt_etc_host module.")
169 }
170}
Jaewoong Jungc3fcdb42019-02-13 05:50:33 -0800171
172func TestPrebuiltUserShareInstallDirPath(t *testing.T) {
Patrice Arruda300cef92019-02-22 15:47:57 -0800173 ctx, _ := testPrebuiltEtc(t, `
Jaewoong Jungc3fcdb42019-02-13 05:50:33 -0800174 prebuilt_usr_share {
175 name: "foo.conf",
176 src: "foo.conf",
177 sub_dir: "bar",
178 }
179 `)
180
Colin Cross7113d202019-11-20 16:39:12 -0800181 p := ctx.ModuleForTests("foo.conf", "android_arm64_armv8-a").Module().(*PrebuiltEtc)
Colin Crossff6c33d2019-10-02 16:01:35 -0700182 expected := buildDir + "/target/product/test_device/system/usr/share/bar"
183 if p.installDirPath.String() != expected {
184 t.Errorf("expected %q, got %q", expected, p.installDirPath.String())
Jaewoong Jungc3fcdb42019-02-13 05:50:33 -0800185 }
186}
Patrice Arruda300cef92019-02-22 15:47:57 -0800187
188func TestPrebuiltUserShareHostInstallDirPath(t *testing.T) {
189 ctx, config := testPrebuiltEtc(t, `
190 prebuilt_usr_share_host {
191 name: "foo.conf",
192 src: "foo.conf",
193 sub_dir: "bar",
194 }
195 `)
196
197 buildOS := BuildOs.String()
198 p := ctx.ModuleForTests("foo.conf", buildOS+"_common").Module().(*PrebuiltEtc)
Colin Crossff6c33d2019-10-02 16:01:35 -0700199 expected := filepath.Join(buildDir, "host", config.PrebuiltOS(), "usr", "share", "bar")
200 if p.installDirPath.String() != expected {
201 t.Errorf("expected %q, got %q", expected, p.installDirPath.String())
Patrice Arruda300cef92019-02-22 15:47:57 -0800202 }
203}
Patrice Arruda61583eb2019-05-14 08:20:45 -0700204
205func TestPrebuiltFontInstallDirPath(t *testing.T) {
206 ctx, _ := testPrebuiltEtc(t, `
207 prebuilt_font {
208 name: "foo.conf",
209 src: "foo.conf",
210 }
211 `)
212
Colin Cross7113d202019-11-20 16:39:12 -0800213 p := ctx.ModuleForTests("foo.conf", "android_arm64_armv8-a").Module().(*PrebuiltEtc)
Colin Crossff6c33d2019-10-02 16:01:35 -0700214 expected := buildDir + "/target/product/test_device/system/fonts"
215 if p.installDirPath.String() != expected {
216 t.Errorf("expected %q, got %q", expected, p.installDirPath.String())
Patrice Arruda61583eb2019-05-14 08:20:45 -0700217 }
218}
Patrice Arruda057a8b12019-06-03 15:29:27 -0700219
220func TestPrebuiltFirmwareDirPath(t *testing.T) {
Colin Crossff6c33d2019-10-02 16:01:35 -0700221 targetPath := buildDir + "/target/product/test_device"
Patrice Arruda057a8b12019-06-03 15:29:27 -0700222 tests := []struct {
223 description string
224 config string
225 expectedPath string
226 }{{
227 description: "prebuilt: system firmware",
228 config: `
229 prebuilt_firmware {
230 name: "foo.conf",
231 src: "foo.conf",
232 }`,
233 expectedPath: filepath.Join(targetPath, "system/etc/firmware"),
234 }, {
235 description: "prebuilt: vendor firmware",
236 config: `
237 prebuilt_firmware {
238 name: "foo.conf",
239 src: "foo.conf",
240 soc_specific: true,
241 sub_dir: "sub_dir",
242 }`,
243 expectedPath: filepath.Join(targetPath, "vendor/firmware/sub_dir"),
244 }}
245 for _, tt := range tests {
246 t.Run(tt.description, func(t *testing.T) {
247 ctx, _ := testPrebuiltEtc(t, tt.config)
Colin Cross7113d202019-11-20 16:39:12 -0800248 p := ctx.ModuleForTests("foo.conf", "android_arm64_armv8-a").Module().(*PrebuiltEtc)
Colin Crossff6c33d2019-10-02 16:01:35 -0700249 if p.installDirPath.String() != tt.expectedPath {
Patrice Arruda057a8b12019-06-03 15:29:27 -0700250 t.Errorf("expected %q, got %q", tt.expectedPath, p.installDirPath)
251 }
252 })
253 }
254}