public void BsonBinaryReader_should_support_reading_more_than_2GB()
        {
            RequireEnvironment.Check().EnvironmentVariable("EXPLICIT");

            var binaryData = new BsonBinaryData(new byte[1024 * 1024]);

            var tempFileName = Path.GetTempFileName();

            try
            {
                using (var stream = new FileStream(tempFileName, FileMode.Open))
                {
                    using (var binaryWriter = new BsonBinaryWriter(stream))
                    {
                        while (stream.Position < (long)int.MaxValue * 4)
                        {
                            binaryWriter.WriteStartDocument();
                            binaryWriter.WriteName("x");
                            binaryWriter.WriteBinaryData(binaryData);
                            binaryWriter.WriteEndDocument();
                        }
                    }

                    var endOfFilePosition = stream.Position;
                    stream.Position = 0;

                    using (var binaryReader = new BsonBinaryReader(stream))
                    {
                        while (!binaryReader.IsAtEndOfFile())
                        {
                            binaryReader.ReadStartDocument();
                            var bookmark = binaryReader.GetBookmark();

                            binaryReader.ReadName("x");
                            binaryReader.ReturnToBookmark(bookmark);

                            binaryReader.ReadName("x");
                            var readBinaryData = binaryReader.ReadBinaryData();
                            Assert.Equal(binaryData.Bytes.Length, readBinaryData.Bytes.Length);

                            binaryReader.ReadEndDocument();
                        }
                    }

                    Assert.Equal(endOfFilePosition, stream.Position);
                }
            }
            finally
            {
                try
                {
                    File.Delete(tempFileName);
                }
                catch
                {
                    // ignore exceptions
                }
            }
        }
Example #2
0
        public void BackpatchSize_should_throw_when_size_is_larger_than_2GB()
        {
            using (var stream = new NullBsonStream())
                using (var binaryWriter = new BsonBinaryWriter(stream))
                {
                    var bytes      = new byte[int.MaxValue / 2]; // 1GB
                    var binaryData = new BsonBinaryData(bytes);

                    binaryWriter.WriteStartDocument();
                    binaryWriter.WriteName("array");
                    binaryWriter.WriteStartArray();
                    binaryWriter.WriteBinaryData(binaryData);
                    binaryWriter.WriteBinaryData(binaryData);

                    Action action = () => binaryWriter.WriteEndArray(); // indirectly calls private BackpatchSize method

                    action.ShouldThrow <FormatException>();
                }
        }
        public void BsonBinaryWriter_should_support_writing_more_than_2GB()
        {
            using (var stream = new NullBsonStream())
                using (var binaryWriter = new BsonBinaryWriter(stream))
                {
                    var bigBinaryData = new BsonBinaryData(new byte[int.MaxValue / 2 - 1000]);
                    for (var i = 0; i < 3; i++)
                    {
                        binaryWriter.WriteStartDocument();
                        binaryWriter.WriteName("x");
                        binaryWriter.WriteBinaryData(bigBinaryData);
                        binaryWriter.WriteEndDocument();
                    }

                    var smallBinaryData = new BsonBinaryData(new byte[2000]);
                    binaryWriter.WriteStartDocument();
                    binaryWriter.WriteName("x");
                    binaryWriter.WriteBinaryData(smallBinaryData);
                    binaryWriter.WriteEndDocument();
                }
        }