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 /*
28 *
29 * This tests are based upon the description at
30 *
31 * http://wiki.secondlife.com/wiki/LLSD
32 *
33 * and (partially) generated by the (supposed) reference implementation at
34 *
35 * http://svn.secondlife.com/svn/linden/release/indra/lib/python/indra/base/llsd.py
36 *
37 */
38  
39 using System;
40 using System.IO;
41 using System.Text;
42 using System.Xml;
43 using NUnit.Framework;
44 using OpenMetaverse.StructuredData;
45  
46 namespace OpenMetaverse.Tests
47 {
48  
49 [TestFixture()]
50 public class BinarySDTests
51 {
52 private static readonly byte[] binaryHead = Encoding.ASCII.GetBytes("<?llsd/binary?>\n");
53  
54 [Test()]
55 public void HelperFunctions()
56 {
57 string s = "this is a teststring so that we can find something from the beginning";
58 byte[] sBinary = Encoding.ASCII.GetBytes(s);
59 MemoryStream stream = new MemoryStream(sBinary);
60  
61 stream.Position = 0L;
62 bool result = OSDParser.FindString(stream, "this");
63 Assert.AreEqual(true, result);
64 Assert.AreEqual(4L, stream.Position);
65  
66 stream.Position = 10L;
67 result = OSDParser.FindString(stream, "teststring");
68 Assert.AreEqual(true, result);
69 Assert.AreEqual(20L, stream.Position);
70  
71 stream.Position = 25L;
72 result = OSDParser.FindString(stream, "notfound");
73 Assert.AreEqual(false, result);
74 Assert.AreEqual(25L, stream.Position);
75  
76 stream.Position = 60L;
77 result = OSDParser.FindString(stream, "beginningAndMore");
78 Assert.AreEqual(false, result);
79 Assert.AreEqual(60L, stream.Position);
80  
81 byte[] sFrontWhiteSpace = Encoding.ASCII.GetBytes(" \t\t\n\rtest");
82 MemoryStream streamTwo = new MemoryStream(sFrontWhiteSpace);
83 OSDParser.SkipWhiteSpace(streamTwo);
84 Assert.AreEqual(7L, streamTwo.Position);
85  
86 byte[] sMiddleWhiteSpace = Encoding.ASCII.GetBytes("test \t\t\n\rtest");
87 MemoryStream streamThree = new MemoryStream(sMiddleWhiteSpace);
88 streamThree.Position = 4L;
89 OSDParser.SkipWhiteSpace(streamThree);
90 Assert.AreEqual(9L, streamThree.Position);
91  
92 byte[] sNoWhiteSpace = Encoding.ASCII.GetBytes("testtesttest");
93 MemoryStream streamFour = new MemoryStream(sNoWhiteSpace);
94 OSDParser.SkipWhiteSpace(streamFour);
95 Assert.AreEqual(0L, streamFour.Position);
96  
97 }
98  
99 // Testvalues for Undef:
100 private static byte[] binaryUndefValue = { 0x21 };
101 private static byte[] binaryUndef = (byte[])ConcatenateArrays(binaryHead, binaryUndefValue);
102  
103 [Test()]
104 public void DeserializeUndef()
105 {
106 OSD llsdUndef = OSDParser.DeserializeLLSDBinary(binaryUndef);
107 Assert.AreEqual(OSDType.Unknown, llsdUndef.Type);
108 }
109  
110 [Test()]
111 public void SerializeUndef()
112 {
113 OSD llsdUndef = new OSD();
114 byte[] binaryUndefSerialized = OSDParser.SerializeLLSDBinary(llsdUndef);
115 Assert.AreEqual(binaryUndef, binaryUndefSerialized);
116 }
117  
118 private static byte[] binaryTrueValue = { 0x31 };
119 private static byte[] binaryTrue = (byte[])ConcatenateArrays(binaryHead, binaryTrueValue);
120  
121  
122 private static byte[] binaryFalseValue = { 0x30 };
123 private static byte[] binaryFalse = (byte[])ConcatenateArrays(binaryHead, binaryFalseValue);
124  
125 [Test()]
126 public void DeserializeBool()
127 {
128 OSD llsdTrue = OSDParser.DeserializeLLSDBinary(binaryTrue);
129 Assert.AreEqual(OSDType.Boolean, llsdTrue.Type);
130 Assert.AreEqual(true, llsdTrue.AsBoolean());
131  
132 OSD llsdFalse = OSDParser.DeserializeLLSDBinary(binaryFalse);
133 Assert.AreEqual(OSDType.Boolean, llsdFalse.Type);
134 Assert.AreEqual(false, llsdFalse.AsBoolean());
135 }
136  
137 [Test()]
138 public void SerializeBool()
139 {
140 OSD llsdTrue = OSD.FromBoolean(true);
141 byte[] binaryTrueSerialized = OSDParser.SerializeLLSDBinary(llsdTrue);
142 Assert.AreEqual(binaryTrue, binaryTrueSerialized);
143  
144 OSD llsdFalse = OSD.FromBoolean(false);
145 byte[] binaryFalseSerialized = OSDParser.SerializeLLSDBinary(llsdFalse);
146 Assert.AreEqual(binaryFalse, binaryFalseSerialized);
147 }
148  
149 private static byte[] binaryZeroIntValue = { 0x69, 0x0, 0x0, 0x0, 0x0 };
150 private static byte[] binaryZeroInt = (byte[])ConcatenateArrays(binaryHead, binaryZeroIntValue);
151  
152 private static byte[] binaryAnIntValue = { 0x69, 0x0, 0x12, 0xd7, 0x9b };
153 private static byte[] binaryAnInt = (byte[])ConcatenateArrays(binaryHead, binaryAnIntValue);
154  
155 [Test()]
156 public void DeserializeInteger()
157 {
158 OSD llsdZeroInteger = OSDParser.DeserializeLLSDBinary(binaryZeroInt);
159 Assert.AreEqual(OSDType.Integer, llsdZeroInteger.Type);
160 Assert.AreEqual(0, llsdZeroInteger.AsInteger());
161  
162  
163 OSD llsdAnInteger = OSDParser.DeserializeLLSDBinary(binaryAnInt);
164 Assert.AreEqual(OSDType.Integer, llsdAnInteger.Type);
165 Assert.AreEqual(1234843, llsdAnInteger.AsInteger());
166 }
167  
168 [Test()]
169 public void SerializeInteger()
170 {
171 OSD llsdZeroInt = OSD.FromInteger(0);
172 byte[] binaryZeroIntSerialized = OSDParser.SerializeLLSDBinary(llsdZeroInt);
173 Assert.AreEqual(binaryZeroInt, binaryZeroIntSerialized);
174  
175 binaryZeroIntSerialized = OSDParser.SerializeLLSDBinary(llsdZeroInt, false);
176 Assert.AreEqual(binaryZeroIntValue, binaryZeroIntSerialized);
177  
178 OSD llsdAnInt = OSD.FromInteger(1234843);
179 byte[] binaryAnIntSerialized = OSDParser.SerializeLLSDBinary(llsdAnInt);
180 Assert.AreEqual(binaryAnInt, binaryAnIntSerialized);
181  
182 binaryAnIntSerialized = OSDParser.SerializeLLSDBinary(llsdAnInt, false);
183 Assert.AreEqual(binaryAnIntValue, binaryAnIntSerialized);
184 }
185  
186 private static byte[] binaryRealValue = { 0x72, 0x41, 0x2c, 0xec, 0xf6, 0x77, 0xce, 0xd9, 0x17 };
187 private static byte[] binaryReal = (byte[])ConcatenateArrays(binaryHead, binaryRealValue);
188  
189 [Test()]
190 public void DeserializeReal()
191 {
192 OSD llsdReal = OSDParser.DeserializeLLSDBinary(binaryReal);
193 Assert.AreEqual(OSDType.Real, llsdReal.Type);
194 Assert.AreEqual(947835.234d, llsdReal.AsReal());
195 }
196  
197 [Test()]
198 public void SerializeReal()
199 {
200 OSD llsdReal = OSD.FromReal(947835.234d);
201 byte[] binaryRealSerialized = OSDParser.SerializeLLSDBinary(llsdReal);
202 Assert.AreEqual(binaryReal, binaryRealSerialized);
203  
204 binaryRealSerialized = OSDParser.SerializeLLSDBinary(llsdReal);
205 Assert.AreEqual(binaryReal, binaryRealSerialized);
206 }
207  
208 private static byte[] binaryAUUIDValue = { 0x75, 0x97, 0xf4, 0xae, 0xca, 0x88, 0xa1, 0x42, 0xa1,
209 0xb3, 0x85, 0xb9, 0x7b, 0x18, 0xab, 0xb2, 0x55 };
210 private static byte[] binaryAUUID = (byte[])ConcatenateArrays(binaryHead, binaryAUUIDValue);
211  
212 private static byte[] binaryZeroUUIDValue = { 0x75, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0 };
213 private static byte[] binaryZeroUUID = (byte[])ConcatenateArrays(binaryHead, binaryZeroUUIDValue);
214  
215  
216 [Test()]
217 public void DeserializeUUID()
218 {
219 OSD llsdAUUID = OSDParser.DeserializeLLSDBinary(binaryAUUID);
220 Assert.AreEqual(OSDType.UUID, llsdAUUID.Type);
221 Assert.AreEqual("97f4aeca-88a1-42a1-b385-b97b18abb255", llsdAUUID.AsString());
222  
223 OSD llsdZeroUUID = OSDParser.DeserializeLLSDBinary(binaryZeroUUID);
224 Assert.AreEqual(OSDType.UUID, llsdZeroUUID.Type);
225 Assert.AreEqual("00000000-0000-0000-0000-000000000000", llsdZeroUUID.AsString());
226  
227 }
228  
229 [Test()]
230 public void SerializeUUID()
231 {
232 OSD llsdAUUID = OSD.FromUUID(new UUID("97f4aeca-88a1-42a1-b385-b97b18abb255"));
233 byte[] binaryAUUIDSerialized = OSDParser.SerializeLLSDBinary(llsdAUUID);
234 Assert.AreEqual(binaryAUUID, binaryAUUIDSerialized);
235  
236 binaryAUUIDSerialized = OSDParser.SerializeLLSDBinary(llsdAUUID);
237 Assert.AreEqual(binaryAUUID, binaryAUUIDSerialized);
238  
239 OSD llsdZeroUUID = OSD.FromUUID(new UUID("00000000-0000-0000-0000-000000000000"));
240 byte[] binaryZeroUUIDSerialized = OSDParser.SerializeLLSDBinary(llsdZeroUUID);
241 Assert.AreEqual(binaryZeroUUID, binaryZeroUUIDSerialized);
242  
243 binaryZeroUUIDSerialized = OSDParser.SerializeLLSDBinary(llsdZeroUUID);
244 Assert.AreEqual(binaryZeroUUID, binaryZeroUUIDSerialized);
245 }
246  
247 private static byte[] binaryBinStringValue = { 0x62, 0x0, 0x0, 0x0, 0x34, // this line is the encoding header
248 0x74, 0x65, 0x73, 0x74, 0x69, 0x6e, 0x67, 0x20, 0x61, 0x20, 0x73,
249 0x69, 0x6d, 0x70, 0x6c, 0x65, 0x20, 0x62, 0x69, 0x6e, 0x61, 0x72, 0x79, 0x20, 0x63, 0x6f,
250 0x6e, 0x76, 0x65, 0x72, 0x73, 0x69, 0x6f, 0x6e, 0x20, 0x66, 0x6f, 0x72, 0x20, 0x74, 0x68,
251 0x69, 0x73, 0x20, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67, 0xa, 0xd };
252 private static byte[] binaryBinString = (byte[])ConcatenateArrays(binaryHead, binaryBinStringValue);
253  
254 [Test()]
255 public void DeserializeLLSDBinary()
256 {
257 OSD llsdBytes = OSDParser.DeserializeLLSDBinary(binaryBinString);
258 Assert.AreEqual(OSDType.Binary, llsdBytes.Type);
259 byte[] contentBinString = { 0x74, 0x65, 0x73, 0x74, 0x69, 0x6e, 0x67, 0x20, 0x61, 0x20, 0x73,
260 0x69, 0x6d, 0x70, 0x6c, 0x65, 0x20, 0x62, 0x69, 0x6e, 0x61, 0x72, 0x79, 0x20, 0x63, 0x6f,
261 0x6e, 0x76, 0x65, 0x72, 0x73, 0x69, 0x6f, 0x6e, 0x20, 0x66, 0x6f, 0x72, 0x20, 0x74, 0x68,
262 0x69, 0x73, 0x20, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67, 0xa, 0xd };
263 Assert.AreEqual(contentBinString, llsdBytes.AsBinary());
264 }
265  
266 [Test()]
267 public void SerializeLLSDBinary()
268 {
269 byte[] contentBinString = { 0x74, 0x65, 0x73, 0x74, 0x69, 0x6e, 0x67, 0x20, 0x61, 0x20, 0x73,
270 0x69, 0x6d, 0x70, 0x6c, 0x65, 0x20, 0x62, 0x69, 0x6e, 0x61, 0x72, 0x79, 0x20, 0x63, 0x6f,
271 0x6e, 0x76, 0x65, 0x72, 0x73, 0x69, 0x6f, 0x6e, 0x20, 0x66, 0x6f, 0x72, 0x20, 0x74, 0x68,
272 0x69, 0x73, 0x20, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67, 0xa, 0xd };
273 OSD llsdBinary = OSD.FromBinary(contentBinString);
274 byte[] binaryBinarySerialized = OSDParser.SerializeLLSDBinary(llsdBinary);
275 Assert.AreEqual(binaryBinString, binaryBinarySerialized);
276 }
277  
278 private static byte[] binaryEmptyStringValue = { 0x73, 0x0, 0x0, 0x0, 0x0 };
279 private static byte[] binaryEmptyString = (byte[])ConcatenateArrays(binaryHead, binaryEmptyStringValue);
280 private static byte[] binaryLongStringValue = { 0x73, 0x0, 0x0, 0x0, 0x25,
281 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
282 0x67, 0x68, 0x69, 0x6a, 0x6b, 0x6c,
283 0x6d, 0x6e, 0x6f, 0x70, 0x71, 0x72,
284 0x73, 0x74, 0x75, 0x76, 0x77, 0x78,
285 0x79, 0x7a, 0x30, 0x31, 0x32, 0x33,
286 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x30 };
287 private static byte[] binaryLongString = (byte[])ConcatenateArrays(binaryHead, binaryLongStringValue);
288  
289 [Test()]
290 public void DeserializeString()
291 {
292 OSD llsdEmptyString = OSDParser.DeserializeLLSDBinary(binaryEmptyString);
293 Assert.AreEqual(OSDType.String, llsdEmptyString.Type);
294 string contentEmptyString = "";
295 Assert.AreEqual(contentEmptyString, llsdEmptyString.AsString());
296  
297 OSD llsdLongString = OSDParser.DeserializeLLSDBinary(binaryLongString);
298 Assert.AreEqual(OSDType.String, llsdLongString.Type);
299 string contentLongString = "abcdefghijklmnopqrstuvwxyz01234567890";
300 Assert.AreEqual(contentLongString, llsdLongString.AsString());
301 }
302  
303 [Test()]
304 public void SerializeString()
305 {
306 OSD llsdString = OSD.FromString("abcdefghijklmnopqrstuvwxyz01234567890");
307 byte[] binaryLongStringSerialized = OSDParser.SerializeLLSDBinary(llsdString);
308 Assert.AreEqual(binaryLongString, binaryLongStringSerialized);
309  
310 // A test with some utf8 characters
311 string contentAStringXML = "<x>&#x196;&#x214;&#x220;&#x228;&#x246;&#x252;</x>";
312 byte[] bytes = Encoding.UTF8.GetBytes(contentAStringXML);
313 XmlTextReader xtr = new XmlTextReader(new MemoryStream(bytes, false));
314 xtr.Read();
315 xtr.Read();
316  
317 string contentAString = xtr.ReadString();
318 OSD llsdAString = OSD.FromString(contentAString);
319 byte[] binaryAString = OSDParser.SerializeLLSDBinary(llsdAString);
320 OSD llsdAStringDS = OSDParser.DeserializeLLSDBinary(binaryAString);
321 Assert.AreEqual(OSDType.String, llsdAStringDS.Type);
322 Assert.AreEqual(contentAString, llsdAStringDS.AsString());
323  
324 // we also test for a 4byte character.
325 string xml = "<x>&#x10137;</x>";
326 byte[] bytesTwo = Encoding.UTF8.GetBytes(xml);
327 XmlTextReader xtrTwo = new XmlTextReader(new MemoryStream(bytesTwo, false));
328 xtrTwo.Read();
329 xtrTwo.Read();
330 string content = xtrTwo.ReadString();
331  
332 OSD llsdStringOne = OSD.FromString(content);
333 byte[] binaryAStringOneSerialized = OSDParser.SerializeLLSDBinary(llsdStringOne);
334 OSD llsdStringOneDS = OSDParser.DeserializeLLSDBinary(binaryAStringOneSerialized);
335 Assert.AreEqual(OSDType.String, llsdStringOneDS.Type);
336 Assert.AreEqual(content, llsdStringOneDS.AsString());
337  
338 }
339  
340 // Be careful. The current and above mentioned reference implementation has a bug that
341 // doesnt allow proper binary Uri encoding.
342 // We compare here to a fixed version of Uri encoding
343 private static byte[] binaryURIValue = { 0x6c, 0x0, 0x0, 0x0, 0x18, // this line is the encoding header
344 0x68, 0x74, 0x74, 0x70, 0x3a, 0x2f, 0x2f, 0x77, 0x77, 0x77, 0x2e, 0x74,
345 0x65, 0x73, 0x74, 0x75, 0x72, 0x6c, 0x2e, 0x74, 0x65, 0x73, 0x74, 0x2f };
346 private static byte[] binaryURI = (byte[])ConcatenateArrays(binaryHead, binaryURIValue);
347  
348 [Test()]
349 public void DeserializeURI()
350 {
351 OSD llsdURI = OSDParser.DeserializeLLSDBinary(binaryURI);
352 Assert.AreEqual(OSDType.URI, llsdURI.Type);
353 Uri uri = new Uri("http://www.testurl.test/");
354 Assert.AreEqual(uri, llsdURI.AsUri());
355  
356 }
357  
358 [Test()]
359 public void SerializeURI()
360 {
361 OSD llsdUri = OSD.FromUri(new Uri("http://www.testurl.test/"));
362 byte[] binaryURISerialized = OSDParser.SerializeLLSDBinary(llsdUri);
363 Assert.AreEqual(binaryURI, binaryURISerialized);
364 }
365  
366 // Here is a problem.
367 // The reference implementation does serialize to a local timestamp and not to a universal timestamp,
368 // which means, this implementation and the reference implementation only work the same in the universal
369 // timezone. Therefore this binaryDateTimeValue is generated in the UTC timezone by the reference
370 // implementation.
371 private static byte[] binaryDateTimeValue = { 100, 0, 0, 192, 141, 167, 222, 209, 65 };
372 private static byte[] binaryDateTime = (byte[])ConcatenateArrays(binaryHead, binaryDateTimeValue);
373  
374 [Test()]
375 public void DeserializeDateTime()
376 {
377 OSD llsdDateTime = OSDParser.DeserializeLLSDBinary(binaryDateTime);
378 Assert.AreEqual(OSDType.Date, llsdDateTime.Type);
379 DateTime dt = new DateTime(2008, 1, 1, 20, 10, 31, 0, DateTimeKind.Utc);
380 DateTime dateLocal = llsdDateTime.AsDate();
381 Assert.AreEqual(dt, dateLocal.ToUniversalTime());
382 }
383  
384 [Test()]
385 public void SerializeDateTime()
386 {
387 DateTime dt = new DateTime(2008, 1, 1, 20, 10, 31, 0, DateTimeKind.Utc);
388 OSD llsdDate = OSD.FromDate(dt);
389 byte[] binaryDateSerialized = OSDParser.SerializeLLSDBinary(llsdDate);
390 Assert.AreEqual(binaryDateTime, binaryDateSerialized);
391  
392 // check if a *local* time can be serialized and deserialized
393 DateTime dtOne = new DateTime(2009, 12, 30, 8, 25, 10, DateTimeKind.Local);
394 OSD llsdDateOne = OSD.FromDate(dtOne);
395 byte[] binaryDateOneSerialized = OSDParser.SerializeLLSDBinary(llsdDateOne);
396 OSD llsdDateOneDS = OSDParser.DeserializeLLSDBinary(binaryDateOneSerialized);
397 Assert.AreEqual(OSDType.Date, llsdDateOneDS.Type);
398 Assert.AreEqual(dtOne, llsdDateOneDS.AsDate());
399  
400 DateTime dtTwo = new DateTime(2010, 11, 11, 10, 8, 20, DateTimeKind.Utc);
401 OSD llsdDateTwo = OSD.FromDate(dtTwo);
402 byte[] binaryDateTwoSerialized = OSDParser.SerializeLLSDBinary(llsdDateTwo);
403 OSD llsdDateTwoDS = OSDParser.DeserializeLLSDBinary(binaryDateTwoSerialized);
404 Assert.AreEqual(OSDType.Date, llsdDateOneDS.Type);
405 Assert.AreEqual(dtTwo.ToLocalTime(), llsdDateTwoDS.AsDate());
406 }
407  
408 // Data for empty array { }
409 private static byte[] binaryEmptyArrayValue = { 0x5b, 0x0, 0x0, 0x0, 0x0, 0x5d };
410 // Encoding header + num of elements + tail
411 private static byte[] binaryEmptyArray = (byte[])ConcatenateArrays(binaryHead, binaryEmptyArrayValue);
412 // Data for simple array { 0 }
413 private static byte[] binarySimpleArrayValue = { 0x5b, 0x0, 0x0, 0x0, 0x1, // Encoding header + num of elements
414 0x69, 0x0, 0x0, 0x0, 0x0, 0x5d };
415 private static byte[] binarySimpleArray = (byte[])ConcatenateArrays(binaryHead, binarySimpleArrayValue);
416  
417 // Data for simple array { 0, 0 }
418 private static byte[] binarySimpleArrayTwoValue = { 0x5b, 0x0, 0x0, 0x0, 0x2, // Encoding header + num of elements
419 0x69, 0x0, 0x0, 0x0, 0x0,
420 0x69, 0x0, 0x0, 0x0, 0x0, 0x5d };
421 private static byte[] binarySimpleArrayTwo = (byte[])ConcatenateArrays(binaryHead, binarySimpleArrayTwoValue);
422  
423 [Test()]
424 public void DeserializeArray()
425 {
426 OSD llsdEmptyArray = OSDParser.DeserializeLLSDBinary(binaryEmptyArray);
427 Assert.AreEqual(OSDType.Array, llsdEmptyArray.Type);
428 OSDArray llsdEmptyArrayArray = (OSDArray)llsdEmptyArray;
429 Assert.AreEqual(0, llsdEmptyArrayArray.Count);
430  
431  
432 OSD llsdSimpleArray = OSDParser.DeserializeLLSDBinary(binarySimpleArray);
433 Assert.AreEqual(OSDType.Array, llsdSimpleArray.Type);
434 OSDArray llsdArray = (OSDArray)llsdSimpleArray;
435 Assert.AreEqual(OSDType.Integer, llsdArray[0].Type);
436 Assert.AreEqual(0, llsdArray[0].AsInteger());
437  
438  
439 OSD llsdSimpleArrayTwo = OSDParser.DeserializeLLSDBinary(binarySimpleArrayTwo);
440 Assert.AreEqual(OSDType.Array, llsdSimpleArrayTwo.Type);
441 OSDArray llsdArrayTwo = (OSDArray)llsdSimpleArrayTwo;
442 Assert.AreEqual(2, llsdArrayTwo.Count);
443  
444 Assert.AreEqual(OSDType.Integer, llsdArrayTwo[0].Type);
445 Assert.AreEqual(0, llsdArrayTwo[0].AsInteger());
446 Assert.AreEqual(OSDType.Integer, llsdArrayTwo[1].Type);
447 Assert.AreEqual(0, llsdArrayTwo[1].AsInteger());
448 }
449  
450 [Test()]
451 public void SerializeArray()
452 {
453 OSDArray llsdEmptyArray = new OSDArray();
454 byte[] binaryEmptyArraySerialized = OSDParser.SerializeLLSDBinary(llsdEmptyArray);
455 Assert.AreEqual(binaryEmptyArray, binaryEmptyArraySerialized);
456  
457 binaryEmptyArraySerialized = OSDParser.SerializeLLSDBinary(llsdEmptyArray, false);
458 Assert.AreEqual(binaryEmptyArrayValue, binaryEmptyArraySerialized);
459  
460 OSDArray llsdSimpleArray = new OSDArray();
461 llsdSimpleArray.Add(OSD.FromInteger(0));
462 byte[] binarySimpleArraySerialized = OSDParser.SerializeLLSDBinary(llsdSimpleArray);
463 Assert.AreEqual(binarySimpleArray, binarySimpleArraySerialized);
464  
465 binarySimpleArraySerialized = OSDParser.SerializeLLSDBinary(llsdSimpleArray, false);
466 Assert.AreEqual(binarySimpleArrayValue, binarySimpleArraySerialized);
467  
468 OSDArray llsdSimpleArrayTwo = new OSDArray();
469 llsdSimpleArrayTwo.Add(OSD.FromInteger(0));
470 llsdSimpleArrayTwo.Add(OSD.FromInteger(0));
471 byte[] binarySimpleArrayTwoSerialized = OSDParser.SerializeLLSDBinary(llsdSimpleArrayTwo);
472 Assert.AreEqual(binarySimpleArrayTwo, binarySimpleArrayTwoSerialized);
473  
474 binarySimpleArrayTwoSerialized = OSDParser.SerializeLLSDBinary(llsdSimpleArrayTwo, false);
475 Assert.AreEqual(binarySimpleArrayTwoValue, binarySimpleArrayTwoSerialized);
476 }
477  
478 // Data for empty dictionary { }
479 private static byte[] binaryEmptyMapValue = { 0x7b, 0x0, 0x0, 0x0, 0x0, 0x7d };
480 private static byte[] binaryEmptyMap = (byte[])ConcatenateArrays(binaryHead, binaryEmptyMapValue);
481  
482 // Data for simple dictionary { test = 0 }
483 private static byte[] binarySimpleMapValue = { 0x7b, 0x0, 0x0, 0x0, 0x1, // Encoding header + num of elements
484 0x6b, 0x0, 0x0, 0x0, 0x4, // 'k' + keylength
485 0x74, 0x65, 0x73, 0x74, // key 'test'
486 0x69, 0x0, 0x0, 0x0, 0x0, // i + '0'
487 0x7d };
488 private static byte[] binarySimpleMap = (byte[])ConcatenateArrays(binaryHead, binarySimpleMapValue);
489  
490 // Data for simple dictionary { t0st = 241, tes1 = "aha", test = undef }
491 private static byte[] binarySimpleMapTwoValue = { 0x7b, 0x0, 0x0, 0x0, 0x3, // Encoding header + num of elements
492 0x6b, 0x0, 0x0, 0x0, 0x4, // 'k' + keylength
493 0x74, 0x65, 0x73, 0x74, // key 'test'
494 0x21, // undef
495 0x6b, 0x0, 0x0, 0x0, 0x4, // k + keylength
496 0x74, 0x65, 0x73, 0x31, // key 'tes1'
497 0x73, 0x0, 0x0, 0x0, 0x3, // string head + length
498 0x61, 0x68, 0x61, // 'aha'
499 0x6b, 0x0, 0x0, 0x0, 0x4, // k + keylength
500 0x74, 0x30, 0x73, 0x74, // key 't0st'
501 0x69, 0x0, 0x0, 0x0, 0xf1, // integer 241
502 0x7d };
503 private static byte[] binarySimpleMapTwo = (byte[])ConcatenateArrays(binaryHead, binarySimpleMapTwoValue);
504  
505 [Test()]
506 public void DeserializeDictionary()
507 {
508 OSDMap llsdEmptyMap = (OSDMap)OSDParser.DeserializeLLSDBinary(binaryEmptyMap);
509 Assert.AreEqual(OSDType.Map, llsdEmptyMap.Type);
510 Assert.AreEqual(0, llsdEmptyMap.Count);
511  
512 OSDMap llsdSimpleMap = (OSDMap)OSDParser.DeserializeLLSDBinary(binarySimpleMap);
513 Assert.AreEqual(OSDType.Map, llsdSimpleMap.Type);
514 Assert.AreEqual(1, llsdSimpleMap.Count);
515 Assert.AreEqual(OSDType.Integer, llsdSimpleMap["test"].Type);
516 Assert.AreEqual(0, llsdSimpleMap["test"].AsInteger());
517  
518 OSDMap llsdSimpleMapTwo = (OSDMap)OSDParser.DeserializeLLSDBinary(binarySimpleMapTwo);
519 Assert.AreEqual(OSDType.Map, llsdSimpleMapTwo.Type);
520 Assert.AreEqual(3, llsdSimpleMapTwo.Count);
521 Assert.AreEqual(OSDType.Unknown, llsdSimpleMapTwo["test"].Type);
522 Assert.AreEqual(OSDType.String, llsdSimpleMapTwo["tes1"].Type);
523 Assert.AreEqual("aha", llsdSimpleMapTwo["tes1"].AsString());
524 Assert.AreEqual(OSDType.Integer, llsdSimpleMapTwo["t0st"].Type);
525 Assert.AreEqual(241, llsdSimpleMapTwo["t0st"].AsInteger());
526  
527  
528 }
529  
530 [Test()]
531 public void SerializeDictionary()
532 {
533 OSDMap llsdEmptyMap = new OSDMap();
534 byte[] binaryEmptyMapSerialized = OSDParser.SerializeLLSDBinary(llsdEmptyMap);
535 Assert.AreEqual(binaryEmptyMap, binaryEmptyMapSerialized);
536  
537 OSDMap llsdSimpleMap = new OSDMap();
538 llsdSimpleMap["test"] = OSD.FromInteger(0);
539 byte[] binarySimpleMapSerialized = OSDParser.SerializeLLSDBinary(llsdSimpleMap);
540 Assert.AreEqual(binarySimpleMap, binarySimpleMapSerialized);
541  
542 OSDMap llsdSimpleMapTwo = new OSDMap();
543 llsdSimpleMapTwo["t0st"] = OSD.FromInteger(241);
544 llsdSimpleMapTwo["tes1"] = OSD.FromString("aha");
545 llsdSimpleMapTwo["test"] = new OSD();
546 byte[] binarySimpleMapTwoSerialized = OSDParser.SerializeLLSDBinary(llsdSimpleMapTwo);
547  
548 // We dont compare here to the original serialized value, because, as maps dont preserve order,
549 // the original serialized value is not *exactly* the same. Instead we compare to a deserialized
550 // version created by this deserializer.
551 OSDMap llsdSimpleMapDeserialized = (OSDMap)OSDParser.DeserializeLLSDBinary(binarySimpleMapTwoSerialized);
552 Assert.AreEqual(OSDType.Map, llsdSimpleMapDeserialized.Type);
553 Assert.AreEqual(3, llsdSimpleMapDeserialized.Count);
554 Assert.AreEqual(OSDType.Integer, llsdSimpleMapDeserialized["t0st"].Type);
555 Assert.AreEqual(241, llsdSimpleMapDeserialized["t0st"].AsInteger());
556 Assert.AreEqual(OSDType.String, llsdSimpleMapDeserialized["tes1"].Type);
557 Assert.AreEqual("aha", llsdSimpleMapDeserialized["tes1"].AsString());
558 Assert.AreEqual(OSDType.Unknown, llsdSimpleMapDeserialized["test"].Type);
559  
560 // we also test for a 4byte key character.
561 string xml = "<x>&#x10137;</x>";
562 byte[] bytes = Encoding.UTF8.GetBytes(xml);
563 XmlTextReader xtr = new XmlTextReader(new MemoryStream(bytes, false));
564 xtr.Read();
565 xtr.Read();
566 string content = xtr.ReadString();
567  
568 OSDMap llsdSimpleMapThree = new OSDMap();
569 OSD llsdSimpleValue = OSD.FromString(content);
570 llsdSimpleMapThree[content] = llsdSimpleValue;
571 Assert.AreEqual(content, llsdSimpleMapThree[content].AsString());
572  
573 byte[] binarySimpleMapThree = OSDParser.SerializeLLSDBinary(llsdSimpleMapThree);
574 OSDMap llsdSimpleMapThreeDS = (OSDMap)OSDParser.DeserializeLLSDBinary(binarySimpleMapThree);
575 Assert.AreEqual(OSDType.Map, llsdSimpleMapThreeDS.Type);
576 Assert.AreEqual(1, llsdSimpleMapThreeDS.Count);
577 Assert.AreEqual(content, llsdSimpleMapThreeDS[content].AsString());
578  
579 }
580  
581 private static byte[] binaryNestedValue = { 0x5b, 0x0, 0x0, 0x0, 0x3,
582 0x7b, 0x0, 0x0, 0x0, 0x2,
583 0x6b, 0x0, 0x0, 0x0, 0x4,
584 0x74, 0x65, 0x73, 0x74,
585 0x73, 0x0, 0x0, 0x0, 0x4,
586 0x77, 0x68, 0x61, 0x74,
587 0x6b, 0x0, 0x0, 0x0, 0x4,
588 0x74, 0x30, 0x73,
589 0x74, 0x5b, 0x0, 0x0, 0x0, 0x2,
590 0x69, 0x0, 0x0, 0x0, 0x1,
591 0x69, 0x0, 0x0, 0x0, 0x2,
592 0x5d, 0x7d, 0x69, 0x0, 0x0, 0x0,
593 0x7c, 0x69, 0x0, 0x0, 0x3, 0xdb,
594 0x5d };
595 private static byte[] binaryNested = (byte[])ConcatenateArrays(binaryHead, binaryNestedValue);
596  
597 [Test()]
598 public void DeserializeNestedComposite()
599 {
600 OSD llsdNested = OSDParser.DeserializeLLSDBinary(binaryNested);
601 Assert.AreEqual(OSDType.Array, llsdNested.Type);
602 OSDArray llsdArray = (OSDArray)llsdNested;
603 Assert.AreEqual(3, llsdArray.Count);
604  
605 OSDMap llsdMap = (OSDMap)llsdArray[0];
606 Assert.AreEqual(OSDType.Map, llsdMap.Type);
607 Assert.AreEqual(2, llsdMap.Count);
608  
609 OSDArray llsdNestedArray = (OSDArray)llsdMap["t0st"];
610 Assert.AreEqual(OSDType.Array, llsdNestedArray.Type);
611 OSDInteger llsdNestedIntOne = (OSDInteger)llsdNestedArray[0];
612 Assert.AreEqual(OSDType.Integer, llsdNestedIntOne.Type);
613 Assert.AreEqual(1, llsdNestedIntOne.AsInteger());
614 OSDInteger llsdNestedIntTwo = (OSDInteger)llsdNestedArray[1];
615 Assert.AreEqual(OSDType.Integer, llsdNestedIntTwo.Type);
616 Assert.AreEqual(2, llsdNestedIntTwo.AsInteger());
617  
618 OSDString llsdString = (OSDString)llsdMap["test"];
619 Assert.AreEqual(OSDType.String, llsdString.Type);
620 Assert.AreEqual("what", llsdString.AsString());
621  
622 OSDInteger llsdIntOne = (OSDInteger)llsdArray[1];
623 Assert.AreEqual(OSDType.Integer, llsdIntOne.Type);
624 Assert.AreEqual(124, llsdIntOne.AsInteger());
625 OSDInteger llsdIntTwo = (OSDInteger)llsdArray[2];
626 Assert.AreEqual(OSDType.Integer, llsdIntTwo.Type);
627 Assert.AreEqual(987, llsdIntTwo.AsInteger());
628  
629 }
630  
631 [Test()]
632 public void SerializeNestedComposite()
633 {
634 OSDArray llsdNested = new OSDArray();
635 OSDMap llsdMap = new OSDMap();
636 OSDArray llsdArray = new OSDArray();
637 llsdArray.Add(OSD.FromInteger(1));
638 llsdArray.Add(OSD.FromInteger(2));
639 llsdMap["t0st"] = llsdArray;
640 llsdMap["test"] = OSD.FromString("what");
641 llsdNested.Add(llsdMap);
642 llsdNested.Add(OSD.FromInteger(124));
643 llsdNested.Add(OSD.FromInteger(987));
644  
645 byte[] binaryNestedSerialized = OSDParser.SerializeLLSDBinary(llsdNested);
646 // Because maps don't preserve order, we compare here to a deserialized value.
647 OSDArray llsdNestedDeserialized = (OSDArray)OSDParser.DeserializeLLSDBinary(binaryNestedSerialized);
648 Assert.AreEqual(OSDType.Array, llsdNestedDeserialized.Type);
649 Assert.AreEqual(3, llsdNestedDeserialized.Count);
650  
651 OSDMap llsdMapDeserialized = (OSDMap)llsdNestedDeserialized[0];
652 Assert.AreEqual(OSDType.Map, llsdMapDeserialized.Type);
653 Assert.AreEqual(2, llsdMapDeserialized.Count);
654 Assert.AreEqual(OSDType.Array, llsdMapDeserialized["t0st"].Type);
655  
656 OSDArray llsdNestedArray = (OSDArray)llsdMapDeserialized["t0st"];
657 Assert.AreEqual(OSDType.Array, llsdNestedArray.Type);
658 Assert.AreEqual(2, llsdNestedArray.Count);
659 Assert.AreEqual(OSDType.Integer, llsdNestedArray[0].Type);
660 Assert.AreEqual(1, llsdNestedArray[0].AsInteger());
661 Assert.AreEqual(OSDType.Integer, llsdNestedArray[1].Type);
662 Assert.AreEqual(2, llsdNestedArray[1].AsInteger());
663  
664 Assert.AreEqual(OSDType.String, llsdMapDeserialized["test"].Type);
665 Assert.AreEqual("what", llsdMapDeserialized["test"].AsString());
666  
667 Assert.AreEqual(OSDType.Integer, llsdNestedDeserialized[1].Type);
668 Assert.AreEqual(124, llsdNestedDeserialized[1].AsInteger());
669  
670 Assert.AreEqual(OSDType.Integer, llsdNestedDeserialized[2].Type);
671 Assert.AreEqual(987, llsdNestedDeserialized[2].AsInteger());
672  
673 }
674  
675 [Test()]
676 public void SerializeLongMessage()
677 {
678 // each 80 chars
679 string sOne = "asdklfjasadlfkjaerotiudfgjkhsdklgjhsdklfghasdfklhjasdfkjhasdfkljahsdfjklaasdfkj8";
680 string sTwo = "asdfkjlaaweoiugsdfjkhsdfg,.mnasdgfkljhrtuiohfglökajsdfoiwghjkdlaaaaseldkfjgheus9";
681  
682 OSD stringOne = OSD.FromString( sOne );
683 OSD stringTwo = OSD.FromString(sTwo);
684  
685 OSDMap llsdMap = new OSDMap();
686 llsdMap["testOne"] = stringOne;
687 llsdMap["testTwo"] = stringTwo;
688 llsdMap["testThree"] = stringOne;
689 llsdMap["testFour"] = stringTwo;
690 llsdMap["testFive"] = stringOne;
691 llsdMap["testSix"] = stringTwo;
692 llsdMap["testSeven"] = stringOne;
693 llsdMap["testEight"] = stringTwo;
694 llsdMap["testNine"] = stringOne;
695 llsdMap["testTen"] = stringTwo;
696  
697  
698 byte[] binaryData = OSDParser.SerializeLLSDBinary( llsdMap );
699  
700 OSDMap llsdMapDS = (OSDMap)OSDParser.DeserializeLLSDBinary( binaryData );
701 Assert.AreEqual( OSDType.Map, llsdMapDS.Type );
702 Assert.AreEqual( 10, llsdMapDS.Count );
703 Assert.AreEqual( sOne, llsdMapDS["testOne"].AsString());
704 Assert.AreEqual( sTwo, llsdMapDS["testTwo"].AsString());
705 Assert.AreEqual( sOne, llsdMapDS["testThree"].AsString());
706 Assert.AreEqual( sTwo, llsdMapDS["testFour"].AsString());
707 Assert.AreEqual( sOne, llsdMapDS["testFive"].AsString());
708 Assert.AreEqual( sTwo, llsdMapDS["testSix"].AsString());
709 Assert.AreEqual( sOne, llsdMapDS["testSeven"].AsString());
710 Assert.AreEqual( sTwo, llsdMapDS["testEight"].AsString());
711 Assert.AreEqual( sOne, llsdMapDS["testNine"].AsString());
712 Assert.AreEqual( sTwo, llsdMapDS["testTen"].AsString());
713 }
714  
715  
716 static Array ConcatenateArrays(params Array[] arrays)
717 {
718 if (arrays == null)
719 {
720 throw new ArgumentNullException("arrays");
721 }
722 if (arrays.Length == 0)
723 {
724 throw new ArgumentException("No arrays specified");
725 }
726  
727 Type type = arrays[0].GetType().GetElementType();
728 int totalLength = arrays[0].Length;
729 for (int i = 1; i < arrays.Length; i++)
730 {
731 if (arrays[i].GetType().GetElementType() != type)
732 {
733 throw new ArgumentException("Arrays must all be of the same type");
734 }
735 totalLength += arrays[i].Length;
736 }
737  
738 Array ret = Array.CreateInstance(type, totalLength);
739 int index = 0;
740 foreach (Array array in arrays)
741 {
742 Array.Copy(array, 0, ret, index, array.Length);
743 index += array.Length;
744 }
745 return ret;
746 }
747 }
748 }