Revert r215321.
[official-gcc.git] / libgo / go / net / unix_test.go
blob05643ddf9aeae6dcdb1989f22a1c8b6d29b80cb8
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
7 package net
9 import (
10 "bytes"
11 "os"
12 "reflect"
13 "runtime"
14 "syscall"
15 "testing"
16 "time"
19 func TestReadUnixgramWithUnnamedSocket(t *testing.T) {
20 addr := testUnixAddr()
21 la, err := ResolveUnixAddr("unixgram", addr)
22 if err != nil {
23 t.Fatalf("ResolveUnixAddr failed: %v", err)
25 c, err := ListenUnixgram("unixgram", la)
26 if err != nil {
27 t.Fatalf("ListenUnixgram failed: %v", err)
29 defer func() {
30 c.Close()
31 os.Remove(addr)
32 }()
34 off := make(chan bool)
35 data := [5]byte{1, 2, 3, 4, 5}
36 go func() {
37 defer func() { off <- true }()
38 s, err := syscall.Socket(syscall.AF_UNIX, syscall.SOCK_DGRAM, 0)
39 if err != nil {
40 t.Errorf("syscall.Socket failed: %v", err)
41 return
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)
47 return
49 }()
51 <-off
52 b := make([]byte, 64)
53 c.SetReadDeadline(time.Now().Add(100 * time.Millisecond))
54 n, from, err := c.ReadFrom(b)
55 if err != nil {
56 t.Fatalf("UnixConn.ReadFrom failed: %v", err)
58 if from != nil {
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)
72 if err != nil {
73 t.Fatalf("ResolveUnixAddr failed: %v", err)
75 c, err := ListenUnixgram("unixgram", la)
76 if err != nil {
77 t.Fatalf("ListenUnixgram failed: %v", err)
79 defer func() {
80 c.Close()
81 os.Remove(addr)
82 }()
84 off := make(chan bool)
85 go func() {
86 defer func() { off <- true }()
87 c, err := DialUnix("unixgram", nil, la)
88 if err != nil {
89 t.Errorf("DialUnix failed: %v", err)
90 return
92 defer c.Close()
93 if _, err := c.Write([]byte{1, 2, 3, 4, 5}); err != nil {
94 t.Errorf("UnixConn.Write failed: %v", err)
95 return
97 }()
99 <-off
100 c.SetReadDeadline(time.Now().Add(100 * time.Millisecond))
101 _, from, err := c.ReadFrom(nil)
102 if err != nil {
103 t.Fatalf("UnixConn.ReadFrom failed: %v", err)
105 if from != nil {
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)
117 if err != nil {
118 t.Fatalf("ListenUnixgram failed: %v", err)
120 defer c1.Close()
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)
132 if err != nil {
133 t.Fatalf("DialUnix failed: %v", err)
135 defer c2.Close()
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)
148 if err != nil {
149 t.Fatalf("ListenUnix failed: %v", err)
151 ln.Close()
154 func TestUnixgramWrite(t *testing.T) {
155 addr := testUnixAddr()
156 laddr, err := ResolveUnixAddr("unixgram", addr)
157 if err != nil {
158 t.Fatalf("ResolveUnixAddr failed: %v", err)
160 c, err := ListenPacket("unixgram", addr)
161 if err != nil {
162 t.Fatalf("ListenPacket failed: %v", err)
164 defer os.Remove(addr)
165 defer c.Close()
167 testUnixgramWriteConn(t, laddr)
168 testUnixgramWritePacketConn(t, laddr)
171 func testUnixgramWriteConn(t *testing.T, raddr *UnixAddr) {
172 c, err := Dial("unixgram", raddr.String())
173 if err != nil {
174 t.Fatalf("Dial failed: %v", err)
176 defer c.Close()
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)
201 if err != nil {
202 t.Fatalf("ListenPacket failed: %v", err)
204 defer os.Remove(addr)
205 defer c.Close()
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()} {
223 laddr := laddr
224 taddr := testUnixAddr()
225 ta, err := ResolveUnixAddr("unix", taddr)
226 if err != nil {
227 t.Fatalf("ResolveUnixAddr failed: %v", err)
229 ln, err := ListenUnix("unix", ta)
230 if err != nil {
231 t.Fatalf("ListenUnix failed: %v", err)
233 defer func() {
234 ln.Close()
235 os.Remove(taddr)
238 done := make(chan int)
239 go transponder(t, ln, done)
241 la, err := ResolveUnixAddr("unix", laddr)
242 if err != nil {
243 t.Fatalf("ResolveUnixAddr failed: %v", err)
245 c, err := DialUnix("unix", la, ta)
246 if err != nil {
247 t.Fatalf("DialUnix failed: %v", err)
249 defer func() {
250 c.Close()
251 if la != nil {
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 }{
263 {ln.Addr(), ta},
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)
273 <-done
277 func TestUnixgramConnLocalAndRemoteNames(t *testing.T) {
278 for _, laddr := range []string{"", testUnixAddr()} {
279 laddr := laddr
280 taddr := testUnixAddr()
281 ta, err := ResolveUnixAddr("unixgram", taddr)
282 if err != nil {
283 t.Fatalf("ResolveUnixAddr failed: %v", err)
285 c1, err := ListenUnixgram("unixgram", ta)
286 if err != nil {
287 t.Fatalf("ListenUnixgram failed: %v", err)
289 defer func() {
290 c1.Close()
291 os.Remove(taddr)
294 var la *UnixAddr
295 if laddr != "" {
296 if la, err = ResolveUnixAddr("unixgram", laddr); err != nil {
297 t.Fatalf("ResolveUnixAddr failed: %v", err)
300 c2, err := DialUnix("unixgram", la, ta)
301 if err != nil {
302 t.Fatalf("DialUnix failed: %v", err)
304 defer func() {
305 c2.Close()
306 if la != nil {
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)