//go:build linux package dbreads import ( "os" "path/filepath" "testing" "golang.org/x/sys/unix" ) func me() (uint32, error) { return uint32(os.Getuid()), nil } func other() (uint32, error) { return uint32(os.Getuid()) + 1, nil } func socketpairFile(t *testing.T, typ int) (*os.File, *os.File) { t.Helper() fds, err := unix.Socketpair(unix.AF_UNIX, typ, 0) if err != nil { t.Fatal(err) } a, b := os.NewFile(uintptr(fds[0]), "a"), os.NewFile(uintptr(fds[1]), "b") t.Cleanup(func() { a.Close(); b.Close() }) return a, b } // The worker runs only on a connected unix stream socket whose peer is the // agent's user (spec ยง8.3): it cannot be started by hand or pointed at a // file. func TestStdinConnRefusesWhatIsNotTheAgentsSocket(t *testing.T) { t.Parallel() file, err := os.Create(filepath.Join(t.TempDir(), "f")) if err != nil { t.Fatal(err) } defer file.Close() r, w, err := os.Pipe() if err != nil { t.Fatal(err) } defer r.Close() defer w.Close() dgram, _ := socketpairFile(t, unix.SOCK_DGRAM) stream, _ := socketpairFile(t, unix.SOCK_STREAM) for name, f := range map[string]*os.File{"a file": file, "a pipe": r, "a datagram socket": dgram} { if _, err := StdinConn(f, me); err == nil { t.Errorf("%s accepted", name) } } if _, err := StdinConn(stream, other); err == nil { t.Fatal("a peer that is not the agent's user accepted") } c, err := StdinConn(stream, me) if err != nil { t.Fatal(err) } c.Close() }