Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 1 | // 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 Cross | 635c3b0 | 2016-05-18 15:37:25 -0700 | [diff] [blame] | 15 | package android |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 16 | |
| 17 | import ( |
| 18 | "errors" |
| 19 | "fmt" |
Colin Cross | 937664a | 2019-03-06 10:17:32 -0800 | [diff] [blame] | 20 | "io/ioutil" |
| 21 | "os" |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 22 | "reflect" |
| 23 | "strings" |
| 24 | "testing" |
Dan Willemsen | 00269f2 | 2017-07-06 16:59:48 -0700 | [diff] [blame] | 25 | |
| 26 | "github.com/google/blueprint/pathtools" |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 27 | ) |
| 28 | |
| 29 | type strsTestCase struct { |
| 30 | in []string |
| 31 | out string |
| 32 | err []error |
| 33 | } |
| 34 | |
| 35 | var commonValidatePathTestCases = []strsTestCase{ |
| 36 | { |
| 37 | in: []string{""}, |
| 38 | out: "", |
| 39 | }, |
| 40 | { |
| 41 | in: []string{"a/b"}, |
| 42 | out: "a/b", |
| 43 | }, |
| 44 | { |
| 45 | in: []string{"a/b", "c"}, |
| 46 | out: "a/b/c", |
| 47 | }, |
| 48 | { |
| 49 | in: []string{"a/.."}, |
| 50 | out: ".", |
| 51 | }, |
| 52 | { |
| 53 | in: []string{"."}, |
| 54 | out: ".", |
| 55 | }, |
| 56 | { |
| 57 | in: []string{".."}, |
| 58 | out: "", |
| 59 | err: []error{errors.New("Path is outside directory: ..")}, |
| 60 | }, |
| 61 | { |
| 62 | in: []string{"../a"}, |
| 63 | out: "", |
| 64 | err: []error{errors.New("Path is outside directory: ../a")}, |
| 65 | }, |
| 66 | { |
| 67 | in: []string{"b/../../a"}, |
| 68 | out: "", |
| 69 | err: []error{errors.New("Path is outside directory: ../a")}, |
| 70 | }, |
| 71 | { |
| 72 | in: []string{"/a"}, |
| 73 | out: "", |
| 74 | err: []error{errors.New("Path is outside directory: /a")}, |
| 75 | }, |
Dan Willemsen | 80a7c2a | 2015-12-21 14:57:11 -0800 | [diff] [blame] | 76 | { |
| 77 | in: []string{"a", "../b"}, |
| 78 | out: "", |
| 79 | err: []error{errors.New("Path is outside directory: ../b")}, |
| 80 | }, |
| 81 | { |
| 82 | in: []string{"a", "b/../../c"}, |
| 83 | out: "", |
| 84 | err: []error{errors.New("Path is outside directory: ../c")}, |
| 85 | }, |
| 86 | { |
| 87 | in: []string{"a", "./.."}, |
| 88 | out: "", |
| 89 | err: []error{errors.New("Path is outside directory: ..")}, |
| 90 | }, |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 91 | } |
| 92 | |
| 93 | var validateSafePathTestCases = append(commonValidatePathTestCases, []strsTestCase{ |
| 94 | { |
| 95 | in: []string{"$host/../$a"}, |
| 96 | out: "$a", |
| 97 | }, |
| 98 | }...) |
| 99 | |
| 100 | var validatePathTestCases = append(commonValidatePathTestCases, []strsTestCase{ |
| 101 | { |
| 102 | in: []string{"$host/../$a"}, |
| 103 | out: "", |
| 104 | err: []error{errors.New("Path contains invalid character($): $host/../$a")}, |
| 105 | }, |
| 106 | { |
| 107 | in: []string{"$host/.."}, |
| 108 | out: "", |
| 109 | err: []error{errors.New("Path contains invalid character($): $host/..")}, |
| 110 | }, |
| 111 | }...) |
| 112 | |
| 113 | func TestValidateSafePath(t *testing.T) { |
| 114 | for _, testCase := range validateSafePathTestCases { |
Colin Cross | dc75ae7 | 2018-02-22 13:48:13 -0800 | [diff] [blame] | 115 | t.Run(strings.Join(testCase.in, ","), func(t *testing.T) { |
| 116 | ctx := &configErrorWrapper{} |
Colin Cross | 1ccfcc3 | 2018-02-22 13:54:26 -0800 | [diff] [blame] | 117 | out, err := validateSafePath(testCase.in...) |
| 118 | if err != nil { |
| 119 | reportPathError(ctx, err) |
| 120 | } |
Colin Cross | dc75ae7 | 2018-02-22 13:48:13 -0800 | [diff] [blame] | 121 | check(t, "validateSafePath", p(testCase.in), out, ctx.errors, testCase.out, testCase.err) |
| 122 | }) |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 123 | } |
| 124 | } |
| 125 | |
| 126 | func TestValidatePath(t *testing.T) { |
| 127 | for _, testCase := range validatePathTestCases { |
Colin Cross | dc75ae7 | 2018-02-22 13:48:13 -0800 | [diff] [blame] | 128 | t.Run(strings.Join(testCase.in, ","), func(t *testing.T) { |
| 129 | ctx := &configErrorWrapper{} |
Colin Cross | 1ccfcc3 | 2018-02-22 13:54:26 -0800 | [diff] [blame] | 130 | out, err := validatePath(testCase.in...) |
| 131 | if err != nil { |
| 132 | reportPathError(ctx, err) |
| 133 | } |
Colin Cross | dc75ae7 | 2018-02-22 13:48:13 -0800 | [diff] [blame] | 134 | check(t, "validatePath", p(testCase.in), out, ctx.errors, testCase.out, testCase.err) |
| 135 | }) |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 136 | } |
| 137 | } |
| 138 | |
| 139 | func TestOptionalPath(t *testing.T) { |
| 140 | var path OptionalPath |
| 141 | checkInvalidOptionalPath(t, path) |
| 142 | |
| 143 | path = OptionalPathForPath(nil) |
| 144 | checkInvalidOptionalPath(t, path) |
| 145 | } |
| 146 | |
| 147 | func checkInvalidOptionalPath(t *testing.T, path OptionalPath) { |
Colin Cross | dc75ae7 | 2018-02-22 13:48:13 -0800 | [diff] [blame] | 148 | t.Helper() |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 149 | if path.Valid() { |
| 150 | t.Errorf("Uninitialized OptionalPath should not be valid") |
| 151 | } |
| 152 | if path.String() != "" { |
| 153 | t.Errorf("Uninitialized OptionalPath String() should return \"\", not %q", path.String()) |
| 154 | } |
| 155 | defer func() { |
| 156 | if r := recover(); r == nil { |
| 157 | t.Errorf("Expected a panic when calling Path() on an uninitialized OptionalPath") |
| 158 | } |
| 159 | }() |
| 160 | path.Path() |
| 161 | } |
| 162 | |
| 163 | func check(t *testing.T, testType, testString string, |
| 164 | got interface{}, err []error, |
| 165 | expected interface{}, expectedErr []error) { |
Colin Cross | dc75ae7 | 2018-02-22 13:48:13 -0800 | [diff] [blame] | 166 | t.Helper() |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 167 | |
| 168 | printedTestCase := false |
| 169 | e := func(s string, expected, got interface{}) { |
Colin Cross | dc75ae7 | 2018-02-22 13:48:13 -0800 | [diff] [blame] | 170 | t.Helper() |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 171 | if !printedTestCase { |
| 172 | t.Errorf("test case %s: %s", testType, testString) |
| 173 | printedTestCase = true |
| 174 | } |
| 175 | t.Errorf("incorrect %s", s) |
| 176 | t.Errorf(" expected: %s", p(expected)) |
| 177 | t.Errorf(" got: %s", p(got)) |
| 178 | } |
| 179 | |
| 180 | if !reflect.DeepEqual(expectedErr, err) { |
| 181 | e("errors:", expectedErr, err) |
| 182 | } |
| 183 | |
| 184 | if !reflect.DeepEqual(expected, got) { |
| 185 | e("output:", expected, got) |
| 186 | } |
| 187 | } |
| 188 | |
| 189 | func p(in interface{}) string { |
| 190 | if v, ok := in.([]interface{}); ok { |
| 191 | s := make([]string, len(v)) |
| 192 | for i := range v { |
| 193 | s[i] = fmt.Sprintf("%#v", v[i]) |
| 194 | } |
| 195 | return "[" + strings.Join(s, ", ") + "]" |
| 196 | } else { |
| 197 | return fmt.Sprintf("%#v", in) |
| 198 | } |
| 199 | } |
Dan Willemsen | 00269f2 | 2017-07-06 16:59:48 -0700 | [diff] [blame] | 200 | |
| 201 | type moduleInstallPathContextImpl struct { |
| 202 | androidBaseContextImpl |
| 203 | |
| 204 | inData bool |
| 205 | inSanitizerDir bool |
Jiyong Park | f9332f1 | 2018-02-01 00:54:12 +0900 | [diff] [blame] | 206 | inRecovery bool |
Dan Willemsen | 00269f2 | 2017-07-06 16:59:48 -0700 | [diff] [blame] | 207 | } |
| 208 | |
| 209 | func (moduleInstallPathContextImpl) Fs() pathtools.FileSystem { |
| 210 | return pathtools.MockFs(nil) |
| 211 | } |
| 212 | |
Colin Cross | aabf679 | 2017-11-29 00:27:14 -0800 | [diff] [blame] | 213 | func (m moduleInstallPathContextImpl) Config() Config { |
Dan Willemsen | 00269f2 | 2017-07-06 16:59:48 -0700 | [diff] [blame] | 214 | return m.androidBaseContextImpl.config |
| 215 | } |
| 216 | |
| 217 | func (moduleInstallPathContextImpl) AddNinjaFileDeps(deps ...string) {} |
| 218 | |
| 219 | func (m moduleInstallPathContextImpl) InstallInData() bool { |
| 220 | return m.inData |
| 221 | } |
| 222 | |
| 223 | func (m moduleInstallPathContextImpl) InstallInSanitizerDir() bool { |
| 224 | return m.inSanitizerDir |
| 225 | } |
| 226 | |
Jiyong Park | f9332f1 | 2018-02-01 00:54:12 +0900 | [diff] [blame] | 227 | func (m moduleInstallPathContextImpl) InstallInRecovery() bool { |
| 228 | return m.inRecovery |
| 229 | } |
| 230 | |
Dan Willemsen | 00269f2 | 2017-07-06 16:59:48 -0700 | [diff] [blame] | 231 | func TestPathForModuleInstall(t *testing.T) { |
Colin Cross | 6ccbc91 | 2017-10-10 23:07:38 -0700 | [diff] [blame] | 232 | testConfig := TestConfig("", nil) |
Dan Willemsen | 00269f2 | 2017-07-06 16:59:48 -0700 | [diff] [blame] | 233 | |
| 234 | hostTarget := Target{Os: Linux} |
| 235 | deviceTarget := Target{Os: Android} |
| 236 | |
| 237 | testCases := []struct { |
| 238 | name string |
| 239 | ctx *moduleInstallPathContextImpl |
| 240 | in []string |
| 241 | out string |
| 242 | }{ |
| 243 | { |
| 244 | name: "host binary", |
| 245 | ctx: &moduleInstallPathContextImpl{ |
| 246 | androidBaseContextImpl: androidBaseContextImpl{ |
| 247 | target: hostTarget, |
| 248 | }, |
| 249 | }, |
| 250 | in: []string{"bin", "my_test"}, |
| 251 | out: "host/linux-x86/bin/my_test", |
| 252 | }, |
| 253 | |
| 254 | { |
| 255 | name: "system binary", |
| 256 | ctx: &moduleInstallPathContextImpl{ |
| 257 | androidBaseContextImpl: androidBaseContextImpl{ |
| 258 | target: deviceTarget, |
| 259 | }, |
| 260 | }, |
| 261 | in: []string{"bin", "my_test"}, |
| 262 | out: "target/product/test_device/system/bin/my_test", |
| 263 | }, |
| 264 | { |
| 265 | name: "vendor binary", |
| 266 | ctx: &moduleInstallPathContextImpl{ |
| 267 | androidBaseContextImpl: androidBaseContextImpl{ |
| 268 | target: deviceTarget, |
Jiyong Park | 2db7692 | 2017-11-08 16:03:48 +0900 | [diff] [blame] | 269 | kind: socSpecificModule, |
Dan Willemsen | 00269f2 | 2017-07-06 16:59:48 -0700 | [diff] [blame] | 270 | }, |
| 271 | }, |
| 272 | in: []string{"bin", "my_test"}, |
| 273 | out: "target/product/test_device/vendor/bin/my_test", |
| 274 | }, |
Jiyong Park | 2db7692 | 2017-11-08 16:03:48 +0900 | [diff] [blame] | 275 | { |
| 276 | name: "odm binary", |
| 277 | ctx: &moduleInstallPathContextImpl{ |
| 278 | androidBaseContextImpl: androidBaseContextImpl{ |
| 279 | target: deviceTarget, |
| 280 | kind: deviceSpecificModule, |
| 281 | }, |
| 282 | }, |
| 283 | in: []string{"bin", "my_test"}, |
| 284 | out: "target/product/test_device/odm/bin/my_test", |
| 285 | }, |
| 286 | { |
Jaekyun Seok | 5cfbfbb | 2018-01-10 19:00:15 +0900 | [diff] [blame] | 287 | name: "product binary", |
Jiyong Park | 2db7692 | 2017-11-08 16:03:48 +0900 | [diff] [blame] | 288 | ctx: &moduleInstallPathContextImpl{ |
| 289 | androidBaseContextImpl: androidBaseContextImpl{ |
| 290 | target: deviceTarget, |
| 291 | kind: productSpecificModule, |
| 292 | }, |
| 293 | }, |
| 294 | in: []string{"bin", "my_test"}, |
Jaekyun Seok | 5cfbfbb | 2018-01-10 19:00:15 +0900 | [diff] [blame] | 295 | out: "target/product/test_device/product/bin/my_test", |
Jiyong Park | 2db7692 | 2017-11-08 16:03:48 +0900 | [diff] [blame] | 296 | }, |
Dario Freni | fd05a74 | 2018-05-29 13:28:54 +0100 | [diff] [blame] | 297 | { |
Dario Freni | 95cf767 | 2018-08-17 00:57:57 +0100 | [diff] [blame] | 298 | name: "product_services binary", |
Dario Freni | fd05a74 | 2018-05-29 13:28:54 +0100 | [diff] [blame] | 299 | ctx: &moduleInstallPathContextImpl{ |
| 300 | androidBaseContextImpl: androidBaseContextImpl{ |
| 301 | target: deviceTarget, |
| 302 | kind: productServicesSpecificModule, |
| 303 | }, |
| 304 | }, |
| 305 | in: []string{"bin", "my_test"}, |
Dario Freni | 95cf767 | 2018-08-17 00:57:57 +0100 | [diff] [blame] | 306 | out: "target/product/test_device/product_services/bin/my_test", |
Dario Freni | fd05a74 | 2018-05-29 13:28:54 +0100 | [diff] [blame] | 307 | }, |
Dan Willemsen | 00269f2 | 2017-07-06 16:59:48 -0700 | [diff] [blame] | 308 | |
| 309 | { |
| 310 | name: "system native test binary", |
| 311 | ctx: &moduleInstallPathContextImpl{ |
| 312 | androidBaseContextImpl: androidBaseContextImpl{ |
| 313 | target: deviceTarget, |
| 314 | }, |
| 315 | inData: true, |
| 316 | }, |
| 317 | in: []string{"nativetest", "my_test"}, |
| 318 | out: "target/product/test_device/data/nativetest/my_test", |
| 319 | }, |
| 320 | { |
| 321 | name: "vendor native test binary", |
| 322 | ctx: &moduleInstallPathContextImpl{ |
| 323 | androidBaseContextImpl: androidBaseContextImpl{ |
| 324 | target: deviceTarget, |
Jiyong Park | 2db7692 | 2017-11-08 16:03:48 +0900 | [diff] [blame] | 325 | kind: socSpecificModule, |
| 326 | }, |
| 327 | inData: true, |
| 328 | }, |
| 329 | in: []string{"nativetest", "my_test"}, |
| 330 | out: "target/product/test_device/data/nativetest/my_test", |
| 331 | }, |
| 332 | { |
| 333 | name: "odm native test binary", |
| 334 | ctx: &moduleInstallPathContextImpl{ |
| 335 | androidBaseContextImpl: androidBaseContextImpl{ |
| 336 | target: deviceTarget, |
| 337 | kind: deviceSpecificModule, |
| 338 | }, |
| 339 | inData: true, |
| 340 | }, |
| 341 | in: []string{"nativetest", "my_test"}, |
| 342 | out: "target/product/test_device/data/nativetest/my_test", |
| 343 | }, |
| 344 | { |
Jaekyun Seok | 5cfbfbb | 2018-01-10 19:00:15 +0900 | [diff] [blame] | 345 | name: "product native test binary", |
Jiyong Park | 2db7692 | 2017-11-08 16:03:48 +0900 | [diff] [blame] | 346 | ctx: &moduleInstallPathContextImpl{ |
| 347 | androidBaseContextImpl: androidBaseContextImpl{ |
| 348 | target: deviceTarget, |
| 349 | kind: productSpecificModule, |
Dan Willemsen | 00269f2 | 2017-07-06 16:59:48 -0700 | [diff] [blame] | 350 | }, |
| 351 | inData: true, |
| 352 | }, |
| 353 | in: []string{"nativetest", "my_test"}, |
| 354 | out: "target/product/test_device/data/nativetest/my_test", |
| 355 | }, |
| 356 | |
| 357 | { |
Dario Freni | 95cf767 | 2018-08-17 00:57:57 +0100 | [diff] [blame] | 358 | name: "product_services native test binary", |
Dario Freni | fd05a74 | 2018-05-29 13:28:54 +0100 | [diff] [blame] | 359 | ctx: &moduleInstallPathContextImpl{ |
| 360 | androidBaseContextImpl: androidBaseContextImpl{ |
| 361 | target: deviceTarget, |
| 362 | kind: productServicesSpecificModule, |
| 363 | }, |
| 364 | inData: true, |
| 365 | }, |
| 366 | in: []string{"nativetest", "my_test"}, |
| 367 | out: "target/product/test_device/data/nativetest/my_test", |
| 368 | }, |
| 369 | |
| 370 | { |
Dan Willemsen | 00269f2 | 2017-07-06 16:59:48 -0700 | [diff] [blame] | 371 | name: "sanitized system binary", |
| 372 | ctx: &moduleInstallPathContextImpl{ |
| 373 | androidBaseContextImpl: androidBaseContextImpl{ |
| 374 | target: deviceTarget, |
| 375 | }, |
| 376 | inSanitizerDir: true, |
| 377 | }, |
| 378 | in: []string{"bin", "my_test"}, |
| 379 | out: "target/product/test_device/data/asan/system/bin/my_test", |
| 380 | }, |
| 381 | { |
| 382 | name: "sanitized vendor binary", |
| 383 | ctx: &moduleInstallPathContextImpl{ |
| 384 | androidBaseContextImpl: androidBaseContextImpl{ |
| 385 | target: deviceTarget, |
Jiyong Park | 2db7692 | 2017-11-08 16:03:48 +0900 | [diff] [blame] | 386 | kind: socSpecificModule, |
Dan Willemsen | 00269f2 | 2017-07-06 16:59:48 -0700 | [diff] [blame] | 387 | }, |
| 388 | inSanitizerDir: true, |
| 389 | }, |
| 390 | in: []string{"bin", "my_test"}, |
| 391 | out: "target/product/test_device/data/asan/vendor/bin/my_test", |
| 392 | }, |
Jiyong Park | 2db7692 | 2017-11-08 16:03:48 +0900 | [diff] [blame] | 393 | { |
| 394 | name: "sanitized odm binary", |
| 395 | ctx: &moduleInstallPathContextImpl{ |
| 396 | androidBaseContextImpl: androidBaseContextImpl{ |
| 397 | target: deviceTarget, |
| 398 | kind: deviceSpecificModule, |
| 399 | }, |
| 400 | inSanitizerDir: true, |
| 401 | }, |
| 402 | in: []string{"bin", "my_test"}, |
| 403 | out: "target/product/test_device/data/asan/odm/bin/my_test", |
| 404 | }, |
| 405 | { |
Jaekyun Seok | 5cfbfbb | 2018-01-10 19:00:15 +0900 | [diff] [blame] | 406 | name: "sanitized product binary", |
Jiyong Park | 2db7692 | 2017-11-08 16:03:48 +0900 | [diff] [blame] | 407 | ctx: &moduleInstallPathContextImpl{ |
| 408 | androidBaseContextImpl: androidBaseContextImpl{ |
| 409 | target: deviceTarget, |
| 410 | kind: productSpecificModule, |
| 411 | }, |
| 412 | inSanitizerDir: true, |
| 413 | }, |
| 414 | in: []string{"bin", "my_test"}, |
Jaekyun Seok | 5cfbfbb | 2018-01-10 19:00:15 +0900 | [diff] [blame] | 415 | out: "target/product/test_device/data/asan/product/bin/my_test", |
Jiyong Park | 2db7692 | 2017-11-08 16:03:48 +0900 | [diff] [blame] | 416 | }, |
Dan Willemsen | 00269f2 | 2017-07-06 16:59:48 -0700 | [diff] [blame] | 417 | |
| 418 | { |
Dario Freni | 95cf767 | 2018-08-17 00:57:57 +0100 | [diff] [blame] | 419 | name: "sanitized product_services binary", |
Dario Freni | fd05a74 | 2018-05-29 13:28:54 +0100 | [diff] [blame] | 420 | ctx: &moduleInstallPathContextImpl{ |
| 421 | androidBaseContextImpl: androidBaseContextImpl{ |
| 422 | target: deviceTarget, |
| 423 | kind: productServicesSpecificModule, |
| 424 | }, |
| 425 | inSanitizerDir: true, |
| 426 | }, |
| 427 | in: []string{"bin", "my_test"}, |
Dario Freni | 95cf767 | 2018-08-17 00:57:57 +0100 | [diff] [blame] | 428 | out: "target/product/test_device/data/asan/product_services/bin/my_test", |
Dario Freni | fd05a74 | 2018-05-29 13:28:54 +0100 | [diff] [blame] | 429 | }, |
| 430 | |
| 431 | { |
Dan Willemsen | 00269f2 | 2017-07-06 16:59:48 -0700 | [diff] [blame] | 432 | name: "sanitized system native test binary", |
| 433 | ctx: &moduleInstallPathContextImpl{ |
| 434 | androidBaseContextImpl: androidBaseContextImpl{ |
| 435 | target: deviceTarget, |
| 436 | }, |
| 437 | inData: true, |
| 438 | inSanitizerDir: true, |
| 439 | }, |
| 440 | in: []string{"nativetest", "my_test"}, |
| 441 | out: "target/product/test_device/data/asan/data/nativetest/my_test", |
| 442 | }, |
| 443 | { |
| 444 | name: "sanitized vendor native test binary", |
| 445 | ctx: &moduleInstallPathContextImpl{ |
| 446 | androidBaseContextImpl: androidBaseContextImpl{ |
| 447 | target: deviceTarget, |
Jiyong Park | 2db7692 | 2017-11-08 16:03:48 +0900 | [diff] [blame] | 448 | kind: socSpecificModule, |
| 449 | }, |
| 450 | inData: true, |
| 451 | inSanitizerDir: true, |
| 452 | }, |
| 453 | in: []string{"nativetest", "my_test"}, |
| 454 | out: "target/product/test_device/data/asan/data/nativetest/my_test", |
| 455 | }, |
| 456 | { |
| 457 | name: "sanitized odm native test binary", |
| 458 | ctx: &moduleInstallPathContextImpl{ |
| 459 | androidBaseContextImpl: androidBaseContextImpl{ |
| 460 | target: deviceTarget, |
| 461 | kind: deviceSpecificModule, |
| 462 | }, |
| 463 | inData: true, |
| 464 | inSanitizerDir: true, |
| 465 | }, |
| 466 | in: []string{"nativetest", "my_test"}, |
| 467 | out: "target/product/test_device/data/asan/data/nativetest/my_test", |
| 468 | }, |
| 469 | { |
Jaekyun Seok | 5cfbfbb | 2018-01-10 19:00:15 +0900 | [diff] [blame] | 470 | name: "sanitized product native test binary", |
Jiyong Park | 2db7692 | 2017-11-08 16:03:48 +0900 | [diff] [blame] | 471 | ctx: &moduleInstallPathContextImpl{ |
| 472 | androidBaseContextImpl: androidBaseContextImpl{ |
| 473 | target: deviceTarget, |
| 474 | kind: productSpecificModule, |
Dan Willemsen | 00269f2 | 2017-07-06 16:59:48 -0700 | [diff] [blame] | 475 | }, |
| 476 | inData: true, |
| 477 | inSanitizerDir: true, |
| 478 | }, |
| 479 | in: []string{"nativetest", "my_test"}, |
| 480 | out: "target/product/test_device/data/asan/data/nativetest/my_test", |
| 481 | }, |
Dario Freni | fd05a74 | 2018-05-29 13:28:54 +0100 | [diff] [blame] | 482 | { |
Dario Freni | 95cf767 | 2018-08-17 00:57:57 +0100 | [diff] [blame] | 483 | name: "sanitized product_services native test binary", |
Dario Freni | fd05a74 | 2018-05-29 13:28:54 +0100 | [diff] [blame] | 484 | ctx: &moduleInstallPathContextImpl{ |
| 485 | androidBaseContextImpl: androidBaseContextImpl{ |
| 486 | target: deviceTarget, |
| 487 | kind: productServicesSpecificModule, |
| 488 | }, |
| 489 | inData: true, |
| 490 | inSanitizerDir: true, |
| 491 | }, |
| 492 | in: []string{"nativetest", "my_test"}, |
| 493 | out: "target/product/test_device/data/asan/data/nativetest/my_test", |
| 494 | }, |
Dan Willemsen | 00269f2 | 2017-07-06 16:59:48 -0700 | [diff] [blame] | 495 | } |
| 496 | |
| 497 | for _, tc := range testCases { |
| 498 | t.Run(tc.name, func(t *testing.T) { |
| 499 | tc.ctx.androidBaseContextImpl.config = testConfig |
| 500 | output := PathForModuleInstall(tc.ctx, tc.in...) |
| 501 | if output.basePath.path != tc.out { |
| 502 | t.Errorf("unexpected path:\n got: %q\nwant: %q\n", |
| 503 | output.basePath.path, |
| 504 | tc.out) |
| 505 | } |
| 506 | }) |
| 507 | } |
| 508 | } |
Colin Cross | 5e6cfbe | 2017-11-03 15:20:35 -0700 | [diff] [blame] | 509 | |
| 510 | func TestDirectorySortedPaths(t *testing.T) { |
Colin Cross | 07e5161 | 2019-03-05 12:46:40 -0800 | [diff] [blame^] | 511 | config := TestConfig("out", nil) |
| 512 | |
| 513 | ctx := PathContextForTesting(config, map[string][]byte{ |
| 514 | "a.txt": nil, |
| 515 | "a/txt": nil, |
| 516 | "a/b/c": nil, |
| 517 | "a/b/d": nil, |
| 518 | "b": nil, |
| 519 | "b/b.txt": nil, |
| 520 | "a/a.txt": nil, |
| 521 | }) |
| 522 | |
Colin Cross | 5e6cfbe | 2017-11-03 15:20:35 -0700 | [diff] [blame] | 523 | makePaths := func() Paths { |
| 524 | return Paths{ |
Colin Cross | 07e5161 | 2019-03-05 12:46:40 -0800 | [diff] [blame^] | 525 | PathForSource(ctx, "a.txt"), |
| 526 | PathForSource(ctx, "a/txt"), |
| 527 | PathForSource(ctx, "a/b/c"), |
| 528 | PathForSource(ctx, "a/b/d"), |
| 529 | PathForSource(ctx, "b"), |
| 530 | PathForSource(ctx, "b/b.txt"), |
| 531 | PathForSource(ctx, "a/a.txt"), |
Colin Cross | 5e6cfbe | 2017-11-03 15:20:35 -0700 | [diff] [blame] | 532 | } |
| 533 | } |
| 534 | |
| 535 | expected := []string{ |
| 536 | "a.txt", |
| 537 | "a/a.txt", |
| 538 | "a/b/c", |
| 539 | "a/b/d", |
| 540 | "a/txt", |
| 541 | "b", |
| 542 | "b/b.txt", |
| 543 | } |
| 544 | |
| 545 | paths := makePaths() |
Colin Cross | a140bb0 | 2018-04-17 10:52:26 -0700 | [diff] [blame] | 546 | reversePaths := ReversePaths(paths) |
Colin Cross | 5e6cfbe | 2017-11-03 15:20:35 -0700 | [diff] [blame] | 547 | |
| 548 | sortedPaths := PathsToDirectorySortedPaths(paths) |
| 549 | reverseSortedPaths := PathsToDirectorySortedPaths(reversePaths) |
| 550 | |
| 551 | if !reflect.DeepEqual(Paths(sortedPaths).Strings(), expected) { |
| 552 | t.Fatalf("sorted paths:\n %#v\n != \n %#v", paths.Strings(), expected) |
| 553 | } |
| 554 | |
| 555 | if !reflect.DeepEqual(Paths(reverseSortedPaths).Strings(), expected) { |
| 556 | t.Fatalf("sorted reversed paths:\n %#v\n !=\n %#v", reversePaths.Strings(), expected) |
| 557 | } |
| 558 | |
| 559 | expectedA := []string{ |
| 560 | "a/a.txt", |
| 561 | "a/b/c", |
| 562 | "a/b/d", |
| 563 | "a/txt", |
| 564 | } |
| 565 | |
| 566 | inA := sortedPaths.PathsInDirectory("a") |
| 567 | if !reflect.DeepEqual(inA.Strings(), expectedA) { |
| 568 | t.Errorf("FilesInDirectory(a):\n %#v\n != \n %#v", inA.Strings(), expectedA) |
| 569 | } |
| 570 | |
| 571 | expectedA_B := []string{ |
| 572 | "a/b/c", |
| 573 | "a/b/d", |
| 574 | } |
| 575 | |
| 576 | inA_B := sortedPaths.PathsInDirectory("a/b") |
| 577 | if !reflect.DeepEqual(inA_B.Strings(), expectedA_B) { |
| 578 | t.Errorf("FilesInDirectory(a/b):\n %#v\n != \n %#v", inA_B.Strings(), expectedA_B) |
| 579 | } |
| 580 | |
| 581 | expectedB := []string{ |
| 582 | "b/b.txt", |
| 583 | } |
| 584 | |
| 585 | inB := sortedPaths.PathsInDirectory("b") |
| 586 | if !reflect.DeepEqual(inB.Strings(), expectedB) { |
| 587 | t.Errorf("FilesInDirectory(b):\n %#v\n != \n %#v", inA.Strings(), expectedA) |
| 588 | } |
| 589 | } |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 590 | |
| 591 | func TestMaybeRel(t *testing.T) { |
| 592 | testCases := []struct { |
| 593 | name string |
| 594 | base string |
| 595 | target string |
| 596 | out string |
| 597 | isRel bool |
| 598 | }{ |
| 599 | { |
| 600 | name: "normal", |
| 601 | base: "a/b/c", |
| 602 | target: "a/b/c/d", |
| 603 | out: "d", |
| 604 | isRel: true, |
| 605 | }, |
| 606 | { |
| 607 | name: "parent", |
| 608 | base: "a/b/c/d", |
| 609 | target: "a/b/c", |
| 610 | isRel: false, |
| 611 | }, |
| 612 | { |
| 613 | name: "not relative", |
| 614 | base: "a/b", |
| 615 | target: "c/d", |
| 616 | isRel: false, |
| 617 | }, |
| 618 | { |
| 619 | name: "abs1", |
| 620 | base: "/a", |
| 621 | target: "a", |
| 622 | isRel: false, |
| 623 | }, |
| 624 | { |
| 625 | name: "abs2", |
| 626 | base: "a", |
| 627 | target: "/a", |
| 628 | isRel: false, |
| 629 | }, |
| 630 | } |
| 631 | |
| 632 | for _, testCase := range testCases { |
| 633 | t.Run(testCase.name, func(t *testing.T) { |
| 634 | ctx := &configErrorWrapper{} |
| 635 | out, isRel := MaybeRel(ctx, testCase.base, testCase.target) |
| 636 | if len(ctx.errors) > 0 { |
| 637 | t.Errorf("MaybeRel(..., %s, %s) reported unexpected errors %v", |
| 638 | testCase.base, testCase.target, ctx.errors) |
| 639 | } |
| 640 | if isRel != testCase.isRel || out != testCase.out { |
| 641 | t.Errorf("MaybeRel(..., %s, %s) want %v, %v got %v, %v", |
| 642 | testCase.base, testCase.target, testCase.out, testCase.isRel, out, isRel) |
| 643 | } |
| 644 | }) |
| 645 | } |
| 646 | } |
Colin Cross | 7b3dcc3 | 2019-01-24 13:14:39 -0800 | [diff] [blame] | 647 | |
| 648 | func TestPathForSource(t *testing.T) { |
| 649 | testCases := []struct { |
| 650 | name string |
| 651 | buildDir string |
| 652 | src string |
| 653 | err string |
| 654 | }{ |
| 655 | { |
| 656 | name: "normal", |
| 657 | buildDir: "out", |
| 658 | src: "a/b/c", |
| 659 | }, |
| 660 | { |
| 661 | name: "abs", |
| 662 | buildDir: "out", |
| 663 | src: "/a/b/c", |
| 664 | err: "is outside directory", |
| 665 | }, |
| 666 | { |
| 667 | name: "in out dir", |
| 668 | buildDir: "out", |
| 669 | src: "out/a/b/c", |
| 670 | err: "is in output", |
| 671 | }, |
| 672 | } |
| 673 | |
| 674 | funcs := []struct { |
| 675 | name string |
| 676 | f func(ctx PathContext, pathComponents ...string) (SourcePath, error) |
| 677 | }{ |
| 678 | {"pathForSource", pathForSource}, |
| 679 | {"safePathForSource", safePathForSource}, |
| 680 | } |
| 681 | |
| 682 | for _, f := range funcs { |
| 683 | t.Run(f.name, func(t *testing.T) { |
| 684 | for _, test := range testCases { |
| 685 | t.Run(test.name, func(t *testing.T) { |
| 686 | testConfig := TestConfig(test.buildDir, nil) |
| 687 | ctx := &configErrorWrapper{config: testConfig} |
| 688 | _, err := f.f(ctx, test.src) |
| 689 | if len(ctx.errors) > 0 { |
| 690 | t.Fatalf("unexpected errors %v", ctx.errors) |
| 691 | } |
| 692 | if err != nil { |
| 693 | if test.err == "" { |
| 694 | t.Fatalf("unexpected error %q", err.Error()) |
| 695 | } else if !strings.Contains(err.Error(), test.err) { |
| 696 | t.Fatalf("incorrect error, want substring %q got %q", test.err, err.Error()) |
| 697 | } |
| 698 | } else { |
| 699 | if test.err != "" { |
| 700 | t.Fatalf("missing error %q", test.err) |
| 701 | } |
| 702 | } |
| 703 | }) |
| 704 | } |
| 705 | }) |
| 706 | } |
| 707 | } |
Colin Cross | 8854a5a | 2019-02-11 14:14:16 -0800 | [diff] [blame] | 708 | |
Colin Cross | 937664a | 2019-03-06 10:17:32 -0800 | [diff] [blame] | 709 | type expandSourcesTestModule struct { |
| 710 | ModuleBase |
| 711 | props struct { |
| 712 | Srcs []string `android:"path"` |
| 713 | Exclude_srcs []string `android:"path"` |
| 714 | } |
| 715 | |
| 716 | srcs Paths |
| 717 | rels []string |
| 718 | } |
| 719 | |
| 720 | func expandSourcesTestModuleFactory() Module { |
| 721 | module := &expandSourcesTestModule{} |
| 722 | module.AddProperties(&module.props) |
| 723 | InitAndroidModule(module) |
| 724 | return module |
| 725 | } |
| 726 | |
| 727 | func (p *expandSourcesTestModule) GenerateAndroidBuildActions(ctx ModuleContext) { |
| 728 | p.srcs = ctx.ExpandSources(p.props.Srcs, p.props.Exclude_srcs) |
| 729 | |
| 730 | for _, src := range p.srcs { |
| 731 | p.rels = append(p.rels, src.Rel()) |
| 732 | } |
| 733 | } |
| 734 | |
| 735 | func TestExpandSources(t *testing.T) { |
| 736 | tests := []struct { |
| 737 | name string |
| 738 | bp string |
| 739 | srcs []string |
| 740 | rels []string |
| 741 | }{ |
| 742 | { |
| 743 | name: "path", |
| 744 | bp: ` |
| 745 | test { |
| 746 | name: "foo", |
| 747 | srcs: ["src/b"], |
| 748 | }`, |
| 749 | srcs: []string{"foo/src/b"}, |
| 750 | rels: []string{"src/b"}, |
| 751 | }, |
| 752 | { |
| 753 | name: "glob", |
| 754 | bp: ` |
| 755 | test { |
| 756 | name: "foo", |
| 757 | srcs: [ |
| 758 | "src/*", |
| 759 | "src/e/*", |
| 760 | ], |
| 761 | }`, |
| 762 | srcs: []string{"foo/src/b", "foo/src/c", "foo/src/d", "foo/src/e/e"}, |
| 763 | rels: []string{"src/b", "src/c", "src/d", "src/e/e"}, |
| 764 | }, |
| 765 | { |
| 766 | name: "recursive glob", |
| 767 | bp: ` |
| 768 | test { |
| 769 | name: "foo", |
| 770 | srcs: ["src/**/*"], |
| 771 | }`, |
| 772 | srcs: []string{"foo/src/b", "foo/src/c", "foo/src/d", "foo/src/e/e"}, |
| 773 | rels: []string{"src/b", "src/c", "src/d", "src/e/e"}, |
| 774 | }, |
| 775 | { |
| 776 | name: "filegroup", |
| 777 | bp: ` |
| 778 | test { |
| 779 | name: "foo", |
| 780 | srcs: [":a"], |
| 781 | }`, |
| 782 | srcs: []string{"fg/src/a"}, |
| 783 | rels: []string{"src/a"}, |
| 784 | }, |
| 785 | { |
| 786 | name: "special characters glob", |
| 787 | bp: ` |
| 788 | test { |
| 789 | name: "foo", |
| 790 | srcs: ["src_special/*"], |
| 791 | }`, |
| 792 | srcs: []string{"foo/src_special/$"}, |
| 793 | rels: []string{"src_special/$"}, |
| 794 | }, |
| 795 | } |
| 796 | |
| 797 | buildDir, err := ioutil.TempDir("", "soong_path_for_module_src_test") |
| 798 | if err != nil { |
| 799 | t.Fatal(err) |
| 800 | } |
| 801 | defer os.RemoveAll(buildDir) |
| 802 | |
| 803 | for _, test := range tests { |
| 804 | t.Run(test.name, func(t *testing.T) { |
| 805 | config := TestConfig(buildDir, nil) |
| 806 | ctx := NewTestContext() |
| 807 | |
| 808 | ctx.RegisterModuleType("test", ModuleFactoryAdaptor(expandSourcesTestModuleFactory)) |
| 809 | ctx.RegisterModuleType("filegroup", ModuleFactoryAdaptor(FileGroupFactory)) |
| 810 | |
| 811 | fgBp := ` |
| 812 | filegroup { |
| 813 | name: "a", |
| 814 | srcs: ["src/a"], |
| 815 | } |
| 816 | ` |
| 817 | |
| 818 | mockFS := map[string][]byte{ |
| 819 | "fg/Android.bp": []byte(fgBp), |
| 820 | "foo/Android.bp": []byte(test.bp), |
| 821 | "fg/src/a": nil, |
| 822 | "foo/src/b": nil, |
| 823 | "foo/src/c": nil, |
| 824 | "foo/src/d": nil, |
| 825 | "foo/src/e/e": nil, |
| 826 | "foo/src_special/$": nil, |
| 827 | } |
| 828 | |
| 829 | ctx.MockFileSystem(mockFS) |
| 830 | |
| 831 | ctx.Register() |
| 832 | _, errs := ctx.ParseFileList(".", []string{"fg/Android.bp", "foo/Android.bp"}) |
| 833 | FailIfErrored(t, errs) |
| 834 | _, errs = ctx.PrepareBuildActions(config) |
| 835 | FailIfErrored(t, errs) |
| 836 | |
| 837 | m := ctx.ModuleForTests("foo", "").Module().(*expandSourcesTestModule) |
| 838 | |
| 839 | if g, w := m.srcs.Strings(), test.srcs; !reflect.DeepEqual(g, w) { |
| 840 | t.Errorf("want srcs %q, got %q", w, g) |
| 841 | } |
| 842 | |
| 843 | if g, w := m.rels, test.rels; !reflect.DeepEqual(g, w) { |
| 844 | t.Errorf("want rels %q, got %q", w, g) |
| 845 | } |
| 846 | }) |
| 847 | } |
| 848 | } |
| 849 | |
Colin Cross | 8854a5a | 2019-02-11 14:14:16 -0800 | [diff] [blame] | 850 | func ExampleOutputPath_ReplaceExtension() { |
| 851 | ctx := &configErrorWrapper{ |
| 852 | config: TestConfig("out", nil), |
| 853 | } |
Colin Cross | 2cdd5df | 2019-02-25 10:25:24 -0800 | [diff] [blame] | 854 | p := PathForOutput(ctx, "system/framework").Join(ctx, "boot.art") |
Colin Cross | 8854a5a | 2019-02-11 14:14:16 -0800 | [diff] [blame] | 855 | p2 := p.ReplaceExtension(ctx, "oat") |
| 856 | fmt.Println(p, p2) |
Colin Cross | 2cdd5df | 2019-02-25 10:25:24 -0800 | [diff] [blame] | 857 | fmt.Println(p.Rel(), p2.Rel()) |
Colin Cross | 8854a5a | 2019-02-11 14:14:16 -0800 | [diff] [blame] | 858 | |
| 859 | // Output: |
| 860 | // out/system/framework/boot.art out/system/framework/boot.oat |
Colin Cross | 2cdd5df | 2019-02-25 10:25:24 -0800 | [diff] [blame] | 861 | // boot.art boot.oat |
Colin Cross | 8854a5a | 2019-02-11 14:14:16 -0800 | [diff] [blame] | 862 | } |
Colin Cross | 40e3373 | 2019-02-15 11:08:35 -0800 | [diff] [blame] | 863 | |
| 864 | func ExampleOutputPath_FileInSameDir() { |
| 865 | ctx := &configErrorWrapper{ |
| 866 | config: TestConfig("out", nil), |
| 867 | } |
Colin Cross | 2cdd5df | 2019-02-25 10:25:24 -0800 | [diff] [blame] | 868 | p := PathForOutput(ctx, "system/framework").Join(ctx, "boot.art") |
Colin Cross | 40e3373 | 2019-02-15 11:08:35 -0800 | [diff] [blame] | 869 | p2 := p.InSameDir(ctx, "oat", "arm", "boot.vdex") |
| 870 | fmt.Println(p, p2) |
Colin Cross | 2cdd5df | 2019-02-25 10:25:24 -0800 | [diff] [blame] | 871 | fmt.Println(p.Rel(), p2.Rel()) |
Colin Cross | 40e3373 | 2019-02-15 11:08:35 -0800 | [diff] [blame] | 872 | |
| 873 | // Output: |
| 874 | // out/system/framework/boot.art out/system/framework/oat/arm/boot.vdex |
Colin Cross | 2cdd5df | 2019-02-25 10:25:24 -0800 | [diff] [blame] | 875 | // boot.art oat/arm/boot.vdex |
Colin Cross | 40e3373 | 2019-02-15 11:08:35 -0800 | [diff] [blame] | 876 | } |