Пример #1
0
        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
              }
        }
Пример #2
0
 /// <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);
 }