-
Notifications
You must be signed in to change notification settings - Fork 7
/
peercred_unix_test.go
74 lines (66 loc) · 1.38 KB
/
peercred_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
// Copyright (c) 2021 AUTHORS All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
//go:build go1.15 && (linux || darwin || freebsd || illumos || solaris)
package peercred
import (
"fmt"
"net"
"os"
"path/filepath"
"runtime"
"testing"
)
func TestUnixSock(t *testing.T) {
d := t.TempDir()
path := filepath.Join(d, "foo.sock")
sock, err := net.Listen("unix", path)
if err != nil {
t.Fatal(err)
}
defer sock.Close()
clientConnCh := make(chan net.Conn, 1)
go func() {
defer close(clientConnCh)
c, err := net.Dial("unix", path)
if err != nil {
t.Error(err)
return
}
clientConnCh <- c
}()
clientConn, ok := <-clientConnCh
if !ok {
return
}
defer clientConn.Close()
c, err := sock.Accept()
if err != nil {
t.Fatalf("Accept: %v", err)
}
defer c.Close()
creds, err := Get(c)
if err != nil {
t.Fatalf("Get: %v", err)
}
uid, ok := creds.UserID()
if !ok {
t.Errorf("no UID")
}
if got, want := uid, fmt.Sprint(os.Getuid()); got != want {
t.Errorf("UID = %q; want %q", got, want)
}
pid, ok := creds.PID()
if runtime.GOOS == "freebsd" {
if ok {
t.Error("PID ok; want !ok. Thank you for fixing FreeBSD, please update the test.")
}
} else {
if !ok {
t.Errorf("no PID")
}
if got, want := pid, os.Getpid(); got != want {
t.Errorf("PID = %v; want %v", got, want)
}
}
}