Merge pull request #2034 from alexrp/ctx-cleanup
[mono.git] / mcs / class / System.ServiceModel / Test / System.ServiceModel / NetTcpBindingTest.cs
1 //
2 // NetTcpBindingTest.cs
3 //
4 // Author:
5 //      Atsushi Enomoto <atsushi@ximian.com>
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
38 using MonoTests.Helpers;
39
40 namespace MonoTests.System.ServiceModel
41 {
42         [TestFixture]
43         public class NetTcpBindingTest
44         {
45                 [Test]
46                 public void DefaultValues ()
47                 {
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");
52
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");
57
58                         Assert.IsFalse (n.TransactionFlow, "#4");
59                         var tx = n.CreateBindingElements ().Find<TransactionFlowBindingElement> ();
60                         Assert.IsNotNull (tx, "#tx1");
61
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");
65
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");
72                         
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");
77                 }
78
79                 [Test]
80                 public void MessageSecurityAndBindings ()
81                 {
82                         var n = new NetTcpBinding ();
83                         n.Security.Mode = SecurityMode.Message;
84                         
85                         Assert.AreEqual (SecurityAlgorithmSuite.Default, n.Security.Message.AlgorithmSuite, "#sec1");
86                         Assert.AreEqual (MessageCredentialType.Windows/*huh*/, n.Security.Message.ClientCredentialType, "#sec2");
87
88                         Assert.AreEqual (TransferMode.Buffered, n.TransferMode, "#sec3");
89
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");
96
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");
101                 }
102
103                 [Test]
104                 public void MessageSecurityAndBindings2 ()
105                 {
106                         var n = new NetTcpBinding () { TransferMode = TransferMode.Streamed };
107                         n.Security.Mode = SecurityMode.Message;
108                         
109                         Assert.AreEqual (SecurityAlgorithmSuite.Default, n.Security.Message.AlgorithmSuite, "#sec1");
110                         Assert.AreEqual (MessageCredentialType.Windows/*huh*/, n.Security.Message.ClientCredentialType, "#sec2");
111
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");
118
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");
124                 }
125
126                 [Test]
127                 public void MessageSecurityAndBindings3 ()
128                 {
129                         var n = new NetTcpBinding () { TransferMode = TransferMode.Streamed };
130                         n.Security.Mode = SecurityMode.Message;
131                         n.Security.Message.ClientCredentialType = MessageCredentialType.Certificate;
132                         
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");
139
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");
145                 }
146
147                 [Test]
148                 public void MessageSecurityAndBindings4 ()
149                 {
150                         var n = new NetTcpBinding ();
151                         n.Security.Mode = SecurityMode.Message;
152                         n.Security.Message.ClientCredentialType = MessageCredentialType.Certificate;
153
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");
160
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");
165                 }
166
167                 [Test]
168                 public void BufferedConnection ()
169                 {
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));
175                         try {
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"));
181                         } finally {
182                                 host.Close (TimeSpan.FromSeconds (5));
183                                 var t = new TcpListener (port);
184                                 t.Start ();
185                                 t.Stop ();
186                         }
187                         Assert.IsTrue (Foo.AddCalled, "#1");
188                         Assert.IsTrue (Foo.JoinCalled, "#2");
189                 }
190
191                 [Test]
192                 public void StreamedConnection ()
193                 {
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));
199                         try {
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"));
206                         } finally {
207                                 host.Close (TimeSpan.FromSeconds (5));
208                                 var t = new TcpListener (port);
209                                 t.Start ();
210                                 t.Stop ();
211                         }
212                         Assert.IsTrue (Foo.AddCalled, "#1");
213                         Assert.IsTrue (Foo.JoinCalled, "#2");
214                 }
215
216                 [Test]
217                 public void ReaderQuotasDefault_Bug15153 ()
218                 {
219                         NetTcpBinding binding = new NetTcpBinding(SecurityMode.None);
220                         binding.ReaderQuotas.MaxStringContentLength = 8192;
221                 }
222
223                 [ServiceContract]
224                 public interface IFoo
225                 {
226                         [OperationContract]
227                         int Add (short s, int i);
228                         [OperationContract]
229                         string Join (string s1, string s2);
230                 }
231
232                 public interface IFooClient : IFoo, IClientChannel
233                 {
234                 }
235
236                 public class Foo : IFoo
237                 {
238                         public static bool AddCalled;
239                         public static bool JoinCalled;
240
241                         public int Add (short s, int i)
242                         {
243                                 AddCalled = true;
244                                 return s + i;
245                         }
246
247                         public string Join (string s1, string s2)
248                         {
249                                 JoinCalled = true;
250                                 return s1 + " " + s2;
251                         }
252                 }
253         }
254 }