2 // TcpTransportBindingElementTest.cs
5 // Atsushi Enomoto <atsushi@ximian.com>
7 // Copyright (C) 2008 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.ServiceModel;
34 using System.ServiceModel.Channels;
35 using System.ServiceModel.Description;
36 using System.Threading;
38 using NUnit.Framework;
40 using MonoTests.Helpers;
42 namespace MonoTests.System.ServiceModel.Channels
45 public class TcpTransportBindingElementTest
47 static BindingParameterCollection empty_params =
48 new BindingParameterCollection ();
51 public void DefaultValues ()
53 TcpTransportBindingElement be =
54 new TcpTransportBindingElement ();
55 Assert.AreEqual (TimeSpan.FromSeconds (5), be.ChannelInitializationTimeout, "#1");
56 Assert.AreEqual (0x2000, be.ConnectionBufferSize, "#2");
57 Assert.AreEqual (HostNameComparisonMode.StrongWildcard, be.HostNameComparisonMode, "#3");
58 Assert.AreEqual (0x10000, be.MaxBufferSize, "#4");
59 Assert.AreEqual (TimeSpan.FromMilliseconds (200), be.MaxOutputDelay, "#5");
60 Assert.AreEqual (1, be.MaxPendingAccepts, "#6");
61 Assert.AreEqual (10, be.MaxPendingConnections, "#7");
62 Assert.AreEqual (TransferMode.Buffered, be.TransferMode, "#8");
64 Assert.AreEqual (10, be.ListenBacklog, "#9");
65 Assert.IsFalse (be.PortSharingEnabled, "#10");
66 Assert.AreEqual ("net.tcp", be.Scheme, "#11");
67 Assert.IsFalse (be.TeredoEnabled, "#12");
68 TcpConnectionPoolSettings pool = be.ConnectionPoolSettings;
69 Assert.IsNotNull (pool, "#13");
70 Assert.AreEqual ("default", pool.GroupName, "#14");
71 Assert.AreEqual (TimeSpan.FromSeconds (120), pool.IdleTimeout, "#15");
72 Assert.AreEqual (TimeSpan.FromSeconds (300), pool.LeaseTimeout, "#16");
73 Assert.AreEqual (10, pool.MaxOutboundConnectionsPerEndpoint, "#17");
77 public void CanBuildChannelFactory ()
79 TcpTransportBindingElement be =
80 new TcpTransportBindingElement ();
81 BindingContext ctx = new BindingContext (
82 new CustomBinding (), empty_params);
83 Assert.IsFalse (be.CanBuildChannelFactory<IRequestChannel> (ctx), "#1");
84 Assert.IsFalse (be.CanBuildChannelFactory<IInputChannel> (ctx), "#2");
85 Assert.IsFalse (be.CanBuildChannelFactory<IReplyChannel> (ctx), "#3");
86 Assert.IsFalse (be.CanBuildChannelFactory<IOutputChannel> (ctx), "#4");
88 Assert.IsFalse (be.CanBuildChannelFactory<IRequestSessionChannel> (ctx), "#5");
89 Assert.IsFalse (be.CanBuildChannelFactory<IInputSessionChannel> (ctx), "#6");
90 Assert.IsFalse (be.CanBuildChannelFactory<IReplySessionChannel> (ctx), "#7");
91 Assert.IsFalse (be.CanBuildChannelFactory<IOutputSessionChannel> (ctx), "#8");
93 // IServiceChannel is not supported
94 Assert.IsFalse (be.CanBuildChannelFactory<IServiceChannel> (ctx), "#9");
95 Assert.IsFalse (be.CanBuildChannelFactory<IClientChannel> (ctx), "#10");
97 Assert.IsFalse (be.CanBuildChannelFactory<IDuplexChannel> (ctx), "#11");
98 Assert.IsTrue (be.CanBuildChannelFactory<IDuplexSessionChannel> (ctx), "#12");
102 public void CanBuildChannelListener ()
104 TcpTransportBindingElement be =
105 new TcpTransportBindingElement ();
106 BindingContext ctx = new BindingContext (
107 new CustomBinding (), empty_params);
108 Assert.IsFalse (be.CanBuildChannelListener<IReplyChannel> (ctx), "#1");
109 Assert.IsFalse (be.CanBuildChannelListener<IOutputChannel> (ctx), "#2");
110 Assert.IsFalse (be.CanBuildChannelListener<IRequestChannel> (ctx), "#3");
111 Assert.IsFalse (be.CanBuildChannelListener<IInputChannel> (ctx), "#4");
113 Assert.IsFalse (be.CanBuildChannelListener<IReplySessionChannel> (ctx), "#5");
114 Assert.IsFalse (be.CanBuildChannelListener<IOutputSessionChannel> (ctx), "#6");
115 Assert.IsFalse (be.CanBuildChannelListener<IRequestSessionChannel> (ctx), "#7");
116 Assert.IsFalse (be.CanBuildChannelListener<IInputSessionChannel> (ctx), "#8");
118 // IServiceChannel is not supported
119 Assert.IsFalse (be.CanBuildChannelListener<IServiceChannel> (ctx), "#9");
120 Assert.IsFalse (be.CanBuildChannelListener<IClientChannel> (ctx), "#10");
122 Assert.IsFalse (be.CanBuildChannelListener<IDuplexChannel> (ctx), "#11");
123 Assert.IsTrue (be.CanBuildChannelListener<IDuplexSessionChannel> (ctx), "#12");
127 public void CanBuildChannelListener2 ()
129 TcpTransportBindingElement be =
130 new TcpTransportBindingElement ();
131 be.TransferMode = TransferMode.Streamed;
132 BindingContext ctx = new BindingContext (
133 new CustomBinding (), empty_params);
134 Assert.IsTrue (be.CanBuildChannelListener<IReplyChannel> (ctx), "#1");
135 Assert.IsFalse (be.CanBuildChannelListener<IOutputChannel> (ctx), "#2");
136 Assert.IsFalse (be.CanBuildChannelListener<IRequestChannel> (ctx), "#3");
137 Assert.IsFalse (be.CanBuildChannelListener<IInputChannel> (ctx), "#4");
139 Assert.IsFalse (be.CanBuildChannelListener<IReplySessionChannel> (ctx), "#5");
140 Assert.IsFalse (be.CanBuildChannelListener<IOutputSessionChannel> (ctx), "#6");
141 Assert.IsFalse (be.CanBuildChannelListener<IRequestSessionChannel> (ctx), "#7");
142 Assert.IsFalse (be.CanBuildChannelListener<IInputSessionChannel> (ctx), "#8");
144 // IServiceChannel is not supported
145 Assert.IsFalse (be.CanBuildChannelListener<IServiceChannel> (ctx), "#9");
146 Assert.IsFalse (be.CanBuildChannelListener<IClientChannel> (ctx), "#10");
148 Assert.IsFalse (be.CanBuildChannelListener<IDuplexChannel> (ctx), "#11");
149 Assert.IsFalse (be.CanBuildChannelListener<IDuplexSessionChannel> (ctx), "#12");
153 public void GetPrpertyBindingDeliveryCapabilities ()
155 var be = new TcpTransportBindingElement ();
156 var dc = be.GetProperty<IBindingDeliveryCapabilities> (new BindingContext (new CustomBinding (), new BindingParameterCollection ()));
157 Assert.IsTrue (dc.AssuresOrderedDelivery, "#1");
158 Assert.IsFalse (dc.QueuedDelivery, "#2");
162 public void GetPropertySecurityCapabilities ()
164 var b = new TcpTransportBindingElement ();
165 var s = b.GetProperty<ISecurityCapabilities> (new BindingContext (new CustomBinding (), new BindingParameterCollection ()));
166 Assert.IsNull (s, "#1");
170 public void SimpleDuplexBuffered () // sample[svc|cli]4.exe
172 ServiceHost host = new ServiceHost (typeof (Foo));
173 var bindingS = new CustomBinding (new BindingElement [] {
174 new BinaryMessageEncodingBindingElement (),
175 new TcpTransportBindingElement () });
176 bindingS.ReceiveTimeout = TimeSpan.FromSeconds (5);
177 bindingS.OpenTimeout = TimeSpan.FromSeconds (20);
178 int port = NetworkHelpers.FindFreePort ();
179 host.AddServiceEndpoint (typeof (IFoo),
180 bindingS, new Uri ("net.tcp://localhost:" + port));
181 host.Description.Behaviors.Find<ServiceBehaviorAttribute> ().IncludeExceptionDetailInFaults = true;
185 for (int i = 0; i < 2; i++) {
186 var bindingC = new NetTcpBinding ();
187 bindingC.Security.Mode = SecurityMode.None;
188 IFooChannel proxy = new ChannelFactory<IFooChannel> (bindingC, new EndpointAddress ("net.tcp://localhost:" + port + "/")).CreateChannel ();
191 Assert.AreEqual ("TEST FOR ECHO", proxy.Echo ("TEST FOR ECHO"), "#1");
192 var sid = proxy.SessionId;
193 Assert.AreEqual (3000, proxy.Add (1000, 2000), "#2");
194 Assert.AreEqual (sid, proxy.SessionId, "#3");
199 } catch (Exception ex) {
200 Console.Error.WriteLine (ex);
207 [Category ("NotWorking")]
208 // FIXME: To enable this, we must fix the "request-reply TCP channel must close the connection" issue.
209 // It is strange, but the standalone test just works.
210 public void SimpleRequestReplyStreamed () // sample[svc|cli]5.exe
212 ServiceHost host = new ServiceHost (typeof (Foo));
213 NetTcpBinding bindingS = new NetTcpBinding ();
214 bindingS.TransferMode = TransferMode.Streamed;
215 bindingS.Security.Mode = SecurityMode.None;
216 bindingS.ReceiveTimeout = TimeSpan.FromSeconds (5);
217 bindingS.OpenTimeout = TimeSpan.FromSeconds (20);
218 int port = NetworkHelpers.FindFreePort ();
219 host.AddServiceEndpoint (typeof (IFoo),
220 bindingS, new Uri ("net.tcp://localhost:" + port));
221 host.Description.Behaviors.Find<ServiceBehaviorAttribute> ().IncludeExceptionDetailInFaults = true;
225 for (int i = 0; i < 2; i++) {
226 var bindingC = new NetTcpBinding ();
227 bindingS.TransferMode = TransferMode.Streamed;
228 bindingC.Security.Mode = SecurityMode.None;
229 IFooChannel proxy = new ChannelFactory<IFooChannel> (bindingC, new EndpointAddress ("net.tcp://localhost:" + port + "/")).CreateChannel ();
232 Assert.AreEqual ("TEST FOR ECHO", proxy.Echo ("TEST FOR ECHO"), "#1");
233 Assert.AreEqual (3000, proxy.Add (1000, 2000), "#2");
244 public interface IFoo
247 string Echo (string msg);
250 uint Add (uint v1, uint v2);
253 public interface IFooChannel : IFoo, IClientChannel
259 public string Echo (string msg)
264 public uint Add (uint v1, uint v2)