Skip to content

Commit

Permalink
adds test to verify a downloaded artifact has expected sha256 filename
Browse files Browse the repository at this point in the history
Signed-off-by: Amit Singh <[email protected]>
  • Loading branch information
semmet95 authored and anthonydahanne committed Sep 14, 2023
1 parent 59a6098 commit aaf573f
Showing 1 changed file with 16 additions and 1 deletion.
17 changes: 16 additions & 1 deletion dependency_cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"crypto/sha256"
"encoding/hex"
"fmt"
"hash"
"io"
"net/http"
"os"
Expand Down Expand Up @@ -157,6 +158,7 @@ func testDependencyCache(t *testing.T, context spec.G, it spec.S) {
downloadPath string
dependency libpak.BuildpackDependency
dependencyCache libpak.DependencyCache
hasher hash.Hash
server *ghttp.Server
)

Expand All @@ -166,6 +168,8 @@ func testDependencyCache(t *testing.T, context spec.G, it spec.S) {
cachePath = t.TempDir()
Expect(err).NotTo(HaveOccurred())

hasher = sha256.New()

downloadPath = t.TempDir()
Expect(err).NotTo(HaveOccurred())

Expand Down Expand Up @@ -319,12 +323,23 @@ func testDependencyCache(t *testing.T, context spec.G, it spec.S) {
Expect(io.ReadAll(a)).To(Equal([]byte("alternate-fixture")))
})

it("sets downloaded file name to uri's sha256", func() {
server.AppendHandlers(ghttp.RespondWith(http.StatusOK, "test-fixture"))

hasher.Write([]byte(dependency.URI))

a, err := dependencyCache.Artifact(dependency)
Expect(err).NotTo(HaveOccurred())

Expect(io.ReadAll(a)).To(Equal([]byte("test-fixture")))
Expect(filepath.Base(a.Name())).To(Equal(hex.EncodeToString(hasher.Sum(nil))))
})

it("sets downloaded file name to uri's sha256 with empty SHA256 and query parameters in the uri", func() {
dependency.SHA256 = ""
dependency.URI = fmt.Sprintf("%s/test-path?param1=value1&param2=value2", server.URL())
server.AppendHandlers(ghttp.RespondWith(http.StatusOK, "alternate-fixture"))

hasher := sha256.New()
hasher.Write([]byte(dependency.URI))

a, err := dependencyCache.Artifact(dependency)
Expand Down

0 comments on commit aaf573f

Please sign in to comment.