Simplify atomic file replacement and bounded reads
This commit is contained in:
@@ -139,30 +139,23 @@ func TestInstallDownloadedTempFileSetsPermissions(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestWriteFileAtomicRejectsSymlinkedDestinationAncestor(t *testing.T) {
|
||||
func TestWriteFileAtomicUsesOrdinaryDestinationPathSemantics(t *testing.T) {
|
||||
root := t.TempDir()
|
||||
outside := t.TempDir()
|
||||
sentinel := filepath.Join(outside, "sentinel.txt")
|
||||
if err := os.WriteFile(sentinel, []byte("unchanged"), 0o600); err != nil {
|
||||
t.Fatalf("WriteFile(sentinel) error = %v", err)
|
||||
}
|
||||
if err := os.Symlink(outside, filepath.Join(root, "redirect")); err != nil {
|
||||
t.Skipf("Symlink unavailable: %v", err)
|
||||
}
|
||||
|
||||
err := WriteFileAtomic(filepath.Join(root, "redirect", "output.txt"), []byte("new"), 0o640)
|
||||
if err == nil {
|
||||
t.Fatal("WriteFileAtomic() error = nil, want symlink ancestor rejection")
|
||||
path := filepath.Join(root, "redirect", "output.txt")
|
||||
if err := WriteFileAtomic(path, []byte("new"), 0o640); err != nil {
|
||||
t.Fatalf("WriteFileAtomic() error = %v", err)
|
||||
}
|
||||
data, err := os.ReadFile(sentinel)
|
||||
data, err := os.ReadFile(filepath.Join(outside, "output.txt"))
|
||||
if err != nil {
|
||||
t.Fatalf("ReadFile(sentinel) error = %v", err)
|
||||
t.Fatalf("ReadFile(output) error = %v", err)
|
||||
}
|
||||
if string(data) != "unchanged" {
|
||||
t.Fatalf("outside sentinel = %q, want unchanged", data)
|
||||
}
|
||||
if _, err := os.Stat(filepath.Join(outside, "output.txt")); !os.IsNotExist(err) {
|
||||
t.Fatalf("outside output exists: stat err = %v", err)
|
||||
if string(data) != "new" {
|
||||
t.Fatalf("outside output = %q, want new", data)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user