Files
hakurei/internal/pkg/tar_test.go
Ophestra 4da26681b5 internal/pkg: compute http identifier from url
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>
2026-01-05 00:43:21 +09:00

115 lines
3.6 KiB
Go

package pkg_test
import (
"archive/tar"
"bytes"
"compress/gzip"
"crypto/sha512"
"io/fs"
"net/http"
"testing"
"testing/fstest"
"hakurei.app/container/check"
"hakurei.app/internal/pkg"
)
func TestTar(t *testing.T) {
t.Parallel()
testdataFsys := fstest.MapFS{
".": {Mode: fs.ModeDir | 0700},
"checksum": {Mode: fs.ModeDir | 0700},
"checksum/1TL00Qb8dcqayX7wTO8WNaraHvY6b-KCsctLDTrb64QBCmxj_-byK1HdIUwMaFEP": {Mode: fs.ModeDir | 0700},
"checksum/1TL00Qb8dcqayX7wTO8WNaraHvY6b-KCsctLDTrb64QBCmxj_-byK1HdIUwMaFEP/check": {Mode: 0400, Data: []byte{0, 0}},
"checksum/1TL00Qb8dcqayX7wTO8WNaraHvY6b-KCsctLDTrb64QBCmxj_-byK1HdIUwMaFEP/lib": {Mode: fs.ModeDir | 0700},
"checksum/1TL00Qb8dcqayX7wTO8WNaraHvY6b-KCsctLDTrb64QBCmxj_-byK1HdIUwMaFEP/lib/pkgconfig": {Mode: fs.ModeDir | 0700},
"checksum/1TL00Qb8dcqayX7wTO8WNaraHvY6b-KCsctLDTrb64QBCmxj_-byK1HdIUwMaFEP/lib/libedac.so": {Mode: 01000000777, Data: []byte("/proc/nonexistent/libedac.so")},
"identifier": {Mode: fs.ModeDir | 0700},
"identifier/HnySzeLQvSBZuTUcvfmLEX_OmH4yJWWH788NxuLuv7kVn8_uPM6Ks4rqFWM2NZJY": {Mode: 01000000777, Data: []byte("../checksum/1TL00Qb8dcqayX7wTO8WNaraHvY6b-KCsctLDTrb64QBCmxj_-byK1HdIUwMaFEP")},
"identifier/Zx5ZG9BAwegNT3zQwCySuI2ktCXxNgxirkGLFjW4FW06PtojYVaCdtEw8yuntPLa": {Mode: 01000000777, Data: []byte("../checksum/1TL00Qb8dcqayX7wTO8WNaraHvY6b-KCsctLDTrb64QBCmxj_-byK1HdIUwMaFEP")},
"work": {Mode: fs.ModeDir | 0700},
}
var testdata string
{
var buf bytes.Buffer
w := tar.NewWriter(&buf)
if err := w.AddFS(testdataFsys); err != nil {
t.Fatalf("AddFS: error = %v", err)
}
if err := w.Close(); err != nil {
t.Fatalf("Close: error = %v", err)
}
var zbuf bytes.Buffer
gw := gzip.NewWriter(&zbuf)
if _, err := gw.Write(buf.Bytes()); err != nil {
t.Fatalf("Write: error = %v", err)
}
if err := gw.Close(); err != nil {
t.Fatalf("Close: error = %v", err)
}
testdata = zbuf.String()
}
testdataChecksum := func() pkg.Checksum {
h := sha512.New384()
h.Write([]byte(testdata))
return (pkg.Checksum)(h.Sum(nil))
}()
var transport http.Transport
client := http.Client{Transport: &transport}
transport.RegisterProtocol("file", http.NewFileTransportFS(fstest.MapFS{
"testdata": {Data: []byte(testdata), Mode: 0400},
}))
checkWithCache(t, []cacheTestCase{
{"http", nil, func(t *testing.T, base *check.Absolute, c *pkg.Cache) {
wantIdent := func() pkg.ID {
h := sha512.New384()
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{byte(pkg.KindHTTPGet), 0, 0, 0, 0, 0, 0, 0})
httpIdent := pkg.KindHTTPGet.Ident([]byte("file:///testdata"))
h.Write(httpIdent[:])
return pkg.ID(h.Sum(nil))
}()
a := pkg.NewHTTPGetTar(
t.Context(),
&client,
"file:///testdata",
testdataChecksum,
pkg.TarGzip,
)
if id := pkg.Ident(a); id != wantIdent {
t.Fatalf("Ident: %s, want %s", pkg.Encode(id), pkg.Encode(wantIdent))
}
wantPathname := base.Append(
"identifier",
pkg.Encode(wantIdent),
)
wantChecksum := pkg.MustDecode(
"yJlSb2A3jxaMLuKqwp1GwHOguAHddS9MjygF9ICEeegKfRvgLPdPmNh8mva47f8o",
)
if pathname, checksum, err := c.Cure(a); err != nil {
t.Fatalf("Cure: error = %v", err)
} else if !pathname.Is(wantPathname) {
t.Fatalf("Cure: %q, want %q", pathname, wantPathname)
} else if checksum != wantChecksum {
t.Fatalf("Cure: %v", &pkg.ChecksumMismatchError{
Got: checksum,
Want: wantChecksum,
})
}
}, pkg.MustDecode("lmVlYEGNFkwGVpzzS8KYjGBVB6FCyPtk9ViX88zen0GgTKLgGqO6eFxb4dpcP6bR")},
})
}