-
Notifications
You must be signed in to change notification settings - Fork 974
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add test for zip file containing 0 code length entries
- Loading branch information
Showing
2 changed files
with
77 additions
and
0 deletions.
There are no files selected for viewing
20 changes: 20 additions & 0 deletions
20
test/ICSharpCode.SharpZipLib.Tests/TestSupport/ThreadEx.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
57
test/ICSharpCode.SharpZipLib.Tests/Zip/ZipCorruptionHandling.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
}); | ||
} | ||
|
||
} | ||
|
||
} |