From a01755afed043d9d4da26ba1ec3bfa2902ce149e Mon Sep 17 00:00:00 2001 From: ulif <uli@gnufix.de> Date: Fri, 29 May 2015 14:41:35 +0200 Subject: [PATCH] Add -r/--randomsource option. The new option is not active yet. --- diceware/__init__.py | 7 +++++++ tests/exp_help_output.txt | 5 ++++- tests/test_diceware.py | 18 ++++++++++++++++++ 3 files changed, 29 insertions(+), 1 deletion(-) diff --git a/diceware/__init__.py b/diceware/__init__.py index e325774..01cd566 100644 --- a/diceware/__init__.py +++ b/diceware/__init__.py @@ -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'), diff --git a/tests/exp_help_output.txt b/tests/exp_help_output.txt index 0358e47..53fc85b 100644 --- a/tests/exp_help_output.txt +++ b/tests/exp_help_output.txt @@ -1,5 +1,5 @@ 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. diff --git a/tests/test_diceware.py b/tests/test_diceware.py index fa4768e..71b2bc4 100644 --- a/tests/test_diceware.py +++ b/tests/test_diceware.py @@ -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 -- GitLab