diff --git a/crates/lox-distributor/Cargo.toml b/crates/lox-distributor/Cargo.toml index c321e28..43a6623 100644 --- a/crates/lox-distributor/Cargo.toml +++ b/crates/lox-distributor/Cargo.toml @@ -22,4 +22,5 @@ serde_with = "3.0.0" serde_json = "1.0.87" lox = { path = "../lox-library", version = "0.1.0"} +lox_utils = { path = "../lox-utils", version = "0.1.0"} rdsys_backend = { path = "../rdsys-backend-api", version = "0.1.0"} diff --git a/crates/lox-distributor/src/request_handler.rs b/crates/lox-distributor/src/request_handler.rs index 98682a4..1aedaf5 100644 --- a/crates/lox-distributor/src/request_handler.rs +++ b/crates/lox-distributor/src/request_handler.rs @@ -72,3 +72,109 @@ pub async fn handle( }, } } + +#[cfg(test)] +mod tests { + use super::*; + + use lox::{BridgeAuth, BridgeDb}; + use std::sync::{Arc, Mutex}; + + trait LoxClient { + fn invite(&self) -> Request
; + fn reachability(&self) -> Request; + fn pubkeys(&self) -> Request; + } + + struct LoxClientMock {} + + impl LoxClient for LoxClientMock { + fn invite(&self) -> Request { + let req = Request::builder() + .method("POST") + .uri("http://localhost/invite") + .body(Body::empty()) + .unwrap(); + req + } + fn reachability(&self) -> Request { + let req = Request::builder() + .method("POST") + .uri("http://localhost/reachability") + .body(Body::empty()) + .unwrap(); + req + } + + fn pubkeys(&self) -> Request { + let req = Request::builder() + .method("POST") + .uri("http://localhost/pubkeys") + .body(Body::empty()) + .unwrap(); + req + } + } + struct TestHarness { + context: LoxServerContext, + } + + impl TestHarness { + fn new() -> Self { + let mut bridgedb = BridgeDb::new(); + let mut lox_auth = BridgeAuth::new(bridgedb.pubkey); + + // Make 3 x num_buckets open invitation bridges, in sets of 3 + for _ in 0..5 { + let bucket = [ + lox_context::random(), + lox_context::random(), + lox_context::random(), + ]; + lox_auth.add_openinv_bridges(bucket, &mut bridgedb); + } + + // Add hot_spare more hot spare buckets + for _ in 0..5 { + let bucket = [ + lox_context::random(), + lox_context::random(), + lox_context::random(), + ]; + lox_auth.add_spare_bucket(bucket); + } + // Create the encrypted bridge table + lox_auth.enc_bridge_table(); + let context = lox_context::LoxServerContext { + db: Arc::new(Mutex::new(bridgedb)), + ba: Arc::new(Mutex::new(lox_auth)), + extra_bridges: Arc::new(Mutex::new(Vec::new())), + unreplaced_bridges: Arc::new(Mutex::new(Vec::new())), + }; + Self { context } + } + } + + #[tokio::test] + async fn test_handle() { + let th = TestHarness::new(); + let lc = LoxClientMock {}; + // Test Invite + let invite_request = lc.invite(); + let response = handle(th.context.clone(), invite_request).await.unwrap(); + println!("Server response?: {:?}", response); + assert_eq!(response.status(), StatusCode::OK); + // Test Reachability + let reachability_request = lc.reachability(); + let reachability_response = handle(th.context.clone(), reachability_request) + .await + .unwrap(); + println!("Server response?: {:?}", reachability_response); + assert_eq!(reachability_response.status(), StatusCode::OK); + // Test Pubkeys + let pubkey_request = lc.pubkeys(); + let pubkey_response = handle(th.context.clone(), pubkey_request).await.unwrap(); + println!("Server response?: {:?}", pubkey_response); + assert_eq!(pubkey_response.status(), StatusCode::OK); + } +} diff --git a/crates/lox-utils/Cargo.toml b/crates/lox-utils/Cargo.toml index 7877b08..14a17cb 100644 --- a/crates/lox-utils/Cargo.toml +++ b/crates/lox-utils/Cargo.toml @@ -1,5 +1,5 @@ [package] -name = "lox-utils" +name = "lox_utils" version = "0.1.0" authors = ["The Tor Project, Inc.", "Lindsey Tulloch