-
Notifications
You must be signed in to change notification settings - Fork 0
/
enigma_test.rb
114 lines (81 loc) · 3.1 KB
/
enigma_test.rb
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
require_relative 'lib/enigma'
require 'minitest/autorun'
class EnigmaTest < Minitest::Test
def test_map_gets_built_on_initialize
e = Enigma.new
assert_equal ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", " ", "!", "@", "$", "%", "^", "&", "*", "(", ")", "[", "]", ",", ".", "<", ">", ";", ":", "/", "?", "|"], e.dictionary
end
def test_add_date_offsets
e = Enigma.new
assert_equal [18,29,39,51], e.add_date_offsets([12,23,34,45], 140216)
end
def test_create_rotations_from_random_key
e = Enigma.new
numbers = (1..5).map{rand(9)}
assert_equal [[numbers[0],numbers[1]].join.to_i, [numbers[1],numbers[2]].join.to_i, [numbers[2],numbers[3]].join.to_i, [numbers[3],numbers[4]].join.to_i], e.create_rotations_from_key_array(numbers)
end
def test_build_rotation_with_specified_digits
e = Enigma.new
assert_equal [12,23,34,45], e.create_rotations_from_key_array(e.key_to_array(12345))
end
def test_build_rotation_with_specified_string
e = Enigma.new
assert_equal [12,23,34,45], e.create_rotations_from_key_array(e.key_to_array("12345"))
end
def test_encrypt_with_date_specified
e = Enigma.new
assert_equal "OR42K", e.encrypt("words", "12345", Date.today)
assert_equal "OR42K", e.encrypt("words", "12345", 180216)
assert_equal "OR42K", e.encrypt("words", "12345", "180216")
end
def test_encrypt_without_date
e = Enigma.new
assert_equal "OR42K", e.encrypt("words", "12345")
end
def test_encrypt_message_only
e = Enigma.new
refute_equal "words", e.encrypt("words")
end
def test_decrypt
e = Enigma.new
assert_equal "words", e.decrypt("OR42K", "12345")
end
def test_decrypt_high_key
e = Enigma.new
assert_equal "words", e.decrypt("SKMzO", "99999")
end
def test_encrpyt_and_decrypt_with_random_key
e = Enigma.new
10.times do
key = (1..5).map{rand(9)}.join
coded = e.encrypt("words", key)
assert_equal "words", e.decrypt(coded, key)
end
end
def test_handling_capitals
e = Enigma.new
assert_equal "O*4/K", e.encrypt("wOrDs", "12345")
end
def test_crack_with_and_without_date_in_two_forms
e = Enigma.new
coded = e.encrypt("words ..end..", (1..5).map{rand(9)}.join)
assert_equal "words ..end..", e.crack(coded, Date.today)
assert_equal "words ..end..", e.crack(coded, 180216)
assert_equal "words ..end..", e.crack(coded)
end
def test_date_to_int_with_Date
e = Enigma.new
date = Date.today
assert_equal Date.today.strftime("%d%m%y").to_i, e.date_to_int(date)
end
def test_date_to_int_with_String
e = Enigma.new
date = Date.today
assert_equal Date.today.strftime("%d%m%y").to_i, e.date_to_int(date)
end
def test_date_to_int_with_Fixnum
e = Enigma.new
date = Date.today
assert_equal Date.today.strftime("%d%m%y").to_i, e.date_to_int(date)
end
end