Debian Codegraph @

tests/by-util/test_runcon.rs

raw
1 // This file is part of the uutils coreutils package.
2 //
3 // For the full copyright and license information, please view the LICENSE
4 // file that was distributed with this source code.
5
6 // spell-checker:ignore (jargon) xattributes
7
8 #![cfg(all(feature = "selinux", any(target_os = "linux", target_os = "android")))]
9
10 use uutests::new_ucmd;
11
12 // TODO: Check the implementation of `--compute` somehow.
13
14 #[test]
15 fn version() {
16 new_ucmd!().arg("--version").succeeds();
17 new_ucmd!().arg("-V").succeeds();
18 }
19
20 #[test]
21 fn help() {
22 new_ucmd!().arg("--help").succeeds();
23 new_ucmd!().arg("-h").succeeds();
24 }
25
26 #[test]
27 fn invalid_input() {
28 new_ucmd!().arg("-/").fails_with_code(125);
29 }
30
31 #[test]
32 fn print() {
33 new_ucmd!().succeeds();
34
35 for flag in ["-c", "--compute"] {
36 new_ucmd!().arg(flag).succeeds();
37 }
38
39 for flag in [
40 "-t", "--type", "-u", "--user", "-r", "--role", "-l", "--range",
41 ] {
42 new_ucmd!().args(&[flag, "example"]).succeeds();
43 new_ucmd!().args(&[flag, "example1,example2"]).succeeds();
44 }
45 }
46
47 #[cfg(target_os = "linux")]
48 #[test]
49 fn print_context_write_error_reported_not_panic() {
50 use std::fs::OpenOptions;
51
52 let dev_full = OpenOptions::new().write(true).open("/dev/full").unwrap();
53 new_ucmd!()
54 .set_stdout(dev_full)
55 .fails_with_code(1)
56 .stderr_is("runcon: write error: No space left on device\n");
57 }
58
59 #[test]
60 fn invalid() {
61 new_ucmd!().arg("invalid").fails_with_code(1);
62
63 let args = &[
64 "unconfined_u:unconfined_r:unconfined_t:s0",
65 "inexistent-file",
66 ];
67 // When SELinux is enabled, this should fail with 127 (command not found)
68 // When SELinux is not enabled, this fails with 1 (SELinux not enabled error)
69 let expected_code = if uucore::selinux::is_selinux_enabled() {
70 127
71 } else {
72 1
73 };
74 new_ucmd!().args(args).fails_with_code(expected_code);
75
76 let args = &["invalid", "/bin/true"];
77 new_ucmd!().args(args).fails_with_code(1);
78
79 let args = &["--compute", "inexistent-file"];
80 new_ucmd!().args(args).fails_with_code(1);
81
82 let args = &["--compute", "--compute"];
83 new_ucmd!().args(args).fails_with_code(125);
84
85 // clap has an issue that makes this test fail: https://github.com/clap-rs/clap/issues/1543
86 // TODO: Enable this code once the issue is fixed in the clap version we're using.
87 //new_ucmd!().arg("--compute=example").fails_with_code(1);
88
89 for flag in [
90 "-t", "--type", "-u", "--user", "-r", "--role", "-l", "--range",
91 ] {
92 new_ucmd!().arg(flag).fails_with_code(125);
93
94 let args = &[flag, "example", flag, "example"];
95 new_ucmd!().args(args).fails_with_code(125);
96 }
97 }
98
99 #[test]
100 #[cfg(all(feature = "selinux", any(target_os = "linux", target_os = "android")))]
101 fn plain_context() {
102 let ctx = "unconfined_u:unconfined_r:unconfined_t:s0-s0";
103 new_ucmd!().args(&[ctx, "/bin/true"]).succeeds();
104 new_ucmd!().args(&[ctx, "/bin/false"]).fails_with_code(1);
105
106 let output = new_ucmd!().args(&[ctx, "sestatus", "-v"]).succeeds();
107 let r = get_sestatus_context(output.stdout());
108 assert_eq!(r, "unconfined_u:unconfined_r:unconfined_t:s0");
109
110 let ctx = "system_u:unconfined_r:unconfined_t:s0-s0";
111 new_ucmd!().args(&[ctx, "/bin/true"]).succeeds();
112
113 let ctx = "system_u:system_r:unconfined_t:s0";
114 let output = new_ucmd!().args(&[ctx, "sestatus", "-v"]).succeeds();
115 assert_eq!(get_sestatus_context(output.stdout()), ctx);
116 }
117
118 #[test]
119 #[cfg(all(feature = "selinux", any(target_os = "linux", target_os = "android")))]
120 fn custom_context() {
121 let t_ud = "unconfined_t";
122 let u_ud = "unconfined_u";
123 let r_ud = "unconfined_r";
124
125 new_ucmd!().args(&["--compute", "/bin/true"]).succeeds();
126
127 let args = &["--compute", "/bin/false"];
128 new_ucmd!().args(args).fails_with_code(1);
129
130 let args = &["--type", t_ud, "/bin/true"];
131 new_ucmd!().args(args).succeeds();
132
133 let args = &["--compute", "--type", t_ud, "/bin/true"];
134 new_ucmd!().args(args).succeeds();
135
136 let args = &["--user=system_u", "/bin/true"];
137 new_ucmd!().args(args).succeeds();
138
139 let args = &["--compute", "--user=system_u", "/bin/true"];
140 new_ucmd!().args(args).succeeds();
141
142 let args = &["--role=system_r", "/bin/true"];
143 new_ucmd!().args(args).succeeds();
144
145 let args = &["--compute", "--role=system_r", "/bin/true"];
146 new_ucmd!().args(args).succeeds();
147
148 new_ucmd!().args(&["--range=s0", "/bin/true"]).succeeds();
149
150 let args = &["--compute", "--range=s0", "/bin/true"];
151 new_ucmd!().args(args).succeeds();
152
153 for (ctx, u, r) in [
154 ("unconfined_u:unconfined_r:unconfined_t:s0", u_ud, r_ud),
155 ("system_u:unconfined_r:unconfined_t:s0", "system_u", r_ud),
156 ("unconfined_u:system_r:unconfined_t:s0", u_ud, "system_r"),
157 ("system_u:system_r:unconfined_t:s0", "system_u", "system_r"),
158 ] {
159 let args = &["-t", t_ud, "-u", u, "-r", r, "-l", "s0", "sestatus", "-v"];
160
161 let output = new_ucmd!().args(args).succeeds();
162 assert_eq!(get_sestatus_context(output.stdout()), ctx);
163 }
164 }
165
166 fn get_sestatus_context(output: &[u8]) -> &str {
167 let re = regex::bytes::Regex::new(r"Current context:\s*(\S+)\s*")
168 .expect("Invalid regular expression");
169
170 output
171 .split(|&b| b == b'\n')
172 .find(|&b| b.starts_with(b"Current context:"))
173 .and_then(|line| {
174 re.captures_iter(line)
175 .next()
176 .and_then(|c| c.get(1))
177 .as_ref()
178 .map(regex::bytes::Match::as_bytes)
179 })
180 .and_then(|bytes| std::str::from_utf8(bytes).ok())
181 .expect("Output of sestatus is unexpected")
182 }