XEnginePersistenceTests.cs 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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.IO;
  30. using System.Linq;
  31. using System.Threading;
  32. using Nini.Config;
  33. using NUnit.Framework;
  34. using OpenMetaverse;
  35. using OpenSim.Framework;
  36. using OpenSim.Region.CoreModules.Avatar.Attachments;
  37. using OpenSim.Region.CoreModules.Framework.InventoryAccess;
  38. using OpenSim.Region.Framework.Scenes;
  39. using OpenSim.Region.ScriptEngine.XEngine;
  40. using OpenSim.Services.Interfaces;
  41. using OpenSim.Tests.Common;
  42. namespace OpenSim.Region.ScriptEngine.Tests
  43. {
  44. [TestFixture]
  45. public class XEnginePersistenceTests : OpenSimTestCase
  46. {
  47. private AutoResetEvent m_chatEvent = new AutoResetEvent(false);
  48. private void OnChatFromWorld(object sender, OSChatMessage oscm)
  49. {
  50. // Console.WriteLine("Got chat [{0}]", oscm.Message);
  51. // m_osChatMessageReceived = oscm;
  52. m_chatEvent.Set();
  53. }
  54. private void AddCommonConfig(IConfigSource config, List<object> modules)
  55. {
  56. config.AddConfig("Modules");
  57. config.Configs["Modules"].Set("InventoryAccessModule", "BasicInventoryAccessModule");
  58. AttachmentsModule attMod = new AttachmentsModule();
  59. attMod.DebugLevel = 1;
  60. modules.Add(attMod);
  61. modules.Add(new BasicInventoryAccessModule());
  62. }
  63. private void AddScriptingConfig(IConfigSource config, XEngine.XEngine xEngine, List<object> modules)
  64. {
  65. IConfig startupConfig = config.AddConfig("Startup");
  66. startupConfig.Set("DefaultScriptEngine", "XEngine");
  67. IConfig xEngineConfig = config.AddConfig("XEngine");
  68. xEngineConfig.Set("Enabled", "true");
  69. xEngineConfig.Set("StartDelay", "0");
  70. // These tests will not run with AppDomainLoading = true, at least on mono. For unknown reasons, the call
  71. // to AssemblyResolver.OnAssemblyResolve fails.
  72. xEngineConfig.Set("AppDomainLoading", "false");
  73. modules.Add(xEngine);
  74. }
  75. private Scene CreateScriptingEnabledTestScene(XEngine.XEngine xEngine)
  76. {
  77. IConfigSource config = new IniConfigSource();
  78. List<object> modules = new List<object>();
  79. AddCommonConfig(config, modules);
  80. AddScriptingConfig(config, xEngine, modules);
  81. Scene scene
  82. = new SceneHelpers().SetupScene(
  83. "attachments-test-scene", TestHelpers.ParseTail(999), 1000, 1000, config);
  84. SceneHelpers.SetupSceneModules(scene, config, modules.ToArray());
  85. scene.StartScripts();
  86. return scene;
  87. }
  88. [Test]
  89. public void TestScriptedAttachmentPersistence()
  90. {
  91. TestHelpers.InMethod();
  92. // TestHelpers.EnableLogging();
  93. XEngine.XEngine xEngine = new XEngine.XEngine();
  94. Scene scene = CreateScriptingEnabledTestScene(xEngine);
  95. UserAccount ua1 = UserAccountHelpers.CreateUserWithInventory(scene, 0x1);
  96. ScenePresence sp = SceneHelpers.AddScenePresence(scene, ua1);
  97. SceneObjectGroup so = SceneHelpers.CreateSceneObject(1, sp.UUID, "att-name", 0x10);
  98. TaskInventoryHelpers.AddScript(
  99. scene.AssetService,
  100. so.RootPart,
  101. "scriptItem",
  102. "default { attach(key id) { if (id != NULL_KEY) { llSay(0, \"Hello World\"); } } }");
  103. InventoryItemBase userItem = UserInventoryHelpers.AddInventoryItem(scene, so, 0x100, 0x1000);
  104. // FIXME: Right now, we have to do a tricksy chat listen to make sure we know when the script is running.
  105. // In the future, we need to be able to do this programatically more predicably.
  106. scene.EventManager.OnChatFromWorld += OnChatFromWorld;
  107. SceneObjectGroup rezzedSo
  108. = scene.AttachmentsModule.RezSingleAttachmentFromInventory(sp, userItem.ID, (uint)AttachmentPoint.Chest);
  109. TaskInventoryItem rezzedScriptItem = rezzedSo.RootPart.Inventory.GetInventoryItem("scriptItem");
  110. // Wait for chat to signal rezzed script has been started.
  111. m_chatEvent.WaitOne(60000);
  112. // Force save
  113. xEngine.DoBackup(new Object[] { 0 });
  114. // Console.WriteLine("ItemID {0}", rezzedScriptItem.ItemID);
  115. //
  116. // foreach (
  117. // string s in Directory.EnumerateFileSystemEntries(
  118. // string.Format("ScriptEngines/{0}", scene.RegionInfo.RegionID)))
  119. // Console.WriteLine(s);
  120. Assert.IsFalse(
  121. File.Exists(
  122. string.Format("ScriptEngines/{0}/{1}.state", scene.RegionInfo.RegionID, rezzedScriptItem.ItemID)));
  123. scene.AttachmentsModule.DetachSingleAttachmentToInv(sp, rezzedSo);
  124. }
  125. }
  126. }