Dan Willemsen | 82218f2 | 2017-06-19 16:35:00 -0700 | [diff] [blame] | 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 | |
| 15 | package main |
| 16 | |
| 17 | import ( |
| 18 | "bytes" |
| 19 | "fmt" |
| 20 | "reflect" |
| 21 | "testing" |
| 22 | |
| 23 | "android/soong/third_party/zip" |
| 24 | ) |
| 25 | |
| 26 | var testCases = []struct { |
| 27 | name string |
| 28 | |
| 29 | inputFiles []string |
| 30 | sortGlobs bool |
Colin Cross | 8936b02 | 2017-06-23 13:00:17 -0700 | [diff] [blame] | 31 | sortJava bool |
Dan Willemsen | 82218f2 | 2017-06-19 16:35:00 -0700 | [diff] [blame] | 32 | args []string |
Colin Cross | f3831d0 | 2017-11-22 12:53:08 -0800 | [diff] [blame^] | 33 | excludes []string |
Dan Willemsen | 82218f2 | 2017-06-19 16:35:00 -0700 | [diff] [blame] | 34 | |
| 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 Willemsen | 82218f2 | 2017-06-19 16:35:00 -0700 | [diff] [blame] | 45 | { // 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 Cross | f3831d0 | 2017-11-22 12:53:08 -0800 | [diff] [blame^] | 92 | "RADIO/", |
Dan Willemsen | 82218f2 | 2017-06-19 16:35:00 -0700 | [diff] [blame] | 93 | "RADIO/a", |
Colin Cross | f3831d0 | 2017-11-22 12:53:08 -0800 | [diff] [blame^] | 94 | "IMAGES/", |
Dan Willemsen | 82218f2 | 2017-06-19 16:35:00 -0700 | [diff] [blame] | 95 | "IMAGES/system.img", |
| 96 | "IMAGES/b.txt", |
| 97 | "IMAGES/recovery.img", |
| 98 | "IMAGES/vendor.img", |
Colin Cross | f3831d0 | 2017-11-22 12:53:08 -0800 | [diff] [blame^] | 99 | "OTA/", |
Dan Willemsen | 82218f2 | 2017-06-19 16:35:00 -0700 | [diff] [blame] | 100 | "OTA/b", |
| 101 | "OTA/android-info.txt", |
| 102 | }, |
| 103 | sortGlobs: true, |
Colin Cross | f3831d0 | 2017-11-22 12:53:08 -0800 | [diff] [blame^] | 104 | args: []string{"**/*"}, |
Dan Willemsen | 82218f2 | 2017-06-19 16:35:00 -0700 | [diff] [blame] | 105 | |
| 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 Cross | 0638299 | 2017-06-23 14:08:42 -0700 | [diff] [blame] | 117 | name: "sort all implicit", |
| 118 | |
| 119 | inputFiles: []string{ |
Colin Cross | f3831d0 | 2017-11-22 12:53:08 -0800 | [diff] [blame^] | 120 | "RADIO/", |
Colin Cross | 0638299 | 2017-06-23 14:08:42 -0700 | [diff] [blame] | 121 | "RADIO/a", |
Colin Cross | f3831d0 | 2017-11-22 12:53:08 -0800 | [diff] [blame^] | 122 | "IMAGES/", |
Colin Cross | 0638299 | 2017-06-23 14:08:42 -0700 | [diff] [blame] | 123 | "IMAGES/system.img", |
| 124 | "IMAGES/b.txt", |
| 125 | "IMAGES/recovery.img", |
| 126 | "IMAGES/vendor.img", |
Colin Cross | f3831d0 | 2017-11-22 12:53:08 -0800 | [diff] [blame^] | 127 | "OTA/", |
Colin Cross | 0638299 | 2017-06-23 14:08:42 -0700 | [diff] [blame] | 128 | "OTA/b", |
| 129 | "OTA/android-info.txt", |
| 130 | }, |
| 131 | sortGlobs: true, |
| 132 | args: nil, |
| 133 | |
| 134 | outputFiles: []string{ |
Colin Cross | f3831d0 | 2017-11-22 12:53:08 -0800 | [diff] [blame^] | 135 | "IMAGES/", |
Colin Cross | 0638299 | 2017-06-23 14:08:42 -0700 | [diff] [blame] | 136 | "IMAGES/b.txt", |
| 137 | "IMAGES/recovery.img", |
| 138 | "IMAGES/system.img", |
| 139 | "IMAGES/vendor.img", |
Colin Cross | f3831d0 | 2017-11-22 12:53:08 -0800 | [diff] [blame^] | 140 | "OTA/", |
Colin Cross | 0638299 | 2017-06-23 14:08:42 -0700 | [diff] [blame] | 141 | "OTA/android-info.txt", |
| 142 | "OTA/b", |
Colin Cross | f3831d0 | 2017-11-22 12:53:08 -0800 | [diff] [blame^] | 143 | "RADIO/", |
Colin Cross | 0638299 | 2017-06-23 14:08:42 -0700 | [diff] [blame] | 144 | "RADIO/a", |
| 145 | }, |
| 146 | }, |
| 147 | { |
Colin Cross | 8936b02 | 2017-06-23 13:00:17 -0700 | [diff] [blame] | 148 | 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 Willemsen | 82218f2 | 2017-06-19 16:35:00 -0700 | [diff] [blame] | 177 | name: "double input", |
| 178 | |
| 179 | inputFiles: []string{ |
| 180 | "b", |
| 181 | "a", |
| 182 | }, |
Colin Cross | f3831d0 | 2017-11-22 12:53:08 -0800 | [diff] [blame^] | 183 | args: []string{"a:a2", "**/*"}, |
Dan Willemsen | 82218f2 | 2017-06-19 16:35:00 -0700 | [diff] [blame] | 184 | |
| 185 | outputFiles: []string{ |
| 186 | "a2", |
| 187 | "b", |
| 188 | "a", |
| 189 | }, |
| 190 | }, |
Colin Cross | f3831d0 | 2017-11-22 12:53:08 -0800 | [diff] [blame^] | 191 | { |
| 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 Willemsen | 82218f2 | 2017-06-19 16:35:00 -0700 | [diff] [blame] | 254 | } |
| 255 | |
| 256 | func errorString(e error) string { |
| 257 | if e == nil { |
| 258 | return "" |
| 259 | } |
| 260 | return e.Error() |
| 261 | } |
| 262 | |
| 263 | func 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 Cross | f3831d0 | 2017-11-22 12:53:08 -0800 | [diff] [blame^] | 285 | err = zip2zip(inputReader, outputWriter, testCase.sortGlobs, testCase.sortJava, false, testCase.args, testCase.excludes) |
Dan Willemsen | 82218f2 | 2017-06-19 16:35:00 -0700 | [diff] [blame] | 286 | 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 | } |