DispatchRuntimeTest.cs 14 KB

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