SceneSetupHelpers.cs 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517
  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.Net;
  29. using System.Collections.Generic;
  30. using Nini.Config;
  31. using OpenMetaverse;
  32. using OpenSim.Framework;
  33. using OpenSim.Framework.Communications;
  34. using OpenSim.Framework.Console;
  35. using OpenSim.Framework.Servers;
  36. using OpenSim.Framework.Servers.HttpServer;
  37. using OpenSim.Region.Physics.Manager;
  38. using OpenSim.Region.Framework;
  39. using OpenSim.Region.Framework.Interfaces;
  40. using OpenSim.Region.Framework.Scenes;
  41. using OpenSim.Region.CoreModules.Agent.Capabilities;
  42. using OpenSim.Region.CoreModules.Avatar.Gods;
  43. using OpenSim.Region.CoreModules.ServiceConnectorsOut.Asset;
  44. using OpenSim.Region.CoreModules.ServiceConnectorsOut.Authentication;
  45. using OpenSim.Region.CoreModules.ServiceConnectorsOut.Inventory;
  46. using OpenSim.Region.CoreModules.ServiceConnectorsOut.Grid;
  47. using OpenSim.Region.CoreModules.ServiceConnectorsOut.UserAccounts;
  48. using OpenSim.Services.Interfaces;
  49. using OpenSim.Tests.Common.Mock;
  50. namespace OpenSim.Tests.Common.Setup
  51. {
  52. /// <summary>
  53. /// Helpers for setting up scenes.
  54. /// </summary>
  55. public class SceneSetupHelpers
  56. {
  57. // These static variables in order to allow regions to be linked by shared modules and same
  58. // CommunicationsManager.
  59. private static ISharedRegionModule m_assetService = null;
  60. // private static ISharedRegionModule m_authenticationService = null;
  61. private static ISharedRegionModule m_inventoryService = null;
  62. private static ISharedRegionModule m_gridService = null;
  63. private static ISharedRegionModule m_userAccountService = null;
  64. /// <summary>
  65. /// Set up a test scene
  66. /// </summary>
  67. ///
  68. /// Automatically starts service threads, as would the normal runtime.
  69. ///
  70. /// <returns></returns>
  71. public static TestScene SetupScene()
  72. {
  73. return SetupScene("");
  74. }
  75. /// <summary>
  76. /// Set up a test scene
  77. /// </summary>
  78. ///
  79. /// <param name="realServices">Starts real inventory and asset services, as opposed to mock ones, if true</param>
  80. /// <returns></returns>
  81. public static TestScene SetupScene(String realServices)
  82. {
  83. return SetupScene(
  84. "Unit test region", UUID.Random(), 1000, 1000, realServices);
  85. }
  86. // REFACTORING PROBLEM. No idea what the difference is with the previous one
  87. ///// <summary>
  88. ///// Set up a test scene
  89. ///// </summary>
  90. /////
  91. ///// <param name="realServices">Starts real inventory and asset services, as opposed to mock ones, if true</param>
  92. ///// <param name="cm">This should be the same if simulating two scenes within a standalone</param>
  93. ///// <returns></returns>
  94. //public static TestScene SetupScene(String realServices)
  95. //{
  96. // return SetupScene(
  97. // "Unit test region", UUID.Random(), 1000, 1000, "");
  98. //}
  99. /// <summary>
  100. /// Set up a test scene
  101. /// </summary>
  102. /// <param name="name">Name of the region</param>
  103. /// <param name="id">ID of the region</param>
  104. /// <param name="x">X co-ordinate of the region</param>
  105. /// <param name="y">Y co-ordinate of the region</param>
  106. /// <param name="cm">This should be the same if simulating two scenes within a standalone</param>
  107. /// <returns></returns>
  108. public static TestScene SetupScene(string name, UUID id, uint x, uint y)
  109. {
  110. return SetupScene(name, id, x, y,"");
  111. }
  112. /// <summary>
  113. /// Set up a scene. If it's more then one scene, use the same CommunicationsManager to link regions
  114. /// or a different, to get a brand new scene with new shared region modules.
  115. /// </summary>
  116. /// <param name="name">Name of the region</param>
  117. /// <param name="id">ID of the region</param>
  118. /// <param name="x">X co-ordinate of the region</param>
  119. /// <param name="y">Y co-ordinate of the region</param>
  120. /// <param name="cm">This should be the same if simulating two scenes within a standalone</param>
  121. /// <param name="realServices">Starts real inventory and asset services, as opposed to mock ones, if true</param>
  122. /// <returns></returns>
  123. public static TestScene SetupScene(
  124. string name, UUID id, uint x, uint y, String realServices)
  125. {
  126. bool newScene = false;
  127. Console.WriteLine("Setting up test scene {0}", name);
  128. // REFACTORING PROBLEM!
  129. //// If cm is the same as our last commsManager used, this means the tester wants to link
  130. //// regions. In this case, don't use the sameshared region modules and dont initialize them again.
  131. //// Also, no need to start another MainServer and MainConsole instance.
  132. //if (cm == null || cm != commsManager)
  133. //{
  134. // System.Console.WriteLine("Starting a brand new scene");
  135. // newScene = true;
  136. MainConsole.Instance = new MockConsole("TEST PROMPT");
  137. // MainServer.Instance = new BaseHttpServer(980);
  138. // commsManager = cm;
  139. //}
  140. // We must set up a console otherwise setup of some modules may fail
  141. RegionInfo regInfo = new RegionInfo(x, y, new IPEndPoint(IPAddress.Loopback, 9000), "127.0.0.1");
  142. regInfo.RegionName = name;
  143. regInfo.RegionID = id;
  144. AgentCircuitManager acm = new AgentCircuitManager();
  145. SceneCommunicationService scs = new SceneCommunicationService();
  146. StorageManager sm = new StorageManager("OpenSim.Data.Null.dll", "", "");
  147. IConfigSource configSource = new IniConfigSource();
  148. TestScene testScene = new TestScene(
  149. regInfo, acm, scs, sm, null, false, false, false, configSource, null);
  150. INonSharedRegionModule capsModule = new CapabilitiesModule();
  151. capsModule.Initialise(new IniConfigSource());
  152. testScene.AddRegionModule(capsModule.Name, capsModule);
  153. capsModule.AddRegion(testScene);
  154. IRegionModule godsModule = new GodsModule();
  155. godsModule.Initialise(testScene, new IniConfigSource());
  156. testScene.AddModule(godsModule.Name, godsModule);
  157. realServices = realServices.ToLower();
  158. // IConfigSource config = new IniConfigSource();
  159. // If we have a brand new scene, need to initialize shared region modules
  160. if ((m_assetService == null && m_inventoryService == null) || newScene)
  161. {
  162. if (realServices.Contains("asset"))
  163. StartAssetService(testScene, true);
  164. else
  165. StartAssetService(testScene, false);
  166. // For now, always started a 'real' authenication service
  167. StartAuthenticationService(testScene, true);
  168. if (realServices.Contains("inventory"))
  169. StartInventoryService(testScene, true);
  170. else
  171. StartInventoryService(testScene, false);
  172. if (realServices.Contains("grid"))
  173. StartGridService(testScene, true);
  174. StartUserAccountService(testScene);
  175. }
  176. // If not, make sure the shared module gets references to this new scene
  177. else
  178. {
  179. m_assetService.AddRegion(testScene);
  180. m_assetService.RegionLoaded(testScene);
  181. m_inventoryService.AddRegion(testScene);
  182. m_inventoryService.RegionLoaded(testScene);
  183. m_userAccountService.AddRegion(testScene);
  184. m_userAccountService.RegionLoaded(testScene);
  185. }
  186. m_inventoryService.PostInitialise();
  187. m_assetService.PostInitialise();
  188. m_userAccountService.PostInitialise();
  189. testScene.RegionInfo.EstateSettings.EstateOwner = UUID.Random();
  190. testScene.SetModuleInterfaces();
  191. testScene.LandChannel = new TestLandChannel(testScene);
  192. testScene.LoadWorldMap();
  193. PhysicsPluginManager physicsPluginManager = new PhysicsPluginManager();
  194. physicsPluginManager.LoadPluginsFromAssembly("Physics/OpenSim.Region.Physics.BasicPhysicsPlugin.dll");
  195. testScene.PhysicsScene
  196. = physicsPluginManager.GetPhysicsScene("basicphysics", "ZeroMesher", new IniConfigSource(), "test");
  197. // It's really not a good idea to use static variables as they carry over between tests, leading to
  198. // problems that are extremely hard to debug. Really, these static fields need to be eliminated -
  199. // tests using multiple regions that need to share modules need to find another solution.
  200. m_assetService = null;
  201. m_inventoryService = null;
  202. m_gridService = null;
  203. m_userAccountService = null;
  204. return testScene;
  205. }
  206. private static void StartAssetService(Scene testScene, bool real)
  207. {
  208. ISharedRegionModule assetService = new LocalAssetServicesConnector();
  209. IConfigSource config = new IniConfigSource();
  210. config.AddConfig("Modules");
  211. config.AddConfig("AssetService");
  212. config.Configs["Modules"].Set("AssetServices", "LocalAssetServicesConnector");
  213. if (real)
  214. config.Configs["AssetService"].Set("LocalServiceModule", "OpenSim.Services.AssetService.dll:AssetService");
  215. else
  216. config.Configs["AssetService"].Set("LocalServiceModule", "OpenSim.Tests.Common.dll:MockAssetService");
  217. config.Configs["AssetService"].Set("StorageProvider", "OpenSim.Tests.Common.dll");
  218. assetService.Initialise(config);
  219. assetService.AddRegion(testScene);
  220. assetService.RegionLoaded(testScene);
  221. testScene.AddRegionModule(assetService.Name, assetService);
  222. m_assetService = assetService;
  223. }
  224. private static void StartAuthenticationService(Scene testScene, bool real)
  225. {
  226. ISharedRegionModule service = new LocalAuthenticationServicesConnector();
  227. IConfigSource config = new IniConfigSource();
  228. config.AddConfig("Modules");
  229. config.AddConfig("AuthenticationService");
  230. config.Configs["Modules"].Set("AuthenticationServices", "LocalAuthenticationServicesConnector");
  231. if (real)
  232. config.Configs["AuthenticationService"].Set(
  233. "LocalServiceModule", "OpenSim.Services.AuthenticationService.dll:PasswordAuthenticationService");
  234. else
  235. config.Configs["AuthenticationService"].Set(
  236. "LocalServiceModule", "OpenSim.Tests.Common.dll:MockAuthenticationService");
  237. config.Configs["AuthenticationService"].Set("StorageProvider", "OpenSim.Data.Null.dll");
  238. service.Initialise(config);
  239. service.AddRegion(testScene);
  240. service.RegionLoaded(testScene);
  241. testScene.AddRegionModule(service.Name, service);
  242. //m_authenticationService = service;
  243. }
  244. private static void StartInventoryService(Scene testScene, bool real)
  245. {
  246. ISharedRegionModule inventoryService = new LocalInventoryServicesConnector();
  247. IConfigSource config = new IniConfigSource();
  248. config.AddConfig("Modules");
  249. config.AddConfig("InventoryService");
  250. config.Configs["Modules"].Set("InventoryServices", "LocalInventoryServicesConnector");
  251. if (real)
  252. config.Configs["InventoryService"].Set("LocalServiceModule", "OpenSim.Services.InventoryService.dll:InventoryService");
  253. else
  254. config.Configs["InventoryService"].Set("LocalServiceModule", "OpenSim.Tests.Common.dll:MockInventoryService");
  255. config.Configs["InventoryService"].Set("StorageProvider", "OpenSim.Tests.Common.dll");
  256. inventoryService.Initialise(config);
  257. inventoryService.AddRegion(testScene);
  258. inventoryService.RegionLoaded(testScene);
  259. testScene.AddRegionModule(inventoryService.Name, inventoryService);
  260. m_inventoryService = inventoryService;
  261. }
  262. private static void StartGridService(Scene testScene, bool real)
  263. {
  264. IConfigSource config = new IniConfigSource();
  265. config.AddConfig("Modules");
  266. config.AddConfig("GridService");
  267. config.Configs["Modules"].Set("GridServices", "LocalGridServicesConnector");
  268. config.Configs["GridService"].Set("StorageProvider", "OpenSim.Data.Null.dll:NullRegionData");
  269. if (real)
  270. config.Configs["GridService"].Set("LocalServiceModule", "OpenSim.Services.GridService.dll:GridService");
  271. if (m_gridService == null)
  272. {
  273. ISharedRegionModule gridService = new LocalGridServicesConnector();
  274. gridService.Initialise(config);
  275. m_gridService = gridService;
  276. }
  277. //else
  278. // config.Configs["GridService"].Set("LocalServiceModule", "OpenSim.Tests.Common.dll:TestGridService");
  279. m_gridService.AddRegion(testScene);
  280. m_gridService.RegionLoaded(testScene);
  281. //testScene.AddRegionModule(m_gridService.Name, m_gridService);
  282. }
  283. /// <summary>
  284. /// Start a user account service
  285. /// </summary>
  286. /// <param name="testScene"></param>
  287. private static void StartUserAccountService(Scene testScene)
  288. {
  289. IConfigSource config = new IniConfigSource();
  290. config.AddConfig("Modules");
  291. config.AddConfig("UserAccountService");
  292. config.Configs["Modules"].Set("UserAccountServices", "LocalUserAccountServicesConnector");
  293. config.Configs["UserAccountService"].Set("StorageProvider", "OpenSim.Data.Null.dll");
  294. config.Configs["UserAccountService"].Set(
  295. "LocalServiceModule", "OpenSim.Services.UserAccountService.dll:UserAccountService");
  296. if (m_userAccountService == null)
  297. {
  298. ISharedRegionModule userAccountService = new LocalUserAccountServicesConnector();
  299. userAccountService.Initialise(config);
  300. m_userAccountService = userAccountService;
  301. }
  302. m_userAccountService.AddRegion(testScene);
  303. m_userAccountService.RegionLoaded(testScene);
  304. testScene.AddRegionModule(m_userAccountService.Name, m_userAccountService);
  305. }
  306. /// <summary>
  307. /// Setup modules for a scene using their default settings.
  308. /// </summary>
  309. /// <param name="scene"></param>
  310. /// <param name="modules"></param>
  311. public static void SetupSceneModules(Scene scene, params object[] modules)
  312. {
  313. SetupSceneModules(scene, new IniConfigSource(), modules);
  314. }
  315. /// <summary>
  316. /// Setup modules for a scene.
  317. /// </summary>
  318. /// <param name="scene"></param>
  319. /// <param name="config"></param>
  320. /// <param name="modules"></param>
  321. public static void SetupSceneModules(Scene scene, IConfigSource config, params object[] modules)
  322. {
  323. List<IRegionModuleBase> newModules = new List<IRegionModuleBase>();
  324. foreach (object module in modules)
  325. {
  326. if (module is IRegionModule)
  327. {
  328. IRegionModule m = (IRegionModule)module;
  329. m.Initialise(scene, config);
  330. scene.AddModule(m.Name, m);
  331. m.PostInitialise();
  332. }
  333. else if (module is IRegionModuleBase)
  334. {
  335. // for the new system, everything has to be initialised first,
  336. // shared modules have to be post-initialised, then all get an AddRegion with the scene
  337. IRegionModuleBase m = (IRegionModuleBase)module;
  338. m.Initialise(config);
  339. newModules.Add(m);
  340. }
  341. }
  342. foreach (IRegionModuleBase module in newModules)
  343. {
  344. if (module is ISharedRegionModule) ((ISharedRegionModule)module).PostInitialise();
  345. }
  346. foreach (IRegionModuleBase module in newModules)
  347. {
  348. module.AddRegion(scene);
  349. module.RegionLoaded(scene);
  350. scene.AddRegionModule(module.Name, module);
  351. }
  352. scene.SetModuleInterfaces();
  353. }
  354. /// <summary>
  355. /// Generate some standard agent connection data.
  356. /// </summary>
  357. /// <param name="agentId"></param>
  358. /// <returns></returns>
  359. public static AgentCircuitData GenerateAgentData(UUID agentId)
  360. {
  361. string firstName = "testfirstname";
  362. AgentCircuitData agentData = new AgentCircuitData();
  363. agentData.AgentID = agentId;
  364. agentData.firstname = firstName;
  365. agentData.lastname = "testlastname";
  366. agentData.SessionID = UUID.Zero;
  367. agentData.SecureSessionID = UUID.Zero;
  368. agentData.circuitcode = 123;
  369. agentData.BaseFolder = UUID.Zero;
  370. agentData.InventoryFolder = UUID.Zero;
  371. agentData.startpos = Vector3.Zero;
  372. agentData.CapsPath = "http://wibble.com";
  373. return agentData;
  374. }
  375. /// <summary>
  376. /// Add a root agent where the details of the agent connection (apart from the id) are unimportant for the test
  377. /// </summary>
  378. /// <param name="scene"></param>
  379. /// <param name="agentId"></param>
  380. /// <returns></returns>
  381. public static TestClient AddRootAgent(Scene scene, UUID agentId)
  382. {
  383. return AddRootAgent(scene, GenerateAgentData(agentId));
  384. }
  385. /// <summary>
  386. /// Add a root agent.
  387. /// </summary>
  388. ///
  389. /// This function
  390. ///
  391. /// 1) Tells the scene that an agent is coming. Normally, the login service (local if standalone, from the
  392. /// userserver if grid) would give initial login data back to the client and separately tell the scene that the
  393. /// agent was coming.
  394. ///
  395. /// 2) Connects the agent with the scene
  396. ///
  397. /// This function performs actions equivalent with notifying the scene that an agent is
  398. /// coming and then actually connecting the agent to the scene. The one step missed out is the very first
  399. ///
  400. /// <param name="scene"></param>
  401. /// <param name="agentData"></param>
  402. /// <returns></returns>
  403. public static TestClient AddRootAgent(Scene scene, AgentCircuitData agentData)
  404. {
  405. string reason;
  406. // We emulate the proper login sequence here by doing things in three stages
  407. // Stage 1: simulate login by telling the scene to expect a new user connection
  408. scene.NewUserConnection(agentData, (uint)TeleportFlags.ViaLogin, out reason);
  409. // Stage 2: add the new client as a child agent to the scene
  410. TestClient client = new TestClient(agentData, scene);
  411. scene.AddNewClient(client);
  412. // Stage 3: Invoke agent crossing, which converts the child agent into a root agent (with appearance,
  413. // inventory, etc.)
  414. //scene.AgentCrossing(agentData.AgentID, new Vector3(90, 90, 90), false); OBSOLETE
  415. ScenePresence scp = scene.GetScenePresence(agentData.AgentID);
  416. scp.MakeRootAgent(new Vector3(90,90,90), true);
  417. return client;
  418. }
  419. /// <summary>
  420. /// Add a test object
  421. /// </summary>
  422. /// <param name="scene"></param>
  423. /// <returns></returns>
  424. public static SceneObjectPart AddSceneObject(Scene scene)
  425. {
  426. return AddSceneObject(scene, "Test Object");
  427. }
  428. /// <summary>
  429. /// Add a test object
  430. /// </summary>
  431. /// <param name="scene"></param>
  432. /// <param name="name"></param>
  433. /// <returns></returns>
  434. public static SceneObjectPart AddSceneObject(Scene scene, string name)
  435. {
  436. SceneObjectPart part
  437. = new SceneObjectPart(UUID.Zero, PrimitiveBaseShape.Default, Vector3.Zero, Quaternion.Identity, Vector3.Zero);
  438. part.Name = name;
  439. //part.UpdatePrimFlags(false, false, true);
  440. //part.ObjectFlags |= (uint)PrimFlags.Phantom;
  441. scene.AddNewSceneObject(new SceneObjectGroup(part), false);
  442. return part;
  443. }
  444. /// <summary>
  445. /// Delete a scene object asynchronously
  446. /// </summary>
  447. /// <param name="scene"></param>
  448. /// <param name="part"></param>
  449. /// <param name="action"></param>
  450. /// <param name="destinationId"></param>
  451. /// <param name="client"></param>
  452. public static void DeleteSceneObjectAsync(
  453. TestScene scene, SceneObjectPart part, DeRezAction action, UUID destinationId, IClientAPI client)
  454. {
  455. // Turn off the timer on the async sog deleter - we'll crank it by hand within a unit test
  456. AsyncSceneObjectGroupDeleter sogd = scene.SceneObjectGroupDeleter;
  457. sogd.Enabled = false;
  458. scene.DeRezObject(client, part.LocalId, UUID.Zero, action, destinationId);
  459. sogd.InventoryDeQueueAndDelete();
  460. }
  461. }
  462. }