Skip to content

Commit 28ed94b

Browse files
committed
add fust test for downloader
Signed-off-by: Adam Korczynski <[email protected]>
1 parent 43d66a5 commit 28ed94b

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed

pkg/downloader/fuzz_test.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package downloader
2+
3+
import (
4+
"context"
5+
"os"
6+
"path/filepath"
7+
"testing"
8+
9+
"github.com/opencontainers/go-digest"
10+
)
11+
12+
var a = digest.Algorithm("sha256")
13+
14+
func FuzzDownload(f *testing.F) {
15+
f.Fuzz(func(t *testing.T, fileContents []byte, checkDigest bool) {
16+
localFile := filepath.Join(t.TempDir(), "localFile")
17+
remoteFile := filepath.Join(t.TempDir(), "remoteFile")
18+
err := os.WriteFile(remoteFile, fileContents, 0o600)
19+
if err != nil {
20+
t.Fatal(err)
21+
}
22+
testLocalFileURL := "file://" + remoteFile
23+
if checkDigest {
24+
d := a.FromBytes(fileContents)
25+
_, _ = Download(context.Background(),
26+
localFile,
27+
testLocalFileURL,
28+
WithExpectedDigest(d))
29+
} else {
30+
_, _ = Download(context.Background(),
31+
localFile,
32+
testLocalFileURL)
33+
}
34+
})
35+
}

0 commit comments

Comments
 (0)