forked from uutils/coreutils
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_chroot.rs
202 lines (166 loc) · 5.65 KB
/
test_chroot.rs
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
// This file is part of the uutils coreutils package.
//
// For the full copyright and license information, please view the LICENSE
// file that was distributed with this source code.
// spell-checker:ignore (words) araba newroot userspec chdir pwd's isroot
#[cfg(not(target_os = "android"))]
use crate::common::util::is_ci;
use crate::common::util::{run_ucmd_as_root, TestScenario};
#[test]
fn test_invalid_arg() {
new_ucmd!().arg("--definitely-invalid").fails().code_is(125);
}
#[test]
fn test_missing_operand() {
let result = new_ucmd!().fails();
result.code_is(125);
assert!(result
.stderr_str()
.starts_with("error: the following required arguments were not provided"));
assert!(result.stderr_str().contains("<newroot>"));
}
#[test]
#[cfg(not(target_os = "android"))]
fn test_enter_chroot_fails() {
// NOTE: since #2689 this test also ensures that we don't regress #2687
let (at, mut ucmd) = at_and_ucmd!();
at.mkdir("jail");
let result = ucmd.arg("jail").fails();
result.code_is(125);
assert!(result
.stderr_str()
.starts_with("chroot: cannot chroot to 'jail': Operation not permitted (os error 1)"));
}
#[test]
fn test_no_such_directory() {
let (at, mut ucmd) = at_and_ucmd!();
at.touch(at.plus_as_string("a"));
ucmd.arg("a")
.fails()
.stderr_is("chroot: cannot change root directory to 'a': no such directory\n")
.code_is(125);
}
#[test]
fn test_invalid_user_spec() {
let (at, mut ucmd) = at_and_ucmd!();
at.mkdir("a");
let result = ucmd.arg("a").arg("--userspec=ARABA:").fails();
result.code_is(125);
assert!(result.stderr_str().starts_with("chroot: invalid userspec"));
}
#[test]
#[cfg(not(target_os = "android"))]
fn test_preference_of_userspec() {
let scene = TestScenario::new(util_name!());
let result = scene.cmd("whoami").run();
if is_ci() && result.stderr_str().contains("No such user/group") {
// In the CI, some server are failing to return whoami.
// As seems to be a configuration issue, ignoring it
return;
}
println!("result.stdout = {}", result.stdout_str());
println!("result.stderr = {}", result.stderr_str());
let username = result.stdout_str().trim_end();
let ts = TestScenario::new("id");
let result = ts.cmd("id").arg("-g").arg("-n").run();
println!("result.stdout = {}", result.stdout_str());
println!("result.stderr = {}", result.stderr_str());
if is_ci() && result.stderr_str().contains("cannot find name for user ID") {
// In the CI, some server are failing to return id.
// As seems to be a configuration issue, ignoring it
return;
}
let group_name = result.stdout_str().trim_end();
let (at, mut ucmd) = at_and_ucmd!();
at.mkdir("a");
let result = ucmd
.arg("a")
.arg("--user")
.arg("fake")
.arg("-G")
.arg("ABC,DEF")
.arg(format!("--userspec={username}:{group_name}"))
.fails();
result.code_is(125);
println!("result.stdout = {}", result.stdout_str());
println!("result.stderr = {}", result.stderr_str());
}
#[test]
fn test_default_shell() {
// NOTE: This test intends to trigger code which can only be reached with root permissions.
let ts = TestScenario::new(util_name!());
let at = &ts.fixtures;
let dir = "CHROOT_DIR";
at.mkdir(dir);
let shell = std::env::var("SHELL").unwrap_or_else(|_| "/bin/sh".to_string());
let expected = format!("chroot: failed to run command '{shell}': No such file or directory");
if let Ok(result) = run_ucmd_as_root(&ts, &[dir]) {
result.stderr_contains(expected);
} else {
print!("Test skipped; requires root user");
}
}
#[test]
fn test_chroot() {
let ts = TestScenario::new(util_name!());
let at = &ts.fixtures;
let dir = "CHROOT_DIR";
at.mkdir(dir);
if let Ok(result) = run_ucmd_as_root(&ts, &[dir, "whoami"]) {
result.success().no_stderr().stdout_is("root");
} else {
print!("Test skipped; requires root user");
}
if let Ok(result) = run_ucmd_as_root(&ts, &[dir, "pwd"]) {
result.success().no_stderr().stdout_is("/");
} else {
print!("Test skipped; requires root user");
}
}
#[test]
fn test_chroot_skip_chdir_not_root() {
let (at, mut ucmd) = at_and_ucmd!();
let dir = "foobar";
at.mkdir(dir);
ucmd.arg("--skip-chdir")
.arg(dir)
.fails()
.stderr_contains("chroot: option --skip-chdir only permitted if NEWROOT is old '/'")
.code_is(125);
}
#[test]
fn test_chroot_skip_chdir() {
let ts = TestScenario::new(util_name!());
let at = ts.fixtures.clone();
let dirs = ["/", "/.", "/..", "isroot"];
at.symlink_file("/", "isroot");
for dir in dirs {
let env_cd = std::env::current_dir().unwrap();
if let Ok(result) = run_ucmd_as_root(&ts, &[dir, "--skip-chdir"]) {
// Should return the same path
assert_eq!(
result.success().no_stderr().stdout_str(),
env_cd.to_str().unwrap()
);
} else {
print!("Test skipped; requires root user");
}
}
}
#[test]
fn test_chroot_extra_arg() {
let ts = TestScenario::new(util_name!());
let at = &ts.fixtures;
let dir = "CHROOT_DIR";
at.mkdir(dir);
let env_cd = std::env::current_dir().unwrap();
// Verify that -P is pwd's and not chroot
if let Ok(result) = run_ucmd_as_root(&ts, &[dir, "pwd", "-P"]) {
assert_eq!(
result.success().no_stderr().stdout_str(),
env_cd.to_str().unwrap()
);
} else {
print!("Test skipped; requires root user");
}
}