SignatureDescriptionTest.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. //
  2. // SignatureDescriptionTest.cs - NUnit Test Cases for SignatureDescription
  3. //
  4. // Author:
  5. // Sebastien Pouliot ([email protected])
  6. //
  7. // (C) 2002 Motus Technologies Inc. (http://www.motus.com)
  8. //
  9. using NUnit.Framework;
  10. using System;
  11. using System.Security;
  12. using System.Security.Cryptography;
  13. namespace MonoTests.System.Security.Cryptography {
  14. public class SignatureDescriptionTest : TestCase {
  15. protected SignatureDescription sig;
  16. protected static DSA dsa;
  17. protected static RSA rsa;
  18. protected override void SetUp ()
  19. {
  20. sig = new SignatureDescription();
  21. // key generation is VERY long so one time is enough
  22. if (dsa == null)
  23. dsa = DSA.Create ();
  24. if (rsa == null)
  25. rsa = RSA.Create ();
  26. }
  27. protected override void TearDown () {}
  28. public void AssertEquals (string msg, byte[] array1, byte[] array2)
  29. {
  30. AllTests.AssertEquals (msg, array1, array2);
  31. }
  32. public void TestConstructors ()
  33. {
  34. // empty constructor
  35. SignatureDescription sig = new SignatureDescription ();
  36. AssertNotNull ("SignatureDescription()", sig);
  37. // null constructor
  38. try {
  39. sig = new SignatureDescription (null);
  40. Fail ("SignatureDescription(null): Expected ArgumentNullException but got none");
  41. }
  42. catch (ArgumentNullException) {
  43. // LAMESPEC: Documented as CryptographicException
  44. // but thrown as ArgumentNullException
  45. }
  46. catch (Exception e) {
  47. Fail ("SignatureDescription(null): Expected ArgumentNullException but got: " + e.ToString ());
  48. }
  49. // (empty) SecurityElement constructor
  50. SecurityElement se = new SecurityElement ("xml");
  51. sig = new SignatureDescription (se);
  52. AssertNotNull ("SignatureDescription(SecurityElement)", sig);
  53. }
  54. public void TestProperties ()
  55. {
  56. string invalid = "invalid";
  57. AssertNull ("DeformatterAlgorithm 1", sig.DeformatterAlgorithm);
  58. sig.DeformatterAlgorithm = invalid;
  59. AssertNotNull ("DeformatterAlgorithm 2", sig.DeformatterAlgorithm);
  60. AssertEquals ("DeformatterAlgorithm 3", invalid, sig.DeformatterAlgorithm);
  61. sig.DeformatterAlgorithm = null;
  62. AssertNull ("DeformatterAlgorithm 4", sig.DeformatterAlgorithm);
  63. AssertNull ("DigestAlgorithm 1", sig.DigestAlgorithm);
  64. sig.DigestAlgorithm = invalid;
  65. AssertNotNull ("DigestAlgorithm 2", sig.DigestAlgorithm);
  66. AssertEquals ("DigestAlgorithm 3", invalid, sig.DigestAlgorithm);
  67. sig.DigestAlgorithm = null;
  68. AssertNull ("DigestAlgorithm 4", sig.DigestAlgorithm);
  69. AssertNull ("FormatterAlgorithm 1", sig.FormatterAlgorithm);
  70. sig.FormatterAlgorithm = invalid;
  71. AssertNotNull ("FormatterAlgorithm 2", sig.FormatterAlgorithm);
  72. AssertEquals ("FormatterAlgorithm 3", invalid, sig.FormatterAlgorithm);
  73. sig.FormatterAlgorithm = null;
  74. AssertNull ("FormatterAlgorithm 4", sig.FormatterAlgorithm);
  75. AssertNull ("KeyAlgorithm 1", sig.KeyAlgorithm);
  76. sig.KeyAlgorithm = invalid;
  77. AssertNotNull ("KeyAlgorithm 2", sig.KeyAlgorithm);
  78. AssertEquals ("KeyAlgorithm 3", invalid, sig.KeyAlgorithm);
  79. sig.KeyAlgorithm = null;
  80. AssertNull ("KeyAlgorithm 4", sig.KeyAlgorithm);
  81. }
  82. public void TestDeformatter ()
  83. {
  84. AsymmetricSignatureDeformatter def = null;
  85. // Deformatter with all properties null
  86. try {
  87. def = sig.CreateDeformatter (dsa);
  88. Fail ("Expected ArgumentNullException but got none");
  89. }
  90. catch (ArgumentNullException) {
  91. // this is what we expect
  92. }
  93. catch (Exception e) {
  94. Fail ("Expected ArgumentNullException but got: " + e.ToString ());
  95. }
  96. // Deformatter with invalid DeformatterAlgorithm property
  97. sig.DeformatterAlgorithm = "DSA";
  98. try {
  99. def = sig.CreateDeformatter (dsa);
  100. Fail ("Expected InvalidCastException but got none");
  101. }
  102. catch (InvalidCastException) {
  103. // this is what we expect
  104. }
  105. catch (Exception e) {
  106. Fail ("Expected InvalidCastException but got: " + e.ToString ());
  107. }
  108. // Deformatter with valid DeformatterAlgorithm property
  109. sig.DeformatterAlgorithm = "DSASignatureDeformatter";
  110. try {
  111. def = sig.CreateDeformatter (dsa);
  112. Fail ("Expected NullReferenceException but got none");
  113. }
  114. catch (NullReferenceException) {
  115. // this is what we expect
  116. }
  117. catch (Exception e) {
  118. Fail ("Expected NullReferenceException but got: " + e.ToString ());
  119. }
  120. // Deformatter with valid DeformatterAlgorithm property
  121. sig.KeyAlgorithm = "DSA";
  122. sig.DigestAlgorithm = "SHA1";
  123. sig.DeformatterAlgorithm = "DSASignatureDeformatter";
  124. try {
  125. def = sig.CreateDeformatter (dsa);
  126. Fail ("Expected NullReferenceException but got none");
  127. }
  128. catch (NullReferenceException) {
  129. // this is what we expect
  130. }
  131. catch (Exception e) {
  132. Fail ("Expected NullReferenceException but got: " + e.ToString ());
  133. }
  134. }
  135. public void TestDigest ()
  136. {
  137. bool rightClass = false;
  138. HashAlgorithm hash = null;
  139. // null hash
  140. try {
  141. hash = sig.CreateDigest ();
  142. Fail ("Expected ArgumentNullException but got none");
  143. }
  144. catch (ArgumentNullException) {
  145. // this is what we expect
  146. }
  147. catch (Exception e) {
  148. Fail ("Expected ArgumentNullException but got: " + e.ToString ());
  149. }
  150. sig.DigestAlgorithm = "SHA1";
  151. hash = sig.CreateDigest ();
  152. AssertNotNull ("CreateDigest(SHA1)", hash);
  153. rightClass = (hash.ToString ().IndexOf (sig.DigestAlgorithm) > 0);
  154. Assert ("CreateDigest(SHA1)", rightClass);
  155. sig.DigestAlgorithm = "MD5";
  156. hash = sig.CreateDigest ();
  157. AssertNotNull ("CreateDigest(MD5)", hash);
  158. rightClass = (hash.ToString ().IndexOf (sig.DigestAlgorithm) > 0);
  159. Assert ("CreateDigest(MD5)", rightClass);
  160. sig.DigestAlgorithm = "SHA256";
  161. hash = sig.CreateDigest ();
  162. AssertNotNull ("CreateDigest(SHA256)", hash);
  163. rightClass = (hash.ToString ().IndexOf (sig.DigestAlgorithm) > 0);
  164. Assert ("CreateDigest(SHA256)", rightClass);
  165. sig.DigestAlgorithm = "SHA384";
  166. hash = sig.CreateDigest ();
  167. AssertNotNull ("CreateDigest(SHA384)", hash);
  168. rightClass = (hash.ToString ().IndexOf (sig.DigestAlgorithm) > 0);
  169. Assert ("CreateDigest(SHA384)", rightClass);
  170. sig.DigestAlgorithm = "SHA512";
  171. hash = sig.CreateDigest ();
  172. AssertNotNull ("CreateDigest(SHA512)", hash);
  173. rightClass = (hash.ToString ().IndexOf (sig.DigestAlgorithm) > 0);
  174. Assert ("CreateDigest(SHA512)", rightClass);
  175. sig.DigestAlgorithm = "bad";
  176. hash = sig.CreateDigest ();
  177. AssertNull ("CreateDigest(bad)", hash);
  178. }
  179. public void TestFormatter ()
  180. {
  181. AsymmetricSignatureFormatter fmt = null;
  182. // Formatter with all properties null
  183. try {
  184. fmt = sig.CreateFormatter (dsa);
  185. Fail ("Expected ArgumentNullException but got none");
  186. }
  187. catch (ArgumentNullException) {
  188. // this is what we expect
  189. }
  190. catch (Exception e) {
  191. Fail ("Expected ArgumentNullException but got: " + e.ToString ());
  192. }
  193. // Formatter with invalid FormatterAlgorithm property
  194. sig.FormatterAlgorithm = "DSA";
  195. try {
  196. fmt = sig.CreateFormatter (dsa);
  197. Fail ("Expected InvalidCastException but got none");
  198. }
  199. catch (InvalidCastException) {
  200. // this is what we expect
  201. }
  202. catch (Exception e) {
  203. Fail ("Expected InvalidCastException but got: " + e.ToString ());
  204. }
  205. // Formatter with valid FormatterAlgorithm property
  206. sig.FormatterAlgorithm = "DSASignatureFormatter";
  207. try {
  208. fmt = sig.CreateFormatter (dsa);
  209. Fail ("Expected NullReferenceException but got none");
  210. }
  211. catch (NullReferenceException) {
  212. // this is what we expect
  213. }
  214. catch (Exception e) {
  215. Fail ("Expected NullReferenceException but got: " + e.ToString ());
  216. }
  217. // Deformatter with valid DeformatterAlgorithm property
  218. sig.KeyAlgorithm = "DSA";
  219. sig.DigestAlgorithm = "SHA1";
  220. sig.FormatterAlgorithm = "DSASignatureFormatter";
  221. try {
  222. fmt = sig.CreateFormatter (dsa);
  223. Fail ("Expected NullReferenceException but got none");
  224. }
  225. catch (NullReferenceException) {
  226. // this is what we expect
  227. }
  228. catch (Exception e) {
  229. Fail ("Expected NullReferenceException but got: " + e.ToString ());
  230. }
  231. }
  232. public void TestDSASignatureDescription ()
  233. {
  234. // internal class - we cannot create one without CryptoConfig
  235. SignatureDescription sd = (SignatureDescription) CryptoConfig.CreateFromName ("http://www.w3.org/2000/09/xmldsig#dsa-sha1");
  236. AssertEquals ("DSA.DigestAlgorithm", "System.Security.Cryptography.SHA1CryptoServiceProvider", sd.DigestAlgorithm);
  237. AssertEquals ("DSA.DeformatterAlgorithm", "System.Security.Cryptography.DSASignatureDeformatter", sd.DeformatterAlgorithm);
  238. AssertEquals ("DSA.FormatterAlgorithm", "System.Security.Cryptography.DSASignatureFormatter", sd.FormatterAlgorithm);
  239. AssertEquals ("DSA.KeyAlgorithm", "System.Security.Cryptography.DSACryptoServiceProvider", sd.KeyAlgorithm);
  240. HashAlgorithm hash = sd.CreateDigest();
  241. AssertEquals ("DSA.CreateDigest", "System.Security.Cryptography.SHA1CryptoServiceProvider", hash.ToString ());
  242. AssertEquals ("DSA.Create", dsa.ToString (), sd.KeyAlgorithm);
  243. AsymmetricSignatureDeformatter asd = sd.CreateDeformatter (dsa);
  244. AssertEquals ("DSA.CreateDeformatter", "System.Security.Cryptography.DSASignatureDeformatter", asd.ToString ());
  245. AsymmetricSignatureFormatter asf = sd.CreateFormatter (dsa);
  246. AssertEquals ("DSA.CreateFormatter", "System.Security.Cryptography.DSASignatureFormatter", asf.ToString ());
  247. }
  248. public void TestRSASignatureDescription ()
  249. {
  250. // internal class - we cannot create one without CryptoConfig
  251. SignatureDescription sd = (SignatureDescription) CryptoConfig.CreateFromName ("http://www.w3.org/2000/09/xmldsig#rsa-sha1");
  252. AssertEquals ("RSA.DigestAlgorithm", "System.Security.Cryptography.SHA1CryptoServiceProvider", sd.DigestAlgorithm);
  253. AssertEquals ("RSA.DeformatterAlgorithm", "System.Security.Cryptography.RSAPKCS1SignatureDeformatter", sd.DeformatterAlgorithm);
  254. AssertEquals ("RSA.FormatterAlgorithm", "System.Security.Cryptography.RSAPKCS1SignatureFormatter", sd.FormatterAlgorithm);
  255. AssertEquals ("RSA.KeyAlgorithm", "System.Security.Cryptography.RSACryptoServiceProvider", sd.KeyAlgorithm);
  256. HashAlgorithm hash = sd.CreateDigest();
  257. AssertEquals ("RSA.CreateDigest", "System.Security.Cryptography.SHA1CryptoServiceProvider", hash.ToString ());
  258. AssertEquals ("RSA.Create", rsa.ToString (), sd.KeyAlgorithm);
  259. AsymmetricSignatureDeformatter asd = sd.CreateDeformatter (rsa);
  260. AssertEquals ("RSA.CreateDeformatter", "System.Security.Cryptography.RSAPKCS1SignatureDeformatter", asd.ToString ());
  261. AsymmetricSignatureFormatter asf = sd.CreateFormatter (rsa);
  262. AssertEquals ("RSA.CreateFormatter", "System.Security.Cryptography.RSAPKCS1SignatureFormatter", asf.ToString ());
  263. }
  264. }
  265. }