BaseCalls.cs 16 KB

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