* gcc.dg/guality/guality.exp: Skip on AIX.
[official-gcc.git] / libgo / go / net / packetconn_test.go
blobec5dd710f5533a9a9cbd9a3e2c1426771f1bb7fc
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.
5 // This file implements API tests across platforms and will never have a build
6 // tag.
8 package net
10 import (
11 "os"
12 "runtime"
13 "strings"
14 "testing"
15 "time"
18 func strfunc(s string) func() string {
19 return func() string {
20 return s
24 var packetConnTests = []struct {
25 net string
26 addr1 func() string
27 addr2 func() string
29 {"udp", strfunc("127.0.0.1:0"), strfunc("127.0.0.1:0")},
30 {"ip:icmp", strfunc("127.0.0.1"), strfunc("127.0.0.1")},
31 {"unixgram", testUnixAddr, testUnixAddr},
34 func TestPacketConn(t *testing.T) {
35 closer := func(c PacketConn, net, addr1, addr2 string) {
36 c.Close()
37 switch net {
38 case "unixgram":
39 os.Remove(addr1)
40 os.Remove(addr2)
44 for i, tt := range packetConnTests {
45 var wb []byte
46 netstr := strings.Split(tt.net, ":")
47 switch netstr[0] {
48 case "udp":
49 wb = []byte("UDP PACKETCONN TEST")
50 case "ip":
51 switch runtime.GOOS {
52 case "plan9":
53 continue
55 if os.Getuid() != 0 {
56 continue
58 var err error
59 wb, err = (&icmpMessage{
60 Type: icmpv4EchoRequest, Code: 0,
61 Body: &icmpEcho{
62 ID: os.Getpid() & 0xffff, Seq: i + 1,
63 Data: []byte("IP PACKETCONN TEST"),
65 }).Marshal()
66 if err != nil {
67 t.Fatalf("icmpMessage.Marshal failed: %v", err)
69 case "unixgram":
70 switch runtime.GOOS {
71 case "plan9", "windows":
72 continue
74 wb = []byte("UNIXGRAM PACKETCONN TEST")
75 default:
76 continue
79 addr1, addr2 := tt.addr1(), tt.addr2()
80 c1, err := ListenPacket(tt.net, addr1)
81 if err != nil {
82 t.Fatalf("ListenPacket failed: %v", err)
84 defer closer(c1, netstr[0], addr1, addr2)
85 c1.LocalAddr()
86 c1.SetDeadline(time.Now().Add(100 * time.Millisecond))
87 c1.SetReadDeadline(time.Now().Add(100 * time.Millisecond))
88 c1.SetWriteDeadline(time.Now().Add(100 * time.Millisecond))
90 c2, err := ListenPacket(tt.net, addr2)
91 if err != nil {
92 t.Fatalf("ListenPacket failed: %v", err)
94 defer closer(c2, netstr[0], addr1, addr2)
95 c2.LocalAddr()
96 c2.SetDeadline(time.Now().Add(100 * time.Millisecond))
97 c2.SetReadDeadline(time.Now().Add(100 * time.Millisecond))
98 c2.SetWriteDeadline(time.Now().Add(100 * time.Millisecond))
100 if _, err := c1.WriteTo(wb, c2.LocalAddr()); err != nil {
101 t.Fatalf("PacketConn.WriteTo failed: %v", err)
103 rb2 := make([]byte, 128)
104 if _, _, err := c2.ReadFrom(rb2); err != nil {
105 t.Fatalf("PacketConn.ReadFrom failed: %v", err)
107 if _, err := c2.WriteTo(wb, c1.LocalAddr()); err != nil {
108 t.Fatalf("PacketConn.WriteTo failed: %v", err)
110 rb1 := make([]byte, 128)
111 if _, _, err := c1.ReadFrom(rb1); err != nil {
112 t.Fatalf("PacketConn.ReadFrom failed: %v", err)
117 func TestConnAndPacketConn(t *testing.T) {
118 closer := func(c PacketConn, net, addr1, addr2 string) {
119 c.Close()
120 switch net {
121 case "unixgram":
122 os.Remove(addr1)
123 os.Remove(addr2)
127 for i, tt := range packetConnTests {
128 var wb []byte
129 netstr := strings.Split(tt.net, ":")
130 switch netstr[0] {
131 case "udp":
132 wb = []byte("UDP PACKETCONN TEST")
133 case "ip":
134 switch runtime.GOOS {
135 case "plan9":
136 continue
138 if os.Getuid() != 0 {
139 continue
141 var err error
142 wb, err = (&icmpMessage{
143 Type: icmpv4EchoRequest, Code: 0,
144 Body: &icmpEcho{
145 ID: os.Getpid() & 0xffff, Seq: i + 1,
146 Data: []byte("IP PACKETCONN TEST"),
148 }).Marshal()
149 if err != nil {
150 t.Fatalf("icmpMessage.Marshal failed: %v", err)
152 case "unixgram":
153 switch runtime.GOOS {
154 case "plan9", "windows":
155 continue
157 wb = []byte("UNIXGRAM PACKETCONN TEST")
158 default:
159 continue
162 addr1, addr2 := tt.addr1(), tt.addr2()
163 c1, err := ListenPacket(tt.net, addr1)
164 if err != nil {
165 t.Fatalf("ListenPacket failed: %v", err)
167 defer closer(c1, netstr[0], addr1, addr2)
168 c1.LocalAddr()
169 c1.SetDeadline(time.Now().Add(100 * time.Millisecond))
170 c1.SetReadDeadline(time.Now().Add(100 * time.Millisecond))
171 c1.SetWriteDeadline(time.Now().Add(100 * time.Millisecond))
173 c2, err := Dial(tt.net, c1.LocalAddr().String())
174 if err != nil {
175 t.Fatalf("Dial failed: %v", err)
177 defer c2.Close()
178 c2.LocalAddr()
179 c2.RemoteAddr()
180 c2.SetDeadline(time.Now().Add(100 * time.Millisecond))
181 c2.SetReadDeadline(time.Now().Add(100 * time.Millisecond))
182 c2.SetWriteDeadline(time.Now().Add(100 * time.Millisecond))
184 if _, err := c2.Write(wb); err != nil {
185 t.Fatalf("Conn.Write failed: %v", err)
187 rb1 := make([]byte, 128)
188 if _, _, err := c1.ReadFrom(rb1); err != nil {
189 t.Fatalf("PacetConn.ReadFrom failed: %v", err)
191 var dst Addr
192 switch netstr[0] {
193 case "ip":
194 dst = &IPAddr{IP: IPv4(127, 0, 0, 1)}
195 case "unixgram":
196 continue
197 default:
198 dst = c2.LocalAddr()
200 if _, err := c1.WriteTo(wb, dst); err != nil {
201 t.Fatalf("PacketConn.WriteTo failed: %v", err)
203 rb2 := make([]byte, 128)
204 if _, err := c2.Read(rb2); err != nil {
205 t.Fatalf("Conn.Read failed: %v", err)