1
0

LSL_ApiNotecardTests.cs 10 KB

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