2 // HttpTransportBindingElementTest.cs
5 // Atsushi Enomoto <atsushi@ximian.com>
7 // Copyright (C) 2005 Novell, Inc. http://www.novell.com
9 // Permission is hereby granted, free of charge, to any person obtaining
10 // a copy of this software and associated documentation files (the
11 // "Software"), to deal in the Software without restriction, including
12 // without limitation the rights to use, copy, modify, merge, publish,
13 // distribute, sublicense, and/or sell copies of the Software, and to
14 // permit persons to whom the Software is furnished to do so, subject to
15 // the following conditions:
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29 using System.Collections.ObjectModel;
32 using System.Net.Security;
33 using System.Reflection;
34 using System.ServiceModel;
35 using System.ServiceModel.Channels;
36 using System.ServiceModel.Description;
37 using System.Threading;
39 using NUnit.Framework;
42 using System.Security.Authentication.ExtendedProtection;
45 namespace MonoTests.System.ServiceModel.Channels
48 public class HttpTransportBindingElementTest
50 static BindingParameterCollection empty_params =
51 new BindingParameterCollection ();
54 public void DefaultValues ()
56 HttpTransportBindingElement be =
57 new HttpTransportBindingElement ();
58 Assert.AreEqual (false, be.AllowCookies, "#1");
59 Assert.AreEqual (AuthenticationSchemes.Anonymous,
60 be.AuthenticationScheme, "#2");
61 Assert.AreEqual (false, be.BypassProxyOnLocal, "#3");
62 Assert.AreEqual (default (HostNameComparisonMode),
63 be.HostNameComparisonMode, "#4");
64 Assert.AreEqual (0x10000, be.MaxBufferSize, "#6");
65 Assert.IsNull (be.ProxyAddress, "#7");
66 Assert.AreEqual (AuthenticationSchemes.Anonymous,
67 be.ProxyAuthenticationScheme, "#8");
68 Assert.AreEqual (String.Empty, be.Realm, "#9");
69 Assert.AreEqual ("http", be.Scheme, "#10");
70 Assert.AreEqual (default (TransferMode),
71 be.TransferMode, "#11");
72 Assert.AreEqual (false,
73 be.UnsafeConnectionNtlmAuthentication, "#12");
74 Assert.AreEqual (true, be.UseDefaultWebProxy, "#13");
78 public void CanBuildChannelFactory ()
80 HttpTransportBindingElement be =
81 new HttpTransportBindingElement ();
82 BindingContext ctx = new BindingContext (
83 new CustomBinding (), empty_params);
84 Assert.IsTrue (be.CanBuildChannelFactory<IRequestChannel> (ctx), "#1");
85 Assert.IsFalse (be.CanBuildChannelFactory<IInputChannel> (ctx), "#2");
86 Assert.IsFalse (be.CanBuildChannelFactory<IReplyChannel> (ctx), "#3");
87 Assert.IsFalse (be.CanBuildChannelFactory<IOutputChannel> (ctx), "#4");
88 // seems like it does not support session channels by itself ?
89 Assert.IsFalse (be.CanBuildChannelFactory<IRequestSessionChannel> (ctx), "#5");
90 Assert.IsFalse (be.CanBuildChannelFactory<IInputSessionChannel> (ctx), "#6");
91 Assert.IsFalse (be.CanBuildChannelFactory<IReplySessionChannel> (ctx), "#7");
92 Assert.IsFalse (be.CanBuildChannelFactory<IOutputSessionChannel> (ctx), "#8");
94 // IServiceChannel is not supported
95 Assert.IsFalse (be.CanBuildChannelListener<IServiceChannel> (ctx), "#9");
99 public void CanBuildChannelListener ()
101 HttpTransportBindingElement be =
102 new HttpTransportBindingElement ();
103 BindingContext ctx = new BindingContext (
104 new CustomBinding (), empty_params);
105 Assert.IsTrue (be.CanBuildChannelListener<IReplyChannel> (ctx), "#1");
106 Assert.IsFalse (be.CanBuildChannelListener<IOutputChannel> (ctx), "#2");
107 Assert.IsFalse (be.CanBuildChannelListener<IRequestChannel> (ctx), "#3");
108 Assert.IsFalse (be.CanBuildChannelListener<IInputChannel> (ctx), "#4");
109 // seems like it does not support session channels by itself ?
110 Assert.IsFalse (be.CanBuildChannelListener<IReplySessionChannel> (ctx), "#5");
111 Assert.IsFalse (be.CanBuildChannelListener<IOutputSessionChannel> (ctx), "#6");
112 Assert.IsFalse (be.CanBuildChannelListener<IRequestSessionChannel> (ctx), "#7");
113 Assert.IsFalse (be.CanBuildChannelListener<IInputSessionChannel> (ctx), "#8");
115 // IServiceChannel is not supported
116 Assert.IsFalse (be.CanBuildChannelListener<IServiceChannel> (ctx), "#9");
120 public void BuildChannelFactory ()
122 BindingContext ctx = new BindingContext (
124 new HttpTransportBindingElement ()),
126 // returns HttpChannelFactory
127 IChannelFactory<IRequestChannel> f =
128 ctx.BuildInnerChannelFactory<IRequestChannel> ();
129 f.Open (); // required
130 IChannel c = f.CreateChannel (new EndpointAddress (
131 "http://www.mono-project.com"));
135 [ExpectedException (typeof (InvalidOperationException))]
136 public void CreateChannelWithoutOpen ()
138 BindingContext ctx = new BindingContext (
140 new HttpTransportBindingElement ()),
142 // returns HttpChannelFactory
143 IChannelFactory<IRequestChannel> f =
144 ctx.BuildInnerChannelFactory<IRequestChannel> ();
145 IChannel c = f.CreateChannel (new EndpointAddress (
146 "http://www.mono-project.com"));
150 public void BuildChannelFactoryTwoHttp ()
152 BindingContext ctx = new BindingContext (
154 new HttpTransportBindingElement (),
155 new HttpTransportBindingElement ()),
157 ctx.BuildInnerChannelFactory<IRequestChannel> ();
161 public void BuildChannelFactoryHttpThenMessage ()
163 BindingContext ctx = new BindingContext (
165 new HttpTransportBindingElement (),
166 new BinaryMessageEncodingBindingElement ()),
168 IChannelFactory<IRequestChannel> cf =
169 ctx.BuildInnerChannelFactory<IRequestChannel> ();
174 // with July CTP it still works ...
175 public void BuildChannelFactoryHttpNoMessage ()
177 BindingContext ctx = new BindingContext (
179 new HttpTransportBindingElement ()),
181 IChannelFactory<IRequestChannel> cf =
182 ctx.BuildInnerChannelFactory<IRequestChannel> ();
187 public void BuildChannelFactoryIgnoresRemaining ()
189 BindingContext ctx = new BindingContext (
191 new HttpTransportBindingElement (),
192 new InvalidBindingElement ()),
194 ctx.BuildInnerChannelFactory<IRequestChannel> ();
197 // Disable this test anytime when HttpTransportBindingElement.BuildChannelFactory() doesn't return ChannelFactoryBase`1 anymore. It's not an API requirement.
199 [ExpectedException (typeof (ArgumentNullException))]
200 public void BuildChannelFactory_CreateChannelNullVia ()
202 var ctx = new BindingContext (new CustomBinding (), empty_params);
203 var cf = new HttpTransportBindingElement ().BuildChannelFactory<IRequestChannel> (ctx);
204 Assert.IsTrue (cf is ChannelFactoryBase<IRequestChannel>, "#1");
206 cf.CreateChannel (new EndpointAddress ("http://localhost:8080"), null);
210 [ExpectedException (typeof (ArgumentException))]
211 public void CreateChannelInvalidScheme ()
213 IChannelFactory<IRequestChannel> f = new BasicHttpBinding ().BuildChannelFactory<IRequestChannel> (new BindingParameterCollection ());
215 f.CreateChannel (new EndpointAddress ("stream:dummy"));
219 public void BuildChannelListenerWithoutListenUri ()
221 new BasicHttpBinding ().BuildChannelListener<IReplyChannel> (new BindingParameterCollection ());
224 // when AddressingVersion is None (in MessageVersion), then
225 // EndpointAddress.Uri and via URIs must match.
227 [ExpectedException (typeof (ArgumentException))]
228 public void EndpointAddressAndViaMustMatchOnAddressingNone ()
231 var ch = ChannelFactory<IFoo>.CreateChannel (new BasicHttpBinding (), new EndpointAddress ("http://localhost:37564/"), new Uri ("http://localhost:8080/HogeService"));
232 ((ICommunicationObject) ch).Close ();
233 } catch (TargetInvocationException) {
234 // we throw this exception so far. Since it is
235 // very internal difference (channel is created
236 // inside ClientRuntimeChannel.ctor() while .NET
237 // does it in ChannelFactory<T>.CreateChannel(),
238 // there is no point of treating it as failure).
239 throw new ArgumentException ();
244 public void GetPropertyMessageVersion ()
246 var be = new HttpTransportBindingElement ();
247 var mv = be.GetProperty<MessageVersion> (new BindingContext (new CustomBinding (), empty_params));
248 Assert.AreEqual (MessageVersion.Soap12WSAddressing10, mv, "#1");
252 public void GetPrpertyBindingDeliveryCapabilities ()
254 var be = new HttpTransportBindingElement ();
255 var dc = be.GetProperty<IBindingDeliveryCapabilities> (new BindingContext (new CustomBinding (), empty_params));
256 Assert.IsFalse (dc.AssuresOrderedDelivery, "#1");
257 Assert.IsFalse (dc.QueuedDelivery, "#2");
261 public void GetPrpertySecurityCapabilities ()
263 var be = new HttpTransportBindingElement ();
264 var sec = be.GetProperty<ISecurityCapabilities> (new BindingContext (new CustomBinding (), empty_params));
265 Assert.IsNotNull (sec, "#1.1");
266 Assert.AreEqual (ProtectionLevel.None, sec.SupportedRequestProtectionLevel, "#1.2");
267 Assert.AreEqual (ProtectionLevel.None, sec.SupportedResponseProtectionLevel, "#1.3");
268 Assert.IsFalse (sec.SupportsClientAuthentication, "#1.4");
269 Assert.IsFalse (sec.SupportsClientWindowsIdentity, "#1.5");
270 Assert.IsFalse (sec.SupportsServerAuthentication , "#1.6");
272 be = new HttpTransportBindingElement ();
273 be.AuthenticationScheme = AuthenticationSchemes.Negotiate;
274 sec = be.GetProperty<ISecurityCapabilities> (new BindingContext (new CustomBinding (), empty_params));
275 Assert.IsNotNull (sec, "#2.1");
276 Assert.AreEqual (ProtectionLevel.None, sec.SupportedRequestProtectionLevel, "#2.2");
277 Assert.AreEqual (ProtectionLevel.None, sec.SupportedResponseProtectionLevel, "#2.3");
278 Assert.IsTrue (sec.SupportsClientAuthentication, "#2.4");
279 Assert.IsTrue (sec.SupportsClientWindowsIdentity, "#2.5");
280 Assert.IsTrue (sec.SupportsServerAuthentication , "#2.6");
282 // almost the same, only differ at SupportsServerAuth
283 be = new HttpTransportBindingElement ();
284 be.AuthenticationScheme = AuthenticationSchemes.Ntlm;
285 sec = be.GetProperty<ISecurityCapabilities> (new BindingContext (new CustomBinding (), empty_params));
286 Assert.IsNotNull (sec, "#3.1");
287 Assert.AreEqual (ProtectionLevel.None, sec.SupportedRequestProtectionLevel, "#3.2");
288 Assert.AreEqual (ProtectionLevel.None, sec.SupportedResponseProtectionLevel, "#3.3");
289 Assert.IsTrue (sec.SupportsClientAuthentication, "#3.4");
290 Assert.IsTrue (sec.SupportsClientWindowsIdentity, "#3.5");
291 Assert.IsFalse (sec.SupportsServerAuthentication , "#3.6");
293 be = new HttpTransportBindingElement ();
294 be.AuthenticationScheme = AuthenticationSchemes.Basic;
295 sec = be.GetProperty<ISecurityCapabilities> (new BindingContext (new CustomBinding (), empty_params));
296 Assert.IsNotNull (sec, "#4.1");
297 Assert.AreEqual (ProtectionLevel.None, sec.SupportedRequestProtectionLevel, "#4.2");
298 Assert.AreEqual (ProtectionLevel.None, sec.SupportedResponseProtectionLevel, "#4.3");
299 Assert.IsTrue (sec.SupportsClientAuthentication, "#4.4");
300 Assert.IsTrue (sec.SupportsClientWindowsIdentity, "#4.5");
301 Assert.IsFalse (sec.SupportsServerAuthentication , "#4.6");
303 be = new HttpTransportBindingElement ();
304 be.AuthenticationScheme = AuthenticationSchemes.Digest;
305 sec = be.GetProperty<ISecurityCapabilities> (new BindingContext (new CustomBinding (), empty_params));
306 Assert.IsNotNull (sec, "#5.1");
307 Assert.AreEqual (ProtectionLevel.None, sec.SupportedRequestProtectionLevel, "#5.2");
308 Assert.AreEqual (ProtectionLevel.None, sec.SupportedResponseProtectionLevel, "#5.3");
309 Assert.IsTrue (sec.SupportsClientAuthentication, "#5.4");
310 Assert.IsTrue (sec.SupportsClientWindowsIdentity, "#5.5");
311 Assert.IsFalse (sec.SupportsServerAuthentication , "#5.6");
320 string DoWork (string s1, string s2);
325 #region connection test
330 [Ignore ("It somehow fails...")]
331 // It is almost identical to http-low-level-binding
332 public void LowLevelHttpConnection ()
334 HttpTransportBindingElement lel =
335 new HttpTransportBindingElement ();
338 BindingContext lbc = new BindingContext (
339 new CustomBinding (),
340 new BindingParameterCollection (),
341 new Uri ("http://localhost:37564"),
342 String.Empty, ListenUriMode.Explicit);
343 listener = lel.BuildChannelListener<IReplyChannel> (lbc);
351 Thread svc = new Thread (delegate () {
353 svcret = LowLevelHttpConnection_SetupService ();
354 } catch (Exception ex) {
355 svcret = ex.ToString ();
360 // Client code goes here.
362 HttpTransportBindingElement el =
363 new HttpTransportBindingElement ();
364 BindingContext ctx = new BindingContext (
365 new CustomBinding (),
366 new BindingParameterCollection ());
367 IChannelFactory<IRequestChannel> factory =
368 el.BuildChannelFactory<IRequestChannel> (ctx);
372 IRequestChannel request = factory.CreateChannel (
373 new EndpointAddress ("http://localhost:37564"));
379 Message reqmsg = Message.CreateMessage (
380 MessageVersion.Default, "Echo");
381 // sync version does not work here.
382 Message msg = request.Request (reqmsg, TimeSpan.FromSeconds (5));
384 using (XmlWriter w = XmlWriter.Create (TextWriter.Null)) {
385 msg.WriteMessage (w);
389 Assert.Fail (svcret.Length > 0 ? svcret : "service code did not finish until this test expected.");
391 if (request.State == CommunicationState.Opened)
395 if (factory.State == CommunicationState.Opened)
399 if (listener.State == CommunicationState.Opened)
404 IChannelListener<IReplyChannel> listener;
406 string LowLevelHttpConnection_SetupService ()
408 IReplyChannel reply = listener.AcceptChannel ();
410 if (!reply.WaitForRequest (TimeSpan.FromSeconds (10)))
411 return "No request reached here.";
413 svcret = "Receiving request ...";
414 RequestContext ctx = reply.ReceiveRequest ();
416 return "No request context returned.";
418 svcret = "Starting reply ...";
419 ctx.Reply (Message.CreateMessage (MessageVersion.Default, "Ack"));
428 public void ExportPolicyDefault ()
430 IPolicyExportExtension binding_element = new HttpTransportBindingElement ();
431 PolicyConversionContext conversion_context = new CustomPolicyConversionContext ();
432 binding_element.ExportPolicy (new WsdlExporter (), conversion_context);
434 PolicyAssertionCollection binding_assertions = conversion_context.GetBindingAssertions ();
435 BindingElementCollection binding_elements = conversion_context.BindingElements;
436 Assert.AreEqual (1, binding_assertions.Count, "#A0");
437 Assert.AreEqual (0, binding_elements.Count, "#A1");
439 // wsaw:UsingAddressing
440 XmlNode using_addressing_node = FindAssertion (binding_assertions, "wsaw:UsingAddressing");
441 Assert.AreEqual (true, using_addressing_node != null, "#B0");
442 Assert.AreEqual ("UsingAddressing", using_addressing_node.LocalName, "#B1");
443 Assert.AreEqual ("http://www.w3.org/2006/05/addressing/wsdl", using_addressing_node.NamespaceURI, "#B2");
444 Assert.AreEqual (0, using_addressing_node.Attributes.Count, "#B3");
445 Assert.AreEqual (0, using_addressing_node.ChildNodes.Count, "#B4");
446 Assert.AreEqual (String.Empty, using_addressing_node.InnerText, "#B5");
450 public void ExportPolicy ()
452 HttpTransportBindingElement http_binding_element = new HttpTransportBindingElement ();
455 // Specify some non-default values
457 http_binding_element.AllowCookies = !http_binding_element.AllowCookies;
458 http_binding_element.AuthenticationScheme = AuthenticationSchemes.Ntlm;
459 http_binding_element.BypassProxyOnLocal = !http_binding_element.BypassProxyOnLocal;
460 http_binding_element.HostNameComparisonMode = HostNameComparisonMode.WeakWildcard;
461 http_binding_element.KeepAliveEnabled = !http_binding_element.KeepAliveEnabled;
462 http_binding_element.ManualAddressing = !http_binding_element.ManualAddressing;
463 http_binding_element.MaxBufferPoolSize = http_binding_element.MaxBufferPoolSize / 2;
464 http_binding_element.MaxBufferSize = http_binding_element.MaxBufferSize / 2;
465 http_binding_element.MaxReceivedMessageSize = http_binding_element.MaxReceivedMessageSize / 2;
466 http_binding_element.ProxyAddress = new Uri ("http://proxyaddress.com");
467 http_binding_element.ProxyAuthenticationScheme = AuthenticationSchemes.Basic;
468 http_binding_element.Realm = "RandomRealm";
469 http_binding_element.TransferMode = TransferMode.Streamed;
470 http_binding_element.UnsafeConnectionNtlmAuthentication = !http_binding_element.UnsafeConnectionNtlmAuthentication;
471 http_binding_element.UseDefaultWebProxy = !http_binding_element.UseDefaultWebProxy;
473 http_binding_element.DecompressionEnabled = !http_binding_element.DecompressionEnabled;
474 http_binding_element.ExtendedProtectionPolicy = new ExtendedProtectionPolicy (PolicyEnforcement.WhenSupported);
478 // Actual call to ExportPolicy
480 IPolicyExportExtension binding_element = http_binding_element as IPolicyExportExtension;
481 PolicyConversionContext conversion_context = new CustomPolicyConversionContext ();
482 binding_element.ExportPolicy (new WsdlExporter (), conversion_context);
484 PolicyAssertionCollection binding_assertions = conversion_context.GetBindingAssertions ();
485 BindingElementCollection binding_elements = conversion_context.BindingElements;
486 Assert.AreEqual (2, binding_assertions.Count, "#A0");
487 Assert.AreEqual (0, binding_elements.Count, "#A1");
489 // AuthenticationScheme - the only property that causes information to be exported.
490 XmlNode authentication_node = FindAssertion (binding_assertions, "http:NtlmAuthentication");
491 Assert.AreEqual (true, authentication_node != null, "#B0");
492 Assert.AreEqual ("NtlmAuthentication", authentication_node.LocalName, "#B1");
493 Assert.AreEqual ("http://schemas.microsoft.com/ws/06/2004/policy/http", authentication_node.NamespaceURI, "#B2");
494 Assert.AreEqual (String.Empty, authentication_node.InnerText, "#B3");
495 Assert.AreEqual (0, authentication_node.Attributes.Count, "#B4");
498 // For some reason PolicyAssertionCollection.Find is not working as expected,
499 // so do the lookup manually.
500 XmlNode FindAssertion (PolicyAssertionCollection assertionCollection, string name)
502 foreach (XmlNode node in assertionCollection)
503 if (node.Name == name)