use std::sync::atomic::AtomicBool;
use std::sync::Arc;
use std::thread;
use crossbeam::channel::bounded;
use crate::checkers::athena::Athena;
use crate::checkers::checker_type::{Check, Checker};
use crate::checkers::CheckerTypes;
use crate::config::get_config;
use crate::filtration_system::{filter_and_get_decoders, MyResults};
use crate::{timer, DecoderResult};
mod astar;
mod bfs;
mod helper_functions;
pub fn search_for_plaintext(input: String) -> Option<DecoderResult> {
let config = get_config();
let timeout = config.timeout;
let timer = timer::start(timeout);
let (result_sender, result_recv) = bounded::<Option<DecoderResult>>(1);
let stop = Arc::new(AtomicBool::new(false));
let s = stop.clone();
let handle = thread::spawn(move || astar::astar(input, result_sender, s));
let top_results_mode = config.top_results;
let mut first_result = None;
loop {
if let Ok(res) = result_recv.try_recv() {
log::info!("Found potential plaintext result");
log::trace!("Result details: {:?}", res);
if top_results_mode {
if first_result.is_none() {
first_result = res;
}
} else {
stop.store(true, std::sync::atomic::Ordering::Relaxed);
handle.join().unwrap();
return res;
}
}
if timer.try_recv().is_ok() {
stop.store(true, std::sync::atomic::Ordering::Relaxed);
log::info!("Search timer expired");
handle.join().unwrap();
if top_results_mode {
return first_result;
}
return None;
}
std::thread::sleep(std::time::Duration::from_millis(10));
}
}
#[allow(dead_code)]
fn perform_decoding(text: &DecoderResult) -> MyResults {
let decoders = filter_and_get_decoders(text);
let athena_checker = Checker::<Athena>::new();
let checker = CheckerTypes::CheckAthena(athena_checker);
decoders.run(&text.text[0], checker)
}
#[cfg(test)]
mod tests {
use super::*;
fn exit_condition(input: &str) -> bool {
let athena_checker = Checker::<Athena>::new();
let checker = CheckerTypes::CheckAthena(athena_checker);
checker.check(input).is_identified
}
#[test]
fn exit_condition_succeeds() {
let result = exit_condition("https://www.google.com");
assert!(result);
}
#[test]
fn exit_condition_fails() {
let result = exit_condition("vjkrerkdnxhrfjekfdjexk");
assert!(!result);
}
#[test]
fn perform_decoding_succeeds() {
let dc = DecoderResult::_new("aHR0cHM6Ly93d3cuZ29vZ2xlLmNvbQ==");
let result = perform_decoding(&dc);
assert!(
result
._break_value()
.expect("expected successful value, none found")
.success
);
}
#[test]
fn perform_decoding_succeeds_empty_string() {
let dc = DecoderResult::_new("");
let result = perform_decoding(&dc);
assert!(result._break_value().is_none());
}
}