From 804585f13db1987883ae356d78443adc1f5f810e Mon Sep 17 00:00:00 2001 From: Davide De Rosa Date: Sat, 27 Oct 2018 10:40:13 +0200 Subject: [PATCH] Fix/improve some poor filename parsing --- .../ConnectionService+Configurations.swift | 2 +- .../Model/ConnectionService+Migration.swift | 4 ++-- .../Sources/Model/ConnectionService.swift | 7 +++---- .../Services/InfrastructureFactory.swift | 2 +- .../ConnectionServiceTests.swift | 19 +++++++++++++++++++ 5 files changed, 26 insertions(+), 8 deletions(-) diff --git a/Passepartout/Sources/Model/ConnectionService+Configurations.swift b/Passepartout/Sources/Model/ConnectionService+Configurations.swift index c22f1dc8..9a717fc9 100644 --- a/Passepartout/Sources/Model/ConnectionService+Configurations.swift +++ b/Passepartout/Sources/Model/ConnectionService+Configurations.swift @@ -44,6 +44,6 @@ extension ConnectionService { private func targetConfigurationURL(for profile: ConnectionProfile) -> URL { let contextURL = ConnectionService.ProfileKey(profile).contextURL(in: self) - return contextURL.appendingPathComponent("\(profile.id).ovpn") + return contextURL.appendingPathComponent(profile.id).appendingPathExtension("ovpn") } } diff --git a/Passepartout/Sources/Model/ConnectionService+Migration.swift b/Passepartout/Sources/Model/ConnectionService+Migration.swift index 802a6f0b..d220f1b9 100644 --- a/Passepartout/Sources/Model/ConnectionService+Migration.swift +++ b/Passepartout/Sources/Model/ConnectionService+Migration.swift @@ -145,7 +145,7 @@ extension ConnectionService { // provider["id"] = id // provider.removeValue(forKey: "name") - let url = providersParentURL.appendingPathComponent("\(id).json") + let url = providersParentURL.appendingPathComponent(id).appendingPathExtension("json") let data = try JSONSerialization.data(withJSONObject: provider, options: []) try data.write(to: url) } else if var host = p["host"] as? [String: Any] { @@ -155,7 +155,7 @@ extension ConnectionService { // host["id"] = id // host.removeValue(forKey: "title") - let url = hostsParentURL.appendingPathComponent("\(id).json") + let url = hostsParentURL.appendingPathComponent(id).appendingPathExtension("json") let data = try JSONSerialization.data(withJSONObject: host, options: []) try data.write(to: url) } diff --git a/Passepartout/Sources/Model/ConnectionService.swift b/Passepartout/Sources/Model/ConnectionService.swift index cf04e423..954d50d7 100644 --- a/Passepartout/Sources/Model/ConnectionService.swift +++ b/Passepartout/Sources/Model/ConnectionService.swift @@ -304,15 +304,14 @@ class ConnectionService: Codable { } private static func profileId(fromURL url: URL) -> String? { - let filename = url.lastPathComponent - guard let extRange = filename.range(of: ".json") else { + guard url.pathExtension == "json" else { return nil } - return String(filename[filename.startIndex.. URL { - return directory.appendingPathComponent("\(profileId).json") + return directory.appendingPathComponent(profileId).appendingPathExtension("json") } // MARK: Profiles diff --git a/Passepartout/Sources/Services/InfrastructureFactory.swift b/Passepartout/Sources/Services/InfrastructureFactory.swift index cbcecdce..d8fbcb58 100644 --- a/Passepartout/Sources/Services/InfrastructureFactory.swift +++ b/Passepartout/Sources/Services/InfrastructureFactory.swift @@ -203,7 +203,7 @@ class InfrastructureFactory { } private func cacheURL(for name: Infrastructure.Name) -> URL { - return cachePath.appendingPathComponent("\(name.webName).json") + return cachePath.appendingPathComponent(name.webName).appendingPathExtension("json") } private func cacheModificationDate(for name: Infrastructure.Name) -> Date? { diff --git a/PassepartoutTests-iOS/ConnectionServiceTests.swift b/PassepartoutTests-iOS/ConnectionServiceTests.swift index 0f315da8..4a77003b 100644 --- a/PassepartoutTests-iOS/ConnectionServiceTests.swift +++ b/PassepartoutTests-iOS/ConnectionServiceTests.swift @@ -58,4 +58,23 @@ class ConnectionServiceTests: XCTestCase { XCTAssert(activeProfile.parameters.sessionConfiguration.cipher == .aes256cbc) XCTAssert(activeProfile.parameters.sessionConfiguration.ca.pem == "bogus+ca") } + + func testPathExtension() { + XCTAssertTrue(privateTestPathExtension("file:///foo/bar/johndoe.json")) + XCTAssertFalse(privateTestPathExtension("file:///foo/bar/break.json.johndoe.json")) + } + + private func privateTestPathExtension(_ string: String) -> Bool { + let url = URL(string: string)! + let filename = url.lastPathComponent + guard let extRange = filename.range(of: ".json") else { + return false + } + guard url.pathExtension == "json" else { + return false + } + let name1 = String(filename[filename.startIndex..