Skip to content

Commit e2553b8

Browse files
committed
refactor: extract mod into file
1 parent 2d99866 commit e2553b8

4 files changed

Lines changed: 105 additions & 109 deletions

File tree

src/shared/crypto.rs

Lines changed: 0 additions & 109 deletions
This file was deleted.
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
use rand::rngs::ThreadRng;
2+
use rand::Rng;
3+
4+
pub type Seed = [u8; 32];
5+
6+
lazy_static! {
7+
pub static ref RANDOM_SEED: Seed = Rng::gen(&mut ThreadRng::default());
8+
}

src/shared/crypto/keys.rs

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
pub mod seeds {
2+
use self::detail::CURRENT_SEED;
3+
use crate::shared::crypto::ephemeral_instance_keys::{Seed, RANDOM_SEED};
4+
5+
pub trait Keeper {
6+
type Seed: Sized + Default + AsMut<[u8]>;
7+
fn get_seed() -> &'static Self::Seed;
8+
}
9+
10+
pub struct Instance;
11+
pub struct Current;
12+
13+
impl Keeper for Instance {
14+
type Seed = Seed;
15+
16+
fn get_seed() -> &'static Self::Seed {
17+
&RANDOM_SEED
18+
}
19+
}
20+
21+
impl Keeper for Current {
22+
type Seed = Seed;
23+
24+
#[allow(clippy::needless_borrow)]
25+
fn get_seed() -> &'static Self::Seed {
26+
&CURRENT_SEED
27+
}
28+
}
29+
30+
#[cfg(test)]
31+
mod tests {
32+
use super::detail::ZEROED_TEST_SEED;
33+
use super::{Current, Instance, Keeper};
34+
use crate::shared::crypto::ephemeral_instance_keys::Seed;
35+
36+
pub struct ZeroedTestSeed;
37+
38+
impl Keeper for ZeroedTestSeed {
39+
type Seed = Seed;
40+
41+
#[allow(clippy::needless_borrow)]
42+
fn get_seed() -> &'static Self::Seed {
43+
&ZEROED_TEST_SEED
44+
}
45+
}
46+
47+
#[test]
48+
fn the_default_seed_and_the_zeroed_seed_should_be_the_same_when_testing() {
49+
assert_eq!(Current::get_seed(), ZeroedTestSeed::get_seed());
50+
}
51+
52+
#[test]
53+
fn the_default_seed_and_the_instance_seed_should_be_different_when_testing() {
54+
assert_ne!(Current::get_seed(), Instance::get_seed());
55+
}
56+
}
57+
58+
mod detail {
59+
use crate::shared::crypto::ephemeral_instance_keys::Seed;
60+
61+
#[allow(dead_code)]
62+
pub const ZEROED_TEST_SEED: &Seed = &[0u8; 32];
63+
64+
#[cfg(test)]
65+
pub use ZEROED_TEST_SEED as CURRENT_SEED;
66+
67+
#[cfg(not(test))]
68+
pub use crate::shared::crypto::ephemeral_instance_keys::RANDOM_SEED as CURRENT_SEED;
69+
70+
#[cfg(test)]
71+
mod tests {
72+
use std::convert::TryInto;
73+
74+
use crate::shared::crypto::ephemeral_instance_keys::RANDOM_SEED;
75+
use crate::shared::crypto::keys::seeds::detail::ZEROED_TEST_SEED;
76+
use crate::shared::crypto::keys::seeds::CURRENT_SEED;
77+
78+
#[test]
79+
fn it_should_have_a_zero_test_seed() {
80+
assert_eq!(*ZEROED_TEST_SEED, [0u8; 32]);
81+
}
82+
83+
#[test]
84+
fn it_should_default_to_zeroed_seed_when_testing() {
85+
assert_eq!(*CURRENT_SEED, *ZEROED_TEST_SEED);
86+
}
87+
88+
#[test]
89+
fn it_should_have_a_large_random_seed() {
90+
assert!(u128::from_ne_bytes((*RANDOM_SEED)[..16].try_into().unwrap()) > u128::from(u64::MAX));
91+
assert!(u128::from_ne_bytes((*RANDOM_SEED)[16..].try_into().unwrap()) > u128::from(u64::MAX));
92+
}
93+
}
94+
}
95+
}

src/shared/crypto/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
pub mod ephemeral_instance_keys;
2+
pub mod keys;

0 commit comments

Comments
 (0)