HGAssetMapperTests.cs 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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.Threading;
  29. using System.Xml;
  30. using Nini.Config;
  31. using NUnit.Framework;
  32. using OpenMetaverse;
  33. using OpenSim.Framework;
  34. using OpenSim.Region.CoreModules.Framework.InventoryAccess;
  35. using OpenSim.Region.Framework.Scenes;
  36. using OpenSim.Region.ScriptEngine.XEngine;
  37. using OpenSim.Services.Interfaces;
  38. using OpenSim.Tests.Common;
  39. namespace OpenSim.Region.CoreModules.Framework.InventoryAccess.Tests
  40. {
  41. [TestFixture]
  42. public class HGAssetMapperTests : OpenSimTestCase
  43. {
  44. [Test]
  45. public void TestPostAssetRewrite()
  46. {
  47. TestHelpers.InMethod();
  48. // TestHelpers.EnableLogging();
  49. XEngine xengine = new OpenSim.Region.ScriptEngine.XEngine.XEngine();
  50. xengine.DebugLevel = 1;
  51. IniConfigSource configSource = new IniConfigSource();
  52. IConfig startupConfig = configSource.AddConfig("Startup");
  53. startupConfig.Set("DefaultScriptEngine", "XEngine");
  54. IConfig xEngineConfig = configSource.AddConfig("XEngine");
  55. xEngineConfig.Set("Enabled", "true");
  56. xEngineConfig.Set("StartDelay", "0");
  57. xEngineConfig.Set("AppDomainLoading", "false");
  58. string homeUrl = "http://hg.HomeTestPostAssetRewriteGrid.com";
  59. string foreignUrl = "http://hg.ForeignTestPostAssetRewriteGrid.com";
  60. int soIdTail = 0x1;
  61. UUID assetId = TestHelpers.ParseTail(0x10);
  62. UUID userId = TestHelpers.ParseTail(0x100);
  63. UUID sceneId = TestHelpers.ParseTail(0x1000);
  64. string userFirstName = "TestPostAsset";
  65. string userLastName = "Rewrite";
  66. int soPartsCount = 3;
  67. Scene scene = new SceneHelpers().SetupScene("TestPostAssetRewriteScene", sceneId, 1000, 1000, configSource);
  68. SceneHelpers.SetupSceneModules(scene, configSource, xengine);
  69. scene.StartScripts();
  70. HGAssetMapper hgam = new HGAssetMapper(scene, homeUrl);
  71. UserAccount ua
  72. = UserAccountHelpers.CreateUserWithInventory(scene, userFirstName, userLastName, userId, "password");
  73. SceneObjectGroup so = SceneHelpers.AddSceneObject(scene, soPartsCount, ua.PrincipalID, "part", soIdTail);
  74. RezScript(
  75. scene, so.UUID, "default { state_entry() { llSay(0, \"Hello World\"); } }", "item1", ua.PrincipalID);
  76. AssetBase asset = AssetHelpers.CreateAsset(assetId, so);
  77. asset.CreatorID = foreignUrl;
  78. hgam.PostAsset(foreignUrl, asset);
  79. // Check transformed asset.
  80. AssetBase ncAssetGet = scene.AssetService.Get(assetId.ToString());
  81. Assert.AreEqual(foreignUrl, ncAssetGet.CreatorID);
  82. string xmlData = Utils.BytesToString(ncAssetGet.Data);
  83. XmlDocument ncAssetGetXmlDoc = new XmlDocument();
  84. ncAssetGetXmlDoc.XmlResolver=null;
  85. ncAssetGetXmlDoc.LoadXml(xmlData);
  86. // Console.WriteLine(ncAssetGetXmlDoc.OuterXml);
  87. XmlNodeList creatorDataNodes = ncAssetGetXmlDoc.GetElementsByTagName("CreatorData");
  88. Assert.AreEqual(soPartsCount, creatorDataNodes.Count);
  89. //Console.WriteLine("creatorDataNodes {0}", creatorDataNodes.Count);
  90. foreach (XmlNode creatorDataNode in creatorDataNodes)
  91. {
  92. Assert.AreEqual(
  93. string.Format("{0};{1} {2}", homeUrl, ua.FirstName, ua.LastName), creatorDataNode.InnerText);
  94. }
  95. // Check that saved script nodes have attributes
  96. XmlNodeList savedScriptStateNodes = ncAssetGetXmlDoc.GetElementsByTagName("SavedScriptState");
  97. Assert.AreEqual(1, savedScriptStateNodes.Count);
  98. Assert.AreEqual(1, savedScriptStateNodes[0].Attributes.Count);
  99. XmlNode uuidAttribute = savedScriptStateNodes[0].Attributes.GetNamedItem("UUID");
  100. Assert.NotNull(uuidAttribute);
  101. // XXX: To check the actual UUID attribute we would have to do some work to retreive the UUID of the task
  102. // item created earlier.
  103. }
  104. private void RezScript(Scene scene, UUID soId, string script, string itemName, UUID userId)
  105. {
  106. InventoryItemBase itemTemplate = new InventoryItemBase();
  107. // itemTemplate.ID = itemId;
  108. itemTemplate.Name = itemName;
  109. itemTemplate.Folder = soId;
  110. itemTemplate.InvType = (int)InventoryType.LSL;
  111. // XXX: Ultimately it would be better to be able to directly manipulate the script engine to rez a script
  112. // immediately for tests rather than chunter through it's threaded mechanisms.
  113. AutoResetEvent chatEvent = new AutoResetEvent(false);
  114. scene.EventManager.OnChatFromWorld += (s, c) =>
  115. {
  116. // Console.WriteLine("Got chat [{0}]", c.Message);
  117. chatEvent.Set();
  118. };
  119. scene.RezNewScript(userId, itemTemplate, script);
  120. // Console.WriteLine("HERE");
  121. Assert.IsTrue(chatEvent.WaitOne(60000), "Chat event in HGAssetMapperTests.RezScript not received");
  122. }
  123. }
  124. }