Add storage backends and safety checks
This commit is contained in:
233
internal/adapters/local/backend_test.go
Normal file
233
internal/adapters/local/backend_test.go
Normal file
@@ -0,0 +1,233 @@
|
||||
package local
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"errors"
|
||||
"io"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"reflect"
|
||||
"testing"
|
||||
|
||||
"gitea.maximumdirect.net/eric/distributor/internal/storage"
|
||||
)
|
||||
|
||||
func TestBackendRejectsTraversal(t *testing.T) {
|
||||
backend := newBackend(t)
|
||||
_, err := backend.ReadFile(context.Background(), "../outside")
|
||||
if !storage.IsInvalidPath(err) {
|
||||
t.Fatalf("ReadFile traversal error = %v, want invalid path", err)
|
||||
}
|
||||
_, err = backend.WriteFile(context.Background(), "/absolute", []byte("data"), storage.WriteOptions{})
|
||||
if !storage.IsInvalidPath(err) {
|
||||
t.Fatalf("WriteFile absolute path error = %v, want invalid path", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestBackendReadWriteAndStream(t *testing.T) {
|
||||
backend := newBackend(t)
|
||||
|
||||
entry, err := backend.WriteFile(context.Background(), "reports/report.md", []byte("hello"), storage.WriteOptions{})
|
||||
if err != nil {
|
||||
t.Fatalf("WriteFile() error = %v", err)
|
||||
}
|
||||
if entry.Path != "reports/report.md" || entry.Type != storage.EntryTypeFile || entry.Size != 5 {
|
||||
t.Fatalf("entry = %#v, want written file metadata", entry)
|
||||
}
|
||||
|
||||
data, err := backend.ReadFile(context.Background(), "reports/report.md")
|
||||
if err != nil {
|
||||
t.Fatalf("ReadFile() error = %v", err)
|
||||
}
|
||||
if string(data) != "hello" {
|
||||
t.Fatalf("ReadFile() = %q, want hello", data)
|
||||
}
|
||||
|
||||
reader, err := backend.OpenReader(context.Background(), "reports/report.md")
|
||||
if err != nil {
|
||||
t.Fatalf("OpenReader() error = %v", err)
|
||||
}
|
||||
streamed, err := io.ReadAll(reader)
|
||||
closeErr := reader.Close()
|
||||
if err != nil || closeErr != nil {
|
||||
t.Fatalf("read stream error = %v close = %v", err, closeErr)
|
||||
}
|
||||
if !bytes.Equal(streamed, data) {
|
||||
t.Fatalf("streamed = %q, want %q", streamed, data)
|
||||
}
|
||||
|
||||
_, err = backend.WriteFile(context.Background(), "reports/report.md", []byte("again"), storage.WriteOptions{})
|
||||
if !storage.IsAlreadyExists(err) {
|
||||
t.Fatalf("WriteFile without overwrite error = %v, want already exists", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestBackendStatWalkAndList(t *testing.T) {
|
||||
backend := newBackend(t)
|
||||
mustWrite(t, backend, "b/two.txt", "2")
|
||||
mustWrite(t, backend, "a/one.txt", "1")
|
||||
|
||||
entry, err := backend.Stat(context.Background(), "a/one.txt")
|
||||
if err != nil {
|
||||
t.Fatalf("Stat() error = %v", err)
|
||||
}
|
||||
if entry.Type != storage.EntryTypeFile || entry.Size != 1 {
|
||||
t.Fatalf("entry = %#v, want file size 1", entry)
|
||||
}
|
||||
|
||||
entries, err := storage.List(context.Background(), backend, "", storage.WalkOptions{Recursive: true})
|
||||
if err != nil {
|
||||
t.Fatalf("List() error = %v", err)
|
||||
}
|
||||
var paths []string
|
||||
for _, entry := range entries {
|
||||
paths = append(paths, entry.Path)
|
||||
}
|
||||
want := []string{"a", "a/one.txt", "b", "b/two.txt"}
|
||||
if !reflect.DeepEqual(paths, want) {
|
||||
t.Fatalf("paths = %v, want %v", paths, want)
|
||||
}
|
||||
|
||||
entries, err = storage.List(context.Background(), backend, "", storage.WalkOptions{Recursive: false})
|
||||
if err != nil {
|
||||
t.Fatalf("List nonrecursive error = %v", err)
|
||||
}
|
||||
paths = paths[:0]
|
||||
for _, entry := range entries {
|
||||
paths = append(paths, entry.Path)
|
||||
}
|
||||
want = []string{"a", "b"}
|
||||
if !reflect.DeepEqual(paths, want) {
|
||||
t.Fatalf("nonrecursive paths = %v, want %v", paths, want)
|
||||
}
|
||||
}
|
||||
|
||||
func TestBackendSymlinkReportingAndReadRejection(t *testing.T) {
|
||||
root := t.TempDir()
|
||||
backend, err := New(root)
|
||||
if err != nil {
|
||||
t.Fatalf("New() error = %v", err)
|
||||
}
|
||||
if err := os.WriteFile(filepath.Join(root, "target.txt"), []byte("target"), 0o600); err != nil {
|
||||
t.Fatalf("write target: %v", err)
|
||||
}
|
||||
if err := os.Symlink("target.txt", filepath.Join(root, "link.txt")); err != nil {
|
||||
t.Fatalf("symlink: %v", err)
|
||||
}
|
||||
|
||||
entry, err := backend.Stat(context.Background(), "link.txt")
|
||||
if err != nil {
|
||||
t.Fatalf("Stat() error = %v", err)
|
||||
}
|
||||
if entry.Type != storage.EntryTypeSymlink {
|
||||
t.Fatalf("entry type = %s, want symlink", entry.Type)
|
||||
}
|
||||
_, err = backend.ReadFile(context.Background(), "link.txt")
|
||||
if !storage.IsUnsupported(err) {
|
||||
t.Fatalf("ReadFile symlink error = %v, want unsupported", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestBackendWriteFromSizeMismatchLeavesNoFinalFile(t *testing.T) {
|
||||
backend := newBackend(t)
|
||||
_, err := backend.WriteFrom(context.Background(), "out.txt", bytes.NewBufferString("short"), storage.WriteOptions{SizeKnown: true, Size: 99})
|
||||
if !storage.IsConflict(err) {
|
||||
t.Fatalf("WriteFrom size mismatch error = %v, want conflict", err)
|
||||
}
|
||||
_, err = backend.Stat(context.Background(), "out.txt")
|
||||
if !storage.IsNotFound(err) {
|
||||
t.Fatalf("Stat after failed write error = %v, want not found", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestBackendManagedDeletion(t *testing.T) {
|
||||
backend := newBackend(t)
|
||||
mustWrite(t, backend, "bundle/report.html", "html")
|
||||
mustWrite(t, backend, "bundle/keep.txt", "keep")
|
||||
mustWrite(t, backend, "bundle/.distributor.json", "{}")
|
||||
|
||||
err := backend.DeleteManagedBundle(context.Background(), "bundle", []string{"report.html"}, storage.DeleteOptions{PruneEmptyDirs: true})
|
||||
if err != nil {
|
||||
t.Fatalf("DeleteManagedBundle() error = %v", err)
|
||||
}
|
||||
if _, err := backend.Stat(context.Background(), "bundle/report.html"); !storage.IsNotFound(err) {
|
||||
t.Fatalf("managed output stat error = %v, want not found", err)
|
||||
}
|
||||
if _, err := backend.Stat(context.Background(), "bundle/.distributor.json"); !storage.IsNotFound(err) {
|
||||
t.Fatalf("state stat error = %v, want not found", err)
|
||||
}
|
||||
if _, err := backend.Stat(context.Background(), "bundle/keep.txt"); err != nil {
|
||||
t.Fatalf("unlisted file stat error = %v", err)
|
||||
}
|
||||
if err := backend.DeleteManagedBundle(context.Background(), "bundle", []string{""}, storage.DeleteOptions{}); !storage.IsInvalidPath(err) {
|
||||
t.Fatalf("DeleteManagedBundle invalid output error = %v, want invalid path", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestBackendHasAny(t *testing.T) {
|
||||
backend := newBackend(t)
|
||||
found, err := backend.HasAny(context.Background(), "missing")
|
||||
if err != nil {
|
||||
t.Fatalf("HasAny missing error = %v", err)
|
||||
}
|
||||
if found {
|
||||
t.Fatal("HasAny missing = true, want false")
|
||||
}
|
||||
mustWrite(t, backend, "bundle/report.md", "report")
|
||||
found, err = backend.HasAny(context.Background(), "bundle")
|
||||
if err != nil {
|
||||
t.Fatalf("HasAny bundle error = %v", err)
|
||||
}
|
||||
if !found {
|
||||
t.Fatal("HasAny bundle = false, want true")
|
||||
}
|
||||
found, err = backend.HasAny(context.Background(), "bund")
|
||||
if err != nil {
|
||||
t.Fatalf("HasAny sibling prefix error = %v", err)
|
||||
}
|
||||
if found {
|
||||
t.Fatal("HasAny prefix sibling = true, want false")
|
||||
}
|
||||
}
|
||||
|
||||
func TestBackendWalkStops(t *testing.T) {
|
||||
backend := newBackend(t)
|
||||
mustWrite(t, backend, "a.txt", "a")
|
||||
mustWrite(t, backend, "b.txt", "b")
|
||||
visited := 0
|
||||
err := backend.Walk(context.Background(), "", storage.WalkOptions{Recursive: true}, func(storage.Entry) error {
|
||||
visited++
|
||||
return storage.ErrStopWalk
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("Walk() error = %v", err)
|
||||
}
|
||||
if visited != 1 {
|
||||
t.Fatalf("visited = %d, want 1", visited)
|
||||
}
|
||||
|
||||
errSentinel := errors.New("callback")
|
||||
err = backend.Walk(context.Background(), "", storage.WalkOptions{Recursive: true}, func(storage.Entry) error {
|
||||
return errSentinel
|
||||
})
|
||||
if !errors.Is(err, errSentinel) {
|
||||
t.Fatalf("Walk callback error = %v, want sentinel", err)
|
||||
}
|
||||
}
|
||||
|
||||
func newBackend(t *testing.T) *Backend {
|
||||
t.Helper()
|
||||
backend, err := New(t.TempDir())
|
||||
if err != nil {
|
||||
t.Fatalf("New() error = %v", err)
|
||||
}
|
||||
return backend
|
||||
}
|
||||
|
||||
func mustWrite(t *testing.T, backend *Backend, path, data string) {
|
||||
t.Helper()
|
||||
if _, err := backend.WriteFile(context.Background(), path, []byte(data), storage.WriteOptions{}); err != nil {
|
||||
t.Fatalf("WriteFile(%q) error = %v", path, err)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user