-
Notifications
You must be signed in to change notification settings - Fork 2
/
enigma_test.rb
102 lines (88 loc) · 2.87 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
require "minitest/autorun"
require_relative "enigma"
require_relative "keyboard"
require_relative "lampboard"
require_relative "spindle"
require_relative "reflector_spindle"
require_relative "rotor"
require_relative "wiring"
class Enigma::Test < Minitest::Test
def test_encyphers_a_character
test_rotor = Rotor.new([1, 0])
enigma = Enigma.new(
input: Keyboard.new,
output: Lampboard.new,
transformer: Spindle.new(test_rotor)
)
result = enigma.cypher("A")
refute_equal "A", result
assert_kind_of String, result
end
def test_encyphers_different_characters_for_each
test_rotor = Rotor.new([2, 1, 0])
enigma = Enigma.new(
input: Keyboard.new,
output: Lampboard.new,
transformer: Spindle.new(test_rotor)
)
encyphered_character = enigma.cypher("A")
refute_equal encyphered_character, enigma.cypher("B")
end
def test_will_encypher_differently_for_repeated_character
test_rotor = Rotor.new([2, 1, 0])
enigma = Enigma.new(
input: Keyboard.new,
output: Lampboard.new,
transformer: Spindle.new(test_rotor)
)
first_encyphered_character = enigma.cypher("A")
second_encyphered_character = enigma.cypher("A")
refute_equal first_encyphered_character, second_encyphered_character
end
def test_encyphers_a_phrase
rotor_i = Rotor.new(ROTOR_I)
rotor_ii = Rotor.new(ROTOR_II)
rotor_iii = Rotor.new(ROTOR_III)
enigma = Enigma.new(
input: Keyboard.new,
output: Lampboard.new,
transformer: Spindle.new(rotor_i, rotor_ii, rotor_iii)
)
encyphered_phrase = enigma.cypher("HELLOWORLD")
assert_equal "IWLYOVBHFZ", encyphered_phrase
end
def test_encyphers_a_phrase_with_reflector
rotor_i = Rotor.new(ROTOR_I)
rotor_ii = Rotor.new(ROTOR_II)
rotor_iii = Rotor.new(ROTOR_III)
enigma = Enigma.new(
input: Keyboard.new,
output: Lampboard.new,
transformer: ReflectorSpindle.new(rotor_i, rotor_ii, rotor_iii)
)
encyphered_phrase = enigma.cypher("HELLOWORLD")
assert_equal "BORXECDZFQ", encyphered_phrase
end
def test_encyphers_and_decrypts_a_phrase
rotor_i = Rotor.new(ROTOR_I)
rotor_ii = Rotor.new(ROTOR_II)
rotor_iii = Rotor.new(ROTOR_III)
encypher_enigma = Enigma.new(
input: Keyboard.new,
output: Lampboard.new,
transformer: ReflectorSpindle.new(rotor_i, rotor_ii, rotor_iii)
)
starting_phrase = "WETTERBERICHT"
encyphered_phrase = encypher_enigma.cypher(starting_phrase)
rotor_i = Rotor.new(ROTOR_I)
rotor_ii = Rotor.new(ROTOR_II)
rotor_iii = Rotor.new(ROTOR_III)
decypher_enigma = Enigma.new(
input: Keyboard.new,
output: Lampboard.new,
transformer: ReflectorSpindle.new(rotor_i, rotor_ii, rotor_iii)
)
decyphered_phrase = decypher_enigma.cypher(encyphered_phrase)
assert_equal starting_phrase, decyphered_phrase
end
end