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 |
| 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 Cross | 8936b02 | 2017-06-23 13:00:17 -0700 | [diff] [blame^] | 120 | name: "sort jar", |
| 121 | |
| 122 | inputFiles: []string{ |
| 123 | "MANIFEST.MF", |
| 124 | "META-INF/MANIFEST.MF", |
| 125 | "META-INF/aaa/", |
| 126 | "META-INF/aaa/aaa", |
| 127 | "META-INF/AAA", |
| 128 | "META-INF.txt", |
| 129 | "META-INF/", |
| 130 | "AAA", |
| 131 | "aaa", |
| 132 | }, |
| 133 | sortJava: true, |
| 134 | args: nil, |
| 135 | |
| 136 | outputFiles: []string{ |
| 137 | "META-INF/", |
| 138 | "META-INF/MANIFEST.MF", |
| 139 | "META-INF/AAA", |
| 140 | "META-INF/aaa/", |
| 141 | "META-INF/aaa/aaa", |
| 142 | "AAA", |
| 143 | "MANIFEST.MF", |
| 144 | "META-INF.txt", |
| 145 | "aaa", |
| 146 | }, |
| 147 | }, |
| 148 | { |
Dan Willemsen | 82218f2 | 2017-06-19 16:35:00 -0700 | [diff] [blame] | 149 | name: "double input", |
| 150 | |
| 151 | inputFiles: []string{ |
| 152 | "b", |
| 153 | "a", |
| 154 | }, |
| 155 | args: []string{"a:a2", "**"}, |
| 156 | |
| 157 | outputFiles: []string{ |
| 158 | "a2", |
| 159 | "b", |
| 160 | "a", |
| 161 | }, |
| 162 | }, |
| 163 | } |
| 164 | |
| 165 | func errorString(e error) string { |
| 166 | if e == nil { |
| 167 | return "" |
| 168 | } |
| 169 | return e.Error() |
| 170 | } |
| 171 | |
| 172 | func TestZip2Zip(t *testing.T) { |
| 173 | for _, testCase := range testCases { |
| 174 | t.Run(testCase.name, func(t *testing.T) { |
| 175 | inputBuf := &bytes.Buffer{} |
| 176 | outputBuf := &bytes.Buffer{} |
| 177 | |
| 178 | inputWriter := zip.NewWriter(inputBuf) |
| 179 | for _, file := range testCase.inputFiles { |
| 180 | w, err := inputWriter.Create(file) |
| 181 | if err != nil { |
| 182 | t.Fatal(err) |
| 183 | } |
| 184 | fmt.Fprintln(w, "test") |
| 185 | } |
| 186 | inputWriter.Close() |
| 187 | inputBytes := inputBuf.Bytes() |
| 188 | inputReader, err := zip.NewReader(bytes.NewReader(inputBytes), int64(len(inputBytes))) |
| 189 | if err != nil { |
| 190 | t.Fatal(err) |
| 191 | } |
| 192 | |
| 193 | outputWriter := zip.NewWriter(outputBuf) |
Colin Cross | 8936b02 | 2017-06-23 13:00:17 -0700 | [diff] [blame^] | 194 | err = zip2zip(inputReader, outputWriter, testCase.sortGlobs, testCase.sortJava, false, testCase.args) |
Dan Willemsen | 82218f2 | 2017-06-19 16:35:00 -0700 | [diff] [blame] | 195 | if errorString(testCase.err) != errorString(err) { |
| 196 | t.Fatalf("Unexpected error:\n got: %q\nwant: %q", errorString(err), errorString(testCase.err)) |
| 197 | } |
| 198 | |
| 199 | outputWriter.Close() |
| 200 | outputBytes := outputBuf.Bytes() |
| 201 | outputReader, err := zip.NewReader(bytes.NewReader(outputBytes), int64(len(outputBytes))) |
| 202 | if err != nil { |
| 203 | t.Fatal(err) |
| 204 | } |
| 205 | var outputFiles []string |
| 206 | if len(outputReader.File) > 0 { |
| 207 | outputFiles = make([]string, len(outputReader.File)) |
| 208 | for i, file := range outputReader.File { |
| 209 | outputFiles[i] = file.Name |
| 210 | } |
| 211 | } |
| 212 | |
| 213 | if !reflect.DeepEqual(testCase.outputFiles, outputFiles) { |
| 214 | t.Fatalf("Output file list does not match:\n got: %v\nwant: %v", outputFiles, testCase.outputFiles) |
| 215 | } |
| 216 | }) |
| 217 | } |
| 218 | } |