aboutsummaryrefslogtreecommitdiffhomepage
path: root/hst/config_test.go
blob: 7c6ade8c8b0dac0d015bc4e93bc8dda08f735f72 (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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
package hst_test

import (
	"reflect"
	"testing"

	"hakurei.app/fhs"
	"hakurei.app/hst"
)

func TestConfigValidate(t *testing.T) {
	t.Parallel()

	testCases := []struct {
		name    string
		config  *hst.Config
		flags   int
		wantErr error
	}{
		{"nil", nil, 0, &hst.AppError{Step: "validate configuration", Err: hst.ErrConfigNull,
			Msg: "invalid configuration"}},

		{"identity lower", &hst.Config{Identity: -1}, 0, &hst.AppError{Step: "validate configuration", Err: hst.ErrIdentityBounds,
			Msg: "identity -1 out of range"}},
		{"identity upper", &hst.Config{Identity: 10000}, 0, &hst.AppError{Step: "validate configuration", Err: hst.ErrIdentityBounds,
			Msg: "identity 10000 out of range"}},

		{"sched lower", &hst.Config{SchedPolicy: -1}, 0, &hst.AppError{Step: "validate configuration", Err: hst.ErrSchedPolicyBounds,
			Msg: "scheduling policy -1 out of range"}},
		{"sched upper", &hst.Config{SchedPolicy: 0xcafe}, 0, &hst.AppError{Step: "validate configuration", Err: hst.ErrSchedPolicyBounds,
			Msg: "scheduling policy 51966 out of range"}},

		{"dbus session", &hst.Config{SessionBus: &hst.BusConfig{See: []string{""}}}, 0,
			&hst.BadInterfaceError{Interface: "", Segment: "session"}},
		{"dbus system", &hst.Config{SystemBus: &hst.BusConfig{See: []string{""}}}, 0,
			&hst.BadInterfaceError{Interface: "", Segment: "system"}},

		{"container", &hst.Config{}, 0, &hst.AppError{Step: "validate configuration", Err: hst.ErrConfigNull,
			Msg: "configuration missing container state"}},
		{"home", &hst.Config{Container: &hst.ContainerConfig{}}, 0, &hst.AppError{Step: "validate configuration", Err: hst.ErrConfigNull,
			Msg: "container configuration missing path to home directory"}},
		{"shell", &hst.Config{Container: &hst.ContainerConfig{
			Home: fhs.AbsTmp,
		}}, 0, &hst.AppError{Step: "validate configuration", Err: hst.ErrConfigNull,
			Msg: "container configuration missing path to shell"}},
		{"path", &hst.Config{Container: &hst.ContainerConfig{
			Home:  fhs.AbsTmp,
			Shell: fhs.AbsTmp,
		}}, 0, &hst.AppError{Step: "validate configuration", Err: hst.ErrConfigNull,
			Msg: "container configuration missing path to initial program"}},

		{"env equals", &hst.Config{Container: &hst.ContainerConfig{
			Home:  fhs.AbsTmp,
			Shell: fhs.AbsTmp,
			Path:  fhs.AbsTmp,
			Env:   map[string]string{"TERM=": ""},
		}}, 0, &hst.AppError{Step: "validate configuration", Err: hst.ErrEnviron,
			Msg: `invalid environment variable "TERM="`}},
		{"env NUL", &hst.Config{Container: &hst.ContainerConfig{
			Home:  fhs.AbsTmp,
			Shell: fhs.AbsTmp,
			Path:  fhs.AbsTmp,
			Env:   map[string]string{"TERM\x00": ""},
		}}, 0, &hst.AppError{Step: "validate configuration", Err: hst.ErrEnviron,
			Msg: `invalid environment variable "TERM\x00"`}},

		{"insecure pulse", &hst.Config{Enablements: new(hst.EPulse), Container: &hst.ContainerConfig{
			Home:  fhs.AbsTmp,
			Shell: fhs.AbsTmp,
			Path:  fhs.AbsTmp,
		}}, 0, &hst.AppError{Step: "validate configuration", Err: hst.ErrInsecure,
			Msg: "enablement PulseAudio is insecure and no longer supported"}},

		{"direct wayland", &hst.Config{Enablements: new(hst.EWayland), DirectWayland: true, Container: &hst.ContainerConfig{
			Home:  fhs.AbsTmp,
			Shell: fhs.AbsTmp,
			Path:  fhs.AbsTmp,
		}}, 0, &hst.AppError{Step: "validate configuration", Err: hst.ErrInsecure,
			Msg: "direct_wayland is insecure and no longer supported"}},
		{"direct wayland allow", &hst.Config{Enablements: new(hst.EWayland), DirectWayland: true, Container: &hst.ContainerConfig{
			Home:  fhs.AbsTmp,
			Shell: fhs.AbsTmp,
			Path:  fhs.AbsTmp,
		}}, hst.VAllowInsecure, nil},

		{"direct pipewire", &hst.Config{Enablements: new(hst.EPipeWire), DirectPipeWire: true, Container: &hst.ContainerConfig{
			Home:  fhs.AbsTmp,
			Shell: fhs.AbsTmp,
			Path:  fhs.AbsTmp,
		}}, 0, &hst.AppError{Step: "validate configuration", Err: hst.ErrInsecure,
			Msg: "direct_pipewire is insecure and no longer supported"}},
		{"direct pipewire allow", &hst.Config{Enablements: new(hst.EPipeWire), DirectPipeWire: true, Container: &hst.ContainerConfig{
			Home:  fhs.AbsTmp,
			Shell: fhs.AbsTmp,
			Path:  fhs.AbsTmp,
		}}, hst.VAllowInsecure, nil},

		{"direct pulse", &hst.Config{Enablements: new(hst.EPulse), DirectPulse: true, Container: &hst.ContainerConfig{
			Home:  fhs.AbsTmp,
			Shell: fhs.AbsTmp,
			Path:  fhs.AbsTmp,
		}}, 0, &hst.AppError{Step: "validate configuration", Err: hst.ErrInsecure,
			Msg: "direct_pulse is insecure and no longer supported"}},
		{"direct pulse allow", &hst.Config{Enablements: new(hst.EPulse), DirectPulse: true, Container: &hst.ContainerConfig{
			Home:  fhs.AbsTmp,
			Shell: fhs.AbsTmp,
			Path:  fhs.AbsTmp,
		}}, hst.VAllowInsecure, nil},

		{"valid", &hst.Config{Container: &hst.ContainerConfig{
			Home:  fhs.AbsTmp,
			Shell: fhs.AbsTmp,
			Path:  fhs.AbsTmp,
		}}, 0, nil},
	}
	for _, tc := range testCases {
		t.Run(tc.name, func(t *testing.T) {
			t.Parallel()
			if err := tc.config.Validate(tc.flags); !reflect.DeepEqual(err, tc.wantErr) {
				t.Errorf("Validate: error = %#v, want %#v", err, tc.wantErr)
			}
		})
	}
}

func TestExtraPermConfig(t *testing.T) {
	t.Parallel()

	testCases := []struct {
		name   string
		config *hst.ExtraPermConfig
		want   string
	}{
		{"nil", nil, "<invalid>"},
		{"nil path", &hst.ExtraPermConfig{Path: nil}, "<invalid>"},
		{"r", &hst.ExtraPermConfig{Path: fhs.AbsRoot, Read: true}, "r--:/"},
		{"r+", &hst.ExtraPermConfig{Ensure: true, Path: fhs.AbsRoot, Read: true}, "r--+:/"},
		{"w", &hst.ExtraPermConfig{Path: hst.AbsPrivateTmp, Write: true}, "-w-:/.hakurei"},
		{"w+", &hst.ExtraPermConfig{Ensure: true, Path: hst.AbsPrivateTmp, Write: true}, "-w-+:/.hakurei"},
		{"x", &hst.ExtraPermConfig{Path: fhs.AbsRunUser, Execute: true}, "--x:/run/user/"},
		{"x+", &hst.ExtraPermConfig{Ensure: true, Path: fhs.AbsRunUser, Execute: true}, "--x+:/run/user/"},
		{"rwx", &hst.ExtraPermConfig{Path: fhs.AbsTmp, Read: true, Write: true, Execute: true}, "rwx:/tmp/"},
		{"rwx+", &hst.ExtraPermConfig{Ensure: true, Path: fhs.AbsTmp, Read: true, Write: true, Execute: true}, "rwx+:/tmp/"},
	}

	for _, tc := range testCases {
		t.Run(tc.name, func(t *testing.T) {
			t.Parallel()
			if got := tc.config.String(); got != tc.want {
				t.Errorf("String: %q, want %q", got, tc.want)
			}
		})
	}
}