|
1 |
| -/* Copyright 2010-2015 MongoDB Inc. |
| 1 | +/* Copyright 2010-2016 MongoDB Inc. |
2 | 2 | *
|
3 | 3 | * Licensed under the Apache License, Version 2.0 (the "License");
|
4 | 4 | * you may not use this file except in compliance with the License.
|
|
17 | 17 | using System.Collections.Generic;
|
18 | 18 | using System.IO;
|
19 | 19 | using System.Linq;
|
20 |
| -using MongoDB.Bson; |
| 20 | +using FluentAssertions; |
21 | 21 | using MongoDB.Bson.IO;
|
22 | 22 | using MongoDB.Bson.Serialization;
|
23 |
| -using FluentAssertions; |
24 | 23 | using NUnit.Framework;
|
25 | 24 |
|
26 | 25 | namespace MongoDB.Bson.Tests.IO
|
@@ -58,6 +57,151 @@ public void BsonBinaryReader_should_support_reading_multiple_documents(
|
58 | 57 | }
|
59 | 58 | }
|
60 | 59 |
|
| 60 | + [Test] |
| 61 | + public void ReadBsonType_should_throw_when_bson_type_is_invalid_for_a() |
| 62 | + { |
| 63 | + var bytes = new byte[] { 0, 0, 0, 0, 0xf0, 97, 0 }; |
| 64 | + var expectedMessage = $"Detected unknown BSON type \"\\xf0\" for fieldname \"a\". Are you using the latest driver version?"; |
| 65 | + |
| 66 | + using (var memoryStream = new MemoryStream(bytes)) |
| 67 | + using (var subject = new BsonBinaryReader(memoryStream)) |
| 68 | + { |
| 69 | + subject.ReadStartDocument(); |
| 70 | + |
| 71 | + Action action = () => subject.ReadBsonType(); |
| 72 | + |
| 73 | + action.ShouldThrow<FormatException>().WithMessage(expectedMessage); |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + [Test] |
| 78 | + public void ReadBsonType_should_throw_when_bson_type_is_invalid_for_a_0() |
| 79 | + { |
| 80 | + var bytes = new byte[] { 0, 0, 0, 0, 4, 97, 0, 0, 0, 0, 0, 0xf0, 48, 0 }; |
| 81 | + var expectedMessage = $"Detected unknown BSON type \"\\xf0\" for fieldname \"a.0\". Are you using the latest driver version?"; |
| 82 | + |
| 83 | + using (var memoryStream = new MemoryStream(bytes)) |
| 84 | + using (var subject = new BsonBinaryReader(memoryStream)) |
| 85 | + { |
| 86 | + subject.ReadStartDocument(); |
| 87 | + subject.ReadBsonType(); |
| 88 | + subject.ReadName(); |
| 89 | + subject.ReadStartArray(); |
| 90 | + |
| 91 | + Action action = () => subject.ReadBsonType(); |
| 92 | + |
| 93 | + action.ShouldThrow<FormatException>().WithMessage(expectedMessage); |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + [Test] |
| 98 | + public void ReadBsonType_should_throw_when_bson_type_is_invalid_for_a_0_b() |
| 99 | + { |
| 100 | + var bytes = new byte[] { 0, 0, 0, 0, 4, 97, 0, 0, 0, 0, 0, 3, 48, 0, 0, 0, 0, 0, 0xf0, 98, 0 }; |
| 101 | + var expectedMessage = $"Detected unknown BSON type \"\\xf0\" for fieldname \"a.0.b\". Are you using the latest driver version?"; |
| 102 | + |
| 103 | + using (var memoryStream = new MemoryStream(bytes)) |
| 104 | + using (var subject = new BsonBinaryReader(memoryStream)) |
| 105 | + { |
| 106 | + subject.ReadStartDocument(); |
| 107 | + subject.ReadBsonType(); |
| 108 | + subject.ReadName(); |
| 109 | + subject.ReadStartArray(); |
| 110 | + subject.ReadBsonType(); |
| 111 | + subject.ReadStartDocument(); |
| 112 | + |
| 113 | + Action action = () => subject.ReadBsonType(); |
| 114 | + |
| 115 | + action.ShouldThrow<FormatException>().WithMessage(expectedMessage); |
| 116 | + } |
| 117 | + } |
| 118 | + |
| 119 | + [Test] |
| 120 | + public void ReadBsonType_should_throw_when_bson_type_is_invalid_for_a_1() |
| 121 | + { |
| 122 | + var bytes = new byte[] { 0, 0, 0, 0, 4, 97, 0, 0, 0, 0, 0, 0x10, 48, 0, 0, 0, 0, 0, 0xf0, 49, 0 }; |
| 123 | + var expectedMessage = $"Detected unknown BSON type \"\\xf0\" for fieldname \"a.1\". Are you using the latest driver version?"; |
| 124 | + |
| 125 | + using (var memoryStream = new MemoryStream(bytes)) |
| 126 | + using (var subject = new BsonBinaryReader(memoryStream)) |
| 127 | + { |
| 128 | + subject.ReadStartDocument(); |
| 129 | + subject.ReadBsonType(); |
| 130 | + subject.ReadName(); |
| 131 | + subject.ReadStartArray(); |
| 132 | + subject.ReadBsonType(); |
| 133 | + subject.ReadInt32(); |
| 134 | + |
| 135 | + Action action = () => subject.ReadBsonType(); |
| 136 | + |
| 137 | + action.ShouldThrow<FormatException>().WithMessage(expectedMessage); |
| 138 | + } |
| 139 | + } |
| 140 | + |
| 141 | + [Test] |
| 142 | + public void ReadBsonType_should_throw_when_bson_type_is_invalid_for_a_1_b() |
| 143 | + { |
| 144 | + var bytes = new byte[] { 0, 0, 0, 0, 4, 97, 0, 0, 0, 0, 0, 0x10, 48, 0, 0, 0, 0, 0, 3, 49, 0, 0, 0, 0, 0, 0xf0, 98, 0 }; |
| 145 | + var expectedMessage = $"Detected unknown BSON type \"\\xf0\" for fieldname \"a.1.b\". Are you using the latest driver version?"; |
| 146 | + |
| 147 | + using (var memoryStream = new MemoryStream(bytes)) |
| 148 | + using (var subject = new BsonBinaryReader(memoryStream)) |
| 149 | + { |
| 150 | + subject.ReadStartDocument(); |
| 151 | + subject.ReadBsonType(); |
| 152 | + subject.ReadName(); |
| 153 | + subject.ReadStartArray(); |
| 154 | + subject.ReadBsonType(); |
| 155 | + subject.ReadInt32(); |
| 156 | + subject.ReadBsonType(); |
| 157 | + subject.ReadStartDocument(); |
| 158 | + |
| 159 | + Action action = () => subject.ReadBsonType(); |
| 160 | + |
| 161 | + action.ShouldThrow<FormatException>().WithMessage(expectedMessage); |
| 162 | + } |
| 163 | + } |
| 164 | + |
| 165 | + [Test] |
| 166 | + public void ReadBsonType_should_throw_when_bson_type_is_invalid_for_a_b() |
| 167 | + { |
| 168 | + var bytes = new byte[] { 0, 0, 0, 0, 3, 97, 0, 0, 0, 0, 0, 0xf0, 98, 0 }; |
| 169 | + var expectedMessage = $"Detected unknown BSON type \"\\xf0\" for fieldname \"a.b\". Are you using the latest driver version?"; |
| 170 | + |
| 171 | + using (var memoryStream = new MemoryStream(bytes)) |
| 172 | + using (var subject = new BsonBinaryReader(memoryStream)) |
| 173 | + { |
| 174 | + subject.ReadStartDocument(); |
| 175 | + subject.ReadBsonType(); |
| 176 | + subject.ReadName(); |
| 177 | + subject.ReadStartDocument(); |
| 178 | + |
| 179 | + Action action = () => subject.ReadBsonType(); |
| 180 | + |
| 181 | + action.ShouldThrow<FormatException>().WithMessage(expectedMessage); |
| 182 | + } |
| 183 | + } |
| 184 | + |
| 185 | + [Test] |
| 186 | + public void ReadBsonType_should_throw_when_bson_type_is_invalid_for_b() |
| 187 | + { |
| 188 | + var bytes = new byte[] { 0, 0, 0, 0, 0x10, 97, 0, 0, 0, 0, 0, 0xf0, 98, 0 }; |
| 189 | + var expectedMessage = $"Detected unknown BSON type \"\\xf0\" for fieldname \"b\". Are you using the latest driver version?"; |
| 190 | + |
| 191 | + using (var memoryStream = new MemoryStream(bytes)) |
| 192 | + using (var subject = new BsonBinaryReader(memoryStream)) |
| 193 | + { |
| 194 | + subject.ReadStartDocument(); |
| 195 | + subject.ReadBsonType(); |
| 196 | + subject.ReadName(); |
| 197 | + subject.ReadInt32(); |
| 198 | + |
| 199 | + Action action = () => subject.ReadBsonType(); |
| 200 | + |
| 201 | + action.ShouldThrow<FormatException>().WithMessage(expectedMessage); |
| 202 | + } |
| 203 | + } |
| 204 | + |
61 | 205 | [Test]
|
62 | 206 | public void TestHelloWorld()
|
63 | 207 | {
|
|
0 commit comments