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

https://github.com/t-ashula/mono · C# · 174 lines · 123 code · 22 blank · 29 comment · 0 complexity · 5198b44a5eb19e56f1c9e6ba58a7772b MD5 · raw file

  1. //
  2. // DSASignatureDeformatterTest.cs - NUnit Test Cases for DSASignatureDeformatter
  3. //
  4. // Author:
  5. // Sebastien Pouliot <sebastien@ximian.com>
  6. //
  7. // (C) 2002 Motus Technologies Inc. (http://www.motus.com)
  8. // Copyright (C) 2004 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.Security;
  32. using System.Security.Cryptography;
  33. namespace MonoTests.System.Security.Cryptography {
  34. [TestFixture]
  35. public class DSASignatureDeformatterTest {
  36. protected DSASignatureDeformatter def;
  37. protected static DSA dsa;
  38. protected static RSA rsa;
  39. static byte[] hash = { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13 };
  40. static byte[] sign = { 0x50, 0xd2, 0xb0, 0x8b, 0xcd, 0x5e, 0xb2, 0xc2, 0x35, 0x82, 0xd3, 0x76, 0x07, 0x79, 0xbb, 0x55, 0x98, 0x72, 0x43, 0xe8,
  41. 0x74, 0xc9, 0x35, 0xf8, 0xc9, 0xbd, 0x69, 0x2f, 0x08, 0x34, 0xfa, 0x5a, 0x59, 0x23, 0x2a, 0x85, 0x7b, 0xa3, 0xb3, 0x82 };
  42. public DSASignatureDeformatterTest ()
  43. {
  44. // key generation is VERY long so one time is enough
  45. dsa = DSA.Create ();
  46. rsa = RSA.Create ();
  47. }
  48. [SetUp]
  49. public void SetUp ()
  50. {
  51. def = new DSASignatureDeformatter ();
  52. }
  53. [Test]
  54. public void Constructor_Empty ()
  55. {
  56. DSASignatureDeformatter def = new DSASignatureDeformatter ();
  57. Assert.IsNotNull (def);
  58. }
  59. [Test]
  60. [ExpectedException (typeof (ArgumentNullException))]
  61. public void Constructor_Null ()
  62. {
  63. DSASignatureDeformatter def = new DSASignatureDeformatter (null);
  64. Assert.IsNotNull (def);
  65. }
  66. [Test]
  67. public void Constructor_DSA ()
  68. {
  69. DSASignatureDeformatter def = new DSASignatureDeformatter (dsa);
  70. Assert.IsNotNull (def);
  71. }
  72. [Test]
  73. [ExpectedException (typeof (InvalidCastException))]
  74. public void Constructor_RSA ()
  75. {
  76. DSASignatureDeformatter def = new DSASignatureDeformatter (rsa);
  77. }
  78. [Test]
  79. [ExpectedException (typeof (ArgumentNullException))]
  80. public void SetHash_Null ()
  81. {
  82. def.SetHashAlgorithm (null);
  83. }
  84. [Test]
  85. public void SetHash_SHA1 ()
  86. {
  87. def.SetHashAlgorithm ("SHA1");
  88. }
  89. [Test]
  90. [ExpectedException (typeof (CryptographicUnexpectedOperationException))]
  91. public void SetHash_MD5 ()
  92. {
  93. def.SetHashAlgorithm ("MD5");
  94. }
  95. [Test]
  96. [ExpectedException (typeof (ArgumentNullException))]
  97. public void SetKey_Null ()
  98. {
  99. def.SetKey (null);
  100. }
  101. [Test]
  102. [ExpectedException (typeof (InvalidCastException))]
  103. public void SetKey_RSA ()
  104. {
  105. def.SetKey (rsa);
  106. }
  107. [Test]
  108. public void SetKey_DSA ()
  109. {
  110. def.SetKey (dsa);
  111. }
  112. [Test]
  113. [ExpectedException (typeof (CryptographicUnexpectedOperationException))]
  114. public void Verify_NoKeyPair ()
  115. {
  116. def.VerifySignature (hash, sign);
  117. }
  118. [Test]
  119. [ExpectedException (typeof (ArgumentNullException))]
  120. public void Verify_NullSignature ()
  121. {
  122. dsa.ImportParameters (AllTests.GetKey (false));
  123. def.SetKey (dsa);
  124. def.VerifySignature (hash, null);
  125. }
  126. [Test]
  127. [ExpectedException (typeof (ArgumentNullException))]
  128. public void Verify_NullHash ()
  129. {
  130. dsa.ImportParameters (AllTests.GetKey (false));
  131. def.SetKey (dsa);
  132. byte[] s = null; // overloaded method
  133. def.VerifySignature (s, sign);
  134. }
  135. [Test]
  136. public void Verify ()
  137. {
  138. dsa.ImportParameters (AllTests.GetKey (false));
  139. def.SetKey (dsa);
  140. Assert.IsTrue (def.VerifySignature (hash, sign));
  141. }
  142. [Test]
  143. public void Verify_Bad ()
  144. {
  145. dsa.ImportParameters (AllTests.GetKey (false));
  146. def.SetKey (dsa);
  147. byte[] badSign = { 0x49, 0xd2, 0xb0, 0x8b, 0xcd, 0x5e, 0xb2, 0xc2, 0x35, 0x82, 0xd3, 0x76, 0x07, 0x79, 0xbb, 0x55, 0x98, 0x72, 0x43, 0xe8,
  148. 0x74, 0xc9, 0x35, 0xf8, 0xc9, 0xbd, 0x69, 0x2f, 0x08, 0x34, 0xfa, 0x5a, 0x59, 0x23, 0x2a, 0x85, 0x7b, 0xa3, 0xb3, 0x82 };
  149. Assert.IsFalse (def.VerifySignature (hash, badSign));
  150. }
  151. }
  152. }