From 6ffdcec47bfbe68eaf8ec03ecdb8b52ca66921fb Mon Sep 17 00:00:00 2001 From: Davide De Rosa Date: Wed, 19 Sep 2018 18:10:40 +0200 Subject: [PATCH] Return optional from String/IPv4 conversion --- TunnelKit/Sources/AppExtension/DNSResolver.swift | 4 ++-- TunnelKitTests/DNSTests.swift | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/TunnelKit/Sources/AppExtension/DNSResolver.swift b/TunnelKit/Sources/AppExtension/DNSResolver.swift index 98a3165..5c188eb 100644 --- a/TunnelKit/Sources/AppExtension/DNSResolver.swift +++ b/TunnelKit/Sources/AppExtension/DNSResolver.swift @@ -104,10 +104,10 @@ public class DNSResolver { return "\(a).\(b).\(c).\(d)" } - public static func ipv4(fromString string: String) -> UInt32 { + public static func ipv4(fromString string: String) -> UInt32? { let comps = string.components(separatedBy: ".") guard comps.count == 4 else { - preconditionFailure() + return nil } var ipv4: UInt32 = 0 var bits: UInt32 = 0 diff --git a/TunnelKitTests/DNSTests.swift b/TunnelKitTests/DNSTests.swift index a818a88..965b46b 100644 --- a/TunnelKitTests/DNSTests.swift +++ b/TunnelKitTests/DNSTests.swift @@ -43,7 +43,7 @@ class DNSTests: XCTestCase { XCTAssertEqual(DNSResolver.ipv4(fromString: addr), ip) XCTAssertEqual(DNSResolver.string(fromIPv4: ip), addr) - XCTAssertEqual(DNSResolver.string(fromIPv4: DNSResolver.ipv4(fromString: addr)), addr) + XCTAssertEqual(DNSResolver.string(fromIPv4: DNSResolver.ipv4(fromString: addr)!), addr) XCTAssertEqual(DNSResolver.ipv4(fromString: DNSResolver.string(fromIPv4: ip)), ip) } }