Add method to parse configuration from [String]

With an optional original URL. Helps testing. Take the chance to
also disambiguate method overloads.
This commit is contained in:
Davide De Rosa 2018-11-04 11:37:00 +01:00
parent 0b04f2bb51
commit c1065751b6
6 changed files with 13 additions and 9 deletions

View File

@ -91,7 +91,7 @@ class IssueReporter: NSObject {
}
if let url = configurationURL {
do {
let parsedFile = try TunnelKitProvider.Configuration.parsed(from: url, returnsStripped: true)
let parsedFile = try TunnelKitProvider.Configuration.parsed(fromURL: url, returnsStripped: true)
if let attachment = parsedFile.strippedLines?.joined(separator: "\n").data(using: .utf8) {
vc.addAttachmentData(attachment, mimeType: AppConstants.IssueReporter.MIME.configuration, fileName: AppConstants.IssueReporter.Filenames.configuration)
}

View File

@ -35,7 +35,7 @@ extension ParsedFile {
let file: ParsedFile
log.debug("Parsing configuration URL: \(url)")
do {
file = try TunnelKitProvider.Configuration.parsed(from: url)
file = try TunnelKitProvider.Configuration.parsed(fromURL: url)
} catch let e {
let message = localizedMessage(forError: e)
alertImportError(url: url, in: viewController, withMessage: message)

View File

@ -123,7 +123,7 @@ class ConfigurationViewController: UIViewController, TableModelHost {
}
let parsedFile: ParsedFile
do {
parsedFile = try TunnelKitProvider.Configuration.parsed(from: originalURL)
parsedFile = try TunnelKitProvider.Configuration.parsed(fromURL: originalURL)
} catch let e {
log.error("Could not parse original configuration: \(e)")
return

View File

@ -88,7 +88,7 @@ class WizardHostViewController: UITableViewController, TableModelHost {
// MARK: Actions
private func useSuggestedTitle() {
cellTitle?.field.text = parsedFile?.url.normalizedFilename
cellTitle?.field.text = parsedFile?.url?.normalizedFilename
}
@IBAction private func next() {

View File

@ -30,7 +30,7 @@ import SwiftyBeaver
private let log = SwiftyBeaver.self
struct ParsedFile {
let url: URL
let url: URL?
let hostname: String
@ -77,8 +77,12 @@ extension TunnelKitProvider.Configuration {
static let externalFiles = Utils.regex("^(ca|cert|key|tls-auth|tls-crypt) ")
}
static func parsed(from url: URL, returnsStripped: Bool = false) throws -> ParsedFile {
static func parsed(fromURL url: URL, returnsStripped: Bool = false) throws -> ParsedFile {
let lines = try String(contentsOf: url).trimmedLines()
return try parsed(fromLines: lines, originalURL: url, returnsStripped: returnsStripped)
}
static func parsed(fromLines lines: [String], originalURL: URL? = nil, returnsStripped: Bool = false) throws -> ParsedFile {
var strippedLines: [String]? = returnsStripped ? [] : nil
var warning: ApplicationError? = nil
@ -343,7 +347,7 @@ extension TunnelKitProvider.Configuration {
builder.endpointProtocols = endpointProtocols
return ParsedFile(
url: url,
url: originalURL,
hostname: hostname,
configuration: builder.build(),
strippedLines: strippedLines,

View File

@ -39,7 +39,7 @@ class FileConfigurationTests: XCTestCase {
}
func testPIA() throws {
let file = try TunnelKitProvider.Configuration.parsed(from: url(withName: "pia-hungary"))
let file = try TunnelKitProvider.Configuration.parsed(fromURL: url(withName: "pia-hungary"))
XCTAssertEqual(file.hostname, "hungary.privateinternetaccess.com")
XCTAssertEqual(file.configuration.sessionConfiguration.cipher, .aes128cbc)
XCTAssertEqual(file.configuration.sessionConfiguration.digest, .sha1)
@ -50,7 +50,7 @@ class FileConfigurationTests: XCTestCase {
}
func testStripped() throws {
let lines = try TunnelKitProvider.Configuration.parsed(from: url(withName: "pia-hungary"), returnsStripped: true).strippedLines!
let lines = try TunnelKitProvider.Configuration.parsed(fromURL: url(withName: "pia-hungary"), returnsStripped: true).strippedLines!
let stripped = lines.joined(separator: "\n")
print(stripped)
}