-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathfedorasource_test.go
More file actions
481 lines (421 loc) · 16.5 KB
/
fedorasource_test.go
File metadata and controls
481 lines (421 loc) · 16.5 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
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
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
//nolint:testpackage // Allow to test private functions
package fedorasource
import (
"context"
"path/filepath"
"testing"
"github.com/microsoft/azure-linux-dev-tools/internal/global/testctx"
"github.com/microsoft/azure-linux-dev-tools/internal/utils/downloader/downloader_test"
"github.com/microsoft/azure-linux-dev-tools/internal/utils/fileperms"
"github.com/microsoft/azure-linux-dev-tools/internal/utils/fileutils"
"github.com/microsoft/azure-linux-dev-tools/internal/utils/retry"
"github.com/spf13/afero"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.uber.org/mock/gomock"
)
const (
testLookasideURI = "https://example.com/$hashtype/$hash/$pkg/$filename"
testPackageName = "test-package"
testRepoDir = "/test/repo"
testEmptyRepoDir = "/test/empty-repo"
testFilePerms = fileperms.PublicFile
testDirPerms = fileperms.PublicDir
// Test source file data.
testSourcesContent = `SHA512 (example-1.0.tar.gz) = af5ae0eb4ad9c3f07b7d78ec9dfd03f6a00c257df6b829b21051d4ba2d106bf` +
`9d2f7563c0373b45e0ce5b1ad8a3bad9c05a2769547e67f4bc53692950db0ba37
SHA256 (patch-1.patch) = 67899aaa0f2f55e55e715cb65596449cb29bb0a76a764fe8f1e51bf4d0af648f
`
testSingleSourceContent = `SHA512 (example-1.0.tar.gz) = abc123def456
`
// Expected URLs (must match the hashes in testSourcesContent).
testExpectedURL1 = "https://example.com/sha512/af5ae0eb4ad9c3f07b7d78ec9dfd03f6a00c257df6b829b21051d4ba2d106bf9d2f" +
"7563c0373b45e0ce5b1ad8a3bad9c05a2769547e67f4bc53692950db0ba37/test-package/example-1.0.tar.gz"
testExpectedURL2 = "https://example.com/sha256/67899aaa0f2f55e55e715cb65596449cb29bb0a76a764fe8f1e51bf4d0af648f/" +
"test-package/patch-1.patch"
// File names.
testSourcesFile = "sources"
testTarballFile = "example-1.0.tar.gz"
testPatchFile = "patch-1.patch"
)
func TestNewFedoraRepoExtractorImpl(t *testing.T) {
ctrl := gomock.NewController(t)
ctx := testctx.NewCtx()
mockDownloader := downloader_test.NewMockDownloader(ctrl)
extractor, err := NewFedoraRepoExtractorImpl(ctx, ctx.FS(), mockDownloader, retry.Disabled())
require.NoError(t, err)
require.NotNil(t, extractor)
}
func TestNewFedoraRepoExtractorImplValidation(t *testing.T) {
ctrl := gomock.NewController(t)
ctx := testctx.NewCtx()
mockDownloader := downloader_test.NewMockDownloader(ctrl)
t.Run("nil dryRunnable should fail", func(t *testing.T) {
extractor, err := NewFedoraRepoExtractorImpl(nil, ctx.FS(), mockDownloader, retry.Disabled())
require.Error(t, err)
require.Nil(t, extractor)
})
t.Run("nil filesystem should fail", func(t *testing.T) {
extractor, err := NewFedoraRepoExtractorImpl(ctx, nil, mockDownloader, retry.Disabled())
require.Error(t, err)
require.Nil(t, extractor)
})
t.Run("nil downloader should fail", func(t *testing.T) {
extractor, err := NewFedoraRepoExtractorImpl(ctx, ctx.FS(), nil, retry.Disabled())
require.Error(t, err)
require.Nil(t, extractor)
})
}
func TestExtractSourcesFromRepo(t *testing.T) {
ctrl := gomock.NewController(t)
ctx := testctx.NewCtx()
mockDownloader := downloader_test.NewMockDownloader(ctrl)
extractor, err := NewFedoraRepoExtractorImpl(ctx, ctx.FS(), mockDownloader, retry.Disabled())
require.NoError(t, err)
require.NoError(t, ctx.FS().MkdirAll(testRepoDir, testDirPerms))
setupSourcesFile(t, ctx.FS(), testRepoDir, testSourcesContent)
// Mock downloader should create files with content matching the expected hashes
mockDownloader.EXPECT().Download(
gomock.Any(),
testExpectedURL1,
filepath.Join(testRepoDir, testTarballFile),
).DoAndReturn(func(_ context.Context, _ string, destPath string) error {
// Create a file with content that matches the expected SHA512 hash
return afero.WriteFile(ctx.FS(), destPath, []byte("test content for tarball"), testFilePerms)
})
mockDownloader.EXPECT().Download(
gomock.Any(),
testExpectedURL2,
filepath.Join(testRepoDir, testPatchFile),
).DoAndReturn(func(_ context.Context, _ string, destPath string) error {
// Create a file with content that matches the expected SHA256 hash
return afero.WriteFile(ctx.FS(), destPath, []byte("test patch content"), testFilePerms)
})
err = extractor.ExtractSourcesFromRepo(context.Background(), testRepoDir, testPackageName, testLookasideURI, nil)
require.NoError(t, err)
}
func TestExtractSourcesFromRepoValidation(t *testing.T) {
ctrl := gomock.NewController(t)
ctx := testctx.NewCtx()
mockDownloader := downloader_test.NewMockDownloader(ctrl)
extractor, err := NewFedoraRepoExtractorImpl(ctx, ctx.FS(), mockDownloader, retry.Disabled())
require.NoError(t, err)
t.Run("empty repo dir", func(t *testing.T) {
err := extractor.ExtractSourcesFromRepo(context.Background(), "", testPackageName, testLookasideURI, nil)
require.Error(t, err)
assert.Contains(t, err.Error(), "repository directory cannot be empty")
})
t.Run("empty lookaside URI", func(t *testing.T) {
err := extractor.ExtractSourcesFromRepo(context.Background(), testRepoDir, testPackageName, "", nil)
require.Error(t, err)
assert.Contains(t, err.Error(), "lookaside base URI cannot be empty")
})
t.Run("missing sources file", func(t *testing.T) {
require.NoError(t, ctx.FS().MkdirAll(testEmptyRepoDir, fileperms.PublicDir))
// Missing sources file is valid - it means no external sources to download
err := extractor.ExtractSourcesFromRepo(
context.Background(), testEmptyRepoDir, testPackageName, testLookasideURI, nil,
)
require.NoError(t, err)
})
}
func TestExtractSourcesFromRepoDownloadFailure(t *testing.T) {
ctrl := gomock.NewController(t)
ctx := testctx.NewCtx()
mockDownloader := downloader_test.NewMockDownloader(ctrl)
extractor, err := NewFedoraRepoExtractorImpl(ctx, ctx.FS(), mockDownloader, retry.Disabled())
require.NoError(t, err)
require.NoError(t, ctx.FS().MkdirAll(testRepoDir, testDirPerms))
setupSourcesFile(t, ctx.FS(), testRepoDir, testSingleSourceContent)
downloadErr := assert.AnError
mockDownloader.EXPECT().Download(gomock.Any(), gomock.Any(), gomock.Any()).
Return(downloadErr)
err = extractor.ExtractSourcesFromRepo(context.Background(), testRepoDir, testPackageName, testLookasideURI, nil)
require.Error(t, err)
require.ErrorIs(t, err, downloadErr)
assert.Contains(t, err.Error(), "failed to download sources")
}
func TestExtractSourcesFromRepoHashMismatch(t *testing.T) {
ctrl := gomock.NewController(t)
ctx := testctx.NewCtx()
mockDownloader := downloader_test.NewMockDownloader(ctrl)
extractor, err := NewFedoraRepoExtractorImpl(ctx, ctx.FS(), mockDownloader, retry.Disabled())
require.NoError(t, err)
require.NoError(t, ctx.FS().MkdirAll(testRepoDir, testDirPerms))
setupSourcesFile(t, ctx.FS(), testRepoDir, testSingleSourceContent)
// Mock downloader creates a file with WRONG content (hash mismatch)
mockDownloader.EXPECT().Download(gomock.Any(), gomock.Any(), gomock.Any()).
DoAndReturn(func(_ context.Context, _ string, destPath string) error {
// Create a file with content that does NOT match the expected hash
return afero.WriteFile(ctx.FS(), destPath, []byte("wrong content"), testFilePerms)
})
err = extractor.ExtractSourcesFromRepo(context.Background(), testRepoDir, testPackageName, testLookasideURI, nil)
require.Error(t, err)
assert.Contains(t, err.Error(), "hash mismatch")
}
// setupSourcesFile creates a sources file in the specified directory with the given content.
func setupSourcesFile(t *testing.T, fs afero.Fs, repoDir string, content string) {
t.Helper()
sourcesPath := filepath.Join(repoDir, testSourcesFile)
require.NoError(t, afero.WriteFile(fs, sourcesPath, []byte(content), testFilePerms))
}
func TestParseSourcesFile(t *testing.T) {
t.Run("modern format parses hash type and filename", func(t *testing.T) {
content := "SHA512 (file.tar.gz) = abc123\n"
sources, err := parseSourcesFile(content, "pkg", "https://example.com/$hashtype/$hash/$pkg/$filename")
require.NoError(t, err)
require.Len(t, sources, 1)
assert.Equal(t, "file.tar.gz", sources[0].fileName)
assert.Equal(t, fileutils.HashType("SHA512"), sources[0].hashType)
assert.Equal(t, "abc123", sources[0].expectedHash)
assert.Equal(t, "https://example.com/sha512/abc123/pkg/file.tar.gz", sources[0].uri)
})
t.Run("legacy format defaults to MD5", func(t *testing.T) {
content := "abc123def456 legacy.tar.gz\n"
sources, err := parseSourcesFile(content, "pkg", "https://example.com/$hashtype/$hash/$pkg/$filename")
require.NoError(t, err)
require.Len(t, sources, 1)
assert.Equal(t, "legacy.tar.gz", sources[0].fileName)
assert.Equal(t, fileutils.HashType("MD5"), sources[0].hashType)
assert.Equal(t, "abc123def456", sources[0].expectedHash)
})
t.Run("mixed case hex values are preserved", func(t *testing.T) {
content := "SHA256 (file.tar.gz) = AbCdEf123456\n"
sources, err := parseSourcesFile(content, "pkg", "https://example.com/$hashtype/$hash/$pkg/$filename")
require.NoError(t, err)
assert.Equal(t, "AbCdEf123456", sources[0].expectedHash)
})
t.Run("skips empty lines and comments", func(t *testing.T) {
content := "\n# this is a comment\nSHA256 (file.tar.gz) = abc123\n\n"
sources, err := parseSourcesFile(content, "pkg", "https://example.com/$hashtype/$hash/$pkg/$filename")
require.NoError(t, err)
require.Len(t, sources, 1)
})
t.Run("multiple entries", func(t *testing.T) {
content := "SHA512 (first.tar.gz) = aabbcc112233\nSHA256 (second.patch) = ddeeff445566\n"
sources, err := parseSourcesFile(content, "pkg", "https://example.com/$hashtype/$hash/$pkg/$filename")
require.NoError(t, err)
require.Len(t, sources, 2)
assert.Equal(t, "first.tar.gz", sources[0].fileName)
assert.Equal(t, "second.patch", sources[1].fileName)
})
t.Run("invalid format returns error with line number", func(t *testing.T) {
content := "SHA512 (valid.tar.gz) = abc123\nnot a valid line\n"
_, err := parseSourcesFile(content, "pkg", "https://example.com/$hashtype/$hash/$pkg/$filename")
require.Error(t, err)
assert.Contains(t, err.Error(), "line 2")
})
t.Run("empty content returns empty slice", func(t *testing.T) {
sources, err := parseSourcesFile("", "pkg", "https://example.com/$hashtype/$hash/$pkg/$filename")
require.NoError(t, err)
assert.Empty(t, sources)
})
}
func TestBuildLookasideURL(t *testing.T) {
tests := []struct {
name string
template string
pkg string
filename string
hashType string
hash string
expected string
expectedError string
}{
{
name: "standard template",
template: "https://example.com/repo/pkgs/$pkg/$filename/$hashtype/$hash/$filename",
pkg: "my-pkg",
filename: "source.tar.gz",
hashType: "SHA512",
hash: "abc123",
expected: "https://example.com/repo/pkgs/my-pkg/source.tar.gz/sha512/abc123/source.tar.gz",
},
{
name: "different placeholder order",
template: "https://example.com/$hashtype/$hash/$pkg/$filename",
pkg: "test-pkg",
filename: "file.tar.gz",
hashType: "SHA256",
hash: "def456",
expected: "https://example.com/sha256/def456/test-pkg/file.tar.gz",
},
{
name: "template without filename placeholder",
template: "https://example.com/$pkg/$hashtype/$hash",
pkg: "my-pkg",
filename: "source.tar.gz",
hashType: "SHA512",
hash: "abc123",
expected: "https://example.com/my-pkg/sha512/abc123",
},
{
name: "packageName containing placeholder",
template: "https://example.com/$pkg/$filename/$hashtype/$hash",
pkg: "evil-$filename-pkg",
filename: "source.tar.gz",
hashType: "SHA512",
hash: "abc123",
expectedError: "ambiguous substitution",
},
{
name: "fileName containing placeholder",
template: "https://example.com/$pkg/$filename/$hashtype/$hash",
pkg: "my-pkg",
filename: "$hash-source.tar.gz",
hashType: "SHA512",
hash: "abc123",
expectedError: "ambiguous substitution",
},
{
name: "filename with slash is path-escaped",
template: "https://example.com/$pkg/$filename/$hashtype/$hash",
pkg: "my-pkg",
filename: "foo/bar",
hashType: "SHA512",
hash: "abc123",
expected: "https://example.com/my-pkg/foo%2Fbar/sha512/abc123",
},
{
name: "filename with question mark is path-escaped",
template: "https://example.com/$pkg/$filename/$hashtype/$hash",
pkg: "my-pkg",
filename: "file?x=1",
hashType: "SHA512",
hash: "abc123",
expected: "https://example.com/my-pkg/file%3Fx=1/sha512/abc123",
},
{
name: "filename with hash is path-escaped",
template: "https://example.com/$pkg/$filename/$hashtype/$hash",
pkg: "my-pkg",
filename: "file#frag",
hashType: "SHA512",
hash: "abc123",
expected: "https://example.com/my-pkg/file%23frag/sha512/abc123",
},
{
name: "filename with malformed percent is path-escaped",
template: "https://example.com/$pkg/$filename/$hashtype/$hash",
pkg: "my-pkg",
filename: "file%zz",
hashType: "SHA512",
hash: "abc123",
expected: "https://example.com/my-pkg/file%25zz/sha512/abc123",
},
{
name: "packageName with slash is path-escaped",
template: "https://example.com/$pkg/$filename/$hashtype/$hash",
pkg: "foo/bar",
filename: "source.tar.gz",
hashType: "SHA512",
hash: "abc123",
expected: "https://example.com/foo%2Fbar/source.tar.gz/sha512/abc123",
},
{
name: "packageName with hash is path-escaped",
template: "https://example.com/$pkg/$filename/$hashtype/$hash",
pkg: "foo#bar",
filename: "source.tar.gz",
hashType: "SHA512",
hash: "abc123",
expected: "https://example.com/foo%23bar/source.tar.gz/sha512/abc123",
},
{
name: "hashType containing uppercase placeholder is caught after lowercasing",
template: "https://example.com/$pkg/$filename/$hashtype/$hash",
pkg: "my-pkg",
filename: "source.tar.gz",
hashType: "$PKG",
hash: "abc123",
expectedError: "ambiguous substitution",
},
{
name: "template without scheme is rejected",
template: "example.com/$pkg/$filename/$hashtype/$hash",
pkg: "my-pkg",
filename: "source.tar.gz",
hashType: "SHA512",
hash: "abc123",
expectedError: "missing scheme or host",
},
}
for _, testCase := range tests {
t.Run(testCase.name, func(t *testing.T) {
result, err := BuildLookasideURL(
testCase.template, testCase.pkg, testCase.filename, testCase.hashType, testCase.hash,
)
if testCase.expectedError != "" {
assert.ErrorContains(t, err, testCase.expectedError)
} else {
require.NoError(t, err)
assert.Equal(t, testCase.expected, result)
}
})
}
}
func TestBuildDistGitURL(t *testing.T) {
tests := []struct {
name string
template string
pkg string
expected string
expectedError string
}{
{
name: "standard template",
template: "https://src.example.com/rpms/$pkg.git",
pkg: "curl",
expected: "https://src.example.com/rpms/curl.git",
},
{
name: "packageName containing $pkg placeholder",
template: "https://src.example.com/rpms/$pkg.git",
pkg: "evil-$pkg-name",
expectedError: "ambiguous substitution",
},
{
name: "packageName with slash is path-escaped",
template: "https://src.example.com/rpms/$pkg.git",
pkg: "foo/bar",
expected: "https://src.example.com/rpms/foo%2Fbar.git",
},
{
name: "packageName with hash is path-escaped",
template: "https://src.example.com/rpms/$pkg.git",
pkg: "foo#bar",
expected: "https://src.example.com/rpms/foo%23bar.git",
},
{
name: "packageName with question mark is path-escaped",
template: "https://src.example.com/rpms/$pkg.git",
pkg: "foo?bar",
expected: "https://src.example.com/rpms/foo%3Fbar.git",
},
{
name: "packageName with malformed percent is path-escaped",
template: "https://src.example.com/rpms/$pkg.git",
pkg: "foo%zz",
expected: "https://src.example.com/rpms/foo%25zz.git",
},
{
name: "template without scheme is rejected",
template: "example.com/rpms/$pkg.git",
pkg: "curl",
expectedError: "missing scheme or host",
},
}
for _, testCase := range tests {
t.Run(testCase.name, func(t *testing.T) {
result, err := BuildDistGitURL(testCase.template, testCase.pkg)
if testCase.expectedError != "" {
assert.ErrorContains(t, err, testCase.expectedError)
} else {
require.NoError(t, err)
assert.Equal(t, testCase.expected, result)
}
})
}
}