2010-03-12 Jb Evain <jbevain@novell.com>
[mono.git] / mcs / class / System.ServiceModel / Test / System.ServiceModel.Dispatcher / DispatchRuntimeTest.cs
1 //
2 // DispatchRuntimeTest.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
29 using System;
30 using System.Collections.Generic;
31 using System.Runtime.Serialization;
32 using System.ServiceModel;
33 using System.ServiceModel.Channels;
34 using System.ServiceModel.Description;
35 using System.ServiceModel.Dispatcher;
36 using System.ServiceModel.Security;
37 using System.Xml;
38 using NUnit.Framework;
39 using System.Collections.ObjectModel;
40 using SMMessage = System.ServiceModel.Channels.Message;
41 using System.Threading;
42
43 namespace MonoTests.System.ServiceModel.Dispatcher
44 {
45         [TestFixture]
46         public class DispatchRuntimeTest
47         {
48                 [Test]
49                 public void TestConstructor ()
50                 {
51                         // This test is rather to just detect implementation 
52                         // differences than digging in-depth meanings, so feel
53                         // free to change if MS implementation does not make 
54                         // sense.
55                         DispatchRuntime r = new EndpointDispatcher (
56                                 new EndpointAddress ("http://localhost:8080"), "IFoo", "urn:foo").DispatchRuntime;
57                         Assert.AreEqual (AuditLogLocation.Default,
58                                          r.SecurityAuditLogLocation, "#1");
59
60                         Assert.IsTrue (r.AutomaticInputSessionShutdown, "#2");
61                         Assert.IsNotNull (r.CallbackClientRuntime, "#3");
62                         Assert.IsNull (r.ExternalAuthorizationPolicies, "#4");
63                         Assert.IsFalse (r.IgnoreTransactionMessageProperty, "#5");
64                         Assert.IsFalse (r.ImpersonateCallerForAllOperations, "#6");
65                         Assert.AreEqual (0, r.InputSessionShutdownHandlers.Count, "#7");
66                         Assert.AreEqual (0, r.InstanceContextInitializers.Count, "#8");
67                         //Assert.AreEqual (0, r.InstanceContextLifetimes.Count, "#9");
68                         Assert.IsNull (r.InstanceProvider, "#10");
69                         Assert.AreEqual (AuditLevel.None,
70                                          r.MessageAuthenticationAuditLevel, "#11");
71                         Assert.AreEqual (0, r.MessageInspectors.Count, "#12");
72                         Assert.AreEqual (0, r.Operations.Count, "#13");
73                         Assert.IsNull (r.OperationSelector, "#14");
74                         // This is silly, but anyways there will be similar 
75                         // functionality that just represents unix "Groups".
76                         Assert.AreEqual (PrincipalPermissionMode.UseWindowsGroups,
77                                          r.PrincipalPermissionMode, "#15");
78                         Assert.IsFalse (r.ReleaseServiceInstanceOnTransactionComplete, "#16");
79                         Assert.IsNull (r.RoleProvider, "#17");
80                         Assert.AreEqual (AuditLevel.None, r.ServiceAuthorizationAuditLevel, "#18");
81                         Assert.IsNull (r.ServiceAuthorizationManager, "#19");
82                         Assert.IsTrue (r.SuppressAuditFailure, "#20");
83                         Assert.IsNull (r.SynchronizationContext, "#21");
84                         Assert.IsFalse (r.TransactionAutoCompleteOnSessionClose, "#22");
85                         Assert.IsNull (r.Type, "#23");
86                         Assert.IsNotNull (r.UnhandledDispatchOperation, "#24");
87                         DispatchOperation udo = r.UnhandledDispatchOperation;
88                         Assert.AreEqual ("*", udo.Name, "#24-2");
89                         Assert.AreEqual ("*", udo.Action, "#24-3");
90                         Assert.AreEqual ("*", udo.ReplyAction, "#24-4");
91                         Assert.IsFalse (udo.IsOneWay, "#24-5");
92                 }
93
94                 [Test]
95                 [Category ("NotWorking")] // It somehow stopped working properly recently in Nov. 2009, not sure where the source of the problem lies.
96                 public void TestInstanceBehavior1()
97                 {
98                         
99                         Result res = new Result ();
100                         MessageInspectBehavior b = new MessageInspectBehavior ();
101                         b.instanceCtxInitializer = new MyInstanceContextInitializer (res);
102                         b.instanceCtxProvider = new MyInstanceContextProvider (null, res);
103                         //b.instanceProvider = new MyInstanceProvider (null, res);
104                         b.msgInspect = new MyMessageInspector (res);
105                         string expected = "GetExistingInstanceContext , InitializeInstanceContext , OperationContext , InstanceContext = Opening , Initialize , OperationContext , InstanceContext = Opening , AfterReceiveRequest , OperationContext , InstanceContext = Opened , BeforeSendReply , OperationContext , InstanceContext = Opened , IsIdle , OperationContext , InstanceContext = Opened , NotifyIdle , OperationContext , InstanceContext = Opened , ";
106                         TestInstanceBehavior (b, expected, res, 1);
107                         Assert.IsTrue (res.Done, "done");
108                 }
109
110                 [Test]
111                 [Category ("NotWorking")] // It somehow stopped working properly recently in Nov. 2009, not sure where the source of the problem lies.
112                 public void TestInstanceBehavior2 ()
113                 {
114                         Result res = new Result ();
115                         MessageInspectBehavior b = new MessageInspectBehavior ();
116                         b.instanceCtxInitializer = new MyInstanceContextInitializer (res);
117                         b.instanceCtxProvider = new MyInstanceContextProvider (null, res);
118                         b.instanceProvider = new MyInstanceProvider (new AllActions (res), res);
119                         b.msgInspect = new MyMessageInspector (res);
120                         string expected = "GetExistingInstanceContext , InitializeInstanceContext , OperationContext , InstanceContext = Opening , Initialize , OperationContext , InstanceContext = Opening , AfterReceiveRequest , OperationContext , InstanceContext = Opened , GetInstance1 , OperationContext , InstanceContext = Opened , BeforeSendReply , OperationContext , InstanceContext = Opened , ReleaseInstance , OperationContext , InstanceContext = Opened , IsIdle , OperationContext , InstanceContext = Opened , NotifyIdle , OperationContext , InstanceContext = Opened , ";
121                         TestInstanceBehavior (b, expected, res, 1);
122                         Assert.IsTrue (res.Done, "done");
123                 }
124
125                 [Test]
126                 [Category ("NotWorking")] // It somehow stopped working properly recently in Nov. 2009, not sure where the source of the problem lies.
127                 public void TestInstanceBehavior3 ()
128                 {
129                         Result res = new Result ();
130                         MessageInspectBehavior b = new MessageInspectBehavior ();
131                         b.instanceCtxInitializer = new MyInstanceContextInitializer (res);
132
133                         InstanceContext c = new InstanceContext (new AllActions (res));
134                         
135                         b.instanceCtxProvider = new MyInstanceContextProvider (c, res);
136                         b.instanceProvider = new MyInstanceProvider (new AllActions (res), res);
137                         b.msgInspect = new MyMessageInspector (res);
138                         string expected = "GetExistingInstanceContext , InitializeInstanceContext , OperationContext , InstanceContext = Opening , Initialize , OperationContext , InstanceContext = Opening , AfterReceiveRequest , OperationContext , InstanceContext = Opened , BeforeSendReply , OperationContext , InstanceContext = Opened , ";
139                         TestInstanceBehavior (b, expected, res, 1);                                     Assert.IsTrue (res.Done, "done");
140                 }
141
142                 [Test]
143                 [Category ("NotWorking")]
144                 public void TestInstanceBehavior4 ()
145                 {
146                         Result res = new Result ();
147                         MessageInspectBehavior b = new MessageInspectBehavior ();
148                         b.instanceCtxInitializer = new MyInstanceContextInitializer (res);      
149                 
150                         b.instanceCtxProvider = new MyInstanceContextProvider (null, res);
151                         b.instanceProvider = new MyInstanceProvider (new AllActions (res), res);
152                         b.msgInspect = new MyMessageInspector (res);
153                         string expected = "GetExistingInstanceContext , InitializeInstanceContext , OperationContext , InstanceContext = Opening , Initialize , OperationContext , InstanceContext = Opening , AfterReceiveRequest , OperationContext , InstanceContext = Opened , GetInstance1 , OperationContext , InstanceContext = Opened , BeforeSendReply , OperationContext , InstanceContext = Opened , ReleaseInstance , OperationContext , InstanceContext = Opened , IsIdle , OperationContext , InstanceContext = Opened , NotifyIdle , OperationContext , InstanceContext = Opened , GetExistingInstanceContext , InitializeInstanceContext , OperationContext , InstanceContext = Opening , Initialize , OperationContext , InstanceContext = Opening , AfterReceiveRequest , OperationContext , InstanceContext = Opened , GetInstance1 , OperationContext , InstanceContext = Opened , BeforeSendReply , OperationContext , InstanceContext = Opened , ReleaseInstance , OperationContext , InstanceContext = Opened , IsIdle , OperationContext , InstanceContext = Opened , NotifyIdle , OperationContext , InstanceContext = Opened , ";
154                         TestInstanceBehavior (b, expected, res, 2);                                     Assert.IsTrue (res.Done, "done");
155                 }
156
157                 void TestInstanceBehavior (MessageInspectBehavior b, string expected, Result actual, int invocations)
158                 {
159                         ServiceHost h = new ServiceHost (typeof (AllActions), new Uri ("http://localhost:8080"));
160                         try {
161                                 h.AddServiceEndpoint (typeof (IAllActions).FullName, new BasicHttpBinding (), "AllActions");
162                                 h.Description.Behaviors.Add (b);
163                                 ServiceDebugBehavior db = h.Description.Behaviors.Find<ServiceDebugBehavior> ();
164                                 db.IncludeExceptionDetailInFaults = true;
165                                 h.Open ();
166                                 AllActionsProxy p = new AllActionsProxy (new BasicHttpBinding () { SendTimeout = TimeSpan.FromSeconds (5), ReceiveTimeout = TimeSpan.FromSeconds (5) }, new EndpointAddress ("http://localhost:8080/AllActions"));
167
168                                 for (int i = 0; i < invocations; ++i)
169                                         p.Get (10);
170                                 p.Close ();
171
172                                 //let ther server finish his work
173                                 Thread.Sleep (100);
174                                 Assert.AreEqual (expected, actual.string_res);
175                                 actual.Done = true;
176                         }
177                         finally {                               
178                                 h.Close ();
179                         }
180                 }
181         }
182
183
184 #region helpers
185
186         #region message inspectors
187
188         public class MessageInspectBehavior : IServiceBehavior
189         {
190                 public MyMessageInspector msgInspect;
191                 public MyInstanceContextProvider instanceCtxProvider;
192                 public MyInstanceProvider instanceProvider;
193                 public MyInstanceContextInitializer instanceCtxInitializer;
194
195                 public void AddBindingParameters (ServiceDescription serviceDescription, ServiceHostBase serviceHostBase, Collection<ServiceEndpoint> endpoints, BindingParameterCollection bindingParameters) {
196                         
197                 }
198
199                 public void ApplyDispatchBehavior (ServiceDescription serviceDescription, ServiceHostBase serviceHostBase) {
200                         ChannelDispatcher d = serviceHostBase.ChannelDispatchers [0] as ChannelDispatcher;
201                         d.Endpoints [0].DispatchRuntime.MessageInspectors.Add (msgInspect);
202                         d.Endpoints [0].DispatchRuntime.InstanceContextProvider = instanceCtxProvider;
203                         d.Endpoints [0].DispatchRuntime.InstanceProvider = instanceProvider;
204                         d.Endpoints [0].DispatchRuntime.InstanceContextInitializers.Add (instanceCtxInitializer);
205                 }
206
207                 public void Validate (ServiceDescription serviceDescription, ServiceHostBase serviceHostBase) {
208                         
209                 }
210         }
211
212         public class MyMessageInspector : IDispatchMessageInspector
213         {
214                 Result res;
215
216                 public MyMessageInspector (Result result) {
217                         res = result;
218                 }
219                 #region IDispatchMessageInspector Members
220
221                 public object AfterReceiveRequest (ref Message request, IClientChannel channel, InstanceContext instanceContext) {
222                         res.string_res += "AfterReceiveRequest , ";
223                         res.AddCurrentOperationContextInfo ();
224                         return null;
225                 }
226
227                 public void BeforeSendReply (ref Message reply, object correlationState) {
228                         res.string_res += "BeforeSendReply , ";
229                         res.AddCurrentOperationContextInfo ();
230                 }
231
232                 #endregion
233         }
234
235         #endregion
236
237         #region InstanceProvider
238
239         public class MyInstanceProvider : IInstanceProvider
240         {               
241                 object instance;
242                 Result res;
243
244                 public MyInstanceProvider (object obj, Result result) {
245                         instance = obj;
246                         res = result;
247                 }
248
249                 #region IInstanceProvider Members
250
251                 public object GetInstance (InstanceContext instanceContext, Message message) {
252                         res.string_res += "GetInstance1 , ";
253                         res.AddCurrentOperationContextInfo ();
254                         return instance;
255                 }
256
257                 public object GetInstance (InstanceContext instanceContext) {
258                         res.string_res += "GetInstance2 , ";
259                         res.AddCurrentOperationContextInfo ();
260                         return instance;
261                 }
262
263                 public void ReleaseInstance (InstanceContext instanceContext, object instance) {
264                         res.string_res += "ReleaseInstance , ";
265                         res.AddCurrentOperationContextInfo ();
266                 }
267
268                 #endregion
269         }
270
271         #endregion
272
273         #region InstanceContextProvider
274
275         public class MyInstanceContextProvider : IInstanceContextProvider
276         {
277
278                 InstanceContext existing;
279                 Result res;
280
281                 public MyInstanceContextProvider (InstanceContext exist, Result result) {
282                         existing = exist;
283                         res = result;
284                 }
285
286                 #region IInstanceContextProvider Members
287
288                 public InstanceContext GetExistingInstanceContext (Message message, IContextChannel channel) {
289                         res.string_res += "GetExistingInstanceContext , ";
290                         res.AddCurrentOperationContextInfo ();
291                         return existing;
292                 }
293
294                 public void InitializeInstanceContext (InstanceContext instanceContext, Message message, IContextChannel channel) {
295                         res.string_res += "InitializeInstanceContext , ";
296                         res.AddCurrentOperationContextInfo ();
297                 }
298
299                 public bool IsIdle (InstanceContext instanceContext) {
300                         res.string_res += "IsIdle , ";
301                         res.AddCurrentOperationContextInfo ();
302                         return false;
303                 }
304
305                 public void NotifyIdle (InstanceContextIdleCallback callback, InstanceContext instanceContext) {
306                         res.string_res += "NotifyIdle , ";
307                         res.AddCurrentOperationContextInfo ();                  
308                 }
309
310                 #endregion
311         }
312
313         #endregion
314
315         #region InstanceContextInitializer
316
317         public class MyInstanceContextInitializer : IInstanceContextInitializer
318         {
319                 Result res;
320
321                 public MyInstanceContextInitializer (Result result) {
322                         res = result;
323                 }
324
325                 public void Initialize (InstanceContext instanceContext, Message message) {
326                         res.string_res += "Initialize , ";
327                         res.AddCurrentOperationContextInfo ();
328                 }
329         }
330
331         #endregion
332
333         #region Helpers
334
335         public class Result
336         {
337                 public bool Done;
338                 public string string_res = "";
339
340                 public void AddCurrentOperationContextInfo()
341                 {
342                         if (OperationContext.Current != null) {
343                                 string_res += "OperationContext , ";
344                                 if (OperationContext.Current.InstanceContext != null) {
345                                         string_res += ("InstanceContext = " + OperationContext.Current.InstanceContext.State + " , ");
346                                         //if (OperationContext.Current.InstanceContext != null)
347                                         //    string_res += ("Instance = " + OperationContext.Current.InstanceContext.GetServiceInstance () + " , ");
348                                 }
349                         }
350                 }
351         }
352
353         class AllActions : IAllActions, IDisposable
354         {
355                 Result res;
356
357                 public AllActions () { }
358
359                 public AllActions (Result result) {
360                         res = result;
361                 }
362
363                 [OperationBehavior (ReleaseInstanceMode = ReleaseInstanceMode.BeforeAndAfterCall)]
364                 public int Get(int i)
365                 {
366                         return i;
367                 }
368
369                 public void Dispose () {
370                         if (res != null)
371                                 res.string_res += "Disposed , ";
372                 }
373         }
374
375         [ServiceContract (Namespace = "http://MonoTests.System.ServiceModel.Dispatcher")]
376         public interface IAllActions
377         {               
378                 [OperationContract]             
379                 int Get(int i);
380         }
381
382         #endregion
383
384         #region ClientProxy
385         
386         public class AllActionsProxy : ClientBase<IAllActions>, IAllActions
387         {
388                 public AllActionsProxy (Binding binding, EndpointAddress remoteAddress) :
389                         base (binding, remoteAddress)
390                 {
391                 }
392
393                 public int Get (int i) {
394                         return base.Channel.Get (i);
395                 }
396         }       
397
398         #endregion
399 #endregion
400
401 }