comparison testharness/tests/end2end.rs @ 104:a2676475e86b default tip

Create the very start of a test suite. - Creates a new testharness package - Sets up the outlines of a test suite that will execute there - A basic container where maybe those tests can execute
author Paul Fisher <paul@pfish.zone>
date Wed, 25 Jun 2025 16:56:56 -0400
parents
children
comparison
equal deleted inserted replaced
103:dfcd96a74ac4 104:a2676475e86b
1 use std::any::Any;
2 use std::convert::Infallible;
3 use std::error::Error;
4 use std::io;
5 use std::panic::UnwindSafe;
6 use std::path::{Path, PathBuf};
7 use std::{fs, panic};
8
9 const PAM_CONFIG: &str = "\
10 auth required pam_testharness.so
11 account required pam_testharness.so
12 password required pam_testharness.so
13 session required pam_testharness.so
14 ";
15 const PAM_CONFIG_PATH: &str = "/etc/pam.d/testharness";
16 const PAM_MODULE_PATH: &str = "/lib/security/pam_testharness.so";
17
18 #[derive(Debug, thiserror::Error)]
19 enum TestError {
20 #[error("error in test harness: {0}")]
21 HarnessError(#[from] io::Error),
22 #[error("panic in test: {0:?}")]
23 Panic(Box<dyn Any + Send>),
24 #[error(transparent)]
25 TestError(anyhow::Error),
26 }
27
28 #[test]
29 fn test_auth_only() -> Result<(), TestError> {
30 harness(|| Ok::<(), Infallible>(()))
31 }
32
33 fn harness<E: Error + Send + Sync + 'static>(
34 test: impl Fn() -> Result<(), E> + UnwindSafe,
35 ) -> Result<(), TestError> {
36 let dylib_path = test_cdylib::build_current_project();
37 let module_path = Path::new(PAM_MODULE_PATH);
38 let config_path = Path::new(PAM_CONFIG_PATH);
39 let parent = module_path.parent().unwrap();
40 fs::create_dir_all(parent)?;
41 fs::copy(dylib_path, module_path)?;
42 fs::write(config_path, PAM_CONFIG)?;
43 panic::catch_unwind(test)
44 .map_err(TestError::Panic)?
45 .map_err(|e| TestError::TestError(e.into()))?;
46 fs::remove_file(module_path)?;
47 fs::remove_file(config_path)?;
48 // If the /lib/security directory can't be removed, that's OK.
49 let _ = fs::remove_dir(parent);
50 Ok(())
51 }