LSL_ApiInventoryTests.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  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.CoreModules.World.Permissions;
  42. using OpenSim.Region.ScriptEngine.Shared;
  43. using OpenSim.Region.ScriptEngine.Shared.Api;
  44. using OpenSim.Region.ScriptEngine.Shared.Instance;
  45. using OpenSim.Services.Interfaces;
  46. using OpenSim.Tests.Common;
  47. using PermissionMask = OpenSim.Framework.PermissionMask;
  48. namespace OpenSim.Region.ScriptEngine.Shared.Tests
  49. {
  50. /// <summary>
  51. /// Tests for inventory functions in LSL
  52. /// </summary>
  53. [TestFixture]
  54. public class LSL_ApiInventoryTests : OpenSimTestCase
  55. {
  56. protected Scene m_scene;
  57. protected XEngine.XEngine m_engine;
  58. [SetUp]
  59. public override void SetUp()
  60. {
  61. base.SetUp();
  62. IConfigSource initConfigSource = new IniConfigSource();
  63. IConfig config = initConfigSource.AddConfig("Startup");
  64. config.Set("serverside_object_permissions", true);
  65. config =initConfigSource.AddConfig("Permissions");
  66. config.Set("permissionmodules", "DefaultPermissionsModule");
  67. config.Set("serverside_object_permissions", true);
  68. config.Set("propagate_permissions", true);
  69. config = initConfigSource.AddConfig("XEngine");
  70. config.Set("Enabled", "true");
  71. m_scene = new SceneHelpers().SetupScene();
  72. SceneHelpers.SetupSceneModules(m_scene, initConfigSource, new object[] { new DefaultPermissionsModule() });
  73. m_engine = new XEngine.XEngine();
  74. m_engine.Initialise(initConfigSource);
  75. m_engine.AddRegion(m_scene);
  76. }
  77. /// <summary>
  78. /// Test giving inventory from an object to an object where both are owned by the same user.
  79. /// </summary>
  80. [Test]
  81. public void TestLlGiveInventoryO2OSameOwner()
  82. {
  83. TestHelpers.InMethod();
  84. // log4net.Config.XmlConfigurator.Configure();
  85. UUID userId = TestHelpers.ParseTail(0x1);
  86. string inventoryItemName = "item1";
  87. SceneObjectGroup so1 = SceneHelpers.CreateSceneObject(1, userId, "so1", 0x10);
  88. m_scene.AddSceneObject(so1);
  89. // Create an object embedded inside the first
  90. UUID itemId = TestHelpers.ParseTail(0x20);
  91. TaskInventoryHelpers.AddSceneObject(m_scene.AssetService, so1.RootPart, inventoryItemName, itemId, userId);
  92. LSL_Api api = new LSL_Api();
  93. api.Initialize(m_engine, so1.RootPart, null);
  94. // Create a second object
  95. SceneObjectGroup so2 = SceneHelpers.CreateSceneObject(1, userId, "so2", 0x100);
  96. m_scene.AddSceneObject(so2);
  97. api.llGiveInventory(so2.UUID.ToString(), inventoryItemName);
  98. // Item has copy permissions so original should stay intact.
  99. List<TaskInventoryItem> originalItems = so1.RootPart.Inventory.GetInventoryItems();
  100. Assert.That(originalItems.Count, Is.EqualTo(1));
  101. List<TaskInventoryItem> copiedItems = so2.RootPart.Inventory.GetInventoryItems(inventoryItemName);
  102. Assert.That(copiedItems.Count, Is.EqualTo(1));
  103. Assert.That(copiedItems[0].Name, Is.EqualTo(inventoryItemName));
  104. }
  105. /// <summary>
  106. /// Test giving inventory from an object to an object where they have different owners
  107. /// </summary>
  108. [Test]
  109. public void TestLlGiveInventoryO2ODifferentOwners()
  110. {
  111. TestHelpers.InMethod();
  112. // log4net.Config.XmlConfigurator.Configure();
  113. UUID user1Id = TestHelpers.ParseTail(0x1);
  114. UUID user2Id = TestHelpers.ParseTail(0x2);
  115. string inventoryItemName = "item1";
  116. SceneObjectGroup so1 = SceneHelpers.CreateSceneObject(1, user1Id, "so1", 0x10);
  117. m_scene.AddSceneObject(so1);
  118. LSL_Api api = new LSL_Api();
  119. api.Initialize(m_engine, so1.RootPart, null);
  120. // Create an object embedded inside the first
  121. UUID itemId = TestHelpers.ParseTail(0x20);
  122. TaskInventoryHelpers.AddSceneObject(m_scene.AssetService, so1.RootPart, inventoryItemName, itemId, user1Id);
  123. // Create a second object
  124. SceneObjectGroup so2 = SceneHelpers.CreateSceneObject(1, user2Id, "so2", 0x100);
  125. m_scene.AddSceneObject(so2);
  126. LSL_Api api2 = new LSL_Api();
  127. api2.Initialize(m_engine, so2.RootPart, null);
  128. // *** Firstly, we test where llAllowInventoryDrop() has not been called. ***
  129. api.llGiveInventory(so2.UUID.ToString(), inventoryItemName);
  130. {
  131. // Item has copy permissions so original should stay intact.
  132. List<TaskInventoryItem> originalItems = so1.RootPart.Inventory.GetInventoryItems();
  133. Assert.That(originalItems.Count, Is.EqualTo(1));
  134. // Should have not copied
  135. List<TaskInventoryItem> copiedItems = so2.RootPart.Inventory.GetInventoryItems(inventoryItemName);
  136. Assert.That(copiedItems.Count, Is.EqualTo(0));
  137. }
  138. // *** Secondly, we turn on allow inventory drop in the target and retest. ***
  139. api2.llAllowInventoryDrop(1);
  140. api.llGiveInventory(so2.UUID.ToString(), inventoryItemName);
  141. {
  142. // Item has copy permissions so original should stay intact.
  143. List<TaskInventoryItem> originalItems = so1.RootPart.Inventory.GetInventoryItems();
  144. Assert.That(originalItems.Count, Is.EqualTo(1));
  145. // Should now have copied.
  146. List<TaskInventoryItem> copiedItems = so2.RootPart.Inventory.GetInventoryItems(inventoryItemName);
  147. Assert.That(copiedItems.Count, Is.EqualTo(1));
  148. Assert.That(copiedItems[0].Name, Is.EqualTo(inventoryItemName));
  149. }
  150. }
  151. /// <summary>
  152. /// Test giving inventory from an object to an avatar that is not the object's owner.
  153. /// </summary>
  154. [Test]
  155. public void TestLlGiveInventoryO2DifferentAvatar()
  156. {
  157. TestHelpers.InMethod();
  158. // TestHelpers.EnableLogging();
  159. UUID user1Id = TestHelpers.ParseTail(0x1);
  160. UUID user2Id = TestHelpers.ParseTail(0x2);
  161. string inventoryItemName = "item1";
  162. SceneObjectGroup so1 = SceneHelpers.CreateSceneObject(1, user1Id, "so1", 0x10);
  163. m_scene.AddSceneObject(so1);
  164. LSL_Api api = new LSL_Api();
  165. api.Initialize(m_engine, so1.RootPart, null);
  166. // Create an object embedded inside the first
  167. UUID itemId = TestHelpers.ParseTail(0x20);
  168. TaskInventoryHelpers.AddSceneObject(m_scene.AssetService, so1.RootPart, inventoryItemName, itemId, user1Id);
  169. UserAccountHelpers.CreateUserWithInventory(m_scene, user2Id);
  170. api.llGiveInventory(user2Id.ToString(), inventoryItemName);
  171. InventoryItemBase receivedItem
  172. = UserInventoryHelpers.GetInventoryItem(
  173. m_scene.InventoryService, user2Id, string.Format("Objects/{0}", inventoryItemName));
  174. Assert.IsNotNull(receivedItem);
  175. }
  176. /// <summary>
  177. /// Test giving inventory from an object to an avatar that is not the object's owner and where the next
  178. /// permissions do not include mod.
  179. /// </summary>
  180. [Test]
  181. public void TestLlGiveInventoryO2DifferentAvatarNoMod()
  182. {
  183. TestHelpers.InMethod();
  184. // TestHelpers.EnableLogging();
  185. UUID user1Id = TestHelpers.ParseTail(0x1);
  186. UUID user2Id = TestHelpers.ParseTail(0x2);
  187. string inventoryItemName = "item1";
  188. SceneObjectGroup so1 = SceneHelpers.CreateSceneObject(1, user1Id, "so1", 0x10);
  189. m_scene.AddSceneObject(so1);
  190. LSL_Api api = new LSL_Api();
  191. api.Initialize(m_engine, so1.RootPart, null);
  192. // Create an object embedded inside the first
  193. UUID itemId = TestHelpers.ParseTail(0x20);
  194. TaskInventoryItem tii
  195. = TaskInventoryHelpers.AddSceneObject(m_scene.AssetService, so1.RootPart, inventoryItemName, itemId, user1Id);
  196. tii.NextPermissions &= ~((uint)PermissionMask.Modify);
  197. UserAccountHelpers.CreateUserWithInventory(m_scene, user2Id);
  198. api.llGiveInventory(user2Id.ToString(), inventoryItemName);
  199. InventoryItemBase receivedItem
  200. = UserInventoryHelpers.GetInventoryItem(
  201. m_scene.InventoryService, user2Id, string.Format("Objects/{0}", inventoryItemName));
  202. Assert.IsNotNull(receivedItem);
  203. Assert.AreEqual(0, receivedItem.CurrentPermissions & (uint)PermissionMask.Modify);
  204. }
  205. [Test]
  206. public void TestLlRemoteLoadScriptPin()
  207. {
  208. TestHelpers.InMethod();
  209. // TestHelpers.EnableLogging();
  210. UUID user1Id = TestHelpers.ParseTail(0x1);
  211. UUID user2Id = TestHelpers.ParseTail(0x2);
  212. SceneObjectGroup sourceSo = SceneHelpers.AddSceneObject(m_scene, "sourceSo", user1Id);
  213. m_scene.AddSceneObject(sourceSo);
  214. LSL_Api api = new LSL_Api();
  215. api.Initialize(m_engine, sourceSo.RootPart, null);
  216. TaskInventoryHelpers.AddScript(m_scene.AssetService, sourceSo.RootPart, "script", "Hello World");
  217. SceneObjectGroup targetSo = SceneHelpers.AddSceneObject(m_scene, "targetSo", user1Id);
  218. SceneObjectGroup otherOwnedTargetSo = SceneHelpers.AddSceneObject(m_scene, "otherOwnedTargetSo", user2Id);
  219. // Test that we cannot load a script when the target pin has never been set (i.e. it is zero)
  220. api.llRemoteLoadScriptPin(targetSo.UUID.ToString(), "script", 0, 0, 0);
  221. Assert.IsNull(targetSo.RootPart.Inventory.GetInventoryItem("script"));
  222. // Test that we cannot load a script when the given pin does not match the target
  223. targetSo.RootPart.ScriptAccessPin = 5;
  224. api.llRemoteLoadScriptPin(targetSo.UUID.ToString(), "script", 3, 0, 0);
  225. Assert.IsNull(targetSo.RootPart.Inventory.GetInventoryItem("script"));
  226. // Test that we cannot load into a prim with a different owner
  227. otherOwnedTargetSo.RootPart.ScriptAccessPin = 3;
  228. api.llRemoteLoadScriptPin(otherOwnedTargetSo.UUID.ToString(), "script", 3, 0, 0);
  229. Assert.IsNull(otherOwnedTargetSo.RootPart.Inventory.GetInventoryItem("script"));
  230. // Test that we can load a script when given pin and dest pin match.
  231. targetSo.RootPart.ScriptAccessPin = 3;
  232. api.llRemoteLoadScriptPin(targetSo.UUID.ToString(), "script", 3, 0, 0);
  233. TaskInventoryItem insertedItem = targetSo.RootPart.Inventory.GetInventoryItem("script");
  234. Assert.IsNotNull(insertedItem);
  235. // Test that we can no longer load if access pin is unset
  236. targetSo.RootPart.Inventory.RemoveInventoryItem(insertedItem.ItemID);
  237. Assert.IsNull(targetSo.RootPart.Inventory.GetInventoryItem("script"));
  238. targetSo.RootPart.ScriptAccessPin = 0;
  239. api.llRemoteLoadScriptPin(otherOwnedTargetSo.UUID.ToString(), "script", 3, 0, 0);
  240. Assert.IsNull(otherOwnedTargetSo.RootPart.Inventory.GetInventoryItem("script"));
  241. }
  242. }
  243. }