aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/OpenSim/Tests/Permissions/Common.cs
blob: 84dadf28a357c3080f58d81056d8066cf15db4a2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
using System;
using System.Collections.Generic;
using System.Reflection;
using System.Threading;
using Nini.Config;
using NUnit.Framework;
using OpenMetaverse;
using OpenSim.Framework;
using OpenSim.Region.Framework.Scenes;
using OpenSim.Region.CoreModules.World.Permissions;
using OpenSim.Region.CoreModules.Avatar.Inventory.Transfer;
using OpenSim.Region.CoreModules.Framework.InventoryAccess;
using OpenSim.Region.Framework.Scenes.Serialization;
using OpenSim.Services.Interfaces;
using OpenSim.Tests.Common;
using PermissionMask = OpenSim.Framework.PermissionMask;

namespace OpenSim.Tests.Permissions
{
    [SetUpFixture]
    public class Common : OpenSimTestCase
    {
        public static Common TheInstance;

        public static TestScene TheScene
        {
            get { return TheInstance.m_Scene; }
        }

        public static ScenePresence[] TheAvatars
        {
            get { return TheInstance.m_Avatars;  }
        }

        private static string Perms = "Owner: {0}; Group: {1}; Everyone: {2}; Next: {3}";
        private TestScene m_Scene;
        private ScenePresence[] m_Avatars = new ScenePresence[3];

        [SetUp]
        public override void SetUp()
        {
            if (TheInstance == null)
                TheInstance = this;

            base.SetUp();
            TestHelpers.EnableLogging();

            IConfigSource config = new IniConfigSource();
            config.AddConfig("Messaging");
            config.Configs["Messaging"].Set("InventoryTransferModule", "InventoryTransferModule");
            config.AddConfig("Modules");
            config.Configs["Modules"].Set("InventoryAccessModule", "BasicInventoryAccessModule");
            config.AddConfig("InventoryService");
            config.Configs["InventoryService"].Set("LocalServiceModule", "OpenSim.Services.InventoryService.dll:XInventoryService");
            config.Configs["InventoryService"].Set("StorageProvider", "OpenSim.Tests.Common.dll:TestXInventoryDataPlugin");

            m_Scene = new SceneHelpers().SetupScene("Test", UUID.Random(), 1000, 1000, config);
            // Add modules
            SceneHelpers.SetupSceneModules(m_Scene, config, new DefaultPermissionsModule(), new InventoryTransferModule(), new BasicInventoryAccessModule());

            SetUpBasicEnvironment();
        }

        /// <summary>
        /// The basic environment consists of:
        /// - 3 avatars: A1, A2, A3
        /// - 6 simple boxes inworld belonging to A0 and with Next Owner perms:
        ///   C, CT, MC, MCT, MT, T
        /// - Copies of all of these boxes in A0's inventory in the Objects folder
        /// - One additional box in A0's inventory which is a copy of MCT, but 
        ///   with C removed in inventory. This one is called MCT-C
        /// </summary>
        private void SetUpBasicEnvironment()
        {
            Console.WriteLine("===> SetUpBasicEnvironment <===");

            // Add 3 avatars
            for (int i = 0; i < 3; i++)
            {
                UUID id = TestHelpers.ParseTail(i + 1);

                m_Avatars[i] = AddScenePresence("Bot", "Bot_" + (i+1), id);
                Assert.That(m_Avatars[i], Is.Not.Null);
                Assert.That(m_Avatars[i].IsChildAgent, Is.False);
                Assert.That(m_Avatars[i].UUID, Is.EqualTo(id));
                Assert.That(m_Scene.GetScenePresences().Count, Is.EqualTo(i + 1));
            }

            AddA1Object("Box C", 10, PermissionMask.Copy);
            AddA1Object("Box CT", 11, PermissionMask.Copy | PermissionMask.Transfer);
            AddA1Object("Box MC", 12, PermissionMask.Modify | PermissionMask.Copy);
            AddA1Object("Box MCT", 13, PermissionMask.Modify | PermissionMask.Copy | PermissionMask.Transfer);
            AddA1Object("Box MT", 14, PermissionMask.Modify | PermissionMask.Transfer);
            AddA1Object("Box T", 15, PermissionMask.Transfer);

            // MCT-C
            AddA1Object("Box MCT-C", 16, PermissionMask.Modify | PermissionMask.Copy | PermissionMask.Transfer);

            Thread.Sleep(5000);

            InventoryFolderBase objsFolder = UserInventoryHelpers.GetInventoryFolder(m_Scene.InventoryService, m_Avatars[0].UUID, "Objects");
            List<InventoryItemBase> items = m_Scene.InventoryService.GetFolderItems(m_Avatars[0].UUID, objsFolder.ID);
            Assert.That(items.Count, Is.EqualTo(7));

            RevokePermission(0, "Box MCT-C", PermissionMask.Copy);
        }

        private ScenePresence AddScenePresence(string first, string last, UUID id)
        {
            UserAccount ua1 = UserAccountHelpers.CreateUserWithInventory(m_Scene, first, last, id, "pw");
            ScenePresence sp = SceneHelpers.AddScenePresence(m_Scene, id);
            Assert.That(m_Scene.AuthenticateHandler.GetAgentCircuitData(id), Is.Not.Null);

            return sp;
        }

        private void AddA1Object(string name, int suffix, PermissionMask nextOwnerPerms)
        {
            // Create a Box. Default permissions are just T
            SceneObjectGroup box = AddSceneObject(name, suffix, 1, m_Avatars[0].UUID);
            Assert.True((box.RootPart.NextOwnerMask & (int)PermissionMask.Copy) == 0);
            Assert.True((box.RootPart.NextOwnerMask & (int)PermissionMask.Modify) == 0);
            Assert.True((box.RootPart.NextOwnerMask & (int)PermissionMask.Transfer) != 0);

            // field = 16 is NextOwner 
            // set = 1 means add the permission; set = 0 means remove permission

            if ((nextOwnerPerms & PermissionMask.Copy) != 0)
                m_Scene.HandleObjectPermissionsUpdate((IClientAPI)m_Avatars[0].ClientView, m_Avatars[0].UUID,
                    ((IClientAPI)(m_Avatars[0].ClientView)).SessionId, 16, box.LocalId, (uint)PermissionMask.Copy, 1);

            if ((nextOwnerPerms & PermissionMask.Modify) != 0)
                m_Scene.HandleObjectPermissionsUpdate((IClientAPI)m_Avatars[0].ClientView, m_Avatars[0].UUID,
                    ((IClientAPI)(m_Avatars[0].ClientView)).SessionId, 16, box.LocalId, (uint)PermissionMask.Modify, 1);

            if ((nextOwnerPerms & PermissionMask.Transfer) == 0)
                m_Scene.HandleObjectPermissionsUpdate((IClientAPI)m_Avatars[0].ClientView, m_Avatars[0].UUID,
                    ((IClientAPI)(m_Avatars[0].ClientView)).SessionId, 16, box.LocalId, (uint)PermissionMask.Transfer, 0);

            PrintPerms(box);
            AssertPermissions(nextOwnerPerms, (PermissionMask)box.RootPart.NextOwnerMask, box.OwnerID.ToString().Substring(34) + " : " + box.Name);

            TakeCopyToInventory(box);

        }

        public void RevokePermission(int ownerIndex, string name, PermissionMask perm)
        {
            InventoryItemBase item = Common.TheInstance.GetItemFromInventory(m_Avatars[ownerIndex].UUID, "Objects", name);
            Assert.That(item, Is.Not.Null);

            // Clone it, so to avoid aliasing -- just like the viewer does.
            InventoryItemBase clone = Common.TheInstance.CloneInventoryItem(item);
            // Revoke the permission in this copy
            clone.NextPermissions &= ~(uint)perm;
            Common.TheInstance.AssertPermissions((PermissionMask)clone.NextPermissions & ~perm,
                (PermissionMask)clone.NextPermissions, Common.TheInstance.IdStr(clone));
            Assert.That(clone.ID == item.ID);

            // Update properties of the item in inventory. This should affect the original item above.
            Common.TheScene.UpdateInventoryItemAsset(m_Avatars[ownerIndex].ControllingClient, UUID.Zero, clone.ID, clone);

            item = Common.TheInstance.GetItemFromInventory(m_Avatars[ownerIndex].UUID, "Objects", name);
            Assert.That(item, Is.Not.Null);
            Common.TheInstance.PrintPerms(item);
            Common.TheInstance.AssertPermissions((PermissionMask)item.NextPermissions & ~perm,
                (PermissionMask)item.NextPermissions, Common.TheInstance.IdStr(item));

        }

        public void PrintPerms(SceneObjectGroup sog)
        {
            Console.WriteLine("SOG " + sog.Name + " (" + sog.OwnerID.ToString().Substring(34) + "): " + 
                String.Format(Perms, (PermissionMask)sog.EffectiveOwnerPerms,
                    (PermissionMask)sog.EffectiveGroupPerms, (PermissionMask)sog.EffectiveEveryOnePerms, (PermissionMask)sog.RootPart.NextOwnerMask));

        }

        public void PrintPerms(InventoryItemBase item)
        {
            Console.WriteLine("Inv " + item.Name + " (" + item.Owner.ToString().Substring(34) + "): " + 
                String.Format(Perms, (PermissionMask)item.BasePermissions,
                    (PermissionMask)item.GroupPermissions, (PermissionMask)item.EveryOnePermissions, (PermissionMask)item.NextPermissions));

        }

        public void AssertPermissions(PermissionMask desired, PermissionMask actual, string message)
        {
            if ((desired & PermissionMask.Copy) != 0)
                Assert.True((actual & PermissionMask.Copy) != 0, message);
            else
                Assert.True((actual & PermissionMask.Copy) == 0, message);

            if ((desired & PermissionMask.Modify) != 0)
                Assert.True((actual & PermissionMask.Modify) != 0, message);
            else
                Assert.True((actual & PermissionMask.Modify) == 0, message);

            if ((desired & PermissionMask.Transfer) != 0)
                Assert.True((actual & PermissionMask.Transfer) != 0, message);
            else
                Assert.True((actual & PermissionMask.Transfer) == 0, message);

        }

        public SceneObjectGroup AddSceneObject(string name, int suffix, int partsToTestCount, UUID ownerID)
        {
            SceneObjectGroup so = SceneHelpers.CreateSceneObject(partsToTestCount, ownerID, name, suffix);
            so.Name = name;
            so.Description = name;

            Assert.That(m_Scene.AddNewSceneObject(so, false), Is.True);
            SceneObjectGroup retrievedSo = m_Scene.GetSceneObjectGroup(so.UUID);

            // If the parts have the same UUID then we will consider them as one and the same
            Assert.That(retrievedSo.PrimCount, Is.EqualTo(partsToTestCount));

            return so;
        }

        public void TakeCopyToInventory(SceneObjectGroup sog)
        {
            InventoryFolderBase objsFolder = UserInventoryHelpers.GetInventoryFolder(m_Scene.InventoryService, sog.OwnerID, "Objects");
            Assert.That(objsFolder, Is.Not.Null);

            List<uint> localIds = new List<uint>(); localIds.Add(sog.LocalId);
            // This is an async operation
            m_Scene.DeRezObjects((IClientAPI)m_Avatars[0].ClientView, localIds, sog.UUID, DeRezAction.TakeCopy, objsFolder.ID);
        }

        public InventoryItemBase GetItemFromInventory(UUID userID, string folderName, string itemName)
        {
            InventoryFolderBase objsFolder = UserInventoryHelpers.GetInventoryFolder(m_Scene.InventoryService, userID, folderName);
            Assert.That(objsFolder, Is.Not.Null);
            List<InventoryItemBase> items = m_Scene.InventoryService.GetFolderItems(userID, objsFolder.ID);
            InventoryItemBase item = items.Find(i => i.Name == itemName);
            Assert.That(item, Is.Not.Null);

            return item;
        }

        public InventoryItemBase CloneInventoryItem(InventoryItemBase item)
        {
            InventoryItemBase clone = new InventoryItemBase(item.ID);
            clone.Name = item.Name;
            clone.Description = item.Description;
            clone.AssetID = item.AssetID;
            clone.AssetType = item.AssetType;
            clone.BasePermissions = item.BasePermissions;
            clone.CreatorId = item.CreatorId;
            clone.CurrentPermissions = item.CurrentPermissions;
            clone.EveryOnePermissions = item.EveryOnePermissions;
            clone.Flags = item.Flags;
            clone.Folder = item.Folder;
            clone.GroupID = item.GroupID;
            clone.GroupOwned = item.GroupOwned;
            clone.GroupPermissions = item.GroupPermissions;
            clone.InvType = item.InvType;
            clone.NextPermissions = item.NextPermissions;
            clone.Owner = item.Owner;

            return clone;
        }

        public string IdStr(InventoryItemBase item)
        {
            return item.Owner.ToString().Substring(34) + " : " + item.Name;
        }

        public string IdStr(SceneObjectGroup sog)
        {
            return sog.OwnerID.ToString().Substring(34) + " : " + sog.Name;
        }

        public void GiveInventoryItem(UUID itemId, ScenePresence giverSp, ScenePresence receiverSp)
        {
            TestClient giverClient = (TestClient)giverSp.ControllingClient;
            TestClient receiverClient = (TestClient)receiverSp.ControllingClient;

            UUID initialSessionId = TestHelpers.ParseTail(0x10);
            byte[] giveImBinaryBucket = new byte[17];
            byte[] itemIdBytes = itemId.GetBytes();
            Array.Copy(itemIdBytes, 0, giveImBinaryBucket, 1, itemIdBytes.Length);

            GridInstantMessage giveIm
                = new GridInstantMessage(
                    m_Scene,
                    giverSp.UUID,
                    giverSp.Name,
                    receiverSp.UUID,
                    (byte)InstantMessageDialog.InventoryOffered,
                    false,
                    "inventory offered msg",
                    initialSessionId,
                    false,
                    Vector3.Zero,
                    giveImBinaryBucket,
                    true);

            giverClient.HandleImprovedInstantMessage(giveIm);

            // These details might not all be correct.
            GridInstantMessage acceptIm
                = new GridInstantMessage(
                    m_Scene,
                    receiverSp.UUID,
                    receiverSp.Name,
                    giverSp.UUID,
                    (byte)InstantMessageDialog.InventoryAccepted,
                    false,
                    "inventory accepted msg",
                    initialSessionId,
                    false,
                    Vector3.Zero,
                    null,
                    true);

            receiverClient.HandleImprovedInstantMessage(acceptIm);
        }
    }
}