public void TestCreateSessionInternalError()
        {
            using (var server = new PopPseudoServer()) {
            server.Start();

            var credential = new NetworkCredential("user", "pass");
            var request = WebRequest.Create(string.Format("pop://{0};auth=digest-md5@{1}/", credential.UserName, server.HostPort)) as PopWebRequest;

            request.Credentials = credential;
            request.KeepAlive = false;
            request.UsingSaslMechanisms = new[] {"DIGEST-MD5"};
            request.Timeout = 1000;

            // greeting
            server.EnqueueResponse("+OK\r\n");
            // CAPA response
            server.EnqueueResponse("+OK\r\n" +
                               "SASL DIGEST-MD5 CRAM-MD5 NTLM PLAIN LOGIN\r\n" +
                               ".\r\n");
            // AUTH DIGEST-MD5 response
            server.EnqueueResponse("+ xxx-invalid-response-xxx\r\n");

            try {
              using (var response = request.GetResponse()) {
              }
            }
            catch (WebException ex) {
              WebExceptionAssertion(WebExceptionStatus.UnknownError, ex);
              Assert.IsInstanceOfType(typeof(PopException), ex.InnerException);
            }
              }
        }
        public void TestCreateSessionAppropriateMechanismNotFound()
        {
            using (var server = new PopPseudoServer()) {
            server.Start();

            var credential = new NetworkCredential("user", "pass");
            var requestUri = new Uri(string.Format("pop://user@{0}/", server.HostPort));
            var request = WebRequest.Create(requestUri) as PopWebRequest;

            request.Credentials = credential;
            request.UseTlsIfAvailable = true;
            request.KeepAlive = false;
            request.Timeout = 1000;
            request.AllowInsecureLogin = false; // LOGINDISABLE equiv

            // greeting
            server.EnqueueResponse("+OK\r\n");
            // CAPA response
            server.EnqueueResponse("+OK\r\n" +
                               ".\r\n");

            try {
              using (var response = request.GetResponse()) {
              }
            }
            catch (WebException ex) {
              WebExceptionAssertion(WebExceptionStatus.RequestCanceled, ex);
            }
              }
        }
        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 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 Setup()
        {
            server = new PopPseudoServer();
              server.Start();

              host = server.ServerEndPoint.Address.ToString();
              port = server.ServerEndPoint.Port;

              credential = new NetworkCredential(username, password, host);
              authority = new Uri(string.Format("{0}://{1}@{2}:{3}",
                                  PopUri.UriSchemePop,
                                  username,
                                  host,
                                  port));
        }
        public void TestCreateSessionConnectFailure()
        {
            using (var server = new PopPseudoServer()) {
            server.Start();

            var credential = new NetworkCredential("user", "pass");
            var request = WebRequest.Create(string.Format("pop://{0}@{1}/", credential.UserName, server.HostPort)) as PopWebRequest;

            request.Timeout = 500;

            server.Stop();

            try {
              using (var response = request.GetResponse()) {
              }
            }
            catch (WebException ex) {
              WebExceptionAssertion(WebExceptionStatus.ConnectFailure, ex);
            }
              }
        }
Beispiel #8
0
        public void TestBeginConnectException()
        {
            using (var server = new PopPseudoServer()) {
            server.Start();

            using (var client = new PopClient(server.Host, server.Port, "user")) {
              client.Profile.Timeout = 250;

              Assert.IsFalse(client.IsConnected);

              var asyncResult = client.BeginConnect("pass");

              try {
            client.EndConnect(asyncResult);
            Assert.Fail("PopConnectionException not thrown");
              }
              catch (PopConnectionException) {
            Assert.IsNull((client.Profile as IPopSessionProfile).Credentials);
              }

              Assert.IsFalse(client.IsConnected);
            }
              }
        }
        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());
              }
        }
Beispiel #10
0
        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());
              }
        }
Beispiel #11
0
        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());
              }
        }
Beispiel #12
0
        public void TestCreateSessionConnectSecurePortException()
        {
            using (var server = new PopPseudoServer()) {
            server.Start();

            var prof = new Profile(new NetworkCredential("user", "pass"), "pops", "user", server.HostPort);

            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);
            }
              }
        }
Beispiel #13
0
        public void TestCreateSessionCreateSslStreamCallbackNull()
        {
            using (var server = new PopPseudoServer()) {
            server.Start();

            var prof = new Profile(new NetworkCredential("user", "pass"), "POPS", "user", server.HostPort);

            prof.Timeout = 500;

            server.Stop();

            PopSessionCreator.CreateSession(prof, null, null);
              }
        }
Beispiel #14
0
        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());
              }
        }
Beispiel #15
0
        public void TestCreateSessionConnectRefusedByTimeout()
        {
            using (var server = new PopPseudoServer()) {
            server.Start();

            var prof = new Profile(new NetworkCredential("user", "pass"), "user", server.HostPort);

            prof.Timeout = 500;

            server.Stop();

            try {
              PopSessionCreator.CreateSession(prof, null, null);

              Assert.Fail("PopConnectionException not thrown");
            }
            catch (PopConnectionException ex) {
              var timeoutException = ex.InnerException as TimeoutException;

              Assert.IsNotNull(timeoutException);
            }
              }
        }
Beispiel #16
0
        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;
            }
              }
        }
Beispiel #17
0
        public void TestCreateSessionTimeout()
        {
            using (var server = new PopPseudoServer()) {
            server.Start();

            var credential = new NetworkCredential("user", "pass");
            var request = WebRequest.Create(string.Format("pop://{0}@{1}/", credential.UserName, server.HostPort)) as PopWebRequest;

            request.KeepAlive = false;
            request.Method = "NOOP";
            request.Timeout = 500;

            // greeting
            server.EnqueueResponse("+OK\r\n");
            // CAPABILITY response (not respond termination response)
            server.EnqueueResponse("+OK\r\n");

            try {
              using (var response = request.GetResponse()) {
              }
            }
            catch (WebException ex) {
              WebExceptionAssertion(WebExceptionStatus.Timeout, ex);
            }
              }
        }
Beispiel #18
0
        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());
            }
              }
        }
Beispiel #19
0
        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());
              }
        }
Beispiel #20
0
        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());
              }
        }
Beispiel #21
0
        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());
              }
        }
Beispiel #22
0
        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
              }
        }
Beispiel #23
0
        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());
              }
        }
Beispiel #24
0
        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());
              }
        }
Beispiel #25
0
        private void AuthenticateExceptionWhileAuthentication(string userinfo)
        {
            using (var server = new PopPseudoServer()) {
            server.Start();

            var prof = new Profile(new NetworkCredential("user", "pass"), userinfo, server.HostPort);

            prof.UsingSaslMechanisms = new[] {"DIGEST-MD5", "PLAIN"};

            // greeting
            server.EnqueueResponse("+OK\r\n");
            // CAPA response
            server.EnqueueResponse("+OK\r\n" +
                               "SASL DIGEST-MD5 CRAM-MD5 NTLM PLAIN LOGIN\r\n" +
                               ".\r\n");
            // AUTH DIGEST-MD5 response
            server.EnqueueResponse("+ xxx-invalid-response-xxx\r\n");

            try {
              using (var session = PopSessionCreator.CreateSession(prof, null, null));
              Assert.Fail("PopException not thrown");
            }
            catch (PopException ex) {
              Assert.IsNotNull(ex.InnerException);
            }
              }
        }
Beispiel #26
0
        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
              }
        }
Beispiel #27
0
        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());
              }
        }
Beispiel #28
0
        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());
              }
        }
Beispiel #29
0
        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
              }
        }
Beispiel #30
0
        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());
              }
        }