Merge pull request #303 from ermshiperete/5278
[mono.git] / mcs / class / System.ServiceModel / Test / System.ServiceModel / EndpointAddressTest.cs
1 //
2 // EndpointBehaviorCollectionTest.cs
3 //
4 // Authors:
5 //      Duncan Mak <duncan@ximian.com>
6 //      Atsushi Enomoto <atsushi@ximian.com>
7 //
8 // Copyright (C) 2005-2006 Novell, Inc.  http://www.novell.com
9 //
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:
17 // 
18 // The above copyright notice and this permission notice shall be
19 // included in all copies or substantial portions of the Software.
20 // 
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.
28 //
29 using System;
30 using System.Collections.Generic;
31 using System.IO;
32 using System.Security.Cryptography.X509Certificates;
33 using System.Security.Cryptography.Xml;
34 using System.ServiceModel;
35 using System.ServiceModel.Channels;
36 using System.Text;
37 using System.Xml;
38 using System.Xml.Schema;
39 using System.Xml.Serialization;
40 using NUnit.Framework;
41
42 namespace MonoTests.System.ServiceModel
43 {
44         [TestFixture]
45         public class EndpointAddressTest
46         {
47                 EndpointAddress address;
48                 string namespace_uri = "http://schemas.xmlsoap.org/ws/2004/08/addressing";
49
50                 [Test]
51                 public void AnonymousUri ()
52                 {
53                         Assert.AreEqual ("http://schemas.microsoft.com/2005/12/ServiceModel/Addressing/Anonymous", EndpointAddress.AnonymousUri.AbsoluteUri, "#1");
54
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");
58                 }
59
60                 [Test]
61                 public void AnonymousUri2 ()
62                 {
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");
67                 }
68
69                 [Test]
70                 public void NoneUri ()
71                 {
72                         Assert.AreEqual ("http://schemas.microsoft.com/2005/12/ServiceModel/Addressing/None", EndpointAddress.NoneUri.AbsoluteUri, "#1");
73
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");
77                 }
78
79                 [Test]
80                 [ExpectedException (typeof (ArgumentNullException))]
81                 public void ConstructorNullString ()
82                 {
83                         new EndpointAddress ((string) null);
84                 }
85
86                 [Test]
87                 [ExpectedException (typeof (ArgumentException))]
88                 public void ConstructorRelativeUri ()
89                 {
90                         new EndpointAddress (new Uri (String.Empty, UriKind.RelativeOrAbsolute));
91                 }
92
93                 [Test]
94                 public void Headers ()
95                 {
96                         EndpointAddress e = new EndpointAddress ("urn:foo");
97                         Assert.IsNotNull (e.Headers, "#1");
98                         // This code results in NullReferenceException, which
99                         // is nasty.
100                         //Assert.AreEqual (0, e.Headers.Count, "#2");
101                 }
102
103                 [Test]
104                 public void EqualsTest ()
105                 {
106                         address = new EndpointAddress ("urn:foo");
107                         Assert.IsFalse (address == null, "#1"); // don't throw NullReferenceException
108                         Assert.IsTrue ((EndpointAddress) null == null, "#2");
109
110                         Assert.IsTrue (address == new EndpointAddress ("urn:foo"), "#3");
111                 }
112
113                 [Test]
114                 public void ReadFrom0 ()
115                 {
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");
124                 }
125
126                 [Test]
127                 public void ReadFrom1 ()
128                 {
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");
137                 }
138
139                 [Test]
140                 public void ReadFrom2 ()
141                 {
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);
147
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");
151                 }
152
153                 [Test]
154                 [ExpectedException (typeof (XmlException))]
155                 public void ReadFrom2Error ()
156                 {
157                         
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);
162
163                         //Reading address with e10 address!
164                         EndpointAddress.ReadFrom (AddressingVersion.WSAddressingAugust2004, reader);
165                 }
166
167                 [Test]
168                 public void ReadFrom3 ()
169                 {
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);
175                         
176                         Assert.AreEqual ("http://www.w3.org/2005/08/addressing/anonymous", a.Uri.AbsoluteUri, "#1");
177                         Assert.IsFalse (a.IsAnonymous, "#2");
178                 }
179
180                 [Test]
181                 [ExpectedException (typeof (XmlException))]
182                 public void ReadFrom3Error ()
183                 {
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);
189                 }
190
191                 [Test]
192                 [ExpectedException (typeof (XmlException))]
193                 public void ReadFrom4Error ()
194                 {
195                         
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);
200
201                         // null localName and ns
202                         EndpointAddress.ReadFrom (AddressingVersion.WSAddressing10, reader, (string) null, null);
203                 }
204
205                 [Test]
206                 [ExpectedException (typeof (XmlException))]
207                 public void ReadFrom4Error2 ()
208                 {
209                         
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);
214
215                         // null localName and ns
216                         EndpointAddress.ReadFrom (AddressingVersion.WSAddressing10, reader, (XmlDictionaryString) null, null);
217                 }
218
219                 [Test]
220                 public void ReadFromE10 ()
221                 {
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);
226                         
227                         EndpointAddress10 e10 = EndpointAddress10.FromEndpointAddress (new EndpointAddress (("http://test")));
228                         ((IXmlSerializable) e10).ReadXml (reader);
229
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");
233                 }
234
235                 [Test]
236                 [ExpectedException (typeof (XmlException))]
237                 public void ReadFromE10Error ()
238                 {
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);
244
245                         EndpointAddress10 e10 = EndpointAddress10.FromEndpointAddress (new EndpointAddress (("http://test")));
246                         ((IXmlSerializable) e10).ReadXml (reader);
247                         
248                 }
249
250                 [Test]
251                 [ExpectedException (typeof (XmlException))]
252                 public void ReadFromE10Error2 ()
253                 {
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);
259
260                         EndpointAddress10 e10 = EndpointAddress10.FromEndpointAddress (new EndpointAddress (("http://test")));
261                         ((IXmlSerializable) e10).ReadXml (reader);
262
263                 }
264
265                 [Test]
266                 public void ReadFromAugust2004 ()
267                 {
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>";
269                         
270                         XmlReader src = XmlReader.Create (new StringReader (xml));
271                         XmlDictionaryReader reader =
272                                 XmlDictionaryReader.CreateDictionaryReader (src);
273
274                         EndpointAddressAugust2004 e2k4 = EndpointAddressAugust2004.FromEndpointAddress (new EndpointAddress ("http://test"));
275                         
276                         ((IXmlSerializable) e2k4).ReadXml (reader);
277                         Console.WriteLine (e2k4.ToEndpointAddress ().Uri.AbsoluteUri);
278
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");
282                 }
283
284                 [Test]
285                 [ExpectedException (typeof (XmlException))]
286                 public void ReadFromAugust2004Error ()
287                 {
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>";
290
291                         XmlReader src = XmlReader.Create (new StringReader (xml));
292                         XmlDictionaryReader reader =
293                                 XmlDictionaryReader.CreateDictionaryReader (src);
294
295                         EndpointAddressAugust2004 e2k4 = EndpointAddressAugust2004.FromEndpointAddress (new EndpointAddress ("http://test"));
296
297                         ((IXmlSerializable) e2k4).ReadXml (reader);
298                 }
299
300                 [Test]
301                 [ExpectedException (typeof (XmlException))]
302                 public void ReadFromAugust2004Error2 ()
303                 {
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>";
306
307                         XmlReader src = XmlReader.Create (new StringReader (xml));
308                         XmlDictionaryReader reader =
309                                 XmlDictionaryReader.CreateDictionaryReader (src);
310
311                         EndpointAddressAugust2004 e2k4 = EndpointAddressAugust2004.FromEndpointAddress (new EndpointAddress ("http://test"));
312
313                         ((IXmlSerializable) e2k4).ReadXml (reader);
314                 }
315
316                 [Test]
317                 [ExpectedException (typeof (ArgumentException))]
318                 public void ReadFromWrongXml ()
319                 {
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);
325                 }
326
327                 [Test]
328                 [ExpectedException (typeof (ArgumentException))]
329                 public void ReadFromWrongXml2 ()
330                 {
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);
336                 }
337
338                 [Test]
339                 public void WriteToAddressingNone ()
340                 {
341                         EndpointAddress a = new EndpointAddress ("http://localhost:8080");
342                         StringWriter sw = new StringWriter ();
343                         XmlWriterSettings xws = new XmlWriterSettings ();
344                         xws.OmitXmlDeclaration = true;
345                         // #1
346                         using (XmlDictionaryWriter xw = XmlDictionaryWriter.CreateDictionaryWriter (XmlWriter.Create (sw, xws))) {
347                                 a.WriteTo (AddressingVersion.None, xw, "From", "http://www.w3.org/2005/08/addressing");
348                         }
349                         Assert.AreEqual ("<From xmlns=\"http://www.w3.org/2005/08/addressing\">http://localhost:8080/</From>", sw.ToString (), "#1");
350
351                         // #2
352                         sw = new StringWriter ();
353                         using (XmlDictionaryWriter xw = XmlDictionaryWriter.CreateDictionaryWriter (XmlWriter.Create (sw, xws))) {
354                                 a.WriteTo (AddressingVersion.None, xw);
355                         }
356                         Assert.AreEqual ("<EndpointReference xmlns=\"http://schemas.microsoft.com/ws/2005/05/addressing/none\">http://localhost:8080/</EndpointReference>", sw.ToString (), "#2");
357                 }
358
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>";
360
361                 string C14N (string xml)
362                 {
363                         XmlDsigExcC14NTransform t = new XmlDsigExcC14NTransform ();
364                         XmlDocument doc = new XmlDocument ();
365                         doc.LoadXml (xml);
366                         t.LoadInput (doc);
367                         return new StreamReader (t.GetOutput () as Stream).ReadToEnd ();
368                 }
369
370                 [Test]
371                 public void WriteToWSA10 ()
372                 {
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);
382                         }
383                         Assert.AreEqual (C14N ("<EndpointReference xmlns=\"http://www.w3.org/2005/08/addressing\"><Address>http://localhost:8080/</Address>" + identity1 + "</EndpointReference>"), C14N (sw.ToString ()), "#2");
384                 }
385
386                 [Test]
387                 public void WriteContentsToWSA10 ()
388                 {
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 ();
400                         }
401                         Assert.AreEqual (C14N ("<root><Address xmlns=\"http://www.w3.org/2005/08/addressing\">http://localhost:8080/</Address>" + identity1 + "</root>"), C14N (sw.ToString ()), "#2");
402                 }
403
404 /* GetSchema() does not exist anymore
405                 [Test]
406                 public void GetSchemaTest ()
407                 {
408                         address = new EndpointAddress ("http://tempuri.org/foo");
409                         Assert.IsNull (address.GetSchema ());
410                 }
411
412                 [Test]
413                 [Category ("NotWorking")]
414                 public void GetSchemaTestWithEmptySet ()
415                 {
416                         XmlSchemaSet set = new XmlSchemaSet ();
417                         Assert.AreEqual (0, set.Count, "#1");
418                         Assert.IsFalse (set.Contains (namespace_uri), "#2");
419
420                         XmlQualifiedName n = EndpointAddress.GetSchema (set);
421
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");
425
426                         int count = 5;
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);
432                         }
433                 }
434 */
435         }
436 }
437