forked from uutils/coreutils
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_mknod.rs
More file actions
260 lines (236 loc) · 6.46 KB
/
Copy pathtest_mknod.rs
File metadata and controls
260 lines (236 loc) · 6.46 KB
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
// 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 nconfined
use std::os::unix::fs::PermissionsExt;
#[cfg(feature = "feat_selinux")]
use uucore::selinux::get_getfattr_output;
use uutests::new_ucmd;
use uutests::util::TestScenario;
use uutests::util::run_ucmd_as_root;
use uutests::util_name;
//Reject 2^32+1 major/minor device number
#[test]
fn test_mknod_overflow_major_minor() {
new_ucmd!()
.arg("lg32")
.arg("c")
.arg("4294967296")
.arg("1")
.fails_with_code(1)
.no_stdout()
.stderr_contains("invalid value '4294967296'"); //clap generated message, thats fine.
}
#[test]
fn test_mknod_invalid_arg() {
new_ucmd!()
.arg("--foo")
.fails_with_code(1)
.no_stdout()
.stderr_contains("unexpected argument '--foo' found");
}
#[test]
fn test_mknod_help() {
new_ucmd!()
.arg("--help")
.succeeds()
.no_stderr()
.stdout_contains("Usage:");
}
#[test]
fn test_mknod_version() {
assert!(
new_ucmd!()
.arg("--version")
.succeeds()
.no_stderr()
.stdout_str()
.starts_with("mknod")
);
}
#[test]
fn test_mknod_fifo_default_writable() {
let ts = TestScenario::new(util_name!());
ts.ucmd().arg("test_file").arg("p").succeeds();
assert!(ts.fixtures.is_fifo("test_file"));
assert!(!ts.fixtures.metadata("test_file").permissions().readonly());
}
#[test]
fn test_mknod_fifo_mnemonic_usage() {
let ts = TestScenario::new(util_name!());
ts.ucmd().arg("test_file").arg("pipe").succeeds();
assert!(ts.fixtures.is_fifo("test_file"));
}
#[test]
fn test_mknod_fifo_read_only() {
let ts = TestScenario::new(util_name!());
ts.ucmd()
.arg("-m")
.arg("a=r")
.arg("test_file")
.arg("p")
.succeeds();
assert!(ts.fixtures.is_fifo("test_file"));
assert!(ts.fixtures.metadata("test_file").permissions().readonly());
}
#[test]
fn test_mknod_fifo_invalid_extra_operand() {
new_ucmd!()
.arg("test_file")
.arg("p")
.arg("1")
.arg("2")
.fails()
.stderr_contains("Fifos do not have major and minor device numbers");
}
#[test]
fn test_mknod_character_device_requires_major_and_minor() {
new_ucmd!()
.arg("test_file")
.arg("c")
.fails_with_code(1)
.stderr_contains("Special files require major and minor device numbers.");
new_ucmd!()
.arg("test_file")
.arg("c")
.arg("1")
.fails_with_code(1)
.stderr_contains("Special files require major and minor device numbers.");
new_ucmd!()
.arg("test_file")
.arg("c")
.arg("1")
.arg("c")
.fails()
.stderr_contains("invalid value 'c'");
new_ucmd!()
.arg("test_file")
.arg("c")
.arg("c")
.arg("1")
.fails()
.stderr_contains("invalid value 'c'");
}
#[test]
fn test_mknod_invalid_mode() {
new_ucmd!()
.arg("--mode")
.arg("rw")
.arg("test_file")
.arg("p")
.fails()
.no_stdout()
.code_is(1)
.stderr_contains("invalid mode");
}
#[test]
fn test_mknod_mode_permissions() {
for test_mode in [0o0666, 0o0000, 0o0444, 0o0004, 0o0040, 0o0400, 0o0644] {
let ts = TestScenario::new(util_name!());
let filename = format!("null_file-{test_mode:04o}");
if let Ok(result) = run_ucmd_as_root(
&ts,
&[
"--mode",
&format!("{test_mode:04o}"),
&filename,
"c",
"1",
"3",
],
) {
result.success().no_stdout();
} else {
print!("Test skipped; `mknod c 1 3` for null char dev requires root user");
break;
}
assert!(ts.fixtures.is_char_device(&filename));
let permissions = ts.fixtures.metadata(&filename).permissions();
assert_eq!(test_mode, PermissionsExt::mode(&permissions) & 0o777);
}
}
#[test]
fn test_mknod_mode_comma_separated() {
let ts = TestScenario::new(util_name!());
ts.ucmd()
.arg("-m")
.arg("u=rwx,g=rx,o=")
.arg("test_file")
.arg("p")
.succeeds();
assert!(ts.fixtures.is_fifo("test_file"));
assert_eq!(
ts.fixtures.metadata("test_file").permissions().mode() & 0o777,
0o750
);
}
#[test]
#[cfg(feature = "feat_selinux")]
fn test_mknod_selinux() {
let ts = TestScenario::new(util_name!());
let at = &ts.fixtures;
let dest = "test_file";
let args = [
"-Z",
"--context",
"--context=unconfined_u:object_r:user_tmp_t:s0",
];
for arg in args {
ts.ucmd()
.arg(arg)
.arg("-m")
.arg("a=r")
.arg(dest)
.arg("p")
.succeeds();
assert!(ts.fixtures.is_fifo("test_file"));
assert!(ts.fixtures.metadata("test_file").permissions().readonly());
let context_value = get_getfattr_output(&at.plus_as_string(dest));
assert!(
context_value.contains("unconfined_u"),
"Expected 'unconfined_u' not found in getfattr output:\n{context_value}"
);
at.remove(&at.plus_as_string(dest));
}
}
#[test]
#[cfg(feature = "feat_selinux")]
fn test_mknod_selinux_invalid() {
let scene = TestScenario::new(util_name!());
let at = &scene.fixtures;
let dest = "orig";
let args = [
"--context=a",
"--context=unconfined_u:object_r:user_tmp_t:s0:a",
"--context=nconfined_u:object_r:user_tmp_t:s0",
];
for arg in args {
new_ucmd!()
.arg(arg)
.arg("-m")
.arg("a=r")
.arg(dest)
.arg("p")
.fails()
.stderr_contains("failed to");
if at.file_exists(dest) {
at.remove(dest);
}
}
}
#[test]
#[cfg(feature = "feat_selinux")]
fn test_mknod_selinux_invalid_cleanup() {
let scene = TestScenario::new(util_name!());
let at = &scene.fixtures;
let dest = "test_fifo";
new_ucmd!()
.arg("--context=invalid_context_t")
.arg(at.plus_as_string(dest))
.arg("p")
.fails()
.no_stdout();
// invalid context → node must not exist
assert!(!at.file_exists(dest));
}