1
0

LSL_ApiHttpTests.cs 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  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.IO;
  30. using System.Net;
  31. using System.Reflection;
  32. using System.Text;
  33. using log4net;
  34. using Nini.Config;
  35. using NUnit.Framework;
  36. using OpenMetaverse;
  37. using OpenSim.Framework;
  38. using OpenSim.Framework.Servers;
  39. using OpenSim.Framework.Servers.HttpServer;
  40. using OpenSim.Region.CoreModules.Scripting.LSLHttp;
  41. using OpenSim.Region.Framework.Scenes;
  42. using OpenSim.Region.ScriptEngine.Shared;
  43. using OpenSim.Region.ScriptEngine.Shared.Api;
  44. using OpenSim.Region.ScriptEngine.Shared.ScriptBase;
  45. using OpenSim.Services.Interfaces;
  46. using OpenSim.Tests.Common;
  47. namespace OpenSim.Region.ScriptEngine.Shared.Tests
  48. {
  49. /// <summary>
  50. /// Tests for HTTP related functions in LSL
  51. /// </summary>
  52. [TestFixture]
  53. public class LSL_ApiHttpTests : OpenSimTestCase
  54. {
  55. private Scene m_scene;
  56. private MockScriptEngine m_engine;
  57. private UrlModule m_urlModule;
  58. private TaskInventoryItem m_scriptItem;
  59. private LSL_Api m_lslApi;
  60. [TestFixtureSetUp]
  61. public void TestFixtureSetUp()
  62. {
  63. // Don't allow tests to be bamboozled by asynchronous events. Execute everything on the same thread.
  64. Util.FireAndForgetMethod = FireAndForgetMethod.RegressionTest;
  65. }
  66. [TestFixtureTearDown]
  67. public void TestFixureTearDown()
  68. {
  69. // We must set this back afterwards, otherwise later tests will fail since they're expecting multiple
  70. // threads. Possibly, later tests should be rewritten so none of them require async stuff (which regression
  71. // tests really shouldn't).
  72. Util.FireAndForgetMethod = Util.DefaultFireAndForgetMethod;
  73. }
  74. [SetUp]
  75. public override void SetUp()
  76. {
  77. base.SetUp();
  78. // This is an unfortunate bit of clean up we have to do because MainServer manages things through static
  79. // variables and the VM is not restarted between tests.
  80. uint port = 9999;
  81. MainServer.RemoveHttpServer(port);
  82. BaseHttpServer server = new BaseHttpServer(port, false, 0, "");
  83. MainServer.AddHttpServer(server);
  84. MainServer.Instance = server;
  85. server.Start();
  86. m_engine = new MockScriptEngine();
  87. m_urlModule = new UrlModule();
  88. m_scene = new SceneHelpers().SetupScene();
  89. SceneHelpers.SetupSceneModules(m_scene, new IniConfigSource(), m_engine, m_urlModule);
  90. SceneObjectGroup so = SceneHelpers.AddSceneObject(m_scene);
  91. m_scriptItem = TaskInventoryHelpers.AddScript(m_scene, so.RootPart);
  92. // This is disconnected from the actual script - the mock engine does not set up any LSL_Api atm.
  93. // Possibly this could be done and we could obtain it directly from the MockScriptEngine.
  94. m_lslApi = new LSL_Api();
  95. m_lslApi.Initialize(m_engine, so.RootPart, m_scriptItem, null);
  96. }
  97. [TearDown]
  98. public void TearDown()
  99. {
  100. MainServer.Instance.Stop();
  101. }
  102. [Test]
  103. public void TestLlReleaseUrl()
  104. {
  105. TestHelpers.InMethod();
  106. m_lslApi.llRequestURL();
  107. string returnedUri = m_engine.PostedEvents[m_scriptItem.ItemID][0].Params[2].ToString();
  108. {
  109. // Check that the initial number of URLs is correct
  110. Assert.That(m_lslApi.llGetFreeURLs().value, Is.EqualTo(m_urlModule.TotalUrls - 1));
  111. }
  112. {
  113. // Check releasing a non-url
  114. m_lslApi.llReleaseURL("GARBAGE");
  115. Assert.That(m_lslApi.llGetFreeURLs().value, Is.EqualTo(m_urlModule.TotalUrls - 1));
  116. }
  117. {
  118. // Check releasing a non-existing url
  119. m_lslApi.llReleaseURL("http://example.com");
  120. Assert.That(m_lslApi.llGetFreeURLs().value, Is.EqualTo(m_urlModule.TotalUrls - 1));
  121. }
  122. {
  123. // Check URL release
  124. m_lslApi.llReleaseURL(returnedUri);
  125. Assert.That(m_lslApi.llGetFreeURLs().value, Is.EqualTo(m_urlModule.TotalUrls));
  126. HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(returnedUri);
  127. bool gotExpectedException = false;
  128. try
  129. {
  130. using (HttpWebResponse webResponse = (HttpWebResponse)webRequest.GetResponse())
  131. {}
  132. }
  133. catch (WebException e)
  134. {
  135. using (HttpWebResponse response = (HttpWebResponse)e.Response)
  136. gotExpectedException = response.StatusCode == HttpStatusCode.NotFound;
  137. }
  138. Assert.That(gotExpectedException, Is.True);
  139. }
  140. {
  141. // Check releasing the same URL again
  142. m_lslApi.llReleaseURL(returnedUri);
  143. Assert.That(m_lslApi.llGetFreeURLs().value, Is.EqualTo(m_urlModule.TotalUrls));
  144. }
  145. }
  146. [Test]
  147. public void TestLlRequestUrl()
  148. {
  149. TestHelpers.InMethod();
  150. string requestId = m_lslApi.llRequestURL();
  151. Assert.That(requestId, Is.Not.EqualTo(UUID.Zero.ToString()));
  152. string returnedUri;
  153. {
  154. // Check that URL is correctly set up
  155. Assert.That(m_lslApi.llGetFreeURLs().value, Is.EqualTo(m_urlModule.TotalUrls - 1));
  156. Assert.That(m_engine.PostedEvents.ContainsKey(m_scriptItem.ItemID));
  157. List<EventParams> events = m_engine.PostedEvents[m_scriptItem.ItemID];
  158. Assert.That(events.Count, Is.EqualTo(1));
  159. EventParams eventParams = events[0];
  160. Assert.That(eventParams.EventName, Is.EqualTo("http_request"));
  161. UUID returnKey;
  162. string rawReturnKey = eventParams.Params[0].ToString();
  163. string method = eventParams.Params[1].ToString();
  164. returnedUri = eventParams.Params[2].ToString();
  165. Assert.That(UUID.TryParse(rawReturnKey, out returnKey), Is.True);
  166. Assert.That(method, Is.EqualTo(ScriptBaseClass.URL_REQUEST_GRANTED));
  167. Assert.That(Uri.IsWellFormedUriString(returnedUri, UriKind.Absolute), Is.True);
  168. }
  169. {
  170. // Check that request to URL works.
  171. string testResponse = "Hello World";
  172. m_engine.ClearPostedEvents();
  173. m_engine.PostEventHook
  174. += (itemId, evp) => m_lslApi.llHTTPResponse(evp.Params[0].ToString(), 200, testResponse);
  175. // Console.WriteLine("Trying {0}", returnedUri);
  176. AssertHttpResponse(returnedUri, testResponse);
  177. Assert.That(m_engine.PostedEvents.ContainsKey(m_scriptItem.ItemID));
  178. List<EventParams> events = m_engine.PostedEvents[m_scriptItem.ItemID];
  179. Assert.That(events.Count, Is.EqualTo(1));
  180. EventParams eventParams = events[0];
  181. Assert.That(eventParams.EventName, Is.EqualTo("http_request"));
  182. UUID returnKey;
  183. string rawReturnKey = eventParams.Params[0].ToString();
  184. string method = eventParams.Params[1].ToString();
  185. string body = eventParams.Params[2].ToString();
  186. Assert.That(UUID.TryParse(rawReturnKey, out returnKey), Is.True);
  187. Assert.That(method, Is.EqualTo("GET"));
  188. Assert.That(body, Is.EqualTo(""));
  189. }
  190. }
  191. private void AssertHttpResponse(string uri, string expectedResponse)
  192. {
  193. HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(uri);
  194. using (HttpWebResponse webResponse = (HttpWebResponse)webRequest.GetResponse())
  195. {
  196. using (Stream stream = webResponse.GetResponseStream())
  197. {
  198. using (StreamReader reader = new StreamReader(stream))
  199. {
  200. Assert.That(reader.ReadToEnd(), Is.EqualTo(expectedResponse));
  201. }
  202. }
  203. }
  204. }
  205. }
  206. }