//go:build linux package main import ( "errors" "os" "path/filepath" "testing" ) // Exercises the ENOSYS/EINVAL/EOPNOTSUPP fallback of // safeRenameNoReplaceUnderRoot (pathsafe.go) directly, so kernels/filesystems // without renameat2 flag support get the same observable semantics: an // existing destination is refused with the errSafeDestExists sentinel the // handler maps onto 409, a missing source surfaces as ENOENT (mapped onto // 404), and a free destination renames exactly like the atomic path. func TestRenameNoReplaceFallback_SameSemantics(t *testing.T) { sr, root := newSafeRootTestDir(t) if err := os.MkdirAll(filepath.Join(root, "d"), 0o750); err != nil { t.Fatalf("mkdir: %v", err) } if err := os.WriteFile(filepath.Join(root, "d", "old.txt"), []byte("moved"), 0o644); err != nil { t.Fatalf("seed old: %v", err) } if err := os.WriteFile(filepath.Join(root, "d", "new.txt"), []byte("precious"), 0o644); err != nil { t.Fatalf("seed dest: %v", err) } dirFd, err := safeOpenDirAt(sr, "d") if err != nil { t.Fatalf("open dir: %v", err) } defer dirFd.Close() err = renameNoReplaceFallback(dirFd, "old.txt", "new.txt") if err == nil { t.Fatalf("fallback must refuse an existing destination") } if !errors.Is(err, errSafeDestExists) { t.Fatalf("expected errSafeDestExists, got %v", err) } if got, rerr := os.ReadFile(filepath.Join(root, "d", "new.txt")); rerr != nil || string(got) != "precious" { t.Fatalf("destination content must survive refusal: (%q,%v)", string(got), rerr) } // A free destination name renames exactly like the atomic path. if err := renameNoReplaceFallback(dirFd, "old.txt", "free.txt"); err != nil { t.Fatalf("fallback rename to free name: %v", err) } if _, lerr := os.Lstat(filepath.Join(root, "d", "old.txt")); !os.IsNotExist(lerr) { t.Fatalf("old name should be gone, err=%v", lerr) } if got, rerr := os.ReadFile(filepath.Join(root, "d", "free.txt")); rerr != nil || string(got) != "moved" { t.Fatalf("renamed content mismatch: (%q,%v)", string(got), rerr) } // A missing source surfaces as ENOENT for the handler to map onto 404. err = renameNoReplaceFallback(dirFd, "gone.txt", "whatever.txt") if !os.IsNotExist(err) { t.Fatalf("expected ENOENT-shaped error for missing source, got %v", err) } }