diff --git a/diceware/wordlist.py b/diceware/wordlist.py index 1bb9da9a8edc0d6b93ba7e925a19e16af6564687..06bd28b28320adb3ba3d8950d5634c99c66f92d0 100644 --- a/diceware/wordlist.py +++ b/diceware/wordlist.py @@ -79,54 +79,6 @@ def refine_wordlist_entry(entry, signed=False): return entry -def get_wordlist(file_descriptor): - """Parse file in `file_descriptor` and build a word list of it. - - `file_descriptor` is expected to be a file descriptor, already - opened for reading. The descriptor will be closed after - processing. - - A wordlist is expected to contain lines of words. Each line a - word. Empty lines are ignored. Returns a list of terms (lines) - found. - """ - result = [ - line.strip() for line in file_descriptor.readlines() - if line.strip() != ''] - file_descriptor.close() - return result - - -def get_signed_wordlist(file_descriptor): - """Parse cryptographically signed file in `file_descriptor` and - build a wordlist out of it. - - `file_descriptor` is expected to be a file descriptor, already - opened for reading. The descriptor will be closed after - processing. - - Signed wordlists are expected to be wordlists as described in - `get_wordlist()` but with ASCII armored signatures (as described in - RFC 4880). - - The signature headers/footers are stripped and the contained list of - words returned. - """ - result = [] - while file_descriptor.readline().strip(): - # wait for first empty line - pass - for line in file_descriptor.readlines(): - line = refine_wordlist_entry(line, signed=True) - if not line: - continue - elif line == '-----BEGIN PGP SIGNATURE-----': - break - result += [line, ] - file_descriptor.close() - return result - - def get_wordlist_path(name): """Get path to a wordlist file for a wordlist named `name`. diff --git a/tests/test_wordlist.py b/tests/test_wordlist.py index 846e84b7e1c9a9e2fe9b675fc65f67a52d3ca9f8..09b5cd54cf9d676fecbf570febf4681e72dfc0db 100644 --- a/tests/test_wordlist.py +++ b/tests/test_wordlist.py @@ -1,9 +1,9 @@ import os import pytest from diceware.wordlist import ( - WORDLISTS_DIR, RE_WORDLIST_NAME, RE_NUMBERED_WORDLIST_ENTRY, get_wordlist, - get_signed_wordlist, get_wordlist_path, get_wordlist_names, - is_signed_wordlist, refine_wordlist_entry, WordList, + WORDLISTS_DIR, RE_WORDLIST_NAME, RE_NUMBERED_WORDLIST_ENTRY, + get_wordlist_path, get_wordlist_names, is_signed_wordlist, + refine_wordlist_entry, WordList, ) @@ -15,78 +15,6 @@ def wordlists_dir(request, monkeypatch, tmpdir): return tmpdir -class Test_GetWordList(object): - - def test_get_wordlist_en_8k(self): - # we can get a list of words out of english 8k wordlist. - en_src = os.path.join(WORDLISTS_DIR, 'wordlist_en_8k.txt') - with open(en_src, 'r') as fd: - en_result = get_wordlist(fd) - assert en_result[0] == 'a' - assert en_result[-1] == '@' - assert len(en_result) == 8192 - - def test_get_wordlist_simple(self, tmpdir): - # simple wordlists can be created - in_file = tmpdir.mkdir("work").join("mywordlist") - in_file.write("a\nb\n") - with open(in_file.strpath, 'r') as fd: - result = get_wordlist(fd) - assert ['a', 'b'] == result - - def test_get_wordlist_ignore_empty_lines(self, tmpdir): - # we ignore empty lines in wordlists - in_file = tmpdir.mkdir("work").join("mywordlist") - in_file.write("\n\na\n\n") - with open(in_file.strpath, 'r') as fd: - result = get_wordlist(fd) - assert ['a'] == result - - def test_get_wordlist_closes_fd(self, tmpdir): - # we close passed-in file descriptors - in_file = tmpdir.join("somewordlist") - in_file.write("aaa\nbbb\n") - with open(in_file.strpath, 'r') as fd: - get_wordlist(fd) - assert fd.closed is True - - -class Test_GetSignedWordList(object): - - def test_get_signed_wordlist_handles_clearsigned_files(self, tmpdir): - # we can process cryptogrphically signed files - in_path = os.path.join( - os.path.dirname(__file__), "sample_signed_wordlist.asc") - with open(in_path, 'r') as fd: - result = get_signed_wordlist(fd) - assert ["foo", "bar", "-dash-at-start", "baz"] == result - - def test_get_signed_wordlist_handles_en_orig(self, tmpdir): - # we can process the original diceware list from diceware.com - wlist_path = os.path.join(WORDLISTS_DIR, 'wordlist_en_orig.asc') - with open(wlist_path, 'r') as fd: - result = get_signed_wordlist(fd) - assert len(result) == 7776 - assert "a" == result[0] - assert "@" == result[-1] - - def test_get_signed_wordlist_ignore_empty_lines(self, tmpdir): - # we ignore empty lines in wordlists - in_path = os.path.join( - os.path.dirname(__file__), "sample_signed_wordlist.asc") - with open(in_path, 'r') as fd: - result = get_signed_wordlist(fd) - assert '' not in result - - def test_get_signed_wordlist_closes_fd(self, tmpdir): - # we close passed-in file descriptors - in_path = os.path.join( - os.path.dirname(__file__), "sample_signed_wordlist.asc") - with open(in_path, 'r') as fd: - get_signed_wordlist(fd) - assert fd.closed is True - - class TestWordlistModule(object): def test_re_wordlist_name(self):