2003-11-06 Sebastien Pouliot <spouliot@videotron.ca>
[mono.git] / mcs / class / Microsoft.Web.Services / Test / Microsoft.Web.Services.Security / BinarySecurityTokenTest.cs
1 //
2 // BinarySecurityTokenTest.cs 
3 //      - NUnit Test Cases for BinarySecurityToken
4 //
5 // Author:
6 //      Sebastien Pouliot (spouliot@motus.com)
7 //
8 // (C) 2002, 2003 Motus Technologies Inc. (http://www.motus.com)
9 //
10
11 using NUnit.Framework;
12 using Microsoft.Web.Services.Security;
13 using System;
14 using System.Security.Cryptography;
15 using System.Xml;
16 using MWSS = Microsoft.Web.Services.Security;
17
18 namespace MonoTests.MS.Web.Services.Security {
19
20         // non-abstract BinarySecurityToken for test uses only
21         public class BinarySecurityToken : MWSS.BinarySecurityToken {
22
23 #if WSE1
24                 public BinarySecurityToken (XmlElement element) : base (element) {}
25 #endif
26                 public BinarySecurityToken (XmlQualifiedName valueType) : base (valueType) {}
27
28                 public override AuthenticationKey AuthenticationKey {
29                         get { return null; }
30                 }
31
32                 public override DecryptionKey DecryptionKey {
33                         get { return null; }
34                 }
35
36                 public override EncryptionKey EncryptionKey {
37                         get { return null; }
38                 }
39
40                 public override SignatureKey SignatureKey {
41                         get { return null; }
42                 }
43
44                 public override bool SupportsDataEncryption {
45                         get { return false; }
46                 }
47
48                 public override bool SupportsDigitalSignature {
49                         get { return false; }
50                 }
51 #if WSE1
52                 public override void Verify() {}
53 #else
54                 public override bool Equals (SecurityToken token) 
55                 {
56                         return false;
57                 }
58
59                 public override int GetHashCode () 
60                 {
61                         return 0;
62                 }
63
64                 public override bool IsCurrent {
65                         get { return false; }
66                 }
67 #endif
68         }
69
70         [TestFixture]
71         public class BinarySecurityTokenTest : Assertion {
72
73                 private static string name = "mono";
74                 private static string ns = "http://www.go-mono.com/";
75 #if WSE1
76                 [Test]
77                 [ExpectedException (typeof (ArgumentNullException))]
78                 public void ConstructorNullXmlElement () 
79                 {
80                         // we do not want to confuse the compiler about null ;-)
81                         XmlElement xel = null;
82                         BinarySecurityToken bst = new BinarySecurityToken (xel);
83                 }
84
85                 [Test]
86                 public void ConstructorXmlElement () 
87                 {
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 ();
90                         doc.LoadXml (xml);
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-"));
99                 }
100 #endif
101                 [Test]
102                 [ExpectedException (typeof (ArgumentNullException))]
103                 public void ConstructorNullXmlQualifiedName () 
104                 {
105                         // we do not want to confuse the compiler about null ;-)
106                         XmlQualifiedName xqn = null;
107                         BinarySecurityToken bst = new BinarySecurityToken (xqn);
108                 }
109
110                 [Test]
111                 public void ConstructorXmlQualifiedName () 
112                 {
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-"));
122                 }
123
124                 [Test]
125                 [ExpectedException (typeof (ArgumentNullException))]
126                 public void NullEncodingType () 
127                 {
128                         XmlQualifiedName xqn = new XmlQualifiedName (name, ns);
129                         BinarySecurityToken bst = new BinarySecurityToken (xqn);
130                         bst.EncodingType = null;
131                 }
132
133                 [Test]
134                 [ExpectedException (typeof (ArgumentNullException))]
135                 public void NullValueType () 
136                 {
137                         XmlQualifiedName xqn = new XmlQualifiedName (name, ns);
138                         BinarySecurityToken bst = new BinarySecurityToken (xqn);
139                         bst.ValueType = null;
140                 }
141
142                 [Test]
143                 [ExpectedException (typeof (ArgumentNullException))]
144                 public void GetXmlNull () 
145                 {
146                         XmlQualifiedName xqn = new XmlQualifiedName (name, ns);
147                         BinarySecurityToken bst = new BinarySecurityToken (xqn);
148                         XmlElement xel = bst.GetXml (null);
149                 }
150
151                 [Test]
152                 public void GetXml () 
153                 {
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);
167                 }
168
169                 [Test]
170                 [ExpectedException (typeof (ArgumentNullException))]
171                 public void LoadXmlNull () 
172                 {
173                         XmlQualifiedName xqn = new XmlQualifiedName (name, ns);
174                         BinarySecurityToken bst = new BinarySecurityToken (xqn);
175                         bst.LoadXml (null);
176                 }
177
178                 [Test]
179                 public void LoadXml () 
180                 {
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);
192                 }
193
194                 [Test]
195                 public void RawData () 
196                 {
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];
201                         bst.RawData = raw;
202                         AssertNotNull ("RawData", bst.RawData);
203                         AssertEquals ("RawData[0]=0", 0x00, bst.RawData [0]);
204                         raw [0] = 1;
205                         // same buffer or copy ?
206                         AssertEquals ("RawData[0]=1", 0x01, bst.RawData [0]);
207                         bst.RawData = null;
208                         AssertNull ("RawData(null)", bst.RawData);
209                 }
210         }
211 }