Merge pull request #5714 from alexischr/update_bockbuild
[mono.git] / mcs / class / System.ServiceModel / Test / System.ServiceModel.Channels / TcpTransportBindingElementTest.cs
1 //
2 // TcpTransportBindingElementTest.cs
3 //
4 // Author:
5 //      Atsushi Enomoto <atsushi@ximian.com>
6 //
7 // Copyright (C) 2008 Novell, Inc.  http://www.novell.com
8 //
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:
16 // 
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
19 // 
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.
27 //
28 using System;
29 using System.Collections.ObjectModel;
30 using System.IO;
31 using System.Net;
32 using System.Net.Security;
33 using System.ServiceModel;
34 using System.ServiceModel.Channels;
35 using System.ServiceModel.Description;
36 using System.Threading;
37 using System.Xml;
38 using NUnit.Framework;
39
40 using MonoTests.Helpers;
41
42 namespace MonoTests.System.ServiceModel.Channels
43 {
44         [TestFixture]
45         public class TcpTransportBindingElementTest
46         {
47                 static BindingParameterCollection empty_params =
48                         new BindingParameterCollection ();
49
50                 [Test]
51                 public void DefaultValues ()
52                 {
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");
63
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");
74                 }
75
76                 [Test]
77                 public void CanBuildChannelFactory ()
78                 {
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");
87
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");
92
93                         // IServiceChannel is not supported
94                         Assert.IsFalse (be.CanBuildChannelFactory<IServiceChannel> (ctx), "#9");
95                         Assert.IsFalse (be.CanBuildChannelFactory<IClientChannel> (ctx), "#10");
96
97                         Assert.IsFalse (be.CanBuildChannelFactory<IDuplexChannel> (ctx), "#11");
98                         Assert.IsTrue (be.CanBuildChannelFactory<IDuplexSessionChannel> (ctx), "#12");
99                 }
100
101                 [Test]
102                 public void CanBuildChannelListener ()
103                 {
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");
112
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");
117
118                         // IServiceChannel is not supported
119                         Assert.IsFalse (be.CanBuildChannelListener<IServiceChannel> (ctx), "#9");
120                         Assert.IsFalse (be.CanBuildChannelListener<IClientChannel> (ctx), "#10");
121
122                         Assert.IsFalse (be.CanBuildChannelListener<IDuplexChannel> (ctx), "#11");
123                         Assert.IsTrue (be.CanBuildChannelListener<IDuplexSessionChannel> (ctx), "#12");
124                 }
125
126                 [Test]
127                 public void CanBuildChannelListener2 ()
128                 {
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");
138
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");
143
144                         // IServiceChannel is not supported
145                         Assert.IsFalse (be.CanBuildChannelListener<IServiceChannel> (ctx), "#9");
146                         Assert.IsFalse (be.CanBuildChannelListener<IClientChannel> (ctx), "#10");
147
148                         Assert.IsFalse (be.CanBuildChannelListener<IDuplexChannel> (ctx), "#11");
149                         Assert.IsFalse (be.CanBuildChannelListener<IDuplexSessionChannel> (ctx), "#12");
150                 }
151
152                 [Test]
153                 public void GetPrpertyBindingDeliveryCapabilities ()
154                 {
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");
159                 }
160
161                 [Test]
162                 public void GetPropertySecurityCapabilities ()
163                 {
164                         var b = new TcpTransportBindingElement ();
165                         var s = b.GetProperty<ISecurityCapabilities> (new BindingContext (new CustomBinding (), new BindingParameterCollection ()));
166                         Assert.IsNull (s, "#1");
167                 }
168
169                 [Test]
170                 public void SimpleDuplexBuffered () // sample[svc|cli]4.exe
171                 {
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;
182                         host.Open ();
183
184                         try {
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 ();
189                                         proxy.Open ();
190                                         try {
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");
195                                         } finally {
196                                                 proxy.Close ();
197                                         }
198                                 }
199                         } catch (Exception ex) {
200                                 Console.Error.WriteLine (ex);
201                         } finally {
202                                 host.Close ();
203                         }
204                 }
205
206                 [Test]
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
211                 {
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;
222                         host.Open ();
223
224                         try {
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 ();
230                                         proxy.Open ();
231                                         try {
232                                                 Assert.AreEqual ("TEST FOR ECHO", proxy.Echo ("TEST FOR ECHO"), "#1");
233                                                 Assert.AreEqual (3000, proxy.Add (1000, 2000), "#2");
234                                         } finally {
235                                                 proxy.Close ();
236                                         }
237                                 }
238                         } finally {
239                                 host.Close ();
240                         }
241                 }
242
243                 [ServiceContract]
244                 public interface IFoo
245                 {
246                         [OperationContract]
247                         string Echo (string msg);
248
249                         [OperationContract]
250                         uint Add (uint v1, uint v2);
251                 }
252
253                 public interface IFooChannel : IFoo, IClientChannel
254                 {
255                 }
256
257                 class Foo : IFoo
258                 {
259                         public string Echo (string msg)
260                         {
261                                 return msg;
262                         }
263                         
264                         public uint Add (uint v1, uint v2)
265                         {
266                                 return v1 + v2;
267                         }
268                 }
269         }
270 }