aboutsummaryrefslogtreecommitdiffhomepage
path: root/hst/dbus_test.go
blob: 8b04494b992c2347dfbe3434925e5bd4f36aa2dd (plain)
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
package hst_test

import (
	"reflect"
	"slices"
	"testing"

	"hakurei.app/container"
	"hakurei.app/hst"
)

func TestBadInterfaceError(t *testing.T) {
	testCases := []struct {
		name string
		err  error
		want string
	}{
		{"nil", (*hst.BadInterfaceError)(nil), "<nil>"},
		{"session", &hst.BadInterfaceError{Interface: "\x00", Segment: "session"},
			`bad interface string "\x00" in session bus configuration`},
		{"system", &hst.BadInterfaceError{Interface: "\x01", Segment: "system"},
			`bad interface string "\x01" in system bus configuration`},
	}
	for _, tc := range testCases {
		t.Run(tc.name, func(t *testing.T) {
			if gotError := tc.err.Error(); gotError != tc.want {
				t.Errorf("Error: %s, want %s", gotError, tc.want)
			}
			if gotMessage, ok := container.GetErrorMessage(tc.err); !ok {
				t.Error("GetErrorMessage: ok = false")
			} else if gotMessage != tc.want {
				t.Errorf("GetErrorMessage: %s, want %s", gotMessage, tc.want)
			}
		})
	}
}

func TestBusConfigInterfaces(t *testing.T) {
	testCases := []struct {
		name   string
		c      *hst.BusConfig
		cutoff int
		want   []string
	}{
		{"nil", nil, 0, nil},
		{"all", &hst.BusConfig{
			See: []string{"see"}, Talk: []string{"talk"}, Own: []string{"own"},
			Call:      map[string]string{"call": "unreachable"},
			Broadcast: map[string]string{"broadcast": "unreachable"},
		}, 0, []string{"see", "talk", "own", "call", "broadcast"}},

		{"all cutoff", &hst.BusConfig{
			See: []string{"see"}, Talk: []string{"talk"}, Own: []string{"own"},
			Call:      map[string]string{"call": "unreachable"},
			Broadcast: map[string]string{"broadcast": "unreachable"},
		}, 3, []string{"see", "talk", "own"}},

		{"cutoff see", &hst.BusConfig{See: []string{"see"}}, 1, []string{"see"}},
		{"cutoff talk", &hst.BusConfig{Talk: []string{"talk"}}, 1, []string{"talk"}},
		{"cutoff own", &hst.BusConfig{Own: []string{"own"}}, 1, []string{"own"}},
		{"cutoff call", &hst.BusConfig{Call: map[string]string{"call": "unreachable"}}, 1, []string{"call"}},
		{"cutoff broadcast", &hst.BusConfig{Broadcast: map[string]string{"broadcast": "unreachable"}}, 1, []string{"broadcast"}},
	}
	for _, tc := range testCases {
		t.Run(tc.name, func(t *testing.T) {
			var got []string
			if tc.cutoff > 0 {
				var i int
				got = make([]string, 0, tc.cutoff)
				for v := range tc.c.Interfaces {
					i++
					got = append(got, v)
					if i == tc.cutoff {
						break
					}
				}
			} else {
				got = slices.Collect(tc.c.Interfaces)
			}

			if !slices.Equal(got, tc.want) {
				t.Errorf("Interfaces: %q, want %q", got, tc.want)
			}
		})
	}
}

func TestBusConfigCheckInterfaces(t *testing.T) {
	testCases := []struct {
		name string
		c    *hst.BusConfig
		err  error
	}{
		{"nil", nil, nil},
		{"zero", &hst.BusConfig{See: []string{""}},
			&hst.BadInterfaceError{Interface: "", Segment: "zero"}},
		{"suffix", &hst.BusConfig{See: []string{".*"}},
			&hst.BadInterfaceError{Interface: ".*", Segment: "suffix"}},
		{"valid suffix", &hst.BusConfig{See: []string{"..*"}}, nil},
		{"valid", &hst.BusConfig{See: []string{"."}}, nil},
	}
	for _, tc := range testCases {
		t.Run(tc.name, func(t *testing.T) {
			if err := tc.c.CheckInterfaces(tc.name); !reflect.DeepEqual(err, tc.err) {
				t.Errorf("CheckInterfaces: error = %#v, want %#v", err, tc.err)
			}
		})
	}
}