-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathextract_test.go
More file actions
141 lines (123 loc) · 3.04 KB
/
extract_test.go
File metadata and controls
141 lines (123 loc) · 3.04 KB
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
137
138
139
140
141
package tinygo
import (
"archive/tar"
"archive/zip"
"compress/gzip"
"os"
"path/filepath"
"strings"
"testing"
)
func TestExtractTarGz(t *testing.T) {
tmpDir, err := os.MkdirTemp("", "extract-test-*")
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(tmpDir)
archiveFile, err := os.CreateTemp("", "archive-*.tar.gz")
if err != nil {
t.Fatal(err)
}
defer os.Remove(archiveFile.Name())
defer archiveFile.Close()
gw := gzip.NewWriter(archiveFile)
tw := tar.NewWriter(gw)
files := []struct {
Name, Body string
}{
{"tinygo/bin/tinygo", "fake binary"},
{"tinygo/README.md", "some info"},
}
for _, file := range files {
hdr := &tar.Header{
Name: file.Name,
Mode: 0755,
Size: int64(len(file.Body)),
}
if err := tw.WriteHeader(hdr); err != nil {
t.Fatal(err)
}
if _, err := tw.Write([]byte(file.Body)); err != nil {
t.Fatal(err)
}
}
tw.Close()
gw.Close()
if err := extractTarGz(archiveFile.Name(), tmpDir); err != nil {
t.Errorf("failed to extract tar.gz: %v", err)
}
for _, file := range files {
path := filepath.Join(tmpDir, file.Name)
if _, err := os.Stat(path); os.IsNotExist(err) {
t.Errorf("expected file %s to exist", path)
}
}
}
func TestExtractZip(t *testing.T) {
tmpDir, err := os.MkdirTemp("", "extract-zip-test-*")
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(tmpDir)
archiveFile, err := os.CreateTemp("", "archive-*.zip")
if err != nil {
t.Fatal(err)
}
defer os.Remove(archiveFile.Name())
defer archiveFile.Close()
zw := zip.NewWriter(archiveFile)
files := []struct {
Name, Body string
}{
{"tinygo/bin/tinygo.exe", "fake binary"},
{"tinygo/README.md", "some info"},
}
for _, file := range files {
f, err := zw.Create(file.Name)
if err != nil {
t.Fatal(err)
}
if _, err := f.Write([]byte(file.Body)); err != nil {
t.Fatal(err)
}
}
zw.Close()
if err := extractZip(archiveFile.Name(), tmpDir); err != nil {
t.Errorf("failed to extract zip: %v", err)
}
for _, file := range files {
path := filepath.Join(tmpDir, file.Name)
if _, err := os.Stat(path); os.IsNotExist(err) {
t.Errorf("expected file %s to exist", path)
}
}
}
func TestZipSlip(t *testing.T) {
tmpDir, err := os.MkdirTemp("", "zipslip-test-*")
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(tmpDir)
// Test tar.gz
archiveFile, _ := os.CreateTemp("", "slip-*.tar.gz")
gw := gzip.NewWriter(archiveFile)
tw := tar.NewWriter(gw)
tw.WriteHeader(&tar.Header{Name: "../../etc/passwd", Size: 0})
tw.Close()
gw.Close()
archiveFile.Close()
err = extractTarGz(archiveFile.Name(), tmpDir)
if err == nil || !strings.Contains(err.Error(), "illegal file path") {
t.Errorf("expected illegal file path error for tar.gz, got %v", err)
}
// Test zip
archiveFile2, _ := os.CreateTemp("", "slip-*.zip")
zw := zip.NewWriter(archiveFile2)
zw.Create("../../etc/passwd")
zw.Close()
archiveFile2.Close()
err = extractZip(archiveFile2.Name(), tmpDir)
if err == nil || !strings.Contains(err.Error(), "illegal file path") {
t.Errorf("expected illegal file path error for zip, got %v", err)
}
}