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