Skip to content
Snippets Groups Projects
Commit 2c9dd75f authored by ulif's avatar ulif
Browse files

Add regexp to find valid wordlist filenames.

For wordlists we require a certain filename format. This is
described by a regular expression we introduce hereby.
parent ca55a488
No related branches found
No related tags found
No related merge requests found
......@@ -29,6 +29,10 @@ RE_WORDLIST_NAME = re.compile('^[a-zA-Z0-9_-]+$')
#: A regular expression matching numbered entries in wordlists.
RE_NUMBERED_WORDLIST_ENTRY = re.compile('^[0-9]+\s+([^\s]+)$')
#: A regular expression describing valid wordlist file names.
RE_VALID_WORDLIST_FILENAME = re.compile(
'^wordlist_([a-zA-Z0-9_-]+)\.[a-zA-Z0-9][a-zA-Z0-9\.]+[a-zA-Z0-9]+$')
def get_wordlist_names():
"""Get a all names of wordlists stored locally.
......
......@@ -2,7 +2,8 @@ import os
import pytest
from diceware.wordlist import (
WORDLISTS_DIR, RE_WORDLIST_NAME, RE_NUMBERED_WORDLIST_ENTRY,
get_wordlist_path, get_wordlist_names, refine_wordlist_entry, WordList,
RE_VALID_WORDLIST_FILENAME, get_wordlist_path, get_wordlist_names,
refine_wordlist_entry, WordList,
)
......@@ -44,6 +45,30 @@ class TestWordlistModule(object):
assert RE_NUMBERED_WORDLIST_ENTRY.match('12a11 foo') is None
assert RE_NUMBERED_WORDLIST_ENTRY.match('foo bar') is None
def test_re_valid_wordlist_filename(self):
# RE_VALID_WORDLIST_FILENAME really detects filenames we allow
# Valid filenames
regexp = RE_VALID_WORDLIST_FILENAME
assert regexp.match("wordlist_foo.txt") is not None
assert regexp.match("wordlist_foo_bar.asc") is not None
assert regexp.match("wordlist_name-withdots.txt.asc") is not None
# We can get the internal wordlist name
assert regexp.match("wordlist_foo.txt").groups()[0] == "foo"
assert regexp.match(
"wordlist_foo_bar.asc").groups()[0] == "foo_bar"
assert regexp.match(
"wordlist_name-with.dots.txt.asc").groups()[0] == "name-with"
# Invalid names
assert regexp.match("wordlist-without-underscore.txt") is None
assert regexp.match("wordlist_invalid_ch=r.txt") is None
assert regexp.match("wordlist_without_dot_txt") is None
assert regexp.match("nowordlist_foo.txt") is None
assert regexp.match("wordlist_name.") is None
assert regexp.match("wordlist_name.txt.") is None
assert regexp.match("wordlist_name.txt..") is None
assert regexp.match("wordlist_name.txt/..") is None
assert regexp.match("wordlist_.txt") is None
def test_get_wordlist_path(self):
# we can get valid wordlist paths
assert os.path.exists(get_wordlist_path('en_8k'))
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment