mirror of
https://github.com/rwinkhart/peercred-mini.git
synced 2026-08-27 20:36:30 -04:00
Start of peercred package, with Linux only for now.
This commit is contained in:
@@ -0,0 +1,62 @@
|
||||
// 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.
|
||||
|
||||
// +build go1.15
|
||||
// +build linux
|
||||
|
||||
package peercred // import "inet.af/peercred"
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestUnix(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()
|
||||
|
||||
go func() {
|
||||
c, err := net.Dial("unix", path)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
return
|
||||
}
|
||||
c.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 !ok {
|
||||
t.Errorf("no PID")
|
||||
}
|
||||
if got, want := pid, os.Getpid(); got != want {
|
||||
t.Errorf("PID = %v; want %v", got, want)
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user