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;

View File

@ -7,6 +7,7 @@ use std::sync::Arc;
use futures::future::join_all;
use reqwest::{Certificate, Identity};
use crate::base::err_utils::new_err;
use crate::base::RemoteConfig;
use crate::tcp_relay_client::client_config::ClientConfig;
use crate::tcp_relay_client::relay_client::relay_client;
@ -54,15 +55,14 @@ async fn get_server_config(conf: &ClientConfig) -> Result<RemoteConfig, Box<dyn
/// Core logic of the application
pub async fn run_app(mut args: ClientConfig) -> std::io::Result<()> {
args.load_certificates();
args.load_certificates()?;
let args = Arc::new(args);
// Check arguments coherence
if args.tls_cert.is_some() != args.tls_key.is_some() {
log::error!(
"If you specify one of TLS certificate / key, you must then specify the other!"
);
panic!();
return Err(new_err(
"If you specify one of TLS certificate / key, you must then specify the other!",
));
}
if args.get_client_keypair().is_some() {