SignedXmlTest.cs 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461
  1. //
  2. // SignedXmlTest.cs - NUnit Test Cases for SignedXml
  3. //
  4. // Author:
  5. // Sebastien Pouliot <[email protected]>
  6. //
  7. // (C) 2002, 2003 Motus Technologies Inc. (http://www.motus.com)
  8. // Copyright (C) 2004-2005 Novell, Inc (http://www.novell.com)
  9. //
  10. using System;
  11. using System.Security.Cryptography;
  12. using System.Security.Cryptography.Xml;
  13. using System.Text;
  14. using System.Xml;
  15. using NUnit.Framework;
  16. namespace MonoTests.System.Security.Cryptography.Xml {
  17. public class SignedXmlEx : SignedXml {
  18. // required to test protected GetPublicKey in SignedXml
  19. public AsymmetricAlgorithm PublicGetPublicKey ()
  20. {
  21. return base.GetPublicKey ();
  22. }
  23. }
  24. [TestFixture]
  25. public class SignedXmlTest : Assertion {
  26. private const string signature = "<Signature xmlns=\"http://www.w3.org/2000/09/xmldsig#\"><SignedInfo><CanonicalizationMethod Algorithm=\"http://www.w3.org/TR/2001/REC-xml-c14n-20010315\" /><SignatureMethod Algorithm=\"http://www.w3.org/2000/09/xmldsig#rsa-sha1\" /><Reference URI=\"#MyObjectId\"><DigestMethod Algorithm=\"http://www.w3.org/2000/09/xmldsig#sha1\" /><DigestValue>CTnnhjxUQHJmD+t1MjVXrOW+MCA=</DigestValue></Reference></SignedInfo><SignatureValue>dbFt6Zw3vR+Xh7LbM/vuifyFA7gPh/NlDM2Glz/SJBsveISieuTBpZlk/zavAeuXR/Nu0Ztt4OP4tCOg09a2RNlrTP0dhkeEfL1jTzpnVaLHuQbCiwOWCgbRif7Xt7N12FuiHYb3BltP/YyXS4E12NxlGlqnDiFA1v/mkK5+C1o=</SignatureValue><KeyInfo><KeyValue xmlns=\"http://www.w3.org/2000/09/xmldsig#\"><RSAKeyValue><Modulus>hEfTJNa2idz2u+fSYDDG4Lx/xuk4aBbvOPVNqgc1l9Y8t7Pt+ZyF+kkF3uUl8Y0700BFGAsprnhwrWENK+PGdtvM5796ZKxCCa0ooKkofiT4355HqK26hpV8dvj38vq/rkJe1jHZgkTKa+c/0vjcYZOI/RT/IZv9JfXxVWLuLxk=</Modulus><Exponent>EQ==</Exponent></RSAKeyValue></KeyValue></KeyInfo><Object Id=\"MyObjectId\" xmlns=\"http://www.w3.org/2000/09/xmldsig#\"><ObjectListTag xmlns=\"\" /></Object></Signature>";
  27. [Test]
  28. public void StaticValues ()
  29. {
  30. AssertEquals ("XmlDsigCanonicalizationUrl", "http://www.w3.org/TR/2001/REC-xml-c14n-20010315", SignedXml.XmlDsigCanonicalizationUrl);
  31. AssertEquals ("XmlDsigCanonicalizationWithCommentsUrl", "http://www.w3.org/TR/2001/REC-xml-c14n-20010315#WithComments", SignedXml.XmlDsigCanonicalizationWithCommentsUrl);
  32. AssertEquals ("XmlDsigDSAUrl", "http://www.w3.org/2000/09/xmldsig#dsa-sha1", SignedXml.XmlDsigDSAUrl);
  33. AssertEquals ("XmlDsigHMACSHA1Url", "http://www.w3.org/2000/09/xmldsig#hmac-sha1", SignedXml.XmlDsigHMACSHA1Url);
  34. AssertEquals ("XmlDsigMinimalCanonicalizationUrl", "http://www.w3.org/2000/09/xmldsig#minimal", SignedXml.XmlDsigMinimalCanonicalizationUrl);
  35. AssertEquals ("XmlDsigNamespaceUrl", "http://www.w3.org/2000/09/xmldsig#", SignedXml.XmlDsigNamespaceUrl);
  36. AssertEquals ("XmlDsigRSASHA1Url", "http://www.w3.org/2000/09/xmldsig#rsa-sha1", SignedXml.XmlDsigRSASHA1Url);
  37. AssertEquals ("XmlDsigSHA1Url", "http://www.w3.org/2000/09/xmldsig#sha1", SignedXml.XmlDsigSHA1Url);
  38. }
  39. [Test]
  40. public void Constructor_Empty ()
  41. {
  42. XmlDocument doc = new XmlDocument ();
  43. doc.LoadXml (signature);
  44. XmlNodeList xnl = doc.GetElementsByTagName ("Signature", SignedXml.XmlDsigNamespaceUrl);
  45. XmlElement xel = (XmlElement) xnl [0];
  46. SignedXml sx = new SignedXml (doc);
  47. sx.LoadXml (xel);
  48. Assert ("CheckSignature", sx.CheckSignature ());
  49. }
  50. [Test]
  51. public void Constructor_XmlDocument ()
  52. {
  53. XmlDocument doc = new XmlDocument ();
  54. doc.LoadXml (signature);
  55. XmlNodeList xnl = doc.GetElementsByTagName ("Signature", SignedXml.XmlDsigNamespaceUrl);
  56. XmlElement xel = (XmlElement) xnl [0];
  57. SignedXml sx = new SignedXml (doc);
  58. sx.LoadXml (doc.DocumentElement);
  59. Assert ("CheckSignature", sx.CheckSignature ());
  60. }
  61. [Test]
  62. #if NET_2_0
  63. [Ignore ("2.0 throws a NullReferenceException - reported as FDBK25892")]
  64. // http://lab.msdn.microsoft.com/ProductFeedback/viewfeedback.aspx?feedbackid=02dd9730-d1ad-4170-8c82-36858c55fbe2
  65. #endif
  66. [ExpectedException (typeof (ArgumentNullException))]
  67. public void Constructor_XmlDocument_Null ()
  68. {
  69. XmlDocument doc = null;
  70. SignedXml sx = new SignedXml (doc);
  71. }
  72. [Test]
  73. public void Constructor_XmlElement ()
  74. {
  75. XmlDocument doc = new XmlDocument ();
  76. doc.LoadXml (signature);
  77. XmlNodeList xnl = doc.GetElementsByTagName ("Signature", SignedXml.XmlDsigNamespaceUrl);
  78. XmlElement xel = (XmlElement) xnl [0];
  79. SignedXml sx = new SignedXml (doc.DocumentElement);
  80. sx.LoadXml (xel);
  81. Assert ("CheckSignature", sx.CheckSignature ());
  82. }
  83. [Test]
  84. #if !NET_2_0
  85. [ExpectedException (typeof (CryptographicException))]
  86. #endif
  87. public void Constructor_XmlElement_WithoutLoadXml ()
  88. {
  89. XmlDocument doc = new XmlDocument ();
  90. doc.LoadXml (signature);
  91. XmlNodeList xnl = doc.GetElementsByTagName ("Signature", SignedXml.XmlDsigNamespaceUrl);
  92. XmlElement xel = (XmlElement) xnl [0];
  93. SignedXml sx = new SignedXml (doc.DocumentElement);
  94. Assert ("!CheckSignature", !sx.CheckSignature ());
  95. // SignedXml (XmlElement) != SignedXml () + LoadXml (XmlElement)
  96. }
  97. [Test]
  98. [ExpectedException (typeof (ArgumentNullException))]
  99. public void Constructor_XmlElement_Null ()
  100. {
  101. XmlElement xel = null;
  102. SignedXml sx = new SignedXml (xel);
  103. }
  104. // sample from MSDN (url)
  105. public SignedXml MSDNSample ()
  106. {
  107. // Create example data to sign.
  108. XmlDocument document = new XmlDocument ();
  109. XmlNode node = document.CreateNode (XmlNodeType.Element, "", "MyElement", "samples");
  110. node.InnerText = "This is some text";
  111. document.AppendChild (node);
  112. // Create the SignedXml message.
  113. SignedXml signedXml = new SignedXml ();
  114. // Create a data object to hold the data to sign.
  115. DataObject dataObject = new DataObject ();
  116. dataObject.Data = document.ChildNodes;
  117. dataObject.Id = "MyObjectId";
  118. // Add the data object to the signature.
  119. signedXml.AddObject (dataObject);
  120. // Create a reference to be able to package everything into the
  121. // message.
  122. Reference reference = new Reference ();
  123. reference.Uri = "#MyObjectId";
  124. // Add it to the message.
  125. signedXml.AddReference (reference);
  126. return signedXml;
  127. }
  128. [Test]
  129. public void AsymmetricRSASignature ()
  130. {
  131. SignedXml signedXml = MSDNSample ();
  132. RSA key = RSA.Create ();
  133. signedXml.SigningKey = key;
  134. // Add a KeyInfo.
  135. KeyInfo keyInfo = new KeyInfo ();
  136. keyInfo.AddClause (new RSAKeyValue (key));
  137. signedXml.KeyInfo = keyInfo;
  138. AssertEquals ("KeyInfo", 1, signedXml.KeyInfo.Count);
  139. AssertNull ("SignatureLength", signedXml.SignatureLength);
  140. AssertNull ("SignatureMethod", signedXml.SignatureMethod);
  141. AssertNull ("SignatureValue", signedXml.SignatureValue);
  142. AssertNull ("SigningKeyName", signedXml.SigningKeyName);
  143. // Compute the signature.
  144. signedXml.ComputeSignature ();
  145. AssertNull ("SigningKeyName", signedXml.SigningKeyName);
  146. AssertEquals ("SignatureMethod", SignedXml.XmlDsigRSASHA1Url, signedXml.SignatureMethod);
  147. AssertEquals ("SignatureValue", 128, signedXml.SignatureValue.Length);
  148. AssertNull ("SigningKeyName", signedXml.SigningKeyName);
  149. // Get the XML representation of the signature.
  150. XmlElement xmlSignature = signedXml.GetXml ();
  151. // LAMESPEC: we must reload the signature or it won't work
  152. // MS framework throw a "malformed element"
  153. SignedXml vrfy = new SignedXml ();
  154. vrfy.LoadXml (xmlSignature);
  155. // assert that we can verify our own signature
  156. Assert ("RSA-Compute/Verify", vrfy.CheckSignature ());
  157. }
  158. [Test]
  159. public void AsymmetricDSASignature ()
  160. {
  161. SignedXml signedXml = MSDNSample ();
  162. DSA key = DSA.Create ();
  163. signedXml.SigningKey = key;
  164. // Add a KeyInfo.
  165. KeyInfo keyInfo = new KeyInfo ();
  166. keyInfo.AddClause (new DSAKeyValue (key));
  167. signedXml.KeyInfo = keyInfo;
  168. AssertEquals ("KeyInfo", 1, signedXml.KeyInfo.Count);
  169. AssertNull ("SignatureLength", signedXml.SignatureLength);
  170. AssertNull ("SignatureMethod", signedXml.SignatureMethod);
  171. AssertNull ("SignatureValue", signedXml.SignatureValue);
  172. AssertNull ("SigningKeyName", signedXml.SigningKeyName);
  173. // Compute the signature.
  174. signedXml.ComputeSignature ();
  175. AssertNull ("SignatureLength", signedXml.SignatureLength);
  176. AssertEquals ("SignatureMethod", SignedXml.XmlDsigDSAUrl, signedXml.SignatureMethod);
  177. AssertEquals ("SignatureValue", 40, signedXml.SignatureValue.Length);
  178. AssertNull ("SigningKeyName", signedXml.SigningKeyName);
  179. // Get the XML representation of the signature.
  180. XmlElement xmlSignature = signedXml.GetXml ();
  181. // LAMESPEC: we must reload the signature or it won't work
  182. // MS framework throw a "malformed element"
  183. SignedXml vrfy = new SignedXml ();
  184. vrfy.LoadXml (xmlSignature);
  185. // assert that we can verify our own signature
  186. Assert ("DSA-Compute/Verify", vrfy.CheckSignature ());
  187. }
  188. [Test]
  189. public void SymmetricHMACSHA1Signature ()
  190. {
  191. SignedXml signedXml = MSDNSample ();
  192. // Compute the signature.
  193. byte[] secretkey = Encoding.Default.GetBytes ("password");
  194. HMACSHA1 hmac = new HMACSHA1 (secretkey);
  195. #if NET_2_0
  196. AssertEquals ("KeyInfo", 0, signedXml.KeyInfo.Count);
  197. #else
  198. AssertNull ("KeyInfo", signedXml.KeyInfo);
  199. #endif
  200. AssertNull ("SignatureLength", signedXml.SignatureLength);
  201. AssertNull ("SignatureMethod", signedXml.SignatureMethod);
  202. AssertNull ("SignatureValue", signedXml.SignatureValue);
  203. AssertNull ("SigningKeyName", signedXml.SigningKeyName);
  204. signedXml.ComputeSignature (hmac);
  205. #if NET_2_0
  206. AssertEquals ("KeyInfo", 0, signedXml.KeyInfo.Count);
  207. #else
  208. AssertNull ("KeyInfo", signedXml.KeyInfo);
  209. #endif
  210. AssertNull ("SignatureLength", signedXml.SignatureLength);
  211. AssertEquals ("SignatureMethod", SignedXml.XmlDsigHMACSHA1Url, signedXml.SignatureMethod);
  212. AssertEquals ("SignatureValue", 20, signedXml.SignatureValue.Length);
  213. AssertNull ("SigningKeyName", signedXml.SigningKeyName);
  214. // Get the XML representation of the signature.
  215. XmlElement xmlSignature = signedXml.GetXml ();
  216. // LAMESPEC: we must reload the signature or it won't work
  217. // MS framework throw a "malformed element"
  218. SignedXml vrfy = new SignedXml ();
  219. vrfy.LoadXml (xmlSignature);
  220. // assert that we can verify our own signature
  221. Assert ("HMACSHA1-Compute/Verify", vrfy.CheckSignature (hmac));
  222. }
  223. [Test]
  224. [ExpectedException (typeof (CryptographicException))]
  225. public void SymmetricMACTripleDESSignature ()
  226. {
  227. SignedXml signedXml = MSDNSample ();
  228. // Compute the signature.
  229. byte[] secretkey = Encoding.Default.GetBytes ("password");
  230. MACTripleDES hmac = new MACTripleDES (secretkey);
  231. signedXml.ComputeSignature (hmac);
  232. }
  233. // Using empty constructor
  234. // LAMESPEC: The two other constructors don't seems to apply in verifying signatures
  235. [Test]
  236. public void AsymmetricRSAVerify ()
  237. {
  238. string value = "<Signature xmlns=\"http://www.w3.org/2000/09/xmldsig#\"><SignedInfo><CanonicalizationMethod Algorithm=\"http://www.w3.org/TR/2001/REC-xml-c14n-20010315\" /><SignatureMethod Algorithm=\"http://www.w3.org/2000/09/xmldsig#rsa-sha1\" /><Reference URI=\"#MyObjectId\"><DigestMethod Algorithm=\"http://www.w3.org/2000/09/xmldsig#sha1\" /><DigestValue>/Vvq6sXEVbtZC8GwNtLQnGOy/VI=</DigestValue></Reference></SignedInfo><SignatureValue>A6XuE8Cy9iOffRXaW9b0+dUcMUJQnlmwLsiqtQnADbCtZXnXAaeJ6nGnQ4Mm0IGi0AJc7/2CoJReXl7iW4hltmFguG1e3nl0VxCyCTHKGOCo1u8R3K+B1rTaenFbSxs42EM7/D9KETsPlzfYfis36yM3PqatiCUOsoMsAiMGzlc=</SignatureValue><KeyInfo><KeyValue xmlns=\"http://www.w3.org/2000/09/xmldsig#\"><RSAKeyValue><Modulus>tI8QYIpbG/m6JLyvP+S3X8mzcaAIayxomyTimSh9UCpEucRnGvLw0P73uStNpiF7wltTZA1HEsv+Ha39dY/0j/Wiy3RAodGDRNuKQao1wu34aNybZ673brbsbHFUfw/o7nlKD2xO84fbajBZmKtBBDy63NHt+QL+grSrREPfCTM=</Modulus><Exponent>AQAB</Exponent></RSAKeyValue></KeyValue></KeyInfo><Object Id=\"MyObjectId\"><MyElement xmlns=\"samples\">This is some text</MyElement></Object></Signature>";
  239. XmlDocument doc = new XmlDocument ();
  240. doc.LoadXml (value);
  241. SignedXml v1 = new SignedXml ();
  242. v1.LoadXml (doc.DocumentElement);
  243. Assert ("RSA-CheckSignature()", v1.CheckSignature ());
  244. SignedXml v2 = new SignedXml ();
  245. v2.LoadXml (doc.DocumentElement);
  246. AsymmetricAlgorithm key = null;
  247. bool vrfy = v2.CheckSignatureReturningKey (out key);
  248. Assert ("RSA-CheckSignatureReturningKey()", vrfy);
  249. SignedXml v3 = new SignedXml ();
  250. v3.LoadXml (doc.DocumentElement);
  251. Assert ("RSA-CheckSignature(key)", v3.CheckSignature (key));
  252. }
  253. // Using empty constructor
  254. // LAMESPEC: The two other constructors don't seems to apply in verifying signatures
  255. [Test]
  256. public void AsymmetricDSAVerify ()
  257. {
  258. string value = "<Signature xmlns=\"http://www.w3.org/2000/09/xmldsig#\"><SignedInfo><CanonicalizationMethod Algorithm=\"http://www.w3.org/TR/2001/REC-xml-c14n-20010315\" /><SignatureMethod Algorithm=\"http://www.w3.org/2000/09/xmldsig#dsa-sha1\" /><Reference URI=\"#MyObjectId\"><DigestMethod Algorithm=\"http://www.w3.org/2000/09/xmldsig#sha1\" /><DigestValue>/Vvq6sXEVbtZC8GwNtLQnGOy/VI=</DigestValue></Reference></SignedInfo><SignatureValue>BYz/qRGjGsN1yMFPxWa3awUZm1y4I/IxOQroMxkOteRGgk1HIwhRYw==</SignatureValue><KeyInfo><KeyValue xmlns=\"http://www.w3.org/2000/09/xmldsig#\"><DSAKeyValue><P>iglVaZ+LsSL8Y0aDXmFMBwva3xHqIypr3l/LtqBH9ziV2Sh1M4JVasAiKqytWIWt/s/Uk8Ckf2tO2Ww1vsNi1NL+Kg9T7FE52sn380/rF0miwGkZeidzm74OWhykb3J+wCTXaIwOzAWI1yN7FoeoN7wzF12jjlSXAXeqPMlViqk=</P><Q>u4sowiJMHilNRojtdmIuQY2YnB8=</Q><G>SdnN7d+wn1n+HH4Hr8MIryIRYgcXdbZ5TH7jAnuWc1koqRc1AZfcYAZ6RDf+orx6Lzn055FTFiN+1NHQfGUtXJCWW0zz0FVV1NJux7WRj8vGTldjJ5ef0oCenkpwDjcIxWsZgVobve4GPoyN1sAc1scnkJB59oupibklmF4y72A=</G><Y>XejzS8Z51yfl0zbYnxSYYbHqreSLjNCoGPB/KjM1TOyV5sMjz0StKtGrFWryTWc7EgvFY7kUth4e04VKf9HbK8z/FifHTXj8+Tszbjzw8GfInnBwLN+vJgbpnjtypmiI5Bm2nLiRbfkdAHP+OrKtr/EauM9GQfYuaxm3/Vj8B84=</Y><J>vGwGg9wqwwWP9xsoPoXu6kHArJtadiNKe9azBiUx5Ob883gd5wlKfEcGuKkBmBySGbgwxyOsIBovd9Kk48hF01ymfQzAAuHR0EdJECSsTsTTKVTLQNBU32O+PRbLYpv4E8kt6rNL83JLJCBY</J><Seed>sqzn8J6fd2gtEyq6YOqiUSHgPE8=</Seed><PgenCounter>sQ==</PgenCounter></DSAKeyValue></KeyValue></KeyInfo><Object Id=\"MyObjectId\"><MyElement xmlns=\"samples\">This is some text</MyElement></Object></Signature>";
  259. XmlDocument doc = new XmlDocument ();
  260. doc.LoadXml (value);
  261. SignedXml v1 = new SignedXml ();
  262. v1.LoadXml (doc.DocumentElement);
  263. Assert ("DSA-CheckSignature()", v1.CheckSignature ());
  264. SignedXml v2 = new SignedXml ();
  265. v2.LoadXml (doc.DocumentElement);
  266. AsymmetricAlgorithm key = null;
  267. bool vrfy = v2.CheckSignatureReturningKey (out key);
  268. Assert ("DSA-CheckSignatureReturningKey()", vrfy);
  269. SignedXml v3 = new SignedXml ();
  270. v3.LoadXml (doc.DocumentElement);
  271. Assert ("DSA-CheckSignature(key)", v3.CheckSignature (key));
  272. }
  273. [Test]
  274. public void SymmetricHMACSHA1Verify ()
  275. {
  276. string value = "<Signature xmlns=\"http://www.w3.org/2000/09/xmldsig#\"><SignedInfo><CanonicalizationMethod Algorithm=\"http://www.w3.org/TR/2001/REC-xml-c14n-20010315\" /><SignatureMethod Algorithm=\"http://www.w3.org/2000/09/xmldsig#hmac-sha1\" /><Reference URI=\"#MyObjectId\"><DigestMethod Algorithm=\"http://www.w3.org/2000/09/xmldsig#sha1\" /><DigestValue>/Vvq6sXEVbtZC8GwNtLQnGOy/VI=</DigestValue></Reference></SignedInfo><SignatureValue>e2RxYr5yGbvTqZLCFcgA2RAC0yE=</SignatureValue><Object Id=\"MyObjectId\"><MyElement xmlns=\"samples\">This is some text</MyElement></Object></Signature>";
  277. XmlDocument doc = new XmlDocument ();
  278. doc.LoadXml (value);
  279. SignedXml v1 = new SignedXml ();
  280. v1.LoadXml (doc.DocumentElement);
  281. byte[] secretkey = Encoding.Default.GetBytes ("password");
  282. HMACSHA1 hmac = new HMACSHA1 (secretkey);
  283. Assert ("HMACSHA1-CheckSignature(key)", v1.CheckSignature (hmac));
  284. }
  285. [Test]
  286. // adapted from http://bugzilla.ximian.com/show_bug.cgi?id=52084
  287. public void GetIdElement ()
  288. {
  289. XmlDocument doc = new XmlDocument ();
  290. doc.LoadXml (signature);
  291. SignedXml v1 = new SignedXml ();
  292. v1.LoadXml (doc.DocumentElement);
  293. Assert ("CheckSignature", v1.CheckSignature ());
  294. XmlElement xel = v1.GetIdElement (doc, "MyObjectId");
  295. Assert ("GetIdElement", xel.InnerXml.StartsWith ("<ObjectListTag"));
  296. }
  297. [Test]
  298. public void GetPublicKey ()
  299. {
  300. XmlDocument doc = new XmlDocument ();
  301. doc.LoadXml (signature);
  302. SignedXmlEx sxe = new SignedXmlEx ();
  303. sxe.LoadXml (doc.DocumentElement);
  304. AsymmetricAlgorithm aa1 = sxe.PublicGetPublicKey ();
  305. Assert ("First Public Key is RSA", (aa1 is RSA));
  306. AsymmetricAlgorithm aa2 = sxe.PublicGetPublicKey ();
  307. AssertNull ("Second Public Key is null", aa2);
  308. }
  309. #if NET_2_0
  310. [Test]
  311. // [ExpectedException (typeof (ArgumentNullException))]
  312. public void AddObject_Null ()
  313. {
  314. SignedXml sx = new SignedXml ();
  315. // still no ArgumentNullExceptions for this one
  316. sx.AddObject (null);
  317. }
  318. [Test]
  319. [ExpectedException (typeof (ArgumentNullException))]
  320. public void AddReference_Null ()
  321. {
  322. SignedXml sx = new SignedXml ();
  323. sx.AddReference (null);
  324. }
  325. #else
  326. [Test]
  327. public void Add_Null ()
  328. {
  329. SignedXml sx = new SignedXml ();
  330. // no ArgumentNull exceptions for those
  331. sx.AddObject (null);
  332. sx.AddReference (null);
  333. }
  334. #endif
  335. [Test]
  336. [ExpectedException (typeof (CryptographicException))]
  337. public void GetXml_WithoutInfo ()
  338. {
  339. SignedXml sx = new SignedXml ();
  340. XmlElement xel = sx.GetXml ();
  341. }
  342. [Test]
  343. [ExpectedException (typeof (ArgumentNullException))]
  344. public void LoadXml_Null ()
  345. {
  346. SignedXml sx = new SignedXml ();
  347. sx.LoadXml (null);
  348. }
  349. [Test]
  350. public void SigningKeyName ()
  351. {
  352. SignedXmlEx sxe = new SignedXmlEx ();
  353. AssertNull ("SigningKeyName", sxe.SigningKeyName);
  354. sxe.SigningKeyName = "mono";
  355. AssertEquals ("SigningKeyName", "mono", sxe.SigningKeyName);
  356. }
  357. [Test]
  358. public void CheckSignatureEmptySafe ()
  359. {
  360. SignedXml sx;
  361. KeyInfoClause kic;
  362. KeyInfo ki;
  363. // empty keyinfo passes...
  364. sx = new SignedXml ();
  365. sx.KeyInfo = new KeyInfo ();
  366. Assert (!sx.CheckSignature ());
  367. // with empty KeyInfoName
  368. kic = new KeyInfoName ();
  369. ki = new KeyInfo ();
  370. ki.AddClause (kic);
  371. sx.KeyInfo = ki;
  372. Assert (!sx.CheckSignature ());
  373. }
  374. [Test]
  375. #if !NET_2_0
  376. [ExpectedException (typeof (CryptographicException))]
  377. #endif
  378. public void CheckSignatureEmpty ()
  379. {
  380. SignedXml sx = new SignedXml ();
  381. Assert (!sx.CheckSignature ());
  382. }
  383. }
  384. }