diff options
Diffstat (limited to 'OpenSim/Tests/Permissions/Common.cs')
-rw-r--r-- | OpenSim/Tests/Permissions/Common.cs | 259 |
1 files changed, 259 insertions, 0 deletions
diff --git a/OpenSim/Tests/Permissions/Common.cs b/OpenSim/Tests/Permissions/Common.cs new file mode 100644 index 0000000..63b91b6 --- /dev/null +++ b/OpenSim/Tests/Permissions/Common.cs | |||
@@ -0,0 +1,259 @@ | |||
1 | using System; | ||
2 | using System.Collections.Generic; | ||
3 | using System.Reflection; | ||
4 | using System.Threading; | ||
5 | using Nini.Config; | ||
6 | using NUnit.Framework; | ||
7 | using OpenMetaverse; | ||
8 | using OpenSim.Framework; | ||
9 | using OpenSim.Region.Framework.Scenes; | ||
10 | using OpenSim.Region.CoreModules.World.Permissions; | ||
11 | using OpenSim.Region.CoreModules.Avatar.Inventory.Transfer; | ||
12 | using OpenSim.Region.CoreModules.Framework.InventoryAccess; | ||
13 | using OpenSim.Region.Framework.Scenes.Serialization; | ||
14 | using OpenSim.Services.Interfaces; | ||
15 | using OpenSim.Tests.Common; | ||
16 | using PermissionMask = OpenSim.Framework.PermissionMask; | ||
17 | |||
18 | namespace OpenSim.Tests.Permissions | ||
19 | { | ||
20 | [SetUpFixture] | ||
21 | public class Common : OpenSimTestCase | ||
22 | { | ||
23 | public static Common TheInstance; | ||
24 | |||
25 | public static TestScene TheScene | ||
26 | { | ||
27 | get { return TheInstance.m_Scene; } | ||
28 | } | ||
29 | |||
30 | public static ScenePresence[] TheAvatars | ||
31 | { | ||
32 | get { return TheInstance.m_Avatars; } | ||
33 | } | ||
34 | |||
35 | private static string Perms = "Owner: {0}; Group: {1}; Everyone: {2}; Next: {3}"; | ||
36 | private TestScene m_Scene; | ||
37 | private ScenePresence[] m_Avatars = new ScenePresence[3]; | ||
38 | |||
39 | [SetUp] | ||
40 | public override void SetUp() | ||
41 | { | ||
42 | if (TheInstance == null) | ||
43 | TheInstance = this; | ||
44 | |||
45 | base.SetUp(); | ||
46 | TestHelpers.EnableLogging(); | ||
47 | |||
48 | IConfigSource config = new IniConfigSource(); | ||
49 | config.AddConfig("Messaging"); | ||
50 | config.Configs["Messaging"].Set("InventoryTransferModule", "InventoryTransferModule"); | ||
51 | config.AddConfig("Modules"); | ||
52 | config.Configs["Modules"].Set("InventoryAccessModule", "BasicInventoryAccessModule"); | ||
53 | config.AddConfig("InventoryService"); | ||
54 | config.Configs["InventoryService"].Set("LocalServiceModule", "OpenSim.Services.InventoryService.dll:XInventoryService"); | ||
55 | config.Configs["InventoryService"].Set("StorageProvider", "OpenSim.Tests.Common.dll:TestXInventoryDataPlugin"); | ||
56 | |||
57 | m_Scene = new SceneHelpers().SetupScene("Test", UUID.Random(), 1000, 1000, config); | ||
58 | // Add modules | ||
59 | SceneHelpers.SetupSceneModules(m_Scene, config, new DefaultPermissionsModule(), new InventoryTransferModule(), new BasicInventoryAccessModule()); | ||
60 | |||
61 | SetUpBasicEnvironment(); | ||
62 | } | ||
63 | |||
64 | /// <summary> | ||
65 | /// The basic environment consists of: | ||
66 | /// - 3 avatars: A1, A2, A3 | ||
67 | /// - 6 simple boxes inworld belonging to A0 and with Next Owner perms: | ||
68 | /// C, CT, MC, MCT, MT, T | ||
69 | /// - Copies of all of these boxes in A0's inventory in the Objects folder | ||
70 | /// - One additional box in A0's inventory which is a copy of MCT, but | ||
71 | /// with C removed in inventory. This one is called MCT-C | ||
72 | /// </summary> | ||
73 | private void SetUpBasicEnvironment() | ||
74 | { | ||
75 | Console.WriteLine("===> SetUpBasicEnvironment <==="); | ||
76 | |||
77 | // Add 3 avatars | ||
78 | for (int i = 0; i < 3; i++) | ||
79 | { | ||
80 | UUID id = TestHelpers.ParseTail(i + 1); | ||
81 | |||
82 | m_Avatars[i] = AddScenePresence("Bot", "Bot_" + (i+1), id); | ||
83 | Assert.That(m_Avatars[i], Is.Not.Null); | ||
84 | Assert.That(m_Avatars[i].IsChildAgent, Is.False); | ||
85 | Assert.That(m_Avatars[i].UUID, Is.EqualTo(id)); | ||
86 | Assert.That(m_Scene.GetScenePresences().Count, Is.EqualTo(i + 1)); | ||
87 | } | ||
88 | |||
89 | AddA1Object("Box C", 10, PermissionMask.Copy); | ||
90 | AddA1Object("Box CT", 11, PermissionMask.Copy | PermissionMask.Transfer); | ||
91 | AddA1Object("Box MC", 12, PermissionMask.Modify | PermissionMask.Copy); | ||
92 | AddA1Object("Box MCT", 13, PermissionMask.Modify | PermissionMask.Copy | PermissionMask.Transfer); | ||
93 | AddA1Object("Box MT", 14, PermissionMask.Modify | PermissionMask.Transfer); | ||
94 | AddA1Object("Box T", 15, PermissionMask.Transfer); | ||
95 | |||
96 | Thread.Sleep(5000); | ||
97 | |||
98 | InventoryFolderBase objsFolder = UserInventoryHelpers.GetInventoryFolder(m_Scene.InventoryService, m_Avatars[0].UUID, "Objects"); | ||
99 | List<InventoryItemBase> items = m_Scene.InventoryService.GetFolderItems(m_Avatars[0].UUID, objsFolder.ID); | ||
100 | Assert.That(items.Count, Is.EqualTo(6)); | ||
101 | } | ||
102 | |||
103 | private ScenePresence AddScenePresence(string first, string last, UUID id) | ||
104 | { | ||
105 | UserAccount ua1 = UserAccountHelpers.CreateUserWithInventory(m_Scene, first, last, id, "pw"); | ||
106 | ScenePresence sp = SceneHelpers.AddScenePresence(m_Scene, id); | ||
107 | Assert.That(m_Scene.AuthenticateHandler.GetAgentCircuitData(id), Is.Not.Null); | ||
108 | |||
109 | return sp; | ||
110 | } | ||
111 | |||
112 | private void AddA1Object(string name, int suffix, PermissionMask nextOwnerPerms) | ||
113 | { | ||
114 | // Create a Box. Default permissions are just T | ||
115 | SceneObjectGroup box = AddSceneObject(name, suffix, 1, m_Avatars[0].UUID); | ||
116 | Assert.True((box.RootPart.NextOwnerMask & (int)PermissionMask.Copy) == 0); | ||
117 | Assert.True((box.RootPart.NextOwnerMask & (int)PermissionMask.Modify) == 0); | ||
118 | Assert.True((box.RootPart.NextOwnerMask & (int)PermissionMask.Transfer) != 0); | ||
119 | |||
120 | // field = 16 is NextOwner | ||
121 | // set = 1 means add the permission; set = 0 means remove permission | ||
122 | |||
123 | if ((nextOwnerPerms & PermissionMask.Copy) != 0) | ||
124 | m_Scene.HandleObjectPermissionsUpdate((IClientAPI)m_Avatars[0].ClientView, m_Avatars[0].UUID, | ||
125 | ((IClientAPI)(m_Avatars[0].ClientView)).SessionId, 16, box.LocalId, (uint)PermissionMask.Copy, 1); | ||
126 | |||
127 | if ((nextOwnerPerms & PermissionMask.Modify) != 0) | ||
128 | m_Scene.HandleObjectPermissionsUpdate((IClientAPI)m_Avatars[0].ClientView, m_Avatars[0].UUID, | ||
129 | ((IClientAPI)(m_Avatars[0].ClientView)).SessionId, 16, box.LocalId, (uint)PermissionMask.Modify, 1); | ||
130 | |||
131 | if ((nextOwnerPerms & PermissionMask.Transfer) == 0) | ||
132 | m_Scene.HandleObjectPermissionsUpdate((IClientAPI)m_Avatars[0].ClientView, m_Avatars[0].UUID, | ||
133 | ((IClientAPI)(m_Avatars[0].ClientView)).SessionId, 16, box.LocalId, (uint)PermissionMask.Transfer, 0); | ||
134 | |||
135 | PrintPerms(box); | ||
136 | AssertPermissions(nextOwnerPerms, (PermissionMask)box.RootPart.NextOwnerMask, box.OwnerID.ToString().Substring(34) + " : " + box.Name); | ||
137 | |||
138 | TakeCopyToInventory(box); | ||
139 | |||
140 | } | ||
141 | |||
142 | public void PrintPerms(SceneObjectGroup sog) | ||
143 | { | ||
144 | Console.WriteLine("SOG " + sog.Name + " (" + sog.OwnerID.ToString().Substring(34) + "): " + | ||
145 | String.Format(Perms, (PermissionMask)sog.EffectiveOwnerPerms, | ||
146 | (PermissionMask)sog.EffectiveGroupPerms, (PermissionMask)sog.EffectiveEveryOnePerms, (PermissionMask)sog.RootPart.NextOwnerMask)); | ||
147 | |||
148 | } | ||
149 | |||
150 | public void PrintPerms(InventoryItemBase item) | ||
151 | { | ||
152 | Console.WriteLine("Inv " + item.Name + " (" + item.Owner.ToString().Substring(34) + "): " + | ||
153 | String.Format(Perms, (PermissionMask)item.BasePermissions, | ||
154 | (PermissionMask)item.GroupPermissions, (PermissionMask)item.EveryOnePermissions, (PermissionMask)item.NextPermissions)); | ||
155 | |||
156 | } | ||
157 | |||
158 | public void AssertPermissions(PermissionMask desired, PermissionMask actual, string message) | ||
159 | { | ||
160 | if ((desired & PermissionMask.Copy) != 0) | ||
161 | Assert.True((actual & PermissionMask.Copy) != 0, message); | ||
162 | else | ||
163 | Assert.True((actual & PermissionMask.Copy) == 0, message); | ||
164 | |||
165 | if ((desired & PermissionMask.Modify) != 0) | ||
166 | Assert.True((actual & PermissionMask.Modify) != 0, message); | ||
167 | else | ||
168 | Assert.True((actual & PermissionMask.Modify) == 0, message); | ||
169 | |||
170 | if ((desired & PermissionMask.Transfer) != 0) | ||
171 | Assert.True((actual & PermissionMask.Transfer) != 0, message); | ||
172 | else | ||
173 | Assert.True((actual & PermissionMask.Transfer) == 0, message); | ||
174 | |||
175 | } | ||
176 | |||
177 | public SceneObjectGroup AddSceneObject(string name, int suffix, int partsToTestCount, UUID ownerID) | ||
178 | { | ||
179 | SceneObjectGroup so = SceneHelpers.CreateSceneObject(partsToTestCount, ownerID, name, suffix); | ||
180 | so.Name = name; | ||
181 | so.Description = name; | ||
182 | |||
183 | Assert.That(m_Scene.AddNewSceneObject(so, false), Is.True); | ||
184 | SceneObjectGroup retrievedSo = m_Scene.GetSceneObjectGroup(so.UUID); | ||
185 | |||
186 | // If the parts have the same UUID then we will consider them as one and the same | ||
187 | Assert.That(retrievedSo.PrimCount, Is.EqualTo(partsToTestCount)); | ||
188 | |||
189 | return so; | ||
190 | } | ||
191 | |||
192 | public void TakeCopyToInventory(SceneObjectGroup sog) | ||
193 | { | ||
194 | InventoryFolderBase objsFolder = UserInventoryHelpers.GetInventoryFolder(m_Scene.InventoryService, sog.OwnerID, "Objects"); | ||
195 | Assert.That(objsFolder, Is.Not.Null); | ||
196 | |||
197 | List<uint> localIds = new List<uint>(); localIds.Add(sog.LocalId); | ||
198 | // This is an async operation | ||
199 | m_Scene.DeRezObjects((IClientAPI)m_Avatars[0].ClientView, localIds, sog.UUID, DeRezAction.TakeCopy, objsFolder.ID); | ||
200 | } | ||
201 | |||
202 | public InventoryItemBase GetItemFromInventory(UUID userID, string folderName, string itemName) | ||
203 | { | ||
204 | InventoryFolderBase objsFolder = UserInventoryHelpers.GetInventoryFolder(m_Scene.InventoryService, userID, folderName); | ||
205 | Assert.That(objsFolder, Is.Not.Null); | ||
206 | List<InventoryItemBase> items = m_Scene.InventoryService.GetFolderItems(userID, objsFolder.ID); | ||
207 | InventoryItemBase item = items.Find(i => i.Name == itemName); | ||
208 | Assert.That(item, Is.Not.Null); | ||
209 | |||
210 | return item; | ||
211 | } | ||
212 | |||
213 | public void GiveInventoryItem(UUID itemId, ScenePresence giverSp, ScenePresence receiverSp) | ||
214 | { | ||
215 | TestClient giverClient = (TestClient)giverSp.ControllingClient; | ||
216 | TestClient receiverClient = (TestClient)receiverSp.ControllingClient; | ||
217 | |||
218 | UUID initialSessionId = TestHelpers.ParseTail(0x10); | ||
219 | byte[] giveImBinaryBucket = new byte[17]; | ||
220 | byte[] itemIdBytes = itemId.GetBytes(); | ||
221 | Array.Copy(itemIdBytes, 0, giveImBinaryBucket, 1, itemIdBytes.Length); | ||
222 | |||
223 | GridInstantMessage giveIm | ||
224 | = new GridInstantMessage( | ||
225 | m_Scene, | ||
226 | giverSp.UUID, | ||
227 | giverSp.Name, | ||
228 | receiverSp.UUID, | ||
229 | (byte)InstantMessageDialog.InventoryOffered, | ||
230 | false, | ||
231 | "inventory offered msg", | ||
232 | initialSessionId, | ||
233 | false, | ||
234 | Vector3.Zero, | ||
235 | giveImBinaryBucket, | ||
236 | true); | ||
237 | |||
238 | giverClient.HandleImprovedInstantMessage(giveIm); | ||
239 | |||
240 | // These details might not all be correct. | ||
241 | GridInstantMessage acceptIm | ||
242 | = new GridInstantMessage( | ||
243 | m_Scene, | ||
244 | receiverSp.UUID, | ||
245 | receiverSp.Name, | ||
246 | giverSp.UUID, | ||
247 | (byte)InstantMessageDialog.InventoryAccepted, | ||
248 | false, | ||
249 | "inventory accepted msg", | ||
250 | initialSessionId, | ||
251 | false, | ||
252 | Vector3.Zero, | ||
253 | null, | ||
254 | true); | ||
255 | |||
256 | receiverClient.HandleImprovedInstantMessage(acceptIm); | ||
257 | } | ||
258 | } | ||
259 | } | ||