public void TestArrayElements()
        {
            String hexdoc = "82000000075f6964004a78937917220000000061cf0461005d0000" +
                            "00013000000000000000f03f013100000000000000004001320000" +
                            "000000000008400133000000000000001040013400000000000000" +
                            "145001350000000000000018400136000000000000001c40013700" +
                            "00000000000020400002620005000000746573740000";

            byte[] bytes = HexToBytes(hexdoc);
            MemoryStream buf = new MemoryStream(bytes);
            BsonReader reader = new BsonReader(buf);

            BsonDocument bdoc = new BsonDocument();
            bdoc.Read(reader);
            Assert.AreEqual(BsonDataType.Array, (BsonDataType)bdoc["a"].Val.TypeNum);

            buf = new MemoryStream();
            BsonWriter writer = new BsonWriter(buf);
            bdoc.Write(writer);

            String hexdump = BitConverter.ToString(buf.ToArray());
            hexdump = hexdump.Replace("-","").ToLower();

            Assert.AreEqual(hexdoc, hexdump);
        }
Esempio n. 2
0
        public void TestBinaryRead()
        {
            string hex = "28000000075f6964004b1971811d8b0f00c0000000056461746100070000000203000000e188b400";

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

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

            Assert.AreEqual (hex, outhex.ToLower());
        }
        public void TestArraysWithHoles()
        {
            String hexdoc =
                "46000000075F6964004A79BFD517220000000061D304617272617900" +
                "29000000023000020000006100023100020000006200023200020000" +
                "0063000234000200000065000000";
            byte[] bytes = HexToBytes(hexdoc);
            MemoryStream buf = new MemoryStream(bytes);
            BsonReader reader = new BsonReader(buf);
            BsonDocument bdoc = new BsonDocument();
            bdoc.Read(reader);
            Assert.AreEqual(BsonDataType.Array, (BsonDataType)bdoc["array"].Val.TypeNum);

            buf = new MemoryStream();
            BsonWriter writer = new BsonWriter(buf);
            bdoc.Write(writer);

            String hexdump = BitConverter.ToString(buf.ToArray());
            hexdump = hexdump.Replace("-","");

            Assert.AreEqual(hexdoc, hexdump);
        }
        public void TestElements()
        {
            BsonDocument bdoc = new BsonDocument();
            MemoryStream buf = new MemoryStream();
            BsonWriter writer = new BsonWriter(buf);

            Oid oid = new Oid("4a753ad8fac16ea58b290351");

            bdoc.Append("_id", new BsonElement("_id",new BsonOid(oid)))
                .Append("a", new BsonElement("a",new BsonNumber(1)))
                .Append("b", new BsonElement("b",new BsonString("test")));
            bdoc.Write(writer);

            writer.Flush();

            Byte[] output = buf.ToArray();
            String hexdump = BitConverter.ToString(output);
            hexdump = hexdump.Replace("-","");
                             //0         1         2         3         4         5         6         7         8         9
                             //0123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789
            string expected = "2D000000075F6964004A753AD8FAC16EA58B290351016100000000000000F03F02620005000000746573740000";
            Assert.AreEqual(expected,hexdump, "Dump not correct");
        }
        public void TestNumberElements()
        {
            BsonDocument bdoc = new BsonDocument();
            MemoryStream buf = new MemoryStream();
            BsonWriter writer = new BsonWriter(buf);

            Oid oid = new Oid("4a75384cfac16ea58b290350");

            bdoc.Append("_id", new BsonElement("_id",new BsonOid(oid)))
                .Append("a", new BsonElement("a",new BsonNumber(1)))
                .Append("b", new BsonElement("b",new BsonNumber(2)));
            bdoc.Write(writer);

            writer.Flush();

            Byte[] output = buf.ToArray();
            String hexdump = BitConverter.ToString(output);
            hexdump = hexdump.Replace("-","");

            Assert.AreEqual("2C000000075F6964004A75384CFAC16EA58B290350016100000000000000F03F016200000000000000004000",hexdump, "Dump not correct");
        }
        public void TestFormattingWithUKPound()
        {
            BsonDocument doc = new BsonDocument();
            MemoryStream buf = new MemoryStream();
            BsonWriter writer = new BsonWriter(buf);

            doc.Add("_id", new BsonOid(new Oid("4ABBED9D1D8B0F0218000001")));
            doc.Add("test", new BsonString("1234£56"));
            doc.Write(writer);
            writer.Flush();

            Byte[] output = buf.ToArray();
            String hexdump = BitConverter.ToString(output);
            hexdump = hexdump.Replace("-","");
            //Assert.AreEqual(20,output[0],"Size didn't take into count null terminator");
            Assert.AreEqual("29000000075F6964004ABBED9D1D8B0F02180000010274657374000900000031323334C2A335360000",hexdump, "Dump not correct");
        }
        public void TestFormatting()
        {
            BsonDocument doc = new BsonDocument();
            MemoryStream buf = new MemoryStream();
            BsonWriter writer = new BsonWriter(buf);

            doc.Add("test", new BsonString("test"));
            doc.Write(writer);
            writer.Flush();

            Byte[] output = buf.ToArray();
            String hexdump = BitConverter.ToString(output);
            hexdump = hexdump.Replace("-","");
            Assert.AreEqual(20,output[0],"Size didn't take into count null terminator");
            Assert.AreEqual("1400000002746573740005000000746573740000",hexdump, "Dump not correct");
        }