paths_test.go 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323
  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. package android
  15. import (
  16. "errors"
  17. "fmt"
  18. "reflect"
  19. "strconv"
  20. "strings"
  21. "testing"
  22. "github.com/google/blueprint/proptools"
  23. )
  24. type strsTestCase struct {
  25. in []string
  26. out string
  27. err []error
  28. }
  29. var commonValidatePathTestCases = []strsTestCase{
  30. {
  31. in: []string{""},
  32. out: "",
  33. },
  34. {
  35. in: []string{"a/b"},
  36. out: "a/b",
  37. },
  38. {
  39. in: []string{"a/b", "c"},
  40. out: "a/b/c",
  41. },
  42. {
  43. in: []string{"a/.."},
  44. out: ".",
  45. },
  46. {
  47. in: []string{"."},
  48. out: ".",
  49. },
  50. {
  51. in: []string{".."},
  52. out: "",
  53. err: []error{errors.New("Path is outside directory: ..")},
  54. },
  55. {
  56. in: []string{"../a"},
  57. out: "",
  58. err: []error{errors.New("Path is outside directory: ../a")},
  59. },
  60. {
  61. in: []string{"b/../../a"},
  62. out: "",
  63. err: []error{errors.New("Path is outside directory: ../a")},
  64. },
  65. {
  66. in: []string{"/a"},
  67. out: "",
  68. err: []error{errors.New("Path is outside directory: /a")},
  69. },
  70. {
  71. in: []string{"a", "../b"},
  72. out: "",
  73. err: []error{errors.New("Path is outside directory: ../b")},
  74. },
  75. {
  76. in: []string{"a", "b/../../c"},
  77. out: "",
  78. err: []error{errors.New("Path is outside directory: ../c")},
  79. },
  80. {
  81. in: []string{"a", "./.."},
  82. out: "",
  83. err: []error{errors.New("Path is outside directory: ..")},
  84. },
  85. }
  86. var validateSafePathTestCases = append(commonValidatePathTestCases, []strsTestCase{
  87. {
  88. in: []string{"$host/../$a"},
  89. out: "$a",
  90. },
  91. }...)
  92. var validatePathTestCases = append(commonValidatePathTestCases, []strsTestCase{
  93. {
  94. in: []string{"$host/../$a"},
  95. out: "",
  96. err: []error{errors.New("Path contains invalid character($): $host/../$a")},
  97. },
  98. {
  99. in: []string{"$host/.."},
  100. out: "",
  101. err: []error{errors.New("Path contains invalid character($): $host/..")},
  102. },
  103. }...)
  104. func TestValidateSafePath(t *testing.T) {
  105. for _, testCase := range validateSafePathTestCases {
  106. t.Run(strings.Join(testCase.in, ","), func(t *testing.T) {
  107. ctx := &configErrorWrapper{}
  108. out, err := validateSafePath(testCase.in...)
  109. if err != nil {
  110. reportPathError(ctx, err)
  111. }
  112. check(t, "validateSafePath", p(testCase.in), out, ctx.errors, testCase.out, testCase.err)
  113. })
  114. }
  115. }
  116. func TestValidatePath(t *testing.T) {
  117. for _, testCase := range validatePathTestCases {
  118. t.Run(strings.Join(testCase.in, ","), func(t *testing.T) {
  119. ctx := &configErrorWrapper{}
  120. out, err := validatePath(testCase.in...)
  121. if err != nil {
  122. reportPathError(ctx, err)
  123. }
  124. check(t, "validatePath", p(testCase.in), out, ctx.errors, testCase.out, testCase.err)
  125. })
  126. }
  127. }
  128. func TestOptionalPath(t *testing.T) {
  129. var path OptionalPath
  130. checkInvalidOptionalPath(t, path)
  131. path = OptionalPathForPath(nil)
  132. checkInvalidOptionalPath(t, path)
  133. }
  134. func checkInvalidOptionalPath(t *testing.T, path OptionalPath) {
  135. t.Helper()
  136. if path.Valid() {
  137. t.Errorf("Uninitialized OptionalPath should not be valid")
  138. }
  139. if path.String() != "" {
  140. t.Errorf("Uninitialized OptionalPath String() should return \"\", not %q", path.String())
  141. }
  142. defer func() {
  143. if r := recover(); r == nil {
  144. t.Errorf("Expected a panic when calling Path() on an uninitialized OptionalPath")
  145. }
  146. }()
  147. path.Path()
  148. }
  149. func check(t *testing.T, testType, testString string,
  150. got interface{}, err []error,
  151. expected interface{}, expectedErr []error) {
  152. t.Helper()
  153. printedTestCase := false
  154. e := func(s string, expected, got interface{}) {
  155. t.Helper()
  156. if !printedTestCase {
  157. t.Errorf("test case %s: %s", testType, testString)
  158. printedTestCase = true
  159. }
  160. t.Errorf("incorrect %s", s)
  161. t.Errorf(" expected: %s", p(expected))
  162. t.Errorf(" got: %s", p(got))
  163. }
  164. if !reflect.DeepEqual(expectedErr, err) {
  165. e("errors:", expectedErr, err)
  166. }
  167. if !reflect.DeepEqual(expected, got) {
  168. e("output:", expected, got)
  169. }
  170. }
  171. func p(in interface{}) string {
  172. if v, ok := in.([]interface{}); ok {
  173. s := make([]string, len(v))
  174. for i := range v {
  175. s[i] = fmt.Sprintf("%#v", v[i])
  176. }
  177. return "[" + strings.Join(s, ", ") + "]"
  178. } else {
  179. return fmt.Sprintf("%#v", in)
  180. }
  181. }
  182. type moduleInstallPathContextImpl struct {
  183. baseModuleContext
  184. inData bool
  185. inTestcases bool
  186. inSanitizerDir bool
  187. inRamdisk bool
  188. inRecovery bool
  189. inRoot bool
  190. forceOS *OsType
  191. forceArch *ArchType
  192. }
  193. func (m moduleInstallPathContextImpl) Config() Config {
  194. return m.baseModuleContext.config
  195. }
  196. func (moduleInstallPathContextImpl) AddNinjaFileDeps(deps ...string) {}
  197. func (m moduleInstallPathContextImpl) InstallInData() bool {
  198. return m.inData
  199. }
  200. func (m moduleInstallPathContextImpl) InstallInTestcases() bool {
  201. return m.inTestcases
  202. }
  203. func (m moduleInstallPathContextImpl) InstallInSanitizerDir() bool {
  204. return m.inSanitizerDir
  205. }
  206. func (m moduleInstallPathContextImpl) InstallInRamdisk() bool {
  207. return m.inRamdisk
  208. }
  209. func (m moduleInstallPathContextImpl) InstallInRecovery() bool {
  210. return m.inRecovery
  211. }
  212. func (m moduleInstallPathContextImpl) InstallInRoot() bool {
  213. return m.inRoot
  214. }
  215. func (m moduleInstallPathContextImpl) InstallBypassMake() bool {
  216. return false
  217. }
  218. func (m moduleInstallPathContextImpl) InstallForceOS() (*OsType, *ArchType) {
  219. return m.forceOS, m.forceArch
  220. }
  221. func pathTestConfig(buildDir string) Config {
  222. return TestConfig(buildDir, nil, "", nil)
  223. }
  224. func TestPathForModuleInstall(t *testing.T) {
  225. testConfig := pathTestConfig("")
  226. hostTarget := Target{Os: Linux, Arch: Arch{ArchType: X86}}
  227. deviceTarget := Target{Os: Android, Arch: Arch{ArchType: Arm64}}
  228. testCases := []struct {
  229. name string
  230. ctx *moduleInstallPathContextImpl
  231. in []string
  232. out string
  233. }{
  234. {
  235. name: "host binary",
  236. ctx: &moduleInstallPathContextImpl{
  237. baseModuleContext: baseModuleContext{
  238. os: hostTarget.Os,
  239. target: hostTarget,
  240. },
  241. },
  242. in: []string{"bin", "my_test"},
  243. out: "host/linux-x86/bin/my_test",
  244. },
  245. {
  246. name: "system binary",
  247. ctx: &moduleInstallPathContextImpl{
  248. baseModuleContext: baseModuleContext{
  249. os: deviceTarget.Os,
  250. target: deviceTarget,
  251. },
  252. },
  253. in: []string{"bin", "my_test"},
  254. out: "target/product/test_device/system/bin/my_test",
  255. },
  256. {
  257. name: "vendor binary",
  258. ctx: &moduleInstallPathContextImpl{
  259. baseModuleContext: baseModuleContext{
  260. os: deviceTarget.Os,
  261. target: deviceTarget,
  262. earlyModuleContext: earlyModuleContext{
  263. kind: socSpecificModule,
  264. },
  265. },
  266. },
  267. in: []string{"bin", "my_test"},
  268. out: "target/product/test_device/vendor/bin/my_test",
  269. },
  270. {
  271. name: "odm binary",
  272. ctx: &moduleInstallPathContextImpl{
  273. baseModuleContext: baseModuleContext{
  274. os: deviceTarget.Os,
  275. target: deviceTarget,
  276. earlyModuleContext: earlyModuleContext{
  277. kind: deviceSpecificModule,
  278. },
  279. },
  280. },
  281. in: []string{"bin", "my_test"},
  282. out: "target/product/test_device/odm/bin/my_test",
  283. },
  284. {
  285. name: "product binary",
  286. ctx: &moduleInstallPathContextImpl{
  287. baseModuleContext: baseModuleContext{
  288. os: deviceTarget.Os,
  289. target: deviceTarget,
  290. earlyModuleContext: earlyModuleContext{
  291. kind: productSpecificModule,
  292. },
  293. },
  294. },
  295. in: []string{"bin", "my_test"},
  296. out: "target/product/test_device/product/bin/my_test",
  297. },
  298. {
  299. name: "system_ext binary",
  300. ctx: &moduleInstallPathContextImpl{
  301. baseModuleContext: baseModuleContext{
  302. os: deviceTarget.Os,
  303. target: deviceTarget,
  304. earlyModuleContext: earlyModuleContext{
  305. kind: systemExtSpecificModule,
  306. },
  307. },
  308. },
  309. in: []string{"bin", "my_test"},
  310. out: "target/product/test_device/system_ext/bin/my_test",
  311. },
  312. {
  313. name: "root binary",
  314. ctx: &moduleInstallPathContextImpl{
  315. baseModuleContext: baseModuleContext{
  316. os: deviceTarget.Os,
  317. target: deviceTarget,
  318. },
  319. inRoot: true,
  320. },
  321. in: []string{"my_test"},
  322. out: "target/product/test_device/root/my_test",
  323. },
  324. {
  325. name: "recovery binary",
  326. ctx: &moduleInstallPathContextImpl{
  327. baseModuleContext: baseModuleContext{
  328. os: deviceTarget.Os,
  329. target: deviceTarget,
  330. },
  331. inRecovery: true,
  332. },
  333. in: []string{"bin/my_test"},
  334. out: "target/product/test_device/recovery/root/system/bin/my_test",
  335. },
  336. {
  337. name: "recovery root binary",
  338. ctx: &moduleInstallPathContextImpl{
  339. baseModuleContext: baseModuleContext{
  340. os: deviceTarget.Os,
  341. target: deviceTarget,
  342. },
  343. inRecovery: true,
  344. inRoot: true,
  345. },
  346. in: []string{"my_test"},
  347. out: "target/product/test_device/recovery/root/my_test",
  348. },
  349. {
  350. name: "system native test binary",
  351. ctx: &moduleInstallPathContextImpl{
  352. baseModuleContext: baseModuleContext{
  353. os: deviceTarget.Os,
  354. target: deviceTarget,
  355. },
  356. inData: true,
  357. },
  358. in: []string{"nativetest", "my_test"},
  359. out: "target/product/test_device/data/nativetest/my_test",
  360. },
  361. {
  362. name: "vendor native test binary",
  363. ctx: &moduleInstallPathContextImpl{
  364. baseModuleContext: baseModuleContext{
  365. os: deviceTarget.Os,
  366. target: deviceTarget,
  367. earlyModuleContext: earlyModuleContext{
  368. kind: socSpecificModule,
  369. },
  370. },
  371. inData: true,
  372. },
  373. in: []string{"nativetest", "my_test"},
  374. out: "target/product/test_device/data/nativetest/my_test",
  375. },
  376. {
  377. name: "odm native test binary",
  378. ctx: &moduleInstallPathContextImpl{
  379. baseModuleContext: baseModuleContext{
  380. os: deviceTarget.Os,
  381. target: deviceTarget,
  382. earlyModuleContext: earlyModuleContext{
  383. kind: deviceSpecificModule,
  384. },
  385. },
  386. inData: true,
  387. },
  388. in: []string{"nativetest", "my_test"},
  389. out: "target/product/test_device/data/nativetest/my_test",
  390. },
  391. {
  392. name: "product native test binary",
  393. ctx: &moduleInstallPathContextImpl{
  394. baseModuleContext: baseModuleContext{
  395. os: deviceTarget.Os,
  396. target: deviceTarget,
  397. earlyModuleContext: earlyModuleContext{
  398. kind: productSpecificModule,
  399. },
  400. },
  401. inData: true,
  402. },
  403. in: []string{"nativetest", "my_test"},
  404. out: "target/product/test_device/data/nativetest/my_test",
  405. },
  406. {
  407. name: "system_ext native test binary",
  408. ctx: &moduleInstallPathContextImpl{
  409. baseModuleContext: baseModuleContext{
  410. os: deviceTarget.Os,
  411. target: deviceTarget,
  412. earlyModuleContext: earlyModuleContext{
  413. kind: systemExtSpecificModule,
  414. },
  415. },
  416. inData: true,
  417. },
  418. in: []string{"nativetest", "my_test"},
  419. out: "target/product/test_device/data/nativetest/my_test",
  420. },
  421. {
  422. name: "sanitized system binary",
  423. ctx: &moduleInstallPathContextImpl{
  424. baseModuleContext: baseModuleContext{
  425. os: deviceTarget.Os,
  426. target: deviceTarget,
  427. },
  428. inSanitizerDir: true,
  429. },
  430. in: []string{"bin", "my_test"},
  431. out: "target/product/test_device/data/asan/system/bin/my_test",
  432. },
  433. {
  434. name: "sanitized vendor binary",
  435. ctx: &moduleInstallPathContextImpl{
  436. baseModuleContext: baseModuleContext{
  437. os: deviceTarget.Os,
  438. target: deviceTarget,
  439. earlyModuleContext: earlyModuleContext{
  440. kind: socSpecificModule,
  441. },
  442. },
  443. inSanitizerDir: true,
  444. },
  445. in: []string{"bin", "my_test"},
  446. out: "target/product/test_device/data/asan/vendor/bin/my_test",
  447. },
  448. {
  449. name: "sanitized odm binary",
  450. ctx: &moduleInstallPathContextImpl{
  451. baseModuleContext: baseModuleContext{
  452. os: deviceTarget.Os,
  453. target: deviceTarget,
  454. earlyModuleContext: earlyModuleContext{
  455. kind: deviceSpecificModule,
  456. },
  457. },
  458. inSanitizerDir: true,
  459. },
  460. in: []string{"bin", "my_test"},
  461. out: "target/product/test_device/data/asan/odm/bin/my_test",
  462. },
  463. {
  464. name: "sanitized product binary",
  465. ctx: &moduleInstallPathContextImpl{
  466. baseModuleContext: baseModuleContext{
  467. os: deviceTarget.Os,
  468. target: deviceTarget,
  469. earlyModuleContext: earlyModuleContext{
  470. kind: productSpecificModule,
  471. },
  472. },
  473. inSanitizerDir: true,
  474. },
  475. in: []string{"bin", "my_test"},
  476. out: "target/product/test_device/data/asan/product/bin/my_test",
  477. },
  478. {
  479. name: "sanitized system_ext binary",
  480. ctx: &moduleInstallPathContextImpl{
  481. baseModuleContext: baseModuleContext{
  482. os: deviceTarget.Os,
  483. target: deviceTarget,
  484. earlyModuleContext: earlyModuleContext{
  485. kind: systemExtSpecificModule,
  486. },
  487. },
  488. inSanitizerDir: true,
  489. },
  490. in: []string{"bin", "my_test"},
  491. out: "target/product/test_device/data/asan/system_ext/bin/my_test",
  492. },
  493. {
  494. name: "sanitized system native test binary",
  495. ctx: &moduleInstallPathContextImpl{
  496. baseModuleContext: baseModuleContext{
  497. os: deviceTarget.Os,
  498. target: deviceTarget,
  499. },
  500. inData: true,
  501. inSanitizerDir: true,
  502. },
  503. in: []string{"nativetest", "my_test"},
  504. out: "target/product/test_device/data/asan/data/nativetest/my_test",
  505. },
  506. {
  507. name: "sanitized vendor native test binary",
  508. ctx: &moduleInstallPathContextImpl{
  509. baseModuleContext: baseModuleContext{
  510. os: deviceTarget.Os,
  511. target: deviceTarget,
  512. earlyModuleContext: earlyModuleContext{
  513. kind: socSpecificModule,
  514. },
  515. },
  516. inData: true,
  517. inSanitizerDir: true,
  518. },
  519. in: []string{"nativetest", "my_test"},
  520. out: "target/product/test_device/data/asan/data/nativetest/my_test",
  521. },
  522. {
  523. name: "sanitized odm native test binary",
  524. ctx: &moduleInstallPathContextImpl{
  525. baseModuleContext: baseModuleContext{
  526. os: deviceTarget.Os,
  527. target: deviceTarget,
  528. earlyModuleContext: earlyModuleContext{
  529. kind: deviceSpecificModule,
  530. },
  531. },
  532. inData: true,
  533. inSanitizerDir: true,
  534. },
  535. in: []string{"nativetest", "my_test"},
  536. out: "target/product/test_device/data/asan/data/nativetest/my_test",
  537. },
  538. {
  539. name: "sanitized product native test binary",
  540. ctx: &moduleInstallPathContextImpl{
  541. baseModuleContext: baseModuleContext{
  542. os: deviceTarget.Os,
  543. target: deviceTarget,
  544. earlyModuleContext: earlyModuleContext{
  545. kind: productSpecificModule,
  546. },
  547. },
  548. inData: true,
  549. inSanitizerDir: true,
  550. },
  551. in: []string{"nativetest", "my_test"},
  552. out: "target/product/test_device/data/asan/data/nativetest/my_test",
  553. },
  554. {
  555. name: "sanitized system_ext native test binary",
  556. ctx: &moduleInstallPathContextImpl{
  557. baseModuleContext: baseModuleContext{
  558. os: deviceTarget.Os,
  559. target: deviceTarget,
  560. earlyModuleContext: earlyModuleContext{
  561. kind: systemExtSpecificModule,
  562. },
  563. },
  564. inData: true,
  565. inSanitizerDir: true,
  566. },
  567. in: []string{"nativetest", "my_test"},
  568. out: "target/product/test_device/data/asan/data/nativetest/my_test",
  569. }, {
  570. name: "device testcases",
  571. ctx: &moduleInstallPathContextImpl{
  572. baseModuleContext: baseModuleContext{
  573. os: deviceTarget.Os,
  574. target: deviceTarget,
  575. },
  576. inTestcases: true,
  577. },
  578. in: []string{"my_test", "my_test_bin"},
  579. out: "target/product/test_device/testcases/my_test/my_test_bin",
  580. }, {
  581. name: "host testcases",
  582. ctx: &moduleInstallPathContextImpl{
  583. baseModuleContext: baseModuleContext{
  584. os: hostTarget.Os,
  585. target: hostTarget,
  586. },
  587. inTestcases: true,
  588. },
  589. in: []string{"my_test", "my_test_bin"},
  590. out: "host/linux-x86/testcases/my_test/my_test_bin",
  591. }, {
  592. name: "forced host testcases",
  593. ctx: &moduleInstallPathContextImpl{
  594. baseModuleContext: baseModuleContext{
  595. os: deviceTarget.Os,
  596. target: deviceTarget,
  597. },
  598. inTestcases: true,
  599. forceOS: &Linux,
  600. forceArch: &X86,
  601. },
  602. in: []string{"my_test", "my_test_bin"},
  603. out: "host/linux-x86/testcases/my_test/my_test_bin",
  604. },
  605. }
  606. for _, tc := range testCases {
  607. t.Run(tc.name, func(t *testing.T) {
  608. tc.ctx.baseModuleContext.config = testConfig
  609. output := PathForModuleInstall(tc.ctx, tc.in...)
  610. if output.basePath.path != tc.out {
  611. t.Errorf("unexpected path:\n got: %q\nwant: %q\n",
  612. output.basePath.path,
  613. tc.out)
  614. }
  615. })
  616. }
  617. }
  618. func TestDirectorySortedPaths(t *testing.T) {
  619. config := TestConfig("out", nil, "", map[string][]byte{
  620. "Android.bp": nil,
  621. "a.txt": nil,
  622. "a/txt": nil,
  623. "a/b/c": nil,
  624. "a/b/d": nil,
  625. "b": nil,
  626. "b/b.txt": nil,
  627. "a/a.txt": nil,
  628. })
  629. ctx := PathContextForTesting(config)
  630. makePaths := func() Paths {
  631. return Paths{
  632. PathForSource(ctx, "a.txt"),
  633. PathForSource(ctx, "a/txt"),
  634. PathForSource(ctx, "a/b/c"),
  635. PathForSource(ctx, "a/b/d"),
  636. PathForSource(ctx, "b"),
  637. PathForSource(ctx, "b/b.txt"),
  638. PathForSource(ctx, "a/a.txt"),
  639. }
  640. }
  641. expected := []string{
  642. "a.txt",
  643. "a/a.txt",
  644. "a/b/c",
  645. "a/b/d",
  646. "a/txt",
  647. "b",
  648. "b/b.txt",
  649. }
  650. paths := makePaths()
  651. reversePaths := ReversePaths(paths)
  652. sortedPaths := PathsToDirectorySortedPaths(paths)
  653. reverseSortedPaths := PathsToDirectorySortedPaths(reversePaths)
  654. if !reflect.DeepEqual(Paths(sortedPaths).Strings(), expected) {
  655. t.Fatalf("sorted paths:\n %#v\n != \n %#v", paths.Strings(), expected)
  656. }
  657. if !reflect.DeepEqual(Paths(reverseSortedPaths).Strings(), expected) {
  658. t.Fatalf("sorted reversed paths:\n %#v\n !=\n %#v", reversePaths.Strings(), expected)
  659. }
  660. expectedA := []string{
  661. "a/a.txt",
  662. "a/b/c",
  663. "a/b/d",
  664. "a/txt",
  665. }
  666. inA := sortedPaths.PathsInDirectory("a")
  667. if !reflect.DeepEqual(inA.Strings(), expectedA) {
  668. t.Errorf("FilesInDirectory(a):\n %#v\n != \n %#v", inA.Strings(), expectedA)
  669. }
  670. expectedA_B := []string{
  671. "a/b/c",
  672. "a/b/d",
  673. }
  674. inA_B := sortedPaths.PathsInDirectory("a/b")
  675. if !reflect.DeepEqual(inA_B.Strings(), expectedA_B) {
  676. t.Errorf("FilesInDirectory(a/b):\n %#v\n != \n %#v", inA_B.Strings(), expectedA_B)
  677. }
  678. expectedB := []string{
  679. "b/b.txt",
  680. }
  681. inB := sortedPaths.PathsInDirectory("b")
  682. if !reflect.DeepEqual(inB.Strings(), expectedB) {
  683. t.Errorf("FilesInDirectory(b):\n %#v\n != \n %#v", inA.Strings(), expectedA)
  684. }
  685. }
  686. func TestMaybeRel(t *testing.T) {
  687. testCases := []struct {
  688. name string
  689. base string
  690. target string
  691. out string
  692. isRel bool
  693. }{
  694. {
  695. name: "normal",
  696. base: "a/b/c",
  697. target: "a/b/c/d",
  698. out: "d",
  699. isRel: true,
  700. },
  701. {
  702. name: "parent",
  703. base: "a/b/c/d",
  704. target: "a/b/c",
  705. isRel: false,
  706. },
  707. {
  708. name: "not relative",
  709. base: "a/b",
  710. target: "c/d",
  711. isRel: false,
  712. },
  713. {
  714. name: "abs1",
  715. base: "/a",
  716. target: "a",
  717. isRel: false,
  718. },
  719. {
  720. name: "abs2",
  721. base: "a",
  722. target: "/a",
  723. isRel: false,
  724. },
  725. }
  726. for _, testCase := range testCases {
  727. t.Run(testCase.name, func(t *testing.T) {
  728. ctx := &configErrorWrapper{}
  729. out, isRel := MaybeRel(ctx, testCase.base, testCase.target)
  730. if len(ctx.errors) > 0 {
  731. t.Errorf("MaybeRel(..., %s, %s) reported unexpected errors %v",
  732. testCase.base, testCase.target, ctx.errors)
  733. }
  734. if isRel != testCase.isRel || out != testCase.out {
  735. t.Errorf("MaybeRel(..., %s, %s) want %v, %v got %v, %v",
  736. testCase.base, testCase.target, testCase.out, testCase.isRel, out, isRel)
  737. }
  738. })
  739. }
  740. }
  741. func TestPathForSource(t *testing.T) {
  742. testCases := []struct {
  743. name string
  744. buildDir string
  745. src string
  746. err string
  747. }{
  748. {
  749. name: "normal",
  750. buildDir: "out",
  751. src: "a/b/c",
  752. },
  753. {
  754. name: "abs",
  755. buildDir: "out",
  756. src: "/a/b/c",
  757. err: "is outside directory",
  758. },
  759. {
  760. name: "in out dir",
  761. buildDir: "out",
  762. src: "out/a/b/c",
  763. err: "is in output",
  764. },
  765. }
  766. funcs := []struct {
  767. name string
  768. f func(ctx PathContext, pathComponents ...string) (SourcePath, error)
  769. }{
  770. {"pathForSource", pathForSource},
  771. {"safePathForSource", safePathForSource},
  772. }
  773. for _, f := range funcs {
  774. t.Run(f.name, func(t *testing.T) {
  775. for _, test := range testCases {
  776. t.Run(test.name, func(t *testing.T) {
  777. testConfig := pathTestConfig(test.buildDir)
  778. ctx := &configErrorWrapper{config: testConfig}
  779. _, err := f.f(ctx, test.src)
  780. if len(ctx.errors) > 0 {
  781. t.Fatalf("unexpected errors %v", ctx.errors)
  782. }
  783. if err != nil {
  784. if test.err == "" {
  785. t.Fatalf("unexpected error %q", err.Error())
  786. } else if !strings.Contains(err.Error(), test.err) {
  787. t.Fatalf("incorrect error, want substring %q got %q", test.err, err.Error())
  788. }
  789. } else {
  790. if test.err != "" {
  791. t.Fatalf("missing error %q", test.err)
  792. }
  793. }
  794. })
  795. }
  796. })
  797. }
  798. }
  799. type pathForModuleSrcTestModule struct {
  800. ModuleBase
  801. props struct {
  802. Srcs []string `android:"path"`
  803. Exclude_srcs []string `android:"path"`
  804. Src *string `android:"path"`
  805. Module_handles_missing_deps bool
  806. }
  807. src string
  808. rel string
  809. srcs []string
  810. rels []string
  811. missingDeps []string
  812. }
  813. func pathForModuleSrcTestModuleFactory() Module {
  814. module := &pathForModuleSrcTestModule{}
  815. module.AddProperties(&module.props)
  816. InitAndroidModule(module)
  817. return module
  818. }
  819. func (p *pathForModuleSrcTestModule) GenerateAndroidBuildActions(ctx ModuleContext) {
  820. var srcs Paths
  821. if p.props.Module_handles_missing_deps {
  822. srcs, p.missingDeps = PathsAndMissingDepsForModuleSrcExcludes(ctx, p.props.Srcs, p.props.Exclude_srcs)
  823. } else {
  824. srcs = PathsForModuleSrcExcludes(ctx, p.props.Srcs, p.props.Exclude_srcs)
  825. }
  826. p.srcs = srcs.Strings()
  827. for _, src := range srcs {
  828. p.rels = append(p.rels, src.Rel())
  829. }
  830. if p.props.Src != nil {
  831. src := PathForModuleSrc(ctx, *p.props.Src)
  832. if src != nil {
  833. p.src = src.String()
  834. p.rel = src.Rel()
  835. }
  836. }
  837. if !p.props.Module_handles_missing_deps {
  838. p.missingDeps = ctx.GetMissingDependencies()
  839. }
  840. ctx.Build(pctx, BuildParams{
  841. Rule: Touch,
  842. Output: PathForModuleOut(ctx, "output"),
  843. })
  844. }
  845. type pathForModuleSrcOutputFileProviderModule struct {
  846. ModuleBase
  847. props struct {
  848. Outs []string
  849. Tagged []string
  850. }
  851. outs Paths
  852. tagged Paths
  853. }
  854. func pathForModuleSrcOutputFileProviderModuleFactory() Module {
  855. module := &pathForModuleSrcOutputFileProviderModule{}
  856. module.AddProperties(&module.props)
  857. InitAndroidModule(module)
  858. return module
  859. }
  860. func (p *pathForModuleSrcOutputFileProviderModule) GenerateAndroidBuildActions(ctx ModuleContext) {
  861. for _, out := range p.props.Outs {
  862. p.outs = append(p.outs, PathForModuleOut(ctx, out))
  863. }
  864. for _, tagged := range p.props.Tagged {
  865. p.tagged = append(p.tagged, PathForModuleOut(ctx, tagged))
  866. }
  867. }
  868. func (p *pathForModuleSrcOutputFileProviderModule) OutputFiles(tag string) (Paths, error) {
  869. switch tag {
  870. case "":
  871. return p.outs, nil
  872. case ".tagged":
  873. return p.tagged, nil
  874. default:
  875. return nil, fmt.Errorf("unsupported tag %q", tag)
  876. }
  877. }
  878. type pathForModuleSrcTestCase struct {
  879. name string
  880. bp string
  881. srcs []string
  882. rels []string
  883. src string
  884. rel string
  885. }
  886. func testPathForModuleSrc(t *testing.T, buildDir string, tests []pathForModuleSrcTestCase) {
  887. for _, test := range tests {
  888. t.Run(test.name, func(t *testing.T) {
  889. ctx := NewTestContext()
  890. ctx.RegisterModuleType("test", pathForModuleSrcTestModuleFactory)
  891. ctx.RegisterModuleType("output_file_provider", pathForModuleSrcOutputFileProviderModuleFactory)
  892. ctx.RegisterModuleType("filegroup", FileGroupFactory)
  893. fgBp := `
  894. filegroup {
  895. name: "a",
  896. srcs: ["src/a"],
  897. }
  898. `
  899. ofpBp := `
  900. output_file_provider {
  901. name: "b",
  902. outs: ["gen/b"],
  903. tagged: ["gen/c"],
  904. }
  905. `
  906. mockFS := map[string][]byte{
  907. "fg/Android.bp": []byte(fgBp),
  908. "foo/Android.bp": []byte(test.bp),
  909. "ofp/Android.bp": []byte(ofpBp),
  910. "fg/src/a": nil,
  911. "foo/src/b": nil,
  912. "foo/src/c": nil,
  913. "foo/src/d": nil,
  914. "foo/src/e/e": nil,
  915. "foo/src_special/$": nil,
  916. }
  917. config := TestConfig(buildDir, nil, "", mockFS)
  918. ctx.Register(config)
  919. _, errs := ctx.ParseFileList(".", []string{"fg/Android.bp", "foo/Android.bp", "ofp/Android.bp"})
  920. FailIfErrored(t, errs)
  921. _, errs = ctx.PrepareBuildActions(config)
  922. FailIfErrored(t, errs)
  923. m := ctx.ModuleForTests("foo", "").Module().(*pathForModuleSrcTestModule)
  924. if g, w := m.srcs, test.srcs; !reflect.DeepEqual(g, w) {
  925. t.Errorf("want srcs %q, got %q", w, g)
  926. }
  927. if g, w := m.rels, test.rels; !reflect.DeepEqual(g, w) {
  928. t.Errorf("want rels %q, got %q", w, g)
  929. }
  930. if g, w := m.src, test.src; g != w {
  931. t.Errorf("want src %q, got %q", w, g)
  932. }
  933. if g, w := m.rel, test.rel; g != w {
  934. t.Errorf("want rel %q, got %q", w, g)
  935. }
  936. })
  937. }
  938. }
  939. func TestPathsForModuleSrc(t *testing.T) {
  940. tests := []pathForModuleSrcTestCase{
  941. {
  942. name: "path",
  943. bp: `
  944. test {
  945. name: "foo",
  946. srcs: ["src/b"],
  947. }`,
  948. srcs: []string{"foo/src/b"},
  949. rels: []string{"src/b"},
  950. },
  951. {
  952. name: "glob",
  953. bp: `
  954. test {
  955. name: "foo",
  956. srcs: [
  957. "src/*",
  958. "src/e/*",
  959. ],
  960. }`,
  961. srcs: []string{"foo/src/b", "foo/src/c", "foo/src/d", "foo/src/e/e"},
  962. rels: []string{"src/b", "src/c", "src/d", "src/e/e"},
  963. },
  964. {
  965. name: "recursive glob",
  966. bp: `
  967. test {
  968. name: "foo",
  969. srcs: ["src/**/*"],
  970. }`,
  971. srcs: []string{"foo/src/b", "foo/src/c", "foo/src/d", "foo/src/e/e"},
  972. rels: []string{"src/b", "src/c", "src/d", "src/e/e"},
  973. },
  974. {
  975. name: "filegroup",
  976. bp: `
  977. test {
  978. name: "foo",
  979. srcs: [":a"],
  980. }`,
  981. srcs: []string{"fg/src/a"},
  982. rels: []string{"src/a"},
  983. },
  984. {
  985. name: "output file provider",
  986. bp: `
  987. test {
  988. name: "foo",
  989. srcs: [":b"],
  990. }`,
  991. srcs: []string{buildDir + "/.intermediates/ofp/b/gen/b"},
  992. rels: []string{"gen/b"},
  993. },
  994. {
  995. name: "output file provider tagged",
  996. bp: `
  997. test {
  998. name: "foo",
  999. srcs: [":b{.tagged}"],
  1000. }`,
  1001. srcs: []string{buildDir + "/.intermediates/ofp/b/gen/c"},
  1002. rels: []string{"gen/c"},
  1003. },
  1004. {
  1005. name: "output file provider with exclude",
  1006. bp: `
  1007. test {
  1008. name: "foo",
  1009. srcs: [":b", ":c"],
  1010. exclude_srcs: [":c"]
  1011. }
  1012. output_file_provider {
  1013. name: "c",
  1014. outs: ["gen/c"],
  1015. }`,
  1016. srcs: []string{buildDir + "/.intermediates/ofp/b/gen/b"},
  1017. rels: []string{"gen/b"},
  1018. },
  1019. {
  1020. name: "special characters glob",
  1021. bp: `
  1022. test {
  1023. name: "foo",
  1024. srcs: ["src_special/*"],
  1025. }`,
  1026. srcs: []string{"foo/src_special/$"},
  1027. rels: []string{"src_special/$"},
  1028. },
  1029. }
  1030. testPathForModuleSrc(t, buildDir, tests)
  1031. }
  1032. func TestPathForModuleSrc(t *testing.T) {
  1033. tests := []pathForModuleSrcTestCase{
  1034. {
  1035. name: "path",
  1036. bp: `
  1037. test {
  1038. name: "foo",
  1039. src: "src/b",
  1040. }`,
  1041. src: "foo/src/b",
  1042. rel: "src/b",
  1043. },
  1044. {
  1045. name: "glob",
  1046. bp: `
  1047. test {
  1048. name: "foo",
  1049. src: "src/e/*",
  1050. }`,
  1051. src: "foo/src/e/e",
  1052. rel: "src/e/e",
  1053. },
  1054. {
  1055. name: "filegroup",
  1056. bp: `
  1057. test {
  1058. name: "foo",
  1059. src: ":a",
  1060. }`,
  1061. src: "fg/src/a",
  1062. rel: "src/a",
  1063. },
  1064. {
  1065. name: "output file provider",
  1066. bp: `
  1067. test {
  1068. name: "foo",
  1069. src: ":b",
  1070. }`,
  1071. src: buildDir + "/.intermediates/ofp/b/gen/b",
  1072. rel: "gen/b",
  1073. },
  1074. {
  1075. name: "output file provider tagged",
  1076. bp: `
  1077. test {
  1078. name: "foo",
  1079. src: ":b{.tagged}",
  1080. }`,
  1081. src: buildDir + "/.intermediates/ofp/b/gen/c",
  1082. rel: "gen/c",
  1083. },
  1084. {
  1085. name: "special characters glob",
  1086. bp: `
  1087. test {
  1088. name: "foo",
  1089. src: "src_special/*",
  1090. }`,
  1091. src: "foo/src_special/$",
  1092. rel: "src_special/$",
  1093. },
  1094. }
  1095. testPathForModuleSrc(t, buildDir, tests)
  1096. }
  1097. func TestPathsForModuleSrc_AllowMissingDependencies(t *testing.T) {
  1098. bp := `
  1099. test {
  1100. name: "foo",
  1101. srcs: [":a"],
  1102. exclude_srcs: [":b"],
  1103. src: ":c",
  1104. }
  1105. test {
  1106. name: "bar",
  1107. srcs: [":d"],
  1108. exclude_srcs: [":e"],
  1109. module_handles_missing_deps: true,
  1110. }
  1111. `
  1112. config := TestConfig(buildDir, nil, bp, nil)
  1113. config.TestProductVariables.Allow_missing_dependencies = proptools.BoolPtr(true)
  1114. ctx := NewTestContext()
  1115. ctx.SetAllowMissingDependencies(true)
  1116. ctx.RegisterModuleType("test", pathForModuleSrcTestModuleFactory)
  1117. ctx.Register(config)
  1118. _, errs := ctx.ParseFileList(".", []string{"Android.bp"})
  1119. FailIfErrored(t, errs)
  1120. _, errs = ctx.PrepareBuildActions(config)
  1121. FailIfErrored(t, errs)
  1122. foo := ctx.ModuleForTests("foo", "").Module().(*pathForModuleSrcTestModule)
  1123. if g, w := foo.missingDeps, []string{"a", "b", "c"}; !reflect.DeepEqual(g, w) {
  1124. t.Errorf("want foo missing deps %q, got %q", w, g)
  1125. }
  1126. if g, w := foo.srcs, []string{}; !reflect.DeepEqual(g, w) {
  1127. t.Errorf("want foo srcs %q, got %q", w, g)
  1128. }
  1129. if g, w := foo.src, ""; g != w {
  1130. t.Errorf("want foo src %q, got %q", w, g)
  1131. }
  1132. bar := ctx.ModuleForTests("bar", "").Module().(*pathForModuleSrcTestModule)
  1133. if g, w := bar.missingDeps, []string{"d", "e"}; !reflect.DeepEqual(g, w) {
  1134. t.Errorf("want bar missing deps %q, got %q", w, g)
  1135. }
  1136. if g, w := bar.srcs, []string{}; !reflect.DeepEqual(g, w) {
  1137. t.Errorf("want bar srcs %q, got %q", w, g)
  1138. }
  1139. }
  1140. func ExampleOutputPath_ReplaceExtension() {
  1141. ctx := &configErrorWrapper{
  1142. config: TestConfig("out", nil, "", nil),
  1143. }
  1144. p := PathForOutput(ctx, "system/framework").Join(ctx, "boot.art")
  1145. p2 := p.ReplaceExtension(ctx, "oat")
  1146. fmt.Println(p, p2)
  1147. fmt.Println(p.Rel(), p2.Rel())
  1148. // Output:
  1149. // out/system/framework/boot.art out/system/framework/boot.oat
  1150. // boot.art boot.oat
  1151. }
  1152. func ExampleOutputPath_FileInSameDir() {
  1153. ctx := &configErrorWrapper{
  1154. config: TestConfig("out", nil, "", nil),
  1155. }
  1156. p := PathForOutput(ctx, "system/framework").Join(ctx, "boot.art")
  1157. p2 := p.InSameDir(ctx, "oat", "arm", "boot.vdex")
  1158. fmt.Println(p, p2)
  1159. fmt.Println(p.Rel(), p2.Rel())
  1160. // Output:
  1161. // out/system/framework/boot.art out/system/framework/oat/arm/boot.vdex
  1162. // boot.art oat/arm/boot.vdex
  1163. }
  1164. func BenchmarkFirstUniquePaths(b *testing.B) {
  1165. implementations := []struct {
  1166. name string
  1167. f func(Paths) Paths
  1168. }{
  1169. {
  1170. name: "list",
  1171. f: firstUniquePathsList,
  1172. },
  1173. {
  1174. name: "map",
  1175. f: firstUniquePathsMap,
  1176. },
  1177. }
  1178. const maxSize = 1024
  1179. uniquePaths := make(Paths, maxSize)
  1180. for i := range uniquePaths {
  1181. uniquePaths[i] = PathForTesting(strconv.Itoa(i))
  1182. }
  1183. samePath := make(Paths, maxSize)
  1184. for i := range samePath {
  1185. samePath[i] = uniquePaths[0]
  1186. }
  1187. f := func(b *testing.B, imp func(Paths) Paths, paths Paths) {
  1188. for i := 0; i < b.N; i++ {
  1189. b.ReportAllocs()
  1190. paths = append(Paths(nil), paths...)
  1191. imp(paths)
  1192. }
  1193. }
  1194. for n := 1; n <= maxSize; n <<= 1 {
  1195. b.Run(strconv.Itoa(n), func(b *testing.B) {
  1196. for _, implementation := range implementations {
  1197. b.Run(implementation.name, func(b *testing.B) {
  1198. b.Run("same", func(b *testing.B) {
  1199. f(b, implementation.f, samePath[:n])
  1200. })
  1201. b.Run("unique", func(b *testing.B) {
  1202. f(b, implementation.f, uniquePaths[:n])
  1203. })
  1204. })
  1205. }
  1206. })
  1207. }
  1208. }