DispatchRuntimeTest.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  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 (), new EndpointAddress ("http://localhost:8080/AllActions"));
  146. for (int i = 0; i < invocations; ++i)
  147. p.Get (10);
  148. //let ther server finish his work
  149. Thread.Sleep (100);
  150. Assert.AreEqual (expected, actual.string_res);
  151. }
  152. finally {
  153. h.Close ();
  154. }
  155. }
  156. }
  157. #region helpers
  158. #region message inspectors
  159. public class MessageInspectBehavior : IServiceBehavior
  160. {
  161. public MyMessageInspector msgInspect;
  162. public MyInstanceContextProvider instanceCtxProvider;
  163. public MyInstanceProvider instanceProvider;
  164. public MyInstanceContextInitializer instanceCtxInitializer;
  165. public void AddBindingParameters (ServiceDescription serviceDescription, ServiceHostBase serviceHostBase, Collection<ServiceEndpoint> endpoints, BindingParameterCollection bindingParameters) {
  166. }
  167. public void ApplyDispatchBehavior (ServiceDescription serviceDescription, ServiceHostBase serviceHostBase) {
  168. ChannelDispatcher d = serviceHostBase.ChannelDispatchers [0] as ChannelDispatcher;
  169. d.Endpoints [0].DispatchRuntime.MessageInspectors.Add (msgInspect);
  170. d.Endpoints [0].DispatchRuntime.InstanceContextProvider = instanceCtxProvider;
  171. d.Endpoints [0].DispatchRuntime.InstanceProvider = instanceProvider;
  172. d.Endpoints [0].DispatchRuntime.InstanceContextInitializers.Add (instanceCtxInitializer);
  173. }
  174. public void Validate (ServiceDescription serviceDescription, ServiceHostBase serviceHostBase) {
  175. }
  176. }
  177. public class MyMessageInspector : IDispatchMessageInspector
  178. {
  179. Result res;
  180. public MyMessageInspector (Result result) {
  181. res = result;
  182. }
  183. #region IDispatchMessageInspector Members
  184. public object AfterReceiveRequest (ref Message request, IClientChannel channel, InstanceContext instanceContext) {
  185. res.string_res += "AfterReceiveRequest , ";
  186. res.AddCurrentOperationContextInfo ();
  187. return null;
  188. }
  189. public void BeforeSendReply (ref Message reply, object correlationState) {
  190. res.string_res += "BeforeSendReply , ";
  191. res.AddCurrentOperationContextInfo ();
  192. }
  193. #endregion
  194. }
  195. #endregion
  196. #region InstanceProvider
  197. public class MyInstanceProvider : IInstanceProvider
  198. {
  199. object instance;
  200. Result res;
  201. public MyInstanceProvider (object obj, Result result) {
  202. instance = obj;
  203. res = result;
  204. }
  205. #region IInstanceProvider Members
  206. public object GetInstance (InstanceContext instanceContext, Message message) {
  207. res.string_res += "GetInstance1 , ";
  208. res.AddCurrentOperationContextInfo ();
  209. return instance;
  210. }
  211. public object GetInstance (InstanceContext instanceContext) {
  212. res.string_res += "GetInstance2 , ";
  213. res.AddCurrentOperationContextInfo ();
  214. return instance;
  215. }
  216. public void ReleaseInstance (InstanceContext instanceContext, object instance) {
  217. res.string_res += "ReleaseInstance , ";
  218. res.AddCurrentOperationContextInfo ();
  219. }
  220. #endregion
  221. }
  222. #endregion
  223. #region InstanceContextProvider
  224. public class MyInstanceContextProvider : IInstanceContextProvider
  225. {
  226. InstanceContext existing;
  227. Result res;
  228. public MyInstanceContextProvider (InstanceContext exist, Result result) {
  229. existing = exist;
  230. res = result;
  231. }
  232. #region IInstanceContextProvider Members
  233. public InstanceContext GetExistingInstanceContext (Message message, IContextChannel channel) {
  234. res.string_res += "GetExistingInstanceContext , ";
  235. res.AddCurrentOperationContextInfo ();
  236. return existing;
  237. }
  238. public void InitializeInstanceContext (InstanceContext instanceContext, Message message, IContextChannel channel) {
  239. res.string_res += "InitializeInstanceContext , ";
  240. res.AddCurrentOperationContextInfo ();
  241. }
  242. public bool IsIdle (InstanceContext instanceContext) {
  243. res.string_res += "IsIdle , ";
  244. res.AddCurrentOperationContextInfo ();
  245. return false;
  246. }
  247. public void NotifyIdle (InstanceContextIdleCallback callback, InstanceContext instanceContext) {
  248. res.string_res += "NotifyIdle , ";
  249. res.AddCurrentOperationContextInfo ();
  250. }
  251. #endregion
  252. }
  253. #endregion
  254. #region InstanceContextInitializer
  255. public class MyInstanceContextInitializer : IInstanceContextInitializer
  256. {
  257. Result res;
  258. public MyInstanceContextInitializer (Result result) {
  259. res = result;
  260. }
  261. public void Initialize (InstanceContext instanceContext, Message message) {
  262. res.string_res += "Initialize , ";
  263. res.AddCurrentOperationContextInfo ();
  264. }
  265. }
  266. #endregion
  267. #region Helpers
  268. public class Result
  269. {
  270. public string string_res = "";
  271. public void AddCurrentOperationContextInfo()
  272. {
  273. if (OperationContext.Current != null) {
  274. string_res += "OperationContext , ";
  275. if (OperationContext.Current.InstanceContext != null) {
  276. string_res += ("InstanceContext = " + OperationContext.Current.InstanceContext.State + " , ");
  277. //if (OperationContext.Current.InstanceContext != null)
  278. // string_res += ("Instance = " + OperationContext.Current.InstanceContext.GetServiceInstance () + " , ");
  279. }
  280. }
  281. }
  282. }
  283. class AllActions : IAllActions, IDisposable
  284. {
  285. Result res;
  286. public AllActions () { }
  287. public AllActions (Result result) {
  288. res = result;
  289. }
  290. [OperationBehavior (ReleaseInstanceMode = ReleaseInstanceMode.BeforeAndAfterCall)]
  291. public int Get(int i)
  292. {
  293. return i;
  294. }
  295. public void Dispose () {
  296. if (res != null)
  297. res.string_res += "Disposed , ";
  298. }
  299. }
  300. [ServiceContract (Namespace = "http://MonoTests.System.ServiceModel.Dispatcher")]
  301. public interface IAllActions
  302. {
  303. [OperationContract]
  304. int Get(int i);
  305. }
  306. #endregion
  307. #region ClientProxy
  308. public class AllActionsProxy : ClientBase<IAllActions>, IAllActions
  309. {
  310. public AllActionsProxy (Binding binding, EndpointAddress remoteAddress) :
  311. base (binding, remoteAddress)
  312. {
  313. }
  314. public int Get (int i) {
  315. return base.Channel.Get (i);
  316. }
  317. }
  318. #endregion
  319. #endregion
  320. }