blob: 9932c1b13ac8749986050abdb786b15aa75f34b6 [file] [log] [blame]
Dan Willemsena1354b32017-02-17 13:14:43 -08001// 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 },
Nan Zhang10e8e932017-09-19 17:13:11 -070062 {
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 },
Dan Willemsena1354b32017-02-17 13:14:43 -080067}
68
69func TestStripZip64Extras(t *testing.T) {
70 for _, testcase := range stripZip64Testcases {
Nan Zhang10e8e932017-09-19 17:13:11 -070071 got := stripExtras(testcase.in)
Dan Willemsena1354b32017-02-17 13:14:43 -080072 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}
Colin Cross373147b2020-12-17 15:08:01 -080077
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}