blob: 212ab28e5114fa50026dbf1f146f57fec1bf2e2c [file] [log] [blame]
Dan Willemsen82218f22017-06-19 16:35:00 -07001// 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
15package main
16
17import (
18 "bytes"
19 "fmt"
20 "reflect"
21 "testing"
22
23 "android/soong/third_party/zip"
24)
25
26var testCases = []struct {
27 name string
28
29 inputFiles []string
30 sortGlobs bool
Colin Cross8936b022017-06-23 13:00:17 -070031 sortJava bool
Dan Willemsen82218f22017-06-19 16:35:00 -070032 args []string
Colin Crossf3831d02017-11-22 12:53:08 -080033 excludes []string
Dan Willemsen82218f22017-06-19 16:35:00 -070034
35 outputFiles []string
36 err error
37}{
38 {
39 name: "unsupported \\",
40
41 args: []string{"a\\b:b"},
42
43 err: fmt.Errorf("\\ characters are not currently supported"),
44 },
Dan Willemsen82218f22017-06-19 16:35:00 -070045 { // This is modelled after the update package build rules in build/make/core/Makefile
46 name: "filter globs",
47
48 inputFiles: []string{
49 "RADIO/a",
50 "IMAGES/system.img",
51 "IMAGES/b.txt",
52 "IMAGES/recovery.img",
53 "IMAGES/vendor.img",
54 "OTA/android-info.txt",
55 "OTA/b",
56 },
57 args: []string{"OTA/android-info.txt:android-info.txt", "IMAGES/*.img:."},
58
59 outputFiles: []string{
60 "android-info.txt",
61 "system.img",
62 "recovery.img",
63 "vendor.img",
64 },
65 },
66 {
67 name: "sorted filter globs",
68
69 inputFiles: []string{
70 "RADIO/a",
71 "IMAGES/system.img",
72 "IMAGES/b.txt",
73 "IMAGES/recovery.img",
74 "IMAGES/vendor.img",
75 "OTA/android-info.txt",
76 "OTA/b",
77 },
78 sortGlobs: true,
79 args: []string{"IMAGES/*.img:.", "OTA/android-info.txt:android-info.txt"},
80
81 outputFiles: []string{
82 "recovery.img",
83 "system.img",
84 "vendor.img",
85 "android-info.txt",
86 },
87 },
88 {
89 name: "sort all",
90
91 inputFiles: []string{
Colin Crossf3831d02017-11-22 12:53:08 -080092 "RADIO/",
Dan Willemsen82218f22017-06-19 16:35:00 -070093 "RADIO/a",
Colin Crossf3831d02017-11-22 12:53:08 -080094 "IMAGES/",
Dan Willemsen82218f22017-06-19 16:35:00 -070095 "IMAGES/system.img",
96 "IMAGES/b.txt",
97 "IMAGES/recovery.img",
98 "IMAGES/vendor.img",
Colin Crossf3831d02017-11-22 12:53:08 -080099 "OTA/",
Dan Willemsen82218f22017-06-19 16:35:00 -0700100 "OTA/b",
101 "OTA/android-info.txt",
102 },
103 sortGlobs: true,
Colin Crossf3831d02017-11-22 12:53:08 -0800104 args: []string{"**/*"},
Dan Willemsen82218f22017-06-19 16:35:00 -0700105
106 outputFiles: []string{
107 "IMAGES/b.txt",
108 "IMAGES/recovery.img",
109 "IMAGES/system.img",
110 "IMAGES/vendor.img",
111 "OTA/android-info.txt",
112 "OTA/b",
113 "RADIO/a",
114 },
115 },
116 {
Colin Cross06382992017-06-23 14:08:42 -0700117 name: "sort all implicit",
118
119 inputFiles: []string{
Colin Crossf3831d02017-11-22 12:53:08 -0800120 "RADIO/",
Colin Cross06382992017-06-23 14:08:42 -0700121 "RADIO/a",
Colin Crossf3831d02017-11-22 12:53:08 -0800122 "IMAGES/",
Colin Cross06382992017-06-23 14:08:42 -0700123 "IMAGES/system.img",
124 "IMAGES/b.txt",
125 "IMAGES/recovery.img",
126 "IMAGES/vendor.img",
Colin Crossf3831d02017-11-22 12:53:08 -0800127 "OTA/",
Colin Cross06382992017-06-23 14:08:42 -0700128 "OTA/b",
129 "OTA/android-info.txt",
130 },
131 sortGlobs: true,
132 args: nil,
133
134 outputFiles: []string{
Colin Crossf3831d02017-11-22 12:53:08 -0800135 "IMAGES/",
Colin Cross06382992017-06-23 14:08:42 -0700136 "IMAGES/b.txt",
137 "IMAGES/recovery.img",
138 "IMAGES/system.img",
139 "IMAGES/vendor.img",
Colin Crossf3831d02017-11-22 12:53:08 -0800140 "OTA/",
Colin Cross06382992017-06-23 14:08:42 -0700141 "OTA/android-info.txt",
142 "OTA/b",
Colin Crossf3831d02017-11-22 12:53:08 -0800143 "RADIO/",
Colin Cross06382992017-06-23 14:08:42 -0700144 "RADIO/a",
145 },
146 },
147 {
Colin Cross8936b022017-06-23 13:00:17 -0700148 name: "sort jar",
149
150 inputFiles: []string{
151 "MANIFEST.MF",
152 "META-INF/MANIFEST.MF",
153 "META-INF/aaa/",
154 "META-INF/aaa/aaa",
155 "META-INF/AAA",
156 "META-INF.txt",
157 "META-INF/",
158 "AAA",
159 "aaa",
160 },
161 sortJava: true,
162 args: nil,
163
164 outputFiles: []string{
165 "META-INF/",
166 "META-INF/MANIFEST.MF",
167 "META-INF/AAA",
168 "META-INF/aaa/",
169 "META-INF/aaa/aaa",
170 "AAA",
171 "MANIFEST.MF",
172 "META-INF.txt",
173 "aaa",
174 },
175 },
176 {
Dan Willemsen82218f22017-06-19 16:35:00 -0700177 name: "double input",
178
179 inputFiles: []string{
180 "b",
181 "a",
182 },
Colin Crossf3831d02017-11-22 12:53:08 -0800183 args: []string{"a:a2", "**/*"},
Dan Willemsen82218f22017-06-19 16:35:00 -0700184
185 outputFiles: []string{
186 "a2",
187 "b",
188 "a",
189 },
190 },
Colin Crossf3831d02017-11-22 12:53:08 -0800191 {
192 name: "multiple matches",
193
194 inputFiles: []string{
195 "a/a",
196 },
197 args: []string{"a/a", "a/*"},
198
199 outputFiles: []string{
200 "a/a",
201 },
202 },
203 {
204 name: "multiple conflicting matches",
205
206 inputFiles: []string{
207 "a/a",
208 "a/b",
209 },
210 args: []string{"a/b:a/a", "a/*"},
211
212 err: fmt.Errorf(`multiple entries for "a/a" with different contents`),
213 },
214 {
215 name: "excludes",
216
217 inputFiles: []string{
218 "a/a",
219 "a/b",
220 },
221 args: nil,
222 excludes: []string{"a/a"},
223
224 outputFiles: []string{
225 "a/b",
226 },
227 },
228 {
229 name: "excludes with include",
230
231 inputFiles: []string{
232 "a/a",
233 "a/b",
234 },
235 args: []string{"a/*"},
236 excludes: []string{"a/a"},
237
238 outputFiles: []string{
239 "a/b",
240 },
241 },
242 {
243 name: "excludes with glob",
244
245 inputFiles: []string{
246 "a/a",
247 "a/b",
248 },
249 args: []string{"a/*"},
250 excludes: []string{"a/*"},
251
252 outputFiles: nil,
253 },
Dan Willemsen82218f22017-06-19 16:35:00 -0700254}
255
256func errorString(e error) string {
257 if e == nil {
258 return ""
259 }
260 return e.Error()
261}
262
263func TestZip2Zip(t *testing.T) {
264 for _, testCase := range testCases {
265 t.Run(testCase.name, func(t *testing.T) {
266 inputBuf := &bytes.Buffer{}
267 outputBuf := &bytes.Buffer{}
268
269 inputWriter := zip.NewWriter(inputBuf)
270 for _, file := range testCase.inputFiles {
271 w, err := inputWriter.Create(file)
272 if err != nil {
273 t.Fatal(err)
274 }
275 fmt.Fprintln(w, "test")
276 }
277 inputWriter.Close()
278 inputBytes := inputBuf.Bytes()
279 inputReader, err := zip.NewReader(bytes.NewReader(inputBytes), int64(len(inputBytes)))
280 if err != nil {
281 t.Fatal(err)
282 }
283
284 outputWriter := zip.NewWriter(outputBuf)
Colin Crossf3831d02017-11-22 12:53:08 -0800285 err = zip2zip(inputReader, outputWriter, testCase.sortGlobs, testCase.sortJava, false, testCase.args, testCase.excludes)
Dan Willemsen82218f22017-06-19 16:35:00 -0700286 if errorString(testCase.err) != errorString(err) {
287 t.Fatalf("Unexpected error:\n got: %q\nwant: %q", errorString(err), errorString(testCase.err))
288 }
289
290 outputWriter.Close()
291 outputBytes := outputBuf.Bytes()
292 outputReader, err := zip.NewReader(bytes.NewReader(outputBytes), int64(len(outputBytes)))
293 if err != nil {
294 t.Fatal(err)
295 }
296 var outputFiles []string
297 if len(outputReader.File) > 0 {
298 outputFiles = make([]string, len(outputReader.File))
299 for i, file := range outputReader.File {
300 outputFiles[i] = file.Name
301 }
302 }
303
304 if !reflect.DeepEqual(testCase.outputFiles, outputFiles) {
305 t.Fatalf("Output file list does not match:\n got: %v\nwant: %v", outputFiles, testCase.outputFiles)
306 }
307 })
308 }
309}