1package zerobus
2
3import (
4 "bufio"
5 "errors"
6 "net"
7 "path/filepath"
8 "strings"
9 "testing"
10)
11
12// fakeBus is a minimal bus on a Unix socket that answers the handshake and
13// Hello, then writes the given frames to the client one byte at a time, so
14// every message is split across reads.
15func fakeBus(t *testing.T, authReply string, frames ...[]byte) string {
16 t.Helper()
17 sock := filepath.Join(t.TempDir(), "bus")
18 l, err := net.Listen("unix", sock)
19 if err != nil {
20 t.Fatal(err)
21 }
22 t.Cleanup(func() { _ = l.Close() })
23 go func() {
24 c, err := l.Accept()
25 if err != nil {
26 return
27 }
28 defer func() { _ = c.Close() }()
29 r := bufio.NewReader(c)
30 if _, err := r.ReadString('\n'); err != nil { // \0AUTH EXTERNAL ...
31 return
32 }
33 var hello Encoder
34 hello.begin(TypeMethodReturn, 1, 1, "", "", "", "", "", "s")
35 hello.Str(":1.7")
36 b, _ := hello.finish()
37 // The OK line and the Hello reply arrive in the same write: the
38 // client must keep the bytes after the line.
39 if _, err := c.Write(append([]byte(authReply), b...)); err != nil {
40 return
41 }
42 if _, err := r.ReadString('\n'); err != nil { // BEGIN
43 return
44 }
45 for _, f := range frames {
46 for i := range f {
47 if _, err := c.Write(f[i : i+1]); err != nil {
48 return
49 }
50 }
51 }
52 _, _ = r.ReadByte() // wait for the client to close
53 }()
54 return "unix:path=" + sock
55}
56
57func signal(member string) []byte {
58 var e Encoder
59 e.begin(TypeSignal, 2, 0, "", "/a", "a.b", member, "", "s")
60 e.Str(member)
61 b, _ := e.finish()
62 return append([]byte(nil), b...)
63}
64
65func TestFakeBusSplitFrames(t *testing.T) {
66 addr := fakeBus(t, "OK 0123456789abcdef\r\n", signal("One"), signal("Two"))
67 c := dial(t, addr)
68 if c.UniqueName() != ":1.7" {
69 t.Fatalf("unique name %q", c.UniqueName())
70 }
71 for _, want := range []string{"One", "Two"} {
72 m, err := c.ReadMessage()
73 if err != nil {
74 t.Fatal(err)
75 }
76 if m.Member != want || m.Body().Str() != want {
77 t.Fatalf("got %q, want %q", m.Member, want)
78 }
79 }
80}
81
82func TestFakeBusRejected(t *testing.T) {
83 addr := fakeBus(t, "REJECTED EXTERNAL\r\n")
84 if c, err := Dial(addr); err == nil {
85 _ = c.Close()
86 t.Fatal("rejected auth accepted")
87 }
88}
89
90func TestFakeBusMalformedIsSticky(t *testing.T) {
91 bad := signal("Bad")
92 bad[3] = 9 // protocol version
93 c := dial(t, fakeBus(t, "OK 0123456789abcdef\r\n", bad))
94 _, err := c.ReadMessage()
95 if !errors.Is(err, ErrMalformed) {
96 t.Fatalf("err = %v, want ErrMalformed", err)
97 }
98 if _, err := c.ReadMessage(); !errors.Is(err, ErrMalformed) {
99 t.Fatalf("second read: %v, want the same error", err)
100 }
101}
102
103func TestSessionBusFromRuntimeDir(t *testing.T) {
104 addr := fakeBus(t, "OK 0123456789abcdef\r\n")
105 t.Setenv("DBUS_SESSION_BUS_ADDRESS", "")
106 t.Setenv("XDG_RUNTIME_DIR", filepath.Dir(strings.TrimPrefix(addr, "unix:path=")))
107 c, err := SessionBus()
108 if err != nil {
109 t.Fatal(err)
110 }
111 _ = c.Close()
112}