Re-verify QA-1..QA-11 fixes and the stage-2C config/session/signal changes used by the old UI. New bugs QA-12..QA-19 are skip-tagged tests; QA-12 (SIGTERM can hang vcom: main and Bubble Tea both handle it) blocks the release. The cross-device trash test now cleans the topdir trash it uses. Report: plans/rework/05-qa-v0.2.8.md, section RC2. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
437 lines
13 KiB
Go
437 lines
13 KiB
Go
package vfs
|
||
|
||
// QA (v0.2.8): independent checks of the stage-1 fixes in archive.go and
|
||
// ops.go (U-P0-1 archive safety, U-P0-4 trash as default delete, MakeDir).
|
||
// Tests that expose a still-open bug are skipped with "BUG QA-n" and are
|
||
// described in plans/rework/05-qa-v0.2.8.md.
|
||
|
||
import (
|
||
"archive/tar"
|
||
"archive/zip"
|
||
"compress/gzip"
|
||
"context"
|
||
"io"
|
||
"net/url"
|
||
"os"
|
||
"path/filepath"
|
||
"strconv"
|
||
"strings"
|
||
"syscall"
|
||
"testing"
|
||
)
|
||
|
||
func qaWrite(t *testing.T, p string, data string, mode os.FileMode) {
|
||
t.Helper()
|
||
if err := os.MkdirAll(filepath.Dir(p), 0o755); err != nil {
|
||
t.Fatal(err)
|
||
}
|
||
if err := os.WriteFile(p, []byte(data), mode); err != nil {
|
||
t.Fatal(err)
|
||
}
|
||
}
|
||
|
||
// Every format: archiving a file whose archive name equals its own name
|
||
// into the same directory is refused and leaves the source intact.
|
||
func TestQAArchiveIntoSourceDirAllFormats(t *testing.T) {
|
||
cases := []struct{ name, format string }{
|
||
{"data.zip", "zip"},
|
||
{"data.tar", "tar"},
|
||
{"data.tar", "tar.gz"}, // data.tar -> data.tar.gz: allowed, different file
|
||
{"data.gz", "tar.gz"}, // data.gz -> data.tar.gz: allowed
|
||
}
|
||
for _, tc := range cases {
|
||
t.Run(tc.name+"->"+tc.format, func(t *testing.T) {
|
||
d := t.TempDir()
|
||
src := filepath.Join(d, tc.name)
|
||
payload := strings.Repeat("z", 50000)
|
||
qaWrite(t, src, payload, 0o644)
|
||
arc := filepath.Join(d, ArchiveName([]string{src}, tc.format))
|
||
err := CreateArchive(context.Background(), []string{src}, arc, nil)
|
||
if arc == src {
|
||
if err == nil {
|
||
t.Fatal("archive equal to source must be refused")
|
||
}
|
||
} else if err != nil {
|
||
t.Fatalf("archive %s of %s should work: %v", arc, src, err)
|
||
}
|
||
if b, _ := os.ReadFile(src); string(b) != payload {
|
||
t.Fatalf("source changed (len %d)", len(b))
|
||
}
|
||
if left, _ := filepath.Glob(filepath.Join(d, ".vcom-tmp-*")); len(left) != 0 {
|
||
t.Fatalf("temp files left: %v", left)
|
||
}
|
||
})
|
||
}
|
||
}
|
||
|
||
// The produced archive must actually contain the source (zip, tar, tar.gz),
|
||
// i.e. the temp+rename path did not break the formats.
|
||
func TestQAArchiveContentsRoundTrip(t *testing.T) {
|
||
for _, format := range []string{"zip", "tar", "tar.gz"} {
|
||
t.Run(format, func(t *testing.T) {
|
||
d, out := t.TempDir(), t.TempDir()
|
||
qaWrite(t, filepath.Join(d, "proj", "a.txt"), "alpha", 0o644)
|
||
qaWrite(t, filepath.Join(d, "proj", "sub", "б.txt"), "бета", 0o644)
|
||
src := filepath.Join(d, "proj")
|
||
arc := filepath.Join(out, ArchiveName([]string{src}, format))
|
||
if err := CreateArchive(context.Background(), []string{src}, arc, nil); err != nil {
|
||
t.Fatal(err)
|
||
}
|
||
names := qaListArchive(t, arc)
|
||
joined := strings.Join(names, "\n")
|
||
for _, want := range []string{"a.txt", "б.txt"} {
|
||
if !strings.Contains(joined, want) {
|
||
t.Fatalf("%s missing from %s: %v", want, arc, names)
|
||
}
|
||
}
|
||
})
|
||
}
|
||
}
|
||
|
||
func qaListArchive(t *testing.T, p string) []string {
|
||
t.Helper()
|
||
var names []string
|
||
switch {
|
||
case strings.HasSuffix(p, ".zip"):
|
||
zr, err := zip.OpenReader(p)
|
||
if err != nil {
|
||
t.Fatal(err)
|
||
}
|
||
defer zr.Close()
|
||
for _, f := range zr.File {
|
||
names = append(names, f.Name)
|
||
}
|
||
default:
|
||
f, err := os.Open(p)
|
||
if err != nil {
|
||
t.Fatal(err)
|
||
}
|
||
defer f.Close()
|
||
var r io.Reader = f
|
||
if strings.HasSuffix(p, ".gz") {
|
||
gz, err := gzip.NewReader(f)
|
||
if err != nil {
|
||
t.Fatal(err)
|
||
}
|
||
r = gz
|
||
}
|
||
tr := tar.NewReader(r)
|
||
for {
|
||
h, err := tr.Next()
|
||
if err == io.EOF {
|
||
break
|
||
}
|
||
if err != nil {
|
||
t.Fatal(err)
|
||
}
|
||
names = append(names, h.Name)
|
||
}
|
||
}
|
||
return names
|
||
}
|
||
|
||
// Overwriting an existing archive after confirmation must not widen its
|
||
// permissions: a private 0600 archive became world-readable 0644.
|
||
func TestQAArchiveOverwriteKeepsPermissions(t *testing.T) {
|
||
d, out := t.TempDir(), t.TempDir()
|
||
qaWrite(t, filepath.Join(d, "secret.txt"), "pin 1234", 0o600)
|
||
arc := filepath.Join(out, "secret.zip")
|
||
qaWrite(t, arc, "old", 0o600)
|
||
if err := CreateArchive(context.Background(), []string{filepath.Join(d, "secret.txt")}, arc, nil); err != nil {
|
||
t.Fatal(err)
|
||
}
|
||
st, err := os.Stat(arc)
|
||
if err != nil {
|
||
t.Fatal(err)
|
||
}
|
||
if st.Mode().Perm() != 0o600 {
|
||
t.Fatalf("existing 0600 archive replaced with mode %o", st.Mode().Perm())
|
||
}
|
||
}
|
||
|
||
// A long but valid file name (NAME_MAX is 255 bytes) can no longer be
|
||
// archived: the temp name ".vcom-tmp-<rand>-<name>" exceeds NAME_MAX.
|
||
func TestQAArchiveLongName(t *testing.T) {
|
||
d, out := t.TempDir(), t.TempDir()
|
||
base := strings.Repeat("n", 240)
|
||
src := filepath.Join(d, base+".txt")
|
||
qaWrite(t, src, "x", 0o644)
|
||
arc := filepath.Join(out, ArchiveName([]string{src}, "zip")) // 244 bytes: valid
|
||
if err := CreateArchive(context.Background(), []string{src}, arc, nil); err != nil {
|
||
t.Fatalf("archive with a valid %d-byte name failed: %v", len(filepath.Base(arc)), err)
|
||
}
|
||
}
|
||
|
||
// A source that is a symlink to a directory, archived into that directory:
|
||
// the link is resolved by nobody, so the check does not see the archive is
|
||
// inside the (dereferenced) source. Make sure at least nothing is destroyed
|
||
// and the archive does not contain itself.
|
||
func TestQAArchiveSymlinkDirSourceIntoTarget(t *testing.T) {
|
||
d := t.TempDir()
|
||
proj := filepath.Join(d, "proj")
|
||
qaWrite(t, filepath.Join(proj, "a.txt"), "alpha", 0o644)
|
||
link := filepath.Join(d, "link")
|
||
if err := os.Symlink(proj, link); err != nil {
|
||
t.Fatal(err)
|
||
}
|
||
arc := filepath.Join(proj, "link.tar")
|
||
err := CreateArchive(context.Background(), []string{link}, arc, nil)
|
||
if b, _ := os.ReadFile(filepath.Join(proj, "a.txt")); string(b) != "alpha" {
|
||
t.Fatal("source content damaged")
|
||
}
|
||
if err == nil {
|
||
for _, n := range qaListArchive(t, arc) {
|
||
if strings.Contains(n, "link.tar") || strings.Contains(n, ".vcom-tmp") {
|
||
t.Fatalf("archive contains itself: %v", n)
|
||
}
|
||
}
|
||
}
|
||
}
|
||
|
||
// Cancelled archive into a directory with an existing archive: old archive
|
||
// stays, no temp file (tar and tar.gz paths, the developer only tested zip).
|
||
func TestQAArchiveCancelKeepsOldAllFormats(t *testing.T) {
|
||
for _, format := range []string{"zip", "tar", "tar.gz"} {
|
||
t.Run(format, func(t *testing.T) {
|
||
d, out := t.TempDir(), t.TempDir()
|
||
qaWrite(t, filepath.Join(d, "src", "f"), "x", 0o644)
|
||
src := filepath.Join(d, "src")
|
||
arc := filepath.Join(out, ArchiveName([]string{src}, format))
|
||
qaWrite(t, arc, "OLD", 0o644)
|
||
ctx, cancel := context.WithCancel(context.Background())
|
||
cancel()
|
||
if err := CreateArchive(ctx, []string{src}, arc, nil); err == nil {
|
||
t.Fatal("cancelled archive should fail")
|
||
}
|
||
if b, _ := os.ReadFile(arc); string(b) != "OLD" {
|
||
t.Fatalf("old archive replaced: %q", b)
|
||
}
|
||
if left, _ := filepath.Glob(filepath.Join(out, ".vcom-tmp-*")); len(left) != 0 {
|
||
t.Fatalf("temp files left: %v", left)
|
||
}
|
||
})
|
||
}
|
||
}
|
||
|
||
// --- Trash (now the default delete) ---------------------------------------
|
||
|
||
func qaTrashHome(t *testing.T) string {
|
||
t.Helper()
|
||
home := t.TempDir()
|
||
t.Setenv("HOME", home)
|
||
t.Setenv("XDG_DATA_HOME", filepath.Join(home, ".local", "share"))
|
||
return filepath.Join(home, ".local", "share", "Trash")
|
||
}
|
||
|
||
// Names with spaces and Cyrillic, and same-name collisions, all land in the
|
||
// trash under distinct names with a matching .trashinfo.
|
||
func TestQATrashNamesAndCollisions(t *testing.T) {
|
||
trash := qaTrashHome(t)
|
||
work := filepath.Join(os.Getenv("HOME"), "work") // same filesystem as HOME
|
||
names := []string{"my file.txt", "отчёт 2026.txt"}
|
||
for _, n := range names {
|
||
qaWrite(t, filepath.Join(work, "a", n), "A "+n, 0o644)
|
||
qaWrite(t, filepath.Join(work, "b", n), "B "+n, 0o644)
|
||
}
|
||
for _, sub := range []string{"a", "b"} {
|
||
for _, n := range names {
|
||
if err := MoveToTrash(filepath.Join(work, sub, n)); err != nil {
|
||
t.Fatalf("MoveToTrash(%s/%s): %v", sub, n, err)
|
||
}
|
||
}
|
||
}
|
||
files, _ := os.ReadDir(filepath.Join(trash, "files"))
|
||
infos, _ := os.ReadDir(filepath.Join(trash, "info"))
|
||
if len(files) != 4 || len(infos) != 4 {
|
||
t.Fatalf("want 4 trashed files and 4 infos, got %d/%d", len(files), len(infos))
|
||
}
|
||
for _, f := range files {
|
||
if _, err := os.Stat(filepath.Join(trash, "info", f.Name()+".trashinfo")); err != nil {
|
||
t.Errorf("no trashinfo for %s", f.Name())
|
||
}
|
||
}
|
||
}
|
||
|
||
// The XDG trash spec requires Path= in .trashinfo to be percent-encoded
|
||
// (RFC 2396). Unencoded spaces/Cyrillic/newlines break restore in other
|
||
// tools (gio, trash-cli) — a newline in a file name corrupts the info file.
|
||
func TestQATrashInfoPathEncoded(t *testing.T) {
|
||
trash := qaTrashHome(t)
|
||
work := filepath.Join(os.Getenv("HOME"), "work")
|
||
p := filepath.Join(work, "my file\nx.txt")
|
||
qaWrite(t, p, "x", 0o644)
|
||
if err := MoveToTrash(p); err != nil {
|
||
t.Fatal(err)
|
||
}
|
||
b, err := os.ReadFile(filepath.Join(trash, "info", "my file\nx.txt.trashinfo"))
|
||
if err != nil {
|
||
t.Fatal(err)
|
||
}
|
||
var pathLine string
|
||
for _, l := range strings.Split(string(b), "\n") {
|
||
if strings.HasPrefix(l, "Path=") {
|
||
pathLine = strings.TrimPrefix(l, "Path=")
|
||
}
|
||
}
|
||
got, err := url.PathUnescape(pathLine)
|
||
if err != nil || got != p {
|
||
t.Fatalf("Path=%q does not decode to %q (err %v)", pathLine, p, err)
|
||
}
|
||
}
|
||
|
||
// XDG_DATA_HOME must be honoured ($XDG_DATA_HOME/Trash), otherwise vcom and
|
||
// the desktop use different trash cans.
|
||
func TestQATrashHonoursXDGDataHome(t *testing.T) {
|
||
home := t.TempDir()
|
||
t.Setenv("HOME", home)
|
||
xdg := filepath.Join(home, "xdgdata")
|
||
t.Setenv("XDG_DATA_HOME", xdg)
|
||
p := filepath.Join(home, "f.txt")
|
||
qaWrite(t, p, "x", 0o644)
|
||
if err := MoveToTrash(p); err != nil {
|
||
t.Fatal(err)
|
||
}
|
||
if _, err := os.Stat(filepath.Join(xdg, "Trash", "files", "f.txt")); err != nil {
|
||
t.Fatalf("file not in $XDG_DATA_HOME/Trash: %v", err)
|
||
}
|
||
}
|
||
|
||
// otherDevice returns a writable directory on a different filesystem than
|
||
// home, or "" if none is available.
|
||
func otherDevice(t *testing.T, home string) string {
|
||
t.Helper()
|
||
var hs syscall.Stat_t
|
||
if err := syscall.Stat(home, &hs); err != nil {
|
||
return ""
|
||
}
|
||
wd, _ := os.Getwd()
|
||
for _, cand := range []string{"/dev/shm", os.Getenv("XDG_RUNTIME_DIR"), "/var/tmp", wd} {
|
||
if cand == "" {
|
||
continue
|
||
}
|
||
var cs syscall.Stat_t
|
||
if err := syscall.Stat(cand, &cs); err != nil || cs.Dev == hs.Dev {
|
||
continue
|
||
}
|
||
dir, err := os.MkdirTemp(cand, ".vcom-qa-*")
|
||
if err != nil {
|
||
continue
|
||
}
|
||
t.Cleanup(func() { os.RemoveAll(dir) })
|
||
return dir
|
||
}
|
||
return ""
|
||
}
|
||
|
||
// With trash as the default, deleting a file on another filesystem than
|
||
// $HOME (/tmp on tmpfs, /mnt, USB, a second disk) must still work: the spec
|
||
// uses $topdir/.Trash-$uid or a copy. Here MoveToTrash only does os.Rename
|
||
// and fails with EXDEV.
|
||
func TestQATrashCrossDevice(t *testing.T) {
|
||
home := qaTrashHome(t)
|
||
_ = home
|
||
dir := otherDevice(t, os.Getenv("HOME"))
|
||
if dir == "" {
|
||
t.Skip("no second filesystem available")
|
||
}
|
||
name := "vcom-qa-" + filepath.Base(dir) + ".txt"
|
||
p := filepath.Join(dir, name)
|
||
qaWrite(t, p, "x", 0o644)
|
||
cleanupTopdirTrash(t, dir, name)
|
||
if err := MoveToTrash(p); err != nil {
|
||
t.Fatalf("MoveToTrash across filesystems: %v", err)
|
||
}
|
||
if _, err := os.Lstat(p); !os.IsNotExist(err) {
|
||
t.Fatal("file still in place")
|
||
}
|
||
}
|
||
|
||
// A trashed broken symlink with the same name must not be silently replaced
|
||
// by the next trashed item (collision check uses Stat, which follows links).
|
||
func TestQATrashCollisionWithBrokenSymlink(t *testing.T) {
|
||
trash := qaTrashHome(t)
|
||
work := filepath.Join(os.Getenv("HOME"), "work")
|
||
if err := os.MkdirAll(work, 0o755); err != nil {
|
||
t.Fatal(err)
|
||
}
|
||
link := filepath.Join(work, "x")
|
||
if err := os.Symlink("/nonexistent/target", link); err != nil {
|
||
t.Fatal(err)
|
||
}
|
||
if err := MoveToTrash(link); err != nil {
|
||
t.Fatal(err)
|
||
}
|
||
qaWrite(t, link, "file now", 0o644)
|
||
if err := MoveToTrash(link); err != nil {
|
||
t.Fatal(err)
|
||
}
|
||
files, _ := os.ReadDir(filepath.Join(trash, "files"))
|
||
if len(files) != 2 {
|
||
t.Fatalf("want 2 items in trash, got %d (first one overwritten)", len(files))
|
||
}
|
||
}
|
||
|
||
// --- MakeDir ---------------------------------------------------------------
|
||
|
||
func TestQAMakeDirEdgeCases(t *testing.T) {
|
||
d := t.TempDir()
|
||
if _, err := MakeDir(d, "a/b"); err != nil {
|
||
t.Fatalf("nested name: %v", err)
|
||
}
|
||
if _, err := MakeDir(d, "a/b"); err == nil {
|
||
t.Fatal("existing nested dir must error")
|
||
}
|
||
if _, err := MakeDir(d, "a"); err == nil {
|
||
t.Fatal("existing parent must error")
|
||
}
|
||
qaWrite(t, filepath.Join(d, "file"), "x", 0o644)
|
||
if _, err := MakeDir(d, "file"); err == nil {
|
||
t.Fatal("name of an existing file must error")
|
||
}
|
||
if _, err := MakeDir(d, "папка с пробелом"); err != nil {
|
||
t.Fatalf("cyrillic name: %v", err)
|
||
}
|
||
}
|
||
|
||
// cleanupTopdirTrash removes name (and its .trashinfo) from the topdir trash
|
||
// of dir's filesystem after the test, and the trash dirs themselves if the
|
||
// test created them: the topdir is a real mount (/dev/shm, /run/user/...).
|
||
func cleanupTopdirTrash(t *testing.T, dir, name string) {
|
||
t.Helper()
|
||
var st syscall.Stat_t
|
||
if err := syscall.Stat(dir, &st); err != nil {
|
||
return
|
||
}
|
||
top := dir
|
||
for {
|
||
parent := filepath.Dir(top)
|
||
var ps syscall.Stat_t
|
||
if parent == top || syscall.Stat(parent, &ps) != nil || ps.Dev != st.Dev {
|
||
break
|
||
}
|
||
top = parent
|
||
}
|
||
uid := strconv.Itoa(os.Getuid())
|
||
trashes := []string{filepath.Join(top, ".Trash-"+uid), filepath.Join(top, ".Trash", uid)}
|
||
existed := map[string]bool{}
|
||
for _, tr := range trashes {
|
||
if _, err := os.Lstat(tr); err == nil {
|
||
existed[tr] = true
|
||
}
|
||
}
|
||
t.Cleanup(func() {
|
||
for _, tr := range trashes {
|
||
matches, _ := filepath.Glob(filepath.Join(tr, "files", name+"*"))
|
||
for _, m := range matches {
|
||
os.RemoveAll(m)
|
||
os.Remove(filepath.Join(tr, "info", filepath.Base(m)+".trashinfo"))
|
||
}
|
||
if !existed[tr] {
|
||
os.Remove(filepath.Join(tr, "files"))
|
||
os.Remove(filepath.Join(tr, "info"))
|
||
os.Remove(filepath.Join(tr, "expunged"))
|
||
os.Remove(tr)
|
||
}
|
||
}
|
||
})
|
||
}
|