File: zip_test.go

package info (click to toggle)
golang-github-gabriel-vasile-mimetype 1.4.8%2Bdfsg1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 464 kB
  • sloc: makefile: 3
file content (136 lines) | stat: -rw-r--r-- 3,298 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
package magic

import (
	"archive/zip"
	"bytes"
	"fmt"
	"io"
	"testing"
)

func createZip(files []string) (*bytes.Buffer, error) {
	buf := bytes.NewBuffer(nil)
	w := zip.NewWriter(buf)

	for _, f := range files {
		_, err := w.Create(f)
		if err != nil {
			return nil, err
		}
	}

	return buf, w.Close()
}

func createZipUncompressed(content *bytes.Buffer) (*bytes.Buffer, error) {
	buf := bytes.NewBuffer(nil)
	w := zip.NewWriter(buf)

	for i := 0; i < 5; i++ {
		file, err := w.CreateHeader(&zip.FileHeader{
			Name:   fmt.Sprintf("file%d", i),
			Method: zip.Store, // Store means 0 compression.
		})
		if err != nil {
			return nil, err
		}
		if _, err := io.Copy(file, content); err != nil {
			return nil, err
		}
	}

	return buf, w.Close()
}

func TestZeroZip(t *testing.T) {
	tcases := []struct {
		name  string
		files []string
		xlsx  bool
		docx  bool
		pptx  bool
		jar   bool
	}{{
		name:  "empty zip",
		files: nil,
	}, {
		name:  "no customXml",
		files: []string{"foo", "word/"},
	}, {
		name:  "customXml, but no word/",
		files: []string{"customXml"},
	}, {
		name:  "customXml, and other files, but no word/",
		files: []string{"customXml", "1", "2", "3"},
	}, {
		name:  "customXml, and other files, but word/ is the 7th file", // we only check until 6th file
		files: []string{"customXml", "1", "2", "3", "4", "5", "word/"},
	}, {
		name:  "customXml, word/ xl/ pptx/ after 5 files",
		files: []string{"1", "2", "3", "4", "5", "customXml", "word/", "xl/", "ppt/"},
	}, {
		name:  "customXml, word/",
		files: []string{"customXml", "word/"},
		docx:  true,
	}, {
		name:  "customXml, word/with_suffix",
		files: []string{"customXml", "word/with_suffix"},
		docx:  true,
	}, {
		name:  "customXml, word/",
		files: []string{"customXml", "word/media"},
		docx:  true,
	}, {
		name:  "customXml, xl/",
		files: []string{"customXml", "xl/media"},
		xlsx:  true,
	}, {
		name:  "customXml, ppt/",
		files: []string{"customXml", "ppt/media"},
		pptx:  true,
	}, {
		name:  "META-INF",
		files: []string{"META-INF/MANIFEST.MF"},
		jar:   true,
	}, {
		name:  "1 2 3 4 5 6 META-INF", // we only check first 6 files
		files: []string{"1", "2", "3", "4", "5", "6", "META-INF/MANIFEST.MF"},
		jar:   false,
	}}

	for _, tc := range tcases {
		t.Run(tc.name, func(t *testing.T) {
			buf, err := createZip(tc.files)
			if err != nil {
				t.Fatal(err)
			}

			docx := Docx(buf.Bytes(), 0)
			xlsx := Xlsx(buf.Bytes(), 0)
			pptx := Pptx(buf.Bytes(), 0)
			jar := Jar(buf.Bytes(), 0)

			if tc.docx != docx || tc.xlsx != xlsx || tc.pptx != pptx || tc.jar != jar {
				t.Errorf(`expected %t %t %t %t;
                got %t %t %t %t`, tc.docx, tc.xlsx, tc.pptx, tc.jar, docx, xlsx, pptx, jar)
			}

			// #400 - xlsx, docx, pptx put as is (compression lvl 0) inside a zip
			// It should continue to get detected as regular zip, not xlsx or docx or pptx.
			uncompressedZip, err := createZipUncompressed(buf)
			if err != nil {
				t.Fatal(err)
			}

			docx = Docx(uncompressedZip.Bytes(), 0)
			xlsx = Xlsx(uncompressedZip.Bytes(), 0)
			pptx = Pptx(uncompressedZip.Bytes(), 0)
			jar = Jar(uncompressedZip.Bytes(), 0)

			if docx || xlsx || pptx || jar {
				t.Errorf(`uncompressedZip: expected false, false, false;
                got %t %t %t %t`, docx, xlsx, pptx, jar)
			}
		})
	}
}