Skip to content

Commit

Permalink
Extra test for updating zips whose entries have descriptors with no s…
Browse files Browse the repository at this point in the history
…ignature bytes
  • Loading branch information
Numpsy committed Aug 19, 2020
1 parent 3b33af9 commit a3bed39
Showing 1 changed file with 59 additions and 1 deletion.
60 changes: 59 additions & 1 deletion test/ICSharpCode.SharpZipLib.Tests/Zip/ZipFileHandling.cs
Expand Up @@ -1692,7 +1692,7 @@ public void TestDescriptorUpdateOnAdd([Values] UseZip64 useZip64, [Values] FileU
/// <param name="updateMode">Whether safe or direct updates should be used</param>
[Test]
[Category("Zip")]
public void TestDescriptorUpdateOnReplacee([Values] UseZip64 useZip64, [Values] FileUpdateMode updateMode)
public void TestDescriptorUpdateOnReplace([Values] UseZip64 useZip64, [Values] FileUpdateMode updateMode)
{
MemoryStream msw = new MemoryStreamWithoutSeek();
using (ZipOutputStream outStream = new ZipOutputStream(msw))
Expand Down Expand Up @@ -1742,5 +1742,63 @@ public void TestDescriptorUpdateOnReplacee([Values] UseZip64 useZip64, [Values]
}
}
}

// This is the initial zipfile generated by the 'TestDescriptorUpdateOnReplace' test, but with descriptor
// fields that don't have the signature bytes.
const string TestZipFileWithSignaturelessDescriptors = @"UEsDBBQACAAIAHO8E1EAAAAAAAAAAAAAAAANAAAA
U3RyaXBlZE1hcmxpbosEAN0GtcADAAAAAQAAAFBLAwQUAAgACABzvBNRAAAAAAAAAAAAAAAACwAAAFdoaXRlTWFybGlui
wYA8We7LgMAAAABAAAAUEsDBBQACAAIAHO8E1EAAAAAAAAAAAAAAAAIAAAAU2FpbGZpc2hjBgA3vgtLAwAAAAEAAABQSw
ECMwAUAAgACABzvBNR3Qa1wAMAAAABAAAADQAAAAAAAAAAAAAAAAAAAAAAU3RyaXBlZE1hcmxpblBLAQIzABQACAAIAHO
8E1HxZ7suAwAAAAEAAAALAAAAAAAAAAAAAAAAADoAAABXaGl0ZU1hcmxpblBLAQIzABQACAAIAHO8E1E3vgtLAwAAAAEA
AAAIAAAAAAAAAAAAAAAAAHIAAABTYWlsZmlzaFBLBQYAAAAAAwADAKoAAACnAAAAAAA=";

/// <summary>
/// Test for https://github.com/icsharpcode/SharpZipLib/issues/147, when replacing items in a zip, using a file whose descriptors
/// don't have signature bytes
/// </summary>
/// <param name="useZip64">Whether Zip64 should be used in the test archive</param>
/// <param name="updateMode">Whether safe or direct updates should be used</param>
[Test]
[Category("Zip")]
public void TestSignaturelessDescriptorUpdateOnReplace([Values] UseZip64 useZip64, [Values] FileUpdateMode updateMode)
{
var fileBytes = Convert.FromBase64String(TestZipFileWithSignaturelessDescriptors);

using (var memoryStream = new MemoryStream())
{
memoryStream.Write(fileBytes, 0, fileBytes.Length);
memoryStream.Position = 0;

using (var zipFile = new ZipFile(memoryStream, leaveOpen: true))
{
zipFile.BeginUpdate(new MemoryArchiveStorage(updateMode));
zipFile.Delete("WhiteMarlin");
zipFile.Add(new StringMemoryDataSource("Kajikia albida"), "WhiteMarlin");
zipFile.CommitUpdate();

// @@NOTE@@ TestArchive fails if an entry has a descriptor with no signature.
// Assert.That(zipFile.TestArchive(true), Is.True);
Assert.That(zipFile.Count, Is.EqualTo(3));

// Test for expected descriptor states:
// 'StripedMarlin' should still have a descriptor in Direct update mode as the entry data will be kept, but won't have one
// in Safe update mode as that recreates the whole archive.
// 'WhiteMarlin' should no longer have one because the entry is new and didn't need one
// 'Sailfish' should have its descriptor removed.
var entry = zipFile.GetEntry("StripedMarlin");

if (updateMode == FileUpdateMode.Direct)
Assert.True(((GeneralBitFlags)entry.Flags).HasFlag(GeneralBitFlags.Descriptor));
else
Assert.False(((GeneralBitFlags)entry.Flags).HasFlag(GeneralBitFlags.Descriptor));

entry = zipFile.GetEntry("WhiteMarlin");
Assert.False(((GeneralBitFlags)entry.Flags).HasFlag(GeneralBitFlags.Descriptor));

entry = zipFile.GetEntry("Sailfish");
Assert.False(((GeneralBitFlags)entry.Flags).HasFlag(GeneralBitFlags.Descriptor));
}
}
}
}
}

0 comments on commit a3bed39

Please sign in to comment.