FetchInventoryDescendents2HandlerTests.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  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 = new UUID("00000000-0000-0000-0000-000000000001");
  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. folder.Type = (short)FolderType.None;
  96. m_scene.InventoryService.AddFolder(folder);
  97. // Add a link to notecard 2 in Test Folder
  98. item.AssetID = item.ID; // use item ID of notecard 2
  99. item.ID = new UUID("40000000-0000-0000-0000-000000000004");
  100. item.AssetType = (int)AssetType.Link;
  101. item.Folder = folder.ID;
  102. item.Name = "Link to notecard";
  103. m_scene.InventoryService.AddItem(item);
  104. // Add a link to the Objects folder in Test Folder
  105. item.AssetID = m_scene.InventoryService.GetFolderForType(m_userID, FolderType.Object).ID; // use item ID of Objects folder
  106. item.ID = new UUID("50000000-0000-0000-0000-000000000005");
  107. item.AssetType = (int)AssetType.LinkFolder;
  108. item.Folder = folder.ID;
  109. item.Name = "Link to Objects folder";
  110. m_scene.InventoryService.AddItem(item);
  111. InventoryCollection coll = m_scene.InventoryService.GetFolderContent(m_userID, m_rootFolderID);
  112. m_rootDescendents = coll.Items.Count + coll.Folders.Count;
  113. Console.WriteLine("Number of descendents: " + m_rootDescendents);
  114. }
  115. [Test]
  116. public void Test_001_SimpleFolder()
  117. {
  118. TestHelpers.InMethod();
  119. Init();
  120. FetchInvDescHandler handler = new FetchInvDescHandler(m_scene.InventoryService, null, m_scene);
  121. TestOSHttpRequest req = new TestOSHttpRequest();
  122. TestOSHttpResponse resp = new TestOSHttpResponse();
  123. 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>";
  124. request += m_rootFolderID;
  125. request += "</uuid><key>owner_id</key><uuid>";
  126. request += m_userID.ToString();
  127. request += "</uuid><key>sort_order</key><integer>1</integer></map></array></map></llsd>";
  128. string llsdresponse = handler.FetchInventoryDescendentsRequest(request, "/FETCH", string.Empty, req, resp);
  129. Assert.That(llsdresponse != null, Is.True, "Incorrect null response");
  130. Assert.That(llsdresponse != string.Empty, Is.True, "Incorrect empty response");
  131. Assert.That(llsdresponse.Contains(m_userID.ToString()), Is.True, "Response should contain userID");
  132. string descendents = "descendents</key><integer>" + m_rootDescendents + "</integer>";
  133. Assert.That(llsdresponse.Contains(descendents), Is.True, "Incorrect number of descendents");
  134. Console.WriteLine(llsdresponse);
  135. }
  136. [Test]
  137. public void Test_002_MultipleFolders()
  138. {
  139. TestHelpers.InMethod();
  140. FetchInvDescHandler handler = new FetchInvDescHandler(m_scene.InventoryService, null, m_scene);
  141. TestOSHttpRequest req = new TestOSHttpRequest();
  142. TestOSHttpResponse resp = new TestOSHttpResponse();
  143. string request = "<llsd><map><key>folders</key><array>";
  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_rootFolderID;
  146. request += "</uuid><key>owner_id</key><uuid>00000000-0000-0000-0000-000000000001</uuid><key>sort_order</key><integer>1</integer></map>";
  147. request += "<map><key>fetch_folders</key><integer>1</integer><key>fetch_items</key><boolean>1</boolean><key>folder_id</key><uuid>";
  148. request += m_notecardsFolder;
  149. request += "</uuid><key>owner_id</key><uuid>00000000-0000-0000-0000-000000000001</uuid><key>sort_order</key><integer>1</integer></map>";
  150. request += "</array></map></llsd>";
  151. string llsdresponse = handler.FetchInventoryDescendentsRequest(request, "/FETCH", string.Empty, req, resp);
  152. Console.WriteLine(llsdresponse);
  153. string descendents = "descendents</key><integer>" + m_rootDescendents + "</integer>";
  154. Assert.That(llsdresponse.Contains(descendents), Is.True, "Incorrect number of descendents for root folder");
  155. descendents = "descendents</key><integer>2</integer>";
  156. Assert.That(llsdresponse.Contains(descendents), Is.True, "Incorrect number of descendents for Notecard folder");
  157. Assert.That(llsdresponse.Contains("10000000-0000-0000-0000-000000000001"), Is.True, "Notecard 1 is missing from response");
  158. Assert.That(llsdresponse.Contains("20000000-0000-0000-0000-000000000002"), Is.True, "Notecard 2 is missing from response");
  159. }
  160. [Test]
  161. public void Test_003_Links()
  162. {
  163. TestHelpers.InMethod();
  164. FetchInvDescHandler handler = new FetchInvDescHandler(m_scene.InventoryService, null, m_scene);
  165. TestOSHttpRequest req = new TestOSHttpRequest();
  166. TestOSHttpResponse resp = new TestOSHttpResponse();
  167. 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>";
  168. request += "f0000000-0000-0000-0000-00000000000f";
  169. request += "</uuid><key>owner_id</key><uuid>00000000-0000-0000-0000-000000000001</uuid><key>sort_order</key><integer>1</integer></map></array></map></llsd>";
  170. string llsdresponse = handler.FetchInventoryDescendentsRequest(request, "/FETCH", string.Empty, req, resp);
  171. Console.WriteLine(llsdresponse);
  172. string descendents = "descendents</key><integer>2</integer>";
  173. Assert.That(llsdresponse.Contains(descendents), Is.True, "Incorrect number of descendents for Test Folder");
  174. // Make sure that the note card link is included
  175. Assert.That(llsdresponse.Contains("Link to notecard"), Is.True, "Link to notecard is missing");
  176. //Make sure the notecard item itself is included
  177. Assert.That(llsdresponse.Contains("Test Notecard 2"), Is.True, "Notecard 2 item (the source) is missing");
  178. // Make sure that the source item is before the link item
  179. int pos1 = llsdresponse.IndexOf("Test Notecard 2");
  180. int pos2 = llsdresponse.IndexOf("Link to notecard");
  181. Assert.Less(pos1, pos2, "Source of link is after link");
  182. // Make sure the folder link is included
  183. Assert.That(llsdresponse.Contains("Link to Objects folder"), Is.True, "Link to Objects folder is missing");
  184. /* contents of link folder are not supposed to be listed
  185. // Make sure the objects inside the Objects folder are included
  186. // Note: I'm not entirely sure this is needed, but that's what I found in the implementation
  187. Assert.That(llsdresponse.Contains("Some Object"), Is.True, "Some Object item (contents of the source) is missing");
  188. */
  189. // Make sure that the source item is before the link item
  190. pos1 = llsdresponse.IndexOf("Some Object");
  191. pos2 = llsdresponse.IndexOf("Link to Objects folder");
  192. Assert.Less(pos1, pos2, "Contents of source of folder link is after folder link");
  193. }
  194. [Test]
  195. public void Test_004_DuplicateFolders()
  196. {
  197. TestHelpers.InMethod();
  198. FetchInvDescHandler handler = new FetchInvDescHandler(m_scene.InventoryService, null, m_scene);
  199. TestOSHttpRequest req = new TestOSHttpRequest();
  200. TestOSHttpResponse resp = new TestOSHttpResponse();
  201. string request = "<llsd><map><key>folders</key><array>";
  202. request += "<map><key>fetch_folders</key><integer>1</integer><key>fetch_items</key><boolean>1</boolean><key>folder_id</key><uuid>";
  203. request += m_rootFolderID;
  204. request += "</uuid><key>owner_id</key><uuid>00000000-0000-0000-0000-000000000000</uuid><key>sort_order</key><integer>1</integer></map>";
  205. request += "<map><key>fetch_folders</key><integer>1</integer><key>fetch_items</key><boolean>1</boolean><key>folder_id</key><uuid>";
  206. request += m_notecardsFolder;
  207. request += "</uuid><key>owner_id</key><uuid>00000000-0000-0000-0000-000000000000</uuid><key>sort_order</key><integer>1</integer></map>";
  208. request += "<map><key>fetch_folders</key><integer>1</integer><key>fetch_items</key><boolean>1</boolean><key>folder_id</key><uuid>";
  209. request += m_rootFolderID;
  210. request += "</uuid><key>owner_id</key><uuid>00000000-0000-0000-0000-000000000000</uuid><key>sort_order</key><integer>1</integer></map>";
  211. request += "<map><key>fetch_folders</key><integer>1</integer><key>fetch_items</key><boolean>1</boolean><key>folder_id</key><uuid>";
  212. request += m_notecardsFolder;
  213. request += "</uuid><key>owner_id</key><uuid>00000000-0000-0000-0000-000000000000</uuid><key>sort_order</key><integer>1</integer></map>";
  214. request += "</array></map></llsd>";
  215. string llsdresponse = handler.FetchInventoryDescendentsRequest(request, "/FETCH", string.Empty, req, resp);
  216. Console.WriteLine(llsdresponse);
  217. string root_folder = "<key>folder_id</key><uuid>" + m_rootFolderID + "</uuid>";
  218. string notecards_folder = "<key>folder_id</key><uuid>" + m_notecardsFolder + "</uuid>";
  219. Assert.That(llsdresponse.Contains(root_folder), "Missing root folder");
  220. Assert.That(llsdresponse.Contains(notecards_folder), "Missing notecards folder");
  221. int count = Regex.Matches(llsdresponse, root_folder).Count;
  222. Assert.AreEqual(1, count, "More than 1 root folder in response");
  223. count = Regex.Matches(llsdresponse, notecards_folder).Count;
  224. Assert.AreEqual(2, count, "More than 1 notecards folder in response"); // Notecards will also be under root, so 2
  225. }
  226. [Test]
  227. public void Test_005_FolderZero()
  228. {
  229. TestHelpers.InMethod();
  230. Init();
  231. FetchInvDescHandler handler = new FetchInvDescHandler(m_scene.InventoryService, null, m_scene);
  232. TestOSHttpRequest req = new TestOSHttpRequest();
  233. TestOSHttpResponse resp = new TestOSHttpResponse();
  234. 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>";
  235. request += UUID.Zero;
  236. request += "</uuid><key>owner_id</key><uuid>00000000-0000-0000-0000-000000000000</uuid><key>sort_order</key><integer>1</integer></map></array></map></llsd>";
  237. string llsdresponse = handler.FetchInventoryDescendentsRequest(request, "/FETCH", string.Empty, req, resp);
  238. Assert.That(llsdresponse != null, Is.True, "Incorrect null response");
  239. Assert.That(llsdresponse != string.Empty, Is.True, "Incorrect empty response");
  240. // we do return a answer now
  241. //Assert.That(llsdresponse.Contains("bad_folders</key><array><uuid>00000000-0000-0000-0000-000000000000"), Is.True, "Folder Zero should be a bad folder");
  242. Console.WriteLine(llsdresponse);
  243. }
  244. }
  245. }