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

            using (MemoryStream compressed = new MemoryStream())
            {
                LzmaEncoder encoder = new LzmaEncoder();
                encoder.Encode(s_sampledata, compressed);
                compressed.Flush();

                // Check the constructor for ArgumentNullException while we're here
                try { using (LzmaReader decompressor = new LzmaReader(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 (LzmaReader decompressor = new LzmaReader(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)); }
                }

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

                    while (decompressor.Read(buffer, 0, 8192) != 0)
                    {
                    }
                }
            }
        }
Beispiel #2
0
        public void Lzma_Dispose()
        {
            byte[] buffer = new byte[8192];                     // 8KiB data buffer

            // Create a memorystream to hold the compressed sample data
            using (MemoryStream ms = new MemoryStream())
            {
                LzmaEncoder encoder = new LzmaEncoder();
                encoder.Encode(s_sampledata, ms);

                // Create a decompression stream and immediately dispose of it
                LzmaReader stream = new LzmaReader(ms);
                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)); }
            }
        }
Beispiel #3
0
        public void Lzma_Position()
        {
            // Start with a MemoryStream created from the sample data
            using (MemoryStream compressed = new MemoryStream(s_sampledata))
            {
                LzmaEncoder encoder = new LzmaEncoder();
                encoder.Encode(s_sampledata, compressed);

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

                    // Read some data from the stream, position should be non-zero
                    byte[] buffer = new byte[8192];
                    reader.Read(buffer, 0, 8192);
                    Assert.AreNotEqual(0L, reader.Position);
                }
            }
        }