SamlAssertionTest.cs 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. //
  2. // SamlAssertionTest.cs
  3. //
  4. // Author:
  5. // Atsushi Enomoto <[email protected]>
  6. //
  7. // Copyright (C) 2006 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.Globalization;
  30. using System.IO;
  31. using System.IdentityModel.Claims;
  32. using System.IdentityModel.Selectors;
  33. using System.IdentityModel.Tokens;
  34. using System.Xml;
  35. using NUnit.Framework;
  36. using MonoTests.System.IdentityModel.Common;
  37. namespace MonoTests.System.IdentityModel.Tokens
  38. {
  39. [TestFixture]
  40. public class SamlAssertionTest
  41. {
  42. [Test]
  43. public void DefaultValues ()
  44. {
  45. DateTime d1 = DateTime.UtcNow, d2;
  46. SamlAssertion a = new SamlAssertion ();
  47. d2 = DateTime.UtcNow;
  48. Assert.IsNull (a.Advice, "#1");
  49. Assert.IsNotNull (a.AssertionId, "#2");
  50. Assert.IsTrue (d1 <= a.IssueInstant && a.IssueInstant <= d2, "#3");
  51. Assert.IsNull (a.Issuer, "#4");
  52. Assert.AreEqual (1, a.MajorVersion, "#5");
  53. Assert.AreEqual (1, a.MinorVersion, "#6");
  54. Assert.IsNull (a.SigningCredentials, "#7");
  55. Assert.IsNull (a.SigningToken, "#8");
  56. }
  57. XmlDictionaryWriter CreateWriter (StringWriter sw)
  58. {
  59. return XmlDictionaryWriter.CreateDictionaryWriter (XmlWriter.Create (sw));
  60. }
  61. [Test]
  62. [ExpectedException (typeof (ArgumentException))]
  63. public void ConstructorAssertionIdStartsWithNumbers ()
  64. {
  65. new SamlAssertion ("01234567890",
  66. "dummy-issuer", DateTime.UtcNow, null, null, new SamlStatement [] {new SamlAuthenticationStatement ()});
  67. }
  68. [Test]
  69. [ExpectedException (typeof (ArgumentException))]
  70. [Category ("NotDotNet")]
  71. public void ConstructorAssertionIdContainsInvalidChar ()
  72. {
  73. new SamlAssertion ("invalid-name%here",
  74. "dummy-issuer", DateTime.UtcNow, null, null, new SamlStatement [] {new SamlAuthenticationStatement ()});
  75. }
  76. [Test]
  77. [ExpectedException (typeof (ArgumentException))]
  78. public void ConstructorEmptyIssuer ()
  79. {
  80. new SamlAssertion ("SamlSecurityToken-" + Guid.NewGuid (),
  81. String.Empty, DateTime.UtcNow, null, null, null);
  82. }
  83. [Test]
  84. [ExpectedException (typeof (ArgumentNullException))]
  85. public void ConstructorNullStatements ()
  86. {
  87. new SamlAssertion ("SamlSecurityToken-" + Guid.NewGuid (),
  88. "dummy-issuer", DateTime.UtcNow, null, null, null);
  89. }
  90. [Test]
  91. [ExpectedException (typeof (ArgumentException))]
  92. public void ConstructorStatementsContainNull ()
  93. {
  94. new SamlAssertion ("SamlSecurityToken-" + Guid.NewGuid (),
  95. "dummy-issuer", DateTime.UtcNow, null, null, new SamlStatement [] {null});
  96. }
  97. [Test]
  98. [ExpectedException (typeof (ArgumentException))]
  99. public void ConstructorNoStatement ()
  100. {
  101. new SamlAssertion ("SamlSecurityToken-" + Guid.NewGuid (),
  102. "dummy-issuer", DateTime.UtcNow, null, null, new SamlStatement [0]);
  103. }
  104. [Test]
  105. public void Constructor ()
  106. {
  107. new SamlAssertion ("SamlSecurityToken-" + Guid.NewGuid (),
  108. "dummy-issuer", DateTime.UtcNow, null, null,
  109. new SamlStatement [] {new SamlAuthenticationStatement ()});
  110. }
  111. [Test]
  112. [ExpectedException (typeof (SecurityTokenException))]
  113. public void WriteXmlNullIssuer ()
  114. {
  115. SamlAssertion a = new SamlAssertion ();
  116. using (XmlDictionaryWriter dw = CreateWriter (new StringWriter ())) {
  117. a.WriteXml (dw, null, null);
  118. }
  119. }
  120. [Test]
  121. [ExpectedException (typeof (SecurityTokenException))]
  122. public void WriteXmlNoStatement ()
  123. {
  124. SamlAssertion a = new SamlAssertion ();
  125. a.Issuer = "my_boss";
  126. using (XmlDictionaryWriter dw = CreateWriter (new StringWriter ())) {
  127. a.WriteXml (dw, null, null);
  128. }
  129. }
  130. [Test]
  131. [ExpectedException (typeof (ArgumentNullException))]
  132. public void WriteXmlNullSerializer ()
  133. {
  134. SamlAssertion a = new SamlAssertion ();
  135. a.Statements.Add (new SamlAttributeStatement ());
  136. a.Issuer = "my_hero";
  137. using (XmlDictionaryWriter dw = CreateWriter (new StringWriter ())) {
  138. a.WriteXml (dw, null, null);
  139. }
  140. }
  141. [Test]
  142. [ExpectedException (typeof (InvalidOperationException))]
  143. public void WriteXmlWithoutSamlSubject ()
  144. {
  145. SamlAssertion a = new SamlAssertion ();
  146. a.Statements.Add (new SamlAttributeStatement ());
  147. a.Issuer = "my_boss";
  148. StringWriter sw = new StringWriter ();
  149. using (XmlDictionaryWriter dw = CreateWriter (sw)) {
  150. a.WriteXml (dw, new SamlSerializer (), null);
  151. }
  152. Assert.AreEqual ("<?xml version=\"1.0\" ?>", sw.ToString ());
  153. }
  154. [Test]
  155. public void WriteXmlValid ()
  156. {
  157. SamlAssertion a = new SamlAssertion ();
  158. SamlSubject subject = new SamlSubject (
  159. SamlConstants.UserNameNamespace,
  160. "urn:myqualifier",
  161. "myname");
  162. SamlAttribute attr = new SamlAttribute (Claim.CreateNameClaim ("myname"));
  163. SamlAttributeStatement statement =
  164. new SamlAttributeStatement (subject, new SamlAttribute [] {attr});
  165. a.Advice = new SamlAdvice (new string [] {"urn:testadvice1"});
  166. DateTime notBefore = DateTime.SpecifyKind (new DateTime (2000, 1, 1), DateTimeKind.Utc);
  167. DateTime notOnAfter = DateTime.SpecifyKind (new DateTime (2006, 1, 1), DateTimeKind.Utc);
  168. a.Conditions = new SamlConditions (notBefore, notOnAfter);
  169. a.Statements.Add (statement);
  170. a.Issuer = "my_hero";
  171. StringWriter sw = new StringWriter ();
  172. string id = a.AssertionId;
  173. DateTime instant = a.IssueInstant;
  174. using (XmlDictionaryWriter dw = CreateWriter (sw)) {
  175. a.WriteXml (dw, new SamlSerializer (), null);
  176. }
  177. string expected = String.Format ("<?xml version=\"1.0\" encoding=\"utf-16\"?><saml:Assertion MajorVersion=\"1\" MinorVersion=\"1\" AssertionID=\"{0}\" Issuer=\"my_hero\" IssueInstant=\"{1}\" xmlns:saml=\"urn:oasis:names:tc:SAML:1.0:assertion\"><saml:Conditions NotBefore=\"{3}\" NotOnOrAfter=\"{4}\" /><saml:Advice><saml:AssertionIDReference>urn:testadvice1</saml:AssertionIDReference></saml:Advice><saml:AttributeStatement><saml:Subject><saml:NameIdentifier Format=\"urn:oasis:names:tc:SAML:1.1:nameid-format:WindowsDomainQualifiedName\" NameQualifier=\"urn:myqualifier\">myname</saml:NameIdentifier></saml:Subject><saml:Attribute AttributeName=\"name\" AttributeNamespace=\"{2}\"><saml:AttributeValue>myname</saml:AttributeValue></saml:Attribute></saml:AttributeStatement></saml:Assertion>",
  178. id,
  179. instant.ToString ("yyyy-MM-ddTHH:mm:ss.fff'Z'", CultureInfo.InvariantCulture),
  180. "http://schemas.xmlsoap.org/ws/2005/05/identity/claims",
  181. notBefore.ToString ("yyyy-MM-ddTHH:mm:ss.fff'Z'", CultureInfo.InvariantCulture),
  182. notOnAfter.ToString ("yyyy-MM-ddTHH:mm:ss.fff'Z'", CultureInfo.InvariantCulture));
  183. Assert.AreEqual (expected, sw.ToString ());
  184. }
  185. }
  186. }