1
0

LSL_ApiHttpTests.cs 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  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. m_engine = new MockScriptEngine();
  83. m_urlModule = new UrlModule();
  84. IConfigSource config = new IniConfigSource();
  85. config.AddConfig("Network");
  86. config.Configs["Network"].Set("ExternalHostNameForLSL", "127.0.0.1");
  87. m_scene = new SceneHelpers().SetupScene();
  88. BaseHttpServer server = new BaseHttpServer(port);
  89. MainServer.AddHttpServer(server);
  90. MainServer.Instance = server;
  91. server.Start();
  92. SceneHelpers.SetupSceneModules(m_scene, config, m_engine, m_urlModule);
  93. SceneObjectGroup so = SceneHelpers.AddSceneObject(m_scene);
  94. m_scriptItem = TaskInventoryHelpers.AddScript(m_scene.AssetService, so.RootPart);
  95. // This is disconnected from the actual script - the mock engine does not set up any LSL_Api atm.
  96. // Possibly this could be done and we could obtain it directly from the MockScriptEngine.
  97. m_lslApi = new LSL_Api();
  98. m_lslApi.Initialize(m_engine, so.RootPart, m_scriptItem);
  99. }
  100. [TearDown]
  101. public void TearDown()
  102. {
  103. MainServer.Instance.Stop();
  104. }
  105. [Test]
  106. public void TestLlReleaseUrl()
  107. {
  108. TestHelpers.InMethod();
  109. m_lslApi.llRequestURL();
  110. string returnedUri = m_engine.PostedEvents[m_scriptItem.ItemID][0].Params[2].ToString();
  111. {
  112. // Check that the initial number of URLs is correct
  113. Assert.That(m_lslApi.llGetFreeURLs().value, Is.EqualTo(m_urlModule.TotalUrls - 1));
  114. }
  115. {
  116. // Check releasing a non-url
  117. m_lslApi.llReleaseURL("GARBAGE");
  118. Assert.That(m_lslApi.llGetFreeURLs().value, Is.EqualTo(m_urlModule.TotalUrls - 1));
  119. }
  120. {
  121. // Check releasing a non-existing url
  122. m_lslApi.llReleaseURL("http://example.com");
  123. Assert.That(m_lslApi.llGetFreeURLs().value, Is.EqualTo(m_urlModule.TotalUrls - 1));
  124. }
  125. {
  126. // Check URL release
  127. m_lslApi.llReleaseURL(returnedUri);
  128. Assert.That(m_lslApi.llGetFreeURLs().value, Is.EqualTo(m_urlModule.TotalUrls));
  129. HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(returnedUri);
  130. bool gotExpectedException = false;
  131. try
  132. {
  133. using (HttpWebResponse webResponse = (HttpWebResponse)webRequest.GetResponse())
  134. {}
  135. }
  136. catch (WebException)
  137. {
  138. // using (HttpWebResponse response = (HttpWebResponse)e.Response)
  139. // gotExpectedException = response.StatusCode == HttpStatusCode.NotFound;
  140. gotExpectedException = true;
  141. }
  142. Assert.That(gotExpectedException, Is.True);
  143. }
  144. {
  145. // Check releasing the same URL again
  146. m_lslApi.llReleaseURL(returnedUri);
  147. Assert.That(m_lslApi.llGetFreeURLs().value, Is.EqualTo(m_urlModule.TotalUrls));
  148. }
  149. }
  150. [Test]
  151. public void TestLlRequestUrl()
  152. {
  153. TestHelpers.InMethod();
  154. string requestId = m_lslApi.llRequestURL();
  155. Assert.That(requestId, Is.Not.EqualTo(UUID.Zero.ToString()));
  156. string returnedUri;
  157. {
  158. // Check that URL is correctly set up
  159. Assert.That(m_lslApi.llGetFreeURLs().value, Is.EqualTo(m_urlModule.TotalUrls - 1));
  160. Assert.That(m_engine.PostedEvents.ContainsKey(m_scriptItem.ItemID));
  161. List<EventParams> events = m_engine.PostedEvents[m_scriptItem.ItemID];
  162. Assert.That(events.Count, Is.EqualTo(1));
  163. EventParams eventParams = events[0];
  164. Assert.That(eventParams.EventName, Is.EqualTo("http_request"));
  165. UUID returnKey;
  166. string rawReturnKey = eventParams.Params[0].ToString();
  167. string method = eventParams.Params[1].ToString();
  168. returnedUri = eventParams.Params[2].ToString();
  169. Assert.That(UUID.TryParse(rawReturnKey, out returnKey), Is.True);
  170. Assert.That(method, Is.EqualTo(ScriptBaseClass.URL_REQUEST_GRANTED));
  171. Assert.That(Uri.IsWellFormedUriString(returnedUri, UriKind.Absolute), Is.True);
  172. }
  173. {
  174. // Check that request to URL works.
  175. string testResponse = "Hello World";
  176. m_engine.ClearPostedEvents();
  177. m_engine.PostEventHook
  178. += (itemId, evp) => m_lslApi.llHTTPResponse(evp.Params[0].ToString(), 200, testResponse);
  179. // Console.WriteLine("Trying {0}", returnedUri);
  180. AssertHttpResponse(returnedUri, testResponse);
  181. Assert.That(m_engine.PostedEvents.ContainsKey(m_scriptItem.ItemID));
  182. List<EventParams> events = m_engine.PostedEvents[m_scriptItem.ItemID];
  183. Assert.That(events.Count, Is.EqualTo(1));
  184. EventParams eventParams = events[0];
  185. Assert.That(eventParams.EventName, Is.EqualTo("http_request"));
  186. UUID returnKey;
  187. string rawReturnKey = eventParams.Params[0].ToString();
  188. string method = eventParams.Params[1].ToString();
  189. string body = eventParams.Params[2].ToString();
  190. Assert.That(UUID.TryParse(rawReturnKey, out returnKey), Is.True);
  191. Assert.That(method, Is.EqualTo("GET"));
  192. Assert.That(body, Is.EqualTo(""));
  193. }
  194. }
  195. private void AssertHttpResponse(string uri, string expectedResponse)
  196. {
  197. HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(uri);
  198. using (HttpWebResponse webResponse = (HttpWebResponse)webRequest.GetResponse())
  199. {
  200. using (Stream stream = webResponse.GetResponseStream())
  201. {
  202. using (StreamReader reader = new StreamReader(stream))
  203. {
  204. Assert.That(reader.ReadToEnd(), Is.EqualTo(expectedResponse));
  205. }
  206. }
  207. }
  208. }
  209. }
  210. }