@@ -5,7 +5,7 @@ import Utils
55
66@testable import Networking
77
8- final class PeerTests {
8+ final class MockPeerEventTests {
99 let registration : QuicRegistration
1010 let certData : Data
1111 let badCertData : Data
@@ -98,6 +98,66 @@ final class PeerTests {
9898 )
9999 }
100100
101+ @Test
102+ func connected( ) async throws {
103+ let serverHandler = MockPeerEventHandler ( )
104+ let clientHandler = MockPeerEventHandler ( )
105+ let privateKey1 = try Ed25519 . SecretKey ( from: Data32 ( ) )
106+ let cert = try generateSelfSignedCertificate ( privateKey: privateKey1)
107+ let serverConfiguration = try QuicConfiguration (
108+ registration: registration,
109+ pkcs12: certData,
110+ alpns: [ Data ( " testalpn " . utf8) ] ,
111+ client: false ,
112+ settings: QuicSettings . defaultSettings
113+ )
114+
115+ let listener = try QuicListener (
116+ handler: serverHandler,
117+ registration: registration,
118+ configuration: serverConfiguration,
119+ listenAddress: NetAddr ( ipAddress: " 127.0.0.1 " , port: 0 ) !,
120+ alpns: [ Data ( " testalpn " . utf8) ]
121+ )
122+
123+ let listenAddress = try listener. listenAddress ( )
124+ // Client setup with certificate
125+ let clientConfiguration = try QuicConfiguration (
126+ registration: registration,
127+ pkcs12: cert,
128+ alpns: [ Data ( " testalpn " . utf8) ] ,
129+ client: true ,
130+ settings: QuicSettings . defaultSettings
131+ )
132+
133+ let clientConnection = try QuicConnection (
134+ handler: clientHandler,
135+ registration: registration,
136+ configuration: clientConfiguration
137+ )
138+
139+ // Attempt to connect
140+ try clientConnection. connect ( to: listenAddress)
141+ let stream1 = try clientConnection. createStream ( )
142+ try stream1. send ( data: Data ( " test data 1 " . utf8) )
143+
144+ try ? await Task . sleep ( for: . milliseconds( 100 ) )
145+ let ( _, info) = serverHandler. events. value. compactMap {
146+ switch $0 {
147+ case let . newConnection( _, connection, info) :
148+ ( connection, info) as ( QuicConnection , ConnectionInfo ) ?
149+ default :
150+ nil
151+ }
152+ } . first!
153+ let ( ipAddress2, _) = info. remoteAddress. getAddressAndPort ( )
154+
155+ #expect( info. negotiatedAlpn == Data ( " testalpn " . utf8) )
156+ #expect( info. serverName == " 127.0.0.1 " )
157+ #expect( info. localAddress == listenAddress)
158+ #expect( ipAddress2 == " 127.0.0.1 " )
159+ }
160+
101161 @Test
102162 func rejectsConDueToBadClientCert( ) async throws {
103163 let serverHandler = MockPeerEventHandler ( )
0 commit comments