forked from Velocidex/velociraptor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmount_test.go
119 lines (97 loc) · 3.25 KB
/
mount_test.go
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
package accessors
import (
"io/ioutil"
"testing"
"www.velocidex.com/golang/velociraptor/vtesting/assert"
)
func TestMountFilesystemAccessor(t *testing.T) {
// The root filesystem contains some directories where the other
// filesystems are mounted.
root_path := MustNewLinuxOSPath("")
root_fs_accessor := NewVirtualFilesystemAccessor(root_path)
root_fs_accessor.SetVirtualDirectory(
MustNewLinuxOSPath("/usr"),
&VirtualFileInfo{
IsDir_: true,
})
root_fs_accessor.SetVirtualDirectory(
MustNewLinuxOSPath("/home"), &VirtualFileInfo{
IsDir_: true,
})
root_fs_accessor.SetVirtualDirectory(
MustNewLinuxOSPath("/lib/foo"), &VirtualFileInfo{
RawData: []byte("lib foo file"),
})
// Child filesystem contains some files.
bin_fs_accessor := NewVirtualFilesystemAccessor(root_path)
bin_fs_accessor.SetVirtualDirectory(
MustNewLinuxOSPath("/bin/ls"), &VirtualFileInfo{
RawData: []byte("bin ls file"),
})
// This will contain a deeper mount again.
bin_fs_accessor.SetVirtualDirectory(
MustNewLinuxOSPath("/bin/deep"), &VirtualFileInfo{
IsDir_: true,
})
bin_fs_accessor.SetVirtualDirectory(
MustNewLinuxOSPath("/bin/foo/bar"), &VirtualFileInfo{
RawData: []byte("bar file"),
})
// Another filesystem will be mounted deeper again
deep_fs := NewVirtualFilesystemAccessor(root_path)
deep_fs.SetVirtualDirectory(
MustNewLinuxOSPath("/Users/mic/test.txt"), &VirtualFileInfo{
RawData: []byte("text"),
})
// Create a mount filesystem to organize the different
// filesystems. Use Linux path convensions.
mount_fs := NewMountFileSystemAccessor(
MustNewLinuxOSPath(""), root_fs_accessor)
// This means the root of the bin_fs_accessor is mounted at /usr
mount_fs.AddMapping(
MustNewLinuxOSPath("/"),
MustNewLinuxOSPath("/usr"), bin_fs_accessor)
// It is also possible to mount into a directory inside another
// filesystem. This is similar to NTFS hard links or Linux "bind"
// mounts. The following means that the tree under /home is taken
// from /bin/foo/ on the bin_fs_accessor
mount_fs.AddMapping(
MustNewLinuxOSPath("/bin/foo"),
MustNewLinuxOSPath("/home"), bin_fs_accessor)
// Mount deep_fs inside the bin_fs_accessor mount point
mount_fs.AddMapping(
MustNewLinuxOSPath("/"),
MustNewLinuxOSPath("/usr/bin/deep"), deep_fs)
ls := func(path string) []string {
children, err := mount_fs.ReadDir(path)
assert.NoError(t, err)
results := []string{}
for _, c := range children {
results = append(results, c.FullPath())
}
return results
}
// Listing the root filesystem
assert.Equal(t, []string{"/usr", "/home", "/lib"}, ls("/"))
assert.Equal(t,
[]string{"/usr/bin/ls", "/usr/bin/deep", "/usr/bin/foo"},
ls("/usr/bin"))
// /usr/bin/deep/Users/mic/ is mounted twice:
// 1. /usr/bin is mounted to bin_fs_accessor
// 2. /usr/bin/deep is mounted to deep_fs
assert.Equal(t,
[]string{"/usr/bin/deep/Users/mic/test.txt"},
ls("/usr/bin/deep/Users/mic/"))
assert.Equal(t,
[]string{"/usr/bin/deep/Users/mic"},
ls("/usr/bin/deep/Users/"))
// Check the file contents
cat := func(path string) string {
fd, err := mount_fs.Open(path)
assert.NoError(t, err)
data, err := ioutil.ReadAll(fd)
assert.NoError(t, err)
return string(data)
}
assert.Equal(t, "text", cat("/usr/bin/deep/Users/mic/test.txt"))
}