forked from uutils/coreutils
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_runcon.rs
155 lines (120 loc) · 4.67 KB
/
test_runcon.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
// 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 (jargon) xattributes
#![cfg(feature = "feat_selinux")]
use crate::common::util::*;
// TODO: Check the implementation of `--compute` somehow.
#[test]
fn version() {
new_ucmd!().arg("--version").succeeds();
new_ucmd!().arg("-V").succeeds();
}
#[test]
fn help() {
new_ucmd!().arg("--help").succeeds();
new_ucmd!().arg("-h").succeeds();
}
#[test]
fn print() {
new_ucmd!().succeeds();
for flag in ["-c", "--compute"] {
new_ucmd!().arg(flag).succeeds();
}
for flag in [
"-t", "--type", "-u", "--user", "-r", "--role", "-l", "--range",
] {
new_ucmd!().args(&[flag, "example"]).succeeds();
new_ucmd!().args(&[flag, "example1,example2"]).succeeds();
}
}
#[test]
fn invalid() {
new_ucmd!().arg("invalid").fails().code_is(1);
let args = &[
"unconfined_u:unconfined_r:unconfined_t:s0",
"inexistent-file",
];
new_ucmd!().args(args).fails().code_is(127);
let args = &["invalid", "/bin/true"];
new_ucmd!().args(args).fails().code_is(1);
let args = &["--compute", "inexistent-file"];
new_ucmd!().args(args).fails().code_is(1);
let args = &["--compute", "--compute"];
new_ucmd!().args(args).fails().code_is(1);
// clap has an issue that makes this test fail: https://github.com/clap-rs/clap/issues/1543
// TODO: Enable this code once the issue is fixed in the clap version we're using.
//new_ucmd!().arg("--compute=example").fails().code_is(1);
for flag in [
"-t", "--type", "-u", "--user", "-r", "--role", "-l", "--range",
] {
new_ucmd!().arg(flag).fails().code_is(1);
let args = &[flag, "example", flag, "example"];
new_ucmd!().args(args).fails().code_is(1);
}
}
#[test]
fn plain_context() {
let ctx = "unconfined_u:unconfined_r:unconfined_t:s0-s0";
new_ucmd!().args(&[ctx, "/bin/true"]).succeeds();
new_ucmd!().args(&[ctx, "/bin/false"]).fails().code_is(1);
let output = new_ucmd!().args(&[ctx, "sestatus", "-v"]).succeeds();
let r = get_sestatus_context(output.stdout());
assert_eq!(r, "unconfined_u:unconfined_r:unconfined_t:s0");
let ctx = "system_u:unconfined_r:unconfined_t:s0-s0";
new_ucmd!().args(&[ctx, "/bin/true"]).succeeds();
let ctx = "system_u:system_r:unconfined_t:s0";
let output = new_ucmd!().args(&[ctx, "sestatus", "-v"]).succeeds();
assert_eq!(get_sestatus_context(output.stdout()), ctx);
}
#[test]
fn custom_context() {
let t_ud = "unconfined_t";
let u_ud = "unconfined_u";
let r_ud = "unconfined_r";
new_ucmd!().args(&["--compute", "/bin/true"]).succeeds();
let args = &["--compute", "/bin/false"];
new_ucmd!().args(args).fails().code_is(1);
let args = &["--type", t_ud, "/bin/true"];
new_ucmd!().args(args).succeeds();
let args = &["--compute", "--type", t_ud, "/bin/true"];
new_ucmd!().args(args).succeeds();
let args = &["--user=system_u", "/bin/true"];
new_ucmd!().args(args).succeeds();
let args = &["--compute", "--user=system_u", "/bin/true"];
new_ucmd!().args(args).succeeds();
let args = &["--role=system_r", "/bin/true"];
new_ucmd!().args(args).succeeds();
let args = &["--compute", "--role=system_r", "/bin/true"];
new_ucmd!().args(args).succeeds();
new_ucmd!().args(&["--range=s0", "/bin/true"]).succeeds();
let args = &["--compute", "--range=s0", "/bin/true"];
new_ucmd!().args(args).succeeds();
for (ctx, u, r) in [
("unconfined_u:unconfined_r:unconfined_t:s0", u_ud, r_ud),
("system_u:unconfined_r:unconfined_t:s0", "system_u", r_ud),
("unconfined_u:system_r:unconfined_t:s0", u_ud, "system_r"),
("system_u:system_r:unconfined_t:s0", "system_u", "system_r"),
] {
let args = &["-t", t_ud, "-u", u, "-r", r, "-l", "s0", "sestatus", "-v"];
let output = new_ucmd!().args(args).succeeds();
assert_eq!(get_sestatus_context(output.stdout()), ctx);
}
}
fn get_sestatus_context(output: &[u8]) -> &str {
let re = regex::bytes::Regex::new(r"Current context:\s*(\S+)\s*")
.expect("Invalid regular expression");
output
.split(|&b| b == b'\n')
.find(|&b| b.starts_with(b"Current context:"))
.and_then(|line| {
re.captures_iter(line)
.next()
.and_then(|c| c.get(1))
.as_ref()
.map(regex::bytes::Match::as_bytes)
})
.and_then(|bytes| std::str::from_utf8(bytes).ok())
.expect("Output of sestatus is unexpected")
}