carlos / platform Public

Clone
git clone https://amadan.net/carlos/platform

Plain git — no account needed to clone.

Download

Download this file

1//go:build linux
2
3package dbreads
4
5import (
6 "os"
7 "path/filepath"
8 "testing"
9
10 "golang.org/x/sys/unix"
11)
12
13func me() (uint32, error) { return uint32(os.Getuid()), nil }
14
15func other() (uint32, error) { return uint32(os.Getuid()) + 1, nil }
16
17func socketpairFile(t *testing.T, typ int) (*os.File, *os.File) {
18 t.Helper()
19 fds, err := unix.Socketpair(unix.AF_UNIX, typ, 0)
20 if err != nil {
21 t.Fatal(err)
22 }
23 a, b := os.NewFile(uintptr(fds[0]), "a"), os.NewFile(uintptr(fds[1]), "b")
24 t.Cleanup(func() { a.Close(); b.Close() })
25 return a, b
26}
27
28// The worker runs only on a connected unix stream socket whose peer is the
29// agent's user (spec §8.3): it cannot be started by hand or pointed at a
30// file.
31func TestStdinConnRefusesWhatIsNotTheAgentsSocket(t *testing.T) {
32 t.Parallel()
33 file, err := os.Create(filepath.Join(t.TempDir(), "f"))
34 if err != nil {
35 t.Fatal(err)
36 }
37 defer file.Close()
38 r, w, err := os.Pipe()
39 if err != nil {
40 t.Fatal(err)
41 }
42 defer r.Close()
43 defer w.Close()
44 dgram, _ := socketpairFile(t, unix.SOCK_DGRAM)
45 stream, _ := socketpairFile(t, unix.SOCK_STREAM)
46 for name, f := range map[string]*os.File{"a file": file, "a pipe": r, "a datagram socket": dgram} {
47 if _, err := StdinConn(f, me); err == nil {
48 t.Errorf("%s accepted", name)
49 }
50 }
51 if _, err := StdinConn(stream, other); err == nil {
52 t.Fatal("a peer that is not the agent's user accepted")
53 }
54 c, err := StdinConn(stream, me)
55 if err != nil {
56 t.Fatal(err)
57 }
58 c.Close()
59}
60