-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_ydotool_injector.rs
More file actions
207 lines (181 loc) · 6.43 KB
/
Copy pathtest_ydotool_injector.rs
File metadata and controls
207 lines (181 loc) · 6.43 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
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
//! Unit tests for ydotool_injector.rs
use crate::ydotool_injector::{
candidate_socket_paths, locate_existing_socket, ydotool_daemon_socket, YdotoolInjector,
};
use crate::types::{InjectionConfig, InjectionContext};
use crate::TextInjector;
use anyhow::Result;
use serial_test::serial;
use std::env;
use std::fs::{self, File};
use std::os::unix::fs::PermissionsExt;
use std::path::{Path, PathBuf};
use tempfile::{tempdir, TempDir};
/// A test harness to create a controlled environment for ydotool tests.
struct TestHarness {
_temp_dir: TempDir,
bin_dir: PathBuf,
home_dir: PathBuf,
runtime_dir: PathBuf,
original_path: String,
/// Path to a file that mock binaries can use to report arguments.
output_file: PathBuf,
}
impl TestHarness {
fn new() -> Result<Self> {
let temp_dir = tempdir()?;
let base_path = temp_dir.path();
let bin_dir = base_path.join("bin");
let home_dir = base_path.join("home");
let runtime_dir = base_path.join("run");
let uinput_path = base_path.join("uinput");
let output_file = base_path.join("output.log");
fs::create_dir_all(&bin_dir)?;
fs::create_dir_all(&home_dir)?;
fs::create_dir_all(&runtime_dir)?;
File::create(&uinput_path)?;
File::create(&output_file)?;
let original_path = env::var("PATH").unwrap_or_default();
let new_path = format!("{}:{}", bin_dir.display(), original_path);
env::set_var("PATH", new_path);
env::set_var("HOME", &home_dir);
env::set_var("XDG_RUNTIME_DIR", &runtime_dir);
env::set_var("UINPUT_PATH_OVERRIDE", &uinput_path);
env::remove_var("YDOTOOL_SOCKET");
env::remove_var("UID");
Ok(Self {
_temp_dir: temp_dir,
bin_dir,
home_dir,
runtime_dir,
original_path,
output_file,
})
}
/// Creates a mock executable file that echoes a specific path.
fn create_which_mock(&self, target_binary: &Path) -> Result<()> {
let content = format!("#!/bin/sh\necho {}", target_binary.display());
self.create_mock_binary("which", &content, true)?;
Ok(())
}
fn create_mock_binary(&self, name: &str, content: &str, executable: bool) -> Result<PathBuf> {
let path = self.bin_dir.join(name);
fs::write(&path, content)?;
if executable {
fs::set_permissions(&path, fs::Permissions::from_mode(0o755))?;
}
Ok(path)
}
fn create_mock_socket(&self, path: &Path) -> Result<()> {
if let Some(parent) = path.parent() {
fs::create_dir_all(parent)?;
}
File::create(path)?;
Ok(())
}
/// Reads the content of the argument log file.
fn read_output(&self) -> Result<String> {
Ok(fs::read_to_string(&self.output_file)?)
}
}
impl Drop for TestHarness {
fn drop(&mut self) {
env::set_var("PATH", &self.original_path);
env::remove_var("HOME");
env::remove_var("XDG_RUNTIME_DIR");
env::remove_var("YDOTOOL_SOCKET");
env::remove_var("UID");
env::remove_var("UINPUT_PATH_OVERRIDE");
}
}
#[test]
#[serial]
fn test_candidate_socket_paths_priority() {
let harness = TestHarness::new().unwrap();
env::set_var("YDOTOOL_SOCKET", "/custom/socket");
env::set_var("UID", "1001");
let paths = candidate_socket_paths();
assert_eq!(paths.len(), 4);
assert_eq!(paths[0], PathBuf::from("/custom/socket"));
}
#[test]
#[serial]
fn test_locate_existing_socket_finds_first_available() {
let harness = TestHarness::new().unwrap();
let _ = harness.runtime_dir.join(".ydotool_socket");
let expected_socket = harness.home_dir.join(".ydotool").join("socket");
harness.create_mock_socket(&expected_socket).unwrap();
let located = locate_existing_socket();
assert_eq!(located, Some(expected_socket));
}
#[tokio::test]
#[serial]
async fn test_check_binary_permissions_success() {
let harness = TestHarness::new().unwrap();
let ydotool_path = harness
.create_mock_binary("ydotool", "#!/bin/sh\nexit 0", true)
.unwrap();
harness.create_which_mock(&ydotool_path).unwrap();
let result = YdotoolInjector::check_binary_permissions("ydotool");
assert!(result.is_ok());
}
#[tokio::test]
#[serial]
async fn test_check_ydotool_available_when_binary_and_socket_present() {
let harness = TestHarness::new().unwrap();
let ydotool_path = harness
.create_mock_binary("ydotool", "", true)
.unwrap();
harness.create_which_mock(&ydotool_path).unwrap();
let socket_path = harness.home_dir.join(".ydotool/socket");
harness.create_mock_socket(&socket_path).unwrap();
let injector = YdotoolInjector::new(InjectionConfig::default());
assert!(injector.is_available().await);
}
#[tokio::test]
#[serial]
async fn test_inject_text_uses_paste_by_default() {
let harness = TestHarness::new().unwrap();
let ydotool_script = format!(
"#!/bin/sh\necho \"$@\" > {}",
harness.output_file.display()
);
let ydotool_path = harness
.create_mock_binary("ydotool", &ydotool_script, true)
.unwrap();
harness.create_which_mock(&ydotool_path).unwrap();
let socket_path = harness.home_dir.join(".ydotool/socket");
harness.create_mock_socket(&socket_path).unwrap();
let injector = YdotoolInjector::new(InjectionConfig::default());
let result = injector.inject_text("hello", None).await;
assert!(result.is_ok());
let output = harness.read_output().unwrap();
assert!(output.contains("key ctrl+v"));
}
#[tokio::test]
#[serial]
async fn test_inject_text_falls_back_to_type() {
let harness = TestHarness::new().unwrap();
// This mock fails for 'key' command, but succeeds for 'type'
let ydotool_script = format!(
r#"#!/bin/sh
if [ "$1" = "key" ]; then
exit 1
else
echo "$@" > {}
fi
"#,
harness.output_file.display()
);
let ydotool_path = harness
.create_mock_binary("ydotool", &ydotool_script, true)
.unwrap();
harness.create_which_mock(&ydotool_path).unwrap();
let socket_path = harness.home_dir.join(".ydotool/socket");
harness.create_mock_socket(&socket_path).unwrap();
let injector = YdotoolInjector::new(InjectionConfig::default());
let result = injector.inject_text("world", None).await;
assert!(result.is_ok());
let output = harness.read_output().unwrap();
assert!(output.contains("type --delay 10 world"));
}