1
0

OSSL_ApiAttachmentTests.cs 11 KB

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