From a5178a322028ee136ae7b30067ab14616f178278 Mon Sep 17 00:00:00 2001 From: Zshan0 Date: Sun, 19 Jun 2022 10:56:30 +0530 Subject: [PATCH] Adding default port value if not provided. Rather than throwing error for invalid host address due to missing port, default port is inserted at the end and validity is checked again. Added tests for the same. --- src/installer/step/mod.rs | 69 ++++++++++++++++++++++++++++++++++++++- 1 file changed, 68 insertions(+), 1 deletion(-) diff --git a/src/installer/step/mod.rs b/src/installer/step/mod.rs index eea3d24f..818778d4 100644 --- a/src/installer/step/mod.rs +++ b/src/installer/step/mod.rs @@ -339,6 +339,18 @@ impl Step for DefineCoordinator { self.host.valid = SocketAddr::from_str(&self.host.value).is_ok(); + if !self.host.valid { + // trim spaces at the end if any. + let clean_value = self.host.value.trim_end(); + let value = format!("{}:8383", clean_value).to_string(); + + self.host.valid = SocketAddr::from_str(&value).is_ok(); + + if self.host.valid { + self.host.value = value; + } + } + if !self.host.valid || !self.noise_key.valid { return false; } @@ -555,9 +567,10 @@ impl From for Box { #[cfg(test)] mod tests { + use super::DefineCoordinator as DefineCoordinatorStep; use super::{DefineCpfpDescriptor as DefineCpfpDescriptorStep, *}; use crate::installer::config::Config; - use crate::installer::message::{DefineCpfpDescriptor, ParticipantXpub, *}; + use crate::installer::message::{DefineCoordinator, DefineCpfpDescriptor, ParticipantXpub, *}; const STAKEHOLDERS_XPUBS: [&str; 4] = [ "xpub6DEzq5DNPx2rPiZJ7wvFhxRKUKDoV1GwjFmFdaxFfbsw9HsHyxc9usoRUMxqJaMrwoXh4apahsGEnjAS4cVCBDgqsx5Groww22AdHbgxVDg", @@ -618,6 +631,60 @@ mod tests { } } + const HOST_NO_PORT: [&str; 3] = ["193.168.1.13", "193.168.1.13 ", "193.168.1.13 "]; + const HOST_PORT: [&str; 1] = ["193.168.1.13:8383"]; + const INCORRECT_HOST: [&str; 1] = ["193.168.1."]; + const NOISE_KEYS: [&str; 1] = + // 64 character long string + ["1234678901234567890123456789012345678901234567890123456789012345"]; + + #[test] + fn define_coordinator_host() { + let mut ctx = Context::new(bitcoin::Network::Bitcoin); + let mut manager_config = Config::new(); + + // Valid host with port already existing. + for host in HOST_PORT.iter() { + let mut coordinator_step = DefineCoordinatorStep::new(); + coordinator_step.update(message::Message::DefineCoordinator( + DefineCoordinator::NoiseKeyEdited(NOISE_KEYS[0].to_string()), + )); + + coordinator_step.update(message::Message::DefineCoordinator( + DefineCoordinator::HostEdited(host.to_string()), + )); + let is_valid = coordinator_step.apply(&mut ctx, &mut manager_config); + assert!(is_valid); + } + + // No port. + for host in HOST_NO_PORT.iter() { + let mut coordinator_step = DefineCoordinatorStep::new(); + coordinator_step.update(message::Message::DefineCoordinator( + DefineCoordinator::NoiseKeyEdited(NOISE_KEYS[0].to_string()), + )); + + coordinator_step.update(Message::DefineCoordinator(DefineCoordinator::HostEdited( + host.to_string(), + ))); + let is_valid = coordinator_step.apply(&mut ctx, &mut manager_config); + assert!(is_valid); + } + // Incorrect host addresses for avoiding false positive. + for host in INCORRECT_HOST.iter() { + let mut coordinator_step = DefineCoordinatorStep::new(); + coordinator_step.update(message::Message::DefineCoordinator( + DefineCoordinator::NoiseKeyEdited(NOISE_KEYS[0].to_string()), + )); + + coordinator_step.update(Message::DefineCoordinator(DefineCoordinator::HostEdited( + host.to_string(), + ))); + let is_valid = coordinator_step.apply(&mut ctx, &mut manager_config); + assert!(!is_valid); + } + } + #[test] fn define_deposit_descriptor() { let mut ctx = Context::new(bitcoin::Network::Bitcoin);