OSSL_ApiAttachmentTests.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  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.Attachments;
  39. using OpenSim.Region.CoreModules.Framework.InventoryAccess;
  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 OSSL attachment functions
  50. /// </summary>
  51. /// <remarks>
  52. /// TODO: Add tests for all functions
  53. /// </remarks>
  54. [TestFixture]
  55. public class OSSL_ApiAttachmentTests : OpenSimTestCase
  56. {
  57. protected Scene m_scene;
  58. protected XEngine.XEngine m_engine;
  59. [SetUp]
  60. public override void SetUp()
  61. {
  62. base.SetUp();
  63. IConfigSource initConfigSource = new IniConfigSource();
  64. IConfig xengineConfig = initConfigSource.AddConfig("XEngine");
  65. xengineConfig.Set("Enabled", "true");
  66. xengineConfig.Set("AllowOSFunctions", "true");
  67. xengineConfig.Set("OSFunctionThreatLevel", "Severe");
  68. IConfig modulesConfig = initConfigSource.AddConfig("Modules");
  69. modulesConfig.Set("InventoryAccessModule", "BasicInventoryAccessModule");
  70. m_scene = new SceneHelpers().SetupScene();
  71. SceneHelpers.SetupSceneModules(
  72. m_scene, initConfigSource, new AttachmentsModule(), new BasicInventoryAccessModule());
  73. m_engine = new XEngine.XEngine();
  74. m_engine.Initialise(initConfigSource);
  75. m_engine.AddRegion(m_scene);
  76. }
  77. [Test]
  78. public void TestOsForceAttachToAvatarFromInventory()
  79. {
  80. TestHelpers.InMethod();
  81. // TestHelpers.EnableLogging();
  82. string taskInvObjItemName = "sphere";
  83. UUID taskInvObjItemId = UUID.Parse("00000000-0000-0000-0000-100000000000");
  84. AttachmentPoint attachPoint = AttachmentPoint.Chin;
  85. UserAccount ua1 = UserAccountHelpers.CreateUserWithInventory(m_scene, 0x1);
  86. ScenePresence sp = SceneHelpers.AddScenePresence(m_scene, ua1.PrincipalID);
  87. SceneObjectGroup inWorldObj = SceneHelpers.AddSceneObject(m_scene, "inWorldObj", ua1.PrincipalID);
  88. TaskInventoryItem scriptItem = TaskInventoryHelpers.AddScript(m_scene, inWorldObj.RootPart);
  89. new LSL_Api().Initialize(m_engine, inWorldObj.RootPart, scriptItem);
  90. OSSL_Api osslApi = new OSSL_Api();
  91. osslApi.Initialize(m_engine, inWorldObj.RootPart, scriptItem);
  92. // SceneObjectGroup sog1 = SceneHelpers.CreateSceneObject(1, ua1.PrincipalID);
  93. // Create an object embedded inside the first
  94. TaskInventoryHelpers.AddSceneObject(m_scene, inWorldObj.RootPart, taskInvObjItemName, taskInvObjItemId, ua1.PrincipalID);
  95. osslApi.osForceAttachToAvatarFromInventory(taskInvObjItemName, (int)attachPoint);
  96. // Check scene presence status
  97. Assert.That(sp.HasAttachments(), Is.True);
  98. List<SceneObjectGroup> attachments = sp.GetAttachments();
  99. Assert.That(attachments.Count, Is.EqualTo(1));
  100. SceneObjectGroup attSo = attachments[0];
  101. Assert.That(attSo.Name, Is.EqualTo(taskInvObjItemName));
  102. Assert.That(attSo.AttachmentPoint, Is.EqualTo((uint)attachPoint));
  103. Assert.That(attSo.IsAttachment);
  104. Assert.That(attSo.UsesPhysics, Is.False);
  105. Assert.That(attSo.IsTemporary, Is.False);
  106. // Check appearance status
  107. List<AvatarAttachment> attachmentsInAppearance = sp.Appearance.GetAttachments();
  108. Assert.That(attachmentsInAppearance.Count, Is.EqualTo(1));
  109. Assert.That(sp.Appearance.GetAttachpoint(attachmentsInAppearance[0].ItemID), Is.EqualTo((uint)attachPoint));
  110. }
  111. /// <summary>
  112. /// Make sure we can't force attach anything other than objects.
  113. /// </summary>
  114. [Test]
  115. public void TestOsForceAttachToAvatarFromInventoryNotObject()
  116. {
  117. TestHelpers.InMethod();
  118. // TestHelpers.EnableLogging();
  119. string taskInvObjItemName = "sphere";
  120. UUID taskInvObjItemId = UUID.Parse("00000000-0000-0000-0000-100000000000");
  121. AttachmentPoint attachPoint = AttachmentPoint.Chin;
  122. UserAccount ua1 = UserAccountHelpers.CreateUserWithInventory(m_scene, 0x1);
  123. ScenePresence sp = SceneHelpers.AddScenePresence(m_scene, ua1.PrincipalID);
  124. SceneObjectGroup inWorldObj = SceneHelpers.AddSceneObject(m_scene, "inWorldObj", ua1.PrincipalID);
  125. TaskInventoryItem scriptItem = TaskInventoryHelpers.AddScript(m_scene, inWorldObj.RootPart);
  126. new LSL_Api().Initialize(m_engine, inWorldObj.RootPart, scriptItem);
  127. OSSL_Api osslApi = new OSSL_Api();
  128. osslApi.Initialize(m_engine, inWorldObj.RootPart, scriptItem);
  129. // Create an object embedded inside the first
  130. TaskInventoryHelpers.AddNotecard(
  131. m_scene, inWorldObj.RootPart, taskInvObjItemName, taskInvObjItemId, TestHelpers.ParseTail(0x900));
  132. bool exceptionCaught = false;
  133. try
  134. {
  135. osslApi.osForceAttachToAvatarFromInventory(taskInvObjItemName, (int)attachPoint);
  136. }
  137. catch (Exception)
  138. {
  139. exceptionCaught = true;
  140. }
  141. Assert.That(exceptionCaught, Is.True);
  142. // Check scene presence status
  143. Assert.That(sp.HasAttachments(), Is.False);
  144. List<SceneObjectGroup> attachments = sp.GetAttachments();
  145. Assert.That(attachments.Count, Is.EqualTo(0));
  146. // Check appearance status
  147. List<AvatarAttachment> attachmentsInAppearance = sp.Appearance.GetAttachments();
  148. Assert.That(attachmentsInAppearance.Count, Is.EqualTo(0));
  149. }
  150. [Test]
  151. public void TestOsForceAttachToOtherAvatarFromInventory()
  152. {
  153. TestHelpers.InMethod();
  154. // TestHelpers.EnableLogging();
  155. string taskInvObjItemName = "sphere";
  156. UUID taskInvObjItemId = UUID.Parse("00000000-0000-0000-0000-100000000000");
  157. AttachmentPoint attachPoint = AttachmentPoint.Chin;
  158. UserAccount ua1 = UserAccountHelpers.CreateUserWithInventory(m_scene, "user", "one", 0x1, "pass");
  159. UserAccount ua2 = UserAccountHelpers.CreateUserWithInventory(m_scene, "user", "two", 0x2, "pass");
  160. ScenePresence sp = SceneHelpers.AddScenePresence(m_scene, ua1);
  161. SceneObjectGroup inWorldObj = SceneHelpers.AddSceneObject(m_scene, "inWorldObj", ua1.PrincipalID);
  162. TaskInventoryItem scriptItem = TaskInventoryHelpers.AddScript(m_scene, inWorldObj.RootPart);
  163. new LSL_Api().Initialize(m_engine, inWorldObj.RootPart, scriptItem);
  164. OSSL_Api osslApi = new OSSL_Api();
  165. osslApi.Initialize(m_engine, inWorldObj.RootPart, scriptItem);
  166. // Create an object embedded inside the first
  167. TaskInventoryHelpers.AddSceneObject(m_scene, inWorldObj.RootPart, taskInvObjItemName, taskInvObjItemId, ua1.PrincipalID);
  168. ScenePresence sp2 = SceneHelpers.AddScenePresence(m_scene, ua2);
  169. osslApi.osForceAttachToOtherAvatarFromInventory(sp2.UUID.ToString(), taskInvObjItemName, (int)attachPoint);
  170. // Check scene presence status
  171. Assert.That(sp.HasAttachments(), Is.False);
  172. List<SceneObjectGroup> attachments = sp.GetAttachments();
  173. Assert.That(attachments.Count, Is.EqualTo(0));
  174. Assert.That(sp2.HasAttachments(), Is.True);
  175. List<SceneObjectGroup> attachments2 = sp2.GetAttachments();
  176. Assert.That(attachments2.Count, Is.EqualTo(1));
  177. SceneObjectGroup attSo = attachments2[0];
  178. Assert.That(attSo.Name, Is.EqualTo(taskInvObjItemName));
  179. Assert.That(attSo.OwnerID, Is.EqualTo(ua2.PrincipalID));
  180. Assert.That(attSo.AttachmentPoint, Is.EqualTo((uint)attachPoint));
  181. Assert.That(attSo.IsAttachment);
  182. Assert.That(attSo.UsesPhysics, Is.False);
  183. Assert.That(attSo.IsTemporary, Is.False);
  184. // Check appearance status
  185. List<AvatarAttachment> attachmentsInAppearance = sp.Appearance.GetAttachments();
  186. Assert.That(attachmentsInAppearance.Count, Is.EqualTo(0));
  187. List<AvatarAttachment> attachmentsInAppearance2 = sp2.Appearance.GetAttachments();
  188. Assert.That(attachmentsInAppearance2.Count, Is.EqualTo(1));
  189. Assert.That(sp2.Appearance.GetAttachpoint(attachmentsInAppearance2[0].ItemID), Is.EqualTo((uint)attachPoint));
  190. }
  191. }
  192. }