[runtime] Fixed get_process_module module name.
[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 namespace MonoTests.System.ServiceModel.Channels
41 {
42         [TestFixture]
43         public class TcpTransportBindingElementTest
44         {
45                 static BindingParameterCollection empty_params =
46                         new BindingParameterCollection ();
47
48                 [Test]
49                 public void DefaultValues ()
50                 {
51                         TcpTransportBindingElement be =
52                                 new TcpTransportBindingElement ();
53                         Assert.AreEqual (TimeSpan.FromSeconds (5), be.ChannelInitializationTimeout, "#1");
54                         Assert.AreEqual (0x2000, be.ConnectionBufferSize, "#2");
55                         Assert.AreEqual (HostNameComparisonMode.StrongWildcard, be.HostNameComparisonMode, "#3");
56                         Assert.AreEqual (0x10000, be.MaxBufferSize, "#4");
57                         Assert.AreEqual (TimeSpan.FromMilliseconds (200), be.MaxOutputDelay, "#5");
58                         Assert.AreEqual (1, be.MaxPendingAccepts, "#6");
59                         Assert.AreEqual (10, be.MaxPendingConnections, "#7");
60                         Assert.AreEqual (TransferMode.Buffered, be.TransferMode, "#8");
61
62                         Assert.AreEqual (10, be.ListenBacklog, "#9");
63                         Assert.IsFalse (be.PortSharingEnabled, "#10");
64                         Assert.AreEqual ("net.tcp", be.Scheme, "#11");
65                         Assert.IsFalse (be.TeredoEnabled, "#12");
66                         TcpConnectionPoolSettings pool = be.ConnectionPoolSettings;
67                         Assert.IsNotNull (pool, "#13");
68                         Assert.AreEqual ("default", pool.GroupName, "#14");
69                         Assert.AreEqual (TimeSpan.FromSeconds (120), pool.IdleTimeout, "#15");
70                         Assert.AreEqual (TimeSpan.FromSeconds (300), pool.LeaseTimeout, "#16");
71                         Assert.AreEqual (10, pool.MaxOutboundConnectionsPerEndpoint, "#17");
72                 }
73
74                 [Test]
75                 public void CanBuildChannelFactory ()
76                 {
77                         TcpTransportBindingElement be =
78                                 new TcpTransportBindingElement ();
79                         BindingContext ctx = new BindingContext (
80                                 new CustomBinding (), empty_params);
81                         Assert.IsFalse (be.CanBuildChannelFactory<IRequestChannel> (ctx), "#1");
82                         Assert.IsFalse (be.CanBuildChannelFactory<IInputChannel> (ctx), "#2");
83                         Assert.IsFalse (be.CanBuildChannelFactory<IReplyChannel> (ctx), "#3");
84                         Assert.IsFalse (be.CanBuildChannelFactory<IOutputChannel> (ctx), "#4");
85
86                         Assert.IsFalse (be.CanBuildChannelFactory<IRequestSessionChannel> (ctx), "#5");
87                         Assert.IsFalse (be.CanBuildChannelFactory<IInputSessionChannel> (ctx), "#6");
88                         Assert.IsFalse (be.CanBuildChannelFactory<IReplySessionChannel> (ctx), "#7");
89                         Assert.IsFalse (be.CanBuildChannelFactory<IOutputSessionChannel> (ctx), "#8");
90
91                         // IServiceChannel is not supported
92                         Assert.IsFalse (be.CanBuildChannelFactory<IServiceChannel> (ctx), "#9");
93                         Assert.IsFalse (be.CanBuildChannelFactory<IClientChannel> (ctx), "#10");
94
95                         Assert.IsFalse (be.CanBuildChannelFactory<IDuplexChannel> (ctx), "#11");
96                         Assert.IsTrue (be.CanBuildChannelFactory<IDuplexSessionChannel> (ctx), "#12");
97                 }
98
99                 [Test]
100                 public void CanBuildChannelListener ()
101                 {
102                         TcpTransportBindingElement be =
103                                 new TcpTransportBindingElement ();
104                         BindingContext ctx = new BindingContext (
105                                 new CustomBinding (), empty_params);
106                         Assert.IsFalse (be.CanBuildChannelListener<IReplyChannel> (ctx), "#1");
107                         Assert.IsFalse (be.CanBuildChannelListener<IOutputChannel> (ctx), "#2");
108                         Assert.IsFalse (be.CanBuildChannelListener<IRequestChannel> (ctx), "#3");
109                         Assert.IsFalse (be.CanBuildChannelListener<IInputChannel> (ctx), "#4");
110
111                         Assert.IsFalse (be.CanBuildChannelListener<IReplySessionChannel> (ctx), "#5");
112                         Assert.IsFalse (be.CanBuildChannelListener<IOutputSessionChannel> (ctx), "#6");
113                         Assert.IsFalse (be.CanBuildChannelListener<IRequestSessionChannel> (ctx), "#7");
114                         Assert.IsFalse (be.CanBuildChannelListener<IInputSessionChannel> (ctx), "#8");
115
116                         // IServiceChannel is not supported
117                         Assert.IsFalse (be.CanBuildChannelListener<IServiceChannel> (ctx), "#9");
118                         Assert.IsFalse (be.CanBuildChannelListener<IClientChannel> (ctx), "#10");
119
120                         Assert.IsFalse (be.CanBuildChannelListener<IDuplexChannel> (ctx), "#11");
121                         Assert.IsTrue (be.CanBuildChannelListener<IDuplexSessionChannel> (ctx), "#12");
122                 }
123
124                 [Test]
125                 public void CanBuildChannelListener2 ()
126                 {
127                         TcpTransportBindingElement be =
128                                 new TcpTransportBindingElement ();
129                         be.TransferMode = TransferMode.Streamed;
130                         BindingContext ctx = new BindingContext (
131                                 new CustomBinding (), empty_params);
132                         Assert.IsTrue (be.CanBuildChannelListener<IReplyChannel> (ctx), "#1");
133                         Assert.IsFalse (be.CanBuildChannelListener<IOutputChannel> (ctx), "#2");
134                         Assert.IsFalse (be.CanBuildChannelListener<IRequestChannel> (ctx), "#3");
135                         Assert.IsFalse (be.CanBuildChannelListener<IInputChannel> (ctx), "#4");
136
137                         Assert.IsFalse (be.CanBuildChannelListener<IReplySessionChannel> (ctx), "#5");
138                         Assert.IsFalse (be.CanBuildChannelListener<IOutputSessionChannel> (ctx), "#6");
139                         Assert.IsFalse (be.CanBuildChannelListener<IRequestSessionChannel> (ctx), "#7");
140                         Assert.IsFalse (be.CanBuildChannelListener<IInputSessionChannel> (ctx), "#8");
141
142                         // IServiceChannel is not supported
143                         Assert.IsFalse (be.CanBuildChannelListener<IServiceChannel> (ctx), "#9");
144                         Assert.IsFalse (be.CanBuildChannelListener<IClientChannel> (ctx), "#10");
145
146                         Assert.IsFalse (be.CanBuildChannelListener<IDuplexChannel> (ctx), "#11");
147                         Assert.IsFalse (be.CanBuildChannelListener<IDuplexSessionChannel> (ctx), "#12");
148                 }
149
150                 [Test]
151                 public void GetPrpertyBindingDeliveryCapabilities ()
152                 {
153                         var be = new TcpTransportBindingElement ();
154                         var dc = be.GetProperty<IBindingDeliveryCapabilities> (new BindingContext (new CustomBinding (), new BindingParameterCollection ()));
155                         Assert.IsTrue (dc.AssuresOrderedDelivery, "#1");
156                         Assert.IsFalse (dc.QueuedDelivery, "#2");
157                 }
158
159                 [Test]
160                 public void GetPropertySecurityCapabilities ()
161                 {
162                         var b = new TcpTransportBindingElement ();
163                         var s = b.GetProperty<ISecurityCapabilities> (new BindingContext (new CustomBinding (), new BindingParameterCollection ()));
164                         Assert.IsNull (s, "#1");
165                 }
166
167                 [Test]
168                 public void SimpleDuplexBuffered () // sample[svc|cli]4.exe
169                 {
170                         ServiceHost host = new ServiceHost (typeof (Foo));
171                         var bindingS = new CustomBinding (new BindingElement [] {
172                                 new BinaryMessageEncodingBindingElement (),
173                                 new TcpTransportBindingElement () });
174                         bindingS.ReceiveTimeout = TimeSpan.FromSeconds (5);
175                         bindingS.OpenTimeout = TimeSpan.FromSeconds (20);
176                         host.AddServiceEndpoint (typeof (IFoo),
177                                 bindingS, new Uri ("net.tcp://localhost:37564"));
178                         host.Description.Behaviors.Find<ServiceBehaviorAttribute> ().IncludeExceptionDetailInFaults = true;
179                         host.Open ();
180
181                         try {
182                                 for (int i = 0; i < 2; i++) {
183                                         var bindingC = new NetTcpBinding ();
184                                         bindingC.Security.Mode = SecurityMode.None;
185                                         IFooChannel proxy = new ChannelFactory<IFooChannel> (bindingC, new EndpointAddress ("net.tcp://localhost:37564/")).CreateChannel ();
186                                         proxy.Open ();
187                                         try {
188                                                 Assert.AreEqual ("TEST FOR ECHO", proxy.Echo ("TEST FOR ECHO"), "#1");
189                                                 var sid = proxy.SessionId;
190                                                 Assert.AreEqual (3000, proxy.Add (1000, 2000), "#2");
191                                                 Assert.AreEqual (sid, proxy.SessionId, "#3");
192                                         } finally {
193                                                 proxy.Close ();
194                                         }
195                                 }
196                         } catch (Exception ex) {
197                                 Console.Error.WriteLine (ex);
198                         } finally {
199                                 host.Close ();
200                         }
201                 }
202
203                 [Test]
204                 [Category ("NotWorking")]
205                 // FIXME: To enable this, we must fix the "request-reply TCP channel must close the connection" issue.
206                 // It is strange, but the standalone test just works.
207                 public void SimpleRequestReplyStreamed () // sample[svc|cli]5.exe
208                 {
209                         ServiceHost host = new ServiceHost (typeof (Foo));
210                         NetTcpBinding bindingS = new NetTcpBinding ();
211                         bindingS.TransferMode = TransferMode.Streamed;
212                         bindingS.Security.Mode = SecurityMode.None;
213                         bindingS.ReceiveTimeout = TimeSpan.FromSeconds (5);
214                         bindingS.OpenTimeout = TimeSpan.FromSeconds (20);
215                         host.AddServiceEndpoint (typeof (IFoo),
216                                 bindingS, new Uri ("net.tcp://localhost:37564"));
217                         host.Description.Behaviors.Find<ServiceBehaviorAttribute> ().IncludeExceptionDetailInFaults = true;
218                         host.Open ();
219
220                         try {
221                                 for (int i = 0; i < 2; i++) {
222                                         var bindingC = new NetTcpBinding ();
223                                         bindingS.TransferMode = TransferMode.Streamed;
224                                         bindingC.Security.Mode = SecurityMode.None;
225                                         IFooChannel proxy = new ChannelFactory<IFooChannel> (bindingC, new EndpointAddress ("net.tcp://localhost:37564/")).CreateChannel ();
226                                         proxy.Open ();
227                                         try {
228                                                 Assert.AreEqual ("TEST FOR ECHO", proxy.Echo ("TEST FOR ECHO"), "#1");
229                                                 Assert.AreEqual (3000, proxy.Add (1000, 2000), "#2");
230                                         } finally {
231                                                 proxy.Close ();
232                                         }
233                                 }
234                         } finally {
235                                 host.Close ();
236                         }
237                 }
238
239                 [ServiceContract]
240                 public interface IFoo
241                 {
242                         [OperationContract]
243                         string Echo (string msg);
244
245                         [OperationContract]
246                         uint Add (uint v1, uint v2);
247                 }
248
249                 public interface IFooChannel : IFoo, IClientChannel
250                 {
251                 }
252
253                 class Foo : IFoo
254                 {
255                         public string Echo (string msg)
256                         {
257                                 return msg;
258                         }
259                         
260                         public uint Add (uint v1, uint v2)
261                         {
262                                 return v1 + v2;
263                         }
264                 }
265         }
266 }