2 // NetTcpBindingTest.cs
5 // Atsushi Enomoto <atsushi@ximian.com>
7 // Copyright (C) 2009 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;
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;
38 namespace MonoTests.System.ServiceModel
41 public class NetTcpBindingTest
44 public void DefaultValues ()
46 var n = new NetTcpBinding ();
47 Assert.AreEqual (HostNameComparisonMode.StrongWildcard, n.HostNameComparisonMode, "#1");
48 Assert.AreEqual (10, n.ListenBacklog, "#2");
49 Assert.AreEqual (false, n.PortSharingEnabled, "#3");
51 var tr = n.CreateBindingElements ().Find<TcpTransportBindingElement> ();
52 Assert.IsNotNull (tr, "#tr1");
53 Assert.AreEqual (false, tr.TeredoEnabled, "#tr2");
54 Assert.AreEqual ("net.tcp", tr.Scheme, "#tr3");
56 Assert.IsFalse (n.TransactionFlow, "#4");
57 var tx = n.CreateBindingElements ().Find<TransactionFlowBindingElement> ();
58 Assert.IsNotNull (tx, "#tx1");
62 public void BufferedConnection ()
64 var host = new ServiceHost (typeof (Foo));
65 var bindingsvc = new CustomBinding (new BinaryMessageEncodingBindingElement (), new TcpTransportBindingElement ());
66 host.AddServiceEndpoint (typeof (IFoo), bindingsvc, "net.tcp://localhost:37564/");
67 host.Open (TimeSpan.FromSeconds (5));
69 var bindingcli = new NetTcpBinding () { TransactionFlow = false };
70 bindingcli.Security.Mode = SecurityMode.None;
71 var cli = new ChannelFactory<IFooClient> (bindingcli, new EndpointAddress ("net.tcp://localhost:37564/")).CreateChannel ();
72 Assert.AreEqual (5, cli.Add (1, 4));
73 Assert.AreEqual ("monkey science", cli.Join ("monkey", "science"));
75 host.Close (TimeSpan.FromSeconds (5));
76 var t = new TcpListener (37564);
80 Assert.IsTrue (Foo.AddCalled, "#1");
81 Assert.IsTrue (Foo.JoinCalled, "#2");
85 public void StreamedConnection ()
87 var host = new ServiceHost (typeof (Foo));
88 var bindingsvc = new CustomBinding (new BinaryMessageEncodingBindingElement (), new TcpTransportBindingElement () { TransferMode = TransferMode.Streamed });
89 host.AddServiceEndpoint (typeof (IFoo), bindingsvc, "net.tcp://localhost:37564/");
90 host.Open (TimeSpan.FromSeconds (5));
92 var bindingcli = new NetTcpBinding () { TransactionFlow = false };
93 bindingcli.TransferMode = TransferMode.Streamed;
94 bindingcli.Security.Mode = SecurityMode.None;
95 var cli = new ChannelFactory<IFooClient> (bindingcli, new EndpointAddress ("net.tcp://localhost:37564/")).CreateChannel ();
96 Assert.AreEqual (5, cli.Add (1, 4));
97 Assert.AreEqual ("monkey science", cli.Join ("monkey", "science"));
99 host.Close (TimeSpan.FromSeconds (5));
100 var t = new TcpListener (37564);
104 Assert.IsTrue (Foo.AddCalled, "#1");
105 Assert.IsTrue (Foo.JoinCalled, "#2");
109 public interface IFoo
112 int Add (short s, int i);
114 string Join (string s1, string s2);
117 public interface IFooClient : IFoo, IClientChannel
121 public class Foo : IFoo
123 public static bool AddCalled;
124 public static bool JoinCalled;
126 public int Add (short s, int i)
132 public string Join (string s1, string s2)
135 return s1 + " " + s2;