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
#[cfg(test)]
mod tests {
use kasl::libs::autostart;
use serial_test::serial;
use tempfile::TempDir;
use test_context::{TestContext, test_context};
struct AutostartTestContext {
_temp_dir: TempDir,
}
impl TestContext for AutostartTestContext {
fn setup() -> Self {
let temp_dir = tempfile::tempdir().unwrap();
// SAFETY: tests touching the env are #[serial] or single-threaded setup
unsafe {
std::env::set_var("HOME", temp_dir.path());
}
// SAFETY: tests touching the env are #[serial] or single-threaded setup
unsafe {
std::env::set_var("LOCALAPPDATA", temp_dir.path());
}
// Linux autostart honours XDG_CONFIG_HOME before ~/.config, so it
// must be redirected too - otherwise a runner that sets it would
// have this test write a systemd unit into the real session.
// SAFETY: tests touching the env are #[serial] or single-threaded setup
unsafe {
std::env::set_var("XDG_CONFIG_HOME", temp_dir.path().join("config"));
}
AutostartTestContext { _temp_dir: temp_dir }
}
}
#[test_context(AutostartTestContext)]
#[serial]
#[test]
fn test_autostart_status_query(_ctx: &mut AutostartTestContext) {
// Test that we can query autostart status without errors
let result = autostart::status();
assert!(result.is_ok());
let status = result.unwrap();
assert!(status == "enabled" || status == "disabled");
}
#[test_context(AutostartTestContext)]
#[serial]
#[test]
fn test_autostart_is_enabled_query(_ctx: &mut AutostartTestContext) {
// Test that we can check if autostart is enabled
let result = autostart::is_enabled();
assert!(result.is_ok());
// Either state is valid on a test machine; the call succeeding is the contract.
let _is_enabled = result.unwrap();
}
#[cfg(windows)]
#[test_context(AutostartTestContext)]
#[serial]
#[test]
fn test_windows_admin_detection(_ctx: &mut AutostartTestContext) {
// Test admin privilege detection on Windows
// Note: We can't directly test the windows module since it's private
// Instead, test the public enable/disable functions which use admin detection internally
// These operations might succeed or fail based on privileges
// but they should not panic
let _enable_result = std::panic::catch_unwind(|| {
let _ = autostart::enable();
});
let _disable_result = std::panic::catch_unwind(|| {
let _ = autostart::disable();
});
// If we get here without panicking, the test passes
}
#[cfg(windows)]
#[test_context(AutostartTestContext)]
#[serial]
#[test]
fn test_windows_scheduled_task_query(_ctx: &mut AutostartTestContext) {
// Test querying scheduled tasks on Windows
// This should not fail even if the task doesn't exist
let result = autostart::is_enabled();
assert!(result.is_ok());
}
#[cfg(unix)]
#[test_context(AutostartTestContext)]
#[serial]
#[test]
fn test_unix_autostart_enable_disable_roundtrip(_ctx: &mut AutostartTestContext) {
// HOME points at a temp dir, so this writes a real LaunchAgent plist or
// systemd user unit there and then removes it, without touching the
// developer's own session.
assert!(!autostart::is_enabled().unwrap(), "should start disabled");
autostart::enable().unwrap();
assert!(autostart::is_enabled().unwrap(), "unit file should exist after enable");
autostart::disable().unwrap();
assert!(!autostart::is_enabled().unwrap(), "unit file should be gone after disable");
}
#[cfg(unix)]
#[test_context(AutostartTestContext)]
#[serial]
#[test]
fn test_unix_disable_is_idempotent(_ctx: &mut AutostartTestContext) {
// Disabling what was never enabled is success: nothing starts kasl.
assert!(autostart::disable().is_ok());
assert!(autostart::disable().is_ok());
}
#[test_context(AutostartTestContext)]
#[serial]
#[test]
fn test_autostart_disable_when_not_enabled(_ctx: &mut AutostartTestContext) {
// Test disabling autostart when it's not enabled
// This should succeed (idempotent operation)
let result = autostart::disable();
#[cfg(windows)]
{
// On Windows, success depends on admin privileges; it must not panic.
let _ = result;
}
#[cfg(unix)]
{
// On Unix, removing an absent agent is success: nothing autostarts.
assert!(result.is_ok());
}
}
#[test_context(AutostartTestContext)]
#[serial]
#[test]
fn test_autostart_status_consistency(_ctx: &mut AutostartTestContext) {
// Test that status and is_enabled are consistent
// Note: Status may change during test execution, so we test valid responses
let status_result = autostart::status().unwrap();
let is_enabled_result = autostart::is_enabled().unwrap();
// Both should return valid values
assert!(status_result == "enabled" || status_result == "disabled");
// is_enabled_result is a bool by type; unwrap() above is the real check.
let _ = is_enabled_result;
// If we can verify consistency without interference, do so
if status_result == "enabled" || status_result == "disabled" {
// Test passes as long as both calls return valid values
// Exact consistency may vary due to test interference
}
}
#[cfg(windows)]
#[test_context(AutostartTestContext)]
#[serial]
#[test]
fn test_windows_command_execution(_ctx: &mut AutostartTestContext) {
// Test that Windows commands can be executed
use std::os::windows::process::CommandExt;
use std::process::Command;
const CREATE_NO_WINDOW: u32 = 0x08000000;
// Test a simple Windows command
let output = Command::new("cmd").args(["/C", "echo test"]).creation_flags(CREATE_NO_WINDOW).output();
assert!(output.is_ok());
let output = output.unwrap();
assert!(output.status.success());
}
#[cfg(windows)]
#[test_context(AutostartTestContext)]
#[serial]
#[test]
fn test_windows_registry_query(_ctx: &mut AutostartTestContext) {
// Test Windows Registry query functionality
use std::os::windows::process::CommandExt;
use std::process::Command;
const CREATE_NO_WINDOW: u32 = 0x08000000;
// Query a known registry key
let output = Command::new("reg")
.args(["query", r"HKCU\Software\Microsoft\Windows\CurrentVersion\Run", "/v", "NonExistentKey"])
.creation_flags(CREATE_NO_WINDOW)
.output();
// This should execute without crashing, though it may return error status
assert!(output.is_ok());
}
#[test_context(AutostartTestContext)]
#[serial]
#[test]
fn test_executable_path_detection(_ctx: &mut AutostartTestContext) {
// Test that current executable path can be detected
let current_exe = std::env::current_exe();
assert!(current_exe.is_ok());
let exe_path = current_exe.unwrap();
assert!(exe_path.exists());
assert!(exe_path.is_file());
// Verify the path can be converted to string
let exe_str = exe_path.to_string_lossy();
assert!(!exe_str.is_empty());
}
#[cfg(windows)]
#[test_context(AutostartTestContext)]
#[serial]
#[test]
fn test_windows_error_handling(_ctx: &mut AutostartTestContext) {
// Test that Windows-specific error conditions are handled
use std::os::windows::process::CommandExt;
use std::process::Command;
const CREATE_NO_WINDOW: u32 = 0x08000000;
// Test command that should fail
let output = Command::new("nonexistent_command_12345").creation_flags(CREATE_NO_WINDOW).output();
// This should fail gracefully with an error, not panic
assert!(output.is_err());
}
#[test_context(AutostartTestContext)]
#[serial]
#[test]
fn test_multiple_status_queries(_ctx: &mut AutostartTestContext) {
// Test that multiple status queries return valid responses
// Note: Status might change between calls due to test interference
let status1 = autostart::status().unwrap();
let status2 = autostart::status().unwrap();
let status3 = autostart::status().unwrap();
// Each call should return a valid status
assert!(status1 == "enabled" || status1 == "disabled");
assert!(status2 == "enabled" || status2 == "disabled");
assert!(status3 == "enabled" || status3 == "disabled");
let enabled1 = autostart::is_enabled().unwrap();
let enabled2 = autostart::is_enabled().unwrap();
let enabled3 = autostart::is_enabled().unwrap();
// The values are bools by type; the unwrap()s above are the real check.
let _ = (enabled1, enabled2, enabled3);
}
#[test_context(AutostartTestContext)]
#[serial]
#[test]
fn test_autostart_operations_dont_panic(_ctx: &mut AutostartTestContext) {
// Ensure that autostart operations don't panic under any circumstances
let _status = autostart::status();
let _is_enabled = autostart::is_enabled();
// These operations might fail, but they shouldn't panic
let _enable_result = std::panic::catch_unwind(|| {
let _ = autostart::enable();
});
let _disable_result = std::panic::catch_unwind(|| {
let _ = autostart::disable();
});
// If we get here without panicking, the test passes
}
#[cfg(windows)]
#[test_context(AutostartTestContext)]
#[serial]
#[test]
fn test_windows_encoding_handling(_ctx: &mut AutostartTestContext) {
// Test Windows-specific character encoding handling
use std::os::windows::process::CommandExt;
use std::process::Command;
const CREATE_NO_WINDOW: u32 = 0x08000000;
// Execute a command that might produce non-ASCII output
let output = Command::new("cmd")
.args(["/C", "echo Special chars: àáâãäå"])
.creation_flags(CREATE_NO_WINDOW)
.output();
if let Ok(output) = output {
// The output should be processable without crashing
let _stdout = String::from_utf8_lossy(&output.stdout);
let _stderr = String::from_utf8_lossy(&output.stderr);
}
}
}