Example #1
0
        public void TestStreamWritePositiveShift()
        {
            const int N_SAMPLES = 100;

            var rnd = new Random();

            var bufferLength = rnd.Next(100, 1000);
            var testBytes    = new byte[bufferLength];
            var resultBytes  = new byte[bufferLength];

            for (var i = 0; i < N_SAMPLES; i++)
            {
                var shift = rnd.Next(0, byte.MaxValue * 3);
                rnd.NextBytes(testBytes);
                var expectedBytes = testBytes.Select(b => (byte)((b + shift) % (byte.MaxValue + 1))).ToArray();
                using (var mStream = new MemoryStream())
                    using (var caesar = new CaesarStream(mStream, shift))
                    {
                        caesar.Write(testBytes, 0, bufferLength);

                        mStream.Seek(0, SeekOrigin.Begin);
                        mStream.Read(resultBytes, 0, bufferLength);
                        CollectionAssert.AreEqual(resultBytes, expectedBytes);
                    }
            }
        }
Example #2
0
        public void TestStreamReadWriteDuality()
        {
            const int N_SAMPLES = 100;

            var rnd = new Random();

            var bufferLength = rnd.Next(100, 1000);
            var testBytes    = new byte[bufferLength];
            var resultBytes  = new byte[bufferLength];

            for (var i = 0; i < N_SAMPLES; i++)
            {
                var shift = rnd.Next(0, byte.MaxValue * 3);
                rnd.NextBytes(testBytes);
                using (MemoryStream sourceStream = new MemoryStream(testBytes),
                       destStream = new MemoryStream())
                    using (CaesarStream caeToRead = new CaesarStream(sourceStream, shift),
                           caeToWrite = new CaesarStream(destStream, -shift))
                    {
                        caeToRead.Read(resultBytes, 0, bufferLength);
                        caeToWrite.Write(resultBytes, 0, bufferLength);

                        destStream.Seek(0, SeekOrigin.Begin);
                        destStream.Read(resultBytes, 0, bufferLength);
                        CollectionAssert.AreEqual(resultBytes, testBytes);
                    }
            }
        }
Example #3
0
        static void Main(string[] args)
        {
            const string input = "abcdefghijklmnopqrstuvwxyz " +
                                 "ABCDEFGHIJKLMNOPQRSTUVWXYZ " +
                                 "1234567890 " +
                                 "!@#$%";

            FileStream   fileToWrite = File.Create("input.txt");
            CaesarStream caeToWrite  = new CaesarStream(fileToWrite, 5);

            caeToWrite.Write(Encoding.ASCII.GetBytes(input));
            fileToWrite.Close();
            Console.WriteLine("before:   '{0}'", input);

            FileStream   fileToRead = File.OpenRead("input.txt");
            CaesarStream caeToRead  = new CaesarStream(fileToRead, -5);
            var          bytes      = new byte[1000];

            caeToRead.Read(bytes);
            Console.WriteLine("after:    '{0}'", Encoding.ASCII.GetString(bytes));

            FileStream fileToCipher = File.OpenRead("input.txt");
            var        bytes2       = new byte[1000];

            fileToCipher.Read(bytes2);
            Console.WriteLine("ciphered: '{0}'", Encoding.ASCII.GetString(bytes2));
        }
Example #4
0
        public void TestRead()
        {
            string     teststring = "Defg";
            FileStream file       = File.Create("Test2");

            file.Write(Encoding.UTF8.GetBytes(teststring), 0, teststring.Length);
            file.Close();

            FileStream   readFileStream = File.Open("Test2", FileMode.Open);
            CaesarStream caesarStream   = new CaesarStream(readFileStream, -3);

            byte[] buffer = new byte[4];
            caesarStream.Read(buffer, 0, teststring.Length);

            Assert.AreEqual("Abcd", Encoding.UTF8.GetString(buffer));
        }
Example #5
0
        public void TestWrite()
        {
            string       teststring   = "Test";
            FileStream   file         = File.Create("Test");
            CaesarStream caesarStream = new CaesarStream(file, 3);

            caesarStream.Write(Encoding.UTF8.GetBytes(teststring), 0, teststring.Length);
            caesarStream.Close();
            file.Close();

            FileStream   readFileStream = File.Open("Test", FileMode.Open);
            StreamReader reader         = new StreamReader(readFileStream);
            string       readText       = reader.ReadToEnd();

            Assert.AreEqual("Whvw", readText);
        }
Example #6
0
        public void Test()
        {
            string testFile = "test.txt";
            string text     = "traaaalalalala";

            byte[] textToFile   = Encoding.ASCII.GetBytes(text);
            byte[] textFromFile = new byte[textToFile.Length];

            FileStream   fileToWrite = File.Create(testFile);
            CaesarStream caeToWrite  = new CaesarStream(fileToWrite, 5);

            caeToWrite.Write(textToFile, 0, textToFile.Length);
            fileToWrite.Close();

            FileStream   fileToRead = File.Open(testFile, FileMode.Open);
            CaesarStream caeToRead  = new CaesarStream(fileToRead, -5);


            caeToRead.Read(textFromFile, 0, textToFile.Length);
            string fromFile = Encoding.ASCII.GetString(textFromFile);

            Assert.AreEqual(textToFile, textFromFile);
        }