summaryrefslogtreecommitdiff
path: root/internal/config/atomicfile/atomicfile_test.go
diff options
context:
space:
mode:
authorjwijenbergh <jeroenwijenbergh@protonmail.com>2024-06-19 14:53:01 +0200
committerJeroen Wijenbergh <46386452+jwijenbergh@users.noreply.github.com>2024-06-25 12:41:13 +0000
commit8f95d2d8ec2ae7143fe95c59f7f406e0bd1656dc (patch)
treeee1987310037ee17d90a543006cc396c56cefa0b /internal/config/atomicfile/atomicfile_test.go
parent5a06e4f2755e1265f34acc3fb9d819a7a22b7cd0 (diff)
Config: Atomic file writes using tailscale package
Diffstat (limited to 'internal/config/atomicfile/atomicfile_test.go')
-rw-r--r--internal/config/atomicfile/atomicfile_test.go47
1 files changed, 47 insertions, 0 deletions
diff --git a/internal/config/atomicfile/atomicfile_test.go b/internal/config/atomicfile/atomicfile_test.go
new file mode 100644
index 0000000..78c93e6
--- /dev/null
+++ b/internal/config/atomicfile/atomicfile_test.go
@@ -0,0 +1,47 @@
+// Copyright (c) Tailscale Inc & AUTHORS
+// SPDX-License-Identifier: BSD-3-Clause
+
+//go:build !js && !windows
+
+package atomicfile
+
+import (
+ "net"
+ "os"
+ "path/filepath"
+ "runtime"
+ "strings"
+ "testing"
+)
+
+func TestDoesNotOverwriteIrregularFiles(t *testing.T) {
+ // Per tailscale/tailscale#7658 as one example, almost any imagined use of
+ // atomicfile.Write should likely not attempt to overwrite an irregular file
+ // such as a device node, socket, or named pipe.
+
+ const filename = "TestDoesNotOverwriteIrregularFiles"
+ var path string
+ // macOS private temp does not allow unix socket creation, but /tmp does.
+ if runtime.GOOS == "darwin" {
+ path = filepath.Join("/tmp", filename)
+ t.Cleanup(func() { os.Remove(path) })
+ } else {
+ path = filepath.Join(t.TempDir(), filename)
+ }
+
+ // The least troublesome thing to make that is not a file is a unix socket.
+ // Making a null device sadly requires root.
+ l, err := net.ListenUnix("unix", &net.UnixAddr{Name: path, Net: "unix"})
+ if err != nil {
+ t.Fatal(err)
+ }
+ defer l.Close()
+
+ err = WriteFile(path, []byte("hello"), 0644)
+ if err == nil {
+ t.Fatal("expected error, got nil")
+ }
+ if !strings.Contains(err.Error(), "is not a regular file") {
+ t.Fatalf("unexpected error: %v", err)
+ }
+}