blob: 3f2f16f51ebf92d5cd54fd38fd783b9e694d1323 [file] [log] [blame]
Jaewoong Jung4b79e982020-06-01 10:45:49 -07001package sh
Jaewoong Jung8eaeb092019-05-16 14:58:29 -07002
3import (
Jaewoong Jung4b79e982020-06-01 10:45:49 -07004 "io/ioutil"
5 "os"
Jaewoong Jung91dbd522020-05-29 16:15:32 -07006 "path/filepath"
Jaewoong Jung8eaeb092019-05-16 14:58:29 -07007 "reflect"
8 "testing"
Jaewoong Jung4b79e982020-06-01 10:45:49 -07009
10 "android/soong/android"
Jaewoong Jung91dbd522020-05-29 16:15:32 -070011 "android/soong/cc"
Jaewoong Jung8eaeb092019-05-16 14:58:29 -070012)
13
Jaewoong Jung4b79e982020-06-01 10:45:49 -070014var buildDir string
15
16func setUp() {
17 var err error
18 buildDir, err = ioutil.TempDir("", "soong_sh_test")
19 if err != nil {
20 panic(err)
21 }
22}
23
24func tearDown() {
25 os.RemoveAll(buildDir)
26}
27
28func TestMain(m *testing.M) {
29 run := func() int {
30 setUp()
31 defer tearDown()
32
33 return m.Run()
34 }
35
36 os.Exit(run())
37}
38
39func testShBinary(t *testing.T, bp string) (*android.TestContext, android.Config) {
Colin Cross98be1bb2019-12-13 20:41:13 -080040 fs := map[string][]byte{
Jaewoong Jung8eaeb092019-05-16 14:58:29 -070041 "test.sh": nil,
42 "testdata/data1": nil,
43 "testdata/sub/data2": nil,
44 }
Colin Cross98be1bb2019-12-13 20:41:13 -080045
Jaewoong Jung4b79e982020-06-01 10:45:49 -070046 config := android.TestArchConfig(buildDir, nil, bp, fs)
Colin Cross98be1bb2019-12-13 20:41:13 -080047
Jaewoong Jung4b79e982020-06-01 10:45:49 -070048 ctx := android.NewTestArchContext()
Colin Cross98be1bb2019-12-13 20:41:13 -080049 ctx.RegisterModuleType("sh_test", ShTestFactory)
50 ctx.RegisterModuleType("sh_test_host", ShTestHostFactory)
Jaewoong Jung91dbd522020-05-29 16:15:32 -070051
52 cc.RegisterRequiredBuildComponentsForTest(ctx)
53
Colin Cross98be1bb2019-12-13 20:41:13 -080054 ctx.Register(config)
Jaewoong Jung8eaeb092019-05-16 14:58:29 -070055 _, errs := ctx.ParseFileList(".", []string{"Android.bp"})
Jaewoong Jung4b79e982020-06-01 10:45:49 -070056 android.FailIfErrored(t, errs)
Jaewoong Jung8eaeb092019-05-16 14:58:29 -070057 _, errs = ctx.PrepareBuildActions(config)
Jaewoong Jung4b79e982020-06-01 10:45:49 -070058 android.FailIfErrored(t, errs)
Jaewoong Jung8eaeb092019-05-16 14:58:29 -070059
60 return ctx, config
61}
62
Jaewoong Jung4aedc862020-06-10 17:23:46 -070063func TestShTestSubDir(t *testing.T) {
64 ctx, config := testShBinary(t, `
65 sh_test {
66 name: "foo",
67 src: "test.sh",
68 sub_dir: "foo_test"
69 }
70 `)
71
72 mod := ctx.ModuleForTests("foo", "android_arm64_armv8-a").Module().(*ShTest)
73
74 entries := android.AndroidMkEntriesForTest(t, config, "", mod)[0]
75
76 expectedPath := "/tmp/target/product/test_device/data/nativetest64/foo_test"
77 actualPath := entries.EntryMap["LOCAL_MODULE_PATH"][0]
78 if expectedPath != actualPath {
79 t.Errorf("Unexpected LOCAL_MODULE_PATH expected: %q, actual: %q", expectedPath, actualPath)
80 }
81}
82
83func TestShTest(t *testing.T) {
Jaewoong Jung8eaeb092019-05-16 14:58:29 -070084 ctx, config := testShBinary(t, `
85 sh_test {
86 name: "foo",
87 src: "test.sh",
88 filename: "test.sh",
89 data: [
90 "testdata/data1",
91 "testdata/sub/data2",
92 ],
93 }
94 `)
95
96 mod := ctx.ModuleForTests("foo", "android_arm64_armv8-a").Module().(*ShTest)
97
Jaewoong Jung4b79e982020-06-01 10:45:49 -070098 entries := android.AndroidMkEntriesForTest(t, config, "", mod)[0]
Jaewoong Jung4aedc862020-06-10 17:23:46 -070099
100 expectedPath := "/tmp/target/product/test_device/data/nativetest64/foo"
101 actualPath := entries.EntryMap["LOCAL_MODULE_PATH"][0]
102 if expectedPath != actualPath {
103 t.Errorf("Unexpected LOCAL_MODULE_PATH expected: %q, actual: %q", expectedPath, actualPath)
104 }
105
106 expectedData := []string{":testdata/data1", ":testdata/sub/data2"}
107 actualData := entries.EntryMap["LOCAL_TEST_DATA"]
108 if !reflect.DeepEqual(expectedData, actualData) {
109 t.Errorf("Unexpected test data expected: %q, actual: %q", expectedData, actualData)
Jaewoong Jung8eaeb092019-05-16 14:58:29 -0700110 }
111}
Jaewoong Jung61a83682019-07-01 09:08:50 -0700112
Jaewoong Jung91dbd522020-05-29 16:15:32 -0700113func TestShTest_dataModules(t *testing.T) {
114 ctx, config := testShBinary(t, `
115 sh_test {
116 name: "foo",
117 src: "test.sh",
118 host_supported: true,
119 data_bins: ["bar"],
120 data_libs: ["libbar"],
121 }
122
123 cc_binary {
124 name: "bar",
125 host_supported: true,
126 shared_libs: ["libbar"],
127 no_libcrt: true,
128 nocrt: true,
129 system_shared_libs: [],
130 stl: "none",
131 }
132
133 cc_library {
134 name: "libbar",
135 host_supported: true,
136 no_libcrt: true,
137 nocrt: true,
138 system_shared_libs: [],
139 stl: "none",
140 }
141 `)
142
143 arches := []string{"android_arm64_armv8-a", "linux_glibc_x86_64"}
144 for _, arch := range arches {
145 variant := ctx.ModuleForTests("foo", arch)
146
147 relocated := variant.Output("relocated/lib64/libbar.so")
148 expectedInput := filepath.Join(buildDir, ".intermediates/libbar/"+arch+"_shared/libbar.so")
149 if relocated.Input.String() != expectedInput {
150 t.Errorf("Unexpected relocation input, expected: %q, actual: %q",
151 expectedInput, relocated.Input.String())
152 }
153
154 mod := variant.Module().(*ShTest)
155 entries := android.AndroidMkEntriesForTest(t, config, "", mod)[0]
156 expectedData := []string{
157 filepath.Join(buildDir, ".intermediates/bar", arch, ":bar"),
158 filepath.Join(buildDir, ".intermediates/foo", arch, "relocated/:lib64/libbar.so"),
159 }
160 actualData := entries.EntryMap["LOCAL_TEST_DATA"]
161 if !reflect.DeepEqual(expectedData, actualData) {
162 t.Errorf("Unexpected test data, expected: %q, actual: %q", expectedData, actualData)
163 }
164 }
165}
166
Jaewoong Jung61a83682019-07-01 09:08:50 -0700167func TestShTestHost(t *testing.T) {
168 ctx, _ := testShBinary(t, `
169 sh_test_host {
170 name: "foo",
171 src: "test.sh",
172 filename: "test.sh",
173 data: [
174 "testdata/data1",
175 "testdata/sub/data2",
176 ],
177 }
178 `)
179
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700180 buildOS := android.BuildOs.String()
Jaewoong Jung61a83682019-07-01 09:08:50 -0700181 mod := ctx.ModuleForTests("foo", buildOS+"_x86_64").Module().(*ShTest)
182 if !mod.Host() {
183 t.Errorf("host bit is not set for a sh_test_host module.")
184 }
185}
Jaewoong Jung91dbd522020-05-29 16:15:32 -0700186
187func TestShTestHost_dataDeviceModules(t *testing.T) {
188 ctx, config := testShBinary(t, `
189 sh_test_host {
190 name: "foo",
191 src: "test.sh",
192 data_device_bins: ["bar"],
193 data_device_libs: ["libbar"],
194 }
195
196 cc_binary {
197 name: "bar",
198 shared_libs: ["libbar"],
199 no_libcrt: true,
200 nocrt: true,
201 system_shared_libs: [],
202 stl: "none",
203 }
204
205 cc_library {
206 name: "libbar",
207 no_libcrt: true,
208 nocrt: true,
209 system_shared_libs: [],
210 stl: "none",
211 }
212 `)
213
Jaewoong Jung181cfdf2020-08-14 11:40:38 -0700214 buildOS := android.BuildOs.String()
215 variant := ctx.ModuleForTests("foo", buildOS+"_x86_64")
Jaewoong Jung91dbd522020-05-29 16:15:32 -0700216
217 relocated := variant.Output("relocated/lib64/libbar.so")
218 expectedInput := filepath.Join(buildDir, ".intermediates/libbar/android_arm64_armv8-a_shared/libbar.so")
219 if relocated.Input.String() != expectedInput {
220 t.Errorf("Unexpected relocation input, expected: %q, actual: %q",
221 expectedInput, relocated.Input.String())
222 }
223
224 mod := variant.Module().(*ShTest)
225 entries := android.AndroidMkEntriesForTest(t, config, "", mod)[0]
226 expectedData := []string{
227 filepath.Join(buildDir, ".intermediates/bar/android_arm64_armv8-a/:bar"),
228 // libbar has been relocated, and so has a variant that matches the host arch.
229 filepath.Join(buildDir, ".intermediates/foo/linux_glibc_x86_64/relocated/:lib64/libbar.so"),
230 }
231 actualData := entries.EntryMap["LOCAL_TEST_DATA"]
232 if !reflect.DeepEqual(expectedData, actualData) {
233 t.Errorf("Unexpected test data, expected: %q, actual: %q", expectedData, actualData)
234 }
235}