public void TestAuthenticateAnonymousMechanismSpecifiedAuthenticationFailure() { using (var server = new PopPseudoServer()) { server.Start(); var prof = new Profile(null, "user%40pop.example.net;auth=anonymous", server.HostPort); // greeting server.EnqueueResponse("+OK\r\n"); // CAPA response server.EnqueueResponse("+OK\r\n" + "SASL ANONYMOUS\r\n" + ".\r\n"); // AUTH response server.EnqueueResponse("-ERR\r\n"); try { Assert.IsNull(PopSessionCreator.CreateSession(prof, null, null)); Assert.Fail("PopAuthenticationException not thrown"); } catch (PopAuthenticationException ex) { Assert.IsNull(ex.InnerException); Assert.IsNotNull(ex.Result); Assert.AreEqual(Protocol.Client.PopCommandResultCode.Error, ex.Result.Code); } server.DequeueRequest(); // CAPA StringAssert.StartsWith("AUTH ANONYMOUS dXNlckBwb3AuZXhhbXBsZS5uZXQ=", server.DequeueRequest()); } }
public void TestAuthenticateAnonymousMechanismSpecified() { using (var server = new PopPseudoServer()) { server.Start(); var prof = new Profile(null, "user%40pop.example.net;auth=anonymous", server.HostPort); // greeting server.EnqueueResponse("+OK\r\n"); // CAPA response server.EnqueueResponse("+OK\r\n" + "SASL ANONYMOUS\r\n" + ".\r\n"); // AUTH response server.EnqueueResponse("+OK\r\n"); using (var session = PopSessionCreator.CreateSession(prof, null, null)) { Assert.AreEqual(PopSessionState.Transaction, session.State); Assert.AreEqual(prof.Authority, session.Authority); } server.DequeueRequest(); // CAPA StringAssert.StartsWith("AUTH ANONYMOUS dXNlckBwb3AuZXhhbXBsZS5uZXQ=", server.DequeueRequest()); } }
public void TestCreateSessionStlsFailure() { using (var server = new PopPseudoServer()) { server.Start(); var prof = new Profile(new NetworkCredential("user", "pass"), "user", server.HostPort); // greeting server.EnqueueResponse("+OK\r\n"); // CAPA response server.EnqueueResponse("+OK\r\n" + "STLS\r\n" + ".\r\n"); // STLS response server.EnqueueResponse("-ERR\r\n"); var streamUpgraded = false; try { using (var session = PopSessionCreator.CreateSession(prof, null, delegate(ConnectionBase connection, Stream baseStream) { streamUpgraded = true; return baseStream; // TODO: return SSL stream })); Assert.Fail("PopUpgradeConnectionException not thrown"); } catch (PopUpgradeConnectionException ex) { Assert.IsNull(ex.InnerException); } Assert.IsFalse(streamUpgraded, "stream upgraded"); server.DequeueRequest(); // CAPA StringAssert.StartsWith("STLS", server.DequeueRequest()); } }
public void TestCreateSessionStlsException() { using (var server = new PopPseudoServer()) { server.Start(); var prof = new Profile(new NetworkCredential("user", "pass"), "user", server.HostPort); // greeting server.EnqueueResponse("+OK\r\n"); // CAPA response server.EnqueueResponse("+OK\r\n" + "STLS\r\n" + ".\r\n"); // STLS response server.EnqueueResponse("+OK\r\n"); try { using (var session = PopSessionCreator.CreateSession(prof, null, delegate(ConnectionBase connection, Stream baseStream) { throw new System.Security.Authentication.AuthenticationException(); })); Assert.Fail("PopUpgradeConnectionException not thrown"); } catch (PopUpgradeConnectionException ex) { Assert.IsNotNull(ex.InnerException); Assert.IsInstanceOfType(typeof(System.Security.Authentication.AuthenticationException), ex.InnerException); } server.DequeueRequest(); // CAPA StringAssert.StartsWith("STLS", server.DequeueRequest()); } }
public void TestAuthenticateUserAndAuthMechanismSpecifiedAuthenticationFailure() { using (var server = new PopPseudoServer()) { server.Start(); var prof = new Profile(new NetworkCredential("user", "pass"), "user;auth=plain", server.HostPort); // greeting server.EnqueueResponse("+OK\r\n"); // CAPA response server.EnqueueResponse("+OK\r\n" + "SASL PLAIN\r\n" + ".\r\n"); // AUTH response server.EnqueueResponse("-ERR\r\n"); try { Assert.IsNull(PopSessionCreator.CreateSession(prof, null, null)); Assert.Fail("PopAuthenticationException not thrown"); } catch (PopAuthenticationException ex) { Assert.IsNull(ex.InnerException); Assert.IsNotNull(ex.Result); Assert.AreEqual(Protocol.Client.PopCommandResultCode.Error, ex.Result.Code); } server.DequeueRequest(); // CAPA StringAssert.StartsWith("AUTH PLAIN", server.DequeueRequest()); } }
public void TestCreateSessionUseTlsIfAvailable() { using (var server = new PopPseudoServer()) { server.Start(); var credential = new NetworkCredential("user", "pass"); var requestUri = new Uri(string.Format("pop://{0}@{1}/", credential.UserName, server.HostPort)); var request = WebRequest.Create(requestUri) as PopWebRequest; request.Credentials = credential; request.UseTlsIfAvailable = true; request.KeepAlive = false; request.Method = "NOOP"; request.Timeout = 1000; // greeting server.EnqueueResponse("+OK\r\n"); // CAPA response server.EnqueueResponse("+OK\r\n" + "STLS\r\n" + ".\r\n"); // STLS response server.EnqueueResponse("+OK\r\n"); // CAPA response server.EnqueueResponse("+OK\r\n" + ".\r\n"); // USER response server.EnqueueResponse("+OK\r\n"); // PASS response server.EnqueueResponse("+OK\r\n"); // NOOP response server.EnqueueResponse("+OK\r\n"); // QUIT response server.EnqueueResponse("+OK\r\n"); var defaultCallback = PopSessionManager.CreateSslStreamCallback; try { var streamUpgraded = false; PopSessionManager.CreateSslStreamCallback = delegate(ConnectionBase connection, Stream baseStream) { streamUpgraded = true; return baseStream; // TODO: return SSL stream }; using (var response = request.GetResponse()) { Assert.AreEqual(response.ResponseUri, requestUri); } Assert.IsTrue(streamUpgraded, "stream upgraded"); server.DequeueRequest(); // CAPA StringAssert.StartsWith("STLS", server.DequeueRequest()); } finally { PopSessionManager.CreateSslStreamCallback = defaultCallback; } } }
private void AuthenticateWithAppropriateMechanism(string userinfo) { using (var server = new PopPseudoServer()) { server.Start(); var prof = new Profile(new NetworkCredential("user", "pass"), userinfo, server.HostPort); prof.UsingSaslMechanisms = new[] {"ANONYMOUS", "LOGIN", "PLAIN"}; prof.AllowInsecureLogin = true; // greeting server.EnqueueResponse("+OK\r\n"); // CAPA response server.EnqueueResponse("+OK\r\n" + "SASL DIGEST-MD5 CRAM-MD5 NTLM PLAIN LOGIN ANONYMOUS\r\n" + ".\r\n"); // AUTH LOGIN response server.EnqueueResponse("+ \r\n"); server.EnqueueResponse("+OK\r\n"); using (var session = PopSessionCreator.CreateSession(prof, null, null)) { Assert.AreEqual(PopSessionState.Transaction, session.State); Assert.AreEqual(prof.Authority, session.Authority); } server.DequeueRequest(); // CAPA StringAssert.StartsWith("AUTH LOGIN", server.DequeueRequest()); } }
private void AuthenticateInsecureLoginDisallowedSecureSession(bool apopAvailable) { using (var server = new PopPseudoServer()) { server.Start(); var prof = new Profile(new NetworkCredential("user", "pass"), "user", server.HostPort); prof.UsingSaslMechanisms = new[] {"ANONYMOUS", "LOGIN", "PLAIN"}; prof.AllowInsecureLogin = false; // greeting server.EnqueueResponse(apopAvailable ? "+OK <timestamp@localhost>\r\n" : "+OK\r\n"); // CAPA response server.EnqueueResponse("+OK\r\n" + "STLS\r\n" + ".\r\n"); // STLS response server.EnqueueResponse("+OK\r\n"); // CAPA response server.EnqueueResponse("+OK\r\n" + "SASL PLAIN LOGIN ANONYMOUS\r\n" + ".\r\n"); // AUTH LOGIN response server.EnqueueResponse("-ERR\r\n"); // AUTH PLAIN response server.EnqueueResponse("-ERR\r\n"); if (apopAvailable) { // APOP response server.EnqueueResponse("+OK\r\n"); } else { // USER response server.EnqueueResponse("+OK\r\n"); // PASS response server.EnqueueResponse("+OK\r\n"); } using (var session = PopSessionCreator.CreateSession(prof, null, delegate(ConnectionBase connection, Stream baseStream) { return baseStream; // TODO: return SSL stream })) { Assert.AreEqual(PopSessionState.Transaction, session.State); Assert.AreEqual(prof.Authority, session.Authority); Assert.IsTrue(session.IsSecureConnection); } server.DequeueRequest(); // CAPA server.DequeueRequest(); // STLS server.DequeueRequest(); // CAPA StringAssert.Contains("AUTH LOGIN", server.DequeueRequest()); StringAssert.Contains("AUTH PLAIN", server.DequeueRequest()); if (apopAvailable) { StringAssert.Contains("APOP", server.DequeueRequest()); } else { StringAssert.Contains("USER", server.DequeueRequest()); StringAssert.Contains("PASS", server.DequeueRequest()); } } }
public void TestAuthenticateNoUserAndNoAuthMechanismSpecified() { using (var server = new PopPseudoServer()) { server.Start(); var prof = new Profile(null, null, server.HostPort); // greeting server.EnqueueResponse("+OK\r\n"); // CAPA response server.EnqueueResponse("+OK\r\n" + "SASL ANONYMOUS\r\n" + ".\r\n"); // AUTH response server.EnqueueResponse("-ERR\r\n"); // USER response server.EnqueueResponse("+OK\r\n"); // PASS response server.EnqueueResponse("+OK\r\n"); using (var session = PopSessionCreator.CreateSession(prof, null, null)) { Assert.AreEqual(PopSessionState.Transaction, session.State); Assert.AreEqual(prof.Authority, session.Authority); } server.DequeueRequest(); // CAPA StringAssert.StartsWith("AUTH ANONYMOUS YW5vbnltb3Vz", server.DequeueRequest()); StringAssert.StartsWith("USER anonymous", server.DequeueRequest()); StringAssert.StartsWith("PASS anonymous@", server.DequeueRequest()); } }
public void TestAuthenticateInsecureLoginDisallowedFallbackToNextAppropriateMechanismAuthenticationFailure() { using (var server = new PopPseudoServer()) { server.Start(); var prof = new Profile(new NetworkCredential("user", "pass"), "user;auth=*", server.HostPort); prof.UsingSaslMechanisms = new[] {"LOGIN", "PLAIN", "ANONYMOUS", "DIGEST-MD5", "CRAM-MD5"}; prof.AllowInsecureLogin = false; // greeting server.EnqueueResponse("+OK <timestamp@localhost>\r\n"); // CAPA response server.EnqueueResponse("+OK\r\n" + "SASL X-UNKNOWN DIGEST-MD5 CRAM-MD5 NTLM PLAIN LOGIN ANONYMOUS\r\n" + ".\r\n"); // AUTH DIGEST-MD5 response server.EnqueueResponse("+ cmVhbG09ImVsd29vZC5pbm5vc29mdC5jb20iLG5vbmNlPSJPQTZNRzl0" + "RVFHbTJoaCIscW9wPSJhdXRoIixhbGdvcml0aG09bWQ1LXNlc3MsY2hh" + "cnNldD11dGYtOA==\r\n"); server.EnqueueResponse("-ERR\r\n"); // AUTH CRAM-MD5 response server.EnqueueResponse("+ PDQwMDEzNDQxMTIxNDM1OTQuMTI3MjQ5OTU1MEBsb2NhbGhvc3Q+\r\n"); server.EnqueueResponse("-ERR\r\n"); try { Assert.IsNull(PopSessionCreator.CreateSession(prof, null, null)); Assert.Fail("PopAuthenticationException not thrown"); } catch (PopAuthenticationException ex) { Assert.IsNull(ex.InnerException); Assert.IsNotNull(ex.Result); Assert.AreEqual(Protocol.Client.PopCommandResultCode.Error, ex.Result.Code); } server.DequeueRequest(); // CAPA StringAssert.Contains("AUTH DIGEST-MD5", server.DequeueRequest()); server.DequeueRequest(); // AUTH DIGEST-MD5 client response StringAssert.Contains("AUTH CRAM-MD5", server.DequeueRequest()); } }
private void AuthenticateWithAppropriateMechanismFallbackToUserPassAuthenticationFailure(string userinfo) { using (var server = new PopPseudoServer()) { server.Start(); var prof = new Profile(new NetworkCredential("user", "pass"), userinfo, server.HostPort); prof.AllowInsecureLogin = true; // greeting server.EnqueueResponse("+OK\r\n"); // CAPA response server.EnqueueResponse("+OK\r\n" + ".\r\n"); // USER response server.EnqueueResponse("-ERR\r\n"); try { Assert.IsNull(PopSessionCreator.CreateSession(prof, null, null)); Assert.Fail("PopAuthenticationException not thrown"); } catch (PopAuthenticationException ex) { Assert.IsNull(ex.InnerException); Assert.IsNotNull(ex.Result); Assert.AreEqual(Protocol.Client.PopCommandResultCode.Error, ex.Result.Code); } server.DequeueRequest(); // CAPA StringAssert.StartsWith("USER user", server.DequeueRequest()); } }
private void AuthenticateWithAppropriateMechanismFallbackToUserPass(string userinfo) { using (var server = new PopPseudoServer()) { server.Start(); var prof = new Profile(new NetworkCredential("user", "pass"), userinfo, server.HostPort); prof.AllowInsecureLogin = true; // greeting server.EnqueueResponse("+OK\r\n"); // CAPA response server.EnqueueResponse("+OK\r\n" + ".\r\n"); // USER response server.EnqueueResponse("+OK\r\n"); // PASS response server.EnqueueResponse("+OK\r\n"); using (var session = PopSessionCreator.CreateSession(prof, null, null)) { Assert.AreEqual(PopSessionState.Transaction, session.State); Assert.AreEqual(prof.Authority, session.Authority); } server.DequeueRequest(); // CAPA StringAssert.StartsWith("USER user", server.DequeueRequest()); StringAssert.StartsWith("PASS pass", server.DequeueRequest()); } }
public void TestAuthenticateInsecureLoginDisallowedAppropriateMechanismNotFound() { using (var server = new PopPseudoServer()) { server.Start(); var prof = new Profile(new NetworkCredential("user", "pass"), "user;auth=*", server.HostPort); prof.UsingSaslMechanisms = new[] {"ANONYMOUS", "LOGIN", "PLAIN"}; prof.AllowInsecureLogin = false; // greeting server.EnqueueResponse("+OK <timestamp@localhost>\r\n"); // CAPA response server.EnqueueResponse("+OK\r\n" + "SASL X-UNKNOWN DIGEST-MD5 CRAM-MD5 NTLM PLAIN LOGIN ANONYMOUS\r\n" + ".\r\n"); try { using (var session = PopSessionCreator.CreateSession(prof, null, null)); Assert.Fail("PopAuthenticationException not thrown"); } catch (PopAuthenticationException ex) { Assert.IsNull(ex.InnerException); Assert.IsNull(ex.Result); } server.DequeueRequest(); // CAPA } }
public void TestAuthenticateInsecureLoginDisallowed() { using (var server = new PopPseudoServer()) { server.Start(); var prof = new Profile(new NetworkCredential("user", "pass"), "user;auth=*", server.HostPort); prof.UsingSaslMechanisms = new[] {"LOGIN", "PLAIN", "ANONYMOUS", "CRAM-MD5"}; prof.AllowInsecureLogin = false; // greeting server.EnqueueResponse("+OK <timestamp@localhost>\r\n"); // CAPA response server.EnqueueResponse("+OK\r\n" + "SASL X-UNKNOWN DIGEST-MD5 CRAM-MD5 NTLM PLAIN LOGIN ANONYMOUS\r\n" + ".\r\n"); // AUTH CRAM-MD5 response server.EnqueueResponse("+ PDQwMDEzNDQxMTIxNDM1OTQuMTI3MjQ5OTU1MEBsb2NhbGhvc3Q+\r\n"); server.EnqueueResponse("+OK\r\n"); using (var session = PopSessionCreator.CreateSession(prof, null, null)) { Assert.AreEqual(PopSessionState.Transaction, session.State); Assert.AreEqual(prof.Authority, session.Authority); } server.DequeueRequest(); // CAPA StringAssert.Contains("AUTH CRAM-MD5", server.DequeueRequest()); } }
public void TestAuthenticateNoUserAndNoAuthMechanismSpecifiedAppropriateMechanismNotFound() { using (var server = new PopPseudoServer()) { server.Start(); var prof = new Profile(null, null, server.HostPort); // greeting server.EnqueueResponse("+OK\r\n"); // CAPA response server.EnqueueResponse("+OK\r\n" + ".\r\n"); try { using (var session = PopSessionCreator.CreateSession(prof, null, null)); Assert.Fail("PopAuthenticationException not thrown"); } catch (PopAuthenticationException ex) { Assert.IsNull(ex.InnerException); Assert.IsNull(ex.Result); } server.DequeueRequest(); // CAPA } }
public void TestCreateSessionStlsUseTlsIfAvailable() { using (var server = new PopPseudoServer()) { server.Start(); var prof = new Profile(new NetworkCredential("user", "pass"), "user", server.HostPort); prof.AllowInsecureLogin = false; prof.UseTlsIfAvailable = true; // greeting server.EnqueueResponse("+OK\r\n"); // CAPA response server.EnqueueResponse("+OK\r\n" + "STLS\r\n" + ".\r\n"); // STLS response server.EnqueueResponse("+OK\r\n"); // CAPA response server.EnqueueResponse("+OK\r\n" + ".\r\n"); // USER response server.EnqueueResponse("+OK\r\n"); // PASS response server.EnqueueResponse("+OK\r\n"); var streamUpgraded = false; using (var session = PopSessionCreator.CreateSession(prof, null, delegate(ConnectionBase connection, Stream baseStream) { streamUpgraded = true; return baseStream; // TODO: return SSL stream })) { Assert.IsTrue(streamUpgraded, "stream upgraded"); Assert.AreEqual(PopSessionState.Transaction, session.State); Assert.AreEqual(prof.Authority, session.Authority); Assert.IsTrue(session.IsSecureConnection); } server.DequeueRequest(); // CAPA StringAssert.StartsWith("STLS", server.DequeueRequest()); } }
public void TestAuthenticateNoUserAndNoAuthMechanismSpecifiedAuthenticationFailure() { using (var server = new PopPseudoServer()) { server.Start(); var prof = new Profile(null, null, server.HostPort); // greeting server.EnqueueResponse("+OK\r\n"); // CAPA response server.EnqueueResponse("+OK\r\n" + ".\r\n"); // USER response server.EnqueueResponse("+OK\r\n"); // PASS response server.EnqueueResponse("-ERR\r\n"); try { Assert.IsNull(PopSessionCreator.CreateSession(prof, null, null)); Assert.Fail("PopAuthenticationException not thrown"); } catch (PopAuthenticationException ex) { Assert.IsNull(ex.InnerException); Assert.IsNotNull(ex.Result); Assert.AreEqual(Protocol.Client.PopCommandResultCode.Error, ex.Result.Code); } server.DequeueRequest(); // CAPA StringAssert.StartsWith("USER anonymous", server.DequeueRequest()); // anonymous credential StringAssert.StartsWith("PASS anonymous@", server.DequeueRequest()); } }
private void AuthenticateSpecificMechanism(NetworkCredential credential) { using (var server = new PopPseudoServer()) { server.Start(); var prof = new Profile(new NetworkCredential("pop-user", (string)null), "pop", server.HostPort); prof.UsingSaslMechanisms = new[] {"LOGIN", "PLAIN", "ANONYMOUS", "DIGEST-MD5", "CRAM-MD5"}; prof.AllowInsecureLogin = true; // greeting server.EnqueueResponse("+OK ready\r\n"); // CAPABILITY response server.EnqueueResponse("+OK\r\n" + ".\r\n"); // AUTH X-PSEUDO-MECHANISM response server.EnqueueResponse("+\r\n"); server.EnqueueResponse("+OK\r\n"); using (var authMechanism = new SaslPseudoMechanism(false, 1)) { authMechanism.Credential = credential; using (var session = PopSessionCreator.CreateSession(prof, authMechanism, null)) { Assert.AreEqual(PopSessionState.Transaction, session.State); if (credential == null) Assert.AreEqual(new Uri(string.Format("pop://AUTH=X-PSEUDO-MECHANISM@{0}/", server.HostPort)), session.Authority); else Assert.AreEqual(new Uri(string.Format("pop://sasl-user;AUTH=X-PSEUDO-MECHANISM@{0}/", server.HostPort)), session.Authority); } Assert.AreSame(credential, authMechanism.Credential, "credential must be kept"); Assert.AreEqual(Smdn.Security.Authentication.Sasl.SaslExchangeStatus.Succeeded, authMechanism.ExchangeStatus); } server.DequeueRequest(); // CAPABILITY StringAssert.Contains("AUTH X-PSEUDO-MECHANISM", server.DequeueRequest()); } }
public void TestAuthenticateUserAndAuthMechanismSpecified() { using (var server = new PopPseudoServer()) { server.Start(); var prof = new Profile(new NetworkCredential("user", "pass"), "user;auth=plain", server.HostPort); // greeting server.EnqueueResponse("+OK\r\n"); // CAPA response server.EnqueueResponse("+OK\r\n" + "SASL PLAIN\r\n" + ".\r\n"); // AUTH response server.EnqueueResponse("+OK\r\n"); using (var session = PopSessionCreator.CreateSession(prof, null, null)) { Assert.AreEqual(PopSessionState.Transaction, session.State); Assert.AreEqual(prof.Authority, session.Authority); } server.DequeueRequest(); // CAPA StringAssert.StartsWith("AUTH PLAIN", server.DequeueRequest()); } }
private void AuthenticateWithAppropriateMechanismAppropriateMechanismNotFound(string userinfo) { using (var server = new PopPseudoServer()) { server.Start(); var prof = new Profile(new NetworkCredential("user", "pass"), userinfo, server.HostPort); prof.AllowInsecureLogin = false; // LOGINDISABLE equiv // greeting server.EnqueueResponse("+OK\r\n"); // CAPA response server.EnqueueResponse("+OK\r\n" + ".\r\n"); try { using (var session = PopSessionCreator.CreateSession(prof, null, null)); Assert.Fail("PopAuthenticationException not thrown"); } catch (PopAuthenticationException ex) { Assert.IsNull(ex.InnerException); Assert.IsNull(ex.Result); } server.DequeueRequest(); // CAPA } }
public void TestDisposeAsyncConnectRunning() { using (var server = new PopPseudoServer()) { server.Start(); using (var client = new PopClient(server.Host, server.Port, "user")) { client.Profile.AllowInsecureLogin = true; client.Profile.Timeout = 5000; Assert.IsFalse(client.IsConnected); var asyncResult = (PopClient.ConnectAsyncResult)client.BeginConnect("pass"); // greeting server.EnqueueResponse("+OK\r\n"); // CAPA server.EnqueueResponse("+OK\r\n" + ".\r\n"); // USER/PASS server.EnqueueResponse("+OK\r\n"); server.EnqueueResponse("+OK\r\n"); Assert.IsFalse(asyncResult.EndConnectCalled); ((IDisposable)client).Dispose(); StringAssert.StartsWith("CAPA", server.DequeueRequest()); StringAssert.StartsWith("USER user", server.DequeueRequest()); StringAssert.StartsWith("PASS pass", server.DequeueRequest()); Assert.IsFalse(client.IsConnected); client.EndConnect(asyncResult); Assert.IsTrue(asyncResult.EndConnectCalled); Assert.IsFalse(client.IsConnected); } } }