-
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.
dev: use clock in minimal connection cookie implementation
- Loading branch information
Showing
9 changed files
with
330 additions
and
49 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
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,84 @@ | ||
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); | ||
} | ||
|
||
mod detail { | ||
|
||
#[cfg(not(test))] | ||
pub use INSTANCE_SEED as DEFAULT_SEED; | ||
|
||
pub use crate::static_keys::static_seeds::INSTANCE_SEED; | ||
#[cfg(test)] | ||
pub 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); | ||
} | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
|
||
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()) | ||
} | ||
} | ||
} | ||
} |
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; |
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
Oops, something went wrong.