public void TestReadBigDocument()
        {
            MemoryStream ms     = new MemoryStream();
            BsonWriter   writer = new BsonWriter(ms);

            Document expected = new Document();

            expected.Append("str", "test")
            .Append("int", 45)
            .Append("long", (long)46)
            .Append("num", 4.5)
            .Append("date", DateTime.Today)
            .Append("_id", new OidGenerator().Generate())
            .Append("code", new Code("return 1;"))
            .Append("subdoc", new Document().Append("a", 1).Append("b", 2))
            .Append("array", new String[] { "a", "b", "c", "d" })
            .Append("codewscope", new CodeWScope("return 2;", new Document().Append("c", 1)))
            .Append("binary", new Binary(new byte[] { 0, 1, 2, 3 }))
            .Append("regex", new MongoRegex("[A-Z]"))
            .Append("minkey", MongoMinKey.Value)
            .Append("maxkey", MongoMaxKey.Value)
            ;
            writer.Write(expected);
            writer.Flush();
            ms.Seek(0, SeekOrigin.Begin);

            BsonReader reader = new BsonReader(ms);
            Document   doc    = reader.Read();

            Assert.IsNotNull(doc);
        }
Example #2
0
        public void TestReadBigDocument()
        {
            MemoryStream ms = new MemoryStream();
            BsonWriter writer = new BsonWriter(ms);

            Document expected = new Document();
            expected.Append("str", "test")
                .Append("int", 45)
                .Append("long", (long)46)
                .Append("num", 4.5)
                .Append("date",DateTime.Today)
                .Append("_id", new OidGenerator().Generate())
                .Append("code", new Code("return 1;"))
                .Append("subdoc", new Document().Append("a",1).Append("b",2))
                .Append("array", new String[]{"a","b","c","d"})
                .Append("codewscope", new CodeWScope("return 2;", new Document().Append("c",1)))
                .Append("binary", new Binary(new byte[]{0,1,2,3}))
                .Append("regex", new MongoRegex("[A-Z]"))
                .Append("minkey", MongoMinKey.Value)
                .Append("maxkey", MongoMaxKey.Value)
            ;
            writer.Write(expected);
            writer.Flush();
            ms.Seek(0,SeekOrigin.Begin);

            BsonReader reader = new BsonReader(ms);
            Document doc = reader.Read();

            Assert.IsNotNull(doc);
        }
        public void TestReadSimpleDocument()
        {
            byte[]       buf    = HexToBytes("1400000002746573740005000000746573740000");
            MemoryStream ms     = new MemoryStream(buf);
            BsonReader   reader = new BsonReader(ms);

            Document doc = reader.Read();

            Assert.IsNotNull(doc, "Document was null");
            Assert.IsTrue(doc.Contains("test"));
            Assert.AreEqual("test", (String)doc["test"]);
        }
        protected Document WriteAndRead(Document source)
        {
            MemoryStream ms     = new MemoryStream();
            BsonWriter   writer = new BsonWriter(ms);

            writer.Write(source);
            writer.Flush();
            ms.Seek(0, SeekOrigin.Begin);

            BsonReader reader = new BsonReader(ms);

            return(reader.Read());
        }
Example #5
0
        public void TestRoundTrip()
        {
            Document idoc = new Document();
            idoc.Add("b",new Binary(new byte[]{(byte)1,(byte)2}));

            MemoryStream stream = new MemoryStream();
            BsonWriter writer = new BsonWriter(stream);
            writer.Write(idoc);

            stream.Seek(0,SeekOrigin.Begin);
            BsonReader reader = new BsonReader(stream);
            Document odoc = reader.Read();

            Assert.AreEqual(idoc.ToString(), odoc.ToString());
        }
        public void Read(Stream stream)
        {
            stream = new BufferedStream(stream, 256);
            BinaryReader reader = new BinaryReader(stream);
            this.Header = ReadHeader(reader);
            this.ResponseFlag = reader.ReadInt32();
            this.CursorID = reader.ReadInt64();
            this.StartingFrom = reader.ReadInt32();
            this.NumberReturned = reader.ReadInt32();

            BsonReader breader = new BsonReader(documentFactory, stream);
            List<Document> docs = new List<Document>();
            for(int num = 0; num < this.NumberReturned; num++){
                docs.Add(breader.Read());
            }
            this.Documents = docs.ToArray();
        }
Example #7
0
        public void TestRoundTrip()
        {
            Document idoc = new Document();

            idoc.Add("b", new Binary(new byte[] { (byte)1, (byte)2 }));

            MemoryStream stream = new MemoryStream();
            BsonWriter   writer = new BsonWriter(stream);

            writer.Write(idoc);

            stream.Seek(0, SeekOrigin.Begin);
            BsonReader reader = new BsonReader(stream);
            Document   odoc   = reader.Read();

            Assert.AreEqual(idoc.ToString(), odoc.ToString());
        }
Example #8
0
        public void TestBinaryRead()
        {
            string hex = "28000000075f6964004b1971811d8b0f00c0000000056461746100070000000203000000e188b400";

            byte[] data = DecodeHex (hex);
            MemoryStream inmem = new MemoryStream (data);
            BsonReader inreader = new BsonReader (inmem);
            Document indoc = new Document ();
            indoc = inreader.Read();

            MemoryStream outmem = new MemoryStream ();
            BsonWriter outwriter = new BsonWriter (outmem);
            outwriter.Write(indoc);
            byte[] outdata = outmem.ToArray ();
            String outhex = BitConverter.ToString (outdata);
            outhex = outhex.Replace ("-", "");

            Assert.AreEqual (hex, outhex.ToLower());
        }
        public void TestDateUTC()
        {
            DateTime now = DateTime.UtcNow;
            MemoryStream ms = new MemoryStream();
            BsonWriter writer = new BsonWriter(ms);

            Document source = new Document();
            source.Append("d",now);

            writer.Write(source);
            writer.Flush();
            ms.Seek(0,SeekOrigin.Begin);

            BsonReader reader = new BsonReader(ms);
            Document copy = reader.Read();

            DateTime then = (DateTime)copy["d"];

            Assert.AreEqual(now.Hour,then.Hour, "Date did not round trip right.");
        }
Example #10
0
        public void TestBinaryRead()
        {
            string hex = "28000000075f6964004b1971811d8b0f00c0000000056461746100070000000203000000e188b400";

            byte[]       data     = DecodeHex(hex);
            MemoryStream inmem    = new MemoryStream(data);
            BsonReader   inreader = new BsonReader(inmem);
            Document     indoc    = new Document();

            indoc = inreader.Read();

            MemoryStream outmem    = new MemoryStream();
            BsonWriter   outwriter = new BsonWriter(outmem);

            outwriter.Write(indoc);
            byte[] outdata = outmem.ToArray();
            String outhex  = BitConverter.ToString(outdata);

            outhex = outhex.Replace("-", "");

            Assert.AreEqual(hex, outhex.ToLower());
        }
        public void TestDBRef()
        {
            MemoryStream ms = new MemoryStream();
            BsonWriter writer = new BsonWriter(ms);

            Document source = new Document();
            source.Append("x",1).Append("ref",new DBRef("refs","ref1"));

            writer.Write(source);
            writer.Flush();
            ms.Seek(0,SeekOrigin.Begin);

            BsonReader reader = new BsonReader(ms);
            Document copy = reader.Read();

            Assert.IsTrue(copy.Contains("ref"));
            Assert.IsTrue(copy["ref"].GetType() == typeof(DBRef));

            DBRef sref = (DBRef)source["ref"];
            DBRef cref = (DBRef)copy["ref"];

            Assert.AreEqual(sref.Id, cref.Id);
        }
Example #12
0
        protected Document WriteAndRead(Document source)
        {
            MemoryStream ms = new MemoryStream();
            BsonWriter writer = new BsonWriter(ms);

            writer.Write(source);
            writer.Flush();
            ms.Seek(0, SeekOrigin.Begin);

            BsonReader reader = new BsonReader(ms);
            return reader.Read();
        }
Example #13
0
        public void TestReadSimpleDocument()
        {
            byte[] buf = HexToBytes("1400000002746573740005000000746573740000");
            MemoryStream ms = new MemoryStream(buf);
            BsonReader reader = new BsonReader(ms);

            Document doc = reader.Read();

            Assert.IsNotNull(doc, "Document was null");
            Assert.IsTrue(doc.Contains("test"));
            Assert.AreEqual("test", (String)doc["test"]);
        }
Example #14
0
 static void DoDecode(byte[] buff)
 {
     MemoryStream ms = new MemoryStream(buff);
     for(int i = 0; i < perTrial; i++){
         BsonReader reader = new BsonReader(ms);
         reader.Read();
         ms.Seek(0,SeekOrigin.Begin);
     }
 }