[gen] Real words: memorable passwords from EFF words a US English dictionary knows; strict-site symbols ASCII only (v0.2.1)
This commit is contained in:
@@ -1,8 +1,8 @@
|
||||
//! The mandatory generator tests (CLAUDE.md).
|
||||
|
||||
use custos_gen::{
|
||||
ClassRule, EFF_WORDS, MemorableOptions, NicknameOptions, RandomOptions, Separator, StrictOptions, WordKind,
|
||||
memorable, memorable_entropy, nickname, nickname_parts, normalize_name, pin, random, strict,
|
||||
ClassRule, MAX_LENGTH, MEMORABLE_WORDS, MemorableOptions, NicknameOptions, RandomOptions, Separator, StrictOptions,
|
||||
WordKind, memorable, memorable_entropy, nickname, nickname_parts, normalize_name, pin, random, strict,
|
||||
};
|
||||
use regex::Regex;
|
||||
use std::collections::HashSet;
|
||||
@@ -10,11 +10,24 @@ use std::collections::HashSet;
|
||||
const SAMPLES: usize = 10_000;
|
||||
|
||||
#[test]
|
||||
fn eff_list_is_complete_and_plain() {
|
||||
assert_eq!(EFF_WORDS.len(), 7_776);
|
||||
let unique: HashSet<_> = EFF_WORDS.iter().collect();
|
||||
assert_eq!(unique.len(), 7_776, "a word repeats after removing hyphens");
|
||||
assert!(EFF_WORDS.iter().all(|w| !w.is_empty() && w.bytes().all(|b| b.is_ascii_lowercase())));
|
||||
fn memorable_words_are_real_english_words() {
|
||||
assert_eq!(MEMORABLE_WORDS.len(), 7_322);
|
||||
let unique: HashSet<_> = MEMORABLE_WORDS.iter().collect();
|
||||
assert_eq!(unique.len(), 7_322);
|
||||
assert!(MEMORABLE_WORDS.iter().all(|w| w.len() >= 3 && w.bytes().all(|b| b.is_ascii_lowercase())));
|
||||
// Every word comes from the EFF list...
|
||||
let eff: HashSet<&str> = include_str!("../wordlists/eff_large_wordlist.txt")
|
||||
.lines()
|
||||
.filter_map(|l| l.split('\t').nth(1))
|
||||
.map(str::trim)
|
||||
.collect();
|
||||
assert!(MEMORABLE_WORDS.iter().all(|w| eff.contains(w)));
|
||||
// ...minus brand names, names, coinages and hyphenated words that no dictionary lists.
|
||||
for not_a_word in ["walmart", "iphone", "skype", "myspace", "monday", "december", "pranker", "finlike", "felt-tip"]
|
||||
{
|
||||
assert!(eff.contains(not_a_word), "{not_a_word} is not in the EFF list any more");
|
||||
assert!(!MEMORABLE_WORDS.contains(¬_a_word), "{not_a_word}");
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -57,7 +70,7 @@ fn memorable_shape_at_every_slider_value_and_option() {
|
||||
|
||||
#[test]
|
||||
fn memorable_entropy_counts_only_random_choices() {
|
||||
let per_word = 7_776f64.log2() + 10f64.log2();
|
||||
let per_word = 7_322f64.log2() + 10f64.log2();
|
||||
for words in 3..=10u8 {
|
||||
let o = MemorableOptions { words, ..Default::default() };
|
||||
let e = memorable_entropy(&o);
|
||||
@@ -70,7 +83,7 @@ fn memorable_entropy_counts_only_random_choices() {
|
||||
let four = memorable_entropy(&MemorableOptions::default());
|
||||
assert!((64.0..66.0).contains(&four), "4 words with digits is about 65 bits: {four}");
|
||||
let no_digits = MemorableOptions { digit_after_word: false, ..Default::default() };
|
||||
assert!((memorable_entropy(&no_digits) - 4.0 * 7_776f64.log2()).abs() < 1e-9);
|
||||
assert!((memorable_entropy(&no_digits) - 4.0 * 7_322f64.log2()).abs() < 1e-9);
|
||||
// A random-digit separator is a random choice per gap.
|
||||
let digit_sep = MemorableOptions { separator: Separator::Digit, ..Default::default() };
|
||||
assert!((memorable_entropy(&digit_sep) - (four + 3.0 * 10f64.log2())).abs() < 1e-9);
|
||||
@@ -234,6 +247,24 @@ fn random_and_pin() {
|
||||
assert!((p.entropy_bits - 6.0 * 10f64.log2()).abs() < 1e-9);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn random_and_strict_use_only_english_letters_digits_and_ascii_symbols() {
|
||||
let ok = |c: char| c.is_ascii_alphanumeric() || c.is_ascii_punctuation();
|
||||
let all = RandomOptions { length: MAX_LENGTH, ..RandomOptions::default() };
|
||||
for _ in 0..SAMPLES / 10 {
|
||||
let s = random(&all).unwrap().value.expose().to_owned();
|
||||
assert!(s.chars().all(ok), "{s}");
|
||||
}
|
||||
// Symbols the site lists outside ASCII, letters, digits and spaces are ignored.
|
||||
let o = StrictOptions { length: 32, allowed_symbols: "€§ž é1!".into(), ..StrictOptions::default() };
|
||||
for _ in 0..SAMPLES / 10 {
|
||||
let s = strict(&o).unwrap().value.expose().to_owned();
|
||||
assert!(s.chars().all(|c| c.is_ascii_alphanumeric() || c == '!'), "{s}");
|
||||
}
|
||||
let none_left = StrictOptions { allowed_symbols: "€§".into(), ..StrictOptions::default() };
|
||||
assert!(strict(&none_left).is_err(), "required symbols, but none of the listed ones is ASCII");
|
||||
}
|
||||
|
||||
/// Nicknames (≈ 30 000 combinations by design: they are names, not secrets) and short PINs are
|
||||
/// excluded: their spaces are small enough that repeats in 10 000 draws are expected.
|
||||
#[test]
|
||||
|
||||
Reference in New Issue
Block a user