TcpTransportBindingElementTest.cs 10 KB

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