2
0

CngAlgorithmTest.cs 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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. #if !MOBILE
  29. using System;
  30. using System.Security.Cryptography;
  31. using NUnit.Framework;
  32. namespace MonoTests.System.Security.Cryptography {
  33. [TestFixture]
  34. public class CngAlgorithmTest {
  35. [Test]
  36. public void StaticProperties ()
  37. {
  38. Assert.IsNotNull (CngAlgorithm.ECDiffieHellmanP256, "ECDiffieHellmanP256");
  39. Assert.IsNotNull (CngAlgorithm.ECDiffieHellmanP384, "ECDiffieHellmanP384");
  40. Assert.IsNotNull (CngAlgorithm.ECDiffieHellmanP521, "ECDiffieHellmanP521");
  41. Assert.IsNotNull (CngAlgorithm.ECDsaP256, "ECDsaP256");
  42. Assert.IsNotNull (CngAlgorithm.ECDsaP384, "ECDsaP384");
  43. Assert.IsNotNull (CngAlgorithm.ECDsaP521, "ECDsaP521");
  44. Assert.IsNotNull (CngAlgorithm.MD5, "MD5");
  45. Assert.IsNotNull (CngAlgorithm.Sha1, "Sha1");
  46. Assert.IsNotNull (CngAlgorithm.Sha256, "Sha256");
  47. Assert.IsNotNull (CngAlgorithm.Sha384, "Sha384");
  48. Assert.IsNotNull (CngAlgorithm.Sha512, "Sha512");
  49. }
  50. [Test]
  51. [ExpectedException (typeof (ArgumentNullException))]
  52. public void ConstructorNull ()
  53. {
  54. new CngAlgorithm (null);
  55. }
  56. [Test]
  57. [ExpectedException (typeof (ArgumentException))]
  58. public void ConstructorEmpty ()
  59. {
  60. new CngAlgorithm (String.Empty);
  61. }
  62. static CngAlgorithm mono = new CngAlgorithm ("mono");
  63. private void Check (CngAlgorithm algo)
  64. {
  65. Assert.AreEqual (algo.Algorithm, algo.ToString (), "Algorithm/ToString");
  66. Assert.AreEqual (algo.GetHashCode (), algo.Algorithm.GetHashCode (), "GetHashCode");
  67. Assert.IsTrue (algo.Equals (algo), "Equals(self)");
  68. Assert.IsTrue (algo.Equals ((object)algo), "Equals((object)self)");
  69. CngAlgorithm copy = new CngAlgorithm (algo.Algorithm);
  70. Assert.AreEqual (algo.GetHashCode (), copy.GetHashCode (), "Copy");
  71. Assert.IsTrue (algo.Equals (copy), "Equals(copy)");
  72. Assert.IsTrue (algo.Equals ((object)copy), "Equals((object)copy)");
  73. Assert.IsTrue (algo == copy, "algo==copy");
  74. Assert.IsFalse (algo != copy, "algo!=copy");
  75. Assert.IsFalse (algo.Equals (mono), "Equals(mono)");
  76. Assert.IsFalse (algo.Equals ((object)mono), "Equals((object)mono)");
  77. Assert.IsFalse (algo == mono, "algo==mono");
  78. Assert.IsTrue (algo != mono, "algo!=mono");
  79. }
  80. [Test]
  81. public void ConstructorCustom ()
  82. {
  83. CngAlgorithm algo = new CngAlgorithm ("custom");
  84. Check (algo);
  85. Assert.IsFalse (algo.Equals ((CngAlgorithm) null), "Equals((CngAlgorithm)null)");
  86. Assert.IsFalse (algo.Equals ((object) null), "Equals((object)null)");
  87. }
  88. [Test]
  89. public void ECDiffieHellmanP256 ()
  90. {
  91. CngAlgorithm algo = CngAlgorithm.ECDiffieHellmanP256;
  92. Assert.AreEqual ("ECDH_P256", algo.Algorithm, "Algorithm");
  93. Assert.IsTrue (algo.Equals (CngAlgorithm.ECDiffieHellmanP256), "Equals(static)");
  94. Assert.IsTrue (Object.ReferenceEquals (algo, CngAlgorithm.ECDiffieHellmanP256), "ReferenceEquals");
  95. Check (algo);
  96. }
  97. [Test]
  98. public void ECDiffieHellmanP384 ()
  99. {
  100. CngAlgorithm algo = CngAlgorithm.ECDiffieHellmanP384;
  101. Assert.AreEqual ("ECDH_P384", algo.Algorithm, "Algorithm");
  102. Assert.IsTrue (algo.Equals (CngAlgorithm.ECDiffieHellmanP384), "Equals(static)");
  103. Assert.IsTrue (Object.ReferenceEquals (algo, CngAlgorithm.ECDiffieHellmanP384), "ReferenceEquals");
  104. Check (algo);
  105. }
  106. [Test]
  107. public void ECDiffieHellmanP521 ()
  108. {
  109. CngAlgorithm algo = CngAlgorithm.ECDiffieHellmanP521;
  110. Assert.AreEqual ("ECDH_P521", algo.Algorithm, "Algorithm");
  111. Assert.IsTrue (algo.Equals (CngAlgorithm.ECDiffieHellmanP521), "Equals(static)");
  112. Assert.IsTrue (Object.ReferenceEquals (algo, CngAlgorithm.ECDiffieHellmanP521), "ReferenceEquals");
  113. Check (algo);
  114. }
  115. [Test]
  116. public void ECDsaP256 ()
  117. {
  118. CngAlgorithm algo = CngAlgorithm.ECDsaP256;
  119. Assert.AreEqual ("ECDSA_P256", algo.Algorithm, "Algorithm");
  120. Assert.IsTrue (algo.Equals (CngAlgorithm.ECDsaP256), "Equals(static)");
  121. Assert.IsTrue (Object.ReferenceEquals (algo, CngAlgorithm.ECDsaP256), "ReferenceEquals");
  122. Check (algo);
  123. }
  124. [Test]
  125. public void ECDsaP384 ()
  126. {
  127. CngAlgorithm algo = CngAlgorithm.ECDsaP384;
  128. Assert.AreEqual ("ECDSA_P384", algo.Algorithm, "Algorithm");
  129. Assert.IsTrue (algo.Equals (CngAlgorithm.ECDsaP384), "Equals(static)");
  130. Assert.IsTrue (Object.ReferenceEquals (algo, CngAlgorithm.ECDsaP384), "ReferenceEquals");
  131. Check (algo);
  132. }
  133. [Test]
  134. public void ECDsaP521 ()
  135. {
  136. CngAlgorithm algo = CngAlgorithm.ECDsaP521;
  137. Assert.AreEqual ("ECDSA_P521", algo.Algorithm, "Algorithm");
  138. Assert.IsTrue (algo.Equals (CngAlgorithm.ECDsaP521), "Equals(static)");
  139. Assert.IsTrue (Object.ReferenceEquals (algo, CngAlgorithm.ECDsaP521), "ReferenceEquals");
  140. Check (algo);
  141. }
  142. [Test]
  143. public void MD5 ()
  144. {
  145. CngAlgorithm algo = CngAlgorithm.MD5;
  146. Assert.AreEqual ("MD5", algo.Algorithm, "Algorithm");
  147. Assert.IsTrue (algo.Equals (CngAlgorithm.MD5), "Equals(static)");
  148. Assert.IsTrue (Object.ReferenceEquals (algo, CngAlgorithm.MD5), "ReferenceEquals");
  149. Check (algo);
  150. }
  151. [Test]
  152. public void Sha1 ()
  153. {
  154. CngAlgorithm algo = CngAlgorithm.Sha1;
  155. Assert.AreEqual ("SHA1", algo.Algorithm, "Algorithm");
  156. Assert.IsTrue (algo.Equals (CngAlgorithm.Sha1), "Equals(static)");
  157. Assert.IsTrue (Object.ReferenceEquals (algo, CngAlgorithm.Sha1), "ReferenceEquals");
  158. Check (algo);
  159. }
  160. [Test]
  161. public void Sha256 ()
  162. {
  163. CngAlgorithm algo = CngAlgorithm.Sha256;
  164. Assert.AreEqual ("SHA256", algo.Algorithm, "Algorithm");
  165. Assert.IsTrue (algo.Equals (CngAlgorithm.Sha256), "Equals(static)");
  166. Assert.IsTrue (Object.ReferenceEquals (algo, CngAlgorithm.Sha256), "ReferenceEquals");
  167. Check (algo);
  168. }
  169. [Test]
  170. public void Sha384 ()
  171. {
  172. CngAlgorithm algo = CngAlgorithm.Sha384;
  173. Assert.AreEqual ("SHA384", algo.Algorithm, "Algorithm");
  174. Assert.IsTrue (algo.Equals (CngAlgorithm.Sha384), "Equals(static)");
  175. Assert.IsTrue (Object.ReferenceEquals (algo, CngAlgorithm.Sha384), "ReferenceEquals");
  176. Check (algo);
  177. }
  178. [Test]
  179. public void Sha512 ()
  180. {
  181. CngAlgorithm algo = CngAlgorithm.Sha512;
  182. Assert.AreEqual ("SHA512", algo.Algorithm, "Algorithm");
  183. Assert.IsTrue (algo.Equals (CngAlgorithm.Sha512), "Equals(static)");
  184. Assert.IsTrue (Object.ReferenceEquals (algo, CngAlgorithm.Sha512), "ReferenceEquals");
  185. Check (algo);
  186. }
  187. }
  188. }
  189. #endif