From 9037b90747ccd153b57dfb223a6400456dcddcf5 Mon Sep 17 00:00:00 2001 From: Jeroen Leenarts Date: Wed, 15 Aug 2018 20:35:21 +0200 Subject: Add Validator for Endpoints. Signed-off-by: Jason A. Donenfeld --- WireGuardTests/ValidatorsTests.swift | 56 ++++++++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100644 WireGuardTests/ValidatorsTests.swift (limited to 'WireGuardTests/ValidatorsTests.swift') diff --git a/WireGuardTests/ValidatorsTests.swift b/WireGuardTests/ValidatorsTests.swift new file mode 100644 index 0000000..1efae8d --- /dev/null +++ b/WireGuardTests/ValidatorsTests.swift @@ -0,0 +1,56 @@ +// +// ValidatorsTests.swift +// WireGuardTests +// +// Created by Jeroen Leenarts on 15-08-18. +// Copyright © 2018 WireGuard. All rights reserved. +// + +import XCTest +@testable import WireGuard + +class ValidatorsTests: XCTestCase { + func testIPv6Endpoint() throws { + XCTFail("Still needs implementation") + } + + func testIPv4Endpoint() throws { + _ = try Endpoint(endpointString: "192.168.0.1:12345") + } + + func testIPv4Endpoint_invalidIP() throws { + XCTAssertThrowsError(try Endpoint(endpointString: "12345:12345")) { (error) in + guard case EndpointValidationError.invalidIP(let value) = error else { + return XCTFail("Unexpected error") + } + XCTAssertEqual(value, "12345") + } + } + + func testIPv4Endpoint_invalidPort() throws { + XCTAssertThrowsError(try Endpoint(endpointString: "192.168.0.1:-12345")) { (error) in + guard case EndpointValidationError.invalidPort(let value) = error else { + return XCTFail("Unexpected error") + } + XCTAssertEqual(value, "-12345") + } + } + + func testIPv4Endpoint_noIpAndPort() throws { + + func executeTest(endpointString: String) { + XCTAssertThrowsError(try Endpoint(endpointString: endpointString)) { (error) in + guard case EndpointValidationError.noIpAndPort(let value) = error else { + return XCTFail("Unexpected error") + } + XCTAssertEqual(value, endpointString, file: #file, line: #line) + } + } + + executeTest(endpointString: ":") + executeTest(endpointString: "192.168.0.1") + executeTest(endpointString: "192.168.0.1:") + executeTest(endpointString: ":12345") + executeTest(endpointString: "12345") + } +} -- cgit v1.2.3-59-g8ed1b