From 58d99806d409ed05760e1cc64027305e039f26d7 Mon Sep 17 00:00:00 2001 From: MedzikUser Date: Tue, 19 Apr 2022 21:08:13 +0200 Subject: [PATCH] utils (database/crypto): update comments and tests --- utils/src/crypto/hash.rs | 16 ++++++++-- utils/src/crypto/mod.rs | 1 + utils/src/database/sqlite.rs | 58 +++++++++++++++++++++++++----------- 3 files changed, 55 insertions(+), 20 deletions(-) diff --git a/utils/src/crypto/hash.rs b/utils/src/crypto/hash.rs index efc044f..c2b829d 100644 --- a/utils/src/crypto/hash.rs +++ b/utils/src/crypto/hash.rs @@ -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 { 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 { 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> { 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"; diff --git a/utils/src/crypto/mod.rs b/utils/src/crypto/mod.rs index 03931e6..e8ca832 100644 --- a/utils/src/crypto/mod.rs +++ b/utils/src/crypto/mod.rs @@ -1,4 +1,5 @@ mod error; mod hash; +pub use hex::encode; pub use {error::*, hash::*}; diff --git a/utils/src/database/sqlite.rs b/utils/src/database/sqlite.rs index de03825..f79f609 100644 --- a/utils/src/database/sqlite.rs +++ b/utils/src/database/sqlite.rs @@ -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 { 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 { 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"); + } +}