|
| 1 | +/* Copyright 2010-2012 10gen Inc. |
| 2 | +* |
| 3 | +* Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +* you may not use this file except in compliance with the License. |
| 5 | +* You may obtain a copy of the License at |
| 6 | +* |
| 7 | +* http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +* |
| 9 | +* Unless required by applicable law or agreed to in writing, software |
| 10 | +* distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +* See the License for the specific language governing permissions and |
| 13 | +* limitations under the License. |
| 14 | +*/ |
| 15 | + |
| 16 | +using System; |
| 17 | +using System.Collections.Generic; |
| 18 | +using System.IO; |
| 19 | +using System.Linq; |
| 20 | +using System.Text; |
| 21 | +using NUnit.Framework; |
| 22 | + |
| 23 | +using MongoDB.Bson; |
| 24 | +using MongoDB.Bson.IO; |
| 25 | +using MongoDB.Bson.Serialization; |
| 26 | + |
| 27 | +namespace MongoDB.BsonUnitTests.IO |
| 28 | +{ |
| 29 | + [TestFixture] |
| 30 | + public class BsonBufferTests |
| 31 | + { |
| 32 | + [Test] |
| 33 | + public void TestReadCStringEmpty() |
| 34 | + { |
| 35 | + var bytes = new byte[] { 8, 0, 0, 0, (byte)BsonType.Boolean, 0, 0, 0 }; |
| 36 | + Assert.AreEqual(8, bytes.Length); |
| 37 | + var document = BsonSerializer.Deserialize<BsonDocument>(bytes); |
| 38 | + Assert.AreEqual("", document.GetElement(0).Name); |
| 39 | + } |
| 40 | + |
| 41 | + [Test] |
| 42 | + public void TestReadCStringOneCharacter() |
| 43 | + { |
| 44 | + var bytes = new byte[] { 9, 0, 0, 0, (byte)BsonType.Boolean, (byte)'b', 0, 0, 0 }; |
| 45 | + Assert.AreEqual(9, bytes.Length); |
| 46 | + var document = BsonSerializer.Deserialize<BsonDocument>(bytes); |
| 47 | + Assert.AreEqual("b", document.GetElement(0).Name); |
| 48 | + } |
| 49 | + |
| 50 | + [Test] |
| 51 | + public void TestReadCStringOneCharacterDecoderException() |
| 52 | + { |
| 53 | + var bytes = new byte[] { 9, 0, 0, 0, (byte)BsonType.Boolean, 0x80, 0, 0, 0 }; |
| 54 | + Assert.AreEqual(9, bytes.Length); |
| 55 | + var ex = Assert.Throws<DecoderFallbackException>(() => { BsonSerializer.Deserialize<BsonDocument>(bytes); }); |
| 56 | + } |
| 57 | + |
| 58 | + [Test] |
| 59 | + public void TestReadCStringTwoCharacters() |
| 60 | + { |
| 61 | + var bytes = new byte[] { 10, 0, 0, 0, (byte)BsonType.Boolean, (byte)'b', (byte)'b', 0, 0, 0 }; |
| 62 | + Assert.AreEqual(10, bytes.Length); |
| 63 | + var document = BsonSerializer.Deserialize<BsonDocument>(bytes); |
| 64 | + Assert.AreEqual("bb", document.GetElement(0).Name); |
| 65 | + } |
| 66 | + |
| 67 | + [Test] |
| 68 | + public void TestReadCStringTwoCharactersDecoderException() |
| 69 | + { |
| 70 | + var bytes = new byte[] { 10, 0, 0, 0, (byte)BsonType.Boolean, (byte)'b', 0x80, 0, 0, 0 }; |
| 71 | + Assert.AreEqual(10, bytes.Length); |
| 72 | + var ex = Assert.Throws<DecoderFallbackException>(() => { BsonSerializer.Deserialize<BsonDocument>(bytes); }); |
| 73 | + } |
| 74 | + |
| 75 | + [Test] |
| 76 | + public void TestReadStringEmpty() |
| 77 | + { |
| 78 | + var bytes = new byte[] { 13, 0, 0, 0, (byte)BsonType.String, (byte)'s', 0, 1, 0, 0, 0, 0, 0 }; |
| 79 | + Assert.AreEqual(13, bytes.Length); |
| 80 | + var document = BsonSerializer.Deserialize<BsonDocument>(bytes); |
| 81 | + Assert.AreEqual("", document["s"].AsString); |
| 82 | + } |
| 83 | + |
| 84 | + [Test] |
| 85 | + public void TestReadStringInvalidLength() |
| 86 | + { |
| 87 | + var bytes = new byte[] { 13, 0, 0, 0, (byte)BsonType.String, (byte)'s', 0, 0, 0, 0, 0, 0, 0 }; |
| 88 | + Assert.AreEqual(13, bytes.Length); |
| 89 | + var ex = Assert.Throws<FileFormatException>(() => { BsonSerializer.Deserialize<BsonDocument>(bytes); }); |
| 90 | + Assert.AreEqual("Invalid string length: 0 (the length includes the null terminator so it must be greater than or equal to 1).", ex.Message); |
| 91 | + } |
| 92 | + |
| 93 | + [Test] |
| 94 | + public void TestReadStringMissingNullTerminator() |
| 95 | + { |
| 96 | + var bytes = new byte[] { 13, 0, 0, 0, (byte)BsonType.String, (byte)'s', 0, 1, 0, 0, 0, 123, 0 }; |
| 97 | + Assert.AreEqual(13, bytes.Length); |
| 98 | + var ex = Assert.Throws<FileFormatException>(() => { BsonSerializer.Deserialize<BsonDocument>(bytes); }); |
| 99 | + Assert.AreEqual("String is missing null terminator.", ex.Message); |
| 100 | + } |
| 101 | + |
| 102 | + [Test] |
| 103 | + public void TestReadStringOneCharacter() |
| 104 | + { |
| 105 | + var bytes = new byte[] { 14, 0, 0, 0, (byte)BsonType.String, (byte)'s', 0, 2, 0, 0, 0, (byte)'x', 0, 0 }; |
| 106 | + Assert.AreEqual(14, bytes.Length); |
| 107 | + var document = BsonSerializer.Deserialize<BsonDocument>(bytes); |
| 108 | + Assert.AreEqual("x", document["s"].AsString); |
| 109 | + } |
| 110 | + |
| 111 | + [Test] |
| 112 | + public void TestReadStringOneCharacterDecoderException() |
| 113 | + { |
| 114 | + var bytes = new byte[] { 14, 0, 0, 0, (byte)BsonType.String, (byte)'s', 0, 2, 0, 0, 0, 0x80, 0, 0 }; |
| 115 | + Assert.AreEqual(14, bytes.Length); |
| 116 | + var ex = Assert.Throws<DecoderFallbackException>(() => { BsonSerializer.Deserialize<BsonDocument>(bytes); }); |
| 117 | + } |
| 118 | + |
| 119 | + [Test] |
| 120 | + public void TestReadStringTwoCharacters() |
| 121 | + { |
| 122 | + var bytes = new byte[] { 15, 0, 0, 0, (byte)BsonType.String, (byte)'s', 0, 3, 0, 0, 0, (byte)'x', (byte)'y', 0, 0 }; |
| 123 | + Assert.AreEqual(15, bytes.Length); |
| 124 | + var document = BsonSerializer.Deserialize<BsonDocument>(bytes); |
| 125 | + Assert.AreEqual("xy", document["s"].AsString); |
| 126 | + } |
| 127 | + |
| 128 | + [Test] |
| 129 | + public void TestReadStringTwoCharactersDecoderException() |
| 130 | + { |
| 131 | + var bytes = new byte[] { 15, 0, 0, 0, (byte)BsonType.String, (byte)'s', 0, 3, 0, 0, 0, (byte)'x', 0x80, 0, 0 }; |
| 132 | + Assert.AreEqual(15, bytes.Length); |
| 133 | + var ex = Assert.Throws<DecoderFallbackException>(() => { BsonSerializer.Deserialize<BsonDocument>(bytes); }); |
| 134 | + } |
| 135 | + } |
| 136 | +} |
0 commit comments