| 1 |
package magic |
| 2 |
|
| 3 |
import ( |
| 4 |
"archive/zip" |
| 5 |
"bytes" |
| 6 |
"fmt" |
| 7 |
"io" |
| 8 |
"testing" |
| 9 |
) |
| 10 |
|
| 11 |
func createZip(files []string) (*bytes.Buffer, error) { |
| 12 |
buf := bytes.NewBuffer(nil) |
| 13 |
w := zip.NewWriter(buf) |
| 14 |
|
| 15 |
for _, f := range files { |
| 16 |
_, err := w.Create(f) |
| 17 |
if err != nil { |
| 18 |
return nil, err |
| 19 |
} |
| 20 |
} |
| 21 |
|
| 22 |
return buf, w.Close() |
| 23 |
} |
| 24 |
|
| 25 |
func createZipUncompressed(content *bytes.Buffer) (*bytes.Buffer, error) { |
| 26 |
buf := bytes.NewBuffer(nil) |
| 27 |
w := zip.NewWriter(buf) |
| 28 |
|
| 29 |
for i := 0; i < 5; i++ { |
| 30 |
file, err := w.CreateHeader(&zip.FileHeader{ |
| 31 |
Name: fmt.Sprintf("file%d", i), |
| 32 |
Method: zip.Store, |
| 33 |
}) |
| 34 |
if err != nil { |
| 35 |
return nil, err |
| 36 |
} |
| 37 |
if _, err := io.Copy(file, content); err != nil { |
| 38 |
return nil, err |
| 39 |
} |
| 40 |
} |
| 41 |
|
| 42 |
return buf, w.Close() |
| 43 |
} |
| 44 |
|
| 45 |
func TestZeroZip(t *testing.T) { |
| 46 |
tcases := []struct { |
| 47 |
name string |
| 48 |
files []string |
| 49 |
xlsx bool |
| 50 |
docx bool |
| 51 |
pptx bool |
| 52 |
jar bool |
| 53 |
}{{ |
| 54 |
name: "empty zip", |
| 55 |
files: nil, |
| 56 |
}, { |
| 57 |
name: "no customXml/", |
| 58 |
files: []string{"foo", "word/"}, |
| 59 |
}, { |
| 60 |
name: "customXml/, but no word/", |
| 61 |
files: []string{"customXml/"}, |
| 62 |
}, { |
| 63 |
name: "customXml/, and other files, but no word/", |
| 64 |
files: []string{"customXml/", "1", "2", "3"}, |
| 65 |
}, { |
| 66 |
name: "customXml/, and other files, but word/ is the 7th file", |
| 67 |
files: []string{"customXml/", "1", "2", "3", "4", "5", "word/"}, |
| 68 |
docx: true, |
| 69 |
}, { |
| 70 |
name: "customXml/, word/ xl/ pptx/ after 5 files", |
| 71 |
files: []string{"1", "2", "3", "4", "5", "customXml/", "word/", "xl/", "ppt/"}, |
| 72 |
}, { |
| 73 |
name: "customXml/, word/", |
| 74 |
files: []string{"customXml/", "word/"}, |
| 75 |
docx: true, |
| 76 |
}, { |
| 77 |
name: "customXml/, word/with_suffix", |
| 78 |
files: []string{"customXml/", "word/with_suffix"}, |
| 79 |
docx: true, |
| 80 |
}, { |
| 81 |
name: "customXml/, word/", |
| 82 |
files: []string{"customXml/", "word/media"}, |
| 83 |
docx: true, |
| 84 |
}, { |
| 85 |
name: "customXml/, xl/", |
| 86 |
files: []string{"customXml/", "xl/media"}, |
| 87 |
xlsx: true, |
| 88 |
}, { |
| 89 |
name: "customXml/, ppt/", |
| 90 |
files: []string{"customXml/", "ppt/media"}, |
| 91 |
pptx: true, |
| 92 |
}, { |
| 93 |
name: "manifest file first", |
| 94 |
files: []string{"META-INF/MANIFEST.MF"}, |
| 95 |
jar: true, |
| 96 |
}, { |
| 97 |
name: "manifest dir first", |
| 98 |
files: []string{"META-INF/"}, |
| 99 |
jar: true, |
| 100 |
}, { |
| 101 |
name: "META-INF but not manifest first", |
| 102 |
files: []string{"META-INF/com.github.org", "META-INF/"}, |
| 103 |
jar: false, |
| 104 |
}, { |
| 105 |
name: "manifest second file", |
| 106 |
files: []string{"1", "META-INF/MANIFEST.MF"}, |
| 107 |
jar: false, |
| 108 |
}, { |
| 109 |
name: "ppt/ after 15 files", |
| 110 |
files: []string{ |
| 111 |
"[Content_Types].xml", |
| 112 |
"_rels/.rels", |
| 113 |
"customXml/_rels/item1.xml", |
| 114 |
"customXml/_rels/item2.xml.rels", |
| 115 |
"customXml/_rels/item3.xml.rels", |
| 116 |
"customXml/_rels/item4.xml.rels", |
| 117 |
"customXml/item1.xml", |
| 118 |
"customXml/item2.xml", |
| 119 |
"customXml/item3.xml", |
| 120 |
"customXml/itemProps1.xml", |
| 121 |
"customXml/itemProps2.xml", |
| 122 |
"customXml/itemProps3.xml", |
| 123 |
"docProps/app.xml", |
| 124 |
"docProps/core.xml", |
| 125 |
"docProps/custom.xml", |
| 126 |
"ppt/_rels/presentation.xml.rel", |
| 127 |
}, |
| 128 |
pptx: true, |
| 129 |
}, { |
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
name: "docProps dir (not file) is first", |
| 136 |
files: []string{"docProps/custom.xml", "xl/"}, |
| 137 |
xlsx: true, |
| 138 |
}} |
| 139 |
|
| 140 |
for i, tc := range tcases { |
| 141 |
t.Run(tc.name, func(t *testing.T) { |
| 142 |
buf, err := createZip(tc.files) |
| 143 |
if err != nil { |
| 144 |
t.Fatal(err) |
| 145 |
} |
| 146 |
|
| 147 |
docx := Docx(buf.Bytes(), 0) |
| 148 |
xlsx := Xlsx(buf.Bytes(), 0) |
| 149 |
pptx := Pptx(buf.Bytes(), 0) |
| 150 |
jar := Jar(buf.Bytes(), 0) |
| 151 |
|
| 152 |
if tc.docx != docx || tc.xlsx != xlsx || tc.pptx != pptx || tc.jar != jar { |
| 153 |
t.Errorf(` |
| 154 |
docx xlsx pptx jar %d |
| 155 |
expected %t %t %t %t; |
| 156 |
got %t %t %t %t`, i, tc.docx, tc.xlsx, tc.pptx, tc.jar, docx, xlsx, pptx, jar) |
| 157 |
} |
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
uncompressedZip, err := createZipUncompressed(buf) |
| 162 |
if err != nil { |
| 163 |
t.Fatal(err) |
| 164 |
} |
| 165 |
|
| 166 |
docx = Docx(uncompressedZip.Bytes(), 0) |
| 167 |
xlsx = Xlsx(uncompressedZip.Bytes(), 0) |
| 168 |
pptx = Pptx(uncompressedZip.Bytes(), 0) |
| 169 |
jar = Jar(uncompressedZip.Bytes(), 0) |
| 170 |
|
| 171 |
if docx || xlsx || pptx || jar { |
| 172 |
t.Errorf(` |
| 173 |
uncompressedZip: docx xlsx pptx jar %d |
| 174 |
expected false false false false |
| 175 |
got %t %t %t %t`, i, docx, xlsx, pptx, jar) |
| 176 |
} |
| 177 |
}) |
| 178 |
} |
| 179 |
} |
| 180 |
|
| 181 |
func BenchmarkZip(b *testing.B) { |
| 182 |
buf, err := createZip([]string{ |
| 183 |
"[Content_Types].xml", |
| 184 |
"_rels/.rels", |
| 185 |
"customXml/_rels/item1.xml", |
| 186 |
"customXml/_rels/item2.xml.rels", |
| 187 |
"customXml/_rels/item3.xml.rels", |
| 188 |
"customXml/_rels/item4.xml.rels", |
| 189 |
"customXml/item1.xml", |
| 190 |
"customXml/item2.xml", |
| 191 |
"customXml/item3.xml", |
| 192 |
"customXml/itemProps1.xml", |
| 193 |
"customXml/itemProps2.xml", |
| 194 |
"customXml/itemProps3.xml", |
| 195 |
"docProps/app.xml", |
| 196 |
"docProps/core.xml", |
| 197 |
"docProps/custom.xml", |
| 198 |
"ppt/_rels/presentation.xml.rel", |
| 199 |
"xl/_rels/presentation.xml.rel", |
| 200 |
"word/_rels/presentation.xml.rel", |
| 201 |
"doc.kml", |
| 202 |
}) |
| 203 |
if err != nil { |
| 204 |
b.Fatal(err) |
| 205 |
} |
| 206 |
|
| 207 |
b.ReportAllocs() |
| 208 |
for b.Loop() { |
| 209 |
Docx(buf.Bytes(), 0) |
| 210 |
Xlsx(buf.Bytes(), 0) |
| 211 |
Pptx(buf.Bytes(), 0) |
| 212 |
Jar(buf.Bytes(), 0) |
| 213 |
KMZ(buf.Bytes(), 0) |
| 214 |
} |
| 215 |
} |