def test_get_wordlist_path_ignores_subdirs(self, wordlists_dir): # we subdirs are ignored, when looking for wordlists. path1 = wordlists_dir.mkdir('wordlist_subdir1.txt') path2 = wordlists_dir.join('wordlist_subdir2.txt') path2.write('foo\n') assert os.path.isdir(str(path1)) assert os.path.isfile(str(path2)) assert get_wordlist_path('subdir1') is None assert get_wordlist_path('subdir2') == str(path2)
def test_get_wordlist_path(self, wordlists_dir): # we can get valid wordlist paths path1 = wordlists_dir.join("wordlist_foo.txt") path1.write("foo\n") path2 = wordlists_dir.join("wordlist_bar.asc") path2.write("bar\n") assert get_wordlist_path('foo') == path1 assert get_wordlist_path('bar') == path2 assert get_wordlist_path('zz') is None
def test_get_wordlist_path_accepts_any_ext(self, wordlists_dir): # we cope with any filename extension, not only .txt path1 = wordlists_dir.join("wordlist_foo.txt") path1.write("foo\n") path2 = wordlists_dir.join("wordlist_bar.asc") path2.write("bar\n") path3 = wordlists_dir.join("wordlist_baz.txt.asc") path3.write("baz\n") assert get_wordlist_path("foo") == str(path1) assert get_wordlist_path("bar") == str(path2) assert get_wordlist_path("baz") == str(path3) assert get_wordlist_path("not-existing") is None
def get_passphrase(options=None): """Get a diceware passphrase. `options` is a set of arguments as provided by `argparse.OptionParser.parse_args()`. The passphrase returned will contain `options.num` words deliimted by `options.delimiter` and `options.specials` special chars. For the passphrase generation we will use the random source registered under the name `options.randomsource` (something like "system" or "dice"). If `options.caps` is ``True``, all words will be caps. If `options.infile`, a file descriptor, is given, it will be used instead of a 'built-in' wordlist. `options.infile` must be open for reading. """ if options is None: options = handle_options(args=[]) if options.infile is None: options.infile = open(get_wordlist_path(options.wordlist), 'r') word_list = WordList(options.infile) rnd_source = get_random_sources()[options.randomsource] rnd = rnd_source(options) words = [rnd.choice(list(word_list)) for x in range(options.num)] if options.caps: words = [x.capitalize() for x in words] result = options.delimiter.join(words) for _ in range(options.specials): result = insert_special_char(result, rnd=rnd) return result
def get_passphrase(options=None): """Get a diceware passphrase. `options` is a set of arguments as provided by `argparse.OptionParser.parse_args()`. The passphrase returned will contain `options.num` words deliimted by `options.delimiter` and `options.specials` special chars. For the passphrase generation we will use the random source registered under the name `options.randomsource` (something like "system" or "dice"). If `options.caps` is ``True``, all words will be caps. If `options.infile`, a file descriptor, is given, it will be used instead of a 'built-in' wordlist. `options.infile` must be open for reading. """ if options is None: options = handle_options(args=[]) if options.infile is None: options.infile = get_wordlist_path(options.wordlist) word_list = WordList(options.infile) rnd_source = get_random_sources()[options.randomsource] rnd = rnd_source(options) words = [rnd.choice(list(word_list)) for x in range(options.num)] if options.caps: words = [x.capitalize() for x in words] result = options.delimiter.join(words) for _ in range(options.specials): result = insert_special_char(result, rnd=rnd) return result
def test_get_wordlist_path_requires_ascii(self): # non ASCII alphabet chars are not accepted in language specifier with pytest.raises(ValueError) as exc_info: get_wordlist_path('../../tmp') assert exc_info.value.args[0].startswith( 'Not a valid wordlist name')