Exemple #1
0
        public void TestArgumentExceptions()
        {
            using (var rc4 = new RC4()) {
                var buffer = new byte[16];

                Assert.AreEqual(1, rc4.InputBlockSize, "InputBlockSize");
                Assert.AreEqual(1, rc4.OutputBlockSize, "OutputBlockSize");
                Assert.IsFalse(rc4.CanReuseTransform, "CanReuseTransform");
                Assert.IsTrue(rc4.CanTransformMultipleBlocks, "CanTransformMultipleBlocks");

                Assert.Throws <InvalidOperationException> (() => { var x = rc4.Key; });
                Assert.Throws <ArgumentNullException> (() => { rc4.Key = null; });
                Assert.Throws <ArgumentException> (() => { rc4.Key = new byte[0]; });

                rc4.GenerateIV();
                rc4.GenerateKey();
                rc4.CreateDecryptor();
                rc4.CreateEncryptor();

                // TransformBlock input buffer parameters
                Assert.Throws <ArgumentNullException> (() => rc4.TransformBlock(null, 0, buffer.Length, buffer, 0));
                Assert.Throws <ArgumentOutOfRangeException> (() => rc4.TransformBlock(buffer, -1, buffer.Length, buffer, 0));
                Assert.Throws <ArgumentOutOfRangeException> (() => rc4.TransformBlock(buffer, 0, -1, buffer, 0));

                // TransformBlock output buffer parameters
                Assert.Throws <ArgumentNullException> (() => rc4.TransformBlock(buffer, 0, buffer.Length, null, 0));
                Assert.Throws <ArgumentOutOfRangeException> (() => rc4.TransformBlock(buffer, 0, buffer.Length, buffer, -1));

                // TransformFinalBlock
                Assert.Throws <ArgumentNullException> (() => rc4.TransformFinalBlock(null, 0, buffer.Length));
                Assert.Throws <ArgumentOutOfRangeException> (() => rc4.TransformFinalBlock(buffer, -1, buffer.Length));
                Assert.Throws <ArgumentOutOfRangeException> (() => rc4.TransformFinalBlock(buffer, 0, -1));
            }
        }