LSL_ApiNotecardTests.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Net;
  5. using System.Reflection;
  6. using System.Text;
  7. using log4net;
  8. using Nini.Config;
  9. using NUnit.Framework;
  10. using OpenMetaverse;
  11. using OpenSim.Framework;
  12. using OpenSim.Framework.Servers;
  13. using OpenSim.Framework.Servers.HttpServer;
  14. using OpenSim.Region.CoreModules.Scripting.LSLHttp;
  15. using OpenSim.Region.Framework.Scenes;
  16. using OpenSim.Region.ScriptEngine.Shared;
  17. using OpenSim.Region.ScriptEngine.Shared.Api;
  18. using OpenSim.Region.ScriptEngine.Shared.ScriptBase;
  19. using OpenSim.Services.Interfaces;
  20. using OpenSim.Tests.Common;
  21. namespace OpenSim.Region.ScriptEngine.Shared.Tests
  22. {
  23. /// <summary>
  24. /// Tests for notecard related functions in LSL
  25. /// </summary>
  26. [TestFixture]
  27. public class LSL_ApiNotecardTests : OpenSimTestCase
  28. {
  29. private Scene m_scene;
  30. private MockScriptEngine m_engine;
  31. private SceneObjectGroup m_so;
  32. private TaskInventoryItem m_scriptItem;
  33. private LSL_Api m_lslApi;
  34. [TestFixtureSetUp]
  35. public void TestFixtureSetUp()
  36. {
  37. // Don't allow tests to be bamboozled by asynchronous events. Execute everything on the same thread.
  38. Util.FireAndForgetMethod = FireAndForgetMethod.RegressionTest;
  39. }
  40. [TestFixtureTearDown]
  41. public void TestFixureTearDown()
  42. {
  43. // We must set this back afterwards, otherwise later tests will fail since they're expecting multiple
  44. // threads. Possibly, later tests should be rewritten so none of them require async stuff (which regression
  45. // tests really shouldn't).
  46. Util.FireAndForgetMethod = Util.DefaultFireAndForgetMethod;
  47. }
  48. [SetUp]
  49. public override void SetUp()
  50. {
  51. base.SetUp();
  52. m_engine = new MockScriptEngine();
  53. m_scene = new SceneHelpers().SetupScene();
  54. SceneHelpers.SetupSceneModules(m_scene, new IniConfigSource(), m_engine);
  55. m_so = SceneHelpers.AddSceneObject(m_scene);
  56. m_scriptItem = TaskInventoryHelpers.AddScript(m_scene.AssetService, m_so.RootPart);
  57. // This is disconnected from the actual script - the mock engine does not set up any LSL_Api atm.
  58. // Possibly this could be done and we could obtain it directly from the MockScriptEngine.
  59. m_lslApi = new LSL_Api();
  60. m_lslApi.Initialize(m_engine, m_so.RootPart, m_scriptItem, null);
  61. }
  62. [Test]
  63. public void TestLlGetNotecardLine()
  64. {
  65. TestHelpers.InMethod();
  66. string[] ncLines = { "One", "Twoè", "Three" };
  67. TaskInventoryItem ncItem
  68. = TaskInventoryHelpers.AddNotecard(m_scene.AssetService, m_so.RootPart, "nc", "1", "10", string.Join("\n", ncLines));
  69. AssertValidNotecardLine(ncItem.Name, 0, ncLines[0]);
  70. AssertValidNotecardLine(ncItem.Name, 2, ncLines[2]);
  71. AssertValidNotecardLine(ncItem.Name, 3, ScriptBaseClass.EOF);
  72. AssertValidNotecardLine(ncItem.Name, 4, ScriptBaseClass.EOF);
  73. // XXX: Is this correct or do we really expect no dataserver event to fire at all?
  74. AssertValidNotecardLine(ncItem.Name, -1, "");
  75. AssertValidNotecardLine(ncItem.Name, -2, "");
  76. }
  77. [Test]
  78. public void TestLlGetNotecardLine_NoNotecard()
  79. {
  80. TestHelpers.InMethod();
  81. AssertInValidNotecardLine("nc", 0);
  82. }
  83. [Test]
  84. public void TestLlGetNotecardLine_NotANotecard()
  85. {
  86. TestHelpers.InMethod();
  87. TaskInventoryItem ncItem = TaskInventoryHelpers.AddScript(m_scene.AssetService, m_so.RootPart, "nc1", "Not important");
  88. AssertInValidNotecardLine(ncItem.Name, 0);
  89. }
  90. private void AssertValidNotecardLine(string ncName, int lineNumber, string assertLine)
  91. {
  92. string key = m_lslApi.llGetNotecardLine(ncName, lineNumber);
  93. Assert.That(key, Is.Not.EqualTo(UUID.Zero.ToString()));
  94. Assert.That(m_engine.PostedEvents.Count, Is.EqualTo(1));
  95. Assert.That(m_engine.PostedEvents.ContainsKey(m_scriptItem.ItemID));
  96. List<EventParams> events = m_engine.PostedEvents[m_scriptItem.ItemID];
  97. Assert.That(events.Count, Is.EqualTo(1));
  98. EventParams eventParams = events[0];
  99. Assert.That(eventParams.EventName, Is.EqualTo("dataserver"));
  100. Assert.That(eventParams.Params[0].ToString(), Is.EqualTo(key));
  101. Assert.That(eventParams.Params[1].ToString(), Is.EqualTo(assertLine));
  102. m_engine.ClearPostedEvents();
  103. }
  104. private void AssertInValidNotecardLine(string ncName, int lineNumber)
  105. {
  106. string key = m_lslApi.llGetNotecardLine(ncName, lineNumber);
  107. Assert.That(key, Is.EqualTo(UUID.Zero.ToString()));
  108. Assert.That(m_engine.PostedEvents.Count, Is.EqualTo(0));
  109. }
  110. // [Test]
  111. // public void TestLlReleaseUrl()
  112. // {
  113. // TestHelpers.InMethod();
  114. //
  115. // m_lslApi.llRequestURL();
  116. // string returnedUri = m_engine.PostedEvents[m_scriptItem.ItemID][0].Params[2].ToString();
  117. //
  118. // {
  119. // // Check that the initial number of URLs is correct
  120. // Assert.That(m_lslApi.llGetFreeURLs().value, Is.EqualTo(m_urlModule.TotalUrls - 1));
  121. // }
  122. //
  123. // {
  124. // // Check releasing a non-url
  125. // m_lslApi.llReleaseURL("GARBAGE");
  126. // Assert.That(m_lslApi.llGetFreeURLs().value, Is.EqualTo(m_urlModule.TotalUrls - 1));
  127. // }
  128. //
  129. // {
  130. // // Check releasing a non-existing url
  131. // m_lslApi.llReleaseURL("http://example.com");
  132. // Assert.That(m_lslApi.llGetFreeURLs().value, Is.EqualTo(m_urlModule.TotalUrls - 1));
  133. // }
  134. //
  135. // {
  136. // // Check URL release
  137. // m_lslApi.llReleaseURL(returnedUri);
  138. // Assert.That(m_lslApi.llGetFreeURLs().value, Is.EqualTo(m_urlModule.TotalUrls));
  139. //
  140. // HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(returnedUri);
  141. //
  142. // bool gotExpectedException = false;
  143. //
  144. // try
  145. // {
  146. // using (HttpWebResponse webResponse = (HttpWebResponse)webRequest.GetResponse())
  147. // {}
  148. // }
  149. // catch (WebException e)
  150. // {
  151. // using (HttpWebResponse response = (HttpWebResponse)e.Response)
  152. // gotExpectedException = response.StatusCode == HttpStatusCode.NotFound;
  153. // }
  154. //
  155. // Assert.That(gotExpectedException, Is.True);
  156. // }
  157. //
  158. // {
  159. // // Check releasing the same URL again
  160. // m_lslApi.llReleaseURL(returnedUri);
  161. // Assert.That(m_lslApi.llGetFreeURLs().value, Is.EqualTo(m_urlModule.TotalUrls));
  162. // }
  163. // }
  164. //
  165. // [Test]
  166. // public void TestLlRequestUrl()
  167. // {
  168. // TestHelpers.InMethod();
  169. //
  170. // string requestId = m_lslApi.llRequestURL();
  171. // Assert.That(requestId, Is.Not.EqualTo(UUID.Zero.ToString()));
  172. // string returnedUri;
  173. //
  174. // {
  175. // // Check that URL is correctly set up
  176. // Assert.That(m_lslApi.llGetFreeURLs().value, Is.EqualTo(m_urlModule.TotalUrls - 1));
  177. //
  178. // Assert.That(m_engine.PostedEvents.ContainsKey(m_scriptItem.ItemID));
  179. //
  180. // List<EventParams> events = m_engine.PostedEvents[m_scriptItem.ItemID];
  181. // Assert.That(events.Count, Is.EqualTo(1));
  182. // EventParams eventParams = events[0];
  183. // Assert.That(eventParams.EventName, Is.EqualTo("http_request"));
  184. //
  185. // UUID returnKey;
  186. // string rawReturnKey = eventParams.Params[0].ToString();
  187. // string method = eventParams.Params[1].ToString();
  188. // returnedUri = eventParams.Params[2].ToString();
  189. //
  190. // Assert.That(UUID.TryParse(rawReturnKey, out returnKey), Is.True);
  191. // Assert.That(method, Is.EqualTo(ScriptBaseClass.URL_REQUEST_GRANTED));
  192. // Assert.That(Uri.IsWellFormedUriString(returnedUri, UriKind.Absolute), Is.True);
  193. // }
  194. //
  195. // {
  196. // // Check that request to URL works.
  197. // string testResponse = "Hello World";
  198. //
  199. // m_engine.ClearPostedEvents();
  200. // m_engine.PostEventHook
  201. // += (itemId, evp) => m_lslApi.llHTTPResponse(evp.Params[0].ToString(), 200, testResponse);
  202. //
  203. //// Console.WriteLine("Trying {0}", returnedUri);
  204. // HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(returnedUri);
  205. //
  206. // AssertHttpResponse(returnedUri, testResponse);
  207. //
  208. // Assert.That(m_engine.PostedEvents.ContainsKey(m_scriptItem.ItemID));
  209. //
  210. // List<EventParams> events = m_engine.PostedEvents[m_scriptItem.ItemID];
  211. // Assert.That(events.Count, Is.EqualTo(1));
  212. // EventParams eventParams = events[0];
  213. // Assert.That(eventParams.EventName, Is.EqualTo("http_request"));
  214. //
  215. // UUID returnKey;
  216. // string rawReturnKey = eventParams.Params[0].ToString();
  217. // string method = eventParams.Params[1].ToString();
  218. // string body = eventParams.Params[2].ToString();
  219. //
  220. // Assert.That(UUID.TryParse(rawReturnKey, out returnKey), Is.True);
  221. // Assert.That(method, Is.EqualTo("GET"));
  222. // Assert.That(body, Is.EqualTo(""));
  223. // }
  224. // }
  225. //
  226. // private void AssertHttpResponse(string uri, string expectedResponse)
  227. // {
  228. // HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(uri);
  229. //
  230. // using (HttpWebResponse webResponse = (HttpWebResponse)webRequest.GetResponse())
  231. // {
  232. // using (Stream stream = webResponse.GetResponseStream())
  233. // {
  234. // using (StreamReader reader = new StreamReader(stream))
  235. // {
  236. // Assert.That(reader.ReadToEnd(), Is.EqualTo(expectedResponse));
  237. // }
  238. // }
  239. // }
  240. // }
  241. }
  242. }