FetchInventoryDescendents2HandlerTests.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  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.Linq;
  30. using System.Net;
  31. using System.Text.RegularExpressions;
  32. using log4net;
  33. using log4net.Config;
  34. using NUnit.Framework;
  35. using OpenMetaverse;
  36. using OpenSim.Capabilities.Handlers;
  37. using OpenSim.Framework;
  38. using OpenSim.Framework.Servers.HttpServer;
  39. using OpenSim.Region.Framework.Scenes;
  40. using OpenSim.Services.Interfaces;
  41. using OpenSim.Tests.Common;
  42. namespace OpenSim.Capabilities.Handlers.FetchInventory.Tests
  43. {
  44. [TestFixture]
  45. public class FetchInventoryDescendents2HandlerTests : OpenSimTestCase
  46. {
  47. private UUID m_userID = UUID.Zero;
  48. private Scene m_scene;
  49. private UUID m_rootFolderID;
  50. private int m_rootDescendents;
  51. private UUID m_notecardsFolder;
  52. private UUID m_objectsFolder;
  53. private void Init()
  54. {
  55. // Create an inventory that looks like this:
  56. //
  57. // /My Inventory
  58. // <other system folders>
  59. // /Objects
  60. // Some Object
  61. // /Notecards
  62. // Notecard 1
  63. // Notecard 2
  64. // /Test Folder
  65. // Link to notecard -> /Notecards/Notecard 2
  66. // Link to Objects folder -> /Objects
  67. m_scene = new SceneHelpers().SetupScene();
  68. m_scene.InventoryService.CreateUserInventory(m_userID);
  69. m_rootFolderID = m_scene.InventoryService.GetRootFolder(m_userID).ID;
  70. InventoryFolderBase of = m_scene.InventoryService.GetFolderForType(m_userID, FolderType.Object);
  71. m_objectsFolder = of.ID;
  72. // Add an object
  73. InventoryItemBase item = new InventoryItemBase(new UUID("b0000000-0000-0000-0000-00000000000b"), m_userID);
  74. item.AssetID = UUID.Random();
  75. item.AssetType = (int)AssetType.Object;
  76. item.Folder = m_objectsFolder;
  77. item.Name = "Some Object";
  78. m_scene.InventoryService.AddItem(item);
  79. InventoryFolderBase ncf = m_scene.InventoryService.GetFolderForType(m_userID, FolderType.Notecard);
  80. m_notecardsFolder = ncf.ID;
  81. // Add a notecard
  82. item = new InventoryItemBase(new UUID("10000000-0000-0000-0000-000000000001"), m_userID);
  83. item.AssetID = UUID.Random();
  84. item.AssetType = (int)AssetType.Notecard;
  85. item.Folder = m_notecardsFolder;
  86. item.Name = "Test Notecard 1";
  87. m_scene.InventoryService.AddItem(item);
  88. // Add another notecard
  89. item.ID = new UUID("20000000-0000-0000-0000-000000000002");
  90. item.AssetID = new UUID("a0000000-0000-0000-0000-00000000000a");
  91. item.Name = "Test Notecard 2";
  92. m_scene.InventoryService.AddItem(item);
  93. // Add a folder
  94. InventoryFolderBase folder = new InventoryFolderBase(new UUID("f0000000-0000-0000-0000-00000000000f"), "Test Folder", m_userID, m_rootFolderID);
  95. m_scene.InventoryService.AddFolder(folder);
  96. // Add a link to notecard 2 in Test Folder
  97. item.AssetID = item.ID; // use item ID of notecard 2
  98. item.ID = new UUID("40000000-0000-0000-0000-000000000004");
  99. item.AssetType = (int)AssetType.Link;
  100. item.Folder = folder.ID;
  101. item.Name = "Link to notecard";
  102. m_scene.InventoryService.AddItem(item);
  103. // Add a link to the Objects folder in Test Folder
  104. item.AssetID = m_scene.InventoryService.GetFolderForType(m_userID, FolderType.Object).ID; // use item ID of Objects folder
  105. item.ID = new UUID("50000000-0000-0000-0000-000000000005");
  106. item.AssetType = (int)AssetType.LinkFolder;
  107. item.Folder = folder.ID;
  108. item.Name = "Link to Objects folder";
  109. m_scene.InventoryService.AddItem(item);
  110. InventoryCollection coll = m_scene.InventoryService.GetFolderContent(m_userID, m_rootFolderID);
  111. m_rootDescendents = coll.Items.Count + coll.Folders.Count;
  112. Console.WriteLine("Number of descendents: " + m_rootDescendents);
  113. }
  114. [Test]
  115. public void Test_001_SimpleFolder()
  116. {
  117. TestHelpers.InMethod();
  118. Init();
  119. FetchInvDescHandler handler = new FetchInvDescHandler(m_scene.InventoryService, null, m_scene);
  120. TestOSHttpRequest req = new TestOSHttpRequest();
  121. TestOSHttpResponse resp = new TestOSHttpResponse();
  122. string request = "<llsd><map><key>folders</key><array><map><key>fetch_folders</key><integer>1</integer><key>fetch_items</key><boolean>1</boolean><key>folder_id</key><uuid>";
  123. request += m_rootFolderID;
  124. request += "</uuid><key>owner_id</key><uuid>00000000-0000-0000-0000-000000000000</uuid><key>sort_order</key><integer>1</integer></map></array></map></llsd>";
  125. string llsdresponse = handler.FetchInventoryDescendentsRequest(request, "/FETCH", string.Empty, req, resp);
  126. Assert.That(llsdresponse != null, Is.True, "Incorrect null response");
  127. Assert.That(llsdresponse != string.Empty, Is.True, "Incorrect empty response");
  128. Assert.That(llsdresponse.Contains("00000000-0000-0000-0000-000000000000"), Is.True, "Response should contain userID");
  129. string descendents = "descendents</key><integer>" + m_rootDescendents + "</integer>";
  130. Assert.That(llsdresponse.Contains(descendents), Is.True, "Incorrect number of descendents");
  131. Console.WriteLine(llsdresponse);
  132. }
  133. [Test]
  134. public void Test_002_MultipleFolders()
  135. {
  136. TestHelpers.InMethod();
  137. FetchInvDescHandler handler = new FetchInvDescHandler(m_scene.InventoryService, null, m_scene);
  138. TestOSHttpRequest req = new TestOSHttpRequest();
  139. TestOSHttpResponse resp = new TestOSHttpResponse();
  140. string request = "<llsd><map><key>folders</key><array>";
  141. request += "<map><key>fetch_folders</key><integer>1</integer><key>fetch_items</key><boolean>1</boolean><key>folder_id</key><uuid>";
  142. request += m_rootFolderID;
  143. request += "</uuid><key>owner_id</key><uuid>00000000-0000-0000-0000-000000000000</uuid><key>sort_order</key><integer>1</integer></map>";
  144. request += "<map><key>fetch_folders</key><integer>1</integer><key>fetch_items</key><boolean>1</boolean><key>folder_id</key><uuid>";
  145. request += m_notecardsFolder;
  146. request += "</uuid><key>owner_id</key><uuid>00000000-0000-0000-0000-000000000000</uuid><key>sort_order</key><integer>1</integer></map>";
  147. request += "</array></map></llsd>";
  148. string llsdresponse = handler.FetchInventoryDescendentsRequest(request, "/FETCH", string.Empty, req, resp);
  149. Console.WriteLine(llsdresponse);
  150. string descendents = "descendents</key><integer>" + m_rootDescendents + "</integer>";
  151. Assert.That(llsdresponse.Contains(descendents), Is.True, "Incorrect number of descendents for root folder");
  152. descendents = "descendents</key><integer>2</integer>";
  153. Assert.That(llsdresponse.Contains(descendents), Is.True, "Incorrect number of descendents for Notecard folder");
  154. Assert.That(llsdresponse.Contains("10000000-0000-0000-0000-000000000001"), Is.True, "Notecard 1 is missing from response");
  155. Assert.That(llsdresponse.Contains("20000000-0000-0000-0000-000000000002"), Is.True, "Notecard 2 is missing from response");
  156. }
  157. [Test]
  158. public void Test_003_Links()
  159. {
  160. TestHelpers.InMethod();
  161. FetchInvDescHandler handler = new FetchInvDescHandler(m_scene.InventoryService, null, m_scene);
  162. TestOSHttpRequest req = new TestOSHttpRequest();
  163. TestOSHttpResponse resp = new TestOSHttpResponse();
  164. string request = "<llsd><map><key>folders</key><array><map><key>fetch_folders</key><integer>1</integer><key>fetch_items</key><boolean>1</boolean><key>folder_id</key><uuid>";
  165. request += "f0000000-0000-0000-0000-00000000000f";
  166. request += "</uuid><key>owner_id</key><uuid>00000000-0000-0000-0000-000000000000</uuid><key>sort_order</key><integer>1</integer></map></array></map></llsd>";
  167. string llsdresponse = handler.FetchInventoryDescendentsRequest(request, "/FETCH", string.Empty, req, resp);
  168. Console.WriteLine(llsdresponse);
  169. string descendents = "descendents</key><integer>2</integer>";
  170. Assert.That(llsdresponse.Contains(descendents), Is.True, "Incorrect number of descendents for Test Folder");
  171. // Make sure that the note card link is included
  172. Assert.That(llsdresponse.Contains("Link to notecard"), Is.True, "Link to notecard is missing");
  173. //Make sure the notecard item itself is included
  174. Assert.That(llsdresponse.Contains("Test Notecard 2"), Is.True, "Notecard 2 item (the source) is missing");
  175. // Make sure that the source item is before the link item
  176. int pos1 = llsdresponse.IndexOf("Test Notecard 2");
  177. int pos2 = llsdresponse.IndexOf("Link to notecard");
  178. Assert.Less(pos1, pos2, "Source of link is after link");
  179. // Make sure the folder link is included
  180. Assert.That(llsdresponse.Contains("Link to Objects folder"), Is.True, "Link to Objects folder is missing");
  181. // Make sure the objects inside the Objects folder are included
  182. // Note: I'm not entirely sure this is needed, but that's what I found in the implementation
  183. Assert.That(llsdresponse.Contains("Some Object"), Is.True, "Some Object item (contents of the source) is missing");
  184. // Make sure that the source item is before the link item
  185. pos1 = llsdresponse.IndexOf("Some Object");
  186. pos2 = llsdresponse.IndexOf("Link to Objects folder");
  187. Assert.Less(pos1, pos2, "Contents of source of folder link is after folder link");
  188. }
  189. [Test]
  190. public void Test_004_DuplicateFolders()
  191. {
  192. TestHelpers.InMethod();
  193. FetchInvDescHandler handler = new FetchInvDescHandler(m_scene.InventoryService, null, m_scene);
  194. TestOSHttpRequest req = new TestOSHttpRequest();
  195. TestOSHttpResponse resp = new TestOSHttpResponse();
  196. string request = "<llsd><map><key>folders</key><array>";
  197. request += "<map><key>fetch_folders</key><integer>1</integer><key>fetch_items</key><boolean>1</boolean><key>folder_id</key><uuid>";
  198. request += m_rootFolderID;
  199. request += "</uuid><key>owner_id</key><uuid>00000000-0000-0000-0000-000000000000</uuid><key>sort_order</key><integer>1</integer></map>";
  200. request += "<map><key>fetch_folders</key><integer>1</integer><key>fetch_items</key><boolean>1</boolean><key>folder_id</key><uuid>";
  201. request += m_notecardsFolder;
  202. request += "</uuid><key>owner_id</key><uuid>00000000-0000-0000-0000-000000000000</uuid><key>sort_order</key><integer>1</integer></map>";
  203. request += "<map><key>fetch_folders</key><integer>1</integer><key>fetch_items</key><boolean>1</boolean><key>folder_id</key><uuid>";
  204. request += m_rootFolderID;
  205. request += "</uuid><key>owner_id</key><uuid>00000000-0000-0000-0000-000000000000</uuid><key>sort_order</key><integer>1</integer></map>";
  206. request += "<map><key>fetch_folders</key><integer>1</integer><key>fetch_items</key><boolean>1</boolean><key>folder_id</key><uuid>";
  207. request += m_notecardsFolder;
  208. request += "</uuid><key>owner_id</key><uuid>00000000-0000-0000-0000-000000000000</uuid><key>sort_order</key><integer>1</integer></map>";
  209. request += "</array></map></llsd>";
  210. string llsdresponse = handler.FetchInventoryDescendentsRequest(request, "/FETCH", string.Empty, req, resp);
  211. Console.WriteLine(llsdresponse);
  212. string root_folder = "<key>folder_id</key><uuid>" + m_rootFolderID + "</uuid>";
  213. string notecards_folder = "<key>folder_id</key><uuid>" + m_notecardsFolder + "</uuid>";
  214. Assert.That(llsdresponse.Contains(root_folder), "Missing root folder");
  215. Assert.That(llsdresponse.Contains(notecards_folder), "Missing notecards folder");
  216. int count = Regex.Matches(llsdresponse, root_folder).Count;
  217. Assert.AreEqual(1, count, "More than 1 root folder in response");
  218. count = Regex.Matches(llsdresponse, notecards_folder).Count;
  219. Assert.AreEqual(2, count, "More than 1 notecards folder in response"); // Notecards will also be under root, so 2
  220. }
  221. [Test]
  222. public void Test_005_FolderZero()
  223. {
  224. TestHelpers.InMethod();
  225. Init();
  226. FetchInvDescHandler handler = new FetchInvDescHandler(m_scene.InventoryService, null, m_scene);
  227. TestOSHttpRequest req = new TestOSHttpRequest();
  228. TestOSHttpResponse resp = new TestOSHttpResponse();
  229. string request = "<llsd><map><key>folders</key><array><map><key>fetch_folders</key><integer>1</integer><key>fetch_items</key><boolean>1</boolean><key>folder_id</key><uuid>";
  230. request += UUID.Zero;
  231. request += "</uuid><key>owner_id</key><uuid>00000000-0000-0000-0000-000000000000</uuid><key>sort_order</key><integer>1</integer></map></array></map></llsd>";
  232. string llsdresponse = handler.FetchInventoryDescendentsRequest(request, "/FETCH", string.Empty, req, resp);
  233. Assert.That(llsdresponse != null, Is.True, "Incorrect null response");
  234. Assert.That(llsdresponse != string.Empty, Is.True, "Incorrect empty response");
  235. Assert.That(llsdresponse.Contains("bad_folders</key><array><uuid>00000000-0000-0000-0000-000000000000"), Is.True, "Folder Zero should be a bad folder");
  236. Console.WriteLine(llsdresponse);
  237. }
  238. }
  239. }