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 |
| 31 | args []string |
| 32 | |
| 33 | outputFiles []string |
| 34 | err error |
| 35 | }{ |
| 36 | { |
| 37 | name: "unsupported \\", |
| 38 | |
| 39 | args: []string{"a\\b:b"}, |
| 40 | |
| 41 | err: fmt.Errorf("\\ characters are not currently supported"), |
| 42 | }, |
| 43 | { |
| 44 | name: "unsupported **", |
| 45 | |
| 46 | args: []string{"a/**:b"}, |
| 47 | |
| 48 | err: fmt.Errorf("** is only supported on its own, not with other characters"), |
| 49 | }, |
| 50 | { // This is modelled after the update package build rules in build/make/core/Makefile |
| 51 | name: "filter globs", |
| 52 | |
| 53 | inputFiles: []string{ |
| 54 | "RADIO/a", |
| 55 | "IMAGES/system.img", |
| 56 | "IMAGES/b.txt", |
| 57 | "IMAGES/recovery.img", |
| 58 | "IMAGES/vendor.img", |
| 59 | "OTA/android-info.txt", |
| 60 | "OTA/b", |
| 61 | }, |
| 62 | args: []string{"OTA/android-info.txt:android-info.txt", "IMAGES/*.img:."}, |
| 63 | |
| 64 | outputFiles: []string{ |
| 65 | "android-info.txt", |
| 66 | "system.img", |
| 67 | "recovery.img", |
| 68 | "vendor.img", |
| 69 | }, |
| 70 | }, |
| 71 | { |
| 72 | name: "sorted filter globs", |
| 73 | |
| 74 | inputFiles: []string{ |
| 75 | "RADIO/a", |
| 76 | "IMAGES/system.img", |
| 77 | "IMAGES/b.txt", |
| 78 | "IMAGES/recovery.img", |
| 79 | "IMAGES/vendor.img", |
| 80 | "OTA/android-info.txt", |
| 81 | "OTA/b", |
| 82 | }, |
| 83 | sortGlobs: true, |
| 84 | args: []string{"IMAGES/*.img:.", "OTA/android-info.txt:android-info.txt"}, |
| 85 | |
| 86 | outputFiles: []string{ |
| 87 | "recovery.img", |
| 88 | "system.img", |
| 89 | "vendor.img", |
| 90 | "android-info.txt", |
| 91 | }, |
| 92 | }, |
| 93 | { |
| 94 | name: "sort all", |
| 95 | |
| 96 | inputFiles: []string{ |
| 97 | "RADIO/a", |
| 98 | "IMAGES/system.img", |
| 99 | "IMAGES/b.txt", |
| 100 | "IMAGES/recovery.img", |
| 101 | "IMAGES/vendor.img", |
| 102 | "OTA/b", |
| 103 | "OTA/android-info.txt", |
| 104 | }, |
| 105 | sortGlobs: true, |
| 106 | args: []string{"**"}, |
| 107 | |
| 108 | outputFiles: []string{ |
| 109 | "IMAGES/b.txt", |
| 110 | "IMAGES/recovery.img", |
| 111 | "IMAGES/system.img", |
| 112 | "IMAGES/vendor.img", |
| 113 | "OTA/android-info.txt", |
| 114 | "OTA/b", |
| 115 | "RADIO/a", |
| 116 | }, |
| 117 | }, |
| 118 | { |
| 119 | name: "double input", |
| 120 | |
| 121 | inputFiles: []string{ |
| 122 | "b", |
| 123 | "a", |
| 124 | }, |
| 125 | args: []string{"a:a2", "**"}, |
| 126 | |
| 127 | outputFiles: []string{ |
| 128 | "a2", |
| 129 | "b", |
| 130 | "a", |
| 131 | }, |
| 132 | }, |
| 133 | } |
| 134 | |
| 135 | func errorString(e error) string { |
| 136 | if e == nil { |
| 137 | return "" |
| 138 | } |
| 139 | return e.Error() |
| 140 | } |
| 141 | |
| 142 | func TestZip2Zip(t *testing.T) { |
| 143 | for _, testCase := range testCases { |
| 144 | t.Run(testCase.name, func(t *testing.T) { |
| 145 | inputBuf := &bytes.Buffer{} |
| 146 | outputBuf := &bytes.Buffer{} |
| 147 | |
| 148 | inputWriter := zip.NewWriter(inputBuf) |
| 149 | for _, file := range testCase.inputFiles { |
| 150 | w, err := inputWriter.Create(file) |
| 151 | if err != nil { |
| 152 | t.Fatal(err) |
| 153 | } |
| 154 | fmt.Fprintln(w, "test") |
| 155 | } |
| 156 | inputWriter.Close() |
| 157 | inputBytes := inputBuf.Bytes() |
| 158 | inputReader, err := zip.NewReader(bytes.NewReader(inputBytes), int64(len(inputBytes))) |
| 159 | if err != nil { |
| 160 | t.Fatal(err) |
| 161 | } |
| 162 | |
| 163 | outputWriter := zip.NewWriter(outputBuf) |
| 164 | err = zip2zip(inputReader, outputWriter, testCase.sortGlobs, false, testCase.args) |
| 165 | if errorString(testCase.err) != errorString(err) { |
| 166 | t.Fatalf("Unexpected error:\n got: %q\nwant: %q", errorString(err), errorString(testCase.err)) |
| 167 | } |
| 168 | |
| 169 | outputWriter.Close() |
| 170 | outputBytes := outputBuf.Bytes() |
| 171 | outputReader, err := zip.NewReader(bytes.NewReader(outputBytes), int64(len(outputBytes))) |
| 172 | if err != nil { |
| 173 | t.Fatal(err) |
| 174 | } |
| 175 | var outputFiles []string |
| 176 | if len(outputReader.File) > 0 { |
| 177 | outputFiles = make([]string, len(outputReader.File)) |
| 178 | for i, file := range outputReader.File { |
| 179 | outputFiles[i] = file.Name |
| 180 | } |
| 181 | } |
| 182 | |
| 183 | if !reflect.DeepEqual(testCase.outputFiles, outputFiles) { |
| 184 | t.Fatalf("Output file list does not match:\n got: %v\nwant: %v", outputFiles, testCase.outputFiles) |
| 185 | } |
| 186 | }) |
| 187 | } |
| 188 | } |