summaryrefslogtreecommitdiff
path: root/libgo/go/net/unicast_test.go
diff options
context:
space:
mode:
Diffstat (limited to 'libgo/go/net/unicast_test.go')
-rw-r--r--libgo/go/net/unicast_test.go99
1 files changed, 99 insertions, 0 deletions
diff --git a/libgo/go/net/unicast_test.go b/libgo/go/net/unicast_test.go
new file mode 100644
index 00000000000..6ed6f59cdd6
--- /dev/null
+++ b/libgo/go/net/unicast_test.go
@@ -0,0 +1,99 @@
+// Copyright 2011 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"
+)
+
+var unicastTests = []struct {
+ net string
+ laddr string
+ ipv6 bool
+ packet bool
+}{
+ {"tcp4", "127.0.0.1:0", false, false},
+ {"tcp6", "[::1]:0", true, false},
+ {"udp4", "127.0.0.1:0", false, true},
+ {"udp6", "[::1]:0", true, true},
+}
+
+func TestUnicastTCPAndUDP(t *testing.T) {
+ if runtime.GOOS == "plan9" || runtime.GOOS == "windows" {
+ return
+ }
+
+ for _, tt := range unicastTests {
+ if tt.ipv6 && !supportsIPv6 {
+ continue
+ }
+ var fd *netFD
+ if !tt.packet {
+ c, err := Listen(tt.net, tt.laddr)
+ if err != nil {
+ t.Fatalf("Listen failed: %v", err)
+ }
+ defer c.Close()
+ fd = c.(*TCPListener).fd
+ } else {
+ c, err := ListenPacket(tt.net, tt.laddr)
+ if err != nil {
+ t.Fatalf("ListenPacket failed: %v", err)
+ }
+ defer c.Close()
+ fd = c.(*UDPConn).fd
+ }
+ if !tt.ipv6 {
+ testIPv4UnicastSocketOptions(t, fd)
+ } else {
+ testIPv6UnicastSocketOptions(t, fd)
+ }
+ }
+}
+
+func testIPv4UnicastSocketOptions(t *testing.T, fd *netFD) {
+ tos, err := ipv4TOS(fd)
+ if err != nil {
+ t.Fatalf("ipv4TOS failed: %v", err)
+ }
+ t.Logf("IPv4 TOS: %v", tos)
+ err = setIPv4TOS(fd, 1)
+ if err != nil {
+ t.Fatalf("setIPv4TOS failed: %v", err)
+ }
+
+ ttl, err := ipv4TTL(fd)
+ if err != nil {
+ t.Fatalf("ipv4TTL failed: %v", err)
+ }
+ t.Logf("IPv4 TTL: %v", ttl)
+ err = setIPv4TTL(fd, 1)
+ if err != nil {
+ t.Fatalf("setIPv4TTL failed: %v", err)
+ }
+}
+
+func testIPv6UnicastSocketOptions(t *testing.T, fd *netFD) {
+ tos, err := ipv6TrafficClass(fd)
+ if err != nil {
+ t.Fatalf("ipv6TrafficClass failed: %v", err)
+ }
+ t.Logf("IPv6 TrafficClass: %v", tos)
+ err = setIPv6TrafficClass(fd, 1)
+ if err != nil {
+ t.Fatalf("setIPv6TrafficClass failed: %v", err)
+ }
+
+ hoplim, err := ipv6HopLimit(fd)
+ if err != nil {
+ t.Fatalf("ipv6HopLimit failed: %v", err)
+ }
+ t.Logf("IPv6 HopLimit: %v", hoplim)
+ err = setIPv6HopLimit(fd, 1)
+ if err != nil {
+ t.Fatalf("setIPv6HopLimit failed: %v", err)
+ }
+}