vcom/internal/fs/remote/qa_remote_test.go
vrubelroman 846ee4d2bc fix(remote): report existing name on F7 in SSH panes; clean path in RemoveRecursive
F7 with a taken name in an SSH pane silently succeeded because mkdir went
through MkdirAll (QA-10). The new SSHClient.MkdirNew creates missing
parents but fails with ErrExist on an existing name, like the local
MakeDir.

RemoveRecursive cleans its path first: lstat("link/") resolves the link on
POSIX servers and deleted the target's contents (QA-9; not reachable from
the UI).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-01 03:56:54 +03:00

176 lines
4.9 KiB
Go

package remote
// QA (v0.2.8): independent checks of N-P0-3 (RemoveRecursive never follows
// symlinks) and N-P0-8 (Close is safe under concurrent use, refcount).
import (
"os"
"path/filepath"
"sync"
"testing"
"time"
)
func TestQARemoveRecursiveSymlinkVariants(t *testing.T) {
_, c, dir := setup(t)
target := filepath.Join(dir, "target")
writeFile(t, filepath.Join(target, "keep.txt"), 10, 0o644)
file := filepath.Join(dir, "file.txt")
writeFile(t, file, 10, 0o644)
mk := func(name, to string) string {
p := filepath.Join(dir, name)
if err := os.Symlink(to, p); err != nil {
t.Fatal(err)
}
return p
}
cases := map[string]string{
"broken": mk("broken", filepath.Join(dir, "nowhere")),
"to-file": mk("to-file", file),
"relative": mk("relative", "target"),
"to-parent": mk("to-parent", ".."),
}
for name, p := range cases {
if err := c.RemoveRecursive(p); err != nil {
t.Errorf("%s: RemoveRecursive: %v", name, err)
}
if _, err := os.Lstat(p); !os.IsNotExist(err) {
t.Errorf("%s: link not removed", name)
}
}
if _, err := os.Stat(filepath.Join(target, "keep.txt")); err != nil {
t.Fatalf("target content removed: %v", err)
}
if _, err := os.Stat(file); err != nil {
t.Fatalf("link target file removed: %v", err)
}
}
// A directory containing symlinks to a sibling directory and to a file
// outside: only the links go, targets survive.
func TestQARemoveRecursiveDirWithOutsideLinks(t *testing.T) {
_, c, dir := setup(t)
outside := filepath.Join(dir, "outside")
writeFile(t, filepath.Join(outside, "a.txt"), 10, 0o644)
writeFile(t, filepath.Join(outside, "b.txt"), 10, 0o644)
victim := filepath.Join(dir, "victim")
writeFile(t, filepath.Join(victim, "own.txt"), 10, 0o644)
if err := os.MkdirAll(filepath.Join(victim, "deep", "deeper"), 0o755); err != nil {
t.Fatal(err)
}
if err := os.Symlink(outside, filepath.Join(victim, "deep", "deeper", "dirlink")); err != nil {
t.Fatal(err)
}
if err := os.Symlink(filepath.Join(outside, "b.txt"), filepath.Join(victim, "filelink")); err != nil {
t.Fatal(err)
}
if err := c.RemoveRecursive(victim); err != nil {
t.Fatalf("RemoveRecursive: %v", err)
}
if _, err := os.Lstat(victim); !os.IsNotExist(err) {
t.Fatal("victim dir still exists")
}
for _, f := range []string{"a.txt", "b.txt"} {
if _, err := os.Stat(filepath.Join(outside, f)); err != nil {
t.Fatalf("outside/%s removed through a link: %v", f, err)
}
}
}
// "link/" (trailing slash) makes lstat resolve the link on POSIX servers.
// The UI never builds such paths today, but RemoveRecursive is a public
// helper: with a trailing slash it still empties the target.
func TestQARemoveRecursiveSymlinkTrailingSlash(t *testing.T) {
_, c, dir := setup(t)
target := filepath.Join(dir, "target")
writeFile(t, filepath.Join(target, "keep.txt"), 10, 0o644)
link := filepath.Join(dir, "link")
if err := os.Symlink(target, link); err != nil {
t.Fatal(err)
}
_ = c.RemoveRecursive(link + "/")
if _, err := os.Stat(filepath.Join(target, "keep.txt")); err != nil {
t.Fatalf("target content removed via link/: %v", err)
}
}
// Close while uploads and downloads are streaming: errors, no panic, no race.
func TestQACloseDuringTransfers(t *testing.T) {
_, c, dir := setup(t)
src := filepath.Join(dir, "big.bin")
writeFile(t, src, 4<<20, 0o644)
var wg sync.WaitGroup
panics := make(chan any, 16)
for i := 0; i < 3; i++ {
wg.Add(2)
go func(i int) {
defer wg.Done()
defer func() {
if r := recover(); r != nil {
panics <- r
}
}()
_ = c.CopyFileToRemote(src, filepath.Join(dir, "up", "u"+string(rune('0'+i))))
}(i)
go func(i int) {
defer wg.Done()
defer func() {
if r := recover(); r != nil {
panics <- r
}
}()
_ = c.CopyFileFromRemote(src, filepath.Join(dir, "down", "d"+string(rune('0'+i))))
_ = c.RemoveRecursive(filepath.Join(dir, "up"))
_ = c.ExecWithProgress("true", func(string) {})
_ = c.MkdirAll(filepath.Join(dir, "m", "n"))
}(i)
}
time.Sleep(10 * time.Millisecond)
var cw sync.WaitGroup
for i := 0; i < 3; i++ {
cw.Add(1)
go func() { defer cw.Done(); c.Close() }()
}
cw.Wait()
wg.Wait()
close(panics)
for p := range panics {
t.Fatalf("panic during Close: %v", p)
}
if c.IsConnected() {
t.Fatal("still connected after Close")
}
}
// Concurrent Acquire/Release from panels and jobs: the client is closed
// exactly when the last user leaves, never earlier.
func TestQAAcquireReleaseConcurrent(t *testing.T) {
_, c, dir := setup(t)
const n = 50
for i := 0; i < n; i++ {
c.Acquire()
}
var wg sync.WaitGroup
for i := 0; i < n; i++ {
wg.Add(1)
go func() {
defer wg.Done()
_, _ = c.ReadDir(dir)
c.Release()
}()
}
wg.Wait()
if !c.IsConnected() {
t.Fatal("client closed while the original user still holds it")
}
if c.Users() != 1 {
t.Fatalf("users=%d, want 1", c.Users())
}
c.Release()
if c.IsConnected() {
t.Fatal("last Release must close")
}
// Extra Release (double release by a buggy caller) must not panic.
c.Release()
}