2015-07-18 08:48:07 -07:00
|
|
|
//
|
|
|
|
// EnigmaTests.swift
|
|
|
|
// EnigmaTests
|
|
|
|
//
|
|
|
|
// Created by Eryn Wells on 2015-07-18.
|
|
|
|
// Copyright © 2015 Eryn Wells. All rights reserved.
|
|
|
|
//
|
|
|
|
|
|
|
|
import XCTest
|
2015-07-18 10:39:53 -07:00
|
|
|
@testable import Enigma
|
|
|
|
|
2015-07-18 18:37:58 -07:00
|
|
|
let alphaSeries = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
|
|
|
|
|
2015-07-19 01:30:38 -07:00
|
|
|
|
|
|
|
class MachineTests: XCTestCase {
|
|
|
|
func testThatItTurnsOn() {
|
|
|
|
let machine = try! Machine(rotors: [Rotor(.EnigmaI), Rotor(.EnigmaII), Rotor(.EnigmaIII)], reflector: Reflector(.EnigmaB), plugboard: Plugboard())
|
|
|
|
print(try! machine.encode("A"))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-07-18 10:39:53 -07:00
|
|
|
class RotorTests: XCTestCase {
|
|
|
|
let rotorSeries = "EKMFLGDQVZNTOWYHXUSPAIBRCJ"
|
2015-07-19 08:40:29 -07:00
|
|
|
let rot13Series = "NOPQRSTUVWXYZABCDEFGHIJKLM"
|
|
|
|
|
|
|
|
func makeRotorWithSeries(series: String) -> Rotor! {
|
|
|
|
do {
|
|
|
|
return try Rotor(series: series)
|
|
|
|
} catch let error {
|
|
|
|
XCTFail("Unable to create rotor: \(error)")
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2015-07-18 10:39:53 -07:00
|
|
|
|
2015-07-18 10:47:14 -07:00
|
|
|
func testThatUnadvancedSubstitutionWorks() {
|
2015-07-19 08:40:29 -07:00
|
|
|
let rotor = makeRotorWithSeries(rotorSeries)
|
2015-07-18 10:47:14 -07:00
|
|
|
for (plainCharacter, cipherCharacter) in zip(alphaSeries.characters, rotorSeries.characters) {
|
|
|
|
XCTAssertEqual(try! rotor.encode(plainCharacter), cipherCharacter)
|
|
|
|
}
|
2015-07-18 10:39:53 -07:00
|
|
|
}
|
|
|
|
|
2015-07-18 22:59:58 -07:00
|
|
|
func testThatRotorCanAdvanceToRot13() {
|
2015-07-19 08:40:29 -07:00
|
|
|
let rotor = makeRotorWithSeries(alphaSeries)
|
2015-07-18 10:47:14 -07:00
|
|
|
rotor.advance(13)
|
|
|
|
for (plainCharacter, cipherCharacter) in zip(alphaSeries.characters, rot13Series.characters) {
|
2015-07-18 10:39:53 -07:00
|
|
|
XCTAssertEqual(try! rotor.encode(plainCharacter), cipherCharacter)
|
|
|
|
}
|
|
|
|
}
|
2015-07-18 22:59:58 -07:00
|
|
|
|
|
|
|
func testThatRotorCanSetToRot13() {
|
2015-07-19 08:40:29 -07:00
|
|
|
let rotor = makeRotorWithSeries(alphaSeries)
|
2015-07-18 22:59:58 -07:00
|
|
|
rotor.position = 13
|
|
|
|
for (plainCharacter, cipherCharacter) in zip(alphaSeries.characters, rot13Series.characters) {
|
|
|
|
XCTAssertEqual(try! rotor.encode(plainCharacter), cipherCharacter)
|
|
|
|
}
|
|
|
|
}
|
2015-07-19 08:40:29 -07:00
|
|
|
|
|
|
|
func testThatRotorCanDoInverseEncoding() {
|
|
|
|
let rotor = makeRotorWithSeries(rotorSeries)
|
|
|
|
for (plainCharacter, cipherCharacter) in zip(alphaSeries.characters, rotorSeries.characters) {
|
|
|
|
do {
|
|
|
|
let encodedCharacter = try rotor.inverseEncode(cipherCharacter)
|
|
|
|
XCTAssertEqual(encodedCharacter, plainCharacter)
|
|
|
|
} catch let error {
|
|
|
|
XCTFail("Error inverse-encoding \(cipherCharacter) -> \(plainCharacter): \(error)")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func testThatRingSettingWorks() {
|
|
|
|
let rotor = makeRotorWithSeries(alphaSeries)
|
|
|
|
rotor.ringPosition = 1
|
|
|
|
let characters = Array(alphaSeries.characters)
|
|
|
|
for (index, c) in alphaSeries.characters.enumerate() {
|
|
|
|
do {
|
|
|
|
let encodedCharacter = try rotor.encode(c)
|
|
|
|
XCTAssertEqual(encodedCharacter, characters[(index + 1) % characters.count])
|
|
|
|
} catch let error {
|
|
|
|
XCTFail("Error encoding with ring setting = 1: \(error)")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-07-18 18:37:58 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-07-19 00:00:59 -07:00
|
|
|
class ReflectorTests: XCTestCase {
|
|
|
|
func testThatReflectorReflects() {
|
|
|
|
var reflector: Reflector! = nil
|
|
|
|
do {
|
|
|
|
reflector = try Reflector(series: Reflector.Wiring.EnigmaA.rawValue)
|
|
|
|
} catch let error {
|
|
|
|
XCTFail("Error creating reflector: \(error)")
|
|
|
|
}
|
|
|
|
do {
|
|
|
|
let encodeA = try reflector.encode("A")
|
|
|
|
let encodeE = try reflector.encode("E")
|
|
|
|
XCTAssertEqual(encodeA, "E")
|
|
|
|
XCTAssertEqual(encodeE, "A")
|
|
|
|
} catch {
|
|
|
|
XCTFail("Reflector encoding failed")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-07-18 18:37:58 -07:00
|
|
|
class PlugboardTests: XCTestCase {
|
|
|
|
func testThatEmptyPlugboardPassesThroughAllCharacters() {
|
2015-07-18 22:59:58 -07:00
|
|
|
let plugboard = Plugboard()
|
2015-07-18 18:37:58 -07:00
|
|
|
for c in alphaSeries.characters {
|
|
|
|
XCTAssertEqual(try! plugboard.encode(c), c)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func testThatPlugboardPairsAreBidirectional() {
|
2015-07-18 22:59:58 -07:00
|
|
|
let plugboard = Plugboard()
|
|
|
|
plugboard.addPlug("A", b: "H")
|
2015-07-18 18:37:58 -07:00
|
|
|
XCTAssertEqual(try! plugboard.encode("A"), "H")
|
|
|
|
XCTAssertEqual(try! plugboard.encode("H"), "A")
|
|
|
|
}
|
2015-07-18 10:39:53 -07:00
|
|
|
}
|