Skip to content

Commit

Permalink
Add test for zip file containing 0 code length entries
Browse files Browse the repository at this point in the history
  • Loading branch information
piksel committed Feb 5, 2019
1 parent fe9e0d8 commit dee7f8d
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 0 deletions.
20 changes: 20 additions & 0 deletions test/ICSharpCode.SharpZipLib.Tests/TestSupport/ThreadEx.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using System;
using System.Threading;
using System.Reflection;

public class ThreadEx
{
public static void Abort(Thread thread)
{
MethodInfo abort = null;
foreach(MethodInfo m in thread.GetType().GetMethods(BindingFlags.NonPublic | BindingFlags.Instance))
{
if (m.Name.Equals("AbortInternal") && m.GetParameters().Length == 0) abort = m;
}
if (abort == null)
{
throw new Exception("Failed to get Thread.Abort method");
}
abort.Invoke(thread, new object[0]);
}
}
57 changes: 57 additions & 0 deletions test/ICSharpCode.SharpZipLib.Tests/Zip/ZipCorruptionHandling.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
using System;
using System.IO;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading;
using ICSharpCode.SharpZipLib;
using ICSharpCode.SharpZipLib.Zip;
using NUnit.Framework;

namespace ICSharpCode.SharpZipLib.Tests.Zip
{
public class ZipCorruptionHandling
{

const string TestFileZeroCodeLength = "UEsDBBQA+AAIANwyZ0U5T8HwjQAAAL8AAAAIABUAbGltZXJpY2t" +
"VVAkAAzBXXFR6LmBUVXgEAPQB9AEFjTEOwjAQBHu/YkVDg3gHoUaivjgHtmKfI5+D5d9zbndHM6/AldFJQTIJ" +
"PrVkPOkgce9QlJFi5hr9rhD+cUUvZ9qgnuRuBAtId97Qw0AL1Kbw5h6MykeKdlyWdlWs7OlUdgsodRqKVo0v8" +
"JWyGWZ6mLpuiii2t2Bl0mZ54QksOIpqXNPATF/eH1BLAQIXAxQAAgAIANxQZ0U5T8HwjQAAAL8AAAAIAA0AAA" +
"AAAAEAAACggQAAAABsaW1lcgEAQwAAAMgAAAAAAA==";

[Test]
[Category("Zip")]
public void ZeroCodeLengthZipFile()
{
Assert.Throws<SharpZipBaseException>(() => {
Exception threadException = null;
var testThread = new Thread(() => {
try {
var fileBytes = Convert.FromBase64String(TestFileZeroCodeLength);
using (var ms = new MemoryStream(fileBytes))
using (var zip = new ZipInputStream(ms))
{
while (zip.GetNextEntry() != null) { }
}
}
catch (Exception x) {
threadException = x;
}
});
testThread.Start();
if(!testThread.Join(5000)){
// Aborting threads is deprecated in .NET Core, but if we don't do this,
// the poor CI will hang for 2 hours upon running this test
ThreadEx.Abort(testThread);
throw new TimeoutException("Timed out waiting for GetNextEntry() to return");
}
else if(threadException != null){
throw threadException;
}
});
}

}

}

0 comments on commit dee7f8d

Please sign in to comment.