2018-08-15 18:35:21 +00:00
|
|
|
//
|
|
|
|
// ValidatorsTests.swift
|
|
|
|
// WireGuardTests
|
|
|
|
//
|
|
|
|
// Created by Jeroen Leenarts on 15-08-18.
|
2018-08-15 18:38:56 +00:00
|
|
|
// Copyright © 2018 Jason A. Donenfeld <Jason@zx2c4.com>. All rights reserved.
|
2018-08-15 18:35:21 +00:00
|
|
|
//
|
|
|
|
|
|
|
|
import XCTest
|
|
|
|
@testable import WireGuard
|
|
|
|
|
|
|
|
class ValidatorsTests: XCTestCase {
|
2018-08-15 20:52:37 +00:00
|
|
|
func testEndpoint() throws {
|
|
|
|
_ = try Endpoint(endpointString: "[2607:f938:3001:4000::aac]:12345")
|
2018-08-15 18:35:21 +00:00
|
|
|
_ = try Endpoint(endpointString: "192.168.0.1:12345")
|
|
|
|
}
|
|
|
|
|
2018-08-15 20:52:37 +00:00
|
|
|
func testEndpoint_invalidIP() throws {
|
|
|
|
func executeTest(endpointString: String, ipString: String, file: StaticString = #file, line: UInt = #line) {
|
|
|
|
XCTAssertThrowsError(try Endpoint(endpointString: endpointString)) { (error) in
|
|
|
|
guard case EndpointValidationError.invalidIP(let value) = error else {
|
|
|
|
return XCTFail("Unexpected error: \(error)", file: file, line: line)
|
|
|
|
}
|
|
|
|
XCTAssertEqual(value, ipString, file: file, line: line)
|
2018-08-15 18:35:21 +00:00
|
|
|
}
|
|
|
|
}
|
2018-08-15 20:52:37 +00:00
|
|
|
|
|
|
|
executeTest(endpointString: "12345:12345", ipString: "12345")
|
|
|
|
executeTest(endpointString: ":12345", ipString: "")
|
2018-08-15 18:35:21 +00:00
|
|
|
}
|
|
|
|
|
2018-08-15 20:52:37 +00:00
|
|
|
func testEndpoint_invalidPort() throws {
|
|
|
|
func executeTest(endpointString: String, portString: String, file: StaticString = #file, line: UInt = #line) {
|
|
|
|
XCTAssertThrowsError(try Endpoint(endpointString: endpointString)) { (error) in
|
|
|
|
guard case EndpointValidationError.invalidPort(let value) = error else {
|
|
|
|
return XCTFail("Unexpected error: \(error)", file: file, line: line)
|
|
|
|
}
|
|
|
|
XCTAssertEqual(value, portString, file: file, line: line)
|
2018-08-15 18:35:21 +00:00
|
|
|
}
|
|
|
|
}
|
2018-08-15 20:52:37 +00:00
|
|
|
|
|
|
|
executeTest(endpointString: ":", portString: "")
|
|
|
|
executeTest(endpointString: "[2607:f938:3001:4000::aac]:-12345", portString: "-12345")
|
|
|
|
executeTest(endpointString: "[2607:f938:3001:4000::aac]", portString: "aac]")
|
|
|
|
executeTest(endpointString: "[2607:f938:3001:4000::aac]:", portString: "")
|
|
|
|
executeTest(endpointString: "192.168.0.1:-12345", portString: "-12345")
|
|
|
|
executeTest(endpointString: "192.168.0.1:", portString: "")
|
|
|
|
|
2018-08-15 18:35:21 +00:00
|
|
|
}
|
|
|
|
|
2018-08-15 20:52:37 +00:00
|
|
|
func testEndpoint_noIpAndPort() throws {
|
2018-08-15 18:35:21 +00:00
|
|
|
|
2018-08-15 20:52:37 +00:00
|
|
|
func executeTest(endpointString: String, file: StaticString = #file, line: UInt = #line) {
|
2018-08-15 18:35:21 +00:00
|
|
|
XCTAssertThrowsError(try Endpoint(endpointString: endpointString)) { (error) in
|
|
|
|
guard case EndpointValidationError.noIpAndPort(let value) = error else {
|
2018-08-15 20:52:37 +00:00
|
|
|
return XCTFail("Unexpected error: \(error)", file: file, line: line)
|
2018-08-15 18:35:21 +00:00
|
|
|
}
|
2018-08-15 20:52:37 +00:00
|
|
|
XCTAssertEqual(value, endpointString, file: file, line: line)
|
2018-08-15 18:35:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
executeTest(endpointString: "192.168.0.1")
|
|
|
|
executeTest(endpointString: "12345")
|
|
|
|
}
|
2018-08-15 22:34:16 +00:00
|
|
|
|
|
|
|
func testCIDRAddress() throws {
|
|
|
|
_ = try CIDRAddress(stringRepresentation: "2607:f938:3001:4000::aac/24")
|
|
|
|
_ = try CIDRAddress(stringRepresentation: "192.168.0.1/24")
|
|
|
|
}
|
|
|
|
|
|
|
|
func testIPv4CIDRAddress() throws {
|
|
|
|
_ = try CIDRAddress(stringRepresentation: "192.168.0.1/24")
|
|
|
|
}
|
|
|
|
|
|
|
|
func testCIDRAddress_invalidIP() throws {
|
|
|
|
func executeTest(stringRepresentation: String, ipString: String, file: StaticString = #file, line: UInt = #line) {
|
|
|
|
XCTAssertThrowsError(try CIDRAddress(stringRepresentation: stringRepresentation)) { (error) in
|
|
|
|
guard case CIDRAddressValidationError.invalidIP(let value) = error else {
|
|
|
|
return XCTFail("Unexpected error: \(error)", file: file, line: line)
|
|
|
|
}
|
|
|
|
XCTAssertEqual(value, ipString, file: file, line: line)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
executeTest(stringRepresentation: "12345/12345", ipString: "12345")
|
|
|
|
executeTest(stringRepresentation: "/12345", ipString: "")
|
|
|
|
}
|
|
|
|
|
|
|
|
func testCIDRAddress_invalidSubnet() throws {
|
|
|
|
func executeTest(stringRepresentation: String, subnetString: String, file: StaticString = #file, line: UInt = #line) {
|
|
|
|
XCTAssertThrowsError(try CIDRAddress(stringRepresentation: stringRepresentation)) { (error) in
|
|
|
|
guard case CIDRAddressValidationError.invalidSubnet(let value) = error else {
|
|
|
|
return XCTFail("Unexpected error: \(error)", file: file, line: line)
|
|
|
|
}
|
|
|
|
XCTAssertEqual(value, subnetString, file: file, line: line)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
executeTest(stringRepresentation: "/", subnetString: "")
|
|
|
|
executeTest(stringRepresentation: "2607:f938:3001:4000::aac/a", subnetString: "a")
|
|
|
|
executeTest(stringRepresentation: "2607:f938:3001:4000:/aac", subnetString: "aac")
|
|
|
|
executeTest(stringRepresentation: "2607:f938:3001:4000::aac/", subnetString: "")
|
|
|
|
executeTest(stringRepresentation: "192.168.0.1/a", subnetString: "a")
|
|
|
|
executeTest(stringRepresentation: "192.168.0.1/", subnetString: "")
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func testCIDRAddress_noIpAndSubnet() throws {
|
|
|
|
|
|
|
|
func executeTest(stringRepresentation: String, file: StaticString = #file, line: UInt = #line) {
|
|
|
|
XCTAssertThrowsError(try CIDRAddress(stringRepresentation: stringRepresentation)) { (error) in
|
|
|
|
guard case CIDRAddressValidationError.noIpAndSubnet(let value) = error else {
|
|
|
|
return XCTFail("Unexpected error: \(error)", file: file, line: line)
|
|
|
|
}
|
|
|
|
XCTAssertEqual(value, stringRepresentation, file: file, line: line)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
executeTest(stringRepresentation: "192.168.0.1")
|
|
|
|
executeTest(stringRepresentation: "12345")
|
|
|
|
}
|
|
|
|
|
2018-08-15 18:35:21 +00:00
|
|
|
}
|