Skip to content

Commit

Permalink
add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
beac0n committed Jul 10, 2024
1 parent 9126ae2 commit a04a870
Show file tree
Hide file tree
Showing 10 changed files with 245 additions and 89 deletions.
2 changes: 1 addition & 1 deletion rustfmt.toml
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ required_version = "1.7.0"
unstable_features = false
disable_all_formatting = false
skip_children = false
hide_parse_errors = false
show_parse_errors = true
error_on_line_overflow = false
error_on_unformatted = false
ignore = []
Expand Down
4 changes: 2 additions & 2 deletions src/bin/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use std::str;
use clap::{Parser, Subcommand};

use ruroco::client::{gen, send};
use ruroco::common::init_logger;
use ruroco::common::{init_logger, time};

#[derive(Parser, Debug)]
#[command(version, about, long_about = None)]
Expand Down Expand Up @@ -78,6 +78,6 @@ fn main() -> Result<(), String> {
address,
command,
deadline,
} => send(private_pem_path, address, command, deadline),
} => send(private_pem_path, address, command, deadline, time()?),
};
}
2 changes: 1 addition & 1 deletion src/blocklist.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ use std::fs;
use std::path::PathBuf;

use log::error;
use serde::ser::{SerializeSeq, Serializer};
use serde::{Deserialize, Serialize};
use serde::ser::{Serializer, SerializeSeq};

use crate::common::get_blocklist_path;

Expand Down
8 changes: 5 additions & 3 deletions src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use openssl::pkey::Private;
use openssl::rsa::Rsa;
use openssl::version::version;

use crate::common::{PADDING_SIZE, RSA_PADDING, time};
use crate::common::{PADDING_SIZE, RSA_PADDING};

fn pem_load_err<I: Display, E: Debug>(err: I, val: E) -> String {
format!("Could not load {val:?}: {err}")
Expand All @@ -23,11 +23,12 @@ pub fn send(
address: String,
command: String,
deadline: u16,
now: u128,
) -> Result<(), String> {
info!("Connecting to udp://{address}, loading PEM from {pem_path:?}, using {} ...", version());

let rsa = get_rsa_private(&pem_path)?;
let data_to_encrypt = get_data_to_encrypt(&command, &rsa, deadline)?;
let data_to_encrypt = get_data_to_encrypt(&command, &rsa, deadline, now)?;
let encrypted_data = encrypt_data(&data_to_encrypt, &rsa)?;

// create UDP socket and send the encrypted data to the specified address
Expand Down Expand Up @@ -57,11 +58,12 @@ fn get_data_to_encrypt(
command: &str,
rsa: &Rsa<Private>,
deadline: u16,
now: u128,
) -> Result<Vec<u8>, String> {
// collect data to encrypt: now-timestamp + command + random data -> all as bytes
let mut data_to_encrypt = Vec::new();

let timestamp = time()? + (u128::from(deadline) * 1_000_000_000);
let timestamp = now + (u128::from(deadline) * 1_000_000_000);
let timestamp_bytes = timestamp.to_le_bytes().to_vec();
let timestamp_len = timestamp_bytes.len();
data_to_encrypt.extend(timestamp_bytes);
Expand Down
2 changes: 1 addition & 1 deletion src/commander.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
use std::{fs, str};
use std::collections::HashMap;
use std::fs::Permissions;
use std::io::Read;
use std::os::unix::fs::{chown, PermissionsExt};
use std::os::unix::net::{UnixListener, UnixStream};
use std::path::PathBuf;
use std::process::Command;
use std::{fs, str};

use log::{error, info, warn};
use users::{get_group_by_name, get_user_by_name};
Expand Down
6 changes: 2 additions & 4 deletions src/server.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use std::{env, fs, str};
use std::io::Write;
use std::net::{SocketAddr, UdpSocket};
use std::os::fd::{FromRawFd, RawFd};
use std::os::unix::net::UnixStream;
use std::path::PathBuf;
use std::{env, fs, str};

use log::{error, info};
use openssl::error::ErrorStack;
Expand All @@ -12,7 +12,7 @@ use openssl::rsa::Rsa;
use openssl::version::version;

use crate::blocklist::Blocklist;
use crate::common::{get_socket_path, RSA_PADDING, time};
use crate::common::{get_socket_path, time, RSA_PADDING};

pub struct Server {
rsa: Rsa<Public>,
Expand Down Expand Up @@ -158,8 +158,6 @@ impl Server {
"Successfully validated data - now {} is before deadline {}",
data.now_ns, data.deadline_ns
);
// TODO: blacklist data.deadline_ns until data.deadline_ns == data.now_ns
// TODO: remove all blacklisted timestamps that are now too old in the next validate call
self.send_command(&data.command_name);
self.update_block_list(&data);
}
Expand Down
70 changes: 70 additions & 0 deletions tests/blocklist_test.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
#[cfg(test)]
mod tests {
use std::{env, fs};

use ruroco::blocklist::Blocklist;
use ruroco::common::get_blocklist_path;

fn create_blocklist() -> Blocklist {
let config_dir = &env::current_dir().unwrap();
let _ = fs::remove_file(get_blocklist_path(config_dir));
Blocklist::create(config_dir)
}

#[test]
fn test_add() {
let mut blocklist = create_blocklist();
let number: u128 = 42;
let another_number: u128 = 1337;

blocklist.add(number);
assert_eq!(blocklist.get().len(), 1);
assert_eq!(blocklist.get().first().unwrap().clone(), number);

blocklist.add(another_number);
assert_eq!(blocklist.get().len(), 2);
assert_eq!(blocklist.get().get(0).unwrap().clone(), number);
assert_eq!(blocklist.get().get(1).unwrap().clone(), another_number);
}

#[test]
fn test_clean() {
let mut blocklist = create_blocklist();

blocklist.add(21);
blocklist.add(42);
blocklist.add(63);
blocklist.add(84);
blocklist.add(105);

assert_eq!(blocklist.get().len(), 5);

blocklist.clean(63);
assert_eq!(blocklist.get().len(), 2);
assert_eq!(blocklist.get().get(0).unwrap().clone(), 84);
assert_eq!(blocklist.get().get(1).unwrap().clone(), 105);
}

#[test]
fn test_save() {
let mut blocklist = create_blocklist();

blocklist.add(42);
blocklist.save();
blocklist.add(1337);

let other_blocklist = Blocklist::create(&env::current_dir().unwrap());
assert_eq!(other_blocklist.get().len(), 1);
assert_eq!(other_blocklist.get().first().unwrap().clone(), 42);
}

#[test]
fn test_is_blocked() {
let mut blocklist = create_blocklist();

blocklist.add(42);

assert!(blocklist.is_blocked(42));
assert!(!blocklist.is_blocked(1337));
}
}
41 changes: 33 additions & 8 deletions tests/client_send_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ mod tests {
use rand::distributions::{Alphanumeric, DistString};

use ruroco::client::gen;
use ruroco::common::time;

use super::*;

Expand All @@ -22,7 +23,13 @@ mod tests {
fn test_send_no_such_file() {
let pem_file_name = gen_file_name(".pem");
let pem_path = PathBuf::from(&pem_file_name);
let result = send(pem_path, String::from("127.0.0.1:1234"), String::from("default"), 5);
let result = send(
pem_path,
String::from("127.0.0.1:1234"),
String::from("default"),
5,
time().unwrap(),
);

assert_eq!(
result.unwrap_err().to_string(),
Expand All @@ -36,7 +43,13 @@ mod tests {
File::create(&pem_file_name).unwrap();

let pem_path = PathBuf::from(&pem_file_name);
let result = send(pem_path, String::from("127.0.0.1:1234"), String::from("default"), 5);
let result = send(
pem_path,
String::from("127.0.0.1:1234"),
String::from("default"),
5,
time().unwrap(),
);

let _ = fs::remove_file(&pem_file_name);

Expand All @@ -56,7 +69,8 @@ mod tests {
gen(private_pem_path.clone(), public_pem_path, 1024).unwrap();

let address = String::from("127.0.0.1:asd");
let result = send(private_pem_path, address.clone(), String::from("default"), 5);
let result =
send(private_pem_path, address.clone(), String::from("default"), 5, time().unwrap());

let _ = fs::remove_file(&private_file);
let _ = fs::remove_file(&public_file);
Expand All @@ -77,7 +91,8 @@ mod tests {
gen(private_pem_path.clone(), public_pem_path, 1024).unwrap();

let address = String::from("999.999.999.999:9999");
let result = send(private_pem_path, address.clone(), String::from("default"), 5);
let result =
send(private_pem_path, address.clone(), String::from("default"), 5, time().unwrap());

let _ = fs::remove_file(&private_file);
let _ = fs::remove_file(&public_file);
Expand All @@ -100,8 +115,13 @@ mod tests {
let public_pem_path = PathBuf::from(&public_file);
gen(private_pem_path.clone(), public_pem_path, 1024).unwrap();

let result =
send(private_pem_path, String::from("127.0.0.1:1234"), "default".repeat(24), 5);
let result = send(
private_pem_path,
String::from("127.0.0.1:1234"),
"default".repeat(24),
5,
time().unwrap(),
);

let _ = fs::remove_file(&private_file);
let _ = fs::remove_file(&public_file);
Expand All @@ -121,8 +141,13 @@ mod tests {
let public_pem_path = PathBuf::from(&public_file);
gen(private_pem_path.clone(), public_pem_path, 1024).unwrap();

let result =
send(private_pem_path, String::from("127.0.0.1:1234"), String::from("default"), 5);
let result = send(
private_pem_path,
String::from("127.0.0.1:1234"),
String::from("default"),
5,
time().unwrap(),
);

let _ = fs::remove_file(&private_file);
let _ = fs::remove_file(&public_file);
Expand Down
2 changes: 1 addition & 1 deletion tests/commander_test.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
#[cfg(test)]
mod tests {
use std::{fs, thread};
use std::collections::HashMap;
use std::path::{Path, PathBuf};
use std::time::Duration;
use std::{fs, thread};

use rand::distributions::{Alphanumeric, DistString};

Expand Down
Loading

0 comments on commit a04a870

Please sign in to comment.