Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
D
diceware-debian
Manage
Activity
Members
Labels
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Deploy
Releases
Model registry
Analyze
Value stream analytics
Contributor analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
drebs
diceware-debian
Commits
a119da5e
Commit
a119da5e
authored
10 years ago
by
ulif
Browse files
Options
Downloads
Patches
Plain Diff
Fix get_wordlist().
parent
02ad00b9
No related branches found
No related tags found
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
diceware/diceware.py
+12
-1
12 additions, 1 deletion
diceware/diceware.py
tests/test_diceware.py
+33
-7
33 additions, 7 deletions
tests/test_diceware.py
with
45 additions
and
8 deletions
diceware/diceware.py
+
12
−
1
View file @
a119da5e
...
...
@@ -3,8 +3,18 @@ import os
#: The directory in which wordlists are stored
SRC_DIR
=
os
.
path
.
dirname
(
__file__
)
def
get_wordlist
(
path
):
"""
Parse file at `path` and build a word list of it.
A wordlist is expected to contain lines of format::
<NUMS><TAB><WORD>
\n
for instance::
136512
\t
Term
"""
result
=
[]
with
open
(
path
,
'
r
'
)
as
fd
:
...
...
@@ -12,7 +22,8 @@ def get_wordlist(path):
if
not
'
\t
'
in
line
:
continue
term
=
line
.
split
(
'
\t
'
)[
1
].
strip
()
result
.
append
(
term
)
if
term
!=
''
:
# do not accept empty strings
result
.
append
(
term
)
return
result
...
...
This diff is collapsed.
Click to expand it.
tests/test_diceware.py
+
33
−
7
View file @
a119da5e
...
...
@@ -2,10 +2,36 @@ import os
from
diceware.diceware
import
SRC_DIR
,
get_wordlist
def
test_get_wordlist_en
():
# we can get a list of words out of english wordlist.
en_src
=
os
.
path
.
join
(
SRC_DIR
,
'
wordlist_en.asc
'
)
en_result
=
get_wordlist
(
en_src
)
assert
en_result
[
0
]
==
'
a
'
assert
en_result
[
-
1
]
==
'
@
'
assert
len
(
en_result
)
==
7776
class
Test_GetWordList
(
object
):
def
test_get_wordlist_en
(
self
):
# we can get a list of words out of english wordlist.
en_src
=
os
.
path
.
join
(
SRC_DIR
,
'
wordlist_en.asc
'
)
en_result
=
get_wordlist
(
en_src
)
assert
en_result
[
0
]
==
'
a
'
assert
en_result
[
-
1
]
==
'
@
'
assert
len
(
en_result
)
==
7776
def
test_get_wordlist_simple
(
self
,
tmpdir
):
# simple wordlists can be created
in_file
=
tmpdir
.
mkdir
(
"
work
"
).
join
(
"
mywordlist
"
)
in_file
.
write
(
"
01
\t
a
\n
02
\t
b
\n
"
)
assert
[
'
a
'
,
'
b
'
]
==
get_wordlist
(
in_file
.
strpath
)
def
test_get_wordlist_ignore_empty_lines
(
self
,
tmpdir
):
# we ignore empty lines in wordlists
in_file
=
tmpdir
.
mkdir
(
"
work
"
).
join
(
"
mywordlist
"
)
in_file
.
write
(
"
\n\n\n
"
)
assert
[]
==
get_wordlist
(
in_file
.
strpath
)
def
test_get_wordlist_ignore_non_data
(
self
,
tmpdir
):
# lines without tabs ('\t') are ignored
in_file
=
tmpdir
.
mkdir
(
"
work
"
).
join
(
"
mywordlist
"
)
in_file
.
write
(
"
111111 a
\n
111112 b
\n
"
)
assert
[]
==
get_wordlist
(
in_file
.
strpath
)
def
test_get_wordlist_broken
(
self
,
tmpdir
):
# we handle broken lines gracefully
in_file
=
tmpdir
.
mkdir
(
"
work
"
).
join
(
"
mywordlist
"
)
in_file
.
write
(
"
11
\t
a
\t
12
\n
22
\t\n\n
33
\t
c
"
)
assert
[
'
a
'
,
'
c
'
]
==
get_wordlist
(
in_file
.
strpath
)
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment