@ -7,7 +7,9 @@ package atomicfile
import (
import (
"net"
"net"
"os"
"path/filepath"
"path/filepath"
"runtime"
"strings"
"strings"
"testing"
"testing"
)
)
@ -17,18 +19,25 @@ func TestDoesNotOverwriteIrregularFiles(t *testing.T) {
// atomicfile.Write should likely not attempt to overwrite an irregular file
// atomicfile.Write should likely not attempt to overwrite an irregular file
// such as a device node, socket, or named pipe.
// such as a device node, socket, or named pipe.
d := t . TempDir ( )
const filename = "TestDoesNotOverwriteIrregularFiles"
special := filepath . Join ( d , "special" )
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.
// The least troublesome thing to make that is not a file is a unix socket.
// Making a null device sadly requries root.
// Making a null device sadly requries root.
l , err := net . ListenUnix ( "unix" , & net . UnixAddr { Name : special , Net : "unix" } )
l , err := net . ListenUnix ( "unix" , & net . UnixAddr { Name : path , Net : "unix" } )
if err != nil {
if err != nil {
t . Fatal ( err )
t . Fatal ( err )
}
}
defer l . Close ( )
defer l . Close ( )
err = WriteFile ( special , [ ] byte ( "hello" ) , 0644 )
err = WriteFile ( path , [ ] byte ( "hello" ) , 0644 )
if err == nil {
if err == nil {
t . Fatal ( "expected error, got nil" )
t . Fatal ( "expected error, got nil" )
}
}