54 lines
1.5 KiB
Go
54 lines
1.5 KiB
Go
package nodefiles
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"io/fs"
|
|
"os"
|
|
"path/filepath"
|
|
"testing"
|
|
)
|
|
|
|
func TestBufferedWritesRequireSuccessfulCommit(t *testing.T) {
|
|
dir := t.TempDir()
|
|
file := filepath.Join(dir, "index.txt")
|
|
if err := os.WriteFile(file, []byte("original"), 0660); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
root := New(context.Background(), "sv0123456789", func(_ context.Context, q Request) (Response, error) { return Execute(dir, q), nil })
|
|
f, _ := root.OpenFile("index.txt", os.O_WRONLY|os.O_TRUNC, 0660)
|
|
_, _ = f.Write([]byte("partial"))
|
|
f.Abort()
|
|
_ = f.Close()
|
|
raw, _ := os.ReadFile(file)
|
|
if string(raw) != "original" {
|
|
t.Fatal("aborted upload changed original")
|
|
}
|
|
f, _ = root.OpenFile("index.txt", os.O_WRONLY|os.O_CREATE|os.O_EXCL, 0660)
|
|
_, _ = f.Write([]byte("new"))
|
|
if err := f.Close(); !errors.Is(err, fs.ErrExist) {
|
|
t.Fatalf("exclusive write: %v", err)
|
|
}
|
|
f, _ = root.OpenFile("index.txt", os.O_WRONLY|os.O_TRUNC, 0660)
|
|
_, _ = f.Write([]byte("committed"))
|
|
if err := f.Close(); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
raw, _ = os.ReadFile(file)
|
|
if string(raw) != "committed" {
|
|
t.Fatal("successful commit missing")
|
|
}
|
|
}
|
|
func TestSymlinkEscapeDenied(t *testing.T) {
|
|
dir := t.TempDir()
|
|
outside := t.TempDir()
|
|
_ = os.WriteFile(filepath.Join(outside, "secret"), []byte("marker"), 0600)
|
|
if err := os.Symlink(outside, filepath.Join(dir, "escape")); err != nil {
|
|
t.Skip("symlinks unavailable")
|
|
}
|
|
out := Execute(dir, Request{Operation: "read", Path: "escape/secret"})
|
|
if out.Error != "denied" || len(out.Data) != 0 {
|
|
t.Fatal("symlink escape read")
|
|
}
|
|
}
|