PageRenderTime 60ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/tests/MongoDB.Bson.Tests/IO/JsonReaderTests.cs

http://github.com/mongodb/mongo-csharp-driver
C# | 1647 lines | 1504 code | 124 blank | 19 comment | 11 complexity | 9ab72abac967c362318b98f93e0f67a3 MD5 | raw file
Possible License(s): Apache-2.0
  1. /* Copyright 2010-present MongoDB 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. using System;
  16. using System.Collections.Generic;
  17. using System.IO;
  18. using System.Linq;
  19. using System.Text;
  20. using FluentAssertions;
  21. using MongoDB.Bson.IO;
  22. using MongoDB.Bson.Serialization;
  23. using MongoDB.Bson.TestHelpers;
  24. using MongoDB.Bson.TestHelpers.XunitExtensions;
  25. using Xunit;
  26. namespace MongoDB.Bson.Tests.IO
  27. {
  28. public class JsonReaderTests
  29. {
  30. private IBsonReader _bsonReader;
  31. [Theory]
  32. [ParameterAttributeData]
  33. public void JsonReader_should_support_reading_multiple_documents(
  34. [Range(0, 3)]
  35. int numberOfDocuments)
  36. {
  37. var document = new BsonDocument("x", 1);
  38. var json = document.ToJson();
  39. var input = Enumerable.Repeat(json, numberOfDocuments).Aggregate("", (a, j) => a + j);
  40. var expectedResult = Enumerable.Repeat(document, numberOfDocuments);
  41. using (var jsonReader = new JsonReader(input))
  42. {
  43. var result = new List<BsonDocument>();
  44. jsonReader.State.Should().Be(BsonReaderState.Initial);
  45. while (!jsonReader.IsAtEndOfFile())
  46. {
  47. jsonReader.ReadStartDocument();
  48. var name = jsonReader.ReadName();
  49. var value = jsonReader.ReadInt32();
  50. jsonReader.ReadEndDocument();
  51. jsonReader.State.Should().Be(BsonReaderState.Initial);
  52. var resultDocument = new BsonDocument(name, value);
  53. result.Add(resultDocument);
  54. }
  55. result.Should().Equal(expectedResult);
  56. }
  57. }
  58. [Fact]
  59. public void TestArrayEmpty()
  60. {
  61. var json = "[]";
  62. using (_bsonReader = new JsonReader(json))
  63. {
  64. Assert.Equal(BsonType.Array, _bsonReader.ReadBsonType());
  65. _bsonReader.ReadStartArray();
  66. Assert.Equal(BsonType.EndOfDocument, _bsonReader.ReadBsonType());
  67. _bsonReader.ReadEndArray();
  68. Assert.Equal(BsonReaderState.Initial, _bsonReader.State);
  69. Assert.True(_bsonReader.IsAtEndOfFile());
  70. }
  71. Assert.Equal(json, BsonSerializer.Deserialize<BsonArray>(json).ToJson());
  72. }
  73. [Fact]
  74. public void TestArrayOneElement()
  75. {
  76. var json = "[1]";
  77. using (_bsonReader = new JsonReader(json))
  78. {
  79. Assert.Equal(BsonType.Array, _bsonReader.ReadBsonType());
  80. _bsonReader.ReadStartArray();
  81. Assert.Equal(BsonType.Int32, _bsonReader.ReadBsonType());
  82. Assert.Equal(1, _bsonReader.ReadInt32());
  83. Assert.Equal(BsonType.EndOfDocument, _bsonReader.ReadBsonType());
  84. _bsonReader.ReadEndArray();
  85. Assert.Equal(BsonReaderState.Initial, _bsonReader.State);
  86. Assert.True(_bsonReader.IsAtEndOfFile());
  87. }
  88. Assert.Equal(json, BsonSerializer.Deserialize<BsonArray>(json).ToJson());
  89. }
  90. [Fact]
  91. public void TestArrayTwoElements()
  92. {
  93. var json = "[1, 2]";
  94. using (_bsonReader = new JsonReader(json))
  95. {
  96. Assert.Equal(BsonType.Array, _bsonReader.ReadBsonType());
  97. _bsonReader.ReadStartArray();
  98. Assert.Equal(BsonType.Int32, _bsonReader.ReadBsonType());
  99. Assert.Equal(1, _bsonReader.ReadInt32());
  100. Assert.Equal(BsonType.Int32, _bsonReader.ReadBsonType());
  101. Assert.Equal(2, _bsonReader.ReadInt32());
  102. Assert.Equal(BsonType.EndOfDocument, _bsonReader.ReadBsonType());
  103. _bsonReader.ReadEndArray();
  104. Assert.Equal(BsonReaderState.Initial, _bsonReader.State);
  105. Assert.True(_bsonReader.IsAtEndOfFile());
  106. }
  107. Assert.Equal(json, BsonSerializer.Deserialize<BsonArray>(json).ToJson());
  108. }
  109. [Theory]
  110. [InlineData("{ $binary : \"AQ==\", $type : 0 }", new byte[] { 1 }, BsonBinarySubType.Binary)]
  111. [InlineData("{ $binary : \"AQ==\", $type : 128 }", new byte[] { 1 }, BsonBinarySubType.UserDefined)]
  112. [InlineData("{ $binary : \"AQ==\", $type : \"0\" }", new byte[] { 1 }, BsonBinarySubType.Binary)]
  113. [InlineData("{ $binary : \"AQ==\", $type : \"00\" }", new byte[] { 1 }, BsonBinarySubType.Binary)]
  114. [InlineData("{ $binary : \"AQ==\", $type : \"80\" }", new byte[] { 1 }, BsonBinarySubType.UserDefined)]
  115. [InlineData("{ $binary : { base64 : \"AQ==\", subType : \"0\" } }", new byte[] { 1 }, BsonBinarySubType.Binary)]
  116. [InlineData("{ $binary : { base64 : \"AQ==\", subType : \"00\" } }", new byte[] { 1 }, BsonBinarySubType.Binary)]
  117. [InlineData("{ $binary : { base64 : \"AQ==\", subType : \"80\" } }", new byte[] { 1 }, BsonBinarySubType.UserDefined)]
  118. [InlineData("{ $binary : { subType : \"0\", base64 : \"AQ==\" } }", new byte[] { 1 }, BsonBinarySubType.Binary)]
  119. [InlineData("{ $binary : { subType : \"00\", base64 : \"AQ==\" } }", new byte[] { 1 }, BsonBinarySubType.Binary)]
  120. [InlineData("{ $binary : { subType : \"80\", base64 : \"AQ==\" } }", new byte[] { 1 }, BsonBinarySubType.UserDefined)]
  121. [InlineData("BinData(0, \"AQ==\")", new byte[] { 1 }, BsonBinarySubType.Binary)]
  122. [InlineData("BinData(128, \"AQ==\")", new byte[] { 1 }, BsonBinarySubType.UserDefined)]
  123. [InlineData("HexData(0, \"01\")", new byte[] { 1 }, BsonBinarySubType.Binary)]
  124. [InlineData("HexData(128, \"01\")", new byte[] { 1 }, BsonBinarySubType.UserDefined)]
  125. public void TestBinaryData(string json, byte[] expectedBytes, BsonBinarySubType expectedSubType)
  126. {
  127. using (var reader = new JsonReader(json))
  128. {
  129. var result = reader.ReadBinaryData();
  130. result.Should().Be(new BsonBinaryData(expectedBytes, expectedSubType));
  131. reader.State.Should().Be(BsonReaderState.Initial);
  132. reader.IsAtEndOfFile().Should().BeTrue();
  133. }
  134. }
  135. [Theory]
  136. [InlineData("{ $binary")]
  137. [InlineData("{ $binary :")]
  138. [InlineData("{ $binary : \"AQ==\"")]
  139. [InlineData("{ $binary : \"AQ==\",")]
  140. [InlineData("{ $binary : \"AQ==\", $type")]
  141. [InlineData("{ $binary : \"AQ==\", $type :")]
  142. [InlineData("{ $binary : \"AQ==\", $type : 0")]
  143. [InlineData("{ $binary : {")]
  144. [InlineData("{ $binary : { base64")]
  145. [InlineData("{ $binary : { base64 :")]
  146. [InlineData("{ $binary : { base64 : \"AQ==\"")]
  147. [InlineData("{ $binary : { base64 : \"AQ==\",")]
  148. [InlineData("{ $binary : { base64 : \"AQ==\", subType")]
  149. [InlineData("{ $binary : { base64 : \"AQ==\", subType :")]
  150. [InlineData("{ $binary : { base64 : \"AQ==\", subType : \"0\"")]
  151. [InlineData("{ $binary : { base64 : \"AQ==\", subType : \"0\" }")]
  152. [InlineData("{ $binary : { subType")]
  153. [InlineData("{ $binary : { subType :")]
  154. [InlineData("{ $binary : { subType : \"0\"")]
  155. [InlineData("{ $binary : { subType : \"0\",")]
  156. [InlineData("{ $binary : { subType : \"0\", base64")]
  157. [InlineData("{ $binary : { subType : \"0\", base64 :")]
  158. [InlineData("{ $binary : { subType : \"0\", base64 : \"AQ==\"")]
  159. [InlineData("{ $binary : { subType : \"0\", base64 : \"AQ==\" }")]
  160. [InlineData("BinData")]
  161. [InlineData("BinData(")]
  162. [InlineData("BinData(0")]
  163. [InlineData("BinData(0,")]
  164. [InlineData("BinData(0, \"AQ==\"")]
  165. [InlineData("HexData")]
  166. [InlineData("HexData(")]
  167. [InlineData("HexData(0")]
  168. [InlineData("HexData(0,")]
  169. [InlineData("HexData(0, \"01\"")]
  170. public void TestBinaryDataEndOfFile(string json)
  171. {
  172. using (var reader = new JsonReader(json))
  173. {
  174. var exception = Record.Exception(() => reader.ReadBinaryData());
  175. exception.Should().BeOfType<FormatException>();
  176. }
  177. }
  178. [Theory]
  179. [InlineData("{ $binary [")]
  180. [InlineData("{ $binary : [")]
  181. [InlineData("{ $binary : \"AQ==\" [")]
  182. [InlineData("{ $binary : \"AQ==\", [")]
  183. [InlineData("{ $binary : \"AQ==\", $type [")]
  184. [InlineData("{ $binary : \"AQ==\", $type : [")]
  185. [InlineData("{ $binary : \"AQ==\", $type : 0 [")]
  186. [InlineData("{ $binary : { [")]
  187. [InlineData("{ $binary : { base64 [")]
  188. [InlineData("{ $binary : { base64 : [")]
  189. [InlineData("{ $binary : { base64 : \"AQ==\" [")]
  190. [InlineData("{ $binary : { base64 : \"AQ==\", [")]
  191. [InlineData("{ $binary : { base64 : \"AQ==\", subType [")]
  192. [InlineData("{ $binary : { base64 : \"AQ==\", subType : [")]
  193. [InlineData("{ $binary : { base64 : \"AQ==\", subType : \"\" [")]
  194. [InlineData("{ $binary : { base64 : \"AQ==\", subType : \"0\" [")]
  195. [InlineData("{ $binary : { base64 : \"AQ==\", subType : \"0\" } [")]
  196. [InlineData("{ $binary : { subType [")]
  197. [InlineData("{ $binary : { subType : [")]
  198. [InlineData("{ $binary : { subType : \"\" [")]
  199. [InlineData("{ $binary : { subType : \"0\" [")]
  200. [InlineData("{ $binary : { subType : \"0\", [")]
  201. [InlineData("{ $binary : { subType : \"0\", base64 [")]
  202. [InlineData("{ $binary : { subType : \"0\", base64 : [")]
  203. [InlineData("{ $binary : { subType : \"0\", base64 : \"AQ==\" [")]
  204. [InlineData("{ $binary : { subType : \"0\", base64 : \"AQ==\" } [")]
  205. [InlineData("BinData [")]
  206. [InlineData("BinData( [")]
  207. [InlineData("BinData(0 [")]
  208. [InlineData("BinData(0, [")]
  209. [InlineData("BinData(0, \"AQ==\" [")]
  210. [InlineData("HexData [")]
  211. [InlineData("HexData( [")]
  212. [InlineData("HexData(0 [")]
  213. [InlineData("HexData(0, [")]
  214. [InlineData("HexData(0, \"01\" [")]
  215. public void TestBinaryDataInvalidToken(string json)
  216. {
  217. using (var reader = new JsonReader(json))
  218. {
  219. var exception = Record.Exception(() => reader.ReadBinaryData());
  220. exception.Should().BeOfType<FormatException>();
  221. }
  222. }
  223. [Fact]
  224. public void TestBookmark()
  225. {
  226. var json = "{ \"x\" : 1, \"y\" : 2 }";
  227. using (_bsonReader = new JsonReader(json))
  228. {
  229. // do everything twice returning to bookmark in between
  230. var bookmark = _bsonReader.GetBookmark();
  231. Assert.Equal(BsonType.Document, _bsonReader.ReadBsonType());
  232. _bsonReader.ReturnToBookmark(bookmark);
  233. Assert.Equal(BsonType.Document, _bsonReader.ReadBsonType());
  234. bookmark = _bsonReader.GetBookmark();
  235. _bsonReader.ReadStartDocument();
  236. _bsonReader.ReturnToBookmark(bookmark);
  237. _bsonReader.ReadStartDocument();
  238. bookmark = _bsonReader.GetBookmark();
  239. Assert.Equal(BsonType.Int32, _bsonReader.ReadBsonType());
  240. _bsonReader.ReturnToBookmark(bookmark);
  241. Assert.Equal(BsonType.Int32, _bsonReader.ReadBsonType());
  242. bookmark = _bsonReader.GetBookmark();
  243. Assert.Equal("x", _bsonReader.ReadName());
  244. _bsonReader.ReturnToBookmark(bookmark);
  245. Assert.Equal("x", _bsonReader.ReadName());
  246. bookmark = _bsonReader.GetBookmark();
  247. Assert.Equal(1, _bsonReader.ReadInt32());
  248. _bsonReader.ReturnToBookmark(bookmark);
  249. Assert.Equal(1, _bsonReader.ReadInt32());
  250. bookmark = _bsonReader.GetBookmark();
  251. Assert.Equal(BsonType.Int32, _bsonReader.ReadBsonType());
  252. _bsonReader.ReturnToBookmark(bookmark);
  253. Assert.Equal(BsonType.Int32, _bsonReader.ReadBsonType());
  254. bookmark = _bsonReader.GetBookmark();
  255. Assert.Equal("y", _bsonReader.ReadName());
  256. _bsonReader.ReturnToBookmark(bookmark);
  257. Assert.Equal("y", _bsonReader.ReadName());
  258. bookmark = _bsonReader.GetBookmark();
  259. Assert.Equal(2, _bsonReader.ReadInt32());
  260. _bsonReader.ReturnToBookmark(bookmark);
  261. Assert.Equal(2, _bsonReader.ReadInt32());
  262. bookmark = _bsonReader.GetBookmark();
  263. Assert.Equal(BsonType.EndOfDocument, _bsonReader.ReadBsonType());
  264. _bsonReader.ReturnToBookmark(bookmark);
  265. Assert.Equal(BsonType.EndOfDocument, _bsonReader.ReadBsonType());
  266. bookmark = _bsonReader.GetBookmark();
  267. _bsonReader.ReadEndDocument();
  268. _bsonReader.ReturnToBookmark(bookmark);
  269. _bsonReader.ReadEndDocument();
  270. Assert.Equal(BsonReaderState.Initial, _bsonReader.State);
  271. Assert.True(_bsonReader.IsAtEndOfFile());
  272. }
  273. Assert.Equal(json, BsonSerializer.Deserialize<BsonDocument>(json).ToJson());
  274. }
  275. [Fact]
  276. public void TestBooleanFalse()
  277. {
  278. var json = "false";
  279. using (_bsonReader = new JsonReader(json))
  280. {
  281. Assert.Equal(BsonType.Boolean, _bsonReader.ReadBsonType());
  282. Assert.Equal(false, _bsonReader.ReadBoolean());
  283. Assert.Equal(BsonReaderState.Initial, _bsonReader.State);
  284. Assert.True(_bsonReader.IsAtEndOfFile());
  285. }
  286. Assert.Equal(json, BsonSerializer.Deserialize<bool>(json).ToJson());
  287. }
  288. [Fact]
  289. public void TestBooleanTrue()
  290. {
  291. var json = "true";
  292. using (_bsonReader = new JsonReader(json))
  293. {
  294. Assert.Equal(BsonType.Boolean, _bsonReader.ReadBsonType());
  295. Assert.Equal(true, _bsonReader.ReadBoolean());
  296. Assert.Equal(BsonReaderState.Initial, _bsonReader.State);
  297. Assert.True(_bsonReader.IsAtEndOfFile());
  298. }
  299. Assert.Equal(json, BsonSerializer.Deserialize<bool>(json).ToJson());
  300. }
  301. [Theory]
  302. [InlineData("{ $date : 0 }", 0L)]
  303. [InlineData("{ $date : -9223372036854775808 }", -9223372036854775808L)]
  304. [InlineData("{ $date : 9223372036854775807 }", 9223372036854775807L)]
  305. [InlineData("{ $date : { $numberLong : 0 } }", 0L)]
  306. [InlineData("{ $date : { $numberLong : -9223372036854775808 } }", -9223372036854775808L)]
  307. [InlineData("{ $date : { $numberLong : 9223372036854775807 } }", 9223372036854775807L)]
  308. [InlineData("{ $date : { $numberLong : \"0\" } }", 0L)]
  309. [InlineData("{ $date : { $numberLong : \"-9223372036854775808\" } }", -9223372036854775808L)]
  310. [InlineData("{ $date : { $numberLong : \"9223372036854775807\" } }", 9223372036854775807L)]
  311. [InlineData("{ $date : \"1970-01-01T00:00:00Z\" }", 0L)]
  312. [InlineData("{ $date : \"0001-01-01T00:00:00Z\" }", -62135596800000L)]
  313. [InlineData("{ $date : \"1970-01-01T00:00:00.000Z\" }", 0L)]
  314. [InlineData("{ $date : \"0001-01-01T00:00:00.000Z\" }", -62135596800000L)]
  315. [InlineData("{ $date : \"9999-12-31T23:59:59.999Z\" }", 253402300799999L)]
  316. [InlineData("new Date(0)", 0L)]
  317. [InlineData("new Date(9223372036854775807)", 9223372036854775807L)]
  318. [InlineData("new Date(-9223372036854775808)", -9223372036854775808L)]
  319. [InlineData("ISODate(\"1970-01-01T00:00:00Z\")", 0L)]
  320. [InlineData("ISODate(\"0001-01-01T00:00:00Z\")", -62135596800000L)]
  321. [InlineData("ISODate(\"1970-01-01T00:00:00.000Z\")", 0L)]
  322. [InlineData("ISODate(\"0001-01-01T00:00:00.000Z\")", -62135596800000L)]
  323. [InlineData("ISODate(\"9999-12-31T23:59:59.999Z\")", 253402300799999L)]
  324. public void TestDateTime(string json, long expectedResult)
  325. {
  326. using (var reader = new JsonReader(json))
  327. {
  328. var result = reader.ReadDateTime();
  329. result.Should().Be(expectedResult);
  330. reader.State.Should().Be(BsonReaderState.Initial);
  331. reader.IsAtEndOfFile().Should().BeTrue();
  332. }
  333. }
  334. [Fact]
  335. public void TestDateTimeMinBson()
  336. {
  337. var json = "new Date(-9223372036854775808)";
  338. using (_bsonReader = new JsonReader(json))
  339. {
  340. Assert.Equal(BsonType.DateTime, _bsonReader.ReadBsonType());
  341. Assert.Equal(-9223372036854775808, _bsonReader.ReadDateTime());
  342. Assert.Equal(BsonReaderState.Initial, _bsonReader.State);
  343. Assert.True(_bsonReader.IsAtEndOfFile());
  344. }
  345. Assert.Equal(json, BsonSerializer.Deserialize<BsonDateTime>(json).ToJson());
  346. }
  347. [Fact]
  348. public void TestDateTimeMaxBson()
  349. {
  350. var json = "new Date(9223372036854775807)";
  351. using (_bsonReader = new JsonReader(json))
  352. {
  353. Assert.Equal(BsonType.DateTime, _bsonReader.ReadBsonType());
  354. Assert.Equal(9223372036854775807, _bsonReader.ReadDateTime());
  355. Assert.Equal(BsonReaderState.Initial, _bsonReader.State);
  356. Assert.True(_bsonReader.IsAtEndOfFile());
  357. }
  358. Assert.Equal(json, BsonSerializer.Deserialize<BsonDateTime>(json).ToJson());
  359. }
  360. [Theory]
  361. [InlineData("ISODate(\"1970\")", 0L, "ISODate(\"1970-01-01T00:00:00Z\")")]
  362. [InlineData("ISODate(\"1970-01\")", 0L, "ISODate(\"1970-01-01T00:00:00Z\")")]
  363. [InlineData("ISODate(\"1970-01-02\")", 86400000L, "ISODate(\"1970-01-02T00:00:00Z\")")]
  364. [InlineData("ISODate(\"1970-01-02T00\")", 86400000L, "ISODate(\"1970-01-02T00:00:00Z\")")]
  365. [InlineData("ISODate(\"1970-01-02T00:01\")", 86460000L, "ISODate(\"1970-01-02T00:01:00Z\")")]
  366. [InlineData("ISODate(\"1970-01-02T00:01:02\")", 86462000L, "ISODate(\"1970-01-02T00:01:02Z\")")]
  367. [InlineData("ISODate(\"1970-01-02T00:01:02.003\")", 86462003L, "ISODate(\"1970-01-02T00:01:02.003Z\")")]
  368. [InlineData("ISODate(\"1970-01-02T00Z\")", 86400000L, "ISODate(\"1970-01-02T00:00:00Z\")")]
  369. [InlineData("ISODate(\"1970-01-02T00:01Z\")", 86460000L, "ISODate(\"1970-01-02T00:01:00Z\")")]
  370. [InlineData("ISODate(\"1970-01-02T00:01:02Z\")", 86462000L, "ISODate(\"1970-01-02T00:01:02Z\")")]
  371. [InlineData("ISODate(\"1970-01-02T00:01:02.003Z\")", 86462003L, "ISODate(\"1970-01-02T00:01:02.003Z\")")]
  372. [InlineData("ISODate(\"1970-01-02T00+00\")", 86400000L, "ISODate(\"1970-01-02T00:00:00Z\")")]
  373. [InlineData("ISODate(\"1970-01-02T00:01+00\")", 86460000L, "ISODate(\"1970-01-02T00:01:00Z\")")]
  374. [InlineData("ISODate(\"1970-01-02T00:01:02+00\")", 86462000L, "ISODate(\"1970-01-02T00:01:02Z\")")]
  375. [InlineData("ISODate(\"1970-01-02T00:01:02.003+00\")", 86462003L, "ISODate(\"1970-01-02T00:01:02.003Z\")")]
  376. [InlineData("ISODate(\"1970-01-02T00+00:00\")", 86400000L, "ISODate(\"1970-01-02T00:00:00Z\")")]
  377. [InlineData("ISODate(\"1970-01-02T00:01+00:00\")", 86460000L, "ISODate(\"1970-01-02T00:01:00Z\")")]
  378. [InlineData("ISODate(\"1970-01-02T00:01:02+00:00\")", 86462000L, "ISODate(\"1970-01-02T00:01:02Z\")")]
  379. [InlineData("ISODate(\"1970-01-02T00:01:02.003+00:00\")", 86462003L, "ISODate(\"1970-01-02T00:01:02.003Z\")")]
  380. [InlineData("ISODate(\"19700102\")", 86400000L, "ISODate(\"1970-01-02T00:00:00Z\")")]
  381. [InlineData("ISODate(\"19700102T00\")", 86400000L, "ISODate(\"1970-01-02T00:00:00Z\")")]
  382. [InlineData("ISODate(\"19700102T0001\")", 86460000L, "ISODate(\"1970-01-02T00:01:00Z\")")]
  383. [InlineData("ISODate(\"19700102T000102\")", 86462000L, "ISODate(\"1970-01-02T00:01:02Z\")")]
  384. [InlineData("ISODate(\"19700102T000102.003\")", 86462003L, "ISODate(\"1970-01-02T00:01:02.003Z\")")]
  385. [InlineData("ISODate(\"19700102T00Z\")", 86400000L, "ISODate(\"1970-01-02T00:00:00Z\")")]
  386. [InlineData("ISODate(\"19700102T0001Z\")", 86460000L, "ISODate(\"1970-01-02T00:01:00Z\")")]
  387. [InlineData("ISODate(\"19700102T000102Z\")", 86462000L, "ISODate(\"1970-01-02T00:01:02Z\")")]
  388. [InlineData("ISODate(\"19700102T000102.003Z\")", 86462003L, "ISODate(\"1970-01-02T00:01:02.003Z\")")]
  389. [InlineData("ISODate(\"19700102T00+00\")", 86400000L, "ISODate(\"1970-01-02T00:00:00Z\")")]
  390. [InlineData("ISODate(\"19700102T0001+00\")", 86460000L, "ISODate(\"1970-01-02T00:01:00Z\")")]
  391. [InlineData("ISODate(\"19700102T000102+00\")", 86462000L, "ISODate(\"1970-01-02T00:01:02Z\")")]
  392. [InlineData("ISODate(\"19700102T000102.003+00\")", 86462003L, "ISODate(\"1970-01-02T00:01:02.003Z\")")]
  393. [InlineData("ISODate(\"19700102T00+00:00\")", 86400000L, "ISODate(\"1970-01-02T00:00:00Z\")")]
  394. [InlineData("ISODate(\"19700102T0001+00:00\")", 86460000L, "ISODate(\"1970-01-02T00:01:00Z\")")]
  395. [InlineData("ISODate(\"19700102T000102+00:00\")", 86462000L, "ISODate(\"1970-01-02T00:01:02Z\")")]
  396. [InlineData("ISODate(\"19700102T000102.003+00:00\")", 86462003L, "ISODate(\"1970-01-02T00:01:02.003Z\")")]
  397. public void TestDateTimeShell(string json, long expectedResult, string canonicalJson)
  398. {
  399. using (_bsonReader = new JsonReader(json))
  400. {
  401. Assert.Equal(BsonType.DateTime, _bsonReader.ReadBsonType());
  402. Assert.Equal(expectedResult, _bsonReader.ReadDateTime());
  403. Assert.Equal(BsonReaderState.Initial, _bsonReader.State);
  404. Assert.True(_bsonReader.IsAtEndOfFile());
  405. }
  406. var jsonSettings = new JsonWriterSettings { OutputMode = JsonOutputMode.Shell };
  407. Assert.Equal(canonicalJson, BsonSerializer.Deserialize<DateTime>(json).ToJson(jsonSettings));
  408. }
  409. [Fact]
  410. public void TestDateTimeStrict()
  411. {
  412. var json = "{ \"$date\" : 0 }";
  413. using (_bsonReader = new JsonReader(json))
  414. {
  415. Assert.Equal(BsonType.DateTime, _bsonReader.ReadBsonType());
  416. Assert.Equal(0, _bsonReader.ReadDateTime());
  417. Assert.Equal(BsonReaderState.Initial, _bsonReader.State);
  418. Assert.True(_bsonReader.IsAtEndOfFile());
  419. }
  420. #pragma warning disable 618
  421. var jsonSettings = new JsonWriterSettings { OutputMode = JsonOutputMode.Strict };
  422. #pragma warning restore 618
  423. Assert.Equal(json, BsonSerializer.Deserialize<DateTime>(json).ToJson(jsonSettings));
  424. }
  425. [Fact]
  426. public void TestDateTimeStrictIso8601()
  427. {
  428. var json = "{ \"$date\" : \"1970-01-01T00:00:00Z\" }";
  429. using (_bsonReader = new JsonReader(json))
  430. {
  431. Assert.Equal(BsonType.DateTime, _bsonReader.ReadBsonType());
  432. Assert.Equal(0, _bsonReader.ReadDateTime());
  433. Assert.Equal(BsonReaderState.Initial, _bsonReader.State);
  434. Assert.True(_bsonReader.IsAtEndOfFile());
  435. }
  436. var expected = "{ \"$date\" : 0 }"; // it's still not ISO8601 on the way out
  437. #pragma warning disable 618
  438. var jsonSettings = new JsonWriterSettings { OutputMode = JsonOutputMode.Strict };
  439. #pragma warning restore 618
  440. Assert.Equal(expected, BsonSerializer.Deserialize<DateTime>(json).ToJson(jsonSettings));
  441. }
  442. [Theory]
  443. [InlineData("{ $date: { \"$numberLong\": \"1552949630483\" } }", 1552949630483L)]
  444. [InlineData("{ $date: { $numberLong: \"1552949630483\" } }", 1552949630483L)]
  445. public void TestDateTimeWithNumberLong(string json, long expectedResult)
  446. {
  447. using (var reader = new JsonReader(json))
  448. {
  449. var result = reader.ReadDateTime();
  450. result.Should().Be(expectedResult);
  451. reader.State.Should().Be(BsonReaderState.Initial);
  452. reader.IsAtEndOfFile().Should().BeTrue();
  453. }
  454. }
  455. [Theory]
  456. [InlineData("NumberDecimal(1)", "1", "NumberDecimal(\"1\")")]
  457. [InlineData("NumberDecimal(2147483648)", "2147483648", "NumberDecimal(\"2147483648\")")]
  458. [InlineData("NumberDecimal(\"1.5\")", "1.5", "NumberDecimal(\"1.5\")")]
  459. public void TestDecimal128Constructor(string json, string expectedValueString, string expectedJson)
  460. {
  461. using (_bsonReader = new JsonReader(json))
  462. {
  463. Assert.Equal(BsonType.Decimal128, _bsonReader.ReadBsonType());
  464. Assert.Equal(Decimal128.Parse(expectedValueString), _bsonReader.ReadDecimal128());
  465. Assert.Equal(BsonReaderState.Initial, _bsonReader.State);
  466. Assert.True(_bsonReader.IsAtEndOfFile());
  467. }
  468. Assert.Equal(expectedJson, BsonSerializer.Deserialize<BsonDecimal128>(json).ToJson());
  469. }
  470. [Theory]
  471. [InlineData("{ $numberDecimal : 1 }", "1", "NumberDecimal(\"1\")")]
  472. [InlineData("{ $numberDecimal : 2147483648 }", "2147483648", "NumberDecimal(\"2147483648\")")]
  473. [InlineData("{ $numberDecimal : \"1.5\" }", "1.5", "NumberDecimal(\"1.5\")")]
  474. [InlineData("{ $numberDecimal : \"Infinity\" }", "Infinity", "NumberDecimal(\"Infinity\")")]
  475. [InlineData("{ $numberDecimal : \"-Infinity\" }", "-Infinity", "NumberDecimal(\"-Infinity\")")]
  476. [InlineData("{ $numberDecimal : \"NaN\" }", "NaN", "NumberDecimal(\"NaN\")")]
  477. public void TestDecimal128ExtendedJson(string json, string expectedValueString, string expectedJson)
  478. {
  479. using (_bsonReader = new JsonReader(json))
  480. {
  481. Assert.Equal(BsonType.Decimal128, _bsonReader.ReadBsonType());
  482. Assert.Equal(Decimal128.Parse(expectedValueString), _bsonReader.ReadDecimal128());
  483. Assert.Equal(BsonReaderState.Initial, _bsonReader.State);
  484. Assert.True(_bsonReader.IsAtEndOfFile());
  485. }
  486. Assert.Equal(expectedJson, BsonSerializer.Deserialize<BsonDecimal128>(json).ToJson());
  487. }
  488. [Fact]
  489. public void TestDocumentEmpty()
  490. {
  491. var json = "{ }";
  492. using (_bsonReader = new JsonReader(json))
  493. {
  494. Assert.Equal(BsonType.Document, _bsonReader.ReadBsonType());
  495. _bsonReader.ReadStartDocument();
  496. Assert.Equal(BsonType.EndOfDocument, _bsonReader.ReadBsonType());
  497. _bsonReader.ReadEndDocument();
  498. Assert.Equal(BsonReaderState.Initial, _bsonReader.State);
  499. Assert.True(_bsonReader.IsAtEndOfFile());
  500. }
  501. Assert.Equal(json, BsonSerializer.Deserialize<BsonDocument>(json).ToJson());
  502. }
  503. [Fact]
  504. public void TestDocumentNested()
  505. {
  506. var json = "{ \"a\" : { \"x\" : 1 }, \"y\" : 2 }";
  507. using (_bsonReader = new JsonReader(json))
  508. {
  509. Assert.Equal(BsonType.Document, _bsonReader.ReadBsonType());
  510. _bsonReader.ReadStartDocument();
  511. Assert.Equal(BsonType.Document, _bsonReader.ReadBsonType());
  512. Assert.Equal("a", _bsonReader.ReadName());
  513. _bsonReader.ReadStartDocument();
  514. Assert.Equal(BsonType.Int32, _bsonReader.ReadBsonType());
  515. Assert.Equal("x", _bsonReader.ReadName());
  516. Assert.Equal(1, _bsonReader.ReadInt32());
  517. Assert.Equal(BsonType.EndOfDocument, _bsonReader.ReadBsonType());
  518. _bsonReader.ReadEndDocument();
  519. Assert.Equal(BsonType.Int32, _bsonReader.ReadBsonType());
  520. Assert.Equal("y", _bsonReader.ReadName());
  521. Assert.Equal(2, _bsonReader.ReadInt32());
  522. Assert.Equal(BsonType.EndOfDocument, _bsonReader.ReadBsonType());
  523. _bsonReader.ReadEndDocument();
  524. Assert.Equal(BsonReaderState.Initial, _bsonReader.State);
  525. Assert.True(_bsonReader.IsAtEndOfFile());
  526. }
  527. Assert.Equal(json, BsonSerializer.Deserialize<BsonDocument>(json).ToJson());
  528. }
  529. [Fact]
  530. public void TestDocumentOneElement()
  531. {
  532. var json = "{ \"x\" : 1 }";
  533. using (_bsonReader = new JsonReader(json))
  534. {
  535. Assert.Equal(BsonType.Document, _bsonReader.ReadBsonType());
  536. _bsonReader.ReadStartDocument();
  537. Assert.Equal(BsonType.Int32, _bsonReader.ReadBsonType());
  538. Assert.Equal("x", _bsonReader.ReadName());
  539. Assert.Equal(1, _bsonReader.ReadInt32());
  540. Assert.Equal(BsonType.EndOfDocument, _bsonReader.ReadBsonType());
  541. _bsonReader.ReadEndDocument();
  542. Assert.Equal(BsonReaderState.Initial, _bsonReader.State);
  543. Assert.True(_bsonReader.IsAtEndOfFile());
  544. }
  545. Assert.Equal(json, BsonSerializer.Deserialize<BsonDocument>(json).ToJson());
  546. }
  547. [Fact]
  548. public void TestDocumentTwoElements()
  549. {
  550. var json = "{ \"x\" : 1, \"y\" : 2 }";
  551. using (_bsonReader = new JsonReader(json))
  552. {
  553. Assert.Equal(BsonType.Document, _bsonReader.ReadBsonType());
  554. _bsonReader.ReadStartDocument();
  555. Assert.Equal(BsonType.Int32, _bsonReader.ReadBsonType());
  556. Assert.Equal("x", _bsonReader.ReadName());
  557. Assert.Equal(1, _bsonReader.ReadInt32());
  558. Assert.Equal(BsonType.Int32, _bsonReader.ReadBsonType());
  559. Assert.Equal("y", _bsonReader.ReadName());
  560. Assert.Equal(2, _bsonReader.ReadInt32());
  561. Assert.Equal(BsonType.EndOfDocument, _bsonReader.ReadBsonType());
  562. _bsonReader.ReadEndDocument();
  563. Assert.Equal(BsonReaderState.Initial, _bsonReader.State);
  564. Assert.True(_bsonReader.IsAtEndOfFile());
  565. }
  566. Assert.Equal(json, BsonSerializer.Deserialize<BsonDocument>(json).ToJson());
  567. }
  568. [Theory]
  569. [InlineData("1.0", 1.0)]
  570. [InlineData("-1.0", -1.0)]
  571. [InlineData("1.5", 1.5)]
  572. [InlineData("{ $numberDouble : 1 }", 1.0)]
  573. [InlineData("{ $numberDouble : 1.0 }", 1.0)]
  574. [InlineData("{ $numberDouble : \"1\" }", 1.0)]
  575. [InlineData("{ $numberDouble : \"-1\" }", -1.0)]
  576. [InlineData("{ $numberDouble : \"1.5\" }", 1.5)]
  577. [InlineData("{ $numberDouble : \"Infinity\" }", double.PositiveInfinity)]
  578. [InlineData("{ $numberDouble : \"-Infinity\" }", double.NegativeInfinity)]
  579. [InlineData("{ $numberDouble : \"NaN\" }", double.NaN)]
  580. public void TestDouble(string json, double expectedValue)
  581. {
  582. using (var reader = new JsonReader(json))
  583. {
  584. reader.ReadBsonType().Should().Be(BsonType.Double);
  585. reader.ReadDouble().Should().Be(expectedValue);
  586. reader.State.Should().Be(BsonReaderState.Initial);
  587. reader.IsAtEndOfFile().Should().BeTrue();
  588. }
  589. }
  590. [Theory]
  591. [InlineData("{ $numberDouble")]
  592. [InlineData("{ $numberDouble :")]
  593. [InlineData("{ $numberDouble : \"1\"")]
  594. public void TestDoubleEndOfFile(string json)
  595. {
  596. using (var reader = new JsonReader(json))
  597. {
  598. var exception = Record.Exception(() => reader.ReadDouble());
  599. exception.Should().BeOfType<FormatException>();
  600. }
  601. }
  602. [Theory]
  603. [InlineData("{ $numberDouble [")]
  604. [InlineData("{ $numberDouble : [")]
  605. [InlineData("{ $numberDouble : \"1\" [")]
  606. public void TestDoubleInvalidToken(string json)
  607. {
  608. using (var reader = new JsonReader(json))
  609. {
  610. var exception = Record.Exception(() => reader.ReadDouble());
  611. exception.Should().BeOfType<FormatException>();
  612. }
  613. }
  614. [Fact]
  615. public void TestDoubleRoundTrip()
  616. {
  617. var json = "1.5";
  618. using (_bsonReader = new JsonReader(json))
  619. {
  620. Assert.Equal(BsonType.Double, _bsonReader.ReadBsonType());
  621. Assert.Equal(1.5, _bsonReader.ReadDouble());
  622. Assert.Equal(BsonReaderState.Initial, _bsonReader.State);
  623. Assert.True(_bsonReader.IsAtEndOfFile());
  624. }
  625. Assert.Equal(json, BsonSerializer.Deserialize<double>(json).ToJson());
  626. }
  627. [Theory]
  628. [ParameterAttributeData]
  629. [ResetGuidModeAfterTest]
  630. public void TestGuid(
  631. [ClassValues(typeof(GuidModeValues))] GuidMode mode)
  632. {
  633. mode.Set();
  634. #pragma warning disable 618
  635. var guid = new Guid("B5F21E0C2A0D42D6AD03D827008D8AB6");
  636. var json = "CSUUID(\"B5F21E0C2A0D42D6AD03D827008D8AB6\")";
  637. using (_bsonReader = new JsonReader(json))
  638. {
  639. Assert.Equal(BsonType.Binary, _bsonReader.ReadBsonType());
  640. var binaryData = _bsonReader.ReadBinaryData();
  641. Assert.True(binaryData.Bytes.SequenceEqual(guid.ToByteArray()));
  642. Assert.Equal(BsonBinarySubType.UuidLegacy, binaryData.SubType);
  643. if (BsonDefaults.GuidRepresentationMode == GuidRepresentationMode.V2)
  644. {
  645. Assert.Equal(GuidRepresentation.CSharpLegacy, binaryData.GuidRepresentation);
  646. }
  647. Assert.Equal(BsonReaderState.Initial, _bsonReader.State);
  648. Assert.True(_bsonReader.IsAtEndOfFile());
  649. }
  650. var guidRepresentation = BsonDefaults.GuidRepresentationMode == GuidRepresentationMode.V2 ? BsonDefaults.GuidRepresentation : GuidRepresentation.Unspecified;
  651. if (BsonDefaults.GuidRepresentationMode == GuidRepresentationMode.V2 && guidRepresentation != GuidRepresentation.Unspecified)
  652. {
  653. string expected;
  654. switch (guidRepresentation)
  655. {
  656. case GuidRepresentation.CSharpLegacy: expected = "CSUUID(\"b5f21e0c-2a0d-42d6-ad03-d827008d8ab6\")"; break;
  657. case GuidRepresentation.JavaLegacy: expected = "JUUID(\"b5f21e0c-2a0d-42d6-ad03-d827008d8ab6\")"; break;
  658. case GuidRepresentation.PythonLegacy: expected = "PYUUID(\"b5f21e0c-2a0d-42d6-ad03-d827008d8ab6\")"; break;
  659. case GuidRepresentation.Standard: expected = "UUID(\"b5f21e0c-2a0d-42d6-ad03-d827008d8ab6\")"; break;
  660. default: throw new Exception("Unexpected GuidRepresentation.");
  661. }
  662. Assert.Equal(expected, BsonSerializer.Deserialize<Guid>(json).ToJson(new JsonWriterSettings()));
  663. }
  664. else
  665. {
  666. var exception = Record.Exception(() => guid.ToJson(new JsonWriterSettings()));
  667. exception.Should().BeOfType<BsonSerializationException>();
  668. }
  669. #pragma warning restore 618
  670. }
  671. [Fact]
  672. public void TestHexData()
  673. {
  674. var expectedBytes = new byte[] { 0x01, 0x23 };
  675. var json = "HexData(0, \"123\")";
  676. using (_bsonReader = new JsonReader(json))
  677. {
  678. Assert.Equal(BsonType.Binary, _bsonReader.ReadBsonType());
  679. var bytes = _bsonReader.ReadBytes();
  680. Assert.True(expectedBytes.SequenceEqual(bytes));
  681. Assert.Equal(BsonReaderState.Initial, _bsonReader.State);
  682. Assert.True(_bsonReader.IsAtEndOfFile());
  683. }
  684. var expectedJson = "new BinData(0, \"ASM=\")";
  685. Assert.Equal(expectedJson, BsonSerializer.Deserialize<byte[]>(json).ToJson());
  686. }
  687. [Fact]
  688. public void TestInt32()
  689. {
  690. var json = "123";
  691. using (_bsonReader = new JsonReader(json))
  692. {
  693. Assert.Equal(BsonType.Int32, _bsonReader.ReadBsonType());
  694. Assert.Equal(123, _bsonReader.ReadInt32());
  695. Assert.Equal(BsonReaderState.Initial, _bsonReader.State);
  696. Assert.True(_bsonReader.IsAtEndOfFile());
  697. }
  698. Assert.Equal(json, BsonSerializer.Deserialize<int>(json).ToJson());
  699. }
  700. [Theory]
  701. [InlineData("{ $numberInt : 1 }", 1)]
  702. [InlineData("{ $numberInt : -2147483648 }", -2147483648)]
  703. [InlineData("{ $numberInt : 2147483647 }", 2147483647)]
  704. [InlineData("{ $numberInt : \"1\" }", 1)]
  705. [InlineData("{ $numberInt : \"-2147483648\" }", -2147483648)]
  706. [InlineData("{ $numberInt : \"2147483647\" }", 2147483647)]
  707. public void TestInt32ExtendedJson(string json, int expectedResult)
  708. {
  709. using (var reader = new JsonReader(json))
  710. {
  711. var result = reader.ReadInt32();
  712. result.Should().Be(expectedResult);
  713. reader.State.Should().Be(BsonReaderState.Initial);
  714. reader.IsAtEndOfFile().Should().BeTrue();
  715. }
  716. }
  717. [Theory]
  718. // truncated input
  719. [InlineData("{ $numberInt")]
  720. [InlineData("{ $numberInt :")]
  721. [InlineData("{ $numberInt : 1")]
  722. // invalid extended json
  723. [InlineData("{ $numberInt ,")]
  724. [InlineData("{ $numberInt : \"abc\"")]
  725. [InlineData("{ $numberInt : 1,")]
  726. public void TestInt32ExtendedJsonInvalid(string json)
  727. {
  728. using (var reader = new JsonReader(json))
  729. {
  730. var execption = Record.Exception(() => reader.ReadInt32());
  731. execption.Should().BeOfType<FormatException>();
  732. }
  733. }
  734. [Theory]
  735. [InlineData("Number(123)")]
  736. [InlineData("NumberInt(123)")]
  737. public void TestInt32Constructor(string json)
  738. {
  739. using (_bsonReader = new JsonReader(json))
  740. {
  741. Assert.Equal(BsonType.Int32, _bsonReader.ReadBsonType());
  742. Assert.Equal(123, _bsonReader.ReadInt32());
  743. Assert.Equal(BsonReaderState.Initial, _bsonReader.State);
  744. Assert.True(_bsonReader.IsAtEndOfFile());
  745. }
  746. var canonicalJson = "123";
  747. Assert.Equal(canonicalJson, BsonSerializer.Deserialize<int>(new StringReader(json)).ToJson());
  748. }
  749. [Theory]
  750. [InlineData("{ $numberLong: 1 }", 1L)]
  751. [InlineData("{ $numberLong: -9223372036854775808 }", -9223372036854775808L)]
  752. [InlineData("{ $numberLong: 9223372036854775807 }", 9223372036854775807L)]
  753. [InlineData("{ $numberLong: \"1\" }", 1L)]
  754. [InlineData("{ $numberLong: \"-9223372036854775808\" }", -9223372036854775808L)]
  755. [InlineData("{ $numberLong: \"9223372036854775807\" }", 9223372036854775807L)]
  756. [InlineData("NumberLong(1)", 1L)]
  757. [InlineData("NumberLong(-9223372036854775808)", -9223372036854775808L)]
  758. [InlineData("NumberLong(9223372036854775807)", 9223372036854775807L)]
  759. [InlineData("NumberLong(\"1\")", 1L)]
  760. [InlineData("NumberLong(\"-9223372036854775808\")", -9223372036854775808L)]
  761. [InlineData("NumberLong(\"9223372036854775807\")", 9223372036854775807L)]
  762. public void TestInt64(string json, long expectedResult)
  763. {
  764. using (var reader = new JsonReader(json))
  765. {
  766. var result = reader.ReadInt64();
  767. result.Should().Be(expectedResult);
  768. reader.State.Should().Be(BsonReaderState.Initial);
  769. reader.IsAtEndOfFile().Should().BeTrue();
  770. }
  771. }
  772. [Fact]
  773. public void TestInt64ConstructorQuoted()
  774. {
  775. var json = "NumberLong(\"123456789012\")";
  776. using (_bsonReader = new JsonReader(json))
  777. {
  778. Assert.Equal(BsonType.Int64, _bsonReader.ReadBsonType());
  779. Assert.Equal(123456789012, _bsonReader.ReadInt64());
  780. Assert.Equal(BsonReaderState.Initial, _bsonReader.State);
  781. Assert.True(_bsonReader.IsAtEndOfFile());
  782. }
  783. Assert.Equal(json, BsonSerializer.Deserialize<long>(json).ToJson());
  784. }
  785. [Fact]
  786. public void TestInt64ConstructorUnqutoed()
  787. {
  788. var json = "NumberLong(123)";
  789. using (_bsonReader = new JsonReader(json))
  790. {
  791. Assert.Equal(BsonType.Int64, _bsonReader.ReadBsonType());
  792. Assert.Equal(123, _bsonReader.ReadInt64());
  793. Assert.Equal(BsonReaderState.Initial, _bsonReader.State);
  794. Assert.True(_bsonReader.IsAtEndOfFile());
  795. }
  796. Assert.Equal(json, BsonSerializer.Deserialize<long>(json).ToJson());
  797. }
  798. [Fact]
  799. public void TestIsAtEndOfFileWithTwoArrays()
  800. {
  801. var json = "[1,2][1,2]";
  802. using (var jsonReader = new JsonReader(json))
  803. {
  804. var count = 0;
  805. jsonReader.State.Should().Be(BsonReaderState.Initial);
  806. while (!jsonReader.IsAtEndOfFile())
  807. {
  808. var array = BsonSerializer.Deserialize<BsonArray>(jsonReader);
  809. jsonReader.State.Should().Be(BsonReaderState.Initial);
  810. var expected = new BsonArray { 1, 2 };
  811. Assert.Equal(expected, array);
  812. count += 1;
  813. }
  814. Assert.Equal(2, count);
  815. }
  816. }
  817. [Fact]
  818. public void TestIsAtEndOfFileWithTwoDocuments()
  819. {
  820. var json = "{x:1}{x:1}";
  821. using (var jsonReader = new JsonReader(json))
  822. {
  823. var count = 0;
  824. jsonReader.State.Should().Be(BsonReaderState.Initial);
  825. while (!jsonReader.IsAtEndOfFile())
  826. {
  827. var document = BsonSerializer.Deserialize<BsonDocument>(jsonReader);
  828. jsonReader.State.Should().Be(BsonReaderState.Initial);
  829. var expected = new BsonDocument("x", 1);
  830. Assert.Equal(expected, document);
  831. count += 1;
  832. }
  833. Assert.Equal(2, count);
  834. }
  835. }
  836. [Fact]
  837. public void TestInt64ExtendedJson()
  838. {
  839. var json = "{ \"$numberLong\" : \"123\" }";
  840. using (_bsonReader = new JsonReader(json))
  841. {
  842. Assert.Equal(BsonType.Int64, _bsonReader.ReadBsonType());
  843. Assert.Equal(123, _bsonReader.ReadInt64());
  844. Assert.Equal(BsonReaderState.Initial, _bsonReader.State);
  845. Assert.True(_bsonReader.IsAtEndOfFile());
  846. }
  847. var canonicalJson = "NumberLong(123)";
  848. Assert.Equal(canonicalJson, BsonSerializer.Deserialize<long>(new StringReader(json)).ToJson());
  849. }
  850. [Fact]
  851. public void TestJavaScript()
  852. {
  853. string json = "{ \"$code\" : \"function f() { return 1; }\" }";
  854. using (_bsonReader = new JsonReader(json))
  855. {
  856. Assert.Equal(BsonType.JavaScript, _bsonReader.ReadBsonType());
  857. Assert.Equal("function f() { return 1; }", _bsonReader.ReadJavaScript());
  858. Assert.Equal(BsonReaderState.Initial, _bsonReader.State);
  859. Assert.True(_bsonReader.IsAtEndOfFile());
  860. }
  861. Assert.Equal(json, BsonSerializer.Deserialize<BsonJavaScript>(json).ToJson());
  862. }
  863. [Fact]
  864. public void TestJavaScriptWithScope()
  865. {
  866. string json = "{ \"$code\" : \"function f() { return n; }\", \"$scope\" : { \"n\" : 1 } }";
  867. using (_bsonReader = new JsonReader(json))
  868. {
  869. Assert.Equal(BsonType.JavaScriptWithScope, _bsonReader.ReadBsonType());
  870. Assert.Equal("function f() { return n; }", _bsonReader.ReadJavaScriptWithScope());
  871. _bsonReader.ReadStartDocument();
  872. Assert.Equal(BsonType.Int32, _bsonReader.ReadBsonType());
  873. Assert.Equal("n", _bsonReader.ReadName());
  874. Assert.Equal(1, _bsonReader.ReadInt32());
  875. _bsonReader.ReadEndDocument();
  876. Assert.Equal(BsonReaderState.Initial, _bsonReader.State);
  877. Assert.True(_bsonReader.IsAtEndOfFile());
  878. }
  879. Assert.Equal(json, BsonSerializer.Deserialize<BsonJavaScriptWithScope>(json).ToJson());
  880. }
  881. [Theory]
  882. [InlineData("{ $maxKey : 1 }")]
  883. [InlineData("MaxKey")]
  884. public void TestMaxKey(string json)
  885. {
  886. using (var reader = new JsonReader(json))
  887. {
  888. reader.ReadMaxKey();
  889. reader.State.Should().Be(BsonReaderState.Initial);
  890. reader.IsAtEndOfFile().Should().BeTrue();
  891. }
  892. }
  893. [Fact]
  894. public void TestMaxKeyExtendedJson()
  895. {
  896. var json = "{ \"$maxkey\" : 1 }";
  897. using (_bsonReader = new JsonReader(json))
  898. {
  899. Assert.Equal(BsonType.MaxKey, _bsonReader.ReadBsonType());
  900. _bsonReader.ReadMaxKey();
  901. Assert.Equal(BsonReaderState.Initial, _bsonReader.State);
  902. Assert.True(_bsonReader.IsAtEndOfFile());
  903. }
  904. var canonicalJson = "MaxKey";
  905. Assert.Equal(canonicalJson, BsonSerializer.Deserialize<BsonMaxKey>(new StringReader(json)).ToJson());
  906. }
  907. [Fact]
  908. public void TestMaxKeyExtendedJsonWithCapitalK()
  909. {
  910. var json = "{ \"$maxKey\" : 1 }";
  911. using (_bsonReader = new JsonReader(json))
  912. {
  913. Assert.Equal(BsonType.MaxKey, _bsonReader.ReadBsonType());
  914. _bsonReader.ReadMaxKey();
  915. Assert.Equal(BsonReaderState.Initial, _bsonReader.State);
  916. Assert.True(_bsonReader.IsAtEndOfFile());
  917. }
  918. var canonicalJson = "MaxKey";
  919. Assert.Equal(canonicalJson, BsonSerializer.Deserialize<BsonMaxKey>(new StringReader(json)).ToJson());
  920. }
  921. [Fact]
  922. public void TestMaxKeyKeyword()
  923. {
  924. var json = "MaxKey";
  925. using (_bsonReader = new JsonReader(json))
  926. {
  927. Assert.Equal(BsonType.MaxKey, _bsonReader.ReadBsonType());
  928. _bsonReader.ReadMaxKey();
  929. Assert.Equal(BsonReaderState.Initial, _bsonReader.State);
  930. Assert.True(_bsonReader.IsAtEndOfFile());
  931. }
  932. Assert.Equal(json, BsonSerializer.Deserialize<BsonMaxKey>(new StringReader(json)).ToJson());
  933. }
  934. [Theory]
  935. [InlineData("{ $minKey : 1 }")]
  936. [InlineData("MinKey")]
  937. public void TestMinKey(string json)
  938. {
  939. using (var reader = new JsonReader(json))
  940. {
  941. reader.ReadMinKey();
  942. reader.State.Should().Be(BsonReaderState.Initial);
  943. reader.IsAtEndOfFile().Should().BeTrue();
  944. }
  945. }
  946. [Fact]
  947. public void TestMinKeyExtendedJson()
  948. {
  949. var json = "{ \"$minkey\" : 1 }";
  950. using (_bsonReader = new JsonReader(json))
  951. {
  952. Assert.Equal(BsonType.MinKey, _bsonReader.ReadBsonType());
  953. _bsonReader.ReadMinKey();
  954. Assert.Equal(BsonReaderState.Initial, _bsonReader.State);
  955. Assert.True(_bsonReader.IsAtEndOfFile());
  956. }
  957. var canonicalJson = "MinKey";
  958. Assert.Equal(canonicalJson, BsonSerializer.Deserialize<BsonMinKey>(new StringReader(json)).ToJson());
  959. }
  960. [Fact]
  961. public void TestMinKeyExtendedJsonWithCapitalK()
  962. {
  963. var json = "{ \"$minKey\" : 1 }";
  964. using (_bsonReader = new JsonReader(json))
  965. {
  966. Assert.Equal(BsonType.MinKey, _bsonReader.ReadBsonType());
  967. _bsonReader.ReadMinKey();
  968. Assert.Equal(BsonReaderState.Initial, _bsonReader.State);
  969. Assert.True(_bsonReader.IsAtEndOfFile());
  970. }
  971. var canonicalJson = "MinKey";
  972. Assert.Equal(canonicalJson, BsonSerializer.Deserialize<BsonMinKey>(new StringReader(json)).ToJson());
  973. }
  974. [Fact]
  975. public void TestMinKeyKeyword()
  976. {
  977. var json = "MinKey";
  978. using (_bsonReader = new JsonReader(json))
  979. {
  980. Assert.Equal(BsonType.MinKey, _bsonReader.ReadBsonType());
  981. _bsonReader.ReadMinKey();
  982. Assert.Equal(BsonReaderState.Initial, _bsonReader.State);
  983. Assert.True(_bsonReader.IsAtEndOfFile());
  984. }
  985. Assert.Equal(json, BsonSerializer.Deserialize<BsonMinKey>(new StringReader(json)).ToJson());
  986. }
  987. [Fact]
  988. public void TestNestedArray()
  989. {
  990. var json = "{ \"a\" : [1, 2] }";
  991. using (_bsonReader = new JsonReader(json))
  992. {
  993. Assert.Equal(BsonType.Document, _bsonReader.ReadBsonType());
  994. _bsonReader.ReadStartDocument();
  995. Assert.Equal(BsonType.Array, _bsonReader.ReadBsonType());
  996. Assert.Equal("a", _bsonReader.ReadName());
  997. _bsonReader.ReadStartArray();
  998. Assert.Equal(1, _bsonReader.ReadInt32());
  999. Assert.Equal(2, _bsonReader.ReadInt32());
  1000. _bsonReader.ReadEndArray();
  1001. _bsonReader.ReadEndDocument();
  1002. Assert.Equal(BsonReaderState.Initial, _bsonReader.State);
  1003. Assert.True(_bsonReader.IsAtEndOfFile());
  1004. }
  1005. Assert.Equal(json, BsonSerializer.Deserialize<BsonDocument>(json).ToJson());
  1006. }
  1007. [Fact]
  1008. public void TestNestedDocument()
  1009. {
  1010. var json = "{ \"a\" : { \"b\" : 1, \"c\" : 2 } }";
  1011. using (_bsonReader = new JsonReader(json))
  1012. {
  1013. Assert.Equal(BsonType.Document, _bsonReader.ReadBsonType());
  1014. _bsonReader.ReadStartDocument();
  1015. Assert.Equal(BsonType.Document, _bsonReader.ReadBsonType());
  1016. Assert.Equal("a", _bsonReader.ReadName());
  1017. _bsonReader.ReadStartDocument();
  1018. Assert.Equal("b", _bsonReader.ReadName());
  1019. Assert.Equal(1, _bsonReader.ReadInt32());
  1020. Assert.Equal("c", _bsonReader.ReadName());
  1021. Assert.Equal(2, _bsonReader.ReadInt32());
  1022. _bsonReader.ReadEndDocument();
  1023. _bsonReader.ReadEndDocument();
  1024. Assert.Equal(BsonReaderState.Initial, _bsonReader.State);
  1025. Assert.True(_bsonReader.IsAtEndOfFile());
  1026. }
  1027. Assert.Equal(json, BsonSerializer.Deserialize<BsonDocument>(json).ToJson());
  1028. }
  1029. [Fact]
  1030. public void TestNull()
  1031. {
  1032. var json = "null";
  1033. using (_bsonReader = new JsonReader(json))
  1034. {
  1035. Assert.Equal(BsonType.Null, _bsonReader.ReadBsonType());
  1036. _bsonReader.ReadNull();
  1037. Assert.Equal(BsonReaderState.Initial, _bsonReader.State);
  1038. Assert.True(_bsonReader.IsAtEndOfFile());
  1039. }
  1040. Assert.Equal(json, BsonSerializer.Deserialize<BsonNull>(json).ToJson());
  1041. }
  1042. [Theory]
  1043. [InlineData("{ $oid : \"0102030405060708090a0b0c\" }", "0102030405060708090a0b0c")]
  1044. [InlineData("ObjectId(\"0102030405060708090a0b0c\")", "0102030405060708090a0b0c")]
  1045. public void TestObjectId(string json, string expectedResult)
  1046. {
  1047. using (var reader = new JsonReader(json))
  1048. {
  1049. var result = reader.ReadObjectId();
  1050. result.Should().Be(ObjectId.Parse(expectedResult));
  1051. reader.State.Should().Be(BsonReaderState.Initial);
  1052. reader.IsAtEndOfFile().Should().BeTrue();
  1053. }
  1054. }
  1055. [Fact]
  1056. public void TestObjectIdShell()
  1057. {
  1058. var json = "ObjectId(\"4d0ce088e447ad08b4721a37\")";
  1059. using (_bsonReader = new JsonReader(json))
  1060. {
  1061. Assert.Equal(BsonType.ObjectId, _bsonReader.ReadBsonType());
  1062. var objectId = _bsonReader.ReadObjectId();
  1063. Assert.Equal("4d0ce088e447ad08b4721a37", objectId.ToString());
  1064. Assert.Equal(BsonReaderState.Initial, _bsonReader.State);
  1065. Assert.True(_bsonReader.IsAtEndOfFile());
  1066. }
  1067. Assert.Equal(json, BsonSerializer.Deserialize<ObjectId>(json).ToJson());
  1068. }
  1069. [Fact]
  1070. public void TestObjectIdStrict()
  1071. {
  1072. var json = "{ \"$oid\" : \"4d0ce088e447ad08b4721a37\" }";
  1073. using (_bsonReader = new JsonReader(json))
  1074. {
  1075. Assert.Equal(BsonType.ObjectId, _bsonReader.ReadBsonType());
  1076. var objectId = _bsonReader.ReadObjectId();
  1077. Assert.Equal("4d0ce088e447ad08b4721a37", objectId.ToString());
  1078. Assert.Equal(BsonReaderState.Initial, _bsonReader.State);
  1079. Assert.True(_bsonReader.IsAtEndOfFile());
  1080. }
  1081. #pragma warning disable 618
  1082. var jsonSettings = new JsonWriterSettings { OutputMode = JsonOutputMode.Strict };
  1083. #pragma warning restore 618
  1084. Assert.Equal(json, BsonSerializer.Deserialize<ObjectId>(json).ToJson(jsonSettings));
  1085. }
  1086. [Theory]
  1087. [InlineData("{ $regex : \"\", $options : \"\" }", "", "")]
  1088. [InlineData("{ $regex : \"abc\", $options : \"i\" }", "abc", "i")]
  1089. [InlineData("{ $regex : \"abc/\", $options : \"i\" }", "abc/", "i")]
  1090. [InlineData("{ $regularExpression : { pattern : \"\", options : \"\" } }", "", "")]
  1091. [InlineData("{ $regularExpression : { pattern : \"abc\", options : \"i\" } }", "abc", "i")]
  1092. [InlineData("{ $regularExpression : { pattern : \"abc/\", options : \"i\" } }", "abc/", "i")]
  1093. [InlineData("{ $regularExpression : { options : \"\", pattern : \"\" } }", "", "")]
  1094. [InlineData("{ $regularExpression : { options : \"i\", pattern : \"abc\" } }", "abc", "i")]
  1095. [InlineData("{ $regularExpression : { options : \"i\", pattern : \"abc/\" } }", "abc/", "i")]
  1096. [InlineData("RegExp(\"\")", "", "")]
  1097. [InlineData("RegExp(\"\", \"\")", "", "")]
  1098. [InlineData("RegExp(\"abc\")", "abc", "")]
  1099. [InlineData("RegExp(\"abc\", \"i\")", "abc", "i")]
  1100. [InlineData("//", "", "")]
  1101. [InlineData("/abc/i", "abc", "i")]
  1102. [InlineData("/abc\\//i", "abc/", "i")]
  1103. public void TestRegularExpression(string json, string expectedPattern, string expectedOptions)
  1104. {
  1105. using (var reader = new JsonReader(json))
  1106. {
  1107. var result = reader.ReadRegularExpression();
  1108. result.Should().Be(new BsonRegularExpression(expectedPattern, expectedOptions));
  1109. reader.State.Should().Be(BsonReaderState.Initial);
  1110. reader.IsAtEndOfFile().Should().BeTrue();
  1111. }
  1112. }
  1113. [Theory]
  1114. [InlineData("{ $regex")]
  1115. [InlineData("{ $regex :")]
  1116. [InlineData("{ $regex : \"abc\"")]
  1117. [InlineData("{ $regex : \"abc\",")]
  1118. [InlineData("{ $regex : \"abc\", $options")]
  1119. [InlineData("{ $regex : \"abc\", $options :")]
  1120. [InlineData("{ $regex : \"abc\", $options : \"i\"")]
  1121. [InlineData("{ $regularExpression")]
  1122. [InlineData("{ $regularExpression :")]
  1123. [InlineData("{ $regularExpression : {")]
  1124. [InlineData("{ $regularExpression : { pattern")]
  1125. [InlineData("{ $regularExpression : { pattern :")]
  1126. [InlineData("{ $regularExpression : { pattern : \"abc\"")]
  1127. [InlineData("{ $regularExpression : { pattern : \"abc\",")]
  1128. [InlineData("{ $regularExpression : { pattern : \"abc\", options")]
  1129. [InlineData("{ $regularExpression : { pattern : \"abc\", options :")]
  1130. [InlineData("{ $regularExpression : { pattern : \"abc\", options : \"i\"")]
  1131. [InlineData("{ $regularExpression : { pattern : \"abc\", options : \"i\" }")]
  1132. [InlineData("{ $regularExpression : { options")]
  1133. [InlineData("{ $regularExpression : { options :")]
  1134. [InlineData("{ $regularExpression : { options : \"i\"")]
  1135. [InlineData("{ $regularExpression : { options : \"i\",")]
  1136. [InlineData("{ $regularExpression : { options : \"i\", pattern")]
  1137. [InlineData("{ $regularExpression : { options : \"i\", pattern :")]
  1138. [InlineData("{ $regularExpression : { options : \"i\", pattern : \"abc\"")]
  1139. [InlineData("{ $regularExpression : { options : \"i\", pattern : \"abc\" }")]
  1140. [InlineData("RegExp(")]
  1141. [InlineData("RegExp(\"abc\"")]
  1142. [InlineData("RegExp(\"abc\",")]
  1143. [InlineData("RegExp(\"abc\", \"i\"")]
  1144. public void TestRegularExpressionEndOfFile(string json)
  1145. {
  1146. using (var reader = new JsonReader(json))
  1147. {
  1148. var exception = Record.Exception(() => reader.ReadRegularExpression());
  1149. exception.Should().BeOfType<FormatException>();
  1150. }
  1151. }
  1152. [Theory]
  1153. [InlineData("{ $regex [")]
  1154. [InlineData("{ $regex : [")]
  1155. [InlineData("{ $regex : \"abc\" [")]
  1156. [InlineData("{ $regex : \"abc\", [")]
  1157. [InlineData("{ $regex : \"abc\", $options [")]
  1158. [InlineData("{ $regex : \"abc\", $options : [")]
  1159. [InlineData("{ $regex : \"abc\", $options : \"i\" [")]
  1160. [InlineData("{ $regularExpression [")]
  1161. [InlineData("{ $regularExpression : [")]
  1162. [InlineData("{ $regularExpression : { [")]
  1163. [InlineData("{ $regularExpression : { pattern [")]
  1164. [InlineData("{ $regularExpression : { pattern : [")]
  1165. [InlineData("{ $regularExpression : { pattern : \"abc\" [")]
  1166. [InlineData("{ $regularExpression : { pattern : \"abc\", [")]
  1167. [InlineData("{ $regularExpression : { pattern : \"abc\", options [")]
  1168. [InlineData("{ $regularExpression : { pattern : \"abc\", options : [")]
  1169. [InlineData("{ $regularExpression : { pattern : \"abc\", options : \"i\" [")]
  1170. [InlineData("{ $regularExpression : { pattern : \"abc\", options : \"i\" } [")]
  1171. [InlineData("{ $regularExpression : { options [")]
  1172. [InlineData("{ $regularExpression : { options : [")]
  1173. [InlineData("{ $regularExpression : { options : \"i\" [")]
  1174. [InlineData("{ $regularExpression : { options : \"i\", [")]
  1175. [InlineData("{ $regularExpression : { options : \"i\", pattern [")]
  1176. [InlineData("{ $regularExpression : { options : \"i\", pattern : [")]
  1177. [InlineData("{ $regularExpression : { options : \"i\", pattern : \"abc\" [")]
  1178. [InlineData("{ $regularExpression : { options : \"i\", pattern : \"abc\" } [")]
  1179. [InlineData("RegExp(")]
  1180. [InlineData("RegExp(\"abc\"")]
  1181. [InlineData("RegExp(\"abc\",")]
  1182. [InlineData("RegExp(\"abc\", \"i\"")]
  1183. public void TestRegularExpressionInvalidToken(string json)
  1184. {
  1185. using (var reader = new JsonReader(json))
  1186. {
  1187. var exception = Record.Exception(() => reader.ReadRegularExpression());
  1188. exception.Should().BeOfType<FormatException>();
  1189. }
  1190. }
  1191. [Fact]
  1192. public void TestRegularExpressionShell()
  1193. {
  1194. var json = "/pattern/imsx";
  1195. using (_bsonReader = new JsonReader(json))
  1196. {
  1197. Assert.Equal(BsonType.RegularExpression, _bsonReader.ReadBsonType());
  1198. var regex = _bsonReader.ReadRegularExpression();
  1199. Assert.Equal("pattern", regex.Pattern);
  1200. Assert.Equal("imsx", regex.Options);
  1201. Assert.Equal(BsonReaderState.Initial, _bsonReader.State);
  1202. Assert.True(_bsonReader.IsAtEndOfFile());
  1203. }
  1204. Assert.Equal(json, BsonSerializer.Deserialize<BsonRegularExpression>(json).ToJson());
  1205. }
  1206. [Fact]
  1207. public void TestRegularExpressionStrict()
  1208. {
  1209. var json = "{ \"$regex\" : \"pattern\", \"$options\" : \"imsx\" }";
  1210. using (_bsonReader = new JsonReader(json))
  1211. {
  1212. Assert.Equal(BsonType.RegularExpression, _bsonReader.ReadBsonType());
  1213. var regex = _bsonReader.ReadRegularExpression();
  1214. Assert.Equal("pattern", regex.Pattern);
  1215. Assert.Equal("imsx", regex.Options);
  1216. Assert.Equal(BsonReaderState.Initial, _bsonReader.State);
  1217. Assert.True(_bsonReader.IsAtEndOfFile());
  1218. }
  1219. #pragma warning disable 618
  1220. var settings = new JsonWriterSettings { OutputMode = JsonOutputMode.Strict };
  1221. #pragma warning restore 618
  1222. Assert.Equal(json, BsonSerializer.Deserialize<BsonRegularExpression>(json).ToJson(settings));
  1223. }
  1224. [Fact]
  1225. public void TestString()
  1226. {
  1227. var json = "\"abc\"";
  1228. using (_bsonReader = new JsonReader(json))
  1229. {
  1230. Assert.Equal(BsonType.String, _bsonReader.ReadBsonType());
  1231. Assert.Equal("abc", _bsonReader.ReadString());
  1232. Assert.Equal(BsonReaderState.Initial, _bsonReader.State);
  1233. Assert.True(_bsonReader.IsAtEndOfFile());
  1234. }
  1235. Assert.Equal(json, BsonSerializer.Deserialize<string>(json).ToJson());
  1236. }
  1237. [Fact]
  1238. public void TestStringEmpty()
  1239. {
  1240. var json = "\"\"";
  1241. using (_bsonReader = new JsonReader(json))
  1242. {
  1243. Assert.Equal(BsonType.String, _bsonReader.ReadBsonType());
  1244. Assert.Equal("", _bsonReader.ReadString());
  1245. Assert.Equal(BsonReaderState.Initial, _bsonReader.State);
  1246. Assert.True(_bsonReader.IsAtEndOfFile());
  1247. }
  1248. Assert.Equal(json, BsonSerializer.Deserialize<string>(json).ToJson());
  1249. }
  1250. [Fact]
  1251. public void TestSymbol()
  1252. {
  1253. var json = "{ \"$symbol\" : \"symbol\" }";
  1254. using (_bsonReader = new JsonReader(json))
  1255. {
  1256. Assert.Equal(BsonType.Symbol, _bsonReader.ReadBsonType());
  1257. Assert.Equal("symbol", _bsonReader.ReadSymbol());
  1258. Assert.Equal(BsonReaderState.Initial, _bsonReader.State);
  1259. Assert.True(_bsonReader.IsAtEndOfFile());
  1260. }
  1261. Assert.Equal(json, BsonSerializer.Deserialize<BsonSymbol>(json).ToJson());
  1262. }
  1263. [Theory]
  1264. [InlineData("{ $timestamp : { t : 1, i : 2 } }", 0x100000002L)]
  1265. [InlineData("{ $timestamp : { i : 2, t : 1 } }", 0x100000002L)]
  1266. [InlineData("{ $timestamp : { t : -2147483648, i : -2147483648 } }", unchecked((long)0x8000000080000000UL))]
  1267. [InlineData("{ $timestamp : { i : -2147483648, t : -2147483648 } }", unchecked((long)0x8000000080000000UL))]
  1268. [InlineData("{ $timestamp : { t : 2147483647, i : 2147483647 } }", 0x7fffffff7fffffff)]
  1269. [InlineData("{ $timestamp : { i : 2147483647, t : 2147483647 } }", 0x7fffffff7fffffff)]
  1270. [InlineData("Timestamp(1, 2)", 0x100000002L)]
  1271. [InlineData("Timestamp(-2147483648, -2147483648)", unchecked((long)0x8000000080000000UL))]
  1272. [InlineData("Timestamp(2147483647, 2147483647)", 0x7fffffff7fffffff)]
  1273. public void TestTimestamp(string json, long expectedResult)
  1274. {
  1275. using (var reader = new JsonReader(json))
  1276. {
  1277. var result = reader.ReadTimestamp();
  1278. result.Should().Be(expectedResult);
  1279. reader.State.Should().Be(BsonReaderState.Initial);
  1280. reader.IsAtEndOfFile().Should().BeTrue();
  1281. }
  1282. }
  1283. [Fact]
  1284. public void TestTimestampConstructor()
  1285. {
  1286. var json = "Timestamp(1, 2)";
  1287. using (_bsonReader = new JsonReader(json))
  1288. {
  1289. Assert.Equal(BsonType.Timestamp, _bsonReader.ReadBsonType());
  1290. Assert.Equal(new BsonTimestamp(1, 2).Value, _bsonReader.ReadTimestamp());
  1291. Assert.Equal(BsonReaderState.Initial, _bsonReader.State);
  1292. Assert.True(_bsonReader.IsAtEndOfFile());
  1293. }
  1294. Assert.Equal(json, BsonSerializer.Deserialize<BsonTimestamp>(new StringReader(json)).ToJson());
  1295. }
  1296. [Theory]
  1297. [InlineData("{ \"$timestamp\" : { \"t\" : 1, \"i\" : 2 } }")]
  1298. [InlineData("{ \"$timestamp\" : { \"i\" : 2, \"t\" : 1 } }")]
  1299. public void TestTimestampExtendedJsonNewRepresentation(string json)
  1300. {
  1301. using (_bsonReader = new JsonReader(json))
  1302. {
  1303. Assert.Equal(BsonType.Timestamp, _bsonReader.ReadBsonType());
  1304. Assert.Equal(new BsonTimestamp(1, 2).Value, _bsonReader.ReadTimestamp());
  1305. Assert.Equal(BsonReaderState.Initial, _bsonReader.State);
  1306. Assert.True(_bsonReader.IsAtEndOfFile());
  1307. }
  1308. var canonicalJson = "Timestamp(1, 2)";
  1309. Assert.Equal(canonicalJson, BsonSerializer.Deserialize<BsonTimestamp>(new StringReader(json)).ToJson());
  1310. }
  1311. [Theory]
  1312. // truncated input
  1313. [InlineData("{ \"$timestamp\" : {")]
  1314. [InlineData("{ \"$timestamp\" : { \"t\"")]
  1315. [InlineData("{ \"$timestamp\" : { \"t\" :")]
  1316. [InlineData("{ \"$timestamp\" : { \"t\" : 1")]
  1317. [InlineData("{ \"$timestamp\" : { \"t\" : 1,")]
  1318. [InlineData("{ \"$timestamp\" : { \"t\" : 1, \"i\"")]
  1319. [InlineData("{ \"$timestamp\" : { \"t\" : 1, \"i\" :")]
  1320. [InlineData("{ \"$timestamp\" : { \"t\" : 1, \"i\" : 2")]
  1321. [InlineData("{ \"$timestamp\" : { \"t\" : 1, \"i\" : 2 }")]
  1322. // valid JSON but not a valid extended JSON BsonTimestamp
  1323. [InlineData("{ \"$timestamp\" : { }")]
  1324. [InlineData("{ \"$timestamp\" : { \"t\" : 1 } }")]
  1325. [InlineData("{ \"$timestamp\" : { \"i\" : 2 } }")]
  1326. [InlineData("{ \"$timestamp\" : { \"t\" : 1, \"i\" : 2.0 } }")]
  1327. [InlineData("{ \"$timestamp\" : { \"t\" : 1.0, \"x\" : 2 } }")]
  1328. [InlineData("{ \"$timestamp\" : { \"t\" : 1, \"x\" : 2 } }")]
  1329. [InlineData("{ \"$timestamp\" : { \"i\" : 2, \"x\" : 1 } }")]
  1330. public void TestTimestampExtendedJsonNewRepresentationWhenInvalid(string json)
  1331. {
  1332. using (_bsonReader = new JsonReader(json))
  1333. {
  1334. var exception = Record.Exception(() => _bsonReader.ReadBsonType());
  1335. exception.Should().BeOfType<FormatException>();
  1336. }
  1337. }
  1338. [Fact]
  1339. public void TestTimestampExtendedJsonOldRepresentation()
  1340. {
  1341. var json = "{ \"$timestamp\" : NumberLong(1234) }";
  1342. using (_bsonReader = new JsonReader(json))
  1343. {
  1344. Assert.Equal(BsonType.Timestamp, _bsonReader.ReadBsonType());
  1345. Assert.Equal(1234L, _bsonReader.ReadTimestamp());
  1346. Assert.Equal(BsonReaderState.Initial, _bsonReader.State);
  1347. Assert.True(_bsonReader.IsAtEndOfFile());
  1348. }
  1349. var canonicalJson = "Timestamp(0, 1234)";
  1350. Assert.Equal(canonicalJson, BsonSerializer.Deserialize<BsonTimestamp>(new StringReader(json)).ToJson());
  1351. }
  1352. [Theory]
  1353. [InlineData("{ $undefined : true }")]
  1354. [InlineData("undefined")]
  1355. public void TestUndefined(string json)
  1356. {
  1357. using (var reader = new JsonReader(json))
  1358. {
  1359. reader.ReadUndefined();
  1360. reader.State.Should().Be(BsonReaderState.Initial);
  1361. reader.IsAtEndOfFile().Should().BeTrue();
  1362. }
  1363. }
  1364. [Fact]
  1365. public void TestUndefinedExtendedJson()
  1366. {
  1367. var json = "{ \"$undefined\" : true }";
  1368. using (_bsonReader = new JsonReader(json))
  1369. {
  1370. Assert.Equal(BsonType.Undefined, _bsonReader.ReadBsonType());
  1371. _bsonReader.ReadUndefined();
  1372. Assert.Equal(BsonReaderState.Initial, _bsonReader.State);
  1373. Assert.True(_bsonReader.IsAtEndOfFile());
  1374. }
  1375. var canonicalJson = "undefined";
  1376. Assert.Equal(canonicalJson, BsonSerializer.Deserialize<BsonUndefined>(new StringReader(json)).ToJson());
  1377. }
  1378. [Fact]
  1379. public void TestUndefinedKeyword()
  1380. {
  1381. var json = "undefined";
  1382. using (_bsonReader = new JsonReader(json))
  1383. {
  1384. Assert.Equal(BsonType.Undefined, _bsonReader.ReadBsonType());
  1385. _bsonReader.ReadUndefined();
  1386. Assert.Equal(BsonReaderState.Initial, _bsonReader.State);
  1387. Assert.True(_bsonReader.IsAtEndOfFile());
  1388. }
  1389. Assert.Equal(json, BsonSerializer.Deserialize<BsonUndefined>(json).ToJson());
  1390. }
  1391. [Fact]
  1392. public void TestUtf16BigEndian()
  1393. {
  1394. var encoding = new UnicodeEncoding(true, false, true);
  1395. var bytes = BsonUtils.ParseHexString("007b00200022007800220020003a002000310020007d");
  1396. using (var memoryStream = new MemoryStream(bytes))
  1397. using (var streamReader = new StreamReader(memoryStream, encoding))
  1398. {
  1399. var document = BsonSerializer.Deserialize<BsonDocument>(streamReader);
  1400. Assert.Equal(1, document["x"].AsInt32);
  1401. }
  1402. }
  1403. [Fact]
  1404. public void TestUtf16BigEndianAutoDetect()
  1405. {
  1406. var bytes = BsonUtils.ParseHexString("feff007b00200022007800220020003a002000310020007d");
  1407. using (var memoryStream = new MemoryStream(bytes))
  1408. using (var streamReader = new StreamReader(memoryStream, true))
  1409. {
  1410. var document = BsonSerializer.Deserialize<BsonDocument>(streamReader);
  1411. Assert.Equal(1, document["x"].AsInt32);
  1412. }
  1413. }
  1414. [Fact]
  1415. public void TestUtf16LittleEndian()
  1416. {
  1417. var encoding = new UnicodeEncoding(false, false, true);
  1418. var bytes = BsonUtils.ParseHexString("7b00200022007800220020003a002000310020007d00");
  1419. using (var memoryStream = new MemoryStream(bytes))
  1420. using (var streamReader = new StreamReader(memoryStream, encoding))
  1421. {
  1422. var document = BsonSerializer.Deserialize<BsonDocument>(streamReader);
  1423. Assert.Equal(1, document["x"].AsInt32);
  1424. }
  1425. }
  1426. [Fact]
  1427. public void TestUtf16LittleEndianAutoDetect()
  1428. {
  1429. var bytes = BsonUtils.ParseHexString("fffe7b00200022007800220020003a002000310020007d00");
  1430. using (var memoryStream = new MemoryStream(bytes))
  1431. using (var streamReader = new StreamReader(memoryStream, true))
  1432. {
  1433. var document = BsonSerializer.Deserialize<BsonDocument>(streamReader);
  1434. Assert.Equal(1, document["x"].AsInt32);
  1435. }
  1436. }
  1437. [Fact]
  1438. public void TestUtf8()
  1439. {
  1440. var encoding = Utf8Encodings.Strict;
  1441. var bytes = BsonUtils.ParseHexString("7b20227822203a2031207d");
  1442. using (var memoryStream = new MemoryStream(bytes))
  1443. using (var streamReader = new StreamReader(memoryStream, encoding))
  1444. {
  1445. var document = BsonSerializer.Deserialize<BsonDocument>(streamReader);
  1446. Assert.Equal(1, document["x"].AsInt32);
  1447. }
  1448. }
  1449. [Fact]
  1450. public void TestUtf8AutoDetect()
  1451. {
  1452. var bytes = BsonUtils.ParseHexString("7b20227822203a2031207d");
  1453. using (var memoryStream = new MemoryStream(bytes))
  1454. using (var streamReader = new StreamReader(memoryStream, true))
  1455. {
  1456. var document = BsonSerializer.Deserialize<BsonDocument>(streamReader);
  1457. Assert.Equal(1, document["x"].AsInt32);
  1458. }
  1459. }
  1460. [Theory]
  1461. [InlineData("")]
  1462. [InlineData("000")]
  1463. [InlineData("/")]
  1464. [InlineData(":")]
  1465. [InlineData("@")]
  1466. [InlineData("G")]
  1467. [InlineData("`")]
  1468. [InlineData("g")]
  1469. [InlineData("0/")]
  1470. [InlineData("0:")]
  1471. [InlineData("0@")]
  1472. [InlineData("0G")]
  1473. [InlineData("0`")]
  1474. [InlineData("0g")]
  1475. [InlineData("/0")]
  1476. [InlineData(":0")]
  1477. [InlineData("@0")]
  1478. [InlineData("G0")]
  1479. [InlineData("`0")]
  1480. [InlineData("g0")]
  1481. public void IsValidBinaryDataSubTypeString_should_return_false_when_value_is_valid(string value)
  1482. {
  1483. using (var reader = new JsonReader(""))
  1484. {
  1485. var result = reader.IsValidBinaryDataSubTypeString(value);
  1486. result.Should().BeFalse();
  1487. }
  1488. }
  1489. [Theory]
  1490. [InlineData("0")]
  1491. [InlineData("9")]
  1492. [InlineData("a")]
  1493. [InlineData("f")]
  1494. [InlineData("A")]
  1495. [InlineData("F")]
  1496. [InlineData("00")]
  1497. [InlineData("09")]
  1498. [InlineData("0a")]
  1499. [InlineData("0f")]
  1500. [InlineData("0A")]
  1501. [InlineData("0F")]
  1502. [InlineData("90")]
  1503. [InlineData("a0")]
  1504. [InlineData("f0")]
  1505. [InlineData("A0")]
  1506. [InlineData("F0")]
  1507. public void IsValidBinaryDataSubTypeString_should_return_true_when_value_is_valid(string value)
  1508. {
  1509. using (var reader = new JsonReader(""))
  1510. {
  1511. var result = reader.IsValidBinaryDataSubTypeString(value);
  1512. result.Should().BeTrue();
  1513. }
  1514. }
  1515. }
  1516. public static class JsonReaderReflector
  1517. {
  1518. public static bool IsValidBinaryDataSubTypeString(this JsonReader reader, string value)
  1519. {
  1520. return (bool)Reflector.Invoke(reader, nameof(IsValidBinaryDataSubTypeString), value);
  1521. }
  1522. }
  1523. }