diff --git a/.gitignore b/.gitignore index 2acce1b1..ea8fec49 100644 --- a/.gitignore +++ b/.gitignore @@ -3,4 +3,5 @@ /Packages /*.xcodeproj Package.resolved +DerivedData diff --git a/Sources/PostgreSQL/Data/PostgreSQLData+Point.swift b/Sources/PostgreSQL/Data/PostgreSQLData+Point.swift index cf6415cd..bb78039a 100644 --- a/Sources/PostgreSQL/Data/PostgreSQLData+Point.swift +++ b/Sources/PostgreSQL/Data/PostgreSQLData+Point.swift @@ -49,8 +49,10 @@ extension PostgreSQLPoint: PostgreSQLDataConvertible { let parts = string.split(separator: ",") var x = parts[0] var y = parts[1] - assert(x.popFirst()! == "(") - assert(y.popLast()! == ")") + let leftParen = x.popFirst() + assert(leftParen == "(") + let rightParen = y.popLast() + assert(rightParen == ")") return .init(x: Double(x)!, y: Double(y)!) case .binary: let x = value[0..<8] diff --git a/Sources/PostgreSQL/Database/PostgreSQLTransportConfig.swift b/Sources/PostgreSQL/Database/PostgreSQLTransportConfig.swift index 764ba9a2..32996d34 100644 --- a/Sources/PostgreSQL/Database/PostgreSQLTransportConfig.swift +++ b/Sources/PostgreSQL/Database/PostgreSQLTransportConfig.swift @@ -36,6 +36,14 @@ public struct PostgreSQLTransportConfig { public static func customTLS(_ tlsConfiguration: TLSConfiguration)-> PostgreSQLTransportConfig { return .init(method: .tls(tlsConfiguration)) } + + /// Returns `true` if this configuration uses TLS. + public var isTLS: Bool { + switch method { + case .cleartext: return false + case .tls: return true + } + } internal enum Method { case cleartext diff --git a/Sources/PostgreSQL/Utilities.swift b/Sources/PostgreSQL/Utilities.swift index 15b8db9b..4953c993 100644 --- a/Sources/PostgreSQL/Utilities.swift +++ b/Sources/PostgreSQL/Utilities.swift @@ -37,7 +37,8 @@ extension Data { return } for _ in 0.. 350 { let name = try results[128].firstValue(forColumn: "typname")?.decode(String.self) @@ -31,7 +31,7 @@ class PostgreSQLConnectionTests: XCTestCase { } func testParse() throws { - let client = try PostgreSQLConnection.makeTest() + let client = try PostgreSQLConnection.makeTest(transport: .cleartext) let query = """ select * from "pg_type" where "typlen" = $1 or "typlen" = $2 """ @@ -46,7 +46,7 @@ class PostgreSQLConnectionTests: XCTestCase { } func testTypes() throws { - let client = try PostgreSQLConnection.makeTest() + let client = try PostgreSQLConnection.makeTest(transport: .cleartext) let createQuery = """ create table kitchen_sink ( "smallint" smallint, @@ -139,7 +139,7 @@ class PostgreSQLConnectionTests: XCTestCase { } func testParameterizedTypes() throws { - let client = try PostgreSQLConnection.makeTest() + let client = try PostgreSQLConnection.makeTest(transport: .cleartext) let createQuery = """ create table kitchen_sink ( "smallint" smallint, @@ -262,7 +262,7 @@ class PostgreSQLConnectionTests: XCTestCase { var message: String } - let client = try PostgreSQLConnection.makeTest() + let client = try PostgreSQLConnection.makeTest(transport: .cleartext) _ = try client.query("drop table if exists foo;").wait() let createResult = try client.query("create table foo (id integer, dict jsonb);").wait() XCTAssertEqual(createResult.count, 0) @@ -282,7 +282,7 @@ class PostgreSQLConnectionTests: XCTestCase { } func testNull() throws { - let client = try PostgreSQLConnection.makeTest() + let client = try PostgreSQLConnection.makeTest(transport: .cleartext) _ = try client.query("drop table if exists nulltest;").wait() let createResult = try client.query("create table nulltest (i integer not null, d timestamp);").wait() XCTAssertEqual(createResult.count, 0) @@ -297,7 +297,7 @@ class PostgreSQLConnectionTests: XCTestCase { func testGH24() throws { /// PREPARE - let client = try PostgreSQLConnection.makeTest() + let client = try PostgreSQLConnection.makeTest(transport: .cleartext) _ = try client.query(""" DROP TABLE IF EXISTS "acronym+category" """).wait() @@ -448,7 +448,7 @@ class PostgreSQLConnectionTests: XCTestCase { var count: Int } - let connection = try PostgreSQLConnection.makeTest() + let connection = try PostgreSQLConnection.makeTest(transport: .cleartext) _ = try connection.simpleQuery("DROP TABLE IF EXISTS apps").wait() _ = try connection.simpleQuery("CREATE TABLE apps (id INT, platform TEXT, identifier TEXT)").wait() _ = try connection.simpleQuery("INSERT INTO apps VALUES (1, 'a', 'b')").wait() @@ -486,24 +486,16 @@ class PostgreSQLConnectionTests: XCTestCase { } extension PostgreSQLConnection { - /// Creates a test event loop and psql client. - static func makeTest() throws -> PostgreSQLConnection { - #if Xcode - return try _makeTest(hostname: self.dockerMachineHostname) - #else - return try _makeTest(hostname: "localhost") - #endif - } - /// Creates a test event loop and psql client over ssl. static func makeTest(transport: PostgreSQLTransportConfig) throws -> PostgreSQLConnection { - #if Xcode - return try _makeTest(hostname: self.dockerMachineHostname, port: 5433, transport: transport) + #if os(macOS) + return try _makeTest(hostname: "192.168.99.100", password: "vapor_password", port: transport.isTLS ? 5433 : 5432, transport: transport) #else - return try _makeTest(hostname: "localhost-ssl", password: "vapor_password", transport: transport) + return try _makeTest(hostname: transport.isTLS ? "tls" : "cleartext", password: "vapor_password", transport: transport) #endif } - + + /// Creates a test connection. private static func _makeTest(hostname: String, password: String? = nil, port: Int = 5432, transport: PostgreSQLTransportConfig = .cleartext) throws -> PostgreSQLConnection { let group = MultiThreadedEventLoopGroup(numThreads: 1) let client = try PostgreSQLConnection.connect(hostname: hostname, port: port, transport: transport, on: group) { error in @@ -512,10 +504,6 @@ extension PostgreSQLConnection { _ = try client.authenticate(username: "vapor_username", database: "vapor_database", password: password).wait() return client } - - private static var dockerMachineHostname: String { - return (try? Process.execute("docker-machine", "ip")) ?? "192.168.99.100" - } } func +=(lhs: inout [T], rhs: T) { diff --git a/circle.yml b/circle.yml index 67bede28..5173c677 100644 --- a/circle.yml +++ b/circle.yml @@ -14,12 +14,13 @@ jobs: docker: - image: codevapor/swift:4.1 - image: circleci/postgres:latest + name: cleartext environment: POSTGRES_USER: vapor_username POSTGRES_DB: vapor_database POSTGRES_PASSWORD: vapor_password - image: scenecheck/postgres-ssl:latest - name: localhost-ssl + name: tls environment: POSTGRES_USER: vapor_username POSTGRES_DB: vapor_database diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 00000000..df139c4f --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,19 @@ +version: '2' + +services: + cleartext: + image: postgres:latest + environment: + POSTGRES_USER: vapor_username + POSTGRES_DB: vapor_database + POSTGRES_PASSWORD: vapor_password + ports: + - 5432:5432 + tls: + image: scenecheck/postgres-ssl:latest + environment: + POSTGRES_USER: vapor_username + POSTGRES_DB: vapor_database + POSTGRES_PASSWORD: vapor_password + ports: + - 5433:5432