summaryrefslogtreecommitdiffstats
path: root/vendor/golang.org/x/net/internal/netreflect/socket_test.go
blob: 305665ac19039c7939345409a76552732fab6182 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
// Copyright 2016 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 netreflect_test

import (
	"fmt"
	"io/ioutil"
	"net"
	"os"
	"runtime"
	"testing"

	"golang.org/x/net/internal/netreflect"
)

func localPath() string {
	f, err := ioutil.TempFile("", "netreflect")
	if err != nil {
		panic(err)
	}
	path := f.Name()
	f.Close()
	os.Remove(path)
	return path
}

func newLocalListener(network string) (net.Listener, error) {
	switch network {
	case "tcp":
		if ln, err := net.Listen("tcp4", "127.0.0.1:0"); err == nil {
			return ln, nil
		}
		return net.Listen("tcp6", "[::1]:0")
	case "tcp4":
		return net.Listen("tcp4", "127.0.0.1:0")
	case "tcp6":
		return net.Listen("tcp6", "[::1]:0")
	case "unix", "unixpacket":
		return net.Listen(network, localPath())
	}
	return nil, fmt.Errorf("%s is not supported", network)
}

func newLocalPacketListener(network string) (net.PacketConn, error) {
	switch network {
	case "udp":
		if c, err := net.ListenPacket("udp4", "127.0.0.1:0"); err == nil {
			return c, nil
		}
		return net.ListenPacket("udp6", "[::1]:0")
	case "udp4":
		return net.ListenPacket("udp4", "127.0.0.1:0")
	case "udp6":
		return net.ListenPacket("udp6", "[::1]:0")
	case "unixgram":
		return net.ListenPacket(network, localPath())
	}
	return nil, fmt.Errorf("%s is not supported", network)
}

func TestSocketOf(t *testing.T) {
	for _, network := range []string{"tcp", "unix", "unixpacket"} {
		switch runtime.GOOS {
		case "darwin":
			if network == "unixpacket" {
				continue
			}
		case "nacl", "plan9":
			continue
		case "windows":
			if network == "unix" || network == "unixpacket" {
				continue
			}
		}
		ln, err := newLocalListener(network)
		if err != nil {
			t.Error(err)
			continue
		}
		defer func() {
			path := ln.Addr().String()
			ln.Close()
			if network == "unix" || network == "unixpacket" {
				os.Remove(path)
			}
		}()
		c, err := net.Dial(ln.Addr().Network(), ln.Addr().String())
		if err != nil {
			t.Error(err)
			continue
		}
		defer c.Close()
		if _, err := netreflect.SocketOf(c); err != nil {
			t.Error(err)
			continue
		}
	}
}

func TestPacketSocketOf(t *testing.T) {
	for _, network := range []string{"udp", "unixgram"} {
		switch runtime.GOOS {
		case "nacl", "plan9":
			continue
		case "windows":
			if network == "unixgram" {
				continue
			}
		}
		c, err := newLocalPacketListener(network)
		if err != nil {
			t.Error(err)
			continue
		}
		defer c.Close()
		if _, err := netreflect.PacketSocketOf(c); err != nil {
			t.Error(err)
			continue
		}
	}
}