Ejemplo n.º 1
0
        public void Bzip2_CompressDecompress()
        {
            // Start with a MemoryStream created from the sample data
            using (MemoryStream source = new MemoryStream(s_sampledata))
            {
                using (MemoryStream dest = new MemoryStream())
                {
                    // Compress the data into the destination memory stream instance
                    using (Bzip2Writer compressor = new Bzip2Writer(dest, CompressionLevel.Optimal, true)) source.CopyTo(compressor);

                    // The compressed data should be smaller than the source data
                    Assert.IsTrue(dest.Length < source.Length);

                    source.SetLength(0);                                        // Clear the source stream
                    dest.Position = 0;                                          // Reset the destination stream

                    // Decompress the data back into the source memory stream
                    using (Bzip2Reader decompressor = new Bzip2Reader(dest, true)) decompressor.CopyTo(source);

                    // Ensure that the original data has been restored
                    Assert.AreEqual(source.Length, s_sampledata.Length);
                    Assert.IsTrue(s_sampledata.SequenceEqual(source.ToArray()));
                }
            }
        }
Ejemplo n.º 2
0
        public void Bzip2_Flush()
        {
            // Verify behavior of flushing a compression stream
            using (MemoryStream compressed = new MemoryStream())
            {
                using (Bzip2Writer stream = new Bzip2Writer(compressed, true))
                {
                    stream.Write(s_sampledata, 0, s_sampledata.Length);

                    // Get the unflushed length of the compressed stream and flush it
                    long unflushed = compressed.Length;
                    stream.Flush();

                    // The expectation is that the output stream will be longer after the flush
                    long flushedonce = compressed.Length;
                    Assert.IsTrue(compressed.Length > unflushed);

                    // Flushing the same data a second time should not have any impact at all
                    stream.Flush();
                    Assert.AreEqual(compressed.Length, flushedonce);

                    // The stream should still be writable after a flush operation
                    stream.Write(s_sampledata, 0, s_sampledata.Length / 10);
                }
            }

            // Verify behavior of flushing a decompression stream
            using (Bzip2Reader stream = new Bzip2Reader(new MemoryStream(s_sampledata)))
            {
                // Flush has no effect on decompression streams, just ensure it doesn't throw
                stream.Flush();
            }
        }
Ejemplo n.º 3
0
        public void Bzip2_Seek()
        {
            // Verify behavior of a compression stream
            using (Bzip2Writer stream = new Bzip2Writer(new MemoryStream()))
            {
                try { stream.Seek(50, SeekOrigin.Begin); Assert.Fail("Method call should have thrown an exception"); }
                catch (Exception ex) { Assert.IsInstanceOfType(ex, typeof(NotSupportedException)); }

                try { stream.Seek(0, SeekOrigin.Current); Assert.Fail("Method call should have thrown an exception"); }
                catch (Exception ex) { Assert.IsInstanceOfType(ex, typeof(NotSupportedException)); }

                try { stream.Seek(-50, SeekOrigin.End); Assert.Fail("Method call should have thrown an exception"); }
                catch (Exception ex) { Assert.IsInstanceOfType(ex, typeof(NotSupportedException)); }
            }

            // Verify behavior of a decompression stream
            using (Bzip2Reader stream = new Bzip2Reader(new MemoryStream(s_sampledata)))
            {
                try { stream.Seek(50, SeekOrigin.Begin); Assert.Fail("Method call should have thrown an exception"); }
                catch (Exception ex) { Assert.IsInstanceOfType(ex, typeof(NotSupportedException)); }

                try { stream.Seek(0, SeekOrigin.Current); Assert.Fail("Method call should have thrown an exception"); }
                catch (Exception ex) { Assert.IsInstanceOfType(ex, typeof(NotSupportedException)); }

                try { stream.Seek(-50, SeekOrigin.End); Assert.Fail("Method call should have thrown an exception"); }
                catch (Exception ex) { Assert.IsInstanceOfType(ex, typeof(NotSupportedException)); }
            }
        }
Ejemplo n.º 4
0
        public void Bzip2_CanWrite()
        {
            // Verify behavior of a compression stream
            using (Bzip2Writer stream = new Bzip2Writer(new MemoryStream()))
            {
                Assert.IsTrue(stream.CanWrite);
            }

            // Verify behavior of a decompression stream
            using (Bzip2Reader stream = new Bzip2Reader(new MemoryStream(s_sampledata)))
            {
                Assert.IsFalse(stream.CanWrite);
            }
        }
Ejemplo n.º 5
0
        public void Bzip2_ReaderDispose()
        {
            byte[] buffer = new byte[8192];                     // 8KiB data buffer

            // Create a dummy stream and immediately dispose of it
            Bzip2Reader stream = new Bzip2Reader(new MemoryStream(s_sampledata));

            stream.Dispose();

            // Test double dispose
            stream.Dispose();

            // All properties and methods should throw an ObjectDisposedException
            try { var bs = stream.BaseStream; Assert.Fail("Property access should have thrown an exception"); }
            catch (Exception ex) { Assert.IsInstanceOfType(ex, typeof(ObjectDisposedException)); }

            try { var b = stream.CanRead; Assert.Fail("Property access should have thrown an exception"); }
            catch (Exception ex) { Assert.IsInstanceOfType(ex, typeof(ObjectDisposedException)); }

            try { var b = stream.CanSeek; Assert.Fail("Property access should have thrown an exception"); }
            catch (Exception ex) { Assert.IsInstanceOfType(ex, typeof(ObjectDisposedException)); }

            try { var b = stream.CanWrite; Assert.Fail("Property access should have thrown an exception"); }
            catch (Exception ex) { Assert.IsInstanceOfType(ex, typeof(ObjectDisposedException)); }

            try { stream.Flush(); Assert.Fail("Method call should have thrown an exception"); }
            catch (Exception ex) { Assert.IsInstanceOfType(ex, typeof(ObjectDisposedException)); }

            try { var l = stream.Length; Assert.Fail("Property access should have thrown an exception"); }
            catch (Exception ex) { Assert.IsInstanceOfType(ex, typeof(ObjectDisposedException)); }

            try { var l = stream.Position; Assert.Fail("Property access should have thrown an exception"); }
            catch (Exception ex) { Assert.IsInstanceOfType(ex, typeof(ObjectDisposedException)); }

            try { stream.Position = 12345L; Assert.Fail("Property access should have thrown an exception"); }
            catch (Exception ex) { Assert.IsInstanceOfType(ex, typeof(ObjectDisposedException)); }

            try { stream.Read(buffer, 0, 8192); Assert.Fail("Method call should have thrown an exception"); }
            catch (Exception ex) { Assert.IsInstanceOfType(ex, typeof(ObjectDisposedException)); }

            try { stream.Seek(0, SeekOrigin.Current); Assert.Fail("Method call should have thrown an exception"); }
            catch (Exception ex) { Assert.IsInstanceOfType(ex, typeof(ObjectDisposedException)); }

            try { stream.SetLength(12345L); Assert.Fail("Method call should have thrown an exception"); }
            catch (Exception ex) { Assert.IsInstanceOfType(ex, typeof(ObjectDisposedException)); }

            try { stream.Write(buffer, 0, 8192); Assert.Fail("Method call should have thrown an exception"); }
            catch (Exception ex) { Assert.IsInstanceOfType(ex, typeof(ObjectDisposedException)); }
        }
Ejemplo n.º 6
0
        public void Bzip2_DecompressExternal()
        {
            // Decompress a stream created externally to this library
            using (Bzip2Reader reader = new Bzip2Reader(Assembly.GetExecutingAssembly().GetManifestResourceStream("zuki.io.compression.test.thethreemusketeers.bz2")))
            {
                using (MemoryStream dest = new MemoryStream())
                {
                    reader.CopyTo(dest);
                    dest.Flush();

                    // Verify that the output matches the sample data byte-for-byte
                    Assert.IsTrue(Enumerable.SequenceEqual(s_sampledata, dest.ToArray()));
                }
            }
        }
Ejemplo n.º 7
0
        public void Bzip2_SetLength()
        {
            // Verify behavior of a compression stream
            using (Bzip2Writer stream = new Bzip2Writer(new MemoryStream()))
            {
                try { stream.SetLength(12345L); Assert.Fail("Method call should have thrown an exception"); }
                catch (Exception ex) { Assert.IsInstanceOfType(ex, typeof(NotSupportedException)); }
            }

            // Verify behavior of a decompression stream
            using (Bzip2Reader stream = new Bzip2Reader(new MemoryStream(s_sampledata)))
            {
                try { stream.SetLength(12345L); Assert.Fail("Method call should have thrown an exception"); }
                catch (Exception ex) { Assert.IsInstanceOfType(ex, typeof(NotSupportedException)); }
            }
        }
Ejemplo n.º 8
0
        public void Bzip2_BaseStream()
        {
            using (MemoryStream source = new MemoryStream())
            {
                using (Bzip2Writer stream = new Bzip2Writer(source, CompressionLevel.Optimal))
                {
                    Assert.IsNotNull(stream.BaseStream);
                    Assert.AreSame(source, stream.BaseStream);
                }

                using (Bzip2Reader stream = new Bzip2Reader(source, true))
                {
                    Assert.IsNotNull(stream.BaseStream);
                    Assert.AreSame(source, stream.BaseStream);
                }
            }
        }
Ejemplo n.º 9
0
        public void Bzip2_Bzip2Exception()
        {
            using (MemoryStream compressed = new MemoryStream())
            {
                // Start with a compressed MemoryStream created from the sample data
                using (Bzip2Writer compressor = new Bzip2Writer(compressed, CompressionLevel.Optimal, true))
                {
                    compressor.Write(s_sampledata, 0, s_sampledata.Length);
                    compressor.Flush();
                }

                byte[]         buffer       = new byte[8192];
                Bzip2Exception thrown       = null;
                Bzip2Exception deserialized = null;

                // Create a decompressor to test exception cases
                using (Bzip2Reader decompressor = new Bzip2Reader(compressed, true))
                {
                    // Attempting to read from the middle of the compressed stream should throw a Bzip2Exception
                    compressed.Position = compressed.Length / 2;
                    try { decompressor.Read(buffer, 0, 8192); Assert.Fail("Method call should have thrown an exception"); }
                    catch (Bzip2Exception ex) { thrown = ex; }

                    Assert.IsNotNull(thrown);
                    Assert.IsInstanceOfType(thrown, typeof(Bzip2Exception));

                    // Check the error code property
                    Assert.AreEqual(-5, thrown.ErrorCode);                          // BZ_DATA_ERROR_MAGIC (-5)

                    // Serialize and de-serialize the exception with a BinaryFormatter
                    BinaryFormatter formatter = new BinaryFormatter();
                    using (MemoryStream memstream = new MemoryStream())
                    {
                        formatter.Serialize(memstream, thrown);
                        memstream.Seek(0, 0);
                        deserialized = (Bzip2Exception)formatter.Deserialize(memstream);
                    }

                    // Check that the exceptions are equivalent
                    Assert.AreEqual(thrown.ErrorCode, deserialized.ErrorCode);
                    Assert.AreEqual(thrown.StackTrace, deserialized.StackTrace);
                    Assert.AreEqual(thrown.ToString(), deserialized.ToString());
                }
            }
        }
Ejemplo n.º 10
0
        public void Bzip2_Write()
        {
            byte[] buffer = new byte[8192];                     // 8KiB data buffer

            // Compress the sample data using a call to Write directly
            using (MemoryStream compressed = new MemoryStream())
            {
                // Check the constructor for ArgumentNullException while we're here
                try { using (Bzip2Writer compressor = new Bzip2Writer(null)) { }; Assert.Fail("Constructor should have thrown an exception"); }
                catch (Exception ex) { Assert.IsInstanceOfType(ex, typeof(ArgumentNullException)); }

                using (Bzip2Writer compressor = new Bzip2Writer(compressed, CompressionLevel.Optimal, true))
                {
                    // Send in some bum arguments to Write() to check they are caught
                    try { compressor.Write(null); Assert.Fail("Method call should have thrown an exception"); }
                    catch (Exception ex) { Assert.IsInstanceOfType(ex, typeof(ArgumentNullException)); }

                    try { compressor.Write(null, 0, 0); Assert.Fail("Method call should have thrown an exception"); }
                    catch (Exception ex) { Assert.IsInstanceOfType(ex, typeof(ArgumentNullException)); }

                    try { compressor.Write(s_sampledata, -1, 0); Assert.Fail("Method call should have thrown an exception"); }
                    catch (Exception ex) { Assert.IsInstanceOfType(ex, typeof(ArgumentOutOfRangeException)); }

                    try { compressor.Write(s_sampledata, 0, -1); Assert.Fail("Method call should have thrown an exception"); }
                    catch (Exception ex) { Assert.IsInstanceOfType(ex, typeof(ArgumentOutOfRangeException)); }

                    try { compressor.Write(s_sampledata, 0, s_sampledata.Length + 1024); Assert.Fail("Method call should have thrown an exception"); }
                    catch (Exception ex) { Assert.IsInstanceOfType(ex, typeof(ArgumentException)); }

                    // Not writing anything shouldn't throw an exception
                    compressor.Write(s_sampledata, 0, 0);

                    // Compress the data; there really isn't much that can go wrong with Write() itself
                    compressor.Write(s_sampledata, 0, s_sampledata.Length);
                    compressor.Flush();
                }

                using (Bzip2Reader reader = new Bzip2Reader(compressed, true))
                {
                    try { reader.Write(buffer, 0, buffer.Length); Assert.Fail("Method call should have thrown an exception"); }
                    catch (Exception ex) { Assert.IsInstanceOfType(ex, typeof(NotSupportedException)); }
                }
            }
        }
Ejemplo n.º 11
0
        public void Bzip2_Position()
        {
            // Start with a MemoryStream created from the sample data
            using (MemoryStream source = new MemoryStream(s_sampledata))
            {
                using (MemoryStream dest = new MemoryStream())
                {
                    // Test a compression stream
                    using (Bzip2Writer compressor = new Bzip2Writer(dest, CompressionLevel.Optimal, true))
                    {
                        // The stream should report position zero prior to compression
                        Assert.AreEqual(0L, compressor.Position);
                        source.CopyTo(compressor);

                        // The stream should report non-zero after compression
                        Assert.AreNotEqual(0L, compressor.Position);

                        // Attempting to set the position on the stream should throw
                        try { compressor.Position = 12345L; Assert.Fail("Property should have thrown an exception"); }
                        catch (Exception ex) { Assert.IsInstanceOfType(ex, typeof(NotSupportedException)); }
                    }

                    source.SetLength(0);                                // Clear the source stream
                    dest.Position = 0;                                  // Reset the destination stream

                    // Test a decompression stream
                    using (Bzip2Reader decompressor = new Bzip2Reader(dest, true))
                    {
                        // The stream should report position zero prior to compression
                        Assert.AreEqual(0L, decompressor.Position);
                        decompressor.CopyTo(source);

                        // The stream should report non-zero after compression
                        Assert.AreNotEqual(0L, decompressor.Position);

                        // Attempting to set the position on the stream should throw
                        try { decompressor.Position = 12345L; Assert.Fail("Property should have thrown an exception"); }
                        catch (Exception ex) { Assert.IsInstanceOfType(ex, typeof(NotSupportedException)); }
                    }
                }
            }
        }
Ejemplo n.º 12
0
        public void Bzip2_Read()
        {
            byte[] buffer = new byte[8192];                             // 8KiB data buffer

            using (MemoryStream compressed = new MemoryStream())
            {
                // Start with a compressed MemoryStream created from the sample data
                using (Bzip2Writer compressor = new Bzip2Writer(compressed, CompressionLevel.Optimal, true))
                {
                    try { compressor.Read(buffer, 0, 8192); Assert.Fail("Method call should have thrown an exception"); }
                    catch (Exception ex) { Assert.IsInstanceOfType(ex, typeof(NotSupportedException)); }

                    compressor.Write(s_sampledata, 0, s_sampledata.Length);
                    compressor.Flush();
                }

                // Check the constructor for ArgumentNullException while we're here
                try { using (Bzip2Reader decompressor = new Bzip2Reader(null, false)) { }; Assert.Fail("Constructor should have thrown an exception"); }
                catch (Exception ex) { Assert.IsInstanceOfType(ex, typeof(ArgumentNullException)); }

                // Create a decompressor to test some of the error cases
                using (Bzip2Reader decompressor = new Bzip2Reader(compressed, true))
                {
                    // Send in some bum arguments to Read() to check they are caught
                    try { decompressor.Read(null, 0, 0); Assert.Fail("Method call should have thrown an exception"); }
                    catch (Exception ex) { Assert.IsInstanceOfType(ex, typeof(ArgumentNullException)); }

                    try { decompressor.Read(buffer, -1, 0); Assert.Fail("Method call should have thrown an exception"); }
                    catch (Exception ex) { Assert.IsInstanceOfType(ex, typeof(ArgumentOutOfRangeException)); }

                    try { decompressor.Read(buffer, 0, -1); Assert.Fail("Method call should have thrown an exception"); }
                    catch (Exception ex) { Assert.IsInstanceOfType(ex, typeof(ArgumentOutOfRangeException)); }

                    try { decompressor.Read(buffer, 0, buffer.Length + 1024); Assert.Fail("Method call should have thrown an exception"); }
                    catch (Exception ex) { Assert.IsInstanceOfType(ex, typeof(ArgumentException)); }

                    // Attempting to read from the end of the compressed stream should throw an InvalidDataException
                    try { decompressor.Read(buffer, 0, 8192); Assert.Fail("Method call should have thrown an exception"); }
                    catch (Exception ex) { Assert.IsInstanceOfType(ex, typeof(InvalidDataException)); }

                    // Attempting to read from the middle of the compressed stream should throw a Bzip2Exception
                    compressed.Position = compressed.Position / 2;
                    try { decompressor.Read(buffer, 0, 8192); Assert.Fail("Method call should have thrown an exception"); }
                    catch (Exception ex) { Assert.IsInstanceOfType(ex, typeof(Bzip2Exception)); }

                    // The decompression stream is trashed at this point since the input buffer was filled
                    // with data from the middle.  Thought about a special case handler for that, but it's
                    // a fringe case.  Verify that the stream is indeed trashed ...
                    compressed.Position = 0;
                    try { decompressor.Read(buffer, 0, 8192); Assert.Fail("Method call should have thrown an exception"); }
                    catch (Exception ex) { Assert.IsInstanceOfType(ex, typeof(Bzip2Exception)); }
                }

                // Create a new decompressor against the same stream and make sure it doesn't throw
                using (Bzip2Reader decompressor = new Bzip2Reader(compressed, true))
                {
                    // Reading zero bytes should not throw an exception
                    decompressor.Read(buffer, 0, 0);

                    while (decompressor.Read(buffer, 0, 8192) != 0)
                    {
                    }
                }
            }
        }