(t *testing.T)
| 15 | ) |
| 16 | |
| 17 | func 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 | } |
nothing calls this directly
no test coverage detected
searching dependent graphs…