File: bind_filter_test.go

package info (click to toggle)
golang-github-microsoft-go-winio 0.6.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 764 kB
  • sloc: makefile: 3
file content (302 lines) | stat: -rw-r--r-- 7,588 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
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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
//go:build windows
// +build windows

package bindfilter

import (
	"errors"
	"fmt"
	"os"
	"path/filepath"
	"strings"
	"testing"

	"golang.org/x/sys/windows"
)

func TestApplyFileBinding(t *testing.T) {
	requireElevated(t)

	source := t.TempDir()
	destination := t.TempDir()
	fileName := "testFile.txt"
	srcFile := filepath.Join(source, fileName)
	dstFile := filepath.Join(destination, fileName)

	err := ApplyFileBinding(destination, source, false)
	if err != nil {
		t.Fatal(err)
	}
	defer removeFileBinding(t, destination)

	data := []byte("bind filter test")

	if err := os.WriteFile(srcFile, data, 0600); err != nil {
		t.Fatal(err)
	}

	readData, err := os.ReadFile(dstFile)
	if err != nil {
		t.Fatal(err)
	}

	if string(readData) != string(data) {
		t.Fatalf("source and destination file contents differ. Expected: %s, got: %s", string(data), string(readData))
	}

	// Remove the file on the mount point. The mount is not read-only, this should work.
	if err := os.Remove(dstFile); err != nil {
		t.Fatalf("failed to remove file from mount point: %s", err)
	}

	// Check that it's gone from the source as well.
	if _, err := os.Stat(srcFile); err == nil {
		t.Fatalf("expected file %s to be gone but is not", srcFile)
	}
}

func removeFileBinding(t *testing.T, mountpoint string) {
	t.Helper()
	if err := RemoveFileBinding(mountpoint); err != nil {
		t.Logf("failed to remove file binding from %s: %q", mountpoint, err)
	}
}

func TestApplyFileBindingReadOnly(t *testing.T) {
	requireElevated(t)

	source := t.TempDir()
	destination := t.TempDir()
	fileName := "testFile.txt"
	srcFile := filepath.Join(source, fileName)
	dstFile := filepath.Join(destination, fileName)

	err := ApplyFileBinding(destination, source, true)
	if err != nil {
		t.Fatal(err)
	}
	defer removeFileBinding(t, destination)

	data := []byte("bind filter test")

	if err := os.WriteFile(srcFile, data, 0600); err != nil {
		t.Fatal(err)
	}

	readData, err := os.ReadFile(dstFile)
	if err != nil {
		t.Fatal(err)
	}

	if string(readData) != string(data) {
		t.Fatalf("source and destination file contents differ. Expected: %s, got: %s", string(data), string(readData))
	}

	// Attempt to remove the file on the mount point
	err = os.Remove(dstFile)
	if err == nil {
		t.Fatalf("should not be able to remove a file from a read-only mount")
	}
	if !errors.Is(err, os.ErrPermission) {
		t.Fatalf("expected an access denied error, got: %q", err)
	}

	// Attempt to write on the read-only mount point.
	err = os.WriteFile(dstFile, []byte("something else"), 0600)
	if err == nil {
		t.Fatalf("should not be able to overwrite a file from a read-only mount")
	}
	if !errors.Is(err, os.ErrPermission) {
		t.Fatalf("expected an access denied error, got: %q", err)
	}
}

func TestEnsureOnlyOneTargetCanBeMounted(t *testing.T) {
	requireElevated(t)
	requireBuild(t, RS5+1) // support added after RS5

	source := t.TempDir()
	secondarySource := t.TempDir()
	destination := t.TempDir()

	err := ApplyFileBinding(destination, source, false)
	if err != nil {
		t.Fatal(err)
	}

	defer removeFileBinding(t, destination)

	err = ApplyFileBinding(destination, secondarySource, false)
	if err == nil {
		removeFileBinding(t, destination)
		t.Fatalf("we should not be able to mount multiple targets in the same destination")
	}
}

func checkSourceIsMountedOnDestination(src, dst string) (bool, error) {
	mappings, err := GetBindMappings(dst)
	if err != nil {
		return false, err
	}

	found := false
	// There may be pre-existing mappings on the system.
	for _, mapping := range mappings {
		if mapping.MountPoint == dst {
			found = true
			if len(mapping.Targets) != 1 {
				return false, fmt.Errorf("expected only one target, got: %s", strings.Join(mapping.Targets, ", "))
			}
			if mapping.Targets[0] != src {
				return false, fmt.Errorf("expected target to be %s, got %s", src, mapping.Targets[0])
			}
			break
		}
	}

	return found, nil
}

func TestGetBindMappings(t *testing.T) {
	requireElevated(t)
	requireBuild(t, RS5+1) // support added after RS5

	// GetBindMappings will expand short paths like ADMINI~1 and PROGRA~1 to their
	// full names. In order to properly match the names later, we expand them here.
	srcShort := t.TempDir()
	source, err := getFinalPath(srcShort)
	if err != nil {
		t.Fatalf("failed to get long path")
	}

	dstShort := t.TempDir()
	destination, err := getFinalPath(dstShort)
	if err != nil {
		t.Fatalf("failed to get long path")
	}

	err = ApplyFileBinding(destination, source, false)
	if err != nil {
		t.Fatal(err)
	}
	defer removeFileBinding(t, destination)

	hasMapping, err := checkSourceIsMountedOnDestination(source, destination)
	if err != nil {
		t.Fatal(err)
	}

	if !hasMapping {
		t.Fatalf("expected to find %s mounted on %s, but could not", source, destination)
	}
}

func TestRemoveFileBinding(t *testing.T) {
	requireElevated(t)

	srcShort := t.TempDir()
	source, err := getFinalPath(srcShort)
	if err != nil {
		t.Fatalf("failed to get long path")
	}

	dstShort := t.TempDir()
	destination, err := getFinalPath(dstShort)
	if err != nil {
		t.Fatalf("failed to get long path")
	}

	fileName := "testFile.txt"
	srcFile := filepath.Join(source, fileName)
	dstFile := filepath.Join(destination, fileName)
	data := []byte("bind filter test")

	if err := os.WriteFile(srcFile, data, 0600); err != nil {
		t.Fatal(err)
	}

	err = ApplyFileBinding(destination, source, false)
	if err != nil {
		t.Fatal(err)
	}

	if _, err := os.Stat(dstFile); err != nil {
		removeFileBinding(t, destination)
		t.Fatalf("expected to find %s, but could not", dstFile)
	}

	if err := RemoveFileBinding(destination); err != nil {
		t.Fatal(err)
	}

	if _, err := os.Stat(dstFile); err == nil {
		t.Fatalf("expected %s to be gone, but it is not", dstFile)
	}
}

func TestGetBindMappingsSymlinks(t *testing.T) {
	requireElevated(t)
	requireBuild(t, RS5+1) // support added after RS5

	srcShort := t.TempDir()
	sourceNested := filepath.Join(srcShort, "source")
	if err := os.MkdirAll(sourceNested, 0600); err != nil {
		t.Fatalf("failed to create folder: %s", err)
	}
	simlinkSource := filepath.Join(srcShort, "symlink")
	if err := os.Symlink(sourceNested, simlinkSource); err != nil {
		t.Fatalf("failed to create symlink: %s", err)
	}

	// We'll need the long form of the source folder, as we expect bfSetupFilter()
	// to resolve the symlink and create a mapping to the actual source the symlink
	// points to.
	source, err := getFinalPath(sourceNested)
	if err != nil {
		t.Fatalf("failed to get long path")
	}

	dstShort := t.TempDir()
	destination, err := getFinalPath(dstShort)
	if err != nil {
		t.Fatalf("failed to get long path")
	}

	// Use the symlink as a source for the mapping.
	err = ApplyFileBinding(destination, simlinkSource, false)
	if err != nil {
		t.Fatal(err)
	}
	defer removeFileBinding(t, destination)

	// We expect the mapping to point to the folder the symlink points to, not to the
	// actual symlink.
	hasMapping, err := checkSourceIsMountedOnDestination(source, destination)
	if err != nil {
		t.Fatal(err)
	}

	if !hasMapping {
		t.Fatalf("expected to find %s mounted on %s, but could not", source, destination)
	}
}

func requireElevated(tb testing.TB) {
	tb.Helper()
	if !windows.GetCurrentProcessToken().IsElevated() {
		tb.Skip("requires elevated privileges")
	}
}

const RS5 = 17763

//todo: also check that `bindfltapi.dll` exists

// require current build to be >= build
func requireBuild(tb testing.TB, build uint32) {
	tb.Helper()
	_, _, b := windows.RtlGetNtVersionNumbers()
	if b < build {
		tb.Skipf("requires build %d+; current build is %d", build, b)
	}
}