Restructure and add unit tests...maybe it works now?
This commit is contained in:
+67
-8
@@ -2,25 +2,26 @@ use std::fs::File;
|
||||
use std::io::{BufRead, BufReader};
|
||||
|
||||
use crate::options::OPTIONS;
|
||||
use crate::server::{Handler, Request};
|
||||
use crate::request::Request;
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct FileHandler;
|
||||
|
||||
|
||||
impl FileHandler {
|
||||
pub fn new() -> FileHandler {}
|
||||
}
|
||||
pub fn new() -> FileHandler {
|
||||
FileHandler {}
|
||||
}
|
||||
|
||||
impl Handler for FileHandler {
|
||||
pub fn check_auth(&request: Request) -> bool {
|
||||
pub fn check_auth(&self, request: &Request) -> bool {
|
||||
// Don't allow empty userids or passwords
|
||||
//
|
||||
if request.userid.is_empty() || request.userid.contains('/') || request.password.is_empty() {
|
||||
if request.userid.is_empty() || request.userid.contains('/') || request.password.is_empty()
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
let mut file_name = OPTIONS.password_dir.clone();
|
||||
file_name.push(request.userid);
|
||||
file_name.push(&request.userid);
|
||||
|
||||
if let Ok(file) = File::open(file_name) {
|
||||
for line in BufReader::new(file).lines().flatten() {
|
||||
@@ -33,3 +34,61 @@ impl Handler for FileHandler {
|
||||
false
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use crate::handler::FileHandler;
|
||||
use crate::request::Request;
|
||||
|
||||
#[test]
|
||||
fn it_checks_good_password() {
|
||||
let handler = FileHandler::new();
|
||||
let request = Request {
|
||||
userid: "bp".to_string(),
|
||||
password: "some-rad-password".to_string(),
|
||||
service: "ignore-this".to_string(),
|
||||
realm: "also-ignore-this".to_string(),
|
||||
};
|
||||
|
||||
assert_eq!(handler.check_auth(&request), true);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn it_fails_bad_password() {
|
||||
let handler = FileHandler::new();
|
||||
let request = Request {
|
||||
userid: "bp".to_string(),
|
||||
password: "not-correct".to_string(),
|
||||
service: "ignore-this".to_string(),
|
||||
realm: "also-ignore-this".to_string(),
|
||||
};
|
||||
|
||||
assert_eq!(handler.check_auth(&request), false);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn it_fails_bad_userid() {
|
||||
let handler = FileHandler::new();
|
||||
let request = Request {
|
||||
userid: "bp-xxx".to_string(),
|
||||
password: "some-rad-password".to_string(),
|
||||
service: "ignore-this".to_string(),
|
||||
realm: "also-ignore-this".to_string(),
|
||||
};
|
||||
|
||||
assert_eq!(handler.check_auth(&request), false);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn it_ignores_comments() {
|
||||
let handler = FileHandler::new();
|
||||
let request = Request {
|
||||
userid: "bp".to_string(),
|
||||
password: "commented-out".to_string(),
|
||||
service: "ignore-this".to_string(),
|
||||
realm: "also-ignore-this".to_string(),
|
||||
};
|
||||
|
||||
assert_eq!(handler.check_auth(&request), false);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user