Skip to content

Commit

Permalink
Fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
amaury1093 committed Dec 19, 2024
1 parent 6bcfa56 commit 1505911
Show file tree
Hide file tree
Showing 8 changed files with 154 additions and 18 deletions.
108 changes: 104 additions & 4 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion backend/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ tracing = "0.1.40"
tracing-subscriber = "0.3.18"
uuid = "1.10"
warp = "0.3"
rustls = { version = "0.23", features = ["ring"] }

[dev-dependencies]
toml = "0.8"
3 changes: 0 additions & 3 deletions backend/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,6 @@ const CARGO_PKG_VERSION: &str = env!("CARGO_PKG_VERSION");
async fn main() -> Result<(), anyhow::Error> {
// Initialize logging.
tracing_subscriber::fmt::init();
rustls::crypto::ring::default_provider()
.install_default()
.expect("Failed to install rustls crypto provider");

info!(target: LOG_TARGET, version=?CARGO_PKG_VERSION, "Running Reacher");
let mut config = load_config().await?;
Expand Down
1 change: 0 additions & 1 deletion cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ check-if-email-exists = { path = "../core" }
clap = { version = "3.2", features = ["derive", "env"] }
once_cell = "1.19"
openssl = { version = "0.10", features = ["vendored"] }
rustls = { version = "0.23", features = ["ring"] }
serde = "1.0"
serde_json = "1.0"
tracing-subscriber = "0.3.18"
Expand Down
3 changes: 0 additions & 3 deletions cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,9 +90,6 @@ pub(crate) static CONF: Lazy<Cli> = Lazy::new(Cli::parse);
#[tokio::main]
async fn main() -> Result<(), anyhow::Error> {
tracing_subscriber::fmt::init();
rustls::crypto::ring::default_provider()
.install_default()
.expect("Failed to install rustls crypto provider");

let to_email = &CONF.to_email;

Expand Down
2 changes: 2 additions & 0 deletions core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ futures = { version = "0.3.30" }
hickory-proto = "0.24.0"
hickory-resolver = "0.24.0"
levenshtein = "1.0.5"
lettre = { version = "0.11", features = ["smtp-transport"] }
log = "0.4.22"
mailchecker = "6.0.11"
md5 = "0.7.0"
Expand All @@ -37,6 +38,7 @@ reqwest = { version = "0.12.5", default-features = false, features = [
"json",
"rustls-tls",
] }
rustls = { version = "0.23", features = ["ring"] }
sentry = { version = "0.32", default-features = false, features = [
"reqwest",
"rustls",
Expand Down
14 changes: 14 additions & 0 deletions core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,10 @@ use hickory_proto::rr::rdata::MX;
use misc::{check_misc, MiscDetails};
use mx::check_mx;
use rand::Rng;
use rustls::crypto::ring;
use smtp::{check_smtp, SmtpDetails, SmtpError};
pub use smtp::{is_gmail, is_hotmail, is_hotmail_b2b, is_hotmail_b2c, is_yahoo};
use std::sync::Once;
use std::time::{Duration, SystemTime};
use syntax::{check_syntax, get_similar_mail_provider};
pub use util::input_output::*;
Expand All @@ -88,6 +90,16 @@ use crate::rules::{has_rule, Rule};
/// The target where to log check-if-email-exists logs.
pub const LOG_TARGET: &str = "reacher";

static INIT: Once = Once::new();

/// check-if-email-exists uses rustls for its TLS connections. This function
/// initializes the default crypto provider for rustls.
pub fn initialize_crypto_provider() {
INIT.call_once(|| {
ring::default_provider().install_default().unwrap();
});
}

/// Given an email's misc and smtp details, calculate an estimate of our
/// confidence on how reachable the email is.
///
Expand Down Expand Up @@ -120,6 +132,7 @@ fn calculate_reachable(misc: &MiscDetails, smtp: &Result<SmtpDetails, SmtpError>
/// Returns a `CheckEmailOutput` output, whose `is_reachable` field is one of
/// `Safe`, `Invalid`, `Risky` or `Unknown`.
pub async fn check_email(input: &CheckEmailInput) -> CheckEmailOutput {
initialize_crypto_provider();
let start_time = SystemTime::now();
let to_email = &input.to_email;

Expand Down Expand Up @@ -273,6 +286,7 @@ pub async fn check_email(input: &CheckEmailInput) -> CheckEmailOutput {
},
};

#[cfg(feature = "sentry")]
log_unknown_errors(&output, &input.backend_name);

output
Expand Down
40 changes: 34 additions & 6 deletions core/src/smtp/yahoo/headless.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,37 @@ use crate::{smtp::SmtpDetails, LOG_TARGET};
/// browser. Make sure you have a WebDriver server running locally before
/// running this, or this will error.
pub async fn check_headless(to_email: &str, webdriver: &str) -> Result<SmtpDetails, HeadlessError> {
tracing::debug!(
target: LOG_TARGET,
email=%to_email,
"Using Yahoo password recovery in headless navigator"
);
let mut attempts = 0;
let max_attempts = 3;
let mut last_error = None;

while attempts < max_attempts {
attempts += 1;
tracing::debug!(
target: LOG_TARGET,
email=%to_email,
attempt=%attempts,
"Using Yahoo password recovery in headless navigator"
);

match check_headless_inner(to_email, webdriver).await {
Ok(result) => return Ok(result),
Err(e) => {
last_error = Some(e);
if attempts < max_attempts {
sleep(Duration::from_secs(1));
}
}
}
}

Err(last_error.unwrap())
}

async fn check_headless_inner(
to_email: &str,
webdriver: &str,
) -> Result<SmtpDetails, HeadlessError> {
let c = create_headless_client(webdriver).await?;

// Navigate to Microsoft password recovery page.
Expand Down Expand Up @@ -107,15 +132,18 @@ pub async fn check_headless(to_email: &str, webdriver: &str) -> Result<SmtpDetai

#[cfg(test)]
mod tests {
use crate::initialize_crypto_provider;

use super::check_headless;

// Ignoring this test as it requires a local process of WebDriver running on
// "http://localhost:9515". To debug the headless password recovery page,
// run chromedriver and remove the "#[ignore]".
// Also see: https://github.com/jonhoo/fantoccini
#[tokio::test]
#[ignore = "Run a webdriver server locally to test this"]
// #[ignore = "Run a webdriver server on port 9515 locally to test this"]
async fn test_yahoo_address() {
initialize_crypto_provider();
// Run 5 headless sessions with the below dummy emails.
for _ in 0..5 {
// Email does not exist.
Expand Down

0 comments on commit 1505911

Please sign in to comment.