Skip to content

Commit

Permalink
convert checksums to Data
Browse files Browse the repository at this point in the history
  • Loading branch information
leogdion committed Jan 18, 2024
1 parent 42a39dc commit a2117b5
Show file tree
Hide file tree
Showing 6 changed files with 141 additions and 9 deletions.
48 changes: 48 additions & 0 deletions Sources/IPSWDownloads/Data.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
//
// Data.swift
// IPSWDownloads
//
// Created by Data.swift
// Copyright © 2024 BrightDigit.
//
// Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation
// files (the “Software”), to deal in the Software without
// restriction, including without limitation the rights to use,
// copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following
// conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.
//

import Foundation
extension Data {
internal init(hexString: String) throws {
var data = Data(capacity: hexString.count / 2)

var index = hexString.startIndex
while index < hexString.endIndex {
let byteString = hexString[index ..< hexString.index(index, offsetBy: 2)]
if let byte = UInt8(byteString, radix: 16) {
data.append(byte)
} else {
throw RuntimeError.invalidDataHexString(hexString)
}
index = hexString.index(index, offsetBy: 2)
}

self = data
}
}
12 changes: 6 additions & 6 deletions Sources/IPSWDownloads/Firmware.swift
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,10 @@ public struct Firmware {
public let buildid: String

/// The SHA-1 checksum of the firmware.
public let sha1sum: String
public let sha1sum: Data

/// The MD5 checksum of the firmware.
public let md5sum: String
public let md5sum: Data

/// The size of the firmware file in bytes.
public let filesize: Int
Expand Down Expand Up @@ -78,8 +78,8 @@ public struct Firmware {
identifier: String,
version: OperatingSystemVersion,
buildid: String,
sha1sum: String,
md5sum: String,
sha1sum: Data,
md5sum: Data,
filesize: Int,
url: URL,
releasedate: Date,
Expand Down Expand Up @@ -110,8 +110,8 @@ extension Firmware {
identifier: component.identifier,
version: OperatingSystemVersion(string: component.version),
buildid: component.buildid,
sha1sum: component.sha1sum,
md5sum: component.md5sum,
sha1sum: Data(hexString: component.sha1sum),
md5sum: Data(hexString: component.md5sum),
filesize: component.filesize,
url: URL(validatingURL: component.url),
releasedate: component.releasedate,
Expand Down
1 change: 1 addition & 0 deletions Sources/IPSWDownloads/RuntimeError.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,5 @@ import Foundation
internal enum RuntimeError: Error {
case invalidURL(String)
case invalidVersion(String)
case invalidDataHexString(String)
}
83 changes: 83 additions & 0 deletions Tests/IPSWDownloadsTests/DataTest.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
//
// DataTest.swift
// IPSWDownloads
//
// Created by DataTest.swift
// Copyright © 2024 BrightDigit.
//
// Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation
// files (the “Software”), to deal in the Software without
// restriction, including without limitation the rights to use,
// copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following
// conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.
//

import Foundation
@testable import IPSWDownloads
import XCTest

extension String {
static let validHexCharacters = "0123456789abcdefABCDEF"

static func random(ofLength length: Int, validCharacters: String) -> String {
guard !validCharacters.isEmpty else {
return ""
}

var randomString = ""

for _ in 0 ..< length {
let randomIndex = Int.random(in: 0 ..< validCharacters.count)
let character = validCharacters[validCharacters.index(validCharacters.startIndex, offsetBy: randomIndex)]
randomString.append(character)
}

return randomString
}
}

public class DataTests: XCTestCase {
func testInitStringValid() throws {
let validCount = Int.random(in: 20 ... 50)
let values: [String] = (0 ..< validCount).map { _ in
Int.random(in: 5 ... 25)
}.map { length in
.random(ofLength: length * 2, validCharacters: String.validHexCharacters)
}

for value in values {
_ = try Data(hexString: value)
}
}

func testInitStringInvalid() throws {
let invalidCount = Int.random(in: 20 ... 50)
let strings: [String] = (0 ..< invalidCount).map { _ in
UUID().uuidString
}
for string in strings {
var actual: String?
do {
_ = try Data(hexString: string)
} catch let RuntimeError.invalidDataHexString(value) {
actual = value
}
XCTAssertEqual(string, actual)
}
}
}
2 changes: 1 addition & 1 deletion Tests/IPSWDownloadsTests/IPSWDownloadsTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ final class IPSWDownloadsTest: XCTestCase {
client = IPSWDownloads(transport: URLSessionTransport())
}

func testExample() async throws {
func testDeviceWithIdentifier() async throws {
let device = try await client.device(withIdentifier: "VirtualMac2,1", type: .ipsw)
XCTAssertEqual(device.identifier, "VirtualMac2,1")
XCTAssertGreaterThan(device.firmwares.count, 10)
Expand Down
4 changes: 2 additions & 2 deletions Tests/IPSWDownloadsTests/OperatingSystemVersionTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,8 @@ public class OperatingSystemVersionTests: XCTestCase {
}

func testInitStringInvalid() throws {
let validCount = Int.random(in: 20 ... 50)
let strings: [String] = (0 ..< validCount).map { _ in
let invalidCount = Int.random(in: 20 ... 50)
let strings: [String] = (0 ..< invalidCount).map { _ in
UUID().uuidString
}
for string in strings {
Expand Down

0 comments on commit a2117b5

Please sign in to comment.