aboutsummaryrefslogtreecommitdiffhomepage
path: root/system/dispatcher_test.go
blob: 53d2b664728268a5aabd2de787876b5714d99c2d (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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
package system

import (
	"reflect"
	"slices"
	"testing"

	"hakurei.app/container/stub"
	"hakurei.app/system/acl"
)

// call initialises a [stub.Call].
// This keeps composites analysis happy without making the test cases too bloated.
func call(name string, args stub.ExpectArgs, ret any, err error) stub.Call {
	return stub.Call{Name: name, Args: args, Ret: ret, Err: err}
}

type opBehaviourTestCase struct {
	name string
	uid  int
	ec   Enablement
	op   Op

	apply        []stub.Call
	wantErrApply error

	revert        []stub.Call
	wantErrRevert error
}

func checkOpBehaviour(t *testing.T, testCases []opBehaviourTestCase) {
	t.Helper()

	t.Run("behaviour", func(t *testing.T) {
		t.Helper()

		for _, tc := range testCases {
			t.Run(tc.name, func(t *testing.T) {
				t.Helper()

				var ec *Criteria
				if tc.ec != 0xff {
					ec = (*Criteria)(&tc.ec)
				}

				sys, s := InternalNew(t, stub.Expect{Calls: slices.Concat(tc.apply, []stub.Call{{Name: stub.CallSeparator}}, tc.revert)}, tc.uid)
				defer stub.HandleExit(t)
				errApply := tc.op.apply(sys)
				s.Expects(stub.CallSeparator)
				if !reflect.DeepEqual(errApply, tc.wantErrApply) {
					t.Errorf("apply: error = %v, want %v", errApply, tc.wantErrApply)
				}
				if errApply != nil {
					goto out
				}

				if err := tc.op.revert(sys, ec); !reflect.DeepEqual(err, tc.wantErrRevert) {
					t.Errorf("revert: error = %v, want %v", err, tc.wantErrRevert)
				}

			out:
				s.VisitIncomplete(func(s *stub.Stub[syscallDispatcher]) {
					count := s.Pos() - 1 // separator
					if count < len(tc.apply) {
						t.Errorf("apply: %d calls, want %d", count, len(tc.apply))
					} else {
						t.Errorf("revert: %d calls, want %d", count-len(tc.apply), len(tc.revert))
					}
				})
			})
		}
	})
}

type opsBuilderTestCase struct {
	name string
	uid  int
	f    func(sys *I)
	want []Op
	exp  stub.Expect
}

func checkOpsBuilder(t *testing.T, fname string, testCases []opsBuilderTestCase) {
	t.Helper()

	t.Run("build", func(t *testing.T) {
		t.Helper()

		for _, tc := range testCases {
			t.Run(tc.name, func(t *testing.T) {
				t.Helper()

				sys, s := InternalNew(t, tc.exp, tc.uid)
				defer stub.HandleExit(t)
				tc.f(sys)
				s.VisitIncomplete(func(s *stub.Stub[syscallDispatcher]) {
					t.Helper()

					t.Errorf("%s: %d calls, want %d", fname, s.Pos(), s.Len())
				})
				if !slices.EqualFunc(sys.ops, tc.want, func(op Op, v Op) bool { return op.Is(v) }) {
					t.Errorf("ops: %#v, want %#v", sys.ops, tc.want)
				}
			})
		}
	})
}

type opIsTestCase struct {
	name  string
	op, v Op
	want  bool
}

func checkOpIs(t *testing.T, testCases []opIsTestCase) {
	t.Helper()

	t.Run("is", func(t *testing.T) {
		t.Helper()

		for _, tc := range testCases {
			t.Run(tc.name, func(t *testing.T) {
				t.Helper()

				if got := tc.op.Is(tc.v); got != tc.want {
					t.Errorf("Is: %v, want %v", got, tc.want)
				}
			})
		}
	})
}

type opMetaTestCase struct {
	name string
	op   Op

	wantType   Enablement
	wantPath   string
	wantString string
}

func checkOpMeta(t *testing.T, testCases []opMetaTestCase) {
	t.Helper()

	t.Run("meta", func(t *testing.T) {
		t.Helper()

		for _, tc := range testCases {
			t.Run(tc.name, func(t *testing.T) {
				t.Helper()

				t.Run("type", func(t *testing.T) {
					t.Helper()

					if got := tc.op.Type(); got != tc.wantType {
						t.Errorf("Type: %q, want %q", got, tc.wantType)
					}
				})

				t.Run("path", func(t *testing.T) {
					t.Helper()

					if got := tc.op.Path(); got != tc.wantPath {
						t.Errorf("Path: %q, want %q", got, tc.wantPath)
					}
				})

				t.Run("string", func(t *testing.T) {
					t.Helper()

					if got := tc.op.String(); got != tc.wantString {
						t.Errorf("String: %s, want %s", got, tc.wantString)
					}
				})
			})
		}
	})
}

// InternalNew initialises [I] with a stub syscallDispatcher.
func InternalNew(t *testing.T, want stub.Expect, uid int) (*I, *stub.Stub[syscallDispatcher]) {
	k := stub.New(t, func(s *stub.Stub[syscallDispatcher]) syscallDispatcher { return &kstub{s} }, want)
	sys := New(t.Context(), uid)
	sys.syscallDispatcher = &kstub{k}
	return sys, k
}

type kstub struct{ *stub.Stub[syscallDispatcher] }

func (k *kstub) new(f func(k syscallDispatcher)) { k.Helper(); k.New(f) }

func (k *kstub) aclUpdate(name string, uid int, perms ...acl.Perm) error {
	k.Helper()
	return k.Expects("aclUpdate").Error(
		stub.CheckArg(k.Stub, "name", name, 0),
		stub.CheckArg(k.Stub, "uid", uid, 1),
		stub.CheckArgReflect(k.Stub, "perms", perms, 2))
}

func (k *kstub) verbose(v ...any) {
	k.Helper()
	if k.Expects("verbose").Error(
		stub.CheckArgReflect(k.Stub, "v", v, 0)) != nil {
		k.FailNow()
	}
}

func (k *kstub) verbosef(format string, v ...any) {
	k.Helper()
	if k.Expects("verbosef").Error(
		stub.CheckArg(k.Stub, "format", format, 0),
		stub.CheckArgReflect(k.Stub, "v", v, 1)) != nil {
		k.FailNow()
	}
}