File: transport_nonce_tcp_test.go

package info (click to toggle)
golang-dbus 5.1.0-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bookworm-backports, bookworm-proposed-updates, forky, sid, trixie
  • size: 592 kB
  • sloc: makefile: 2
file content (66 lines) | stat: -rw-r--r-- 1,530 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package dbus

import (
	"bufio"
	"io/ioutil"
	"os"
	"os/exec"
	"testing"
)

func TestTcpNonceConnection(t *testing.T) {
	addr, process := startDaemon(t, `<!DOCTYPE busconfig PUBLIC "-//freedesktop//DTD D-BUS Bus Configuration 1.0//EN"
 "http://www.freedesktop.org/standards/dbus/1.0/busconfig.dtd">
<busconfig>
	<type>session</type>
		<listen>nonce-tcp:</listen>
		<auth>ANONYMOUS</auth>
		<allow_anonymous/>
                <apparmor mode="disabled"/>
		<policy context="default">
			<allow send_destination="*" eavesdrop="true"/>
			<allow eavesdrop="true"/>
			<allow own="*"/>
		</policy>
</busconfig>
`)
	defer process.Kill()

	conn, err := Connect(addr, WithAuth(AuthAnonymous()))
	if err != nil {
		t.Fatal(err)
	}
	if err = conn.Close(); err != nil {
		t.Fatal(err)
	}
}

// startDaemon starts a dbus-daemon instance with the given config
// and returns its address string and underlying process.
func startDaemon(t *testing.T, config string) (string, *os.Process) {
	cfg, err := ioutil.TempFile("", "")
	if err != nil {
		t.Fatal(err)
	}
	defer os.Remove(cfg.Name())
	if _, err = cfg.Write([]byte(config)); err != nil {
		t.Fatal(err)
	}

	cmd := exec.Command("dbus-daemon", "--nofork", "--print-address", "--config-file", cfg.Name())
	cmd.Stderr = os.Stderr
	out, err := cmd.StdoutPipe()
	if err != nil {
		t.Fatal(err)
	}
	if err = cmd.Start(); err != nil {
		t.Fatal(err)
	}
	r := bufio.NewReader(out)
	l, _, err := r.ReadLine()
	if err != nil {
		cmd.Process.Kill()
		t.Fatal(err)
	}
	return string(l), cmd.Process
}