"""The phrase: shape, entropy, and the rejection of anything unusable.""" from __future__ import annotations import re import pytest from hhstream_live import phrase SHAPE = re.compile(r"^[a-z]+(-[a-z]+){3}-[a-z0-9]{5}$") WORD = re.compile(r"^[a-z]+$") def test_generated_phrase_has_the_agreed_shape(): for _ in range(50): generated = phrase.generate() assert SHAPE.match(generated), generated head, _, tail = generated.rpartition("-") assert len(tail) == 5 assert all(word in phrase.words() for word in head.split("-")) def test_entropy_is_what_the_plan_claims(): # Four EFF-large words plus a five-character base-36 tail, ~77.5 bits. assert phrase.entropy_bits() > 75 def test_wordlist_is_the_eff_large_list_minus_its_hyphenated_entries(): words = phrase.words() # The file is the EFF large list, 7776 lines; four of them are hyphenated # and the loader drops them, because a hyphen is the phrase's separator. assert len(words) == 7772 assert len(set(words)) == 7772 assert "abacus" in words assert "drop-down" not in words def test_every_loaded_word_is_plain_lowercase_letters(): """Not a sampling test: one hyphenated word makes a six-token phrase. `test_generated_phrase_has_the_agreed_shape` would only catch that by luck -- four draws out of 7776 hit a hyphenated entry about 0.2% of the time -- so the property is asserted over the whole list instead. """ assert all(WORD.match(word) for word in phrase.words()) def test_generate_is_not_reproducible_from_a_seed(): # SystemRandom/secrets: seeding the module RNG must not pin the output. import random random.seed(1234) first = phrase.generate() random.seed(1234) second = phrase.generate() assert first != second def test_normalize_lowercases_and_trims(): assert phrase.normalize(" Amber-Otter-7K2 ") == "amber-otter-7k2" def test_validate_accepts_a_good_phrase_and_returns_the_normalized_form(): assert phrase.validate("Amber-Otter-Lantern-Harbor-7K2QP") == ( "amber-otter-lantern-harbor-7k2qp" ) def test_validate_accepts_everything_generate_produces(): for _ in range(50): generated = phrase.generate() assert phrase.validate(generated) == generated @pytest.mark.parametrize("bad", ["", "single", "two-tokens", "has space-phrase-x", "dash--double-phrase", "-leading-hyphen", "trailing-hyphen-", "under_score-in-phrase"]) def test_validate_rejects_a_phrase_that_is_not_the_shape(bad): with pytest.raises(ValueError): phrase.validate(bad) @pytest.mark.parametrize("weak", [ "a-b-c", # the reviewer's example: three letters "ab-cd-ef-gh-12345", # four "words" of two letters "amber-otter-lantern-7k2qp", # three words, not four "amber-otter-lantern-harbor", # four words, no tail "amber-otter-lantern-harbor-7k2", # a three-character tail "amber-otter-lantern-harbor-7k2qpz", # a six-character tail "amber-otter-lantern-harbor-hill-7k2qp", # five words "amber-0tter-lantern-harbor-7k2qp", # a digit inside a word ]) def test_validate_rejects_a_phrase_weaker_than_the_generated_shape(weak): """`--phrase` is a password for a LAN-facing stream, not a label. Anything `validate` accepts is something an operator can set, so the floor has to be the shape `generate` produces -- otherwise `a-b-c` opens a stream. """ with pytest.raises(ValueError): phrase.validate(weak) def test_validate_rejects_an_overlong_phrase(): with pytest.raises(ValueError): phrase.validate("-".join(["averyverylongword"] * 8))