Example #1
0
        public void AlphabeticalRotorStepsCorrectly()
        {
            Rotor rotor = new Rotor(AlphabetUtils.AlphabetString, "rotor");

            // Step one notch.
            rotor.SetRotorKey('B');

            Assert.AreEqual('B', alphabet[rotor.GetValueFromKey(alphabet['A'], false)]);
            Assert.AreEqual('C', alphabet[rotor.GetValueFromKey(alphabet['B'], false)]);
            Assert.AreEqual('D', alphabet[rotor.GetValueFromKey(alphabet['C'], false)]);
            Assert.AreEqual('E', alphabet[rotor.GetValueFromKey(alphabet['D'], false)]);
            Assert.AreEqual('F', alphabet[rotor.GetValueFromKey(alphabet['E'], false)]);
            Assert.AreEqual('G', alphabet[rotor.GetValueFromKey(alphabet['F'], false)]);
            Assert.AreEqual('H', alphabet[rotor.GetValueFromKey(alphabet['G'], false)]);
            Assert.AreEqual('I', alphabet[rotor.GetValueFromKey(alphabet['H'], false)]);
            Assert.AreEqual('J', alphabet[rotor.GetValueFromKey(alphabet['I'], false)]);
            Assert.AreEqual('K', alphabet[rotor.GetValueFromKey(alphabet['J'], false)]);
            Assert.AreEqual('L', alphabet[rotor.GetValueFromKey(alphabet['K'], false)]);
            Assert.AreEqual('M', alphabet[rotor.GetValueFromKey(alphabet['L'], false)]);
            Assert.AreEqual('N', alphabet[rotor.GetValueFromKey(alphabet['M'], false)]);
            Assert.AreEqual('O', alphabet[rotor.GetValueFromKey(alphabet['N'], false)]);
            Assert.AreEqual('P', alphabet[rotor.GetValueFromKey(alphabet['O'], false)]);
            Assert.AreEqual('Q', alphabet[rotor.GetValueFromKey(alphabet['P'], false)]);
            Assert.AreEqual('R', alphabet[rotor.GetValueFromKey(alphabet['Q'], false)]);
            Assert.AreEqual('S', alphabet[rotor.GetValueFromKey(alphabet['R'], false)]);
            Assert.AreEqual('T', alphabet[rotor.GetValueFromKey(alphabet['S'], false)]);
            Assert.AreEqual('U', alphabet[rotor.GetValueFromKey(alphabet['T'], false)]);
            Assert.AreEqual('V', alphabet[rotor.GetValueFromKey(alphabet['U'], false)]);
            Assert.AreEqual('W', alphabet[rotor.GetValueFromKey(alphabet['V'], false)]);
            Assert.AreEqual('X', alphabet[rotor.GetValueFromKey(alphabet['W'], false)]);
            Assert.AreEqual('Y', alphabet[rotor.GetValueFromKey(alphabet['X'], false)]);
            Assert.AreEqual('Z', alphabet[rotor.GetValueFromKey(alphabet['Y'], false)]);
            Assert.AreEqual('A', alphabet[rotor.GetValueFromKey(alphabet['Z'], false)]);
        }
Example #2
0
        public void AlphabeticalRotorReturnsCorrectMapping()
        {
            Rotor rotor = new Rotor(AlphabetUtils.AlphabetString, "rotor");

            foreach (char letter in AlphabetUtils.Alphabet)
            {
                Assert.AreEqual(letter, alphabet[rotor.GetValueFromKey(alphabet[letter], false)]);
            }
        }
Example #3
0
        public void NullListArgumentConstructor()
        {
            Rotor[] rotors = new Rotor[3];

            rotors[0] = null;
            rotors[1] = new Rotor();
            rotors[2] = new Rotor();

            Enigma enigma = new Enigma(rotors);
        }
Example #4
0
        public void ConstructorReturnsCorrectRotorCount()
        {
            Rotor[] rotors = new Rotor[4];

            rotors[0] = new Rotor(AlphabetUtils.AlphabetString, "rotor");
            rotors[1] = new Rotor(AlphabetUtils.AlphabetString, "rotor");
            rotors[2] = new Rotor(AlphabetUtils.AlphabetString, "rotor");
            rotors[3] = new Rotor(AlphabetUtils.AlphabetString, "rotor");
            rotors[3].IsReflector = true;

            Enigma enigma = new Enigma(rotors);
            Assert.AreEqual(4, enigma.Rotors.Count);
        }
Example #5
0
        public void SetEncryptionKeyReturnsCorrectTextForOneRotor()
        {
            const string plaintext = "HELLO";

            Rotor[] rotors = new Rotor[1];

            rotors[0] = new Rotor(AlphabetUtils.AlphabetString, "rotor");

            Enigma enigma = new Enigma(rotors);

            enigma.SetEncryptionKey("A");
            Assert.AreEqual(AlphabetUtils.AlphabetString, enigma.SubmitString(AlphabetUtils.AlphabetString));
            Assert.AreEqual(AlphabetUtils.ReverseAlphabetString, enigma.SubmitString(AlphabetUtils.ReverseAlphabetString));
            Assert.AreEqual(plaintext, enigma.SubmitString(plaintext));
        }
Example #6
0
        public void PressKeyWithOneRotorReturnsCorrectLetter()
        {
            Rotor[] rotors = new Rotor[1];

            rotors[0] = new Rotor();

            Enigma enigma = new Enigma(rotors);

            Assert.AreEqual('Z', enigma.PressKey('A'));
            Assert.AreEqual('Y', enigma.PressKey('B'));
            Assert.AreEqual('X', enigma.PressKey('C'));
            Assert.AreEqual('W', enigma.PressKey('D'));
            Assert.AreEqual('V', enigma.PressKey('E'));
            Assert.AreEqual('U', enigma.PressKey('F'));
        }
Example #7
0
 public void WrontRotorTypeGeneratesException()
 {
     Rotor rotor = new Rotor(AlphabetUtils.AlphabetString, "error");
 }
Example #8
0
        public void RotorConstructor()
        {
            Rotor rotor = new Rotor();

            Assert.AreEqual(26, rotor.Length);
        }
Example #9
0
        private IEnumerable<Rotor> GetRotorsTemplates(XDocument doc)
        {
            IEnumerable<XElement> rotorCollection;

            try
            {
                rotorCollection = doc.Root.Element("rotors").Elements("rotor");
            }

            catch (Exception)
            {
                throw;
            }

            var rotorDescriptors = from rotor in rotorCollection
                                   select new RotorDescriptor
                                   {
                                       Name = (string)rotor.Attribute("name"),
                                       Type = (string)rotor.Attribute("type"),
                                       Id = (string)rotor.Element("id"),
                                       Mapping = (string)rotor.Element("mapping"),
                                       Date = (string)rotor.Element("date"),
                                       Model = (string)rotor.Element("model")
                                   };

            List<Rotor> result = new List<Rotor>(rotorDescriptors.Count());

            foreach (var descriptor in rotorDescriptors)
            {
                Rotor rotor = new Rotor(descriptor);

                result.Add(rotor);
            }

            return result;
        }
Example #10
0
        public void SetEncryptionKeyReturnsCorrectTextForOneRotorAndOneDeflectorAndOneLetterInput()
        {
            const string plaintext = "A";

            Rotor[] rotors = new Rotor[2];

            rotors[0] = new Rotor(AlphabetUtils.AlphabetString, "rotor");
            rotors[1] = new Rotor(AlphabetUtils.ReverseAlphabetString, "reflector");

            Enigma enigma = new Enigma(rotors);

            enigma.SetEncryptionKey("BB");

            string ciphertext = enigma.SubmitString(plaintext);

            Assert.AreEqual(plaintext, enigma.SubmitString(ciphertext), "Encryption symmetry is not ensured.");
        }
Example #11
0
        public void SymmetryIsEnsuredWithOneFixedRotorAndReflector()
        {
            const string plaintext = "HELLO";

            Rotor[] rotors = new Rotor[2];
            rotors[0] = new Rotor(AlphabetUtils.AlphabetString, "rotor");
            rotors[1] = new Rotor(AlphabetUtils.ReverseAlphabetString, "reflector");

            Enigma enigma = new Enigma(rotors);

            enigma.SetEncryptionKey("BB");

            string ciphertext = enigma.SubmitString(plaintext);

            Assert.AreEqual(plaintext, enigma.SubmitString(ciphertext), "Encryption symmetry is not ensured.");
        }