Start of peercred package, with Linux only for now.

This commit is contained in:
Brad Fitzpatrick
2021-02-16 10:02:57 -08:00
parent 9aa5636256
commit 215a1c795a
6 changed files with 168 additions and 0 deletions
+62
View File
@@ -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)
}
}