Skip to content

Commit

Permalink
Add back support for *net.UnixCon with seqpacket type (#1378)
Browse files Browse the repository at this point in the history
This was broken by PR: #1322
  • Loading branch information
joliveirinha authored Jun 8, 2022
1 parent eb4745b commit ff611cd
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 1 deletion.
2 changes: 1 addition & 1 deletion client.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func isPacketConn(c net.Conn) bool {
}

if ua, ok := c.LocalAddr().(*net.UnixAddr); ok {
return ua.Net == "unixgram"
return ua.Net == "unixgram" || ua.Net == "unixpacket"
}

return true
Expand Down
21 changes: 21 additions & 0 deletions client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,27 @@ func TestIsPacketConn(t *testing.T) {
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)
}

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 {
Expand Down
15 changes: 15 additions & 0 deletions server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,21 @@ func RunLocalUnixGramServer(laddr string, opts ...func(*Server)) (*Server, strin
return RunLocalServer(pc, nil, opts...)
}

func RunLocalUnixSeqPacketServer(laddr string) (chan interface{}, string, error) {
pc, err := net.Listen("unixpacket", laddr)
if err != nil {
return nil, "", err
}

shutdownChan := make(chan interface{})
go func() {
pc.Accept()
<-shutdownChan
}()

return shutdownChan, pc.Addr().String(), nil
}

func TestServing(t *testing.T) {
for _, tc := range []struct {
name string
Expand Down

0 comments on commit ff611cd

Please sign in to comment.