blob: 53c8ce2bd62940169161fc348a6756b937a8316a [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
33
34 outputFiles []string
35 err error
36}{
37 {
38 name: "unsupported \\",
39
40 args: []string{"a\\b:b"},
41
42 err: fmt.Errorf("\\ characters are not currently supported"),
43 },
44 {
45 name: "unsupported **",
46
47 args: []string{"a/**:b"},
48
49 err: fmt.Errorf("** is only supported on its own, not with other characters"),
50 },
51 { // This is modelled after the update package build rules in build/make/core/Makefile
52 name: "filter globs",
53
54 inputFiles: []string{
55 "RADIO/a",
56 "IMAGES/system.img",
57 "IMAGES/b.txt",
58 "IMAGES/recovery.img",
59 "IMAGES/vendor.img",
60 "OTA/android-info.txt",
61 "OTA/b",
62 },
63 args: []string{"OTA/android-info.txt:android-info.txt", "IMAGES/*.img:."},
64
65 outputFiles: []string{
66 "android-info.txt",
67 "system.img",
68 "recovery.img",
69 "vendor.img",
70 },
71 },
72 {
73 name: "sorted filter globs",
74
75 inputFiles: []string{
76 "RADIO/a",
77 "IMAGES/system.img",
78 "IMAGES/b.txt",
79 "IMAGES/recovery.img",
80 "IMAGES/vendor.img",
81 "OTA/android-info.txt",
82 "OTA/b",
83 },
84 sortGlobs: true,
85 args: []string{"IMAGES/*.img:.", "OTA/android-info.txt:android-info.txt"},
86
87 outputFiles: []string{
88 "recovery.img",
89 "system.img",
90 "vendor.img",
91 "android-info.txt",
92 },
93 },
94 {
95 name: "sort all",
96
97 inputFiles: []string{
98 "RADIO/a",
99 "IMAGES/system.img",
100 "IMAGES/b.txt",
101 "IMAGES/recovery.img",
102 "IMAGES/vendor.img",
103 "OTA/b",
104 "OTA/android-info.txt",
105 },
106 sortGlobs: true,
107 args: []string{"**"},
108
109 outputFiles: []string{
110 "IMAGES/b.txt",
111 "IMAGES/recovery.img",
112 "IMAGES/system.img",
113 "IMAGES/vendor.img",
114 "OTA/android-info.txt",
115 "OTA/b",
116 "RADIO/a",
117 },
118 },
119 {
Colin Cross06382992017-06-23 14:08:42 -0700120 name: "sort all implicit",
121
122 inputFiles: []string{
123 "RADIO/a",
124 "IMAGES/system.img",
125 "IMAGES/b.txt",
126 "IMAGES/recovery.img",
127 "IMAGES/vendor.img",
128 "OTA/b",
129 "OTA/android-info.txt",
130 },
131 sortGlobs: true,
132 args: nil,
133
134 outputFiles: []string{
135 "IMAGES/b.txt",
136 "IMAGES/recovery.img",
137 "IMAGES/system.img",
138 "IMAGES/vendor.img",
139 "OTA/android-info.txt",
140 "OTA/b",
141 "RADIO/a",
142 },
143 },
144 {
Colin Cross8936b022017-06-23 13:00:17 -0700145 name: "sort jar",
146
147 inputFiles: []string{
148 "MANIFEST.MF",
149 "META-INF/MANIFEST.MF",
150 "META-INF/aaa/",
151 "META-INF/aaa/aaa",
152 "META-INF/AAA",
153 "META-INF.txt",
154 "META-INF/",
155 "AAA",
156 "aaa",
157 },
158 sortJava: true,
159 args: nil,
160
161 outputFiles: []string{
162 "META-INF/",
163 "META-INF/MANIFEST.MF",
164 "META-INF/AAA",
165 "META-INF/aaa/",
166 "META-INF/aaa/aaa",
167 "AAA",
168 "MANIFEST.MF",
169 "META-INF.txt",
170 "aaa",
171 },
172 },
173 {
Dan Willemsen82218f22017-06-19 16:35:00 -0700174 name: "double input",
175
176 inputFiles: []string{
177 "b",
178 "a",
179 },
180 args: []string{"a:a2", "**"},
181
182 outputFiles: []string{
183 "a2",
184 "b",
185 "a",
186 },
187 },
188}
189
190func errorString(e error) string {
191 if e == nil {
192 return ""
193 }
194 return e.Error()
195}
196
197func TestZip2Zip(t *testing.T) {
198 for _, testCase := range testCases {
199 t.Run(testCase.name, func(t *testing.T) {
200 inputBuf := &bytes.Buffer{}
201 outputBuf := &bytes.Buffer{}
202
203 inputWriter := zip.NewWriter(inputBuf)
204 for _, file := range testCase.inputFiles {
205 w, err := inputWriter.Create(file)
206 if err != nil {
207 t.Fatal(err)
208 }
209 fmt.Fprintln(w, "test")
210 }
211 inputWriter.Close()
212 inputBytes := inputBuf.Bytes()
213 inputReader, err := zip.NewReader(bytes.NewReader(inputBytes), int64(len(inputBytes)))
214 if err != nil {
215 t.Fatal(err)
216 }
217
218 outputWriter := zip.NewWriter(outputBuf)
Colin Cross8936b022017-06-23 13:00:17 -0700219 err = zip2zip(inputReader, outputWriter, testCase.sortGlobs, testCase.sortJava, false, testCase.args)
Dan Willemsen82218f22017-06-19 16:35:00 -0700220 if errorString(testCase.err) != errorString(err) {
221 t.Fatalf("Unexpected error:\n got: %q\nwant: %q", errorString(err), errorString(testCase.err))
222 }
223
224 outputWriter.Close()
225 outputBytes := outputBuf.Bytes()
226 outputReader, err := zip.NewReader(bytes.NewReader(outputBytes), int64(len(outputBytes)))
227 if err != nil {
228 t.Fatal(err)
229 }
230 var outputFiles []string
231 if len(outputReader.File) > 0 {
232 outputFiles = make([]string, len(outputReader.File))
233 for i, file := range outputReader.File {
234 outputFiles[i] = file.Name
235 }
236 }
237
238 if !reflect.DeepEqual(testCase.outputFiles, outputFiles) {
239 t.Fatalf("Output file list does not match:\n got: %v\nwant: %v", outputFiles, testCase.outputFiles)
240 }
241 })
242 }
243}