forked from savsgio/atreugo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlistener_unix_test.go
160 lines (144 loc) · 2.84 KB
/
listener_unix_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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
//go:build !windows
// +build !windows
package atreugo
import (
"errors"
"strings"
"testing"
"time"
)
func TestAtreugo_getListener(t *testing.T) { // nolint:funlen,gocognit
type want struct {
addr string
network string
err bool
}
const unixNetwork = "unix"
tests := []struct {
name string
args Config
want want
}{
{
name: "Ok",
args: Config{
Addr: "127.0.0.1:8000",
},
want: want{
addr: "127.0.0.1:8000",
network: "tcp",
err: false,
},
},
{
name: "TCPKeepAlive",
args: Config{
Addr: "127.0.0.1:8000",
TCPKeepalive: true,
TCPKeepalivePeriod: 10 * time.Second,
},
want: want{
addr: "127.0.0.1:8000",
network: "tcp",
err: false,
},
},
{
name: "Reuseport",
args: Config{
Addr: "127.0.0.1:8000",
Network: "tcp4",
Reuseport: true,
},
want: want{
addr: "127.0.0.1:8000",
network: "tcp",
err: false,
},
},
{
name: "Unix",
args: Config{
Addr: "/tmp/test.sock",
Network: unixNetwork,
},
want: want{
addr: "/tmp/test.sock",
network: unixNetwork,
err: false,
},
},
{
name: "UnixRemoveError",
args: Config{
Addr: "/bin/sh",
Network: unixNetwork,
},
want: want{
addr: "/bin/sh",
network: "unix",
err: true,
},
},
{
name: "UnixChmodError",
args: Config{
Addr: "/tmp/test.sock",
Network: unixNetwork,
},
want: want{
err: true,
},
},
{
name: "Error",
args: Config{
Network: "fake",
},
want: want{
err: true,
},
},
}
for _, test := range tests {
tt := test
t.Run(tt.name, func(t *testing.T) {
t.Helper()
defer func() {
err := recover()
if tt.want.err && err == nil {
t.Errorf("Panic expected")
} else if !tt.want.err && err != nil {
t.Errorf("Unexpected panic: %v", err)
}
}()
s := New(tt.args)
if tt.name == "UnixChmodError" {
s.cfg.chmodUnixSocket = func(addr string) error {
return errors.New("chmod error")
}
}
ln, err := s.getListener()
if err != nil {
panic(err)
}
lnAddress := ln.Addr().String()
if lnAddress != tt.want.addr {
t.Errorf("Listener address: '%s', want '%s'", lnAddress, tt.want.addr)
}
lnNetwork := ln.Addr().Network()
if lnNetwork != tt.want.network {
t.Errorf("Listener network: '%s', want '%s'", lnNetwork, tt.want.network)
}
tcpLn, ok := ln.(*tcpKeepaliveListener)
if !ok {
if strings.HasPrefix(lnNetwork, "tcp") && !s.cfg.Reuseport {
t.Error("Listener is not wrapped as tcpKeepaliveListener")
}
} else if tcpLn.keepalivePeriod != tt.args.TCPKeepalivePeriod {
t.Errorf("tcpKeepaliveListener.keepalivePeriod == %d, want %d", tcpLn.keepalivePeriod, tt.args.TCPKeepalivePeriod)
}
ln.Close()
})
}
}