Don't ignore drives with type "aufs" or "overlay" (Xamarin-31021)
[mono.git] / mcs / class / System.ServiceModel / Test / System.ServiceModel / IntegratedConnectionTest.cs
1 //
2 // IntegratedConnectionTest.cs
3 //
4 // Author:
5 //      Atsushi Enomoto <atsushi@ximian.com>
6 //
7 // Copyright (C) 2006 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.Runtime.Serialization;
34 using System.ServiceModel;
35 using System.ServiceModel.Channels;
36 using System.ServiceModel.Description;
37 using System.ServiceModel.Dispatcher;
38 using System.Threading;
39 using System.Xml;
40 using NUnit.Framework;
41
42 using MonoTests.Helpers;
43
44 namespace MonoTests.System.ServiceModel
45 {
46         // FIXME: uncomment when NUnit tests on HTTP connection got as
47         // stable as non-nunit samples.
48         
49         [TestFixture]
50         public class IntegratedConnectionTest
51         {
52                 [Test]
53                 [Ignore ("With Orcas it does not work fine")]
54                 // It is almost identical to samples/basic-http-binding
55                 public void SimpleHttpConnection ()
56                 {
57                         // Service
58                         ServiceHost host = new ServiceHost (typeof (Foo));
59                         int port = NetworkHelpers.FindFreePort ();
60                         try {
61                                 Binding binding = new BasicHttpBinding ();
62                                 binding.SendTimeout = binding.ReceiveTimeout = TimeSpan.FromSeconds (5);
63                                 ServiceEndpoint se = host.AddServiceEndpoint ("MonoTests.System.ServiceModel.IFoo",
64                                         binding, new Uri ("http://localhost:" + port));
65                                 host.Open ();
66
67                                 // Client
68                                 ChannelFactory<IFoo> cf = new ChannelFactory<IFoo> (
69                                         new BasicHttpBinding (),
70                                         new EndpointAddress ("http://localhost:" + port + "/"));
71                                 IFoo foo  = cf.CreateChannel ();
72                                 Assert.AreEqual ("Test for EchoTest for Echo", foo.Echo ("Test for Echo"));
73                         } finally {
74                                 if (host.State == CommunicationState.Opened)
75                                         host.Close ();
76                         }
77                 }
78
79                 [Test]
80                 [Ignore ("With Orcas it does not work fine")]
81                 public void SimpleClientBase ()
82                 {
83                         // Service
84                         ServiceHost host = new ServiceHost (typeof (Foo2));
85                         int port = NetworkHelpers.FindFreePort ();
86                         try {
87                                 Binding binding = new BasicHttpBinding ();
88                                 binding.SendTimeout = binding.ReceiveTimeout = TimeSpan.FromSeconds (5);
89                                 host.AddServiceEndpoint ("MonoTests.System.ServiceModel.IFoo2",
90                                 binding, new Uri ("http://localhost:" + port));
91                                 host.Open ();
92
93                                 // Client
94                                 Foo2Proxy proxy = new Foo2Proxy (
95                                         new BasicHttpBinding (),
96                                         new EndpointAddress ("http://localhost:" + port + "/"));
97                                 proxy.Open ();
98                                 try {
99                                         Assert.AreEqual ("TEST FOR ECHOTEST FOR ECHO",
100                                                 proxy.Echo ("TEST FOR ECHO"));
101                                 } finally {
102                                         proxy.Close ();
103                                 }
104                         } finally {
105                                 // Service
106                                 if (host.State == CommunicationState.Opened)
107                                         host.Close ();
108                         }
109                 }
110
111                 [Test]
112                 [Ignore ("With Orcas it does not work fine")]
113                 public void ExchangeMetadata ()
114                 {
115                         // Service
116                         ServiceHost host = new ServiceHost (typeof (MetadataExchange));
117                         int port = NetworkHelpers.FindFreePort ();
118                         try {
119                                 Binding binding = new BasicHttpBinding ();
120                                 binding.ReceiveTimeout = TimeSpan.FromSeconds (5);
121                                 host.AddServiceEndpoint ("IMetadataExchange",
122                                 binding, new Uri ("http://localhost:" + port));
123                                 host.Open ();
124                                 // Client
125
126                                 MetadataExchangeProxy proxy = new MetadataExchangeProxy (
127                                         new BasicHttpBinding (),
128                                         new EndpointAddress ("http://localhost:" + port + "/"));
129                                 proxy.Open ();
130
131                                 try {
132                                         Message req = Message.CreateMessage (MessageVersion.Soap11, "http://schemas.xmlsoap.org/ws/2004/09/transfer/Get");
133                                         Message res = proxy.Get (req);
134                                 } finally {
135                                         proxy.Close ();
136                                 }
137                         } finally {
138                                 // Service
139                                 if (host.State == CommunicationState.Opened)
140                                         host.Close ();
141                         }
142                 }
143
144                 [ServiceContract(SessionMode = SessionMode.Required)]
145                 interface IFoo3
146                 {
147                         [OperationContract]
148                         int GetInstanceCounter ();
149                 }
150
151                         [ServiceBehavior (InstanceContextMode = InstanceContextMode.PerSession)]
152                 class Foo3 : IFoo3, IDisposable
153                 {
154                         public static int CreatedInstances { get; set; }
155                         public static int DisposedInstances { get; set; }
156
157                         public static int InstanceCounter {
158                                 get { return CreatedInstances - DisposedInstances; }
159                         }
160
161                         public Foo3 ()
162                         {
163                                 CreatedInstances++;
164                         }
165
166                         public int GetInstanceCounter ()
167                         {
168                                 return InstanceCounter;
169                         }
170
171                         public virtual void Dispose ()
172                         {
173                                 DisposedInstances++;
174                         }
175
176                 }
177
178                 private void TestSessionbehaviour (Binding binding, Uri address)
179                 {
180                         ServiceHost host = new ServiceHost (typeof (Foo3), address);
181                         host.AddServiceEndpoint (typeof (IFoo3), binding, address);
182                         host.Description.Behaviors.Add (new ServiceThrottlingBehavior () { MaxConcurrentSessions = 1 });
183                         Foo3.CreatedInstances = Foo3.DisposedInstances = 0; // Reset
184                         host.Open ();
185                         Assert.AreEqual (0, Foo3.InstanceCounter, "Initial state wrong"); // just to be sure
186                         var cd = (ChannelDispatcher) host.ChannelDispatchers [0];
187                         for (int i = 1; i <= 3; ++i) {
188                                 var factory = new ChannelFactory<IFoo3Client> (binding, address.ToString ());
189                                 var proxy = factory.CreateChannel ();
190                                 Assert.AreEqual (1, proxy.GetInstanceCounter (), "One server instance after first call in session #" + i);
191                                 Assert.AreEqual (1, proxy.GetInstanceCounter (), "One server instance after second call in session #" + i);
192                                 factory.Close (); // should close session even when no IsTerminating method has been invoked
193                                 Thread.Sleep (500); // give WCF time to dispose service object
194                                 Assert.AreEqual (0, Foo3.InstanceCounter, "Service instances must be disposed after channel is closed, in session #" + i);
195                                 Assert.AreEqual (i, Foo3.CreatedInstances, "One new instance per session, in session #" + i);
196                         }
197                         host.Close ();
198                 }
199
200                 interface IFoo3Client : IFoo3, IClientChannel {}
201
202                 [Test (Description = "Tests InstanceContextMode.PerSession behavior for NetTcp binding")]
203                 public void TestSessionInstancesNetTcp ()
204                 {
205                         Binding binding = new NetTcpBinding (SecurityMode.None, false);
206                         Uri address = new Uri (binding.Scheme + "://localhost:9999/test");
207                         TestSessionbehaviour (binding, address);
208                 }
209
210                 [Test (Description = "Tests InstanceContextMode.PerSession behavior for WsHttp binding")]
211                 [Category ("NotWorking")] // no working WSHttpBinding in mono.
212                 public void TestSessionInstancesWsHttp ()
213                 {
214                         Binding binding = new WSHttpBinding (SecurityMode.None, true);
215                         Uri address = new Uri (binding.Scheme + "://localhost:9999/test");
216                         TestSessionbehaviour(binding, address);
217                 }
218         }
219
220         #region SimpleConnection classes
221
222         [ServiceContract]
223         public interface IFoo
224         {
225                 [OperationContract]
226                 string Echo (string msg);
227         }
228
229         class Foo : IFoo
230         {
231                 public string Echo (string msg)
232                 {
233                         return msg + msg;
234                 }
235         }
236
237         // This is manually created type for strongly typed request.
238         [DataContract (Name = "Echo", Namespace = "http://tempuri.org/")]
239         public class EchoType
240         {
241                 public EchoType (string msg)
242                 {
243                         this.msg = msg;
244                 }
245
246                 [DataMember]
247                 public string msg = "test";
248         }
249
250         #endregion
251
252         #region SampleClientBase classes
253
254         [ServiceContract]
255         public interface IFoo2
256         {
257                 [OperationContract]
258                 string Echo (string msg);
259         }
260
261         class Foo2 : IFoo2
262         {
263                 public string Echo (string msg)
264                 {
265                         return msg + msg;
266                 }
267         }
268
269         public class Foo2Proxy : ClientBase<IFoo2>, IFoo2
270         {
271                 public Foo2Proxy (Binding binding, EndpointAddress address)
272                         : base (binding, address)
273                 {
274                 }
275
276                 public string Echo (string msg)
277                 {
278                         return Channel.Echo (msg);
279                 }
280         }
281
282         #endregion
283
284         #region ExchangeMetadata classes
285
286         class MetadataExchange : IMetadataExchange
287         {
288                 public Message Get (Message request)
289                 {
290                         XmlDocument doc = new XmlDocument ();
291                         doc.AppendChild (doc.CreateElement ("Metadata", "http://schemas.xmlsoap.org/ws/2004/09/mex"));
292                         return Message.CreateMessage (request.Version,
293                                 "http://schemas.xmlsoap.org/ws/2004/09/transfer/GetResponse",
294                                 new XmlNodeReader (doc));
295                 }
296
297                 public IAsyncResult BeginGet (Message request, AsyncCallback cb, object state)
298                 {
299                         throw new NotImplementedException ();
300                 }
301
302                 public Message EndGet (IAsyncResult result)
303                 {
304                         throw new NotImplementedException ();
305                 }
306         }
307
308         public class MetadataExchangeProxy : ClientBase<IMetadataExchange>, IMetadataExchange
309         {
310                 public MetadataExchangeProxy (Binding binding, EndpointAddress address)
311                         : base (binding, address)
312                 {
313                 }
314
315                 public Message Get (Message request)
316                 {
317                         return Channel.Get (request);
318                 }
319
320                 public IAsyncResult BeginGet (Message request, AsyncCallback callback, object state)
321                 {
322                         throw new NotImplementedException ();
323                 }
324
325                 public Message EndGet (IAsyncResult result)
326                 {
327                         throw new NotImplementedException ();
328                 }
329         }
330
331         #endregion
332 }