Reorganize and refactor, adding integration test for common library stuff

This commit is contained in:
Barry Pederson
2024-11-03 11:44:03 -08:00
parent 00743fbe9c
commit 5f4a0e0e88
9 changed files with 148 additions and 82 deletions
+6 -3
View File
@@ -1,16 +1,19 @@
use crate::repository::PasswordDirectory;
use common::{Request, RESPONSE_NO, RESPONSE_OK};
use common::request::Request;
use std::io::{Error, Write};
use std::os::unix::net::UnixStream;
pub const RESPONSE_NO: [u8; 4] = [0x0, 0x2, b'N', b'O'];
pub const RESPONSE_OK: [u8; 4] = [0x0, 0x2, b'O', b'K'];
pub struct Handler {
stream: UnixStream,
handler: PasswordDirectory,
}
impl Handler {
pub fn new(stream: UnixStream, handler: PasswordDirectory) -> Result<Handler, Error> {
Ok(Handler { stream, handler })
pub fn new(stream: UnixStream, handler: PasswordDirectory) -> Handler {
Handler { stream, handler }
}
fn communicate(&mut self) -> Result<(), Error> {
+2 -2
View File
@@ -1,4 +1,4 @@
use common::Request;
use common::request::Request;
use std::fs::File;
use std::io::{BufRead, BufReader};
use std::path::PathBuf;
@@ -41,7 +41,7 @@ impl PasswordDirectory {
#[cfg(test)]
mod tests {
use crate::repository::PasswordDirectory;
use common::Request;
use common::request::Request;
const TEST_PATH: &str = "./test_passwords";
+5 -5
View File
@@ -9,12 +9,12 @@ use std::thread;
pub struct Server {
listener: UnixListener,
handler: PasswordDirectory,
repository: PasswordDirectory,
socket_name: PathBuf,
}
impl Server {
pub fn new(socket_name: &str, handler: PasswordDirectory) -> Result<Server, Error> {
pub fn new(socket_name: &str, repository: PasswordDirectory) -> Result<Server, Error> {
let socket_name = PathBuf::from(socket_name);
if socket_name.exists() {
@@ -28,7 +28,7 @@ impl Server {
set_permissions(&socket_name, perms)?;
Ok(Server {
handler,
repository,
listener,
socket_name,
})
@@ -40,8 +40,8 @@ impl Server {
match stream {
Ok(stream) => {
/* connection succeeded */
let handler = self.handler.clone();
thread::spawn(|| Handler::new(stream, handler).unwrap().handle_client());
let repository = self.repository.clone();
thread::spawn(|| Handler::new(stream, repository).handle_client());
}
Err(_err) => {
/* connection failed */