I would like to mock AuthenticateAsClientAsync method in the SslStream class. I also want to mock the different security cases used by the SslStream for example: IsAuthenticated, IsEncrypted and IsSigned. Mocking seems possible, because the method and the properties are virtual or override.
internal bool AuthenticateSSLStream(SslStream sslStream)
{
string certName = myCertificate.GetNameInfo(X509NameType.SimpleName, false);
bool isAuthenticationCompleted = sslStream.AuthenticateAsClientAsync(certName,
new X509Certificate2Collection(myCertificate), SslProtocols.Tls11 | SslProtocols.Tls12, true).Wait(10000);
if (!isAuthenticationCompleted || !sslStream.IsAuthenticated || !sslStream.IsEncrypted || !sslStream.IsSigned)
{
throw new AuthenticationException("Authentication is not succeeded!");
}
return isAuthenticationCompleted;
}
I have created a method and pass a SslStream in order to mock it. A X509Certificate2 certificate was passed through the constructor and stored in the variable name myCertificate.
How can I coverage all cases of the condition? How to reach the authentication exception using moq?