diff options
author | Justin Clark-Casey (justincc) | 2013-02-27 20:12:58 +0000 |
---|---|---|
committer | Justin Clark-Casey (justincc) | 2013-02-27 20:12:58 +0000 |
commit | 59bd099b0385cc79bba751e168ef26c923c9f003 (patch) | |
tree | 163203a0ffa27de0ddb15fb86401c65eb2155fe8 | |
parent | Improve description of GenerateMapTiles config option (diff) | |
download | opensim-SC_OLD-59bd099b0385cc79bba751e168ef26c923c9f003.zip opensim-SC_OLD-59bd099b0385cc79bba751e168ef26c923c9f003.tar.gz opensim-SC_OLD-59bd099b0385cc79bba751e168ef26c923c9f003.tar.bz2 opensim-SC_OLD-59bd099b0385cc79bba751e168ef26c923c9f003.tar.xz |
Add regression test for llReleaseUrl() (and for llRequestUrl)
Forgot to add file for llRequestUrl() test in commit b8a7c8b
-rw-r--r-- | OpenSim/Region/CoreModules/Scripting/LSLHttp/UrlModule.cs | 25 | ||||
-rw-r--r-- | OpenSim/Region/ScriptEngine/Shared/Tests/LSL_ApiHttpTests.cs | 250 |
2 files changed, 266 insertions, 9 deletions
diff --git a/OpenSim/Region/CoreModules/Scripting/LSLHttp/UrlModule.cs b/OpenSim/Region/CoreModules/Scripting/LSLHttp/UrlModule.cs index 79e633f..c9cd412 100644 --- a/OpenSim/Region/CoreModules/Scripting/LSLHttp/UrlModule.cs +++ b/OpenSim/Region/CoreModules/Scripting/LSLHttp/UrlModule.cs | |||
@@ -117,17 +117,22 @@ namespace OpenSim.Region.CoreModules.Scripting.LSLHttp | |||
117 | /// </summary> | 117 | /// </summary> |
118 | private Dictionary<string, UrlData> m_UrlMap = new Dictionary<string, UrlData>(); | 118 | private Dictionary<string, UrlData> m_UrlMap = new Dictionary<string, UrlData>(); |
119 | 119 | ||
120 | /// <summary> | ||
121 | /// Maximum number of external urls that can be set up by this module. | ||
122 | /// </summary> | ||
123 | private int m_TotalUrls = 100; | ||
124 | |||
125 | private uint m_HttpsPort = 0; | 120 | private uint m_HttpsPort = 0; |
126 | private IHttpServer m_HttpServer = null; | 121 | private IHttpServer m_HttpServer = null; |
127 | private IHttpServer m_HttpsServer = null; | 122 | private IHttpServer m_HttpsServer = null; |
128 | 123 | ||
129 | public string ExternalHostNameForLSL { get; private set; } | 124 | public string ExternalHostNameForLSL { get; private set; } |
130 | 125 | ||
126 | /// <summary> | ||
127 | /// The default maximum number of urls | ||
128 | /// </summary> | ||
129 | public const int DefaultTotalUrls = 100; | ||
130 | |||
131 | /// <summary> | ||
132 | /// Maximum number of external urls that can be set up by this module. | ||
133 | /// </summary> | ||
134 | public int TotalUrls { get; set; } | ||
135 | |||
131 | public Type ReplaceableInterface | 136 | public Type ReplaceableInterface |
132 | { | 137 | { |
133 | get { return null; } | 138 | get { return null; } |
@@ -158,7 +163,9 @@ namespace OpenSim.Region.CoreModules.Scripting.LSLHttp | |||
158 | IConfig llFunctionsConfig = config.Configs["LL-Functions"]; | 163 | IConfig llFunctionsConfig = config.Configs["LL-Functions"]; |
159 | 164 | ||
160 | if (llFunctionsConfig != null) | 165 | if (llFunctionsConfig != null) |
161 | m_TotalUrls = llFunctionsConfig.GetInt("max_external_urls_per_simulator", m_TotalUrls); | 166 | TotalUrls = llFunctionsConfig.GetInt("max_external_urls_per_simulator", DefaultTotalUrls); |
167 | else | ||
168 | TotalUrls = DefaultTotalUrls; | ||
162 | } | 169 | } |
163 | 170 | ||
164 | public void PostInitialise() | 171 | public void PostInitialise() |
@@ -209,7 +216,7 @@ namespace OpenSim.Region.CoreModules.Scripting.LSLHttp | |||
209 | 216 | ||
210 | lock (m_UrlMap) | 217 | lock (m_UrlMap) |
211 | { | 218 | { |
212 | if (m_UrlMap.Count >= m_TotalUrls) | 219 | if (m_UrlMap.Count >= TotalUrls) |
213 | { | 220 | { |
214 | engine.PostScriptEvent(itemID, "http_request", new Object[] { urlcode.ToString(), "URL_REQUEST_DENIED", "" }); | 221 | engine.PostScriptEvent(itemID, "http_request", new Object[] { urlcode.ToString(), "URL_REQUEST_DENIED", "" }); |
215 | return urlcode; | 222 | return urlcode; |
@@ -254,7 +261,7 @@ namespace OpenSim.Region.CoreModules.Scripting.LSLHttp | |||
254 | 261 | ||
255 | lock (m_UrlMap) | 262 | lock (m_UrlMap) |
256 | { | 263 | { |
257 | if (m_UrlMap.Count >= m_TotalUrls) | 264 | if (m_UrlMap.Count >= TotalUrls) |
258 | { | 265 | { |
259 | engine.PostScriptEvent(itemID, "http_request", new Object[] { urlcode.ToString(), "URL_REQUEST_DENIED", "" }); | 266 | engine.PostScriptEvent(itemID, "http_request", new Object[] { urlcode.ToString(), "URL_REQUEST_DENIED", "" }); |
260 | return urlcode; | 267 | return urlcode; |
@@ -382,7 +389,7 @@ namespace OpenSim.Region.CoreModules.Scripting.LSLHttp | |||
382 | public int GetFreeUrls() | 389 | public int GetFreeUrls() |
383 | { | 390 | { |
384 | lock (m_UrlMap) | 391 | lock (m_UrlMap) |
385 | return m_TotalUrls - m_UrlMap.Count; | 392 | return TotalUrls - m_UrlMap.Count; |
386 | } | 393 | } |
387 | 394 | ||
388 | public void ScriptRemoved(UUID itemID) | 395 | public void ScriptRemoved(UUID itemID) |
diff --git a/OpenSim/Region/ScriptEngine/Shared/Tests/LSL_ApiHttpTests.cs b/OpenSim/Region/ScriptEngine/Shared/Tests/LSL_ApiHttpTests.cs new file mode 100644 index 0000000..b0baa1c --- /dev/null +++ b/OpenSim/Region/ScriptEngine/Shared/Tests/LSL_ApiHttpTests.cs | |||
@@ -0,0 +1,250 @@ | |||
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 | |||
28 | using System; | ||
29 | using System.Collections.Generic; | ||
30 | using System.IO; | ||
31 | using System.Net; | ||
32 | using System.Reflection; | ||
33 | using System.Text; | ||
34 | using log4net; | ||
35 | using Nini.Config; | ||
36 | using NUnit.Framework; | ||
37 | using OpenMetaverse; | ||
38 | using OpenSim.Framework; | ||
39 | using OpenSim.Framework.Servers; | ||
40 | using OpenSim.Framework.Servers.HttpServer; | ||
41 | using OpenSim.Region.CoreModules.Scripting.LSLHttp; | ||
42 | using OpenSim.Region.Framework.Scenes; | ||
43 | using OpenSim.Region.ScriptEngine.Shared; | ||
44 | using OpenSim.Region.ScriptEngine.Shared.Api; | ||
45 | using OpenSim.Region.ScriptEngine.Shared.ScriptBase; | ||
46 | using OpenSim.Services.Interfaces; | ||
47 | using OpenSim.Tests.Common; | ||
48 | using OpenSim.Tests.Common.Mock; | ||
49 | |||
50 | namespace OpenSim.Region.ScriptEngine.Shared.Tests | ||
51 | { | ||
52 | /// <summary> | ||
53 | /// Tests for HTTP related functions in LSL | ||
54 | /// </summary> | ||
55 | [TestFixture] | ||
56 | public class LSL_ApiHttpTests : OpenSimTestCase | ||
57 | { | ||
58 | private Scene m_scene; | ||
59 | private MockScriptEngine m_engine; | ||
60 | private UrlModule m_urlModule; | ||
61 | |||
62 | private TaskInventoryItem m_scriptItem; | ||
63 | private LSL_Api m_lslApi; | ||
64 | |||
65 | [TestFixtureSetUp] | ||
66 | public void TestFixtureSetUp() | ||
67 | { | ||
68 | // Don't allow tests to be bamboozled by asynchronous events. Execute everything on the same thread. | ||
69 | Util.FireAndForgetMethod = FireAndForgetMethod.RegressionTest; | ||
70 | } | ||
71 | |||
72 | [TestFixtureTearDown] | ||
73 | public void TestFixureTearDown() | ||
74 | { | ||
75 | // We must set this back afterwards, otherwise later tests will fail since they're expecting multiple | ||
76 | // threads. Possibly, later tests should be rewritten so none of them require async stuff (which regression | ||
77 | // tests really shouldn't). | ||
78 | Util.FireAndForgetMethod = Util.DefaultFireAndForgetMethod; | ||
79 | } | ||
80 | |||
81 | [SetUp] | ||
82 | public override void SetUp() | ||
83 | { | ||
84 | base.SetUp(); | ||
85 | |||
86 | // This is an unfortunate bit of clean up we have to do because MainServer manages things through static | ||
87 | // variables and the VM is not restarted between tests. | ||
88 | uint port = 9999; | ||
89 | MainServer.RemoveHttpServer(port); | ||
90 | |||
91 | BaseHttpServer server = new BaseHttpServer(port, false, 0, ""); | ||
92 | MainServer.AddHttpServer(server); | ||
93 | MainServer.Instance = server; | ||
94 | |||
95 | server.Start(); | ||
96 | |||
97 | m_engine = new MockScriptEngine(); | ||
98 | m_urlModule = new UrlModule(); | ||
99 | |||
100 | m_scene = new SceneHelpers().SetupScene(); | ||
101 | SceneHelpers.SetupSceneModules(m_scene, new IniConfigSource(), m_engine, m_urlModule); | ||
102 | |||
103 | SceneObjectGroup so = SceneHelpers.AddSceneObject(m_scene); | ||
104 | m_scriptItem = TaskInventoryHelpers.AddScript(m_scene, so.RootPart); | ||
105 | |||
106 | // This is disconnected from the actual script - the mock engine does not set up any LSL_Api atm. | ||
107 | // Possibly this could be done and we could obtain it directly from the MockScriptEngine. | ||
108 | m_lslApi = new LSL_Api(); | ||
109 | m_lslApi.Initialize(m_engine, so.RootPart, m_scriptItem, null); | ||
110 | } | ||
111 | |||
112 | [TearDown] | ||
113 | public void TearDown() | ||
114 | { | ||
115 | MainServer.Instance.Stop(); | ||
116 | } | ||
117 | |||
118 | [Test] | ||
119 | public void TestLlReleaseUrl() | ||
120 | { | ||
121 | TestHelpers.InMethod(); | ||
122 | |||
123 | m_lslApi.llRequestURL(); | ||
124 | string returnedUri = m_engine.PostedEvents[m_scriptItem.ItemID][0].Params[2].ToString(); | ||
125 | |||
126 | { | ||
127 | // Check that the initial number of URLs is correct | ||
128 | Assert.That(m_lslApi.llGetFreeURLs().value, Is.EqualTo(m_urlModule.TotalUrls - 1)); | ||
129 | } | ||
130 | |||
131 | { | ||
132 | // Check releasing a non-url | ||
133 | m_lslApi.llReleaseURL("GARBAGE"); | ||
134 | Assert.That(m_lslApi.llGetFreeURLs().value, Is.EqualTo(m_urlModule.TotalUrls - 1)); | ||
135 | } | ||
136 | |||
137 | { | ||
138 | // Check releasing a non-existing url | ||
139 | m_lslApi.llReleaseURL("http://example.com"); | ||
140 | Assert.That(m_lslApi.llGetFreeURLs().value, Is.EqualTo(m_urlModule.TotalUrls - 1)); | ||
141 | } | ||
142 | |||
143 | { | ||
144 | // Check URL release | ||
145 | m_lslApi.llReleaseURL(returnedUri); | ||
146 | Assert.That(m_lslApi.llGetFreeURLs().value, Is.EqualTo(m_urlModule.TotalUrls)); | ||
147 | |||
148 | HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(returnedUri); | ||
149 | |||
150 | bool gotExpectedException = false; | ||
151 | |||
152 | try | ||
153 | { | ||
154 | using (HttpWebResponse webResponse = (HttpWebResponse)webRequest.GetResponse()) | ||
155 | {} | ||
156 | } | ||
157 | catch (WebException e) | ||
158 | { | ||
159 | using (HttpWebResponse response = (HttpWebResponse)e.Response) | ||
160 | gotExpectedException = response.StatusCode == HttpStatusCode.NotFound; | ||
161 | } | ||
162 | |||
163 | Assert.That(gotExpectedException, Is.True); | ||
164 | } | ||
165 | |||
166 | { | ||
167 | // Check releasing the same URL again | ||
168 | m_lslApi.llReleaseURL(returnedUri); | ||
169 | Assert.That(m_lslApi.llGetFreeURLs().value, Is.EqualTo(m_urlModule.TotalUrls)); | ||
170 | } | ||
171 | } | ||
172 | |||
173 | [Test] | ||
174 | public void TestLlRequestUrl() | ||
175 | { | ||
176 | TestHelpers.InMethod(); | ||
177 | |||
178 | string requestId = m_lslApi.llRequestURL(); | ||
179 | Assert.That(requestId, Is.Not.EqualTo(UUID.Zero.ToString())); | ||
180 | string returnedUri; | ||
181 | |||
182 | { | ||
183 | // Check that URL is correctly set up | ||
184 | Assert.That(m_lslApi.llGetFreeURLs().value, Is.EqualTo(m_urlModule.TotalUrls - 1)); | ||
185 | |||
186 | Assert.That(m_engine.PostedEvents.ContainsKey(m_scriptItem.ItemID)); | ||
187 | |||
188 | List<EventParams> events = m_engine.PostedEvents[m_scriptItem.ItemID]; | ||
189 | Assert.That(events.Count, Is.EqualTo(1)); | ||
190 | EventParams eventParams = events[0]; | ||
191 | Assert.That(eventParams.EventName, Is.EqualTo("http_request")); | ||
192 | |||
193 | UUID returnKey; | ||
194 | string rawReturnKey = eventParams.Params[0].ToString(); | ||
195 | string method = eventParams.Params[1].ToString(); | ||
196 | returnedUri = eventParams.Params[2].ToString(); | ||
197 | |||
198 | Assert.That(UUID.TryParse(rawReturnKey, out returnKey), Is.True); | ||
199 | Assert.That(method, Is.EqualTo(ScriptBaseClass.URL_REQUEST_GRANTED)); | ||
200 | Assert.That(Uri.IsWellFormedUriString(returnedUri, UriKind.Absolute), Is.True); | ||
201 | } | ||
202 | |||
203 | { | ||
204 | // Check that request to URL works. | ||
205 | string testResponse = "Hello World"; | ||
206 | |||
207 | m_engine.ClearPostedEvents(); | ||
208 | m_engine.PostEventHook | ||
209 | += (itemId, evp) => m_lslApi.llHTTPResponse(evp.Params[0].ToString(), 200, testResponse); | ||
210 | |||
211 | // Console.WriteLine("Trying {0}", returnedUri); | ||
212 | HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(returnedUri); | ||
213 | |||
214 | AssertHttpResponse(returnedUri, testResponse); | ||
215 | |||
216 | Assert.That(m_engine.PostedEvents.ContainsKey(m_scriptItem.ItemID)); | ||
217 | |||
218 | List<EventParams> events = m_engine.PostedEvents[m_scriptItem.ItemID]; | ||
219 | Assert.That(events.Count, Is.EqualTo(1)); | ||
220 | EventParams eventParams = events[0]; | ||
221 | Assert.That(eventParams.EventName, Is.EqualTo("http_request")); | ||
222 | |||
223 | UUID returnKey; | ||
224 | string rawReturnKey = eventParams.Params[0].ToString(); | ||
225 | string method = eventParams.Params[1].ToString(); | ||
226 | string body = eventParams.Params[2].ToString(); | ||
227 | |||
228 | Assert.That(UUID.TryParse(rawReturnKey, out returnKey), Is.True); | ||
229 | Assert.That(method, Is.EqualTo("GET")); | ||
230 | Assert.That(body, Is.EqualTo("")); | ||
231 | } | ||
232 | } | ||
233 | |||
234 | private void AssertHttpResponse(string uri, string expectedResponse) | ||
235 | { | ||
236 | HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(uri); | ||
237 | |||
238 | using (HttpWebResponse webResponse = (HttpWebResponse)webRequest.GetResponse()) | ||
239 | { | ||
240 | using (Stream stream = webResponse.GetResponseStream()) | ||
241 | { | ||
242 | using (StreamReader reader = new StreamReader(stream)) | ||
243 | { | ||
244 | Assert.That(reader.ReadToEnd(), Is.EqualTo(expectedResponse)); | ||
245 | } | ||
246 | } | ||
247 | } | ||
248 | } | ||
249 | } | ||
250 | } \ No newline at end of file | ||