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 using MonoTests.Helpers;
40 namespace MonoTests.System.ServiceModel
43 public class NetTcpBindingTest
46 public void DefaultValues ()
48 var n = new NetTcpBinding ();
49 Assert.AreEqual (HostNameComparisonMode.StrongWildcard, n.HostNameComparisonMode, "#1");
50 Assert.AreEqual (10, n.ListenBacklog, "#2");
51 Assert.AreEqual (false, n.PortSharingEnabled, "#3");
53 var tr = n.CreateBindingElements ().Find<TcpTransportBindingElement> ();
54 Assert.IsNotNull (tr, "#tr1");
55 Assert.AreEqual (false, tr.TeredoEnabled, "#tr2");
56 Assert.AreEqual ("net.tcp", tr.Scheme, "#tr3");
58 Assert.IsFalse (n.TransactionFlow, "#4");
59 var tx = n.CreateBindingElements ().Find<TransactionFlowBindingElement> ();
60 Assert.IsNotNull (tx, "#tx1");
62 Assert.AreEqual (SecurityMode.Transport, n.Security.Mode, "#sec1");
63 Assert.AreEqual (ProtectionLevel.EncryptAndSign, n.Security.Transport.ProtectionLevel, "#sec2");
64 Assert.AreEqual (TcpClientCredentialType.Windows/*huh*/, n.Security.Transport.ClientCredentialType, "#sec3");
66 var bc = n.CreateBindingElements ();
67 Assert.AreEqual (4, bc.Count, "#bc1");
68 Assert.AreEqual (typeof (TransactionFlowBindingElement), bc [0].GetType (), "#bc2");
69 Assert.AreEqual (typeof (BinaryMessageEncodingBindingElement), bc [1].GetType (), "#bc3");
70 Assert.AreEqual (typeof (WindowsStreamSecurityBindingElement), bc [2].GetType (), "#bc4");
71 Assert.AreEqual (typeof (TcpTransportBindingElement), bc [3].GetType (), "#bc5");
73 Assert.IsFalse (n.CanBuildChannelFactory<IRequestChannel> (), "#cbf1");
74 Assert.IsFalse (n.CanBuildChannelFactory<IOutputChannel> (), "#cbf2");
75 Assert.IsFalse (n.CanBuildChannelFactory<IDuplexChannel> (), "#cbf3");
76 Assert.IsTrue (n.CanBuildChannelFactory<IDuplexSessionChannel> (), "#cbf4");
80 public void MessageSecurityAndBindings ()
82 var n = new NetTcpBinding ();
83 n.Security.Mode = SecurityMode.Message;
85 Assert.AreEqual (SecurityAlgorithmSuite.Default, n.Security.Message.AlgorithmSuite, "#sec1");
86 Assert.AreEqual (MessageCredentialType.Windows/*huh*/, n.Security.Message.ClientCredentialType, "#sec2");
88 Assert.AreEqual (TransferMode.Buffered, n.TransferMode, "#sec3");
90 var bc = n.CreateBindingElements ();
91 Assert.AreEqual (4, bc.Count, "#bc1");
92 Assert.AreEqual (typeof (TransactionFlowBindingElement), bc [0].GetType (), "#bc2");
93 Assert.AreEqual (typeof (SymmetricSecurityBindingElement), bc [1].GetType (), "#bc3");
94 Assert.AreEqual (typeof (BinaryMessageEncodingBindingElement), bc [2].GetType (), "#bc4");
95 Assert.AreEqual (typeof (TcpTransportBindingElement), bc [3].GetType (), "#bc5");
97 Assert.IsFalse (n.CanBuildChannelFactory<IRequestChannel> (), "#cbf1");
98 Assert.IsFalse (n.CanBuildChannelFactory<IOutputChannel> (), "#cbf2");
99 Assert.IsFalse (n.CanBuildChannelFactory<IDuplexChannel> (), "#cbf3");
100 Assert.IsTrue (n.CanBuildChannelFactory<IDuplexSessionChannel> (), "#cbf4");
104 public void MessageSecurityAndBindings2 ()
106 var n = new NetTcpBinding () { TransferMode = TransferMode.Streamed };
107 n.Security.Mode = SecurityMode.Message;
109 Assert.AreEqual (SecurityAlgorithmSuite.Default, n.Security.Message.AlgorithmSuite, "#sec1");
110 Assert.AreEqual (MessageCredentialType.Windows/*huh*/, n.Security.Message.ClientCredentialType, "#sec2");
112 var bc = n.CreateBindingElements ();
113 Assert.AreEqual (4, bc.Count, "#bc1");
114 Assert.AreEqual (typeof (TransactionFlowBindingElement), bc [0].GetType (), "#bc2");
115 Assert.AreEqual (typeof (SymmetricSecurityBindingElement), bc [1].GetType (), "#bc3");
116 Assert.AreEqual (typeof (BinaryMessageEncodingBindingElement), bc [2].GetType (), "#bc4");
117 Assert.AreEqual (typeof (TcpTransportBindingElement), bc [3].GetType (), "#bc5");
119 Assert.IsFalse (n.CanBuildChannelFactory<IRequestChannel> (), "#cbf1");
120 Assert.IsFalse (n.CanBuildChannelFactory<IOutputChannel> (), "#cbf2");
121 Assert.IsFalse (n.CanBuildChannelFactory<IDuplexChannel> (), "#cbf3");
122 Assert.IsFalse (n.CanBuildChannelFactory<IDuplexSessionChannel> (), "#cbf4");
123 Assert.IsTrue (n.CanBuildChannelFactory<IRequestSessionChannel> (), "#cbf5");
127 public void MessageSecurityAndBindings3 ()
129 var n = new NetTcpBinding () { TransferMode = TransferMode.Streamed };
130 n.Security.Mode = SecurityMode.Message;
131 n.Security.Message.ClientCredentialType = MessageCredentialType.Certificate;
133 var bc = n.CreateBindingElements ();
134 Assert.AreEqual (4, bc.Count, "#bc1");
135 Assert.AreEqual (typeof (TransactionFlowBindingElement), bc [0].GetType (), "#bc2");
136 Assert.AreEqual (typeof (SymmetricSecurityBindingElement), bc [1].GetType (), "#bc3");
137 Assert.AreEqual (typeof (BinaryMessageEncodingBindingElement), bc [2].GetType (), "#bc4");
138 Assert.AreEqual (typeof (TcpTransportBindingElement), bc [3].GetType (), "#bc5");
140 Assert.IsFalse (n.CanBuildChannelFactory<IRequestChannel> (), "#cbf1");
141 Assert.IsFalse (n.CanBuildChannelFactory<IOutputChannel> (), "#cbf2");
142 Assert.IsFalse (n.CanBuildChannelFactory<IDuplexChannel> (), "#cbf3");
143 Assert.IsFalse (n.CanBuildChannelFactory<IDuplexSessionChannel> (), "#cbf4");
144 Assert.IsTrue (n.CanBuildChannelFactory<IRequestSessionChannel> (), "#cbf5");
148 public void MessageSecurityAndBindings4 ()
150 var n = new NetTcpBinding ();
151 n.Security.Mode = SecurityMode.Message;
152 n.Security.Message.ClientCredentialType = MessageCredentialType.Certificate;
154 var bc = n.CreateBindingElements ();
155 Assert.AreEqual (4, bc.Count, "#bc1");
156 Assert.AreEqual (typeof (TransactionFlowBindingElement), bc [0].GetType (), "#bc2");
157 Assert.AreEqual (typeof (SymmetricSecurityBindingElement), bc [1].GetType (), "#bc3");
158 Assert.AreEqual (typeof (BinaryMessageEncodingBindingElement), bc [2].GetType (), "#bc4");
159 Assert.AreEqual (typeof (TcpTransportBindingElement), bc [3].GetType (), "#bc5");
161 Assert.IsFalse (n.CanBuildChannelFactory<IRequestChannel> (), "#cbf1");
162 Assert.IsFalse (n.CanBuildChannelFactory<IOutputChannel> (), "#cbf2");
163 Assert.IsFalse (n.CanBuildChannelFactory<IDuplexChannel> (), "#cbf3");
164 Assert.IsTrue (n.CanBuildChannelFactory<IDuplexSessionChannel> (), "#cbf4");
168 public void BufferedConnection ()
170 var host = new ServiceHost (typeof (Foo));
171 var bindingsvc = new CustomBinding (new BinaryMessageEncodingBindingElement (), new TcpTransportBindingElement ());
172 int port = NetworkHelpers.FindFreePort ();
173 host.AddServiceEndpoint (typeof (IFoo), bindingsvc, "net.tcp://localhost:" + port + "/");
174 host.Open (TimeSpan.FromSeconds (5));
176 var bindingcli = new NetTcpBinding () { TransactionFlow = false };
177 bindingcli.Security.Mode = SecurityMode.None;
178 var cli = new ChannelFactory<IFooClient> (bindingcli, new EndpointAddress ("net.tcp://localhost:" + port + "/")).CreateChannel ();
179 Assert.AreEqual (5, cli.Add (1, 4));
180 Assert.AreEqual ("monkey science", cli.Join ("monkey", "science"));
182 host.Close (TimeSpan.FromSeconds (5));
183 var t = new TcpListener (port);
187 Assert.IsTrue (Foo.AddCalled, "#1");
188 Assert.IsTrue (Foo.JoinCalled, "#2");
192 public void StreamedConnection ()
194 var host = new ServiceHost (typeof (Foo));
195 var bindingsvc = new CustomBinding (new BinaryMessageEncodingBindingElement (), new TcpTransportBindingElement () { TransferMode = TransferMode.Streamed });
196 int port = NetworkHelpers.FindFreePort ();
197 host.AddServiceEndpoint (typeof (IFoo), bindingsvc, "net.tcp://localhost:" + port + "/");
198 host.Open (TimeSpan.FromSeconds (5));
200 var bindingcli = new NetTcpBinding () { TransactionFlow = false };
201 bindingcli.TransferMode = TransferMode.Streamed;
202 bindingcli.Security.Mode = SecurityMode.None;
203 var cli = new ChannelFactory<IFooClient> (bindingcli, new EndpointAddress ("net.tcp://localhost:" + port + "/")).CreateChannel ();
204 Assert.AreEqual (5, cli.Add (1, 4));
205 Assert.AreEqual ("monkey science", cli.Join ("monkey", "science"));
207 host.Close (TimeSpan.FromSeconds (5));
208 var t = new TcpListener (port);
212 Assert.IsTrue (Foo.AddCalled, "#1");
213 Assert.IsTrue (Foo.JoinCalled, "#2");
217 public void ReaderQuotasDefault_Bug15153 ()
219 NetTcpBinding binding = new NetTcpBinding(SecurityMode.None);
220 binding.ReaderQuotas.MaxStringContentLength = 8192;
224 public interface IFoo
227 int Add (short s, int i);
229 string Join (string s1, string s2);
232 public interface IFooClient : IFoo, IClientChannel
236 public class Foo : IFoo
238 public static bool AddCalled;
239 public static bool JoinCalled;
241 public int Add (short s, int i)
247 public string Join (string s1, string s2)
250 return s1 + " " + s2;