protected override async Task Start(TestContext ctx, SslStream sslStream, CancellationToken cancellationToken) { ctx.LogDebug(1, "Connected."); var targetHost = Parameters.TargetHost ?? PortableEndPoint.HostName ?? PortableEndPoint.Address; ctx.LogDebug(1, "Using '{0}' as target host.", targetHost); var protocol = sslStreamProvider.GetProtocol(Parameters, IsServer); var clientCertificates = sslStreamProvider.GetClientCertificates(Parameters); Task task; string function; if (HasFlag(SslStreamFlags.SyncAuthenticate)) { function = "SslStream.AuthenticateAsClient()"; ctx.LogDebug(1, "Calling {0} synchronously.", function); task = Task.Run(() => sslStream.AuthenticateAsClient(targetHost, clientCertificates, protocol, false)); } else if (HasFlag(SslStreamFlags.BeginEndAuthenticate)) { function = "SslStream.BeginAuthenticateAsClient()"; ctx.LogDebug(1, "Calling {0}.", function); task = Task.Factory.FromAsync( (callback, state) => sslStream.BeginAuthenticateAsClient(targetHost, clientCertificates, protocol, false, callback, state), (result) => sslStream.EndAuthenticateAsClient(result), null); } else { function = "SslStream.AuthenticateAsClientAsync()"; ctx.LogDebug(1, "Calling {0} async.", function); task = sslStream.AuthenticateAsClientAsync(targetHost, clientCertificates, protocol, false); } try { await task.ConfigureAwait(false); ctx.LogDebug(1, "{0} completed successfully.", function); } catch (Exception ex) { if (Parameters.ExpectClientException || Parameters.ExpectServerException) { ctx.LogDebug(1, "{0} failed (expected exception): {1}", function, ex.GetType().Name); } else { ctx.LogDebug(1, "{0} failed: {1}.", function, ex); } throw; } }
protected override async Task Start(TestContext ctx, SslStream sslStream, CancellationToken cancellationToken) { var certificate = Parameters.ServerCertificate; var protocol = sslStreamProvider.GetProtocol(Parameters, IsServer); var askForCert = Parameters.AskForClientCertificate || Parameters.RequireClientCertificate; Task task; string function; if (HasFlag(SslStreamFlags.SyncAuthenticate)) { function = "SslStream.AuthenticateAsServer()"; ctx.LogDebug(1, "Calling {0} synchronously.", function); task = Task.Run(() => sslStream.AuthenticateAsServer(certificate, askForCert, protocol, false)); } else if (HasFlag(SslStreamFlags.BeginEndAuthenticate)) { function = "SslStream.BeginAuthenticateAsServer()"; ctx.LogDebug(1, "Calling {0}.", function); task = Task.Factory.FromAsync( (callback, state) => sslStream.BeginAuthenticateAsServer(certificate, askForCert, protocol, false, callback, state), (result) => sslStream.EndAuthenticateAsServer(result), null); } else { function = "SslStream.AuthenticateAsServerAsync()"; ctx.LogDebug(1, "Calling {0} async.", function); task = sslStream.AuthenticateAsServerAsync(certificate, askForCert, protocol, false); } try { await task.ConfigureAwait(false); ctx.LogDebug(1, "{0} completed successfully.", function); } catch (Exception ex) { if (Parameters.ExpectClientException || Parameters.ExpectServerException) { ctx.LogDebug(1, "{0} failed (expected exception): {1}", function, ex.GetType().Name); } else { ctx.LogDebug(1, "{0} failed: {1}.", function, ex); } throw; } }