Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Gh 597 #5

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions automap/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,6 @@ path = "src/main.rs"
[lib]
name = "automap_lib"
path = "src/lib.rs"

[features]
no_test_share = []
7 changes: 1 addition & 6 deletions automap/src/comm_layer/igdp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ use std::time::{Duration, Instant};
pub const HOUSEKEEPING_THREAD_LOOP_DELAY_MS: u64 = 100;
pub const PUBLIC_IP_POLL_DELAY_SECONDS: u64 = 60;

trait GatewayFactory {
trait GatewayFactory: Send {
fn make(&self, options: SearchOptions) -> Result<Box<dyn GatewayWrapper>, SearchError>;
}

Expand Down Expand Up @@ -580,20 +580,15 @@ impl MappingAdderReal {
#[cfg(test)]
mod tests {
use super::*;
use crate::control_layer::automap_control::AutomapChange;
use crate::mocks::LocalIpFinderMock;
use core::ptr::addr_of;
use crossbeam_channel::unbounded;
use igd::RequestError;
use masq_lib::test_utils::logging::{init_test_logging, TestLogHandler};
use masq_lib::utils::AutomapProtocol;
use std::cell::RefCell;
use std::net::Ipv6Addr;
use std::ops::Sub;
use std::str::FromStr;
use std::sync::{Arc, Mutex};
use std::thread;
use std::time::Duration;

fn clone_get_external_ip_error(error: &GetExternalIpError) -> GetExternalIpError {
match error {
Expand Down
2 changes: 1 addition & 1 deletion automap/src/comm_layer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ impl AutomapError {
}
}

pub trait Transactor {
pub trait Transactor: Send {
fn find_routers(&self) -> Result<Vec<IpAddr>, AutomapError>;
fn get_public_ip(&self, router_ip: IpAddr) -> Result<IpAddr, AutomapError>;
fn add_mapping(
Expand Down
243 changes: 14 additions & 229 deletions automap/src/control_layer/automap_control.rs
Original file line number Diff line number Diff line change
Expand Up @@ -332,234 +332,31 @@ impl AutomapControlReal {
}
}

#[cfg(any(test, not(feature = "no_test_share")))]
pub fn replace_transactor(
subject: AutomapControlReal,
transactor: Box<dyn Transactor>,
) -> AutomapControlReal {
let idx = AutomapControlReal::find_transactor_index(
subject.transactors.borrow_mut(),
transactor.protocol(),
);
subject.transactors.borrow_mut()[idx] = transactor;
subject
}

#[cfg(test)]
mod tests {
use super::*;
use crate::comm_layer::Transactor;
use crate::mocks::{TransactorMock, PUBLIC_IP, ROUTER_IP};
use crossbeam_channel::{unbounded, TryRecvError};
use lazy_static::lazy_static;
use std::any::Any;
use std::cell::RefCell;
use std::net::IpAddr;
use std::ptr::addr_of;
use std::str::FromStr;
use std::sync::{Arc, Mutex};

lazy_static! {
static ref ROUTER_IP: IpAddr = IpAddr::from_str("1.2.3.4").unwrap();
static ref PUBLIC_IP: IpAddr = IpAddr::from_str("2.3.4.5").unwrap();
}

struct TransactorMock {
housekeeping_thread_started: bool,
protocol: AutomapProtocol,
find_routers_results: RefCell<Vec<Result<Vec<IpAddr>, AutomapError>>>,
get_public_ip_params: Arc<Mutex<Vec<IpAddr>>>,
get_public_ip_results: RefCell<Vec<Result<IpAddr, AutomapError>>>,
add_mapping_params: Arc<Mutex<Vec<(IpAddr, u16, u32)>>>,
add_mapping_results: RefCell<Vec<Result<u32, AutomapError>>>,
add_permanent_mapping_params: Arc<Mutex<Vec<(IpAddr, u16)>>>,
add_permanent_mapping_results: RefCell<Vec<Result<u32, AutomapError>>>,
delete_mapping_params: Arc<Mutex<Vec<(IpAddr, u16)>>>,
delete_mapping_results: RefCell<Vec<Result<(), AutomapError>>>,
start_housekeeping_thread_params: Arc<Mutex<Vec<(ChangeHandler, IpAddr)>>>,
start_housekeeping_thread_results:
RefCell<Vec<Result<Sender<HousekeepingThreadCommand>, AutomapError>>>,
stop_housekeeping_thread_params: Arc<Mutex<Vec<()>>>,
stop_housekeeping_thread_results: RefCell<Vec<Result<ChangeHandler, AutomapError>>>,
}

impl Transactor for TransactorMock {
fn find_routers(&self) -> Result<Vec<IpAddr>, AutomapError> {
self.find_routers_results.borrow_mut().remove(0)
}

fn get_public_ip(&self, router_ip: IpAddr) -> Result<IpAddr, AutomapError> {
if !self.housekeeping_thread_started {
panic!("Housekeeping thread must be started before get_public_ip()")
}
self.get_public_ip_params.lock().unwrap().push(router_ip);
self.get_public_ip_results.borrow_mut().remove(0)
}

fn add_mapping(
&self,
router_ip: IpAddr,
hole_port: u16,
lifetime: u32,
) -> Result<u32, AutomapError> {
if !self.housekeeping_thread_started {
panic!("Housekeeping thread must be started before add_mapping()")
}
self.add_mapping_params
.lock()
.unwrap()
.push((router_ip, hole_port, lifetime));
self.add_mapping_results.borrow_mut().remove(0)
}

fn add_permanent_mapping(
&self,
router_ip: IpAddr,
hole_port: u16,
) -> Result<u32, AutomapError> {
if !self.housekeeping_thread_started {
panic!("Housekeeping thread must be started before add_permanent_mapping()")
}
self.add_permanent_mapping_params
.lock()
.unwrap()
.push((router_ip, hole_port));
self.add_permanent_mapping_results.borrow_mut().remove(0)
}

fn delete_mapping(&self, router_ip: IpAddr, hole_port: u16) -> Result<(), AutomapError> {
self.delete_mapping_params
.lock()
.unwrap()
.push((router_ip, hole_port));
self.delete_mapping_results.borrow_mut().remove(0)
}

fn protocol(&self) -> AutomapProtocol {
self.protocol
}

fn start_housekeeping_thread(
&mut self,
change_handler: ChangeHandler,
router_ip: IpAddr,
) -> Result<Sender<HousekeepingThreadCommand>, AutomapError> {
self.start_housekeeping_thread_params
.lock()
.unwrap()
.push((change_handler, router_ip));
let result = self
.start_housekeeping_thread_results
.borrow_mut()
.remove(0);
self.housekeeping_thread_started = true;
result
}

fn stop_housekeeping_thread(&mut self) -> Result<ChangeHandler, AutomapError> {
self.stop_housekeeping_thread_params
.lock()
.unwrap()
.push(());
let result = self.stop_housekeeping_thread_results.borrow_mut().remove(0);
self.housekeeping_thread_started = false;
result
}

fn as_any(&self) -> &dyn Any {
self
}
}

impl TransactorMock {
pub fn new(protocol: AutomapProtocol) -> Self {
Self {
housekeeping_thread_started: false,
protocol,
find_routers_results: RefCell::new(vec![]),
get_public_ip_params: Arc::new(Mutex::new(vec![])),
get_public_ip_results: RefCell::new(vec![]),
add_mapping_params: Arc::new(Mutex::new(vec![])),
add_mapping_results: RefCell::new(vec![]),
add_permanent_mapping_params: Arc::new(Mutex::new(vec![])),
add_permanent_mapping_results: RefCell::new(vec![]),
delete_mapping_params: Arc::new(Mutex::new(vec![])),
delete_mapping_results: RefCell::new(vec![]),
start_housekeeping_thread_params: Arc::new(Mutex::new(vec![])),
start_housekeeping_thread_results: RefCell::new(vec![]),
stop_housekeeping_thread_params: Arc::new(Mutex::new(vec![])),
stop_housekeeping_thread_results: RefCell::new(vec![]),
}
}

pub fn find_routers_result(self, result: Result<Vec<IpAddr>, AutomapError>) -> Self {
self.find_routers_results.borrow_mut().push(result);
self
}

pub fn get_public_ip_params(mut self, params: &Arc<Mutex<Vec<IpAddr>>>) -> Self {
self.get_public_ip_params = params.clone();
self
}

pub fn get_public_ip_result(self, result: Result<IpAddr, AutomapError>) -> Self {
self.get_public_ip_results.borrow_mut().push(result);
self
}

pub fn add_mapping_params(mut self, params: &Arc<Mutex<Vec<(IpAddr, u16, u32)>>>) -> Self {
self.add_mapping_params = params.clone();
self
}

pub fn add_mapping_result(self, result: Result<u32, AutomapError>) -> Self {
self.add_mapping_results.borrow_mut().push(result);
self
}

pub fn add_permanent_mapping_params(
mut self,
params: &Arc<Mutex<Vec<(IpAddr, u16)>>>,
) -> Self {
self.add_permanent_mapping_params = params.clone();
self
}

pub fn add_permanent_mapping_result(self, result: Result<u32, AutomapError>) -> Self {
self.add_permanent_mapping_results.borrow_mut().push(result);
self
}

pub fn delete_mapping_params(mut self, params: &Arc<Mutex<Vec<(IpAddr, u16)>>>) -> Self {
self.delete_mapping_params = params.clone();
self
}

pub fn delete_mapping_result(self, result: Result<(), AutomapError>) -> Self {
self.delete_mapping_results.borrow_mut().push(result);
self
}

pub fn start_housekeeping_thread_result(
self,
result: Result<Sender<HousekeepingThreadCommand>, AutomapError>,
) -> Self {
self.start_housekeeping_thread_results
.borrow_mut()
.push(result);
self
}

pub fn start_housekeeping_thread_params(
mut self,
params: &Arc<Mutex<Vec<(ChangeHandler, IpAddr)>>>,
) -> Self {
self.start_housekeeping_thread_params = params.clone();
self
}

pub fn stop_housekeeping_thread_params(mut self, params: &Arc<Mutex<Vec<()>>>) -> Self {
self.stop_housekeeping_thread_params = params.clone();
self
}

pub fn stop_housekeeping_thread_result(
self,
result: Result<ChangeHandler, AutomapError>,
) -> Self {
self.stop_housekeeping_thread_results
.borrow_mut()
.push(result);
self
}
}

fn choose_working_protocol_works_for_success(protocol: AutomapProtocol) {
let mut subject = make_multirouter_specific_success_subject(
protocol,
Expand Down Expand Up @@ -1586,18 +1383,6 @@ mod tests {
subject
}

fn replace_transactor(
subject: AutomapControlReal,
transactor: Box<dyn Transactor>,
) -> AutomapControlReal {
let idx = AutomapControlReal::find_transactor_index(
subject.transactors.borrow_mut(),
transactor.protocol(),
);
subject.transactors.borrow_mut()[idx] = transactor;
subject
}

fn assert_all_protocols_failed<T: Debug + PartialEq>(
result: Result<T, AutomapError>,
pcp: AutomapError,
Expand Down
1 change: 0 additions & 1 deletion automap/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,4 @@ pub mod logger;
pub mod probe_researcher;
pub mod protocols;

#[cfg(test)]
pub mod mocks;
Loading