Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
tree: 97623f7b27
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 287 lines (234 sloc) 10.788 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287
using System;
using System.IO;
using System.Text;
using MongoDB.Bson;
using MongoDB.Util;
using NUnit.Framework;

namespace MongoDB.UnitTests.Bson
{
[TestFixture]
    public class TestBsonReader : BsonTestBase
    {
        private char pound = '\u00a3';
        private char euro = '\u20ac';

        private string WriteAndReadString(string val){
            var buf = Encoding.UTF8.GetBytes(val + '\0');

            var ms = new MemoryStream(buf);
            var reader = new BsonReader(ms, new BsonDocumentBuilder());
            return reader.ReadString();
        }

        private string WriteAndReadLenString(string val){
            var ms = new MemoryStream();
            var bs = new BsonWriter(ms, new BsonDocumentDescriptor());
            var w = new BinaryWriter(ms);
            var byteCount = bs.CalculateSize(val, false);
            w.Write(byteCount);
            bs.Write(val, false);
            ms.Seek(0, SeekOrigin.Begin);
            var reader = new BsonReader(ms, new BsonDocumentBuilder());
            return reader.ReadLengthString();
        }

[Test]
        public void TestReadDocWithDocs(){
            // Document doc = new Document().Append("a", new Document().Append("b", new Document().Append("c",new Document())));
            // Console.WriteLine(ConvertDocToHex(doc));
            var buf = HexToBytes("1D000000036100150000000362000D0000000363000500000000000000");
            var ms = new MemoryStream(buf);
            var reader = new BsonReader(ms, new BsonDocumentBuilder());

            var doc = (Document)reader.ReadObject();
            Assert.IsNotNull(doc, "Document was null");
            Assert.AreEqual(buf.Length, reader.Position);
            Assert.IsTrue(doc.ContainsKey("a"));
        }

[Test]
        public void TestReadEmptyDocument(){
            var buf = HexToBytes("0500000000");
            var ms = new MemoryStream(buf);
            var reader = new BsonReader(ms, new BsonDocumentBuilder());

            var doc = (Document)reader.ReadObject();

            Assert.IsNotNull(doc);
        }

[Test]
        public void TestReadLenString(){
            const string expected = "test";
            Assert.AreEqual(expected, WriteAndReadLenString(expected));
        }

[Test]
        public void TestReadLenStringLong(){
            var sb = new StringBuilder();
            sb.Append('t', 150);
            var expected = sb.ToString();
            Assert.AreEqual(expected, WriteAndReadLenString(expected));
        }

[Test]
        public void TestReadLenStringShortTripleByte(){
            var sb = new StringBuilder();
            //sb.Append('1',127); //first char of euro at the end of the boundry.
            //sb.Append(euro, 5);
            //sb.Append('1',128);
            sb.Append(euro);

            var expected = sb.ToString();
            Assert.AreEqual(expected, WriteAndReadLenString(expected));
        }

[Test]
        public void TestReadLenStringTripleByteCharBufferBoundry0(){
            var sb = new StringBuilder();
            sb.Append('1', 127); //first char of euro at the end of the boundry.
            sb.Append(euro, 5);
            sb.Append('1', 128);
            sb.Append(euro);

            var expected = sb.ToString();
            Assert.AreEqual(expected, WriteAndReadLenString(expected));
        }

[Test]
        public void TestReadLenStringTripleByteCharBufferBoundry1(){
            var sb = new StringBuilder();
            sb.Append('1', 126);
            sb.Append(euro, 5); //middle char of euro at the end of the boundry.
            sb.Append('1', 128);
            sb.Append(euro);

            var expected = sb.ToString();
            Assert.AreEqual(expected, WriteAndReadLenString(expected));
        }

[Test]
        public void TestReadLenStringTripleByteCharBufferBoundry2(){
            var sb = new StringBuilder();
            sb.Append('1', 125);
            sb.Append(euro, 5); //last char of the eruo at the end of the boundry.
            sb.Append('1', 128);
            sb.Append(euro);

            var expected = sb.ToString();
            Assert.AreEqual(expected, WriteAndReadLenString(expected));
        }

[Test]
        public void TestReadLenStringTripleByteCharOne(){
            var sb = new StringBuilder();
            sb.Append(euro, 1); //Just one triple byte char in the string.

            var expected = sb.ToString();
            Assert.AreEqual(expected, WriteAndReadLenString(expected));
        }

[Test]
        public void TestReadLenStringValue(){
            const string expected = "test";

            Assert.AreEqual(expected, WriteAndReadLenString(expected));
        }

[Test]
        public void TestReadMultiElementDocument(){
            var buf = HexToBytes("2D000000075F6964004A753AD8FAC16EA58B290351016100000000000000F03F02620005000000746573740000");
            var ms = new MemoryStream(buf);
            var reader = new BsonReader(ms, new BsonDocumentBuilder());

            var doc = (Document)reader.ReadObject();

            Assert.IsNotNull(doc, "Document was null");
            Assert.IsTrue(doc.ContainsKey("_id"));
            Assert.IsTrue(doc.ContainsKey("a"));
            Assert.IsTrue(doc.ContainsKey("b"));
            Assert.AreEqual("4a753ad8fac16ea58b290351", (doc["_id"]).ToString());
            Assert.AreEqual(1, Convert.ToInt32(doc["a"]));
            Assert.AreEqual("test", doc["b"]);
        }

[Test]
        public void TestReadSimpleDocument(){
            var buf = HexToBytes("1400000002746573740005000000746573740000");
            var ms = new MemoryStream(buf);
            var reader = new BsonReader(ms, new BsonDocumentBuilder());

            var doc = reader.Read();

            Assert.IsNotNull(doc, "Document was null");
            Assert.IsTrue(doc.ContainsKey("test"));
            Assert.AreEqual("test", doc["test"]);
        }

[Test]
        public void TestReadString(){
            var buf = HexToBytes("7465737400");
            var ms = new MemoryStream(buf);
            var reader = new BsonReader(ms, new BsonDocumentBuilder());

            var s = reader.ReadString();
            Assert.AreEqual("test", s);
            Assert.AreEqual(4, Encoding.UTF8.GetByteCount(s));
        }
[Test]
        public void TestReadBigDocument(){
            MemoryStream ms = new MemoryStream();
            var writer = new BsonWriter(ms, new BsonDocumentDescriptor());
            
            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)
                .Append("symbol", new MongoSymbol("symbol"))
            ;
            writer.WriteObject(expected);
            writer.Flush();
            ms.Seek(0,SeekOrigin.Begin);
            
            BsonReader reader = new BsonReader(ms, new BsonDocumentBuilder());
            Document doc = reader.Read();
            
            Assert.IsNotNull(doc);
        }

[Test]
        public void TestReadStringBreakDblByteCharOverBuffer(){
            var sb = new StringBuilder();
            sb.Append('1', 127);
            sb.Append(pound); //will break the pound symbol over the buffer boundry.
            //sb.Append("1");

            var expected = sb.ToString();
            Assert.AreEqual(expected, WriteAndReadString(expected));
        }

[Test]
        public void TestReadStringDblByteCharOnEndOfBufferBoundry(){
            var sb = new StringBuilder();
            sb.Append(pound, 66); //puts a pound symbol at the end of the buffer boundry but not broken.
            var expected = sb.ToString();
            Assert.AreEqual(expected, WriteAndReadString(expected));
        }

[Test]
        public void TestReadStringLong(){
            var sb = new StringBuilder();
            sb.Append('t', 256);
            var expected = sb.ToString();
            Assert.AreEqual(expected, WriteAndReadString(expected));
        }

[Test]
        public void TestReadStringTripleByteCharBufferBoundry(){
            var sb = new StringBuilder();
            sb.Append("12");
            sb.Append(euro, 66); //will break the euro symbol over the buffer boundry.

            var expected = sb.ToString();

            Assert.AreEqual(expected, WriteAndReadString(expected));
        }

[Test]
        public void TestReadStringWithUkPound(){
            const string expected = "1234£56";
            Assert.AreEqual(expected, WriteAndReadString(expected));
        }

[Test]
        public void TestReadUtcTimeByDefault(){
            var document = Deserialize("EwAAAAl0aW1lAADJU+klAQAAAA==");

            var dateTime = new DateTime(2010, 1, 1, 10, 0, 0, DateTimeKind.Utc);
            
            Assert.AreEqual(dateTime, document["time"]);
        }

[Test]
        public void TestReadUtcTimeToLocalTime(){
            var settings = new BsonReaderSettings {ReadLocalTime = true};

            var document = Deserialize("EwAAAAl0aW1lAADJU+klAQAAAA==", settings);

            var localtzoffset =TimeZoneInfo.Local.BaseUtcOffset.Hours - 1; //gmt offset the local date was saved in along with the local offset.

            var dateTime = new DateTime(2010, 1, 1, 11, 0, 0, DateTimeKind.Local).AddHours(localtzoffset);
            Assert.AreEqual(dateTime, document["time"]);
        }

[Test]
        public void TestCanReadNagativeDates()
        {
            const string bson = "EwAAAAlkYXRlAIBFaoO2////AA==";

            var document = Deserialize(bson);

            Assert.AreEqual(new DateTime(1960,1,1).ToUniversalTime(),document["date"]);
        }
    }
}
Something went wrong with that request. Please try again.