Esempio n. 1
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()));
                }
            }
        }
Esempio n. 2
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()));
                }
            }
        }
Esempio n. 3
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)); }
            }
        }
Esempio n. 4
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)); }
            }
        }
Esempio n. 5
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);
         }
     }
 }
Esempio n. 6
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)
                    {
                    }
                }
            }
        }
Esempio n. 7
0
        public void Lzma_CompressExternal()
        {
            // This method generates an output file that can be tested externally; "thethreemusketeers.txt" is
            // set to Copy Always to the output directory, it can be diffed after running the external tool
            LzmaEncoder encoder = new LzmaEncoder();

            // Don't use an endmark with this test, the commandline xz-utils version of "lzma" cannot decompress
            // files with an endmark from what I can gather.  Using 7-zip is recommended instead to test, it can
            // open both flavors of LZMA and decompress them successfully
            encoder.WriteEndMark = false;

            using (var outfile = File.Create(Path.Combine(Environment.CurrentDirectory, "thethreemusketeers.lzma")))
            {
                encoder.Encode(s_sampledata, outfile);
                outfile.Flush();
            }
        }
Esempio n. 8
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);
                }
            }
        }
Esempio n. 9
0
        public void Lzma_Encoder()
        {
            LzmaEncoder encoder = new LzmaEncoder();

            // Check the default values
            Assert.AreEqual(LzmaCompressionLevel.Default, encoder.CompressionLevel);
            Assert.AreEqual(LzmaCompressionMode.Default, encoder.CompressionMode);
            Assert.AreEqual(LzmaDictionarySize.Default, encoder.DictionarySize);
            Assert.AreEqual(LzmaFastBytes.Default, encoder.FastBytes);
            Assert.AreEqual(LzmaHashBytes.Default, encoder.HashBytes);
            Assert.AreEqual(LzmaLiteralContextBits.Default, encoder.LiteralContextBits);
            Assert.AreEqual(LzmaLiteralPositionBits.Default, encoder.LiteralPositionBits);
            Assert.AreEqual(LzmaMatchFindMode.Default, encoder.MatchFindMode);
            Assert.AreEqual(LzmaMatchFindPasses.Default, encoder.MatchFindPasses);
            Assert.AreEqual(LzmaPositionBits.Default, encoder.PositionBits);
            Assert.AreEqual(true, encoder.UseMultipleThreads);
            Assert.AreEqual(true, encoder.WriteEndMark);

            // Set and reset encoder parameters to exercise the property setters
            encoder.CompressionLevel = LzmaCompressionLevel.Fastest;
            Assert.AreEqual(LzmaCompressionLevel.Fastest, encoder.CompressionLevel);
            encoder.CompressionLevel = LzmaCompressionLevel.Default;
            Assert.AreEqual(LzmaCompressionLevel.Default, encoder.CompressionLevel);

            encoder.CompressionMode = LzmaCompressionMode.Fast;
            Assert.AreEqual(LzmaCompressionMode.Fast, encoder.CompressionMode);
            encoder.CompressionMode = LzmaCompressionMode.Default;
            Assert.AreEqual(LzmaCompressionMode.Default, encoder.CompressionMode);

            encoder.DictionarySize = LzmaDictionarySize.Maximum;
            Assert.AreEqual(LzmaDictionarySize.Maximum, encoder.DictionarySize);
            encoder.DictionarySize = LzmaDictionarySize.Default;
            Assert.AreEqual(LzmaDictionarySize.Default, encoder.DictionarySize);

            encoder.FastBytes = LzmaFastBytes.Maximum;
            Assert.AreEqual(LzmaFastBytes.Maximum, encoder.FastBytes);
            encoder.FastBytes = LzmaFastBytes.Default;
            Assert.AreEqual(LzmaFastBytes.Default, encoder.FastBytes);

            encoder.HashBytes = LzmaHashBytes.Maximum;
            Assert.AreEqual(LzmaHashBytes.Maximum, encoder.HashBytes);
            encoder.HashBytes = LzmaHashBytes.Default;
            Assert.AreEqual(LzmaHashBytes.Default, encoder.HashBytes);

            encoder.LiteralContextBits = LzmaLiteralContextBits.Maximum;
            Assert.AreEqual(LzmaLiteralContextBits.Maximum, encoder.LiteralContextBits);
            encoder.LiteralContextBits = LzmaLiteralContextBits.Default;
            Assert.AreEqual(LzmaLiteralContextBits.Default, encoder.LiteralContextBits);

            encoder.LiteralPositionBits = LzmaLiteralPositionBits.Maximum;
            Assert.AreEqual(LzmaLiteralPositionBits.Maximum, encoder.LiteralPositionBits);
            encoder.LiteralPositionBits = LzmaLiteralPositionBits.Default;
            Assert.AreEqual(LzmaLiteralPositionBits.Default, encoder.LiteralPositionBits);

            encoder.MatchFindMode = LzmaMatchFindMode.HashChain;
            Assert.AreEqual(LzmaMatchFindMode.HashChain, encoder.MatchFindMode);
            encoder.MatchFindMode = LzmaMatchFindMode.Default;
            Assert.AreEqual(LzmaMatchFindMode.Default, encoder.MatchFindMode);

            encoder.MatchFindPasses = LzmaMatchFindPasses.Maximum;
            Assert.AreEqual(LzmaMatchFindPasses.Maximum, encoder.MatchFindPasses);
            encoder.MatchFindPasses = LzmaMatchFindPasses.Default;
            Assert.AreEqual(LzmaMatchFindPasses.Default, encoder.MatchFindPasses);

            encoder.PositionBits = LzmaPositionBits.Maximum;
            Assert.AreEqual(LzmaPositionBits.Maximum, encoder.PositionBits);
            encoder.PositionBits = LzmaPositionBits.Default;
            Assert.AreEqual(LzmaPositionBits.Default, encoder.PositionBits);

            encoder.UseMultipleThreads = false;
            Assert.AreEqual(false, encoder.UseMultipleThreads);
            encoder.UseMultipleThreads = true;
            Assert.AreEqual(true, encoder.UseMultipleThreads);

            encoder.WriteEndMark = false;
            Assert.AreEqual(false, encoder.WriteEndMark);
            encoder.WriteEndMark = true;
            Assert.AreEqual(true, encoder.WriteEndMark);

            // Check all of the Encoder methods work and encode as expected
            byte[] expected, expectedstreamed, actual;

            // There are 2 expected results since when the length of the input
            // is known it's encoded into the compressed stream
            using (MemoryStream ms = new MemoryStream())
            {
                encoder.Encode(s_sampledata, ms);
                ms.Flush();
                expected = ms.ToArray();
            }

            using (MemoryStream ms = new MemoryStream())
            {
                encoder.Encode(new MemoryStream(s_sampledata), ms);
                ms.Flush();
                expectedstreamed = ms.ToArray();
            }

            // Check parameter validations
            try { actual = encoder.Encode((byte[])null); Assert.Fail("Method call should have thrown an exception"); }
            catch (Exception ex) { Assert.IsInstanceOfType(ex, typeof(ArgumentNullException)); }

            try { actual = encoder.Encode((Stream)null); Assert.Fail("Method call should have thrown an exception"); }
            catch (Exception ex) { Assert.IsInstanceOfType(ex, typeof(ArgumentNullException)); }

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

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

            try { encoder.Encode((byte[])null, new MemoryStream()); Assert.Fail("Method call should have thrown an exception"); }
            catch (Exception ex) { Assert.IsInstanceOfType(ex, typeof(ArgumentNullException)); }

            try { encoder.Encode((byte[])null, 0, 0, new MemoryStream()); Assert.Fail("Method call should have thrown an exception"); }
            catch (Exception ex) { Assert.IsInstanceOfType(ex, typeof(ArgumentNullException)); }

            try { encoder.Encode(s_sampledata, 0, s_sampledata.Length, (Stream)null); Assert.Fail("Method call should have thrown an exception"); }
            catch (Exception ex) { Assert.IsInstanceOfType(ex, typeof(ArgumentNullException)); }

            try { encoder.Encode((Stream)null, new MemoryStream()); Assert.Fail("Method call should have thrown an exception"); }
            catch (Exception ex) { Assert.IsInstanceOfType(ex, typeof(ArgumentNullException)); }

            try { encoder.Encode(new MemoryStream(), (Stream)null); Assert.Fail("Method call should have thrown an exception"); }
            catch (Exception ex) { Assert.IsInstanceOfType(ex, typeof(ArgumentNullException)); }

            // Check actual encoding operations
            actual = encoder.Encode(s_sampledata);
            Assert.IsTrue(Enumerable.SequenceEqual(expected, actual));                                  // length is known

            actual = encoder.Encode(new MemoryStream(s_sampledata));
            Assert.IsTrue(Enumerable.SequenceEqual(expectedstreamed, actual));                          // length is not known

            actual = encoder.Encode(s_sampledata, 0, s_sampledata.Length);                              // length is known
            Assert.IsTrue(Enumerable.SequenceEqual(expected, actual));

            using (MemoryStream dest = new MemoryStream())
            {
                encoder.Encode(s_sampledata, dest);
                Assert.IsTrue(Enumerable.SequenceEqual(expected, dest.ToArray()));                          // length is known
            }

            using (MemoryStream dest = new MemoryStream())
            {
                encoder.Encode(new MemoryStream(s_sampledata), dest);
                Assert.IsTrue(Enumerable.SequenceEqual(expectedstreamed, dest.ToArray()));                  // length is not known
            }

            using (MemoryStream dest = new MemoryStream())
            {
                encoder.Encode(s_sampledata, 0, s_sampledata.Length, dest);
                Assert.IsTrue(Enumerable.SequenceEqual(expected, dest.ToArray()));                          // length is known
            }
        }