From a5177fe8d54af20dca443de68708d2822d0ef920 Mon Sep 17 00:00:00 2001 From: Thomas Zoechling Date: Mon, 10 Jun 2024 09:44:02 +0200 Subject: [PATCH] Increase coverage --- Sources/ZIPFoundation/Archive.swift | 9 +++------ .../Resources/testArchiveReadErrorConditions.zip | Bin 0 -> 6546 bytes .../ZIPFoundationErrorConditionTests.swift | 3 +++ 3 files changed, 6 insertions(+), 6 deletions(-) create mode 100644 Tests/ZIPFoundationTests/Resources/testArchiveReadErrorConditions.zip diff --git a/Sources/ZIPFoundation/Archive.swift b/Sources/ZIPFoundation/Archive.swift index 29daf33a..3f4f438a 100644 --- a/Sources/ZIPFoundation/Archive.swift +++ b/Sources/ZIPFoundation/Archive.swift @@ -297,14 +297,11 @@ public final class Archive: Sequence { private static func scanForZIP64EndOfCentralDirectory(in file: FILEPointer, eocdOffset: UInt64) -> ZIP64EndOfCentralDirectory? { - guard UInt64(ZIP64EndOfCentralDirectoryLocator.size) < eocdOffset else { - return nil - } + guard UInt64(ZIP64EndOfCentralDirectoryLocator.size) < eocdOffset else { return nil } + let locatorOffset = eocdOffset - UInt64(ZIP64EndOfCentralDirectoryLocator.size) + guard UInt64(ZIP64EndOfCentralDirectoryRecord.size) < locatorOffset else { return nil } - guard UInt64(ZIP64EndOfCentralDirectoryRecord.size) < locatorOffset else { - return nil - } let recordOffset = locatorOffset - UInt64(ZIP64EndOfCentralDirectoryRecord.size) guard let locator: ZIP64EndOfCentralDirectoryLocator = Data.readStruct(from: file, at: locatorOffset), let record: ZIP64EndOfCentralDirectoryRecord = Data.readStruct(from: file, at: recordOffset) else { diff --git a/Tests/ZIPFoundationTests/Resources/testArchiveReadErrorConditions.zip b/Tests/ZIPFoundationTests/Resources/testArchiveReadErrorConditions.zip new file mode 100644 index 0000000000000000000000000000000000000000..387ed8e81d686444860db56fe90ff4673a51b1b5 GIT binary patch literal 6546 zcmeHMJxc>Y5S=xrMr~3kSRN=?C`wAZQ_ezz5D3_+rZTISMGGxOSf6TTeam z{h-~d61{CaZR_R+o?wipd68;u(m2E?3Wx%tfG8je{1*kVXS3xO3`#BvhytR(KmlGK zTqH)%V#lCfI*{or0FaOI+EC^@1#?`Bp2dzqcwovRlV>S?m}zbW-;6 zp={5}-cXe89qqe1oRnu!a#27O7*!x=J`246?;(x3|BsU7OB4_V{*(gB@hko=reycl z)Z}=tb&*bx5<{#GM>cW@zz1)|_{-54{4SS(p2dzq)WGB*pk> XySHH3xs^-PGwg8idj&r666kyb7s_b{ literal 0 HcmV?d00001 diff --git a/Tests/ZIPFoundationTests/ZIPFoundationErrorConditionTests.swift b/Tests/ZIPFoundationTests/ZIPFoundationErrorConditionTests.swift index 311c51d5..07952dff 100644 --- a/Tests/ZIPFoundationTests/ZIPFoundationErrorConditionTests.swift +++ b/Tests/ZIPFoundationTests/ZIPFoundationErrorConditionTests.swift @@ -27,6 +27,9 @@ extension ZIPFoundationTests { XCTAssert(result == true) XCTAssertSwiftError(try Archive(url: noEndOfCentralDirectoryArchiveURL, accessMode: .read), throws: Archive.ArchiveError.missingEndOfCentralDirectoryRecord) + let invalidEndOfCentralDirectoryArchiveURL = self.resourceURL(for: #function, pathExtension: "zip") + XCTAssertSwiftError(try Archive(url: invalidEndOfCentralDirectoryArchiveURL, accessMode: .read), + throws: Archive.ArchiveError.missingEndOfCentralDirectoryRecord) self.runWithUnprivilegedGroup { var unreadableArchiveURL = ZIPFoundationTests.tempZipDirectoryURL unreadableArchiveURL.appendPathComponent(processInfo.globallyUniqueString)