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 15package zip 16 17import ( 18 "bytes" 19 "testing" 20) 21 22var stripZip64Testcases = []struct { 23 name string 24 in []byte 25 out []byte 26}{ 27 { 28 name: "empty", 29 in: []byte{}, 30 out: []byte{}, 31 }, 32 { 33 name: "trailing data", 34 in: []byte{1, 2, 3}, 35 out: []byte{1, 2, 3}, 36 }, 37 { 38 name: "valid non-zip64 extra", 39 in: []byte{2, 0, 2, 0, 1, 2}, 40 out: []byte{2, 0, 2, 0, 1, 2}, 41 }, 42 { 43 name: "two valid non-zip64 extras", 44 in: []byte{2, 0, 2, 0, 1, 2, 2, 0, 0, 0}, 45 out: []byte{2, 0, 2, 0, 1, 2, 2, 0, 0, 0}, 46 }, 47 { 48 name: "simple zip64 extra", 49 in: []byte{1, 0, 8, 0, 1, 2, 3, 4, 5, 6, 7, 8}, 50 out: []byte{}, 51 }, 52 { 53 name: "zip64 extra and valid non-zip64 extra", 54 in: []byte{1, 0, 8, 0, 1, 2, 3, 4, 5, 6, 7, 8, 2, 0, 0, 0}, 55 out: []byte{2, 0, 0, 0}, 56 }, 57 { 58 name: "invalid extra", 59 in: []byte{0, 0, 8, 0, 0, 0}, 60 out: []byte{0, 0, 8, 0, 0, 0}, 61 }, 62 { 63 name: "zip64 extra and extended-timestamp extra and valid non-zip64 extra", 64 in: []byte{1, 0, 8, 0, 1, 2, 3, 4, 5, 6, 7, 8, 85, 84, 5, 0, 1, 1, 2, 3, 4, 2, 0, 0, 0}, 65 out: []byte{2, 0, 0, 0}, 66 }, 67} 68 69func TestStripZip64Extras(t *testing.T) { 70 for _, testcase := range stripZip64Testcases { 71 got := stripExtras(testcase.in) 72 if !bytes.Equal(got, testcase.out) { 73 t.Errorf("Failed testcase %s\ninput: %v\n want: %v\n got: %v\n", testcase.name, testcase.in, testcase.out, got) 74 } 75 } 76} 77 78func TestCopyFromZip64(t *testing.T) { 79 if testing.Short() { 80 t.Skip("slow test; skipping") 81 } 82 83 const size = uint32max + 1 84 fromZipBytes := &bytes.Buffer{} 85 fromZip := NewWriter(fromZipBytes) 86 w, err := fromZip.CreateHeaderAndroid(&FileHeader{ 87 Name: "large", 88 Method: Store, 89 UncompressedSize64: size, 90 CompressedSize64: size, 91 }) 92 if err != nil { 93 t.Fatalf("Create: %v", err) 94 } 95 _, err = w.Write(make([]byte, size)) 96 if err != nil { 97 t.Fatalf("Write: %v", err) 98 } 99 err = fromZip.Close() 100 if err != nil { 101 t.Fatalf("Close: %v", err) 102 } 103 fromZip = nil 104 105 fromZipReader, err := NewReader(bytes.NewReader(fromZipBytes.Bytes()), int64(fromZipBytes.Len())) 106 if err != nil { 107 t.Fatalf("NewReader: %v", err) 108 } 109 110 toZipBytes := &bytes.Buffer{} 111 toZip := NewWriter(toZipBytes) 112 err = toZip.CopyFrom(fromZipReader.File[0], fromZipReader.File[0].Name) 113 if err != nil { 114 t.Fatalf("CopyFrom: %v", err) 115 } 116 117 err = toZip.Close() 118 if err != nil { 119 t.Fatalf("Close: %v", err) 120 } 121 122 // Save some memory 123 fromZipReader = nil 124 fromZipBytes.Reset() 125 126 toZipReader, err := NewReader(bytes.NewReader(toZipBytes.Bytes()), int64(toZipBytes.Len())) 127 if err != nil { 128 t.Fatalf("NewReader: %v", err) 129 } 130 131 if len(toZipReader.File) != 1 { 132 t.Fatalf("Expected 1 file in toZip, got %d", len(toZipReader.File)) 133 } 134 135 if g, w := toZipReader.File[0].CompressedSize64, uint64(size); g != w { 136 t.Errorf("Expected CompressedSize64 %d, got %d", w, g) 137 } 138 139 if g, w := toZipReader.File[0].UncompressedSize64, uint64(size); g != w { 140 t.Errorf("Expected UnompressedSize64 %d, got %d", w, g) 141 } 142} 143 144// Test for b/187485108: zip64 output can't be read by p7zip 16.02. 145func TestZip64P7ZipRecords(t *testing.T) { 146 if testing.Short() { 147 t.Skip("slow test; skipping") 148 } 149 150 const size = uint32max + 1 151 zipBytes := &bytes.Buffer{} 152 zip := NewWriter(zipBytes) 153 f, err := zip.CreateHeaderAndroid(&FileHeader{ 154 Name: "large", 155 Method: Store, 156 UncompressedSize64: size, 157 CompressedSize64: size, 158 }) 159 if err != nil { 160 t.Fatalf("Create: %v", err) 161 } 162 _, err = f.Write(make([]byte, size)) 163 if err != nil { 164 t.Fatalf("Write: %v", err) 165 } 166 err = zip.Close() 167 if err != nil { 168 t.Fatalf("Close: %v", err) 169 } 170 171 buf := zipBytes.Bytes() 172 p := findSignatureInBlock(buf) 173 if p < 0 { 174 t.Fatalf("Missing signature") 175 } 176 177 b := readBuf(buf[p+4:]) // skip signature 178 d := &directoryEnd{ 179 diskNbr: uint32(b.uint16()), 180 dirDiskNbr: uint32(b.uint16()), 181 dirRecordsThisDisk: uint64(b.uint16()), 182 directoryRecords: uint64(b.uint16()), 183 directorySize: uint64(b.uint32()), 184 directoryOffset: uint64(b.uint32()), 185 commentLen: b.uint16(), 186 } 187 188 // p7zip 16.02 wants regular end record directoryRecords to be accurate. 189 if g, w := d.directoryRecords, uint64(1); g != w { 190 t.Errorf("wanted directoryRecords %d, got %d", w, g) 191 } 192 193 if g, w := d.directorySize, uint64(uint32max); g != w { 194 t.Errorf("wanted directorySize %d, got %d", w, g) 195 } 196 197 if g, w := d.directoryOffset, uint64(uint32max); g != w { 198 t.Errorf("wanted directoryOffset %d, got %d", w, g) 199 } 200 201 r := bytes.NewReader(buf) 202 203 p64, err := findDirectory64End(r, int64(p)) 204 if err != nil { 205 t.Fatalf("findDirectory64End: %v", err) 206 } 207 if p < 0 { 208 t.Fatalf("findDirectory64End: not found") 209 } 210 err = readDirectory64End(r, p64, d) 211 if err != nil { 212 t.Fatalf("readDirectory64End: %v", err) 213 } 214 215 if g, w := d.directoryRecords, uint64(1); g != w { 216 t.Errorf("wanted directoryRecords %d, got %d", w, g) 217 } 218 219 if g, w := d.directoryOffset, uint64(uint32max); g <= w { 220 t.Errorf("wanted directoryOffset > %d, got %d", w, g) 221 } 222} 223