2 // BinarySecurityTokenTest.cs
3 // - NUnit Test Cases for BinarySecurityToken
6 // Sebastien Pouliot (spouliot@motus.com)
8 // (C) 2002, 2003 Motus Technologies Inc. (http://www.motus.com)
11 using NUnit
.Framework
;
12 using Microsoft
.Web
.Services
.Security
;
14 using System
.Security
.Cryptography
;
16 using MWSS
= Microsoft
.Web
.Services
.Security
;
18 namespace MonoTests
.MS
.Web
.Services
.Security
{
20 // non-abstract BinarySecurityToken for test uses only
21 public class BinarySecurityToken
: MWSS
.BinarySecurityToken
{
24 public BinarySecurityToken (XmlElement element
) : base (element
) {}
26 public BinarySecurityToken (XmlQualifiedName valueType
) : base (valueType
) {}
28 public override AuthenticationKey AuthenticationKey
{
32 public override DecryptionKey DecryptionKey
{
36 public override EncryptionKey EncryptionKey
{
40 public override SignatureKey SignatureKey
{
44 public override bool SupportsDataEncryption
{
48 public override bool SupportsDigitalSignature
{
52 public override void Verify() {}
54 public override bool Equals (SecurityToken token
)
59 public override int GetHashCode ()
64 public override bool IsCurrent
{
71 public class BinarySecurityTokenTest
: Assertion
{
73 private static string name
= "mono";
74 private static string ns
= "http://www.go-mono.com/";
77 [ExpectedException (typeof (ArgumentNullException
))]
78 public void ConstructorNullXmlElement ()
80 // we do not want to confuse the compiler about null ;-)
81 XmlElement xel
= null;
82 BinarySecurityToken bst
= new BinarySecurityToken (xel
);
86 public void ConstructorXmlElement ()
88 string xml
= "<wsse:BinarySecurityToken xmlns:vt=\"http://www.go-mono.com/\" ValueType=\"vt:mono\" EncodingType=\"wsse:Base64Binary\" xmlns:wsu=\"http://schemas.xmlsoap.org/ws/2002/07/utility\" wsu:Id=\"SecurityToken-eb24c89d-012a-431e-af2d-db6a41f1b88e\" xmlns:wsse=\"http://schemas.xmlsoap.org/ws/2002/07/secext\" />";
89 XmlDocument doc
= new XmlDocument ();
91 BinarySecurityToken bst
= new BinarySecurityToken (doc
.DocumentElement
);
92 AssertNotNull ("BinarySecurityToken(XmlQualifiedName)", bst
);
93 AssertEquals ("EncodingType.Name", "Base64Binary", bst
.EncodingType
.Name
);
94 AssertEquals ("EncodingType.Namespace", "http://schemas.xmlsoap.org/ws/2002/07/secext", bst
.EncodingType
.Namespace
);
95 AssertEquals ("ValueType.Name", name
, bst
.ValueType
.Name
);
96 AssertEquals ("ValueType.Namespace", ns
, bst
.ValueType
.Namespace
);
97 AssertNull ("RawData", bst
.RawData
);
98 Assert ("Id", bst
.Id
.StartsWith ("SecurityToken-"));
102 [ExpectedException (typeof (ArgumentNullException
))]
103 public void ConstructorNullXmlQualifiedName ()
105 // we do not want to confuse the compiler about null ;-)
106 XmlQualifiedName xqn
= null;
107 BinarySecurityToken bst
= new BinarySecurityToken (xqn
);
111 public void ConstructorXmlQualifiedName ()
113 XmlQualifiedName xqn
= new XmlQualifiedName (name
, ns
);
114 BinarySecurityToken bst
= new BinarySecurityToken (xqn
);
115 AssertNotNull ("BinarySecurityToken(XmlQualifiedName)", bst
);
116 AssertEquals ("EncodingType.Name", "Base64Binary", bst
.EncodingType
.Name
);
117 AssertEquals ("EncodingType.Namespace", "http://schemas.xmlsoap.org/ws/2002/07/secext", bst
.EncodingType
.Namespace
);
118 AssertEquals ("ValueType.Name", name
, bst
.ValueType
.Name
);
119 AssertEquals ("ValueType.Namespace", ns
, bst
.ValueType
.Namespace
);
120 AssertNull ("RawData", bst
.RawData
);
121 Assert ("Id", bst
.Id
.StartsWith ("SecurityToken-"));
125 [ExpectedException (typeof (ArgumentNullException
))]
126 public void NullEncodingType ()
128 XmlQualifiedName xqn
= new XmlQualifiedName (name
, ns
);
129 BinarySecurityToken bst
= new BinarySecurityToken (xqn
);
130 bst
.EncodingType
= null;
134 [ExpectedException (typeof (ArgumentNullException
))]
135 public void NullValueType ()
137 XmlQualifiedName xqn
= new XmlQualifiedName (name
, ns
);
138 BinarySecurityToken bst
= new BinarySecurityToken (xqn
);
139 bst
.ValueType
= null;
143 [ExpectedException (typeof (ArgumentNullException
))]
144 public void GetXmlNull ()
146 XmlQualifiedName xqn
= new XmlQualifiedName (name
, ns
);
147 BinarySecurityToken bst
= new BinarySecurityToken (xqn
);
148 XmlElement xel
= bst
.GetXml (null);
152 public void GetXml ()
154 XmlQualifiedName xqn
= new XmlQualifiedName (name
, ns
);
155 BinarySecurityToken bst
= new BinarySecurityToken (xqn
);
156 bst
.Id
= "staticIdUsedForNUnit";
157 XmlDocument doc
= new XmlDocument ();
158 XmlElement xel
= bst
.GetXml (doc
);
159 // this one I can't generate exactly like the original so I check each parts
160 Assert ("GetXml(doc)1", xel
.OuterXml
.StartsWith ("<wsse:BinarySecurityToken "));
161 Assert ("GetXml(doc)2", xel
.OuterXml
.IndexOf ("xmlns:vt=\"http://www.go-mono.com/\"") > 0);
162 Assert ("GetXml(doc)3", xel
.OuterXml
.IndexOf ("ValueType=\"vt:mono\"") > 0);
163 Assert ("GetXml(doc)4", xel
.OuterXml
.IndexOf ("EncodingType=\"wsse:Base64Binary\"") > 0);
164 Assert ("GetXml(doc)5", xel
.OuterXml
.IndexOf ("xmlns:wsu=\"http://schemas.xmlsoap.org/ws/2002/07/utility\"") > 0);
165 Assert ("GetXml(doc)6", xel
.OuterXml
.IndexOf ("wsu:Id=\"staticIdUsedForNUnit\"") > 0);
166 Assert ("GetXml(doc)7", xel
.OuterXml
.IndexOf ("xmlns:wsse=\"http://schemas.xmlsoap.org/ws/2002/07/secext\"") > 0);
170 [ExpectedException (typeof (ArgumentNullException
))]
171 public void LoadXmlNull ()
173 XmlQualifiedName xqn
= new XmlQualifiedName (name
, ns
);
174 BinarySecurityToken bst
= new BinarySecurityToken (xqn
);
179 public void LoadXml ()
181 XmlQualifiedName xqn
= new XmlQualifiedName (name
, ns
);
182 BinarySecurityToken bst
= new BinarySecurityToken (xqn
);
183 XmlDocument doc
= new XmlDocument ();
184 doc
.LoadXml ("<wsse:BinarySecurityToken xmlns:vt=\"http://www.go-mono.com/\" ValueType=\"vt:mono\" EncodingType=\"wsse:Base64Binary\" xmlns:wsu=\"http://schemas.xmlsoap.org/ws/2002/07/utility\" wsu:Id=\"staticIdUsedForNUnit\" xmlns:wsse=\"http://schemas.xmlsoap.org/ws/2002/07/secext\" />");
185 bst
.LoadXml (doc
.DocumentElement
);
186 AssertEquals ("EncodingType.Name", "Base64Binary", bst
.EncodingType
.Name
);
187 AssertEquals ("EncodingType.Namespace", "http://schemas.xmlsoap.org/ws/2002/07/secext", bst
.EncodingType
.Namespace
);
188 AssertEquals ("ValueType.Name", name
, bst
.ValueType
.Name
);
189 AssertEquals ("ValueType.Namespace", ns
, bst
.ValueType
.Namespace
);
190 AssertNull ("RawData", bst
.RawData
);
191 AssertEquals ("Id", "staticIdUsedForNUnit", bst
.Id
);
195 public void RawData ()
197 XmlQualifiedName xqn
= new XmlQualifiedName (name
, ns
);
198 BinarySecurityToken bst
= new BinarySecurityToken (xqn
);
199 AssertNull ("RawData(empty)", bst
.RawData
);
200 byte[] raw
= new byte [1];
202 AssertNotNull ("RawData", bst
.RawData
);
203 AssertEquals ("RawData[0]=0", 0x00, bst
.RawData
[0]);
205 // same buffer or copy ?
206 AssertEquals ("RawData[0]=1", 0x01, bst
.RawData
[0]);
208 AssertNull ("RawData(null)", bst
.RawData
);