1// 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 etc
16
17import (
18	"os"
19	"path/filepath"
20	"testing"
21
22	"android/soong/android"
23)
24
25func TestMain(m *testing.M) {
26	os.Exit(m.Run())
27}
28
29var prepareForPrebuiltEtcTest = android.GroupFixturePreparers(
30	android.PrepareForTestWithArchMutator,
31	PrepareForTestWithPrebuiltEtc,
32	android.FixtureMergeMockFs(android.MockFS{
33		"foo.conf": nil,
34		"bar.conf": nil,
35		"baz.conf": nil,
36	}),
37)
38
39func TestPrebuiltEtcVariants(t *testing.T) {
40	result := prepareForPrebuiltEtcTest.RunTestWithBp(t, `
41		prebuilt_etc {
42			name: "foo.conf",
43			src: "foo.conf",
44		}
45		prebuilt_etc {
46			name: "bar.conf",
47			src: "bar.conf",
48			recovery_available: true,
49		}
50		prebuilt_etc {
51			name: "baz.conf",
52			src: "baz.conf",
53			recovery: true,
54		}
55	`)
56
57	foo_variants := result.ModuleVariantsForTests("foo.conf")
58	if len(foo_variants) != 1 {
59		t.Errorf("expected 1, got %#v", foo_variants)
60	}
61
62	bar_variants := result.ModuleVariantsForTests("bar.conf")
63	if len(bar_variants) != 2 {
64		t.Errorf("expected 2, got %#v", bar_variants)
65	}
66
67	baz_variants := result.ModuleVariantsForTests("baz.conf")
68	if len(baz_variants) != 1 {
69		t.Errorf("expected 1, got %#v", bar_variants)
70	}
71}
72
73func TestPrebuiltEtcOutputPath(t *testing.T) {
74	result := prepareForPrebuiltEtcTest.RunTestWithBp(t, `
75		prebuilt_etc {
76			name: "foo.conf",
77			src: "foo.conf",
78			filename: "foo.installed.conf",
79		}
80	`)
81
82	p := result.Module("foo.conf", "android_arm64_armv8-a").(*PrebuiltEtc)
83	android.AssertStringEquals(t, "output file path", "foo.installed.conf", p.outputFilePath.Base())
84}
85
86func TestPrebuiltEtcGlob(t *testing.T) {
87	result := prepareForPrebuiltEtcTest.RunTestWithBp(t, `
88		prebuilt_etc {
89			name: "my_foo",
90			src: "foo.*",
91		}
92		prebuilt_etc {
93			name: "my_bar",
94			src: "bar.*",
95			filename_from_src: true,
96		}
97	`)
98
99	p := result.Module("my_foo", "android_arm64_armv8-a").(*PrebuiltEtc)
100	android.AssertStringEquals(t, "my_foo output file path", "my_foo", p.outputFilePath.Base())
101
102	p = result.Module("my_bar", "android_arm64_armv8-a").(*PrebuiltEtc)
103	android.AssertStringEquals(t, "my_bar output file path", "bar.conf", p.outputFilePath.Base())
104}
105
106func TestPrebuiltEtcAndroidMk(t *testing.T) {
107	result := prepareForPrebuiltEtcTest.RunTestWithBp(t, `
108		prebuilt_etc {
109			name: "foo",
110			src: "foo.conf",
111			owner: "abc",
112			filename_from_src: true,
113			required: ["modA", "moduleB"],
114			host_required: ["hostModA", "hostModB"],
115			target_required: ["targetModA"],
116		}
117	`)
118
119	expected := map[string][]string{
120		"LOCAL_MODULE":                  {"foo"},
121		"LOCAL_MODULE_CLASS":            {"ETC"},
122		"LOCAL_MODULE_OWNER":            {"abc"},
123		"LOCAL_INSTALLED_MODULE_STEM":   {"foo.conf"},
124		"LOCAL_REQUIRED_MODULES":        {"modA", "moduleB"},
125		"LOCAL_HOST_REQUIRED_MODULES":   {"hostModA", "hostModB"},
126		"LOCAL_TARGET_REQUIRED_MODULES": {"targetModA"},
127	}
128
129	mod := result.Module("foo", "android_arm64_armv8-a").(*PrebuiltEtc)
130	entries := android.AndroidMkEntriesForTest(t, result.TestContext, mod)[0]
131	for k, expectedValue := range expected {
132		if value, ok := entries.EntryMap[k]; ok {
133			android.AssertDeepEquals(t, k, expectedValue, value)
134		} else {
135			t.Errorf("No %s defined, saw %q", k, entries.EntryMap)
136		}
137	}
138}
139
140func TestPrebuiltEtcRelativeInstallPathInstallDirPath(t *testing.T) {
141	result := prepareForPrebuiltEtcTest.RunTestWithBp(t, `
142		prebuilt_etc {
143			name: "foo.conf",
144			src: "foo.conf",
145			relative_install_path: "bar",
146		}
147	`)
148
149	p := result.Module("foo.conf", "android_arm64_armv8-a").(*PrebuiltEtc)
150	expected := "out/soong/target/product/test_device/system/etc/bar"
151	android.AssertPathRelativeToTopEquals(t, "install dir", expected, p.installDirPath)
152}
153
154func TestPrebuiltEtcCannotSetRelativeInstallPathAndSubDir(t *testing.T) {
155	prepareForPrebuiltEtcTest.
156		ExtendWithErrorHandler(android.FixtureExpectsAtLeastOneErrorMatchingPattern("relative_install_path is set. Cannot set sub_dir")).
157		RunTestWithBp(t, `
158			prebuilt_etc {
159				name: "foo.conf",
160				src: "foo.conf",
161				sub_dir: "bar",
162				relative_install_path: "bar",
163			}
164		`)
165}
166
167func TestPrebuiltEtcHost(t *testing.T) {
168	result := prepareForPrebuiltEtcTest.RunTestWithBp(t, `
169		prebuilt_etc_host {
170			name: "foo.conf",
171			src: "foo.conf",
172		}
173	`)
174
175	buildOS := android.BuildOs.String()
176	p := result.Module("foo.conf", buildOS+"_common").(*PrebuiltEtc)
177	if !p.Host() {
178		t.Errorf("host bit is not set for a prebuilt_etc_host module.")
179	}
180}
181
182func TestPrebuiltRootInstallDirPath(t *testing.T) {
183	result := prepareForPrebuiltEtcTest.RunTestWithBp(t, `
184		prebuilt_root {
185			name: "foo.conf",
186			src: "foo.conf",
187			filename: "foo.conf",
188		}
189	`)
190
191	p := result.Module("foo.conf", "android_arm64_armv8-a").(*PrebuiltEtc)
192	expected := "out/soong/target/product/test_device/system"
193	android.AssertPathRelativeToTopEquals(t, "install dir", expected, p.installDirPath)
194}
195
196func TestPrebuiltRootInstallDirPathValidate(t *testing.T) {
197	prepareForPrebuiltEtcTest.ExtendWithErrorHandler(android.FixtureExpectsAtLeastOneErrorMatchingPattern("filename cannot contain separator")).RunTestWithBp(t, `
198		prebuilt_root {
199			name: "foo.conf",
200			src: "foo.conf",
201			filename: "foo/bar.conf",
202		}
203	`)
204}
205
206func TestPrebuiltUserShareInstallDirPath(t *testing.T) {
207	result := prepareForPrebuiltEtcTest.RunTestWithBp(t, `
208		prebuilt_usr_share {
209			name: "foo.conf",
210			src: "foo.conf",
211			sub_dir: "bar",
212		}
213	`)
214
215	p := result.Module("foo.conf", "android_arm64_armv8-a").(*PrebuiltEtc)
216	expected := "out/soong/target/product/test_device/system/usr/share/bar"
217	android.AssertPathRelativeToTopEquals(t, "install dir", expected, p.installDirPath)
218}
219
220func TestPrebuiltUserShareHostInstallDirPath(t *testing.T) {
221	result := prepareForPrebuiltEtcTest.RunTestWithBp(t, `
222		prebuilt_usr_share_host {
223			name: "foo.conf",
224			src: "foo.conf",
225			sub_dir: "bar",
226		}
227	`)
228
229	buildOS := android.BuildOs.String()
230	p := result.Module("foo.conf", buildOS+"_common").(*PrebuiltEtc)
231	expected := filepath.Join("out/soong/host", result.Config.PrebuiltOS(), "usr", "share", "bar")
232	android.AssertPathRelativeToTopEquals(t, "install dir", expected, p.installDirPath)
233}
234
235func TestPrebuiltFontInstallDirPath(t *testing.T) {
236	result := prepareForPrebuiltEtcTest.RunTestWithBp(t, `
237		prebuilt_font {
238			name: "foo.conf",
239			src: "foo.conf",
240		}
241	`)
242
243	p := result.Module("foo.conf", "android_arm64_armv8-a").(*PrebuiltEtc)
244	expected := "out/soong/target/product/test_device/system/fonts"
245	android.AssertPathRelativeToTopEquals(t, "install dir", expected, p.installDirPath)
246}
247
248func TestPrebuiltFirmwareDirPath(t *testing.T) {
249	targetPath := "out/soong/target/product/test_device"
250	tests := []struct {
251		description  string
252		config       string
253		expectedPath string
254	}{{
255		description: "prebuilt: system firmware",
256		config: `
257			prebuilt_firmware {
258				name: "foo.conf",
259				src: "foo.conf",
260			}`,
261		expectedPath: filepath.Join(targetPath, "system/etc/firmware"),
262	}, {
263		description: "prebuilt: vendor firmware",
264		config: `
265			prebuilt_firmware {
266				name: "foo.conf",
267				src: "foo.conf",
268				soc_specific: true,
269				sub_dir: "sub_dir",
270			}`,
271		expectedPath: filepath.Join(targetPath, "vendor/firmware/sub_dir"),
272	}}
273	for _, tt := range tests {
274		t.Run(tt.description, func(t *testing.T) {
275			result := prepareForPrebuiltEtcTest.RunTestWithBp(t, tt.config)
276			p := result.Module("foo.conf", "android_arm64_armv8-a").(*PrebuiltEtc)
277			android.AssertPathRelativeToTopEquals(t, "install dir", tt.expectedPath, p.installDirPath)
278		})
279	}
280}
281
282func TestPrebuiltDSPDirPath(t *testing.T) {
283	targetPath := "out/soong/target/product/test_device"
284	tests := []struct {
285		description  string
286		config       string
287		expectedPath string
288	}{{
289		description: "prebuilt: system dsp",
290		config: `
291			prebuilt_dsp {
292				name: "foo.conf",
293				src: "foo.conf",
294			}`,
295		expectedPath: filepath.Join(targetPath, "system/etc/dsp"),
296	}, {
297		description: "prebuilt: vendor dsp",
298		config: `
299			prebuilt_dsp {
300				name: "foo.conf",
301				src: "foo.conf",
302				soc_specific: true,
303				sub_dir: "sub_dir",
304			}`,
305		expectedPath: filepath.Join(targetPath, "vendor/dsp/sub_dir"),
306	}}
307	for _, tt := range tests {
308		t.Run(tt.description, func(t *testing.T) {
309			result := prepareForPrebuiltEtcTest.RunTestWithBp(t, tt.config)
310			p := result.Module("foo.conf", "android_arm64_armv8-a").(*PrebuiltEtc)
311			android.AssertPathRelativeToTopEquals(t, "install dir", tt.expectedPath, p.installDirPath)
312		})
313	}
314}
315
316func TestPrebuiltRFSADirPath(t *testing.T) {
317	targetPath := "out/soong/target/product/test_device"
318	tests := []struct {
319		description  string
320		config       string
321		expectedPath string
322	}{{
323		description: "prebuilt: system rfsa",
324		config: `
325			prebuilt_rfsa {
326				name: "foo.conf",
327				src: "foo.conf",
328			}`,
329		expectedPath: filepath.Join(targetPath, "system/lib/rfsa"),
330	}, {
331		description: "prebuilt: vendor rfsa",
332		config: `
333			prebuilt_rfsa {
334				name: "foo.conf",
335				src: "foo.conf",
336				soc_specific: true,
337				sub_dir: "sub_dir",
338			}`,
339		expectedPath: filepath.Join(targetPath, "vendor/lib/rfsa/sub_dir"),
340	}}
341	for _, tt := range tests {
342		t.Run(tt.description, func(t *testing.T) {
343			result := prepareForPrebuiltEtcTest.RunTestWithBp(t, tt.config)
344			p := result.Module("foo.conf", "android_arm64_armv8-a").(*PrebuiltEtc)
345			android.AssertPathRelativeToTopEquals(t, "install dir", tt.expectedPath, p.installDirPath)
346		})
347	}
348}
349