zip2zip_test.go 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514
  1. // Copyright 2017 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 main
  15. import (
  16. "bytes"
  17. "fmt"
  18. "reflect"
  19. "testing"
  20. "android/soong/third_party/zip"
  21. )
  22. var testCases = []struct {
  23. name string
  24. inputFiles []string
  25. sortGlobs bool
  26. sortJava bool
  27. args []string
  28. excludes []string
  29. includes []string
  30. uncompresses []string
  31. outputFiles []string
  32. storedFiles []string
  33. err error
  34. }{
  35. { // This is modelled after the update package build rules in build/make/core/Makefile
  36. name: "filter globs",
  37. inputFiles: []string{
  38. "RADIO/a",
  39. "IMAGES/system.img",
  40. "IMAGES/b.txt",
  41. "IMAGES/recovery.img",
  42. "IMAGES/vendor.img",
  43. "OTA/android-info.txt",
  44. "OTA/b",
  45. },
  46. args: []string{"OTA/android-info.txt:android-info.txt", "IMAGES/*.img:."},
  47. outputFiles: []string{
  48. "android-info.txt",
  49. "system.img",
  50. "recovery.img",
  51. "vendor.img",
  52. },
  53. },
  54. {
  55. name: "sorted filter globs",
  56. inputFiles: []string{
  57. "RADIO/a",
  58. "IMAGES/system.img",
  59. "IMAGES/b.txt",
  60. "IMAGES/recovery.img",
  61. "IMAGES/vendor.img",
  62. "OTA/android-info.txt",
  63. "OTA/b",
  64. },
  65. sortGlobs: true,
  66. args: []string{"IMAGES/*.img:.", "OTA/android-info.txt:android-info.txt"},
  67. outputFiles: []string{
  68. "recovery.img",
  69. "system.img",
  70. "vendor.img",
  71. "android-info.txt",
  72. },
  73. },
  74. {
  75. name: "sort all",
  76. inputFiles: []string{
  77. "RADIO/",
  78. "RADIO/a",
  79. "IMAGES/",
  80. "IMAGES/system.img",
  81. "IMAGES/b.txt",
  82. "IMAGES/recovery.img",
  83. "IMAGES/vendor.img",
  84. "OTA/",
  85. "OTA/b",
  86. "OTA/android-info.txt",
  87. },
  88. sortGlobs: true,
  89. args: []string{"**/*"},
  90. outputFiles: []string{
  91. "IMAGES/b.txt",
  92. "IMAGES/recovery.img",
  93. "IMAGES/system.img",
  94. "IMAGES/vendor.img",
  95. "OTA/android-info.txt",
  96. "OTA/b",
  97. "RADIO/a",
  98. },
  99. },
  100. {
  101. name: "sort all implicit",
  102. inputFiles: []string{
  103. "RADIO/",
  104. "RADIO/a",
  105. "IMAGES/",
  106. "IMAGES/system.img",
  107. "IMAGES/b.txt",
  108. "IMAGES/recovery.img",
  109. "IMAGES/vendor.img",
  110. "OTA/",
  111. "OTA/b",
  112. "OTA/android-info.txt",
  113. },
  114. sortGlobs: true,
  115. args: nil,
  116. outputFiles: []string{
  117. "IMAGES/",
  118. "IMAGES/b.txt",
  119. "IMAGES/recovery.img",
  120. "IMAGES/system.img",
  121. "IMAGES/vendor.img",
  122. "OTA/",
  123. "OTA/android-info.txt",
  124. "OTA/b",
  125. "RADIO/",
  126. "RADIO/a",
  127. },
  128. },
  129. {
  130. name: "sort jar",
  131. inputFiles: []string{
  132. "MANIFEST.MF",
  133. "META-INF/MANIFEST.MF",
  134. "META-INF/aaa/",
  135. "META-INF/aaa/aaa",
  136. "META-INF/AAA",
  137. "META-INF.txt",
  138. "META-INF/",
  139. "AAA",
  140. "aaa",
  141. },
  142. sortJava: true,
  143. args: nil,
  144. outputFiles: []string{
  145. "META-INF/",
  146. "META-INF/MANIFEST.MF",
  147. "META-INF/AAA",
  148. "META-INF/aaa/",
  149. "META-INF/aaa/aaa",
  150. "AAA",
  151. "MANIFEST.MF",
  152. "META-INF.txt",
  153. "aaa",
  154. },
  155. },
  156. {
  157. name: "double input",
  158. inputFiles: []string{
  159. "b",
  160. "a",
  161. },
  162. args: []string{"a:a2", "**/*"},
  163. outputFiles: []string{
  164. "a2",
  165. "b",
  166. "a",
  167. },
  168. },
  169. {
  170. name: "multiple matches",
  171. inputFiles: []string{
  172. "a/a",
  173. },
  174. args: []string{"a/a", "a/*"},
  175. outputFiles: []string{
  176. "a/a",
  177. },
  178. },
  179. {
  180. name: "multiple conflicting matches",
  181. inputFiles: []string{
  182. "a/a",
  183. "a/b",
  184. },
  185. args: []string{"a/b:a/a", "a/*"},
  186. err: fmt.Errorf(`multiple entries for "a/a" with different contents`),
  187. },
  188. {
  189. name: "excludes",
  190. inputFiles: []string{
  191. "a/a",
  192. "a/b",
  193. },
  194. args: nil,
  195. excludes: []string{"a/a"},
  196. outputFiles: []string{
  197. "a/b",
  198. },
  199. },
  200. {
  201. name: "excludes with args",
  202. inputFiles: []string{
  203. "a/a",
  204. "a/b",
  205. },
  206. args: []string{"a/*"},
  207. excludes: []string{"a/a"},
  208. outputFiles: []string{
  209. "a/b",
  210. },
  211. },
  212. {
  213. name: "excludes over args",
  214. inputFiles: []string{
  215. "a/a",
  216. "a/b",
  217. },
  218. args: []string{"a/a"},
  219. excludes: []string{"a/*"},
  220. outputFiles: nil,
  221. },
  222. {
  223. name: "excludes with includes",
  224. inputFiles: []string{
  225. "a/a",
  226. "a/b",
  227. },
  228. args: nil,
  229. excludes: []string{"a/*"},
  230. includes: []string{"a/b"},
  231. outputFiles: []string{"a/b"},
  232. },
  233. {
  234. name: "excludes with glob",
  235. inputFiles: []string{
  236. "a/a",
  237. "a/b",
  238. },
  239. args: []string{"a/*"},
  240. excludes: []string{"a/*"},
  241. outputFiles: nil,
  242. },
  243. {
  244. name: "uncompress one",
  245. inputFiles: []string{
  246. "a/a",
  247. "a/b",
  248. },
  249. uncompresses: []string{"a/a"},
  250. outputFiles: []string{
  251. "a/a",
  252. "a/b",
  253. },
  254. storedFiles: []string{
  255. "a/a",
  256. },
  257. },
  258. {
  259. name: "uncompress two",
  260. inputFiles: []string{
  261. "a/a",
  262. "a/b",
  263. },
  264. uncompresses: []string{"a/a", "a/b"},
  265. outputFiles: []string{
  266. "a/a",
  267. "a/b",
  268. },
  269. storedFiles: []string{
  270. "a/a",
  271. "a/b",
  272. },
  273. },
  274. {
  275. name: "uncompress glob",
  276. inputFiles: []string{
  277. "a/a",
  278. "a/b",
  279. "a/c.so",
  280. "a/d.so",
  281. },
  282. uncompresses: []string{"a/*.so"},
  283. outputFiles: []string{
  284. "a/a",
  285. "a/b",
  286. "a/c.so",
  287. "a/d.so",
  288. },
  289. storedFiles: []string{
  290. "a/c.so",
  291. "a/d.so",
  292. },
  293. },
  294. {
  295. name: "uncompress rename",
  296. inputFiles: []string{
  297. "a/a",
  298. },
  299. args: []string{"a/a:a/b"},
  300. uncompresses: []string{"a/b"},
  301. outputFiles: []string{
  302. "a/b",
  303. },
  304. storedFiles: []string{
  305. "a/b",
  306. },
  307. },
  308. {
  309. name: "recursive glob",
  310. inputFiles: []string{
  311. "a/a/a",
  312. "a/a/b",
  313. },
  314. args: []string{"a/**/*:b"},
  315. outputFiles: []string{
  316. "b/a/a",
  317. "b/a/b",
  318. },
  319. },
  320. {
  321. name: "glob",
  322. inputFiles: []string{
  323. "a/a/a",
  324. "a/a/b",
  325. "a/b",
  326. "a/c",
  327. },
  328. args: []string{"a/*:b"},
  329. outputFiles: []string{
  330. "b/b",
  331. "b/c",
  332. },
  333. },
  334. {
  335. name: "top level glob",
  336. inputFiles: []string{
  337. "a",
  338. "b",
  339. },
  340. args: []string{"*:b"},
  341. outputFiles: []string{
  342. "b/a",
  343. "b/b",
  344. },
  345. },
  346. {
  347. name: "multilple glob",
  348. inputFiles: []string{
  349. "a/a/a",
  350. "a/a/b",
  351. },
  352. args: []string{"a/*/*:b"},
  353. outputFiles: []string{
  354. "b/a/a",
  355. "b/a/b",
  356. },
  357. },
  358. {
  359. name: "escaping",
  360. inputFiles: []string{"a"},
  361. args: []string{"\\a"},
  362. outputFiles: []string{"a"},
  363. },
  364. }
  365. func errorString(e error) string {
  366. if e == nil {
  367. return ""
  368. }
  369. return e.Error()
  370. }
  371. func TestZip2Zip(t *testing.T) {
  372. for _, testCase := range testCases {
  373. t.Run(testCase.name, func(t *testing.T) {
  374. inputBuf := &bytes.Buffer{}
  375. outputBuf := &bytes.Buffer{}
  376. inputWriter := zip.NewWriter(inputBuf)
  377. for _, file := range testCase.inputFiles {
  378. w, err := inputWriter.Create(file)
  379. if err != nil {
  380. t.Fatal(err)
  381. }
  382. fmt.Fprintln(w, "test")
  383. }
  384. inputWriter.Close()
  385. inputBytes := inputBuf.Bytes()
  386. inputReader, err := zip.NewReader(bytes.NewReader(inputBytes), int64(len(inputBytes)))
  387. if err != nil {
  388. t.Fatal(err)
  389. }
  390. outputWriter := zip.NewWriter(outputBuf)
  391. err = zip2zip(inputReader, outputWriter, testCase.sortGlobs, testCase.sortJava, false,
  392. testCase.args, testCase.excludes, testCase.includes, testCase.uncompresses)
  393. if errorString(testCase.err) != errorString(err) {
  394. t.Fatalf("Unexpected error:\n got: %q\nwant: %q", errorString(err), errorString(testCase.err))
  395. }
  396. outputWriter.Close()
  397. outputBytes := outputBuf.Bytes()
  398. outputReader, err := zip.NewReader(bytes.NewReader(outputBytes), int64(len(outputBytes)))
  399. if err != nil {
  400. t.Fatal(err)
  401. }
  402. var outputFiles []string
  403. var storedFiles []string
  404. if len(outputReader.File) > 0 {
  405. outputFiles = make([]string, len(outputReader.File))
  406. for i, file := range outputReader.File {
  407. outputFiles[i] = file.Name
  408. if file.Method == zip.Store {
  409. storedFiles = append(storedFiles, file.Name)
  410. }
  411. }
  412. }
  413. if !reflect.DeepEqual(testCase.outputFiles, outputFiles) {
  414. t.Fatalf("Output file list does not match:\nwant: %v\n got: %v", testCase.outputFiles, outputFiles)
  415. }
  416. if !reflect.DeepEqual(testCase.storedFiles, storedFiles) {
  417. t.Fatalf("Stored file list does not match:\nwant: %v\n got: %v", testCase.storedFiles, storedFiles)
  418. }
  419. })
  420. }
  421. }
  422. func TestConstantPartOfPattern(t *testing.T) {
  423. testCases := []struct{ in, out string }{
  424. {
  425. in: "",
  426. out: "",
  427. },
  428. {
  429. in: "a",
  430. out: "a",
  431. },
  432. {
  433. in: "*",
  434. out: "",
  435. },
  436. {
  437. in: "a/a",
  438. out: "a/a",
  439. },
  440. {
  441. in: "a/*",
  442. out: "a",
  443. },
  444. {
  445. in: "a/*/a",
  446. out: "a",
  447. },
  448. {
  449. in: "a/**/*",
  450. out: "a",
  451. },
  452. }
  453. for _, test := range testCases {
  454. t.Run(test.in, func(t *testing.T) {
  455. got := constantPartOfPattern(test.in)
  456. if got != test.out {
  457. t.Errorf("want %q, got %q", test.out, got)
  458. }
  459. })
  460. }
  461. }