using System; using System.ComponentModel; using System.Security.Authentication; using Microsoft.VisualStudio.TestTools.UnitTesting; using SyncEngine.Configuration; using SyncEngine.Sync; namespace SyncEngine.Tests { [TestClass] public class MySqlTlsConnectionStringTests { [TestMethod] public void MySqlConnectionString_PinsTls12AndPreferredSsl() { var options = new SyncOptions { MySqlHost = "db.example.com", MySqlPort = 3306, MySqlUser = "sync", MySqlPassword = "secret", MySqlDatabase = "annies_angels_shop" }; var cs = options.MySqlConnectionString; StringAssert.Contains(cs, "SslMode=Preferred"); StringAssert.Contains(cs, "TlsVersion=Tls12"); StringAssert.Contains(cs, "Connection Timeout=30"); StringAssert.Contains(cs, "Server=db.example.com"); StringAssert.Contains(cs, "Database=annies_angels_shop"); } } [TestClass] public class SyncErrorFormatterSslTests { [TestMethod] public void IsSslHandshakeFailure_SslAuthenticationMessage_ReturnsTrue() { var ex = new Exception("SSL Authentication Error", new AuthenticationException("A call to SSPI failed", new Win32Exception("The function requested is not supported"))); Assert.IsTrue(SyncErrorFormatter.IsSslHandshakeFailure(ex)); } [TestMethod] public void IsSslHandshakeFailure_Unrelated_ReturnsFalse() { Assert.IsFalse(SyncErrorFormatter.IsSslHandshakeFailure(new Exception("timeout waiting for host"))); } [TestMethod] public void Describe_SslAuthentication_MentionsTlsHandshakeNotTemporaryNetwork() { // Mimic MySqlConnector wrap: outer message + AuthenticationException + Win32Exception var ex = new Exception( "SSL Authentication Error", new AuthenticationException("A call to SSPI failed", new Win32Exception("The function requested is not supported"))); var message = SyncErrorFormatter.Describe(ex); StringAssert.Contains(message, "TLS handshake"); StringAssert.Contains(message, "SBS 2011"); Assert.IsFalse( message.IndexOf("Temporary MySQL connection failure", StringComparison.OrdinalIgnoreCase) >= 0, "SSL failures must not be described as temporary network failures. Got: " + message); } } }