forked from hashicorp/go-sockaddr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
unixsock_test.go
108 lines (96 loc) · 2.58 KB
/
unixsock_test.go
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
package sockaddr_test
import (
"testing"
sockaddr "github.com/hashicorp/go-sockaddr"
)
func TestUnixSock_impl_SockAddr(t *testing.T) {
tests := []struct {
name string
input sockaddr.UnixSock
dialPacketArgs []string
dialStreamArgs []string
listenPacketArgs []string
listenStreamArgs []string
}{
{
name: "simple",
input: sockaddr.MustUnixSock("/tmp/foo"),
dialPacketArgs: []string{"unixgram", "/tmp/foo"},
dialStreamArgs: []string{"unixgram", "/tmp/foo"},
listenPacketArgs: []string{"unixgram", "/tmp/foo"},
listenStreamArgs: []string{"unixgram", "/tmp/foo"},
},
}
for i, test := range tests {
if test.name == "" {
t.Fatalf("test %d needs a name", i)
}
arg1, arg2 := test.input.DialPacketArgs()
if arg1 != test.dialPacketArgs[0] && arg2 != test.dialPacketArgs[1] {
t.Fatalf("%s: %q %q", test.name, arg1, arg2)
}
arg1, arg2 = test.input.DialStreamArgs()
if arg1 != test.dialStreamArgs[0] && arg2 != test.dialStreamArgs[1] {
t.Fatalf("%s: %q %q", test.name, arg1, arg2)
}
arg1, arg2 = test.input.ListenPacketArgs()
if arg1 != test.listenPacketArgs[0] && arg2 != test.listenPacketArgs[1] {
t.Fatalf("%s: %q %q", test.name, arg1, arg2)
}
arg1, arg2 = test.input.ListenStreamArgs()
if arg1 != test.listenStreamArgs[0] && arg2 != test.listenStreamArgs[1] {
t.Fatalf("%s: %q %q", test.name, arg1, arg2)
}
}
}
func TestUnixSock_Equal(t *testing.T) {
tests := []struct {
name string
input sockaddr.UnixSock
sa sockaddr.SockAddr
equal bool
}{
{
name: "equal",
input: sockaddr.MustUnixSock("/tmp/foo"),
sa: sockaddr.MustUnixSock("/tmp/foo"),
equal: true,
},
{
name: "not equal",
input: sockaddr.MustUnixSock("/tmp/foo"),
sa: sockaddr.MustUnixSock("/tmp/bar"),
equal: false,
},
{
name: "ipv4",
input: sockaddr.MustUnixSock("/tmp/foo"),
sa: sockaddr.MustIPv4Addr("1.2.3.4"),
equal: false,
},
{
name: "ipv6",
input: sockaddr.MustUnixSock("/tmp/foo"),
sa: sockaddr.MustIPv6Addr("::1"),
equal: false,
},
}
for i, test := range tests {
if test.name == "" {
t.Fatalf("test %d needs a name", i)
}
t.Run(test.name, func(t *testing.T) {
us := test.input
if ret := us.Equal(test.sa); ret != test.equal {
t.Fatalf("%s: equal: %v %q %q", test.name, ret, us, test.sa)
}
})
}
}
func TestUnixSockAttrs(t *testing.T) {
const expectedNumAttrs = 1
usa := sockaddr.UnixSockAttrs()
if len(usa) != expectedNumAttrs {
t.Fatalf("wrong number of UnixSockAttrs: %d vs %d", len(usa), expectedNumAttrs)
}
}