blob: e0ade7e50d111fd3a6e9b9ebe75208d071883976 [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 (
Anton Hanssonce0e2582019-02-04 14:19:27 +000018 "bufio"
19 "bytes"
Tao Bao0ba5c942018-08-14 22:20:22 -070020 "io/ioutil"
21 "os"
Patrice Arruda300cef92019-02-22 15:47:57 -080022 "path/filepath"
Anton Hanssonce0e2582019-02-04 14:19:27 +000023 "strings"
Tao Bao0ba5c942018-08-14 22:20:22 -070024 "testing"
25)
26
Patrice Arruda300cef92019-02-22 15:47:57 -080027func testPrebuiltEtc(t *testing.T, bp string) (*TestContext, Config) {
Tao Bao0ba5c942018-08-14 22:20:22 -070028 config, buildDir := setUp(t)
29 defer tearDown(buildDir)
30 ctx := NewTestArchContext()
31 ctx.RegisterModuleType("prebuilt_etc", ModuleFactoryAdaptor(PrebuiltEtcFactory))
Jaewoong Jung4b44fcd2019-02-07 08:28:03 -080032 ctx.RegisterModuleType("prebuilt_etc_host", ModuleFactoryAdaptor(PrebuiltEtcHostFactory))
Jaewoong Jungc3fcdb42019-02-13 05:50:33 -080033 ctx.RegisterModuleType("prebuilt_usr_share", ModuleFactoryAdaptor(PrebuiltUserShareFactory))
Patrice Arruda300cef92019-02-22 15:47:57 -080034 ctx.RegisterModuleType("prebuilt_usr_share_host", ModuleFactoryAdaptor(PrebuiltUserShareHostFactory))
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) {
Patrice Arruda300cef92019-02-22 15:47:57 -0800142 ctx, _ := 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,
148 }
149 `)
150
151 data := AndroidMkData{}
152 data.Required = append(data.Required, "modA", "moduleB")
153
154 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 }
161
162 mod := ctx.ModuleForTests("foo", "android_arm64_armv8-a_core").Module().(*PrebuiltEtc)
163 buf := &bytes.Buffer{}
164 mod.AndroidMk().Custom(buf, "foo", "", "", data)
165 for k, expected := range expected {
166 found := false
167 scanner := bufio.NewScanner(bytes.NewReader(buf.Bytes()))
168 for scanner.Scan() {
169 line := scanner.Text()
170 tok := strings.Split(line, " := ")
171 if tok[0] == k {
172 found = true
173 if tok[1] != expected {
174 t.Errorf("Incorrect %s '%s', expected '%s'", k, tok[1], expected)
175 }
176 }
177 }
178
179 if !found {
180 t.Errorf("No %s defined, saw %s", k, buf.String())
181 }
182 }
183}
Jaewoong Jung24788182019-02-04 14:34:10 -0800184
185func TestPrebuiltEtcHost(t *testing.T) {
Patrice Arruda300cef92019-02-22 15:47:57 -0800186 ctx, _ := testPrebuiltEtc(t, `
Jaewoong Jung24788182019-02-04 14:34:10 -0800187 prebuilt_etc_host {
188 name: "foo.conf",
189 src: "foo.conf",
190 }
191 `)
192
193 buildOS := BuildOs.String()
194 p := ctx.ModuleForTests("foo.conf", buildOS+"_common").Module().(*PrebuiltEtc)
195 if !p.Host() {
196 t.Errorf("host bit is not set for a prebuilt_etc_host module.")
197 }
198}
Jaewoong Jungc3fcdb42019-02-13 05:50:33 -0800199
200func TestPrebuiltUserShareInstallDirPath(t *testing.T) {
Patrice Arruda300cef92019-02-22 15:47:57 -0800201 ctx, _ := testPrebuiltEtc(t, `
Jaewoong Jungc3fcdb42019-02-13 05:50:33 -0800202 prebuilt_usr_share {
203 name: "foo.conf",
204 src: "foo.conf",
205 sub_dir: "bar",
206 }
207 `)
208
209 p := ctx.ModuleForTests("foo.conf", "android_arm64_armv8-a_core").Module().(*PrebuiltEtc)
210 expected := "target/product/test_device/system/usr/share/bar"
211 if p.installDirPath.RelPathString() != expected {
212 t.Errorf("expected %q, got %q", expected, p.installDirPath.RelPathString())
213 }
214}
Patrice Arruda300cef92019-02-22 15:47:57 -0800215
216func TestPrebuiltUserShareHostInstallDirPath(t *testing.T) {
217 ctx, config := testPrebuiltEtc(t, `
218 prebuilt_usr_share_host {
219 name: "foo.conf",
220 src: "foo.conf",
221 sub_dir: "bar",
222 }
223 `)
224
225 buildOS := BuildOs.String()
226 p := ctx.ModuleForTests("foo.conf", buildOS+"_common").Module().(*PrebuiltEtc)
227 expected := filepath.Join("host", config.PrebuiltOS(), "usr", "share", "bar")
228 if p.installDirPath.RelPathString() != expected {
229 t.Errorf("expected %q, got %q", expected, p.installDirPath.RelPathString())
230 }
231}