Add Validator for Endpoints.

Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
This commit is contained in:
Jeroen Leenarts
2018-08-15 20:35:21 +02:00
parent 04454b8451
commit 9037b90747
3 changed files with 137 additions and 0 deletions
+56
View File
@@ -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")
}
}