示例#1
0
        public void Encode_InsertEncodedString_ReturnDecodedString()
        {
            // Arrange
            string input = "gci";

            ///  |R-1|R-2|R-3|U-A|R-3|R-2|R-1|
            ///1 A---K---L---V---I---Q---Q---G
            ///2 A---M---W---U---P---H---L---C
            ///3 A---F---I---R---N---N---T---I
            string expectedOutput = "AAA";

            UkwType ukwType = new UkwA();
            Rotor[] rotors = new Rotor[] { new Rotor1(), new Rotor2(), new Rotor3() };
            int[] rotorPositions = new int[] { 1, 1, 1 }; // A, A, A

            for (int i = 0, amountOfRotors = rotors.Length; i < amountOfRotors; i++)
            {
                rotors[i].SetPosition(rotorPositions[i]);
            }

            Dictionary<char, char> plugboard = new Dictionary<char, char>();
            IEnigmaEncoder encoder = new Enigma_I(ukwType, rotors, rotorPositions, plugboard);

            // Act
            string output = encoder.Encode(input);

            // Assert
            Assert.AreEqual(expectedOutput, output);
        }
        public void NewEnigmaEncoder_InsertEnigmaIWithValideRotorsAndUkwType_ReturnEnigmaI()
        {
            // Arrange
            string enigmaEncoderType = "Enigma_I";

            string uwkTypeName = "UKW-A";
            UkwType ukwType = new UkwA();

            string[] rotorNames = new string[] { "I", "V", "IV" };
            Rotor[] rotors = new Rotor[] { new Rotor1(), new Rotor5(), new Rotor4() };

            int[] rotorPositions = new int[] { 5, 2, 8 }; // E, B, H

            Dictionary<char, char> plugboard = null;
            Dictionary<char, char> testPlugboard = new Dictionary<char, char>();

            IEnigmaEncoder expectedEncoder = new Enigma_I(ukwType, rotors, rotorPositions, testPlugboard);

            // Act
            IEnigmaEncoder enigmaEncoder = EnigmaEncoderFactory.NewEnigmaEncoder(enigmaEncoderType, uwkTypeName, rotorNames, rotorPositions, plugboard);

            // Assert
            bool passedTest = false;

            if (enigmaEncoder.GetUkwType().Identifier == expectedEncoder.GetUkwType().Identifier &&
                enigmaEncoder.GetRotors()[0].Identifier == expectedEncoder.GetRotors()[0].Identifier &&
                enigmaEncoder.GetRotors()[1].Identifier == expectedEncoder.GetRotors()[1].Identifier &&
                enigmaEncoder.GetRotors()[2].Identifier == expectedEncoder.GetRotors()[2].Identifier &&
                enigmaEncoder.GetRotorPositions()[0] == expectedEncoder.GetRotorPositions()[0] &&
                enigmaEncoder.GetRotorPositions()[1] == expectedEncoder.GetRotorPositions()[1] &&
                enigmaEncoder.GetRotorPositions()[2] == expectedEncoder.GetRotorPositions()[2])
            {
                passedTest = true;
            }

            Assert.IsTrue(passedTest);
        }
示例#3
0
        public void Encode_EncodeMessageWithPlugboardSetup_ReturnEncodedString()
        {
            // Arrange
            string input = "aaa";

            ///  |PLU|R-1|R-2|R-3|U-A|R-3|R-2|R-1|PLU|
            ///1 A---D---L---H---P---U---W---M---B---B
            ///2 A---D---G---R---W---K---U---H---N---L
            ///3 A---D---D---K---X---H---D---C---V---V
            string expectedOutput = "BLV";

            UkwType ukwType = new UkwA();
            Rotor[] rotors = new Rotor[] { new Rotor1(), new Rotor2(), new Rotor3() };
            int[] rotorPositions = new int[] { 1, 1, 1 }; // A, A, A

            for (int i = 0, amountOfRotors = rotors.Length; i < amountOfRotors; i++)
            {
                rotors[i].SetPosition(rotorPositions[i]);
            }

            Dictionary<char, char> plugboard = new Dictionary<char, char>();
            plugboard['A'] = 'D';
            plugboard['D'] = 'A';
            plugboard['N'] = 'L';
            plugboard['L'] = 'N';

            IEnigmaEncoder encoder = new Enigma_I(ukwType, rotors, rotorPositions, plugboard);

            // Act
            string output = encoder.Encode(input);

            // Assert
            Assert.AreEqual(expectedOutput, output);
        }
        private static IEnigmaEncoder selectEnigmaEncoder(string enigmaEncoderType, UkwType ukwType, Rotor[] rotors,
            int[] rotorPositions, Dictionary<char, char> plugboard)
        {
            IEnigmaEncoder enigmaEncoder = null;

            switch (enigmaEncoderType)
            {
                case "Enigma_I":
                    enigmaEncoder = new Enigma_I(ukwType, rotors, rotorPositions, plugboard);
                    break;
                default:
                    throw new ArgumentException("Unknown Enigma encoder type: " + enigmaEncoderType);
            }

            return enigmaEncoder;
        }