Files
pages/internal/geduldh/local_test.go
T
Anya Tokugawa d5d21b7841
Build and publish container images / images (push) Successful in 9m34s
Release container images / release (push) Successful in 36s
Refactor architecture into eight components and consolidate reliability work
Extract Grammaratur, Dregarnuhr, Geduldh, Mestionora, Ordoschnellic, Schutzaria, Kunstzeal and Seheweit behind compatible application contracts.

Include accumulated editor resilience, navigation and theme UX, remote CLI, storage/schema and DR groundwork, tests and design/operations documentation.

Verified: uncached full Go suite; go vet ./...; race tests for all eight components; 11 targeted Chromium tests; Node suites; 19 Python CLI tests; reproducible editor build. No push, release tag or deployment in this checkpoint.
2026-11-01 15:23:31 +09:00

265 lines
7.2 KiB
Go

package geduldh
import (
"bytes"
"context"
"crypto/sha256"
"encoding/hex"
"errors"
"io"
"os"
"path/filepath"
"strings"
"sync"
"testing"
)
func digest(data []byte) string { sum := sha256.Sum256(data); return hex.EncodeToString(sum[:]) }
func local(t *testing.T) (*Local, string) {
t.Helper()
root := t.TempDir()
s, err := NewLocal(root)
if err != nil {
t.Fatal(err)
}
return s, root
}
func TestLocalRoundTripAndRange(t *testing.T) {
for _, payload := range [][]byte{nil, []byte("text 日本語"), {0, 0xff, 1, 0, 3}} {
s, _ := local(t)
ctx := context.Background()
hash := digest(payload)
if err := s.PutIfAbsent(ctx, hash, int64(len(payload)), bytes.NewReader(payload)); err != nil {
t.Fatal(err)
}
info, err := s.Stat(ctx, hash)
if err != nil || info.Hash != hash || info.Size != int64(len(payload)) {
t.Fatalf("info=%+v err=%v", info, err)
}
reader, err := s.Open(ctx, hash)
if err != nil {
t.Fatal(err)
}
data, err := io.ReadAll(reader)
if err != nil || !bytes.Equal(data, payload) {
t.Fatalf("data=%v err=%v", data, err)
}
if size, err := reader.Seek(0, io.SeekEnd); err != nil || size != int64(len(payload)) {
t.Fatalf("size=%d err=%v", size, err)
}
if len(payload) > 2 {
buf := make([]byte, 2)
if _, err := reader.ReadAt(buf, 1); err != nil || !bytes.Equal(buf, payload[1:3]) {
t.Fatalf("range=%v err=%v", buf, err)
}
}
reader.Close()
if err := s.Verify(ctx, hash); err != nil {
t.Fatal(err)
}
}
}
func TestLocalRejectsIncompleteOrCorruptInput(t *testing.T) {
payload := []byte("good")
for _, tc := range []struct {
name string
data []byte
size int64
want error
}{
{"hash", []byte("evil"), 4, ErrHashMismatch},
{"short", []byte("goo"), 4, ErrSizeMismatch},
{"long", []byte("good!"), 4, ErrSizeMismatch},
{"negative", payload, -1, ErrSizeMismatch},
} {
t.Run(tc.name, func(t *testing.T) {
s, root := local(t)
err := s.PutIfAbsent(context.Background(), digest(payload), tc.size, bytes.NewReader(tc.data))
if !errors.Is(err, tc.want) {
t.Fatalf("got %v want %v", err, tc.want)
}
if _, err := s.Stat(context.Background(), digest(payload)); !errors.Is(err, os.ErrNotExist) {
t.Fatalf("published bad content: %v", err)
}
assertNoTemps(t, root)
})
}
}
func TestLocalExistingAndConcurrentWritesNeverReplace(t *testing.T) {
s, root := local(t)
payload := []byte("immutable payload")
hash := digest(payload)
ctx := context.Background()
var wg sync.WaitGroup
errorsCh := make(chan error, 12)
for range 12 {
wg.Add(1)
go func() {
defer wg.Done()
errorsCh <- s.PutIfAbsent(ctx, hash, int64(len(payload)), bytes.NewReader(payload))
}()
}
wg.Wait()
close(errorsCh)
for err := range errorsCh {
if err != nil {
t.Fatal(err)
}
}
key, _ := Key(hash)
filename := filepath.Join(root, filepath.FromSlash(key))
before, _ := os.Stat(filename)
if err := s.PutIfAbsent(ctx, hash, int64(len(payload)), bytes.NewReader(payload)); err != nil {
t.Fatal(err)
}
after, _ := os.Stat(filename)
if !os.SameFile(before, after) {
t.Fatal("existing inode replaced")
}
if err := os.WriteFile(filename, []byte("corrupt"), 0644); err != nil {
t.Fatal(err)
}
if err := s.Verify(ctx, hash); !errors.Is(err, ErrHashMismatch) {
t.Fatalf("verify=%v", err)
}
if err := s.PutIfAbsent(ctx, hash, int64(len(payload)), bytes.NewReader(payload)); !errors.Is(err, ErrHashMismatch) {
t.Fatalf("put=%v", err)
}
content, _ := os.ReadFile(filename)
if string(content) != "corrupt" {
t.Fatal("corruption silently overwritten")
}
assertNoTemps(t, root)
}
func TestLocalCancellationAndReaderFailure(t *testing.T) {
s, root := local(t)
ctx, cancel := context.WithCancel(context.Background())
cancel()
hash := digest([]byte("data"))
if err := s.PutIfAbsent(ctx, hash, 4, strings.NewReader("data")); !errors.Is(err, context.Canceled) {
t.Fatal(err)
}
if _, err := s.Open(ctx, hash); !errors.Is(err, context.Canceled) {
t.Fatal(err)
}
failure := errors.New("source failed")
if err := s.PutIfAbsent(context.Background(), hash, 4, failingReader{failure}); !errors.Is(err, failure) {
t.Fatal(err)
}
if _, err := s.Stat(context.Background(), hash); !errors.Is(err, os.ErrNotExist) {
t.Fatal(err)
}
if err := s.PutIfAbsent(context.Background(), hash, 4, strings.NewReader("data")); err != nil {
t.Fatal(err)
}
ctx, cancel = context.WithCancel(context.Background())
r, err := s.Open(ctx, hash)
if err != nil {
t.Fatal(err)
}
defer r.Close()
cancel()
if _, err := r.Read(make([]byte, 1)); !errors.Is(err, context.Canceled) {
t.Fatal(err)
}
if _, err := r.ReadAt(make([]byte, 1), 0); !errors.Is(err, context.Canceled) {
t.Fatal(err)
}
assertNoTemps(t, root)
}
type failingReader struct{ err error }
func (r failingReader) Read([]byte) (int, error) { return 0, r.err }
func TestLocalStreamsWithBoundedReadsAndMidStreamCancellation(t *testing.T) {
s, root := local(t)
payload := bytes.Repeat([]byte("large document\x00"), 100000)
hash := digest(payload)
input := &boundedReader{reader: bytes.NewReader(payload)}
if err := s.PutIfAbsent(context.Background(), hash, int64(len(payload)), input); err != nil {
t.Fatal(err)
}
if input.reads < 2 {
t.Fatal("expected multiple bounded reads")
}
// Cancellation while staging must never publish the final content address.
s2, root2 := local(t)
ctx, cancel := context.WithCancel(context.Background())
input = &boundedReader{reader: bytes.NewReader(payload), cancel: cancel}
if err := s2.PutIfAbsent(ctx, hash, int64(len(payload)), input); !errors.Is(err, context.Canceled) {
t.Fatalf("put=%v", err)
}
if _, err := s2.Stat(context.Background(), hash); !errors.Is(err, os.ErrNotExist) {
t.Fatalf("published canceled upload: %v", err)
}
assertNoTemps(t, root)
assertNoTemps(t, root2)
}
type boundedReader struct {
reader io.Reader
reads int
cancel context.CancelFunc
}
func (r *boundedReader) Read(p []byte) (int, error) {
if len(p) > 64<<10 {
return 0, errors.New("unbounded input read")
}
r.reads++
n, err := r.reader.Read(p)
if r.cancel != nil {
r.cancel()
}
return n, err
}
func TestLocalKeysAndSymlinkEscape(t *testing.T) {
for _, hash := range []string{"", "../../etc/passwd", strings.Repeat("g", 64), strings.Repeat("a", 63)} {
if _, err := Key(hash); !errors.Is(err, ErrInvalidHash) {
t.Fatal(err)
}
}
hash := digest([]byte("data"))
key, err := Key(" " + strings.ToUpper(hash) + " ")
if err != nil || key != "sha256/"+hash[:2]+"/"+hash {
t.Fatalf("key=%q err=%v", key, err)
}
s, root := local(t)
outside := t.TempDir()
if err := os.Symlink(outside, filepath.Join(root, "sha256")); err != nil {
t.Skip(err)
}
if err := s.PutIfAbsent(context.Background(), hash, 4, strings.NewReader("data")); err == nil {
t.Fatal("escaped storage root")
}
if _, err := s.Open(context.Background(), hash); err == nil {
t.Fatal("opened escaped path")
}
entries, err := os.ReadDir(outside)
if err != nil || len(entries) != 0 {
t.Fatalf("outside modified: %v %v", entries, err)
}
}
func assertNoTemps(t *testing.T, root string) {
t.Helper()
err := filepath.WalkDir(root, func(path string, entry os.DirEntry, err error) error {
if err != nil {
return err
}
if strings.HasPrefix(entry.Name(), ".document-") {
t.Errorf("temporary file left: %s", path)
}
return nil
})
if err != nil {
t.Fatal(err)
}
}