internal/pkg: compute http identifier from url
All checks were successful
Test / Create distribution (push) Successful in 44s
Test / Sandbox (push) Successful in 2m30s
Test / ShareFS (push) Successful in 3m40s
Test / Hpkg (push) Successful in 4m24s
Test / Sandbox (race detector) (push) Successful in 4m46s
Test / Hakurei (race detector) (push) Successful in 5m51s
Test / Hakurei (push) Successful in 2m28s
Test / Flake checks (push) Successful in 1m41s
All checks were successful
Test / Create distribution (push) Successful in 44s
Test / Sandbox (push) Successful in 2m30s
Test / ShareFS (push) Successful in 3m40s
Test / Hpkg (push) Successful in 4m24s
Test / Sandbox (race detector) (push) Successful in 4m46s
Test / Hakurei (race detector) (push) Successful in 5m51s
Test / Hakurei (push) Successful in 2m28s
Test / Flake checks (push) Successful in 1m41s
The previous implementation exposes arbitrary user input to the cache as an identifier, which is highly error-prone and can cause the cache to enter an inconsistent state if the user is not careful. This change replaces the implementation to compute identifier late, using url string as params. Signed-off-by: Ophestra <cat@gensokyo.uk>
This commit is contained in:
@@ -61,14 +61,14 @@ func TestFlatten(t *testing.T) {
|
|||||||
{Mode: fs.ModeDir | 0700, Path: "work"},
|
{Mode: fs.ModeDir | 0700, Path: "work"},
|
||||||
}, pkg.MustDecode("St9rlE-mGZ5gXwiv_hzQ_B8bZP-UUvSNmf4nHUZzCMOumb6hKnheZSe0dmnuc4Q2")},
|
}, pkg.MustDecode("St9rlE-mGZ5gXwiv_hzQ_B8bZP-UUvSNmf4nHUZzCMOumb6hKnheZSe0dmnuc4Q2")},
|
||||||
|
|
||||||
{"sample load or store", fstest.MapFS{
|
{"sample http get cure", fstest.MapFS{
|
||||||
".": {Mode: fs.ModeDir | 0700},
|
".": {Mode: fs.ModeDir | 0700},
|
||||||
|
|
||||||
"checksum": {Mode: fs.ModeDir | 0700},
|
"checksum": {Mode: fs.ModeDir | 0700},
|
||||||
"checksum/fLYGIMHgN1louE-JzITJZJo2SDniPu-IHBXubtvQWFO-hXnDVKNuscV7-zlyr5fU": {Mode: 0400, Data: []byte("\x7f\xe1\x69\xa2\xdd\x63\x96\x26\x83\x79\x61\x8b\xf0\x3f\xd5\x16\x9a\x39\x3a\xdb\xcf\xb1\xbc\x8d\x33\xff\x75\xee\x62\x56\xa9\xf0\x27\xac\x13\x94\x69")},
|
"checksum/fLYGIMHgN1louE-JzITJZJo2SDniPu-IHBXubtvQWFO-hXnDVKNuscV7-zlyr5fU": {Mode: 0400, Data: []byte("\x7f\xe1\x69\xa2\xdd\x63\x96\x26\x83\x79\x61\x8b\xf0\x3f\xd5\x16\x9a\x39\x3a\xdb\xcf\xb1\xbc\x8d\x33\xff\x75\xee\x62\x56\xa9\xf0\x27\xac\x13\x94\x69")},
|
||||||
|
|
||||||
"identifier": {Mode: fs.ModeDir | 0700},
|
"identifier": {Mode: fs.ModeDir | 0700},
|
||||||
"identifier/fLYGIMHgN1louE-JzITJZJo2SDniPu-IHBXubtvQWFO-hXnDVKNuscV7-zlyr5fU": {Mode: fs.ModeSymlink | 0777, Data: []byte("../checksum/fLYGIMHgN1louE-JzITJZJo2SDniPu-IHBXubtvQWFO-hXnDVKNuscV7-zlyr5fU")},
|
"identifier/NqVORkT6L9HX6Za7kT2zcibY10qFqBaxEjPiYFrBQX-ZFr3yxCzJxbKOP0zVjeWb": {Mode: fs.ModeSymlink | 0777, Data: []byte("../checksum/fLYGIMHgN1louE-JzITJZJo2SDniPu-IHBXubtvQWFO-hXnDVKNuscV7-zlyr5fU")},
|
||||||
|
|
||||||
"work": {Mode: fs.ModeDir | 0700},
|
"work": {Mode: fs.ModeDir | 0700},
|
||||||
}, []pkg.FlatEntry{
|
}, []pkg.FlatEntry{
|
||||||
@@ -78,10 +78,10 @@ func TestFlatten(t *testing.T) {
|
|||||||
{Mode: 0400, Path: "checksum/fLYGIMHgN1louE-JzITJZJo2SDniPu-IHBXubtvQWFO-hXnDVKNuscV7-zlyr5fU", Data: []byte("\x7f\xe1\x69\xa2\xdd\x63\x96\x26\x83\x79\x61\x8b\xf0\x3f\xd5\x16\x9a\x39\x3a\xdb\xcf\xb1\xbc\x8d\x33\xff\x75\xee\x62\x56\xa9\xf0\x27\xac\x13\x94\x69")},
|
{Mode: 0400, Path: "checksum/fLYGIMHgN1louE-JzITJZJo2SDniPu-IHBXubtvQWFO-hXnDVKNuscV7-zlyr5fU", Data: []byte("\x7f\xe1\x69\xa2\xdd\x63\x96\x26\x83\x79\x61\x8b\xf0\x3f\xd5\x16\x9a\x39\x3a\xdb\xcf\xb1\xbc\x8d\x33\xff\x75\xee\x62\x56\xa9\xf0\x27\xac\x13\x94\x69")},
|
||||||
|
|
||||||
{Mode: fs.ModeDir | 0700, Path: "identifier"},
|
{Mode: fs.ModeDir | 0700, Path: "identifier"},
|
||||||
{Mode: fs.ModeSymlink | 0777, Path: "identifier/fLYGIMHgN1louE-JzITJZJo2SDniPu-IHBXubtvQWFO-hXnDVKNuscV7-zlyr5fU", Data: []byte("../checksum/fLYGIMHgN1louE-JzITJZJo2SDniPu-IHBXubtvQWFO-hXnDVKNuscV7-zlyr5fU")},
|
{Mode: fs.ModeSymlink | 0777, Path: "identifier/NqVORkT6L9HX6Za7kT2zcibY10qFqBaxEjPiYFrBQX-ZFr3yxCzJxbKOP0zVjeWb", Data: []byte("../checksum/fLYGIMHgN1louE-JzITJZJo2SDniPu-IHBXubtvQWFO-hXnDVKNuscV7-zlyr5fU")},
|
||||||
|
|
||||||
{Mode: fs.ModeDir | 0700, Path: "work"},
|
{Mode: fs.ModeDir | 0700, Path: "work"},
|
||||||
}, pkg.MustDecode("4WHaMvRRcCac1uAyXnEklEd2YaNQBj6rXlfMntX9GgYLij3By1znv5QYPGJHYQIH")},
|
}, pkg.MustDecode("bqtn69RkV5E7V7GhhgCFjcvbxmaqrO8DywamM4Tyjf10F6EJBHjXiIa_tFRtF4iN")},
|
||||||
|
|
||||||
{"sample directory step simple", fstest.MapFS{
|
{"sample directory step simple", fstest.MapFS{
|
||||||
".": {Mode: fs.ModeDir | 0700},
|
".": {Mode: fs.ModeDir | 0700},
|
||||||
@@ -200,7 +200,7 @@ func TestFlatten(t *testing.T) {
|
|||||||
"checksum/yJlSb2A3jxaMLuKqwp1GwHOguAHddS9MjygF9ICEeegKfRvgLPdPmNh8mva47f8o/work": {Mode: fs.ModeDir | 0500},
|
"checksum/yJlSb2A3jxaMLuKqwp1GwHOguAHddS9MjygF9ICEeegKfRvgLPdPmNh8mva47f8o/work": {Mode: fs.ModeDir | 0500},
|
||||||
|
|
||||||
"identifier": {Mode: fs.ModeDir | 0700},
|
"identifier": {Mode: fs.ModeDir | 0700},
|
||||||
"identifier/NXP3807i7T1WaRom4ycvaIL8BzBU0awC7eW2_baV9hVS_NpRQMsJApnh0CPNNO8y": {Mode: fs.ModeSymlink | 0777, Data: []byte("../checksum/yJlSb2A3jxaMLuKqwp1GwHOguAHddS9MjygF9ICEeegKfRvgLPdPmNh8mva47f8o")},
|
"identifier/-P_1iw6yVq_letMHncqcExSE0bYcDhYI5OdY6b1wKASf-Corufvj__XTBUq2Qd2a": {Mode: fs.ModeSymlink | 0777, Data: []byte("../checksum/yJlSb2A3jxaMLuKqwp1GwHOguAHddS9MjygF9ICEeegKfRvgLPdPmNh8mva47f8o")},
|
||||||
|
|
||||||
"work": {Mode: fs.ModeDir | 0700},
|
"work": {Mode: fs.ModeDir | 0700},
|
||||||
}, []pkg.FlatEntry{
|
}, []pkg.FlatEntry{
|
||||||
@@ -220,10 +220,10 @@ func TestFlatten(t *testing.T) {
|
|||||||
{Mode: fs.ModeDir | 0500, Path: "checksum/yJlSb2A3jxaMLuKqwp1GwHOguAHddS9MjygF9ICEeegKfRvgLPdPmNh8mva47f8o/work"},
|
{Mode: fs.ModeDir | 0500, Path: "checksum/yJlSb2A3jxaMLuKqwp1GwHOguAHddS9MjygF9ICEeegKfRvgLPdPmNh8mva47f8o/work"},
|
||||||
|
|
||||||
{Mode: fs.ModeDir | 0700, Path: "identifier"},
|
{Mode: fs.ModeDir | 0700, Path: "identifier"},
|
||||||
{Mode: fs.ModeSymlink | 0777, Path: "identifier/NXP3807i7T1WaRom4ycvaIL8BzBU0awC7eW2_baV9hVS_NpRQMsJApnh0CPNNO8y", Data: []byte("../checksum/yJlSb2A3jxaMLuKqwp1GwHOguAHddS9MjygF9ICEeegKfRvgLPdPmNh8mva47f8o")},
|
{Mode: fs.ModeSymlink | 0777, Path: "identifier/-P_1iw6yVq_letMHncqcExSE0bYcDhYI5OdY6b1wKASf-Corufvj__XTBUq2Qd2a", Data: []byte("../checksum/yJlSb2A3jxaMLuKqwp1GwHOguAHddS9MjygF9ICEeegKfRvgLPdPmNh8mva47f8o")},
|
||||||
|
|
||||||
{Mode: fs.ModeDir | 0700, Path: "work"},
|
{Mode: fs.ModeDir | 0700, Path: "work"},
|
||||||
}, pkg.MustDecode("p1HdTOQhIeWzpXdZ45xo00H9CFeXNIvazxOhBAfExlhFO64zt7TUbxoLJ2eAL5oc")},
|
}, pkg.MustDecode("lmVlYEGNFkwGVpzzS8KYjGBVB6FCyPtk9ViX88zen0GgTKLgGqO6eFxb4dpcP6bR")},
|
||||||
}
|
}
|
||||||
for _, tc := range testCases {
|
for _, tc := range testCases {
|
||||||
t.Run(tc.name, func(t *testing.T) {
|
t.Run(tc.name, func(t *testing.T) {
|
||||||
|
|||||||
@@ -11,10 +11,14 @@ import (
|
|||||||
"hakurei.app/container/check"
|
"hakurei.app/container/check"
|
||||||
)
|
)
|
||||||
|
|
||||||
// An httpArtifact is an [Artifact] backed by an [http] request.
|
// An httpArtifact is an [Artifact] backed by a [http] url string. The method is
|
||||||
|
// hardcoded as [http.MethodGet]. Request body is not allowed because it cannot
|
||||||
|
// be deterministically represented by Params.
|
||||||
type httpArtifact struct {
|
type httpArtifact struct {
|
||||||
// Caller-supplied request.
|
// Caller-supplied context.
|
||||||
req *http.Request
|
ctx context.Context
|
||||||
|
// Caller-supplied url string.
|
||||||
|
url string
|
||||||
|
|
||||||
// Caller-supplied checksum of the response body. This is validated during
|
// Caller-supplied checksum of the response body. This is validated during
|
||||||
// curing and the first call to Data.
|
// curing and the first call to Data.
|
||||||
@@ -30,15 +34,6 @@ type httpArtifact struct {
|
|||||||
mu sync.Mutex
|
mu sync.Mutex
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewHTTP returns a new [File] backed by the supplied client and request. If
|
|
||||||
// c is nil, [http.DefaultClient] is used instead.
|
|
||||||
func NewHTTP(c *http.Client, req *http.Request, checksum Checksum) File {
|
|
||||||
if c == nil {
|
|
||||||
c = http.DefaultClient
|
|
||||||
}
|
|
||||||
return &httpArtifact{req: req, checksum: checksum, doFunc: c.Do}
|
|
||||||
}
|
|
||||||
|
|
||||||
// NewHTTPGet returns a new [File] backed by the supplied client. A GET request
|
// NewHTTPGet returns a new [File] backed by the supplied client. A GET request
|
||||||
// is set up for url. If c is nil, [http.DefaultClient] is used instead.
|
// is set up for url. If c is nil, [http.DefaultClient] is used instead.
|
||||||
func NewHTTPGet(
|
func NewHTTPGet(
|
||||||
@@ -46,24 +41,22 @@ func NewHTTPGet(
|
|||||||
c *http.Client,
|
c *http.Client,
|
||||||
url string,
|
url string,
|
||||||
checksum Checksum,
|
checksum Checksum,
|
||||||
) (File, error) {
|
) File {
|
||||||
req, err := http.NewRequestWithContext(ctx, http.MethodGet, url, nil)
|
if ctx == nil {
|
||||||
if err != nil {
|
ctx = context.Background()
|
||||||
return nil, err
|
|
||||||
}
|
}
|
||||||
return NewHTTP(c, req, checksum), nil
|
if c == nil {
|
||||||
|
c = http.DefaultClient
|
||||||
|
}
|
||||||
|
return &httpArtifact{ctx: ctx, url: url, checksum: checksum, doFunc: c.Do}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Kind returns the hardcoded [Kind] constant.
|
// Kind returns the hardcoded [Kind] constant.
|
||||||
func (a *httpArtifact) Kind() Kind { return KindHTTP }
|
func (a *httpArtifact) Kind() Kind { return KindHTTPGet }
|
||||||
|
|
||||||
// ID returns the caller-supplied hash of the response body.
|
// Params returns the backing url string. Context is not represented as it does
|
||||||
func (a *httpArtifact) ID() ID { return a.checksum }
|
// not affect [Cache.Cure] outcome.
|
||||||
|
func (a *httpArtifact) Params() []byte { return []byte(a.url) }
|
||||||
// Params is unreachable.
|
|
||||||
func (a *httpArtifact) Params() []byte {
|
|
||||||
panic("not implemented")
|
|
||||||
}
|
|
||||||
|
|
||||||
// Dependencies returns a nil slice.
|
// Dependencies returns a nil slice.
|
||||||
func (a *httpArtifact) Dependencies() []Artifact { return nil }
|
func (a *httpArtifact) Dependencies() []Artifact { return nil }
|
||||||
@@ -82,8 +75,14 @@ func (e ResponseStatusError) Error() string {
|
|||||||
// do sends the caller-supplied request on the caller-supplied [http.Client]
|
// do sends the caller-supplied request on the caller-supplied [http.Client]
|
||||||
// and reads its response body to EOF and returns the resulting bytes.
|
// and reads its response body to EOF and returns the resulting bytes.
|
||||||
func (a *httpArtifact) do() (data []byte, err error) {
|
func (a *httpArtifact) do() (data []byte, err error) {
|
||||||
|
var req *http.Request
|
||||||
|
req, err = http.NewRequestWithContext(a.ctx, http.MethodGet, a.url, nil)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
var resp *http.Response
|
var resp *http.Response
|
||||||
if resp, err = a.doFunc(a.req); err != nil {
|
if resp, err = a.doFunc(req); err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -2,7 +2,6 @@ package pkg_test
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"crypto/sha512"
|
"crypto/sha512"
|
||||||
"encoding/base64"
|
|
||||||
"net/http"
|
"net/http"
|
||||||
"reflect"
|
"reflect"
|
||||||
"testing"
|
"testing"
|
||||||
@@ -12,7 +11,7 @@ import (
|
|||||||
"hakurei.app/internal/pkg"
|
"hakurei.app/internal/pkg"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestHTTP(t *testing.T) {
|
func TestHTTPGet(t *testing.T) {
|
||||||
t.Parallel()
|
t.Parallel()
|
||||||
|
|
||||||
const testdata = "\x7f\xe1\x69\xa2\xdd\x63\x96\x26\x83\x79\x61\x8b\xf0\x3f\xd5\x16\x9a\x39\x3a\xdb\xcf\xb1\xbc\x8d\x33\xff\x75\xee\x62\x56\xa9\xf0\x27\xac\x13\x94\x69"
|
const testdata = "\x7f\xe1\x69\xa2\xdd\x63\x96\x26\x83\x79\x61\x8b\xf0\x3f\xd5\x16\x9a\x39\x3a\xdb\xcf\xb1\xbc\x8d\x33\xff\x75\xee\x62\x56\xa9\xf0\x27\xac\x13\x94\x69"
|
||||||
@@ -23,8 +22,6 @@ func TestHTTP(t *testing.T) {
|
|||||||
return (pkg.Checksum)(h.Sum(nil))
|
return (pkg.Checksum)(h.Sum(nil))
|
||||||
}()
|
}()
|
||||||
|
|
||||||
testdataChecksumString := base64.URLEncoding.EncodeToString(testdataChecksum[:])
|
|
||||||
|
|
||||||
var transport http.Transport
|
var transport http.Transport
|
||||||
client := http.Client{Transport: &transport}
|
client := http.Client{Transport: &transport}
|
||||||
transport.RegisterProtocol("file", http.NewFileTransportFS(fstest.MapFS{
|
transport.RegisterProtocol("file", http.NewFileTransportFS(fstest.MapFS{
|
||||||
@@ -33,75 +30,66 @@ func TestHTTP(t *testing.T) {
|
|||||||
|
|
||||||
checkWithCache(t, []cacheTestCase{
|
checkWithCache(t, []cacheTestCase{
|
||||||
{"direct", nil, func(t *testing.T, base *check.Absolute, c *pkg.Cache) {
|
{"direct", nil, func(t *testing.T, base *check.Absolute, c *pkg.Cache) {
|
||||||
var got []byte
|
f := pkg.NewHTTPGet(
|
||||||
if f, err := pkg.NewHTTPGet(
|
|
||||||
t.Context(),
|
t.Context(),
|
||||||
&client,
|
&client,
|
||||||
"file:///testdata",
|
"file:///testdata",
|
||||||
testdataChecksum,
|
testdataChecksum,
|
||||||
); err != nil {
|
)
|
||||||
t.Fatalf("NewHTTPGet: error = %v", err)
|
wantIdent := pkg.KindHTTPGet.Ident([]byte("file:///testdata"))
|
||||||
} else if got, err = f.Data(); err != nil {
|
if got, err := f.Data(); err != nil {
|
||||||
t.Fatalf("Data: error = %v", err)
|
t.Fatalf("Data: error = %v", err)
|
||||||
} else if string(got) != testdata {
|
} else if string(got) != testdata {
|
||||||
t.Fatalf("Data: %x, want %x", got, testdata)
|
t.Fatalf("Data: %x, want %x", got, testdata)
|
||||||
} else if gotIdent := pkg.Ident(f); gotIdent != testdataChecksum {
|
} else if gotIdent := pkg.Ident(f); gotIdent != wantIdent {
|
||||||
t.Fatalf("Ident: %x, want %x", gotIdent, testdataChecksum)
|
t.Fatalf("Ident: %s, want %s", pkg.Encode(gotIdent), pkg.Encode(wantIdent))
|
||||||
}
|
}
|
||||||
|
|
||||||
// check direct validation
|
// check direct validation
|
||||||
wantErrMismatch := &pkg.ChecksumMismatchError{
|
f = pkg.NewHTTPGet(
|
||||||
Got: testdataChecksum,
|
|
||||||
}
|
|
||||||
if f, err := pkg.NewHTTPGet(
|
|
||||||
t.Context(),
|
t.Context(),
|
||||||
&client,
|
&client,
|
||||||
"file:///testdata",
|
"file:///testdata",
|
||||||
pkg.Checksum{},
|
pkg.Checksum{},
|
||||||
); err != nil {
|
)
|
||||||
t.Fatalf("NewHTTPGet: error = %v", err)
|
wantErrMismatch := &pkg.ChecksumMismatchError{
|
||||||
} else if _, err = f.Data(); !reflect.DeepEqual(err, wantErrMismatch) {
|
Got: testdataChecksum,
|
||||||
|
}
|
||||||
|
if _, err := f.Data(); !reflect.DeepEqual(err, wantErrMismatch) {
|
||||||
t.Fatalf("Data: error = %#v, want %#v", err, wantErrMismatch)
|
t.Fatalf("Data: error = %#v, want %#v", err, wantErrMismatch)
|
||||||
} else if gotIdent := pkg.Ident(f); gotIdent != (pkg.Checksum{}) {
|
} else if gotIdent := pkg.Ident(f); gotIdent != wantIdent {
|
||||||
t.Fatalf("Ident: %x, want %x", gotIdent, pkg.Checksum{})
|
t.Fatalf("Ident: %s, want %s", pkg.Encode(gotIdent), pkg.Encode(wantIdent))
|
||||||
}
|
}
|
||||||
|
|
||||||
// check direct response error
|
// check direct response error
|
||||||
wantErrNotFound := pkg.ResponseStatusError(http.StatusNotFound)
|
f = pkg.NewHTTPGet(
|
||||||
if f, err := pkg.NewHTTPGet(
|
|
||||||
t.Context(),
|
t.Context(),
|
||||||
&client,
|
&client,
|
||||||
"file:///nonexistent",
|
"file:///nonexistent",
|
||||||
pkg.Checksum{},
|
pkg.Checksum{},
|
||||||
); err != nil {
|
)
|
||||||
t.Fatalf("NewHTTPGet: error = %v", err)
|
wantIdentNonexistent := pkg.KindHTTPGet.Ident([]byte("file:///nonexistent"))
|
||||||
} else if _, err = f.Data(); !reflect.DeepEqual(err, wantErrNotFound) {
|
wantErrNotFound := pkg.ResponseStatusError(http.StatusNotFound)
|
||||||
|
if _, err := f.Data(); !reflect.DeepEqual(err, wantErrNotFound) {
|
||||||
t.Fatalf("Data: error = %#v, want %#v", err, wantErrNotFound)
|
t.Fatalf("Data: error = %#v, want %#v", err, wantErrNotFound)
|
||||||
} else if gotIdent := pkg.Ident(f); gotIdent != (pkg.Checksum{}) {
|
} else if gotIdent := pkg.Ident(f); gotIdent != wantIdentNonexistent {
|
||||||
t.Fatalf("Ident: %x, want %x", gotIdent, pkg.Checksum{})
|
t.Fatalf("Ident: %s, want %s", pkg.Encode(gotIdent), pkg.Encode(wantIdentNonexistent))
|
||||||
}
|
}
|
||||||
}, pkg.MustDecode("E4vEZKhCcL2gPZ2Tt59FS3lDng-d_2SKa2i5G_RbDfwGn6EemptFaGLPUDiOa94C")},
|
}, pkg.MustDecode("E4vEZKhCcL2gPZ2Tt59FS3lDng-d_2SKa2i5G_RbDfwGn6EemptFaGLPUDiOa94C")},
|
||||||
|
|
||||||
{"cure", nil, func(t *testing.T, base *check.Absolute, c *pkg.Cache) {
|
{"cure", nil, func(t *testing.T, base *check.Absolute, c *pkg.Cache) {
|
||||||
f, err := pkg.NewHTTPGet(
|
f := pkg.NewHTTPGet(
|
||||||
t.Context(),
|
t.Context(),
|
||||||
&client,
|
&client,
|
||||||
"file:///testdata",
|
"file:///testdata",
|
||||||
testdataChecksum,
|
testdataChecksum,
|
||||||
)
|
)
|
||||||
if err != nil {
|
wantIdent := pkg.KindHTTPGet.Ident([]byte("file:///testdata"))
|
||||||
t.Fatalf("NewHTTPGet: error = %v", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
wantPathname := base.Append(
|
wantPathname := base.Append(
|
||||||
"identifier",
|
"identifier",
|
||||||
testdataChecksumString,
|
pkg.Encode(wantIdent),
|
||||||
)
|
)
|
||||||
var (
|
if pathname, checksum, err := c.Cure(f); err != nil {
|
||||||
pathname *check.Absolute
|
|
||||||
checksum pkg.Checksum
|
|
||||||
)
|
|
||||||
if pathname, checksum, err = c.Cure(f); err != nil {
|
|
||||||
t.Fatalf("Cure: error = %v", err)
|
t.Fatalf("Cure: error = %v", err)
|
||||||
} else if !pathname.Is(wantPathname) {
|
} else if !pathname.Is(wantPathname) {
|
||||||
t.Fatalf("Cure: %q, want %q", pathname, wantPathname)
|
t.Fatalf("Cure: %q, want %q", pathname, wantPathname)
|
||||||
@@ -109,45 +97,43 @@ func TestHTTP(t *testing.T) {
|
|||||||
t.Fatalf("Cure: %x, want %x", checksum, testdataChecksum)
|
t.Fatalf("Cure: %x, want %x", checksum, testdataChecksum)
|
||||||
}
|
}
|
||||||
|
|
||||||
var got []byte
|
if got, err := f.Data(); err != nil {
|
||||||
if got, err = f.Data(); err != nil {
|
|
||||||
t.Fatalf("Data: error = %v", err)
|
t.Fatalf("Data: error = %v", err)
|
||||||
} else if string(got) != testdata {
|
} else if string(got) != testdata {
|
||||||
t.Fatalf("Data: %x, want %x", got, testdata)
|
t.Fatalf("Data: %x, want %x", got, testdata)
|
||||||
} else if gotIdent := pkg.Ident(f); gotIdent != testdataChecksum {
|
} else if gotIdent := pkg.Ident(f); gotIdent != wantIdent {
|
||||||
t.Fatalf("Ident: %x, want %x", gotIdent, testdataChecksum)
|
t.Fatalf("Ident: %s, want %s", pkg.Encode(gotIdent), pkg.Encode(wantIdent))
|
||||||
}
|
}
|
||||||
|
|
||||||
// check load from cache
|
// check load from cache
|
||||||
if f, err = pkg.NewHTTPGet(
|
f = pkg.NewHTTPGet(
|
||||||
t.Context(),
|
t.Context(),
|
||||||
&client,
|
&client,
|
||||||
"file:///testdata",
|
"file:///testdata",
|
||||||
testdataChecksum,
|
testdataChecksum,
|
||||||
); err != nil {
|
)
|
||||||
t.Fatalf("NewHTTPGet: error = %v", err)
|
if got, err := f.Data(); err != nil {
|
||||||
} else if got, err = f.Data(); err != nil {
|
|
||||||
t.Fatalf("Data: error = %v", err)
|
t.Fatalf("Data: error = %v", err)
|
||||||
} else if string(got) != testdata {
|
} else if string(got) != testdata {
|
||||||
t.Fatalf("Data: %x, want %x", got, testdata)
|
t.Fatalf("Data: %x, want %x", got, testdata)
|
||||||
} else if gotIdent := pkg.Ident(f); gotIdent != testdataChecksum {
|
} else if gotIdent := pkg.Ident(f); gotIdent != wantIdent {
|
||||||
t.Fatalf("Ident: %x, want %x", gotIdent, testdataChecksum)
|
t.Fatalf("Ident: %s, want %s", pkg.Encode(gotIdent), pkg.Encode(wantIdent))
|
||||||
}
|
}
|
||||||
|
|
||||||
// check error passthrough
|
// check error passthrough
|
||||||
wantErrNotFound := pkg.ResponseStatusError(http.StatusNotFound)
|
f = pkg.NewHTTPGet(
|
||||||
if f, err = pkg.NewHTTPGet(
|
|
||||||
t.Context(),
|
t.Context(),
|
||||||
&client,
|
&client,
|
||||||
"file:///nonexistent",
|
"file:///nonexistent",
|
||||||
pkg.Checksum{},
|
pkg.Checksum{},
|
||||||
); err != nil {
|
)
|
||||||
t.Fatalf("NewHTTPGet: error = %v", err)
|
wantIdentNonexistent := pkg.KindHTTPGet.Ident([]byte("file:///nonexistent"))
|
||||||
} else if _, _, err = c.Cure(f); !reflect.DeepEqual(err, wantErrNotFound) {
|
wantErrNotFound := pkg.ResponseStatusError(http.StatusNotFound)
|
||||||
|
if _, _, err := c.Cure(f); !reflect.DeepEqual(err, wantErrNotFound) {
|
||||||
t.Fatalf("Pathname: error = %#v, want %#v", err, wantErrNotFound)
|
t.Fatalf("Pathname: error = %#v, want %#v", err, wantErrNotFound)
|
||||||
} else if gotIdent := pkg.Ident(f); gotIdent != (pkg.Checksum{}) {
|
} else if gotIdent := pkg.Ident(f); gotIdent != wantIdentNonexistent {
|
||||||
t.Fatalf("Ident: %x, want %x", gotIdent, pkg.Checksum{})
|
t.Fatalf("Ident: %s, want %s", pkg.Encode(gotIdent), pkg.Encode(wantIdentNonexistent))
|
||||||
}
|
}
|
||||||
}, pkg.MustDecode("4WHaMvRRcCac1uAyXnEklEd2YaNQBj6rXlfMntX9GgYLij3By1znv5QYPGJHYQIH")},
|
}, pkg.MustDecode("bqtn69RkV5E7V7GhhgCFjcvbxmaqrO8DywamM4Tyjf10F6EJBHjXiIa_tFRtF4iN")},
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -141,8 +141,8 @@ func Ident(a Artifact) ID {
|
|||||||
type Kind uint64
|
type Kind uint64
|
||||||
|
|
||||||
const (
|
const (
|
||||||
// KindHTTP is the kind of [Artifact] returned by [NewHTTP].
|
// KindHTTPGet is the kind of [Artifact] returned by [NewHTTPGet].
|
||||||
KindHTTP Kind = iota
|
KindHTTPGet Kind = iota
|
||||||
// KindTar is the kind of artifact returned by [NewTar].
|
// KindTar is the kind of artifact returned by [NewTar].
|
||||||
KindTar
|
KindTar
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -279,28 +279,28 @@ func TestCache(t *testing.T) {
|
|||||||
|
|
||||||
cureMany(t, c, []cureStep{
|
cureMany(t, c, []cureStep{
|
||||||
{"initial file", newStubFile(
|
{"initial file", newStubFile(
|
||||||
pkg.KindHTTP,
|
pkg.KindHTTPGet,
|
||||||
identifier,
|
identifier,
|
||||||
&testdataChecksum,
|
&testdataChecksum,
|
||||||
[]byte(testdata), nil,
|
[]byte(testdata), nil,
|
||||||
), wantPathname, testdataChecksum, nil},
|
), wantPathname, testdataChecksum, nil},
|
||||||
|
|
||||||
{"identical content", newStubFile(
|
{"identical content", newStubFile(
|
||||||
pkg.KindHTTP,
|
pkg.KindHTTPGet,
|
||||||
identifier0,
|
identifier0,
|
||||||
&testdataChecksum,
|
&testdataChecksum,
|
||||||
[]byte(testdata), nil,
|
[]byte(testdata), nil,
|
||||||
), wantPathname0, testdataChecksum, nil},
|
), wantPathname0, testdataChecksum, nil},
|
||||||
|
|
||||||
{"existing entry", newStubFile(
|
{"existing entry", newStubFile(
|
||||||
pkg.KindHTTP,
|
pkg.KindHTTPGet,
|
||||||
identifier,
|
identifier,
|
||||||
&testdataChecksum,
|
&testdataChecksum,
|
||||||
[]byte(testdata), nil,
|
[]byte(testdata), nil,
|
||||||
), wantPathname, testdataChecksum, nil},
|
), wantPathname, testdataChecksum, nil},
|
||||||
|
|
||||||
{"checksum mismatch", newStubFile(
|
{"checksum mismatch", newStubFile(
|
||||||
pkg.KindHTTP,
|
pkg.KindHTTPGet,
|
||||||
pkg.ID{0xff, 0},
|
pkg.ID{0xff, 0},
|
||||||
new(pkg.Checksum),
|
new(pkg.Checksum),
|
||||||
[]byte(testdata), nil,
|
[]byte(testdata), nil,
|
||||||
@@ -309,7 +309,7 @@ func TestCache(t *testing.T) {
|
|||||||
}},
|
}},
|
||||||
|
|
||||||
{"store without validation", newStubFile(
|
{"store without validation", newStubFile(
|
||||||
pkg.KindHTTP,
|
pkg.KindHTTPGet,
|
||||||
pkg.MustDecode("vsAhtPNo4waRNOASwrQwcIPTqb3SBuJOXw2G4T1mNmVZM-wrQTRllmgXqcIIoRcX"),
|
pkg.MustDecode("vsAhtPNo4waRNOASwrQwcIPTqb3SBuJOXw2G4T1mNmVZM-wrQTRllmgXqcIIoRcX"),
|
||||||
nil,
|
nil,
|
||||||
[]byte{0}, nil,
|
[]byte{0}, nil,
|
||||||
@@ -328,14 +328,14 @@ func TestCache(t *testing.T) {
|
|||||||
}, nil},
|
}, nil},
|
||||||
|
|
||||||
{"error passthrough", newStubFile(
|
{"error passthrough", newStubFile(
|
||||||
pkg.KindHTTP,
|
pkg.KindHTTPGet,
|
||||||
pkg.ID{0xff, 1},
|
pkg.ID{0xff, 1},
|
||||||
nil,
|
nil,
|
||||||
nil, stub.UniqueError(0xcafe),
|
nil, stub.UniqueError(0xcafe),
|
||||||
), nil, pkg.Checksum{}, stub.UniqueError(0xcafe)},
|
), nil, pkg.Checksum{}, stub.UniqueError(0xcafe)},
|
||||||
|
|
||||||
{"error caching", newStubFile(
|
{"error caching", newStubFile(
|
||||||
pkg.KindHTTP,
|
pkg.KindHTTPGet,
|
||||||
pkg.ID{0xff, 1},
|
pkg.ID{0xff, 1},
|
||||||
nil,
|
nil,
|
||||||
nil, nil,
|
nil, nil,
|
||||||
@@ -357,7 +357,7 @@ func TestCache(t *testing.T) {
|
|||||||
}, wantPathname, testdataChecksum, nil},
|
}, wantPathname, testdataChecksum, nil},
|
||||||
|
|
||||||
{"cache miss checksum match", newStubFile(
|
{"cache miss checksum match", newStubFile(
|
||||||
pkg.KindHTTP,
|
pkg.KindHTTPGet,
|
||||||
testdataChecksum,
|
testdataChecksum,
|
||||||
nil,
|
nil,
|
||||||
[]byte(testdata),
|
[]byte(testdata),
|
||||||
@@ -493,7 +493,7 @@ func TestCache(t *testing.T) {
|
|||||||
}},
|
}},
|
||||||
|
|
||||||
{"cache hit bad type", newStubFile(
|
{"cache hit bad type", newStubFile(
|
||||||
pkg.KindHTTP,
|
pkg.KindHTTPGet,
|
||||||
pkg.ID{0xff, 2},
|
pkg.ID{0xff, 2},
|
||||||
&wantChecksum,
|
&wantChecksum,
|
||||||
[]byte(testdata), nil,
|
[]byte(testdata), nil,
|
||||||
@@ -571,7 +571,7 @@ func TestCache(t *testing.T) {
|
|||||||
// check cache activity while a cure is blocking
|
// check cache activity while a cure is blocking
|
||||||
cureMany(t, c, []cureStep{
|
cureMany(t, c, []cureStep{
|
||||||
{"error passthrough", newStubFile(
|
{"error passthrough", newStubFile(
|
||||||
pkg.KindHTTP,
|
pkg.KindHTTPGet,
|
||||||
pkg.ID{0xff, 1},
|
pkg.ID{0xff, 1},
|
||||||
nil,
|
nil,
|
||||||
nil, stub.UniqueError(0xbad),
|
nil, stub.UniqueError(0xbad),
|
||||||
|
|||||||
@@ -43,12 +43,8 @@ func NewHTTPGetTar(
|
|||||||
url string,
|
url string,
|
||||||
checksum Checksum,
|
checksum Checksum,
|
||||||
compression uint64,
|
compression uint64,
|
||||||
) (Artifact, error) {
|
) Artifact {
|
||||||
f, err := NewHTTPGet(ctx, hc, url, checksum)
|
return NewTar(NewHTTPGet(ctx, hc, url, checksum), compression)
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
return NewTar(f, compression), nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Kind returns the hardcoded [Kind] constant.
|
// Kind returns the hardcoded [Kind] constant.
|
||||||
|
|||||||
@@ -74,12 +74,13 @@ func TestTar(t *testing.T) {
|
|||||||
h := sha512.New384()
|
h := sha512.New384()
|
||||||
h.Write([]byte{byte(pkg.KindTar), 0, 0, 0, 0, 0, 0, 0})
|
h.Write([]byte{byte(pkg.KindTar), 0, 0, 0, 0, 0, 0, 0})
|
||||||
h.Write([]byte{pkg.TarGzip, 0, 0, 0, 0, 0, 0, 0})
|
h.Write([]byte{pkg.TarGzip, 0, 0, 0, 0, 0, 0, 0})
|
||||||
h.Write([]byte{byte(pkg.KindHTTP), 0, 0, 0, 0, 0, 0, 0})
|
h.Write([]byte{byte(pkg.KindHTTPGet), 0, 0, 0, 0, 0, 0, 0})
|
||||||
h.Write(testdataChecksum[:])
|
httpIdent := pkg.KindHTTPGet.Ident([]byte("file:///testdata"))
|
||||||
|
h.Write(httpIdent[:])
|
||||||
return pkg.ID(h.Sum(nil))
|
return pkg.ID(h.Sum(nil))
|
||||||
}()
|
}()
|
||||||
|
|
||||||
a, err := pkg.NewHTTPGetTar(
|
a := pkg.NewHTTPGetTar(
|
||||||
t.Context(),
|
t.Context(),
|
||||||
&client,
|
&client,
|
||||||
"file:///testdata",
|
"file:///testdata",
|
||||||
@@ -87,16 +88,10 @@ func TestTar(t *testing.T) {
|
|||||||
pkg.TarGzip,
|
pkg.TarGzip,
|
||||||
)
|
)
|
||||||
|
|
||||||
if err != nil {
|
if id := pkg.Ident(a); id != wantIdent {
|
||||||
t.Fatalf("NewHTTPGetTar: error = %v", err)
|
|
||||||
} else if id := pkg.Ident(a); id != wantIdent {
|
|
||||||
t.Fatalf("Ident: %s, want %s", pkg.Encode(id), pkg.Encode(wantIdent))
|
t.Fatalf("Ident: %s, want %s", pkg.Encode(id), pkg.Encode(wantIdent))
|
||||||
}
|
}
|
||||||
|
|
||||||
var (
|
|
||||||
pathname *check.Absolute
|
|
||||||
checksum pkg.Checksum
|
|
||||||
)
|
|
||||||
wantPathname := base.Append(
|
wantPathname := base.Append(
|
||||||
"identifier",
|
"identifier",
|
||||||
pkg.Encode(wantIdent),
|
pkg.Encode(wantIdent),
|
||||||
@@ -104,7 +99,7 @@ func TestTar(t *testing.T) {
|
|||||||
wantChecksum := pkg.MustDecode(
|
wantChecksum := pkg.MustDecode(
|
||||||
"yJlSb2A3jxaMLuKqwp1GwHOguAHddS9MjygF9ICEeegKfRvgLPdPmNh8mva47f8o",
|
"yJlSb2A3jxaMLuKqwp1GwHOguAHddS9MjygF9ICEeegKfRvgLPdPmNh8mva47f8o",
|
||||||
)
|
)
|
||||||
if pathname, checksum, err = c.Cure(a); err != nil {
|
if pathname, checksum, err := c.Cure(a); err != nil {
|
||||||
t.Fatalf("Cure: error = %v", err)
|
t.Fatalf("Cure: error = %v", err)
|
||||||
} else if !pathname.Is(wantPathname) {
|
} else if !pathname.Is(wantPathname) {
|
||||||
t.Fatalf("Cure: %q, want %q", pathname, wantPathname)
|
t.Fatalf("Cure: %q, want %q", pathname, wantPathname)
|
||||||
@@ -114,6 +109,6 @@ func TestTar(t *testing.T) {
|
|||||||
Want: wantChecksum,
|
Want: wantChecksum,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}, pkg.MustDecode("p1HdTOQhIeWzpXdZ45xo00H9CFeXNIvazxOhBAfExlhFO64zt7TUbxoLJ2eAL5oc")},
|
}, pkg.MustDecode("lmVlYEGNFkwGVpzzS8KYjGBVB6FCyPtk9ViX88zen0GgTKLgGqO6eFxb4dpcP6bR")},
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user