MCPcopy Create free account
hub / github.com/tailscale/tailscale / TestDoesNotOverwriteIrregularFiles

Function TestDoesNotOverwriteIrregularFiles

atomicfile/atomicfile_test.go:17–47  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

15)
16
17func TestDoesNotOverwriteIrregularFiles(t *testing.T) {
18 // Per tailscale/tailscale#7658 as one example, almost any imagined use of
19 // atomicfile.Write should likely not attempt to overwrite an irregular file
20 // such as a device node, socket, or named pipe.
21
22 const filename = "TestDoesNotOverwriteIrregularFiles"
23 var path string
24 // macOS private temp does not allow unix socket creation, but /tmp does.
25 if runtime.GOOS == "darwin" {
26 path = filepath.Join("/tmp", filename)
27 t.Cleanup(func() { os.Remove(path) })
28 } else {
29 path = filepath.Join(t.TempDir(), filename)
30 }
31
32 // The least troublesome thing to make that is not a file is a unix socket.
33 // Making a null device sadly requires root.
34 ln, err := net.ListenUnix("unix", &net.UnixAddr{Name: path, Net: "unix"})
35 if err != nil {
36 t.Fatal(err)
37 }
38 defer ln.Close()
39
40 err = WriteFile(path, []byte("hello"), 0644)
41 if err == nil {
42 t.Fatal("expected error, got nil")
43 }
44 if !strings.Contains(err.Error(), "is not a regular file") {
45 t.Fatalf("unexpected error: %v", err)
46 }
47}

Callers

nothing calls this directly

Calls 10

WriteFileFunction · 0.85
ListenUnixMethod · 0.80
CleanupMethod · 0.65
RemoveMethod · 0.65
TempDirMethod · 0.65
FatalMethod · 0.65
CloseMethod · 0.65
ErrorMethod · 0.65
FatalfMethod · 0.65
ContainsMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…