Add Test for lox-distributor

This commit is contained in:
onyinyang 2023-06-05 18:26:59 -04:00
parent a823dd9ee2
commit dc8774f5b8
No known key found for this signature in database
GPG Key ID: 156A6435430C2036
4 changed files with 121 additions and 14 deletions

View File

@ -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"}

View File

@ -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<Body>;
fn reachability(&self) -> Request<Body>;
fn pubkeys(&self) -> Request<Body>;
}
struct LoxClientMock {}
impl LoxClient for LoxClientMock {
fn invite(&self) -> Request<Body> {
let req = Request::builder()
.method("POST")
.uri("http://localhost/invite")
.body(Body::empty())
.unwrap();
req
}
fn reachability(&self) -> Request<Body> {
let req = Request::builder()
.method("POST")
.uri("http://localhost/reachability")
.body(Body::empty())
.unwrap();
req
}
fn pubkeys(&self) -> Request<Body> {
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);
}
}

View File

@ -1,5 +1,5 @@
[package]
name = "lox-utils"
name = "lox_utils"
version = "0.1.0"
authors = ["The Tor Project, Inc.", "Lindsey Tulloch <onyinyang@torproject.org"]
edition = "2021"

View File

@ -1,56 +1,56 @@
use lox::IssuerPubKey;
use lox::bridge_table::{BridgeLine, ENC_BUCKET_BYTES};
use lox::cred::{Invitation, Lox};
use lox::proto;
use lox::cred::{Lox, Invitation};
use lox::bridge_table::{ENC_BUCKET_BYTES, BridgeLine};
use lox::IssuerPubKey;
use serde::{Deserialize, Serialize};
use serde_with::{serde_as};
use serde_with::serde_as;
#[derive(Deserialize, Serialize)]
pub struct OpenReqState {
pub request: proto::open_invite::Request,
pub state:proto::open_invite::State,
pub state: proto::open_invite::State,
}
#[derive(Deserialize, Serialize)]
pub struct TrustReqState {
pub request: proto::trust_promotion::Request,
pub state:proto::trust_promotion::State,
pub state: proto::trust_promotion::State,
}
#[derive(Deserialize, Serialize)]
pub struct MigReqState {
pub request: proto::migration::Request,
pub state:proto::migration::State,
pub state: proto::migration::State,
}
#[derive(Deserialize, Serialize)]
pub struct LevelupReqState {
pub request: proto::level_up::Request,
pub state:proto::level_up::State,
pub state: proto::level_up::State,
}
#[derive(Deserialize, Serialize)]
pub struct IssueInviteReqState {
pub request: proto::issue_invite::Request,
pub state:proto::issue_invite::State,
pub state: proto::issue_invite::State,
}
#[derive(Deserialize, Serialize)]
pub struct RedeemReqState {
pub request: proto::redeem_invite::Request,
pub state:proto::redeem_invite::State,
pub state: proto::redeem_invite::State,
}
#[derive(Deserialize, Serialize)]
pub struct CheckBlockageReqState {
pub request: proto::check_blockage::Request,
pub state:proto::check_blockage::State,
pub state: proto::check_blockage::State,
}
#[derive(Deserialize, Serialize)]
pub struct BlockageMigReqState {
pub request: proto::blockage_migration::Request,
pub state:proto::blockage_migration::State,
pub state: proto::blockage_migration::State,
}
#[derive(Debug, Deserialize, Serialize)]
@ -65,7 +65,7 @@ pub struct PubKeys {
#[serde_as]
#[derive(Serialize, Deserialize)]
pub struct EncBridgeTable {
#[serde_as(as = "Vec<[_; ENC_BUCKET_BYTES]>")]
#[serde_as(as = "Vec<[_; ENC_BUCKET_BYTES]>")]
pub etable: Vec<[u8; ENC_BUCKET_BYTES]>,
}