-
Notifications
You must be signed in to change notification settings - Fork 41
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branches 'period-clock' and 'crypto-static-keys' into connectio…
…n-cookie
- Loading branch information
Showing
3 changed files
with
101 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
pub mod keys { | ||
|
||
pub mod seeds { | ||
use crate::static_keys::static_seeds::{SeedSize, INSTANCE_SEED}; | ||
|
||
pub trait Seed { | ||
type Seed: Sized + Default + AsMut<[u8]>; | ||
fn seed() -> &'static Self::Seed; | ||
} | ||
|
||
pub struct InstanceSeed; | ||
pub struct DefaultSeed; | ||
|
||
impl Seed for InstanceSeed { | ||
type Seed = SeedSize; | ||
fn seed() -> &'static Self::Seed { | ||
&*INSTANCE_SEED | ||
} | ||
} | ||
|
||
impl Seed for DefaultSeed { | ||
type Seed = SeedSize; | ||
fn seed() -> &'static Self::Seed { | ||
&*self::detail::DEFAULT_SEED | ||
} | ||
} | ||
|
||
pub fn initialize_default_seed() { | ||
lazy_static::initialize(&self::detail::DEFAULT_SEED); | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::{DefaultSeed, InstanceSeed, Seed}; | ||
use crate::static_keys::static_seeds::SeedSize; | ||
|
||
pub struct ZeroedTestSeed; | ||
|
||
impl Seed for ZeroedTestSeed { | ||
type Seed = SeedSize; | ||
fn seed() -> &'static Self::Seed { | ||
&*crate::static_keys::static_seeds::ZEROED_TEST_SEED | ||
} | ||
} | ||
|
||
#[test] | ||
fn the_default_seed_and_the_zeroed_seed_should_be_the_same_when_testing() { | ||
assert_eq!(DefaultSeed::seed(), ZeroedTestSeed::seed()) | ||
} | ||
|
||
#[test] | ||
fn the_default_seed_and_the_instance_seed_should_be_different_when_testing() { | ||
assert_ne!(DefaultSeed::seed(), InstanceSeed::seed()) | ||
} | ||
} | ||
|
||
mod detail { | ||
#[cfg(not(test))] | ||
pub(super) use crate::static_keys::static_seeds::INSTANCE_SEED as DEFAULT_SEED; | ||
#[cfg(test)] | ||
pub(super) use crate::static_keys::static_seeds::ZEROED_TEST_SEED as DEFAULT_SEED; | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use std::convert::TryInto; | ||
|
||
use crate::static_keys::static_seeds::{INSTANCE_SEED, ZEROED_TEST_SEED}; | ||
|
||
#[test] | ||
fn it_should_have_a_zero_test_seed() { | ||
assert_eq!(*ZEROED_TEST_SEED, [0u8; 32]) | ||
} | ||
|
||
#[test] | ||
fn it_should_have_a_large_random_seed() { | ||
assert!(u128::from_ne_bytes((*INSTANCE_SEED)[..16].try_into().unwrap()) > u64::MAX as u128); | ||
assert!(u128::from_ne_bytes((*INSTANCE_SEED)[16..].try_into().unwrap()) > u64::MAX as u128); | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
pub mod clock; | ||
pub mod common; | ||
pub mod crypto; | ||
pub mod utils; |