| 1 | |
| 2 | |
| 3 | |
| 4 | package downloader |
| 5 | |
| 6 | import ( |
| 7 | "os" |
| 8 | "path/filepath" |
| 9 | "testing" |
| 10 | |
| 11 | "github.com/opencontainers/go-digest" |
| 12 | "gotest.tools/v3/assert" |
| 13 | ) |
| 14 | |
| 15 | var algorithm = digest.Algorithm("sha256") |
| 16 | |
| 17 | func FuzzDownload(f *testing.F) { |
| 18 | f.Fuzz(func(t *testing.T, fileContents []byte, checkDigest bool) { |
| 19 | localFile := filepath.Join(t.TempDir(), "localFile") |
| 20 | remoteFile := filepath.Join(t.TempDir(), "remoteFile") |
| 21 | err := os.WriteFile(remoteFile, fileContents, 0o600) |
| 22 | assert.NilError(t, err) |
| 23 | testLocalFileURL := "file://" + remoteFile |
| 24 | if checkDigest { |
| 25 | d := algorithm.FromBytes(fileContents) |
| 26 | _, _ = Download(t.Context(), localFile, testLocalFileURL, WithExpectedDigest(d)) |
| 27 | } else { |
| 28 | _, _ = Download(t.Context(), localFile, testLocalFileURL) |
| 29 | } |
| 30 | }) |
| 31 | } |