Changeset View
Changeset View
Standalone View
Standalone View
services/commtest/tests/tunnelbroker_integration_test.rs
use commtest::tunnelbroker::{ | use futures_util::SinkExt; | ||||
new_session::create_new_session, | use tokio_tungstenite::{connect_async, tungstenite::Message}; | ||||
new_session::get_string_to_sign, | |||||
new_session::sign_string_with_private_key, | |||||
tunnelbroker_utils::{ | |||||
proto::new_session_request::DeviceTypes, | |||||
session_signature_device_id_format_validation, tonic_client_builder, | |||||
}, | |||||
}; | |||||
use openssl::pkey::PKey; | |||||
use openssl::rsa::Rsa; | |||||
#[tokio::test] | #[tokio::test] | ||||
async fn tunnelbroker_integration_test() -> Result<(), anyhow::Error> { | async fn open_websocket_connection() { | ||||
bartek: I know this is a "hello world" test, but just two considerations for future design:
- Is this… | |||||
jonAuthorUnsubmitted Done Inline Actions
It is meant to be a test. It's easier for me to iterate with rust than JS. So I've been implementing it first in rust to just to ensure that the rust is working before trying to debug stuff in JS.
The test is meant to a be a happy case, if it panics or returns an Err it will still fail the test. I don't feel super strongly about this, and might make the transition in the future to use anyhow. Still not sure if anyhow or retaining the original error is more beneficial jon: > Is this function intended to be a test? Or a utility to open a connection?
It is meant to be… | |||||
const DEVICE_ID: &str = | let (mut socket, _) = connect_async("ws://localhost:51001") | ||||
"mobile:OOOTESTb2ueEmQ4QsevRWlXxFCNt055y20T1PHdoYAQRt0S6TLzZWNM6XSvdW000"; | |||||
// Should fail on the wrong device ID format provided | |||||
let mut client = tonic_client_builder().await?; | |||||
assert!( | |||||
session_signature_device_id_format_validation(&mut client) | |||||
.await | .await | ||||
.is_ok(), | .expect("Can't connect"); | ||||
"DeviceID format validation failed on getting session signature request" | |||||
); | |||||
// Generate a keypair | |||||
let keypair = Rsa::generate(1024)?; | |||||
let keypair = PKey::from_rsa(keypair)?; | |||||
let public_key_pem = String::from_utf8(keypair.public_key_to_pem()?)?; | |||||
// Get and sign the string to be signed | let session_request = r#"{ | ||||
let string_to_be_signed = get_string_to_sign(&mut client, DEVICE_ID).await?; | "type": "sessionRequest", | ||||
let signature_base64 = | "accessToken": "xkdeifjsld", | ||||
sign_string_with_private_key(&keypair, &string_to_be_signed)?; | "deviceId": "foo", | ||||
"deviceType": "keyserver" | |||||
}"#; | |||||
// Should fail on the wrong signature provided | socket | ||||
assert!( | .send(Message::Text(session_request.to_string())) | ||||
create_new_session( | |||||
&mut client, | |||||
DEVICE_ID, | |||||
&public_key_pem, | |||||
"wrong_signature", | |||||
"fake_notify_token", | |||||
DeviceTypes::Mobile, | |||||
"v.x.x.x", | |||||
"iOS x.x.x", | |||||
) | |||||
.await | .await | ||||
.is_err(), | .expect("Failed to send message"); | ||||
"New session returns success when using wrong signature" | |||||
); | |||||
// Create a new session with the correct signature | |||||
let _session_id = create_new_session( | |||||
&mut client, | |||||
DEVICE_ID, | |||||
&public_key_pem, | |||||
&signature_base64, | |||||
"fake_notify_token", | |||||
DeviceTypes::Mobile, | |||||
"v.x.x.x", | |||||
"iOS x.x.x", | |||||
) | |||||
.await?; | |||||
Ok(()) | |||||
} | } |
I know this is a "hello world" test, but just two considerations for future design: