-
Notifications
You must be signed in to change notification settings - Fork 91
/
decompression_test.go
206 lines (160 loc) · 4.68 KB
/
decompression_test.go
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
package embeddedpostgres
import (
"archive/tar"
"errors"
"fmt"
"io"
"os"
"path"
"path/filepath"
"syscall"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/xi2/xz"
)
func Test_decompressTarXz(t *testing.T) {
tempDir, err := os.MkdirTemp("", "temp_tar_test")
if err != nil {
panic(err)
}
if err := syscall.Rmdir(tempDir); err != nil {
panic(err)
}
archive, cleanUp := createTempXzArchive()
defer cleanUp()
err = decompressTarXz(defaultTarReader, archive, tempDir)
assert.NoError(t, err)
expectedExtractedFileLocation := filepath.Join(tempDir, "dir1", "dir2", "some_content")
assert.FileExists(t, expectedExtractedFileLocation)
fileContentBytes, err := os.ReadFile(expectedExtractedFileLocation)
assert.NoError(t, err)
assert.Equal(t, "b33r is g00d", string(fileContentBytes))
}
func Test_decompressTarXz_ErrorWhenFileNotExists(t *testing.T) {
err := decompressTarXz(defaultTarReader, "/does-not-exist", "/also-fake")
assert.Error(t, err)
assert.Contains(
t,
err.Error(),
"unable to extract postgres archive /does-not-exist to /also-fake, if running parallel tests, configure RuntimePath to isolate testing directories",
)
}
func Test_decompressTarXz_ErrorWhenErrorDuringRead(t *testing.T) {
tempDir, err := os.MkdirTemp("", "temp_tar_test")
if err != nil {
panic(err)
}
if err := syscall.Rmdir(tempDir); err != nil {
panic(err)
}
archive, cleanUp := createTempXzArchive()
defer cleanUp()
err = decompressTarXz(func(reader *xz.Reader) (func() (*tar.Header, error), func() io.Reader) {
return func() (*tar.Header, error) {
return nil, errors.New("oh noes")
}, nil
}, archive, tempDir)
assert.EqualError(t, err, "unable to extract postgres archive: oh noes")
}
func Test_decompressTarXz_ErrorWhenFailedToReadFileToCopy(t *testing.T) {
tempDir, err := os.MkdirTemp("", "temp_tar_test")
if err != nil {
panic(err)
}
archive, cleanUp := createTempXzArchive()
defer cleanUp()
blockingFile := filepath.Join(tempDir, "blocking")
if err = os.WriteFile(blockingFile, []byte("wazz"), 0000); err != nil {
panic(err)
}
fileBlockingExtractTarReader := func(reader *xz.Reader) (func() (*tar.Header, error), func() io.Reader) {
shouldReadFile := true
return func() (*tar.Header, error) {
if shouldReadFile {
shouldReadFile = false
return &tar.Header{
Typeflag: tar.TypeReg,
Name: "blocking",
}, nil
}
return nil, io.EOF
}, func() io.Reader {
open, _ := os.Open("file_not_exists")
return open
}
}
err = decompressTarXz(fileBlockingExtractTarReader, archive, tempDir)
assert.Regexp(t, "^unable to extract postgres archive:.+$", err)
}
func Test_decompressTarXz_ErrorWhenFileToCopyToNotExists(t *testing.T) {
tempDir, err := os.MkdirTemp("", "temp_tar_test")
if err != nil {
panic(err)
}
if err := syscall.Rmdir(tempDir); err != nil {
panic(err)
}
archive, cleanUp := createTempXzArchive()
defer cleanUp()
fileBlockingExtractTarReader := func(reader *xz.Reader) (func() (*tar.Header, error), func() io.Reader) {
shouldReadFile := true
return func() (*tar.Header, error) {
if shouldReadFile {
shouldReadFile = false
return &tar.Header{
Typeflag: tar.TypeReg,
Name: "some_dir/wazz/dazz/fazz",
}, nil
}
return nil, io.EOF
}, func() io.Reader {
open, _ := os.Open("file_not_exists")
return open
}
}
err = decompressTarXz(fileBlockingExtractTarReader, archive, tempDir)
assert.Regexp(t, "^unable to extract postgres archive:.+$", err)
}
func Test_decompressTarXz_ErrorWhenArchiveCorrupted(t *testing.T) {
tempDir, err := os.MkdirTemp("", "temp_tar_test")
if err != nil {
panic(err)
}
if err := syscall.Rmdir(tempDir); err != nil {
panic(err)
}
archive, cleanup := createTempXzArchive()
defer cleanup()
file, err := os.OpenFile(archive, os.O_WRONLY, 0664)
if err != nil {
panic(err)
}
if _, err := file.Seek(35, 0); err != nil {
panic(err)
}
if _, err := file.WriteString("someJunk"); err != nil {
panic(err)
}
if err := file.Close(); err != nil {
panic(err)
}
err = decompressTarXz(defaultTarReader, archive, tempDir)
assert.EqualError(t, err, "unable to extract postgres archive: xz: data is corrupt")
}
func Test_decompressTarXz_ErrorWithInvalidDestination(t *testing.T) {
archive, cleanUp := createTempXzArchive()
defer cleanUp()
tempDir, err := os.MkdirTemp("", "temp_tar_test")
require.NoError(t, err)
defer func() {
os.RemoveAll(tempDir)
}()
op := fmt.Sprintf(path.Join(tempDir, "%c"), rune(0))
err = decompressTarXz(defaultTarReader, archive, op)
assert.EqualError(
t,
err,
fmt.Sprintf("unable to extract postgres archive: mkdir %s: invalid argument", op),
)
}