Пример #1
0
        public void EndOfFile()
        {
            using (var ms = new MemoryStream())
            {
                var writeBuffer = BlockStreamTests.GetRandomBytes(100);
                var readBuffer  = new byte[60];

                using (var bgzipStream = new BlockGZipStream(ms, CompressionMode.Compress, true))
                {
                    bgzipStream.Write(writeBuffer, 0, writeBuffer.Length);
                }

                ms.Position = 0;

                using (var bgzipStream = new BlockGZipStream(ms, CompressionMode.Decompress))
                {
                    int numBytesRead = bgzipStream.Read(readBuffer, 0, 0);
                    Assert.Equal(0, numBytesRead);

                    numBytesRead = bgzipStream.Read(readBuffer, 0, readBuffer.Length);
                    Assert.Equal(readBuffer.Length, numBytesRead);

                    numBytesRead = bgzipStream.Read(readBuffer, 0, readBuffer.Length);
                    Assert.Equal(writeBuffer.Length - readBuffer.Length, numBytesRead);

                    numBytesRead = bgzipStream.Read(readBuffer, 0, readBuffer.Length);
                    Assert.Equal(0, numBytesRead);
                }
            }
        }
Пример #2
0
        public void UncompressedBlockTruncation()
        {
            const int bufferSize = 10000;
            var       buffer     = BlockStreamTests.GetRandomBytes(bufferSize);

            int copyLength;
            var ms = GetBlockStream(Qlz, buffer, bufferSize, out copyLength);

            ms.Seek(0, SeekOrigin.Begin);

            var readBlock = new Block(Qlz);

            using (var truncatedMs = new MemoryStream())
            {
                truncatedMs.Write(ms.ToArray(), 0, (int)ms.Length - 100);
                truncatedMs.Seek(0, SeekOrigin.Begin);
                // ReSharper disable once AccessToDisposedClosure
                Assert.Throws <IOException>(delegate { readBlock.Read(truncatedMs); });
            }
        }
Пример #3
0
        public void ReadBlockCorrupted()
        {
            using (var ms = new MemoryStream())
                using (var truncatedMs = new MemoryStream())
                    using (var corruptMs = new MemoryStream())
                    {
                        using (var bgzipStream = new BlockGZipStream(ms, CompressionMode.Compress, true))
                            using (var writer = new StreamWriter(bgzipStream, Encoding.ASCII, 4096))
                            {
                                writer.WriteLine("The quick brown fox jumped over the lazy dog.");
                            }

                        var compressedData = ms.ToArray();

                        truncatedMs.Write(compressedData, 0, compressedData.Length - 10);
                        truncatedMs.Position = 0;

                        corruptMs.Write(compressedData, 0, BlockGZipStream.BlockGZipFormatCommon.BlockHeaderLength);
                        var randomBytes = BlockStreamTests.GetRandomBytes(500);
                        corruptMs.Write(randomBytes, 0, randomBytes.Length);
                        corruptMs.Position = 0;

                        var readBuffer = new byte[60];

                        Assert.Throws <CompressionException>(delegate
                        {
                            using (var bgzipStream = new BlockGZipStream(truncatedMs, CompressionMode.Decompress))
                            {
                                bgzipStream.Read(readBuffer, 0, readBuffer.Length);
                            }
                        });

                        Assert.Throws <CompressionException>(delegate
                        {
                            using (var bgzipStream = new BlockGZipStream(corruptMs, CompressionMode.Decompress))
                            {
                                bgzipStream.Read(readBuffer, 0, readBuffer.Length);
                            }
                        });
                    }
        }
Пример #4
0
 /// <summary>
 /// constructor
 /// </summary>
 public CompressionAlgorithmTests()
 {
     _originalBytes = BlockStreamTests.GetRandomBytes(NumOriginalBytes);
 }
 public CompressionAlgorithmTests()
 {
     _originalBytes = BlockStreamTests.GetRandomBytes(NumOriginalBytes);
     _dictBuffer    = LoadZstandardDictionary();
 }