NetTcpBindingTest.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. //
  2. // NetTcpBindingTest.cs
  3. //
  4. // Author:
  5. // Atsushi Enomoto <[email protected]>
  6. //
  7. // Copyright (C) 2009 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.Net.Sockets;
  31. using System.Net.Security;
  32. using System.ServiceModel;
  33. using System.ServiceModel.Channels;
  34. using System.ServiceModel.Security;
  35. using System.Threading;
  36. using NUnit.Framework;
  37. namespace MonoTests.System.ServiceModel
  38. {
  39. [TestFixture]
  40. public class NetTcpBindingTest
  41. {
  42. [Test]
  43. public void DefaultValues ()
  44. {
  45. var n = new NetTcpBinding ();
  46. Assert.AreEqual (HostNameComparisonMode.StrongWildcard, n.HostNameComparisonMode, "#1");
  47. Assert.AreEqual (10, n.ListenBacklog, "#2");
  48. Assert.AreEqual (false, n.PortSharingEnabled, "#3");
  49. var tr = n.CreateBindingElements ().Find<TcpTransportBindingElement> ();
  50. Assert.IsNotNull (tr, "#tr1");
  51. Assert.AreEqual (false, tr.TeredoEnabled, "#tr2");
  52. Assert.AreEqual ("net.tcp", tr.Scheme, "#tr3");
  53. Assert.IsFalse (n.TransactionFlow, "#4");
  54. var tx = n.CreateBindingElements ().Find<TransactionFlowBindingElement> ();
  55. Assert.IsNotNull (tx, "#tx1");
  56. }
  57. [Test]
  58. public void SimpleConnection ()
  59. {
  60. var host = new ServiceHost (typeof (Foo));
  61. var bindingsvc = new NetTcpBinding ();
  62. bindingsvc.Security.Mode = SecurityMode.None;
  63. host.AddServiceEndpoint (typeof (IFoo), bindingsvc, "net.tcp://localhost/");
  64. host.Open (TimeSpan.FromSeconds (5));
  65. try {
  66. var bindingcli = new NetTcpBinding ();
  67. bindingcli.Security.Mode = SecurityMode.None;
  68. var cli = new ChannelFactory<IFooClient> (bindingcli, new EndpointAddress ("net.tcp://localhost/")).CreateChannel ();
  69. Assert.AreEqual (5, cli.Add (1, 4));
  70. Assert.AreEqual ("monkey science", cli.Join ("monkey", "science"));
  71. } finally {
  72. host.Close (TimeSpan.FromSeconds (5));
  73. var t = new TcpListener (808);
  74. t.Start ();
  75. t.Stop ();
  76. }
  77. Assert.IsTrue (Foo.AddCalled, "#1");
  78. Assert.IsTrue (Foo.JoinCalled, "#2");
  79. }
  80. [ServiceContract]
  81. public interface IFoo
  82. {
  83. [OperationContract]
  84. int Add (short s, int i);
  85. [OperationContract]
  86. string Join (string s1, string s2);
  87. }
  88. public interface IFooClient : IFoo, IClientChannel
  89. {
  90. }
  91. public class Foo : IFoo
  92. {
  93. public static bool AddCalled;
  94. public static bool JoinCalled;
  95. public int Add (short s, int i)
  96. {
  97. AddCalled = true;
  98. return s + i;
  99. }
  100. public string Join (string s1, string s2)
  101. {
  102. JoinCalled = true;
  103. return s1 + " " + s2;
  104. }
  105. }
  106. }
  107. }