2003-09-16 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
17 namespace MonoTests.MS.Web.Services.Security {
18
19         // non-abstract BinarySecurityToken for test uses only
20         public class BinarySecurityToken : Microsoft.Web.Services.Security.BinarySecurityToken {
21
22                 public BinarySecurityToken (XmlElement element) : base (element) {}
23
24                 public BinarySecurityToken (XmlQualifiedName valueType) : base (valueType) {}
25
26                 public override AuthenticationKey AuthenticationKey {
27                         get { return null; }
28                 }
29
30                 public override DecryptionKey DecryptionKey {
31                         get { return null; }
32                 }
33
34                 public override EncryptionKey EncryptionKey {
35                         get { return null; }
36                 }
37
38                 public override SignatureKey SignatureKey {
39                         get { return null; }
40                 }
41
42                 public override bool SupportsDataEncryption {
43                         get { return false; }
44                 }
45
46                 public override bool SupportsDigitalSignature {
47                         get { return false; }
48                 }
49
50                 public override void Verify() {}
51         }
52
53         [TestFixture]
54         public class BinarySecurityTokenTest : Assertion {
55
56                 private static string name = "mono";
57                 private static string ns = "http://www.go-mono.com/";
58
59                 [Test]
60                 [ExpectedException (typeof (ArgumentNullException))]
61                 public void ConstructorNullXmlElement () 
62                 {
63                         // we do not want to confuse the compiler about null ;-)
64                         XmlElement xel = null;
65                         BinarySecurityToken bst = new BinarySecurityToken (xel);
66                 }
67
68                 [Test]
69                 public void ConstructorXmlElement () 
70                 {
71                         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\" />";
72                         XmlDocument doc = new XmlDocument ();
73                         doc.LoadXml (xml);
74                         BinarySecurityToken bst = new BinarySecurityToken (doc.DocumentElement);
75                         AssertNotNull ("BinarySecurityToken(XmlQualifiedName)", bst);
76                         AssertEquals ("EncodingType.Name", "Base64Binary", bst.EncodingType.Name);
77                         AssertEquals ("EncodingType.Namespace", "http://schemas.xmlsoap.org/ws/2002/07/secext", bst.EncodingType.Namespace);
78                         AssertEquals ("ValueType.Name", name, bst.ValueType.Name);
79                         AssertEquals ("ValueType.Namespace", ns, bst.ValueType.Namespace);
80                         AssertNull ("RawData", bst.RawData);
81                         Assert ("Id", bst.Id.StartsWith ("SecurityToken-"));
82                 }
83
84                 [Test]
85                 [ExpectedException (typeof (ArgumentNullException))]
86                 public void ConstructorNullXmlQualifiedName () 
87                 {
88                         // we do not want to confuse the compiler about null ;-)
89                         XmlQualifiedName xqn = null;
90                         BinarySecurityToken bst = new BinarySecurityToken (xqn);
91                 }
92
93                 [Test]
94                 public void ConstructorXmlQualifiedName () 
95                 {
96                         XmlQualifiedName xqn = new XmlQualifiedName (name, ns);
97                         BinarySecurityToken bst = new BinarySecurityToken (xqn);
98                         AssertNotNull ("BinarySecurityToken(XmlQualifiedName)", bst);
99                         AssertEquals ("EncodingType.Name", "Base64Binary", bst.EncodingType.Name);
100                         AssertEquals ("EncodingType.Namespace", "http://schemas.xmlsoap.org/ws/2002/07/secext", bst.EncodingType.Namespace);
101                         AssertEquals ("ValueType.Name", name, bst.ValueType.Name);
102                         AssertEquals ("ValueType.Namespace", ns, bst.ValueType.Namespace);
103                         AssertNull ("RawData", bst.RawData);
104                         Assert ("Id", bst.Id.StartsWith ("SecurityToken-"));
105                 }
106
107                 [Test]
108                 [ExpectedException (typeof (ArgumentNullException))]
109                 public void NullEncodingType () 
110                 {
111                         XmlQualifiedName xqn = new XmlQualifiedName (name, ns);
112                         BinarySecurityToken bst = new BinarySecurityToken (xqn);
113                         bst.EncodingType = null;
114                 }
115
116                 [Test]
117                 [ExpectedException (typeof (ArgumentNullException))]
118                 public void NullValueType () 
119                 {
120                         XmlQualifiedName xqn = new XmlQualifiedName (name, ns);
121                         BinarySecurityToken bst = new BinarySecurityToken (xqn);
122                         bst.ValueType = null;
123                 }
124
125                 [Test]
126                 [ExpectedException (typeof (ArgumentNullException))]
127                 public void GetXmlNull () 
128                 {
129                         XmlQualifiedName xqn = new XmlQualifiedName (name, ns);
130                         BinarySecurityToken bst = new BinarySecurityToken (xqn);
131                         XmlElement xel = bst.GetXml (null);
132                 }
133
134                 [Test]
135                 public void GetXml () 
136                 {
137                         XmlQualifiedName xqn = new XmlQualifiedName (name, ns);
138                         BinarySecurityToken bst = new BinarySecurityToken (xqn);
139                         bst.Id = "staticIdUsedForNUnit";
140                         XmlDocument doc = new XmlDocument ();
141                         AssertEquals ("GetXml(doc)", "<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\" />", bst.GetXml (doc).OuterXml);
142                 }
143
144                 [Test]
145                 [ExpectedException (typeof (ArgumentNullException))]
146                 public void LoadXmlNull () 
147                 {
148                         XmlQualifiedName xqn = new XmlQualifiedName (name, ns);
149                         BinarySecurityToken bst = new BinarySecurityToken (xqn);
150                         bst.LoadXml (null);
151                 }
152
153                 [Test]
154                 [ExpectedException (typeof (ArgumentNullException))]
155                 public void LoadXml () 
156                 {
157                         XmlQualifiedName xqn = new XmlQualifiedName (name, ns);
158                         BinarySecurityToken bst = new BinarySecurityToken (xqn);
159                         XmlDocument doc = new XmlDocument ();
160                         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\" />");
161                         bst.LoadXml (doc.DocumentElement);
162                         AssertEquals ("EncodingType.Name", "Base64Binary", bst.EncodingType.Name);
163                         AssertEquals ("EncodingType.Namespace", "http://schemas.xmlsoap.org/ws/2002/07/secext", bst.EncodingType.Namespace);
164                         AssertEquals ("ValueType.Name", name, bst.ValueType.Name);
165                         AssertEquals ("ValueType.Namespace", ns, bst.ValueType.Namespace);
166                         AssertNull ("RawData", bst.RawData);
167                         AssertEquals ("Id", "staticIdUsedForNUnit", bst.Id);
168                 }
169         }
170 }