nspkg / engine / root_filesystem_test.v
101 lines · 97 sloc · 3.92 KB · c01b0795d1db56c7bce2539b2c46b110637fdc1a
Raw
1module engine
2
3import os
4
5fn test_root_filesystem_follows_directory_links_without_escaping_root() ! {
6 base := os.join_path(os.vtmp_dir(), 'native-root-files-${os.getpid()}')
7 dir := os.join_path(base, 'root')
8 os.mkdir_all(os.join_path(dir, 'usr', 'lib'))!
9 os.mkdir_all(os.join_path(base, 'db'))!
10 defer { os.rmdir_all(base) or { eprintln(err) } }
11 file := os.join_path(dir, 'usr', 'lib', 'file')
12 os.write_file(file, 'inside')!
13 os.symlink('/usr/lib', os.join_path(dir, 'lib'))!
14 os.symlink('usr/lib', os.join_path(dir, 'relative'))!
15 os.symlink('../lib', os.join_path(dir, 'usr', 'alias'))!
16 os.symlink('file', os.join_path(dir, 'usr', 'lib', 'leaf'))!
17 os.symlink('cycle', os.join_path(dir, 'cycle'))!
18 mut handle := initialize(dir, os.join_path(base, 'db'))!
19 defer { handle.release() or { eprintln(err) } }
20 mut root := handle.open_root_filesystem()!
21 defer { root.close() }
22 for path in ['lib/file', 'relative/file', 'usr/alias/file', 'usr/./lib/file', 'usr//lib///file',
23 'lib/leaf'] {
24 assert root.md5sum(path)! == compute_md5sum(file) or { '' }
25 }
26 assert root.stat('lib/leaf', false)!.info.mode & 0o170000 == 0o120000
27 assert root.stat('lib/leaf', true)!.info.mode & 0o170000 == 0o100000
28 if _ := root.stat('cycle/file', false) {
29 assert false
30 }
31 for path in ['../outside', '/usr/lib/file', '.', './', 'usr/../../outside', 'usr//../file',
32 './//./', 'bad\x00name'] {
33 if _ := root.resolve(path, false) {
34 assert false, path
35 }
36 }
37 outside := os.join_path(base, 'outside')
38 os.mkdir(outside)!
39 os.write_file(os.join_path(outside, 'sentinel'), 'outside')!
40 os.symlink(outside, os.join_path(dir, 'escape'))!
41 assert !root.stat('escape/sentinel', false)!.present
42 assert os.read_file(os.join_path(outside, 'sentinel'))! == 'outside'
43 root.rename('lib/file', 'relative/renamed')!
44 assert os.read_file(os.join_path(dir, 'usr', 'lib', 'renamed'))! == 'inside'
45 root.unlink('usr/lib/renamed', false)!
46 assert !os.exists(file)
47 root.unlink('lib/', false)!
48 assert !os.is_link(os.join_path(dir, 'lib'))
49 assert os.is_dir(os.join_path(dir, 'usr', 'lib'))
50}
51
52fn test_root_paths_keep_the_opened_directory_when_a_parent_link_changes() ! {
53 base := os.join_path(os.vtmp_dir(), 'native-root-pinned-${os.getpid()}')
54 dir := os.join_path(base, 'root')
55 for path in ['a', 'b', 'db'] { os.mkdir_all(os.join_path(dir, path))! }
56 defer { os.rmdir_all(base) or { eprintln(err) } }
57 os.write_file(os.join_path(dir, 'a', 'file'), 'a')!
58 os.write_file(os.join_path(dir, 'b', 'file'), 'b')!
59 os.symlink('a', os.join_path(dir, 'link'))!
60 mut handle := initialize(dir, os.join_path(dir, 'db'))!
61 defer { handle.release() or { eprintln(err) } }
62 mut root := handle.open_root_filesystem()!
63 defer { root.close() }
64 mut path := root.resolve('link/file', false)!
65 defer { path.close() }
66 os.rm(os.join_path(dir, 'link'))!
67 os.symlink('b', os.join_path(dir, 'link'))!
68 assert C.unlinkat(path.fd, unsafe { &char(path.name.str) }, 0) == 0
69 assert !os.exists(os.join_path(dir, 'a', 'file'))
70 assert os.read_file(os.join_path(dir, 'b', 'file'))! == 'b'
71}
72
73fn test_package_root_accepts_descriptor_zero() ! {
74 base := os.join_path(os.vtmp_dir(), 'native-root-fd-zero-${os.getpid()}')
75 os.mkdir_all(os.join_path(base, 'root'))!
76 os.mkdir_all(os.join_path(base, 'db'))!
77 defer { os.rmdir_all(base) or { eprintln(err) } }
78 os.write_file(os.join_path(base, 'root', 'file'), 'content')!
79 mut handle := initialize(os.join_path(base, 'root'), os.join_path(base, 'db'))!
80 defer { handle.release() or { eprintln(err) } }
81 child := C.fork()
82 assert child >= 0
83 if child == 0 {
84 if C.close(0) != 0 { C._exit(2) }
85 mut root := handle.open_root_filesystem() or {
86 C._exit(3)
87 return
88 }
89 if root.fd != 0 { C._exit(4) }
90 root.unlink('file', false) or { C._exit(5) }
91 root.close()
92 C._exit(0)
93 }
94 mut status := i32(0)
95 for {
96 if C.v_alpm_wait_child(child, &status, 0) >= 0 { break }
97 if C.errno != C.EINTR { return os.last_error() }
98 }
99 assert C.v_alpm_exit_status(status) == 0
100 assert !os.exists(os.join_path(base, 'root', 'file'))
101}
102