Skip to content

Commit

Permalink
Avoid recreating certs in each test
Browse files Browse the repository at this point in the history
  • Loading branch information
einarmo committed Sep 11, 2024
1 parent 16b2e9d commit 4d7ae95
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 5 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@ log/
**/pki-client
**/pki-server
3rd-party/open62541/build/
lib/pki*
lib/pki*
lib/certs
7 changes: 5 additions & 2 deletions lib/tests/integration/core_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use log::debug;
use opcua::{
client::IdentityToken,
core::comms::tcp_codec::{Message, TcpCodec},
core::config::Config,
crypto::SecurityPolicy,
types::{
ApplicationType, DecodingOptions, MessageSecurityMode, NodeId, ReadValueId, StatusCode,
Expand All @@ -19,8 +20,8 @@ use tokio::{
use tokio_util::codec::Decoder;

use crate::utils::{
client_user_token, client_x509_token, default_server, test_server, Tester, CLIENT_USERPASS_ID,
TEST_COUNTER,
client_user_token, client_x509_token, copy_shared_certs, default_server, test_server, Tester,
CLIENT_USERPASS_ID, TEST_COUNTER,
};

#[tokio::test]
Expand All @@ -35,6 +36,8 @@ async fn hello_timeout() {
.discovery_urls(vec![format!("opc.tcp://{}:{}", hostname(), port)])
.pki_dir(format!("./pki-server/{test_id}"))
.hello_timeout(1);
copy_shared_certs(test_id, &server.config().application_description());

let (server, handle) = server.build().unwrap();
let addr = listener.local_addr().unwrap();

Expand Down
65 changes: 63 additions & 2 deletions lib/tests/utils/tester.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
use std::{
fs,
net::SocketAddr,
path::PathBuf,
path::{Path, PathBuf},
sync::{
atomic::{AtomicU16, Ordering},
Arc,
Arc, Mutex,
},
time::Duration,
};
Expand All @@ -14,6 +15,9 @@ use opcua::{
server::{ServerBuilder, ServerHandle, ServerUserToken, ANONYMOUS_USER_TOKEN_ID},
types::{MessageSecurityMode, StatusCode},
};
use opcua_core::config::Config;
use opcua_crypto::CertificateStore;
use opcua_types::ApplicationDescription;
use tokio::net::TcpListener;
use tokio_util::sync::{CancellationToken, DropGuard};

Expand Down Expand Up @@ -230,6 +234,56 @@ pub fn test_server() -> ServerBuilder {
default_server().with_node_manager(test_node_manager())
}

static SHARED_CERT_LOCK: Mutex<()> = Mutex::new(());

pub fn copy_shared_certs(test_id: u16, desc: &ApplicationDescription) {
let _lck = SHARED_CERT_LOCK.lock();
if !Path::new("certs").exists() {
std::fs::create_dir_all("certs/server").unwrap();
std::fs::create_dir_all("certs/client").unwrap();
CertificateStore::create_certificate_and_key(
&desc.clone().into(),
true,
&Path::new("certs/server/cert.der"),
&Path::new("certs/server/private.pem"),
)
.unwrap();
CertificateStore::create_certificate_and_key(
&desc.clone().into(),
true,
&Path::new("certs/client/cert.der"),
&Path::new("certs/client/private.pem"),
)
.unwrap();
}

std::fs::create_dir_all(&format!("pki-server/{test_id}/own")).unwrap();
std::fs::create_dir_all(&format!("pki-server/{test_id}/private")).unwrap();
std::fs::create_dir_all(&format!("pki-client/{test_id}/own")).unwrap();
std::fs::create_dir_all(&format!("pki-client/{test_id}/private")).unwrap();

fs::copy(
"certs/server/cert.der",
&format!("pki-server/{test_id}/own/cert.der"),
)
.unwrap();
fs::copy(
"certs/server/private.pem",
&format!("pki-server/{test_id}/private/private.pem"),
)
.unwrap();
fs::copy(
"certs/client/cert.der",
&format!("pki-client/{test_id}/own/cert.der"),
)
.unwrap();
fs::copy(
"certs/client/private.pem",
&format!("pki-client/{test_id}/private/private.pem"),
)
.unwrap();
}

impl Tester {
async fn listener() -> TcpListener {
TcpListener::bind(format!("{}:0", hostname()))
Expand All @@ -249,6 +303,8 @@ impl Tester {
.discovery_urls(vec![format!("opc.tcp://{}:{}", hostname(), addr.port())])
.pki_dir(format!("./pki-server/{test_id}"));

copy_shared_certs(test_id, &server.config().application_description());

let (server, handle) = server.build().unwrap();
let token = CancellationToken::new();

Expand Down Expand Up @@ -277,6 +333,8 @@ impl Tester {
.pki_dir(format!("./pki-server/{test_id}"))
.discovery_urls(vec![format!("opc.tcp://{}:{}", hostname(), addr.port())]);

copy_shared_certs(test_id, &server.config().application_description());

let (server, handle) = server.build().unwrap();

tokio::task::spawn(server.run_with(listener));
Expand Down Expand Up @@ -304,6 +362,9 @@ impl Tester {
let server = server
.pki_dir(format!("./pki-server/{test_id}"))
.discovery_urls(vec![format!("opc.tcp://{}:{}", hostname(), addr.port())]);

copy_shared_certs(test_id, &server.config().application_description());

let client = client.pki_dir(format!("./pki-client/{test_id}"));

let (server, handle) = server.build().unwrap();
Expand Down

0 comments on commit 4d7ae95

Please sign in to comment.