public void Xz_EncoderCheckSHA256() { // 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 XzEncoder encoder = new XzEncoder(); encoder.Checksum = XzChecksum.SHA256; 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 (XzReader decompressor = new XzReader(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())); } } }
public void Xz_CanWrite() { // Verify behavior of a decompression stream using (XzReader stream = new XzReader(new MemoryStream(s_sampledata))) { Assert.IsFalse(stream.CanWrite); } }
public void Xz_SetLength() { // Verify behavior of a decompression stream using (XzReader stream = new XzReader(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)); } } }
public void Xz_Flush() { // Verify behavior of flushing a decompression stream using (XzReader stream = new XzReader(new MemoryStream(s_sampledata))) { // Flush has no effect on decompression streams, just ensure it doesn't throw stream.Flush(); } }
public void Xz_Dispose() { byte[] buffer = new byte[8192]; // 8KiB data buffer // Create a memorystream to hold the compressed sample data using (MemoryStream ms = new MemoryStream()) { XzEncoder encoder = new XzEncoder(); encoder.Encode(s_sampledata, ms); // Create a decompression stream and immediately dispose of it XzReader stream = new XzReader(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)); } } }
public void Xz_Write() { XzEncoder encoder = new XzEncoder(); var compressed = encoder.Encode(s_sampledata); using (XzReader reader = new XzReader(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)); } } }
public void Xz_BaseStream() { using (MemoryStream dest = new MemoryStream()) { XzEncoder encoder = new XzEncoder(); encoder.Encode(s_sampledata, dest); using (XzReader stream = new XzReader(dest)) { Assert.IsNotNull(stream.BaseStream); Assert.AreSame(dest, stream.BaseStream); } } }
public void Xz_DecompressExternal() { // Decompress a stream created externally to this library using (XzReader reader = new XzReader(Assembly.GetExecutingAssembly().GetManifestResourceStream("zuki.io.compression.test.thethreemusketeers.xz"))) { 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())); } } }
public void Xz_Seek() { // Verify behavior of a decompression stream using (XzReader stream = new XzReader(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)); } } }
public void Xz_Read() { byte[] buffer = new byte[8192]; // 8KiB data buffer using (MemoryStream compressed = new MemoryStream()) { XzEncoder encoder = new XzEncoder(); encoder.Encode(s_sampledata, compressed); compressed.Flush(); // Check the constructor for ArgumentNullException while we're here try { using (XzReader decompressor = new XzReader(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 (XzReader decompressor = new XzReader(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 (XzReader decompressor = new XzReader(compressed, true)) { // Reading zero bytes should not throw an exception decompressor.Read(buffer, 0, 0); while (decompressor.Read(buffer, 0, 8192) != 0) { } } } }
public void Xz_LzmaException() { using (MemoryStream compressed = new MemoryStream()) { XzEncoder encoder = new XzEncoder(); encoder.CompressionLevel = LzmaCompressionLevel.Optimal; encoder.Encode(s_sampledata, compressed); compressed.Flush(); byte[] buffer = new byte[8192]; LzmaException thrown = null; LzmaException deserialized = null; // Create a decompressor to test exception cases using (XzReader decompressor = new XzReader(compressed, true)) { // Attempting to read from the middle of the compressed stream should throw a LzmaException compressed.Position = compressed.Length / 2; try { decompressor.Read(buffer, 0, 8192); Assert.Fail("Method call should have thrown an exception"); } catch (LzmaException ex) { thrown = ex; } Assert.IsNotNull(thrown); Assert.IsInstanceOfType(thrown, typeof(LzmaException)); // Check the error code property Assert.AreEqual(17, thrown.ErrorCode); // SZ_ERROR_NO_ARCHIVE (17) // 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 = (LzmaException)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()); } } }
public void Xz_Position() { // Start with a MemoryStream created from the sample data using (MemoryStream compressed = new MemoryStream(s_sampledata)) { XzEncoder encoder = new XzEncoder(); encoder.Encode(s_sampledata, compressed); compressed.Position = 0; using (XzReader reader = new XzReader(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 still throw for XzReader byte[] buffer = new byte[8192]; try { var p = reader.Position; Assert.Fail("Property should have thrown an exception"); } catch (Exception ex) { Assert.IsInstanceOfType(ex, typeof(NotSupportedException)); } } } }