Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Hex to Bin Converter rewritten to swift #311

Merged
merged 5 commits into from
Jun 14, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions Example/Podfile.lock
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
PODS:
- iOSDFULibrary (4.4.0):
- ZIPFoundation (~> 0.9.9)
- iOSDFULibrary (4.4.2):
- ZIPFoundation (= 0.9.9)
- ZIPFoundation (0.9.9)

DEPENDENCIES:
Expand All @@ -15,9 +15,9 @@ EXTERNAL SOURCES:
:path: "../"

SPEC CHECKSUMS:
iOSDFULibrary: 6e6d7ca105fcc2a804df69d2320802bba7a95d27
iOSDFULibrary: 72a706f0e43fc303c843d80920bba02cfe1bc3d3
ZIPFoundation: 89df685c971926b0323087952320bdfee9f0b6ef

PODFILE CHECKSUM: 16697609d795697cac6384a823bc962ccbddb9d0

COCOAPODS: 1.7.0.beta.3
COCOAPODS: 1.7.1
9 changes: 5 additions & 4 deletions Example/Pods/Local Podspecs/iOSDFULibrary.podspec.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions Example/Pods/Manifest.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1,566 changes: 782 additions & 784 deletions Example/Pods/Pods.xcodeproj/project.pbxproj

Large diffs are not rendered by default.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

192 changes: 192 additions & 0 deletions Example/Tests/Hex2BinConverterTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,192 @@
import UIKit
import XCTest
@testable import iOSDFULibrary

class Hex2BinConverterTests: XCTestCase {

// MARK: - Real HEX files

func testSoftDevice() {
performTest(for: "s110_nrf51822_6.2.1_softdevice", withMbrSize: 0x1000)
}

func testBlinky() {
performTest(for: "blinky_s110_v7_0_0_sdk_v7_1")
}

func testHRM() {
performTest(for: "ble_app_hrm_dfu_s110_v8_0_0_sdk_v8_0")
}

func testBootloader() {
performTest(for: "sdk_15_2_bootloader")
}

func testOnlySoftDevice() {
// This HEX file contains SD+BL, but they are separated by a blank region.
// The second jump (0x04) is from 0001 to 0003 which is not supported.
// Only the SD will then be converted to BIN.
performTest(for: "nrf51422_sdk_5.2_sd_bl", withMbrSize: 0x1000)
}

// MARK: - Special cases

func testSimpleHex() {
let hex = """
:02000000010206
:02000200030480
:00000001FF
""".data(using: .ascii)!

let bin = IntelHex2BinConverter.convert(hex)
XCTAssertNotNil(bin)
XCTAssertEqual(bin, Data([0x01, 0x02, 0x03, 0x04]))
}

func testPrematureEndOfFile() {
let hex = """
:02000000010206
:00000001FF
:02000200030480
""".data(using: .ascii)!

let bin = IntelHex2BinConverter.convert(hex)
XCTAssertNotNil(bin)
XCTAssertEqual(bin, Data([0x01, 0x02]))
}

func testMbr() {
let hex = """
:02000000010206
:02000200030480
:00000001FF
""".data(using: .ascii)!

let bin = IntelHex2BinConverter.convert(hex, mbrSize: 2)
XCTAssertNotNil(bin)
XCTAssertEqual(bin, Data([0x03, 0x04]))
}

func testMbrInsideRecord() {
let hex = """
:02000000010206
:02000200030480
:00000001FF
""".data(using: .ascii)!

let bin = IntelHex2BinConverter.convert(hex, mbrSize: 1)
XCTAssertNotNil(bin)
XCTAssertEqual(bin, Data([0x02, 0x03, 0x04]))
}

func testShortRecordLength() {
let hex = """
:020000040001F9
:02600000010206
:02600200030480
:0400000500016000D5
:00000001FF
""".data(using: .ascii)!

let bin = IntelHex2BinConverter.convert(hex)
XCTAssertNotNil(bin)
XCTAssertEqual(bin, Data([0x01, 0x02, 0x03, 0x04]))
}

func testInvalidRecordLength() {
let hex = """
:020000040001F9
:0460000008280020F56001000F6101001161010006
:106020000000000000000000000000000000000080
:0400000500016000D5
:00000001FF
""".data(using: .ascii)!

let bin = IntelHex2BinConverter.convert(hex)
XCTAssertNil(bin)
}

func testGapInHex() {
let hex = """
:020000040001F9
:1060000008280020F56001000F6101001161010006
:106020000000000000000000000000000000000080
:0400000500016000D5
:00000001FF
""".data(using: .ascii)!

let bin = IntelHex2BinConverter.convert(hex)
XCTAssertNotNil(bin)
XCTAssertEqual(bin, Data([0x08, 0x28, 0x00, 0x20, 0xF5, 0x60, 0x01, 0x00, 0x0F, 0x61, 0x01, 0x00, 0x11, 0x61, 0x01, 0x00]))
}

func testBigJumpInHex() {
let hex = """
:020000040001F9
:1060000008280020F56001000F6101001161010006
:020000040003F9
:100000000000000000000000000000000000000080
:0400000500016000D5
:00000001FF
""".data(using: .ascii)!

let bin = IntelHex2BinConverter.convert(hex)
XCTAssertNotNil(bin)
XCTAssertEqual(bin, Data([0x08, 0x28, 0x00, 0x20, 0xF5, 0x60, 0x01, 0x00, 0x0F, 0x61, 0x01, 0x00, 0x11, 0x61, 0x01, 0x00]))
}

func testExtendedLinearAddressRecord() {
let hex = """
:020000040000F9
:10FFF00008280020F56001000F6101001161010006
:020000040001F9
:100000000000000000000000000000000000000080
:0400000500016000D5
:00000001FF
""".data(using: .ascii)!

let bin = IntelHex2BinConverter.convert(hex)
XCTAssertNotNil(bin)
XCTAssertEqual(bin, Data([0x08, 0x28, 0x00, 0x20, 0xF5, 0x60, 0x01, 0x00, 0x0F, 0x61, 0x01, 0x00, 0x11, 0x61, 0x01, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]))
}

func testExtendedSegmentAddressRecord() {
let hex = """
:020000021000F9
:10FFF00008280020F56001000F6101001161010006
:020000022000F9
:100000000000000000000000000000000000000080
:020000031000D5
:00000001FF
""".data(using: .ascii)!

let bin = IntelHex2BinConverter.convert(hex)
XCTAssertNotNil(bin)
XCTAssertEqual(bin, Data([0x08, 0x28, 0x00, 0x20, 0xF5, 0x60, 0x01, 0x00, 0x0F, 0x61, 0x01, 0x00, 0x11, 0x61, 0x01, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]))
}

// MARK: - Helper methods

private func performTest(for name: String, withMbrSize mbrSize: UInt32 = 0) {
let url = Bundle.main.url(forResource: name, withExtension: "hex", subdirectory: "TestFirmwares")
XCTAssertNotNil(url)

let testUrl = Bundle.main.url(forResource: name, withExtension: "bin", subdirectory: "TestFirmwares")
XCTAssertNotNil(testUrl)

var data: Data!
XCTAssertNoThrow(data = try Data(contentsOf: url!))
XCTAssertNotNil(data)
var testData: Data!
XCTAssertNoThrow(testData = try Data(contentsOf: testUrl!))
XCTAssertNotNil(testData)

let bin = IntelHex2BinConverter.convert(data, mbrSize: mbrSize)
XCTAssertNotNil(bin)

XCTAssertEqual(bin, testData)
}

}
Binary file not shown.
Loading