Files
aego/asset/pipeline/source_fingerprint_test.go
sanspie 50be5c04bc
ci-platforms / darwin amd64 smoke (push) Skipped
GitHub / linux amd64 native GitHub-hosted native CI: success
ci-platforms / windows amd64 smoke (push) Skipped
GitHub / windows amd64 native GitHub-hosted native CI: success
GitHub / darwin arm64 native GitHub-hosted native CI: success
GitHub / darwin amd64 native GitHub-hosted native CI: success
ci / checks (push) Failing after 1h21m34s
ci / native-authoring (push) Skipped
ci-platforms / darwin arm64 smoke (push) Skipped
github-native / gitea-pending (push) Skipped
github-native / darwin amd64 native (push) Skipped
github-native / linux amd64 native (push) Skipped
github-native / windows amd64 native (push) Skipped
github-native / darwin arm64 native (push) Skipped
github-native / gitea-result (push) Skipped
ci / build (amd64, linux, ubuntu-24.04) (push) Successful in 12m4s
ci / release (push) Skipped
Stage 12.5: Art Workspace and asset authoring
Add dockable Art tools, animation and previews, guarded asset workflows,
and the Web Asset Studio built on the shared Art Core. Expand English and
Russian documentation, interactive Try scenes, examples and editor icons.

Add audio waveform navigation, source analysis, guarded previews and a
browser Worker mixer. Preserve source changes and recovery with bounded
checks, and strengthen native, browser and interoperability CI.

Prepare version 0.12.5 and manually built native artifact candidates.
Fix the production site image demo build dependency.
Reuse immutable scene indices when capturing keyframes.
Wait for real Persist save/load completion in component tests.
Stabilize browser Game View captures outside pointer hover.
Aseprite interchange and persistent Audio EDL recovery remain limited;
the artifact workflow does not publish releases or change stable.
2026-11-15 10:37:50 +03:00

189 lines
6.5 KiB
Go

package pipeline
import (
"context"
"crypto/sha256"
"errors"
"os"
"path/filepath"
"testing"
"time"
)
func TestFingerprintSourceBoundsAndDistinguishesMissingFromEmpty(t *testing.T) {
root := t.TempDir()
if err := os.MkdirAll(filepath.Join(root, "assets"), 0o755); err != nil {
t.Fatal(err)
}
p := newPipeline(root, nil, nil)
var absent context.Context // Exercise the API's explicit nil-context rejection.
if _, err := p.FingerprintSource(absent, "assets/empty.wav", 0); err == nil {
t.Fatal("accepted a nil context")
}
var missingPipeline *Pipeline
if _, err := missingPipeline.FingerprintSource(context.Background(), "assets/empty.wav", 0); err == nil {
t.Fatal("accepted a nil pipeline")
}
missing, err := p.FingerprintSource(context.Background(), "assets/missing.wav", 0)
if err != nil || missing.Exists || missing.Size != 0 || missing.SHA256 != [sha256.Size]byte{} {
t.Fatalf("missing source: %+v %v", missing, err)
}
empty := filepath.Join(root, "assets", "empty.wav")
if err := os.WriteFile(empty, []byte{}, 0o644); err != nil {
t.Fatal(err)
}
got, err := p.FingerprintSource(context.Background(), "assets/empty.wav", 0)
if err != nil || !got.Exists || got.Size != 0 || got.SHA256 != sha256.Sum256(nil) {
t.Fatalf("empty source: %+v %v", got, err)
}
// A sparse synthetic source is larger than the existing 16 MiB source
// transaction limit. The fingerprint reads it in bounded chunks only.
const size = (16 << 20) + 37
large := filepath.Join(root, "assets", "large.wav")
f, err := os.OpenFile(large, os.O_CREATE|os.O_EXCL|os.O_WRONLY, 0o644)
if err != nil {
t.Fatal(err)
}
if err := f.Truncate(size); err != nil {
f.Close()
t.Fatal(err)
}
if err := f.Close(); err != nil {
t.Fatal(err)
}
if _, err := p.FingerprintSource(context.Background(), "assets/large.wav", size-1); err == nil {
t.Fatal("accepted a file exceeding the caller's limit")
}
h := sha256.New()
zero := make([]byte, 64<<10)
for remaining := size; remaining > 0; {
n := min(remaining, len(zero))
h.Write(zero[:n])
remaining -= n
}
got, err = p.FingerprintSource(context.Background(), "assets/large.wav", size)
var want [sha256.Size]byte
copy(want[:], h.Sum(nil))
if err != nil || !got.Exists || got.Size != size || got.SHA256 != want {
t.Fatalf("large source: %+v %v", got, err)
}
if _, err := p.SourceBytes("assets/large.wav"); err == nil {
t.Fatal("fingerprinting unexpectedly raised the transaction source limit")
}
if info, err := os.Stat(large); err != nil || info.Size() != size {
t.Fatal("fingerprinting changed the source", err)
}
for _, limit := range []int64{-1, MaxSourceFingerprintBytes + 1} {
if _, err := p.FingerprintSource(context.Background(), "assets/empty.wav", limit); err == nil {
t.Fatalf("accepted invalid limit %d", limit)
}
}
ctx, cancel := context.WithCancel(context.Background())
cancel()
if _, err := p.FingerprintSource(ctx, "assets/empty.wav", 1); !errors.Is(err, context.Canceled) {
t.Fatalf("ignored cancellation: %v", err)
}
}
func TestFingerprintSourceRejectsUnsafePathsAndPendingRecovery(t *testing.T) {
root := t.TempDir()
assets := filepath.Join(root, "assets")
if err := os.MkdirAll(assets, 0o755); err != nil {
t.Fatal(err)
}
if err := os.WriteFile(filepath.Join(assets, "real.wav"), []byte("original"), 0o644); err != nil {
t.Fatal(err)
}
p := newPipeline(root, nil, nil)
for _, rel := range []string{"../outside", "assets/.hidden.wav", "assets/../real.wav", "assets/bad:name", "assets"} {
if _, err := p.FingerprintSource(context.Background(), rel, 1024); err == nil {
t.Errorf("accepted unsafe path %q", rel)
}
}
t.Run("symlink", func(t *testing.T) {
if err := os.Symlink("real.wav", filepath.Join(assets, "link.wav")); err != nil {
t.Skipf("symlink creation unavailable: %v", err)
}
if _, err := p.FingerprintSource(context.Background(), "assets/link.wav", 1024); err == nil {
t.Fatal("followed a symlink to another source")
}
})
journal := filepath.Join(root, filepath.FromSlash(sourceJournalDir))
if err := os.MkdirAll(journal, 0o700); err != nil {
t.Fatal(err)
}
if _, err := p.FingerprintSource(context.Background(), "assets/real.wav", 1024); err == nil {
t.Fatal("fingerprinted while source recovery is pending")
}
if _, err := os.Stat(journal); err != nil {
t.Fatal("fingerprinting changed pending recovery", err)
}
}
func TestFingerprintSourceRejectsChangesAndCancellationDuringRead(t *testing.T) {
for _, reason := range []string{"same-size replacement", "in-place edit", "directory replacement", "cancellation"} {
t.Run(reason, func(t *testing.T) {
root := t.TempDir()
assets := filepath.Join(root, "assets")
if err := os.MkdirAll(assets, 0o755); err != nil {
t.Fatal(err)
}
file := filepath.Join(assets, "sample.wav")
if err := os.WriteFile(file, []byte("original"), 0o644); err != nil {
t.Fatal(err)
}
p := newPipeline(root, nil, nil)
fsroot, err := os.OpenRoot(root)
if err != nil {
t.Fatal(err)
}
defer fsroot.Close()
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
checkpoint := func() {
switch reason {
case "same-size replacement":
candidate := filepath.Join(assets, "candidate.wav")
if err := os.WriteFile(candidate, []byte("external"), 0o644); err != nil {
t.Fatal(err)
}
if err := os.Rename(candidate, file); err != nil {
t.Skipf("replacement of an open file unavailable: %v", err)
}
case "in-place edit":
if err := os.WriteFile(file, []byte("external"), 0o644); err != nil {
t.Fatal(err)
}
future := time.Now().Add(10 * time.Second)
if err := os.Chtimes(file, future, future); err != nil {
t.Fatal(err)
}
case "directory replacement":
if err := os.Rename(assets, filepath.Join(root, "old-assets")); err != nil {
t.Skipf("replacement of an open directory unavailable: %v", err)
}
if err := os.Mkdir(assets, 0o755); err != nil {
t.Fatal(err)
}
if err := os.WriteFile(file, []byte("external"), 0o644); err != nil {
t.Fatal(err)
}
case "cancellation":
cancel()
}
}
got, err := p.fingerprintSource(ctx, fsroot, "assets/sample.wav", 1024, checkpoint)
if err == nil || got.Exists || (reason == "cancellation" && !errors.Is(err, context.Canceled)) {
t.Fatalf("accepted changed source: %+v %v", got, err)
}
want := "external"
if reason == "cancellation" {
want = "original"
}
if data, err := os.ReadFile(file); err != nil || string(data) != want {
t.Fatal("fingerprinting changed the owner's file", err)
}
})
}
}