Skip to content

Commit 35136e1

Browse files
committed
tests: more tests and debug
1 parent ded4854 commit 35136e1

File tree

3 files changed

+32
-28
lines changed

3 files changed

+32
-28
lines changed

rar-common/src/util.rs

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,24 @@ pub fn with_mutable_config<F, R>(file: &mut File, f: F) -> std::io::Result<R>
9494
where
9595
F: FnOnce(&mut File) -> io::Result<R>,
9696
{
97+
let mut val = unlock_immutable(file)?;
98+
let res = f(file);
99+
val |= FS_IMMUTABLE_FL;
100+
lock_immutable(file, val)?;
101+
res
102+
}
103+
104+
pub fn lock_immutable(file: &mut File, mut val: u32) -> Result<(), io::Error> {
105+
immutable_required_privileges(file, || {
106+
if unsafe { nix::libc::ioctl(file.as_raw_fd(), FS_IOC_SETFLAGS, &mut val) } < 0 {
107+
return Err(std::io::Error::last_os_error());
108+
}
109+
Ok(())
110+
})?;
111+
Ok(())
112+
}
113+
114+
pub fn unlock_immutable(file: &mut File) -> Result<u32, io::Error> {
97115
let mut val = 0;
98116
if unsafe { nix::libc::ioctl(file.as_raw_fd(), FS_IOC_GETFLAGS, &mut val) } < 0 {
99117
return Err(std::io::Error::last_os_error());
@@ -109,15 +127,7 @@ where
109127
} else {
110128
warn!("Config file was not immutable.");
111129
}
112-
let res = f(file);
113-
val |= FS_IMMUTABLE_FL;
114-
immutable_required_privileges(file, || {
115-
if unsafe { nix::libc::ioctl(file.as_raw_fd(), FS_IOC_SETFLAGS, &mut val) } < 0 {
116-
return Err(std::io::Error::last_os_error());
117-
}
118-
Ok(())
119-
})?;
120-
res
130+
Ok(val)
121131
}
122132

123133
pub fn warn_if_mutable(file: &File, return_err: bool) -> std::io::Result<()> {
@@ -336,7 +346,7 @@ pub fn open_lock_with_privileges<P: AsRef<Path>>(
336346
if e.kind() != std::io::ErrorKind::PermissionDenied {
337347
return Err(e);
338348
}
339-
debug!("Permission denied while opening file, retrying with privileges",);
349+
debug!("Permission denied while opening {} file, retrying with privileges", p.as_ref().display());
340350
with_privileges(&[Cap::DAC_READ_SEARCH], || options.open(&p)).or_else(|e| {
341351
if e.kind() != std::io::ErrorKind::PermissionDenied {
342352
return Err(e);
@@ -353,7 +363,7 @@ pub fn read_with_privileges<P: AsRef<Path>>(p: P) -> std::io::Result<File> {
353363
if e.kind() != std::io::ErrorKind::PermissionDenied {
354364
return Err(e);
355365
}
356-
debug!("Permission denied while opening file, retrying with privileges",);
366+
debug!("Permission denied while opening {} file, retrying with privileges",p.as_ref().display());
357367
with_privileges(&[Cap::DAC_READ_SEARCH], || std::fs::File::open(&p)).or_else(|e| {
358368
if e.kind() != std::io::ErrorKind::PermissionDenied {
359369
return Err(e);
@@ -368,7 +378,7 @@ pub fn remove_with_privileges<P: AsRef<Path>>(p: P) -> std::io::Result<()> {
368378
if e.kind() != std::io::ErrorKind::PermissionDenied {
369379
return Err(e);
370380
}
371-
debug!("Permission denied while removing file, retrying with privileges",);
381+
debug!("Permission denied while removing {} file, retrying with privileges",p.as_ref().display());
372382
with_privileges(&[Cap::DAC_OVERRIDE], || std::fs::remove_file(&p))
373383
})
374384
}
@@ -378,7 +388,7 @@ pub fn create_dir_all_with_privileges<P: AsRef<Path>>(p: P) -> std::io::Result<(
378388
if e.kind() != std::io::ErrorKind::PermissionDenied {
379389
return Err(e);
380390
}
381-
debug!("Permission denied while creating directory, retrying with privileges",);
391+
debug!("Permission denied while creating {} directory, retrying with privileges",p.as_ref().display());
382392
with_privileges(&[Cap::DAC_OVERRIDE], || std::fs::create_dir_all(p))
383393
})
384394
}

src/sr/pam/mod.rs

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -267,23 +267,21 @@ mod tests {
267267

268268
#[test]
269269
fn test_check_auth_required_but_valid_timeout() {
270+
if env!("RAR_PAM_SERVICE") == "dosr" {
271+
println!("Skipping test_check_auth_required_but_valid_timeout because RAR_PAM_SERVICE is set to original dosr");
272+
return;
273+
}
270274
let authentication = SAuthentication::Perform;
271275
let timeout = create_test_timeout();
272276
let user = create_test_user();
273277

274-
// This test depends on the timeout::is_valid implementation
275-
// In a real environment, you might want to mock this
276-
let result = check_auth(&authentication, &timeout, &user, "Password: ");
277-
// Result will depend on whether there's a valid timeout cookie
278-
// We're just testing that it doesn't panic
279-
assert!(result.is_ok() || result.is_err());
278+
let _ = check_auth(&authentication, &timeout, &user, "Password: ");
280279
}
281280

282281
#[test]
283282
fn test_conversation_handler_no_interact_flag() {
284283
let handler = SrConversationHandler::builder().no_interact().build();
285284

286-
// When no_interact is true, both prompt methods should return ConversationError
287285
let prompt_result = handler.prompt(OsStr::new("Test prompt"));
288286
assert!(matches!(prompt_result, Err(ErrorCode::ConversationError)));
289287

@@ -299,10 +297,8 @@ mod tests {
299297
let custom_prompt = "Enter your secret: ";
300298
let handler = SrConversationHandler::new(custom_prompt);
301299

302-
// Test that the handler stores the custom prompt
303300
assert_eq!(handler.prompt, custom_prompt);
304301

305-
// Test that it recognizes standard PAM prompts
306302
assert!(handler.is_pam_password_prompt(&"Password:"));
307303
assert!(handler.is_pam_password_prompt(&"Password: "));
308304
}

tests/integration_tests.rs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -275,10 +275,8 @@ mod tests {
275275
#[test]
276276
#[serial]
277277
fn test_dosr_auth() {
278-
// check that the /etc/pam.d/dosr_test file exists
279-
use std::fs;
280-
if fs::metadata("/etc/pam.d/dosr_test").is_err() {
281-
eprintln!("Skipping test_dosr_auth: /etc/pam.d/dosr_test not found");
278+
if env!("RAR_PAM_SERVICE") == "dosr" {
279+
println!("Skipping test_dosr_auth because RAR_PAM_SERVICE is set to original dosr");
282280
return;
283281
}
284282
let runner = get_test_runner().expect("Failed to setup test environment");
@@ -294,11 +292,11 @@ mod tests {
294292
);
295293
assert_eq!(result.exit_code, 0);
296294
// assert that a timestamp cookie was created
297-
let path = std::path::Path::new("/var/run/sr/ts").join("0");
295+
let path = std::path::Path::new("/var/run/rar/ts").join("0");
298296
assert!(path.exists(), "Timestamp cookie was not created");
299297
// run dosr -K to delete the timestamp cookie
300298
let result = runner
301-
.run_dosr(&["-K", "/usr/bin/true"])
299+
.run_dosr(&["-K"])
302300
.fixture_name("tests/fixtures/perform_auth.json")
303301
.call()
304302
.expect("Failed to run dosr with auth role");

0 commit comments

Comments
 (0)