diff --git a/.github/workflows/test_ci.yml b/.github/workflows/test_ci.yml new file mode 100644 index 0000000..c1271a0 --- /dev/null +++ b/.github/workflows/test_ci.yml @@ -0,0 +1,20 @@ +name: Build and Test + +on: + push: + branches: [ "master" ] + pull_request: + branches: [ "master" ] + +env: + CARGO_TERM_COLOR: always + +jobs: + build: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + - name: Build + run: cargo build --verbose + - name: Run tests + run: cargo test --verbose \ No newline at end of file diff --git a/examples/get-test-data.rs b/examples/get-test-data.rs new file mode 100644 index 0000000..f39c487 --- /dev/null +++ b/examples/get-test-data.rs @@ -0,0 +1,72 @@ +use std::{ + fs::{create_dir_all, File}, + io::Write, +}; + +use aero2solver::{ + constants::{BASE_URL, USER_AGENT}, + portal::PortalClient, + solver::Aero2Solver, +}; +use anyhow::{anyhow, Result}; + +fn run(solver: &mut Aero2Solver) -> Result<()> { + let client = PortalClient::new(BASE_URL, USER_AGENT)?; + + let state = client.get_state()?; + + let mut tries = 0; + let (solution, captcha) = loop { + tries += 1; + println!("Trying to solve captcha (try {})", tries); + let captcha = client.get_captcha(&state.session_id)?; + match solver.solve(&captcha, 0.9, 8) { + Ok(solution) => { + println!("Captcha solved as {} after {}", solution, tries); + break (solution, captcha); + } + Err(e) => println!("Error while solving captcha: {}", e), + } + }; + + let state = client.submit_captcha(&state.session_id, &solution)?; + match state.message { + Some(ref message) if message.eq("Rozłącz i ponownie połącz się z Internetem.") => { + println!("Captcha solved, code: {}", solution); + } + _ => Err(anyhow!( + "Captcha solved with message: {}", + state.message.unwrap_or_default() + ))?, + } + + let mut file = File::create(format!("./tests/data/captcha_{}.jpg", solution))?; + file.write_all(&captcha)?; + + Ok(()) +} + +fn main() -> Result<()> { + let mut solver = Aero2Solver::new( + "./model/captcha.names", + "./model/captcha.cfg", + "./model/captcha.weights", + )?; + + create_dir_all("./tests/data")?; + + // loop until we have 10 successful solves + let mut success_count = 0; + loop { + if success_count >= 10 { + break; + } + + match run(&mut solver) { + Ok(_) => success_count += 1, + Err(e) => println!("Error: {}", e), + } + } + + Ok(()) +} diff --git a/tests/data/captcha_5wq5kg82.jpg b/tests/data/captcha_5wq5kg82.jpg new file mode 100644 index 0000000..731464e Binary files /dev/null and b/tests/data/captcha_5wq5kg82.jpg differ diff --git a/tests/data/captcha_gvvpvbrk.jpg b/tests/data/captcha_gvvpvbrk.jpg new file mode 100644 index 0000000..ecf516a Binary files /dev/null and b/tests/data/captcha_gvvpvbrk.jpg differ diff --git a/tests/data/captcha_h9q55ini.jpg b/tests/data/captcha_h9q55ini.jpg new file mode 100644 index 0000000..7527946 Binary files /dev/null and b/tests/data/captcha_h9q55ini.jpg differ diff --git a/tests/data/captcha_jky533gk.jpg b/tests/data/captcha_jky533gk.jpg new file mode 100644 index 0000000..bdbb91e Binary files /dev/null and b/tests/data/captcha_jky533gk.jpg differ diff --git a/tests/data/captcha_jv26634d.jpg b/tests/data/captcha_jv26634d.jpg new file mode 100644 index 0000000..642a88c Binary files /dev/null and b/tests/data/captcha_jv26634d.jpg differ diff --git a/tests/data/captcha_jv34w6k8.jpg b/tests/data/captcha_jv34w6k8.jpg new file mode 100644 index 0000000..898683a Binary files /dev/null and b/tests/data/captcha_jv34w6k8.jpg differ diff --git a/tests/data/captcha_kg32n4r6.jpg b/tests/data/captcha_kg32n4r6.jpg new file mode 100644 index 0000000..f24e72f Binary files /dev/null and b/tests/data/captcha_kg32n4r6.jpg differ diff --git a/tests/data/captcha_pvbj3839.jpg b/tests/data/captcha_pvbj3839.jpg new file mode 100644 index 0000000..ec34cd4 Binary files /dev/null and b/tests/data/captcha_pvbj3839.jpg differ diff --git a/tests/data/captcha_qhpy47fr.jpg b/tests/data/captcha_qhpy47fr.jpg new file mode 100644 index 0000000..7f6c8fa Binary files /dev/null and b/tests/data/captcha_qhpy47fr.jpg differ diff --git a/tests/data/captcha_whpvhmt5.jpg b/tests/data/captcha_whpvhmt5.jpg new file mode 100644 index 0000000..db3da48 Binary files /dev/null and b/tests/data/captcha_whpvhmt5.jpg differ diff --git a/tests/offline_solve.rs b/tests/offline_solve.rs new file mode 100644 index 0000000..8c7d447 --- /dev/null +++ b/tests/offline_solve.rs @@ -0,0 +1,38 @@ +use aero2solver::solver::Aero2Solver; +use std::fs; + +#[test] +fn test_solve() { + let mut solver = Aero2Solver::new( + "./model/captcha.names", + "./model/captcha.cfg", + "./model/captcha.weights", + ) + .unwrap(); + + // solve all captchas that match the pattern captcha_*.jpg + let files = fs::read_dir("./tests/data") + .unwrap() + .map(|x| x.unwrap().path()) + .filter(|x| { + x.to_str().unwrap().contains("captcha_") && x.to_str().unwrap().ends_with(".jpg") + }) + .collect::>(); + + for ref file in files { + // extract text between captcha_ and .jpg + let correct_solution = file + .file_stem() + .unwrap() + .to_str() + .unwrap() + .split('_') + .last() + .unwrap(); + + let captcha = fs::read(file).unwrap(); + let solution = solver.solve(&captcha, 0.9, 8).unwrap(); + assert_eq!(solution, correct_solution); + println!("solved test captcha: {}", solution); + } +}