Files
libremetaverse/LibreMetaverse.Tests/NotationLLSDTests.cs

750 lines
35 KiB
C#
Raw Blame History

/*
* Copyright (c) 2006-2016, openmetaverse.co
* Copyright (c) 2021-2024, Sjofn LLC.
* All rights reserved.
*
* - Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* - Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
* - Neither the name of the openmetaverse.co nor the names
* of its contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
/*
*
* This tests are based upon the description at
*
* http://wiki.secondlife.com/wiki/SD
*
* and (partially) generated by the (supposed) reference implementation at
*
* http://svn.secondlife.com/svn/linden/release/indra/lib/python/indra/base/llsd.py
*
*/
using System;
using System.IO;
using System.Text;
using System.Xml;
using NUnit.Framework;
using NUnit.Framework.Legacy;
using OpenMetaverse;
using OpenMetaverse.StructuredData;
namespace LibreMetaverse.Tests
{
[TestFixture()]
public class NotationSDTests
{
[Test()]
public void HelperFunctions()
{
StringReader reader = new StringReader("test1tast2test3");
char[] charsOne = { 't', 'e', 's', 't' };
int resultOne = OSDParser.BufferCharactersEqual(reader, charsOne, 0);
Assert.That(resultOne, Is.EqualTo(charsOne.Length));
char[] charsTwo = { '1', 't', 'e' };
int resultTwo = OSDParser.BufferCharactersEqual(reader, charsTwo, 0);
Assert.That(resultTwo, Is.EqualTo(2));
char[] charsThree = { 'a', 's', 't', '2', 't', 'e', 's' };
int resultThree = OSDParser.BufferCharactersEqual(reader, charsThree, 1);
Assert.That(resultThree, Is.EqualTo(1));
int resultFour = OSDParser.BufferCharactersEqual(reader, charsThree, 0);
Assert.That(resultFour, Is.EqualTo(charsThree.Length));
char[] charsFive = { 't', '3', 'a', 'a' };
int resultFive = OSDParser.BufferCharactersEqual(reader, charsFive, 0);
Assert.That(resultFive, Is.EqualTo(2));
}
[Test()]
public void DeserializeUndef()
{
string s = "!";
OSD llsd = OSDParser.DeserializeLLSDNotation(s);
Assert.That(llsd.Type, Is.EqualTo(OSDType.Unknown));
}
[Test()]
public void SerializeUndef()
{
OSD llsd = new OSD();
string s = OSDParser.SerializeLLSDNotation(llsd);
OSD llsdDS = OSDParser.DeserializeLLSDNotation(s);
Assert.That(llsdDS.Type, Is.EqualTo(OSDType.Unknown));
}
[Test()]
public void DeserializeBoolean()
{
string t = "true";
OSD llsdT = OSDParser.DeserializeLLSDNotation(t);
Assert.That(llsdT.Type, Is.EqualTo(OSDType.Boolean));
Assert.That(llsdT.AsBoolean(), Is.EqualTo(true));
string tTwo = "t";
OSD llsdTTwo = OSDParser.DeserializeLLSDNotation(tTwo);
Assert.That(llsdTTwo.Type, Is.EqualTo(OSDType.Boolean));
Assert.That(llsdTTwo.AsBoolean(), Is.EqualTo(true));
string tThree = "TRUE";
OSD llsdTThree = OSDParser.DeserializeLLSDNotation(tThree);
Assert.That(llsdTThree.Type, Is.EqualTo(OSDType.Boolean));
Assert.That(llsdTThree.AsBoolean(), Is.EqualTo(true));
string tFour = "T";
OSD llsdTFour = OSDParser.DeserializeLLSDNotation(tFour);
Assert.That(llsdTFour.Type, Is.EqualTo(OSDType.Boolean));
Assert.That(llsdTFour.AsBoolean(), Is.EqualTo(true));
string tFive = "1";
OSD llsdTFive = OSDParser.DeserializeLLSDNotation(tFive);
Assert.That(llsdTFive.Type, Is.EqualTo(OSDType.Boolean));
Assert.That(llsdTFive.AsBoolean(), Is.EqualTo(true));
string f = "false";
OSD llsdF = OSDParser.DeserializeLLSDNotation(f);
Assert.That(llsdF.Type, Is.EqualTo(OSDType.Boolean));
Assert.That(llsdF.AsBoolean(), Is.EqualTo(false));
string fTwo = "f";
OSD llsdFTwo = OSDParser.DeserializeLLSDNotation(fTwo);
Assert.That(llsdFTwo.Type, Is.EqualTo(OSDType.Boolean));
Assert.That(llsdFTwo.AsBoolean(), Is.EqualTo(false));
string fThree = "FALSE";
OSD llsdFThree = OSDParser.DeserializeLLSDNotation(fThree);
Assert.That(llsdFThree.Type, Is.EqualTo(OSDType.Boolean));
Assert.That(llsdFThree.AsBoolean(), Is.EqualTo(false));
string fFour = "F";
OSD llsdFFour = OSDParser.DeserializeLLSDNotation(fFour);
Assert.That(llsdFFour.Type, Is.EqualTo(OSDType.Boolean));
Assert.That(llsdFFour.AsBoolean(), Is.EqualTo(false));
string fFive = "0";
OSD llsdFFive = OSDParser.DeserializeLLSDNotation(fFive);
Assert.That(llsdFFive.Type, Is.EqualTo(OSDType.Boolean));
Assert.That(llsdFFive.AsBoolean(), Is.EqualTo(false));
}
[Test()]
public void SerializeBoolean()
{
OSD llsdTrue = OSD.FromBoolean(true);
string sTrue = OSDParser.SerializeLLSDNotation(llsdTrue);
OSD llsdTrueDS = OSDParser.DeserializeLLSDNotation(sTrue);
Assert.That(llsdTrueDS.Type, Is.EqualTo(OSDType.Boolean));
Assert.That(llsdTrueDS.AsBoolean(), Is.EqualTo(true));
OSD llsdFalse = OSD.FromBoolean(false);
string sFalse = OSDParser.SerializeLLSDNotation(llsdFalse);
OSD llsdFalseDS = OSDParser.DeserializeLLSDNotation(sFalse);
Assert.That(llsdFalseDS.Type, Is.EqualTo(OSDType.Boolean));
Assert.That(llsdFalseDS.AsBoolean(), Is.EqualTo(false));
}
[Test()]
public void DeserializeInteger()
{
string integerOne = "i12319423";
OSD llsdOne = OSDParser.DeserializeLLSDNotation(integerOne);
Assert.That(llsdOne.Type, Is.EqualTo(OSDType.Integer));
Assert.That(llsdOne.AsInteger(), Is.EqualTo(12319423));
string integerTwo = "i-489234";
OSD llsdTwo = OSDParser.DeserializeLLSDNotation(integerTwo);
Assert.That(llsdTwo.Type, Is.EqualTo(OSDType.Integer));
Assert.That(llsdTwo.AsInteger(), Is.EqualTo(-489234));
}
[Test()]
public void SerializeInteger()
{
OSD llsdOne = OSD.FromInteger(12319423);
string sOne = OSDParser.SerializeLLSDNotation(llsdOne);
OSD llsdOneDS = OSDParser.DeserializeLLSDNotation(sOne);
Assert.That(llsdOneDS.Type, Is.EqualTo(OSDType.Integer));
Assert.That(llsdOne.AsInteger(), Is.EqualTo(12319423));
OSD llsdTwo = OSD.FromInteger(-71892034);
string sTwo = OSDParser.SerializeLLSDNotation(llsdTwo);
OSD llsdTwoDS = OSDParser.DeserializeLLSDNotation(sTwo);
Assert.That(llsdTwoDS.Type, Is.EqualTo(OSDType.Integer));
Assert.That(llsdTwoDS.AsInteger(), Is.EqualTo(-71892034));
}
[Test()]
public void DeserializeReal()
{
string realOne = "r1123412345.465711";
OSD llsdOne = OSDParser.DeserializeLLSDNotation(realOne);
Assert.That(llsdOne.Type, Is.EqualTo(OSDType.Real));
Assert.That(llsdOne.AsReal(), Is.EqualTo(1123412345.465711d));
string realTwo = "r-11234684.923411";
OSD llsdTwo = OSDParser.DeserializeLLSDNotation(realTwo);
Assert.That(llsdTwo.Type, Is.EqualTo(OSDType.Real));
Assert.That(llsdTwo.AsReal(), Is.EqualTo(-11234684.923411d));
string realThree = "r1";
OSD llsdThree = OSDParser.DeserializeLLSDNotation(realThree);
Assert.That(llsdThree.Type, Is.EqualTo(OSDType.Real));
Assert.That(llsdThree.AsReal(), Is.EqualTo(1d));
string realFour = "r2.0193899999999998204e-06";
OSD llsdFour = OSDParser.DeserializeLLSDNotation(realFour);
Assert.That(llsdFour.Type, Is.EqualTo(OSDType.Real));
Assert.That(llsdFour.AsReal(), Is.EqualTo(2.0193899999999998204e-06d));
string realFive = "r0";
OSD llsdFive = OSDParser.DeserializeLLSDNotation(realFive);
Assert.That(llsdFive.Type, Is.EqualTo(OSDType.Real));
Assert.That(llsdFive.AsReal(), Is.EqualTo(0d));
}
[Test()]
public void SerializeReal()
{
OSD llsdOne = OSD.FromReal(12987234.723847d);
string sOne = OSDParser.SerializeLLSDNotation(llsdOne);
OSD llsdOneDS = OSDParser.DeserializeLLSDNotation(sOne);
Assert.That(llsdOneDS.Type, Is.EqualTo(OSDType.Real));
Assert.That(llsdOneDS.AsReal(), Is.EqualTo(12987234.723847d));
OSD llsdTwo = OSD.FromReal(-32347892.234234d);
string sTwo = OSDParser.SerializeLLSDNotation(llsdTwo);
OSD llsdTwoDS = OSDParser.DeserializeLLSDNotation(sTwo);
Assert.That(llsdTwoDS.Type, Is.EqualTo(OSDType.Real));
Assert.That(llsdTwoDS.AsReal(), Is.EqualTo(-32347892.234234d));
OSD llsdThree = OSD.FromReal( double.MaxValue );
string sThree = OSDParser.SerializeLLSDNotation( llsdThree );
OSD llsdThreeDS = OSDParser.DeserializeLLSDNotation( sThree );
Assert.That(llsdThreeDS.Type, Is.EqualTo(OSDType.Real));
Assert.That(llsdThreeDS.AsReal(), Is.EqualTo(double.MaxValue));
OSD llsdFour = OSD.FromReal(double.MinValue);
string sFour = OSDParser.SerializeLLSDNotation(llsdFour);
OSD llsdFourDS = OSDParser.DeserializeLLSDNotation(sFour);
Assert.That(llsdFourDS.Type, Is.EqualTo(OSDType.Real));
Assert.That(llsdFourDS.AsReal(), Is.EqualTo(double.MinValue));
OSD llsdFive = OSD.FromReal(-1.1123123E+50d);
string sFive = OSDParser.SerializeLLSDNotation(llsdFive);
OSD llsdFiveDS = OSDParser.DeserializeLLSDNotation(sFive);
Assert.That(llsdFiveDS.Type, Is.EqualTo(OSDType.Real));
Assert.That(llsdFiveDS.AsReal(), Is.EqualTo(-1.1123123E+50d));
OSD llsdSix = OSD.FromReal(2.0193899999999998204e-06);
string sSix = OSDParser.SerializeLLSDNotation(llsdSix);
OSD llsdSixDS = OSDParser.DeserializeLLSDNotation(sSix);
Assert.That(llsdSixDS.Type, Is.EqualTo(OSDType.Real));
Assert.That(llsdSixDS.AsReal(), Is.EqualTo(2.0193899999999998204e-06));
}
[Test()]
public void DeserializeUUID()
{
string uuidOne = "u97f4aeca-88a1-42a1-b385-b97b18abb255";
OSD llsdOne = OSDParser.DeserializeLLSDNotation(uuidOne);
Assert.That(llsdOne.Type, Is.EqualTo(OSDType.UUID));
Assert.That(llsdOne.AsString(), Is.EqualTo("97f4aeca-88a1-42a1-b385-b97b18abb255"));
string uuidTwo = "u00000000-0000-0000-0000-000000000000";
OSD llsdTwo = OSDParser.DeserializeLLSDNotation(uuidTwo);
Assert.That(llsdTwo.Type, Is.EqualTo(OSDType.UUID));
Assert.That(llsdTwo.AsString(), Is.EqualTo("00000000-0000-0000-0000-000000000000"));
}
[Test()]
public void SerializeUUID()
{
OSD llsdOne = OSD.FromUUID(new UUID("97f4aeca-88a1-42a1-b385-b97b18abb255"));
string sOne = OSDParser.SerializeLLSDNotation(llsdOne);
OSD llsdOneDS = OSDParser.DeserializeLLSDNotation(sOne);
Assert.That(llsdOneDS.Type, Is.EqualTo(OSDType.UUID));
Assert.That(llsdOneDS.AsString(), Is.EqualTo("97f4aeca-88a1-42a1-b385-b97b18abb255"));
OSD llsdTwo = OSD.FromUUID(new UUID("00000000-0000-0000-0000-000000000000"));
string sTwo = OSDParser.SerializeLLSDNotation(llsdTwo);
OSD llsdTwoDS = OSDParser.DeserializeLLSDNotation(sTwo);
Assert.That(llsdTwoDS.Type, Is.EqualTo(OSDType.UUID));
Assert.That(llsdTwoDS.AsString(), Is.EqualTo("00000000-0000-0000-0000-000000000000"));
}
public void DeserializeString()
{
string sOne = "''";
OSD llsdOne = OSDParser.DeserializeLLSDNotation(sOne);
Assert.That(llsdOne.Type, Is.EqualTo(OSDType.String));
Assert.That(llsdOne.AsString(), Is.Empty);
// This is double escaping. Once for the encoding, and once for csharp.
string sTwo = "'test\\'\"test'";
OSD llsdTwo = OSDParser.DeserializeLLSDNotation(sTwo);
Assert.That(llsdTwo.Type, Is.EqualTo(OSDType.String));
Assert.That(llsdTwo.AsString(), Is.EqualTo("test'\"test"));
// "test \\lest"
char[] cThree = { (char)0x27, (char)0x74, (char)0x65, (char)0x73, (char)0x74, (char)0x20, (char)0x5c,
(char)0x5c, (char)0x6c, (char)0x65, (char)0x73, (char)0x74, (char)0x27 };
string sThree = new string(cThree);
OSD llsdThree = OSDParser.DeserializeLLSDNotation(sThree);
Assert.That(llsdThree.Type, Is.EqualTo(OSDType.String));
Assert.That(llsdThree.AsString(), Is.EqualTo("test \\lest"));
string sFour = "'aa\t la'";
OSD llsdFour = OSDParser.DeserializeLLSDNotation(sFour);
Assert.That(llsdFour.Type, Is.EqualTo(OSDType.String));
Assert.That(llsdFour.AsString(), Is.EqualTo("aa\t la"));
char[] cFive = { (char)0x27, (char)0x5c, (char)0x5c, (char)0x27 };
string sFive = new string(cFive);
OSD llsdFive = OSDParser.DeserializeLLSDNotation(sFive);
Assert.That(llsdFive.Type, Is.EqualTo(OSDType.String));
Assert.That(llsdFive.AsString(), Is.EqualTo("\\"));
string sSix = "s(10)\"1234567890\"";
OSD llsdSix = OSDParser.DeserializeLLSDNotation(sSix);
Assert.That(llsdSix.Type, Is.EqualTo(OSDType.String));
Assert.That(llsdSix.AsString(), Is.EqualTo("1234567890"));
string sSeven = "s(5)\"\\\\\\\\\\\"";
OSD llsdSeven = OSDParser.DeserializeLLSDNotation(sSeven);
Assert.That(llsdSeven.Type, Is.EqualTo(OSDType.String));
Assert.That(llsdSeven.AsString(), Is.EqualTo(@"\\\\\"));
string sEight = "\"aouAOUhsdjklfghskldjfghqeiurtzwieortzaslxfjkgh\"";
OSD llsdEight = OSDParser.DeserializeLLSDNotation(sEight);
Assert.That(llsdEight.Type, Is.EqualTo(OSDType.String));
Assert.That(llsdEight.AsString(), Is.EqualTo("aouAOUhsdjklfghskldjfghqeiurtzwieortzaslxfjkgh"));
}
public void DoSomeStringSerializingActionsAndAsserts(string s)
{
OSD llsdOne = OSD.FromString(s);
string sOne = OSDParser.SerializeLLSDNotation(llsdOne);
OSD llsdOneDS = OSDParser.DeserializeLLSDNotation(sOne);
Assert.That(llsdOne.Type, Is.EqualTo(OSDType.String));
Assert.That(llsdOneDS.AsString(), Is.EqualTo(s));
}
[Test()]
public void SerializeString()
{
DoSomeStringSerializingActionsAndAsserts("");
DoSomeStringSerializingActionsAndAsserts("\\");
DoSomeStringSerializingActionsAndAsserts("\"\"");
DoSomeStringSerializingActionsAndAsserts("<22><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>-these-should-be-some-german-umlauts");
DoSomeStringSerializingActionsAndAsserts("\t\n\r");
DoSomeStringSerializingActionsAndAsserts("asdkjfhaksldjfhalskdjfhaklsjdfhaklsjdhjgzqeuiowrtzserghsldfg" +
"asdlkfhqeiortzsdkfjghslkdrjtzsoidklghuisoehiguhsierughaishdl" +
"asdfkjhueiorthsgsdkfughaslkdfjshldkfjghsldkjghsldkfghsdklghs" +
"wopeighisdjfghklasdfjghsdklfgjhsdklfgjshdlfkgjshdlfkgjshdlfk");
DoSomeStringSerializingActionsAndAsserts("all is N\"\\'othing and n'oting is all");
DoSomeStringSerializingActionsAndAsserts("very\"british is this.");
// We test here also for 4byte characters
string xml = "<x>&#x10137;</x>";
byte[] bytes = Encoding.UTF8.GetBytes(xml);
XmlTextReader xtr = new XmlTextReader(new MemoryStream(bytes, false));
xtr.Read();
xtr.Read();
string content = xtr.ReadString();
DoSomeStringSerializingActionsAndAsserts(content);
}
[Test()]
public void DeserializeURI()
{
string sUriOne = "l\"http://test.com/test test>\\\"/&yes\"";
OSD llsdOne = OSDParser.DeserializeLLSDNotation(sUriOne);
Assert.That(llsdOne.Type, Is.EqualTo(OSDType.URI));
Assert.That(llsdOne.AsString(), Is.EqualTo("http://test.com/test%20test%3E%22/&yes"));
string sUriTwo = "l\"test/test/test?test=1&toast=2\"";
OSD llsdTwo = OSDParser.DeserializeLLSDNotation(sUriTwo);
Assert.That(llsdTwo.Type, Is.EqualTo(OSDType.URI));
Assert.That(llsdTwo.AsString(), Is.EqualTo("test/test/test?test=1&toast=2"));
}
[Test()]
public void SerializeURI()
{
Uri uriOne = new Uri("http://test.org/test test>\\\"/&yes\"", UriKind.RelativeOrAbsolute);
OSD llsdOne = OSD.FromUri(uriOne);
string sUriOne = OSDParser.SerializeLLSDNotation(llsdOne);
OSD llsdOneDS = OSDParser.DeserializeLLSDNotation(sUriOne);
Assert.That(llsdOneDS.Type, Is.EqualTo(OSDType.URI));
Assert.That(llsdOneDS.AsUri(), Is.EqualTo(uriOne));
Uri uriTwo = new Uri("test/test/near/the/end?test=1", UriKind.RelativeOrAbsolute);
OSD llsdTwo = OSD.FromUri(uriTwo);
string sUriTwo = OSDParser.SerializeLLSDNotation(llsdTwo);
OSD llsdTwoDS = OSDParser.DeserializeLLSDNotation(sUriTwo);
Assert.That(llsdTwoDS.Type, Is.EqualTo(OSDType.URI));
Assert.That(llsdTwoDS.AsUri(), Is.EqualTo(uriTwo));
}
[Test()]
public void DeserializeDate()
{
string sDateOne = "d\"2007-12-31T20:49:10Z\"";
OSD llsdOne = OSDParser.DeserializeLLSDNotation(sDateOne);
Assert.That(llsdOne.Type, Is.EqualTo(OSDType.Date));
DateTime dt = new DateTime(2007, 12, 31, 20, 49, 10, 0, DateTimeKind.Utc);
DateTime dtDS = llsdOne.AsDate();
Assert.That(dtDS.ToUniversalTime(), Is.EqualTo(dt));
}
[Test()]
public void SerializeDate()
{
DateTime dtOne = new DateTime(2005, 8, 10, 11, 23, 4, DateTimeKind.Utc);
OSD llsdOne = OSD.FromDate(dtOne);
string sDtOne = OSDParser.SerializeLLSDNotation(llsdOne);
OSD llsdOneDS = OSDParser.DeserializeLLSDNotation(sDtOne);
Assert.That(llsdOneDS.Type, Is.EqualTo(OSDType.Date));
DateTime dtOneDS = llsdOneDS.AsDate();
Assert.That(dtOneDS.ToUniversalTime(), Is.EqualTo(dtOne));
DateTime dtTwo = new DateTime(2010, 10, 11, 23, 00, 10, 100, DateTimeKind.Utc);
OSD llsdTwo = OSD.FromDate(dtTwo);
string sDtTwo = OSDParser.SerializeLLSDNotation(llsdTwo);
OSD llsdTwoDS = OSDParser.DeserializeLLSDNotation(sDtTwo);
Assert.That(llsdTwoDS.Type, Is.EqualTo(OSDType.Date));
DateTime dtTwoDS = llsdTwoDS.AsDate();
Assert.That(dtTwoDS.ToUniversalTime(), Is.EqualTo(dtTwo));
// check if a *local* time can be serialized and deserialized
DateTime dtThree = new DateTime(2009, 12, 30, 8, 25, 10, DateTimeKind.Local);
OSD llsdDateThree = OSD.FromDate(dtThree);
string sDateThreeSerialized = OSDParser.SerializeLLSDNotation(llsdDateThree);
OSD llsdDateThreeDS = OSDParser.DeserializeLLSDNotation(sDateThreeSerialized);
Assert.That(llsdDateThreeDS.Type, Is.EqualTo(OSDType.Date));
Assert.That(llsdDateThreeDS.AsDate(), Is.EqualTo(dtThree));
}
[Test()]
public void SerializeBinary()
{
byte[] binary = { 0x0, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0b,
0x0b, 0x0c, 0x0d, 0x0e, 0x0f };
OSD llsdBinary = OSD.FromBinary(binary);
string sBinarySerialized = OSDParser.SerializeLLSDNotation(llsdBinary);
OSD llsdBinaryDS = OSDParser.DeserializeLLSDNotation(sBinarySerialized);
Assert.That(llsdBinaryDS.Type, Is.EqualTo(OSDType.Binary));
Assert.That(llsdBinaryDS.AsBinary(), Is.EqualTo(binary));
}
[Test()]
public void DeserializeArray()
{
string sArrayOne = "[]";
OSDArray llsdArrayOne = (OSDArray)OSDParser.DeserializeLLSDNotation(sArrayOne);
Assert.That(llsdArrayOne.Type, Is.EqualTo(OSDType.Array));
Assert.That(llsdArrayOne.Count, Is.EqualTo(0));
string sArrayTwo = "[ i0 ]";
OSDArray llsdArrayTwo = (OSDArray)OSDParser.DeserializeLLSDNotation(sArrayTwo);
Assert.That(llsdArrayTwo.Type, Is.EqualTo(OSDType.Array));
Assert.That(llsdArrayTwo.Count, Is.EqualTo(1));
OSDInteger llsdIntOne = (OSDInteger)llsdArrayTwo[0];
Assert.That(llsdIntOne.Type, Is.EqualTo(OSDType.Integer));
Assert.That(llsdIntOne.AsInteger(), Is.EqualTo(0));
string sArrayThree = "[ i0, i1 ]";
OSDArray llsdArrayThree = (OSDArray)OSDParser.DeserializeLLSDNotation(sArrayThree);
Assert.That(llsdArrayThree.Type, Is.EqualTo(OSDType.Array));
Assert.That(llsdArrayThree.Count, Is.EqualTo(2));
OSDInteger llsdIntTwo = (OSDInteger)llsdArrayThree[0];
Assert.That(llsdIntTwo.Type, Is.EqualTo(OSDType.Integer));
Assert.That(llsdIntTwo.AsInteger(), Is.EqualTo(0));
OSDInteger llsdIntThree = (OSDInteger)llsdArrayThree[1];
Assert.That(llsdIntThree.Type, Is.EqualTo(OSDType.Integer));
Assert.That(llsdIntThree.AsInteger(), Is.EqualTo(1));
string sArrayFour = " [ \"testtest\", \"aha\",t,f,i1, r1.2, [ i1] ] ";
OSDArray llsdArrayFour = (OSDArray)OSDParser.DeserializeLLSDNotation(sArrayFour);
Assert.That(llsdArrayFour.Type, Is.EqualTo(OSDType.Array));
Assert.That(llsdArrayFour.Count, Is.EqualTo(7));
Assert.That(llsdArrayFour[0].AsString(), Is.EqualTo("testtest"));
Assert.That(llsdArrayFour[1].AsString(), Is.EqualTo("aha"));
Assert.That(llsdArrayFour[2].AsBoolean(), Is.EqualTo(true));
Assert.That(llsdArrayFour[3].AsBoolean(), Is.EqualTo(false));
Assert.That(llsdArrayFour[4].AsInteger(), Is.EqualTo(1));
Assert.That(llsdArrayFour[5].AsReal(), Is.EqualTo(1.2d));
Assert.That(llsdArrayFour[6].Type, Is.EqualTo(OSDType.Array));
OSDArray llsdArrayFive = (OSDArray)llsdArrayFour[6];
Assert.That(llsdArrayFive[0].AsInteger(), Is.EqualTo(1));
}
[Test()]
public void SerializeArray()
{
OSDArray llsdOne = new OSDArray();
string sOne = OSDParser.SerializeLLSDNotation(llsdOne);
OSDArray llsdOneDS = (OSDArray)OSDParser.DeserializeLLSDNotation(sOne);
Assert.That(llsdOneDS.Type, Is.EqualTo(OSDType.Array));
Assert.That(llsdOneDS.Count, Is.EqualTo(0));
OSD llsdTwo = OSD.FromInteger(123234);
OSD llsdThree = OSD.FromString("asedkfjhaqweiurohzasdf");
OSDArray llsdFour = new OSDArray();
llsdFour.Add(llsdTwo);
llsdFour.Add(llsdThree);
llsdOne.Add(llsdTwo);
llsdOne.Add(llsdThree);
llsdOne.Add(llsdFour);
string sFive = OSDParser.SerializeLLSDNotation(llsdOne);
OSDArray llsdFive = (OSDArray)OSDParser.DeserializeLLSDNotation(sFive);
Assert.That(llsdFive.Type, Is.EqualTo(OSDType.Array));
Assert.That(llsdFive.Count, Is.EqualTo(3));
Assert.That(llsdFive[0].Type, Is.EqualTo(OSDType.Integer));
Assert.That(llsdFive[0].AsInteger(), Is.EqualTo(123234));
Assert.That(llsdFive[1].Type, Is.EqualTo(OSDType.String));
Assert.That(llsdFive[1].AsString(), Is.EqualTo("asedkfjhaqweiurohzasdf"));
OSDArray llsdSix = (OSDArray)llsdFive[2];
Assert.That(llsdSix.Type, Is.EqualTo(OSDType.Array));
Assert.That(llsdSix.Count, Is.EqualTo(2));
Assert.That(llsdSix[0].Type, Is.EqualTo(OSDType.Integer));
Assert.That(llsdSix[0].AsInteger(), Is.EqualTo(123234));
Assert.That(llsdSix[1].Type, Is.EqualTo(OSDType.String));
Assert.That(llsdSix[1].AsString(), Is.EqualTo("asedkfjhaqweiurohzasdf"));
}
[Test()]
public void DeserializeMap()
{
string sMapOne = " { } ";
OSDMap llsdMapOne = (OSDMap)OSDParser.DeserializeLLSDNotation(sMapOne);
Assert.That(llsdMapOne.Type, Is.EqualTo(OSDType.Map));
Assert.That(llsdMapOne.Count, Is.EqualTo(0));
string sMapTwo = " { \"test\":i2 } ";
OSDMap llsdMapTwo = (OSDMap)OSDParser.DeserializeLLSDNotation(sMapTwo);
Assert.That(llsdMapTwo.Type, Is.EqualTo(OSDType.Map));
Assert.That(llsdMapTwo.Count, Is.EqualTo(1));
Assert.That(llsdMapTwo["test"].Type, Is.EqualTo(OSDType.Integer));
Assert.That(llsdMapTwo["test"].AsInteger(), Is.EqualTo(2));
string sMapThree = " { 'test':\"testtesttest\", 'aha':\"muahahaha\" , \"anywhere\":! } ";
OSDMap llsdMapThree = (OSDMap)OSDParser.DeserializeLLSDNotation(sMapThree);
Assert.That(llsdMapThree.Type, Is.EqualTo(OSDType.Map));
Assert.That(llsdMapThree.Count, Is.EqualTo(3));
Assert.That(llsdMapThree["test"].Type, Is.EqualTo(OSDType.String));
Assert.That(llsdMapThree["test"].AsString(), Is.EqualTo("testtesttest"));
Assert.That(llsdMapThree["test"].Type, Is.EqualTo(OSDType.String));
Assert.That(llsdMapThree["aha"].AsString(), Is.EqualTo("muahahaha"));
Assert.That(llsdMapThree["self"].Type, Is.EqualTo(OSDType.Unknown));
string sMapFour = " { 'test' : { 'test' : i1, 't0st' : r2.5 }, 'tist' : \"hello world!\", 'tast' : \"last\" } ";
OSDMap llsdMapFour = (OSDMap)OSDParser.DeserializeLLSDNotation(sMapFour);
Assert.That(llsdMapFour.Type, Is.EqualTo(OSDType.Map));
Assert.That(llsdMapFour.Count, Is.EqualTo(3));
Assert.That(llsdMapFour["tist"].AsString(), Is.EqualTo("hello world!"));
Assert.That(llsdMapFour["tast"].AsString(), Is.EqualTo("last"));
OSDMap llsdMapFive = (OSDMap)llsdMapFour["test"];
Assert.That(llsdMapFive.Type, Is.EqualTo(OSDType.Map));
Assert.That(llsdMapFive.Count, Is.EqualTo(2));
Assert.That(llsdMapFive["test"].Type, Is.EqualTo(OSDType.Integer));
Assert.That(llsdMapFive["test"].AsInteger(), Is.EqualTo(1));
Assert.That(llsdMapFive["t0st"].Type, Is.EqualTo(OSDType.Real));
Assert.That(llsdMapFive["t0st"].AsReal(), Is.EqualTo(2.5d));
}
[Test()]
public void SerializeMap()
{
OSDMap llsdOne = new OSDMap();
string sOne = OSDParser.SerializeLLSDNotation(llsdOne);
OSDMap llsdOneDS = (OSDMap)OSDParser.DeserializeLLSDNotation(sOne);
Assert.That(llsdOneDS.Type, Is.EqualTo(OSDType.Map));
Assert.That(llsdOneDS.Count, Is.EqualTo(0));
OSD llsdTwo = OSD.FromInteger(123234);
OSD llsdThree = OSD.FromString("asedkfjhaqweiurohzasdf");
OSDMap llsdFour = new OSDMap();
llsdFour["test0"] = llsdTwo;
llsdFour["test1"] = llsdThree;
llsdOne["test0"] = llsdTwo;
llsdOne["test1"] = llsdThree;
llsdOne["test2"] = llsdFour;
string sFive = OSDParser.SerializeLLSDNotation(llsdOne);
OSDMap llsdFive = (OSDMap)OSDParser.DeserializeLLSDNotation(sFive);
Assert.That(llsdFive.Type, Is.EqualTo(OSDType.Map));
Assert.That(llsdFive.Count, Is.EqualTo(3));
Assert.That(llsdFive["test0"].Type, Is.EqualTo(OSDType.Integer));
Assert.That(llsdFive["test0"].AsInteger(), Is.EqualTo(123234));
Assert.That(llsdFive["test1"].Type, Is.EqualTo(OSDType.String));
Assert.That(llsdFive["test1"].AsString(), Is.EqualTo("asedkfjhaqweiurohzasdf"));
OSDMap llsdSix = (OSDMap)llsdFive["test2"];
Assert.That(llsdSix.Type, Is.EqualTo(OSDType.Map));
Assert.That(llsdSix.Count, Is.EqualTo(2));
Assert.That(llsdSix["test0"].Type, Is.EqualTo(OSDType.Integer));
Assert.That(llsdSix["test0"].AsInteger(), Is.EqualTo(123234));
Assert.That(llsdSix["test1"].Type, Is.EqualTo(OSDType.String));
Assert.That(llsdSix["test1"].AsString(), Is.EqualTo("asedkfjhaqweiurohzasdf"));
// We test here also for 4byte characters as map keys
string xml = "<x>&#x10137;</x>";
byte[] bytes = Encoding.UTF8.GetBytes(xml);
XmlTextReader xtr = new XmlTextReader(new MemoryStream(bytes, false));
xtr.Read();
xtr.Read();
string content = xtr.ReadString();
OSDMap llsdSeven = new OSDMap();
llsdSeven[content] = OSD.FromString(content);
string sSeven = OSDParser.SerializeLLSDNotation(llsdSeven);
OSDMap llsdSevenDS = (OSDMap)OSDParser.DeserializeLLSDNotation(sSeven);
Assert.That(llsdSevenDS.Type, Is.EqualTo(OSDType.Map));
Assert.That(llsdSevenDS.Count, Is.EqualTo(1));
Assert.That(llsdSevenDS[content].AsString(), Is.EqualTo(content));
}
[Test()]
public void DeserializeRealWorldExamples()
{
string realWorldExample = @"
[
{'destination':'http://secondlife.com'},
{'version':i1},
{
'agent_id':u3c115e51-04f4-523c-9fa6-98aff1034730,
'session_id':u2c585cec-038c-40b0-b42e-a25ebab4d132,
'circuit_code':i1075,
'first_name':'Phoenix',
'last_name':'Linden',
'position':[r70.9247,r254.378,r38.7304],
'look_at':[r-0.043753,r-0.999042,r0],
'granters':[ua2e76fcd-9360-4f6d-a924-000000000003],
'attachment_data':
[
{
'attachment_point':i2,
'item_id':ud6852c11-a74e-309a-0462-50533f1ef9b3,
'asset_id':uc69b29b1-8944-58ae-a7c5-2ca7b23e22fb
},
{
'attachment_point':i10,
'item_id':uff852c22-a74e-309a-0462-50533f1ef900,
'asset_id':u5868dd20-c25a-47bd-8b4c-dedc99ef9479
}
]
}
]";
// We dont do full testing here. We are fine if a few values are right
// and the parser doesnt throw an exception
OSDArray llsdArray = (OSDArray)OSDParser.DeserializeLLSDNotation(realWorldExample);
Assert.That(llsdArray.Type, Is.EqualTo(OSDType.Array));
Assert.That(llsdArray.Count, Is.EqualTo(3));
OSDMap llsdMapOne = (OSDMap)llsdArray[0];
Assert.That(llsdMapOne.Type, Is.EqualTo(OSDType.Map));
Assert.That(llsdMapOne["destination"].AsString(), Is.EqualTo("http://secondlife.com"));
OSDMap llsdMapTwo = (OSDMap)llsdArray[1];
Assert.That(llsdMapTwo.Type, Is.EqualTo(OSDType.Map));
Assert.That(llsdMapTwo["version"].Type, Is.EqualTo(OSDType.Integer));
Assert.That(llsdMapTwo["version"].AsInteger(), Is.EqualTo(1));
OSDMap llsdMapThree = (OSDMap)llsdArray[2];
Assert.That(llsdMapThree["session_id"].Type, Is.EqualTo(OSDType.UUID));
Assert.That(llsdMapThree["session_id"].AsString(), Is.EqualTo("2c585cec-038c-40b0-b42e-a25ebab4d132"));
Assert.That(llsdMapThree["agent_id"].Type, Is.EqualTo(OSDType.UUID));
Assert.That(llsdMapThree["agent_id"].AsString(), Is.EqualTo("3c115e51-04f4-523c-9fa6-98aff1034730"));
}
[Test()]
public void SerializeFormattedTest()
{
// This is not a real test. Instead look at the console.out tab for how formatted notation looks like.
OSDArray llsdArray = new OSDArray();
OSD llsdOne = OSD.FromInteger(1);
OSD llsdTwo = OSD.FromInteger(1);
llsdArray.Add(llsdOne);
llsdArray.Add(llsdTwo);
OSDMap llsdMap = new OSDMap();
OSD llsdThree = OSD.FromInteger(2);
llsdMap["test1"] = llsdThree;
OSD llsdFour = OSD.FromInteger(2);
llsdMap["test2"] = llsdFour;
llsdArray.Add(llsdMap);
OSDArray llsdArrayTwo = new OSDArray();
OSD llsdFive = OSD.FromString("asdflkhjasdhj");
OSD llsdSix = OSD.FromString("asdkfhasjkldfghsd");
llsdArrayTwo.Add(llsdFive);
llsdArrayTwo.Add(llsdSix);
llsdMap["test3"] = llsdArrayTwo;
string sThree = OSDParser.SerializeLLSDNotationFormatted(llsdArray);
// we also try to parse this... and look a little at the results
OSDArray llsdSeven = (OSDArray)OSDParser.DeserializeLLSDNotation(sThree);
Assert.That(llsdSeven.Type, Is.EqualTo(OSDType.Array));
Assert.That(llsdSeven.Count, Is.EqualTo(3));
Assert.That(llsdSeven[0].Type, Is.EqualTo(OSDType.Integer));
Assert.That(llsdSeven[0].AsInteger(), Is.EqualTo(1));
Assert.That(llsdSeven[1].Type, Is.EqualTo(OSDType.Integer));
Assert.That(llsdSeven[1].AsInteger(), Is.EqualTo(1));
Assert.That(llsdSeven[2].Type, Is.EqualTo(OSDType.Map));
// thats enough for now.
}
}
}