From e4ad292f574c07eb4c2e583afefdddb239a04b2b Mon Sep 17 00:00:00 2001 From: Leo Dion Date: Thu, 18 Jan 2024 13:11:49 -0500 Subject: [PATCH] allow nil checksums --- Sources/IPSWDownloads/Data.swift | 8 ++++++++ Sources/IPSWDownloads/Firmware.swift | 12 ++++++------ Tests/IPSWDownloadsTests/DataTest.swift | 6 ++++++ 3 files changed, 20 insertions(+), 6 deletions(-) diff --git a/Sources/IPSWDownloads/Data.swift b/Sources/IPSWDownloads/Data.swift index b832070..2b1a51f 100644 --- a/Sources/IPSWDownloads/Data.swift +++ b/Sources/IPSWDownloads/Data.swift @@ -45,4 +45,12 @@ extension Data { self = data } + + internal init?(hexString: String, emptyIsNil: Bool) throws { + if emptyIsNil, hexString.isEmpty { + return nil + } + + try self.init(hexString: hexString) + } } diff --git a/Sources/IPSWDownloads/Firmware.swift b/Sources/IPSWDownloads/Firmware.swift index 0c3cd6b..03eaa3c 100644 --- a/Sources/IPSWDownloads/Firmware.swift +++ b/Sources/IPSWDownloads/Firmware.swift @@ -41,10 +41,10 @@ public struct Firmware { public let buildid: String /// The SHA-1 checksum of the firmware. - public let sha1sum: Data + public let sha1sum: Data? /// The MD5 checksum of the firmware. - public let md5sum: Data + public let md5sum: Data? /// The size of the firmware file in bytes. public let filesize: Int @@ -78,8 +78,8 @@ public struct Firmware { identifier: String, version: OperatingSystemVersion, buildid: String, - sha1sum: Data, - md5sum: Data, + sha1sum: Data?, + md5sum: Data?, filesize: Int, url: URL, releasedate: Date, @@ -110,8 +110,8 @@ extension Firmware { identifier: component.identifier, version: OperatingSystemVersion(string: component.version), buildid: component.buildid, - sha1sum: Data(hexString: component.sha1sum), - md5sum: Data(hexString: component.md5sum), + sha1sum: Data(hexString: component.sha1sum, emptyIsNil: true), + md5sum: Data(hexString: component.md5sum, emptyIsNil: true), filesize: component.filesize, url: URL(validatingURL: component.url), releasedate: component.releasedate, diff --git a/Tests/IPSWDownloadsTests/DataTest.swift b/Tests/IPSWDownloadsTests/DataTest.swift index b6a366a..f5fe7c7 100644 --- a/Tests/IPSWDownloadsTests/DataTest.swift +++ b/Tests/IPSWDownloadsTests/DataTest.swift @@ -62,6 +62,8 @@ public class DataTests: XCTestCase { for value in values { _ = try Data(hexString: value) + try XCTAssertNotNil(Data(hexString: value, emptyIsNil: true)) + try XCTAssertNotNil(Data(hexString: value, emptyIsNil: false)) } } @@ -80,4 +82,8 @@ public class DataTests: XCTestCase { XCTAssertEqual(string, actual) } } + + func testInitStringEmpty() throws { + try XCTAssertNil(Data(hexString: "", emptyIsNil: true)) + } }