Add new test for client configuration

This commit is contained in:
2022-09-02 10:42:22 +02:00
parent 6f8055f1c7
commit 019ae92605
6 changed files with 66 additions and 21 deletions

View File

@@ -1,3 +1,4 @@
use crate::base::err_utils::encpasulate_error;
use bytes::BufMut;
use clap::Parser;
@@ -45,21 +46,13 @@ pub struct KeysCache {
impl ClientConfig {
/// Load certificates and put them in cache
pub fn load_certificates(&mut self) {
pub fn load_certificates(&mut self) -> std::io::Result<()> {
self._keys_cache = KeysCache {
_root_certificate_cache: self
.root_certificate
.as_ref()
.map(|c| std::fs::read(c).expect("Failed to read root certificate!")),
_tls_cert_cache: self
.tls_cert
.as_ref()
.map(|c| std::fs::read(c).expect("Failed to read client certificate!")),
_tls_key_cache: self
.tls_key
.as_ref()
.map(|c| std::fs::read(c).expect("Failed to read client key!")),
_root_certificate_cache: load_pem_file(&self.root_certificate, "root certificate")?,
_tls_cert_cache: load_pem_file(&self.tls_cert, "client certificate")?,
_tls_key_cache: load_pem_file(&self.tls_cert, "client key")?,
};
Ok(())
}
/// Get client token, returning a dummy token if none was specified
@@ -95,6 +88,16 @@ impl ClientConfig {
}
}
fn load_pem_file(path: &Option<String>, name: &str) -> std::io::Result<Option<Vec<u8>>> {
Ok(match path {
None => None,
Some(p) => Some(
std::fs::read(p)
.map_err(|e| encpasulate_error(e, format!("Failed to load {}!", name)))?,
),
})
}
#[cfg(test)]
mod test {
use crate::tcp_relay_client::client_config::ClientConfig;