Updated after tests with real Enigma machine
[cipher-training.git] / test_enigma.py
1 import unittest
2 import collections
3
4 from enigma import *
5
6 class LetterTransformerTest(unittest.TestCase):
7
8 def test_maps1(self):
9 lt = LetterTransformer([('z', 'a')] + \
10 list(zip(string.ascii_lowercase, string.ascii_lowercase[1:])),
11 raw_transform = True)
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])
18
19
20 def test_maps2(self):
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])
28
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')
35
36
37 class PlugboardTest(unittest.TestCase):
38 def setUp(self):
39 self.pb = Plugboard('ua pf rq so ni ey bg hl tx zj'.upper())
40
41 def test_maps(self):
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)
46
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')
54
55
56 class ReflectorTest(unittest.TestCase):
57 def setUp(self):
58 self.ref = Reflector(reflector_b_spec)
59
60 def test_maps(self):
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)
65
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')
73
74
75 class SimpleWheelTest(unittest.TestCase):
76 def test_init1(self):
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')
86
87 def test_init2(self):
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')
95
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')
101
102 wheel_3.set_position('f')
103 self.assertEqual(wheel_3.position, 5)
104 self.assertEqual(wheel_3.position_l, 'f')
105
106 wheel_3.advance()
107 self.assertEqual(wheel_3.position, 6)
108 self.assertEqual(wheel_3.position_l, 'g')
109
110 wheel_3.set_position(12)
111 self.assertEqual(wheel_3.position, 12)
112 self.assertEqual(wheel_3.position_l, 'm')
113
114 wheel_3.advance()
115 self.assertEqual(wheel_3.position, 13)
116 self.assertEqual(wheel_3.position_l, 'n')
117
118
119 def test_advance(self):
120 wheel_3 = SimpleWheel(wheel_iii_spec)
121 wheel_3.set_position('a')
122 wheel_3.advance()
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')
131
132 for _ in range(24): wheel_3.advance()
133
134 self.assertEqual(wheel_3.position, 25)
135 self.assertEqual(wheel_3.position_l, 'z')
136
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')
143
144 wheel_3.advance()
145 self.assertEqual(wheel_3.position, 0)
146 self.assertEqual(wheel_3.position_l, 'a')
147
148
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')
155
156
157 class WheelTest(unittest.TestCase):
158 def test_init1(self):
159 wheel = Wheel(wheel_iii_spec, wheel_iii_pegs, position='b',
160 ring_setting=1)
161 self.assertEqual(wheel.position, 1)
162 self.assertEqual(wheel.peg_positions, [20])
163 self.assertEqual(wheel.position_l, 'b')
164
165 wheel.advance()
166 self.assertEqual(wheel.position, 2)
167 self.assertEqual(wheel.peg_positions, [19])
168 self.assertEqual(wheel.position_l, 'c')
169
170 def test_init2(self):
171 wheel = Wheel(wheel_vi_spec, wheel_vi_pegs, position='b',
172 ring_setting=3)
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')
183
184 def test_advance(self):
185 wheel = Wheel(wheel_vi_spec, wheel_vi_pegs, position='b',
186 ring_setting=3)
187 wheel.advance()
188
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')
199
200 def test_advance_23(self):
201 wheel = Wheel(wheel_vi_spec, wheel_vi_pegs, position='b',
202 ring_setting=3)
203 for _ in range(23):
204 wheel.advance()
205
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')
216
217 def test_advance_24(self):
218 wheel = Wheel(wheel_vi_spec, wheel_vi_pegs, position='b',
219 ring_setting=3)
220 for _ in range(24):
221 wheel.advance()
222
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')
233
234 def test_advance_25(self):
235 wheel = Wheel(wheel_vi_spec, wheel_vi_pegs, position='b',
236 ring_setting=3)
237 for _ in range(25):
238 wheel.advance()
239
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')
250
251 def test_advance_26(self):
252 wheel = Wheel(wheel_vi_spec, wheel_vi_pegs, position='b',
253 ring_setting=3)
254 for _ in range(26):
255 wheel.advance()
256
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')
267
268 def test_advance_27(self):
269 wheel = Wheel(wheel_vi_spec, wheel_vi_pegs, position='b',
270 ring_setting=3)
271 for _ in range(27):
272 wheel.advance()
273
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')
284
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])
291
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])
296
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])
301
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])
306
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])
311
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])
316
317 wheel_3.advance()
318 self.assertEqual(wheel_3.position, 4)
319 self.assertEqual(wheel_3.position_l, 'g')
320 self.assertEqual(wheel_3.peg_positions, [15])
321
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])
326
327 wheel_3.advance()
328 self.assertEqual(wheel_3.position, 10)
329 self.assertEqual(wheel_3.position_l, 'm')
330 self.assertEqual(wheel_3.peg_positions, [9])
331
332
333 class EnigmaTest(unittest.TestCase):
334
335 def setUp(self):
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,
340 1, 1, 1,
341 '')
342
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,
350 6, 20, 24,
351 'ua pf rq so ni ey bg hl tx zj')
352
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,
358 1, 26, 26,
359 'qm we ro tu zj ps dl fg')
360
361
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')
369
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')
376
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')
383
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')
390
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')
397
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')
404
405
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')
413
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')
420
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')
427
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')
434
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')
441
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')
448
449
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')
454
455 self.enigma.set_wheels('a', 'd', 't')
456 ct = self.enigma.encipher('testmessage')
457 self.assertEqual(ct, 'lawnjgpwjik')
458
459 self.enigma.set_wheels('b', 'd', 'q')
460 ct = self.enigma.encipher('aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa')
461 self.assertEqual(ct,
462 'kvmmwrlqlqsqpeugjrcxzwpfyiyybwloewrouvkpoztceuwtfjzqwpbqldttsr')
463 self.assertEqual(cat(self.enigma.wheel_positions_l), 'cha')
464
465
466 def test_advance_with_ring_settings(self):
467 self.enigma31.set_wheels('j', 'e', 'u')
468
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')
475
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')
482
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')
489
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')
496
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')
503
504
505 def test_advance_with_ring_settings_2(self):
506 self.enigma31.set_wheels('i', 'd', 'z')
507
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')
514
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')
521
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')
528
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')
535
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')
542
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')
549
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')
556
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')
563
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')
570
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')
577
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')
584
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')
591
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')
598
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')
605
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')
612
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')
619
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')
626
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')
633
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')
640
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')
647
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')
654
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')
661
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')
668
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')
675
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')
682
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]))
688
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]))
693
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]))
698
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]))
703
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]))
708
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]))
713
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]))
718
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]))
723
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]))
728
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]))
733
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]))
738
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]))
743
744
745 def test_encipher_with_ring(self):
746
747 self.enigma31.set_wheels('i', 'z', 'd')
748 ct = self.enigma31.encipher('verylongtestmessagewithanextrabitofmessageforgoodmeasure')
749 self.assertEqual(ct,
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')
756
757 self.enigma31.set_wheels('i', 'z', 'd')
758 pt = self.enigma31.decipher('apocwtjuikurcfivlozvhffkoacxufcekthcvodfqpxdjqyckdozlqki')
759 self.assertEqual(pt,
760 'verylongtestmessagewithanextrabitofmessageforgoodmeasure')
761
762 if __name__ == '__main__':
763 unittest.main()