6 class LetterTransformerTest(unittest
.TestCase
):
9 lt
= LetterTransformer([('z', 'a')] + \
10 list(zip(string
.ascii_lowercase
, string
.ascii_lowercase
[1:])),
12 self
.assertEqual(lt
.forward_map
,
13 [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
14 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 0])
15 self
.assertEqual(lt
.backward_map
,
16 [25, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12,
17 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24])
21 lt
= LetterTransformer(cat(collections
.OrderedDict
.fromkeys('zyxwc' + string
.ascii_lowercase
)))
22 self
.assertEqual(lt
.forward_map
,
23 [25, 24, 23, 22, 2, 0, 1, 3, 4, 5, 6, 7, 8, 9,
24 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21])
25 self
.assertEqual(lt
.backward_map
,
26 [5, 6, 4, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16,
27 17, 18, 19, 20, 21, 22, 23, 24, 25, 3, 2, 1, 0])
29 def test_transform(self
):
30 lt
= LetterTransformer(cat(collections
.OrderedDict
.fromkeys('zyxwc' + string
.ascii_lowercase
)))
31 self
.assertEqual(cat(lt
.forward(l
) for l
in string
.ascii_lowercase
),
32 'zyxwcabdefghijklmnopqrstuv')
33 self
.assertEqual(cat(lt
.backward(l
) for l
in string
.ascii_lowercase
),
34 'fgehijklmnopqrstuvwxyzdcba')
37 class PlugboardTest(unittest
.TestCase
):
39 self
.pb
= Plugboard('ua pf rq so ni ey bg hl tx zj'.upper())
42 self
.assertEqual(self
.pb
.forward_map
,
43 [20, 6, 2, 3, 24, 15, 1, 11, 13, 25, 10, 7, 12,
44 8, 18, 5, 17, 16, 14, 23, 0, 21, 22, 19, 4, 9])
45 self
.assertEqual(self
.pb
.forward_map
, self
.pb
.backward_map
)
47 def test_transform(self
):
48 self
.assertEqual(cat(self
.pb
.forward(l
)
49 for l
in string
.ascii_lowercase
),
50 'ugcdypblnzkhmisfrqoxavwtej')
51 self
.assertEqual(cat(self
.pb
.backward(l
)
52 for l
in string
.ascii_lowercase
),
53 'ugcdypblnzkhmisfrqoxavwtej')
56 class ReflectorTest(unittest
.TestCase
):
58 self
.ref
= Reflector(reflector_b_spec
)
61 self
.assertEqual(self
.ref
.forward_map
,
62 [24, 17, 20, 7, 16, 18, 11, 3, 15, 23, 13, 6, 14,
63 10, 12, 8, 4, 1, 5, 25, 2, 22, 21, 9, 0, 19])
64 self
.assertEqual(self
.ref
.forward_map
, self
.ref
.backward_map
)
66 def test_transform(self
):
67 self
.assertEqual(cat(self
.ref
.forward(l
)
68 for l
in string
.ascii_lowercase
),
69 'yruhqsldpxngokmiebfzcwvjat')
70 self
.assertEqual(cat(self
.ref
.backward(l
)
71 for l
in string
.ascii_lowercase
),
72 'yruhqsldpxngokmiebfzcwvjat')
75 class SimpleWheelTest(unittest
.TestCase
):
77 rotor_1_transform
= list(zip(string
.ascii_lowercase
,
78 'EKMFLGDQVZNTOWYHXUSPAIBRCJ'.lower()))
79 wheel_1
= SimpleWheel(rotor_1_transform
, raw_transform
=True)
80 self
.assertEqual(cat(wheel_1
.forward(l
)
81 for l
in string
.ascii_lowercase
),
82 'ekmflgdqvzntowyhxuspaibrcj')
83 self
.assertEqual(cat(wheel_1
.backward(l
)
84 for l
in string
.ascii_lowercase
),
85 'uwygadfpvzbeckmthxslrinqoj')
88 wheel_2
= SimpleWheel(wheel_ii_spec
)
89 self
.assertEqual(cat(wheel_2
.forward(l
)
90 for l
in string
.ascii_lowercase
),
91 'ajdksiruxblhwtmcqgznpyfvoe')
92 self
.assertEqual(cat(wheel_2
.backward(l
)
93 for l
in string
.ascii_lowercase
),
94 'ajpczwrlfbdkotyuqgenhxmivs')
96 def test_set_position(self
):
97 wheel_3
= SimpleWheel(wheel_iii_spec
)
98 wheel_3
.set_position('a')
99 self
.assertEqual(wheel_3
.position
, 0)
100 self
.assertEqual(wheel_3
.position_l
, 'a')
102 wheel_3
.set_position('f')
103 self
.assertEqual(wheel_3
.position
, 5)
104 self
.assertEqual(wheel_3
.position_l
, 'f')
107 self
.assertEqual(wheel_3
.position
, 6)
108 self
.assertEqual(wheel_3
.position_l
, 'g')
110 wheel_3
.set_position(12)
111 self
.assertEqual(wheel_3
.position
, 12)
112 self
.assertEqual(wheel_3
.position_l
, 'm')
115 self
.assertEqual(wheel_3
.position
, 13)
116 self
.assertEqual(wheel_3
.position_l
, 'n')
119 def test_advance(self
):
120 wheel_3
= SimpleWheel(wheel_iii_spec
)
121 wheel_3
.set_position('a')
123 self
.assertEqual(cat(wheel_3
.forward(l
)
124 for l
in string
.ascii_lowercase
),
125 'cegikboqswuymxdhvfzjltrpna')
126 self
.assertEqual(cat(wheel_3
.backward(l
)
127 for l
in string
.ascii_lowercase
),
128 'zfaobrcpdteumygxhwivkqjnls')
129 self
.assertEqual(wheel_3
.position
, 1)
130 self
.assertEqual(wheel_3
.position_l
, 'b')
132 for _
in range(24): wheel_3
.advance()
134 self
.assertEqual(wheel_3
.position
, 25)
135 self
.assertEqual(wheel_3
.position_l
, 'z')
137 self
.assertEqual(cat(wheel_3
.forward(l
)
138 for l
in string
.ascii_lowercase
),
139 'pcegikmdqsuywaozfjxhblnvtr')
140 self
.assertEqual(cat(wheel_3
.backward(l
)
141 for l
in string
.ascii_lowercase
),
142 'nubhcqdterfvgwoaizjykxmslp')
145 self
.assertEqual(wheel_3
.position
, 0)
146 self
.assertEqual(wheel_3
.position_l
, 'a')
149 self
.assertEqual(cat(wheel_3
.forward(l
)
150 for l
in string
.ascii_lowercase
),
151 'bdfhjlcprtxvznyeiwgakmusqo')
152 self
.assertEqual(cat(wheel_3
.backward(l
)
153 for l
in string
.ascii_lowercase
),
154 'tagbpcsdqeufvnzhyixjwlrkom')
157 class WheelTest(unittest
.TestCase
):
158 def test_init1(self
):
159 wheel
= Wheel(wheel_iii_spec
, wheel_iii_pegs
, position
='b',
161 self
.assertEqual(wheel
.position
, 1)
162 self
.assertEqual(wheel
.peg_positions
, [20])
163 self
.assertEqual(wheel
.position_l
, 'b')
166 self
.assertEqual(wheel
.position
, 2)
167 self
.assertEqual(wheel
.peg_positions
, [19])
168 self
.assertEqual(wheel
.position_l
, 'c')
170 def test_init2(self
):
171 wheel
= Wheel(wheel_vi_spec
, wheel_vi_pegs
, position
='b',
173 self
.assertEqual(wheel
.position
, 25)
174 self
.assertIn(11, wheel
.peg_positions
)
175 self
.assertIn(24, wheel
.peg_positions
)
176 self
.assertEqual(wheel
.position_l
, 'b')
177 self
.assertEqual(cat(wheel
.forward(l
)
178 for l
in string
.ascii_lowercase
),
179 'xkqhwpvngzrcfoiaselbtymjdu')
180 self
.assertEqual(cat(wheel
.backward(l
)
181 for l
in string
.ascii_lowercase
),
182 'ptlyrmidoxbswhnfckquzgeavj')
184 def test_advance(self
):
185 wheel
= Wheel(wheel_vi_spec
, wheel_vi_pegs
, position
='b',
189 self
.assertEqual(wheel
.position
, 0)
190 self
.assertIn(10, wheel
.peg_positions
)
191 self
.assertIn(23, wheel
.peg_positions
)
192 self
.assertEqual(wheel
.position_l
, 'c')
193 self
.assertEqual(cat(wheel
.forward(l
)
194 for l
in string
.ascii_lowercase
),
195 'jpgvoumfyqbenhzrdkasxlictw')
196 self
.assertEqual(cat(wheel
.backward(l
)
197 for l
in string
.ascii_lowercase
),
198 'skxqlhcnwarvgmebjptyfdzuio')
200 def test_advance_23(self
):
201 wheel
= Wheel(wheel_vi_spec
, wheel_vi_pegs
, position
='b',
206 self
.assertEqual(wheel
.position
, 22)
207 self
.assertIn(1, wheel
.peg_positions
)
208 self
.assertIn(14, wheel
.peg_positions
)
209 self
.assertEqual(wheel
.position_l
, 'y')
210 self
.assertEqual(cat(wheel
.forward(l
)
211 for l
in string
.ascii_lowercase
),
212 'mgxantkzsyqjcufirldvhoewbp')
213 self
.assertEqual(cat(wheel
.backward(l
)
214 for l
in string
.ascii_lowercase
),
215 'dymswobuplgraevzkqifntxcjh')
217 def test_advance_24(self
):
218 wheel
= Wheel(wheel_vi_spec
, wheel_vi_pegs
, position
='b',
223 self
.assertEqual(wheel
.position
, 23)
224 self
.assertIn(0, wheel
.peg_positions
)
225 self
.assertIn(13, wheel
.peg_positions
)
226 self
.assertEqual(wheel
.position_l
, 'z')
227 self
.assertEqual(cat(wheel
.forward(l
)
228 for l
in string
.ascii_lowercase
),
229 'fwzmsjyrxpibtehqkcugndvaol')
230 self
.assertEqual(cat(wheel
.backward(l
)
231 for l
in string
.ascii_lowercase
),
232 'xlrvnatokfqzduyjphemswbigc')
234 def test_advance_25(self
):
235 wheel
= Wheel(wheel_vi_spec
, wheel_vi_pegs
, position
='b',
240 self
.assertEqual(wheel
.position
, 24)
241 self
.assertIn(25, wheel
.peg_positions
)
242 self
.assertIn(12, wheel
.peg_positions
)
243 self
.assertEqual(wheel
.position_l
, 'a')
244 self
.assertEqual(cat(wheel
.forward(l
)
245 for l
in string
.ascii_lowercase
),
246 'vylrixqwohasdgpjbtfmcuznke')
247 self
.assertEqual(cat(wheel
.backward(l
)
248 for l
in string
.ascii_lowercase
),
249 'kqumzsnjepyctxiogdlrvahfbw')
251 def test_advance_26(self
):
252 wheel
= Wheel(wheel_vi_spec
, wheel_vi_pegs
, position
='b',
257 self
.assertEqual(wheel
.position
, 25)
258 self
.assertIn(24, wheel
.peg_positions
)
259 self
.assertIn(11, wheel
.peg_positions
)
260 self
.assertEqual(wheel
.position_l
, 'b')
261 self
.assertEqual(cat(wheel
.forward(l
)
262 for l
in string
.ascii_lowercase
),
263 'xkqhwpvngzrcfoiaselbtymjdu')
264 self
.assertEqual(cat(wheel
.backward(l
)
265 for l
in string
.ascii_lowercase
),
266 'ptlyrmidoxbswhnfckquzgeavj')
268 def test_advance_27(self
):
269 wheel
= Wheel(wheel_vi_spec
, wheel_vi_pegs
, position
='b',
274 self
.assertEqual(wheel
.position
, 0)
275 self
.assertIn(23, wheel
.peg_positions
)
276 self
.assertIn(10, wheel
.peg_positions
)
277 self
.assertEqual(wheel
.position_l
, 'c')
278 self
.assertEqual(cat(wheel
.forward(l
)
279 for l
in string
.ascii_lowercase
),
280 'jpgvoumfyqbenhzrdkasxlictw')
281 self
.assertEqual(cat(wheel
.backward(l
)
282 for l
in string
.ascii_lowercase
),
283 'skxqlhcnwarvgmebjptyfdzuio')
285 def test_set_position(self
):
286 wheel_3
= Wheel(wheel_iii_spec
, wheel_iii_pegs
, ring_setting
=3)
287 wheel_3
.set_position('a')
288 self
.assertEqual(wheel_3
.position
, 24)
289 self
.assertEqual(wheel_3
.position_l
, 'a')
290 self
.assertEqual(wheel_3
.peg_positions
, [21])
292 wheel_3
.set_position('z')
293 self
.assertEqual(wheel_3
.position
, 23)
294 self
.assertEqual(wheel_3
.position_l
, 'z')
295 self
.assertEqual(wheel_3
.peg_positions
, [22])
297 wheel_3
.set_position(26)
298 self
.assertEqual(wheel_3
.position
, 23)
299 self
.assertEqual(wheel_3
.position_l
, 'z')
300 self
.assertEqual(wheel_3
.peg_positions
, [22])
302 wheel_3
.set_position(27)
303 self
.assertEqual(wheel_3
.position
, 24)
304 self
.assertEqual(wheel_3
.position_l
, 'a')
305 self
.assertEqual(wheel_3
.peg_positions
, [21])
307 wheel_3
.set_position('f')
308 self
.assertEqual(wheel_3
.position
, 3)
309 self
.assertEqual(wheel_3
.position_l
, 'f')
310 self
.assertEqual(wheel_3
.peg_positions
, [16])
312 wheel_3
.set_position(6)
313 self
.assertEqual(wheel_3
.position
, 3)
314 self
.assertEqual(wheel_3
.position_l
, 'f')
315 self
.assertEqual(wheel_3
.peg_positions
, [16])
318 self
.assertEqual(wheel_3
.position
, 4)
319 self
.assertEqual(wheel_3
.position_l
, 'g')
320 self
.assertEqual(wheel_3
.peg_positions
, [15])
322 wheel_3
.set_position(12)
323 self
.assertEqual(wheel_3
.position
, 9)
324 self
.assertEqual(wheel_3
.position_l
, 'l')
325 self
.assertEqual(wheel_3
.peg_positions
, [10])
328 self
.assertEqual(wheel_3
.position
, 10)
329 self
.assertEqual(wheel_3
.position_l
, 'm')
330 self
.assertEqual(wheel_3
.peg_positions
, [9])
333 class EnigmaTest(unittest
.TestCase
):
336 self
.enigma
= Enigma(reflector_b_spec
,
337 wheel_i_spec
, wheel_i_pegs
,
338 wheel_ii_spec
, wheel_ii_pegs
,
339 wheel_iii_spec
, wheel_iii_pegs
,
343 # Setting sheet line 31 from http://www.codesandciphers.org.uk/enigma/enigma3.htm
344 # Enigma simulation settings are
345 # http://enigma.louisedade.co.uk/enigma.html?m3;b;b153;AFTX;AJEU;AU-BG-EY-FP-HL-IN-JZ-OS-QR-TX
346 self
.enigma31
= Enigma(reflector_b_spec
,
347 wheel_i_spec
, wheel_i_pegs
,
348 wheel_v_spec
, wheel_v_pegs
,
349 wheel_iii_spec
, wheel_iii_pegs
,
351 'ua pf rq so ni ey bg hl tx zj')
353 # Settings for Bletchley Park outreach department's Enigma
354 self
.enigma_bp
= Enigma(reflector_b_spec
,
355 wheel_i_spec
, wheel_i_pegs
,
356 wheel_iii_spec
, wheel_iii_pegs
,
357 wheel_ii_spec
, wheel_ii_pegs
,
359 'qm we ro tu zj ps dl fg')
362 def test_middle_advance(self
):
363 self
.enigma
.set_wheels('a', 'a', 't')
364 self
.assertEqual(self
.enigma
.wheel_positions
, (0, 0, 19))
365 self
.assertEqual(cat(self
.enigma
.wheel_positions_l
), 'aat')
366 self
.assertEqual(self
.enigma
.peg_positions
, ([16], [4], [2]))
367 self
.assertEqual(cat(self
.enigma
.lookup(l
) for l
in string
.ascii_lowercase
),
368 'puvioztjdhxmlyeawsrgbcqknf')
370 self
.enigma
.advance()
371 self
.assertEqual(self
.enigma
.wheel_positions
, (0, 0, 20))
372 self
.assertEqual(cat(self
.enigma
.wheel_positions_l
), 'aau')
373 self
.assertEqual(self
.enigma
.peg_positions
, ([16], [4], [1]))
374 self
.assertEqual(cat(self
.enigma
.lookup(l
) for l
in string
.ascii_lowercase
),
375 'baigpldqcowfyzjehvtsxrkumn')
377 self
.enigma
.advance()
378 self
.assertEqual(self
.enigma
.wheel_positions
, (0, 0, 21))
379 self
.assertEqual(cat(self
.enigma
.wheel_positions_l
), 'aav')
380 self
.assertEqual(self
.enigma
.peg_positions
, ([16], [4], [0]))
381 self
.assertEqual(cat(self
.enigma
.lookup(l
) for l
in string
.ascii_lowercase
),
382 'mnvfydiwgzsoablrxpkutchqej')
384 self
.enigma
.advance()
385 self
.assertEqual(self
.enigma
.wheel_positions
, (0, 1, 22))
386 self
.assertEqual(cat(self
.enigma
.wheel_positions_l
), 'abw')
387 self
.assertEqual(self
.enigma
.peg_positions
, ([16], [3], [25]))
388 self
.assertEqual(cat(self
.enigma
.lookup(l
) for l
in string
.ascii_lowercase
),
389 'ulfopcykswhbzvderqixanjtgm')
391 self
.enigma
.advance()
392 self
.assertEqual(self
.enigma
.wheel_positions
, (0, 1, 23))
393 self
.assertEqual(cat(self
.enigma
.wheel_positions_l
), 'abx')
394 self
.assertEqual(self
.enigma
.peg_positions
, ([16], [3], [24]))
395 self
.assertEqual(cat(self
.enigma
.lookup(l
) for l
in string
.ascii_lowercase
),
396 'qmwftdyovursbzhxaklejicpgn')
398 self
.enigma
.advance()
399 self
.assertEqual(self
.enigma
.wheel_positions
, (0, 1, 24))
400 self
.assertEqual(cat(self
.enigma
.wheel_positions_l
), 'aby')
401 self
.assertEqual(self
.enigma
.peg_positions
, ([16], [3], [23]))
402 self
.assertEqual(cat(self
.enigma
.lookup(l
) for l
in string
.ascii_lowercase
),
403 'oljmzxrvucybdqasngpwihtfke')
406 def test_double_advance(self
):
407 self
.enigma
.set_wheels('a', 'd', 't')
408 self
.assertEqual(self
.enigma
.wheel_positions
, (0, 3, 19))
409 self
.assertEqual(cat(self
.enigma
.wheel_positions_l
), 'adt')
410 self
.assertEqual(self
.enigma
.peg_positions
, ([16], [1], [2]))
411 self
.assertEqual(cat(self
.enigma
.lookup(l
) for l
in string
.ascii_lowercase
),
412 'zcbpqxwsjiuonmldethrkygfva')
414 self
.enigma
.advance()
415 self
.assertEqual(self
.enigma
.wheel_positions
, (0, 3, 20))
416 self
.assertEqual(cat(self
.enigma
.wheel_positions_l
), 'adu')
417 self
.assertEqual(self
.enigma
.peg_positions
, ([16], [1], [1]))
418 self
.assertEqual(cat(self
.enigma
.lookup(l
) for l
in string
.ascii_lowercase
),
419 'ehprawjbngotxikcsdqlzyfmvu')
421 self
.enigma
.advance()
422 self
.assertEqual(self
.enigma
.wheel_positions
, (0, 3, 21))
423 self
.assertEqual(cat(self
.enigma
.wheel_positions_l
), 'adv')
424 self
.assertEqual(self
.enigma
.peg_positions
, ([16], [1], [0]))
425 self
.assertEqual(cat(self
.enigma
.lookup(l
) for l
in string
.ascii_lowercase
),
426 'eqzxarpihmnvjkwgbfuyslodtc')
428 self
.enigma
.advance()
429 self
.assertEqual(self
.enigma
.wheel_positions
, (0, 4, 22))
430 self
.assertEqual(cat(self
.enigma
.wheel_positions_l
), 'aew')
431 self
.assertEqual(self
.enigma
.peg_positions
, ([16], [0], [25]))
432 self
.assertEqual(cat(self
.enigma
.lookup(l
) for l
in string
.ascii_lowercase
),
433 'qedcbtpluzmhkongavwfirsyxj')
435 self
.enigma
.advance()
436 self
.assertEqual(self
.enigma
.wheel_positions
, (1, 5, 23))
437 self
.assertEqual(cat(self
.enigma
.wheel_positions_l
), 'bfx')
438 self
.assertEqual(self
.enigma
.peg_positions
, ([15], [25], [24]))
439 self
.assertEqual(cat(self
.enigma
.lookup(l
) for l
in string
.ascii_lowercase
),
440 'iwuedhsfazqxytvrkpgncoblmj')
442 self
.enigma
.advance()
443 self
.assertEqual(self
.enigma
.wheel_positions
, (1, 5, 24))
444 self
.assertEqual(cat(self
.enigma
.wheel_positions_l
), 'bfy')
445 self
.assertEqual(self
.enigma
.peg_positions
, ([15], [25], [23]))
446 self
.assertEqual(cat(self
.enigma
.lookup(l
) for l
in string
.ascii_lowercase
),
447 'baknstqzrmcxjdvygiefwoulph')
450 def test_simple_encipher(self
):
451 self
.enigma
.set_wheels('a', 'a', 'a')
452 ct
= self
.enigma
.encipher('testmessage')
453 self
.assertEqual(ct
, 'olpfhnvflyn')
455 self
.enigma
.set_wheels('a', 'd', 't')
456 ct
= self
.enigma
.encipher('testmessage')
457 self
.assertEqual(ct
, 'lawnjgpwjik')
459 self
.enigma
.set_wheels('b', 'd', 'q')
460 ct
= self
.enigma
.encipher('aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa')
462 'kvmmwrlqlqsqpeugjrcxzwpfyiyybwloewrouvkpoztceuwtfjzqwpbqldttsr')
463 self
.assertEqual(cat(self
.enigma
.wheel_positions_l
), 'cha')
466 def test_advance_with_ring_settings(self
):
467 self
.enigma31
.set_wheels('j', 'e', 'u')
469 self
.enigma31
.advance()
470 self
.assertEqual(self
.enigma31
.wheel_positions
, (4, 11, 24))
471 self
.assertEqual(cat(self
.enigma31
.wheel_positions_l
), 'jev')
472 self
.assertEqual(self
.enigma31
.peg_positions
, ([7], [21], [0]))
473 self
.assertEqual(cat(self
.enigma31
.lookup(l
) for l
in string
.ascii_lowercase
),
474 'mvqjlyowkdieasgzcunxrbhtfp')
476 self
.enigma31
.advance()
477 self
.assertEqual(self
.enigma31
.wheel_positions
, (4, 12, 25))
478 self
.assertEqual(cat(self
.enigma31
.wheel_positions_l
), 'jfw')
479 self
.assertEqual(self
.enigma31
.peg_positions
, ([7], [20], [25]))
480 self
.assertEqual(cat(self
.enigma31
.lookup(l
) for l
in string
.ascii_lowercase
),
481 'sjolzuyvrbwdpxcmtiaqfhknge')
483 self
.enigma31
.advance()
484 self
.assertEqual(self
.enigma31
.wheel_positions
, (4, 12, 0))
485 self
.assertEqual(cat(self
.enigma31
.wheel_positions_l
), 'jfx')
486 self
.assertEqual(self
.enigma31
.peg_positions
, ([7], [20], [24]))
487 self
.assertEqual(cat(self
.enigma31
.lookup(l
) for l
in string
.ascii_lowercase
),
488 'qrxedkoywufmlvgsabpzjnicht')
490 self
.enigma31
.advance()
491 self
.assertEqual(self
.enigma31
.wheel_positions
, (4, 12, 1))
492 self
.assertEqual(cat(self
.enigma31
.wheel_positions_l
), 'jfy')
493 self
.assertEqual(self
.enigma31
.peg_positions
, ([7], [20], [23]))
494 self
.assertEqual(cat(self
.enigma31
.lookup(l
) for l
in string
.ascii_lowercase
),
495 'hpsukliagqefwvtbjxcodnmrzy')
497 self
.enigma31
.advance()
498 self
.assertEqual(self
.enigma31
.wheel_positions
, (4, 12, 2))
499 self
.assertEqual(cat(self
.enigma31
.wheel_positions_l
), 'jfz')
500 self
.assertEqual(self
.enigma31
.peg_positions
, ([7], [20], [22]))
501 self
.assertEqual(cat(self
.enigma31
.lookup(l
) for l
in string
.ascii_lowercase
),
502 'zevnbpyqowrtxdifhkulscjmga')
505 def test_advance_with_ring_settings_2(self
):
506 self
.enigma31
.set_wheels('i', 'd', 'z')
508 self
.enigma31
.advance()
509 self
.assertEqual(self
.enigma31
.wheel_positions
, (3, 10, 3))
510 self
.assertEqual(cat(self
.enigma31
.wheel_positions_l
), 'ida')
511 self
.assertEqual(self
.enigma31
.peg_positions
, ([8], [22], [21]))
512 self
.assertEqual(cat(self
.enigma31
.lookup(l
) for l
in string
.ascii_lowercase
),
513 'ikhpqrvcambzjondefwyxgsutl')
515 self
.enigma31
.advance()
516 self
.assertEqual(self
.enigma31
.wheel_positions
, (3, 10, 4))
517 self
.assertEqual(cat(self
.enigma31
.wheel_positions_l
), 'idb')
518 self
.assertEqual(self
.enigma31
.peg_positions
, ([8], [22], [20]))
519 self
.assertEqual(cat(self
.enigma31
.lookup(l
) for l
in string
.ascii_lowercase
),
520 'cdabskhgzwfmlqvunyexpojtri')
522 self
.enigma31
.advance()
523 self
.assertEqual(self
.enigma31
.wheel_positions
, (3, 10, 5))
524 self
.assertEqual(cat(self
.enigma31
.wheel_positions_l
), 'idc')
525 self
.assertEqual(self
.enigma31
.peg_positions
, ([8], [22], [19]))
526 self
.assertEqual(cat(self
.enigma31
.lookup(l
) for l
in string
.ascii_lowercase
),
527 'pcbwiqhgemyvjsuaftnroldzkx')
529 self
.enigma31
.advance()
530 self
.assertEqual(self
.enigma31
.wheel_positions
, (3, 10, 6))
531 self
.assertEqual(cat(self
.enigma31
.wheel_positions_l
), 'idd')
532 self
.assertEqual(self
.enigma31
.peg_positions
, ([8], [22], [18]))
533 self
.assertEqual(cat(self
.enigma31
.lookup(l
) for l
in string
.ascii_lowercase
),
534 'xcbfvdnouptmlghjzwykierasq')
536 self
.enigma31
.advance()
537 self
.assertEqual(self
.enigma31
.wheel_positions
, (3, 10, 7))
538 self
.assertEqual(cat(self
.enigma31
.wheel_positions_l
), 'ide')
539 self
.assertEqual(self
.enigma31
.peg_positions
, ([8], [22], [17]))
540 self
.assertEqual(cat(self
.enigma31
.lookup(l
) for l
in string
.ascii_lowercase
),
541 'xfvglbdynuseriwqpmkzjcoaht')
543 self
.enigma31
.advance()
544 self
.assertEqual(self
.enigma31
.wheel_positions
, (3, 10, 8))
545 self
.assertEqual(cat(self
.enigma31
.wheel_positions_l
), 'idf')
546 self
.assertEqual(self
.enigma31
.peg_positions
, ([8], [22], [16]))
547 self
.assertEqual(cat(self
.enigma31
.lookup(l
) for l
in string
.ascii_lowercase
),
548 'tfpqlbouynsewjgcdxkahzmriv')
550 self
.enigma31
.advance()
551 self
.assertEqual(self
.enigma31
.wheel_positions
, (3, 10, 9))
552 self
.assertEqual(cat(self
.enigma31
.wheel_positions_l
), 'idg')
553 self
.assertEqual(self
.enigma31
.peg_positions
, ([8], [22], [15]))
554 self
.assertEqual(cat(self
.enigma31
.lookup(l
) for l
in string
.ascii_lowercase
),
555 'cjaunvlwtbygzexrspqidfhokm')
557 self
.enigma31
.advance()
558 self
.assertEqual(self
.enigma31
.wheel_positions
, (3, 10, 10))
559 self
.assertEqual(cat(self
.enigma31
.wheel_positions_l
), 'idh')
560 self
.assertEqual(self
.enigma31
.peg_positions
, ([8], [22], [14]))
561 self
.assertEqual(cat(self
.enigma31
.lookup(l
) for l
in string
.ascii_lowercase
),
562 'yltxkrqvowebzpingfucshjdam')
564 self
.enigma31
.advance()
565 self
.assertEqual(self
.enigma31
.wheel_positions
, (3, 10, 11))
566 self
.assertEqual(cat(self
.enigma31
.wheel_positions_l
), 'idi')
567 self
.assertEqual(self
.enigma31
.peg_positions
, ([8], [22], [13]))
568 self
.assertEqual(cat(self
.enigma31
.lookup(l
) for l
in string
.ascii_lowercase
),
569 'myktluzrnxceaiqsohpdfwvjbg')
571 self
.enigma31
.advance()
572 self
.assertEqual(self
.enigma31
.wheel_positions
, (3, 10, 12))
573 self
.assertEqual(cat(self
.enigma31
.wheel_positions_l
), 'idj')
574 self
.assertEqual(self
.enigma31
.peg_positions
, ([8], [22], [12]))
575 self
.assertEqual(cat(self
.enigma31
.lookup(l
) for l
in string
.ascii_lowercase
),
576 'pynjrmiugdqxfcvakewzhoslbt')
578 self
.enigma31
.advance()
579 self
.assertEqual(self
.enigma31
.wheel_positions
, (3, 10, 13))
580 self
.assertEqual(cat(self
.enigma31
.wheel_positions_l
), 'idk')
581 self
.assertEqual(self
.enigma31
.peg_positions
, ([8], [22], [11]))
582 self
.assertEqual(cat(self
.enigma31
.lookup(l
) for l
in string
.ascii_lowercase
),
583 'mwvedyplnoxhaijgrqtszcbkfu')
585 self
.enigma31
.advance()
586 self
.assertEqual(self
.enigma31
.wheel_positions
, (3, 10, 14))
587 self
.assertEqual(cat(self
.enigma31
.wheel_positions_l
), 'idl')
588 self
.assertEqual(self
.enigma31
.peg_positions
, ([8], [22], [10]))
589 self
.assertEqual(cat(self
.enigma31
.lookup(l
) for l
in string
.ascii_lowercase
),
590 'qcbrfeutvoxpnmjladzhgiykws')
592 self
.enigma31
.advance()
593 self
.assertEqual(self
.enigma31
.wheel_positions
, (3, 10, 15))
594 self
.assertEqual(cat(self
.enigma31
.wheel_positions_l
), 'idm')
595 self
.assertEqual(self
.enigma31
.peg_positions
, ([8], [22], [9]))
596 self
.assertEqual(cat(self
.enigma31
.lookup(l
) for l
in string
.ascii_lowercase
),
597 'dnoahryetsmukbcvwfjilpqzgx')
599 self
.enigma31
.advance()
600 self
.assertEqual(self
.enigma31
.wheel_positions
, (3, 10, 16))
601 self
.assertEqual(cat(self
.enigma31
.wheel_positions_l
), 'idn')
602 self
.assertEqual(self
.enigma31
.peg_positions
, ([8], [22], [8]))
603 self
.assertEqual(cat(self
.enigma31
.lookup(l
) for l
in string
.ascii_lowercase
),
604 'nidcfehgbqsovalyjzkxwmutpr')
606 self
.enigma31
.advance()
607 self
.assertEqual(self
.enigma31
.wheel_positions
, (3, 10, 17))
608 self
.assertEqual(cat(self
.enigma31
.wheel_positions_l
), 'ido')
609 self
.assertEqual(self
.enigma31
.peg_positions
, ([8], [22], [7]))
610 self
.assertEqual(cat(self
.enigma31
.lookup(l
) for l
in string
.ascii_lowercase
),
611 'joifxdulcarhzpbntkwqgysevm')
613 self
.enigma31
.advance()
614 self
.assertEqual(self
.enigma31
.wheel_positions
, (3, 10, 18))
615 self
.assertEqual(cat(self
.enigma31
.wheel_positions_l
), 'idp')
616 self
.assertEqual(self
.enigma31
.peg_positions
, ([8], [22], [6]))
617 self
.assertEqual(cat(self
.enigma31
.lookup(l
) for l
in string
.ascii_lowercase
),
618 'ptnlsxvozmwdjchayuebrgkfqi')
620 self
.enigma31
.advance()
621 self
.assertEqual(self
.enigma31
.wheel_positions
, (3, 10, 19))
622 self
.assertEqual(cat(self
.enigma31
.wheel_positions_l
), 'idq')
623 self
.assertEqual(self
.enigma31
.peg_positions
, ([8], [22], [5]))
624 self
.assertEqual(cat(self
.enigma31
.lookup(l
) for l
in string
.ascii_lowercase
),
625 'slwopzqnmxybihdeguavrtcjkf')
627 self
.enigma31
.advance()
628 self
.assertEqual(self
.enigma31
.wheel_positions
, (3, 10, 20))
629 self
.assertEqual(cat(self
.enigma31
.wheel_positions_l
), 'idr')
630 self
.assertEqual(self
.enigma31
.peg_positions
, ([8], [22], [4]))
631 self
.assertEqual(cat(self
.enigma31
.lookup(l
) for l
in string
.ascii_lowercase
),
632 'hcbedwlamzogixkytsrqvufnpj')
634 self
.enigma31
.advance()
635 self
.assertEqual(self
.enigma31
.wheel_positions
, (3, 10, 21))
636 self
.assertEqual(cat(self
.enigma31
.wheel_positions_l
), 'ids')
637 self
.assertEqual(self
.enigma31
.peg_positions
, ([8], [22], [3]))
638 self
.assertEqual(cat(self
.enigma31
.lookup(l
) for l
in string
.ascii_lowercase
),
639 'odxbjwzrmelkisavuhnyqpfctg')
641 self
.enigma31
.advance()
642 self
.assertEqual(self
.enigma31
.wheel_positions
, (3, 10, 22))
643 self
.assertEqual(cat(self
.enigma31
.wheel_positions_l
), 'idt')
644 self
.assertEqual(self
.enigma31
.peg_positions
, ([8], [22], [2]))
645 self
.assertEqual(cat(self
.enigma31
.lookup(l
) for l
in string
.ascii_lowercase
),
646 'udgbfeclrwnhxksvtioqapjmzy')
648 self
.enigma31
.advance()
649 self
.assertEqual(self
.enigma31
.wheel_positions
, (3, 10, 23))
650 self
.assertEqual(cat(self
.enigma31
.wheel_positions_l
), 'idu')
651 self
.assertEqual(self
.enigma31
.peg_positions
, ([8], [22], [1]))
652 self
.assertEqual(cat(self
.enigma31
.lookup(l
) for l
in string
.ascii_lowercase
),
653 'nrdczqxmowvshaiufblypkjgte')
655 self
.enigma31
.advance()
656 self
.assertEqual(self
.enigma31
.wheel_positions
, (3, 10, 24))
657 self
.assertEqual(cat(self
.enigma31
.wheel_positions_l
), 'idv')
658 self
.assertEqual(self
.enigma31
.peg_positions
, ([8], [22], [0]))
659 self
.assertEqual(cat(self
.enigma31
.lookup(l
) for l
in string
.ascii_lowercase
),
660 'hkifjdoacebqtzgulyvmpsxwrn')
662 self
.enigma31
.advance()
663 self
.assertEqual(self
.enigma31
.wheel_positions
, (3, 11, 25))
664 self
.assertEqual(cat(self
.enigma31
.wheel_positions_l
), 'iew')
665 self
.assertEqual(self
.enigma31
.peg_positions
, ([8], [21], [25]))
666 self
.assertEqual(cat(self
.enigma31
.lookup(l
) for l
in string
.ascii_lowercase
),
667 'yptzuhofqvnmlkgbixwcejsrad')
669 self
.enigma31
.advance()
670 self
.assertEqual(self
.enigma31
.wheel_positions
, (3, 11, 0))
671 self
.assertEqual(cat(self
.enigma31
.wheel_positions_l
), 'iex')
672 self
.assertEqual(self
.enigma31
.peg_positions
, ([8], [21], [24]))
673 self
.assertEqual(cat(self
.enigma31
.lookup(l
) for l
in string
.ascii_lowercase
),
674 'vkdcwhqfjibzsptngumoraeyxl')
676 self
.enigma31
.advance()
677 self
.assertEqual(self
.enigma31
.wheel_positions
, (3, 11, 1))
678 self
.assertEqual(cat(self
.enigma31
.wheel_positions_l
), 'iey')
679 self
.assertEqual(self
.enigma31
.peg_positions
, ([8], [21], [23]))
680 self
.assertEqual(cat(self
.enigma31
.lookup(l
) for l
in string
.ascii_lowercase
),
681 'wenpbqrouxlkychdfgzvitajms')
683 def test_double_advance_with_ring_settings_2(self
):
684 self
.enigma31
.set_wheels('a', 'y', 't')
685 self
.assertEqual(self
.enigma31
.wheel_positions
, (21, 5, 22))
686 self
.assertEqual(cat(self
.enigma31
.wheel_positions_l
), 'ayt')
687 self
.assertEqual(self
.enigma31
.peg_positions
, ([16], [1], [2]))
689 self
.enigma31
.advance()
690 self
.assertEqual(self
.enigma31
.wheel_positions
, (21, 5, 23))
691 self
.assertEqual(cat(self
.enigma31
.wheel_positions_l
), 'ayu')
692 self
.assertEqual(self
.enigma31
.peg_positions
, ([16], [1], [1]))
694 self
.enigma31
.advance()
695 self
.assertEqual(self
.enigma31
.wheel_positions
, (21, 5, 24))
696 self
.assertEqual(cat(self
.enigma31
.wheel_positions_l
), 'ayv')
697 self
.assertEqual(self
.enigma31
.peg_positions
, ([16], [1], [0]))
699 self
.enigma31
.advance()
700 self
.assertEqual(self
.enigma31
.wheel_positions
, (21, 6, 25))
701 self
.assertEqual(cat(self
.enigma31
.wheel_positions_l
), 'azw')
702 self
.assertEqual(self
.enigma31
.peg_positions
, ([16], [0], [25]))
704 self
.enigma31
.advance()
705 self
.assertEqual(self
.enigma31
.wheel_positions
, (22, 7, 0))
706 self
.assertEqual(cat(self
.enigma31
.wheel_positions_l
), 'bax')
707 self
.assertEqual(self
.enigma31
.peg_positions
, ([15], [25], [24]))
709 self
.enigma31
.advance()
710 self
.assertEqual(self
.enigma31
.wheel_positions
, (22, 7, 1))
711 self
.assertEqual(cat(self
.enigma31
.wheel_positions_l
), 'bay')
712 self
.assertEqual(self
.enigma31
.peg_positions
, ([15], [25], [23]))
714 self
.enigma31
.set_wheels('a', 'z', 't')
715 self
.assertEqual(self
.enigma31
.wheel_positions
, (21, 6, 22))
716 self
.assertEqual(cat(self
.enigma31
.wheel_positions_l
), 'azt')
717 self
.assertEqual(self
.enigma31
.peg_positions
, ([16], [0], [2]))
719 self
.enigma31
.advance()
720 self
.assertEqual(self
.enigma31
.wheel_positions
, (22, 7, 23))
721 self
.assertEqual(cat(self
.enigma31
.wheel_positions_l
), 'bau')
722 self
.assertEqual(self
.enigma31
.peg_positions
, ([15], [25], [1]))
724 self
.enigma31
.advance()
725 self
.assertEqual(self
.enigma31
.wheel_positions
, (22, 7, 24))
726 self
.assertEqual(cat(self
.enigma31
.wheel_positions_l
), 'bav')
727 self
.assertEqual(self
.enigma31
.peg_positions
, ([15], [25], [0]))
729 self
.enigma31
.advance()
730 self
.assertEqual(self
.enigma31
.wheel_positions
, (22, 8, 25))
731 self
.assertEqual(cat(self
.enigma31
.wheel_positions_l
), 'bbw')
732 self
.assertEqual(self
.enigma31
.peg_positions
, ([15], [24], [25]))
734 self
.enigma31
.advance()
735 self
.assertEqual(self
.enigma31
.wheel_positions
, (22, 8, 0))
736 self
.assertEqual(cat(self
.enigma31
.wheel_positions_l
), 'bbx')
737 self
.assertEqual(self
.enigma31
.peg_positions
, ([15], [24], [24]))
739 self
.enigma31
.advance()
740 self
.assertEqual(self
.enigma31
.wheel_positions
, (22, 8, 1))
741 self
.assertEqual(cat(self
.enigma31
.wheel_positions_l
), 'bby')
742 self
.assertEqual(self
.enigma31
.peg_positions
, ([15], [24], [23]))
745 def test_encipher_with_ring(self
):
747 self
.enigma31
.set_wheels('i', 'z', 'd')
748 ct
= self
.enigma31
.encipher('verylongtestmessagewithanextrabitofmessageforgoodmeasure')
750 'apocwtjuikurcfivlozvhffkoacxufcekthcvodfqpxdjqyckdozlqki')
751 self
.assertEqual(self
.enigma31
.wheel_positions
, (4, 9, 10))
752 self
.assertEqual(cat(self
.enigma31
.wheel_positions_l
), 'jch')
753 self
.assertEqual(self
.enigma31
.peg_positions
, ([7], [23], [14]))
754 self
.assertEqual(cat(self
.enigma31
.lookup(l
) for l
in string
.ascii_lowercase
),
755 'mopnigfuesqwadbcktjrhylzvx')
757 self
.enigma31
.set_wheels('i', 'z', 'd')
758 pt
= self
.enigma31
.decipher('apocwtjuikurcfivlozvhffkoacxufcekthcvodfqpxdjqyckdozlqki')
760 'verylongtestmessagewithanextrabitofmessageforgoodmeasure')
762 if __name__
== '__main__':