Common.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  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.Threading;
  30. using Nini.Config;
  31. using NUnit.Framework;
  32. using OpenMetaverse;
  33. using OpenSim.Framework;
  34. using OpenSim.Region.Framework.Scenes;
  35. using OpenSim.Region.CoreModules.World.Permissions;
  36. using OpenSim.Region.CoreModules.Avatar.Inventory.Transfer;
  37. using OpenSim.Region.CoreModules.Framework.InventoryAccess;
  38. using OpenSim.Services.Interfaces;
  39. using OpenSim.Tests.Common;
  40. using PermissionMask = OpenSim.Framework.PermissionMask;
  41. namespace OpenSim.Tests.Permissions
  42. {
  43. [SetUpFixture]
  44. public class Common : OpenSimTestCase
  45. {
  46. public static Common TheInstance;
  47. public static TestScene TheScene
  48. {
  49. get { return TheInstance.m_Scene; }
  50. }
  51. public static ScenePresence[] TheAvatars
  52. {
  53. get { return TheInstance.m_Avatars; }
  54. }
  55. private static string Perms = "Owner: {0}; Group: {1}; Everyone: {2}; Next: {3}";
  56. private TestScene m_Scene;
  57. private ScenePresence[] m_Avatars = new ScenePresence[3];
  58. [SetUp]
  59. public override void SetUp()
  60. {
  61. if (TheInstance == null)
  62. TheInstance = this;
  63. base.SetUp();
  64. TestHelpers.EnableLogging();
  65. IConfigSource config = new IniConfigSource();
  66. config.AddConfig("Messaging");
  67. config.Configs["Messaging"].Set("InventoryTransferModule", "InventoryTransferModule");
  68. config.AddConfig("Modules");
  69. config.Configs["Modules"].Set("InventoryAccessModule", "BasicInventoryAccessModule");
  70. config.AddConfig("InventoryService");
  71. config.Configs["InventoryService"].Set("LocalServiceModule", "OpenSim.Services.InventoryService.dll:XInventoryService");
  72. config.Configs["InventoryService"].Set("StorageProvider", "OpenSim.Tests.Common.dll:TestXInventoryDataPlugin");
  73. config.AddConfig("Groups");
  74. config.Configs["Groups"].Set("Enabled", "true");
  75. config.Configs["Groups"].Set("Module", "Groups Module V2");
  76. config.Configs["Groups"].Set("StorageProvider", "OpenSim.Tests.Common.dll:TestGroupsDataPlugin");
  77. config.Configs["Groups"].Set("ServicesConnectorModule", "Groups Local Service Connector");
  78. config.Configs["Groups"].Set("LocalService", "local");
  79. m_Scene = new SceneHelpers().SetupScene("Test", UUID.Random(), 1000, 1000, config);
  80. // Add modules
  81. SceneHelpers.SetupSceneModules(m_Scene, config, new DefaultPermissionsModule(), new InventoryTransferModule(), new BasicInventoryAccessModule());
  82. SetUpBasicEnvironment();
  83. }
  84. /// <summary>
  85. /// The basic environment consists of:
  86. /// - 3 avatars: A1, A2, A3
  87. /// - 6 simple boxes inworld belonging to A0 and with Next Owner perms:
  88. /// C, CT, MC, MCT, MT, T
  89. /// - Copies of all of these boxes in A0's inventory in the Objects folder
  90. /// - One additional box inworld and in A0's inventory which is a copy of MCT, but
  91. /// with C removed in inventory. This one is called MCT-C
  92. /// </summary>
  93. private void SetUpBasicEnvironment()
  94. {
  95. Console.WriteLine("===> SetUpBasicEnvironment <===");
  96. // Add 3 avatars
  97. for (int i = 0; i < 3; i++)
  98. {
  99. UUID id = TestHelpers.ParseTail(i + 1);
  100. m_Avatars[i] = AddScenePresence("Bot", "Bot_" + (i+1), id);
  101. Assert.That(m_Avatars[i], Is.Not.Null);
  102. Assert.That(m_Avatars[i].IsChildAgent, Is.False);
  103. Assert.That(m_Avatars[i].UUID, Is.EqualTo(id));
  104. Assert.That(m_Scene.GetScenePresences().Count, Is.EqualTo(i + 1));
  105. }
  106. AddA1Object("Box C", 10, PermissionMask.Copy);
  107. AddA1Object("Box CT", 11, PermissionMask.Copy | PermissionMask.Transfer);
  108. AddA1Object("Box MC", 12, PermissionMask.Modify | PermissionMask.Copy);
  109. AddA1Object("Box MCT", 13, PermissionMask.Modify | PermissionMask.Copy | PermissionMask.Transfer);
  110. AddA1Object("Box MT", 14, PermissionMask.Modify | PermissionMask.Transfer);
  111. AddA1Object("Box T", 15, PermissionMask.Transfer);
  112. // MCT-C
  113. AddA1Object("Box MCT-C", 16, PermissionMask.Modify | PermissionMask.Copy | PermissionMask.Transfer);
  114. Thread.Sleep(5000);
  115. InventoryFolderBase objsFolder = UserInventoryHelpers.GetInventoryFolder(m_Scene.InventoryService, m_Avatars[0].UUID, "Objects");
  116. List<InventoryItemBase> items = m_Scene.InventoryService.GetFolderItems(m_Avatars[0].UUID, objsFolder.ID);
  117. Assert.That(items.Count, Is.EqualTo(7));
  118. RevokePermission(0, "Box MCT-C", PermissionMask.Copy);
  119. }
  120. private ScenePresence AddScenePresence(string first, string last, UUID id)
  121. {
  122. UserAccount ua1 = UserAccountHelpers.CreateUserWithInventory(m_Scene, first, last, id, "pw");
  123. ScenePresence sp = SceneHelpers.AddScenePresence(m_Scene, id);
  124. Assert.That(m_Scene.AuthenticateHandler.GetAgentCircuitData(id), Is.Not.Null);
  125. return sp;
  126. }
  127. private void AddA1Object(string name, int suffix, PermissionMask nextOwnerPerms)
  128. {
  129. // Create a Box. Default permissions are just T
  130. SceneObjectGroup box = AddSceneObject(name, suffix, 1, m_Avatars[0].UUID);
  131. Assert.True((box.RootPart.NextOwnerMask & (int)PermissionMask.Copy) == 0);
  132. Assert.True((box.RootPart.NextOwnerMask & (int)PermissionMask.Modify) == 0);
  133. Assert.True((box.RootPart.NextOwnerMask & (int)PermissionMask.Transfer) != 0);
  134. // field = 16 is NextOwner
  135. // set = 1 means add the permission; set = 0 means remove permission
  136. if ((nextOwnerPerms & PermissionMask.Copy) != 0)
  137. m_Scene.HandleObjectPermissionsUpdate((IClientAPI)m_Avatars[0].ClientView, m_Avatars[0].UUID,
  138. ((IClientAPI)(m_Avatars[0].ClientView)).SessionId, 16, box.LocalId, (uint)PermissionMask.Copy, 1);
  139. if ((nextOwnerPerms & PermissionMask.Modify) != 0)
  140. m_Scene.HandleObjectPermissionsUpdate((IClientAPI)m_Avatars[0].ClientView, m_Avatars[0].UUID,
  141. ((IClientAPI)(m_Avatars[0].ClientView)).SessionId, 16, box.LocalId, (uint)PermissionMask.Modify, 1);
  142. if ((nextOwnerPerms & PermissionMask.Transfer) == 0)
  143. m_Scene.HandleObjectPermissionsUpdate((IClientAPI)m_Avatars[0].ClientView, m_Avatars[0].UUID,
  144. ((IClientAPI)(m_Avatars[0].ClientView)).SessionId, 16, box.LocalId, (uint)PermissionMask.Transfer, 0);
  145. PrintPerms(box);
  146. AssertPermissions(nextOwnerPerms, (PermissionMask)box.RootPart.NextOwnerMask, box.OwnerID.ToString().Substring(34) + " : " + box.Name);
  147. TakeCopyToInventory(0, box);
  148. }
  149. public void RevokePermission(int ownerIndex, string name, PermissionMask perm)
  150. {
  151. InventoryItemBase item = Common.TheInstance.GetItemFromInventory(m_Avatars[ownerIndex].UUID, "Objects", name);
  152. Assert.That(item, Is.Not.Null);
  153. // Clone it, so to avoid aliasing -- just like the viewer does.
  154. InventoryItemBase clone = Common.TheInstance.CloneInventoryItem(item);
  155. // Revoke the permission in this copy
  156. clone.NextPermissions &= ~(uint)perm;
  157. Common.TheInstance.AssertPermissions((PermissionMask)clone.NextPermissions & ~perm,
  158. (PermissionMask)clone.NextPermissions, Common.TheInstance.IdStr(clone));
  159. Assert.That(clone.ID == item.ID);
  160. // Update properties of the item in inventory. This should affect the original item above.
  161. Common.TheScene.UpdateInventoryItemAsset(m_Avatars[ownerIndex].ControllingClient, UUID.Zero, clone.ID, clone);
  162. item = Common.TheInstance.GetItemFromInventory(m_Avatars[ownerIndex].UUID, "Objects", name);
  163. Assert.That(item, Is.Not.Null);
  164. Common.TheInstance.PrintPerms(item);
  165. Common.TheInstance.AssertPermissions((PermissionMask)item.NextPermissions & ~perm,
  166. (PermissionMask)item.NextPermissions, Common.TheInstance.IdStr(item));
  167. }
  168. public void PrintPerms(SceneObjectGroup sog)
  169. {
  170. Console.WriteLine("SOG " + sog.Name + " (" + sog.OwnerID.ToString().Substring(34) + "): " +
  171. String.Format(Perms, (PermissionMask)sog.EffectiveOwnerPerms,
  172. (PermissionMask)sog.EffectiveGroupPerms, (PermissionMask)sog.EffectiveEveryOnePerms, (PermissionMask)sog.RootPart.NextOwnerMask));
  173. }
  174. public void PrintPerms(InventoryItemBase item)
  175. {
  176. Console.WriteLine("Inv " + item.Name + " (" + item.Owner.ToString().Substring(34) + "): " +
  177. String.Format(Perms, (PermissionMask)item.BasePermissions,
  178. (PermissionMask)item.GroupPermissions, (PermissionMask)item.EveryOnePermissions, (PermissionMask)item.NextPermissions));
  179. }
  180. public void AssertPermissions(PermissionMask desired, PermissionMask actual, string message)
  181. {
  182. if ((desired & PermissionMask.Copy) != 0)
  183. Assert.True((actual & PermissionMask.Copy) != 0, message);
  184. else
  185. Assert.True((actual & PermissionMask.Copy) == 0, message);
  186. if ((desired & PermissionMask.Modify) != 0)
  187. Assert.True((actual & PermissionMask.Modify) != 0, message);
  188. else
  189. Assert.True((actual & PermissionMask.Modify) == 0, message);
  190. if ((desired & PermissionMask.Transfer) != 0)
  191. Assert.True((actual & PermissionMask.Transfer) != 0, message);
  192. else
  193. Assert.True((actual & PermissionMask.Transfer) == 0, message);
  194. }
  195. public SceneObjectGroup AddSceneObject(string name, int suffix, int partsToTestCount, UUID ownerID)
  196. {
  197. SceneObjectGroup so = SceneHelpers.CreateSceneObject(partsToTestCount, ownerID, name, suffix);
  198. so.Name = name;
  199. so.Description = name;
  200. Assert.That(m_Scene.AddNewSceneObject(so, false), Is.True);
  201. SceneObjectGroup retrievedSo = m_Scene.GetSceneObjectGroup(so.UUID);
  202. // If the parts have the same UUID then we will consider them as one and the same
  203. Assert.That(retrievedSo.PrimCount, Is.EqualTo(partsToTestCount));
  204. return so;
  205. }
  206. public void TakeCopyToInventory(int userIndex, SceneObjectGroup sog)
  207. {
  208. InventoryFolderBase objsFolder = UserInventoryHelpers.GetInventoryFolder(m_Scene.InventoryService, m_Avatars[userIndex].UUID, "Objects");
  209. Assert.That(objsFolder, Is.Not.Null);
  210. List<uint> localIds = new List<uint>(); localIds.Add(sog.LocalId);
  211. // This is an async operation
  212. m_Scene.DeRezObjects((IClientAPI)m_Avatars[userIndex].ClientView, localIds, m_Avatars[userIndex].UUID, DeRezAction.TakeCopy, objsFolder.ID);
  213. }
  214. public InventoryItemBase GetItemFromInventory(UUID userID, string folderName, string itemName)
  215. {
  216. InventoryFolderBase objsFolder = UserInventoryHelpers.GetInventoryFolder(m_Scene.InventoryService, userID, folderName);
  217. Assert.That(objsFolder, Is.Not.Null);
  218. List<InventoryItemBase> items = m_Scene.InventoryService.GetFolderItems(userID, objsFolder.ID);
  219. InventoryItemBase item = items.Find(i => i.Name == itemName);
  220. Assert.That(item, Is.Not.Null);
  221. return item;
  222. }
  223. public InventoryItemBase CloneInventoryItem(InventoryItemBase item)
  224. {
  225. InventoryItemBase clone = new InventoryItemBase(item.ID);
  226. clone.Name = item.Name;
  227. clone.Description = item.Description;
  228. clone.AssetID = item.AssetID;
  229. clone.AssetType = item.AssetType;
  230. clone.BasePermissions = item.BasePermissions;
  231. clone.CreatorId = item.CreatorId;
  232. clone.CurrentPermissions = item.CurrentPermissions;
  233. clone.EveryOnePermissions = item.EveryOnePermissions;
  234. clone.Flags = item.Flags;
  235. clone.Folder = item.Folder;
  236. clone.GroupID = item.GroupID;
  237. clone.GroupOwned = item.GroupOwned;
  238. clone.GroupPermissions = item.GroupPermissions;
  239. clone.InvType = item.InvType;
  240. clone.NextPermissions = item.NextPermissions;
  241. clone.Owner = item.Owner;
  242. return clone;
  243. }
  244. public void DeleteObjectsFolders()
  245. {
  246. // Delete everything in A2 and A3's Objects folders, so we can restart
  247. for (int i = 1; i < 3; i++)
  248. {
  249. InventoryFolderBase objsFolder = UserInventoryHelpers.GetInventoryFolder(Common.TheScene.InventoryService, Common.TheAvatars[i].UUID, "Objects");
  250. Assert.That(objsFolder, Is.Not.Null);
  251. List<InventoryItemBase> items = Common.TheScene.InventoryService.GetFolderItems(Common.TheAvatars[i].UUID, objsFolder.ID);
  252. List<UUID> ids = new List<UUID>();
  253. foreach (InventoryItemBase it in items)
  254. ids.Add(it.ID);
  255. Common.TheScene.InventoryService.DeleteItems(Common.TheAvatars[i].UUID, ids);
  256. items = Common.TheScene.InventoryService.GetFolderItems(Common.TheAvatars[i].UUID, objsFolder.ID);
  257. Assert.That(items.Count, Is.EqualTo(0), "A" + (i + 1));
  258. }
  259. }
  260. public string IdStr(InventoryItemBase item)
  261. {
  262. return item.Owner.ToString().Substring(34) + " : " + item.Name;
  263. }
  264. public string IdStr(SceneObjectGroup sog)
  265. {
  266. return sog.OwnerID.ToString().Substring(34) + " : " + sog.Name;
  267. }
  268. public void GiveInventoryItem(UUID itemId, ScenePresence giverSp, ScenePresence receiverSp)
  269. {
  270. TestClient giverClient = (TestClient)giverSp.ControllingClient;
  271. TestClient receiverClient = (TestClient)receiverSp.ControllingClient;
  272. UUID initialSessionId = TestHelpers.ParseTail(0x10);
  273. byte[] giveImBinaryBucket = new byte[17];
  274. byte[] itemIdBytes = itemId.GetBytes();
  275. Array.Copy(itemIdBytes, 0, giveImBinaryBucket, 1, itemIdBytes.Length);
  276. GridInstantMessage giveIm
  277. = new GridInstantMessage(
  278. m_Scene,
  279. giverSp.UUID,
  280. giverSp.Name,
  281. receiverSp.UUID,
  282. (byte)InstantMessageDialog.InventoryOffered,
  283. false,
  284. "inventory offered msg",
  285. initialSessionId,
  286. false,
  287. Vector3.Zero,
  288. giveImBinaryBucket,
  289. true);
  290. giverClient.HandleImprovedInstantMessage(giveIm);
  291. // These details might not all be correct.
  292. GridInstantMessage acceptIm
  293. = new GridInstantMessage(
  294. m_Scene,
  295. receiverSp.UUID,
  296. receiverSp.Name,
  297. giverSp.UUID,
  298. (byte)InstantMessageDialog.InventoryAccepted,
  299. false,
  300. "inventory accepted msg",
  301. initialSessionId,
  302. false,
  303. Vector3.Zero,
  304. null,
  305. true);
  306. receiverClient.HandleImprovedInstantMessage(acceptIm);
  307. }
  308. }
  309. }