Skip to content

Commit ee3f80d

Browse files
Merge pull request #238 from brotskydotcom/issue-236
Switch to using fastrand for testing.
2 parents f1b7ec1 + 766c8bc commit ee3f80d

File tree

5 files changed

+44
-30
lines changed

5 files changed

+44
-30
lines changed

CHANGELOG.md

+7
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
1+
## Version 3.6.2
2+
- Have docs.rs build docs for all modules on all platforms (thanks to @unkcpz - see #235).
3+
- Switch to `fastrand` for tests (see #237).
4+
5+
## Version 3.6.1
6+
- Updated dependencies; no code changes.
7+
18
## Version 3.6.0
29
- Add combination keystore of keyutils and secret service (thanks to @soywod).
310

Cargo.toml

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ keywords = ["password", "credential", "keychain", "keyring", "cross-platform"]
66
license = "MIT OR Apache-2.0"
77
name = "keyring"
88
repository = "https://github.com/hwchen/keyring-rs.git"
9-
version = "3.6.1"
9+
version = "3.6.2"
1010
rust-version = "1.75"
1111
edition = "2021"
1212
exclude = [".github/"]
@@ -71,7 +71,7 @@ base64 = "0.22"
7171
clap = { version = "4", features = ["derive", "wrap_help"] }
7272
doc-comment = "0.3"
7373
env_logger = "0.11.5"
74-
rand = "0.8"
74+
fastrand = "2"
7575
rpassword = "7"
7676
rprompt = "2"
7777
whoami = "1.5"

src/lib.rs

+11-14
Original file line numberDiff line numberDiff line change
@@ -525,7 +525,6 @@ doc_comment::doctest!("../README.md", readme);
525525
#[allow(dead_code)]
526526
mod tests {
527527
use super::{credential::CredentialApi, Entry, Error, Result};
528-
use rand::Rng;
529528
use std::collections::HashMap;
530529

531530
/// Create a platform-specific credential given the constructor, service, and user
@@ -612,22 +611,21 @@ mod tests {
612611
/// the conflicts, and then do any needed cleanup once everything
613612
/// is working correctly. So we export this function for tests to use.
614613
pub fn generate_random_string_of_len(len: usize) -> String {
615-
// from the Rust Cookbook:
616-
// https://rust-lang-nursery.github.io/rust-cookbook/algorithms/randomness.html
617-
#[allow(unused_imports)]
618-
use rand::Rng;
619-
use rand::{distributions::Alphanumeric, thread_rng};
620-
thread_rng()
621-
.sample_iter(&Alphanumeric)
622-
.take(len)
623-
.map(char::from)
624-
.collect()
614+
use fastrand;
615+
use std::iter::repeat_with;
616+
repeat_with(fastrand::alphanumeric).take(len).collect()
625617
}
626618

627619
pub fn generate_random_string() -> String {
628620
generate_random_string_of_len(30)
629621
}
630622

623+
fn generate_random_bytes_of_len(len: usize) -> Vec<u8> {
624+
use fastrand;
625+
use std::iter::repeat_with;
626+
repeat_with(|| fastrand::u8(..)).take(len).collect()
627+
}
628+
631629
pub fn test_empty_service_and_user<F>(f: F)
632630
where
633631
F: Fn(&str, &str) -> Entry,
@@ -684,9 +682,8 @@ mod tests {
684682
{
685683
let name = generate_random_string();
686684
let entry = f(&name, &name);
687-
let mut secret: [u8; 16] = [0; 16];
688-
rand::rngs::OsRng.fill(&mut secret);
689-
test_round_trip_secret("non-ascii password", &entry, &secret);
685+
let secret = generate_random_bytes_of_len(24);
686+
test_round_trip_secret("non-ascii password", &entry, secret.as_slice());
690687
}
691688

692689
pub fn test_update<F>(f: F)

tests/basic.rs

+7-6
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use common::{generate_random_string, init_logger};
1+
use common::{generate_random_bytes_of_len, generate_random_string, init_logger};
22
use keyring::{Entry, Error};
33

44
mod common;
@@ -90,15 +90,16 @@ fn test_round_trip_non_ascii_password() {
9090
fn test_round_trip_random_secret() {
9191
init_logger();
9292

93-
use rand::{rngs::OsRng, Rng};
9493
let name = generate_random_string();
9594
let entry = Entry::new(&name, &name).expect("Can't create entry");
96-
let mut secret: [u8; 16] = [0; 16];
97-
OsRng.fill(&mut secret);
98-
entry.set_secret(&secret).expect("Can't set random secret");
95+
let secret = generate_random_bytes_of_len(24);
96+
entry
97+
.set_secret(secret.as_slice())
98+
.expect("Can't set random secret");
9999
let stored_secret = entry.get_secret().expect("Can't get random secret");
100100
assert_eq!(
101-
&stored_secret, &secret,
101+
&stored_secret,
102+
secret.as_slice(),
102103
"Retrieved and set random secrets don't match"
103104
);
104105
entry

tests/common/mod.rs

+17-8
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,33 @@
1+
#![allow(dead_code)] // not all of these utilities are used by all tests
2+
13
/// When tests fail, they leave keys behind, and those keys
24
/// have to be cleaned up before the tests can be run again
35
/// in order to avoid bad results. So it's a lot easier just
46
/// to have tests use a random string for key names to avoid
57
/// the conflicts, and then do any needed cleanup once everything
68
/// is working correctly. So tests make keys with these functions.
9+
/// When tests fail, they leave keys behind, and those keys
10+
/// have to be cleaned up before the tests can be run again
11+
/// in order to avoid bad results. So it's a lot easier just
12+
/// to have tests use a random string for key names to avoid
13+
/// the conflicts, and then do any needed cleanup once everything
14+
/// is working correctly. So we export this function for tests to use.
715
pub fn generate_random_string_of_len(len: usize) -> String {
8-
// from the Rust Cookbook:
9-
// https://rust-lang-nursery.github.io/rust-cookbook/algorithms/randomness.html
10-
use rand::{distributions::Alphanumeric, thread_rng, Rng};
11-
thread_rng()
12-
.sample_iter(&Alphanumeric)
13-
.take(len)
14-
.map(char::from)
15-
.collect()
16+
use fastrand;
17+
use std::iter::repeat_with;
18+
repeat_with(fastrand::alphanumeric).take(len).collect()
1619
}
1720

1821
pub fn generate_random_string() -> String {
1922
generate_random_string_of_len(30)
2023
}
2124

25+
pub fn generate_random_bytes_of_len(len: usize) -> Vec<u8> {
26+
use fastrand;
27+
use std::iter::repeat_with;
28+
repeat_with(|| fastrand::u8(..)).take(len).collect()
29+
}
30+
2231
pub fn init_logger() {
2332
let _ = env_logger::builder().is_test(true).try_init();
2433
}

0 commit comments

Comments
 (0)