Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: a beginning to dst #3

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions crates/keystore/src/api/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ impl MasterKeyManagementService for API {
) -> Result<tonic::Response<CreateMasterKeyResponse>, tonic::Status> {
let key = self
.keystore
.create_key(request.get_ref().keyring_name.clone(), request.get_ref().master_key_id.clone())
.create_key(request.get_ref().keyring_name.as_str(), request.get_ref().master_key_id.as_str())
.await;

let reply = CreateMasterKeyResponse {
Expand Down Expand Up @@ -86,9 +86,9 @@ impl MasterKeyManagementService for API {
request: tonic::Request<EncryptRequest>,
) -> Result<tonic::Response<EncryptResponse>, tonic::Status> {
let encrypted_value = self.keystore.encrypt(
request.get_ref().keyring_name.clone(),
request.get_ref().master_key_id.clone(),
request.get_ref().plaintext.clone().to_vec(),
request.get_ref().keyring_name.as_str(),
request.get_ref().master_key_id.as_str(),
request.get_ref().plaintext.to_vec(),
).await;

let reply = crate::valv::keystore::v1::EncryptResponse {
Expand All @@ -104,9 +104,9 @@ impl MasterKeyManagementService for API {
request: tonic::Request<DecryptRequest>,
) -> Result<tonic::Response<DecryptResponse>, tonic::Status> {
let decrypted_result = self.keystore.decrypt(
request.get_ref().keyring_name.clone(),
request.get_ref().master_key_id.clone(),
request.get_ref().ciphertext.clone().to_vec(),
request.get_ref().keyring_name.as_str(),
request.get_ref().master_key_id.as_str(),
request.get_ref().ciphertext.to_vec(),
).await;
match decrypted_result {
Ok(decrypted_value) => {
Expand Down
93 changes: 93 additions & 0 deletions crates/keystore/src/clock.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
use std::time::{Duration, SystemTime};
use std::sync::Mutex;

pub struct MockClock {
current_time: Mutex<SystemTime>,
}

impl MockClock {
pub fn new(initial_time: SystemTime) -> Self {
MockClock {
current_time: Mutex::new(initial_time),
}
}

fn now(&self) -> SystemTime {
*self.current_time.lock().unwrap()
}

fn advance(&self, duration: Duration) {
let mut current_time = self.current_time.lock().unwrap();
*current_time += duration;
}
}

pub trait Clock: Send + Sync {
fn now(&self) -> SystemTime;
}

impl Clock for MockClock {
fn now(&self) -> SystemTime {
self.now()
}
}

impl Clock for SystemTime {
fn now(&self) -> SystemTime {
SystemTime::now()
}
}

#[cfg(test)]
mod tests {
use super::*;
use std::time::{Duration, SystemTime};

#[test]
fn test_mock_clock_new() {
let initial_time = SystemTime::UNIX_EPOCH + Duration::from_secs(1000);
let clock = MockClock::new(initial_time);
assert_eq!(clock.now(), initial_time);
}

#[test]
fn test_mock_clock_advance() {
let initial_time = SystemTime::UNIX_EPOCH;
let clock = MockClock::new(initial_time);

clock.advance(Duration::from_secs(60));
assert_eq!(clock.now(), initial_time + Duration::from_secs(60));

clock.advance(Duration::from_secs(30));
assert_eq!(clock.now(), initial_time + Duration::from_secs(90));
}

#[test]
fn test_mock_clock_multiple_advances() {
let initial_time = SystemTime::UNIX_EPOCH;
let clock = MockClock::new(initial_time);

for i in 1..=5 {
clock.advance(Duration::from_secs(10));
assert_eq!(clock.now(), initial_time + Duration::from_secs(i * 10));
}
}

#[test]
fn test_mock_clock_trait_implementation() {
let initial_time = SystemTime::UNIX_EPOCH + Duration::from_secs(500);
let clock: Box<dyn Clock> = Box::new(MockClock::new(initial_time));

assert_eq!(clock.now(), initial_time);
}

#[test]
fn test_system_time_clock_trait_implementation() {
let clock: Box<dyn Clock> = Box::new(SystemTime::UNIX_EPOCH);

// This test is non-deterministic, but we can check if the time is recent
let now = clock.now();
let elapsed = now.duration_since(SystemTime::UNIX_EPOCH).unwrap();
assert!(elapsed.as_secs() > 0);
}
}
4 changes: 2 additions & 2 deletions crates/keystore/src/cmd/main.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::sync::Arc;
use std::{sync::Arc, time::SystemTime};

use clap::Parser;
use keystore::{
Expand All @@ -21,7 +21,7 @@ struct Cli {
async fn main() {
let args = Cli::parse();

let mut keystore = Keystore::new().await;
let mut keystore = Keystore::new(Arc::new(SystemTime::now())).await;

let master_key = args.master_key.clone().expose_secret().clone().into_bytes()[..32]
.try_into()
Expand Down
Loading
Loading