-
Notifications
You must be signed in to change notification settings - Fork 46
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add option -m/--max #67
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -104,6 +104,9 @@ def handle_options(args): | |
parser.add_argument( | ||
'-s', '--specials', default=0, type=int, metavar='NUM', | ||
help="Insert NUM special chars into generated word.") | ||
parser.add_argument( | ||
'-m', '--max', default=0, type=int, metavar='NUM', | ||
help="Truncate to NUM length.") | ||
parser.add_argument( | ||
'-d', '--delimiter', default='', | ||
help="Separate words by DELIMITER. Empty string by default.") | ||
|
@@ -193,6 +196,8 @@ def get_passphrase(options=None): | |
result = options.delimiter.join(words) | ||
for _ in range(options.specials): | ||
result = insert_special_char(result, rnd=rnd) | ||
if options.max > 0: | ||
result = result[:options.max] | ||
Comment on lines
+199
to
+200
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Shouldn't truncating happen before inserting special chars (that might get lost this way round)? |
||
return result | ||
|
||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -30,6 +30,7 @@ | |
num=6, | ||
caps=True, | ||
specials=0, | ||
max=0, | ||
delimiter="", | ||
randomsource="system", | ||
verbose=0, | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -45,6 +45,7 @@ def test_handle_options_defaults(self): | |
assert options.randomsource == "system" | ||
assert options.wordlist == "en_eff" | ||
assert options.verbose == 0 | ||
assert options.max == 0 | ||
|
||
def test_handle_options_infile(self, tmpdir): | ||
# we can give an infile | ||
|
@@ -69,6 +70,14 @@ def test_handle_options_caps(self): | |
assert options.caps is True | ||
options = handle_options(['--no-caps', ]) | ||
assert options.caps is False | ||
|
||
def test_handle_options_max(self): | ||
options = handle_options([]) | ||
assert options.max == 0 | ||
options = handle_options(['-m', '0']) | ||
assert options.max == 0 | ||
options = handle_options(['--max', '0']) | ||
assert options.max == 0 | ||
Comment on lines
+77
to
+80
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Values apart from 0 would be more interesting here. |
||
|
||
def test_handle_options_caps_conflicting_raises_exc(self): | ||
# conflicting caps-settings raise an exception | ||
|
@@ -243,6 +252,12 @@ def test_get_passphrase_delimiters(self): | |
options.delimiter = " " | ||
phrase = get_passphrase(options) | ||
assert " " in phrase | ||
|
||
def test_get_passphrase_max(self): | ||
options = handle_options(args=[]) | ||
options.max = 15 | ||
phrase = get_passphrase(options) | ||
assert len(phrase) == 15 | ||
Comment on lines
+256
to
+260
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This test would be more meaningful if it could ensure, that the original phrase was longer or shorter than 15 chars. |
||
|
||
def test_print_version(self, capsys): | ||
# we can print version infos | ||
|
@@ -362,4 +377,4 @@ def test_main_wordlist(self, argv_handler, capsys, wordlists_dir): | |
sys.argv = ['diceware', '-w', 'foo'] | ||
main() | ||
out, err = capsys.readouterr() | ||
assert out == 'FooFooFooFooFooFoo\n' | ||
assert out == 'FooFooFooFooFooFoo\n' | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The trailing newline is nitpicking, I know. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Default -1 or None would be more appropriate, I think.