Add ctl gateway tls file generation tests
@@ -27,13 +27,13 @@ const KEY_EXTENSION: &str = "key.pem";
#[derive(Error, Debug)]
pub enum Error {
- #[error("ctl gateway certificate lookup failed when trying to match files {0}")]
+ #[error("ctl gateway TLS file lookup failed when trying to match files {0}")]
FailedToMatchPattern(String),
- #[error("ctl gateway certificate lookup failed, err: {0}")]
+ #[error("ctl gateway TLS file lookup failed, err: {0}")]
RustlsReaders(#[from] RustlsReadersError),
- #[error("ctl gateway certificate generation failed, err: {0}")]
+ #[error("ctl gateway TLS file generation failed, err: {0}")]
CertificateGeneration(#[from] RcgenError),
- #[error("writing the ctl gateway certificate failed, err: {0}")]
+ #[error("writing the ctl gateway TLS files failed, err: {0}")]
CertificateWrite(#[from] IoError),
}
@@ -91,3 +91,42 @@ pub fn latest_root_certificate_store(path: impl AsRef<Path>) -> Result<RootCertS
let path = get_latest_path(path, &format!("{}-*.{}", NAME_PREFIX, CRT_EXTENSION))?;
Ok(rustls_wrapper::root_certificate_store_from_file(&path)?)
}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+ use std::{fs,
+ time::Duration};
+ use tempfile::TempDir;
+
+ #[test]
+ fn ctl_gateway_generate_and_read_tls_files() {
+ let tmpdir = TempDir::new().unwrap();
+
+ generate_self_signed_certificate_and_key("a_test_domain", &tmpdir).unwrap();
+ assert_eq!(fs::read_dir(&tmpdir).unwrap().count(), 2);
+ let first_path =
+ get_latest_path(&tmpdir, &format!("{}-*.{}", NAME_PREFIX, CRT_EXTENSION)).unwrap();
+ let certificates = latest_certificates(&tmpdir).unwrap();
+ assert_eq!(certificates.len(), 1);
+ latest_private_key(&tmpdir).unwrap();
+ let root_certificate_store = latest_root_certificate_store(&tmpdir).unwrap();
+ assert_eq!(root_certificate_store.roots.len(), 1);
+
+ // TLS files are named on second boundaries. Wait enough time to guarantee we get a new
+ // name.
+ std::thread::sleep(Duration::from_secs(2));
+
+ generate_self_signed_certificate_and_key("another_domain", &tmpdir).unwrap();
+ assert_eq!(fs::read_dir(&tmpdir).unwrap().count(), 4);
+ let second_path =
+ get_latest_path(&tmpdir, &format!("{}-*.{}", NAME_PREFIX, CRT_EXTENSION)).unwrap();
+ let certificates = latest_certificates(&tmpdir).unwrap();
+ assert_eq!(certificates.len(), 1);
+ latest_private_key(&tmpdir).unwrap();
+ let root_certificate_store = latest_root_certificate_store(&tmpdir).unwrap();
+ assert_eq!(root_certificate_store.roots.len(), 1);
+
+ assert!(second_path > first_path);
+ }
+}
