From a3bed391da11863277a597f6ae9ce2a9b8c20f83 Mon Sep 17 00:00:00 2001 From: Richard Webb Date: Thu, 20 Aug 2020 00:02:06 +0100 Subject: [PATCH] Extra test for updating zips whose entries have descriptors with no signature bytes --- .../Zip/ZipFileHandling.cs | 60 ++++++++++++++++++- 1 file changed, 59 insertions(+), 1 deletion(-) diff --git a/test/ICSharpCode.SharpZipLib.Tests/Zip/ZipFileHandling.cs b/test/ICSharpCode.SharpZipLib.Tests/Zip/ZipFileHandling.cs index 3e150d6d3..e053bed04 100644 --- a/test/ICSharpCode.SharpZipLib.Tests/Zip/ZipFileHandling.cs +++ b/test/ICSharpCode.SharpZipLib.Tests/Zip/ZipFileHandling.cs @@ -1692,7 +1692,7 @@ public void TestDescriptorUpdateOnAdd([Values] UseZip64 useZip64, [Values] FileU /// Whether safe or direct updates should be used [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)) @@ -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="; + + /// + /// 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 + /// + /// Whether Zip64 should be used in the test archive + /// Whether safe or direct updates should be used + [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)); + } + } + } } }