From 33dd519f5630c54425e06b8264995eb5ad6d5682 Mon Sep 17 00:00:00 2001 From: Lars Vahlenberg Date: Sat, 8 Jun 2024 18:26:12 +0200 Subject: [PATCH] Throw exception when bzip2 is corrupt --- .../Compressors/BZip2/CBZip2InputStream.cs | 9 +++++--- .../BZip2/BZip2ReaderTests.cs | 21 +++++++++++++++++++ 2 files changed, 27 insertions(+), 3 deletions(-) create mode 100644 tests/SharpCompress.Test/BZip2/BZip2ReaderTests.cs diff --git a/src/SharpCompress/Compressors/BZip2/CBZip2InputStream.cs b/src/SharpCompress/Compressors/BZip2/CBZip2InputStream.cs index a541a04d..a467942f 100644 --- a/src/SharpCompress/Compressors/BZip2/CBZip2InputStream.cs +++ b/src/SharpCompress/Compressors/BZip2/CBZip2InputStream.cs @@ -1,4 +1,4 @@ -#nullable disable +#nullable disable using System; using System.IO; @@ -42,14 +42,17 @@ internal class CBZip2InputStream : Stream private static void Cadvise() { //System.out.Println("CRC Error"); - //throw new CCoruptionError(); + throw new InvalidOperationException("BZip2 error"); } private static void BadBGLengths() => Cadvise(); private static void BitStreamEOF() => Cadvise(); - private static void CompressedStreamEOF() => Cadvise(); + private static void CompressedStreamEOF() + { + throw new InvalidOperationException("BZip2 compressed file ends unexpectedly"); + } private void MakeMaps() { diff --git a/tests/SharpCompress.Test/BZip2/BZip2ReaderTests.cs b/tests/SharpCompress.Test/BZip2/BZip2ReaderTests.cs new file mode 100644 index 00000000..e0fa0583 --- /dev/null +++ b/tests/SharpCompress.Test/BZip2/BZip2ReaderTests.cs @@ -0,0 +1,21 @@ +using System; +using System.IO; +using SharpCompress.Common; +using SharpCompress.IO; +using SharpCompress.Readers; +using SharpCompress.Readers.GZip; +using Xunit; + +namespace SharpCompress.Test.BZip2; + +public class BZip2ReaderTests : ReaderTests +{ + [Fact] + public void BZip2_Reader_Factory() + { + Stream stream = new MemoryStream( + new byte[] { 0x42, 0x5a, 0x68, 0x34, 0x31, 0x41, 0x59, 0x26, 0x53, 0x59, 0x35 } + ); + Assert.Throws(typeof(InvalidOperationException), () => ReaderFactory.Open(stream)); + } +}