LSL_ApiInventoryTests.cs 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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 log4net;
  32. using Nini.Config;
  33. using NUnit.Framework;
  34. using OpenMetaverse;
  35. using OpenMetaverse.Assets;
  36. using OpenMetaverse.StructuredData;
  37. using OpenSim.Framework;
  38. using OpenSim.Region.CoreModules.Avatar.AvatarFactory;
  39. using OpenSim.Region.OptionalModules.World.NPC;
  40. using OpenSim.Region.Framework.Scenes;
  41. using OpenSim.Region.ScriptEngine.Shared;
  42. using OpenSim.Region.ScriptEngine.Shared.Api;
  43. using OpenSim.Services.Interfaces;
  44. using OpenSim.Tests.Common;
  45. using OpenSim.Tests.Common.Mock;
  46. namespace OpenSim.Region.ScriptEngine.Shared.Tests
  47. {
  48. /// <summary>
  49. /// Tests for inventory functions in LSL
  50. /// </summary>
  51. [TestFixture]
  52. public class LSL_ApiInventoryTests : OpenSimTestCase
  53. {
  54. protected Scene m_scene;
  55. protected XEngine.XEngine m_engine;
  56. [SetUp]
  57. public override void SetUp()
  58. {
  59. base.SetUp();
  60. IConfigSource initConfigSource = new IniConfigSource();
  61. IConfig config = initConfigSource.AddConfig("XEngine");
  62. config.Set("Enabled", "true");
  63. m_scene = new SceneHelpers().SetupScene();
  64. SceneHelpers.SetupSceneModules(m_scene, initConfigSource);
  65. m_engine = new XEngine.XEngine();
  66. m_engine.Initialise(initConfigSource);
  67. m_engine.AddRegion(m_scene);
  68. }
  69. /// <summary>
  70. /// Test giving inventory from an object to an object where both are owned by the same user.
  71. /// </summary>
  72. [Test]
  73. public void TestLlGiveInventoryO2OSameOwner()
  74. {
  75. TestHelpers.InMethod();
  76. // log4net.Config.XmlConfigurator.Configure();
  77. UUID userId = TestHelpers.ParseTail(0x1);
  78. string inventoryItemName = "item1";
  79. SceneObjectGroup so1 = SceneHelpers.CreateSceneObject(1, userId, "so1", 0x10);
  80. m_scene.AddSceneObject(so1);
  81. // Create an object embedded inside the first
  82. UUID itemId = TestHelpers.ParseTail(0x20);
  83. TaskInventoryHelpers.AddSceneObject(m_scene, so1.RootPart, inventoryItemName, itemId, userId);
  84. LSL_Api api = new LSL_Api();
  85. api.Initialize(m_engine, so1.RootPart, null);
  86. // Create a second object
  87. SceneObjectGroup so2 = SceneHelpers.CreateSceneObject(1, userId, "so2", 0x100);
  88. m_scene.AddSceneObject(so2);
  89. api.llGiveInventory(so2.UUID.ToString(), inventoryItemName);
  90. // Item has copy permissions so original should stay intact.
  91. List<TaskInventoryItem> originalItems = so1.RootPart.Inventory.GetInventoryItems();
  92. Assert.That(originalItems.Count, Is.EqualTo(1));
  93. List<TaskInventoryItem> copiedItems = so2.RootPart.Inventory.GetInventoryItems(inventoryItemName);
  94. Assert.That(copiedItems.Count, Is.EqualTo(1));
  95. Assert.That(copiedItems[0].Name, Is.EqualTo(inventoryItemName));
  96. }
  97. /// <summary>
  98. /// Test giving inventory from an object to an object where they have different owners
  99. /// </summary>
  100. [Test]
  101. public void TestLlGiveInventoryO2ODifferentOwners()
  102. {
  103. TestHelpers.InMethod();
  104. // log4net.Config.XmlConfigurator.Configure();
  105. UUID user1Id = TestHelpers.ParseTail(0x1);
  106. UUID user2Id = TestHelpers.ParseTail(0x2);
  107. string inventoryItemName = "item1";
  108. SceneObjectGroup so1 = SceneHelpers.CreateSceneObject(1, user1Id, "so1", 0x10);
  109. m_scene.AddSceneObject(so1);
  110. LSL_Api api = new LSL_Api();
  111. api.Initialize(m_engine, so1.RootPart, null);
  112. // Create an object embedded inside the first
  113. UUID itemId = TestHelpers.ParseTail(0x20);
  114. TaskInventoryHelpers.AddSceneObject(m_scene, so1.RootPart, inventoryItemName, itemId, user1Id);
  115. // Create a second object
  116. SceneObjectGroup so2 = SceneHelpers.CreateSceneObject(1, user2Id, "so2", 0x100);
  117. m_scene.AddSceneObject(so2);
  118. LSL_Api api2 = new LSL_Api();
  119. api2.Initialize(m_engine, so2.RootPart, null);
  120. // *** Firstly, we test where llAllowInventoryDrop() has not been called. ***
  121. api.llGiveInventory(so2.UUID.ToString(), inventoryItemName);
  122. {
  123. // Item has copy permissions so original should stay intact.
  124. List<TaskInventoryItem> originalItems = so1.RootPart.Inventory.GetInventoryItems();
  125. Assert.That(originalItems.Count, Is.EqualTo(1));
  126. // Should have not copied
  127. List<TaskInventoryItem> copiedItems = so2.RootPart.Inventory.GetInventoryItems(inventoryItemName);
  128. Assert.That(copiedItems.Count, Is.EqualTo(0));
  129. }
  130. // *** Secondly, we turn on allow inventory drop in the target and retest. ***
  131. api2.llAllowInventoryDrop(1);
  132. api.llGiveInventory(so2.UUID.ToString(), inventoryItemName);
  133. {
  134. // Item has copy permissions so original should stay intact.
  135. List<TaskInventoryItem> originalItems = so1.RootPart.Inventory.GetInventoryItems();
  136. Assert.That(originalItems.Count, Is.EqualTo(1));
  137. // Should now have copied.
  138. List<TaskInventoryItem> copiedItems = so2.RootPart.Inventory.GetInventoryItems(inventoryItemName);
  139. Assert.That(copiedItems.Count, Is.EqualTo(1));
  140. Assert.That(copiedItems[0].Name, Is.EqualTo(inventoryItemName));
  141. }
  142. }
  143. [Test]
  144. public void TestLlRemoteLoadScriptPin()
  145. {
  146. TestHelpers.InMethod();
  147. // TestHelpers.EnableLogging();
  148. UUID user1Id = TestHelpers.ParseTail(0x1);
  149. UUID user2Id = TestHelpers.ParseTail(0x2);
  150. SceneObjectGroup sourceSo = SceneHelpers.AddSceneObject(m_scene, "sourceSo", user1Id);
  151. m_scene.AddSceneObject(sourceSo);
  152. LSL_Api api = new LSL_Api();
  153. api.Initialize(m_engine, sourceSo.RootPart, null);
  154. TaskInventoryHelpers.AddScript(m_scene, sourceSo.RootPart, "script", "Hello World");
  155. SceneObjectGroup targetSo = SceneHelpers.AddSceneObject(m_scene, "targetSo", user1Id);
  156. SceneObjectGroup otherOwnedTargetSo = SceneHelpers.AddSceneObject(m_scene, "otherOwnedTargetSo", user2Id);
  157. // Test that we cannot load a script when the target pin has never been set (i.e. it is zero)
  158. api.llRemoteLoadScriptPin(targetSo.UUID.ToString(), "script", 0, 0, 0);
  159. Assert.IsNull(targetSo.RootPart.Inventory.GetInventoryItem("script"));
  160. // Test that we cannot load a script when the given pin does not match the target
  161. targetSo.RootPart.ScriptAccessPin = 5;
  162. api.llRemoteLoadScriptPin(targetSo.UUID.ToString(), "script", 3, 0, 0);
  163. Assert.IsNull(targetSo.RootPart.Inventory.GetInventoryItem("script"));
  164. // Test that we cannot load into a prim with a different owner
  165. otherOwnedTargetSo.RootPart.ScriptAccessPin = 3;
  166. api.llRemoteLoadScriptPin(otherOwnedTargetSo.UUID.ToString(), "script", 3, 0, 0);
  167. Assert.IsNull(otherOwnedTargetSo.RootPart.Inventory.GetInventoryItem("script"));
  168. // Test that we can load a script when given pin and dest pin match.
  169. targetSo.RootPart.ScriptAccessPin = 3;
  170. api.llRemoteLoadScriptPin(targetSo.UUID.ToString(), "script", 3, 0, 0);
  171. TaskInventoryItem insertedItem = targetSo.RootPart.Inventory.GetInventoryItem("script");
  172. Assert.IsNotNull(insertedItem);
  173. // Test that we can no longer load if access pin is unset
  174. targetSo.RootPart.Inventory.RemoveInventoryItem(insertedItem.ItemID);
  175. Assert.IsNull(targetSo.RootPart.Inventory.GetInventoryItem("script"));
  176. targetSo.RootPart.ScriptAccessPin = 0;
  177. api.llRemoteLoadScriptPin(otherOwnedTargetSo.UUID.ToString(), "script", 3, 0, 0);
  178. Assert.IsNull(otherOwnedTargetSo.RootPart.Inventory.GetInventoryItem("script"));
  179. }
  180. }
  181. }