Skip to content

Commit

Permalink
tests: skip unix socket tests for non *nix OSes (#1443)
Browse files Browse the repository at this point in the history
* tests: fix unix socket tests on darwin

* tests: add test temp dir implementation for darwin and non darwin
  • Loading branch information
pmalek committed Jun 16, 2023
1 parent d11e3f0 commit 7fadba7
Show file tree
Hide file tree
Showing 3 changed files with 141 additions and 89 deletions.
185 changes: 96 additions & 89 deletions client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,106 +6,114 @@ import (
"errors"
"fmt"
"net"
"path/filepath"
"strconv"
"strings"
"syscall"
"testing"
"time"
)

func TestIsPacketConn(t *testing.T) {
// UDP
s, addrstr, _, err := RunLocalUDPServer(":0")
if err != nil {
t.Fatalf("unable to run test server: %v", err)
}
defer s.Shutdown()
c, err := net.Dial("udp", addrstr)
if err != nil {
t.Fatalf("failed to dial: %v", err)
}
defer c.Close()
if !isPacketConn(c) {
t.Error("UDP connection should be a packet conn")
}
if !isPacketConn(struct{ *net.UDPConn }{c.(*net.UDPConn)}) {
t.Error("UDP connection (wrapped type) should be a packet conn")
}
t.Run("UDP", func(t *testing.T) {
s, addrstr, _, err := RunLocalUDPServer(":0")
if err != nil {
t.Fatalf("unable to run test server: %v", err)
}
defer s.Shutdown()
c, err := net.Dial("udp", addrstr)
if err != nil {
t.Fatalf("failed to dial: %v", err)
}
defer c.Close()
if !isPacketConn(c) {
t.Error("UDP connection should be a packet conn")
}
if !isPacketConn(struct{ *net.UDPConn }{c.(*net.UDPConn)}) {
t.Error("UDP connection (wrapped type) should be a packet conn")
}
})

// TCP
s, addrstr, _, err = RunLocalTCPServer(":0")
if err != nil {
t.Fatalf("unable to run test server: %v", err)
}
defer s.Shutdown()
c, err = net.Dial("tcp", addrstr)
if err != nil {
t.Fatalf("failed to dial: %v", err)
}
defer c.Close()
if isPacketConn(c) {
t.Error("TCP connection should not be a packet conn")
}
if isPacketConn(struct{ *net.TCPConn }{c.(*net.TCPConn)}) {
t.Error("TCP connection (wrapped type) should not be a packet conn")
}
t.Run("TCP", func(t *testing.T) {
s, addrstr, _, err := RunLocalTCPServer(":0")
if err != nil {
t.Fatalf("unable to run test server: %v", err)
}
defer s.Shutdown()
c, err := net.Dial("tcp", addrstr)
if err != nil {
t.Fatalf("failed to dial: %v", err)
}
defer c.Close()
if isPacketConn(c) {
t.Error("TCP connection should not be a packet conn")
}
if isPacketConn(struct{ *net.TCPConn }{c.(*net.TCPConn)}) {
t.Error("TCP connection (wrapped type) should not be a packet conn")
}
})

// Unix datagram
s, addrstr, _, err = RunLocalUnixGramServer(filepath.Join(t.TempDir(), "unixgram.sock"))
if err != nil {
t.Fatalf("unable to run test server: %v", err)
}
defer s.Shutdown()
c, err = net.Dial("unixgram", addrstr)
if err != nil {
t.Fatalf("failed to dial: %v", err)
}
defer c.Close()
if !isPacketConn(c) {
t.Error("Unix datagram connection should be a packet conn")
}
if !isPacketConn(struct{ *net.UnixConn }{c.(*net.UnixConn)}) {
t.Error("Unix datagram connection (wrapped type) should be a packet conn")
}
t.Run("Unix datagram", func(t *testing.T) {
s, addrstr, _, err := RunLocalUnixGramServer(tempFile(t, "unixgram.sock"))
if err != nil {
t.Fatalf("unable to run test server: %v", err)
}
defer s.Shutdown()
c, err := net.Dial("unixgram", addrstr)
if err != nil {
t.Fatalf("failed to dial: %v", err)
}
defer c.Close()
if !isPacketConn(c) {
t.Error("Unix datagram connection should be a packet conn")
}
if !isPacketConn(struct{ *net.UnixConn }{c.(*net.UnixConn)}) {
t.Error("Unix datagram connection (wrapped type) should be a packet conn")
}
})

// Unix Seqpacket
shutChan, addrstr, err := RunLocalUnixSeqPacketServer(filepath.Join(t.TempDir(), "unixpacket.sock"))
if err != nil {
t.Fatalf("unable to run test server: %v", err)
}
t.Run("Unix Seqpacket", func(t *testing.T) {
shutChan, addrstr, err := RunLocalUnixSeqPacketServer(tempFile(t, "unixpacket.sock"))
if err != nil {
if errors.Is(err, syscall.EPROTONOSUPPORT) {
t.Skip("unix seqpacket not supported on this OS")
}
t.Fatalf("unable to run test server: %v", err)
}

defer func() {
shutChan <- &struct{}{}
}()
c, err = net.Dial("unixpacket", addrstr)
if err != nil {
t.Fatalf("failed to dial: %v", err)
}
defer c.Close()
if !isPacketConn(c) {
t.Error("Unix datagram connection should be a packet conn")
}
if !isPacketConn(struct{ *net.UnixConn }{c.(*net.UnixConn)}) {
t.Error("Unix datagram connection (wrapped type) should be a packet conn")
}
defer func() {
shutChan <- &struct{}{}
}()
c, err := net.Dial("unixpacket", addrstr)
if err != nil {
t.Fatalf("failed to dial: %v", err)
}
defer c.Close()
if !isPacketConn(c) {
t.Error("Unix datagram connection should be a packet conn")
}
if !isPacketConn(struct{ *net.UnixConn }{c.(*net.UnixConn)}) {
t.Error("Unix datagram connection (wrapped type) should be a packet conn")
}
})

// Unix stream
s, addrstr, _, err = RunLocalUnixServer(filepath.Join(t.TempDir(), "unixstream.sock"))
if err != nil {
t.Fatalf("unable to run test server: %v", err)
}
defer s.Shutdown()
c, err = net.Dial("unix", addrstr)
if err != nil {
t.Fatalf("failed to dial: %v", err)
}
defer c.Close()
if isPacketConn(c) {
t.Error("Unix stream connection should not be a packet conn")
}
if isPacketConn(struct{ *net.UnixConn }{c.(*net.UnixConn)}) {
t.Error("Unix stream connection (wrapped type) should not be a packet conn")
}
t.Run("Unix stream", func(t *testing.T) {
s, addrstr, _, err := RunLocalUnixServer(tempFile(t, "unixstream.sock"))
if err != nil {
t.Fatalf("unable to run test server: %v", err)
}
defer s.Shutdown()
c, err := net.Dial("unix", addrstr)
if err != nil {
t.Fatalf("failed to dial: %v", err)
}
defer c.Close()
if isPacketConn(c) {
t.Error("Unix stream connection should not be a packet conn")
}
if isPacketConn(struct{ *net.UnixConn }{c.(*net.UnixConn)}) {
t.Error("Unix stream connection (wrapped type) should not be a packet conn")
}
})
}

func TestDialUDP(t *testing.T) {
Expand Down Expand Up @@ -490,7 +498,6 @@ func TestClientConnWriteSinglePacket(t *testing.T) {
m := new(Msg)
m.SetQuestion("miek.nl.", TypeTXT)
err := conn.WriteMsg(m)

if err != nil {
t.Fatalf("failed to write: %v", err)
}
Expand Down
29 changes: 29 additions & 0 deletions tmpdir_darwin_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
//go:build darwin

package dns

import (
"os"
"path/filepath"
"strings"
"testing"
)

// tempDir creates a temporary directory for tests and returns a file path as
// a result of concatenation of said temporary directory path and provided filename.
// The reason for this is to work around some limitations in socket file name
// lengths on darwin.
//
// Ref:
// - https://github.com/golang/go/blob/go1.20.2/src/syscall/ztypes_darwin_arm64.go#L178
// - https://github.com/golang/go/blob/go1.20.2/src/syscall/ztypes_linux_arm64.go#L175
func tempFile(t *testing.T, filename string) string {
t.Helper()

dir, err := os.MkdirTemp("", strings.ReplaceAll(t.Name(), string(filepath.Separator), "-"))
if err != nil {
t.Fatalf("failed to create temp dir: %v", err)
}

return filepath.Join(dir, filename)
}
16 changes: 16 additions & 0 deletions tmpdir_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
//go:build !darwin

package dns

import (
"path/filepath"
"testing"
)

// tempDir creates a temporary directory for tests and returns a file path as
// a result of concatenation of said temporary directory path and provided filename.
func tempFile(t *testing.T, filename string) string {
t.Helper()

return filepath.Join(t.TempDir(), filename)
}

0 comments on commit 7fadba7

Please sign in to comment.