/// <summary> /// { null: null } /// </summary> public void WriteSingleNull(Stream stream) { var root = new BSONDocument(); root.Set(new BSONNullElement("null")); root.WriteAsBSON(stream); }
/// <summary> /// { greetings: "Hello World!" } /// </summary> public void WriteSingleString(Stream stream) { var root = new BSONDocument(); root.Set(new BSONStringElement("greetings", "Hello World!")); root.WriteAsBSON(stream); }
/// <summary> /// { lucky: 7 } /// </summary> public void WriteSingleInt32(Stream stream) { var root = new BSONDocument(); root.Set(new BSONInt32Element("lucky", 7)); root.WriteAsBSON(stream); }
/// <summary> /// { maxkey: <maxkey> } /// </summary> public void WriteSingleMaxKey(Stream stream) { var root = new BSONDocument(); root.Set(new BSONMaxKeyElement("maxkey")); root.WriteAsBSON(stream); }
/// <summary> /// { solarSystemDiameter: 10000000000000 } /// </summary> public void WriteSingleInt64(Stream stream) { var root = new BSONDocument(); root.Set(new BSONInt64Element("solarSystemDiameter", 10000000000000)); root.WriteAsBSON(stream); }
/// <summary> /// { pi: 3.14159265358979 } /// </summary> public void WriteSingleDouble(Stream stream) { var root = new BSONDocument(); root.Set(new BSONDoubleElement("pi", Math.PI)); root.WriteAsBSON(stream); }
/// <summary> /// { booleanFalse: false } /// </summary> public void WriteSingleBooleanFalse(Stream stream) { var root = new BSONDocument(); root.Set(new BSONBooleanElement("booleanFalse", false)); root.WriteAsBSON(stream); }
/// <summary> /// { name: "Gagarin", birth: 1934 } /// </summary> public void WriteStringAndInt32Pair(Stream stream) { var root = new BSONDocument(); root.Set(new BSONStringElement("name", "Gagarin")); root.Set(new BSONInt32Element("birth", 1934)); root.WriteAsBSON(stream); }
/// <summary> /// { name: "Gagarin", birth: 1934 } /// </summary> public void WriteInt32Pair(Stream stream) { var root = new BSONDocument(); root.Set(new BSONInt32Element("name", 404)); root.Set(new BSONInt32Element("birth", 1934)); root.WriteAsBSON(stream); }
/// <summary> /// Array of strings /// { 'fruits': ['apple, 'orange', 'plum'] } --> { 'fruits': { '0': 'apple', '1': 'orange', '2': 'plum' } } /// </summary> public void WriteStringArray(Stream stream) { var root = new BSONDocument(); var array = new[] { new BSONStringElement("apple"), new BSONStringElement("orange"), new BSONStringElement("plum") }; root.Set(new BSONArrayElement("fruits", array)); root.WriteAsBSON(stream); }
/// <summary> /// Array of int32 /// { 'years': [1963, 1984, 2015] } --> { 'years': { '0': 1963, '1': 1984, '2': 2015 } } /// </summary> public void WriteInt32Array(Stream stream) { var root = new BSONDocument(); var array = new[] { new BSONInt32Element(1963), new BSONInt32Element(1984), new BSONInt32Element(2015) }; root.Set(new BSONArrayElement("years", array)); root.WriteAsBSON(stream); }
/// <summary> /// { now: <DateTime from 635000000000000000 ticks> } /// </summary> public void WriteSingleDateTime(Stream stream) { var now = new DateTime(635000000000000000, DateTimeKind.Utc); var root = new BSONDocument(); root.Set(new BSONDateTimeElement("now", now)); root.WriteAsBSON(stream); }
/// <summary> /// Array of strings /// { 'stuff': ['apple, 3, 2.14] } --> { 'stuff': { '0': 'apple', '1': 3, '2': 2.14 } } /// </summary> public void WriteStringInt32DoubleMixedArray(Stream stream) { var root = new BSONDocument(); var array = new BSONElement[] { new BSONStringElement("apple"), new BSONInt32Element(3), new BSONDoubleElement(2.14D) }; root.Set(new BSONArrayElement("stuff", array)); root.WriteAsBSON(stream); }
/// <summary> /// { code: "function(){var x=1;var y='abc';return 1;};" } /// </summary> public void WriteSingleJavaScript(Stream stream) { var root = new BSONDocument(); var code = "function(){var x=1;var y='abc';return 1;};"; root.Set(new BSONJavaScriptElement("code", code)); root.WriteAsBSON(stream); }
/// <summary> /// { nested: { capital: "Moscow" } } /// </summary> public void WriteNestedDocument(Stream stream) { var root = new BSONDocument(); var nested = new BSONDocument(); nested.Set(new BSONStringElement("capital", "Moscow")); root.Set(new BSONDocumentElement("nested", nested)); root.WriteAsBSON(stream); }
/// <summary> /// { binary: <bytes from 'This is binary data'> } /// </summary> public void WriteSingleBinaryData(Stream stream) { var data = Encoding.UTF8.GetBytes("This is binary data"); var root = new BSONDocument(); var binary = new BSONBinary(BSONBinaryType.BinaryOld, data); root.Set(new BSONBinaryElement("binary", binary)); root.WriteAsBSON(stream); }
public void WriteStringsWithDifferentLengths(Stream stream, int count) { stream.Position = 0; var root = new BSONDocument(); var value = new string('a', count); root.Set(new BSONStringElement("vary", value)); root.WriteAsBSON(stream); }
/// <summary> /// { stamp: <seconds since Unix epoch to DateTime from 635000000000000000 ticks with 123 increment> } /// </summary> public void WriteSingleTimestamp(Stream stream) { var root = new BSONDocument(); var now = new DateTime(635000000000000000, DateTimeKind.Utc); var stamp = new BSONTimestamp(now, 123); root.Set(new BSONTimestampElement("stamp", stamp)); root.WriteAsBSON(stream); }
/// <summary> /// { email: <pattern='^[-.\w]+@(?:[a-z\d]{2,}\.)+[a-z]{2,6}$' options=I,M,U> } /// </summary> public void WriteSingleRegularExpression(Stream stream) { var root = new BSONDocument(); var pattern = @"^[-.\w]+@(?:[a-z\d]{2,}\.)+[a-z]{2,6}$"; var options = BSONRegularExpressionOptions.I | BSONRegularExpressionOptions.M | BSONRegularExpressionOptions.U; var regex = new BSONRegularExpression(pattern, options); root.Set(new BSONRegularExpressionElement("email", regex)); root.WriteAsBSON(stream); }
public void WriteBigIntegers(Stream stream) { stream.Position = 0; var root = new BSONDocument(); root.Set(new BSONInt32Element("intMin", int.MinValue)); root.Set(new BSONInt32Element("intMax", int.MaxValue)); root.Set(new BSONInt64Element("longMin", long.MinValue)); root.Set(new BSONInt64Element("longMax", long.MaxValue)); root.WriteAsBSON(stream); }
/// <summary> /// { codeWithScope: "function(){var x=1;var y='abc';return z;}; <with scope: z=23>" } /// </summary> public void WriteSingleJavaScriptWithScope(Stream stream) { var root = new BSONDocument(); var code = "function(){var x=1;var y='abc';return z;};"; var scope = new BSONDocument(); scope.Set(new BSONInt32Element("z", 23)); var jsWithScope = new BSONCodeWithScope(code, scope); root.Set(new BSONJavaScriptWithScopeElement("codeWithScope", jsWithScope)); root.WriteAsBSON(stream); }
private BSONDocument fullCopy(BSONDocument original) { //note: doc.DeepClone does not work as expected (copies references for byte[]) using (var ms = new MemoryStream()) { #pragma warning disable 0618 original.WriteAsBSON(ms); ms.Position = 0; return(new BSONDocument(ms)); #pragma warning restore 0618 } }
/// <summary> /// { objectId: <bytes from hex '507f1f77bcf86cd799439011'> } /// </summary> public void WriteSingleObjectId(Stream stream) { var hex = "507f1f77bcf86cd799439011"; var data = Enumerable.Range(0, hex.Length) .Where(x => x % 2 == 0) .Select(x => Convert.ToByte(hex.Substring(x, 2), 16)) .ToArray(); var root = new BSONDocument(); var objectId = new BSONObjectID(data); root.Set(new BSONObjectIDElement("objectId", objectId)); root.WriteAsBSON(stream); }
private void button1_Click(object sender, EventArgs e) { var toolTip = new ToolTip(); toolTip.SetToolTip(sender as Button, "{} (empty document)"); using (var stream = File.Create("test.bson")) { var root = new BSONDocument(); root.WriteAsBSON(stream); } Process.Start(AppDomain.CurrentDomain.BaseDirectory); }
private void button3_Click(object sender, EventArgs e) { var toolTip = new ToolTip(); toolTip.SetToolTip(sender as Button, "{ pi: 3.14159265358979 }"); using (var stream = File.Create("test.bson")) { var root = new BSONDocument(); root.Set(new BSONStringElement("greetings", "Hello World!")); root.WriteAsBSON(stream); } Process.Start(AppDomain.CurrentDomain.BaseDirectory); }
private void button2_Click(object sender, EventArgs e) { var toolTip = new ToolTip(); toolTip.SetToolTip(sender as Button, "{ greetings: \"Hello World!\" }"); using (var stream = File.Create("test.bson")) { var root = new BSONDocument(); root.Set(new BSONDoubleElement("pi", Math.PI)); root.WriteAsBSON(stream); } Process.Start(AppDomain.CurrentDomain.BaseDirectory); }
private void button4_Click(object sender, EventArgs e) { var toolTip = new ToolTip(); toolTip.SetToolTip(sender as Button, "{ name: \"Gagarin\", birth: 1934 }"); using (var stream = File.Create("test.bson")) { var root = new BSONDocument(); root.Set(new BSONStringElement("name", "Gagarin")); root.Set(new BSONInt32Element("birth", 1934)); root.WriteAsBSON(stream); } Process.Start(AppDomain.CurrentDomain.BaseDirectory); }
public void WriteUnicodeStrings(Stream stream) { stream.Position = 0; var root = new BSONDocument(); root.Set(new BSONStringElement("eng", "hello")); root.Set(new BSONStringElement("rus", "привет")); root.Set(new BSONStringElement("chi", "你好")); root.Set(new BSONStringElement("jap", "こんにちは")); root.Set(new BSONStringElement("gre", "γεια σας")); root.Set(new BSONStringElement("alb", "përshëndetje")); root.Set(new BSONStringElement("arm", "բարեւ Ձեզ")); root.Set(new BSONStringElement("vie", "xin chào")); root.Set(new BSONStringElement("por", "Olá")); root.Set(new BSONStringElement("ukr", "Привіт")); root.Set(new BSONStringElement("ger", "wünsche")); root.WriteAsBSON(stream); }
public static Int32 Write_QUERY(Stream stream, Int32 requestID, Database db, Collection collection, //may be null for $CMD QueryFlags flags, Int32 numberToSkip, Int32 numberToReturn, BSONDocument query, BSONDocument selector //may be null ) { stream.Position = STD_HDR_LEN; //skip the header BinUtils.WriteInt32(stream, (Int32)flags); //if collection==null then query the $CMD collection var fullNameBuffer = collection != null ? collection.m_FullNameCStringBuffer : db.m_CMD_NameCStringBuffer; stream.Write(fullNameBuffer, 0, fullNameBuffer.Length); BinUtils.WriteInt32(stream, numberToSkip); BinUtils.WriteInt32(stream, numberToReturn); query.WriteAsBSON(stream); if (selector != null) { selector.WriteAsBSON(stream); } var total = (Int32)stream.Position; stream.Position = 0; writeStandardHeader(stream, total, requestID, 0, OP_QUERY); return(total); }
public static Int32 Write_QUERY(Stream stream, Int32 requestID, Database db, Collection collection, //may be null for $CMD QueryFlags flags, Int32 numberToSkip, Int32 numberToReturn, BSONDocument query, BSONDocument selector//may be null ) { stream.Position = STD_HDR_LEN;//skip the header BinUtils.WriteInt32(stream, (Int32)flags); //if collection==null then query the $CMD collection var fullNameBuffer = collection!=null ? collection.m_FullNameCStringBuffer : db.m_CMD_NameCStringBuffer; stream.Write(fullNameBuffer, 0, fullNameBuffer.Length); BinUtils.WriteInt32(stream, numberToSkip); BinUtils.WriteInt32(stream, numberToReturn); query.WriteAsBSON(stream); if (selector!=null) selector.WriteAsBSON(stream); var total = (Int32)stream.Position; stream.Position = 0; writeStandardHeader(stream, total, requestID, 0, OP_QUERY); return total; }
public void WriteAndReadSingleDateTime() { using (var stream = new MemoryStream()) { var now = new DateTime(2010, 10, 12, 11, 20, 12, DateTimeKind.Local); var root = new BSONDocument(); root.Set(new BSONDateTimeElement("mydate", now)); root.WriteAsBSON(stream); Assert.AreEqual(stream.Position, 21); // ensure document length is 21 bytes stream.Seek(0, SeekOrigin.Begin); var root2 = new BSONDocument(stream); Assert.AreEqual(stream.Position, 21); // ensure whole document read Assert.AreEqual(1, root2.Count); // ensure whole document read Assert.AreEqual(now.ToUniversalTime(), ((BSONDateTimeElement)root2["mydate"]).Value); } }
public void WriteEmptyDocument() { using (var stream = new MemoryStream()) using (var reader = new BinaryReader(stream)) { var root = new BSONDocument(); root.WriteAsBSON(stream); Assert.AreEqual(stream.Position, 5); // ensure document length is 5 bytes stream.Seek(0, SeekOrigin.Begin); CollectionAssert.AreEqual(reader.ReadBytes(sizeof(int)), BitConverter.GetBytes(5)); // ensure content length is 1 Assert.AreEqual(reader.ReadByte(), (byte)0x00); // ensure last byte is terminator 0x00 Assert.AreEqual(stream.Position, 5); // ensure whole document readed } }
public void WriteNestedDocument() { using (var stream = new MemoryStream()) using (var reader = new BinaryReader(stream)) { var root = new BSONDocument(); var nested = new BSONDocument(); nested.Set(new BSONStringElement("capital", "Moscow")); root.Set(new BSONDocumentElement("nested", nested)); root.WriteAsBSON(stream); Assert.AreEqual(stream.Position, 38); // ensure document length is 38 bytes stream.Seek(0, SeekOrigin.Begin); CollectionAssert.AreEqual(reader.ReadBytes(sizeof(int)), BitConverter.GetBytes(38)); // content length is 38 Assert.AreEqual(reader.ReadByte(), (byte)BSONElementType.Document); // element type is document 0x03 CollectionAssert.AreEqual(reader.ReadBytes(6), Encoding.UTF8.GetBytes("nested")); // element name is 'nested' Assert.AreEqual(reader.ReadByte(), (byte)0x00); // string name terminator 0x00 is present CollectionAssert.AreEqual(reader.ReadBytes(4), BitConverter.GetBytes(25)); // nested document length is 25 Assert.AreEqual(reader.ReadByte(), (byte)BSONElementType.String); // element type is string 0x02 CollectionAssert.AreEqual(reader.ReadBytes(7), Encoding.UTF8.GetBytes("capital")); // element name is 'capital' Assert.AreEqual(reader.ReadByte(), (byte)0x00); // string name terminator 0x00 is present CollectionAssert.AreEqual(reader.ReadBytes(4), BitConverter.GetBytes(7)); // string length is 7 CollectionAssert.AreEqual(reader.ReadBytes(6), Encoding.UTF8.GetBytes("Moscow")); // element value is 'Moscow' Assert.AreEqual(reader.ReadByte(), (byte)0x00); // last byte is terminator 0x00 Assert.AreEqual(reader.ReadByte(), (byte)0x00); // last byte is terminator 0x00 Assert.AreEqual(reader.ReadByte(), (byte)0x00); // last byte is terminator 0x00 Assert.AreEqual(stream.Position, 38); // ensure whole document readed } }
public void WriteReadStringsWithDifferentLengths() { Parallel.For(0, 10*1024, i => { using (var stream = new MemoryStream()) using (var reader = new BinaryReader(stream)) { // Write var root = new BSONDocument(); var value = new string('a', i); root.Set(new BSONStringElement("vary", value)); root.WriteAsBSON(stream); Assert.AreEqual(stream.Position, 16 + i); // ensure document length is 16+i bytes stream.Seek(0, SeekOrigin.Begin); CollectionAssert.AreEqual(reader.ReadBytes(4), BitConverter.GetBytes(16 + i)); // content length is 16+i Assert.AreEqual(reader.ReadByte(), (byte) BSONElementType.String); // element type is string 0x02 CollectionAssert.AreEqual(reader.ReadBytes(4), Encoding.UTF8.GetBytes("vary")); // element name is 'vary' Assert.AreEqual(reader.ReadByte(), (byte) 0x00); // string name terminator 0x00 is present CollectionAssert.AreEqual(reader.ReadBytes(4), BitConverter.GetBytes(i + 1)); // string content length is 13 CollectionAssert.AreEqual(reader.ReadBytes(i), Encoding.UTF8.GetBytes(value)); // element value is value Assert.AreEqual(reader.ReadByte(), (byte) 0x00); // string value terminator 0x00 is present Assert.AreEqual(reader.ReadByte(), (byte) 0x00); // last byte is terminator 0x00 Assert.AreEqual(stream.Position, 16 + i); // ensure whole document readed stream.Position = 0; // Read var deser = new BSONDocument(stream); Assert.AreEqual(deser.ByteSize, 16 + i); Assert.AreEqual(deser.Count, 1); var element = deser["vary"] as BSONStringElement; Assert.IsNotNull(element); Assert.AreEqual(element.ElementType, BSONElementType.String); Assert.AreEqual(element.Name, "vary"); Assert.AreEqual(element.Value, value); Assert.AreEqual(stream.Position, 16 + i); // ensure whole document readed stream.Position = 0; } }); }
public void WriteSingleBooleanTrue() { using (var stream = new MemoryStream()) using (var reader = new BinaryReader(stream)) { var root = new BSONDocument(); root.Set(new BSONBooleanElement("booleanTrue", true)); root.WriteAsBSON(stream); Assert.AreEqual(stream.Position, 19); // ensure document length is 19 bytes stream.Seek(0, SeekOrigin.Begin); CollectionAssert.AreEqual(reader.ReadBytes(4), BitConverter.GetBytes(19)); // content length is 19 Assert.AreEqual(reader.ReadByte(), (byte)BSONElementType.Boolean); // element type is boolean 0x08 CollectionAssert.AreEqual(reader.ReadBytes(11), Encoding.UTF8.GetBytes("booleanTrue")); // element name is 'booleanTrue' Assert.AreEqual(reader.ReadByte(), (byte)0x00); // string name terminator 0x00 is present Assert.AreEqual(reader.ReadByte(), (byte)BSONBoolean.True); // byte content is correct Assert.AreEqual(reader.ReadByte(), (byte)0x00); // ensure last byte is terminator 0x00 Assert.AreEqual(stream.Position, 19); // ensure whole document readed } }
public void WriteSingleRegularExpression() { using (var stream = new MemoryStream()) using (var reader = new BinaryReader(stream)) { var root = new BSONDocument(); var pattern = @"^[-.\w]+@(?:[a-z\d]{2,}\.)+[a-z]{2,6}$"; var options = BSONRegularExpressionOptions.I | BSONRegularExpressionOptions.M |BSONRegularExpressionOptions.U; var regex = new BSONRegularExpression(pattern, options); root.Set(new BSONRegularExpressionElement("email", regex)); root.WriteAsBSON(stream); Assert.AreEqual(stream.Position, 55); // ensure document length is 55 bytes stream.Seek(0, SeekOrigin.Begin); CollectionAssert.AreEqual(reader.ReadBytes(4), BitConverter.GetBytes(55)); // ensure content length is 55 Assert.AreEqual(reader.ReadByte(), (byte)BSONElementType.RegularExpression); // ensure element type is RegularExpression 0x0b CollectionAssert.AreEqual(reader.ReadBytes(5), Encoding.UTF8.GetBytes("email")); // ensure element name is 'email' Assert.AreEqual(reader.ReadByte(), (byte)0x00); // ensure string name terminator 0x00 is present CollectionAssert.AreEqual(reader.ReadBytes(38), Encoding.UTF8.GetBytes(pattern)); // ensure element value is pattern Assert.AreEqual(reader.ReadByte(), (byte)0x00); // ensure string value terminator 0x00 is present CollectionAssert.AreEqual(reader.ReadBytes(3), Encoding.UTF8.GetBytes("imu")); // ensure element value is options in BSON format Assert.AreEqual(reader.ReadByte(), (byte)0x00); // ensure string value terminator 0x00 is present Assert.AreEqual(reader.ReadByte(), (byte)0x00); // ensure last byte is terminator 0x00 Assert.AreEqual(stream.Position, 55); // ensure whole document readed } }
/// <summary> /// {} (empty document) /// </summary> public void WriteEmptyDocument(Stream stream) { var root = new BSONDocument(); root.WriteAsBSON(stream); }
public void WriteUnicodeStrings() { using (var stream = new MemoryStream()) using (var reader = new BinaryReader(stream)) { var root = new BSONDocument(); root.Set(new BSONStringElement("eng", "hello")); root.Set(new BSONStringElement("rus", "привет")); root.Set(new BSONStringElement("chi", "你好")); root.Set(new BSONStringElement("jap", "こんにちは")); root.Set(new BSONStringElement("gre", "γεια σας")); root.Set(new BSONStringElement("alb", "përshëndetje")); root.Set(new BSONStringElement("arm", "բարեւ Ձեզ")); root.Set(new BSONStringElement("vie", "xin chào")); root.Set(new BSONStringElement("por", "Olá")); root.Set(new BSONStringElement("ukr", "Привіт")); root.Set(new BSONStringElement("ger", "wünsche")); root.WriteAsBSON(stream); Assert.AreEqual(stream.Position, 232); // ensure document length is 33 bytes stream.Seek(0, SeekOrigin.Begin); CollectionAssert.AreEqual(reader.ReadBytes(4), BitConverter.GetBytes(232)); // content length is 232 Assert.AreEqual(reader.ReadByte(), (byte) BSONElementType.String); // element type is string 0x02 CollectionAssert.AreEqual(reader.ReadBytes(3), Encoding.UTF8.GetBytes("eng")); // element name is 'eng' Assert.AreEqual(reader.ReadByte(), (byte) 0x00); // string name terminator 0x00 is present CollectionAssert.AreEqual(reader.ReadBytes(4), BitConverter.GetBytes(6)); // string content length is 6 CollectionAssert.AreEqual(reader.ReadBytes(5), Encoding.UTF8.GetBytes("hello")); Assert.AreEqual(reader.ReadByte(), (byte) 0x00); // string value terminator 0x00 is present Assert.AreEqual(reader.ReadByte(), (byte) BSONElementType.String); // element type is string 0x02 CollectionAssert.AreEqual(reader.ReadBytes(3), Encoding.UTF8.GetBytes("rus")); // element name is 'rus' Assert.AreEqual(reader.ReadByte(), (byte) 0x00); // string name terminator 0x00 is present CollectionAssert.AreEqual(reader.ReadBytes(4), BitConverter.GetBytes(13)); // string content length is 13 CollectionAssert.AreEqual(reader.ReadBytes(12), Encoding.UTF8.GetBytes("привет")); Assert.AreEqual(reader.ReadByte(), (byte) 0x00); // string value terminator 0x00 is present Assert.AreEqual(reader.ReadByte(), (byte) BSONElementType.String); // element type is string 0x02 CollectionAssert.AreEqual(reader.ReadBytes(3), Encoding.UTF8.GetBytes("chi")); // element name is 'chi' Assert.AreEqual(reader.ReadByte(), (byte) 0x00); // string name terminator 0x00 is present CollectionAssert.AreEqual(reader.ReadBytes(4), BitConverter.GetBytes(7)); // string content length is 7 CollectionAssert.AreEqual(reader.ReadBytes(6), Encoding.UTF8.GetBytes("你好")); Assert.AreEqual(reader.ReadByte(), (byte) 0x00); // string value terminator 0x00 is present Assert.AreEqual(reader.ReadByte(), (byte) BSONElementType.String); // element type is string 0x02 CollectionAssert.AreEqual(reader.ReadBytes(3), Encoding.UTF8.GetBytes("jap")); // element name is 'jap' Assert.AreEqual(reader.ReadByte(), (byte) 0x00); // string name terminator 0x00 is present CollectionAssert.AreEqual(reader.ReadBytes(4), BitConverter.GetBytes(16)); // string content length is 16 CollectionAssert.AreEqual(reader.ReadBytes(15), Encoding.UTF8.GetBytes("こんにちは")); Assert.AreEqual(reader.ReadByte(), (byte) 0x00); // string value terminator 0x00 is present Assert.AreEqual(reader.ReadByte(), (byte) BSONElementType.String); // element type is string 0x02 CollectionAssert.AreEqual(reader.ReadBytes(3), Encoding.UTF8.GetBytes("gre")); // element name is 'gre' Assert.AreEqual(reader.ReadByte(), (byte) 0x00); // string name terminator 0x00 is present CollectionAssert.AreEqual(reader.ReadBytes(4), BitConverter.GetBytes(16)); // string content length is 16 CollectionAssert.AreEqual(reader.ReadBytes(15), Encoding.UTF8.GetBytes("γεια σας")); Assert.AreEqual(reader.ReadByte(), (byte) 0x00); // string value terminator 0x00 is present Assert.AreEqual(reader.ReadByte(), (byte) BSONElementType.String); // element type is string 0x02 CollectionAssert.AreEqual(reader.ReadBytes(3), Encoding.UTF8.GetBytes("alb")); // element name is 'alb' Assert.AreEqual(reader.ReadByte(), (byte) 0x00); // string name terminator 0x00 is present CollectionAssert.AreEqual(reader.ReadBytes(4), BitConverter.GetBytes(15)); // string content length is 15 CollectionAssert.AreEqual(reader.ReadBytes(14), Encoding.UTF8.GetBytes("përshëndetje")); Assert.AreEqual(reader.ReadByte(), (byte) 0x00); // string value terminator 0x00 is present Assert.AreEqual(reader.ReadByte(), (byte) BSONElementType.String); // element type is string 0x02 CollectionAssert.AreEqual(reader.ReadBytes(3), Encoding.UTF8.GetBytes("arm")); // element name is 'arm' Assert.AreEqual(reader.ReadByte(), (byte) 0x00); // string name terminator 0x00 is present CollectionAssert.AreEqual(reader.ReadBytes(4), BitConverter.GetBytes(18)); // string content length is 18 CollectionAssert.AreEqual(reader.ReadBytes(17), Encoding.UTF8.GetBytes("բարեւ Ձեզ")); Assert.AreEqual(reader.ReadByte(), (byte) 0x00); // string value terminator 0x00 is present Assert.AreEqual(reader.ReadByte(), (byte) BSONElementType.String); // element type is string 0x02 CollectionAssert.AreEqual(reader.ReadBytes(3), Encoding.UTF8.GetBytes("vie")); // element name is 'vie' Assert.AreEqual(reader.ReadByte(), (byte) 0x00); // string name terminator 0x00 is present CollectionAssert.AreEqual(reader.ReadBytes(4), BitConverter.GetBytes(10)); // string content length is 10 CollectionAssert.AreEqual(reader.ReadBytes(9), Encoding.UTF8.GetBytes("xin chào")); Assert.AreEqual(reader.ReadByte(), (byte) 0x00); // string value terminator 0x00 is present Assert.AreEqual(reader.ReadByte(), (byte) BSONElementType.String); // element type is string 0x02 CollectionAssert.AreEqual(reader.ReadBytes(3), Encoding.UTF8.GetBytes("por")); // element name is 'por' Assert.AreEqual(reader.ReadByte(), (byte) 0x00); // string name terminator 0x00 is present CollectionAssert.AreEqual(reader.ReadBytes(4), BitConverter.GetBytes(5)); // string content length is 5 CollectionAssert.AreEqual(reader.ReadBytes(4), Encoding.UTF8.GetBytes("Olá")); Assert.AreEqual(reader.ReadByte(), (byte) 0x00); // string value terminator 0x00 is present Assert.AreEqual(reader.ReadByte(), (byte) BSONElementType.String); // element type is string 0x02 CollectionAssert.AreEqual(reader.ReadBytes(3), Encoding.UTF8.GetBytes("ukr")); // element name is 'ukr' Assert.AreEqual(reader.ReadByte(), (byte) 0x00); // string name terminator 0x00 is present CollectionAssert.AreEqual(reader.ReadBytes(4), BitConverter.GetBytes(13)); // string content length is 13 CollectionAssert.AreEqual(reader.ReadBytes(12), Encoding.UTF8.GetBytes("Привіт")); Assert.AreEqual(reader.ReadByte(), (byte) 0x00); // string value terminator 0x00 is present Assert.AreEqual(reader.ReadByte(), (byte) BSONElementType.String); // element type is string 0x02 CollectionAssert.AreEqual(reader.ReadBytes(3), Encoding.UTF8.GetBytes("ger")); // element name is 'ger' Assert.AreEqual(reader.ReadByte(), (byte) 0x00); // string name terminator 0x00 is present CollectionAssert.AreEqual(reader.ReadBytes(4), BitConverter.GetBytes(9)); // string content length is 9 CollectionAssert.AreEqual(reader.ReadBytes(8), Encoding.UTF8.GetBytes("wünsche")); Assert.AreEqual(reader.ReadByte(), (byte) 0x00); // string value terminator 0x00 is present Assert.AreEqual(reader.ReadByte(), (byte) 0x00); // ensure last byte is terminator 0x00 Assert.AreEqual(stream.Position, 232); // ensure whole document readed } }
public void WriteStringArray() { using (var stream = new MemoryStream()) using (var reader = new BinaryReader(stream)) { var root = new BSONDocument(); var array = new[] { new BSONStringElement("apple"), new BSONStringElement("orange"), new BSONStringElement("plum") }; root.Set(new BSONArrayElement("fruits", array)); root.WriteAsBSON(stream); Assert.AreEqual(stream.Position, 57); // ensure document length is 57 bytes stream.Seek(0, SeekOrigin.Begin); CollectionAssert.AreEqual(reader.ReadBytes(4), BitConverter.GetBytes(57)); // document's content length is 57 Assert.AreEqual(reader.ReadByte(), (byte)BSONElementType.Array); // element type is array 0x04 CollectionAssert.AreEqual(reader.ReadBytes(6), Encoding.UTF8.GetBytes("fruits")); // element name is 'fruits' Assert.AreEqual(reader.ReadByte(), (byte)0x00); // string name terminator 0x00 is present CollectionAssert.AreEqual(reader.ReadBytes(4), BitConverter.GetBytes(44)); // array's content length is 44 Assert.AreEqual(reader.ReadByte(), (byte)BSONElementType.String); // element type is string 0x02 CollectionAssert.AreEqual(reader.ReadBytes(1), Encoding.UTF8.GetBytes("0")); // element name is '0' Assert.AreEqual(reader.ReadByte(), (byte)0x00); // last byte is terminator 0x00 CollectionAssert.AreEqual(reader.ReadBytes(4), BitConverter.GetBytes(6)); // string content length is 6 CollectionAssert.AreEqual(reader.ReadBytes(5),Encoding.UTF8.GetBytes("apple")); // string content length is 'apple' Assert.AreEqual(reader.ReadByte(), (byte)0x00); // last byte is terminator 0x00 Assert.AreEqual(reader.ReadByte(), (byte)BSONElementType.String); // element type is string 0x02 CollectionAssert.AreEqual(reader.ReadBytes(1), Encoding.UTF8.GetBytes("1")); // element name is '1' Assert.AreEqual(reader.ReadByte(), (byte)0x00); // last byte is terminator 0x00 CollectionAssert.AreEqual(reader.ReadBytes(4), BitConverter.GetBytes(7)); // string content length is 7 CollectionAssert.AreEqual(reader.ReadBytes(6),Encoding.UTF8.GetBytes("orange")); // string content length is 'orange' Assert.AreEqual(reader.ReadByte(), (byte)0x00); // last byte is terminator 0x00 Assert.AreEqual(reader.ReadByte(), (byte)BSONElementType.String); // element type is string 0x02 CollectionAssert.AreEqual(reader.ReadBytes(1), Encoding.UTF8.GetBytes("2")); // element name is '2' Assert.AreEqual(reader.ReadByte(), (byte)0x00); // last byte is terminator 0x00 CollectionAssert.AreEqual(reader.ReadBytes(4), BitConverter.GetBytes(5)); // string content length is 5 CollectionAssert.AreEqual(reader.ReadBytes(4),Encoding.UTF8.GetBytes("plum")); // string content length is 'plum' Assert.AreEqual(reader.ReadByte(), (byte)0x00); // last byte is terminator 0x00 Assert.AreEqual(reader.ReadByte(), (byte)0x00); // ensure last byte is terminator 0x00 Assert.AreEqual(reader.ReadByte(), (byte)0x00); // ensure last byte is terminator 0x00 Assert.AreEqual(stream.Position, 57); // ensure whole document readed } }
public void WriteStringAndInt32Pair() { using (var stream = new MemoryStream()) using (var reader = new BinaryReader(stream)) { var root = new BSONDocument(); root.Set(new BSONStringElement("name", "Gagarin")); root.Set(new BSONInt32Element("birth", 1934)); root.WriteAsBSON(stream); Assert.AreEqual(stream.Position, 34); // ensure document length is 38 bytes stream.Seek(0, SeekOrigin.Begin); CollectionAssert.AreEqual(reader.ReadBytes(sizeof(int)), BitConverter.GetBytes(34)); // ensure content length is 34 Assert.AreEqual(reader.ReadByte(), (byte)BSONElementType.String); // ensure element type is string 0x02 CollectionAssert.AreEqual(reader.ReadBytes(4), Encoding.UTF8.GetBytes("name")); // ensure element name is 'name' Assert.AreEqual(reader.ReadByte(), (byte)0x00); // ensure string name terminator 0x00 is present CollectionAssert.AreEqual(reader.ReadBytes(sizeof(int)), BitConverter.GetBytes(8)); // ensure string content length is 8 CollectionAssert.AreEqual(reader.ReadBytes(7), Encoding.UTF8.GetBytes("Gagarin")); // ensure element value is 'Gagarin' Assert.AreEqual(reader.ReadByte(), (byte)0x00); // ensure string value terminator 0x00 is present Assert.AreEqual(reader.ReadByte(), (byte)BSONElementType.Int32); // ensure element type is int 0x10 CollectionAssert.AreEqual(reader.ReadBytes(5), Encoding.UTF8.GetBytes("birth")); // ensure element name is 'birth' Assert.AreEqual(reader.ReadByte(), (byte)0x00); // ensure string name terminator 0x00 is present CollectionAssert.AreEqual(reader.ReadBytes(4), BitConverter.GetBytes(1934)); // ensure element value is int 1934 Assert.AreEqual(reader.ReadByte(), (byte)0x00); // ensure last byte is terminator 0x00 Assert.AreEqual(stream.Position, 34); // ensure whole document readed } }
public void WriteSingleTimestamp() { using (var stream = new MemoryStream()) using (var reader = new BinaryReader(stream)) { var root = new BSONDocument(); var now = new DateTime(635000000000000000).ToUniversalTime(); var stamp = new BSONTimestamp(now, 123); root.Set(new BSONTimestampElement("stamp", stamp)); root.WriteAsBSON(stream); Assert.AreEqual(stream.Position, 20); // ensure document length is 20 bytes stream.Seek(0, SeekOrigin.Begin); CollectionAssert.AreEqual(reader.ReadBytes(4), BitConverter.GetBytes(20)); // content length is 20 Assert.AreEqual(reader.ReadByte(), (byte) BSONElementType.TimeStamp); // element type is TimeStamp 0x11 CollectionAssert.AreEqual(reader.ReadBytes(5), Encoding.UTF8.GetBytes("stamp")); // element name is 'now' Assert.AreEqual(reader.ReadByte(), (byte) 0x00); // string name terminator 0x00 is present CollectionAssert.AreEqual(reader.ReadBytes(4), BitConverter.GetBytes(123)); // increment is correct CollectionAssert.AreEqual(reader.ReadBytes(4), BitConverter.GetBytes((int)now.ToSecondsSinceUnixEpochStart())); // datetime is correct Assert.AreEqual(reader.ReadByte(), (byte) 0x00); // last byte is terminator 0x00 Assert.AreEqual(stream.Position, 20); // ensure whole document readed } }
public void WriteSingleObjectId() { using (var stream = new MemoryStream()) using (var reader = new BinaryReader(stream)) { var hex = "507f1f77bcf86cd799439011"; var data = Enumerable.Range(0, hex.Length) .Where(x => x % 2 == 0) .Select(x => Convert.ToByte(hex.Substring(x, 2), 16)) .ToArray(); var root = new BSONDocument(); var objectId = new BSONObjectID(data); root.Set(new BSONObjectIDElement("objectId", objectId)); root.WriteAsBSON(stream); Assert.AreEqual(stream.Position, 27); // ensure document length is 27 bytes stream.Seek(0, SeekOrigin.Begin); CollectionAssert.AreEqual(reader.ReadBytes(4), BitConverter.GetBytes(27)); // content length is 27 Assert.AreEqual(reader.ReadByte(), (byte)BSONElementType.ObjectID); // element type is objectID 0x07 CollectionAssert.AreEqual(reader.ReadBytes(8), Encoding.UTF8.GetBytes("objectId")); // element name is 'objectId' Assert.AreEqual(reader.ReadByte(), (byte)0x00); // string name terminator 0x00 is present CollectionAssert.AreEqual(reader.ReadBytes(12), data); // byte content is correct Assert.AreEqual(reader.ReadByte(), (byte)0x00); // ensure last byte is terminator 0x00 Assert.AreEqual(stream.Position, 27); // ensure whole document readed } }
public void WriteSingleBinaryData() { using (var stream = new MemoryStream()) using (var reader = new BinaryReader(stream)) { var data = Encoding.UTF8.GetBytes("This is binary data"); var root = new BSONDocument(); var binary = new BSONBinary(BSONBinaryType.BinaryOld, data); root.Set(new BSONBinaryElement("binary", binary)); root.WriteAsBSON(stream); Assert.AreEqual(stream.Position, 37); // ensure document length is 37 bytes stream.Seek(0, SeekOrigin.Begin); CollectionAssert.AreEqual(reader.ReadBytes(4), BitConverter.GetBytes(37)); // content length is 37 Assert.AreEqual(reader.ReadByte(), (byte)BSONElementType.Binary); // element type is binary 0x05 CollectionAssert.AreEqual(reader.ReadBytes(6), Encoding.UTF8.GetBytes("binary")); // element name is 'binary' Assert.AreEqual(reader.ReadByte(), (byte)0x00); // string name terminator 0x00 is present CollectionAssert.AreEqual(reader.ReadBytes(4), BitConverter.GetBytes(19)); // byte length is 19 Assert.AreEqual(reader.ReadByte(), (byte)BSONBinaryType.BinaryOld); // binary type is BinaryOld 0x02 CollectionAssert.AreEqual(reader.ReadBytes(19), data); // byte content is correct Assert.AreEqual(reader.ReadByte(), (byte)0x00); // ensure last byte is terminator 0x00 Assert.AreEqual(stream.Position, 37); // ensure whole document readed } }
public void WriteSingleNull() { using (var stream = new MemoryStream()) using (var reader = new BinaryReader(stream)) { var root = new BSONDocument(); root.Set(new BSONNullElement("null")); root.WriteAsBSON(stream); Assert.AreEqual(stream.Position, 11); // ensure document length is 11 bytes stream.Seek(0, SeekOrigin.Begin); CollectionAssert.AreEqual(reader.ReadBytes(4), BitConverter.GetBytes(11)); // content length is 11 Assert.AreEqual(reader.ReadByte(), (byte)BSONElementType.Null); // element type is null 0x0a CollectionAssert.AreEqual(reader.ReadBytes(4), Encoding.UTF8.GetBytes("null")); // element name is 'null' Assert.AreEqual(reader.ReadByte(), (byte)0x00); // string name terminator 0x00 is present Assert.AreEqual(reader.ReadByte(), (byte)0x00); // last byte is terminator 0x00 Assert.AreEqual(stream.Position, 11); // ensure whole document readed } }
public void WriteReadSingleDateTime() { using (var stream = new MemoryStream()) { var now = new DateTime(2015, 08, 26, 14, 23, 56); var bson1 = new BSONDocument(); bson1.Set(new BSONDateTimeElement("now", now)); bson1.WriteAsBSON(stream); stream.Position = 0; var bson2 = new BSONDocument(stream); var now1 = ((BSONDateTimeElement)bson1["now"]).Value; var now2 = ((BSONDateTimeElement)bson2["now"]).Value; Console.WriteLine("{0} {1}", now1, now1.Kind); Console.WriteLine("{0} {1}", now2, now2.Kind); Assert.AreEqual(now1.ToUniversalTime(), now2); } }
public void WriteSingleMinKey() { using (var stream = new MemoryStream()) using (var reader = new BinaryReader(stream)) { var root = new BSONDocument(); root.Set(new BSONMinKeyElement("minkey")); root.WriteAsBSON(stream); Assert.AreEqual(stream.Position, 13); // ensure document length is 13 bytes stream.Seek(0, SeekOrigin.Begin); CollectionAssert.AreEqual(reader.ReadBytes(4), BitConverter.GetBytes(13)); // ensure content length is 13 Assert.AreEqual(reader.ReadByte(), (byte)BSONElementType.MinKey); // ensure element type is MinKey 0xff CollectionAssert.AreEqual(reader.ReadBytes(6), Encoding.UTF8.GetBytes("minkey")); // ensure element name is 'minkey' Assert.AreEqual(reader.ReadByte(), (byte)0x00); // ensure string name terminator 0x00 is present Assert.AreEqual(reader.ReadByte(), (byte)0x00); // ensure last byte is terminator 0x00 Assert.AreEqual(stream.Position, 13); // ensure whole document readed } }
public void WriteInt32Array() { using (var stream = new MemoryStream()) using (var reader = new BinaryReader(stream)) { var root = new BSONDocument(); var array = new[] { new BSONInt32Element(1963), new BSONInt32Element(1984), new BSONInt32Element(2015) }; root.Set(new BSONArrayElement("years", array)); root.WriteAsBSON(stream); Assert.AreEqual(stream.Position, 38); // ensure document length is 38 bytes stream.Seek(0, SeekOrigin.Begin); CollectionAssert.AreEqual(reader.ReadBytes(4), BitConverter.GetBytes(38)); // document's content length is 38 Assert.AreEqual(reader.ReadByte(), (byte)BSONElementType.Array); // element type is array 0x04 CollectionAssert.AreEqual(reader.ReadBytes(5), Encoding.UTF8.GetBytes("years")); // element name is 'years' Assert.AreEqual(reader.ReadByte(), (byte)0x00); // string name terminator 0x00 is present CollectionAssert.AreEqual(reader.ReadBytes(4), BitConverter.GetBytes(26)); // array's content length is 26 Assert.AreEqual(reader.ReadByte(), (byte)BSONElementType.Int32); // element type is int32 0x10 CollectionAssert.AreEqual(reader.ReadBytes(1), Encoding.UTF8.GetBytes("0")); // element name is '0' Assert.AreEqual(reader.ReadByte(), (byte)0x00); // last byte is terminator 0x00 CollectionAssert.AreEqual(reader.ReadBytes(4), BitConverter.GetBytes(1963)); // value is 1963 Assert.AreEqual(reader.ReadByte(), (byte)BSONElementType.Int32); // element type is int32 0x10 CollectionAssert.AreEqual(reader.ReadBytes(1), Encoding.UTF8.GetBytes("1")); // element name is '1' Assert.AreEqual(reader.ReadByte(), (byte)0x00); // last byte is terminator 0x00 CollectionAssert.AreEqual(reader.ReadBytes(4), BitConverter.GetBytes(1984)); // value is 1984 Assert.AreEqual(reader.ReadByte(), (byte)BSONElementType.Int32); // element type is int32 0x10 CollectionAssert.AreEqual(reader.ReadBytes(1), Encoding.UTF8.GetBytes("2")); // element name is '2' Assert.AreEqual(reader.ReadByte(), (byte)0x00); // last byte is terminator 0x00 CollectionAssert.AreEqual(reader.ReadBytes(4), BitConverter.GetBytes(2015)); // value is 2015 Assert.AreEqual(reader.ReadByte(), (byte)0x00); // ensure last byte is terminator 0x00 Assert.AreEqual(reader.ReadByte(), (byte)0x00); // ensure last byte is terminator 0x00 Assert.AreEqual(stream.Position, 38); // ensure whole document readed } }
public void WriteSingleJavaScriptWithScope() { using (var stream = new MemoryStream()) using (var reader = new BinaryReader(stream)) { var root = new BSONDocument(); var code = "function(){var x=1;var y='abc';return z;};"; var scope = new BSONDocument(); scope.Set(new BSONInt32Element("z", 23)); var jsWithScope = new BSONCodeWithScope(code, scope); root.Set(new BSONJavaScriptWithScopeElement("codeWithScope", jsWithScope)); root.WriteAsBSON(stream); Assert.AreEqual(stream.Position, 83); // ensure document length is 83 bytes stream.Seek(0, SeekOrigin.Begin); CollectionAssert.AreEqual(reader.ReadBytes(4), BitConverter.GetBytes(83)); // content length is 83 Assert.AreEqual(reader.ReadByte(), (byte)BSONElementType.JavaScriptWithScope); // element type is JavaScriptWithScope 0x0f CollectionAssert.AreEqual(reader.ReadBytes(13), Encoding.UTF8.GetBytes("codeWithScope")); // element name is 'codeWithScope' Assert.AreEqual(reader.ReadByte(), (byte)0x00); // string name terminator 0x00 is present CollectionAssert.AreEqual(reader.ReadBytes(4), BitConverter.GetBytes(63)); // full content length is 63 CollectionAssert.AreEqual(reader.ReadBytes(4), BitConverter.GetBytes(43)); // content length is 43 CollectionAssert.AreEqual(reader.ReadBytes(42), Encoding.UTF8.GetBytes(code)); // element value is code Assert.AreEqual(reader.ReadByte(), (byte)0x00); // string value terminator 0x00 is present CollectionAssert.AreEqual(reader.ReadBytes(4), BitConverter.GetBytes(12)); // full scope content length is 12 Assert.AreEqual(reader.ReadByte(), (byte)BSONElementType.Int32); // element type is int 0x10 CollectionAssert.AreEqual(reader.ReadBytes(1), Encoding.UTF8.GetBytes("z")); // element name is 'z' Assert.AreEqual(reader.ReadByte(), (byte)0x00); // string name terminator 0x00 is present CollectionAssert.AreEqual(reader.ReadBytes(4), BitConverter.GetBytes(23)); // z variable value is 23 Assert.AreEqual(reader.ReadByte(), (byte)0x00); // last byte is terminator 0x00 Assert.AreEqual(reader.ReadByte(), (byte)0x00); // last byte is terminator 0x00 Assert.AreEqual(stream.Position, 83); // ensure whole document readed } }
public void WriteBigIntegers() { using (var stream = new MemoryStream()) using (var reader = new BinaryReader(stream)) { var root = new BSONDocument(); root.Set(new BSONInt32Element("intMin", int.MinValue)); root.Set(new BSONInt32Element("intMax", int.MaxValue)); root.Set(new BSONInt64Element("longMin", long.MinValue)); root.Set(new BSONInt64Element("longMax", long.MaxValue)); root.WriteAsBSON(stream); Assert.AreEqual(stream.Position, 63); // ensure document length is 63 bytes stream.Seek(0, SeekOrigin.Begin); CollectionAssert.AreEqual(reader.ReadBytes(4), BitConverter.GetBytes(63)); // content length is 63 Assert.AreEqual(reader.ReadByte(), (byte)BSONElementType.Int32); // element type is int32 0x10 CollectionAssert.AreEqual(reader.ReadBytes(6), Encoding.UTF8.GetBytes("intMin")); // element name is 'intMin' Assert.AreEqual(reader.ReadByte(), (byte)0x00); // string name terminator 0x00 is present CollectionAssert.AreEqual(reader.ReadBytes(4), BitConverter.GetBytes(int.MinValue)); // element value is int.MinValue Assert.AreEqual(reader.ReadByte(), (byte)BSONElementType.Int32); // element type is int32 0x10 CollectionAssert.AreEqual(reader.ReadBytes(6), Encoding.UTF8.GetBytes("intMax")); // element name is 'intMax' Assert.AreEqual(reader.ReadByte(), (byte)0x00); // string name terminator 0x00 is present CollectionAssert.AreEqual(reader.ReadBytes(4), BitConverter.GetBytes(int.MaxValue)); // element value is int.MaxValue Assert.AreEqual(reader.ReadByte(), (byte)BSONElementType.Int64); // element type is int64 0x12 CollectionAssert.AreEqual(reader.ReadBytes(7), Encoding.UTF8.GetBytes("longMin")); // element name is 'longMin' Assert.AreEqual(reader.ReadByte(), (byte)0x00); // string name terminator 0x00 is present CollectionAssert.AreEqual(reader.ReadBytes(8), BitConverter.GetBytes(long.MinValue)); // element value is long.MinValue Assert.AreEqual(reader.ReadByte(), (byte)BSONElementType.Int64); // element type is int64 0x12 CollectionAssert.AreEqual(reader.ReadBytes(7), Encoding.UTF8.GetBytes("longMax")); // element name is 'longMax' Assert.AreEqual(reader.ReadByte(), (byte)0x00); // string name terminator 0x00 is present CollectionAssert.AreEqual(reader.ReadBytes(8), BitConverter.GetBytes(long.MaxValue)); // element value is long.MaxValue Assert.AreEqual(reader.ReadByte(), (byte)0x00); // ensure last byte is terminator 0x00 Assert.AreEqual(stream.Position, 63); // ensure whole document readed } }
public void WriteSingleJavaScript() { using (var stream = new MemoryStream()) using (var reader = new BinaryReader(stream)) { var root = new BSONDocument(); var code = "function(){var x=1;var y='abc';return 1;};"; root.Set(new BSONJavaScriptElement("code", code)); root.WriteAsBSON(stream); Assert.AreEqual(stream.Position, 58); // ensure document length is 58 bytes stream.Seek(0, SeekOrigin.Begin); CollectionAssert.AreEqual(reader.ReadBytes(4), BitConverter.GetBytes(58)); // content length is 58 Assert.AreEqual(reader.ReadByte(), (byte)BSONElementType.JavaScript); // element type is JavaScript 0x0d CollectionAssert.AreEqual(reader.ReadBytes(4), Encoding.UTF8.GetBytes("code")); // element name is 'code' Assert.AreEqual(reader.ReadByte(), (byte)0x00); // string name terminator 0x00 is present CollectionAssert.AreEqual(reader.ReadBytes(4), BitConverter.GetBytes(43)); // js code content length is 43 CollectionAssert.AreEqual(reader.ReadBytes(42), Encoding.UTF8.GetBytes(code)); // element value is code Assert.AreEqual(reader.ReadByte(), (byte)0x00); // string value terminator 0x00 is present Assert.AreEqual(reader.ReadByte(), (byte)0x00); // last byte is terminator 0x00 Assert.AreEqual(stream.Position, 58); // ensure whole document readed } }
private BSONDocument fullCopy(BSONDocument original) { //note: doc.DeepClone does not work as expected (copies references for byte[]) using (var ms = new MemoryStream()) { #pragma warning disable 0618 original.WriteAsBSON(ms); ms.Position = 0; return new BSONDocument(ms); #pragma warning restore 0618 } }
public void WriteSingleInt64() { using (var stream = new MemoryStream()) using (var reader = new BinaryReader(stream)) { var root = new BSONDocument(); root.Set(new BSONInt64Element("solarSystemDiameter", 10000000000000)); root.WriteAsBSON(stream); Assert.AreEqual(stream.Position, 34); // ensure document length is 34 bytes stream.Seek(0, SeekOrigin.Begin); CollectionAssert.AreEqual(reader.ReadBytes(4), BitConverter.GetBytes(34)); // ensure content length is 34 Assert.AreEqual(reader.ReadByte(), (byte)BSONElementType.Int64); // ensure element type is int64 0x12 CollectionAssert.AreEqual(reader.ReadBytes(19), Encoding.UTF8.GetBytes("solarSystemDiameter")); // ensure element name is 'solarSystemDiameter' Assert.AreEqual(reader.ReadByte(), (byte)0x00); // ensure string name terminator 0x00 is present CollectionAssert.AreEqual(reader.ReadBytes(8), BitConverter.GetBytes(10000000000000)); // ensure element value is 10000000000000 Assert.AreEqual(reader.ReadByte(), (byte)0x00); // ensure last byte is terminator 0x00 Assert.AreEqual(stream.Position, 34); // ensure whole document readed } }
public void WriteSingleInt32() { using (var stream = new MemoryStream()) using (var reader = new BinaryReader(stream)) { var root = new BSONDocument(); root.Set(new BSONInt32Element("lucky", 7)); root.WriteAsBSON(stream); Assert.AreEqual(stream.Position, 16); // ensure document length is 16 bytes stream.Seek(0, SeekOrigin.Begin); CollectionAssert.AreEqual(reader.ReadBytes(4), BitConverter.GetBytes(16)); // ensure content length is 16 Assert.AreEqual(reader.ReadByte(), (byte)BSONElementType.Int32); // ensure element type is int32 0x10 CollectionAssert.AreEqual(reader.ReadBytes(5), Encoding.UTF8.GetBytes("lucky")); // ensure element name is 'lucky' Assert.AreEqual(reader.ReadByte(), (byte)0x00); // ensure string name terminator 0x00 is present CollectionAssert.AreEqual(reader.ReadBytes(4), BitConverter.GetBytes(7)); // ensure element value is 7 Assert.AreEqual(reader.ReadByte(), (byte)0x00); // ensure last byte is terminator 0x00 Assert.AreEqual(stream.Position, 16); // ensure whole document readed } }
public void WriteSingleDouble() { using (var stream = new MemoryStream()) using (var reader = new BinaryReader(stream)) { var root = new BSONDocument(); root.Set(new BSONDoubleElement("pi", Math.PI)); root.WriteAsBSON(stream); Assert.AreEqual(stream.Position, 17); // ensure document length is 17 bytes stream.Seek(0, SeekOrigin.Begin); CollectionAssert.AreEqual(reader.ReadBytes(4), BitConverter.GetBytes(17)); // ensure content length is 17 Assert.AreEqual(reader.ReadByte(), (byte)BSONElementType.Double); // ensure element type is double 0x01 CollectionAssert.AreEqual(reader.ReadBytes(2), Encoding.UTF8.GetBytes("pi")); // ensure element name is 'pi' Assert.AreEqual(reader.ReadByte(), (byte)0x00); // ensure string name terminator 0x00 is present CollectionAssert.AreEqual(reader.ReadBytes(8), BitConverter.GetBytes(Math.PI)); // ensure element value is Math.PI Assert.AreEqual(reader.ReadByte(), (byte)0x00); // ensure last byte is terminator 0x00 Assert.AreEqual(stream.Position, 17); // ensure whole document readed } }
public void WriteSingleDateTime() { using (var stream = new MemoryStream()) using (var reader = new BinaryReader(stream)) { var now = new DateTime(635000000000000000).ToUniversalTime(); var root = new BSONDocument(); root.Set(new BSONDateTimeElement("now", now)); root.WriteAsBSON(stream); Assert.AreEqual(stream.Position, 18); // ensure document length is 18 bytes stream.Seek(0, SeekOrigin.Begin); CollectionAssert.AreEqual(reader.ReadBytes(4), BitConverter.GetBytes(18)); // ensure content length is 18 Assert.AreEqual(reader.ReadByte(), (byte) BSONElementType.DateTime); // ensure element type is DateTime 0x09 CollectionAssert.AreEqual(reader.ReadBytes(3), Encoding.UTF8.GetBytes("now")); // ensure element name is 'now' Assert.AreEqual(reader.ReadByte(), (byte) 0x00); // ensure string name terminator 0x00 is present CollectionAssert.AreEqual(reader.ReadBytes(8), BitConverter.GetBytes(now.ToMillisecondsSinceUnixEpochStart())); // ensure element value is correct Assert.AreEqual(reader.ReadByte(), (byte) 0x00); // ensure last byte is terminator 0x00 Assert.AreEqual(stream.Position, 18); // ensure whole document readed } }
public void WriteStringInt32DoubleMixedArray() { using (var stream = new MemoryStream()) using (var reader = new BinaryReader(stream)) { var root = new BSONDocument(); var array = new BSONElement[] { new BSONStringElement("apple"), new BSONInt32Element(3), new BSONDoubleElement(2.14D) }; root.Set(new BSONArrayElement("stuff", array)); root.WriteAsBSON(stream); Assert.AreEqual(stream.Position, 48); // ensure document length is 48 bytes stream.Seek(0, SeekOrigin.Begin); CollectionAssert.AreEqual(reader.ReadBytes(4), BitConverter.GetBytes(48)); // document's content length is 48 Assert.AreEqual(reader.ReadByte(), (byte)BSONElementType.Array); // element type is array 0x04 CollectionAssert.AreEqual(reader.ReadBytes(5), Encoding.UTF8.GetBytes("stuff")); // element name is 'stuff' Assert.AreEqual(reader.ReadByte(), (byte)0x00); // string name terminator 0x00 is present CollectionAssert.AreEqual(reader.ReadBytes(4), BitConverter.GetBytes(36)); // array's content length is 36 Assert.AreEqual(reader.ReadByte(), (byte)BSONElementType.String); // element type is string 0x02 CollectionAssert.AreEqual(reader.ReadBytes(1), Encoding.UTF8.GetBytes("0")); // element name is '0' Assert.AreEqual(reader.ReadByte(), (byte)0x00); // last byte is terminator 0x00 CollectionAssert.AreEqual(reader.ReadBytes(4), BitConverter.GetBytes(6)); // string content length is 6 CollectionAssert.AreEqual(reader.ReadBytes(5),Encoding.UTF8.GetBytes("apple")); // string content length is 'apple' Assert.AreEqual(reader.ReadByte(), (byte)0x00); // last byte is terminator 0x00 Assert.AreEqual(reader.ReadByte(), (byte)BSONElementType.Int32); // element type is int32 0x10 CollectionAssert.AreEqual(reader.ReadBytes(1), Encoding.UTF8.GetBytes("1")); // element name is '1' Assert.AreEqual(reader.ReadByte(), (byte)0x00); // last byte is terminator 0x00 CollectionAssert.AreEqual(reader.ReadBytes(4), BitConverter.GetBytes(3)); // value is 3 Assert.AreEqual(reader.ReadByte(), (byte)BSONElementType.Double); // element type is double 0x01 CollectionAssert.AreEqual(reader.ReadBytes(1), Encoding.UTF8.GetBytes("2")); // element name is '2' Assert.AreEqual(reader.ReadByte(), (byte)0x00); // last byte is terminator 0x00 CollectionAssert.AreEqual(reader.ReadBytes(8), BitConverter.GetBytes(2.14D)); // value is 2.14 Assert.AreEqual(reader.ReadByte(), (byte)0x00); // ensure last byte is terminator 0x00 Assert.AreEqual(reader.ReadByte(), (byte)0x00); // ensure last byte is terminator 0x00 Assert.AreEqual(stream.Position, 48); // ensure whole document readed } }
public void WriteSingleString() { using (var stream = new MemoryStream()) using (var reader = new BinaryReader(stream)) { var root = new BSONDocument(); root.Set(new BSONStringElement("greetings", "Hello World!")); root.WriteAsBSON(stream); Assert.AreEqual(stream.Position, 33); // ensure document length is 33 bytes stream.Seek(0, SeekOrigin.Begin); CollectionAssert.AreEqual(reader.ReadBytes(sizeof(int)), BitConverter.GetBytes(33)); // ensure content length is 33 Assert.AreEqual(reader.ReadByte(), (byte)BSONElementType.String); // ensure element type is string 0x02 CollectionAssert.AreEqual(reader.ReadBytes(9), Encoding.UTF8.GetBytes("greetings")); // ensure element name is 'greetings' Assert.AreEqual(reader.ReadByte(), (byte)0x00); // ensure string name terminator 0x00 is present CollectionAssert.AreEqual(reader.ReadBytes(sizeof(int)), BitConverter.GetBytes(13)); // ensure string content length is 13 CollectionAssert.AreEqual(reader.ReadBytes(12), Encoding.UTF8.GetBytes("Hello World!")); // ensure element value is 'Hello World!' Assert.AreEqual(reader.ReadByte(), (byte)0x00); // ensure string value terminator 0x00 is present Assert.AreEqual(reader.ReadByte(), (byte)0x00); // ensure last byte is terminator 0x00 Assert.AreEqual(stream.Position, 33); // ensure whole document readed } }