2012-02-01 20:26:59 +01:00
|
|
|
// Copyright 2012 The Go Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package net
|
|
|
|
|
|
|
|
import (
|
|
|
|
"runtime"
|
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestWriteToUDP(t *testing.T) {
|
2012-03-30 23:27:11 +02:00
|
|
|
switch runtime.GOOS {
|
|
|
|
case "plan9":
|
|
|
|
t.Logf("skipping test on %q", runtime.GOOS)
|
2012-02-01 20:26:59 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
l, err := ListenPacket("udp", "127.0.0.1:0")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Listen failed: %v", err)
|
|
|
|
}
|
|
|
|
defer l.Close()
|
|
|
|
|
|
|
|
testWriteToConn(t, l.LocalAddr().String())
|
|
|
|
testWriteToPacketConn(t, l.LocalAddr().String())
|
|
|
|
}
|
|
|
|
|
|
|
|
func testWriteToConn(t *testing.T, raddr string) {
|
|
|
|
c, err := Dial("udp", raddr)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Dial failed: %v", err)
|
|
|
|
}
|
|
|
|
defer c.Close()
|
|
|
|
|
|
|
|
ra, err := ResolveUDPAddr("udp", raddr)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("ResolveUDPAddr failed: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = c.(*UDPConn).WriteToUDP([]byte("Connection-oriented mode socket"), ra)
|
|
|
|
if err == nil {
|
2012-03-06 18:57:23 +01:00
|
|
|
t.Fatal("WriteToUDP should fail")
|
2012-02-01 20:26:59 +01:00
|
|
|
}
|
|
|
|
if err != nil && err.(*OpError).Err != ErrWriteToConnected {
|
2012-03-06 18:57:23 +01:00
|
|
|
t.Fatalf("WriteToUDP should fail as ErrWriteToConnected: %v", err)
|
2012-02-01 20:26:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
_, err = c.(*UDPConn).WriteTo([]byte("Connection-oriented mode socket"), ra)
|
|
|
|
if err == nil {
|
2012-03-06 18:57:23 +01:00
|
|
|
t.Fatal("WriteTo should fail")
|
2012-02-01 20:26:59 +01:00
|
|
|
}
|
|
|
|
if err != nil && err.(*OpError).Err != ErrWriteToConnected {
|
2012-03-06 18:57:23 +01:00
|
|
|
t.Fatalf("WriteTo should fail as ErrWriteToConnected: %v", err)
|
2012-02-01 20:26:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
_, err = c.Write([]byte("Connection-oriented mode socket"))
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Write failed: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func testWriteToPacketConn(t *testing.T, raddr string) {
|
|
|
|
c, err := ListenPacket("udp", "127.0.0.1:0")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("ListenPacket failed: %v", err)
|
|
|
|
}
|
|
|
|
defer c.Close()
|
|
|
|
|
|
|
|
ra, err := ResolveUDPAddr("udp", raddr)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("ResolveUDPAddr failed: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = c.(*UDPConn).WriteToUDP([]byte("Connection-less mode socket"), ra)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("WriteToUDP failed: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = c.WriteTo([]byte("Connection-less mode socket"), ra)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("WriteTo failed: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = c.(*UDPConn).Write([]byte("Connection-less mode socket"))
|
|
|
|
if err == nil {
|
2012-03-06 18:57:23 +01:00
|
|
|
t.Fatal("Write should fail")
|
2012-02-01 20:26:59 +01:00
|
|
|
}
|
|
|
|
}
|