github.com/roboticscm/goman@v0.0.0-20210203095141-87c07b4a0a55/src/net/net_test.go (about) 1 // Copyright 2009 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 package net 6 7 import ( 8 "io" 9 "io/ioutil" 10 "os" 11 "runtime" 12 "testing" 13 "time" 14 ) 15 16 func TestShutdown(t *testing.T) { 17 if runtime.GOOS == "plan9" { 18 t.Skipf("skipping test on %q", runtime.GOOS) 19 } 20 ln, err := Listen("tcp", "127.0.0.1:0") 21 if err != nil { 22 if ln, err = Listen("tcp6", "[::1]:0"); err != nil { 23 t.Fatalf("ListenTCP on :0: %v", err) 24 } 25 } 26 27 go func() { 28 defer ln.Close() 29 c, err := ln.Accept() 30 if err != nil { 31 t.Errorf("Accept: %v", err) 32 return 33 } 34 var buf [10]byte 35 n, err := c.Read(buf[:]) 36 if n != 0 || err != io.EOF { 37 t.Errorf("server Read = %d, %v; want 0, io.EOF", n, err) 38 return 39 } 40 c.Write([]byte("response")) 41 c.Close() 42 }() 43 44 c, err := Dial("tcp", ln.Addr().String()) 45 if err != nil { 46 t.Fatalf("Dial: %v", err) 47 } 48 defer c.Close() 49 50 err = c.(*TCPConn).CloseWrite() 51 if err != nil { 52 t.Fatalf("CloseWrite: %v", err) 53 } 54 var buf [10]byte 55 n, err := c.Read(buf[:]) 56 if err != nil { 57 t.Fatalf("client Read: %d, %v", n, err) 58 } 59 got := string(buf[:n]) 60 if got != "response" { 61 t.Errorf("read = %q, want \"response\"", got) 62 } 63 } 64 65 func TestShutdownUnix(t *testing.T) { 66 switch runtime.GOOS { 67 case "nacl", "plan9", "windows": 68 t.Skipf("skipping test on %q", runtime.GOOS) 69 } 70 f, err := ioutil.TempFile("", "go_net_unixtest") 71 if err != nil { 72 t.Fatalf("TempFile: %s", err) 73 } 74 f.Close() 75 tmpname := f.Name() 76 os.Remove(tmpname) 77 ln, err := Listen("unix", tmpname) 78 if err != nil { 79 t.Fatalf("ListenUnix on %s: %s", tmpname, err) 80 } 81 defer func() { 82 ln.Close() 83 os.Remove(tmpname) 84 }() 85 86 go func() { 87 c, err := ln.Accept() 88 if err != nil { 89 t.Errorf("Accept: %v", err) 90 return 91 } 92 var buf [10]byte 93 n, err := c.Read(buf[:]) 94 if n != 0 || err != io.EOF { 95 t.Errorf("server Read = %d, %v; want 0, io.EOF", n, err) 96 return 97 } 98 c.Write([]byte("response")) 99 c.Close() 100 }() 101 102 c, err := Dial("unix", tmpname) 103 if err != nil { 104 t.Fatalf("Dial: %v", err) 105 } 106 defer c.Close() 107 108 err = c.(*UnixConn).CloseWrite() 109 if err != nil { 110 t.Fatalf("CloseWrite: %v", err) 111 } 112 var buf [10]byte 113 n, err := c.Read(buf[:]) 114 if err != nil { 115 t.Fatalf("client Read: %d, %v", n, err) 116 } 117 got := string(buf[:n]) 118 if got != "response" { 119 t.Errorf("read = %q, want \"response\"", got) 120 } 121 } 122 123 func TestTCPListenClose(t *testing.T) { 124 ln, err := Listen("tcp", "127.0.0.1:0") 125 if err != nil { 126 t.Fatalf("Listen failed: %v", err) 127 } 128 129 done := make(chan bool, 1) 130 go func() { 131 time.Sleep(100 * time.Millisecond) 132 ln.Close() 133 }() 134 go func() { 135 c, err := ln.Accept() 136 if err == nil { 137 c.Close() 138 t.Error("Accept succeeded") 139 } else { 140 t.Logf("Accept timeout error: %s (any error is fine)", err) 141 } 142 done <- true 143 }() 144 select { 145 case <-done: 146 case <-time.After(2 * time.Second): 147 t.Fatal("timeout waiting for TCP close") 148 } 149 } 150 151 func TestUDPListenClose(t *testing.T) { 152 switch runtime.GOOS { 153 case "plan9": 154 t.Skipf("skipping test on %q", runtime.GOOS) 155 } 156 ln, err := ListenPacket("udp", "127.0.0.1:0") 157 if err != nil { 158 t.Fatalf("Listen failed: %v", err) 159 } 160 161 buf := make([]byte, 1000) 162 done := make(chan bool, 1) 163 go func() { 164 time.Sleep(100 * time.Millisecond) 165 ln.Close() 166 }() 167 go func() { 168 _, _, err = ln.ReadFrom(buf) 169 if err == nil { 170 t.Error("ReadFrom succeeded") 171 } else { 172 t.Logf("ReadFrom timeout error: %s (any error is fine)", err) 173 } 174 done <- true 175 }() 176 select { 177 case <-done: 178 case <-time.After(2 * time.Second): 179 t.Fatal("timeout waiting for UDP close") 180 } 181 } 182 183 func TestTCPClose(t *testing.T) { 184 switch runtime.GOOS { 185 case "plan9": 186 t.Skipf("skipping test on %q", runtime.GOOS) 187 } 188 l, err := Listen("tcp", "127.0.0.1:0") 189 if err != nil { 190 t.Fatal(err) 191 } 192 defer l.Close() 193 194 read := func(r io.Reader) error { 195 var m [1]byte 196 _, err := r.Read(m[:]) 197 return err 198 } 199 200 go func() { 201 c, err := Dial("tcp", l.Addr().String()) 202 if err != nil { 203 t.Errorf("Dial: %v", err) 204 return 205 } 206 207 go read(c) 208 209 time.Sleep(10 * time.Millisecond) 210 c.Close() 211 }() 212 213 c, err := l.Accept() 214 if err != nil { 215 t.Fatal(err) 216 } 217 defer c.Close() 218 219 for err == nil { 220 err = read(c) 221 } 222 if err != nil && err != io.EOF { 223 t.Fatal(err) 224 } 225 } 226 227 func TestErrorNil(t *testing.T) { 228 c, err := Dial("tcp", "127.0.0.1:65535") 229 if err == nil { 230 t.Fatal("Dial 127.0.0.1:65535 succeeded") 231 } 232 if c != nil { 233 t.Fatalf("Dial returned non-nil interface %T(%v) with err != nil", c, c) 234 } 235 236 // Make Listen fail by relistening on the same address. 237 l, err := Listen("tcp", "127.0.0.1:0") 238 if err != nil { 239 t.Fatalf("Listen 127.0.0.1:0: %v", err) 240 } 241 defer l.Close() 242 l1, err := Listen("tcp", l.Addr().String()) 243 if err == nil { 244 t.Fatalf("second Listen %v: %v", l.Addr(), err) 245 } 246 if l1 != nil { 247 t.Fatalf("Listen returned non-nil interface %T(%v) with err != nil", l1, l1) 248 } 249 250 // Make ListenPacket fail by relistening on the same address. 251 lp, err := ListenPacket("udp", "127.0.0.1:0") 252 if err != nil { 253 t.Fatalf("Listen 127.0.0.1:0: %v", err) 254 } 255 defer lp.Close() 256 lp1, err := ListenPacket("udp", lp.LocalAddr().String()) 257 if err == nil { 258 t.Fatalf("second Listen %v: %v", lp.LocalAddr(), err) 259 } 260 if lp1 != nil { 261 t.Fatalf("ListenPacket returned non-nil interface %T(%v) with err != nil", lp1, lp1) 262 } 263 }