Beispiel #1
0
        public void Lzma_CompressDecompressEndMark()
        {
            // Start with a MemoryStream created from the sample data, when a stream is
            // sent into the encoder an end mark is always written
            using (MemoryStream source = new MemoryStream(s_sampledata))
            {
                using (MemoryStream dest = new MemoryStream())
                {
                    // Compress the data into the destination memory stream instance
                    LzmaEncoder encoder = new LzmaEncoder();
                    encoder.WriteEndMark = true;
                    encoder.Encode(source, dest);

                    // 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 (LzmaReader decompressor = new LzmaReader(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()));
                }
            }
        }
Beispiel #2
0
        public void Lzma_CompressDecompressNoEndMark()
        {
            using (MemoryStream dest = new MemoryStream())
            {
                // Compress the data into the destination memory stream instance using
                // the raw sample data array and without an end mark.  Since the length
                // of the input data is known, the encoder will not enforce the end mark
                LzmaEncoder encoder = new LzmaEncoder();
                encoder.WriteEndMark = false;
                encoder.Encode(s_sampledata, dest);

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

                // Decompress the data back into a new memory stream
                using (MemoryStream uncompressed = new MemoryStream())
                {
                    dest.Position = 0;
                    using (LzmaReader decompressor = new LzmaReader(dest, true)) decompressor.CopyTo(uncompressed);

                    // Ensure that the original data has been restored
                    Assert.AreEqual(uncompressed.Length, s_sampledata.Length);
                    Assert.IsTrue(s_sampledata.SequenceEqual(uncompressed.ToArray()));
                }
            }
        }
Beispiel #3
0
 public void Lzma_CanWrite()
 {
     // Verify behavior of a decompression stream
     using (LzmaReader stream = new LzmaReader(new MemoryStream(s_sampledata)))
     {
         Assert.IsFalse(stream.CanWrite);
     }
 }
Beispiel #4
0
 public void Lzma_SetLength()
 {
     // Verify behavior of a decompression stream
     using (LzmaReader stream = new LzmaReader(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)); }
     }
 }
Beispiel #5
0
 public void Lzma_Flush()
 {
     // Verify behavior of flushing a decompression stream
     using (LzmaReader stream = new LzmaReader(new MemoryStream(s_sampledata)))
     {
         // Flush has no effect on decompression streams, just ensure it doesn't throw
         stream.Flush();
     }
 }
Beispiel #6
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 #7
0
        public void Lzma_Write()
        {
            LzmaEncoder encoder    = new LzmaEncoder();
            var         compressed = encoder.Encode(s_sampledata);

            using (LzmaReader reader = new LzmaReader(new MemoryStream(compressed)))
            {
                byte[] buffer = new byte[2048];
                try { reader.Write(buffer, 0, buffer.Length); Assert.Fail("Method call should have thrown an exception"); }
                catch (Exception ex) { Assert.IsInstanceOfType(ex, typeof(NotSupportedException)); }
            }
        }
Beispiel #8
0
 public void Lzma_BaseStream()
 {
     using (MemoryStream dest = new MemoryStream())
     {
         LzmaEncoder encoder = new LzmaEncoder();
         encoder.Encode(s_sampledata, dest);
         using (LzmaReader stream = new LzmaReader(dest))
         {
             Assert.IsNotNull(stream.BaseStream);
             Assert.AreSame(dest, stream.BaseStream);
         }
     }
 }
Beispiel #9
0
        public void Lzma_DecompressExternal()
        {
            // Decompress a stream created externally to this library
            using (LzmaReader reader = new LzmaReader(Assembly.GetExecutingAssembly().GetManifestResourceStream("zuki.io.compression.test.thethreemusketeers.lzma")))
            {
                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()));
                }
            }
        }
Beispiel #10
0
        public void Lzma_Seek()
        {
            // Verify behavior of a decompression stream
            using (LzmaReader stream = new LzmaReader(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)); }
            }
        }
Beispiel #11
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 #12
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);
                }
            }
        }