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