Esempio n. 1
0
 def test_is_signed_detects_unsigned_files(self, tmpdir):
     # we can tell if a wordlist is not signed
     in_file = tmpdir.mkdir("work").join("mywordlist")
     in_file.write("a\nb\n")
     with open(in_file.strpath, 'r') as fd:
         w_list = WordList(fd)
         w_list.fd = fd
         result = w_list.is_signed()
     assert result is False
Esempio n. 2
0
 def test_is_signed_detects_signed_files(self):
     # we recognize signed wordlists
     in_path = os.path.join(
         os.path.dirname(__file__), "sample_signed_wordlist.asc")
     with open(in_path, "r") as fd:
         w_list = WordList(fd)
         w_list.fd = fd
         result = w_list.is_signed()
     assert result is True
Esempio n. 3
0
 def test_is_signed_detects_unsigned_files(self, tmpdir):
     # we can tell if a wordlist is not signed
     in_file = tmpdir.mkdir("work").join("mywordlist")
     in_file.write("a\nb\n")
     with open(in_file.strpath, 'r') as fd:
         w_list = WordList(fd)
         w_list.fd = fd
         result = w_list.is_signed()
     assert result is False
Esempio n. 4
0
 def test_is_signed_detects_signed_files(self):
     # we recognize signed wordlists
     in_path = os.path.join(
         os.path.dirname(__file__), "sample_signed_wordlist.asc")
     with open(in_path, "r") as fd:
         w_list = WordList(fd)
         w_list.fd = fd
         result = w_list.is_signed()
     assert result is True
Esempio n. 5
0
def wordlist(request, tmpdir):
    """A fixture that delivers a simple WordList instance.
    """
    path = tmpdir.join("mylist.txt")
    path.write("foo\nbar\n")
    w_list = WordList(str(path))
    return w_list
Esempio n. 6
0
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
Esempio n. 7
0
 def test_wordlist_en_eff(self):
     # we can get a list of words out of the EFF-maintained wordlist.
     en_src = os.path.join(WORDLISTS_DIR, 'wordlist_en_eff.txt')
     w_list = list(WordList(en_src))
     assert w_list[0] == "abacus"
     assert w_list[-1] == "zoom"
     assert len(w_list) == 7776
Esempio n. 8
0
 def test_get_signed_wordlist_handles_clearsigned_files(self):
     # we can process cryptogrphically signed files
     in_path = os.path.join(
         os.path.dirname(__file__), "sample_signed_wordlist.asc")
     with open(in_path, 'r') as fd:
         result = list(WordList(fd))
     assert ["foo", "bar", "-dash-at-start", "baz"] == result
Esempio n. 9
0
 def test_wordlist_en(self):
     # we can get a list of words out of the original diceware wordlist.
     en_src = os.path.join(WORDLISTS_DIR, 'wordlist_en_orig.asc')
     w_list = list(WordList(en_src))
     assert w_list[0] == "a"
     assert w_list[-1] == "@"
     assert len(w_list) == 7776
Esempio n. 10
0
 def test_get_signed_wordlist_ignore_empty_lines(self):
     # we ignore empty lines in wordlists
     in_path = os.path.join(
         os.path.dirname(__file__), "sample_signed_wordlist.asc")
     w_list = WordList(in_path)
     result = list(w_list)
     assert '' not in result
Esempio n. 11
0
 def test_get_signed_wordlist_handles_en_orig(self):
     # we can process the original diceware list from diceware.com
     wlist_path = os.path.join(WORDLISTS_DIR, 'wordlist_en_orig.asc')
     w_list = WordList(wlist_path)
     result = list(w_list)
     assert len(result) == 7776
     assert "a" == result[0]
     assert "@" == result[-1]
Esempio n. 12
0
 def test_wordlist_en_8k(self):
     # we can get a list of words out of the reinhold english 8k wordlist.
     en_src = os.path.join(WORDLISTS_DIR, 'wordlist_en.txt')
     w_list = WordList(en_src)
     long_list = list(w_list)
     assert long_list[0] == "a"
     assert long_list[-1] == "@"
     assert len(long_list) == 8192
Esempio n. 13
0
 def test_can_get_wordlist_multiple_times(self, tmpdir):
     # we can get a wordlist several times
     in_file = tmpdir.mkdir("work").join("mywordlist")
     in_file.write("\n\na\n\n")
     w_list = WordList(str(in_file))
     list1 = list(w_list)
     list2 = list(w_list)
     assert list1 == list2
Esempio n. 14
0
 def test_can_get_wordlist_multiple_times_from_fd(self, tmpdir):
     # we can get a wordlist several times also if it is a fd.
     in_file = tmpdir.mkdir("work").join("mywordlist")
     in_file.write("foo\nbar\n")
     w_list = WordList(in_file.strpath)
     list1 = list(w_list)
     list2 = list(w_list)
     assert list1 == list2
Esempio n. 15
0
 def test_open_file_descriptor_signed(self):
     # we can handle signed wordlists from open file descriptors
     in_path = os.path.join(
         os.path.dirname(__file__), "sample_signed_wordlist.asc")
     with open(str(in_path), "r") as my_open_file:
         w_list = WordList(my_open_file)
         result = tuple(w_list)
     assert ("foo", "bar", "-dash-at-start", "baz") == result
Esempio n. 16
0
 def test_open_file_descriptor_simple(self, tmpdir):
     # we handle simple lists from open file descriptors correctly
     in_file = tmpdir.mkdir("work").join("mywordlist")
     in_file.write("foo\nbar\n\nbaz\n")
     with open(str(in_file), "r") as my_open_file:
         w_list = WordList(my_open_file)
         result = tuple(w_list)
     assert result == ("foo", "bar", "baz")
Esempio n. 17
0
 def test_create_accepts_open_file(self, tmpdir):
     # if we pass in an open file, it will be used
     in_file = tmpdir.mkdir("work").join("mywordlist")
     in_file.write("foo\n")
     with open(str(in_file), "r") as my_open_file:
         w_list = WordList(my_open_file)
         assert w_list.fd is not None
         assert w_list.path is None
Esempio n. 18
0
 def test_wordlist_en_securedrop(self):
     # we can get a list of words out of securedrop english 8k wordlist.
     en_src = os.path.join(WORDLISTS_DIR, 'wordlist_en_securedrop.asc')
     w_list = WordList(en_src)
     long_list = list(w_list)
     assert long_list[0] == "0"
     assert long_list[-1] == "zurich"
     assert len(long_list) == 8192
Esempio n. 19
0
 def test_get_wordlist_stdin(self, argv_handler):
     # we can get a wordlist from stdin
     def broken_seek(num):
         raise IOError("Illegal seek")
     fd = StringIO(b"foo\nbar\n".decode('utf-8'))
     fd.seek = broken_seek
     sys.stdin = fd
     w_list = WordList("-")
     assert list(w_list) == ['foo', 'bar']
Esempio n. 20
0
    def test_create_accepts_fd_with_broken_seek(self, argv_handler):
        # we accept files that have no working seek() (like sys.stdin)
        fd = StringIO(b"word1\nword2\n".decode("utf-8"))

        def broken_seek(num):
            raise IOError("Illegal seek")
        fd.seek = broken_seek
        w_list = WordList(fd)
        assert w_list.fd is not fd
Esempio n. 21
0
 def test_create_wordlist(self, tmpdir):
     # we can create `WordList` objects.
     in_file = tmpdir.mkdir("work").join("mywordlist")
     in_file.write("foo\n")
     w_list = WordList(str(in_file))
     assert w_list is not None
     assert hasattr(w_list, "path")
     assert hasattr(w_list, "fd")
     assert hasattr(w_list, "signed")
Esempio n. 22
0
 def test_wordlists_are_generators(self, tmpdir):
     # WordList instances act like generators.
     in_file = tmpdir.mkdir("work").join("mywordlist")
     in_file.write("foo\nbar\n")
     w_list = WordList(str(in_file))
     assert list(w_list) == ["foo", "bar"]
Esempio n. 23
0
 def test_is_signed_detects_signed_files(self):
     # we recognize signed wordlists
     in_path = os.path.join(
         os.path.dirname(__file__), "sample_signed_wordlist.asc")
     w_list = WordList(in_path)
     assert w_list.is_signed() is True
Esempio n. 24
0
 def test_detect_unsigned_wordlists(self, tmpdir):
     # we can detect unsigned wordlist files.
     in_file = tmpdir.mkdir("work").join("mywordlist")
     in_file.write("foo\n")
     w_list = WordList(str(in_file))
     assert w_list.signed is False
Esempio n. 25
0
 def test_detect_signed_wordlists(self):
     # we can detect signed wordlist files.
     in_path = os.path.join(
         os.path.dirname(__file__), "sample_signed_wordlist.asc")
     w_list = WordList(in_path)
     assert w_list.signed is True
Esempio n. 26
0
 def test_is_signed_detects_unsigned_files(self, tmpdir):
     # we can tell if a wordlist is not signed
     in_file = tmpdir.mkdir("work").join("mywordlist")
     in_file.write("a\nb\n")
     w_list = WordList(in_file.strpath)
     assert w_list.is_signed() is False
Esempio n. 27
0
 def test_create_opens_file(self, tmpdir):
     # if we pass-in a path, the file will be opened for reading.
     in_file = tmpdir.mkdir("work").join("mywordlist")
     in_file.write("foo\n")
     w_list = WordList(str(in_file))
     assert w_list.fd is not None
Esempio n. 28
0
 def test_get_wordlist_simple(self, tmpdir):
     # simple wordlists can be created
     in_file = tmpdir.mkdir("work").join("mywordlist")
     in_file.write("a\nb\n")
     result = list(WordList(str(in_file)))
     assert ['a', 'b'] == result
Esempio n. 29
0
 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\na\n\n")
     result = list(WordList(str(in_file)))
     assert ['a'] == result
Esempio n. 30
0
 def test_wordlist_from_signed_file(self):
     # we can get an iterator from signed wordlist.
     in_path = os.path.join(
         os.path.dirname(__file__), "sample_signed_wordlist.asc")
     w_list = WordList(in_path)
     assert list(w_list) == ["foo", "bar", "-dash-at-start", "baz"]
Esempio n. 31
0
 def test_file_simple(self, tmpdir):
     # we handle simple files correctly
     in_file = tmpdir.mkdir("work").join("mywordlist")
     in_file.write("foo\nbar\n\nbaz\n")
     w_list = WordList(in_file.strpath)
     assert tuple(w_list) == ("foo", "bar", "baz")