RemotingServicesTest.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587
  1. //
  2. // System.Runtime.Remoting.RemotingServices NUnit V2.0 test class
  3. //
  4. // Author Jean-Marc ANDRE ([email protected])
  5. //
  6. // ToDo: I didn't write test functions for the method not yep
  7. // implemented by Mono
  8. using System;
  9. using System.Collections;
  10. using NUnit.Framework;
  11. using System.Reflection;
  12. using System.Runtime.Remoting;
  13. using System.Threading;
  14. using System.Runtime.Remoting.Activation;
  15. using System.Runtime.Remoting.Messaging;
  16. using System.Runtime.Remoting.Proxies;
  17. using System.Runtime.Remoting.Channels;
  18. using System.Runtime.Remoting.Channels.Tcp;
  19. namespace MonoTests.System.Runtime.Remoting.RemotingServicesInternal
  20. {
  21. // We need our own proxy to intercept messages to remote object
  22. // and forward them using RemotingServices.ExecuteMessage
  23. public class MyProxy: RealProxy
  24. {
  25. MarshalByRefObject target;
  26. IMessageSink _sink;
  27. MethodBase _mthBase;
  28. bool methodOverloaded = false;
  29. public MethodBase MthBase
  30. {
  31. get{ return _mthBase;}
  32. }
  33. public bool IsMethodOverloaded
  34. {
  35. get{return methodOverloaded;}
  36. }
  37. public MyProxy(Type serverType, MarshalByRefObject target): base(serverType)
  38. {
  39. this.target = target;
  40. IChannel[] registeredChannels = ChannelServices.RegisteredChannels;
  41. string ObjectURI;
  42. // A new IMessageSink chain has to be created
  43. // since the RemotingServices.GetEnvoyChainForProxy() is not yet
  44. // implemented.
  45. foreach(IChannel channel in registeredChannels)
  46. {
  47. IChannelSender channelSender = channel as IChannelSender;
  48. if(channelSender != null)
  49. {
  50. _sink = (IMessageSink) channelSender.CreateMessageSink(RemotingServices.GetObjectUri(target), null, out ObjectURI);
  51. }
  52. }
  53. }
  54. // Messages will be intercepted here and redirected
  55. // to another object.
  56. public override IMessage Invoke(IMessage msg)
  57. {
  58. try
  59. {
  60. if(msg is IConstructionCallMessage)
  61. {
  62. IActivator remActivator = (IActivator) RemotingServices.Connect(typeof(IActivator), "tcp://localhost:1234/RemoteActivationService.rem");
  63. IConstructionReturnMessage crm = remActivator.Activate((IConstructionCallMessage)msg);
  64. return crm;
  65. }
  66. else
  67. {
  68. methodOverloaded = RemotingServices.IsMethodOverloaded((IMethodMessage)msg);
  69. _mthBase = RemotingServices.GetMethodBaseFromMethodMessage((IMethodMessage)msg);
  70. MethodCallMessageWrapper mcm = new MethodCallMessageWrapper((IMethodCallMessage) msg);
  71. mcm.Uri = RemotingServices.GetObjectUri((MarshalByRefObject)target);
  72. MarshalByRefObject objRem = (MarshalByRefObject)Activator.CreateInstance(GetProxiedType());
  73. RemotingServices.ExecuteMessage((MarshalByRefObject)objRem, (IMethodCallMessage)msg);
  74. IMessage rtnMsg = null;
  75. try
  76. {
  77. rtnMsg = _sink.SyncProcessMessage(msg);
  78. }
  79. catch(Exception e)
  80. {
  81. Console.WriteLine(e.Message);
  82. }
  83. Console.WriteLine ("RR:" + rtnMsg);
  84. return rtnMsg;
  85. }
  86. }
  87. catch (Exception ex)
  88. {
  89. Console.WriteLine (ex);
  90. return null;
  91. }
  92. }
  93. } // end MyProxy
  94. // This class is used to create "CAO"
  95. public class MarshalObjectFactory: MarshalByRefObject
  96. {
  97. public MarshalObject GetNewMarshalObject()
  98. {
  99. return new MarshalObject();
  100. }
  101. }
  102. // A class used by the tests
  103. public class MarshalObject: ContextBoundObject
  104. {
  105. public MarshalObject()
  106. {
  107. }
  108. public MarshalObject(int id, string uri)
  109. {
  110. this.id = id;
  111. this.uri = uri;
  112. }
  113. public int Id
  114. {
  115. get{return id;}
  116. set{id = value;}
  117. }
  118. public string Uri
  119. {
  120. get{return uri;}
  121. }
  122. public void Method1()
  123. {
  124. _called++;
  125. methodOneWay = RemotingServices.IsOneWay(MethodBase.GetCurrentMethod());
  126. }
  127. public void Method2()
  128. {
  129. methodOneWay = RemotingServices.IsOneWay(MethodBase.GetCurrentMethod());
  130. }
  131. public void Method2(int i)
  132. {
  133. methodOneWay = RemotingServices.IsOneWay(MethodBase.GetCurrentMethod());
  134. }
  135. [OneWay()]
  136. public void Method3()
  137. {
  138. methodOneWay = RemotingServices.IsOneWay(MethodBase.GetCurrentMethod());
  139. }
  140. public static int Called
  141. {
  142. get{return _called;}
  143. }
  144. public static bool IsMethodOneWay
  145. {
  146. get{return methodOneWay;}
  147. }
  148. private static int _called;
  149. private int id = 0;
  150. private string uri;
  151. private static bool methodOneWay = false;
  152. }
  153. // Another class used by the tests
  154. public class DerivedMarshalObject: MarshalObject
  155. {
  156. public DerivedMarshalObject(){}
  157. public DerivedMarshalObject(int id, string uri): base(id, uri) {}
  158. }
  159. interface A
  160. {
  161. }
  162. interface B: A
  163. {
  164. }
  165. public class CC: MarshalByRefObject
  166. {
  167. }
  168. public class DD: MarshalByRefObject
  169. {
  170. }
  171. } // namespace MonoTests.System.Runtime.Remoting.RemotingServicesInternal
  172. namespace MonoTests.Remoting
  173. {
  174. using MonoTests.System.Runtime.Remoting.RemotingServicesInternal;
  175. // The main test class
  176. [TestFixture]
  177. public class RemotingServicesTest : Assertion
  178. {
  179. private static int MarshalObjectId = 0;
  180. public RemotingServicesTest()
  181. {
  182. MarshalObjectId = 0;
  183. }
  184. // Helper function that create a new
  185. // MarshalObject with an unique ID
  186. private static MarshalObject NewMarshalObject()
  187. {
  188. string uri = "MonoTests.System.Runtime.Remoting.RemotingServicesTest.MarshalObject" + MarshalObjectId.ToString();
  189. MarshalObject objMarshal = new MarshalObject(MarshalObjectId, uri);
  190. MarshalObjectId++;
  191. return objMarshal;
  192. }
  193. // Another helper function
  194. private DerivedMarshalObject NewDerivedMarshalObject()
  195. {
  196. string uri = "MonoTests.System.Runtime.Remoting.RemotingServicesTest.DerivedMarshalObject" + MarshalObjectId.ToString();
  197. DerivedMarshalObject objMarshal = new DerivedMarshalObject(MarshalObjectId, uri);
  198. MarshalObjectId++;
  199. return objMarshal;
  200. }
  201. // The two folling method test RemotingServices.Marshal()
  202. [Test]
  203. public void Marshal1()
  204. {
  205. MarshalObject objMarshal = NewMarshalObject();
  206. ObjRef objRef = RemotingServices.Marshal(objMarshal);
  207. Assert("#A01", objRef.URI != null);
  208. MarshalObject objRem = (MarshalObject) RemotingServices.Unmarshal(objRef);
  209. AssertEquals("#A02", objMarshal.Id, objRem.Id);
  210. objRem.Id = 2;
  211. AssertEquals("#A03", objMarshal.Id, objRem.Id);
  212. // TODO: uncomment when RemotingServices.Disconnect is implemented
  213. //RemotingServices.Disconnect(objMarshal);
  214. objMarshal = NewMarshalObject();
  215. objRef = RemotingServices.Marshal(objMarshal, objMarshal.Uri);
  216. Assert("#A04", objRef.URI.EndsWith(objMarshal.Uri));
  217. // TODO: uncomment when RemotingServices.Disconnect is implemented
  218. //RemotingServices.Disconnect(objMarshal);
  219. }
  220. [Test]
  221. public void Marshal2()
  222. {
  223. DerivedMarshalObject derivedObjMarshal = NewDerivedMarshalObject();
  224. ObjRef objRef = RemotingServices.Marshal(derivedObjMarshal, derivedObjMarshal.Uri, typeof(MarshalObject));
  225. // Check that the type of the marshaled object is MarshalObject
  226. Assert("#A05", objRef.TypeInfo.TypeName.StartsWith((typeof(MarshalObject)).ToString()));
  227. // TODO: uncomment when RemotingServices.Disconnect is implemented
  228. //RemotingServices.Disconnect(derivedObjMarshal);
  229. }
  230. // Tests RemotingServices.GetObjectUri()
  231. [Test]
  232. public void GetObjectUri()
  233. {
  234. MarshalObject objMarshal = NewMarshalObject();
  235. Assert("#A06", RemotingServices.GetObjectUri(objMarshal) == null);
  236. RemotingServices.Marshal(objMarshal);
  237. Assert("#A07", RemotingServices.GetObjectUri(objMarshal) != null);
  238. // TODO: uncomment when RemotingServices.Disconnect is implemented
  239. //RemotingServices.Disconnect(objMarshal);
  240. }
  241. // Tests RemotingServices.Connect
  242. [Test]
  243. public void Connect()
  244. {
  245. MarshalObject objMarshal = NewMarshalObject();
  246. IDictionary props = new Hashtable();
  247. props["name"] = objMarshal.Uri;
  248. props["port"] = 1236;
  249. TcpChannel chn = new TcpChannel(props, null, null);
  250. ChannelServices.RegisterChannel(chn);
  251. RemotingServices.Marshal(objMarshal,objMarshal.Uri);
  252. MarshalObject objRem = (MarshalObject) RemotingServices.Connect(typeof(MarshalObject), "tcp://localhost:1236/" + objMarshal.Uri);
  253. Assert("#A08", RemotingServices.IsTransparentProxy(objRem));
  254. ChannelServices.UnregisterChannel(chn);
  255. RemotingServices.Disconnect(objMarshal);
  256. }
  257. // Tests RemotingServices.Marshal()
  258. [Test]
  259. [ExpectedException(typeof(RemotingException))]
  260. public void MarshalThrowException()
  261. {
  262. MarshalObject objMarshal = NewMarshalObject();
  263. IDictionary props = new Hashtable();
  264. props["name"] = objMarshal.Uri;
  265. props["port"] = 1237;
  266. TcpChannel chn = new TcpChannel(props, null, null);
  267. ChannelServices.RegisterChannel(chn);
  268. RemotingServices.Marshal(objMarshal,objMarshal.Uri);
  269. MarshalObject objRem = (MarshalObject) RemotingServices.Connect(typeof(MarshalObject), "tcp://localhost:1237/" + objMarshal.Uri);
  270. // This line sould throw a RemotingException
  271. // It is forbidden to export an object which is not
  272. // a real object
  273. try
  274. {
  275. RemotingServices.Marshal(objRem, objMarshal.Uri);
  276. }
  277. catch(Exception e)
  278. {
  279. ChannelServices.UnregisterChannel(chn);
  280. // TODO: uncomment when RemotingServices.Disconnect is implemented
  281. //RemotingServices.Disconnect(objMarshal);
  282. throw e;
  283. }
  284. }
  285. // Tests RemotingServices.ExecuteMessage()
  286. // also tests GetMethodBaseFromMessage()
  287. // IsMethodOverloaded()
  288. [Test]
  289. public void ExecuteMessage()
  290. {
  291. TcpChannel chn = null;
  292. try
  293. {
  294. chn = new TcpChannel(1235);
  295. ChannelServices.RegisterChannel(chn);
  296. MarshalObject objMarshal = NewMarshalObject();
  297. RemotingConfiguration.RegisterWellKnownServiceType(typeof(MarshalObject), objMarshal.Uri, WellKnownObjectMode.SingleCall);
  298. // use a proxy to catch the Message
  299. MyProxy proxy = new MyProxy(typeof(MarshalObject), (MarshalObject) Activator.GetObject(typeof(MarshalObject), "tcp://localhost:1235/" + objMarshal.Uri));
  300. MarshalObject objRem = (MarshalObject) proxy.GetTransparentProxy();
  301. objRem.Method1();
  302. // Tests RemotingServices.GetMethodBaseFromMethodMessage()
  303. AssertEquals("#A09","Method1",proxy.MthBase.Name);
  304. Assert("#A09.1", !proxy.IsMethodOverloaded);
  305. objRem.Method2();
  306. Assert("#A09.2", proxy.IsMethodOverloaded);
  307. // Tests RemotingServices.ExecuteMessage();
  308. // If ExecuteMessage does it job well, Method1 should be called 2 times
  309. AssertEquals("#A10", 2, MarshalObject.Called);
  310. }
  311. finally
  312. {
  313. if(chn != null) ChannelServices.UnregisterChannel(chn);
  314. }
  315. }
  316. // Tests the IsOneWay method
  317. [Test]
  318. public void IsOneWay()
  319. {
  320. TcpChannel chn = null;
  321. try
  322. {
  323. chn = new TcpChannel(1238);
  324. ChannelServices.RegisterChannel(chn);
  325. RemotingConfiguration.RegisterWellKnownServiceType(typeof(MarshalObject), "MarshalObject.rem", WellKnownObjectMode.Singleton);
  326. MarshalObject objRem = (MarshalObject) Activator.GetObject(typeof(MarshalObject), "tcp://localhost:1238/MarshalObject.rem");
  327. Assert("#A10.1", RemotingServices.IsTransparentProxy(objRem));
  328. objRem.Method1();
  329. Thread.Sleep(20);
  330. Assert("#A10.2", !MarshalObject.IsMethodOneWay);
  331. objRem.Method3();
  332. Thread.Sleep(20);
  333. Assert("#A10.3", MarshalObject.IsMethodOneWay);
  334. }
  335. finally
  336. {
  337. if(chn != null) ChannelServices.UnregisterChannel(chn);
  338. }
  339. }
  340. [Test]
  341. public void GetObjRefForProxy()
  342. {
  343. TcpChannel chn = null;
  344. try
  345. {
  346. chn = new TcpChannel(1239);
  347. ChannelServices.RegisterChannel(chn);
  348. // Register le factory as a SAO
  349. RemotingConfiguration.RegisterWellKnownServiceType(typeof(MarshalObjectFactory), "MonoTests.System.Runtime.Remoting.RemotingServicesTest.Factory.soap", WellKnownObjectMode.Singleton);
  350. MarshalObjectFactory objFactory = (MarshalObjectFactory) Activator.GetObject(typeof(MarshalObjectFactory), "tcp://localhost:1239/MonoTests.System.Runtime.Remoting.RemotingServicesTest.Factory.soap");
  351. // Get a new "CAO"
  352. MarshalObject objRem = objFactory.GetNewMarshalObject();
  353. ObjRef objRefRem = RemotingServices.GetObjRefForProxy((MarshalByRefObject)objRem);
  354. Assert("#A11", objRefRem != null);
  355. }
  356. finally
  357. {
  358. if(chn != null) ChannelServices.UnregisterChannel(chn);
  359. }
  360. }
  361. // Tests GetRealProxy
  362. [Test]
  363. public void GetRealProxy()
  364. {
  365. TcpChannel chn = null;
  366. try
  367. {
  368. chn = new TcpChannel(1241);
  369. ChannelServices.RegisterChannel(chn);
  370. RemotingConfiguration.RegisterWellKnownServiceType(typeof(MarshalObject), "MonoTests.System.Runtime.Remoting.RemotingServicesTest.MarshalObject.soap", WellKnownObjectMode.Singleton);
  371. MyProxy proxy = new MyProxy(typeof(MarshalObject), (MarshalByRefObject)Activator.GetObject(typeof(MarshalObject), "tcp://localhost:1241/MonoTests.System.Runtime.Remoting.RemotingServicesTest.MarshalObject.soap"));
  372. MarshalObject objRem = (MarshalObject) proxy.GetTransparentProxy();
  373. RealProxy rp = RemotingServices.GetRealProxy(objRem);
  374. Assert("#A12", rp != null);
  375. AssertEquals("#A13", "MonoTests.System.Runtime.Remoting.RemotingServicesInternal.MyProxy", rp.GetType().ToString());
  376. }
  377. finally
  378. {
  379. if(chn != null) ChannelServices.UnregisterChannel(chn);
  380. }
  381. }
  382. // Tests SetObjectUriForMarshal()
  383. [Test]
  384. public void SetObjectUriForMarshal()
  385. {
  386. TcpChannel chn = null;
  387. try
  388. {
  389. chn = new TcpChannel(1242);
  390. ChannelServices.RegisterChannel(chn);
  391. MarshalObject objRem = NewMarshalObject();
  392. RemotingServices.SetObjectUriForMarshal(objRem, objRem.Uri);
  393. RemotingServices.Marshal(objRem);
  394. objRem = (MarshalObject) Activator.GetObject(typeof(MarshalObject), "tcp://localhost:1242/"+objRem.Uri);
  395. Assert("#A14", objRem != null);
  396. }
  397. finally
  398. {
  399. if(chn != null) ChannelServices.UnregisterChannel(chn);
  400. }
  401. }
  402. // Tests GetServeurTypeForUri()
  403. [Test]
  404. public void GetServeurTypeForUri()
  405. {
  406. TcpChannel chn = null;
  407. Type type = typeof(MarshalObject);
  408. try
  409. {
  410. chn = new TcpChannel(1243);
  411. ChannelServices.RegisterChannel(chn);
  412. MarshalObject objRem = NewMarshalObject();
  413. RemotingServices.SetObjectUriForMarshal(objRem, objRem.Uri);
  414. RemotingServices.Marshal(objRem);
  415. Type typeRem = RemotingServices.GetServerTypeForUri(RemotingServices.GetObjectUri(objRem));
  416. AssertEquals("#A15", type, typeRem);
  417. }
  418. finally
  419. {
  420. if(chn != null) ChannelServices.UnregisterChannel(chn);
  421. }
  422. }
  423. // Tests IsObjectOutOfDomain
  424. // Tests IsObjectOutOfContext
  425. [Test]
  426. public void IsObjectOutOf()
  427. {
  428. TcpChannel chn = null;
  429. try
  430. {
  431. chn = new TcpChannel(1245);
  432. ChannelServices.RegisterChannel(chn);
  433. RemotingConfiguration.RegisterWellKnownServiceType(typeof(MarshalObject), "MarshalObject2.rem", WellKnownObjectMode.Singleton);
  434. MarshalObject objRem = (MarshalObject) Activator.GetObject(typeof(MarshalObject), "tcp://localhost:1245/MarshalObject2.rem");
  435. Assert("#A16", RemotingServices.IsObjectOutOfAppDomain(objRem));
  436. Assert("#A17", RemotingServices.IsObjectOutOfContext(objRem));
  437. MarshalObject objMarshal = new MarshalObject();
  438. Assert("#A18", !RemotingServices.IsObjectOutOfAppDomain(objMarshal));
  439. Assert("#A19", !RemotingServices.IsObjectOutOfContext(objMarshal));
  440. }
  441. finally
  442. {
  443. ChannelServices.UnregisterChannel(chn);
  444. }
  445. }
  446. [Test]
  447. public void ConnectProxyCast ()
  448. {
  449. object o;
  450. RemotingConfiguration.Configure (null);
  451. o = RemotingServices.Connect (typeof(MarshalByRefObject), "tcp://localhost:3434/ff1.rem");
  452. Assert ("#m1", o is DD);
  453. Assert ("#m2", o is A);
  454. Assert ("#m3", o is B);
  455. Assert ("#m4", !(o is CC));
  456. o = RemotingServices.Connect (typeof(A), "tcp://localhost:3434/ff3.rem");
  457. Assert ("#a1", o is DD);
  458. Assert ("#a2", o is A);
  459. Assert ("#a3", o is B);
  460. Assert ("#a4", !(o is CC));
  461. o = RemotingServices.Connect (typeof(DD), "tcp://localhost:3434/ff4.rem");
  462. Assert ("#d1", o is DD);
  463. Assert ("#d2", o is A);
  464. Assert ("#d3", o is B);
  465. Assert ("#d4", !(o is CC));
  466. o = RemotingServices.Connect (typeof(CC), "tcp://localhost:3434/ff5.rem");
  467. Assert ("#c1", !(o is DD));
  468. Assert ("#c2", o is A);
  469. Assert ("#c3", o is B);
  470. Assert ("#c4", o is CC);
  471. }
  472. } // end class RemotingServicesTest
  473. } // end of namespace MonoTests.Remoting