Example #1
0
        public void Gzip_ReaderDispose()
        {
            byte[] buffer = new byte[8192];                     // 8KiB data buffer

            // Create a dummy stream and immediately dispose of it
            GzipReader stream = new GzipReader(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)); }
        }
Example #2
0
        public void Gzip_GzipException()
        {
            using (MemoryStream compressed = new MemoryStream())
            {
                // Start with a compressed MemoryStream created from the sample data
                using (GzipWriter compressor = new GzipWriter(compressed, CompressionLevel.Optimal, true))
                {
                    compressor.Write(s_sampledata, 0, s_sampledata.Length);
                    compressor.Flush();
                }

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

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

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

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

                    // 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 = (GzipException)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());
                }
            }
        }
Example #3
0
        public void Gzip_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 (GzipWriter compressor = new GzipWriter(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 (GzipReader decompressor = new GzipReader(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 (GzipReader decompressor = new GzipReader(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 GzipException
                    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(GzipException)); }

                    // 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(GzipException)); }
                }

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

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