utils (database/crypto): update comments and tests
This commit is contained in:
parent
f02bb0e358
commit
58d99806d4
|
@ -1,11 +1,9 @@
|
|||
pub use hex::encode;
|
||||
use sha1::Sha1;
|
||||
use sha2::{Digest, Sha256, Sha512};
|
||||
|
||||
use super::{Error, Result};
|
||||
|
||||
/// create a cryptographic hash from a string (sha1, sha256, sha512)
|
||||
///
|
||||
/// ```
|
||||
/// use homedisk_utils::crypto::{CryptographicHash, encode};
|
||||
///
|
||||
|
@ -24,6 +22,7 @@ pub enum CryptographicHash {
|
|||
}
|
||||
|
||||
impl CryptographicHash {
|
||||
/// Create a new hasher
|
||||
pub fn new(algo: &str) -> Result<Self> {
|
||||
match algo {
|
||||
"SHA-1" | "SHA1" | "Sha1" | "sha1" => Ok(Self::Sha1(Sha1::new())),
|
||||
|
@ -33,6 +32,7 @@ impl CryptographicHash {
|
|||
}
|
||||
}
|
||||
|
||||
/// Set a value for hasher
|
||||
pub fn update(&mut self, input: &[u8]) {
|
||||
match self {
|
||||
Self::Sha1(sha1) => sha1.update(input),
|
||||
|
@ -41,6 +41,7 @@ impl CryptographicHash {
|
|||
}
|
||||
}
|
||||
|
||||
/// Compute hash
|
||||
pub fn finalize(&mut self) -> Vec<u8> {
|
||||
match self {
|
||||
Self::Sha1(sha1) => sha1.finalize_reset().to_vec(),
|
||||
|
@ -49,6 +50,7 @@ impl CryptographicHash {
|
|||
}
|
||||
}
|
||||
|
||||
/// Streamline the hash calculation to a single function
|
||||
pub fn hash(algo: &str, input: &[u8]) -> Result<Vec<u8>> {
|
||||
let mut hasher = Self::new(algo)?;
|
||||
|
||||
|
@ -98,6 +100,16 @@ mod tests {
|
|||
)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn hash_fn() {
|
||||
let hash = CryptographicHash::hash("SHA-512", b"test sha512 hash").unwrap();
|
||||
|
||||
assert_eq!(
|
||||
encode(hash),
|
||||
"b43b4d7178014c92f55be828d66c9f98211fc67b385f7790a5b4b2fcb89fe1831645b5a4c17f3f7f11d8f34d2800a77a2b8faa5a0fb9d6b8f7befbc29a9ce795".to_string()
|
||||
)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn unknown_algorithm() {
|
||||
let algo = "unknow_algo";
|
||||
|
|
|
@ -1,4 +1,5 @@
|
|||
mod error;
|
||||
mod hash;
|
||||
|
||||
pub use hex::encode;
|
||||
pub use {error::*, hash::*};
|
||||
|
|
|
@ -10,13 +10,10 @@ pub struct Database {
|
|||
|
||||
impl Database {
|
||||
/// Open SQLite Database file
|
||||
/// ```
|
||||
/// ```ignore
|
||||
/// use homedisk_utils::database::Database;
|
||||
///
|
||||
/// #[tokio::main]
|
||||
/// async fn main() {
|
||||
/// Database::open("sqlite::memory:").await.unwrap();
|
||||
/// }
|
||||
/// Database::open("sqlite::memory:").await?;
|
||||
/// ```
|
||||
pub async fn open(path: &str) -> Result<Self, Error> {
|
||||
debug!("opening SQLite database");
|
||||
|
@ -27,21 +24,11 @@ impl Database {
|
|||
}
|
||||
|
||||
/// Create new User
|
||||
/// ```
|
||||
/// use std::fs;
|
||||
///
|
||||
/// use sqlx::Executor;
|
||||
/// ```ignore
|
||||
/// use homedisk_utils::database::{Database, User};
|
||||
///
|
||||
/// #[tokio::main]
|
||||
/// async fn main() {
|
||||
/// let db = Database::open("sqlite::memory:").await.unwrap();
|
||||
///
|
||||
/// db.conn.execute(sqlx::query(&fs::read_to_string("../tables.sql").unwrap())).await.unwrap();
|
||||
///
|
||||
/// let user = User::new("medzik", "SuperSecretPassword123");
|
||||
/// db.create_user(&user).await.unwrap();
|
||||
/// }
|
||||
/// let user = User::new("medzik", "SuperSecretPassword123");
|
||||
/// db.create_user(&user).await?;
|
||||
/// ```
|
||||
pub async fn create_user(&self, user: &user::User) -> Result<SqliteQueryResult, Error> {
|
||||
debug!("creating user - {}", user.username);
|
||||
|
@ -54,3 +41,38 @@ impl Database {
|
|||
Ok(self.conn.execute(query).await?)
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use std::fs;
|
||||
|
||||
use sqlx::Executor;
|
||||
|
||||
use crate::database::{Database, User};
|
||||
|
||||
async fn open_db() -> Database {
|
||||
Database::open("sqlite::memory:").await.expect("open db")
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn open_db_in_memory() {
|
||||
open_db().await;
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn create_user() {
|
||||
let db = open_db().await;
|
||||
|
||||
// create user table
|
||||
db.conn
|
||||
.execute(sqlx::query(
|
||||
&fs::read_to_string("../tables.sql").expect("open tables file"),
|
||||
))
|
||||
.await
|
||||
.expect("create tables");
|
||||
|
||||
// create new user
|
||||
let user = User::new("medzik", "SuperSecretPassword123");
|
||||
db.create_user(&user).await.expect("create user");
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue