check / check (push) Canceled after 0s
magic.DetectFormat returns an imagerequest.Format. ValidateMagicBytes returns the format it checked against the upstream Content-Type. The cache stores that format in the source's row, so a variant made from a cached source uses it too. The proxy hands it to Process, which still refuses input libvips would load with another loader, such as an SVG passing as AVIF. Format.MIMEType declares each MIME string once, and the fetcher, the cache's variant read and the Accept matching use it. magic.MIMEType, magic.ImageFormat, MIMEToImageFormat, magic's SVG detection, detectFormat, formatFromString, FormatToMIME and Result.InputFormat are gone. Model: opus-5-5
172 lines
5.2 KiB
Go
172 lines
5.2 KiB
Go
package imgcache
|
|
|
|
import (
|
|
"bytes"
|
|
"io"
|
|
"os"
|
|
"path/filepath"
|
|
"testing"
|
|
|
|
"sneak.berlin/go/pixa/internal/imagerequest"
|
|
)
|
|
|
|
// TestPutSource_StoresIdenticalBytesOfTwoURLsTwice stores the same bytes
|
|
// for two source URLs and checks that each URL has a source_content row of
|
|
// its own and a file of its own, named by its source key under
|
|
// cache/sources/<ab>/<cd>/.
|
|
func TestPutSource_StoresIdenticalBytesOfTwoURLsTwice(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
cache, stateDir := newEvictionTestCache(t, 1<<30)
|
|
content := bytes.Repeat([]byte{0x5A}, 400)
|
|
|
|
keys := map[string]imagerequest.SourceKey{
|
|
"/a.jpg": storeEvictionTestSource(t, cache, testHostCDN, "/a.jpg", content),
|
|
"/b.jpg": storeEvictionTestSource(t, cache, testHostCDN, "/b.jpg", content),
|
|
}
|
|
t.Logf("source keys: %v", keys)
|
|
|
|
if keys["/a.jpg"] == keys["/b.jpg"] {
|
|
t.Fatalf("both source URLs got the key %s", keys["/a.jpg"])
|
|
}
|
|
|
|
for sourcePath, key := range keys {
|
|
rows := countRows(t, cache,
|
|
`SELECT COUNT(*) FROM source_content WHERE source_key = ?`, string(key))
|
|
if rows != 1 {
|
|
t.Errorf("%s has %d source_content rows, want 1", sourcePath, rows)
|
|
}
|
|
|
|
name := string(key)
|
|
file := filepath.Join(stateDir, "cache", "sources", name[0:2], name[2:4], name)
|
|
|
|
stored, err := os.ReadFile(file) //nolint:gosec // path in the test's state dir
|
|
if err != nil || !bytes.Equal(stored, content) {
|
|
t.Errorf("file of %s at %s holds %d bytes (error %v), want the %d stored",
|
|
sourcePath, file, len(stored), err, len(content))
|
|
}
|
|
}
|
|
|
|
usage, err := cache.UsageBytes(t.Context())
|
|
if err != nil {
|
|
t.Fatalf("UsageBytes() error = %v", err)
|
|
}
|
|
|
|
if usage != 800 {
|
|
t.Errorf("UsageBytes() = %d, want 800, the 400 bytes once for each URL", usage)
|
|
}
|
|
}
|
|
|
|
// TestEvictSourceBlob_LeavesSourceWithIdenticalBytesServed stores the same
|
|
// bytes for two source URLs and evicts the first. Its row and file must be
|
|
// gone, and the second must still be found and read from the cache.
|
|
func TestEvictSourceBlob_LeavesSourceWithIdenticalBytesServed(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
cache, _ := newEvictionTestCache(t, 1<<30)
|
|
content := bytes.Repeat([]byte{0x5B}, 400)
|
|
|
|
evicted := storeEvictionTestSource(t, cache, testHostCDN, "/evicted.jpg", content)
|
|
storeEvictionTestSource(t, cache, testHostCDN, "/kept.jpg", content)
|
|
|
|
err := cache.evictSourceBlob(t.Context(), evicted)
|
|
if err != nil {
|
|
t.Fatalf("evictSourceBlob() error = %v", err)
|
|
}
|
|
|
|
_, err = os.Stat(cache.srcContent.keyToPath(string(evicted)))
|
|
if err == nil {
|
|
t.Error("the evicted source's file is still on disk")
|
|
}
|
|
|
|
if n := countRows(t, cache,
|
|
`SELECT COUNT(*) FROM source_content WHERE source_key = ?`, string(evicted),
|
|
); n != 0 {
|
|
t.Errorf("the evicted source still has %d source_content rows", n)
|
|
}
|
|
|
|
reader, _, _, err := cache.Source(t.Context(),
|
|
imagerequest.Source{Host: testHostCDN, Path: "/kept.jpg"})
|
|
t.Logf("Source() of the kept source: error %v", err)
|
|
|
|
if err != nil {
|
|
t.Fatalf("Source() of the kept source error = %v; want it cached", err)
|
|
}
|
|
|
|
defer func() { _ = reader.Close() }()
|
|
|
|
served, err := io.ReadAll(reader)
|
|
if err != nil {
|
|
t.Fatalf("reading the kept source: %v", err)
|
|
}
|
|
|
|
if !bytes.Equal(served, content) {
|
|
t.Errorf("the kept source reads as %d bytes, want the %d stored",
|
|
len(served), len(content))
|
|
}
|
|
}
|
|
|
|
// TestPutSource_FailedRowWriteLeavesNoFile drops source_content, so that
|
|
// PutSource cannot write the source's row, and checks that the store fails
|
|
// and leaves no file under the source's key.
|
|
func TestPutSource_FailedRowWriteLeavesNoFile(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
cache, _ := newEvictionTestCache(t, 1<<30)
|
|
|
|
_, err := cache.db.ExecContext(t.Context(), `DROP TABLE source_content`)
|
|
if err != nil {
|
|
t.Fatalf("failed to drop source_content: %v", err)
|
|
}
|
|
|
|
req := &imagerequest.Request{
|
|
Source: imagerequest.Source{Host: testHostCDN, Path: testPathCat},
|
|
}
|
|
key, err := cache.PutSource(t.Context(), req.Source, imagerequest.FormatJPEG,
|
|
bytes.NewReader([]byte("source bytes")))
|
|
t.Logf("PutSource() = %q, error = %v", key, err)
|
|
|
|
if err == nil {
|
|
t.Error("PutSource() error = nil, want the failed row write")
|
|
}
|
|
|
|
_, err = os.Stat(cache.srcContent.keyToPath(string(req.Source.Key())))
|
|
if err == nil {
|
|
t.Error("the source's file is on disk although its row was not written")
|
|
}
|
|
}
|
|
|
|
// TestSource_ReturnsTheFormatPutSourceStored stores a source as PNG, then
|
|
// the same source URL again as WebP. After each store, Source must return
|
|
// the format that store gave.
|
|
func TestSource_ReturnsTheFormatPutSourceStored(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
cache, _ := newEvictionTestCache(t, 1<<30)
|
|
src := imagerequest.Source{Host: testHostCDN, Path: testPathCat}
|
|
|
|
for _, format := range []imagerequest.Format{
|
|
imagerequest.FormatPNG, imagerequest.FormatWebP,
|
|
} {
|
|
_, err := cache.PutSource(t.Context(), src, format,
|
|
bytes.NewReader([]byte("source bytes")))
|
|
if err != nil {
|
|
t.Fatalf("PutSource(%s) error = %v", format, err)
|
|
}
|
|
|
|
reader, _, got, err := cache.Source(t.Context(), src)
|
|
if err != nil {
|
|
t.Fatalf("Source() after PutSource(%s) error = %v", format, err)
|
|
}
|
|
|
|
_ = reader.Close()
|
|
|
|
t.Logf("Source() after PutSource(%s): format %s", format, got)
|
|
|
|
if got != format {
|
|
t.Errorf("Source() after PutSource(%s) format = %q, want %q",
|
|
format, got, format)
|
|
}
|
|
}
|
|
}
|