1 // Copyright 2013 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
5 // +build !nacl,!plan9,!windows
19 func TestReadUnixgramWithUnnamedSocket(t
*testing
.T
) {
20 addr
:= testUnixAddr()
21 la
, err
:= ResolveUnixAddr("unixgram", addr
)
23 t
.Fatalf("ResolveUnixAddr failed: %v", err
)
25 c
, err
:= ListenUnixgram("unixgram", la
)
27 t
.Fatalf("ListenUnixgram failed: %v", err
)
34 off
:= make(chan bool)
35 data
:= [5]byte{1, 2, 3, 4, 5}
37 defer func() { off
<- true }()
38 s
, err
:= syscall
.Socket(syscall
.AF_UNIX
, syscall
.SOCK_DGRAM
, 0)
40 t
.Errorf("syscall.Socket failed: %v", err
)
43 defer syscall
.Close(s
)
44 rsa
:= &syscall
.SockaddrUnix
{Name
: addr
}
45 if err
:= syscall
.Sendto(s
, data
[:], 0, rsa
); err
!= nil {
46 t
.Errorf("syscall.Sendto failed: %v", err
)
53 c
.SetReadDeadline(time
.Now().Add(100 * time
.Millisecond
))
54 n
, from
, err
:= c
.ReadFrom(b
)
56 t
.Fatalf("UnixConn.ReadFrom failed: %v", err
)
59 t
.Fatalf("neighbor address is %v", from
)
61 if !bytes
.Equal(b
[:n
], data
[:]) {
62 t
.Fatalf("got %v, want %v", b
[:n
], data
[:])
66 func TestReadUnixgramWithZeroBytesBuffer(t
*testing
.T
) {
67 // issue 4352: Recvfrom failed with "address family not
68 // supported by protocol family" if zero-length buffer provided
70 addr
:= testUnixAddr()
71 la
, err
:= ResolveUnixAddr("unixgram", addr
)
73 t
.Fatalf("ResolveUnixAddr failed: %v", err
)
75 c
, err
:= ListenUnixgram("unixgram", la
)
77 t
.Fatalf("ListenUnixgram failed: %v", err
)
84 off
:= make(chan bool)
86 defer func() { off
<- true }()
87 c
, err
:= DialUnix("unixgram", nil, la
)
89 t
.Errorf("DialUnix failed: %v", err
)
93 if _
, err
:= c
.Write([]byte{1, 2, 3, 4, 5}); err
!= nil {
94 t
.Errorf("UnixConn.Write failed: %v", err
)
100 c
.SetReadDeadline(time
.Now().Add(100 * time
.Millisecond
))
101 _
, from
, err
:= c
.ReadFrom(nil)
103 t
.Fatalf("UnixConn.ReadFrom failed: %v", err
)
106 t
.Fatalf("neighbor address is %v", from
)
110 func TestUnixgramAutobind(t
*testing
.T
) {
111 if runtime
.GOOS
!= "linux" {
112 t
.Skip("skipping: autobind is linux only")
115 laddr
:= &UnixAddr
{Name
: "", Net
: "unixgram"}
116 c1
, err
:= ListenUnixgram("unixgram", laddr
)
118 t
.Fatalf("ListenUnixgram failed: %v", err
)
122 // retrieve the autobind address
123 autoAddr
:= c1
.LocalAddr().(*UnixAddr
)
124 if len(autoAddr
.Name
) <= 1 {
125 t
.Fatalf("invalid autobind address: %v", autoAddr
)
127 if autoAddr
.Name
[0] != '@' {
128 t
.Fatalf("invalid autobind address: %v", autoAddr
)
131 c2
, err
:= DialUnix("unixgram", nil, autoAddr
)
133 t
.Fatalf("DialUnix failed: %v", err
)
137 if !reflect
.DeepEqual(c1
.LocalAddr(), c2
.RemoteAddr()) {
138 t
.Fatalf("expected autobind address %v, got %v", c1
.LocalAddr(), c2
.RemoteAddr())
142 func TestUnixAutobindClose(t
*testing
.T
) {
143 if runtime
.GOOS
!= "linux" {
144 t
.Skip("skipping: autobind is linux only")
146 laddr
:= &UnixAddr
{Name
: "", Net
: "unix"}
147 ln
, err
:= ListenUnix("unix", laddr
)
149 t
.Fatalf("ListenUnix failed: %v", err
)
154 func TestUnixgramWrite(t
*testing
.T
) {
155 addr
:= testUnixAddr()
156 laddr
, err
:= ResolveUnixAddr("unixgram", addr
)
158 t
.Fatalf("ResolveUnixAddr failed: %v", err
)
160 c
, err
:= ListenPacket("unixgram", addr
)
162 t
.Fatalf("ListenPacket failed: %v", err
)
164 defer os
.Remove(addr
)
167 testUnixgramWriteConn(t
, laddr
)
168 testUnixgramWritePacketConn(t
, laddr
)
171 func testUnixgramWriteConn(t
*testing
.T
, raddr
*UnixAddr
) {
172 c
, err
:= Dial("unixgram", raddr
.String())
174 t
.Fatalf("Dial failed: %v", err
)
178 if _
, err
:= c
.(*UnixConn
).WriteToUnix([]byte("Connection-oriented mode socket"), raddr
); err
== nil {
179 t
.Fatal("WriteToUnix should fail")
180 } else if err
.(*OpError
).Err
!= ErrWriteToConnected
{
181 t
.Fatalf("WriteToUnix should fail as ErrWriteToConnected: %v", err
)
183 if _
, err
= c
.(*UnixConn
).WriteTo([]byte("Connection-oriented mode socket"), raddr
); err
== nil {
184 t
.Fatal("WriteTo should fail")
185 } else if err
.(*OpError
).Err
!= ErrWriteToConnected
{
186 t
.Fatalf("WriteTo should fail as ErrWriteToConnected: %v", err
)
188 if _
, _
, err
= c
.(*UnixConn
).WriteMsgUnix([]byte("Connection-oriented mode socket"), nil, raddr
); err
== nil {
189 t
.Fatal("WriteTo should fail")
190 } else if err
.(*OpError
).Err
!= ErrWriteToConnected
{
191 t
.Fatalf("WriteMsgUnix should fail as ErrWriteToConnected: %v", err
)
193 if _
, err
:= c
.Write([]byte("Connection-oriented mode socket")); err
!= nil {
194 t
.Fatalf("Write failed: %v", err
)
198 func testUnixgramWritePacketConn(t
*testing
.T
, raddr
*UnixAddr
) {
199 addr
:= testUnixAddr()
200 c
, err
:= ListenPacket("unixgram", addr
)
202 t
.Fatalf("ListenPacket failed: %v", err
)
204 defer os
.Remove(addr
)
207 if _
, err
:= c
.(*UnixConn
).WriteToUnix([]byte("Connectionless mode socket"), raddr
); err
!= nil {
208 t
.Fatalf("WriteToUnix failed: %v", err
)
210 if _
, err
:= c
.WriteTo([]byte("Connectionless mode socket"), raddr
); err
!= nil {
211 t
.Fatalf("WriteTo failed: %v", err
)
213 if _
, _
, err
:= c
.(*UnixConn
).WriteMsgUnix([]byte("Connectionless mode socket"), nil, raddr
); err
!= nil {
214 t
.Fatalf("WriteMsgUnix failed: %v", err
)
216 if _
, err
:= c
.(*UnixConn
).Write([]byte("Connectionless mode socket")); err
== nil {
217 t
.Fatal("Write should fail")
221 func TestUnixConnLocalAndRemoteNames(t
*testing
.T
) {
222 for _
, laddr
:= range []string{"", testUnixAddr()} {
224 taddr
:= testUnixAddr()
225 ta
, err
:= ResolveUnixAddr("unix", taddr
)
227 t
.Fatalf("ResolveUnixAddr failed: %v", err
)
229 ln
, err
:= ListenUnix("unix", ta
)
231 t
.Fatalf("ListenUnix failed: %v", err
)
238 done
:= make(chan int)
239 go transponder(t
, ln
, done
)
241 la
, err
:= ResolveUnixAddr("unix", laddr
)
243 t
.Fatalf("ResolveUnixAddr failed: %v", err
)
245 c
, err
:= DialUnix("unix", la
, ta
)
247 t
.Fatalf("DialUnix failed: %v", err
)
252 defer os
.Remove(laddr
)
255 if _
, err
:= c
.Write([]byte("UNIXCONN LOCAL AND REMOTE NAME TEST")); err
!= nil {
256 t
.Fatalf("UnixConn.Write failed: %v", err
)
259 if runtime
.GOOS
== "linux" && laddr
== "" {
260 laddr
= "@" // autobind feature
262 var connAddrs
= [3]struct{ got
, want Addr
}{
264 {c
.LocalAddr(), &UnixAddr
{Name
: laddr
, Net
: "unix"}},
265 {c
.RemoteAddr(), ta
},
267 for _
, ca
:= range connAddrs
{
268 if !reflect
.DeepEqual(ca
.got
, ca
.want
) {
269 t
.Fatalf("got %#v, expected %#v", ca
.got
, ca
.want
)
277 func TestUnixgramConnLocalAndRemoteNames(t
*testing
.T
) {
278 for _
, laddr
:= range []string{"", testUnixAddr()} {
280 taddr
:= testUnixAddr()
281 ta
, err
:= ResolveUnixAddr("unixgram", taddr
)
283 t
.Fatalf("ResolveUnixAddr failed: %v", err
)
285 c1
, err
:= ListenUnixgram("unixgram", ta
)
287 t
.Fatalf("ListenUnixgram failed: %v", err
)
296 if la
, err
= ResolveUnixAddr("unixgram", laddr
); err
!= nil {
297 t
.Fatalf("ResolveUnixAddr failed: %v", err
)
300 c2
, err
:= DialUnix("unixgram", la
, ta
)
302 t
.Fatalf("DialUnix failed: %v", err
)
307 defer os
.Remove(laddr
)
311 if runtime
.GOOS
== "linux" && laddr
== "" {
312 laddr
= "@" // autobind feature
314 var connAddrs
= [4]struct{ got
, want Addr
}{
315 {c1
.LocalAddr(), ta
},
316 {c1
.RemoteAddr(), nil},
317 {c2
.LocalAddr(), &UnixAddr
{Name
: laddr
, Net
: "unixgram"}},
318 {c2
.RemoteAddr(), ta
},
320 for _
, ca
:= range connAddrs
{
321 if !reflect
.DeepEqual(ca
.got
, ca
.want
) {
322 t
.Fatalf("got %#v, expected %#v", ca
.got
, ca
.want
)