Skip to content

Commit

Permalink
update to use deterministic rng
Browse files Browse the repository at this point in the history
  • Loading branch information
aumetra committed Jan 15, 2025
1 parent 2ffbf43 commit 134305f
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 4 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions lib/komainu/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ headers.workspace = true
http-serde.workspace = true
insta.workspace = true
rand.workspace = true
rand_xorshift.workspace = true
rstest.workspace = true
serde_test.workspace = true

Expand Down
17 changes: 14 additions & 3 deletions lib/komainu/tests/fixtures/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,15 @@ use komainu::{
code_grant::AuthorizerExtractor,
flow::{SuccessTokenResponse, TokenType},
};
use rand::distributions::{Alphanumeric, DistString};
use rand::{
distributions::{Alphanumeric, DistString},
SeedableRng,
};
use rand_xorshift::XorShiftRng;
use std::{
borrow::Cow,
collections::HashMap,
sync::{Arc, Mutex},
sync::{Arc, LazyLock, Mutex},
};

pub mod auth_flow;
Expand All @@ -17,9 +21,16 @@ pub mod refresh_flow;

pub type AuthorizationStorage = Arc<Mutex<HashMap<String, komainu::Authorization<'static>>>>;

#[allow(clippy::unreadable_literal)]
const RNG_SEED: u64 = 0xBADD1E;

static SEEDED_RNG: LazyLock<Mutex<XorShiftRng>> =
LazyLock::new(|| Mutex::new(XorShiftRng::seed_from_u64(RNG_SEED)));

#[inline]
fn generate_secret() -> String {
Alphanumeric.sample_string(&mut rand::thread_rng(), 16)
let mut guard = SEEDED_RNG.lock().unwrap();
Alphanumeric.sample_string(&mut *guard, 16)
}

#[derive(Clone)]
Expand Down
2 changes: 1 addition & 1 deletion lib/komainu/tests/snapshots/flows__success.snap
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ expression: "SerdeResponse::from(acceptor.into_response())"
{
"body": null,
"headers": {
"location": "http://client_1.example/?code=sn7A6s3FVFsptwK6"
"location": "http://client_1.example/?code=YSkam0dTW2DsMrIE"
},
"status": 302
}

0 comments on commit 134305f

Please sign in to comment.