github.com/flyinox/gosm@v0.0.0-20171117061539-16768cb62077/src/net/protoconn_test.go (about) 1 // Copyright 2012 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. 4 5 // This file implements API tests across platforms and will never have a build 6 // tag. 7 8 package net 9 10 import ( 11 "os" 12 "runtime" 13 "testing" 14 "time" 15 ) 16 17 // The full stack test cases for IPConn have been moved to the 18 // following: 19 // golang.org/x/net/ipv4 20 // golang.org/x/net/ipv6 21 // golang.org/x/net/icmp 22 23 func TestTCPListenerSpecificMethods(t *testing.T) { 24 switch runtime.GOOS { 25 case "plan9": 26 t.Skipf("not supported on %s", runtime.GOOS) 27 } 28 29 la, err := ResolveTCPAddr("tcp4", "127.0.0.1:0") 30 if err != nil { 31 t.Fatal(err) 32 } 33 ln, err := ListenTCP("tcp4", la) 34 if err != nil { 35 t.Fatal(err) 36 } 37 defer ln.Close() 38 ln.Addr() 39 ln.SetDeadline(time.Now().Add(30 * time.Nanosecond)) 40 41 if c, err := ln.Accept(); err != nil { 42 if !err.(Error).Timeout() { 43 t.Fatal(err) 44 } 45 } else { 46 c.Close() 47 } 48 if c, err := ln.AcceptTCP(); err != nil { 49 if !err.(Error).Timeout() { 50 t.Fatal(err) 51 } 52 } else { 53 c.Close() 54 } 55 56 if f, err := ln.File(); err != nil { 57 condFatalf(t, "%v", err) 58 } else { 59 f.Close() 60 } 61 } 62 63 func TestTCPConnSpecificMethods(t *testing.T) { 64 la, err := ResolveTCPAddr("tcp4", "127.0.0.1:0") 65 if err != nil { 66 t.Fatal(err) 67 } 68 ln, err := ListenTCP("tcp4", la) 69 if err != nil { 70 t.Fatal(err) 71 } 72 ch := make(chan error, 1) 73 handler := func(ls *localServer, ln Listener) { transponder(ls.Listener, ch) } 74 ls, err := (&streamListener{Listener: ln}).newLocalServer() 75 if err != nil { 76 t.Fatal(err) 77 } 78 defer ls.teardown() 79 if err := ls.buildup(handler); err != nil { 80 t.Fatal(err) 81 } 82 83 ra, err := ResolveTCPAddr("tcp4", ls.Listener.Addr().String()) 84 if err != nil { 85 t.Fatal(err) 86 } 87 c, err := DialTCP("tcp4", nil, ra) 88 if err != nil { 89 t.Fatal(err) 90 } 91 defer c.Close() 92 c.SetKeepAlive(false) 93 c.SetKeepAlivePeriod(3 * time.Second) 94 c.SetLinger(0) 95 c.SetNoDelay(false) 96 c.LocalAddr() 97 c.RemoteAddr() 98 c.SetDeadline(time.Now().Add(someTimeout)) 99 c.SetReadDeadline(time.Now().Add(someTimeout)) 100 c.SetWriteDeadline(time.Now().Add(someTimeout)) 101 102 if _, err := c.Write([]byte("TCPCONN TEST")); err != nil { 103 t.Fatal(err) 104 } 105 rb := make([]byte, 128) 106 if _, err := c.Read(rb); err != nil { 107 t.Fatal(err) 108 } 109 110 for err := range ch { 111 t.Error(err) 112 } 113 } 114 115 func TestUDPConnSpecificMethods(t *testing.T) { 116 la, err := ResolveUDPAddr("udp4", "127.0.0.1:0") 117 if err != nil { 118 t.Fatal(err) 119 } 120 c, err := ListenUDP("udp4", la) 121 if err != nil { 122 t.Fatal(err) 123 } 124 defer c.Close() 125 c.LocalAddr() 126 c.RemoteAddr() 127 c.SetDeadline(time.Now().Add(someTimeout)) 128 c.SetReadDeadline(time.Now().Add(someTimeout)) 129 c.SetWriteDeadline(time.Now().Add(someTimeout)) 130 c.SetReadBuffer(2048) 131 c.SetWriteBuffer(2048) 132 133 wb := []byte("UDPCONN TEST") 134 rb := make([]byte, 128) 135 if _, err := c.WriteToUDP(wb, c.LocalAddr().(*UDPAddr)); err != nil { 136 t.Fatal(err) 137 } 138 if _, _, err := c.ReadFromUDP(rb); err != nil { 139 t.Fatal(err) 140 } 141 if _, _, err := c.WriteMsgUDP(wb, nil, c.LocalAddr().(*UDPAddr)); err != nil { 142 condFatalf(t, "%v", err) 143 } 144 if _, _, _, _, err := c.ReadMsgUDP(rb, nil); err != nil { 145 condFatalf(t, "%v", err) 146 } 147 148 if f, err := c.File(); err != nil { 149 condFatalf(t, "%v", err) 150 } else { 151 f.Close() 152 } 153 154 defer func() { 155 if p := recover(); p != nil { 156 t.Fatalf("panicked: %v", p) 157 } 158 }() 159 160 c.WriteToUDP(wb, nil) 161 c.WriteMsgUDP(wb, nil, nil) 162 } 163 164 func TestIPConnSpecificMethods(t *testing.T) { 165 if os.Getuid() != 0 { 166 t.Skip("must be root") 167 } 168 169 la, err := ResolveIPAddr("ip4", "127.0.0.1") 170 if err != nil { 171 t.Fatal(err) 172 } 173 c, err := ListenIP("ip4:icmp", la) 174 if err != nil { 175 t.Fatal(err) 176 } 177 defer c.Close() 178 c.LocalAddr() 179 c.RemoteAddr() 180 c.SetDeadline(time.Now().Add(someTimeout)) 181 c.SetReadDeadline(time.Now().Add(someTimeout)) 182 c.SetWriteDeadline(time.Now().Add(someTimeout)) 183 c.SetReadBuffer(2048) 184 c.SetWriteBuffer(2048) 185 186 if f, err := c.File(); err != nil { 187 condFatalf(t, "%v", err) 188 } else { 189 f.Close() 190 } 191 192 defer func() { 193 if p := recover(); p != nil { 194 t.Fatalf("panicked: %v", p) 195 } 196 }() 197 198 wb := []byte("IPCONN TEST") 199 c.WriteToIP(wb, nil) 200 c.WriteMsgIP(wb, nil, nil) 201 } 202 203 func TestUnixListenerSpecificMethods(t *testing.T) { 204 if !testableNetwork("unix") { 205 t.Skip("unix test") 206 } 207 208 addr := testUnixAddr() 209 la, err := ResolveUnixAddr("unix", addr) 210 if err != nil { 211 t.Fatal(err) 212 } 213 ln, err := ListenUnix("unix", la) 214 if err != nil { 215 t.Fatal(err) 216 } 217 defer ln.Close() 218 defer os.Remove(addr) 219 ln.Addr() 220 ln.SetDeadline(time.Now().Add(30 * time.Nanosecond)) 221 222 if c, err := ln.Accept(); err != nil { 223 if !err.(Error).Timeout() { 224 t.Fatal(err) 225 } 226 } else { 227 c.Close() 228 } 229 if c, err := ln.AcceptUnix(); err != nil { 230 if !err.(Error).Timeout() { 231 t.Fatal(err) 232 } 233 } else { 234 c.Close() 235 } 236 237 if f, err := ln.File(); err != nil { 238 t.Fatal(err) 239 } else { 240 f.Close() 241 } 242 } 243 244 func TestUnixConnSpecificMethods(t *testing.T) { 245 if !testableNetwork("unixgram") { 246 t.Skip("unixgram test") 247 } 248 249 addr1, addr2, addr3 := testUnixAddr(), testUnixAddr(), testUnixAddr() 250 251 a1, err := ResolveUnixAddr("unixgram", addr1) 252 if err != nil { 253 t.Fatal(err) 254 } 255 c1, err := DialUnix("unixgram", a1, nil) 256 if err != nil { 257 t.Fatal(err) 258 } 259 defer c1.Close() 260 defer os.Remove(addr1) 261 c1.LocalAddr() 262 c1.RemoteAddr() 263 c1.SetDeadline(time.Now().Add(someTimeout)) 264 c1.SetReadDeadline(time.Now().Add(someTimeout)) 265 c1.SetWriteDeadline(time.Now().Add(someTimeout)) 266 c1.SetReadBuffer(2048) 267 c1.SetWriteBuffer(2048) 268 269 a2, err := ResolveUnixAddr("unixgram", addr2) 270 if err != nil { 271 t.Fatal(err) 272 } 273 c2, err := DialUnix("unixgram", a2, nil) 274 if err != nil { 275 t.Fatal(err) 276 } 277 defer c2.Close() 278 defer os.Remove(addr2) 279 c2.LocalAddr() 280 c2.RemoteAddr() 281 c2.SetDeadline(time.Now().Add(someTimeout)) 282 c2.SetReadDeadline(time.Now().Add(someTimeout)) 283 c2.SetWriteDeadline(time.Now().Add(someTimeout)) 284 c2.SetReadBuffer(2048) 285 c2.SetWriteBuffer(2048) 286 287 a3, err := ResolveUnixAddr("unixgram", addr3) 288 if err != nil { 289 t.Fatal(err) 290 } 291 c3, err := ListenUnixgram("unixgram", a3) 292 if err != nil { 293 t.Fatal(err) 294 } 295 defer c3.Close() 296 defer os.Remove(addr3) 297 c3.LocalAddr() 298 c3.RemoteAddr() 299 c3.SetDeadline(time.Now().Add(someTimeout)) 300 c3.SetReadDeadline(time.Now().Add(someTimeout)) 301 c3.SetWriteDeadline(time.Now().Add(someTimeout)) 302 c3.SetReadBuffer(2048) 303 c3.SetWriteBuffer(2048) 304 305 wb := []byte("UNIXCONN TEST") 306 rb1 := make([]byte, 128) 307 rb2 := make([]byte, 128) 308 rb3 := make([]byte, 128) 309 if _, _, err := c1.WriteMsgUnix(wb, nil, a2); err != nil { 310 t.Fatal(err) 311 } 312 if _, _, _, _, err := c2.ReadMsgUnix(rb2, nil); err != nil { 313 t.Fatal(err) 314 } 315 if _, err := c2.WriteToUnix(wb, a1); err != nil { 316 t.Fatal(err) 317 } 318 if _, _, err := c1.ReadFromUnix(rb1); err != nil { 319 t.Fatal(err) 320 } 321 if _, err := c3.WriteToUnix(wb, a1); err != nil { 322 t.Fatal(err) 323 } 324 if _, _, err := c1.ReadFromUnix(rb1); err != nil { 325 t.Fatal(err) 326 } 327 if _, err := c2.WriteToUnix(wb, a3); err != nil { 328 t.Fatal(err) 329 } 330 if _, _, err := c3.ReadFromUnix(rb3); err != nil { 331 t.Fatal(err) 332 } 333 334 if f, err := c1.File(); err != nil { 335 t.Fatal(err) 336 } else { 337 f.Close() 338 } 339 340 defer func() { 341 if p := recover(); p != nil { 342 t.Fatalf("panicked: %v", p) 343 } 344 }() 345 346 c1.WriteToUnix(wb, nil) 347 c1.WriteMsgUnix(wb, nil, nil) 348 c3.WriteToUnix(wb, nil) 349 c3.WriteMsgUnix(wb, nil, nil) 350 }