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

Add -r/--randomsource option.

The new option is not active yet.
parent a1580b35
No related branches found
No related tags found
No related merge requests found
......@@ -86,6 +86,7 @@ def get_random_sources():
def handle_options(args):
"""Handle commandline options.
"""
random_sources = get_random_sources().keys()
parser = argparse.ArgumentParser(description="Create a passphrase")
parser.add_argument(
'-n', '--num', default=6, type=int,
......@@ -103,6 +104,12 @@ def handle_options(args):
parser.add_argument(
'-d', '--delimiter', default='',
help="Separate words by DELIMITER. Empty string by default.")
parser.add_argument(
'-r', '--randomsource', default='system', choices=random_sources,
metavar="SOURCE",
help=(
"Get randomness from this source. Possible values: `%s'. "
"Default: system" % "', `".join(random_sources)))
parser.add_argument(
'infile', nargs='?', metavar='INFILE', default=None,
type=argparse.FileType('r'),
......
usage: diceware [-h] [-n NUM] [-c | --no-caps] [-s NUM] [-d DELIMITER]
[--version]
[-r SOURCE] [--version]
[INFILE]
Create a passphrase
......@@ -16,4 +16,7 @@ optional arguments:
Insert NUM special chars into generated word.
-d DELIMITER, --delimiter DELIMITER
Separate words by DELIMITER. Empty string by default.
-r SOURCE, --randomsource SOURCE
Get randomness from this source. Possible values:
`system'. Default: system
--version output version information and exit.
......@@ -211,6 +211,7 @@ class TestDicewareModule(object):
assert options.infile is None
assert options.version is False
assert options.delimiter == ""
assert options.randomsource == "system"
def test_handle_options_infile(self, tmpdir):
# we can give an infile
......@@ -235,6 +236,23 @@ class TestDicewareModule(object):
options = handle_options(['-d', 'WOW'])
assert options.delimiter == 'WOW'
def test_handle_options_randomsource(self):
# we can choose the source of randomness
source_names = get_random_sources().keys()
for name in source_names:
options = handle_options(['-r', name])
assert options.randomsource == name
options = handle_options(['--randomsource', name])
assert options.randomsource == name
def test_handle_options_randomsource_rejects_invalid(self, capsys):
# we can not choose illegal values for source of randomness
with pytest.raises(SystemExit):
handle_options(['-r', 'not-a-valid-source-name'])
out, err = capsys.readouterr()
assert out == ''
assert "invalid choice" in err
def test_main(self, capsys):
# we can get a passphrase
main([]) # call with default options in place
......
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