ScenePresenceAgentTests.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396
  1. /*
  2. * Copyright (c) Contributors, http://opensimulator.org/
  3. * See CONTRIBUTORS.TXT for a full list of copyright holders.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. * * Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions and the following disclaimer.
  9. * * Redistributions in binary form must reproduce the above copyright
  10. * notice, this list of conditions and the following disclaimer in the
  11. * documentation and/or other materials provided with the distribution.
  12. * * Neither the name of the OpenSimulator Project nor the
  13. * names of its contributors may be used to endorse or promote products
  14. * derived from this software without specific prior written permission.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY
  17. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  18. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  19. * DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY
  20. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  21. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  22. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  23. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  24. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  25. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. using System;
  28. using System.Collections.Generic;
  29. using System.Reflection;
  30. using System.Text;
  31. using System.Threading;
  32. using System.Timers;
  33. using Timer = System.Timers.Timer;
  34. using Nini.Config;
  35. using NUnit.Framework;
  36. using OpenMetaverse;
  37. using OpenSim.Framework;
  38. using OpenSim.Framework.Communications;
  39. using OpenSim.Region.Framework.Scenes;
  40. using OpenSim.Region.Framework.Interfaces;
  41. using OpenSim.Region.ClientStack.Linden;
  42. using OpenSim.Region.CoreModules.Framework.EntityTransfer;
  43. using OpenSim.Region.CoreModules.World.Serialiser;
  44. using OpenSim.Region.CoreModules.ServiceConnectorsOut.Simulation;
  45. using OpenSim.Tests.Common;
  46. using OpenSim.Tests.Common.Mock;
  47. using GridRegion = OpenSim.Services.Interfaces.GridRegion;
  48. namespace OpenSim.Region.Framework.Scenes.Tests
  49. {
  50. /// <summary>
  51. /// Scene presence tests
  52. /// </summary>
  53. [TestFixture]
  54. public class ScenePresenceAgentTests : OpenSimTestCase
  55. {
  56. // public Scene scene, scene2, scene3;
  57. // public UUID agent1, agent2, agent3;
  58. // public static Random random;
  59. // public ulong region1, region2, region3;
  60. // public AgentCircuitData acd1;
  61. // public TestClient testclient;
  62. // [TestFixtureSetUp]
  63. // public void Init()
  64. // {
  65. //// TestHelpers.InMethod();
  66. ////
  67. //// SceneHelpers sh = new SceneHelpers();
  68. ////
  69. //// scene = sh.SetupScene("Neighbour x", UUID.Random(), 1000, 1000);
  70. //// scene2 = sh.SetupScene("Neighbour x+1", UUID.Random(), 1001, 1000);
  71. //// scene3 = sh.SetupScene("Neighbour x-1", UUID.Random(), 999, 1000);
  72. ////
  73. //// ISharedRegionModule interregionComms = new LocalSimulationConnectorModule();
  74. //// interregionComms.Initialise(new IniConfigSource());
  75. //// interregionComms.PostInitialise();
  76. //// SceneHelpers.SetupSceneModules(scene, new IniConfigSource(), interregionComms);
  77. //// SceneHelpers.SetupSceneModules(scene2, new IniConfigSource(), interregionComms);
  78. //// SceneHelpers.SetupSceneModules(scene3, new IniConfigSource(), interregionComms);
  79. //
  80. //// agent1 = UUID.Random();
  81. //// agent2 = UUID.Random();
  82. //// agent3 = UUID.Random();
  83. //
  84. //// region1 = scene.RegionInfo.RegionHandle;
  85. //// region2 = scene2.RegionInfo.RegionHandle;
  86. //// region3 = scene3.RegionInfo.RegionHandle;
  87. // }
  88. [Test]
  89. public void TestCreateRootScenePresence()
  90. {
  91. TestHelpers.InMethod();
  92. // TestHelpers.EnableLogging();
  93. UUID spUuid = TestHelpers.ParseTail(0x1);
  94. TestScene scene = new SceneHelpers().SetupScene();
  95. SceneHelpers.AddScenePresence(scene, spUuid);
  96. Assert.That(scene.AuthenticateHandler.GetAgentCircuitData(spUuid), Is.Not.Null);
  97. Assert.That(scene.AuthenticateHandler.GetAgentCircuits().Count, Is.EqualTo(1));
  98. ScenePresence sp = scene.GetScenePresence(spUuid);
  99. Assert.That(sp, Is.Not.Null);
  100. Assert.That(sp.IsChildAgent, Is.False);
  101. Assert.That(sp.UUID, Is.EqualTo(spUuid));
  102. Assert.That(scene.GetScenePresences().Count, Is.EqualTo(1));
  103. }
  104. [Test]
  105. public void TestCreateDuplicateRootScenePresence()
  106. {
  107. TestHelpers.InMethod();
  108. // TestHelpers.EnableLogging();
  109. UUID spUuid = TestHelpers.ParseTail(0x1);
  110. TestScene scene = new SceneHelpers().SetupScene();
  111. SceneHelpers.AddScenePresence(scene, spUuid);
  112. SceneHelpers.AddScenePresence(scene, spUuid);
  113. Assert.That(scene.AuthenticateHandler.GetAgentCircuitData(spUuid), Is.Not.Null);
  114. Assert.That(scene.AuthenticateHandler.GetAgentCircuits().Count, Is.EqualTo(1));
  115. ScenePresence sp = scene.GetScenePresence(spUuid);
  116. Assert.That(sp, Is.Not.Null);
  117. Assert.That(sp.IsChildAgent, Is.False);
  118. Assert.That(sp.UUID, Is.EqualTo(spUuid));
  119. }
  120. [Test]
  121. public void TestCloseAgent()
  122. {
  123. TestHelpers.InMethod();
  124. // TestHelpers.EnableLogging();
  125. TestScene scene = new SceneHelpers().SetupScene();
  126. ScenePresence sp = SceneHelpers.AddScenePresence(scene, TestHelpers.ParseTail(0x1));
  127. scene.IncomingCloseAgent(sp.UUID, false);
  128. Assert.That(scene.GetScenePresence(sp.UUID), Is.Null);
  129. Assert.That(scene.AuthenticateHandler.GetAgentCircuitData(sp.UUID), Is.Null);
  130. Assert.That(scene.AuthenticateHandler.GetAgentCircuits().Count, Is.EqualTo(0));
  131. // TestHelpers.DisableLogging();
  132. }
  133. [Test]
  134. public void TestCreateChildScenePresence()
  135. {
  136. TestHelpers.InMethod();
  137. // log4net.Config.XmlConfigurator.Configure();
  138. LocalSimulationConnectorModule lsc = new LocalSimulationConnectorModule();
  139. IConfigSource configSource = new IniConfigSource();
  140. IConfig config = configSource.AddConfig("Modules");
  141. config.Set("SimulationServices", "LocalSimulationConnectorModule");
  142. SceneHelpers sceneHelpers = new SceneHelpers();
  143. TestScene scene = sceneHelpers.SetupScene();
  144. SceneHelpers.SetupSceneModules(scene, configSource, lsc);
  145. UUID agentId = TestHelpers.ParseTail(0x01);
  146. AgentCircuitData acd = SceneHelpers.GenerateAgentData(agentId);
  147. acd.child = true;
  148. GridRegion region = scene.GridService.GetRegionByName(UUID.Zero, scene.RegionInfo.RegionName);
  149. string reason;
  150. // *** This is the first stage, when a neighbouring region is told that a viewer is about to try and
  151. // establish a child scene presence. We pass in the circuit code that the client has to connect with ***
  152. // XXX: ViaLogin may not be correct here.
  153. scene.SimulationService.CreateAgent(region, acd, (uint)TeleportFlags.ViaLogin, out reason);
  154. Assert.That(scene.AuthenticateHandler.GetAgentCircuitData(agentId), Is.Not.Null);
  155. Assert.That(scene.AuthenticateHandler.GetAgentCircuits().Count, Is.EqualTo(1));
  156. // There's no scene presence yet since only an agent circuit has been established.
  157. Assert.That(scene.GetScenePresence(agentId), Is.Null);
  158. // *** This is the second stage, where the client established a child agent/scene presence using the
  159. // circuit code given to the scene in stage 1 ***
  160. TestClient client = new TestClient(acd, scene);
  161. scene.AddNewClient(client, PresenceType.User);
  162. Assert.That(scene.AuthenticateHandler.GetAgentCircuitData(agentId), Is.Not.Null);
  163. Assert.That(scene.AuthenticateHandler.GetAgentCircuits().Count, Is.EqualTo(1));
  164. ScenePresence sp = scene.GetScenePresence(agentId);
  165. Assert.That(sp, Is.Not.Null);
  166. Assert.That(sp.UUID, Is.EqualTo(agentId));
  167. Assert.That(sp.IsChildAgent, Is.True);
  168. }
  169. /// <summary>
  170. /// Test that if a root agent logs into a region, a child agent is also established in the neighbouring region
  171. /// </summary>
  172. /// <remarks>
  173. /// Please note that unlike the other tests here, this doesn't rely on anything set up in the instance fields.
  174. /// INCOMPLETE
  175. /// </remarks>
  176. [Test]
  177. public void TestChildAgentEstablishedInNeighbour()
  178. {
  179. TestHelpers.InMethod();
  180. // log4net.Config.XmlConfigurator.Configure();
  181. // UUID agent1Id = UUID.Parse("00000000-0000-0000-0000-000000000001");
  182. TestScene myScene1 = new SceneHelpers().SetupScene("Neighbour y", UUID.Random(), 1000, 1000);
  183. TestScene myScene2 = new SceneHelpers().SetupScene("Neighbour y + 1", UUID.Random(), 1001, 1000);
  184. IConfigSource configSource = new IniConfigSource();
  185. IConfig config = configSource.AddConfig("Startup");
  186. config.Set("serverside_object_permissions", true);
  187. config.Set("EventQueue", true);
  188. EntityTransferModule etm = new EntityTransferModule();
  189. EventQueueGetModule eqgm1 = new EventQueueGetModule();
  190. SceneHelpers.SetupSceneModules(myScene1, configSource, etm, eqgm1);
  191. EventQueueGetModule eqgm2 = new EventQueueGetModule();
  192. SceneHelpers.SetupSceneModules(myScene2, configSource, etm, eqgm2);
  193. // SceneHelpers.AddScenePresence(myScene1, agent1Id);
  194. // ScenePresence childPresence = myScene2.GetScenePresence(agent1);
  195. //
  196. // // TODO: Need to do a fair amount of work to allow synchronous establishment of child agents
  197. // Assert.That(childPresence, Is.Not.Null);
  198. // Assert.That(childPresence.IsChildAgent, Is.True);
  199. }
  200. // /// <summary>
  201. // /// Test adding a root agent to a scene. Doesn't yet actually complete crossing the agent into the scene.
  202. // /// </summary>
  203. // [Test]
  204. // public void T010_TestAddRootAgent()
  205. // {
  206. // TestHelpers.InMethod();
  207. //
  208. // string firstName = "testfirstname";
  209. //
  210. // AgentCircuitData agent = new AgentCircuitData();
  211. // agent.AgentID = agent1;
  212. // agent.firstname = firstName;
  213. // agent.lastname = "testlastname";
  214. // agent.SessionID = UUID.Random();
  215. // agent.SecureSessionID = UUID.Random();
  216. // agent.circuitcode = 123;
  217. // agent.BaseFolder = UUID.Zero;
  218. // agent.InventoryFolder = UUID.Zero;
  219. // agent.startpos = Vector3.Zero;
  220. // agent.CapsPath = GetRandomCapsObjectPath();
  221. // agent.ChildrenCapSeeds = new Dictionary<ulong, string>();
  222. // agent.child = true;
  223. //
  224. // scene.PresenceService.LoginAgent(agent.AgentID.ToString(), agent.SessionID, agent.SecureSessionID);
  225. //
  226. // string reason;
  227. // scene.NewUserConnection(agent, (uint)TeleportFlags.ViaLogin, out reason);
  228. // testclient = new TestClient(agent, scene);
  229. // scene.AddNewClient(testclient);
  230. //
  231. // ScenePresence presence = scene.GetScenePresence(agent1);
  232. //
  233. // Assert.That(presence, Is.Not.Null, "presence is null");
  234. // Assert.That(presence.Firstname, Is.EqualTo(firstName), "First name not same");
  235. // acd1 = agent;
  236. // }
  237. //
  238. // /// <summary>
  239. // /// Test removing an uncrossed root agent from a scene.
  240. // /// </summary>
  241. // [Test]
  242. // public void T011_TestRemoveRootAgent()
  243. // {
  244. // TestHelpers.InMethod();
  245. //
  246. // scene.RemoveClient(agent1);
  247. //
  248. // ScenePresence presence = scene.GetScenePresence(agent1);
  249. //
  250. // Assert.That(presence, Is.Null, "presence is not null");
  251. // }
  252. // I'm commenting this test because it does not represent
  253. // crossings. The Thread.Sleep's in here are not meaningful mocks,
  254. // and they sometimes fail in panda.
  255. // We need to talk in order to develop a test
  256. // that really tests region crossings. There are 3 async components,
  257. // but things are synchronous among them. So there should be
  258. // 3 threads in here.
  259. //[Test]
  260. // public void T021_TestCrossToNewRegion()
  261. // {
  262. // TestHelpers.InMethod();
  263. //
  264. // scene.RegisterRegionWithGrid();
  265. // scene2.RegisterRegionWithGrid();
  266. //
  267. // // Adding child agent to region 1001
  268. // string reason;
  269. // scene2.NewUserConnection(acd1,0, out reason);
  270. // scene2.AddNewClient(testclient, PresenceType.User);
  271. //
  272. // ScenePresence presence = scene.GetScenePresence(agent1);
  273. // presence.MakeRootAgent(new Vector3(0,unchecked(Constants.RegionSize-1),0), true);
  274. //
  275. // ScenePresence presence2 = scene2.GetScenePresence(agent1);
  276. //
  277. // // Adding neighbour region caps info to presence2
  278. //
  279. // string cap = presence.ControllingClient.RequestClientInfo().CapsPath;
  280. // presence2.AddNeighbourRegion(region1, cap);
  281. //
  282. // Assert.That(presence.IsChildAgent, Is.False, "Did not start root in origin region.");
  283. // Assert.That(presence2.IsChildAgent, Is.True, "Is not a child on destination region.");
  284. //
  285. // // Cross to x+1
  286. // presence.AbsolutePosition = new Vector3(Constants.RegionSize+1,3,100);
  287. // presence.Update();
  288. //
  289. // EventWaitHandle wh = new EventWaitHandle (false, EventResetMode.AutoReset, "Crossing");
  290. //
  291. // // Mimicking communication between client and server, by waiting OK from client
  292. // // sent by TestClient.CrossRegion call. Originally, this is network comm.
  293. // if (!wh.WaitOne(5000,false))
  294. // {
  295. // presence.Update();
  296. // if (!wh.WaitOne(8000,false))
  297. // throw new ArgumentException("1 - Timeout waiting for signal/variable.");
  298. // }
  299. //
  300. // // This is a TestClient specific method that fires OnCompleteMovementToRegion event, which
  301. // // would normally be fired after receiving the reply packet from comm. done on the last line.
  302. // testclient.CompleteMovement();
  303. //
  304. // // Crossings are asynchronous
  305. // int timer = 10;
  306. //
  307. // // Make sure cross hasn't already finished
  308. // if (!presence.IsInTransit && !presence.IsChildAgent)
  309. // {
  310. // // If not and not in transit yet, give it some more time
  311. // Thread.Sleep(5000);
  312. // }
  313. //
  314. // // Enough time, should at least be in transit by now.
  315. // while (presence.IsInTransit && timer > 0)
  316. // {
  317. // Thread.Sleep(1000);
  318. // timer-=1;
  319. // }
  320. //
  321. // Assert.That(timer,Is.GreaterThan(0),"Timed out waiting to cross 2->1.");
  322. // Assert.That(presence.IsChildAgent, Is.True, "Did not complete region cross as expected.");
  323. // Assert.That(presence2.IsChildAgent, Is.False, "Did not receive root status after receiving agent.");
  324. //
  325. // // Cross Back
  326. // presence2.AbsolutePosition = new Vector3(-10, 3, 100);
  327. // presence2.Update();
  328. //
  329. // if (!wh.WaitOne(5000,false))
  330. // {
  331. // presence2.Update();
  332. // if (!wh.WaitOne(8000,false))
  333. // throw new ArgumentException("2 - Timeout waiting for signal/variable.");
  334. // }
  335. // testclient.CompleteMovement();
  336. //
  337. // if (!presence2.IsInTransit && !presence2.IsChildAgent)
  338. // {
  339. // // If not and not in transit yet, give it some more time
  340. // Thread.Sleep(5000);
  341. // }
  342. //
  343. // // Enough time, should at least be in transit by now.
  344. // while (presence2.IsInTransit && timer > 0)
  345. // {
  346. // Thread.Sleep(1000);
  347. // timer-=1;
  348. // }
  349. //
  350. // Assert.That(timer,Is.GreaterThan(0),"Timed out waiting to cross 1->2.");
  351. // Assert.That(presence2.IsChildAgent, Is.True, "Did not return from region as expected.");
  352. // Assert.That(presence.IsChildAgent, Is.False, "Presence was not made root in old region again.");
  353. // }
  354. }
  355. }