-
Notifications
You must be signed in to change notification settings - Fork 0
/
socket_test.go
67 lines (59 loc) · 1.58 KB
/
socket_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
package socket
import (
"os"
"testing"
)
func TestParse(t *testing.T) {
tests := []struct {
Input string
Network string
Address string
}{
// Examples from http://nginx.org/en/docs/http/ngx_http_core_module.html#listen
// listen 127.0.0.1:8000;
// listen 127.0.0.1;
// listen 8000;
// listen *:8000;
// listen localhost:8000;
// listen [::]:8000;
// listen [::1];
// listen unix:/var/run/nginx.sock;
{"127.0.0.1:8000", "tcp", "127.0.0.1:8000"},
{"127.0.0.1", "tcp", "127.0.0.1:80"}, // "If only address is given, the port 80 is used."
{"8000", "tcp", ":8000"},
{"*:8000", "tcp", ":8000"},
{"localhost:8000", "tcp", "localhost:8000"},
{"localhost", "tcp", "localhost:80"},
{"[::]:8000", "tcp", "[::]:8000"},
{"[::1]", "tcp", "[::1]:80"},
{"unix:/var/run/nginx.sock", "unix", "/var/run/nginx.sock"},
}
for _, test := range tests {
network, address := Parse(test.Input)
if network != test.Network || address != test.Address {
t.Errorf("For input %q: Expected %q, %q but got %q, %q",
test.Input, test.Network, test.Address, network, address)
}
}
}
func TestListen(t *testing.T) {
file := "sock.test"
mode := os.FileMode(0630)
listener, err := Listen("unix", file, mode)
if err != nil {
t.Errorf("Could not create socket: %s", err)
return
}
defer listener.Close()
// Test that socket has correct file mode
info, err := os.Stat(file)
if err != nil {
t.Errorf("Could not stat socket: %s", err)
return
}
actual := info.Mode().Perm()
if actual != mode {
t.Errorf("Expected socket mode of %d, got %d", mode, actual)
return
}
}