|
| 1 | +using System; |
| 2 | +using System.IO; |
| 3 | +using System.IO.Compression; |
| 4 | +using System.Text; |
| 5 | +using ICSharpCode.SharpZipLib.Checksum; |
| 6 | +using ICSharpCode.SharpZipLib.Tests.TestSupport; |
| 7 | +using ICSharpCode.SharpZipLib.Zip; |
| 8 | +using NUnit.Framework; |
| 9 | + |
| 10 | +namespace ICSharpCode.SharpZipLib.Tests.Zip |
| 11 | +{ |
| 12 | + [TestFixture] |
| 13 | + public class PassthroughTests |
| 14 | + { |
| 15 | + [Test] |
| 16 | + [Category("Zip")] |
| 17 | + public void AddingValidPrecompressedEntryToZipOutputStream() |
| 18 | + { |
| 19 | + using var ms = new MemoryStream(); |
| 20 | + |
| 21 | + using (var outStream = new ZipOutputStream(ms){IsStreamOwner = false}) |
| 22 | + { |
| 23 | + var (compressedData, crc, size) = CreateDeflatedData(); |
| 24 | + var entry = new ZipEntry("dummyfile.tst") |
| 25 | + { |
| 26 | + CompressionMethod = CompressionMethod.Deflated, |
| 27 | + Size = size, |
| 28 | + Crc = (uint)crc.Value, |
| 29 | + CompressedSize = compressedData.Length, |
| 30 | + }; |
| 31 | + |
| 32 | + outStream.PutNextPassthroughEntry(entry); |
| 33 | + |
| 34 | + compressedData.CopyTo(outStream); |
| 35 | + } |
| 36 | + |
| 37 | + Assert.IsTrue(ZipTesting.TestArchive(ms.ToArray())); |
| 38 | + } |
| 39 | + |
| 40 | + private static (MemoryStream, Crc32, int) CreateDeflatedData() |
| 41 | + { |
| 42 | + var data = Encoding.UTF8.GetBytes("Hello, world"); |
| 43 | + |
| 44 | + var crc = new Crc32(); |
| 45 | + crc.Update(data); |
| 46 | + |
| 47 | + var compressedData = new MemoryStream(); |
| 48 | + using(var gz = new DeflateStream(compressedData, CompressionMode.Compress, leaveOpen: true)) |
| 49 | + { |
| 50 | + gz.Write(data, 0, data.Length); |
| 51 | + } |
| 52 | + compressedData.Position = 0; |
| 53 | + |
| 54 | + return (compressedData, crc, data.Length); |
| 55 | + } |
| 56 | + |
| 57 | + [Test] |
| 58 | + [Category("Zip")] |
| 59 | + public void AddingPrecompressedEntryToZipOutputStreamWithInvalidSize() |
| 60 | + { |
| 61 | + using var outStream = new ZipOutputStream(new MemoryStream()); |
| 62 | + var (compressedData, crc, size) = CreateDeflatedData(); |
| 63 | + outStream.Password = "mockpassword"; |
| 64 | + var entry = new ZipEntry("dummyfile.tst") |
| 65 | + { |
| 66 | + CompressionMethod = CompressionMethod.Stored, |
| 67 | + Crc = (uint)crc.Value, |
| 68 | + CompressedSize = compressedData.Length, |
| 69 | + }; |
| 70 | + |
| 71 | + Assert.Throws<ZipException>(() => |
| 72 | + { |
| 73 | + outStream.PutNextPassthroughEntry(entry); |
| 74 | + }); |
| 75 | + } |
| 76 | + |
| 77 | + |
| 78 | + [Test] |
| 79 | + [Category("Zip")] |
| 80 | + public void AddingPrecompressedEntryToZipOutputStreamWithInvalidCompressedSize() |
| 81 | + { |
| 82 | + using var outStream = new ZipOutputStream(new MemoryStream()); |
| 83 | + var (compressedData, crc, size) = CreateDeflatedData(); |
| 84 | + outStream.Password = "mockpassword"; |
| 85 | + var entry = new ZipEntry("dummyfile.tst") |
| 86 | + { |
| 87 | + CompressionMethod = CompressionMethod.Stored, |
| 88 | + Size = size, |
| 89 | + Crc = (uint)crc.Value, |
| 90 | + }; |
| 91 | + |
| 92 | + Assert.Throws<ZipException>(() => |
| 93 | + { |
| 94 | + outStream.PutNextPassthroughEntry(entry); |
| 95 | + }); |
| 96 | + } |
| 97 | + |
| 98 | + [Test] |
| 99 | + [Category("Zip")] |
| 100 | + public void AddingPrecompressedEntryToZipOutputStreamWithNonSupportedMethod() |
| 101 | + { |
| 102 | + using var outStream = new ZipOutputStream(new MemoryStream()); |
| 103 | + var (compressedData, crc, size) = CreateDeflatedData(); |
| 104 | + outStream.Password = "mockpassword"; |
| 105 | + var entry = new ZipEntry("dummyfile.tst") |
| 106 | + { |
| 107 | + CompressionMethod = CompressionMethod.LZMA, |
| 108 | + Size = size, |
| 109 | + Crc = (uint)crc.Value, |
| 110 | + CompressedSize = compressedData.Length, |
| 111 | + }; |
| 112 | + |
| 113 | + Assert.Throws<NotImplementedException>(() => |
| 114 | + { |
| 115 | + outStream.PutNextPassthroughEntry(entry); |
| 116 | + }); |
| 117 | + } |
| 118 | + |
| 119 | + [Test] |
| 120 | + [Category("Zip")] |
| 121 | + public void AddingPrecompressedEntryToZipOutputStreamWithEncryption() |
| 122 | + { |
| 123 | + using var outStream = new ZipOutputStream(new MemoryStream()); |
| 124 | + var (compressedData, crc, size) = CreateDeflatedData(); |
| 125 | + outStream.Password = "mockpassword"; |
| 126 | + var entry = new ZipEntry("dummyfile.tst") |
| 127 | + { |
| 128 | + CompressionMethod = CompressionMethod.Deflated, |
| 129 | + Size = size, |
| 130 | + Crc = (uint)crc.Value, |
| 131 | + CompressedSize = compressedData.Length, |
| 132 | + }; |
| 133 | + |
| 134 | + Assert.Throws<NotImplementedException>(() => |
| 135 | + { |
| 136 | + outStream.PutNextPassthroughEntry(entry); |
| 137 | + }); |
| 138 | + } |
| 139 | + } |
| 140 | +} |
0 commit comments