UserProfileCacheServiceTests.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  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 NUnit.Framework;
  28. using NUnit.Framework.SyntaxHelpers;
  29. using System.Threading;
  30. using OpenMetaverse;
  31. using OpenSim.Data;
  32. using OpenSim.Framework;
  33. using OpenSim.Framework.Communications.Cache;
  34. using OpenSim.Region.Framework.Scenes;
  35. using OpenSim.Region.Communications.Local;
  36. using OpenSim.Tests.Common.Mock;
  37. using OpenSim.Tests.Common.Setup;
  38. using OpenSim.Tests.Common;
  39. namespace OpenSim.Framework.Communications.Tests
  40. {
  41. [TestFixture]
  42. public class UserProfileCacheServiceTests
  43. {
  44. /// <value>Used by tests to indicate whether an async operation timed out</value>
  45. private bool timedOut;
  46. private void InventoryReceived(UUID userId)
  47. {
  48. lock (this)
  49. {
  50. timedOut = false;
  51. Monitor.PulseAll(this);
  52. }
  53. }
  54. [Test]
  55. public void TestGetUserDetails()
  56. {
  57. TestHelper.InMethod();
  58. UUID userId = UUID.Parse("00000000-0000-0000-0000-000000000002");
  59. string firstName = "Bill";
  60. string lastName = "Bailey";
  61. CachedUserInfo nonExistingUserInfo;
  62. TestCommunicationsManager commsManager = new TestCommunicationsManager();
  63. Scene myScene = SceneSetupHelpers.SetupScene(commsManager, "");
  64. // Check we can't retrieve info before it exists by uuid
  65. nonExistingUserInfo = commsManager.UserProfileCacheService.GetUserDetails(userId);
  66. Assert.That(nonExistingUserInfo, Is.Null, "User info found by uuid before user creation");
  67. // Check we can't retrieve info before it exists by name
  68. nonExistingUserInfo = commsManager.UserProfileCacheService.GetUserDetails(firstName, lastName);
  69. Assert.That(nonExistingUserInfo, Is.Null, "User info found by name before user creation");
  70. LocalUserServices lus = (LocalUserServices)commsManager.UserService;
  71. lus.AddUser(firstName, lastName, "troll", "[email protected]", 1000, 1000, userId);
  72. CachedUserInfo existingUserInfo;
  73. // Check we can retrieve info by uuid
  74. existingUserInfo = commsManager.UserProfileCacheService.GetUserDetails(userId);
  75. Assert.That(existingUserInfo, Is.Not.Null, "User info not found by uuid");
  76. // Check we can retrieve info by name
  77. existingUserInfo = commsManager.UserProfileCacheService.GetUserDetails(firstName, lastName);
  78. Assert.That(existingUserInfo, Is.Not.Null, "User info not found by name");
  79. }
  80. /**
  81. * Disabled as not fully implemented
  82. [Test]
  83. public void TestUpdateProfile()
  84. {
  85. UUID userId = UUID.Parse("00000000-0000-0000-0000-000000000292");
  86. string firstName = "Inspector";
  87. string originalLastName = "Morse";
  88. string newLastName = "Gadget";
  89. UserProfileData newProfile = new UserProfileData();
  90. newProfile.ID = userId;
  91. newProfile.FirstName = firstName;
  92. newProfile.SurName = newLastName;
  93. TestCommunicationsManager commsManager = new TestCommunicationsManager();
  94. UserProfileCacheService userCacheService = commsManager.UserProfileCacheService;
  95. IUserDataPlugin userDataPlugin = commsManager.UserDataPlugin;
  96. // Check that we can't update info before it exists
  97. Assert.That(userCacheService.UpdateProfile(newProfile), Is.False);
  98. Assert.That(userDataPlugin.GetUserByUUID(userId), Is.Null);
  99. // Check that we can update a profile once it exists
  100. LocalUserServices lus = (LocalUserServices)commsManager.UserService;
  101. lus.AddUser(firstName, originalLastName, "pingu", "[email protected]", 1000, 1000, userId);
  102. Assert.That(userCacheService.UpdateProfile(newProfile), Is.True);
  103. UserProfileData retrievedProfile = userCacheService.GetUserDetails(userId).UserProfile;
  104. Assert.That(retrievedProfile.SurName, Is.EqualTo(newLastName));
  105. Assert.That(userDataPlugin.GetUserByUUID(userId).SurName, Is.EqualTo(newLastName));
  106. }
  107. */
  108. [Test]
  109. public void TestFetchInventory()
  110. {
  111. TestHelper.InMethod();
  112. Scene myScene = SceneSetupHelpers.SetupScene("inventory");
  113. timedOut = true;
  114. lock (this)
  115. {
  116. UserProfileTestUtils.CreateUserWithInventory(myScene.CommsManager, InventoryReceived);
  117. Monitor.Wait(this, 60000);
  118. }
  119. Assert.That(timedOut, Is.False, "Timed out");
  120. }
  121. [Test]
  122. public void TestGetChildFolder()
  123. {
  124. TestHelper.InMethod();
  125. Scene myScene = SceneSetupHelpers.SetupScene("inventory");
  126. CachedUserInfo userInfo;
  127. lock (this)
  128. {
  129. userInfo = UserProfileTestUtils.CreateUserWithInventory(myScene.CommsManager, InventoryReceived);
  130. Monitor.Wait(this, 60000);
  131. }
  132. UUID folderId = UUID.Parse("00000000-0000-0000-0000-000000000011");
  133. Assert.That(userInfo.RootFolder.GetChildFolder(folderId), Is.Null);
  134. userInfo.CreateFolder("testFolder", folderId, (ushort)AssetType.Animation, userInfo.RootFolder.ID);
  135. Assert.That(userInfo.RootFolder.GetChildFolder(folderId), Is.Not.Null);
  136. }
  137. [Test]
  138. public void TestCreateFolder()
  139. {
  140. TestHelper.InMethod();
  141. Scene myScene = SceneSetupHelpers.SetupScene("inventory");
  142. CachedUserInfo userInfo;
  143. lock (this)
  144. {
  145. userInfo = UserProfileTestUtils.CreateUserWithInventory(myScene.CommsManager, InventoryReceived);
  146. Monitor.Wait(this, 60000);
  147. }
  148. UUID folderId = UUID.Parse("00000000-0000-0000-0000-000000000010");
  149. Assert.That(userInfo.RootFolder.ContainsChildFolder(folderId), Is.False);
  150. // 1: Try a folder create that should fail because the parent id given does not exist
  151. UUID missingFolderId = UUID.Random();
  152. InventoryFolderBase myFolder = new InventoryFolderBase();
  153. myFolder.ID = folderId;
  154. Assert.That(
  155. userInfo.CreateFolder("testFolder1", folderId, (ushort)AssetType.Animation, missingFolderId), Is.False);
  156. Assert.That(myScene.InventoryService.QueryFolder(myFolder), Is.Null);
  157. Assert.That(userInfo.RootFolder.ContainsChildFolder(missingFolderId), Is.False);
  158. Assert.That(userInfo.RootFolder.FindFolder(folderId), Is.Null);
  159. // 2: Try a folder create that should work
  160. Assert.That(
  161. userInfo.CreateFolder("testFolder2", folderId, (ushort)AssetType.Animation, userInfo.RootFolder.ID), Is.True);
  162. Assert.That(myScene.InventoryService.QueryFolder(myFolder), Is.Not.Null);
  163. Assert.That(userInfo.RootFolder.ContainsChildFolder(folderId), Is.True);
  164. }
  165. //[Test]
  166. public void TestUpdateFolder()
  167. {
  168. TestHelper.InMethod();
  169. Scene myScene = SceneSetupHelpers.SetupScene("inventory");
  170. CachedUserInfo userInfo;
  171. lock (this)
  172. {
  173. userInfo = UserProfileTestUtils.CreateUserWithInventory(myScene.CommsManager, InventoryReceived);
  174. Monitor.Wait(this, 60000);
  175. }
  176. UUID folder1Id = UUID.Parse("00000000-0000-0000-0000-000000000060");
  177. InventoryFolderImpl rootFolder = userInfo.RootFolder;
  178. InventoryFolderBase myFolder = new InventoryFolderBase();
  179. myFolder.ID = folder1Id;
  180. userInfo.CreateFolder("folder1", folder1Id, (ushort)AssetType.Animation, rootFolder.ID);
  181. // 1: Test updates that don't involve moving the folder
  182. {
  183. string newFolderName1 = "newFolderName1";
  184. ushort folderType1 = (ushort)AssetType.Texture;
  185. userInfo.UpdateFolder(newFolderName1, folder1Id, folderType1, rootFolder.ID);
  186. InventoryFolderImpl folder1 = rootFolder.GetChildFolder(folder1Id);
  187. Assert.That(newFolderName1, Is.EqualTo(folder1.Name));
  188. Assert.That(folderType1, Is.EqualTo((ushort)folder1.Type));
  189. InventoryFolderBase dataFolder1 = myScene.InventoryService.QueryFolder(myFolder);
  190. Assert.That(newFolderName1, Is.EqualTo(dataFolder1.Name));
  191. Assert.That(folderType1, Is.EqualTo((ushort)dataFolder1.Type));
  192. }
  193. // 2: Test an update that also involves moving the folder
  194. {
  195. UUID folder2Id = UUID.Parse("00000000-0000-0000-0000-000000000061");
  196. userInfo.CreateFolder("folder2", folder2Id, (ushort)AssetType.Animation, rootFolder.ID);
  197. InventoryFolderImpl folder2 = rootFolder.GetChildFolder(folder2Id);
  198. InventoryFolderBase myFolder2 = new InventoryFolderBase();
  199. myFolder2.ID = folder2Id;
  200. string newFolderName2 = "newFolderName2";
  201. ushort folderType2 = (ushort)AssetType.Bodypart;
  202. userInfo.UpdateFolder(newFolderName2, folder1Id, folderType2, folder2Id);
  203. InventoryFolderImpl folder1 = folder2.GetChildFolder(folder1Id);
  204. Assert.That(newFolderName2, Is.EqualTo(folder1.Name));
  205. Assert.That(folderType2, Is.EqualTo((ushort)folder1.Type));
  206. Assert.That(folder2Id, Is.EqualTo(folder1.ParentID));
  207. Assert.That(folder2.ContainsChildFolder(folder1Id), Is.True);
  208. Assert.That(rootFolder.ContainsChildFolder(folder1Id), Is.False);
  209. InventoryFolderBase dataFolder1 = myScene.InventoryService.QueryFolder(myFolder2);
  210. Assert.That(newFolderName2, Is.EqualTo(dataFolder1.Name));
  211. Assert.That(folderType2, Is.EqualTo((ushort)dataFolder1.Type));
  212. Assert.That(folder2Id, Is.EqualTo(dataFolder1.ParentID));
  213. }
  214. }
  215. [Test]
  216. public void TestMoveFolder()
  217. {
  218. TestHelper.InMethod();
  219. Scene myScene = SceneSetupHelpers.SetupScene("inventory");
  220. CachedUserInfo userInfo;
  221. lock (this)
  222. {
  223. userInfo = UserProfileTestUtils.CreateUserWithInventory(myScene.CommsManager, InventoryReceived);
  224. Monitor.Wait(this, 60000);
  225. }
  226. UUID folder1Id = UUID.Parse("00000000-0000-0000-0000-000000000020");
  227. UUID folder2Id = UUID.Parse("00000000-0000-0000-0000-000000000021");
  228. UUID folderToMoveId = UUID.Parse("00000000-0000-0000-0000-000000000030");
  229. InventoryFolderImpl rootFolder = userInfo.RootFolder;
  230. userInfo.CreateFolder("folder1", folder1Id, (ushort)AssetType.Animation, rootFolder.ID);
  231. InventoryFolderImpl folder1 = rootFolder.GetChildFolder(folder1Id);
  232. userInfo.CreateFolder("folder2", folder2Id, (ushort)AssetType.Animation, rootFolder.ID);
  233. InventoryFolderImpl folder2 = rootFolder.GetChildFolder(folder2Id);
  234. // Check folder is currently in folder1
  235. userInfo.CreateFolder("folderToMove", folderToMoveId, (ushort)AssetType.Animation, folder1Id);
  236. Assert.That(folder1.ContainsChildFolder(folderToMoveId), Is.True);
  237. userInfo.MoveFolder(folderToMoveId, folder2Id);
  238. // Check folder is now in folder2 and no trace remains in folder1
  239. InventoryFolderBase myFolder = new InventoryFolderBase();
  240. myFolder.ID = folderToMoveId;
  241. Assert.That(folder2.ContainsChildFolder(folderToMoveId), Is.True);
  242. Assert.That(myScene.InventoryService.QueryFolder(myFolder).ParentID, Is.EqualTo(folder2Id));
  243. Assert.That(folder1.ContainsChildFolder(folderToMoveId), Is.False);
  244. }
  245. [Test]
  246. public void TestPurgeFolder()
  247. {
  248. TestHelper.InMethod();
  249. //log4net.Config.XmlConfigurator.Configure();
  250. Scene myScene = SceneSetupHelpers.SetupScene("inventory");
  251. CachedUserInfo userInfo;
  252. lock (this)
  253. {
  254. userInfo = UserProfileTestUtils.CreateUserWithInventory(myScene.CommsManager, InventoryReceived);
  255. Monitor.Wait(this, 60000);
  256. }
  257. UUID folder1Id = UUID.Parse("00000000-0000-0000-0000-000000000070");
  258. InventoryFolderImpl rootFolder = userInfo.RootFolder;
  259. InventoryFolderBase myFolder = new InventoryFolderBase();
  260. myFolder.ID = folder1Id;
  261. userInfo.CreateFolder("folder1", folder1Id, (ushort)AssetType.Animation, rootFolder.ID);
  262. Assert.That(myScene.InventoryService.QueryFolder(myFolder), Is.Not.Null);
  263. // Test purge
  264. userInfo.PurgeFolder(rootFolder.ID);
  265. Assert.That(rootFolder.RequestListOfFolders(), Is.Empty);
  266. Assert.That(myScene.InventoryService.QueryFolder(myFolder), Is.Null);
  267. }
  268. }
  269. }