1- import XCTest
1+ import Testing
22import NIOCore
33@testable import PostgresNIO
44
5- class AuthenticationStateMachineTests : XCTestCase {
6-
7- func testAuthenticatePlaintext( ) {
5+ @ Suite struct AuthenticationStateMachineTests {
6+
7+ @ Test func testAuthenticatePlaintext( ) {
88 let authContext = AuthContext ( username: " test " , password: " abc123 " , database: " test " )
99
1010 var state = ConnectionStateMachine ( requireBackendKeyData: true )
11- XCTAssertEqual ( state. connected ( tls: . disable) , . provideAuthenticationContext)
12-
13- XCTAssertEqual ( state. provideAuthenticationContext ( authContext) , . sendStartupMessage( authContext) )
14- XCTAssertEqual ( state. authenticationMessageReceived ( . plaintext) , . sendPasswordMessage( . cleartext, authContext) )
15- XCTAssertEqual ( state. authenticationMessageReceived ( . ok) , . wait)
11+ #expect ( state. connected ( tls: . disable) == . provideAuthenticationContext)
12+
13+ #expect ( state. provideAuthenticationContext ( authContext) == . sendStartupMessage( authContext) )
14+ #expect ( state. authenticationMessageReceived ( . plaintext) == . sendPasswordMessage( . cleartext, authContext) )
15+ #expect ( state. authenticationMessageReceived ( . ok) == . wait)
1616 }
1717
18- func testAuthenticateMD5( ) {
18+ @ Test func testAuthenticateMD5( ) {
1919 let authContext = AuthContext ( username: " test " , password: " abc123 " , database: " test " )
2020 var state = ConnectionStateMachine ( requireBackendKeyData: true )
21- XCTAssertEqual ( state. connected ( tls: . disable) , . provideAuthenticationContext)
21+ #expect ( state. connected ( tls: . disable) == . provideAuthenticationContext)
2222 let salt : UInt32 = 0x00_01_02_03
2323
24- XCTAssertEqual ( state. provideAuthenticationContext ( authContext) , . sendStartupMessage( authContext) )
25- XCTAssertEqual ( state. authenticationMessageReceived ( . md5( salt: salt) ) , . sendPasswordMessage( . md5( salt: salt) , authContext) )
26- XCTAssertEqual ( state. authenticationMessageReceived ( . ok) , . wait)
24+ #expect ( state. provideAuthenticationContext ( authContext) == . sendStartupMessage( authContext) )
25+ #expect ( state. authenticationMessageReceived ( . md5( salt: salt) ) == . sendPasswordMessage( . md5( salt: salt) , authContext) )
26+ #expect ( state. authenticationMessageReceived ( . ok) == . wait)
2727 }
2828
29- func testAuthenticateMD5WithoutPassword( ) {
29+ @ Test func testAuthenticateMD5WithoutPassword( ) {
3030 let authContext = AuthContext ( username: " test " , password: nil , database: " test " )
3131 var state = ConnectionStateMachine ( requireBackendKeyData: true )
32- XCTAssertEqual ( state. connected ( tls: . disable) , . provideAuthenticationContext)
32+ #expect ( state. connected ( tls: . disable) == . provideAuthenticationContext)
3333 let salt : UInt32 = 0x00_01_02_03
3434
35- XCTAssertEqual ( state. provideAuthenticationContext ( authContext) , . sendStartupMessage( authContext) )
36- XCTAssertEqual ( state. authenticationMessageReceived ( . md5( salt: salt) ) ,
35+ #expect ( state. provideAuthenticationContext ( authContext) == . sendStartupMessage( authContext) )
36+ #expect ( state. authenticationMessageReceived ( . md5( salt: salt) ) ==
3737 . closeConnectionAndCleanup( . init( action: . close, tasks: [ ] , error: . authMechanismRequiresPassword, closePromise: nil ) ) )
3838 }
3939
40- func testAuthenticateOkAfterStartUpWithoutAuthChallenge( ) {
40+ @ Test func testAuthenticateOkAfterStartUpWithoutAuthChallenge( ) {
4141 let authContext = AuthContext ( username: " test " , password: " abc123 " , database: " test " )
4242 var state = ConnectionStateMachine ( requireBackendKeyData: true )
43- XCTAssertEqual ( state. connected ( tls: . disable) , . provideAuthenticationContext)
44- XCTAssertEqual ( state. provideAuthenticationContext ( authContext) , . sendStartupMessage( authContext) )
45- XCTAssertEqual ( state. authenticationMessageReceived ( . ok) , . wait)
43+ #expect ( state. connected ( tls: . disable) == . provideAuthenticationContext)
44+ #expect ( state. provideAuthenticationContext ( authContext) == . sendStartupMessage( authContext) )
45+ #expect ( state. authenticationMessageReceived ( . ok) == . wait)
4646 }
4747
48- func testAuthenticateSCRAMSHA256WithAtypicalEncoding( ) {
48+ @ Test func testAuthenticateSCRAMSHA256WithAtypicalEncoding( ) {
4949 let authContext = AuthContext ( username: " test " , password: " abc123 " , database: " test " )
5050 var state = ConnectionStateMachine ( requireBackendKeyData: true )
51- XCTAssertEqual ( state. connected ( tls: . disable) , . provideAuthenticationContext)
52- XCTAssertEqual ( state. provideAuthenticationContext ( authContext) , . sendStartupMessage( authContext) )
53-
51+ #expect ( state. connected ( tls: . disable) == . provideAuthenticationContext)
52+ #expect ( state. provideAuthenticationContext ( authContext) == . sendStartupMessage( authContext) )
53+
5454 let saslResponse = state. authenticationMessageReceived ( . sasl( names: [ " SCRAM-SHA-256 " ] ) )
5555 guard case . sendSaslInitialResponse( name: let name, initialResponse: let responseData) = saslResponse else {
56- return XCTFail ( " \( saslResponse) is not .sendSaslInitialResponse " )
56+ Issue . record ( " \( saslResponse) is not .sendSaslInitialResponse " )
57+ return
5758 }
5859 let responseString = String ( decoding: responseData, as: UTF8 . self)
59- XCTAssertEqual ( name, " SCRAM-SHA-256 " )
60- XCTAssert ( responseString. starts ( with: " n,,n=test,r= " ) )
61-
60+ #expect ( name == " SCRAM-SHA-256 " )
61+ #expect ( responseString. starts ( with: " n,,n=test,r= " ) )
62+
6263 let saslContinueResponse = state. authenticationMessageReceived ( . saslContinue( data: . init( bytes:
6364 " r= \( responseString. dropFirst ( 12 ) ) RUJSZHhkeUVFNzRLNERKMkxmU05ITU1NZWcxaQ==,s=ijgUVaWgCDLRJyF963BKNA==,i=4096 " . utf8
6465 ) ) )
6566 guard case . sendSaslResponse( let responseData2) = saslContinueResponse else {
66- return XCTFail ( " \( saslContinueResponse) is not .sendSaslResponse " )
67+ Issue . record ( " \( saslContinueResponse) is not .sendSaslResponse " )
68+ return
6769 }
6870 let response2String = String ( decoding: responseData2, as: UTF8 . self)
69- XCTAssertEqual ( response2String. prefix ( 76 ) , " c=biws,r= \( responseString. dropFirst ( 12 ) ) RUJSZHhkeUVFNzRLNERKMkxmU05ITU1NZWcxaQ==,p= " )
71+ #expect ( response2String. prefix ( 76 ) == " c=biws,r= \( responseString. dropFirst ( 12 ) ) RUJSZHhkeUVFNzRLNERKMkxmU05ITU1NZWcxaQ==,p= " )
7072 }
7173
72- func testAuthenticationFailure( ) {
74+ @ Test func testAuthenticationFailure( ) {
7375 let authContext = AuthContext ( username: " test " , password: " abc123 " , database: " test " )
7476 var state = ConnectionStateMachine ( requireBackendKeyData: true )
75- XCTAssertEqual ( state. connected ( tls: . disable) , . provideAuthenticationContext)
77+ #expect ( state. connected ( tls: . disable) == . provideAuthenticationContext)
7678 let salt : UInt32 = 0x00_01_02_03
7779
78- XCTAssertEqual ( state. provideAuthenticationContext ( authContext) , . sendStartupMessage( authContext) )
79- XCTAssertEqual ( state. authenticationMessageReceived ( . md5( salt: salt) ) , . sendPasswordMessage( . md5( salt: salt) , authContext) )
80+ #expect ( state. provideAuthenticationContext ( authContext) == . sendStartupMessage( authContext) )
81+ #expect ( state. authenticationMessageReceived ( . md5( salt: salt) ) == . sendPasswordMessage( . md5( salt: salt) , authContext) )
8082 let fields : [ PostgresBackendMessage . Field : String ] = [
8183 . message: " password authentication failed for user \" postgres \" " ,
8284 . severity: " FATAL " ,
@@ -86,13 +88,13 @@ class AuthenticationStateMachineTests: XCTestCase {
8688 . line: " 334 " ,
8789 . file: " auth.c "
8890 ]
89- XCTAssertEqual ( state. errorReceived ( . init( fields: fields) ) ,
91+ #expect ( state. errorReceived ( . init( fields: fields) ) ==
9092 . closeConnectionAndCleanup( . init( action: . close, tasks: [ ] , error: . server( . init( fields: fields) ) , closePromise: nil ) ) )
9193 }
9294
9395 // MARK: Test unsupported messages
9496
95- func testUnsupportedAuthMechanism( ) {
97+ @ Test func testUnsupportedAuthMechanism( ) {
9698 let unsupported : [ ( PostgresBackendMessage . Authentication , PSQLError . UnsupportedAuthScheme ) ] = [
9799 ( . kerberosV5, . kerberosV5) ,
98100 ( . scmCredential, . scmCredential) ,
@@ -104,14 +106,14 @@ class AuthenticationStateMachineTests: XCTestCase {
104106 for (message, mechanism) in unsupported {
105107 let authContext = AuthContext ( username: " test " , password: " abc123 " , database: " test " )
106108 var state = ConnectionStateMachine ( requireBackendKeyData: true )
107- XCTAssertEqual ( state. connected ( tls: . disable) , . provideAuthenticationContext)
108- XCTAssertEqual ( state. provideAuthenticationContext ( authContext) , . sendStartupMessage( authContext) )
109- XCTAssertEqual ( state. authenticationMessageReceived ( message) ,
109+ #expect ( state. connected ( tls: . disable) == . provideAuthenticationContext)
110+ #expect ( state. provideAuthenticationContext ( authContext) == . sendStartupMessage( authContext) )
111+ #expect ( state. authenticationMessageReceived ( message) ==
110112 . closeConnectionAndCleanup( . init( action: . close, tasks: [ ] , error: . unsupportedAuthMechanism( mechanism) , closePromise: nil ) ) )
111113 }
112114 }
113115
114- func testUnexpectedMessagesAfterStartUp( ) {
116+ @ Test func testUnexpectedMessagesAfterStartUp( ) {
115117 var buffer = ByteBuffer ( )
116118 buffer. writeBytes ( [ 0 , 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 ] )
117119 let unexpected : [ PostgresBackendMessage . Authentication ] = [
@@ -123,14 +125,14 @@ class AuthenticationStateMachineTests: XCTestCase {
123125 for message in unexpected {
124126 let authContext = AuthContext ( username: " test " , password: " abc123 " , database: " test " )
125127 var state = ConnectionStateMachine ( requireBackendKeyData: true )
126- XCTAssertEqual ( state. connected ( tls: . disable) , . provideAuthenticationContext)
127- XCTAssertEqual ( state. provideAuthenticationContext ( authContext) , . sendStartupMessage( authContext) )
128- XCTAssertEqual ( state. authenticationMessageReceived ( message) ,
128+ #expect ( state. connected ( tls: . disable) == . provideAuthenticationContext)
129+ #expect ( state. provideAuthenticationContext ( authContext) == . sendStartupMessage( authContext) )
130+ #expect ( state. authenticationMessageReceived ( message) ==
129131 . closeConnectionAndCleanup( . init( action: . close, tasks: [ ] , error: . unexpectedBackendMessage( . authentication( message) ) , closePromise: nil ) ) )
130132 }
131133 }
132134
133- func testUnexpectedMessagesAfterPasswordSent( ) {
135+ @ Test func testUnexpectedMessagesAfterPasswordSent( ) {
134136 let salt : UInt32 = 0x00_01_02_03
135137 var buffer = ByteBuffer ( )
136138 buffer. writeBytes ( [ 0 , 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 ] )
@@ -150,10 +152,10 @@ class AuthenticationStateMachineTests: XCTestCase {
150152 for message in unexpected {
151153 let authContext = AuthContext ( username: " test " , password: " abc123 " , database: " test " )
152154 var state = ConnectionStateMachine ( requireBackendKeyData: true )
153- XCTAssertEqual ( state. connected ( tls: . disable) , . provideAuthenticationContext)
154- XCTAssertEqual ( state. provideAuthenticationContext ( authContext) , . sendStartupMessage( authContext) )
155- XCTAssertEqual ( state. authenticationMessageReceived ( . md5( salt: salt) ) , . sendPasswordMessage( . md5( salt: salt) , authContext) )
156- XCTAssertEqual ( state. authenticationMessageReceived ( message) ,
155+ #expect ( state. connected ( tls: . disable) == . provideAuthenticationContext)
156+ #expect ( state. provideAuthenticationContext ( authContext) == . sendStartupMessage( authContext) )
157+ #expect ( state. authenticationMessageReceived ( . md5( salt: salt) ) == . sendPasswordMessage( . md5( salt: salt) , authContext) )
158+ #expect ( state. authenticationMessageReceived ( message) ==
157159 . closeConnectionAndCleanup( . init( action: . close, tasks: [ ] , error: . unexpectedBackendMessage( . authentication( message) ) , closePromise: nil ) ) )
158160 }
159161 }
0 commit comments