SamlAuthorizationDecisionStatementTest.cs 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. //
  2. // SamlAuthorizationDecisionStatementTest.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. namespace MonoTests.System.IdentityModel.Tokens
  37. {
  38. [TestFixture]
  39. public class SamlAuthorizationDecisionStatementTest
  40. {
  41. XmlDictionaryWriter CreateWriter (StringWriter sw)
  42. {
  43. return XmlDictionaryWriter.CreateDictionaryWriter (XmlWriter.Create (sw));
  44. }
  45. XmlDictionaryReader CreateReader (string xml)
  46. {
  47. return XmlDictionaryReader.CreateDictionaryReader (XmlReader.Create (new StringReader (xml)));
  48. }
  49. [Test]
  50. public void DefaultValues ()
  51. {
  52. SamlAuthorizationDecisionStatement a = new SamlAuthorizationDecisionStatement ();
  53. Assert.AreEqual (default (SamlAccessDecision), a.AccessDecision, "#1");
  54. Assert.IsNull (a.Evidence, "#2");
  55. Assert.IsNull (a.Resource, "#3");
  56. Assert.IsNull (a.SamlSubject, "#4");
  57. Assert.AreEqual (0, a.SamlActions.Count, "#5");
  58. }
  59. [Test]
  60. [ExpectedException (typeof (ArgumentNullException))]
  61. public void ConstructorNullSubject ()
  62. {
  63. new SamlAuthorizationDecisionStatement (null, "resource", default (SamlAccessDecision), null);
  64. }
  65. [Test]
  66. [ExpectedException (typeof (SecurityTokenException))] // ???? it is inconsistent behavior with others...
  67. public void ConstructorNullResource ()
  68. {
  69. new SamlAuthorizationDecisionStatement (
  70. new SamlSubject (), null, default (SamlAccessDecision), new SamlAction [0]);
  71. }
  72. [Test]
  73. [ExpectedException (typeof (ArgumentNullException))]
  74. public void ConstructorNullActions ()
  75. {
  76. new SamlAuthorizationDecisionStatement (
  77. new SamlSubject (), "resource", default (SamlAccessDecision), null);
  78. }
  79. [Test]
  80. [ExpectedException (typeof (ArgumentException))]
  81. public void ConstructorActionsContainNull ()
  82. {
  83. new SamlAuthorizationDecisionStatement (
  84. new SamlSubject (), "resource", default (SamlAccessDecision), new SamlAction [] {null});
  85. }
  86. [Test]
  87. [ExpectedException (typeof (ArgumentException))]
  88. public void SetResourceEmpty ()
  89. {
  90. SamlAuthorizationDecisionStatement a = new SamlAuthorizationDecisionStatement ();
  91. a.Resource = String.Empty;
  92. }
  93. [Test]
  94. public void SetEvidenceNull ()
  95. {
  96. SamlAuthorizationDecisionStatement a = new SamlAuthorizationDecisionStatement ();
  97. a.Evidence = null;
  98. }
  99. [Test]
  100. [ExpectedException (typeof (SecurityTokenException))]
  101. public void WriteXmlNoSubject ()
  102. {
  103. SamlAuthorizationDecisionStatement a = new SamlAuthorizationDecisionStatement ();
  104. StringWriter sw = new StringWriter ();
  105. using (XmlDictionaryWriter dw = CreateWriter (sw)) {
  106. a.WriteXml (dw, new SamlSerializer (), null);
  107. }
  108. }
  109. [Test]
  110. [ExpectedException (typeof (SecurityTokenException))]
  111. public void WriteXmlNoResource ()
  112. {
  113. SamlAuthorizationDecisionStatement a = new SamlAuthorizationDecisionStatement ();
  114. a.SamlSubject = new SamlSubject ("myFormat", "myQualifier", "myName");
  115. StringWriter sw = new StringWriter ();
  116. using (XmlDictionaryWriter dw = CreateWriter (sw)) {
  117. a.WriteXml (dw, new SamlSerializer (), null);
  118. }
  119. }
  120. [Test]
  121. [ExpectedException (typeof (SecurityTokenException))]
  122. public void WriteXmlNoAction ()
  123. {
  124. SamlAuthorizationDecisionStatement a = new SamlAuthorizationDecisionStatement ();
  125. a.SamlSubject = new SamlSubject ("myFormat", "myQualifier", "myName");
  126. a.Resource = "resource";
  127. StringWriter sw = new StringWriter ();
  128. using (XmlDictionaryWriter dw = CreateWriter (sw)) {
  129. a.WriteXml (dw, new SamlSerializer (), null);
  130. }
  131. }
  132. [Test]
  133. public void WriteXml1 ()
  134. {
  135. SamlAuthorizationDecisionStatement a = new SamlAuthorizationDecisionStatement ();
  136. a.SamlSubject = new SamlSubject ("myFormat", "myQualifier", "myName");
  137. a.Resource = "resource";
  138. a.SamlActions.Add (new SamlAction ("myAction"));
  139. a.Evidence = new SamlEvidence (new string [] {"myID"});
  140. StringWriter sw = new StringWriter ();
  141. using (XmlDictionaryWriter dw = CreateWriter (sw)) {
  142. a.WriteXml (dw, new SamlSerializer (), null);
  143. }
  144. Assert.AreEqual (String.Format ("<?xml version=\"1.0\" encoding=\"utf-16\"?><saml:AuthorizationDecisionStatement Decision=\"Permit\" Resource=\"resource\" xmlns:saml=\"{0}\"><saml:Subject><saml:NameIdentifier Format=\"myFormat\" NameQualifier=\"myQualifier\">myName</saml:NameIdentifier></saml:Subject><saml:Action>myAction</saml:Action><saml:Evidence><saml:AssertionIDReference>myID</saml:AssertionIDReference></saml:Evidence></saml:AuthorizationDecisionStatement>", SamlConstants.Namespace), sw.ToString ());
  145. }
  146. [Test]
  147. public void ReadXml1 ()
  148. {
  149. SamlSerializer ser = new SamlSerializer ();
  150. string xml = String.Format ("<saml:AuthorizationDecisionStatement Decision=\"Deny\" Resource=\"resource\" xmlns:saml=\"{0}\"><saml:Subject><saml:NameIdentifier Format=\"myFormat\" NameQualifier=\"myQualifier\">myName</saml:NameIdentifier></saml:Subject><saml:Action>myAction</saml:Action><saml:Evidence><saml:AssertionIDReference>myID</saml:AssertionIDReference></saml:Evidence></saml:AuthorizationDecisionStatement>", SamlConstants.Namespace);
  151. XmlDictionaryReader reader = CreateReader (xml);
  152. reader.MoveToContent ();
  153. SamlAuthorizationDecisionStatement s =
  154. new SamlAuthorizationDecisionStatement ();
  155. s.ReadXml (reader, ser, null, null);
  156. Assert.AreEqual (SamlAccessDecision.Deny, s.AccessDecision, "#1");
  157. Assert.IsNotNull (s.SamlSubject, "#2");
  158. Assert.AreEqual (1, s.SamlActions.Count, "#3");
  159. Assert.AreEqual ("myAction", s.SamlActions [0].Action, "#4");
  160. Assert.IsNotNull (s.Evidence, "#5");
  161. Assert.AreEqual (1, s.Evidence.AssertionIdReferences.Count, "#6");
  162. Assert.AreEqual ("myID", s.Evidence.AssertionIdReferences [0], "#7");
  163. Assert.AreEqual ("resource", s.Resource, "#8");
  164. }
  165. }
  166. }