Skip to content

Commit

Permalink
v2, tests: server_app sign_up test, part 2
Browse files Browse the repository at this point in the history
  • Loading branch information
cypherkitty committed Dec 24, 2023
1 parent 8d68e90 commit 6cb67ea
Show file tree
Hide file tree
Showing 9 changed files with 75 additions and 69 deletions.
24 changes: 12 additions & 12 deletions core/src/crypto/encoding.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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))
}
}

Expand Down Expand Up @@ -68,7 +71,8 @@ pub mod base64 {
type Error = CoreError;

fn try_from(base64: &Base64Text) -> Result<Self, Self::Error> {
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)
}
}
Expand Down Expand Up @@ -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);
}
}
Expand Down
8 changes: 4 additions & 4 deletions core/src/crypto/key_pair.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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(())
Expand Down
3 changes: 2 additions & 1 deletion core/src/crypto/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion core/src/node/common/model/device.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
2 changes: 1 addition & 1 deletion core/src/node/db/objects/global_index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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?;
Expand Down
52 changes: 2 additions & 50 deletions core/src/node/server/server_app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -85,7 +85,7 @@ impl<Repo: KvLogEventRepo> ServerApp<Repo> {
Ok(())
}

async fn handle_sync_request(&self, request: SyncRequest) -> Vec<GenericKvLogEvent> {
pub async fn handle_sync_request(&self, request: SyncRequest) -> Vec<GenericKvLogEvent> {
let new_events_result = self.data_sync
.replication(request)
.await;
Expand All @@ -102,51 +102,3 @@ impl<Repo: KvLogEventRepo> ServerApp<Repo> {
}
}
}

#[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);
}
}
}
}
4 changes: 4 additions & 0 deletions core/tests/common.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@

pub fn qwe() {
println!("qwe");
}
Empty file removed core/tests/common/mod.rs
Empty file.
49 changes: 49 additions & 0 deletions core/tests/server_test.rs
Original file line number Diff line number Diff line change
@@ -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(())
}
}

0 comments on commit 6cb67ea

Please sign in to comment.