From 1f155ac969af843d8c43209a7a9f95d791f1505b Mon Sep 17 00:00:00 2001 From: Vecna Date: Mon, 29 Apr 2024 12:54:21 -0400 Subject: [PATCH] Refactor/clean up --- src/{client_lib.rs => lib.rs} | 44 ++++++---------------------- src/main.rs | 25 ++++------------ src/{client_net.rs => networking.rs} | 10 +++++-- src/tests.rs | 33 ++++++++++++++++----- 4 files changed, 48 insertions(+), 64 deletions(-) rename src/{client_lib.rs => lib.rs} (90%) rename src/{client_net.rs => networking.rs} (79%) diff --git a/src/client_lib.rs b/src/lib.rs similarity index 90% rename from src/client_lib.rs rename to src/lib.rs index 2a929af..bb57176 100644 --- a/src/client_lib.rs +++ b/src/lib.rs @@ -1,29 +1,13 @@ -use async_trait::async_trait; -use curve25519_dalek::scalar::Scalar; -use lox_library::bridge_table::from_scalar; -use lox_library::bridge_table::BridgeLine; -use lox_library::bridge_table::BridgeTable; -use lox_library::bridge_table::EncryptedBucket; -use lox_library::bridge_table::MAX_BRIDGES_PER_BUCKET; -use lox_library::proto::*; -use lox_library::scalar_u32; -use lox_library::IssuerPubKey; -use lox_library::OPENINV_LENGTH; -use lox_utils::EncBridgeTable; -use lox_utils::Invite; -use serde::{Deserialize, Serialize}; -use serde_with::serde_as; +use lox_library::{ + bridge_table::{from_scalar, BridgeLine, BridgeTable, EncryptedBucket, MAX_BRIDGES_PER_BUCKET}, + proto::*, + scalar_u32, IssuerPubKey, OPENINV_LENGTH, +}; +use lox_utils::{EncBridgeTable, Invite}; use std::collections::HashMap; -use std::time::Duration; -// used for testing function -use std::io::Write; - -// provides a generic way to make network requests -#[async_trait] -pub trait Networking { - async fn request(&self, endpoint: String, body: Vec) -> Vec; -} +pub mod networking; +use crate::networking::Networking; // Helper functions to get public keys from vector pub fn get_lox_pub(lox_auth_pubkeys: &Vec) -> &IssuerPubKey { @@ -280,14 +264,4 @@ pub async fn blockage_migration( } #[cfg(test)] -// Advance days on server -pub async fn advance_days(net: &dyn Networking, days: u16) -> u32 { - let resp = net - .request( - "/advancedays".to_string(), - serde_json::to_vec(&days).unwrap(), - ) - .await; - let today: u32 = serde_json::from_slice(&resp).unwrap(); - today -} +mod tests; diff --git a/src/main.rs b/src/main.rs index 7e4d997..eaaa85e 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,21 +1,12 @@ -mod client_lib; -use client_lib::*; +use lox_cli::{self, networking::HyperNet, *}; -mod client_net; -use client_net::HyperNet; - -use curve25519_dalek::scalar::Scalar; use getopts::Options; -use lox_library::bridge_table::BridgeLine; -use lox_library::bridge_table::MAX_BRIDGES_PER_BUCKET; -use lox_library::scalar_u32; -use lox_library::IssuerPubKey; +use lox_library::{ + bridge_table::{BridgeLine, MAX_BRIDGES_PER_BUCKET}, + scalar_u32, IssuerPubKey, +}; use serde::Serialize; -use std::env::args; -use std::fs::File; -use std::io::Write; -use std::path::Path; -use std::str::FromStr; +use std::{env::args, fs::File, io::Write, path::Path}; // Prints the argument details for this program fn print_usage(program: &str, opts: Options) { @@ -208,7 +199,3 @@ async fn main() { lox_cred }; } - -// Unit tests -#[cfg(test)] -mod tests; diff --git a/src/client_net.rs b/src/networking.rs similarity index 79% rename from src/client_net.rs rename to src/networking.rs index f14e695..95f1135 100644 --- a/src/client_net.rs +++ b/src/networking.rs @@ -1,10 +1,14 @@ -// This file provides networking using hyper (which -// https://gitlab.torproject.org/onyinyang/lox-server uses). +// This file provides a Networking trait and a working hyper implementation -use crate::client_lib::Networking; use async_trait::async_trait; use hyper::{Body, Client, Method, Request}; +// provides a generic way to make network requests +#[async_trait] +pub trait Networking { + async fn request(&self, endpoint: String, body: Vec) -> Vec; +} + pub struct HyperNet { pub hostname: String, } diff --git a/src/tests.rs b/src/tests.rs index 634a763..1de56c8 100644 --- a/src/tests.rs +++ b/src/tests.rs @@ -11,16 +11,35 @@ trust migration and level up functions. */ // Note: We can't run multiple time-changing tests simultaneously because // they will invalidate invites that haven't been redeemed yet. -use super::client_lib::*; -use super::client_net::HyperNet; -use lox_library::bridge_table::BridgeLine; -use lox_library::proto::level_up::{LEVEL_INTERVAL, LEVEL_INVITATIONS}; -use lox_library::proto::trust_promotion::UNTRUSTED_INTERVAL; -use lox_library::scalar_u32; +use crate::{networking::*, *}; +use lox_library::{ + bridge_table::{self, from_scalar, BridgeLine, BridgeTable}, + proto::{ + level_up::{LEVEL_INTERVAL, LEVEL_INVITATIONS}, + trust_promotion::UNTRUSTED_INTERVAL, + }, + scalar_u32, +}; -use std::cmp::min; +use hyper::StatusCode; +use std::{ + cmp::min, + collections::{HashMap, HashSet}, +}; use tokio::spawn; +// Advance days on server +pub async fn advance_days(net: &dyn Networking, days: u16) -> u32 { + let resp = net + .request( + "/advancedays".to_string(), + serde_json::to_vec(&days).unwrap(), + ) + .await; + let today: u32 = serde_json::from_slice(&resp).unwrap(); + today +} + // These are all combined into the same test because otherwise we run into // issues with server state due to asynchronicity. #[tokio::test]