blob: f8ccc7789090fc77b243c2f16f63a520a2711341 [file] [log] [blame]
Dan Willemsen34cc69e2015-09-23 15:26:20 -07001// Copyright 2015 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
Colin Cross635c3b02016-05-18 15:37:25 -070015package android
Dan Willemsen34cc69e2015-09-23 15:26:20 -070016
17import (
18 "errors"
19 "fmt"
20 "reflect"
Colin Cross27027c72020-02-28 15:34:17 -080021 "strconv"
Dan Willemsen34cc69e2015-09-23 15:26:20 -070022 "strings"
23 "testing"
Inseob Kimd9580b82021-04-13 21:13:49 +090024
25 "github.com/google/blueprint/proptools"
Dan Willemsen34cc69e2015-09-23 15:26:20 -070026)
27
28type strsTestCase struct {
29 in []string
30 out string
31 err []error
32}
33
34var commonValidatePathTestCases = []strsTestCase{
35 {
36 in: []string{""},
37 out: "",
38 },
39 {
40 in: []string{"a/b"},
41 out: "a/b",
42 },
43 {
44 in: []string{"a/b", "c"},
45 out: "a/b/c",
46 },
47 {
48 in: []string{"a/.."},
49 out: ".",
50 },
51 {
52 in: []string{"."},
53 out: ".",
54 },
55 {
56 in: []string{".."},
57 out: "",
58 err: []error{errors.New("Path is outside directory: ..")},
59 },
60 {
61 in: []string{"../a"},
62 out: "",
63 err: []error{errors.New("Path is outside directory: ../a")},
64 },
65 {
66 in: []string{"b/../../a"},
67 out: "",
68 err: []error{errors.New("Path is outside directory: ../a")},
69 },
70 {
71 in: []string{"/a"},
72 out: "",
73 err: []error{errors.New("Path is outside directory: /a")},
74 },
Dan Willemsen80a7c2a2015-12-21 14:57:11 -080075 {
76 in: []string{"a", "../b"},
77 out: "",
78 err: []error{errors.New("Path is outside directory: ../b")},
79 },
80 {
81 in: []string{"a", "b/../../c"},
82 out: "",
83 err: []error{errors.New("Path is outside directory: ../c")},
84 },
85 {
86 in: []string{"a", "./.."},
87 out: "",
88 err: []error{errors.New("Path is outside directory: ..")},
89 },
Dan Willemsen34cc69e2015-09-23 15:26:20 -070090}
91
92var validateSafePathTestCases = append(commonValidatePathTestCases, []strsTestCase{
93 {
94 in: []string{"$host/../$a"},
95 out: "$a",
96 },
97}...)
98
99var validatePathTestCases = append(commonValidatePathTestCases, []strsTestCase{
100 {
101 in: []string{"$host/../$a"},
102 out: "",
103 err: []error{errors.New("Path contains invalid character($): $host/../$a")},
104 },
105 {
106 in: []string{"$host/.."},
107 out: "",
108 err: []error{errors.New("Path contains invalid character($): $host/..")},
109 },
110}...)
111
112func TestValidateSafePath(t *testing.T) {
113 for _, testCase := range validateSafePathTestCases {
Colin Crossdc75ae72018-02-22 13:48:13 -0800114 t.Run(strings.Join(testCase.in, ","), func(t *testing.T) {
115 ctx := &configErrorWrapper{}
Colin Cross1ccfcc32018-02-22 13:54:26 -0800116 out, err := validateSafePath(testCase.in...)
117 if err != nil {
118 reportPathError(ctx, err)
119 }
Colin Crossdc75ae72018-02-22 13:48:13 -0800120 check(t, "validateSafePath", p(testCase.in), out, ctx.errors, testCase.out, testCase.err)
121 })
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700122 }
123}
124
125func TestValidatePath(t *testing.T) {
126 for _, testCase := range validatePathTestCases {
Colin Crossdc75ae72018-02-22 13:48:13 -0800127 t.Run(strings.Join(testCase.in, ","), func(t *testing.T) {
128 ctx := &configErrorWrapper{}
Colin Cross1ccfcc32018-02-22 13:54:26 -0800129 out, err := validatePath(testCase.in...)
130 if err != nil {
131 reportPathError(ctx, err)
132 }
Colin Crossdc75ae72018-02-22 13:48:13 -0800133 check(t, "validatePath", p(testCase.in), out, ctx.errors, testCase.out, testCase.err)
134 })
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700135 }
136}
137
138func TestOptionalPath(t *testing.T) {
139 var path OptionalPath
140 checkInvalidOptionalPath(t, path)
141
142 path = OptionalPathForPath(nil)
143 checkInvalidOptionalPath(t, path)
144}
145
146func checkInvalidOptionalPath(t *testing.T, path OptionalPath) {
Colin Crossdc75ae72018-02-22 13:48:13 -0800147 t.Helper()
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700148 if path.Valid() {
149 t.Errorf("Uninitialized OptionalPath should not be valid")
150 }
151 if path.String() != "" {
152 t.Errorf("Uninitialized OptionalPath String() should return \"\", not %q", path.String())
153 }
154 defer func() {
155 if r := recover(); r == nil {
156 t.Errorf("Expected a panic when calling Path() on an uninitialized OptionalPath")
157 }
158 }()
159 path.Path()
160}
161
162func check(t *testing.T, testType, testString string,
163 got interface{}, err []error,
164 expected interface{}, expectedErr []error) {
Colin Crossdc75ae72018-02-22 13:48:13 -0800165 t.Helper()
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700166
167 printedTestCase := false
168 e := func(s string, expected, got interface{}) {
Colin Crossdc75ae72018-02-22 13:48:13 -0800169 t.Helper()
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700170 if !printedTestCase {
171 t.Errorf("test case %s: %s", testType, testString)
172 printedTestCase = true
173 }
174 t.Errorf("incorrect %s", s)
175 t.Errorf(" expected: %s", p(expected))
176 t.Errorf(" got: %s", p(got))
177 }
178
179 if !reflect.DeepEqual(expectedErr, err) {
180 e("errors:", expectedErr, err)
181 }
182
183 if !reflect.DeepEqual(expected, got) {
184 e("output:", expected, got)
185 }
186}
187
188func p(in interface{}) string {
189 if v, ok := in.([]interface{}); ok {
190 s := make([]string, len(v))
191 for i := range v {
192 s[i] = fmt.Sprintf("%#v", v[i])
193 }
194 return "[" + strings.Join(s, ", ") + "]"
195 } else {
196 return fmt.Sprintf("%#v", in)
197 }
198}
Dan Willemsen00269f22017-07-06 16:59:48 -0700199
Colin Cross98be1bb2019-12-13 20:41:13 -0800200func pathTestConfig(buildDir string) Config {
201 return TestConfig(buildDir, nil, "", nil)
202}
203
Dan Willemsen00269f22017-07-06 16:59:48 -0700204func TestPathForModuleInstall(t *testing.T) {
Colin Cross98be1bb2019-12-13 20:41:13 -0800205 testConfig := pathTestConfig("")
Dan Willemsen00269f22017-07-06 16:59:48 -0700206
Jiyong Park87788b52020-09-01 12:37:45 +0900207 hostTarget := Target{Os: Linux, Arch: Arch{ArchType: X86}}
208 deviceTarget := Target{Os: Android, Arch: Arch{ArchType: Arm64}}
Dan Willemsen00269f22017-07-06 16:59:48 -0700209
210 testCases := []struct {
Jiyong Park957bcd92020-10-20 18:23:33 +0900211 name string
Ulya Trafimovichccc8c852020-10-14 11:29:07 +0100212 ctx *testModuleInstallPathContext
Jiyong Park957bcd92020-10-20 18:23:33 +0900213 in []string
214 out string
215 partitionDir string
Dan Willemsen00269f22017-07-06 16:59:48 -0700216 }{
217 {
218 name: "host binary",
Ulya Trafimovichccc8c852020-10-14 11:29:07 +0100219 ctx: &testModuleInstallPathContext{
Colin Cross0ea8ba82019-06-06 14:33:29 -0700220 baseModuleContext: baseModuleContext{
Colin Crossfb0c16e2019-11-20 17:12:35 -0800221 os: hostTarget.Os,
Dan Willemsen00269f22017-07-06 16:59:48 -0700222 target: hostTarget,
223 },
224 },
Jiyong Park957bcd92020-10-20 18:23:33 +0900225 in: []string{"bin", "my_test"},
226 out: "host/linux-x86/bin/my_test",
227 partitionDir: "host/linux-x86",
Dan Willemsen00269f22017-07-06 16:59:48 -0700228 },
229
230 {
231 name: "system binary",
Ulya Trafimovichccc8c852020-10-14 11:29:07 +0100232 ctx: &testModuleInstallPathContext{
Colin Cross0ea8ba82019-06-06 14:33:29 -0700233 baseModuleContext: baseModuleContext{
Colin Crossfb0c16e2019-11-20 17:12:35 -0800234 os: deviceTarget.Os,
Dan Willemsen00269f22017-07-06 16:59:48 -0700235 target: deviceTarget,
236 },
237 },
Jiyong Park957bcd92020-10-20 18:23:33 +0900238 in: []string{"bin", "my_test"},
239 out: "target/product/test_device/system/bin/my_test",
240 partitionDir: "target/product/test_device/system",
Dan Willemsen00269f22017-07-06 16:59:48 -0700241 },
242 {
243 name: "vendor binary",
Ulya Trafimovichccc8c852020-10-14 11:29:07 +0100244 ctx: &testModuleInstallPathContext{
Colin Cross0ea8ba82019-06-06 14:33:29 -0700245 baseModuleContext: baseModuleContext{
Colin Crossfb0c16e2019-11-20 17:12:35 -0800246 os: deviceTarget.Os,
Dan Willemsen00269f22017-07-06 16:59:48 -0700247 target: deviceTarget,
Colin Cross1184b642019-12-30 18:43:07 -0800248 earlyModuleContext: earlyModuleContext{
249 kind: socSpecificModule,
250 },
Dan Willemsen00269f22017-07-06 16:59:48 -0700251 },
252 },
Jiyong Park957bcd92020-10-20 18:23:33 +0900253 in: []string{"bin", "my_test"},
254 out: "target/product/test_device/vendor/bin/my_test",
255 partitionDir: "target/product/test_device/vendor",
Dan Willemsen00269f22017-07-06 16:59:48 -0700256 },
Jiyong Park2db76922017-11-08 16:03:48 +0900257 {
258 name: "odm binary",
Ulya Trafimovichccc8c852020-10-14 11:29:07 +0100259 ctx: &testModuleInstallPathContext{
Colin Cross0ea8ba82019-06-06 14:33:29 -0700260 baseModuleContext: baseModuleContext{
Colin Crossfb0c16e2019-11-20 17:12:35 -0800261 os: deviceTarget.Os,
Jiyong Park2db76922017-11-08 16:03:48 +0900262 target: deviceTarget,
Colin Cross1184b642019-12-30 18:43:07 -0800263 earlyModuleContext: earlyModuleContext{
264 kind: deviceSpecificModule,
265 },
Jiyong Park2db76922017-11-08 16:03:48 +0900266 },
267 },
Jiyong Park957bcd92020-10-20 18:23:33 +0900268 in: []string{"bin", "my_test"},
269 out: "target/product/test_device/odm/bin/my_test",
270 partitionDir: "target/product/test_device/odm",
Jiyong Park2db76922017-11-08 16:03:48 +0900271 },
272 {
Jaekyun Seok5cfbfbb2018-01-10 19:00:15 +0900273 name: "product binary",
Ulya Trafimovichccc8c852020-10-14 11:29:07 +0100274 ctx: &testModuleInstallPathContext{
Colin Cross0ea8ba82019-06-06 14:33:29 -0700275 baseModuleContext: baseModuleContext{
Colin Crossfb0c16e2019-11-20 17:12:35 -0800276 os: deviceTarget.Os,
Jiyong Park2db76922017-11-08 16:03:48 +0900277 target: deviceTarget,
Colin Cross1184b642019-12-30 18:43:07 -0800278 earlyModuleContext: earlyModuleContext{
279 kind: productSpecificModule,
280 },
Jiyong Park2db76922017-11-08 16:03:48 +0900281 },
282 },
Jiyong Park957bcd92020-10-20 18:23:33 +0900283 in: []string{"bin", "my_test"},
284 out: "target/product/test_device/product/bin/my_test",
285 partitionDir: "target/product/test_device/product",
Jiyong Park2db76922017-11-08 16:03:48 +0900286 },
Dario Frenifd05a742018-05-29 13:28:54 +0100287 {
Justin Yund5f6c822019-06-25 16:47:17 +0900288 name: "system_ext binary",
Ulya Trafimovichccc8c852020-10-14 11:29:07 +0100289 ctx: &testModuleInstallPathContext{
Colin Cross0ea8ba82019-06-06 14:33:29 -0700290 baseModuleContext: baseModuleContext{
Colin Crossfb0c16e2019-11-20 17:12:35 -0800291 os: deviceTarget.Os,
Dario Frenifd05a742018-05-29 13:28:54 +0100292 target: deviceTarget,
Colin Cross1184b642019-12-30 18:43:07 -0800293 earlyModuleContext: earlyModuleContext{
294 kind: systemExtSpecificModule,
295 },
Dario Frenifd05a742018-05-29 13:28:54 +0100296 },
297 },
Jiyong Park957bcd92020-10-20 18:23:33 +0900298 in: []string{"bin", "my_test"},
299 out: "target/product/test_device/system_ext/bin/my_test",
300 partitionDir: "target/product/test_device/system_ext",
Dario Frenifd05a742018-05-29 13:28:54 +0100301 },
Colin Cross90ba5f42019-10-02 11:10:58 -0700302 {
303 name: "root binary",
Ulya Trafimovichccc8c852020-10-14 11:29:07 +0100304 ctx: &testModuleInstallPathContext{
Colin Cross90ba5f42019-10-02 11:10:58 -0700305 baseModuleContext: baseModuleContext{
Colin Crossfb0c16e2019-11-20 17:12:35 -0800306 os: deviceTarget.Os,
Colin Cross90ba5f42019-10-02 11:10:58 -0700307 target: deviceTarget,
308 },
309 inRoot: true,
310 },
Jiyong Park957bcd92020-10-20 18:23:33 +0900311 in: []string{"my_test"},
312 out: "target/product/test_device/root/my_test",
313 partitionDir: "target/product/test_device/root",
Colin Cross90ba5f42019-10-02 11:10:58 -0700314 },
315 {
316 name: "recovery binary",
Ulya Trafimovichccc8c852020-10-14 11:29:07 +0100317 ctx: &testModuleInstallPathContext{
Colin Cross90ba5f42019-10-02 11:10:58 -0700318 baseModuleContext: baseModuleContext{
Colin Crossfb0c16e2019-11-20 17:12:35 -0800319 os: deviceTarget.Os,
Colin Cross90ba5f42019-10-02 11:10:58 -0700320 target: deviceTarget,
321 },
322 inRecovery: true,
323 },
Jiyong Park957bcd92020-10-20 18:23:33 +0900324 in: []string{"bin/my_test"},
325 out: "target/product/test_device/recovery/root/system/bin/my_test",
326 partitionDir: "target/product/test_device/recovery/root/system",
Colin Cross90ba5f42019-10-02 11:10:58 -0700327 },
328 {
329 name: "recovery root binary",
Ulya Trafimovichccc8c852020-10-14 11:29:07 +0100330 ctx: &testModuleInstallPathContext{
Colin Cross90ba5f42019-10-02 11:10:58 -0700331 baseModuleContext: baseModuleContext{
Colin Crossfb0c16e2019-11-20 17:12:35 -0800332 os: deviceTarget.Os,
Colin Cross90ba5f42019-10-02 11:10:58 -0700333 target: deviceTarget,
334 },
335 inRecovery: true,
336 inRoot: true,
337 },
Jiyong Park957bcd92020-10-20 18:23:33 +0900338 in: []string{"my_test"},
339 out: "target/product/test_device/recovery/root/my_test",
340 partitionDir: "target/product/test_device/recovery/root",
Colin Cross90ba5f42019-10-02 11:10:58 -0700341 },
Dan Willemsen00269f22017-07-06 16:59:48 -0700342
343 {
Inseob Kimd9580b82021-04-13 21:13:49 +0900344 name: "ramdisk binary",
345 ctx: &testModuleInstallPathContext{
346 baseModuleContext: baseModuleContext{
347 os: deviceTarget.Os,
348 target: deviceTarget,
349 },
350 inRamdisk: true,
351 },
352 in: []string{"my_test"},
353 out: "target/product/test_device/ramdisk/system/my_test",
354 partitionDir: "target/product/test_device/ramdisk/system",
355 },
356 {
357 name: "ramdisk root binary",
358 ctx: &testModuleInstallPathContext{
359 baseModuleContext: baseModuleContext{
360 os: deviceTarget.Os,
361 target: deviceTarget,
362 },
363 inRamdisk: true,
364 inRoot: true,
365 },
366 in: []string{"my_test"},
367 out: "target/product/test_device/ramdisk/my_test",
368 partitionDir: "target/product/test_device/ramdisk",
369 },
370 {
371 name: "vendor_ramdisk binary",
372 ctx: &testModuleInstallPathContext{
373 baseModuleContext: baseModuleContext{
374 os: deviceTarget.Os,
375 target: deviceTarget,
376 },
377 inVendorRamdisk: true,
378 },
379 in: []string{"my_test"},
380 out: "target/product/test_device/vendor_ramdisk/system/my_test",
381 partitionDir: "target/product/test_device/vendor_ramdisk/system",
382 },
383 {
384 name: "vendor_ramdisk root binary",
385 ctx: &testModuleInstallPathContext{
386 baseModuleContext: baseModuleContext{
387 os: deviceTarget.Os,
388 target: deviceTarget,
389 },
390 inVendorRamdisk: true,
391 inRoot: true,
392 },
393 in: []string{"my_test"},
394 out: "target/product/test_device/vendor_ramdisk/my_test",
395 partitionDir: "target/product/test_device/vendor_ramdisk",
396 },
397 {
Inseob Kim08758f02021-04-08 21:13:22 +0900398 name: "debug_ramdisk binary",
399 ctx: &testModuleInstallPathContext{
400 baseModuleContext: baseModuleContext{
401 os: deviceTarget.Os,
402 target: deviceTarget,
403 },
404 inDebugRamdisk: true,
405 },
406 in: []string{"my_test"},
407 out: "target/product/test_device/debug_ramdisk/my_test",
408 partitionDir: "target/product/test_device/debug_ramdisk",
409 },
410 {
Dan Willemsen00269f22017-07-06 16:59:48 -0700411 name: "system native test binary",
Ulya Trafimovichccc8c852020-10-14 11:29:07 +0100412 ctx: &testModuleInstallPathContext{
Colin Cross0ea8ba82019-06-06 14:33:29 -0700413 baseModuleContext: baseModuleContext{
Colin Crossfb0c16e2019-11-20 17:12:35 -0800414 os: deviceTarget.Os,
Dan Willemsen00269f22017-07-06 16:59:48 -0700415 target: deviceTarget,
416 },
417 inData: true,
418 },
Jiyong Park957bcd92020-10-20 18:23:33 +0900419 in: []string{"nativetest", "my_test"},
420 out: "target/product/test_device/data/nativetest/my_test",
421 partitionDir: "target/product/test_device/data",
Dan Willemsen00269f22017-07-06 16:59:48 -0700422 },
423 {
424 name: "vendor native test binary",
Ulya Trafimovichccc8c852020-10-14 11:29:07 +0100425 ctx: &testModuleInstallPathContext{
Colin Cross0ea8ba82019-06-06 14:33:29 -0700426 baseModuleContext: baseModuleContext{
Colin Crossfb0c16e2019-11-20 17:12:35 -0800427 os: deviceTarget.Os,
Dan Willemsen00269f22017-07-06 16:59:48 -0700428 target: deviceTarget,
Colin Cross1184b642019-12-30 18:43:07 -0800429 earlyModuleContext: earlyModuleContext{
430 kind: socSpecificModule,
431 },
Jiyong Park2db76922017-11-08 16:03:48 +0900432 },
433 inData: true,
434 },
Jiyong Park957bcd92020-10-20 18:23:33 +0900435 in: []string{"nativetest", "my_test"},
436 out: "target/product/test_device/data/nativetest/my_test",
437 partitionDir: "target/product/test_device/data",
Jiyong Park2db76922017-11-08 16:03:48 +0900438 },
439 {
440 name: "odm native test binary",
Ulya Trafimovichccc8c852020-10-14 11:29:07 +0100441 ctx: &testModuleInstallPathContext{
Colin Cross0ea8ba82019-06-06 14:33:29 -0700442 baseModuleContext: baseModuleContext{
Colin Crossfb0c16e2019-11-20 17:12:35 -0800443 os: deviceTarget.Os,
Jiyong Park2db76922017-11-08 16:03:48 +0900444 target: deviceTarget,
Colin Cross1184b642019-12-30 18:43:07 -0800445 earlyModuleContext: earlyModuleContext{
446 kind: deviceSpecificModule,
447 },
Jiyong Park2db76922017-11-08 16:03:48 +0900448 },
449 inData: true,
450 },
Jiyong Park957bcd92020-10-20 18:23:33 +0900451 in: []string{"nativetest", "my_test"},
452 out: "target/product/test_device/data/nativetest/my_test",
453 partitionDir: "target/product/test_device/data",
Jiyong Park2db76922017-11-08 16:03:48 +0900454 },
455 {
Jaekyun Seok5cfbfbb2018-01-10 19:00:15 +0900456 name: "product native test binary",
Ulya Trafimovichccc8c852020-10-14 11:29:07 +0100457 ctx: &testModuleInstallPathContext{
Colin Cross0ea8ba82019-06-06 14:33:29 -0700458 baseModuleContext: baseModuleContext{
Colin Crossfb0c16e2019-11-20 17:12:35 -0800459 os: deviceTarget.Os,
Jiyong Park2db76922017-11-08 16:03:48 +0900460 target: deviceTarget,
Colin Cross1184b642019-12-30 18:43:07 -0800461 earlyModuleContext: earlyModuleContext{
462 kind: productSpecificModule,
463 },
Dan Willemsen00269f22017-07-06 16:59:48 -0700464 },
465 inData: true,
466 },
Jiyong Park957bcd92020-10-20 18:23:33 +0900467 in: []string{"nativetest", "my_test"},
468 out: "target/product/test_device/data/nativetest/my_test",
469 partitionDir: "target/product/test_device/data",
Dan Willemsen00269f22017-07-06 16:59:48 -0700470 },
471
472 {
Justin Yund5f6c822019-06-25 16:47:17 +0900473 name: "system_ext native test binary",
Ulya Trafimovichccc8c852020-10-14 11:29:07 +0100474 ctx: &testModuleInstallPathContext{
Colin Cross0ea8ba82019-06-06 14:33:29 -0700475 baseModuleContext: baseModuleContext{
Colin Crossfb0c16e2019-11-20 17:12:35 -0800476 os: deviceTarget.Os,
Dario Frenifd05a742018-05-29 13:28:54 +0100477 target: deviceTarget,
Colin Cross1184b642019-12-30 18:43:07 -0800478 earlyModuleContext: earlyModuleContext{
479 kind: systemExtSpecificModule,
480 },
Dario Frenifd05a742018-05-29 13:28:54 +0100481 },
482 inData: true,
483 },
Jiyong Park957bcd92020-10-20 18:23:33 +0900484 in: []string{"nativetest", "my_test"},
485 out: "target/product/test_device/data/nativetest/my_test",
486 partitionDir: "target/product/test_device/data",
Dario Frenifd05a742018-05-29 13:28:54 +0100487 },
488
489 {
Dan Willemsen00269f22017-07-06 16:59:48 -0700490 name: "sanitized system binary",
Ulya Trafimovichccc8c852020-10-14 11:29:07 +0100491 ctx: &testModuleInstallPathContext{
Colin Cross0ea8ba82019-06-06 14:33:29 -0700492 baseModuleContext: baseModuleContext{
Colin Crossfb0c16e2019-11-20 17:12:35 -0800493 os: deviceTarget.Os,
Dan Willemsen00269f22017-07-06 16:59:48 -0700494 target: deviceTarget,
495 },
496 inSanitizerDir: true,
497 },
Jiyong Park957bcd92020-10-20 18:23:33 +0900498 in: []string{"bin", "my_test"},
499 out: "target/product/test_device/data/asan/system/bin/my_test",
500 partitionDir: "target/product/test_device/data/asan/system",
Dan Willemsen00269f22017-07-06 16:59:48 -0700501 },
502 {
503 name: "sanitized vendor binary",
Ulya Trafimovichccc8c852020-10-14 11:29:07 +0100504 ctx: &testModuleInstallPathContext{
Colin Cross0ea8ba82019-06-06 14:33:29 -0700505 baseModuleContext: baseModuleContext{
Colin Crossfb0c16e2019-11-20 17:12:35 -0800506 os: deviceTarget.Os,
Dan Willemsen00269f22017-07-06 16:59:48 -0700507 target: deviceTarget,
Colin Cross1184b642019-12-30 18:43:07 -0800508 earlyModuleContext: earlyModuleContext{
509 kind: socSpecificModule,
510 },
Dan Willemsen00269f22017-07-06 16:59:48 -0700511 },
512 inSanitizerDir: true,
513 },
Jiyong Park957bcd92020-10-20 18:23:33 +0900514 in: []string{"bin", "my_test"},
515 out: "target/product/test_device/data/asan/vendor/bin/my_test",
516 partitionDir: "target/product/test_device/data/asan/vendor",
Dan Willemsen00269f22017-07-06 16:59:48 -0700517 },
Jiyong Park2db76922017-11-08 16:03:48 +0900518 {
519 name: "sanitized odm binary",
Ulya Trafimovichccc8c852020-10-14 11:29:07 +0100520 ctx: &testModuleInstallPathContext{
Colin Cross0ea8ba82019-06-06 14:33:29 -0700521 baseModuleContext: baseModuleContext{
Colin Crossfb0c16e2019-11-20 17:12:35 -0800522 os: deviceTarget.Os,
Jiyong Park2db76922017-11-08 16:03:48 +0900523 target: deviceTarget,
Colin Cross1184b642019-12-30 18:43:07 -0800524 earlyModuleContext: earlyModuleContext{
525 kind: deviceSpecificModule,
526 },
Jiyong Park2db76922017-11-08 16:03:48 +0900527 },
528 inSanitizerDir: true,
529 },
Jiyong Park957bcd92020-10-20 18:23:33 +0900530 in: []string{"bin", "my_test"},
531 out: "target/product/test_device/data/asan/odm/bin/my_test",
532 partitionDir: "target/product/test_device/data/asan/odm",
Jiyong Park2db76922017-11-08 16:03:48 +0900533 },
534 {
Jaekyun Seok5cfbfbb2018-01-10 19:00:15 +0900535 name: "sanitized product binary",
Ulya Trafimovichccc8c852020-10-14 11:29:07 +0100536 ctx: &testModuleInstallPathContext{
Colin Cross0ea8ba82019-06-06 14:33:29 -0700537 baseModuleContext: baseModuleContext{
Colin Crossfb0c16e2019-11-20 17:12:35 -0800538 os: deviceTarget.Os,
Jiyong Park2db76922017-11-08 16:03:48 +0900539 target: deviceTarget,
Colin Cross1184b642019-12-30 18:43:07 -0800540 earlyModuleContext: earlyModuleContext{
541 kind: productSpecificModule,
542 },
Jiyong Park2db76922017-11-08 16:03:48 +0900543 },
544 inSanitizerDir: true,
545 },
Jiyong Park957bcd92020-10-20 18:23:33 +0900546 in: []string{"bin", "my_test"},
547 out: "target/product/test_device/data/asan/product/bin/my_test",
548 partitionDir: "target/product/test_device/data/asan/product",
Jiyong Park2db76922017-11-08 16:03:48 +0900549 },
Dan Willemsen00269f22017-07-06 16:59:48 -0700550
551 {
Justin Yund5f6c822019-06-25 16:47:17 +0900552 name: "sanitized system_ext binary",
Ulya Trafimovichccc8c852020-10-14 11:29:07 +0100553 ctx: &testModuleInstallPathContext{
Colin Cross0ea8ba82019-06-06 14:33:29 -0700554 baseModuleContext: baseModuleContext{
Colin Crossfb0c16e2019-11-20 17:12:35 -0800555 os: deviceTarget.Os,
Dario Frenifd05a742018-05-29 13:28:54 +0100556 target: deviceTarget,
Colin Cross1184b642019-12-30 18:43:07 -0800557 earlyModuleContext: earlyModuleContext{
558 kind: systemExtSpecificModule,
559 },
Dario Frenifd05a742018-05-29 13:28:54 +0100560 },
561 inSanitizerDir: true,
562 },
Jiyong Park957bcd92020-10-20 18:23:33 +0900563 in: []string{"bin", "my_test"},
564 out: "target/product/test_device/data/asan/system_ext/bin/my_test",
565 partitionDir: "target/product/test_device/data/asan/system_ext",
Dario Frenifd05a742018-05-29 13:28:54 +0100566 },
567
568 {
Dan Willemsen00269f22017-07-06 16:59:48 -0700569 name: "sanitized system native test binary",
Ulya Trafimovichccc8c852020-10-14 11:29:07 +0100570 ctx: &testModuleInstallPathContext{
Colin Cross0ea8ba82019-06-06 14:33:29 -0700571 baseModuleContext: baseModuleContext{
Colin Crossfb0c16e2019-11-20 17:12:35 -0800572 os: deviceTarget.Os,
Dan Willemsen00269f22017-07-06 16:59:48 -0700573 target: deviceTarget,
574 },
575 inData: true,
576 inSanitizerDir: true,
577 },
Jiyong Park957bcd92020-10-20 18:23:33 +0900578 in: []string{"nativetest", "my_test"},
579 out: "target/product/test_device/data/asan/data/nativetest/my_test",
580 partitionDir: "target/product/test_device/data/asan/data",
Dan Willemsen00269f22017-07-06 16:59:48 -0700581 },
582 {
583 name: "sanitized vendor native test binary",
Ulya Trafimovichccc8c852020-10-14 11:29:07 +0100584 ctx: &testModuleInstallPathContext{
Colin Cross0ea8ba82019-06-06 14:33:29 -0700585 baseModuleContext: baseModuleContext{
Colin Crossfb0c16e2019-11-20 17:12:35 -0800586 os: deviceTarget.Os,
Dan Willemsen00269f22017-07-06 16:59:48 -0700587 target: deviceTarget,
Colin Cross1184b642019-12-30 18:43:07 -0800588 earlyModuleContext: earlyModuleContext{
589 kind: socSpecificModule,
590 },
Jiyong Park2db76922017-11-08 16:03:48 +0900591 },
592 inData: true,
593 inSanitizerDir: true,
594 },
Jiyong Park957bcd92020-10-20 18:23:33 +0900595 in: []string{"nativetest", "my_test"},
596 out: "target/product/test_device/data/asan/data/nativetest/my_test",
597 partitionDir: "target/product/test_device/data/asan/data",
Jiyong Park2db76922017-11-08 16:03:48 +0900598 },
599 {
600 name: "sanitized odm native test binary",
Ulya Trafimovichccc8c852020-10-14 11:29:07 +0100601 ctx: &testModuleInstallPathContext{
Colin Cross0ea8ba82019-06-06 14:33:29 -0700602 baseModuleContext: baseModuleContext{
Colin Crossfb0c16e2019-11-20 17:12:35 -0800603 os: deviceTarget.Os,
Jiyong Park2db76922017-11-08 16:03:48 +0900604 target: deviceTarget,
Colin Cross1184b642019-12-30 18:43:07 -0800605 earlyModuleContext: earlyModuleContext{
606 kind: deviceSpecificModule,
607 },
Jiyong Park2db76922017-11-08 16:03:48 +0900608 },
609 inData: true,
610 inSanitizerDir: true,
611 },
Jiyong Park957bcd92020-10-20 18:23:33 +0900612 in: []string{"nativetest", "my_test"},
613 out: "target/product/test_device/data/asan/data/nativetest/my_test",
614 partitionDir: "target/product/test_device/data/asan/data",
Jiyong Park2db76922017-11-08 16:03:48 +0900615 },
616 {
Jaekyun Seok5cfbfbb2018-01-10 19:00:15 +0900617 name: "sanitized product native test binary",
Ulya Trafimovichccc8c852020-10-14 11:29:07 +0100618 ctx: &testModuleInstallPathContext{
Colin Cross0ea8ba82019-06-06 14:33:29 -0700619 baseModuleContext: baseModuleContext{
Colin Crossfb0c16e2019-11-20 17:12:35 -0800620 os: deviceTarget.Os,
Jiyong Park2db76922017-11-08 16:03:48 +0900621 target: deviceTarget,
Colin Cross1184b642019-12-30 18:43:07 -0800622 earlyModuleContext: earlyModuleContext{
623 kind: productSpecificModule,
624 },
Dan Willemsen00269f22017-07-06 16:59:48 -0700625 },
626 inData: true,
627 inSanitizerDir: true,
628 },
Jiyong Park957bcd92020-10-20 18:23:33 +0900629 in: []string{"nativetest", "my_test"},
630 out: "target/product/test_device/data/asan/data/nativetest/my_test",
631 partitionDir: "target/product/test_device/data/asan/data",
Dan Willemsen00269f22017-07-06 16:59:48 -0700632 },
Dario Frenifd05a742018-05-29 13:28:54 +0100633 {
Justin Yund5f6c822019-06-25 16:47:17 +0900634 name: "sanitized system_ext native test binary",
Ulya Trafimovichccc8c852020-10-14 11:29:07 +0100635 ctx: &testModuleInstallPathContext{
Colin Cross0ea8ba82019-06-06 14:33:29 -0700636 baseModuleContext: baseModuleContext{
Colin Crossfb0c16e2019-11-20 17:12:35 -0800637 os: deviceTarget.Os,
Dario Frenifd05a742018-05-29 13:28:54 +0100638 target: deviceTarget,
Colin Cross1184b642019-12-30 18:43:07 -0800639 earlyModuleContext: earlyModuleContext{
640 kind: systemExtSpecificModule,
641 },
Dario Frenifd05a742018-05-29 13:28:54 +0100642 },
643 inData: true,
644 inSanitizerDir: true,
645 },
Jiyong Park957bcd92020-10-20 18:23:33 +0900646 in: []string{"nativetest", "my_test"},
647 out: "target/product/test_device/data/asan/data/nativetest/my_test",
648 partitionDir: "target/product/test_device/data/asan/data",
Colin Cross6e359402020-02-10 15:29:54 -0800649 }, {
650 name: "device testcases",
Ulya Trafimovichccc8c852020-10-14 11:29:07 +0100651 ctx: &testModuleInstallPathContext{
Colin Cross6e359402020-02-10 15:29:54 -0800652 baseModuleContext: baseModuleContext{
653 os: deviceTarget.Os,
654 target: deviceTarget,
655 },
656 inTestcases: true,
657 },
Jiyong Park957bcd92020-10-20 18:23:33 +0900658 in: []string{"my_test", "my_test_bin"},
659 out: "target/product/test_device/testcases/my_test/my_test_bin",
660 partitionDir: "target/product/test_device/testcases",
Colin Cross6e359402020-02-10 15:29:54 -0800661 }, {
662 name: "host testcases",
Ulya Trafimovichccc8c852020-10-14 11:29:07 +0100663 ctx: &testModuleInstallPathContext{
Colin Cross6e359402020-02-10 15:29:54 -0800664 baseModuleContext: baseModuleContext{
665 os: hostTarget.Os,
666 target: hostTarget,
667 },
668 inTestcases: true,
669 },
Jiyong Park957bcd92020-10-20 18:23:33 +0900670 in: []string{"my_test", "my_test_bin"},
671 out: "host/linux-x86/testcases/my_test/my_test_bin",
672 partitionDir: "host/linux-x86/testcases",
Colin Cross6e359402020-02-10 15:29:54 -0800673 }, {
674 name: "forced host testcases",
Ulya Trafimovichccc8c852020-10-14 11:29:07 +0100675 ctx: &testModuleInstallPathContext{
Colin Cross6e359402020-02-10 15:29:54 -0800676 baseModuleContext: baseModuleContext{
677 os: deviceTarget.Os,
678 target: deviceTarget,
679 },
680 inTestcases: true,
681 forceOS: &Linux,
Jiyong Park87788b52020-09-01 12:37:45 +0900682 forceArch: &X86,
Colin Cross6e359402020-02-10 15:29:54 -0800683 },
Jiyong Park957bcd92020-10-20 18:23:33 +0900684 in: []string{"my_test", "my_test_bin"},
685 out: "host/linux-x86/testcases/my_test/my_test_bin",
686 partitionDir: "host/linux-x86/testcases",
Dario Frenifd05a742018-05-29 13:28:54 +0100687 },
Dan Willemsen00269f22017-07-06 16:59:48 -0700688 }
689
690 for _, tc := range testCases {
691 t.Run(tc.name, func(t *testing.T) {
Colin Cross0ea8ba82019-06-06 14:33:29 -0700692 tc.ctx.baseModuleContext.config = testConfig
Dan Willemsen00269f22017-07-06 16:59:48 -0700693 output := PathForModuleInstall(tc.ctx, tc.in...)
694 if output.basePath.path != tc.out {
695 t.Errorf("unexpected path:\n got: %q\nwant: %q\n",
696 output.basePath.path,
697 tc.out)
698 }
Jiyong Park957bcd92020-10-20 18:23:33 +0900699 if output.partitionDir != tc.partitionDir {
700 t.Errorf("unexpected partitionDir:\n got: %q\nwant: %q\n",
701 output.partitionDir, tc.partitionDir)
702 }
Dan Willemsen00269f22017-07-06 16:59:48 -0700703 })
704 }
705}
Colin Cross5e6cfbe2017-11-03 15:20:35 -0700706
Inseob Kimd9580b82021-04-13 21:13:49 +0900707func TestPathForModuleInstallRecoveryAsBoot(t *testing.T) {
708 testConfig := pathTestConfig("")
709 testConfig.TestProductVariables.BoardUsesRecoveryAsBoot = proptools.BoolPtr(true)
710 testConfig.TestProductVariables.BoardMoveRecoveryResourcesToVendorBoot = proptools.BoolPtr(true)
711 deviceTarget := Target{Os: Android, Arch: Arch{ArchType: Arm64}}
712
713 testCases := []struct {
714 name string
715 ctx *testModuleInstallPathContext
716 in []string
717 out string
718 partitionDir string
719 }{
720 {
721 name: "ramdisk binary",
722 ctx: &testModuleInstallPathContext{
723 baseModuleContext: baseModuleContext{
724 os: deviceTarget.Os,
725 target: deviceTarget,
726 },
727 inRamdisk: true,
728 inRoot: true,
729 },
730 in: []string{"my_test"},
731 out: "target/product/test_device/recovery/root/first_stage_ramdisk/my_test",
732 partitionDir: "target/product/test_device/recovery/root/first_stage_ramdisk",
733 },
734
735 {
736 name: "vendor_ramdisk binary",
737 ctx: &testModuleInstallPathContext{
738 baseModuleContext: baseModuleContext{
739 os: deviceTarget.Os,
740 target: deviceTarget,
741 },
742 inVendorRamdisk: true,
743 inRoot: true,
744 },
745 in: []string{"my_test"},
746 out: "target/product/test_device/vendor_ramdisk/first_stage_ramdisk/my_test",
747 partitionDir: "target/product/test_device/vendor_ramdisk/first_stage_ramdisk",
748 },
749 }
750
751 for _, tc := range testCases {
752 t.Run(tc.name, func(t *testing.T) {
753 tc.ctx.baseModuleContext.config = testConfig
754 output := PathForModuleInstall(tc.ctx, tc.in...)
755 if output.basePath.path != tc.out {
756 t.Errorf("unexpected path:\n got: %q\nwant: %q\n",
757 output.basePath.path,
758 tc.out)
759 }
760 if output.partitionDir != tc.partitionDir {
761 t.Errorf("unexpected partitionDir:\n got: %q\nwant: %q\n",
762 output.partitionDir, tc.partitionDir)
763 }
764 })
765 }
766}
767
Jiyong Park957bcd92020-10-20 18:23:33 +0900768func TestBaseDirForInstallPath(t *testing.T) {
769 testConfig := pathTestConfig("")
770 deviceTarget := Target{Os: Android, Arch: Arch{ArchType: Arm64}}
771
Ulya Trafimovichccc8c852020-10-14 11:29:07 +0100772 ctx := &testModuleInstallPathContext{
Jiyong Park957bcd92020-10-20 18:23:33 +0900773 baseModuleContext: baseModuleContext{
774 os: deviceTarget.Os,
775 target: deviceTarget,
776 },
777 }
778 ctx.baseModuleContext.config = testConfig
779
780 actual := PathForModuleInstall(ctx, "foo", "bar")
781 expectedBaseDir := "target/product/test_device/system"
782 if actual.partitionDir != expectedBaseDir {
783 t.Errorf("unexpected partitionDir:\n got: %q\nwant: %q\n", actual.partitionDir, expectedBaseDir)
784 }
785 expectedRelPath := "foo/bar"
786 if actual.Rel() != expectedRelPath {
787 t.Errorf("unexpected Rel():\n got: %q\nwant: %q\n", actual.Rel(), expectedRelPath)
788 }
789
790 actualAfterJoin := actual.Join(ctx, "baz")
791 // partitionDir is preserved even after joining
792 if actualAfterJoin.partitionDir != expectedBaseDir {
793 t.Errorf("unexpected partitionDir after joining:\n got: %q\nwant: %q\n", actualAfterJoin.partitionDir, expectedBaseDir)
794 }
795 // Rel() is updated though
796 expectedRelAfterJoin := "baz"
797 if actualAfterJoin.Rel() != expectedRelAfterJoin {
798 t.Errorf("unexpected Rel() after joining:\n got: %q\nwant: %q\n", actualAfterJoin.Rel(), expectedRelAfterJoin)
799 }
800}
801
Colin Cross5e6cfbe2017-11-03 15:20:35 -0700802func TestDirectorySortedPaths(t *testing.T) {
Colin Cross98be1bb2019-12-13 20:41:13 -0800803 config := TestConfig("out", nil, "", map[string][]byte{
804 "Android.bp": nil,
805 "a.txt": nil,
806 "a/txt": nil,
807 "a/b/c": nil,
808 "a/b/d": nil,
809 "b": nil,
810 "b/b.txt": nil,
811 "a/a.txt": nil,
Colin Cross07e51612019-03-05 12:46:40 -0800812 })
813
Colin Cross98be1bb2019-12-13 20:41:13 -0800814 ctx := PathContextForTesting(config)
815
Colin Cross5e6cfbe2017-11-03 15:20:35 -0700816 makePaths := func() Paths {
817 return Paths{
Colin Cross07e51612019-03-05 12:46:40 -0800818 PathForSource(ctx, "a.txt"),
819 PathForSource(ctx, "a/txt"),
820 PathForSource(ctx, "a/b/c"),
821 PathForSource(ctx, "a/b/d"),
822 PathForSource(ctx, "b"),
823 PathForSource(ctx, "b/b.txt"),
824 PathForSource(ctx, "a/a.txt"),
Colin Cross5e6cfbe2017-11-03 15:20:35 -0700825 }
826 }
827
828 expected := []string{
829 "a.txt",
830 "a/a.txt",
831 "a/b/c",
832 "a/b/d",
833 "a/txt",
834 "b",
835 "b/b.txt",
836 }
837
838 paths := makePaths()
Colin Crossa140bb02018-04-17 10:52:26 -0700839 reversePaths := ReversePaths(paths)
Colin Cross5e6cfbe2017-11-03 15:20:35 -0700840
841 sortedPaths := PathsToDirectorySortedPaths(paths)
842 reverseSortedPaths := PathsToDirectorySortedPaths(reversePaths)
843
844 if !reflect.DeepEqual(Paths(sortedPaths).Strings(), expected) {
845 t.Fatalf("sorted paths:\n %#v\n != \n %#v", paths.Strings(), expected)
846 }
847
848 if !reflect.DeepEqual(Paths(reverseSortedPaths).Strings(), expected) {
849 t.Fatalf("sorted reversed paths:\n %#v\n !=\n %#v", reversePaths.Strings(), expected)
850 }
851
852 expectedA := []string{
853 "a/a.txt",
854 "a/b/c",
855 "a/b/d",
856 "a/txt",
857 }
858
859 inA := sortedPaths.PathsInDirectory("a")
860 if !reflect.DeepEqual(inA.Strings(), expectedA) {
861 t.Errorf("FilesInDirectory(a):\n %#v\n != \n %#v", inA.Strings(), expectedA)
862 }
863
864 expectedA_B := []string{
865 "a/b/c",
866 "a/b/d",
867 }
868
869 inA_B := sortedPaths.PathsInDirectory("a/b")
870 if !reflect.DeepEqual(inA_B.Strings(), expectedA_B) {
871 t.Errorf("FilesInDirectory(a/b):\n %#v\n != \n %#v", inA_B.Strings(), expectedA_B)
872 }
873
874 expectedB := []string{
875 "b/b.txt",
876 }
877
878 inB := sortedPaths.PathsInDirectory("b")
879 if !reflect.DeepEqual(inB.Strings(), expectedB) {
880 t.Errorf("FilesInDirectory(b):\n %#v\n != \n %#v", inA.Strings(), expectedA)
881 }
882}
Colin Cross43f08db2018-11-12 10:13:39 -0800883
884func TestMaybeRel(t *testing.T) {
885 testCases := []struct {
886 name string
887 base string
888 target string
889 out string
890 isRel bool
891 }{
892 {
893 name: "normal",
894 base: "a/b/c",
895 target: "a/b/c/d",
896 out: "d",
897 isRel: true,
898 },
899 {
900 name: "parent",
901 base: "a/b/c/d",
902 target: "a/b/c",
903 isRel: false,
904 },
905 {
906 name: "not relative",
907 base: "a/b",
908 target: "c/d",
909 isRel: false,
910 },
911 {
912 name: "abs1",
913 base: "/a",
914 target: "a",
915 isRel: false,
916 },
917 {
918 name: "abs2",
919 base: "a",
920 target: "/a",
921 isRel: false,
922 },
923 }
924
925 for _, testCase := range testCases {
926 t.Run(testCase.name, func(t *testing.T) {
927 ctx := &configErrorWrapper{}
928 out, isRel := MaybeRel(ctx, testCase.base, testCase.target)
929 if len(ctx.errors) > 0 {
930 t.Errorf("MaybeRel(..., %s, %s) reported unexpected errors %v",
931 testCase.base, testCase.target, ctx.errors)
932 }
933 if isRel != testCase.isRel || out != testCase.out {
934 t.Errorf("MaybeRel(..., %s, %s) want %v, %v got %v, %v",
935 testCase.base, testCase.target, testCase.out, testCase.isRel, out, isRel)
936 }
937 })
938 }
939}
Colin Cross7b3dcc32019-01-24 13:14:39 -0800940
941func TestPathForSource(t *testing.T) {
942 testCases := []struct {
943 name string
944 buildDir string
945 src string
946 err string
947 }{
948 {
949 name: "normal",
950 buildDir: "out",
951 src: "a/b/c",
952 },
953 {
954 name: "abs",
955 buildDir: "out",
956 src: "/a/b/c",
957 err: "is outside directory",
958 },
959 {
960 name: "in out dir",
961 buildDir: "out",
962 src: "out/a/b/c",
963 err: "is in output",
964 },
965 }
966
967 funcs := []struct {
968 name string
969 f func(ctx PathContext, pathComponents ...string) (SourcePath, error)
970 }{
971 {"pathForSource", pathForSource},
972 {"safePathForSource", safePathForSource},
973 }
974
975 for _, f := range funcs {
976 t.Run(f.name, func(t *testing.T) {
977 for _, test := range testCases {
978 t.Run(test.name, func(t *testing.T) {
Colin Cross98be1bb2019-12-13 20:41:13 -0800979 testConfig := pathTestConfig(test.buildDir)
Colin Cross7b3dcc32019-01-24 13:14:39 -0800980 ctx := &configErrorWrapper{config: testConfig}
981 _, err := f.f(ctx, test.src)
982 if len(ctx.errors) > 0 {
983 t.Fatalf("unexpected errors %v", ctx.errors)
984 }
985 if err != nil {
986 if test.err == "" {
987 t.Fatalf("unexpected error %q", err.Error())
988 } else if !strings.Contains(err.Error(), test.err) {
989 t.Fatalf("incorrect error, want substring %q got %q", test.err, err.Error())
990 }
991 } else {
992 if test.err != "" {
993 t.Fatalf("missing error %q", test.err)
994 }
995 }
996 })
997 }
998 })
999 }
1000}
Colin Cross8854a5a2019-02-11 14:14:16 -08001001
Colin Cross8a497952019-03-05 22:25:09 -08001002type pathForModuleSrcTestModule struct {
Colin Cross937664a2019-03-06 10:17:32 -08001003 ModuleBase
1004 props struct {
1005 Srcs []string `android:"path"`
1006 Exclude_srcs []string `android:"path"`
Colin Cross8a497952019-03-05 22:25:09 -08001007
1008 Src *string `android:"path"`
Colin Crossba71a3f2019-03-18 12:12:48 -07001009
1010 Module_handles_missing_deps bool
Colin Cross937664a2019-03-06 10:17:32 -08001011 }
1012
Colin Cross8a497952019-03-05 22:25:09 -08001013 src string
1014 rel string
1015
1016 srcs []string
Colin Cross937664a2019-03-06 10:17:32 -08001017 rels []string
Colin Cross8a497952019-03-05 22:25:09 -08001018
1019 missingDeps []string
Colin Cross937664a2019-03-06 10:17:32 -08001020}
1021
Colin Cross8a497952019-03-05 22:25:09 -08001022func pathForModuleSrcTestModuleFactory() Module {
1023 module := &pathForModuleSrcTestModule{}
Colin Cross937664a2019-03-06 10:17:32 -08001024 module.AddProperties(&module.props)
1025 InitAndroidModule(module)
1026 return module
1027}
1028
Colin Cross8a497952019-03-05 22:25:09 -08001029func (p *pathForModuleSrcTestModule) GenerateAndroidBuildActions(ctx ModuleContext) {
Colin Crossba71a3f2019-03-18 12:12:48 -07001030 var srcs Paths
1031 if p.props.Module_handles_missing_deps {
1032 srcs, p.missingDeps = PathsAndMissingDepsForModuleSrcExcludes(ctx, p.props.Srcs, p.props.Exclude_srcs)
1033 } else {
1034 srcs = PathsForModuleSrcExcludes(ctx, p.props.Srcs, p.props.Exclude_srcs)
1035 }
Colin Cross8a497952019-03-05 22:25:09 -08001036 p.srcs = srcs.Strings()
Colin Cross937664a2019-03-06 10:17:32 -08001037
Colin Cross8a497952019-03-05 22:25:09 -08001038 for _, src := range srcs {
Colin Cross937664a2019-03-06 10:17:32 -08001039 p.rels = append(p.rels, src.Rel())
1040 }
Colin Cross8a497952019-03-05 22:25:09 -08001041
1042 if p.props.Src != nil {
1043 src := PathForModuleSrc(ctx, *p.props.Src)
1044 if src != nil {
1045 p.src = src.String()
1046 p.rel = src.Rel()
1047 }
1048 }
1049
Colin Crossba71a3f2019-03-18 12:12:48 -07001050 if !p.props.Module_handles_missing_deps {
1051 p.missingDeps = ctx.GetMissingDependencies()
1052 }
Colin Cross6c4f21f2019-06-06 15:41:36 -07001053
1054 ctx.Build(pctx, BuildParams{
1055 Rule: Touch,
1056 Output: PathForModuleOut(ctx, "output"),
1057 })
Colin Cross8a497952019-03-05 22:25:09 -08001058}
1059
Colin Cross41955e82019-05-29 14:40:35 -07001060type pathForModuleSrcOutputFileProviderModule struct {
1061 ModuleBase
1062 props struct {
1063 Outs []string
1064 Tagged []string
1065 }
1066
1067 outs Paths
1068 tagged Paths
1069}
1070
1071func pathForModuleSrcOutputFileProviderModuleFactory() Module {
1072 module := &pathForModuleSrcOutputFileProviderModule{}
1073 module.AddProperties(&module.props)
1074 InitAndroidModule(module)
1075 return module
1076}
1077
1078func (p *pathForModuleSrcOutputFileProviderModule) GenerateAndroidBuildActions(ctx ModuleContext) {
1079 for _, out := range p.props.Outs {
1080 p.outs = append(p.outs, PathForModuleOut(ctx, out))
1081 }
1082
1083 for _, tagged := range p.props.Tagged {
1084 p.tagged = append(p.tagged, PathForModuleOut(ctx, tagged))
1085 }
1086}
1087
1088func (p *pathForModuleSrcOutputFileProviderModule) OutputFiles(tag string) (Paths, error) {
1089 switch tag {
1090 case "":
1091 return p.outs, nil
1092 case ".tagged":
1093 return p.tagged, nil
1094 default:
1095 return nil, fmt.Errorf("unsupported tag %q", tag)
1096 }
1097}
1098
Colin Cross8a497952019-03-05 22:25:09 -08001099type pathForModuleSrcTestCase struct {
1100 name string
1101 bp string
1102 srcs []string
1103 rels []string
1104 src string
1105 rel string
1106}
1107
Paul Duffin54054682021-03-16 21:11:42 +00001108func testPathForModuleSrc(t *testing.T, tests []pathForModuleSrcTestCase) {
Colin Cross8a497952019-03-05 22:25:09 -08001109 for _, test := range tests {
1110 t.Run(test.name, func(t *testing.T) {
Colin Cross8a497952019-03-05 22:25:09 -08001111 fgBp := `
1112 filegroup {
1113 name: "a",
1114 srcs: ["src/a"],
1115 }
1116 `
1117
Colin Cross41955e82019-05-29 14:40:35 -07001118 ofpBp := `
1119 output_file_provider {
1120 name: "b",
1121 outs: ["gen/b"],
1122 tagged: ["gen/c"],
1123 }
1124 `
1125
Paul Duffin54054682021-03-16 21:11:42 +00001126 mockFS := MockFS{
Colin Cross8a497952019-03-05 22:25:09 -08001127 "fg/Android.bp": []byte(fgBp),
1128 "foo/Android.bp": []byte(test.bp),
Colin Cross41955e82019-05-29 14:40:35 -07001129 "ofp/Android.bp": []byte(ofpBp),
Colin Cross8a497952019-03-05 22:25:09 -08001130 "fg/src/a": nil,
1131 "foo/src/b": nil,
1132 "foo/src/c": nil,
1133 "foo/src/d": nil,
1134 "foo/src/e/e": nil,
1135 "foo/src_special/$": nil,
1136 }
1137
Paul Duffin30ac3e72021-03-20 00:36:14 +00001138 result := GroupFixturePreparers(
Paul Duffin54054682021-03-16 21:11:42 +00001139 FixtureRegisterWithContext(func(ctx RegistrationContext) {
1140 ctx.RegisterModuleType("test", pathForModuleSrcTestModuleFactory)
1141 ctx.RegisterModuleType("output_file_provider", pathForModuleSrcOutputFileProviderModuleFactory)
1142 ctx.RegisterModuleType("filegroup", FileGroupFactory)
1143 }),
1144 mockFS.AddToFixture(),
Paul Duffin30ac3e72021-03-20 00:36:14 +00001145 ).RunTest(t)
Colin Cross8a497952019-03-05 22:25:09 -08001146
Paul Duffin54054682021-03-16 21:11:42 +00001147 m := result.ModuleForTests("foo", "").Module().(*pathForModuleSrcTestModule)
Colin Crossae8600b2020-10-29 17:09:13 -07001148
Paul Duffin54054682021-03-16 21:11:42 +00001149 AssertStringPathsRelativeToTopEquals(t, "srcs", result.Config, test.srcs, m.srcs)
1150 AssertStringPathsRelativeToTopEquals(t, "rels", result.Config, test.rels, m.rels)
1151 AssertStringPathRelativeToTopEquals(t, "src", result.Config, test.src, m.src)
1152 AssertStringPathRelativeToTopEquals(t, "rel", result.Config, test.rel, m.rel)
Colin Cross8a497952019-03-05 22:25:09 -08001153 })
1154 }
Colin Cross937664a2019-03-06 10:17:32 -08001155}
1156
Colin Cross8a497952019-03-05 22:25:09 -08001157func TestPathsForModuleSrc(t *testing.T) {
1158 tests := []pathForModuleSrcTestCase{
Colin Cross937664a2019-03-06 10:17:32 -08001159 {
1160 name: "path",
1161 bp: `
1162 test {
1163 name: "foo",
1164 srcs: ["src/b"],
1165 }`,
1166 srcs: []string{"foo/src/b"},
1167 rels: []string{"src/b"},
1168 },
1169 {
1170 name: "glob",
1171 bp: `
1172 test {
1173 name: "foo",
1174 srcs: [
1175 "src/*",
1176 "src/e/*",
1177 ],
1178 }`,
1179 srcs: []string{"foo/src/b", "foo/src/c", "foo/src/d", "foo/src/e/e"},
1180 rels: []string{"src/b", "src/c", "src/d", "src/e/e"},
1181 },
1182 {
1183 name: "recursive glob",
1184 bp: `
1185 test {
1186 name: "foo",
1187 srcs: ["src/**/*"],
1188 }`,
1189 srcs: []string{"foo/src/b", "foo/src/c", "foo/src/d", "foo/src/e/e"},
1190 rels: []string{"src/b", "src/c", "src/d", "src/e/e"},
1191 },
1192 {
1193 name: "filegroup",
1194 bp: `
1195 test {
1196 name: "foo",
1197 srcs: [":a"],
1198 }`,
1199 srcs: []string{"fg/src/a"},
1200 rels: []string{"src/a"},
1201 },
1202 {
Colin Cross41955e82019-05-29 14:40:35 -07001203 name: "output file provider",
1204 bp: `
1205 test {
1206 name: "foo",
1207 srcs: [":b"],
1208 }`,
Paul Duffin54054682021-03-16 21:11:42 +00001209 srcs: []string{"out/soong/.intermediates/ofp/b/gen/b"},
Colin Cross41955e82019-05-29 14:40:35 -07001210 rels: []string{"gen/b"},
1211 },
1212 {
1213 name: "output file provider tagged",
1214 bp: `
1215 test {
1216 name: "foo",
1217 srcs: [":b{.tagged}"],
1218 }`,
Paul Duffin54054682021-03-16 21:11:42 +00001219 srcs: []string{"out/soong/.intermediates/ofp/b/gen/c"},
Colin Cross41955e82019-05-29 14:40:35 -07001220 rels: []string{"gen/c"},
1221 },
1222 {
Jooyung Han7607dd32020-07-05 10:23:14 +09001223 name: "output file provider with exclude",
1224 bp: `
1225 test {
1226 name: "foo",
1227 srcs: [":b", ":c"],
1228 exclude_srcs: [":c"]
1229 }
1230 output_file_provider {
1231 name: "c",
1232 outs: ["gen/c"],
1233 }`,
Paul Duffin54054682021-03-16 21:11:42 +00001234 srcs: []string{"out/soong/.intermediates/ofp/b/gen/b"},
Jooyung Han7607dd32020-07-05 10:23:14 +09001235 rels: []string{"gen/b"},
1236 },
1237 {
Colin Cross937664a2019-03-06 10:17:32 -08001238 name: "special characters glob",
1239 bp: `
1240 test {
1241 name: "foo",
1242 srcs: ["src_special/*"],
1243 }`,
1244 srcs: []string{"foo/src_special/$"},
1245 rels: []string{"src_special/$"},
1246 },
1247 }
1248
Paul Duffin54054682021-03-16 21:11:42 +00001249 testPathForModuleSrc(t, tests)
Colin Cross41955e82019-05-29 14:40:35 -07001250}
1251
1252func TestPathForModuleSrc(t *testing.T) {
Colin Cross8a497952019-03-05 22:25:09 -08001253 tests := []pathForModuleSrcTestCase{
1254 {
1255 name: "path",
1256 bp: `
1257 test {
1258 name: "foo",
1259 src: "src/b",
1260 }`,
1261 src: "foo/src/b",
1262 rel: "src/b",
1263 },
1264 {
1265 name: "glob",
1266 bp: `
1267 test {
1268 name: "foo",
1269 src: "src/e/*",
1270 }`,
1271 src: "foo/src/e/e",
1272 rel: "src/e/e",
1273 },
1274 {
1275 name: "filegroup",
1276 bp: `
1277 test {
1278 name: "foo",
1279 src: ":a",
1280 }`,
1281 src: "fg/src/a",
1282 rel: "src/a",
1283 },
1284 {
Colin Cross41955e82019-05-29 14:40:35 -07001285 name: "output file provider",
1286 bp: `
1287 test {
1288 name: "foo",
1289 src: ":b",
1290 }`,
Paul Duffin54054682021-03-16 21:11:42 +00001291 src: "out/soong/.intermediates/ofp/b/gen/b",
Colin Cross41955e82019-05-29 14:40:35 -07001292 rel: "gen/b",
1293 },
1294 {
1295 name: "output file provider tagged",
1296 bp: `
1297 test {
1298 name: "foo",
1299 src: ":b{.tagged}",
1300 }`,
Paul Duffin54054682021-03-16 21:11:42 +00001301 src: "out/soong/.intermediates/ofp/b/gen/c",
Colin Cross41955e82019-05-29 14:40:35 -07001302 rel: "gen/c",
1303 },
1304 {
Colin Cross8a497952019-03-05 22:25:09 -08001305 name: "special characters glob",
1306 bp: `
1307 test {
1308 name: "foo",
1309 src: "src_special/*",
1310 }`,
1311 src: "foo/src_special/$",
1312 rel: "src_special/$",
1313 },
1314 }
1315
Paul Duffin54054682021-03-16 21:11:42 +00001316 testPathForModuleSrc(t, tests)
Colin Cross8a497952019-03-05 22:25:09 -08001317}
Colin Cross937664a2019-03-06 10:17:32 -08001318
Colin Cross8a497952019-03-05 22:25:09 -08001319func TestPathsForModuleSrc_AllowMissingDependencies(t *testing.T) {
Colin Cross8a497952019-03-05 22:25:09 -08001320 bp := `
1321 test {
1322 name: "foo",
1323 srcs: [":a"],
1324 exclude_srcs: [":b"],
1325 src: ":c",
1326 }
Colin Crossba71a3f2019-03-18 12:12:48 -07001327
1328 test {
1329 name: "bar",
1330 srcs: [":d"],
1331 exclude_srcs: [":e"],
1332 module_handles_missing_deps: true,
1333 }
Colin Cross8a497952019-03-05 22:25:09 -08001334 `
1335
Paul Duffin30ac3e72021-03-20 00:36:14 +00001336 result := GroupFixturePreparers(
Paul Duffin54054682021-03-16 21:11:42 +00001337 PrepareForTestWithAllowMissingDependencies,
1338 FixtureRegisterWithContext(func(ctx RegistrationContext) {
1339 ctx.RegisterModuleType("test", pathForModuleSrcTestModuleFactory)
1340 }),
1341 FixtureWithRootAndroidBp(bp),
Paul Duffin30ac3e72021-03-20 00:36:14 +00001342 ).RunTest(t)
Colin Cross8a497952019-03-05 22:25:09 -08001343
Paul Duffin54054682021-03-16 21:11:42 +00001344 foo := result.ModuleForTests("foo", "").Module().(*pathForModuleSrcTestModule)
Colin Cross8a497952019-03-05 22:25:09 -08001345
Paul Duffin54054682021-03-16 21:11:42 +00001346 AssertArrayString(t, "foo missing deps", []string{"a", "b", "c"}, foo.missingDeps)
1347 AssertArrayString(t, "foo srcs", []string{}, foo.srcs)
1348 AssertStringEquals(t, "foo src", "", foo.src)
Colin Cross98be1bb2019-12-13 20:41:13 -08001349
Paul Duffin54054682021-03-16 21:11:42 +00001350 bar := result.ModuleForTests("bar", "").Module().(*pathForModuleSrcTestModule)
Colin Cross98be1bb2019-12-13 20:41:13 -08001351
Paul Duffin54054682021-03-16 21:11:42 +00001352 AssertArrayString(t, "bar missing deps", []string{"d", "e"}, bar.missingDeps)
1353 AssertArrayString(t, "bar srcs", []string{}, bar.srcs)
Colin Cross937664a2019-03-06 10:17:32 -08001354}
1355
Paul Duffin567465d2021-03-16 01:21:34 +00001356func TestPathRelativeToTop(t *testing.T) {
1357 testConfig := pathTestConfig("/tmp/build/top")
1358 deviceTarget := Target{Os: Android, Arch: Arch{ArchType: Arm64}}
1359
1360 ctx := &testModuleInstallPathContext{
1361 baseModuleContext: baseModuleContext{
1362 os: deviceTarget.Os,
1363 target: deviceTarget,
1364 },
1365 }
1366 ctx.baseModuleContext.config = testConfig
1367
1368 t.Run("install for soong", func(t *testing.T) {
1369 p := PathForModuleInstall(ctx, "install/path")
1370 AssertPathRelativeToTopEquals(t, "install path for soong", "out/soong/target/product/test_device/system/install/path", p)
1371 })
1372 t.Run("install for make", func(t *testing.T) {
1373 p := PathForModuleInstall(ctx, "install/path").ToMakePath()
1374 AssertPathRelativeToTopEquals(t, "install path for make", "out/target/product/test_device/system/install/path", p)
1375 })
1376 t.Run("output", func(t *testing.T) {
1377 p := PathForOutput(ctx, "output/path")
1378 AssertPathRelativeToTopEquals(t, "output path", "out/soong/output/path", p)
1379 })
1380 t.Run("source", func(t *testing.T) {
1381 p := PathForSource(ctx, "source/path")
1382 AssertPathRelativeToTopEquals(t, "source path", "source/path", p)
1383 })
1384 t.Run("mixture", func(t *testing.T) {
1385 paths := Paths{
1386 PathForModuleInstall(ctx, "install/path"),
1387 PathForModuleInstall(ctx, "install/path").ToMakePath(),
1388 PathForOutput(ctx, "output/path"),
1389 PathForSource(ctx, "source/path"),
1390 }
1391
1392 expected := []string{
1393 "out/soong/target/product/test_device/system/install/path",
1394 "out/target/product/test_device/system/install/path",
1395 "out/soong/output/path",
1396 "source/path",
1397 }
1398 AssertPathsRelativeToTopEquals(t, "mixture", expected, paths)
1399 })
1400}
1401
Colin Cross8854a5a2019-02-11 14:14:16 -08001402func ExampleOutputPath_ReplaceExtension() {
1403 ctx := &configErrorWrapper{
Colin Cross98be1bb2019-12-13 20:41:13 -08001404 config: TestConfig("out", nil, "", nil),
Colin Cross8854a5a2019-02-11 14:14:16 -08001405 }
Colin Cross2cdd5df2019-02-25 10:25:24 -08001406 p := PathForOutput(ctx, "system/framework").Join(ctx, "boot.art")
Colin Cross8854a5a2019-02-11 14:14:16 -08001407 p2 := p.ReplaceExtension(ctx, "oat")
1408 fmt.Println(p, p2)
Colin Cross2cdd5df2019-02-25 10:25:24 -08001409 fmt.Println(p.Rel(), p2.Rel())
Colin Cross8854a5a2019-02-11 14:14:16 -08001410
1411 // Output:
1412 // out/system/framework/boot.art out/system/framework/boot.oat
Colin Cross2cdd5df2019-02-25 10:25:24 -08001413 // boot.art boot.oat
Colin Cross8854a5a2019-02-11 14:14:16 -08001414}
Colin Cross40e33732019-02-15 11:08:35 -08001415
Colin Cross41b46762020-10-09 19:26:32 -07001416func ExampleOutputPath_InSameDir() {
Colin Cross40e33732019-02-15 11:08:35 -08001417 ctx := &configErrorWrapper{
Colin Cross98be1bb2019-12-13 20:41:13 -08001418 config: TestConfig("out", nil, "", nil),
Colin Cross40e33732019-02-15 11:08:35 -08001419 }
Colin Cross2cdd5df2019-02-25 10:25:24 -08001420 p := PathForOutput(ctx, "system/framework").Join(ctx, "boot.art")
Colin Cross40e33732019-02-15 11:08:35 -08001421 p2 := p.InSameDir(ctx, "oat", "arm", "boot.vdex")
1422 fmt.Println(p, p2)
Colin Cross2cdd5df2019-02-25 10:25:24 -08001423 fmt.Println(p.Rel(), p2.Rel())
Colin Cross40e33732019-02-15 11:08:35 -08001424
1425 // Output:
1426 // out/system/framework/boot.art out/system/framework/oat/arm/boot.vdex
Colin Cross2cdd5df2019-02-25 10:25:24 -08001427 // boot.art oat/arm/boot.vdex
Colin Cross40e33732019-02-15 11:08:35 -08001428}
Colin Cross27027c72020-02-28 15:34:17 -08001429
1430func BenchmarkFirstUniquePaths(b *testing.B) {
1431 implementations := []struct {
1432 name string
1433 f func(Paths) Paths
1434 }{
1435 {
1436 name: "list",
1437 f: firstUniquePathsList,
1438 },
1439 {
1440 name: "map",
1441 f: firstUniquePathsMap,
1442 },
1443 }
1444 const maxSize = 1024
1445 uniquePaths := make(Paths, maxSize)
1446 for i := range uniquePaths {
1447 uniquePaths[i] = PathForTesting(strconv.Itoa(i))
1448 }
1449 samePath := make(Paths, maxSize)
1450 for i := range samePath {
1451 samePath[i] = uniquePaths[0]
1452 }
1453
1454 f := func(b *testing.B, imp func(Paths) Paths, paths Paths) {
1455 for i := 0; i < b.N; i++ {
1456 b.ReportAllocs()
1457 paths = append(Paths(nil), paths...)
1458 imp(paths)
1459 }
1460 }
1461
1462 for n := 1; n <= maxSize; n <<= 1 {
1463 b.Run(strconv.Itoa(n), func(b *testing.B) {
1464 for _, implementation := range implementations {
1465 b.Run(implementation.name, func(b *testing.B) {
1466 b.Run("same", func(b *testing.B) {
1467 f(b, implementation.f, samePath[:n])
1468 })
1469 b.Run("unique", func(b *testing.B) {
1470 f(b, implementation.f, uniquePaths[:n])
1471 })
1472 })
1473 }
1474 })
1475 }
1476}