-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcheck_generic.rs
More file actions
133 lines (118 loc) · 3.81 KB
/
check_generic.rs
File metadata and controls
133 lines (118 loc) · 3.81 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
use indoc::indoc;
use rstest::{fixture, rstest};
use super::helpers::TestEnv;
#[fixture]
fn check_env() -> TestEnv {
TestEnv::new(indoc! {"
rules:
- deny: 'rm -rf /'
message: 'Dangerous command'
- allow: 'git status'
"})
}
// --- CLI argument mode ---
#[rstest]
#[case::deny_rm(&["rm", "-rf", "/"], 0, "deny")]
#[case::allow_git_status(&["git", "status"], 0, "allow")]
#[case::comment_before_command(&["# description\ngit status"], 0, "allow")]
#[case::comment_only(&["# just a comment"], 0, "ask")]
fn check_command_arg(
check_env: TestEnv,
#[case] command: &[&str],
#[case] expected_exit: i32,
#[case] expected_decision: &str,
) {
let assert = check_env
.command()
.arg("check")
.arg("--")
.args(command)
.assert();
let output = assert.code(expected_exit).get_output().stdout.clone();
let json: serde_json::Value =
serde_json::from_slice(&output).unwrap_or_else(|e| panic!("invalid JSON: {e}"));
assert_eq!(json["decision"], expected_decision);
}
// --- stdin JSON mode ---
#[rstest]
fn check_stdin_json_deny(check_env: TestEnv) {
let assert = check_env
.command()
.arg("check")
.write_stdin(r#"{"command":"rm -rf /"}"#)
.assert();
let output = assert.code(0).get_output().stdout.clone();
let json: serde_json::Value =
serde_json::from_slice(&output).unwrap_or_else(|e| panic!("invalid JSON: {e}"));
assert_eq!(json["decision"], "deny");
}
#[rstest]
fn check_stdin_json_allow(check_env: TestEnv) {
let assert = check_env
.command()
.arg("check")
.write_stdin(r#"{"command":"git status"}"#)
.assert();
let output = assert.code(0).get_output().stdout.clone();
let json: serde_json::Value =
serde_json::from_slice(&output).unwrap_or_else(|e| panic!("invalid JSON: {e}"));
assert_eq!(json["decision"], "allow");
}
// --- No input: exit 2 ---
#[rstest]
fn check_no_input_exits_2(check_env: TestEnv) {
let assert = check_env.command().arg("check").write_stdin("").assert();
assert.code(2);
}
// --- Plaintext stdin ---
#[rstest]
fn check_plaintext_stdin_single_line(check_env: TestEnv) {
let assert = check_env
.command()
.arg("check")
.write_stdin("git status\n")
.assert();
let output = assert.code(0).get_output().stdout.clone();
let json: serde_json::Value =
serde_json::from_slice(&output).unwrap_or_else(|e| panic!("invalid JSON: {e}"));
assert_eq!(json["decision"], "allow");
}
// --- Check deny includes reason ---
#[rstest]
fn check_deny_includes_reason(check_env: TestEnv) {
let assert = check_env
.command()
.args(["check", "--", "rm", "-rf", "/"])
.assert();
let output = assert.code(0).get_output().stdout.clone();
let json: serde_json::Value =
serde_json::from_slice(&output).unwrap_or_else(|e| panic!("invalid JSON: {e}"));
assert_eq!(json["decision"], "deny");
assert!(json["reason"].is_string());
}
// --- Check with sandbox info ---
#[rstest]
fn check_allow_with_sandbox_info() {
let env = TestEnv::new(indoc! {"
rules:
- allow: 'python3 *'
sandbox: restricted
definitions:
sandbox:
restricted:
fs:
writable: [./tmp]
network:
allow: true
"});
let assert = env
.command()
.args(["check", "--", "python3", "script.py"])
.assert();
let output = assert.code(0).get_output().stdout.clone();
let json: serde_json::Value =
serde_json::from_slice(&output).unwrap_or_else(|e| panic!("invalid JSON: {e}"));
assert_eq!(json["decision"], "allow");
assert!(json["sandbox"].is_object());
assert_eq!(json["sandbox"]["preset"], "restricted");
}