corrade-vassal – Blame information for rev 1

Subversion Repositories:
Rev:
Rev Author Line No. Line
1 vero 1 /*
2 * Copyright (c) 2006-2014, openmetaverse.org
3 * All rights reserved.
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 *
8 * - Redistributions of source code must retain the above copyright notice, this
9 * list of conditions and the following disclaimer.
10 * - Neither the name of the openmetaverse.org nor the names
11 * of its contributors may be used to endorse or promote products derived from
12 * this software without specific prior written permission.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
15 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
18 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
19 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
20 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
21 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
22 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
23 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
24 * POSSIBILITY OF SUCH DAMAGE.
25 */
26  
27 using System;
28 using System.Collections.Generic;
29 using System.Text;
30 using NUnit.Framework;
31 using System.Collections;
32 using OpenMetaverse.StructuredData;
33  
34 namespace OpenMetaverse.Tests
35 {
36 /// <summary>
37 /// XmlSDTests is a suite of tests for libsl implementation of the SD XML format.
38 ///
39 /// </summary>
40 [TestFixture]
41 public class XmlSDTests
42 {
43 /// <summary>
44 /// Test that the sample LLSD supplied by Linden Lab is properly deserialized.
45 /// The LLSD string in the test is a pared down version of the sample on the blog.
46 /// http://wiki.secondlife.com/wiki/LLSD
47 /// </summary>
48 [Test]
49 public void DeserializeLLSDSample()
50 {
51 OSD theSD = null;
52 OSDMap map = null;
53 OSD tempSD = null;
54 OSDUUID tempUUID = null;
55 OSDString tempStr = null;
56 OSDReal tempReal = null;
57  
58 String testSD = @"<?xml version='1.0' encoding='UTF-8'?>
59 <llsd>
60 <map>
61 <key>region_id</key>
62 <uuid>67153d5b-3659-afb4-8510-adda2c034649</uuid>
63 <key>scale</key>
64 <string>one minute</string>
65 <key>simulator statistics</key>
66 <map>
67 <key>time dilation</key>
68 <real>0.9878624</real>
69 <key>sim fps</key>
70 <real>44.38898</real>
71 <key>agent updates per second</key>
72 <real>nan</real>
73 <key>total task count</key>
74 <real>4</real>
75 <key>active task count</key>
76 <real>0</real>
77 <key>pending uploads</key>
78 <real>0.0001096525</real>
79 </map>
80 </map>
81 </llsd>";
82  
83 //Deserialize the string
84 byte[] bytes = Encoding.UTF8.GetBytes(testSD);
85 theSD = OSDParser.DeserializeLLSDXml(bytes);
86  
87 //Confirm the contents
88 Assert.IsNotNull(theSD);
89 Assert.IsTrue(theSD is OSDMap);
90 Assert.IsTrue(theSD.Type == OSDType.Map);
91 map = (OSDMap)theSD;
92  
93 tempSD = map["region_id"];
94 Assert.IsNotNull(tempSD);
95 Assert.IsTrue(tempSD is OSDUUID);
96 Assert.IsTrue(tempSD.Type == OSDType.UUID);
97 tempUUID = (OSDUUID)tempSD;
98 Assert.AreEqual(new UUID("67153d5b-3659-afb4-8510-adda2c034649"), tempUUID.AsUUID());
99  
100 tempSD = map["scale"];
101 Assert.IsNotNull(tempSD);
102 Assert.IsTrue(tempSD is OSDString);
103 Assert.IsTrue(tempSD.Type == OSDType.String);
104 tempStr = (OSDString)tempSD;
105 Assert.AreEqual("one minute", tempStr.AsString());
106  
107 tempSD = map["simulator statistics"];
108 Assert.IsNotNull(tempSD);
109 Assert.IsTrue(tempSD is OSDMap);
110 Assert.IsTrue(tempSD.Type == OSDType.Map);
111 map = (OSDMap)tempSD;
112  
113 tempSD = map["time dilation"];
114 Assert.IsNotNull(tempSD);
115 Assert.IsTrue(tempSD is OSDReal);
116 Assert.IsTrue(tempSD.Type == OSDType.Real);
117 tempReal = (OSDReal)tempSD;
118  
119 Assert.AreEqual(0.9878624d, tempReal.AsReal());
120 //TODO - figure out any relevant rounding variability for 64 bit reals
121 tempSD = map["sim fps"];
122 Assert.IsNotNull(tempSD);
123 Assert.IsTrue(tempSD is OSDReal);
124 Assert.IsTrue(tempSD.Type == OSDType.Real);
125 tempReal = (OSDReal)tempSD;
126 Assert.AreEqual(44.38898d, tempReal.AsReal());
127  
128 tempSD = map["agent updates per second"];
129 Assert.IsNotNull(tempSD);
130 Assert.IsTrue(tempSD is OSDReal);
131 Assert.IsTrue(tempSD.Type == OSDType.Real);
132 tempReal = (OSDReal)tempSD;
133 Assert.AreEqual(Double.NaN, tempSD.AsReal());
134  
135 tempSD = map["total task count"];
136 Assert.IsNotNull(tempSD);
137 Assert.IsTrue(tempSD is OSDReal);
138 Assert.IsTrue(tempSD.Type == OSDType.Real);
139 tempReal = (OSDReal)tempSD;
140 Assert.AreEqual(4.0d, tempReal.AsReal());
141  
142 tempSD = map["active task count"];
143 Assert.IsNotNull(tempSD);
144 Assert.IsTrue(tempSD is OSDReal);
145 Assert.IsTrue(tempSD.Type == OSDType.Real);
146 tempReal = (OSDReal)tempSD;
147 Assert.AreEqual(0.0d, tempReal.AsReal());
148  
149 tempSD = map["pending uploads"];
150 Assert.IsNotNull(tempSD);
151 Assert.IsTrue(tempSD is OSDReal);
152 Assert.IsTrue(tempSD.Type == OSDType.Real);
153 tempReal = (OSDReal)tempSD;
154 Assert.AreEqual(0.0001096525d, tempReal.AsReal());
155  
156 }
157  
158 /// <summary>
159 /// Test that various Real representations are parsed correctly.
160 /// </summary>
161 [Test]
162 public void DeserializeReals()
163 {
164 OSD theSD = null;
165 OSDArray array = null;
166 OSDReal tempReal = null;
167  
168 String testSD = @"<?xml version='1.0' encoding='UTF-8'?>
169 <llsd>
170 <array>
171 <real>44.38898</real>
172 <real>nan</real>
173 <real>4</real>
174 <real>-13.333</real>
175 <real/>
176 </array>
177 </llsd>";
178 //Deserialize the string
179 byte[] bytes = Encoding.UTF8.GetBytes(testSD);
180 theSD = OSDParser.DeserializeLLSDXml(bytes);
181  
182 Assert.IsTrue(theSD is OSDArray);
183 array = (OSDArray)theSD;
184  
185 Assert.AreEqual(OSDType.Real, array[0].Type);
186 tempReal = (OSDReal)array[0];
187 Assert.AreEqual(44.38898d, tempReal.AsReal());
188  
189 Assert.AreEqual(OSDType.Real, array[1].Type);
190 tempReal = (OSDReal)array[1];
191 Assert.AreEqual(Double.NaN, tempReal.AsReal());
192  
193 Assert.AreEqual(OSDType.Real, array[2].Type);
194 tempReal = (OSDReal)array[2];
195 Assert.AreEqual(4.0d, tempReal.AsReal());
196  
197 Assert.AreEqual(OSDType.Real, array[3].Type);
198 tempReal = (OSDReal)array[3];
199 Assert.AreEqual(-13.333d, tempReal.AsReal());
200  
201 Assert.AreEqual(OSDType.Real, array[4].Type);
202 tempReal = (OSDReal)array[4];
203 Assert.AreEqual(0d, tempReal.AsReal());
204 }
205  
206 /// <summary>
207 /// Test that various String representations are parsed correctly.
208 /// </summary>
209 [Test]
210 public void DeserializeStrings()
211 {
212 OSD theSD = null;
213 OSDArray array = null;
214 OSDString tempStr = null;
215  
216 String testSD = @"<?xml version='1.0' encoding='UTF-8'?>
217 <llsd>
218 <array>
219 <string>Kissling</string>
220 <string>Attack ships on fire off the shoulder of Orion</string>
221 <string>&lt; &gt; &amp; &apos; &quot;</string>
222 <string/>
223 </array>
224 </llsd>";
225 //Deserialize the string
226 byte[] bytes = Encoding.UTF8.GetBytes(testSD);
227 theSD = OSDParser.DeserializeLLSDXml(bytes);
228  
229 Assert.IsTrue(theSD is OSDArray);
230 array = (OSDArray)theSD;
231  
232 Assert.AreEqual(OSDType.String, array[0].Type);
233 tempStr = (OSDString)array[0];
234 Assert.AreEqual("Kissling", tempStr.AsString());
235  
236 Assert.AreEqual(OSDType.String, array[1].Type);
237 tempStr = (OSDString)array[1];
238 Assert.AreEqual("Attack ships on fire off the shoulder of Orion", tempStr.AsString());
239  
240 Assert.AreEqual(OSDType.String, array[2].Type);
241 tempStr = (OSDString)array[2];
242 Assert.AreEqual("< > & \' \"", tempStr.AsString());
243  
244 Assert.AreEqual(OSDType.String, array[3].Type);
245 tempStr = (OSDString)array[3];
246 Assert.AreEqual("", tempStr.AsString());
247  
248 }
249  
250 /// <summary>
251 /// Test that various Integer representations are parsed correctly.
252 /// These tests currently only test for values within the range of a
253 /// 32 bit signed integer, even though the SD specification says
254 /// the type is a 64 bit signed integer, because LLSInteger is currently
255 /// implemented using int, a.k.a. Int32. Not testing Int64 range until
256 /// it's understood if there was a design reason for the Int32.
257 /// </summary>
258 [Test]
259 public void DeserializeIntegers()
260 {
261 OSD theSD = null;
262 OSDArray array = null;
263 OSDInteger tempInt = null;
264  
265 String testSD = @"<?xml version='1.0' encoding='UTF-8'?>
266 <llsd>
267 <array>
268 <integer>2147483647</integer>
269 <integer>-2147483648</integer>
270 <integer>0</integer>
271 <integer>013</integer>
272 <integer/>
273 </array>
274 </llsd>";
275 //Deserialize the string
276 byte[] bytes = Encoding.UTF8.GetBytes(testSD);
277 theSD = OSDParser.DeserializeLLSDXml(bytes);
278  
279 Assert.IsTrue(theSD is OSDArray);
280 array = (OSDArray)theSD;
281  
282 Assert.AreEqual(OSDType.Integer, array[0].Type);
283 tempInt = (OSDInteger)array[0];
284 Assert.AreEqual(2147483647, tempInt.AsInteger());
285  
286 Assert.AreEqual(OSDType.Integer, array[1].Type);
287 tempInt = (OSDInteger)array[1];
288 Assert.AreEqual(-2147483648, tempInt.AsInteger());
289  
290 Assert.AreEqual(OSDType.Integer, array[2].Type);
291 tempInt = (OSDInteger)array[2];
292 Assert.AreEqual(0, tempInt.AsInteger());
293  
294 Assert.AreEqual(OSDType.Integer, array[3].Type);
295 tempInt = (OSDInteger)array[3];
296 Assert.AreEqual(13, tempInt.AsInteger());
297  
298 Assert.AreEqual(OSDType.Integer, array[4].Type);
299 tempInt = (OSDInteger)array[4];
300 Assert.AreEqual(0, tempInt.AsInteger());
301 }
302  
303 /// <summary>
304 /// Test that various UUID representations are parsed correctly.
305 /// </summary>
306 [Test]
307 public void DeserializeUUID()
308 {
309 OSD theSD = null;
310 OSDArray array = null;
311 OSDUUID tempUUID = null;
312  
313 String testSD = @"<?xml version='1.0' encoding='UTF-8'?>
314 <llsd>
315 <array>
316 <uuid>d7f4aeca-88f1-42a1-b385-b9db18abb255</uuid>
317 <uuid/>
318 </array>
319 </llsd>";
320 //Deserialize the string
321 byte[] bytes = Encoding.UTF8.GetBytes(testSD);
322 theSD = OSDParser.DeserializeLLSDXml(bytes);
323  
324 Assert.IsTrue(theSD is OSDArray);
325 array = (OSDArray)theSD;
326  
327 Assert.AreEqual(OSDType.UUID, array[0].Type);
328 tempUUID = (OSDUUID)array[0];
329 Assert.AreEqual(new UUID("d7f4aeca-88f1-42a1-b385-b9db18abb255"), tempUUID.AsUUID());
330  
331 Assert.AreEqual(OSDType.UUID, array[1].Type);
332 tempUUID = (OSDUUID)array[1];
333 Assert.AreEqual(UUID.Zero, tempUUID.AsUUID());
334 }
335  
336 /// <summary>
337 /// Test that various date representations are parsed correctly.
338 /// </summary>
339 [Test]
340 public void DeserializeDates()
341 {
342 OSD theSD = null;
343 OSDArray array = null;
344 OSDDate tempDate = null;
345 DateTime testDate;
346  
347 String testSD = @"<?xml version='1.0' encoding='UTF-8'?>
348 <llsd>
349 <array>
350 <date>2006-02-01T14:29:53Z</date>
351 <date>1999-01-01T00:00:00Z</date>
352 <date/>
353 </array>
354 </llsd>";
355 //Deserialize the string
356 byte[] bytes = Encoding.UTF8.GetBytes(testSD);
357 theSD = OSDParser.DeserializeLLSDXml(bytes);
358  
359 Assert.IsTrue(theSD is OSDArray);
360 array = (OSDArray)theSD;
361  
362 Assert.AreEqual(OSDType.Date, array[0].Type);
363 tempDate = (OSDDate)array[0];
364 DateTime.TryParse("2006-02-01T14:29:53Z", out testDate);
365 Assert.AreEqual(testDate, tempDate.AsDate());
366  
367 Assert.AreEqual(OSDType.Date, array[1].Type);
368 tempDate = (OSDDate)array[1];
369 DateTime.TryParse("1999-01-01T00:00:00Z", out testDate);
370 Assert.AreEqual(testDate, tempDate.AsDate());
371  
372 Assert.AreEqual(OSDType.Date, array[2].Type);
373 tempDate = (OSDDate)array[2];
374 Assert.AreEqual(Utils.Epoch, tempDate.AsDate());
375 }
376  
377 /// <summary>
378 /// Test that various Boolean representations are parsed correctly.
379 /// </summary>
380 [Test]
381 public void DeserializeBoolean()
382 {
383 OSD theSD = null;
384 OSDArray array = null;
385 OSDBoolean tempBool = null;
386  
387 String testSD = @"<?xml version='1.0' encoding='UTF-8'?>
388 <llsd>
389 <array>
390 <boolean>1</boolean>
391 <boolean>true</boolean>
392 <boolean>0</boolean>
393 <boolean>false</boolean>
394 <boolean/>
395 </array>
396 </llsd>";
397 //Deserialize the string
398 byte[] bytes = Encoding.UTF8.GetBytes(testSD);
399 theSD = OSDParser.DeserializeLLSDXml(bytes);
400  
401 Assert.IsTrue(theSD is OSDArray);
402 array = (OSDArray)theSD;
403  
404 Assert.AreEqual(OSDType.Boolean, array[0].Type);
405 tempBool = (OSDBoolean)array[0];
406 Assert.AreEqual(true, tempBool.AsBoolean());
407  
408 Assert.AreEqual(OSDType.Boolean, array[1].Type);
409 tempBool = (OSDBoolean)array[1];
410 Assert.AreEqual(true, tempBool.AsBoolean());
411  
412 Assert.AreEqual(OSDType.Boolean, array[2].Type);
413 tempBool = (OSDBoolean)array[2];
414 Assert.AreEqual(false, tempBool.AsBoolean());
415  
416 Assert.AreEqual(OSDType.Boolean, array[3].Type);
417 tempBool = (OSDBoolean)array[3];
418 Assert.AreEqual(false, tempBool.AsBoolean());
419  
420 Assert.AreEqual(OSDType.Boolean, array[4].Type);
421 tempBool = (OSDBoolean)array[4];
422 Assert.AreEqual(false, tempBool.AsBoolean());
423 }
424  
425 /// <summary>
426 /// Test that binary elements are parsed correctly.
427 /// </summary>
428 [Test]
429 public void DeserializeBinary()
430 {
431 OSD theSD = null;
432 OSDArray array = null;
433 OSDBinary tempBinary = null;
434  
435 String testSD = @"<?xml version='1.0' encoding='UTF-8'?>
436 <llsd>
437 <array>
438 <binary encoding='base64'>cmFuZG9t</binary>
439 <binary>dGhlIHF1aWNrIGJyb3duIGZveA==</binary>
440 <binary/>
441 </array>
442 </llsd>";
443  
444 //Deserialize the string
445 byte[] bytes = Encoding.UTF8.GetBytes(testSD);
446 theSD = OSDParser.DeserializeLLSDXml(bytes);
447  
448 Assert.IsTrue(theSD is OSDArray);
449 array = (OSDArray)theSD;
450  
451 Assert.AreEqual(OSDType.Binary, array[0].Type);
452 tempBinary = (OSDBinary)array[0];
453 byte[] testData1 = {114, 97, 110, 100, 111, 109};
454 TestHelper.TestBinary(tempBinary, testData1);
455  
456 Assert.AreEqual(OSDType.Binary, array[1].Type);
457 tempBinary = (OSDBinary)array[1];
458 byte[] testData2 = {116, 104, 101, 32, 113, 117, 105, 99, 107, 32, 98,
459 114, 111, 119, 110, 32, 102, 111, 120};
460 TestHelper.TestBinary(tempBinary, testData2);
461  
462 Assert.AreEqual(OSDType.Binary, array[1].Type);
463 tempBinary = (OSDBinary)array[2];
464 Assert.AreEqual(0, tempBinary.AsBinary().Length);
465 }
466  
467 /// <summary>
468 /// Test that undefened elements are parsed correctly.
469 /// Currently this just checks that there is no error since undefined has no
470 /// value and there is no SD child class for Undefined elements - the
471 /// current implementation generates an instance of SD
472 /// </summary>
473 [Test]
474 public void DeserializeUndef()
475 {
476 OSD theSD = null;
477  
478 String testSD = @"<?xml version='1.0' encoding='UTF-8'?>
479 <llsd>
480 <undef/>
481 </llsd>";
482 //Deserialize the string
483 byte[] bytes = Encoding.UTF8.GetBytes(testSD);
484 theSD = OSDParser.DeserializeLLSDXml(bytes);
485  
486 Assert.IsTrue(theSD is OSD);
487 }
488  
489 /// <summary>
490 /// Test that various URI representations are parsed correctly.
491 /// </summary>
492 [Test]
493 public void DeserializeURI()
494 {
495 OSD theSD = null;
496 OSDArray array = null;
497 OSDUri tempURI = null;
498  
499 String testSD = @"<?xml version='1.0' encoding='UTF-8'?>
500 <llsd>
501 <array>
502 <uri>http://sim956.agni.lindenlab.com:12035/runtime/agents</uri>
503 <uri/>
504 </array>
505 </llsd>";
506 //Deserialize the string
507 byte[] bytes = Encoding.UTF8.GetBytes(testSD);
508 theSD = OSDParser.DeserializeLLSDXml(bytes);
509  
510 Assert.IsTrue(theSD is OSDArray);
511 array = (OSDArray)theSD;
512  
513 Assert.AreEqual(OSDType.URI, array[0].Type);
514 tempURI = (OSDUri)array[0];
515 Uri testURI = new Uri(@"http://sim956.agni.lindenlab.com:12035/runtime/agents");
516 Assert.AreEqual(testURI, tempURI.AsUri());
517  
518 Assert.AreEqual(OSDType.URI, array[1].Type);
519 tempURI = (OSDUri)array[1];
520 Assert.AreEqual("", tempURI.AsUri().ToString());
521 }
522  
523 /// <summary>
524 /// Test some nested containers. This is not a very deep or complicated SD graph
525 /// but it should reveal basic nesting issues.
526 /// </summary>
527 [Test]
528 public void DeserializeNestedContainers()
529 {
530 OSD theSD = null;
531 OSDArray array = null;
532 OSDMap map = null;
533 OSD tempSD = null;
534  
535 String testSD = @"<?xml version='1.0' encoding='UTF-8'?>
536 <llsd>
537 <array>
538 <map>
539 <key>Map One</key>
540 <map>
541 <key>Array One</key>
542 <array>
543 <integer>1</integer>
544 <integer>2</integer>
545 </array>
546 </map>
547 </map>
548 <array>
549 <string>A</string>
550 <string>B</string>
551 <array>
552 <integer>1</integer>
553 <integer>4</integer>
554 <integer>9</integer>
555 </array>
556 </array>
557 </array>
558 </llsd>";
559 //Deserialize the string
560 byte[] bytes = Encoding.UTF8.GetBytes(testSD);
561 theSD = OSDParser.DeserializeLLSDXml(bytes);
562  
563 Assert.IsTrue(theSD is OSDArray);
564 array = (OSDArray)theSD;
565 Assert.AreEqual(2, array.Count);
566  
567 //The first element of top level array, a map
568 Assert.AreEqual(OSDType.Map, array[0].Type);
569 map = (OSDMap)array[0];
570 //First nested map
571 tempSD = map["Map One"];
572 Assert.IsNotNull(tempSD);
573 Assert.AreEqual(OSDType.Map, tempSD.Type);
574 map = (OSDMap)tempSD;
575 //First nested array
576 tempSD = map["Array One"];
577 Assert.IsNotNull(tempSD);
578 Assert.AreEqual(OSDType.Array, tempSD.Type);
579 array = (OSDArray)tempSD;
580 Assert.AreEqual(2, array.Count);
581  
582 array = (OSDArray)theSD;
583 //Second element of top level array, an array
584 tempSD = array[1];
585 Assert.AreEqual(OSDType.Array, tempSD.Type);
586 array = (OSDArray)tempSD;
587 Assert.AreEqual(3, array.Count);
588 //Nested array
589 tempSD = array[2];
590 Assert.AreEqual(OSDType.Array, tempSD.Type);
591 array = (OSDArray)tempSD;
592 Assert.AreEqual(3, array.Count);
593 }
594 }
595  
596 internal static class TestHelper
597 {
598 /// <summary>
599 /// Asserts that the contents of the SDBinary match the values and length
600 /// of the supplied byte array
601 /// </summary>
602 /// <param name="inBinary"></param>
603 /// <param name="inExpected"></param>
604 internal static void TestBinary(OSDBinary inBinary, byte[] inExpected)
605 {
606 byte[] binary = inBinary.AsBinary();
607 Assert.AreEqual(inExpected.Length, binary.Length);
608 for (int i = 0; i < inExpected.Length; i++)
609 {
610 if (inExpected[i] != binary[i])
611 {
612 Assert.Fail("Expected " + inExpected[i].ToString() + " at position " + i.ToString() +
613 " but saw " + binary[i].ToString());
614 }
615 }
616 }
617 }
618 }