2 // EndpointBehaviorCollectionTest.cs
5 // Duncan Mak <duncan@ximian.com>
6 // Atsushi Enomoto <atsushi@ximian.com>
8 // Copyright (C) 2005-2006 Novell, Inc. http://www.novell.com
10 // Permission is hereby granted, free of charge, to any person obtaining
11 // a copy of this software and associated documentation files (the
12 // "Software"), to deal in the Software without restriction, including
13 // without limitation the rights to use, copy, modify, merge, publish,
14 // distribute, sublicense, and/or sell copies of the Software, and to
15 // permit persons to whom the Software is furnished to do so, subject to
16 // the following conditions:
18 // The above copyright notice and this permission notice shall be
19 // included in all copies or substantial portions of the Software.
21 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
22 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
23 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
24 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
25 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
26 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
27 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
30 using System.Collections.Generic;
32 using System.Security.Cryptography.X509Certificates;
33 using System.Security.Cryptography.Xml;
34 using System.ServiceModel;
35 using System.ServiceModel.Channels;
38 using System.Xml.Schema;
39 using System.Xml.Serialization;
40 using NUnit.Framework;
42 namespace MonoTests.System.ServiceModel
45 public class EndpointAddressTest
47 EndpointAddress address;
48 string namespace_uri = "http://schemas.xmlsoap.org/ws/2004/08/addressing";
51 public void AnonymousUri ()
53 Assert.AreEqual ("http://schemas.microsoft.com/2005/12/ServiceModel/Addressing/Anonymous", EndpointAddress.AnonymousUri.AbsoluteUri, "#1");
55 address = new EndpointAddress ("http://schemas.microsoft.com/2005/12/ServiceModel/Addressing/Anonymous");
56 Assert.IsTrue (address.IsAnonymous, "#2");
57 Assert.IsFalse (address.IsNone, "#3");
61 public void AnonymousUri2 ()
63 address = new EndpointAddress ("http://www.w3.org/2005/08/addressing/anonymous");
64 Assert.AreEqual ("http://www.w3.org/2005/08/addressing/anonymous", address.Uri.AbsoluteUri, "#1");
65 Assert.IsFalse (address.IsAnonymous, "#2");
66 Assert.IsFalse (address.IsNone, "#3");
70 public void NoneUri ()
72 Assert.AreEqual ("http://schemas.microsoft.com/2005/12/ServiceModel/Addressing/None", EndpointAddress.NoneUri.AbsoluteUri, "#1");
74 address = new EndpointAddress ("http://schemas.microsoft.com/2005/12/ServiceModel/Addressing/None");
75 Assert.IsFalse (address.IsAnonymous, "#2");
76 Assert.IsTrue (address.IsNone, "#3");
80 [ExpectedException (typeof (ArgumentNullException))]
81 public void ConstructorNullString ()
83 new EndpointAddress ((string) null);
87 [ExpectedException (typeof (ArgumentException))]
88 public void ConstructorRelativeUri ()
90 new EndpointAddress (new Uri (String.Empty, UriKind.RelativeOrAbsolute));
94 public void Headers ()
96 EndpointAddress e = new EndpointAddress ("urn:foo");
97 Assert.IsNotNull (e.Headers, "#1");
98 // This code results in NullReferenceException, which
100 //Assert.AreEqual (0, e.Headers.Count, "#2");
104 public void EqualsTest ()
106 address = new EndpointAddress ("urn:foo");
107 Assert.IsFalse (address == null, "#1"); // don't throw NullReferenceException
108 Assert.IsTrue ((EndpointAddress) null == null, "#2");
110 Assert.IsTrue (address == new EndpointAddress ("urn:foo"), "#3");
114 public void ReadFrom0 ()
116 string xml = @"<a:ReplyTo xmlns:a='http://www.w3.org/2005/08/addressing'><a:Address>http://www.w3.org/2005/08/addressing/anonymous</a:Address></a:ReplyTo>";
117 XmlReader src = XmlReader.Create (new StringReader (xml));
118 XmlDictionaryReader reader =
119 XmlDictionaryReader.CreateDictionaryReader (src);
120 EndpointAddress a = EndpointAddress.ReadFrom (reader);
121 Assert.IsNotNull (a, "#1");
122 Assert.AreEqual ("http://schemas.microsoft.com/2005/12/ServiceModel/Addressing/Anonymous", a.Uri.AbsoluteUri, "#2");
123 Assert.IsTrue (a.IsAnonymous, "#3");
127 public void ReadFrom1 ()
129 string xml = @"<a:ReplyTo xmlns:a='http://schemas.xmlsoap.org/ws/2004/08/addressing'><a:Address>http://www.w3.org/2005/08/addressing/anonymous</a:Address></a:ReplyTo>";
130 XmlReader src = XmlReader.Create (new StringReader (xml));
131 XmlDictionaryReader reader =
132 XmlDictionaryReader.CreateDictionaryReader (src);
133 EndpointAddress a = EndpointAddress.ReadFrom (reader);
134 Assert.IsNotNull (a, "#1");
135 Assert.AreEqual ("http://www.w3.org/2005/08/addressing/anonymous", a.Uri.AbsoluteUri, "#2");
136 Assert.IsFalse (a.IsAnonymous, "#3");
140 public void ReadFrom2 ()
142 string xml = @"<a:ReplyTo xmlns:a='http://www.w3.org/2005/08/addressing'><a:Address>http://www.w3.org/2005/08/addressing/anonymous</a:Address></a:ReplyTo>";
143 XmlReader src = XmlReader.Create (new StringReader (xml));
144 XmlDictionaryReader reader =
145 XmlDictionaryReader.CreateDictionaryReader (src);
146 EndpointAddress a = EndpointAddress.ReadFrom (AddressingVersion.WSAddressing10, reader);
148 Assert.IsNotNull (a, "#1");
149 Assert.AreEqual ("http://schemas.microsoft.com/2005/12/ServiceModel/Addressing/Anonymous", a.Uri.AbsoluteUri, "#2");
150 Assert.IsTrue (a.IsAnonymous, "#3");
154 [ExpectedException (typeof (XmlException))]
155 public void ReadFrom2Error ()
158 string xml = @"<a:ReplyTo xmlns:a='http://www.w3.org/2005/08/addressing'><a:Address>http://www.w3.org/2005/08/addressing/anonymous</a:Address></a:ReplyTo>";
159 XmlReader src = XmlReader.Create (new StringReader (xml));
160 XmlDictionaryReader reader =
161 XmlDictionaryReader.CreateDictionaryReader (src);
163 //Reading address with e10 address!
164 EndpointAddress.ReadFrom (AddressingVersion.WSAddressingAugust2004, reader);
168 public void ReadFrom3 ()
170 string xml = @"<a:ReplyTo xmlns:a='http://schemas.xmlsoap.org/ws/2004/08/addressing'><a:Address>http://www.w3.org/2005/08/addressing/anonymous</a:Address></a:ReplyTo>";
171 XmlReader src = XmlReader.Create (new StringReader (xml));
172 XmlDictionaryReader reader =
173 XmlDictionaryReader.CreateDictionaryReader (src);
174 EndpointAddress a = EndpointAddress.ReadFrom (AddressingVersion.WSAddressingAugust2004, reader);
176 Assert.AreEqual ("http://www.w3.org/2005/08/addressing/anonymous", a.Uri.AbsoluteUri, "#1");
177 Assert.IsFalse (a.IsAnonymous, "#2");
181 [ExpectedException (typeof (XmlException))]
182 public void ReadFrom3Error ()
184 string xml = @"<a:ReplyTo xmlns:a='http://schemas.xmlsoap.org/ws/2004/08/addressing'><a:Address>http://www.w3.org/2005/08/addressing/anonymous</a:Address></a:ReplyTo>";
185 XmlReader src = XmlReader.Create (new StringReader (xml));
186 XmlDictionaryReader reader =
187 XmlDictionaryReader.CreateDictionaryReader (src);
188 EndpointAddress.ReadFrom (AddressingVersion.WSAddressing10, reader);
192 [ExpectedException (typeof (XmlException))]
193 public void ReadFrom4Error ()
196 string xml = @"<a:ReplyTo xmlns:a='http://www.w3.org/2005/08/addressing'><a:Address>http://www.w3.org/2005/08/addressing/anonymous</a:Address></a:ReplyTo>";
197 XmlReader src = XmlReader.Create (new StringReader (xml));
198 XmlDictionaryReader reader =
199 XmlDictionaryReader.CreateDictionaryReader (src);
201 // null localName and ns
202 EndpointAddress.ReadFrom (AddressingVersion.WSAddressing10, reader, (string) null, null);
206 [ExpectedException (typeof (XmlException))]
207 public void ReadFrom4Error2 ()
210 string xml = @"<a:ReplyTo xmlns:a='http://www.w3.org/2005/08/addressing'><a:Address>http://www.w3.org/2005/08/addressing/anonymous</a:Address></a:ReplyTo>";
211 XmlReader src = XmlReader.Create (new StringReader (xml));
212 XmlDictionaryReader reader =
213 XmlDictionaryReader.CreateDictionaryReader (src);
215 // null localName and ns
216 EndpointAddress.ReadFrom (AddressingVersion.WSAddressing10, reader, (XmlDictionaryString) null, null);
220 public void ReadFromE10 ()
222 string xml = @"<a:ReplyTo xmlns:a='http://www.w3.org/2005/08/addressing'><a:Address>http://www.w3.org/2005/08/addressing/anonymous</a:Address></a:ReplyTo>";
223 XmlReader src = XmlReader.Create (new StringReader (xml));
224 XmlDictionaryReader reader =
225 XmlDictionaryReader.CreateDictionaryReader (src);
227 EndpointAddress10 e10 = EndpointAddress10.FromEndpointAddress (new EndpointAddress (("http://test")));
228 ((IXmlSerializable) e10).ReadXml (reader);
230 EndpointAddress a = e10.ToEndpointAddress ();
231 Assert.AreEqual ("http://schemas.microsoft.com/2005/12/ServiceModel/Addressing/Anonymous", a.Uri.AbsoluteUri, "#1");
232 Assert.IsTrue (a.IsAnonymous, "#2");
236 [ExpectedException (typeof (XmlException))]
237 public void ReadFromE10Error ()
239 //Address is from August2004 namespace, but reading it with EndpointAddress10
240 string xml = @"<a:ReplyTo xmlns:a='http://schemas.xmlsoap.org/ws/2004/08/addressing'><a:Address>http://www.w3.org/2005/08/addressing/anonymous</a:Address></a:ReplyTo>";
241 XmlReader src = XmlReader.Create (new StringReader (xml));
242 XmlDictionaryReader reader =
243 XmlDictionaryReader.CreateDictionaryReader (src);
245 EndpointAddress10 e10 = EndpointAddress10.FromEndpointAddress (new EndpointAddress (("http://test")));
246 ((IXmlSerializable) e10).ReadXml (reader);
251 [ExpectedException (typeof (XmlException))]
252 public void ReadFromE10Error2 ()
254 //Missing <Address> element
255 string xml = @"<a:ReplyTo xmlns:a='http://www.w3.org/2005/08/addressing'>http://www.w3.org/2005/08/addressing/anonymous</a:ReplyTo>";
256 XmlReader src = XmlReader.Create (new StringReader (xml));
257 XmlDictionaryReader reader =
258 XmlDictionaryReader.CreateDictionaryReader (src);
260 EndpointAddress10 e10 = EndpointAddress10.FromEndpointAddress (new EndpointAddress (("http://test")));
261 ((IXmlSerializable) e10).ReadXml (reader);
266 public void ReadFromAugust2004 ()
268 string xml = @"<a:ReplyTo xmlns:a='http://schemas.xmlsoap.org/ws/2004/08/addressing'><a:Address>http://www.w3.org/2005/08/addressing/anonymous</a:Address></a:ReplyTo>";
270 XmlReader src = XmlReader.Create (new StringReader (xml));
271 XmlDictionaryReader reader =
272 XmlDictionaryReader.CreateDictionaryReader (src);
274 EndpointAddressAugust2004 e2k4 = EndpointAddressAugust2004.FromEndpointAddress (new EndpointAddress ("http://test"));
276 ((IXmlSerializable) e2k4).ReadXml (reader);
277 Console.WriteLine (e2k4.ToEndpointAddress ().Uri.AbsoluteUri);
279 EndpointAddress a = e2k4.ToEndpointAddress ();
280 Assert.AreEqual ("http://www.w3.org/2005/08/addressing/anonymous", a.Uri.AbsoluteUri, "#1");
281 Assert.IsFalse (a.IsAnonymous, "#2");
285 [ExpectedException (typeof (XmlException))]
286 public void ReadFromAugust2004Error ()
288 //Reading address from EndpointAddress10 namespace with EndpointAddressAugust2004
289 string xml = @"<a:ReplyTo xmlns:a='http://www.w3.org/2005/08/addressing'><a:Address>http://www.w3.org/2005/08/addressing/anonymous</a:Address></a:ReplyTo>";
291 XmlReader src = XmlReader.Create (new StringReader (xml));
292 XmlDictionaryReader reader =
293 XmlDictionaryReader.CreateDictionaryReader (src);
295 EndpointAddressAugust2004 e2k4 = EndpointAddressAugust2004.FromEndpointAddress (new EndpointAddress ("http://test"));
297 ((IXmlSerializable) e2k4).ReadXml (reader);
301 [ExpectedException (typeof (XmlException))]
302 public void ReadFromAugust2004Error2 ()
304 //Missing <Address> element
305 string xml = @"<a:ReplyTo xmlns:a='http://schemas.xmlsoap.org/ws/2004/08/addressing'>http://www.w3.org/2005/08/addressing/anonymous</a:ReplyTo>";
307 XmlReader src = XmlReader.Create (new StringReader (xml));
308 XmlDictionaryReader reader =
309 XmlDictionaryReader.CreateDictionaryReader (src);
311 EndpointAddressAugust2004 e2k4 = EndpointAddressAugust2004.FromEndpointAddress (new EndpointAddress ("http://test"));
313 ((IXmlSerializable) e2k4).ReadXml (reader);
317 [ExpectedException (typeof (ArgumentException))]
318 public void ReadFromWrongXml ()
320 string xml = @"<a:Address xmlns:a='http://www.w3.org/2005/08/addressing'>http://www.w3.org/2005/08/addressing/anonymous</a:Address>";
321 XmlReader src = XmlReader.Create (new StringReader (xml));
322 XmlDictionaryReader reader =
323 XmlDictionaryReader.CreateDictionaryReader (src);
324 EndpointAddress.ReadFrom (reader);
328 [ExpectedException (typeof (ArgumentException))]
329 public void ReadFromWrongXml2 ()
331 string xml = @"<a:ReplyTo xmlns:a='http://www.w3.org/2005/08/addressing'>http://www.w3.org/2005/08/addressing/anonymous</a:ReplyTo>";
332 XmlReader src = XmlReader.Create (new StringReader (xml));
333 XmlDictionaryReader reader =
334 XmlDictionaryReader.CreateDictionaryReader (src);
335 EndpointAddress.ReadFrom (reader);
339 public void WriteToAddressingNone ()
341 EndpointAddress a = new EndpointAddress ("http://localhost:8080");
342 StringWriter sw = new StringWriter ();
343 XmlWriterSettings xws = new XmlWriterSettings ();
344 xws.OmitXmlDeclaration = true;
346 using (XmlDictionaryWriter xw = XmlDictionaryWriter.CreateDictionaryWriter (XmlWriter.Create (sw, xws))) {
347 a.WriteTo (AddressingVersion.None, xw, "From", "http://www.w3.org/2005/08/addressing");
349 Assert.AreEqual ("<From xmlns=\"http://www.w3.org/2005/08/addressing\">http://localhost:8080/</From>", sw.ToString (), "#1");
352 sw = new StringWriter ();
353 using (XmlDictionaryWriter xw = XmlDictionaryWriter.CreateDictionaryWriter (XmlWriter.Create (sw, xws))) {
354 a.WriteTo (AddressingVersion.None, xw);
356 Assert.AreEqual ("<EndpointReference xmlns=\"http://schemas.microsoft.com/ws/2005/05/addressing/none\">http://localhost:8080/</EndpointReference>", sw.ToString (), "#2");
359 string identity1 = "<Identity xmlns=\"http://schemas.xmlsoap.org/ws/2006/02/addressingidentity\"><KeyInfo xmlns=\"http://www.w3.org/2000/09/xmldsig#\"><X509Data><X509Certificate>MIIBxTCCAS6gAwIBAgIQEOvBwzgWq0aTzEi0qgWLBTANBgkqhkiG9w0BAQUFADAgMR4wHAYDVQQDExVNb25vIFRlc3QgUm9vdCBBZ2VuY3kwHhcNMDYwNzMxMDYxMDI4WhcNMzkxMjMxMDk1OTU5WjAkMSIwIAYDVQQDExlQb3Vwb3Uncy1Tb2Z0d2FyZS1GYWN0b3J5MIGdMA0GCSqGSIb3DQEBAQUAA4GLADCBhwKBgQCJI5sOaaEOCuXg2Itq+IVym/g13KwFYlyqJXPcBBs91qO1dpBDxUl19GSLFqHXfbIo4UYJZmEdLcS48yHx1w3AbC1raeH6bYhXqS3Mjj6ZnsJI0CyaUNjQkj4fwC3W8q80CuULmORUa6WJiugl5JT80s8s1iCymLtO1cbL1F+6DwIBETANBgkqhkiG9w0BAQUFAAOBgQA5IxtEKCgG0o6YxVDKRRTfiQY4QiCkHxqgfP2E+Cm6guuHykAFvWFqMUtGZq3yco8u83ZgXtjPphhPuzl8fdJsLTXieERsAbfZwcbp6cssTwsSl4JJviHSN17G3qbo0LH9u1QJHesBaH52Hz2iZaBfClxgUQGeWvO0SW+hZo75hg==</X509Certificate></X509Data></KeyInfo></Identity>";
361 string C14N (string xml)
363 XmlDsigExcC14NTransform t = new XmlDsigExcC14NTransform ();
364 XmlDocument doc = new XmlDocument ();
367 return new StreamReader (t.GetOutput () as Stream).ReadToEnd ();
371 public void WriteToWSA10 ()
373 X509Certificate2 cert = new X509Certificate2 ("Test/Resources/test.cer");
374 EndpointAddress a = new EndpointAddress (
375 new Uri ("http://localhost:8080"),
376 new X509CertificateEndpointIdentity (cert));
377 StringWriter sw = new StringWriter ();
378 XmlWriterSettings xws = new XmlWriterSettings ();
379 xws.OmitXmlDeclaration = true;
380 using (XmlDictionaryWriter xw = XmlDictionaryWriter.CreateDictionaryWriter (XmlWriter.Create (sw, xws))) {
381 a.WriteTo (AddressingVersion.WSAddressing10, xw);
383 Assert.AreEqual (C14N ("<EndpointReference xmlns=\"http://www.w3.org/2005/08/addressing\"><Address>http://localhost:8080/</Address>" + identity1 + "</EndpointReference>"), C14N (sw.ToString ()), "#2");
387 public void WriteContentsToWSA10 ()
389 X509Certificate2 cert = new X509Certificate2 ("Test/Resources/test.cer");
390 EndpointAddress a = new EndpointAddress (
391 new Uri ("http://localhost:8080"),
392 new X509CertificateEndpointIdentity (cert));
393 StringWriter sw = new StringWriter ();
394 XmlWriterSettings xws = new XmlWriterSettings ();
395 xws.OmitXmlDeclaration = true;
396 using (XmlDictionaryWriter xw = XmlDictionaryWriter.CreateDictionaryWriter (XmlWriter.Create (sw, xws))) {
397 xw.WriteStartElement ("root");
398 a.WriteContentsTo (AddressingVersion.WSAddressing10, xw);
399 xw.WriteEndElement ();
401 Assert.AreEqual (C14N ("<root><Address xmlns=\"http://www.w3.org/2005/08/addressing\">http://localhost:8080/</Address>" + identity1 + "</root>"), C14N (sw.ToString ()), "#2");
404 /* GetSchema() does not exist anymore
406 public void GetSchemaTest ()
408 address = new EndpointAddress ("http://tempuri.org/foo");
409 Assert.IsNull (address.GetSchema ());
413 [Category ("NotWorking")]
414 public void GetSchemaTestWithEmptySet ()
416 XmlSchemaSet set = new XmlSchemaSet ();
417 Assert.AreEqual (0, set.Count, "#1");
418 Assert.IsFalse (set.Contains (namespace_uri), "#2");
420 XmlQualifiedName n = EndpointAddress.GetSchema (set);
422 // A complete copy of the schema for EndpointReference is added.
423 Assert.AreEqual (1, set.Count, "#3");
424 Assert.IsTrue (set.Contains (namespace_uri), "#4");
427 foreach (XmlSchema schema in set.Schemas ()) {
428 Assert.AreEqual (schema.TargetNamespace, n.Namespace, "#" + count++);
429 Assert.IsTrue (schema.SchemaTypes.Contains (n), "#" + count++);
430 // This prints out the entire Schema!
431 // schema.Write (Console.Out);