forked from anatol/pacoloco
-
Notifications
You must be signed in to change notification settings - Fork 0
/
repo_db_mirror_test.go
352 lines (298 loc) · 7.41 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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
package main
import (
"archive/tar"
"bufio"
"compress/gzip"
"io"
"log"
"os"
"path"
"strings"
"testing"
"github.com/google/go-cmp/cmp"
"github.com/klauspost/compress/zstd"
)
// 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(tarballFilePath string, content []testTarDB) {
file, err := os.Create(tarballFilePath)
if err != nil {
log.Fatal(err)
}
defer file.Close()
gzipWriter := gzip.NewWriter(file)
defer gzipWriter.Close()
tarWriter := tar.NewWriter(gzipWriter)
defer tarWriter.Close()
for _, t := range content {
pkgName := t.PkgName
content := t.Content
addFileToTarWriter(pkgName, content, tarWriter)
}
}
// adds a file to the tar under pkgname/desc
func addFileToTarWriter(pkgName string, content string, tarWriter *tar.Writer) {
header := &tar.Header{
Name: path.Join(pkgName, "desc"),
Size: int64(len(content)),
}
if err := tarWriter.WriteHeader(header); err != nil {
log.Fatal(err)
}
if _, err := io.Copy(tarWriter, strings.NewReader(content)); err != nil {
log.Fatal(err)
}
}
// Uncompresses a gzip file
func TestUncompressGZ(t *testing.T) {
err := uncompressGZ("nope", "nope")
tmpDir := testSetupHelper(t)
if err == nil {
t.Errorf("Should raise an error")
}
filePath := path.Join(tmpDir, "test.gz")
testString := ``
gzipfile, err := os.Create(filePath)
if err != nil {
log.Fatal(err)
}
writer := gzip.NewWriter(gzipfile)
reader := strings.NewReader(testString)
if _, err = io.Copy(writer, reader); err != nil {
log.Fatal(err)
}
writer.Close()
if err = uncompressGZ(filePath, filePath+".uncompressed"); err != nil {
log.Fatal(err)
}
byteStr, err := os.ReadFile(filePath + ".uncompressed")
if string(byteStr) != testString {
t.Errorf("Expected %v, got %v ", testString, string(byteStr))
}
if err != nil {
log.Fatal(err)
}
}
func TestUncompressZSTD(t *testing.T) {
err := uncompressZSTD("nope", "nope")
tmpDir := testSetupHelper(t)
if err == nil {
t.Errorf("Should raise an error")
}
filePath := path.Join(tmpDir, "test.zstd")
testString := ``
zstdfile, err := os.Create(filePath)
if err != nil {
log.Fatal(err)
}
writer, err := zstd.NewWriter(zstdfile)
if err != nil {
log.Fatal(err)
}
reader := strings.NewReader(testString)
if _, err = io.Copy(writer, reader); err != nil {
log.Fatal(err)
}
writer.Close()
if err = uncompressZSTD(filePath, filePath+".uncompressed"); err != nil {
log.Fatal(err)
}
byteStr, err := os.ReadFile(filePath + ".uncompressed")
if string(byteStr) != testString {
t.Errorf("Expected %v, got %v ", testString, string(byteStr))
}
if err != nil {
log.Fatal(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)
if err != nil {
log.Fatal(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)
if _, err = io.Copy(writer, reader); err != nil {
log.Fatal(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()
if size >= zstdBombSize {
log.Fatal("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)
if err != nil {
log.Fatal(err)
}
writer := gzip.NewWriter(gzipfile)
reader := strings.NewReader(testString)
if _, err = io.Copy(writer, reader); err != nil {
log.Fatal(err)
}
writer.Close()
if _, err = extractFilenamesFromTar("nope"); err == nil {
log.Fatal("Should raise an error")
}
// now create a valid db file
filePath = path.Join(tmpDir, "core.db")
createDbTarball(filePath, getTestTarDB())
if err = uncompressGZ(filePath, filePath+".uncompressed"); err != nil {
log.Fatal(err)
}
got, err := extractFilenamesFromTar(filePath + ".uncompressed")
if err != nil {
log.Fatal(err)
}
want := []string{"acl-2.3.1-1-x86_64.pkg.tar.zst", "attr-2.5.1-1-x86_64.pkg.tar.zst"}
if !cmp.Equal(got, want) {
log.Fatalf("Want %v, got %v", 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"
if got != want {
t.Errorf("Want %v, got %v", want, got)
}
}
func TestBuildMirrorPkg(t *testing.T) {
got, err := buildMirrorPkg("libstdc++5-3.3.6-7-x86_64.pkg.tar.zst", "testRepo", "community")
if err != nil {
log.Fatal(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"}
if !cmp.Equal(got, want) {
t.Errorf("Got %v, want %v", got, want)
}
if _, err = buildMirrorPkg("webkit2gtk-2.26.4-1-x86_6-4.pkg.tar.zst", "testRepo", ""); err == nil {
t.Errorf("Should have thrown an error cause the string is invalid")
}
}