Skip to content

Commit 42faf79

Browse files
authored
Merge pull request #138 from thaJeztah/sockets_move_unix_options
sockets: make NewUnixSocket, WithChown, WithChmod unix-only
2 parents 6bb1d15 + 9ffab7e commit 42faf79

File tree

5 files changed

+71
-66
lines changed

5 files changed

+71
-66
lines changed

sockets/unix_socket.go

Lines changed: 0 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -55,26 +55,6 @@ import (
5555
// SockOption sets up socket file's creating option
5656
type SockOption func(string) error
5757

58-
// WithChown modifies the socket file's uid and gid
59-
func WithChown(uid, gid int) SockOption {
60-
return func(path string) error {
61-
if err := os.Chown(path, uid, gid); err != nil {
62-
return err
63-
}
64-
return nil
65-
}
66-
}
67-
68-
// WithChmod modifies socket file's access mode.
69-
func WithChmod(mask os.FileMode) SockOption {
70-
return func(path string) error {
71-
if err := os.Chmod(path, mask); err != nil {
72-
return err
73-
}
74-
return nil
75-
}
76-
}
77-
7858
// NewUnixSocketWithOpts creates a unix socket with the specified options.
7959
// By default, socket permissions are 0000 (i.e.: no access for anyone); pass
8060
// WithChmod() and WithChown() to set the desired ownership and permissions.
@@ -102,8 +82,3 @@ func NewUnixSocketWithOpts(path string, opts ...SockOption) (net.Listener, error
10282

10383
return l, nil
10484
}
105-
106-
// NewUnixSocket creates a unix socket with the specified path and group.
107-
func NewUnixSocket(path string, gid int) (net.Listener, error) {
108-
return NewUnixSocketWithOpts(path, WithChown(0, gid), WithChmod(0o660))
109-
}

sockets/unix_socket_test.go

Lines changed: 0 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package sockets
33
import (
44
"fmt"
55
"net"
6-
"os"
76
"testing"
87
)
98

@@ -31,34 +30,3 @@ func runTest(t *testing.T, path string, l net.Listener, echoStr string) {
3130
t.Fatal(fmt.Errorf("msg may lost"))
3231
}
3332
}
34-
35-
// TestNewUnixSocket run under root user.
36-
func TestNewUnixSocket(t *testing.T) {
37-
if os.Getuid() != 0 {
38-
t.Skip("requires root")
39-
}
40-
gid := os.Getgid()
41-
path := "/tmp/test.sock"
42-
echoStr := "hello"
43-
l, err := NewUnixSocket(path, gid)
44-
if err != nil {
45-
t.Fatal(err)
46-
}
47-
defer func() { _ = l.Close() }()
48-
runTest(t, path, l, echoStr)
49-
}
50-
51-
func TestUnixSocketWithOpts(t *testing.T) {
52-
socketFile, err := os.CreateTemp("", "test*.sock")
53-
if err != nil {
54-
t.Fatal(err)
55-
}
56-
_ = socketFile.Close()
57-
defer func() { _ = os.Remove(socketFile.Name()) }()
58-
59-
l := createTestUnixSocket(t, socketFile.Name())
60-
defer func() { _ = l.Close() }()
61-
62-
echoStr := "hello"
63-
runTest(t, socketFile.Name(), l, echoStr)
64-
}

sockets/unix_socket_unix.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,35 @@ package sockets
44

55
import (
66
"net"
7+
"os"
78
"syscall"
89
)
910

11+
// WithChown modifies the socket file's uid and gid
12+
func WithChown(uid, gid int) SockOption {
13+
return func(path string) error {
14+
if err := os.Chown(path, uid, gid); err != nil {
15+
return err
16+
}
17+
return nil
18+
}
19+
}
20+
21+
// WithChmod modifies socket file's access mode.
22+
func WithChmod(mask os.FileMode) SockOption {
23+
return func(path string) error {
24+
if err := os.Chmod(path, mask); err != nil {
25+
return err
26+
}
27+
return nil
28+
}
29+
}
30+
31+
// NewUnixSocket creates a unix socket with the specified path and group.
32+
func NewUnixSocket(path string, gid int) (net.Listener, error) {
33+
return NewUnixSocketWithOpts(path, WithChown(0, gid), WithChmod(0o660))
34+
}
35+
1036
func listenUnix(path string) (net.Listener, error) {
1137
// net.Listen does not allow for permissions to be set. As a result, when
1238
// specifying custom permissions ("WithChmod()"), there is a short time

sockets/unix_socket_unix_test.go

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,26 @@
33
package sockets
44

55
import (
6-
"net"
76
"os"
87
"syscall"
98
"testing"
109
)
1110

12-
func createTestUnixSocket(t *testing.T, path string) (listener net.Listener) {
11+
func TestUnixSocketWithOpts(t *testing.T) {
12+
socketFile, err := os.CreateTemp("", "test*.sock")
13+
if err != nil {
14+
t.Fatal(err)
15+
}
16+
_ = socketFile.Close()
17+
defer func() { _ = os.Remove(socketFile.Name()) }()
18+
1319
uid, gid := os.Getuid(), os.Getgid()
1420
perms := os.FileMode(0660)
15-
l, err := NewUnixSocketWithOpts(path, WithChown(uid, gid), WithChmod(perms))
21+
l, err := NewUnixSocketWithOpts(socketFile.Name(), WithChown(uid, gid), WithChmod(perms))
1622
if err != nil {
1723
t.Fatal(err)
1824
}
19-
p, err := os.Stat(path)
25+
p, err := os.Stat(socketFile.Name())
2026
if err != nil {
2127
t.Fatal(err)
2228
}
@@ -28,5 +34,25 @@ func createTestUnixSocket(t *testing.T, path string) (listener net.Listener) {
2834
t.Fatalf("unexpected file ownership: expected: %d:%d, got: %d:%d", uid, gid, stat.Uid, stat.Gid)
2935
}
3036
}
31-
return l
37+
38+
defer func() { _ = l.Close() }()
39+
40+
echoStr := "hello"
41+
runTest(t, socketFile.Name(), l, echoStr)
42+
}
43+
44+
// TestNewUnixSocket run under root user.
45+
func TestNewUnixSocket(t *testing.T) {
46+
if os.Getuid() != 0 {
47+
t.Skip("requires root")
48+
}
49+
gid := os.Getgid()
50+
path := "/tmp/test.sock"
51+
echoStr := "hello"
52+
l, err := NewUnixSocket(path, gid)
53+
if err != nil {
54+
t.Fatal(err)
55+
}
56+
defer func() { _ = l.Close() }()
57+
runTest(t, path, l, echoStr)
3258
}
Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,24 @@
11
package sockets
22

33
import (
4-
"net"
4+
"os"
55
"testing"
66
)
77

8-
func createTestUnixSocket(t *testing.T, path string) (listener net.Listener) {
9-
l, err := NewUnixSocketWithOpts(path)
8+
func TestUnixSocketWithOpts(t *testing.T) {
9+
socketFile, err := os.CreateTemp("", "test*.sock")
1010
if err != nil {
1111
t.Fatal(err)
1212
}
13-
return l
13+
_ = socketFile.Close()
14+
defer func() { _ = os.Remove(socketFile.Name()) }()
15+
16+
l, err := NewUnixSocketWithOpts(socketFile.Name())
17+
if err != nil {
18+
t.Fatal(err)
19+
}
20+
defer func() { _ = l.Close() }()
21+
22+
echoStr := "hello"
23+
runTest(t, socketFile.Name(), l, echoStr)
1424
}

0 commit comments

Comments
 (0)