From 6cb67ea481baac6c8d723e733baf47eb2bec83e9 Mon Sep 17 00:00:00 2001 From: metabynull Date: Sun, 24 Dec 2023 10:40:03 -0800 Subject: [PATCH] v2, tests: server_app sign_up test, part 2 --- core/src/crypto/encoding.rs | 24 +++++------ core/src/crypto/key_pair.rs | 8 ++-- core/src/crypto/utils.rs | 3 +- core/src/node/common/model/device.rs | 2 +- core/src/node/db/objects/global_index.rs | 2 +- core/src/node/server/server_app.rs | 52 +----------------------- core/tests/common.rs | 4 ++ core/tests/common/mod.rs | 0 core/tests/server_test.rs | 49 ++++++++++++++++++++++ 9 files changed, 75 insertions(+), 69 deletions(-) create mode 100644 core/tests/common.rs delete mode 100644 core/tests/common/mod.rs create mode 100644 core/tests/server_test.rs diff --git a/core/src/crypto/encoding.rs b/core/src/crypto/encoding.rs index 8a0abf24..383412a4 100644 --- a/core/src/crypto/encoding.rs +++ b/core/src/crypto/encoding.rs @@ -8,8 +8,13 @@ pub mod base64 { #[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] #[serde(rename_all = "camelCase")] - pub struct Base64Text { - pub base64_text: String, + pub struct Base64Text(pub String); + + impl From<&Base64Text> for String { + fn from(base64: &Base64Text) -> Self { + let Base64Text(base64_text) = base64; + base64_text.clone() + } } pub mod encoder { @@ -29,9 +34,7 @@ pub mod base64 { impl From<&[u8]> for Base64Text { fn from(data: &[u8]) -> Self { - Self { - base64_text: base64::encode_engine(data, &URL_SAFE_ENGINE), - } + Self(base64::encode_engine(data, &URL_SAFE_ENGINE)) } } @@ -68,7 +71,8 @@ pub mod base64 { type Error = CoreError; fn try_from(base64: &Base64Text) -> Result { - let data = base64::decode_engine(&base64.base64_text, &URL_SAFE_ENGINE)?; + let Base64Text(base64_text) = base64; + let data = base64::decode_engine(base64_text, &URL_SAFE_ENGINE)?; Ok(data) } } @@ -96,18 +100,14 @@ pub mod base64 { #[test] fn from_vec() { let encoded = Base64Text::from(vec![65, 65, 65]); - let expected = Base64Text { - base64_text: "QUFB".to_string(), - }; + let expected = Base64Text("QUFB".to_string()); assert_eq!(encoded, expected); } #[test] fn from_bytes() { let encoded = Base64Text::from(TEST_STR.as_bytes()); - let expected = Base64Text { - base64_text: ENCODED_URL_SAFE_TEST_STR.to_string(), - }; + let expected = Base64Text(ENCODED_URL_SAFE_TEST_STR.to_string()); assert_eq!(encoded, expected); } } diff --git a/core/src/crypto/key_pair.rs b/core/src/crypto/key_pair.rs index 9dc78fe7..b8ec7710 100644 --- a/core/src/crypto/key_pair.rs +++ b/core/src/crypto/key_pair.rs @@ -142,10 +142,10 @@ impl TransportDsaKeyPair { let owner_pk = self.public_key(); let their_pk = match owner_pk { - pk if pk.base64_text == channel.sender.base64_text => { + pk if pk == channel.sender => { CryptoBoxPublicKey::try_from(&channel.receiver) } - pk if pk.base64_text == channel.receiver.base64_text => { + pk if pk == channel.receiver => { CryptoBoxPublicKey::try_from(&channel.sender) } _ => Err(CoreError::ThirdPartyEncryptionError { @@ -198,8 +198,8 @@ pub mod test { let plain_text = alice_km.transport_key_pair.decrypt(&cipher_text)?; assert_eq!( - Base64Text::from(password.as_bytes()).base64_text, - plain_text.msg.base64_text + Base64Text::from(password.as_bytes()), + plain_text.msg ); Ok(()) diff --git a/core/src/crypto/utils.rs b/core/src/crypto/utils.rs index 0120b578..425a51a0 100644 --- a/core/src/crypto/utils.rs +++ b/core/src/crypto/utils.rs @@ -35,7 +35,8 @@ pub fn rand_64bit_b64_url_enc() -> Base64Text { pub fn generate_uuid_b64_url_enc(value: String) -> String { let hash = Sha256::digest(value.as_bytes()); let uuid = Uuid::from_slice(&hash.as_slice()[..16]).unwrap(); - Base64Text::from(uuid.as_bytes().as_slice()).base64_text + let Base64Text(base64_text) = Base64Text::from(uuid.as_bytes().as_slice()); + base64_text } pub trait NextId { diff --git a/core/src/node/common/model/device.rs b/core/src/node/common/model/device.rs index 35067df4..9761a7d5 100644 --- a/core/src/node/common/model/device.rs +++ b/core/src/node/common/model/device.rs @@ -137,7 +137,7 @@ impl DeviceData { impl From<&OpenBox> for DeviceId { fn from(open_box: &OpenBox) -> Self { - let dsa_pk = open_box.dsa_pk.base64_text.clone(); + let dsa_pk = String::from(&open_box.dsa_pk); let id = generate_uuid_b64_url_enc(dsa_pk); Self(id) } diff --git a/core/src/node/db/objects/global_index.rs b/core/src/node/db/objects/global_index.rs index daeaa4ce..ab1f57dc 100644 --- a/core/src/node/db/objects/global_index.rs +++ b/core/src/node/db/objects/global_index.rs @@ -82,7 +82,7 @@ mod test { let p_global_index = { let p_obj = Arc::new(PersistentObject::new(repo.clone())); - PersistentGlobalIndex { p_obj, server_device } + PersistentGlobalIndex { p_obj, server_device: server_device.clone() } }; p_global_index.init().await?; diff --git a/core/src/node/server/server_app.rs b/core/src/node/server/server_app.rs index 46e06d42..59e3aa22 100644 --- a/core/src/node/server/server_app.rs +++ b/core/src/node/server/server_app.rs @@ -3,7 +3,7 @@ use std::sync::Arc; use tracing::{error, info, instrument}; use crate::node::common::data_transfer::MpscDataTransfer; -use crate::node::common::model::device::{DeviceCredentials, DeviceName}; +use crate::node::common::model::device::DeviceName; use crate::node::db::events::generic_log_event::GenericKvLogEvent; use crate::node::db::objects::global_index::PersistentGlobalIndex; use crate::node::db::objects::persistent_object::PersistentObject; @@ -85,7 +85,7 @@ impl ServerApp { Ok(()) } - async fn handle_sync_request(&self, request: SyncRequest) -> Vec { + pub async fn handle_sync_request(&self, request: SyncRequest) -> Vec { let new_events_result = self.data_sync .replication(request) .await; @@ -102,51 +102,3 @@ impl ServerApp { } } } - -#[cfg(test)] -mod test { - - #[tokio::test] - pub async fn test_server_app() { - use std::sync::Arc; - use crate::crypto::keys::{KeyManager, OpenBox}; - use crate::node::common::model::device::{DeviceData, DeviceName}; - use crate::node::db::descriptors::global_index::GlobalIndexDescriptor; - use crate::node::db::descriptors::object_descriptor::ToObjectDescriptor; - use crate::node::db::events::generic_log_event::ObjIdExtractor; - use crate::node::db::events::global_index::GlobalIndexObject; - use crate::node::db::events::object_id::ObjectId; - use crate::node::db::in_mem_db::InMemKvLogEventRepo; - use crate::node::db::objects::global_index::PersistentGlobalIndex; - use crate::node::db::objects::persistent_object::PersistentObject; - - use super::*; - - let repo = Arc::new(InMemKvLogEventRepo::default()); - - let server_app = ServerApp::init(repo.clone()).await.unwrap(); - - let server_dt = Arc::new(ServerDataTransfer { - dt: MpscDataTransfer::new(), - }); - - let server_app = server_app.run(server_dt.clone()).await; - - assert!(server_app.is_ok()); - - let sync_request = SyncRequest { - device: DeviceData::from(DeviceName::from("client")), - last_synced_event_id: 0, - }; - - server_dt.dt.send_to_server(DataSyncRequest::SyncRequest(sync_request)).await; - - let sync_response = server_dt.dt.service_receive().await.unwrap(); - - match sync_response { - DataSyncResponse { events } => { - assert_eq!(events.len(), 2); - } - } - } -} diff --git a/core/tests/common.rs b/core/tests/common.rs new file mode 100644 index 00000000..89e208d2 --- /dev/null +++ b/core/tests/common.rs @@ -0,0 +1,4 @@ + +pub fn qwe() { + println!("qwe"); +} \ No newline at end of file diff --git a/core/tests/common/mod.rs b/core/tests/common/mod.rs deleted file mode 100644 index e69de29b..00000000 diff --git a/core/tests/server_test.rs b/core/tests/server_test.rs new file mode 100644 index 00000000..e84288c5 --- /dev/null +++ b/core/tests/server_test.rs @@ -0,0 +1,49 @@ +mod common; + +#[cfg(test)] +mod test { + use meta_secret_core::node::{ + server::{ + request::{GlobalIndexRequest, SyncRequest}, + server_app::ServerApp + }, + db::{ + descriptors::global_index::GlobalIndexDescriptor, + descriptors::object_descriptor::ToObjectDescriptor, + events::object_id::ObjectId, + in_mem_db::InMemKvLogEventRepo + } + }; + use std::sync::Arc; + use meta_secret_core::crypto::keys::{KeyManager, OpenBox}; + use meta_secret_core::node::common::model::device::{DeviceData, DeviceName}; + + + #[tokio::test] + pub async fn test_server_app() -> anyhow::Result<()> { + let repo = Arc::new(InMemKvLogEventRepo::default()); + + let server_app = ServerApp::init(repo.clone()).await?; + + let client_device = { + let secret_box = KeyManager::generate_secret_box(); + let open_box = OpenBox::from(&secret_box); + DeviceData::from(DeviceName::from("test_device"), open_box) + }; + + let sync_request = SyncRequest::GlobalIndex(GlobalIndexRequest { + sender: client_device, + global_index: ObjectId::unit(GlobalIndexDescriptor::Index.to_obj_desc()) + }); + + let events = server_app + .handle_sync_request(sync_request) + .await; + + for event in events { + println!("Event: {}", serde_json::to_string(&event).unwrap()); + } + + Ok(()) + } +}