45 lines
1.5 KiB
Go
45 lines
1.5 KiB
Go
package artifacts
|
|
|
|
import (
|
|
"context"
|
|
"crypto/sha256"
|
|
"encoding/hex"
|
|
"errors"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
// A take-in reads a templates archive by its reference, and only bytes of the digest the reference names.
|
|
func TestGetBlobReadsOnlyTheBytesOfItsDigest(t *testing.T) {
|
|
good := []byte("the archive")
|
|
sum := sha256.Sum256(good)
|
|
digest := "sha256:" + hex.EncodeToString(sum[:])
|
|
liar := "sha256:" + strings.Repeat("0", 64)
|
|
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
switch {
|
|
case strings.HasSuffix(r.URL.Path, "/x/files/blobs/"+digest), strings.HasSuffix(r.URL.Path, "/x/files/blobs/"+liar):
|
|
_, _ = w.Write(good)
|
|
default:
|
|
w.WriteHeader(http.StatusNotFound)
|
|
}
|
|
}))
|
|
defer server.Close()
|
|
s := Store{Address: strings.TrimPrefix(server.URL, "http://")}
|
|
ctx := context.Background()
|
|
body, err := s.GetBlob(ctx, "artifact-store://x/files/blobs/"+digest)
|
|
if err != nil || string(body) != string(good) {
|
|
t.Fatalf("the archive: %q %v", body, err)
|
|
}
|
|
if _, err := s.GetBlob(ctx, "artifact-store://x/files/blobs/"+liar); err == nil || !strings.Contains(err.Error(), "refused") {
|
|
t.Fatalf("bytes of another digest are read: %v", err)
|
|
}
|
|
if _, err := s.GetBlob(ctx, "artifact-store://x/other/blobs/"+digest); !errors.Is(err, Gone) {
|
|
t.Fatalf("a blob the store does not hold: %v", err)
|
|
}
|
|
if _, err := s.GetBlob(ctx, "registry.example/x@"+digest); !errors.Is(err, ErrNotOurs) {
|
|
t.Fatalf("a reference outside the store: %v", err)
|
|
}
|
|
}
|