-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcipher_tests.py
79 lines (56 loc) · 2.59 KB
/
cipher_tests.py
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
from enigma_machine import EnigmaMachine
from data.test_paragraphs import paragraphs
from string import ascii_uppercase
import datetime
import random
REPEAT = 10
def generate_private_code() -> str:
return ''.join(random.sample(ascii_uppercase, 8))
def code_test_enigma(machine: EnigmaMachine) -> bool:
"""
Test the encryption-decryption functionality of the EnigmaMachine for randomly selected paragraph and
date in the current month, and a randomly generated private-message code
Pass: True - Selected paragraph and decrypted message match
Fail: False - Selected paragraph and decrypted message match
"""
private_code = generate_private_code()
# print(private_code)
paragraph = random.choice(paragraphs)
machine.set_private_code(private_code)
cipher_1 = machine.cipher_message(paragraph)
machine.set_private_code(private_code)
cipher_2 = machine.cipher_message(cipher_1)
return paragraph == cipher_2
def date_test_enigma(machine: EnigmaMachine) -> bool:
"""
Test the encryption-decryption functionality of the EnigmaMachine for randomly selected paragraph and
date in the current month,
Pass: True - Selected paragraph and decrypted message match
Fail: False - Selected paragraph and decrypted message match
"""
machine.set_preset_date(get_random_day())
# print(machine.message_date)
paragraph = random.choice(paragraphs)
cipher_1 = machine.cipher_message(paragraph)
cipher_2 = machine.cipher_message(cipher_1)
return paragraph == cipher_2
def get_random_day() -> datetime.date:
"""Returns a random date from the current month"""
today = datetime.date.today()
year, month = today.year, today.month
st_date = datetime.date(year, month, 1)
days_in_month = (datetime.date(year, month + 1, 1) - st_date).days - 1
random_day = random.randint(1, days_in_month)
# Generate the datetime.date object for the random_day
random_date = st_date + datetime.timedelta(days=random_day)
return random_date
def main():
# Create an instance of EnigmaMachine
enigma_machine = EnigmaMachine(add_logs=False)
date_test_results = [date_test_enigma(enigma_machine) for _ in range(REPEAT)]
code_test_results = [code_test_enigma(enigma_machine) for _ in range(REPEAT)]
# Print out the cumulative test results to the console
print(f"Date Test Results: Pass - {date_test_results.count(True)} / Fail - {date_test_results.count(False)}")
print(f"Code Test Results: Pass - {code_test_results.count(True)} / Fail - {code_test_results.count(False)}")
if __name__ == '__main__':
main()