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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
use crate::colours;
use crate::parser::ast::Action;
use chrono::Utc;
use std::collections::HashMap;
use std::thread;
use std::time::Duration;
use sysinfo::System;
use uuid as rust_uuid;
/// SystemBackend handles System actor actions and conditions.
/// This includes logging, pausing, timestamps, UUIDs, and service/port checks.
pub struct SystemBackend {
/// Stores output from system actions for test verification
pub last_output: String,
}
impl SystemBackend {
pub fn new() -> Self {
Self {
last_output: String::new(),
}
}
/// Executes a System action. Returns true if the action was handled.
pub fn execute_action(
&mut self,
action: &Action,
env_vars: &mut HashMap<String, String>,
verbose: bool,
) -> bool {
match action {
// System log: surface the message into output and log it.
Action::Log { message } => {
colours::info(&format!("[SYSTEM] {}", message));
if !self.last_output.is_empty() && !self.last_output.ends_with('\n') {
self.last_output.push('\n');
}
self.last_output.push_str(&format!("System: {}\n", message));
true
}
// Pause: sleep for the specified duration (seconds).
Action::Pause { duration } => {
let dur = Duration::from_secs_f32(*duration);
thread::sleep(dur);
true
}
// Timestamp: set a variable to the current timestamp.
Action::Timestamp { variable } => {
let now = Utc::now();
let ts = now.format("%Y-%m-%d_%H:%M:%S").to_string();
env_vars.insert(variable.clone(), ts.clone());
if verbose {
colours::info(&format!("[SYSTEM] Set {} = {}", variable, ts));
}
if !self.last_output.is_empty() && !self.last_output.ends_with('\n') {
self.last_output.push('\n');
}
self.last_output
.push_str(&format!("Timestamp {} = {}\n", variable, ts));
true
}
// Uuid: set a variable to a generated v4 UUID.
Action::Uuid { variable } => {
let uid = rust_uuid::Uuid::new_v4().to_string();
env_vars.insert(variable.clone(), uid.clone());
if verbose {
colours::info(&format!("[SYSTEM] Set {} = {}", variable, uid));
}
if !self.last_output.is_empty() && !self.last_output.ends_with('\n') {
self.last_output.push('\n');
}
self.last_output
.push_str(&format!("Uuid {} = {}\n", variable, uid));
true
}
_ => false, // Not a system action
}
}
/// Clears the last output buffer.
pub fn clear_output(&mut self) {
self.last_output.clear();
}
// --- System Condition Checks ---
/// Checks if a service/process is running using cross-platform sysinfo crate.
pub fn check_service_is_running(&self, name: &str, verbose: bool) -> bool {
if verbose {
println!("[SYSTEM] Checking if service/process '{}' is running", name);
}
let mut sys = System::new();
sys.refresh_processes(sysinfo::ProcessesToUpdate::All, true);
// Check if any process matches the name (case-insensitive on Windows)
let name_lower = name.to_lowercase();
for process in sys.processes().values() {
let process_name = process.name().to_string_lossy().to_lowercase();
// Match exact name or name without extension (for Windows .exe)
if process_name == name_lower
|| process_name == format!("{}.exe", name_lower)
|| process_name.trim_end_matches(".exe") == name_lower
{
if verbose {
println!(
"[SYSTEM] Found process '{}' with PID {}",
process.name().to_string_lossy(),
process.pid()
);
}
return true;
}
}
false
}
/// Checks if a service is stopped (not running).
pub fn check_service_is_stopped(&self, name: &str, verbose: bool) -> bool {
if verbose {
println!("[SYSTEM] Checking if service '{}' is stopped", name);
}
!self.check_service_is_running(name, false)
}
/// Checks if a service/executable is installed on the system (cross-platform).
pub fn check_service_is_installed(&self, name: &str, verbose: bool) -> bool {
if verbose {
println!(
"[SYSTEM] Checking if service/executable '{}' is installed",
name
);
}
// Use the `which` crate for cross-platform executable lookup
if which::which(name).is_ok() {
if verbose {
println!("[SYSTEM] Found '{}' in PATH", name);
}
return true;
}
// Platform-specific additional checks for service definitions
#[cfg(target_os = "macos")]
{
// Check launchd plist files
let launchd_paths = [
format!("/Library/LaunchDaemons/{}.plist", name),
format!("/Library/LaunchAgents/{}.plist", name),
format!(
"{}/Library/LaunchAgents/{}.plist",
std::env::var("HOME").unwrap_or_default(),
name
),
];
for path in &launchd_paths {
if std::path::Path::new(path).exists() {
if verbose {
println!("[SYSTEM] Found launchd plist at {}", path);
}
return true;
}
}
}
#[cfg(target_os = "linux")]
{
// Check systemd unit files
let systemd_paths = [
format!("/etc/systemd/system/{}.service", name),
format!("/lib/systemd/system/{}.service", name),
format!("/usr/lib/systemd/system/{}.service", name),
];
for path in &systemd_paths {
if std::path::Path::new(path).exists() {
if verbose {
println!("[SYSTEM] Found systemd unit at {}", path);
}
return true;
}
}
// Check init.d
let initd_path = format!("/etc/init.d/{}", name);
if std::path::Path::new(&initd_path).exists() {
if verbose {
println!("[SYSTEM] Found init.d script at {}", initd_path);
}
return true;
}
}
#[cfg(target_os = "windows")]
{
// Check Windows services using sc query
let output = std::process::Command::new("sc")
.args(["query", name])
.output();
if let Ok(output) = output {
if output.status.success() {
if verbose {
println!("[SYSTEM] Found Windows service '{}'", name);
}
return true;
}
}
}
false
}
/// Checks if a port is currently listening for connections.
pub fn check_port_is_listening(&self, port: u16, verbose: bool) -> bool {
if verbose {
println!("[SYSTEM] Checking if port {} is listening", port);
}
use std::net::TcpListener;
// Try to bind to the port - if it fails with AddrInUse, the port is in use (listening)
match TcpListener::bind(("127.0.0.1", port)) {
Ok(_) => {
// We were able to bind, so no one is listening on this port
false
}
Err(e) => {
// If the error is "address already in use", then something is listening
if e.kind() == std::io::ErrorKind::AddrInUse {
true
} else {
// Some other error (e.g., permission denied for low ports)
// Fall back to checking with lsof or netstat
self.check_port_with_system_command(port, verbose)
}
}
}
}
/// Checks if a port is closed (not listening).
pub fn check_port_is_closed(&self, port: u16, verbose: bool) -> bool {
if verbose {
println!("[SYSTEM] Checking if port {} is closed", port);
}
!self.check_port_is_listening(port, false)
}
/// Helper function to check port using system commands (fallback).
fn check_port_with_system_command(&self, port: u16, verbose: bool) -> bool {
#[cfg(target_os = "macos")]
{
let output = std::process::Command::new("lsof")
.args(["-i", &format!(":{}", port), "-P", "-n"])
.output();
if let Ok(output) = output {
let stdout = String::from_utf8_lossy(&output.stdout);
if verbose {
println!("[SYSTEM] lsof output: {}", stdout);
}
return stdout.contains("LISTEN");
}
false
}
#[cfg(target_os = "linux")]
{
let output = std::process::Command::new("ss")
.args(["-tlnp", &format!("sport = :{}", port)])
.output();
if let Ok(output) = output {
let stdout = String::from_utf8_lossy(&output.stdout);
if verbose {
println!("[SYSTEM] ss output: {}", stdout);
}
// ss returns header + data lines if port is listening
return stdout.lines().count() > 1;
}
false
}
#[cfg(target_os = "windows")]
{
let output = std::process::Command::new("netstat").args(["-an"]).output();
if let Ok(output) = output {
let stdout = String::from_utf8_lossy(&output.stdout);
if verbose {
println!("[SYSTEM] netstat output (checking for port {})", port);
}
// Look for LISTENING state on the specified port
let port_pattern = format!(":{}", port);
return stdout.lines().any(|line| {
line.contains(&port_pattern) && line.to_uppercase().contains("LISTENING")
});
}
false
}
#[cfg(not(any(target_os = "macos", target_os = "linux", target_os = "windows")))]
{
if verbose {
println!("[SYSTEM] Port check not fully supported on this platform");
}
false
}
}
}
impl Default for SystemBackend {
fn default() -> Self {
Self::new()
}
}