BaseCalls.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553
  1. //
  2. // MonoTests.Remoting.BaseCalls.cs
  3. //
  4. // Author: Lluis Sanchez Gual ([email protected])
  5. //
  6. // 2003 (C) Copyright, Ximian, Inc.
  7. //
  8. using System;
  9. using System.Net;
  10. using System.Text;
  11. using System.Threading;
  12. using System.Collections;
  13. using System.Globalization;
  14. using System.Runtime.Remoting;
  15. using System.Runtime.Remoting.Channels;
  16. using System.Runtime.Remoting.Messaging;
  17. using System.Runtime.Remoting.Lifetime;
  18. using System.Runtime.Remoting.Channels.Tcp;
  19. using System.Runtime.Remoting.Activation;
  20. using System.Runtime.Remoting.Contexts;
  21. using System.Runtime.InteropServices;
  22. using NUnit.Framework;
  23. namespace MonoTests.Remoting
  24. {
  25. public abstract class BaseCallTest : Assertion
  26. {
  27. IChannelSender chs;
  28. string[] remoteUris;
  29. CallsDomainServer server;
  30. int remoteDomId;
  31. [TestFixtureSetUp]
  32. public void Run()
  33. {
  34. try
  35. {
  36. remoteDomId = CreateServer ();
  37. }
  38. catch (Exception ex)
  39. {
  40. Console.WriteLine (ex);
  41. }
  42. }
  43. [TestFixtureTearDown]
  44. public void End ()
  45. {
  46. ShutdownServer ();
  47. }
  48. protected virtual int CreateServer ()
  49. {
  50. ChannelManager cm = CreateChannelManager ();
  51. chs = cm.CreateClientChannel ();
  52. ChannelServices.RegisterChannel (chs);
  53. AppDomain domain = AppDomain.CreateDomain ("testdomain");
  54. server = (CallsDomainServer) domain.CreateInstanceAndUnwrap(GetType().Assembly.FullName,"MonoTests.Remoting.CallsDomainServer");
  55. remoteUris = server.Start (cm);
  56. return server.GetDomId ();
  57. }
  58. protected virtual void ShutdownServer ()
  59. {
  60. server.Stop ();
  61. if (chs != null)
  62. ChannelServices.UnregisterChannel (chs);
  63. }
  64. protected virtual RemoteObject CreateRemoteInstance ()
  65. {
  66. return (RemoteObject) Activator.GetObject (typeof(RemoteObject), remoteUris[0]);
  67. }
  68. protected virtual AbstractRemoteObject CreateRemoteAbstract ()
  69. {
  70. return (AbstractRemoteObject) Activator.GetObject (typeof(AbstractRemoteObject), remoteUris[1]);
  71. }
  72. protected virtual IRemoteObject CreateRemoteInterface ()
  73. {
  74. return (IRemoteObject) Activator.GetObject (typeof(IRemoteObject), remoteUris[2]);
  75. }
  76. public InstanceSurrogate InternalGetInstanceSurrogate ()
  77. {
  78. InstanceSurrogate s = GetInstanceSurrogate ();
  79. s.RemoteObject = CreateRemoteInstance ();
  80. return s;
  81. }
  82. public AbstractSurrogate InternalGetAbstractSurrogate ()
  83. {
  84. AbstractSurrogate s = GetAbstractSurrogate ();
  85. s.RemoteObject = CreateRemoteAbstract ();
  86. return s;
  87. }
  88. public InterfaceSurrogate InternalGetInterfaceSurrogate ()
  89. {
  90. InterfaceSurrogate s = GetInterfaceSurrogate ();
  91. s.RemoteObject = CreateRemoteInterface ();
  92. return s;
  93. }
  94. public abstract InstanceSurrogate GetInstanceSurrogate ();
  95. public abstract AbstractSurrogate GetAbstractSurrogate ();
  96. public abstract InterfaceSurrogate GetInterfaceSurrogate ();
  97. public virtual ChannelManager CreateChannelManager ()
  98. {
  99. return null;
  100. }
  101. //
  102. // The tests
  103. //
  104. [Test]
  105. public void TestInstanceSimple ()
  106. {
  107. RunTestSimple (InternalGetInstanceSurrogate());
  108. }
  109. [Test]
  110. public void TestAbstractSimple ()
  111. {
  112. RunTestSimple (InternalGetAbstractSurrogate());
  113. }
  114. [Test]
  115. public void TestInterfaceSimple ()
  116. {
  117. RunTestSimple (InternalGetInterfaceSurrogate());
  118. }
  119. [Test]
  120. public void TestInstancePrimitiveParams ()
  121. {
  122. RunTestPrimitiveParams (InternalGetInstanceSurrogate());
  123. }
  124. [Test]
  125. public void TestAbstractPrimitiveParams ()
  126. {
  127. RunTestPrimitiveParams (InternalGetAbstractSurrogate());
  128. }
  129. [Test]
  130. public void TestInterfacePrimitiveParams ()
  131. {
  132. RunTestPrimitiveParams (InternalGetInterfaceSurrogate());
  133. }
  134. [Test]
  135. public void TestInstancePrimitiveParamsInOut ()
  136. {
  137. RunTestPrimitiveParamsInOut (InternalGetInstanceSurrogate());
  138. }
  139. [Test]
  140. public void TestAbstractPrimitiveParamsInOut ()
  141. {
  142. RunTestPrimitiveParamsInOut (InternalGetAbstractSurrogate());
  143. }
  144. [Test]
  145. public void TestInterfacePrimitiveParamsInOut ()
  146. {
  147. RunTestPrimitiveParamsInOut (InternalGetInterfaceSurrogate());
  148. }
  149. [Test]
  150. public void TestInstanceComplexParams ()
  151. {
  152. RunTestComplexParams (InternalGetInstanceSurrogate());
  153. }
  154. [Test]
  155. public void TestAbstractComplexParams ()
  156. {
  157. RunTestComplexParams (InternalGetAbstractSurrogate());
  158. }
  159. [Test]
  160. public void TestInterfaceComplexParams ()
  161. {
  162. RunTestComplexParams (InternalGetInterfaceSurrogate());
  163. }
  164. [Test]
  165. public void TestInstanceComplexParamsInOut ()
  166. {
  167. RunTestComplexParamsInOut (InternalGetInstanceSurrogate());
  168. }
  169. [Test]
  170. public void TestAbstractComplexParamsInOut ()
  171. {
  172. RunTestComplexParamsInOut (InternalGetAbstractSurrogate());
  173. }
  174. [Test]
  175. public void TestInterfaceComplexParamsInOut ()
  176. {
  177. RunTestComplexParamsInOut (InternalGetInterfaceSurrogate());
  178. }
  179. [Test]
  180. public void TestInstanceProcessContextData ()
  181. {
  182. RunTestProcessContextData (InternalGetInstanceSurrogate());
  183. }
  184. [Test]
  185. public void TestAbstractProcessContextData ()
  186. {
  187. RunTestProcessContextData (InternalGetAbstractSurrogate());
  188. }
  189. [Test]
  190. public void TestInterfaceProcessContextData ()
  191. {
  192. RunTestProcessContextData (InternalGetInterfaceSurrogate());
  193. }
  194. //
  195. // The tests runners
  196. //
  197. public void RunTestSimple (IRemoteObject testerSurrogate)
  198. {
  199. AssertEquals ("ReturnValue", 130772 + remoteDomId, testerSurrogate.Simple ());
  200. }
  201. public void RunTestPrimitiveParams (IRemoteObject testerSurrogate)
  202. {
  203. AssertEquals ("ReturnValue", "11-22-L-SG@"+remoteDomId, testerSurrogate.PrimitiveParams (11, 22, 'L', "SG"));
  204. }
  205. public void RunTestPrimitiveParamsInOut (IRemoteObject testerSurrogate)
  206. {
  207. int a2, a1 = 9876543;
  208. float b2, b1 = 82437.83f;
  209. char c2, c1 = 's';
  210. string d2, d1 = "asdASDzxcZXC";
  211. string res = testerSurrogate.PrimitiveParamsInOut (ref a1, out a2, ref b1, out b2, ref c1, out c2, ref d1, out d2);
  212. AssertEquals ("ReturnValue", "9876543-82437.83-s-asdASDzxcZXC@" + remoteDomId, res);
  213. AssertEquals ("a2", 12345678, a2);
  214. AssertEquals ("b2", 53455.345f, b2);
  215. AssertEquals ("c2", 'g', c2);
  216. AssertEquals ("d2", "sfARREG$5345DGDfgY7656gDFG>><<dasdasd", d2);
  217. AssertEquals ("a1", 65748392, a1);
  218. AssertEquals ("b1", 98395.654f, b1);
  219. AssertEquals ("c1", 'l', c1);
  220. AssertEquals ("d1", "aasbasbdyhasbduybo234243", d1);
  221. }
  222. public void RunTestComplexParams (IRemoteObject testerSurrogate)
  223. {
  224. ArrayList list = new ArrayList ();
  225. list.Add (new Complex (11,"first"));
  226. Complex c = new Complex (22,"second");
  227. Complex r = testerSurrogate.ComplexParams (list, c, "third");
  228. AssertNotNull ("ReturnValue is null", r);
  229. AssertNotNull ("ReturnValue.Child is null", r.Child);
  230. AssertNotNull ("ReturnValue.Child.Child is null", r.Child.Child);
  231. AssertEquals ("ReturnValue.Id", 33, r.Id);
  232. AssertEquals ("ReturnValue.Name", "third@"+remoteDomId, r.Name);
  233. AssertEquals ("ReturnValue.Child.Id", 22, r.Child.Id);
  234. AssertEquals ("ReturnValue.Child.Name", "second", r.Child.Name);
  235. AssertEquals ("ReturnValue.Child.Child.Id", 11, r.Child.Child.Id);
  236. AssertEquals ("ReturnValue.Child.Child.Name", "first", r.Child.Child.Name);
  237. }
  238. public void RunTestComplexParamsInOut (IRemoteObject testerSurrogate)
  239. {
  240. ArrayList list = new ArrayList ();
  241. list.Add (new Complex (11,"first"));
  242. list.Add (new Complex (22,"second"));
  243. byte[] bytes = new byte [100];
  244. for (byte n=0; n<100; n++) bytes[n] = n;
  245. StringBuilder sb = new StringBuilder ("hello from client");
  246. Complex c;
  247. Complex r = testerSurrogate.ComplexParamsInOut (ref list, out c, bytes, sb, "third");
  248. AssertNotNull ("ReturnValue is null", r);
  249. AssertNotNull ("c is null", c);
  250. AssertNotNull ("list is null", list);
  251. Assert ("Invalid list count", list.Count == 3);
  252. AssertNotNull ("list[0] is null", list[0]);
  253. AssertNotNull ("list[1] is null", list[1]);
  254. AssertNotNull ("list[2] is null", list[2]);
  255. AssertNotNull ("bytes is null", bytes);
  256. AssertNotNull ("sb is null", sb);
  257. AssertEquals ("ReturnValue.Id", 33, r.Id);
  258. AssertEquals ("ReturnValue.Name", "third@"+remoteDomId, r.Name);
  259. AssertEquals ("c.Id", 33, c.Id);
  260. AssertEquals ("c.Name", "third@"+remoteDomId, c.Name);
  261. AssertEquals ("list[2].Id", 33, ((Complex)list[2]).Id);
  262. AssertEquals ("list[2].Name", "third@"+remoteDomId, ((Complex)list[2]).Name);
  263. AssertEquals ("list[1].Id", 22, ((Complex)list[1]).Id);
  264. AssertEquals ("list[1].Name", "second", ((Complex)list[1]).Name);
  265. AssertEquals ("list[0].Id", 11, ((Complex)list[0]).Id);
  266. AssertEquals ("list[0].Name", "first", ((Complex)list[0]).Name);
  267. AssertEquals ("sb", "hello from client and from server", sb.ToString ());
  268. for (int n=0; n<100; n++)
  269. AssertEquals ("bytes["+n+"]", n+1, bytes[n]);
  270. }
  271. public void RunTestProcessContextData (IRemoteObject testerSurrogate)
  272. {
  273. CallContext.FreeNamedDataSlot ("clientData");
  274. CallContext.FreeNamedDataSlot ("serverData");
  275. CallContext.FreeNamedDataSlot ("mustNotPass");
  276. ContextData cdata = new ContextData ();
  277. cdata.data = "hi from client";
  278. cdata.id = 1123;
  279. CallContext.SetData ("clientData", cdata);
  280. CallContext.SetData ("mustNotPass", "more data");
  281. testerSurrogate.ProcessContextData ();
  282. cdata = CallContext.GetData ("clientData") as ContextData;
  283. AssertNotNull ("clientData is null", cdata);
  284. AssertEquals ("clientData.data", "hi from client", cdata.data);
  285. AssertEquals ("clientData.id", 1123, cdata.id);
  286. cdata = CallContext.GetData ("serverData") as ContextData;
  287. AssertNotNull ("serverData is null", cdata);
  288. AssertEquals ("serverData.data", "hi from server", cdata.data);
  289. AssertEquals ("serverData.id", 3211, cdata.id);
  290. string mdata = CallContext.GetData ("mustNotPass") as string;
  291. AssertNotNull ("mustNotPass is null", mdata);
  292. AssertEquals ("mustNotPass", "more data", mdata);
  293. }
  294. }
  295. //
  296. // The server running in the remote domain
  297. //
  298. class CallsDomainServer: MarshalByRefObject
  299. {
  300. IChannelReceiver ch;
  301. public string[] Start(ChannelManager cm)
  302. {
  303. try
  304. {
  305. ch = cm.CreateServerChannel ();
  306. ChannelServices.RegisterChannel ((IChannel)ch);
  307. RemotingConfiguration.RegisterWellKnownServiceType (typeof (RemoteObject), "test1", WellKnownObjectMode.SingleCall);
  308. RemotingConfiguration.RegisterWellKnownServiceType (typeof (RemoteObject), "test2", WellKnownObjectMode.SingleCall);
  309. RemotingConfiguration.RegisterWellKnownServiceType (typeof (RemoteObject), "test3", WellKnownObjectMode.SingleCall);
  310. string[] uris = new string[3];
  311. uris[0] = ch.GetUrlsForUri ("test1")[0];
  312. uris[1] = ch.GetUrlsForUri ("test2")[0];
  313. uris[2] = ch.GetUrlsForUri ("test3")[0];
  314. return uris;
  315. }
  316. catch (Exception ex)
  317. {
  318. Console.WriteLine (ex.ToString());
  319. throw;
  320. }
  321. }
  322. public void Stop ()
  323. {
  324. if (ch != null)
  325. ChannelServices.UnregisterChannel (ch);
  326. }
  327. public int GetDomId ()
  328. {
  329. return Thread.GetDomainID();
  330. }
  331. }
  332. [Serializable]
  333. public class ContextData : ILogicalThreadAffinative
  334. {
  335. public string data;
  336. public int id;
  337. }
  338. [Serializable]
  339. public abstract class ChannelManager
  340. {
  341. public abstract IChannelSender CreateClientChannel ();
  342. public abstract IChannelReceiver CreateServerChannel ();
  343. }
  344. //
  345. // Test interface
  346. //
  347. public interface IRemoteObject
  348. {
  349. int Simple ();
  350. string PrimitiveParams (int a, uint b, char c, string d);
  351. string PrimitiveParamsInOut (ref int a1, out int a2, ref float b1, out float b2, ref char c1, out char c2, ref string d1, out string d2);
  352. Complex ComplexParams (ArrayList a, Complex b, string c);
  353. Complex ComplexParamsInOut (ref ArrayList a, out Complex b, [In,Out] byte[] bytes, [In,Out] StringBuilder sb, string c);
  354. void ProcessContextData ();
  355. }
  356. // Base classes for tester surrogates
  357. public abstract class InstanceSurrogate : IRemoteObject
  358. {
  359. public RemoteObject RemoteObject;
  360. public abstract int Simple ();
  361. public abstract string PrimitiveParams (int a, uint b, char c, string d);
  362. public abstract string PrimitiveParamsInOut (ref int a1, out int a2, ref float b1, out float b2, ref char c1, out char c2, ref string d1, out string d2);
  363. public abstract Complex ComplexParams (ArrayList a, Complex b, string c);
  364. public abstract Complex ComplexParamsInOut (ref ArrayList a, out Complex b, [In,Out] byte[] bytes, [In,Out] StringBuilder sb, string c);
  365. public abstract void ProcessContextData ();
  366. }
  367. public abstract class AbstractSurrogate : IRemoteObject
  368. {
  369. public AbstractRemoteObject RemoteObject;
  370. public abstract int Simple ();
  371. public abstract string PrimitiveParams (int a, uint b, char c, string d);
  372. public abstract string PrimitiveParamsInOut (ref int a1, out int a2, ref float b1, out float b2, ref char c1, out char c2, ref string d1, out string d2);
  373. public abstract Complex ComplexParams (ArrayList a, Complex b, string c);
  374. public abstract Complex ComplexParamsInOut (ref ArrayList a, out Complex b, [In,Out] byte[] bytes, [In,Out] StringBuilder sb, string c);
  375. public abstract void ProcessContextData ();
  376. }
  377. public abstract class InterfaceSurrogate : IRemoteObject
  378. {
  379. public IRemoteObject RemoteObject;
  380. public abstract int Simple ();
  381. public abstract string PrimitiveParams (int a, uint b, char c, string d);
  382. public abstract string PrimitiveParamsInOut (ref int a1, out int a2, ref float b1, out float b2, ref char c1, out char c2, ref string d1, out string d2);
  383. public abstract Complex ComplexParams (ArrayList a, Complex b, string c);
  384. public abstract Complex ComplexParamsInOut (ref ArrayList a, out Complex b, [In,Out] byte[] bytes, [In,Out] StringBuilder sb, string c);
  385. public abstract void ProcessContextData ();
  386. }
  387. //
  388. // Test abstract base class
  389. //
  390. public abstract class AbstractRemoteObject : MarshalByRefObject
  391. {
  392. public abstract int Simple ();
  393. public abstract string PrimitiveParams (int a, uint b, char c, string d);
  394. public abstract string PrimitiveParamsInOut (ref int a1, out int a2, ref float b1, out float b2, ref char c1, out char c2, ref string d1, out string d2);
  395. public abstract Complex ComplexParams (ArrayList a, Complex b, string c);
  396. public abstract Complex ComplexParamsInOut (ref ArrayList a, out Complex b, [In,Out] byte[] bytes, [In,Out] StringBuilder sb, string c);
  397. public abstract void ProcessContextData ();
  398. }
  399. //
  400. // Test class
  401. //
  402. public class RemoteObject : AbstractRemoteObject, IRemoteObject
  403. {
  404. public override int Simple ()
  405. {
  406. return 130772 + Thread.GetDomainID();
  407. }
  408. public override string PrimitiveParams (int a, uint b, char c, string d)
  409. {
  410. return "" + a + "-" + b + "-" + c + "-" + d + "@" + Thread.GetDomainID();
  411. }
  412. public override string PrimitiveParamsInOut (ref int a1, out int a2, ref float b1, out float b2, ref char c1, out char c2, ref string d1, out string d2)
  413. {
  414. string res = "" + a1 + "-" + b1.ToString(CultureInfo.InvariantCulture) + "-" + c1 + "-" + d1 + "@" + Thread.GetDomainID();
  415. a2 = 12345678;
  416. b2 = 53455.345f;
  417. c2 = 'g';
  418. d2 = "sfARREG$5345DGDfgY7656gDFG>><<dasdasd";
  419. a1 = 65748392;
  420. b1 = 98395.654f;
  421. c1 = 'l';
  422. d1 = "aasbasbdyhasbduybo234243";
  423. return res;
  424. }
  425. public override Complex ComplexParams (ArrayList a, Complex b, string c)
  426. {
  427. Complex cp = new Complex (33,c+ "@" + Thread.GetDomainID());
  428. cp.Child = b;
  429. cp.Child.Child = (Complex)a[0];
  430. return cp;
  431. }
  432. public override Complex ComplexParamsInOut (ref ArrayList a, out Complex b, [In,Out] byte[] bytes, [In,Out] StringBuilder sb, string c)
  433. {
  434. b = new Complex (33,c+ "@" + Thread.GetDomainID());
  435. a.Add (b);
  436. for (byte n=0; n<100; n++) bytes[n] = (byte)(bytes[n] + 1);
  437. sb.Append (" and from server");
  438. return b;
  439. }
  440. public override void ProcessContextData ()
  441. {
  442. ContextData cdata = CallContext.GetData ("clientData") as ContextData;
  443. if (cdata == null)
  444. throw new Exception ("server: clientData is null");
  445. if (cdata.data != "hi from client" || cdata.id != 1123)
  446. throw new Exception ("server: clientData is not valid");
  447. cdata = new ContextData ();
  448. cdata.data = "hi from server";
  449. cdata.id = 3211;
  450. CallContext.SetData ("serverData", cdata);
  451. string mdata = CallContext.GetData ("mustNotPass") as string;
  452. if (mdata != null) throw new Exception ("mustNotPass is not null");
  453. }
  454. }
  455. [Serializable]
  456. public class Complex
  457. {
  458. public Complex (int id, string name)
  459. {
  460. Id = id;
  461. Name = name;
  462. }
  463. public string Name;
  464. public int Id;
  465. public Complex Child;
  466. }
  467. }