FetchInventoryDescendents2HandlerTests.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  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.IO;
  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. private string dorequest(FetchInvDescHandler handler, string request)
  116. {
  117. TestOSHttpRequest req = new TestOSHttpRequest();
  118. TestOSHttpResponse resp = new TestOSHttpResponse();
  119. using(ExpiringKey<UUID> bad = new ExpiringKey<UUID>(5000)) // bad but this is test
  120. using (MemoryStream ms = new MemoryStream(Utils.StringToBytes(request), false))
  121. {
  122. req.InputStream = ms;
  123. handler.FetchInventoryDescendentsRequest(req, resp, bad);
  124. }
  125. return Util.UTF8.GetString(resp.RawBuffer);
  126. }
  127. [Test]
  128. public void Test_001_SimpleFolder()
  129. {
  130. TestHelpers.InMethod();
  131. Init();
  132. FetchInvDescHandler handler = new FetchInvDescHandler(m_scene.InventoryService, null, m_scene);
  133. 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>";
  134. request += m_rootFolderID;
  135. request += "</uuid><key>owner_id</key><uuid>";
  136. request += m_userID.ToString();
  137. request += "</uuid><key>sort_order</key><integer>1</integer></map></array></map></llsd>";
  138. string llsdresponse = dorequest(handler, request);
  139. Assert.That(llsdresponse != null, Is.True, "Incorrect null response");
  140. Assert.That(llsdresponse != string.Empty, Is.True, "Incorrect empty response");
  141. Assert.That(llsdresponse.Contains(m_userID.ToString()), Is.True, "Response should contain userID");
  142. string descendents = "descendents</key><integer>" + m_rootDescendents + "</integer>";
  143. Assert.That(llsdresponse.Contains(descendents), Is.True, "Incorrect number of descendents");
  144. Console.WriteLine(llsdresponse);
  145. }
  146. [Test]
  147. public void Test_002_MultipleFolders()
  148. {
  149. TestHelpers.InMethod();
  150. FetchInvDescHandler handler = new FetchInvDescHandler(m_scene.InventoryService, null, m_scene);
  151. string request = "<llsd><map><key>folders</key><array>";
  152. request += "<map><key>fetch_folders</key><integer>1</integer><key>fetch_items</key><boolean>1</boolean><key>folder_id</key><uuid>";
  153. request += m_rootFolderID;
  154. request += "</uuid><key>owner_id</key><uuid>00000000-0000-0000-0000-000000000001</uuid><key>sort_order</key><integer>1</integer></map>";
  155. request += "<map><key>fetch_folders</key><integer>1</integer><key>fetch_items</key><boolean>1</boolean><key>folder_id</key><uuid>";
  156. request += m_notecardsFolder;
  157. request += "</uuid><key>owner_id</key><uuid>00000000-0000-0000-0000-000000000001</uuid><key>sort_order</key><integer>1</integer></map>";
  158. request += "</array></map></llsd>";
  159. string llsdresponse = dorequest(handler, request);
  160. Console.WriteLine(llsdresponse);
  161. string descendents = "descendents</key><integer>" + m_rootDescendents + "</integer>";
  162. Assert.That(llsdresponse.Contains(descendents), Is.True, "Incorrect number of descendents for root folder");
  163. descendents = "descendents</key><integer>2</integer>";
  164. Assert.That(llsdresponse.Contains(descendents), Is.True, "Incorrect number of descendents for Notecard folder");
  165. Assert.That(llsdresponse.Contains("10000000-0000-0000-0000-000000000001"), Is.True, "Notecard 1 is missing from response");
  166. Assert.That(llsdresponse.Contains("20000000-0000-0000-0000-000000000002"), Is.True, "Notecard 2 is missing from response");
  167. }
  168. [Test]
  169. public void Test_003_Links()
  170. {
  171. TestHelpers.InMethod();
  172. FetchInvDescHandler handler = new FetchInvDescHandler(m_scene.InventoryService, null, m_scene);
  173. 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>";
  174. request += "f0000000-0000-0000-0000-00000000000f";
  175. request += "</uuid><key>owner_id</key><uuid>00000000-0000-0000-0000-000000000001</uuid><key>sort_order</key><integer>1</integer></map></array></map></llsd>";
  176. string llsdresponse = dorequest(handler, request);
  177. Console.WriteLine(llsdresponse);
  178. string descendents = "descendents</key><integer>2</integer>";
  179. Assert.That(llsdresponse.Contains(descendents), Is.True, "Incorrect number of descendents for Test Folder");
  180. // Make sure that the note card link is included
  181. Assert.That(llsdresponse.Contains("Link to notecard"), Is.True, "Link to notecard is missing");
  182. //Make sure the notecard item itself is included
  183. Assert.That(llsdresponse.Contains("Test Notecard 2"), Is.True, "Notecard 2 item (the source) is missing");
  184. // Make sure that the source item is before the link item
  185. int pos1 = llsdresponse.IndexOf("Test Notecard 2");
  186. int pos2 = llsdresponse.IndexOf("Link to notecard");
  187. Assert.Less(pos1, pos2, "Source of link is after link");
  188. // Make sure the folder link is included
  189. Assert.That(llsdresponse.Contains("Link to Objects folder"), Is.True, "Link to Objects folder is missing");
  190. /* contents of link folder are not supposed to be listed
  191. // Make sure the objects inside the Objects folder are included
  192. // Note: I'm not entirely sure this is needed, but that's what I found in the implementation
  193. Assert.That(llsdresponse.Contains("Some Object"), Is.True, "Some Object item (contents of the source) is missing");
  194. */
  195. // Make sure that the source item is before the link item
  196. pos1 = llsdresponse.IndexOf("Some Object");
  197. pos2 = llsdresponse.IndexOf("Link to Objects folder");
  198. Assert.Less(pos1, pos2, "Contents of source of folder link is after folder link");
  199. }
  200. [Test]
  201. public void Test_004_DuplicateFolders()
  202. {
  203. TestHelpers.InMethod();
  204. FetchInvDescHandler handler = new FetchInvDescHandler(m_scene.InventoryService, null, m_scene);
  205. string request = "<llsd><map><key>folders</key><array>";
  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_rootFolderID;
  208. request += "</uuid><key>owner_id</key><uuid>00000000-0000-0000-0000-000000000000</uuid><key>sort_order</key><integer>1</integer></map>";
  209. request += "<map><key>fetch_folders</key><integer>1</integer><key>fetch_items</key><boolean>1</boolean><key>folder_id</key><uuid>";
  210. request += m_notecardsFolder;
  211. request += "</uuid><key>owner_id</key><uuid>00000000-0000-0000-0000-000000000000</uuid><key>sort_order</key><integer>1</integer></map>";
  212. request += "<map><key>fetch_folders</key><integer>1</integer><key>fetch_items</key><boolean>1</boolean><key>folder_id</key><uuid>";
  213. request += m_rootFolderID;
  214. request += "</uuid><key>owner_id</key><uuid>00000000-0000-0000-0000-000000000000</uuid><key>sort_order</key><integer>1</integer></map>";
  215. request += "<map><key>fetch_folders</key><integer>1</integer><key>fetch_items</key><boolean>1</boolean><key>folder_id</key><uuid>";
  216. request += m_notecardsFolder;
  217. request += "</uuid><key>owner_id</key><uuid>00000000-0000-0000-0000-000000000000</uuid><key>sort_order</key><integer>1</integer></map>";
  218. request += "</array></map></llsd>";
  219. string llsdresponse = dorequest(handler, request);
  220. Console.WriteLine(llsdresponse);
  221. string root_folder = "<key>folder_id</key><uuid>" + m_rootFolderID + "</uuid>";
  222. string notecards_folder = "<key>folder_id</key><uuid>" + m_notecardsFolder + "</uuid>";
  223. string notecards_category = "<key>category_id</key><uuid>" + m_notecardsFolder + "</uuid>";
  224. Assert.That(llsdresponse.Contains(root_folder), "Missing root folder");
  225. Assert.That(llsdresponse.Contains(notecards_folder), "Missing notecards folder");
  226. int count = Regex.Matches(llsdresponse, root_folder).Count;
  227. Assert.AreEqual(1, count, "More than 1 root folder in response");
  228. count = Regex.Matches(llsdresponse, notecards_folder).Count;
  229. Assert.AreEqual(1, count, "More than 1 notecards folder in response");
  230. count = Regex.Matches(llsdresponse, notecards_category).Count;
  231. Assert.AreEqual(1, count, "More than 1 notecards folder in response"); // Notecards will also be a category on root
  232. }
  233. [Test]
  234. public void Test_005_FolderZero()
  235. {
  236. TestHelpers.InMethod();
  237. Init();
  238. FetchInvDescHandler handler = new FetchInvDescHandler(m_scene.InventoryService, null, m_scene);
  239. 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>";
  240. request += UUID.Zero;
  241. request += "</uuid><key>owner_id</key><uuid>00000000-0000-0000-0000-000000000000</uuid><key>sort_order</key><integer>1</integer></map></array></map></llsd>";
  242. string llsdresponse = dorequest(handler, request);
  243. Assert.That(llsdresponse != null, Is.True, "Incorrect null response");
  244. Assert.That(llsdresponse != string.Empty, Is.True, "Incorrect empty response");
  245. // we do return a answer now
  246. //Assert.That(llsdresponse.Contains("bad_folders</key><array><uuid>00000000-0000-0000-0000-000000000000"), Is.True, "Folder Zero should be a bad folder");
  247. Console.WriteLine(llsdresponse);
  248. }
  249. }
  250. }