~gagbo/diceware.py

ref: 5e095495691260b4f8f6a5183cf73a85d4e1dc48 diceware.py/tests/test_dice_rolls.py -rw-r--r-- 4.0 KiB
5e095495 — Gerry Agbobada Ensure unique random_generator in DicewareResult 2 years ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
#!/usr/bin/env python3
# coding: utf-8
import copy
import pytest
import re
import random

import dice_rolls.diceware_result as dw
import dice_dict.list_to_dict as ltd


@pytest.fixture()
def bundled_dict():
    bundled_path = "data/diceware-fr-5-jets.txt"
    with open(bundled_path, "r") as bundled_dict_file:
        return ltd.create_dictionary(bundled_dict_file)


def test_passphrase_stability(bundled_dict):
    result = dw.DicewareResult(wordsCount=5, systemRand=True, bonusRoll=True)
    result.make_rolls()
    passphrases = []
    passphrases.append(result.password_from_dict(bundled_dict))
    passphrases.append(result.password_from_dict(bundled_dict))
    # No new make_rolls() calls should leave the passphrase stable
    assert passphrases[0] == passphrases[1]
    result.make_rolls()
    passphrases.append(result.password_from_dict(bundled_dict))
    assert passphrases[1] != passphrases[2]


# TODO : make this a lambda to loop over wordsCount ? Or decorator ?
# Or multiple definitions?
def test_passphrase_wordcount(bundled_dict):
    result = dw.DicewareResult(wordsCount=5, systemRand=True, bonusRoll=True)
    result.make_rolls()
    passphrase = result.password_from_dict(bundled_dict)
    m = re.findall(" ", passphrase)
    assert len(m) == 4
    assert len(re.split(r" ", passphrase)) == 5


def test_salt():
    not_salted = dw.DicewareResult(wordsCount=2, systemRand=True, bonusRoll=False)
    salted = dw.DicewareResult(wordsCount=2, systemRand=True, bonusRoll=True)
    not_salted.make_rolls()
    salted.make_rolls()
    assert salted.salt is not None
    assert not_salted.salt is None


def test_non_system_rand():
    stored_results = []
    result = dw.DicewareResult(wordsCount=5, systemRand=False, bonusRoll=True)

    result.ensure_random_generator()
    result.random_generator.seed(15)
    state = random.getstate()

    result.make_rolls()
    stored_results.append(copy.deepcopy(result))
    result.make_rolls()
    stored_results.append(copy.deepcopy(result))

    #result.random_generator.setstate(state)
    result.random_generator.seed(15)
    result.make_rolls()
    stored_results.append(copy.deepcopy(result))
    assert stored_results[0].rolls != stored_results[1].rolls
    assert stored_results[0].rolls == stored_results[2].rolls


def test_salt_char():
    # Default behaviour
    assert dw.get_salt_char() == ' '
    # Invalid with a 0
    assert dw.get_salt_char(0, 1) == ' '
    assert dw.get_salt_char(1, 0) == ' '
    # Invalid with a 7
    assert dw.get_salt_char(7, 1) == ' '
    assert dw.get_salt_char(1, 7) == ' '
    # Valid hardcoded table to ensure different stuff
    assert dw.get_salt_char(1, 1) == '~'
    assert dw.get_salt_char(1, 2) == '&'
    assert dw.get_salt_char(1, 3) == '+'
    assert dw.get_salt_char(1, 4) == ':'
    assert dw.get_salt_char(1, 5) == '?'
    assert dw.get_salt_char(1, 6) == '4'
    assert dw.get_salt_char(2, 1) == '!'
    assert dw.get_salt_char(2, 2) == '*'
    assert dw.get_salt_char(2, 3) == '['
    assert dw.get_salt_char(2, 4) == ';'
    assert dw.get_salt_char(2, 5) == '/'
    assert dw.get_salt_char(2, 6) == '5'
    assert dw.get_salt_char(3, 1) == '#'
    assert dw.get_salt_char(3, 2) == '('
    assert dw.get_salt_char(3, 3) == ']'
    assert dw.get_salt_char(3, 4) == '"'
    assert dw.get_salt_char(3, 5) == '0'
    assert dw.get_salt_char(3, 6) == '6'
    assert dw.get_salt_char(4, 1) == '$'
    assert dw.get_salt_char(4, 2) == ')'
    assert dw.get_salt_char(4, 3) == '\\'
    assert dw.get_salt_char(4, 4) == '\''
    assert dw.get_salt_char(4, 5) == '1'
    assert dw.get_salt_char(4, 6) == '7'
    assert dw.get_salt_char(5, 1) == '%'
    assert dw.get_salt_char(5, 2) == '-'
    assert dw.get_salt_char(5, 3) == '{'
    assert dw.get_salt_char(5, 4) == '<'
    assert dw.get_salt_char(5, 5) == '2'
    assert dw.get_salt_char(5, 6) == '8'
    assert dw.get_salt_char(6, 1) == '^'
    assert dw.get_salt_char(6, 2) == '='
    assert dw.get_salt_char(6, 3) == '}'
    assert dw.get_salt_char(6, 4) == '>'
    assert dw.get_salt_char(6, 5) == '3'
    assert dw.get_salt_char(6, 6) == '9'