CngAlgorithmTest.cs 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. //
  2. // Unit tests for System.Security.Cryptography.CngAlgorithm
  3. //
  4. // Authors:
  5. // Sebastien Pouliot <[email protected]>
  6. //
  7. // Copyright (C) 2008 Novell, Inc (http://www.novell.com)
  8. //
  9. // Permission is hereby granted, free of charge, to any person obtaining
  10. // a copy of this software and associated documentation files (the
  11. // "Software"), to deal in the Software without restriction, including
  12. // without limitation the rights to use, copy, modify, merge, publish,
  13. // distribute, sublicense, and/or sell copies of the Software, and to
  14. // permit persons to whom the Software is furnished to do so, subject to
  15. // the following conditions:
  16. //
  17. // The above copyright notice and this permission notice shall be
  18. // included in all copies or substantial portions of the Software.
  19. //
  20. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  21. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  22. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  23. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  24. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  25. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  26. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  27. //
  28. using System;
  29. using System.Security.Cryptography;
  30. using NUnit.Framework;
  31. namespace MonoTests.System.Security.Cryptography {
  32. [TestFixture]
  33. public class CngAlgorithmTest {
  34. [Test]
  35. public void StaticProperties ()
  36. {
  37. Assert.IsNotNull (CngAlgorithm.ECDiffieHellmanP256, "ECDiffieHellmanP256");
  38. Assert.IsNotNull (CngAlgorithm.ECDiffieHellmanP384, "ECDiffieHellmanP384");
  39. Assert.IsNotNull (CngAlgorithm.ECDiffieHellmanP521, "ECDiffieHellmanP521");
  40. Assert.IsNotNull (CngAlgorithm.ECDsaP256, "ECDsaP256");
  41. Assert.IsNotNull (CngAlgorithm.ECDsaP384, "ECDsaP384");
  42. Assert.IsNotNull (CngAlgorithm.ECDsaP521, "ECDsaP521");
  43. Assert.IsNotNull (CngAlgorithm.MD5, "MD5");
  44. Assert.IsNotNull (CngAlgorithm.Sha1, "Sha1");
  45. Assert.IsNotNull (CngAlgorithm.Sha256, "Sha256");
  46. Assert.IsNotNull (CngAlgorithm.Sha384, "Sha384");
  47. Assert.IsNotNull (CngAlgorithm.Sha512, "Sha512");
  48. }
  49. [Test]
  50. [ExpectedException (typeof (ArgumentNullException))]
  51. public void ConstructorNull ()
  52. {
  53. new CngAlgorithm (null);
  54. }
  55. [Test]
  56. [ExpectedException (typeof (ArgumentException))]
  57. public void ConstructorEmpty ()
  58. {
  59. new CngAlgorithm (String.Empty);
  60. }
  61. static CngAlgorithm mono = new CngAlgorithm ("mono");
  62. private void Check (CngAlgorithm algo)
  63. {
  64. Assert.AreEqual (algo.Algorithm, algo.ToString (), "Algorithm/ToString");
  65. Assert.AreEqual (algo.GetHashCode (), algo.Algorithm.GetHashCode (), "GetHashCode");
  66. Assert.IsTrue (algo.Equals (algo), "Equals(self)");
  67. Assert.IsTrue (algo.Equals ((object)algo), "Equals((object)self)");
  68. CngAlgorithm copy = new CngAlgorithm (algo.Algorithm);
  69. Assert.AreEqual (algo.GetHashCode (), copy.GetHashCode (), "Copy");
  70. Assert.IsTrue (algo.Equals (copy), "Equals(copy)");
  71. Assert.IsTrue (algo.Equals ((object)copy), "Equals((object)copy)");
  72. Assert.IsTrue (algo == copy, "algo==copy");
  73. Assert.IsFalse (algo != copy, "algo!=copy");
  74. Assert.IsFalse (algo.Equals (mono), "Equals(mono)");
  75. Assert.IsFalse (algo.Equals ((object)mono), "Equals((object)mono)");
  76. Assert.IsFalse (algo == mono, "algo==mono");
  77. Assert.IsTrue (algo != mono, "algo!=mono");
  78. }
  79. [Test]
  80. public void ConstructorCustom ()
  81. {
  82. CngAlgorithm algo = new CngAlgorithm ("custom");
  83. Check (algo);
  84. Assert.IsFalse (algo.Equals ((CngAlgorithm) null), "Equals((CngAlgorithm)null)");
  85. Assert.IsFalse (algo.Equals ((object) null), "Equals((object)null)");
  86. }
  87. [Test]
  88. public void ECDiffieHellmanP256 ()
  89. {
  90. CngAlgorithm algo = CngAlgorithm.ECDiffieHellmanP256;
  91. Assert.AreEqual ("ECDH_P256", algo.Algorithm, "Algorithm");
  92. Assert.IsTrue (algo.Equals (CngAlgorithm.ECDiffieHellmanP256), "Equals(static)");
  93. Assert.IsTrue (Object.ReferenceEquals (algo, CngAlgorithm.ECDiffieHellmanP256), "ReferenceEquals");
  94. Check (algo);
  95. }
  96. [Test]
  97. public void ECDiffieHellmanP384 ()
  98. {
  99. CngAlgorithm algo = CngAlgorithm.ECDiffieHellmanP384;
  100. Assert.AreEqual ("ECDH_P384", algo.Algorithm, "Algorithm");
  101. Assert.IsTrue (algo.Equals (CngAlgorithm.ECDiffieHellmanP384), "Equals(static)");
  102. Assert.IsTrue (Object.ReferenceEquals (algo, CngAlgorithm.ECDiffieHellmanP384), "ReferenceEquals");
  103. Check (algo);
  104. }
  105. [Test]
  106. public void ECDiffieHellmanP521 ()
  107. {
  108. CngAlgorithm algo = CngAlgorithm.ECDiffieHellmanP521;
  109. Assert.AreEqual ("ECDH_P521", algo.Algorithm, "Algorithm");
  110. Assert.IsTrue (algo.Equals (CngAlgorithm.ECDiffieHellmanP521), "Equals(static)");
  111. Assert.IsTrue (Object.ReferenceEquals (algo, CngAlgorithm.ECDiffieHellmanP521), "ReferenceEquals");
  112. Check (algo);
  113. }
  114. [Test]
  115. public void ECDsaP256 ()
  116. {
  117. CngAlgorithm algo = CngAlgorithm.ECDsaP256;
  118. Assert.AreEqual ("ECDSA_P256", algo.Algorithm, "Algorithm");
  119. Assert.IsTrue (algo.Equals (CngAlgorithm.ECDsaP256), "Equals(static)");
  120. Assert.IsTrue (Object.ReferenceEquals (algo, CngAlgorithm.ECDsaP256), "ReferenceEquals");
  121. Check (algo);
  122. }
  123. [Test]
  124. public void ECDsaP384 ()
  125. {
  126. CngAlgorithm algo = CngAlgorithm.ECDsaP384;
  127. Assert.AreEqual ("ECDSA_P384", algo.Algorithm, "Algorithm");
  128. Assert.IsTrue (algo.Equals (CngAlgorithm.ECDsaP384), "Equals(static)");
  129. Assert.IsTrue (Object.ReferenceEquals (algo, CngAlgorithm.ECDsaP384), "ReferenceEquals");
  130. Check (algo);
  131. }
  132. [Test]
  133. public void ECDsaP521 ()
  134. {
  135. CngAlgorithm algo = CngAlgorithm.ECDsaP521;
  136. Assert.AreEqual ("ECDSA_P521", algo.Algorithm, "Algorithm");
  137. Assert.IsTrue (algo.Equals (CngAlgorithm.ECDsaP521), "Equals(static)");
  138. Assert.IsTrue (Object.ReferenceEquals (algo, CngAlgorithm.ECDsaP521), "ReferenceEquals");
  139. Check (algo);
  140. }
  141. [Test]
  142. public void MD5 ()
  143. {
  144. CngAlgorithm algo = CngAlgorithm.MD5;
  145. Assert.AreEqual ("MD5", algo.Algorithm, "Algorithm");
  146. Assert.IsTrue (algo.Equals (CngAlgorithm.MD5), "Equals(static)");
  147. Assert.IsTrue (Object.ReferenceEquals (algo, CngAlgorithm.MD5), "ReferenceEquals");
  148. Check (algo);
  149. }
  150. [Test]
  151. public void Sha1 ()
  152. {
  153. CngAlgorithm algo = CngAlgorithm.Sha1;
  154. Assert.AreEqual ("SHA1", algo.Algorithm, "Algorithm");
  155. Assert.IsTrue (algo.Equals (CngAlgorithm.Sha1), "Equals(static)");
  156. Assert.IsTrue (Object.ReferenceEquals (algo, CngAlgorithm.Sha1), "ReferenceEquals");
  157. Check (algo);
  158. }
  159. [Test]
  160. public void Sha256 ()
  161. {
  162. CngAlgorithm algo = CngAlgorithm.Sha256;
  163. Assert.AreEqual ("SHA256", algo.Algorithm, "Algorithm");
  164. Assert.IsTrue (algo.Equals (CngAlgorithm.Sha256), "Equals(static)");
  165. Assert.IsTrue (Object.ReferenceEquals (algo, CngAlgorithm.Sha256), "ReferenceEquals");
  166. Check (algo);
  167. }
  168. [Test]
  169. public void Sha384 ()
  170. {
  171. CngAlgorithm algo = CngAlgorithm.Sha384;
  172. Assert.AreEqual ("SHA384", algo.Algorithm, "Algorithm");
  173. Assert.IsTrue (algo.Equals (CngAlgorithm.Sha384), "Equals(static)");
  174. Assert.IsTrue (Object.ReferenceEquals (algo, CngAlgorithm.Sha384), "ReferenceEquals");
  175. Check (algo);
  176. }
  177. [Test]
  178. public void Sha512 ()
  179. {
  180. CngAlgorithm algo = CngAlgorithm.Sha512;
  181. Assert.AreEqual ("SHA512", algo.Algorithm, "Algorithm");
  182. Assert.IsTrue (algo.Equals (CngAlgorithm.Sha512), "Equals(static)");
  183. Assert.IsTrue (Object.ReferenceEquals (algo, CngAlgorithm.Sha512), "ReferenceEquals");
  184. Check (algo);
  185. }
  186. }
  187. }