mirror of
https://github.com/rwinkhart/sys.git
synced 2026-09-06 00:57:18 -04:00
unix: add GetPeerUcred and UcredGet for solaris
Change-Id: I74ba119fb729ef46899de04c686115f960975bb3 Reviewed-on: https://go-review.googlesource.com/c/sys/+/639755 LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com> Auto-Submit: Ian Lance Taylor <iant@google.com> Reviewed-by: Michael Pratt <mpratt@google.com> Reviewed-by: Ian Lance Taylor <iant@google.com> Reviewed-by: Matt Layher <mdlayher@gmail.com>
This commit is contained in:
committed by
Gopher Robot
parent
d4ac05dc8c
commit
1c14dcadc3
@@ -8,6 +8,7 @@ package unix_test
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net"
|
||||
"os"
|
||||
"os/exec"
|
||||
"path/filepath"
|
||||
@@ -420,3 +421,66 @@ func TestLifreqGetMTU(t *testing.T) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestUcredGet(t *testing.T) {
|
||||
euid := unix.Geteuid()
|
||||
creds, err := unix.UcredGet(unix.Getpid())
|
||||
if err != nil {
|
||||
t.Fatalf("unix.UcredGet failed: %v", err)
|
||||
}
|
||||
if euid != creds.Geteuid() {
|
||||
t.Fatalf("mismatched euid")
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetPeerUcred(t *testing.T) {
|
||||
d := t.TempDir()
|
||||
path := filepath.Join(d, "foo.sock")
|
||||
sock, err := net.Listen("unix", path)
|
||||
if err != nil {
|
||||
t.Fatalf("net.Listen: %v", err)
|
||||
}
|
||||
defer sock.Close()
|
||||
|
||||
c1, err := net.Dial("unix", path)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
return
|
||||
}
|
||||
defer c1.Close()
|
||||
|
||||
c2, err := sock.Accept()
|
||||
if err != nil {
|
||||
t.Fatalf("Accept: %v", err)
|
||||
}
|
||||
defer c2.Close()
|
||||
|
||||
switch unixconn := c2.(type) {
|
||||
case *net.UnixConn:
|
||||
raw, err := unixconn.SyscallConn()
|
||||
if err != nil {
|
||||
t.Fatalf("SyscallConn failed: %v", err)
|
||||
}
|
||||
|
||||
var creds *unix.Ucred
|
||||
cerr := raw.Control(func(fd uintptr) {
|
||||
creds, err = unix.GetPeerUcred(fd)
|
||||
if err != nil {
|
||||
err = fmt.Errorf("unix.GetPeerUcred: %w", err)
|
||||
return
|
||||
}
|
||||
})
|
||||
if cerr != nil {
|
||||
t.Fatalf("raw.Control failed: %v", err)
|
||||
}
|
||||
if creds == nil {
|
||||
t.Fatalf("Got a nil Ucred response")
|
||||
}
|
||||
euid := unix.Geteuid()
|
||||
if euid != creds.Geteuid() {
|
||||
t.Fatalf("mismatched euid")
|
||||
}
|
||||
default:
|
||||
t.Fatalf("Somehow didn't get a UnixConn")
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user