-
Notifications
You must be signed in to change notification settings - Fork 31
/
repo_db_mirror_test.go
302 lines (248 loc) · 6.84 KB
/
repo_db_mirror_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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
package main
import (
"archive/tar"
"bufio"
"compress/gzip"
"io"
"os"
"path"
"strings"
"testing"
"github.com/klauspost/compress/zstd"
"github.com/stretchr/testify/require"
)
// https://gist.github.com/maximilien/328c9ac19ab0a158a8df slightly modified to create a fake package
// it expects to receive as input where to place the tarball(tarballFilePath)
// and an array of structs, where the first value is the package name, and the second one is its content
// Arch dbs are structured as packagename-version-subversion/desc , where desc contains all the relevant info
type testTarDB struct {
PkgName string
Content string
}
// testing content for a db
func getTestTarDB() []testTarDB {
return []testTarDB{
{
PkgName: "acl-2.3.1-1",
Content: `%FILENAME%
acl-2.3.1-1-x86_64.pkg.tar.zst
%NAME%
acl
%BASE%
acl
%VERSION%
2.3.1-1
%DESC%
Access control list utilities, libraries and headers
%CSIZE%
139672
%ISIZE%
333189
%MD5SUM%
739145ae3f34b10884c678378544b10c
%SHA256SUM%
2e87a6382bcffc364015f848217d0afdcffdaa5efab43d5ee1b4d80a9645c5b8
%PGPSIG%
iHUEABYIAB0WIQQEKYl95fO9rFN6MGltQr3RFuAGjwUCYFCB0wAKCRBtQr3RFuAGj3s8AP4hGeKS33E7PGnDJVg8GGnTA6O7bTZg/wQmdNZgpMUiqAD/cjaCnHbXvciixak+KK/mA07ppArJeBo2U6WWwIPajQ0=
%URL%
https://savannah.nongnu.org/projects/acl
%LICENSE%
LGPL
%ARCH%
x86_64
%BUILDDATE%
1615888805
%PACKAGER%
Christian Hesse <[email protected]>
%REPLACES%
xfsacl
%CONFLICTS%
xfsacl
%PROVIDES%
xfsacl
libacl.so=1-64
%DEPENDS%
attr
libattr.so`,
},
{
PkgName: "attr-2.5.1-1",
Content: `%FILENAME%
attr-2.5.1-1-x86_64.pkg.tar.zst
%NAME%
attr
%BASE%
attr
%VERSION%
2.5.1-1
%DESC%
Extended attribute support library for ACL support
%CSIZE%
69800
%ISIZE%
212575
%MD5SUM%
8b1373a95af2480cc778f678e540756f
%SHA256SUM%
44b400abf34e559e5c4cdd4d1cfe799795eef59780525d6d02d36a3f3152b249
%PGPSIG%
iHUEABYIAB0WIQQEKYl95fO9rFN6MGltQr3RFuAGjwUCYFCBZgAKCRBtQr3RFuAGj4MJAPoCNnY2NIrkwFDlNh75ilhhB5hrOkxuL8M7WU6nD/PZDwEAgHkq9lnFtwWxKbeS8Ojic9dQfdvPcX6uZOihFqfAMAY=
%URL%
https://savannah.nongnu.org/projects/attr
%LICENSE%
LGPL
%ARCH%
x86_64
%BUILDDATE%
1615888678
%PACKAGER%
Christian Hesse <[email protected]>
%REPLACES%
xfsattr
%CONFLICTS%
xfsattr
%PROVIDES%
xfsattr
libattr.so=1-64
%DEPENDS%
glibc
%MAKEDEPENDS%
gettext
`,
},
}
}
// creates a test tar file
func createDbTarball(t *testing.T, tarballFilePath string, content []testTarDB) {
file, err := os.Create(tarballFilePath)
require.NoError(t, err)
defer file.Close()
gzipWriter := gzip.NewWriter(file)
defer gzipWriter.Close()
tarWriter := tar.NewWriter(gzipWriter)
defer tarWriter.Close()
for _, c := range content {
require.NoError(t, addFileToTarWriter(c.PkgName, c.Content, tarWriter))
}
}
// adds a file to the tar under pkgname/desc
func addFileToTarWriter(pkgName string, content string, tarWriter *tar.Writer) error {
header := &tar.Header{
Name: path.Join(pkgName, "desc"),
Size: int64(len(content)),
}
if err := tarWriter.WriteHeader(header); err != nil {
return err
}
if _, err := io.Copy(tarWriter, strings.NewReader(content)); err != nil {
return err
}
return nil
}
// Uncompresses a gzip file
func TestUncompressGZ(t *testing.T) {
err := uncompressGZ("nope", "nope")
tmpDir := testSetupHelper(t)
require.Error(t, err)
filePath := path.Join(tmpDir, "test.gz")
testString := ``
gzipfile, err := os.Create(filePath)
require.NoError(t, err)
writer := gzip.NewWriter(gzipfile)
reader := strings.NewReader(testString)
_, err = io.Copy(writer, reader)
require.NoError(t, err)
writer.Close()
require.NoError(t, uncompressGZ(filePath, filePath+".uncompressed"))
byteStr, err := os.ReadFile(filePath + ".uncompressed")
require.NoError(t, err)
require.Equal(t, string(byteStr), testString)
}
func TestUncompressZSTD(t *testing.T) {
err := uncompressZSTD("nope", "nope")
tmpDir := testSetupHelper(t)
require.Error(t, err)
filePath := path.Join(tmpDir, "test.zstd")
testString := ``
zstdfile, err := os.Create(filePath)
require.NoError(t, err)
writer, err := zstd.NewWriter(zstdfile)
require.NoError(t, err)
reader := strings.NewReader(testString)
_, err = io.Copy(writer, reader)
require.NoError(t, err)
writer.Close()
require.NoError(t, uncompressZSTD(filePath, filePath+".uncompressed"))
byteStr, err := os.ReadFile(filePath + ".uncompressed")
require.Equal(t, string(byteStr), testString)
require.NoError(t, err)
}
func TestUncompressZSTDBomb(t *testing.T) {
if testing.Short() {
t.Skip("skipping testing in short mode")
}
tmpDir := testSetupHelper(t)
filePath := path.Join(tmpDir, "test.zstd")
var zstdBombSize int64
zstdBombSize = 120 * 1024 * 1024
zstdfile, err := os.Create(filePath)
require.NoError(t, err)
zero, err := os.Open("/dev/zero")
if err != nil {
t.Skip("Cannot open /dev/zero, skipping gzip bomb test")
}
defer zero.Close()
writer := gzip.NewWriter(zstdfile)
reader := io.LimitReader(bufio.NewReader(zero), zstdBombSize)
_, err = io.Copy(writer, reader)
require.NoError(t, err)
writer.Close()
err = uncompressGZ(filePath, filePath+".uncompressed")
if err != nil {
// It is a success if it happens
return
}
fi, err := os.Stat(filePath + ".uncompressed")
if err != nil {
// It is a success if it happens
return
}
size := fi.Size()
require.Less(t, size, zstdBombSize, "It fully extracted the zstd bomb, this shouldn't happen")
}
func TestExtractFilenamesFromTar(t *testing.T) {
tmpDir := testSetupHelper(t)
filePath := path.Join(tmpDir, "test.gz")
testString := ``
gzipfile, err := os.Create(filePath)
require.NoError(t, err)
writer := gzip.NewWriter(gzipfile)
reader := strings.NewReader(testString)
_, err = io.Copy(writer, reader)
require.NoError(t, err)
writer.Close()
_, err = extractFilenamesFromTar("nope")
require.Error(t, err)
// now create a valid db file
filePath = path.Join(tmpDir, "core.db")
createDbTarball(t, filePath, getTestTarDB())
require.NoError(t, uncompressGZ(filePath, filePath+".uncompressed"))
got, err := extractFilenamesFromTar(filePath + ".uncompressed")
require.NoError(t, err)
want := []string{"acl-2.3.1-1-x86_64.pkg.tar.zst", "attr-2.5.1-1-x86_64.pkg.tar.zst"}
require.Equal(t, want, got)
}
func TestGetPacolocoURL(t *testing.T) {
// create a package
got := getPacolocoURL(Package{PackageName: "webkit2gtk", RepoName: "testRepo", Version: "2.26.4-1", Arch: "x86_64"}, "")
want := "/repo/testRepo/webkit2gtk-2.26.4-1-x86_64"
require.Equal(t, want, got)
}
func TestBuildMirrorPkg(t *testing.T) {
got, err := buildMirrorPkg("libstdc++5-3.3.6-7-x86_64.pkg.tar.zst", "testRepo", "community")
require.NoError(t, err)
want := MirrorPackage{PackageName: "libstdc++5", RepoName: "testRepo", Version: "3.3.6-7", Arch: "x86_64", DownloadURL: "/repo/testRepo/community/libstdc++5-3.3.6-7-x86_64", FileExt: ".pkg.tar.zst"}
require.Equal(t, want, got)
_, err = buildMirrorPkg("webkit2gtk-2.26.4-1-x86_6-4.pkg.tar.zst", "testRepo", "")
require.Errorf(t, err, "Should have thrown an error cause the string is invalid")
}