PageRenderTime 44ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

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

http://github.com/mongodb/mongo-csharp-driver
C# | 104 lines | 78 code | 12 blank | 14 comment | 2 complexity | 2b7e787a1d9d73ea41cec8952fd755aa 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.IO;
  17. using System.Linq;
  18. using FluentAssertions;
  19. using MongoDB.Bson.IO;
  20. using MongoDB.Bson.TestHelpers;
  21. using MongoDB.Bson.TestHelpers.IO;
  22. using MongoDB.Bson.TestHelpers.XunitExtensions;
  23. using Xunit;
  24. namespace MongoDB.Bson.Tests.IO
  25. {
  26. public class BsonBinaryWriterTests
  27. {
  28. [Theory]
  29. [ParameterAttributeData]
  30. public void BsonBinaryWriter_should_support_writing_multiple_documents(
  31. [Range(0, 3)]
  32. int numberOfDocuments)
  33. {
  34. var document = new BsonDocument("x", 1);
  35. var bson = document.ToBson();
  36. var expectedResult = Enumerable.Repeat(bson, numberOfDocuments).Aggregate(Enumerable.Empty<byte>(), (a, b) => a.Concat(b)).ToArray();
  37. using (var stream = new MemoryStream())
  38. using (var binaryWriter = new BsonBinaryWriter(stream))
  39. {
  40. for (var n = 0; n < numberOfDocuments; n++)
  41. {
  42. binaryWriter.WriteStartDocument();
  43. binaryWriter.WriteName("x");
  44. binaryWriter.WriteInt32(1);
  45. binaryWriter.WriteEndDocument();
  46. }
  47. var result = stream.ToArray();
  48. result.Should().Equal(expectedResult);
  49. }
  50. }
  51. [SkippableFact]
  52. public void BsonBinaryWriter_should_support_writing_more_than_2GB()
  53. {
  54. RequireProcess.Check().Bits(64);
  55. using (var stream = new NullBsonStream())
  56. using (var binaryWriter = new BsonBinaryWriter(stream))
  57. {
  58. var bigBinaryData = new BsonBinaryData(new byte[int.MaxValue / 2 - 1000]);
  59. for (var i = 0; i < 3; i++)
  60. {
  61. binaryWriter.WriteStartDocument();
  62. binaryWriter.WriteName("x");
  63. binaryWriter.WriteBinaryData(bigBinaryData);
  64. binaryWriter.WriteEndDocument();
  65. }
  66. var smallBinaryData = new BsonBinaryData(new byte[2000]);
  67. binaryWriter.WriteStartDocument();
  68. binaryWriter.WriteName("x");
  69. binaryWriter.WriteBinaryData(smallBinaryData);
  70. binaryWriter.WriteEndDocument();
  71. }
  72. }
  73. [SkippableFact]
  74. public void BackpatchSize_should_throw_when_size_is_larger_than_2GB()
  75. {
  76. RequireProcess.Check().Bits(64);
  77. using (var stream = new NullBsonStream())
  78. using (var binaryWriter = new BsonBinaryWriter(stream))
  79. {
  80. var bytes = new byte[int.MaxValue / 2]; // 1GB
  81. var binaryData = new BsonBinaryData(bytes);
  82. binaryWriter.WriteStartDocument();
  83. binaryWriter.WriteName("array");
  84. binaryWriter.WriteStartArray();
  85. binaryWriter.WriteBinaryData(binaryData);
  86. binaryWriter.WriteBinaryData(binaryData);
  87. Action action = () => binaryWriter.WriteEndArray(); // indirectly calls private BackpatchSize method
  88. action.ShouldThrow<FormatException>();
  89. }
  90. }
  91. }
  92. }