Brad Fitzpatrick | 5194744 | 2016-03-01 22:57:46 +0000 | [diff] [blame] | 1 | // Copyright 2013 The Go Authors. All rights reserved. |
Jeff R. Allen | 6563d86 | 2013-01-30 10:02:01 -0800 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style |
| 3 | // license that can be found in the LICENSE file. |
| 4 | |
Tamir Duberstein | 4a61764 | 2021-06-11 18:14:03 -0400 | [diff] [blame] | 5 | //go:build !fuchsia && !js && !plan9 && !windows |
Jeff R. Allen | 6563d86 | 2013-01-30 10:02:01 -0800 | [diff] [blame] | 6 | |
| 7 | package net |
| 8 | |
| 9 | import ( |
| 10 | "bytes" |
Brad Fitzpatrick | 2cefd12 | 2016-04-06 19:02:27 +0000 | [diff] [blame] | 11 | "internal/testenv" |
Jeff R. Allen | 6563d86 | 2013-01-30 10:02:01 -0800 | [diff] [blame] | 12 | "os" |
Albert Strasheim | 309d88e | 2013-02-06 06:45:57 -0800 | [diff] [blame] | 13 | "reflect" |
| 14 | "runtime" |
Jeff R. Allen | 6563d86 | 2013-01-30 10:02:01 -0800 | [diff] [blame] | 15 | "syscall" |
| 16 | "testing" |
| 17 | "time" |
| 18 | ) |
| 19 | |
Jeff R. Allen | 6563d86 | 2013-01-30 10:02:01 -0800 | [diff] [blame] | 20 | func TestReadUnixgramWithUnnamedSocket(t *testing.T) { |
Mikio Hara | 7b2b45e | 2015-03-26 23:26:45 +0900 | [diff] [blame] | 21 | if !testableNetwork("unixgram") { |
| 22 | t.Skip("unixgram test") |
David Crawshaw | ac080fa | 2015-03-09 09:06:11 -0400 | [diff] [blame] | 23 | } |
Brad Fitzpatrick | 2cefd12 | 2016-04-06 19:02:27 +0000 | [diff] [blame] | 24 | if runtime.GOOS == "openbsd" { |
| 25 | testenv.SkipFlaky(t, 15157) |
| 26 | } |
Mikio Hara | 7b2b45e | 2015-03-26 23:26:45 +0900 | [diff] [blame] | 27 | |
Bryan C. Mills | 4b3d8d1 | 2021-12-09 11:55:20 -0500 | [diff] [blame] | 28 | addr := testUnixAddr(t) |
Jeff R. Allen | 6563d86 | 2013-01-30 10:02:01 -0800 | [diff] [blame] | 29 | la, err := ResolveUnixAddr("unixgram", addr) |
| 30 | if err != nil { |
Mikio Hara | f77e10f | 2015-05-01 12:38:42 +0900 | [diff] [blame] | 31 | t.Fatal(err) |
Jeff R. Allen | 6563d86 | 2013-01-30 10:02:01 -0800 | [diff] [blame] | 32 | } |
| 33 | c, err := ListenUnixgram("unixgram", la) |
| 34 | if err != nil { |
Mikio Hara | f77e10f | 2015-05-01 12:38:42 +0900 | [diff] [blame] | 35 | t.Fatal(err) |
Jeff R. Allen | 6563d86 | 2013-01-30 10:02:01 -0800 | [diff] [blame] | 36 | } |
| 37 | defer func() { |
| 38 | c.Close() |
| 39 | os.Remove(addr) |
| 40 | }() |
| 41 | |
| 42 | off := make(chan bool) |
| 43 | data := [5]byte{1, 2, 3, 4, 5} |
Jeff R. Allen | 6563d86 | 2013-01-30 10:02:01 -0800 | [diff] [blame] | 44 | go func() { |
| 45 | defer func() { off <- true }() |
| 46 | s, err := syscall.Socket(syscall.AF_UNIX, syscall.SOCK_DGRAM, 0) |
| 47 | if err != nil { |
Mikio Hara | f77e10f | 2015-05-01 12:38:42 +0900 | [diff] [blame] | 48 | t.Error(err) |
Jeff R. Allen | 6563d86 | 2013-01-30 10:02:01 -0800 | [diff] [blame] | 49 | return |
| 50 | } |
| 51 | defer syscall.Close(s) |
| 52 | rsa := &syscall.SockaddrUnix{Name: addr} |
| 53 | if err := syscall.Sendto(s, data[:], 0, rsa); err != nil { |
Mikio Hara | f77e10f | 2015-05-01 12:38:42 +0900 | [diff] [blame] | 54 | t.Error(err) |
Jeff R. Allen | 6563d86 | 2013-01-30 10:02:01 -0800 | [diff] [blame] | 55 | return |
| 56 | } |
| 57 | }() |
| 58 | |
| 59 | <-off |
| 60 | b := make([]byte, 64) |
| 61 | c.SetReadDeadline(time.Now().Add(100 * time.Millisecond)) |
| 62 | n, from, err := c.ReadFrom(b) |
| 63 | if err != nil { |
Mikio Hara | f77e10f | 2015-05-01 12:38:42 +0900 | [diff] [blame] | 64 | t.Fatal(err) |
Jeff R. Allen | 6563d86 | 2013-01-30 10:02:01 -0800 | [diff] [blame] | 65 | } |
| 66 | if from != nil { |
Mikio Hara | f77e10f | 2015-05-01 12:38:42 +0900 | [diff] [blame] | 67 | t.Fatalf("unexpected peer address: %v", from) |
Jeff R. Allen | 6563d86 | 2013-01-30 10:02:01 -0800 | [diff] [blame] | 68 | } |
| 69 | if !bytes.Equal(b[:n], data[:]) { |
Mikio Hara | f77e10f | 2015-05-01 12:38:42 +0900 | [diff] [blame] | 70 | t.Fatalf("got %v; want %v", b[:n], data[:]) |
Jeff R. Allen | 6563d86 | 2013-01-30 10:02:01 -0800 | [diff] [blame] | 71 | } |
| 72 | } |
| 73 | |
Mikio Hara | 03eb132 | 2015-04-20 23:15:00 +0900 | [diff] [blame] | 74 | func TestUnixgramZeroBytePayload(t *testing.T) { |
| 75 | if !testableNetwork("unixgram") { |
| 76 | t.Skip("unixgram test") |
| 77 | } |
| 78 | |
Bryan C. Mills | b55cbbb | 2021-12-09 11:42:42 -0500 | [diff] [blame] | 79 | c1 := newLocalPacketListener(t, "unixgram") |
Mikio Hara | 03eb132 | 2015-04-20 23:15:00 +0900 | [diff] [blame] | 80 | defer os.Remove(c1.LocalAddr().String()) |
| 81 | defer c1.Close() |
| 82 | |
| 83 | c2, err := Dial("unixgram", c1.LocalAddr().String()) |
| 84 | if err != nil { |
| 85 | t.Fatal(err) |
| 86 | } |
| 87 | defer os.Remove(c2.LocalAddr().String()) |
| 88 | defer c2.Close() |
| 89 | |
| 90 | for _, genericRead := range []bool{false, true} { |
| 91 | n, err := c2.Write(nil) |
| 92 | if err != nil { |
| 93 | t.Fatal(err) |
| 94 | } |
| 95 | if n != 0 { |
| 96 | t.Errorf("got %d; want 0", n) |
| 97 | } |
| 98 | c1.SetReadDeadline(time.Now().Add(100 * time.Millisecond)) |
| 99 | var b [1]byte |
| 100 | var peer Addr |
| 101 | if genericRead { |
| 102 | _, err = c1.(Conn).Read(b[:]) |
| 103 | } else { |
| 104 | _, peer, err = c1.ReadFrom(b[:]) |
| 105 | } |
| 106 | switch err { |
| 107 | case nil: // ReadFrom succeeds |
| 108 | if peer != nil { // peer is connected-mode |
| 109 | t.Fatalf("unexpected peer address: %v", peer) |
| 110 | } |
| 111 | default: // Read may timeout, it depends on the platform |
Ian Lance Taylor | d422f54 | 2020-04-17 15:42:12 -0700 | [diff] [blame] | 112 | if !isDeadlineExceeded(err) { |
Mikio Hara | 03eb132 | 2015-04-20 23:15:00 +0900 | [diff] [blame] | 113 | t.Fatal(err) |
| 114 | } |
| 115 | } |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | func TestUnixgramZeroByteBuffer(t *testing.T) { |
Mikio Hara | 7b2b45e | 2015-03-26 23:26:45 +0900 | [diff] [blame] | 120 | if !testableNetwork("unixgram") { |
| 121 | t.Skip("unixgram test") |
David Crawshaw | ac080fa | 2015-03-09 09:06:11 -0400 | [diff] [blame] | 122 | } |
Jeff R. Allen | 6563d86 | 2013-01-30 10:02:01 -0800 | [diff] [blame] | 123 | // issue 4352: Recvfrom failed with "address family not |
| 124 | // supported by protocol family" if zero-length buffer provided |
| 125 | |
Bryan C. Mills | b55cbbb | 2021-12-09 11:42:42 -0500 | [diff] [blame] | 126 | c1 := newLocalPacketListener(t, "unixgram") |
Mikio Hara | 03eb132 | 2015-04-20 23:15:00 +0900 | [diff] [blame] | 127 | defer os.Remove(c1.LocalAddr().String()) |
| 128 | defer c1.Close() |
Jeff R. Allen | 6563d86 | 2013-01-30 10:02:01 -0800 | [diff] [blame] | 129 | |
Mikio Hara | 03eb132 | 2015-04-20 23:15:00 +0900 | [diff] [blame] | 130 | c2, err := Dial("unixgram", c1.LocalAddr().String()) |
| 131 | if err != nil { |
| 132 | t.Fatal(err) |
| 133 | } |
| 134 | defer os.Remove(c2.LocalAddr().String()) |
| 135 | defer c2.Close() |
| 136 | |
Mikio Hara | f77e10f | 2015-05-01 12:38:42 +0900 | [diff] [blame] | 137 | b := []byte("UNIXGRAM ZERO BYTE BUFFER TEST") |
Mikio Hara | 03eb132 | 2015-04-20 23:15:00 +0900 | [diff] [blame] | 138 | for _, genericRead := range []bool{false, true} { |
| 139 | n, err := c2.Write(b) |
Jeff R. Allen | 6563d86 | 2013-01-30 10:02:01 -0800 | [diff] [blame] | 140 | if err != nil { |
Mikio Hara | 03eb132 | 2015-04-20 23:15:00 +0900 | [diff] [blame] | 141 | t.Fatal(err) |
Jeff R. Allen | 6563d86 | 2013-01-30 10:02:01 -0800 | [diff] [blame] | 142 | } |
Mikio Hara | 03eb132 | 2015-04-20 23:15:00 +0900 | [diff] [blame] | 143 | if n != len(b) { |
| 144 | t.Errorf("got %d; want %d", n, len(b)) |
Jeff R. Allen | 6563d86 | 2013-01-30 10:02:01 -0800 | [diff] [blame] | 145 | } |
Mikio Hara | 03eb132 | 2015-04-20 23:15:00 +0900 | [diff] [blame] | 146 | c1.SetReadDeadline(time.Now().Add(100 * time.Millisecond)) |
| 147 | var peer Addr |
| 148 | if genericRead { |
| 149 | _, err = c1.(Conn).Read(nil) |
| 150 | } else { |
| 151 | _, peer, err = c1.ReadFrom(nil) |
| 152 | } |
| 153 | switch err { |
| 154 | case nil: // ReadFrom succeeds |
| 155 | if peer != nil { // peer is connected-mode |
| 156 | t.Fatalf("unexpected peer address: %v", peer) |
| 157 | } |
| 158 | default: // Read may timeout, it depends on the platform |
Ian Lance Taylor | d422f54 | 2020-04-17 15:42:12 -0700 | [diff] [blame] | 159 | if !isDeadlineExceeded(err) { |
Mikio Hara | 03eb132 | 2015-04-20 23:15:00 +0900 | [diff] [blame] | 160 | t.Fatal(err) |
| 161 | } |
| 162 | } |
Jeff R. Allen | 6563d86 | 2013-01-30 10:02:01 -0800 | [diff] [blame] | 163 | } |
| 164 | } |
Albert Strasheim | 309d88e | 2013-02-06 06:45:57 -0800 | [diff] [blame] | 165 | |
Mikio Hara | 67a5181 | 2014-04-02 19:42:05 +0900 | [diff] [blame] | 166 | func TestUnixgramWrite(t *testing.T) { |
Mikio Hara | 7b2b45e | 2015-03-26 23:26:45 +0900 | [diff] [blame] | 167 | if !testableNetwork("unixgram") { |
| 168 | t.Skip("unixgram test") |
David Crawshaw | ac080fa | 2015-03-09 09:06:11 -0400 | [diff] [blame] | 169 | } |
Mikio Hara | 7b2b45e | 2015-03-26 23:26:45 +0900 | [diff] [blame] | 170 | |
Bryan C. Mills | 4b3d8d1 | 2021-12-09 11:55:20 -0500 | [diff] [blame] | 171 | addr := testUnixAddr(t) |
Mikio Hara | 67a5181 | 2014-04-02 19:42:05 +0900 | [diff] [blame] | 172 | laddr, err := ResolveUnixAddr("unixgram", addr) |
| 173 | if err != nil { |
Mikio Hara | f77e10f | 2015-05-01 12:38:42 +0900 | [diff] [blame] | 174 | t.Fatal(err) |
Mikio Hara | 67a5181 | 2014-04-02 19:42:05 +0900 | [diff] [blame] | 175 | } |
| 176 | c, err := ListenPacket("unixgram", addr) |
| 177 | if err != nil { |
Mikio Hara | f77e10f | 2015-05-01 12:38:42 +0900 | [diff] [blame] | 178 | t.Fatal(err) |
Mikio Hara | 67a5181 | 2014-04-02 19:42:05 +0900 | [diff] [blame] | 179 | } |
| 180 | defer os.Remove(addr) |
| 181 | defer c.Close() |
| 182 | |
| 183 | testUnixgramWriteConn(t, laddr) |
| 184 | testUnixgramWritePacketConn(t, laddr) |
| 185 | } |
| 186 | |
| 187 | func testUnixgramWriteConn(t *testing.T, raddr *UnixAddr) { |
| 188 | c, err := Dial("unixgram", raddr.String()) |
| 189 | if err != nil { |
Mikio Hara | f77e10f | 2015-05-01 12:38:42 +0900 | [diff] [blame] | 190 | t.Fatal(err) |
Mikio Hara | 67a5181 | 2014-04-02 19:42:05 +0900 | [diff] [blame] | 191 | } |
| 192 | defer c.Close() |
| 193 | |
Mikio Hara | f77e10f | 2015-05-01 12:38:42 +0900 | [diff] [blame] | 194 | b := []byte("CONNECTED-MODE SOCKET") |
| 195 | if _, err := c.(*UnixConn).WriteToUnix(b, raddr); err == nil { |
| 196 | t.Fatal("should fail") |
Mikio Hara | 67a5181 | 2014-04-02 19:42:05 +0900 | [diff] [blame] | 197 | } else if err.(*OpError).Err != ErrWriteToConnected { |
Mikio Hara | f77e10f | 2015-05-01 12:38:42 +0900 | [diff] [blame] | 198 | t.Fatalf("should fail as ErrWriteToConnected: %v", err) |
Mikio Hara | 67a5181 | 2014-04-02 19:42:05 +0900 | [diff] [blame] | 199 | } |
Mikio Hara | f77e10f | 2015-05-01 12:38:42 +0900 | [diff] [blame] | 200 | if _, err = c.(*UnixConn).WriteTo(b, raddr); err == nil { |
| 201 | t.Fatal("should fail") |
Mikio Hara | 67a5181 | 2014-04-02 19:42:05 +0900 | [diff] [blame] | 202 | } else if err.(*OpError).Err != ErrWriteToConnected { |
Mikio Hara | f77e10f | 2015-05-01 12:38:42 +0900 | [diff] [blame] | 203 | t.Fatalf("should fail as ErrWriteToConnected: %v", err) |
Mikio Hara | 67a5181 | 2014-04-02 19:42:05 +0900 | [diff] [blame] | 204 | } |
Mikio Hara | f77e10f | 2015-05-01 12:38:42 +0900 | [diff] [blame] | 205 | if _, _, err = c.(*UnixConn).WriteMsgUnix(b, nil, raddr); err == nil { |
| 206 | t.Fatal("should fail") |
Mikio Hara | 67a5181 | 2014-04-02 19:42:05 +0900 | [diff] [blame] | 207 | } else if err.(*OpError).Err != ErrWriteToConnected { |
Mikio Hara | f77e10f | 2015-05-01 12:38:42 +0900 | [diff] [blame] | 208 | t.Fatalf("should fail as ErrWriteToConnected: %v", err) |
Mikio Hara | 67a5181 | 2014-04-02 19:42:05 +0900 | [diff] [blame] | 209 | } |
Mikio Hara | f77e10f | 2015-05-01 12:38:42 +0900 | [diff] [blame] | 210 | if _, err := c.Write(b); err != nil { |
| 211 | t.Fatal(err) |
Mikio Hara | 67a5181 | 2014-04-02 19:42:05 +0900 | [diff] [blame] | 212 | } |
| 213 | } |
| 214 | |
| 215 | func testUnixgramWritePacketConn(t *testing.T, raddr *UnixAddr) { |
Bryan C. Mills | 4b3d8d1 | 2021-12-09 11:55:20 -0500 | [diff] [blame] | 216 | addr := testUnixAddr(t) |
Mikio Hara | 67a5181 | 2014-04-02 19:42:05 +0900 | [diff] [blame] | 217 | c, err := ListenPacket("unixgram", addr) |
| 218 | if err != nil { |
Mikio Hara | f77e10f | 2015-05-01 12:38:42 +0900 | [diff] [blame] | 219 | t.Fatal(err) |
Mikio Hara | 67a5181 | 2014-04-02 19:42:05 +0900 | [diff] [blame] | 220 | } |
| 221 | defer os.Remove(addr) |
| 222 | defer c.Close() |
| 223 | |
Mikio Hara | f77e10f | 2015-05-01 12:38:42 +0900 | [diff] [blame] | 224 | b := []byte("UNCONNECTED-MODE SOCKET") |
| 225 | if _, err := c.(*UnixConn).WriteToUnix(b, raddr); err != nil { |
| 226 | t.Fatal(err) |
Mikio Hara | 67a5181 | 2014-04-02 19:42:05 +0900 | [diff] [blame] | 227 | } |
Mikio Hara | f77e10f | 2015-05-01 12:38:42 +0900 | [diff] [blame] | 228 | if _, err := c.WriteTo(b, raddr); err != nil { |
| 229 | t.Fatal(err) |
Mikio Hara | 67a5181 | 2014-04-02 19:42:05 +0900 | [diff] [blame] | 230 | } |
Mikio Hara | f77e10f | 2015-05-01 12:38:42 +0900 | [diff] [blame] | 231 | if _, _, err := c.(*UnixConn).WriteMsgUnix(b, nil, raddr); err != nil { |
| 232 | t.Fatal(err) |
Mikio Hara | 67a5181 | 2014-04-02 19:42:05 +0900 | [diff] [blame] | 233 | } |
Mikio Hara | f77e10f | 2015-05-01 12:38:42 +0900 | [diff] [blame] | 234 | if _, err := c.(*UnixConn).Write(b); err == nil { |
| 235 | t.Fatal("should fail") |
Mikio Hara | 67a5181 | 2014-04-02 19:42:05 +0900 | [diff] [blame] | 236 | } |
| 237 | } |
| 238 | |
Mikio Hara | bfb32dc | 2013-03-23 22:32:19 +0900 | [diff] [blame] | 239 | func TestUnixConnLocalAndRemoteNames(t *testing.T) { |
Mikio Hara | 7b2b45e | 2015-03-26 23:26:45 +0900 | [diff] [blame] | 240 | if !testableNetwork("unix") { |
| 241 | t.Skip("unix test") |
David Crawshaw | ac080fa | 2015-03-09 09:06:11 -0400 | [diff] [blame] | 242 | } |
Mikio Hara | 7b2b45e | 2015-03-26 23:26:45 +0900 | [diff] [blame] | 243 | |
Mikio Hara | f077505 | 2015-04-02 23:11:39 +0900 | [diff] [blame] | 244 | handler := func(ls *localServer, ln Listener) {} |
Bryan C. Mills | 4b3d8d1 | 2021-12-09 11:55:20 -0500 | [diff] [blame] | 245 | for _, laddr := range []string{"", testUnixAddr(t)} { |
ChaiShushan | 6ab49fb | 2013-08-05 11:59:59 +1000 | [diff] [blame] | 246 | laddr := laddr |
Bryan C. Mills | 4b3d8d1 | 2021-12-09 11:55:20 -0500 | [diff] [blame] | 247 | taddr := testUnixAddr(t) |
Mikio Hara | bfb32dc | 2013-03-23 22:32:19 +0900 | [diff] [blame] | 248 | ta, err := ResolveUnixAddr("unix", taddr) |
| 249 | if err != nil { |
Mikio Hara | f77e10f | 2015-05-01 12:38:42 +0900 | [diff] [blame] | 250 | t.Fatal(err) |
Mikio Hara | bfb32dc | 2013-03-23 22:32:19 +0900 | [diff] [blame] | 251 | } |
| 252 | ln, err := ListenUnix("unix", ta) |
| 253 | if err != nil { |
Mikio Hara | f77e10f | 2015-05-01 12:38:42 +0900 | [diff] [blame] | 254 | t.Fatal(err) |
Mikio Hara | bfb32dc | 2013-03-23 22:32:19 +0900 | [diff] [blame] | 255 | } |
Bryan C. Mills | b55cbbb | 2021-12-09 11:42:42 -0500 | [diff] [blame] | 256 | ls := (&streamListener{Listener: ln}).newLocalServer() |
Mikio Hara | f077505 | 2015-04-02 23:11:39 +0900 | [diff] [blame] | 257 | defer ls.teardown() |
| 258 | if err := ls.buildup(handler); err != nil { |
| 259 | t.Fatal(err) |
| 260 | } |
Mikio Hara | bfb32dc | 2013-03-23 22:32:19 +0900 | [diff] [blame] | 261 | |
| 262 | la, err := ResolveUnixAddr("unix", laddr) |
| 263 | if err != nil { |
Mikio Hara | f77e10f | 2015-05-01 12:38:42 +0900 | [diff] [blame] | 264 | t.Fatal(err) |
Mikio Hara | bfb32dc | 2013-03-23 22:32:19 +0900 | [diff] [blame] | 265 | } |
| 266 | c, err := DialUnix("unix", la, ta) |
| 267 | if err != nil { |
Mikio Hara | f77e10f | 2015-05-01 12:38:42 +0900 | [diff] [blame] | 268 | t.Fatal(err) |
Mikio Hara | bfb32dc | 2013-03-23 22:32:19 +0900 | [diff] [blame] | 269 | } |
| 270 | defer func() { |
| 271 | c.Close() |
| 272 | if la != nil { |
| 273 | defer os.Remove(laddr) |
| 274 | } |
| 275 | }() |
| 276 | if _, err := c.Write([]byte("UNIXCONN LOCAL AND REMOTE NAME TEST")); err != nil { |
Mikio Hara | f77e10f | 2015-05-01 12:38:42 +0900 | [diff] [blame] | 277 | t.Fatal(err) |
Mikio Hara | bfb32dc | 2013-03-23 22:32:19 +0900 | [diff] [blame] | 278 | } |
| 279 | |
David Crawshaw | 8b6dafa | 2014-07-08 13:41:18 -0400 | [diff] [blame] | 280 | switch runtime.GOOS { |
| 281 | case "android", "linux": |
| 282 | if laddr == "" { |
| 283 | laddr = "@" // autobind feature |
| 284 | } |
Mikio Hara | bfb32dc | 2013-03-23 22:32:19 +0900 | [diff] [blame] | 285 | } |
| 286 | var connAddrs = [3]struct{ got, want Addr }{ |
| 287 | {ln.Addr(), ta}, |
| 288 | {c.LocalAddr(), &UnixAddr{Name: laddr, Net: "unix"}}, |
| 289 | {c.RemoteAddr(), ta}, |
| 290 | } |
| 291 | for _, ca := range connAddrs { |
| 292 | if !reflect.DeepEqual(ca.got, ca.want) { |
| 293 | t.Fatalf("got %#v, expected %#v", ca.got, ca.want) |
| 294 | } |
| 295 | } |
Mikio Hara | bfb32dc | 2013-03-23 22:32:19 +0900 | [diff] [blame] | 296 | } |
| 297 | } |
| 298 | |
| 299 | func TestUnixgramConnLocalAndRemoteNames(t *testing.T) { |
Mikio Hara | 7b2b45e | 2015-03-26 23:26:45 +0900 | [diff] [blame] | 300 | if !testableNetwork("unixgram") { |
| 301 | t.Skip("unixgram test") |
David Crawshaw | ac080fa | 2015-03-09 09:06:11 -0400 | [diff] [blame] | 302 | } |
Mikio Hara | 7b2b45e | 2015-03-26 23:26:45 +0900 | [diff] [blame] | 303 | |
Bryan C. Mills | 4b3d8d1 | 2021-12-09 11:55:20 -0500 | [diff] [blame] | 304 | for _, laddr := range []string{"", testUnixAddr(t)} { |
ChaiShushan | 6ab49fb | 2013-08-05 11:59:59 +1000 | [diff] [blame] | 305 | laddr := laddr |
Bryan C. Mills | 4b3d8d1 | 2021-12-09 11:55:20 -0500 | [diff] [blame] | 306 | taddr := testUnixAddr(t) |
Mikio Hara | bfb32dc | 2013-03-23 22:32:19 +0900 | [diff] [blame] | 307 | ta, err := ResolveUnixAddr("unixgram", taddr) |
| 308 | if err != nil { |
Mikio Hara | f77e10f | 2015-05-01 12:38:42 +0900 | [diff] [blame] | 309 | t.Fatal(err) |
Mikio Hara | bfb32dc | 2013-03-23 22:32:19 +0900 | [diff] [blame] | 310 | } |
| 311 | c1, err := ListenUnixgram("unixgram", ta) |
| 312 | if err != nil { |
Mikio Hara | f77e10f | 2015-05-01 12:38:42 +0900 | [diff] [blame] | 313 | t.Fatal(err) |
Mikio Hara | bfb32dc | 2013-03-23 22:32:19 +0900 | [diff] [blame] | 314 | } |
| 315 | defer func() { |
| 316 | c1.Close() |
| 317 | os.Remove(taddr) |
| 318 | }() |
| 319 | |
| 320 | var la *UnixAddr |
| 321 | if laddr != "" { |
Mikio Hara | bfb32dc | 2013-03-23 22:32:19 +0900 | [diff] [blame] | 322 | if la, err = ResolveUnixAddr("unixgram", laddr); err != nil { |
Mikio Hara | f77e10f | 2015-05-01 12:38:42 +0900 | [diff] [blame] | 323 | t.Fatal(err) |
Mikio Hara | bfb32dc | 2013-03-23 22:32:19 +0900 | [diff] [blame] | 324 | } |
| 325 | } |
| 326 | c2, err := DialUnix("unixgram", la, ta) |
| 327 | if err != nil { |
Mikio Hara | f77e10f | 2015-05-01 12:38:42 +0900 | [diff] [blame] | 328 | t.Fatal(err) |
Mikio Hara | bfb32dc | 2013-03-23 22:32:19 +0900 | [diff] [blame] | 329 | } |
| 330 | defer func() { |
| 331 | c2.Close() |
| 332 | if la != nil { |
| 333 | defer os.Remove(laddr) |
| 334 | } |
| 335 | }() |
| 336 | |
David Crawshaw | 8b6dafa | 2014-07-08 13:41:18 -0400 | [diff] [blame] | 337 | switch runtime.GOOS { |
| 338 | case "android", "linux": |
| 339 | if laddr == "" { |
| 340 | laddr = "@" // autobind feature |
| 341 | } |
Mikio Hara | bfb32dc | 2013-03-23 22:32:19 +0900 | [diff] [blame] | 342 | } |
David Crawshaw | 8b6dafa | 2014-07-08 13:41:18 -0400 | [diff] [blame] | 343 | |
Mikio Hara | bfb32dc | 2013-03-23 22:32:19 +0900 | [diff] [blame] | 344 | var connAddrs = [4]struct{ got, want Addr }{ |
| 345 | {c1.LocalAddr(), ta}, |
| 346 | {c1.RemoteAddr(), nil}, |
| 347 | {c2.LocalAddr(), &UnixAddr{Name: laddr, Net: "unixgram"}}, |
| 348 | {c2.RemoteAddr(), ta}, |
| 349 | } |
| 350 | for _, ca := range connAddrs { |
| 351 | if !reflect.DeepEqual(ca.got, ca.want) { |
Mikio Hara | f77e10f | 2015-05-01 12:38:42 +0900 | [diff] [blame] | 352 | t.Fatalf("got %#v; want %#v", ca.got, ca.want) |
Mikio Hara | bfb32dc | 2013-03-23 22:32:19 +0900 | [diff] [blame] | 353 | } |
| 354 | } |
Albert Strasheim | 309d88e | 2013-02-06 06:45:57 -0800 | [diff] [blame] | 355 | } |
| 356 | } |
Russ Cox | 18d27b2 | 2015-08-18 22:19:58 -0400 | [diff] [blame] | 357 | |
Russ Cox | 2190750 | 2015-12-10 23:51:36 -0500 | [diff] [blame] | 358 | func TestUnixUnlink(t *testing.T) { |
| 359 | if !testableNetwork("unix") { |
| 360 | t.Skip("unix test") |
| 361 | } |
Bryan C. Mills | 4b3d8d1 | 2021-12-09 11:55:20 -0500 | [diff] [blame] | 362 | name := testUnixAddr(t) |
Russ Cox | eb88b3e | 2016-10-26 20:23:03 -0400 | [diff] [blame] | 363 | |
| 364 | listen := func(t *testing.T) *UnixListener { |
| 365 | l, err := Listen("unix", name) |
| 366 | if err != nil { |
| 367 | t.Fatal(err) |
| 368 | } |
| 369 | return l.(*UnixListener) |
Russ Cox | 2190750 | 2015-12-10 23:51:36 -0500 | [diff] [blame] | 370 | } |
Russ Cox | eb88b3e | 2016-10-26 20:23:03 -0400 | [diff] [blame] | 371 | checkExists := func(t *testing.T, desc string) { |
| 372 | if _, err := os.Stat(name); err != nil { |
| 373 | t.Fatalf("unix socket does not exist %s: %v", desc, err) |
| 374 | } |
Russ Cox | 2190750 | 2015-12-10 23:51:36 -0500 | [diff] [blame] | 375 | } |
Russ Cox | eb88b3e | 2016-10-26 20:23:03 -0400 | [diff] [blame] | 376 | checkNotExists := func(t *testing.T, desc string) { |
| 377 | if _, err := os.Stat(name); err == nil { |
| 378 | t.Fatalf("unix socket does exist %s: %v", desc, err) |
| 379 | } |
Russ Cox | 2190750 | 2015-12-10 23:51:36 -0500 | [diff] [blame] | 380 | } |
Russ Cox | eb88b3e | 2016-10-26 20:23:03 -0400 | [diff] [blame] | 381 | |
| 382 | // Listener should remove on close. |
| 383 | t.Run("Listen", func(t *testing.T) { |
| 384 | l := listen(t) |
| 385 | checkExists(t, "after Listen") |
| 386 | l.Close() |
| 387 | checkNotExists(t, "after Listener close") |
| 388 | }) |
| 389 | |
| 390 | // FileListener should not. |
| 391 | t.Run("FileListener", func(t *testing.T) { |
| 392 | l := listen(t) |
| 393 | f, _ := l.File() |
| 394 | l1, _ := FileListener(f) |
| 395 | checkExists(t, "after FileListener") |
| 396 | f.Close() |
| 397 | checkExists(t, "after File close") |
| 398 | l1.Close() |
| 399 | checkExists(t, "after FileListener close") |
| 400 | l.Close() |
| 401 | checkNotExists(t, "after Listener close") |
| 402 | }) |
| 403 | |
| 404 | // Only first call to l.Close should remove. |
| 405 | t.Run("SecondClose", func(t *testing.T) { |
| 406 | l := listen(t) |
| 407 | checkExists(t, "after Listen") |
| 408 | l.Close() |
| 409 | checkNotExists(t, "after Listener close") |
Russ Cox | 4f1b0a4 | 2020-10-29 14:17:47 -0400 | [diff] [blame] | 410 | if err := os.WriteFile(name, []byte("hello world"), 0666); err != nil { |
Russ Cox | eb88b3e | 2016-10-26 20:23:03 -0400 | [diff] [blame] | 411 | t.Fatalf("cannot recreate socket file: %v", err) |
| 412 | } |
| 413 | checkExists(t, "after writing temp file") |
| 414 | l.Close() |
| 415 | checkExists(t, "after second Listener close") |
| 416 | os.Remove(name) |
| 417 | }) |
| 418 | |
| 419 | // SetUnlinkOnClose should do what it says. |
| 420 | |
| 421 | t.Run("Listen/SetUnlinkOnClose(true)", func(t *testing.T) { |
| 422 | l := listen(t) |
| 423 | checkExists(t, "after Listen") |
| 424 | l.SetUnlinkOnClose(true) |
| 425 | l.Close() |
| 426 | checkNotExists(t, "after Listener close") |
| 427 | }) |
| 428 | |
| 429 | t.Run("Listen/SetUnlinkOnClose(false)", func(t *testing.T) { |
| 430 | l := listen(t) |
| 431 | checkExists(t, "after Listen") |
| 432 | l.SetUnlinkOnClose(false) |
| 433 | l.Close() |
| 434 | checkExists(t, "after Listener close") |
| 435 | os.Remove(name) |
| 436 | }) |
| 437 | |
| 438 | t.Run("FileListener/SetUnlinkOnClose(true)", func(t *testing.T) { |
| 439 | l := listen(t) |
| 440 | f, _ := l.File() |
| 441 | l1, _ := FileListener(f) |
| 442 | checkExists(t, "after FileListener") |
| 443 | l1.(*UnixListener).SetUnlinkOnClose(true) |
| 444 | f.Close() |
| 445 | checkExists(t, "after File close") |
| 446 | l1.Close() |
| 447 | checkNotExists(t, "after FileListener close") |
| 448 | l.Close() |
| 449 | }) |
| 450 | |
| 451 | t.Run("FileListener/SetUnlinkOnClose(false)", func(t *testing.T) { |
| 452 | l := listen(t) |
| 453 | f, _ := l.File() |
| 454 | l1, _ := FileListener(f) |
| 455 | checkExists(t, "after FileListener") |
| 456 | l1.(*UnixListener).SetUnlinkOnClose(false) |
| 457 | f.Close() |
| 458 | checkExists(t, "after File close") |
| 459 | l1.Close() |
| 460 | checkExists(t, "after FileListener close") |
| 461 | l.Close() |
| 462 | }) |
Russ Cox | 2190750 | 2015-12-10 23:51:36 -0500 | [diff] [blame] | 463 | } |