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