LSL_ApiHttpTests.cs 9.8 KB

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