/mcs/class/corlib/Test/System.Security.Cryptography/SymmetricAlgorithm2Test.cs

https://github.com/iainlane/mono · C# · 173 lines · 117 code · 19 blank · 37 comment · 1 complexity · 31ca8c8d68b5a55b3d3d8b9c2e77dbc4 MD5 · raw file

  1. //
  2. // SymmetricAlgorithm2Test.cs -
  3. // Non generated NUnit Test Cases for SymmetricAlgorithm
  4. //
  5. // Author:
  6. // Sebastien Pouliot <spouliot@ximian.com>
  7. //
  8. // Copyright (C) 2004-2005 Novell, Inc (http://www.novell.com)
  9. //
  10. // Permission is hereby granted, free of charge, to any person obtaining
  11. // a copy of this software and associated documentation files (the
  12. // "Software"), to deal in the Software without restriction, including
  13. // without limitation the rights to use, copy, modify, merge, publish,
  14. // distribute, sublicense, and/or sell copies of the Software, and to
  15. // permit persons to whom the Software is furnished to do so, subject to
  16. // the following conditions:
  17. //
  18. // The above copyright notice and this permission notice shall be
  19. // included in all copies or substantial portions of the Software.
  20. //
  21. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  22. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  23. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  24. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  25. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  26. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  27. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  28. //
  29. using NUnit.Framework;
  30. using System;
  31. using System.IO;
  32. using System.Security.Cryptography;
  33. using System.Text;
  34. namespace MonoTests.System.Security.Cryptography {
  35. // SymmetricAlgorithm is a abstract class - so most of it's functionality wont
  36. // be tested here (but will be in its descendants).
  37. [TestFixture]
  38. public class SymmetricAlgorithm2Test {
  39. [Test]
  40. public void KeySize_SameSize ()
  41. {
  42. using (SymmetricAlgorithm algo = SymmetricAlgorithm.Create ()) {
  43. // get a copy of the key
  44. byte[] key = algo.Key;
  45. int ks = algo.KeySize;
  46. // set the key size
  47. algo.KeySize = ks;
  48. // did it change the key ? Yes!
  49. Assert.AreNotEqual (BitConverter.ToString (key), BitConverter.ToString (algo.Key), "Key");
  50. }
  51. }
  52. [Test]
  53. public void BlockSize_SameSize ()
  54. {
  55. using (SymmetricAlgorithm algo = SymmetricAlgorithm.Create ()) {
  56. // get a copy of the IV
  57. byte[] iv = algo.IV;
  58. int bs = algo.BlockSize;
  59. // set the iv size
  60. algo.BlockSize = bs;
  61. // did it change the IV ? No!
  62. Assert.AreEqual (BitConverter.ToString (iv), BitConverter.ToString (algo.IV), "IV");
  63. }
  64. }
  65. [Test]
  66. [ExpectedException (typeof (CryptographicException))]
  67. public void InvalidBlockSize ()
  68. {
  69. SymmetricAlgorithm algo = SymmetricAlgorithm.Create ();
  70. algo.BlockSize = 255;
  71. }
  72. [Test]
  73. [ExpectedException (typeof (CryptographicException))]
  74. public void InvalidFeedbackSize ()
  75. {
  76. SymmetricAlgorithm algo = SymmetricAlgorithm.Create ();
  77. algo.FeedbackSize = algo.BlockSize + 1;
  78. }
  79. [Test]
  80. [ExpectedException (typeof (ArgumentNullException))]
  81. public void IV_Null ()
  82. {
  83. SymmetricAlgorithm algo = SymmetricAlgorithm.Create ();
  84. algo.IV = null;
  85. }
  86. [Test]
  87. [ExpectedException (typeof (CryptographicException))]
  88. public void IV_None ()
  89. {
  90. SymmetricAlgorithm algo = SymmetricAlgorithm.Create ();
  91. algo.IV = new byte [0]; // e.g. stream ciphers
  92. }
  93. [Test]
  94. [ExpectedException (typeof (CryptographicException))]
  95. public void IV_TooBig ()
  96. {
  97. SymmetricAlgorithm algo = SymmetricAlgorithm.Create ();
  98. algo.IV = new byte [algo.BlockSize + 1];
  99. }
  100. [Test]
  101. [ExpectedException (typeof (ArgumentNullException))]
  102. public void Key_Null ()
  103. {
  104. SymmetricAlgorithm algo = SymmetricAlgorithm.Create ();
  105. algo.Key = null;
  106. }
  107. [Test]
  108. [ExpectedException (typeof (CryptographicException))]
  109. public void Key_WrongSize ()
  110. {
  111. SymmetricAlgorithm algo = SymmetricAlgorithm.Create ();
  112. algo.Key = new byte [255];
  113. }
  114. [Test]
  115. [ExpectedException (typeof (CryptographicException))]
  116. public void KeySize_WrongSize ()
  117. {
  118. SymmetricAlgorithm algo = SymmetricAlgorithm.Create ();
  119. int n = 0;
  120. while (algo.ValidKeySize (++n));
  121. algo.KeySize = n;
  122. }
  123. [Test]
  124. [ExpectedException (typeof (CryptographicException))]
  125. public void InvalidCipherMode ()
  126. {
  127. SymmetricAlgorithm algo = SymmetricAlgorithm.Create ();
  128. algo.Mode = (CipherMode) 255;
  129. }
  130. [Test]
  131. [ExpectedException (typeof (CryptographicException))]
  132. public void InvalidPaddingMode ()
  133. {
  134. SymmetricAlgorithm algo = SymmetricAlgorithm.Create ();
  135. algo.Padding = (PaddingMode) 255;
  136. }
  137. [Test]
  138. [ExpectedException (typeof (CryptographicException))]
  139. public void FeedbackZero ()
  140. {
  141. // thanks to Yakk for the sample
  142. DES des = new DESCryptoServiceProvider();
  143. des.FeedbackSize = 0;
  144. des.Padding = PaddingMode.None;
  145. des.Mode = CipherMode.ECB;
  146. des.Key = new byte [8] { 8, 7, 6, 5, 4, 3, 2, 1 };
  147. ICryptoTransform enc = des.CreateEncryptor ();
  148. byte[] response = new byte [16];
  149. byte[] challenge = new byte [16] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16 };
  150. enc.TransformBlock (challenge, 0, challenge.Length, response, 0);
  151. Assert.AreEqual ("7A-64-CD-1B-4F-EE-B5-92-54-90-53-E9-83-71-A6-0C", BitConverter.ToString (response));
  152. }
  153. }
  154. }