1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201
|
//
// SamlAssertionTest.cs
//
// Author:
// Atsushi Enomoto <atsushi@ximian.com>
//
// Copyright (C) 2006 Novell, Inc. http://www.novell.com
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
using System;
using System.Globalization;
using System.IO;
using System.IdentityModel.Claims;
using System.IdentityModel.Selectors;
using System.IdentityModel.Tokens;
using System.Xml;
using NUnit.Framework;
using MonoTests.System.IdentityModel.Common;
namespace MonoTests.System.IdentityModel.Tokens
{
[TestFixture]
public class SamlAssertionTest
{
[Test]
public void DefaultValues ()
{
DateTime d1 = DateTime.UtcNow, d2;
SamlAssertion a = new SamlAssertion ();
d2 = DateTime.UtcNow;
Assert.IsNull (a.Advice, "#1");
Assert.IsNotNull (a.AssertionId, "#2");
Assert.IsTrue (d1 <= a.IssueInstant && a.IssueInstant <= d2, "#3");
Assert.IsNull (a.Issuer, "#4");
Assert.AreEqual (1, a.MajorVersion, "#5");
Assert.AreEqual (1, a.MinorVersion, "#6");
Assert.IsNull (a.SigningCredentials, "#7");
Assert.IsNull (a.SigningToken, "#8");
}
XmlDictionaryWriter CreateWriter (StringWriter sw)
{
return XmlDictionaryWriter.CreateDictionaryWriter (XmlWriter.Create (sw));
}
[Test]
[ExpectedException (typeof (ArgumentException))]
public void ConstructorAssertionIdStartsWithNumbers ()
{
new SamlAssertion ("01234567890",
"dummy-issuer", DateTime.UtcNow, null, null, new SamlStatement [] {new SamlAuthenticationStatement ()});
}
[Test]
[ExpectedException (typeof (ArgumentException))]
[Category ("NotDotNet")]
public void ConstructorAssertionIdContainsInvalidChar ()
{
new SamlAssertion ("invalid-name%here",
"dummy-issuer", DateTime.UtcNow, null, null, new SamlStatement [] {new SamlAuthenticationStatement ()});
}
[Test]
[ExpectedException (typeof (ArgumentException))]
public void ConstructorEmptyIssuer ()
{
new SamlAssertion ("SamlSecurityToken-" + Guid.NewGuid (),
String.Empty, DateTime.UtcNow, null, null, null);
}
[Test]
[ExpectedException (typeof (ArgumentNullException))]
public void ConstructorNullStatements ()
{
new SamlAssertion ("SamlSecurityToken-" + Guid.NewGuid (),
"dummy-issuer", DateTime.UtcNow, null, null, null);
}
[Test]
[ExpectedException (typeof (ArgumentException))]
public void ConstructorStatementsContainNull ()
{
new SamlAssertion ("SamlSecurityToken-" + Guid.NewGuid (),
"dummy-issuer", DateTime.UtcNow, null, null, new SamlStatement [] {null});
}
[Test]
[ExpectedException (typeof (ArgumentException))]
public void ConstructorNoStatement ()
{
new SamlAssertion ("SamlSecurityToken-" + Guid.NewGuid (),
"dummy-issuer", DateTime.UtcNow, null, null, new SamlStatement [0]);
}
[Test]
public void Constructor ()
{
new SamlAssertion ("SamlSecurityToken-" + Guid.NewGuid (),
"dummy-issuer", DateTime.UtcNow, null, null,
new SamlStatement [] {new SamlAuthenticationStatement ()});
}
[Test]
[ExpectedException (typeof (SecurityTokenException))]
public void WriteXmlNullIssuer ()
{
SamlAssertion a = new SamlAssertion ();
using (XmlDictionaryWriter dw = CreateWriter (new StringWriter ())) {
a.WriteXml (dw, null, null);
}
}
[Test]
[ExpectedException (typeof (SecurityTokenException))]
public void WriteXmlNoStatement ()
{
SamlAssertion a = new SamlAssertion ();
a.Issuer = "my_boss";
using (XmlDictionaryWriter dw = CreateWriter (new StringWriter ())) {
a.WriteXml (dw, null, null);
}
}
[Test]
[ExpectedException (typeof (ArgumentNullException))]
public void WriteXmlNullSerializer ()
{
SamlAssertion a = new SamlAssertion ();
a.Statements.Add (new SamlAttributeStatement ());
a.Issuer = "my_hero";
using (XmlDictionaryWriter dw = CreateWriter (new StringWriter ())) {
a.WriteXml (dw, null, null);
}
}
[Test]
[ExpectedException (typeof (InvalidOperationException))]
public void WriteXmlWithoutSamlSubject ()
{
SamlAssertion a = new SamlAssertion ();
a.Statements.Add (new SamlAttributeStatement ());
a.Issuer = "my_boss";
StringWriter sw = new StringWriter ();
using (XmlDictionaryWriter dw = CreateWriter (sw)) {
a.WriteXml (dw, new SamlSerializer (), null);
}
Assert.AreEqual ("<?xml version=\"1.0\" ?>", sw.ToString ());
}
[Test]
public void WriteXmlValid ()
{
SamlAssertion a = new SamlAssertion ();
SamlSubject subject = new SamlSubject (
SamlConstants.UserNameNamespace,
"urn:myqualifier",
"myname");
SamlAttribute attr = new SamlAttribute (Claim.CreateNameClaim ("myname"));
SamlAttributeStatement statement =
new SamlAttributeStatement (subject, new SamlAttribute [] {attr});
a.Advice = new SamlAdvice (new string [] {"urn:testadvice1"});
DateTime notBefore = DateTime.SpecifyKind (new DateTime (2000, 1, 1), DateTimeKind.Utc);
DateTime notOnAfter = DateTime.SpecifyKind (new DateTime (2006, 1, 1), DateTimeKind.Utc);
a.Conditions = new SamlConditions (notBefore, notOnAfter);
a.Statements.Add (statement);
a.Issuer = "my_hero";
StringWriter sw = new StringWriter ();
string id = a.AssertionId;
DateTime instant = a.IssueInstant;
using (XmlDictionaryWriter dw = CreateWriter (sw)) {
a.WriteXml (dw, new SamlSerializer (), null);
}
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>",
id,
instant.ToString ("yyyy-MM-ddTHH:mm:ss.fff'Z'", CultureInfo.InvariantCulture),
"http://schemas.xmlsoap.org/ws/2005/05/identity/claims",
notBefore.ToString ("yyyy-MM-ddTHH:mm:ss.fff'Z'", CultureInfo.InvariantCulture),
notOnAfter.ToString ("yyyy-MM-ddTHH:mm:ss.fff'Z'", CultureInfo.InvariantCulture));
Assert.AreEqual (expected, sw.ToString ());
}
}
}
|