Skip to content

Commit

Permalink
added tests
Browse files Browse the repository at this point in the history
  • Loading branch information
dumbasPL committed Nov 13, 2023
1 parent 99b4e5d commit 1ae0db2
Show file tree
Hide file tree
Showing 13 changed files with 130 additions and 0 deletions.
20 changes: 20 additions & 0 deletions .github/workflows/test_ci.yml
Original file line number Diff line number Diff line change
@@ -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
72 changes: 72 additions & 0 deletions examples/get-test-data.rs
Original file line number Diff line number Diff line change
@@ -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(())
}
Binary file added tests/data/captcha_5wq5kg82.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added tests/data/captcha_gvvpvbrk.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added tests/data/captcha_h9q55ini.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added tests/data/captcha_jky533gk.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added tests/data/captcha_jv26634d.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added tests/data/captcha_jv34w6k8.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added tests/data/captcha_kg32n4r6.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added tests/data/captcha_pvbj3839.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added tests/data/captcha_qhpy47fr.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added tests/data/captcha_whpvhmt5.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
38 changes: 38 additions & 0 deletions tests/offline_solve.rs
Original file line number Diff line number Diff line change
@@ -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::<Vec<_>>();

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);
}
}

0 comments on commit 1ae0db2

Please sign in to comment.