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 "testing" 13 "time" 14 ) 15 16 // The full stack test cases for IPConn have been moved to the 17 // following: 18 // golang.org/x/net/ipv4 19 // golang.org/x/net/ipv6 20 // golang.org/x/net/icmp 21 22 func packetConnTestData(t *testing.T, network string) ([]byte, func()) { 23 if !testableNetwork(network) { 24 return nil, func() { t.Logf("skipping %s test", network) } 25 } 26 return []byte("PACKETCONN TEST"), nil 27 } 28 29 var packetConnTests = []struct { 30 net string 31 addr1 string 32 addr2 string 33 }{ 34 {"udp", "127.0.0.1:0", "127.0.0.1:0"}, 35 {"unixgram", testUnixAddr(), testUnixAddr()}, 36 } 37 38 func TestPacketConn(t *testing.T) { 39 closer := func(c PacketConn, net, addr1, addr2 string) { 40 c.Close() 41 switch net { 42 case "unixgram": 43 os.Remove(addr1) 44 os.Remove(addr2) 45 } 46 } 47 48 for _, tt := range packetConnTests { 49 wb, skipOrFatalFn := packetConnTestData(t, tt.net) 50 if skipOrFatalFn != nil { 51 skipOrFatalFn() 52 continue 53 } 54 55 c1, err := ListenPacket(tt.net, tt.addr1) 56 if err != nil { 57 t.Fatal(err) 58 } 59 defer closer(c1, tt.net, tt.addr1, tt.addr2) 60 c1.LocalAddr() 61 c1.SetDeadline(time.Now().Add(500 * time.Millisecond)) 62 c1.SetReadDeadline(time.Now().Add(500 * time.Millisecond)) 63 c1.SetWriteDeadline(time.Now().Add(500 * time.Millisecond)) 64 65 c2, err := ListenPacket(tt.net, tt.addr2) 66 if err != nil { 67 t.Fatal(err) 68 } 69 defer closer(c2, tt.net, tt.addr1, tt.addr2) 70 c2.LocalAddr() 71 c2.SetDeadline(time.Now().Add(500 * time.Millisecond)) 72 c2.SetReadDeadline(time.Now().Add(500 * time.Millisecond)) 73 c2.SetWriteDeadline(time.Now().Add(500 * time.Millisecond)) 74 rb2 := make([]byte, 128) 75 76 if _, err := c1.WriteTo(wb, c2.LocalAddr()); err != nil { 77 t.Fatal(err) 78 } 79 if _, _, err := c2.ReadFrom(rb2); err != nil { 80 t.Fatal(err) 81 } 82 if _, err := c2.WriteTo(wb, c1.LocalAddr()); err != nil { 83 t.Fatal(err) 84 } 85 rb1 := make([]byte, 128) 86 if _, _, err := c1.ReadFrom(rb1); err != nil { 87 t.Fatal(err) 88 } 89 } 90 } 91 92 func TestConnAndPacketConn(t *testing.T) { 93 closer := func(c PacketConn, net, addr1, addr2 string) { 94 c.Close() 95 switch net { 96 case "unixgram": 97 os.Remove(addr1) 98 os.Remove(addr2) 99 } 100 } 101 102 for _, tt := range packetConnTests { 103 var wb []byte 104 wb, skipOrFatalFn := packetConnTestData(t, tt.net) 105 if skipOrFatalFn != nil { 106 skipOrFatalFn() 107 continue 108 } 109 110 c1, err := ListenPacket(tt.net, tt.addr1) 111 if err != nil { 112 t.Fatal(err) 113 } 114 defer closer(c1, tt.net, tt.addr1, tt.addr2) 115 c1.LocalAddr() 116 c1.SetDeadline(time.Now().Add(500 * time.Millisecond)) 117 c1.SetReadDeadline(time.Now().Add(500 * time.Millisecond)) 118 c1.SetWriteDeadline(time.Now().Add(500 * time.Millisecond)) 119 120 c2, err := Dial(tt.net, c1.LocalAddr().String()) 121 if err != nil { 122 t.Fatal(err) 123 } 124 defer c2.Close() 125 c2.LocalAddr() 126 c2.RemoteAddr() 127 c2.SetDeadline(time.Now().Add(500 * time.Millisecond)) 128 c2.SetReadDeadline(time.Now().Add(500 * time.Millisecond)) 129 c2.SetWriteDeadline(time.Now().Add(500 * time.Millisecond)) 130 131 if _, err := c2.Write(wb); err != nil { 132 t.Fatal(err) 133 } 134 rb1 := make([]byte, 128) 135 if _, _, err := c1.ReadFrom(rb1); err != nil { 136 t.Fatal(err) 137 } 138 var dst Addr 139 switch tt.net { 140 case "unixgram": 141 continue 142 default: 143 dst = c2.LocalAddr() 144 } 145 if _, err := c1.WriteTo(wb, dst); err != nil { 146 t.Fatal(err) 147 } 148 rb2 := make([]byte, 128) 149 if _, err := c2.Read(rb2); err != nil { 150 t.Fatal(err) 151 } 152 } 153 } 154