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
// Common test utilities for test-bd integration tests
#[cfg(test)]
pub mod test_utils {
#![allow(dead_code)]
use std::fs;
use std::path::Path;
/// Check if any ublk block devices exist in /dev
/// Note: We only check for ublkb* (block devices), not ublkc* (control devices)
/// Control devices are always present and managed by the kernel driver
fn check_ublk_devices() -> Result<Vec<String>, String> {
let dev_path = Path::new("/dev");
if !dev_path.exists() {
return Err("/dev directory does not exist".to_string());
}
let mut ublk_devices = Vec::new();
match fs::read_dir(dev_path) {
Ok(entries) => {
for entry in entries.flatten() {
let file_name = entry.file_name();
let name = file_name.to_string_lossy();
// Only check for ublkb* (block devices), not ublkc* (control devices)
if name.starts_with("ublkb") {
ublk_devices.push(name.to_string());
}
}
}
Err(e) => {
return Err(format!("Failed to read /dev directory: {}", e));
}
}
Ok(ublk_devices)
}
/// Check if /run/ublksrvd/ directory exists and list its contents
fn check_ublksrvd_dir() -> Result<Vec<String>, String> {
let ublksrvd_path = Path::new("/run/ublksrvd");
if !ublksrvd_path.exists() {
// Directory doesn't exist, which is fine (no leftover state)
return Ok(Vec::new());
}
let mut files = Vec::new();
match fs::read_dir(ublksrvd_path) {
Ok(entries) => {
for entry in entries.flatten() {
files.push(entry.path().to_string_lossy().to_string());
}
}
Err(e) => {
return Err(format!("Failed to read /run/ublksrvd directory: {}", e));
}
}
Ok(files)
}
/// Setup function to run before tests
/// Ensures no ublk devices exist and /run/ublksrvd/ is empty
pub fn test_setup() -> Result<(), String> {
log::debug!("\n=== Test Setup ===");
// Check for existing ublk devices
let devices = check_ublk_devices()?;
if !devices.is_empty() {
let error_msg = format!(
"Found existing ublk devices at test start: {:?}\n\
Please clean up before running tests:\n\
- List devices: ls -l /dev/ublk*\n\
- Delete devices: for i in /dev/ublkb*; do test-bd del --id $(basename $i | sed 's/ublkb//'); done",
devices
);
log::debug!("ERROR: {}", error_msg);
return Err(error_msg);
}
log::debug!("No ublk devices found in /dev");
// Check /run/ublksrvd/ directory
let ublksrvd_files = check_ublksrvd_dir()?;
if !ublksrvd_files.is_empty() {
let error_msg = format!(
"Found files in /run/ublksrvd/ at test start: {:?}\n\
Please clean up before running tests:\n\
- sudo rm -rf /run/ublksrvd/*",
ublksrvd_files
);
log::debug!("ERROR: {}", error_msg);
return Err(error_msg);
}
log::debug!("/run/ublksrvd/ is empty");
log::debug!("=== Setup Complete ===\n");
Ok(())
}
/// Teardown function to run after tests
/// Verifies no ublk devices remain and /run/ublksrvd/ is empty
/// If failures found, logs them and attempts cleanup
pub fn test_teardown() -> Result<(), String> {
log::debug!("\n=== Test Teardown ===");
let mut errors = Vec::new();
// Check for remaining ublk devices
match check_ublk_devices() {
Ok(devices) => {
if !devices.is_empty() {
let error = format!("Found ublk devices remaining after test: {:?}", devices);
log::debug!("ERROR: {}", error);
errors.push(error);
} else {
log::debug!("No ublk devices remaining");
}
}
Err(e) => {
let error = format!("Failed to check ublk devices: {}", e);
log::debug!("ERROR: {}", error);
errors.push(error);
}
}
// Check /run/ublksrvd/ directory
match check_ublksrvd_dir() {
Ok(files) => {
if !files.is_empty() {
let error = format!("Found files in /run/ublksrvd/ after test: {:?}", files);
log::debug!("ERROR: {}", error);
errors.push(error);
// Attempt to clean up /run/ublksrvd/
log::debug!(
"Attempting to cleantest_mixed_pattern_verification up /run/ublksrvd/..."
);
match fs::remove_dir_all("/run/ublksrvd") {
Ok(()) => {
log::debug!("Successfully cleaned up /run/ublksrvd/");
// Recreate the directory if it's expected to exist
if let Err(e) = fs::create_dir_all("/run/ublksrvd") {
log::debug!("Warning: Failed to recreate /run/ublksrvd/: {}", e);
}
}
Err(e) => {
let cleanup_error = format!("Failed to clean up /run/ublksrvd/: {}", e);
log::debug!("ERROR: {}", cleanup_error);
errors.push(cleanup_error);
}
}
} else {
log::debug!("/run/ublksrvd/ is empty");
}
}
Err(e) => {
let error = format!("Failed to check /run/ublksrvd/: {}", e);
log::debug!("ERROR: {}", error);
errors.push(error);
}
}
log::debug!("=== Teardown Complete ===\n");
if !errors.is_empty() {
Err(format!(
"Teardown found {} issue(s):\n{}",
errors.len(),
errors.join("\n")
))
} else {
Ok(())
}
}
use env_logger::{Builder, Env};
use std::io::Write;
static INIT: std::sync::Once = std::sync::Once::new();
pub fn init_logging() {
INIT.call_once(|| {
// Set RUST_LOG to whatever you want
let env = Env::default().default_filter_or("error");
Builder::from_env(env)
.format(|buf, record| {
let ts = buf.timestamp_millis();
let tid = get_tid();
writeln!(
buf,
"{} [tid:{}] {} - {}",
ts,
tid,
record.level(),
record.args()
)
})
//.filter_level(LevelFilter::Debug)
.init();
});
}
#[inline]
fn get_tid() -> libc::pid_t {
unsafe { libc::syscall(libc::SYS_gettid) as libc::pid_t }
}
pub fn xxd_dump(data: &[u8]) {
use std::io::{self, Write};
const BYTES_PER_LINE: usize = 16;
const GROUP_SIZE: usize = 2; // xxd groups 2 bytes at a time
let mut stdout = io::stdout().lock();
let mut offset = 0usize;
while offset < data.len() {
let remaining = data.len() - offset;
let line_len = remaining.min(BYTES_PER_LINE);
let line_bytes = &data[offset..offset + line_len];
// Offset
write!(stdout, "{:08x}:", offset).unwrap();
// Hex bytes in groups like " 4865 6c6c ..."
let groups_per_line = BYTES_PER_LINE / GROUP_SIZE;
for g in 0..groups_per_line {
let base = g * GROUP_SIZE;
// Always prefix group with a space
write!(stdout, " ").unwrap();
if base >= line_len {
// No bytes left in this group: pad " "
write!(stdout, " ").unwrap();
continue;
}
// First byte
write!(stdout, "{:02x}", line_bytes[base]).unwrap();
// Second byte or padding
if base + 1 < line_len {
write!(stdout, "{:02x}", line_bytes[base + 1]).unwrap();
} else {
write!(stdout, " ").unwrap();
}
}
// Spacer
write!(stdout, " ").unwrap();
// ASCII
for &b in line_bytes {
let ch = if (0x20..=0x7e).contains(&b) {
b as char
} else {
'.'
};
write!(stdout, "{}", ch).unwrap();
}
writeln!(stdout).unwrap();
offset += line_len;
}
}
}