Beispiel #1
0
        public void TestGetResponseStreamResponseHasNoStream()
        {
            using (var server = new PopPseudoServer()) {
            server.Start();

            var request = WebRequest.Create(string.Format("pop://{0}/", server.HostPort)) as PopWebRequest;

            request.Timeout = 1000;
            request.Method = PopWebRequestMethods.NoOp;
            request.KeepAlive = false;

            // greeting
            server.EnqueueResponse("+OK\r\n");
            // CAPA
            server.EnqueueResponse("+OK\r\n" +
                               ".\r\n");
            // USER
            server.EnqueueResponse("+OK\r\n");
            // PASS
            server.EnqueueResponse("+OK\r\n");
            // NOOP
            server.EnqueueResponse("+OK\r\n");
            // QUIT
            server.EnqueueResponse("+OK\r\n");

            using (var response = request.GetResponse()) {
              try {
            response.GetResponseStream();
            Assert.Fail("InvalidOperationException not thrown");
              }
              catch (InvalidOperationException) {
              }
            }
              }
        }
Beispiel #2
0
        public void TestBeginGetResponseCallback()
        {
            using (var server = new PopPseudoServer()) {
            server.Start();

            var request = WebRequest.Create(string.Format("pop://{0}/", server.HostPort)) as PopWebRequest;

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

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

            var asyncResult = request.BeginGetResponse(BeginGetResponseCallbackProc, request);

            Assert.IsNotNull(asyncResult);
            Assert.AreEqual(asyncResult.AsyncState, request);

            Thread.Sleep(1000);

            StringAssert.StartsWith("CAPA", server.DequeueRequest());
            StringAssert.StartsWith("USER", server.DequeueRequest());
            StringAssert.StartsWith("PASS", server.DequeueRequest());

            // NOOP
            server.EnqueueResponse("+OK\r\n");
            // QUIT
            server.EnqueueResponse("+OK\r\n");

            if (!asyncResult.AsyncWaitHandle.WaitOne(5000))
              Assert.Fail("not completed");

            StringAssert.StartsWith("NOOP", server.DequeueRequest());
            StringAssert.StartsWith("QUIT", server.DequeueRequest());
              }
        }
Beispiel #3
0
        public void TestBeginGetResponse()
        {
            using (var server = new PopPseudoServer()) {
            server.Start();

            var request = WebRequest.Create(string.Format("pop://{0}/", server.HostPort)) as PopWebRequest;

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

            // greeting
            server.EnqueueResponse("+OK\r\n");
            // CAPA
            server.EnqueueResponse("+OK\r\n" +
                               ".\r\n");
            // USER
            server.EnqueueResponse("+OK\r\n");
            // PASS
            server.EnqueueResponse("+OK\r\n");
            // NOOP
            server.EnqueueResponse("+OK\r\n");
            // QUIT
            server.EnqueueResponse("+OK\r\n");

            var asyncState = "async state";
            var asyncResult = request.BeginGetResponse(null, asyncState);

            Assert.IsNotNull(asyncResult);
            Assert.AreEqual(asyncResult.AsyncState, asyncState);

            using (var response = request.EndGetResponse(asyncResult)) {
            }

            StringAssert.StartsWith("CAPA", server.DequeueRequest());
            StringAssert.StartsWith("USER", server.DequeueRequest());
            StringAssert.StartsWith("PASS", server.DequeueRequest());
            StringAssert.StartsWith("NOOP", server.DequeueRequest());
            StringAssert.StartsWith("QUIT", server.DequeueRequest());
              }
        }
Beispiel #4
0
        private void KeepAliveAndDisconnected(bool disconnectFromServer)
        {
            using (var server = new PopPseudoServer()) {
            for (var req = 0; req < 2; req++) {
              server.Start();

              /*
               * request/response
               */
              var request = WebRequest.Create(string.Format("pop://{0}/", server.HostPort)) as PopWebRequest;

              request.Timeout = 1000;
              request.Method = "NOOP";
              request.KeepAlive = true;

              // greeting
              server.EnqueueResponse("+OK\r\n");
              // CAPA
              server.EnqueueResponse("+OK\r\n" +
                                 ".\r\n");
              // USER
              server.EnqueueResponse("+OK\r\n");
              // PASS
              server.EnqueueResponse("+OK\r\n");
              // NOOP
              server.EnqueueResponse("+OK\r\n");

              using (var response = request.GetResponse()) {
              }

              server.DequeueRequest(); // CAPA
              server.DequeueRequest(); // USER
              server.DequeueRequest(); // PASS
              StringAssert.StartsWith("NOOP", server.DequeueRequest(), "request #{0}", req);

              if (disconnectFromServer) {
            server.Stop();
              }
              else {
            // QUIT
            server.EnqueueResponse("+OK\r\n");
            PopSessionManager.DisconnectFrom(request.RequestUri);

            server.DequeueRequest(); // QUIT

            server.Stop();
              }
            }
              }
        }
Beispiel #5
0
        private void GetResponseTimeout(int timeout, int readWriteTimeout)
        {
            using (var server = new PopPseudoServer()) {
            server.Start();

            var request = WebRequest.Create(string.Format("pop://{0}/", server.HostPort)) as PopWebRequest;

            request.Timeout = timeout;
            request.ReadWriteTimeout = readWriteTimeout;
            request.Method = "NOOP";

            // greeting
            server.EnqueueResponse("+OK\r\n");
            // CAPA
            server.EnqueueResponse("+OK\r\n" +
                               ".\r\n");
            // USER
            server.EnqueueResponse("+OK\r\n");
            // PASS
            server.EnqueueResponse("+OK\r\n");
            // not respond to NOOP

            try {
              using (var response = request.GetResponse()) {
              }
            }
            catch (WebException ex) {
              Assert.AreEqual(WebExceptionStatus.Timeout, ex.Status);
            }

            server.DequeueRequest(); // CAPA
            server.DequeueRequest(); // USER
            server.DequeueRequest(); // PASS
            StringAssert.StartsWith("NOOP", server.DequeueRequest());
              }
        }
Beispiel #6
0
        private void AuthenticateWithSpecifiedSaslMechanisms(string[] mechanisms)
        {
            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.UsingSaslMechanisms = mechanisms;
            request.Credentials = credential;
            request.Timeout = 1000;
            request.Method = "NOOP";
            request.KeepAlive = true;
            request.AllowInsecureLogin = true;

            // greeting
            server.EnqueueResponse("+OK\r\n");
            // CAPA
            server.EnqueueResponse("+OK\r\n" +
                               "SASL DIGEST-MD5 NTLM CRAM-MD5 PLAIN LOGIN\r\n" +
                               ".\r\n");
            // USER
            server.EnqueueResponse("+OK\r\n");
            // PASS
            server.EnqueueResponse("+OK\r\n");
            // NOOP
            server.EnqueueResponse("+OK\r\n");
            // QUIT
            server.EnqueueResponse("+OK\r\n");

            using (var response = request.GetResponse()) {
            }

            server.DequeueRequest(); // CAPA
            server.DequeueRequest(); // USER
            server.DequeueRequest(); // PASS
            StringAssert.StartsWith("NOOP", server.DequeueRequest());
              }
        }
Beispiel #7
0
        public void TestUsingSaslMechanismsNonEmpty()
        {
            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.UsingSaslMechanisms = new string[] {"X-UNKWNON-MECHANISM", "PLAIN", "login"};
            request.Credentials = credential;
            request.Timeout = 1000;
            request.Method = "NOOP";
            request.KeepAlive = true;
            request.AllowInsecureLogin = true;

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

            try {
              using (var response = request.GetResponse()) {
              }
            }
            catch (WebException ex) {
              Assert.AreEqual(WebExceptionStatus.ProtocolError, ex.Status);
            }

            server.DequeueRequest(); // CAPA
            StringAssert.StartsWith("AUTH PLAIN", server.DequeueRequest());
            StringAssert.StartsWith("AUTH LOGIN", server.DequeueRequest());
              }
        }
Beispiel #8
0
        public void TestGetResponseOk()
        {
            using (var server = new PopPseudoServer()) {
            server.Start();

            var request = WebRequest.Create(string.Format("pop://{0}/", server.HostPort)) as PopWebRequest;

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

            // greeting
            server.EnqueueResponse("+OK\r\n");
            // CAPA
            server.EnqueueResponse("+OK\r\n" +
                               ".\r\n");
            // USER
            server.EnqueueResponse("+OK\r\n");
            // PASS
            server.EnqueueResponse("+OK\r\n");
            // NOOP
            server.EnqueueResponse("+OK\r\n");
            // QUIT
            server.EnqueueResponse("+OK\r\n");

            using (var response = request.GetResponse()) {
              Assert.IsNotNull(response);
              Assert.IsInstanceOfType(typeof(PopWebResponse), response);

              var r = response as PopWebResponse;

              Assert.IsNotNull(r.Result);
            }

            server.DequeueRequest(); // CAPA
            server.DequeueRequest(); // USER
            server.DequeueRequest(); // PASS
            StringAssert.StartsWith("NOOP", server.DequeueRequest());
              }
        }
Beispiel #9
0
        private PopPseudoServer InitializeServer()
        {
            var server = new PopPseudoServer();

              server.Start();

              // greeting
              server.EnqueueResponse("+OK\r\n");
              // CAPA
              server.EnqueueResponse("+OK\r\n" +
                             ".\r\n");
              // USER
              server.EnqueueResponse("+OK\r\n");
              // PASS
              server.EnqueueResponse("+OK\r\n");
              // RETR
              server.EnqueueResponse("+OK\r\n" +
                             message +
                             ".\r\n");
              // QUIT
              server.EnqueueResponse("+OK\r\n");

              return server;
        }
Beispiel #10
0
        public void TestGetResponseErrWithUnxpectedResponseCode()
        {
            using (var server = new PopPseudoServer()) {
            server.Start();

            var request = WebRequest.Create(string.Format("pop://{0}/;MSG=1", server.HostPort)) as PopWebRequest;

            request.Timeout = 1000;
            request.Method = "DELE";
            request.ExpectedErrorResponseCodes = new[] {PopResponseCode.SysTemp};
            request.KeepAlive = false;

            // greeting
            server.EnqueueResponse("+OK\r\n");
            // CAPA
            server.EnqueueResponse("+OK\r\n" +
                               ".\r\n");
            // USER
            server.EnqueueResponse("+OK\r\n");
            // PASS
            server.EnqueueResponse("+OK\r\n");
            // DELE
            server.EnqueueResponse("-ERR [SYS/PERM]\r\n");
            // QUIT
            server.EnqueueResponse("+OK\r\n");

            try {
              using (var response = request.GetResponse()) {
            Assert.Fail("WebException not thrown");
              }
            }
            catch (WebException ex) {
              Assert.AreEqual(WebExceptionStatus.ProtocolError, ex.Status);

              var response = ex.Response as PopWebResponse;

              Assert.IsNotNull(response);
              Assert.AreEqual(response.ResponseCode, PopResponseCode.SysPerm);
            }

            server.DequeueRequest(); // CAPA
            server.DequeueRequest(); // USER
            server.DequeueRequest(); // PASS
            StringAssert.StartsWith("DELE", server.DequeueRequest());
              }
        }
Beispiel #11
0
        public void TestGetResponseErrWithExpectedResponseCode()
        {
            using (var server = new PopPseudoServer()) {
            server.Start();

            var request = WebRequest.Create(string.Format("pop://{0}/;MSG=1", server.HostPort)) as PopWebRequest;

            request.Timeout = 1000;
            request.Method = "DELE";
            request.ExpectedErrorResponseCodes = new[] {PopResponseCode.SysTemp};
            request.KeepAlive = false;

            // greeting
            server.EnqueueResponse("+OK\r\n");
            // CAPA
            server.EnqueueResponse("+OK\r\n" +
                               ".\r\n");
            // USER
            server.EnqueueResponse("+OK\r\n");
            // PASS
            server.EnqueueResponse("+OK\r\n");
            // DELE
            server.EnqueueResponse("-ERR [SYS/TEMP]\r\n");
            // QUIT
            server.EnqueueResponse("+OK\r\n");

            using (var response = request.GetResponse() as PopWebResponse) {
              Assert.IsTrue(response.Result.Failed);
            }

            server.DequeueRequest(); // CAPA
            server.DequeueRequest(); // USER
            server.DequeueRequest(); // PASS
            StringAssert.StartsWith("DELE", server.DequeueRequest());
              }
        }
Beispiel #12
0
        public void TestGetResponseErr()
        {
            using (var server = new PopPseudoServer()) {
            server.Start();

            var request = WebRequest.Create(string.Format("pop://{0}/;MSG=1", server.HostPort)) as PopWebRequest;

            request.Timeout = 1000;
            request.Method = "DELE";
            request.KeepAlive = false;

            // greeting
            server.EnqueueResponse("+OK\r\n");
            // CAPA
            server.EnqueueResponse("+OK\r\n" +
                               ".\r\n");
            // USER
            server.EnqueueResponse("+OK\r\n");
            // PASS
            server.EnqueueResponse("+OK\r\n");
            // DELE
            server.EnqueueResponse("-ERR\r\n");
            // QUIT
            server.EnqueueResponse("+OK\r\n");

            try {
              using (var response = request.GetResponse()) {
              }
            }
            catch (WebException ex) {
              Assert.AreEqual(WebExceptionStatus.ProtocolError, ex.Status);

              Assert.IsNotNull(ex.Response);
              Assert.IsInstanceOfType(typeof(PopWebResponse),ex.Response);

              var r = ex.Response as PopWebResponse;

              Assert.IsNotNull(r.Result);
            }

            server.DequeueRequest(); // CAPA
            server.DequeueRequest(); // USER
            server.DequeueRequest(); // PASS
            StringAssert.StartsWith("DELE", server.DequeueRequest());
              }
        }
Beispiel #13
0
        public void TestEndGetResponseInvalidAsyncResult()
        {
            using (var server = new PopPseudoServer()) {
            server.Start();

            var request1 = WebRequest.Create(string.Format("pop://{0}/", server.HostPort)) as PopWebRequest;
            var request2 = WebRequest.Create(string.Format("pop://{0}/", server.HostPort)) as PopWebRequest;

            var asyncResult1 = request1.BeginGetResponse(null, null);
            var asyncResult2 = request2.BeginGetResponse(null, null);

            request1.EndGetResponse(asyncResult2);
              }
        }
Beispiel #14
0
        private void TestExceptionWhileCloseSession(string test)
        {
            using (var server = new PopPseudoServer()) {
            server.Start();

            var request = WebRequest.Create(string.Format("pop://{0}/", server.HostPort)) as PopWebRequest;

            request.Timeout = 3000;
            request.ReadWriteTimeout = 3000;

            request.Method = PopWebRequestMethods.NoOp;
            request.KeepAlive = false;

            // greeting
            server.EnqueueResponse("+OK\r\n");
            // CAPA
            server.EnqueueResponse("+OK\r\n" +
                               ".\r\n");
            // USER
            server.EnqueueResponse("+OK\r\n");
            // PASS
            server.EnqueueResponse("+OK\r\n");
            // NOOP
            server.EnqueueResponse("+OK\r\n");

            var asyncResult = request.BeginGetResponse(null, null);

            Thread.Sleep(500);

            server.DequeueRequest(); // CAPA
            server.DequeueRequest(); // USER
            server.DequeueRequest(); // PASS
            StringAssert.StartsWith("NOOP", server.DequeueRequest());

            if (test == "not responding")
              // not respond to QUIT
              ;
            else if (test == "disconnected")
              server.Stop();

            using (var response = request.EndGetResponse(asyncResult) as PopWebResponse) {
              Assert.IsTrue(response.Result.Succeeded);
            }
              }
        }
Beispiel #15
0
        public void TestGetResponseKeepAliveTrue()
        {
            using (var server = new PopPseudoServer()) {
            server.Start();

            for (var req = 0; req < 3; req++) {
              /*
               * request/response
               */
              var request = WebRequest.Create(string.Format("pop://{0}/", server.HostPort)) as PopWebRequest;

              request.Timeout = 1000;
              request.Method = "NOOP";
              request.KeepAlive = true;

              if (req == 0) {
            // greeting
            server.EnqueueResponse("+OK\r\n");
            // CAPA
            server.EnqueueResponse("+OK\r\n" +
                                   ".\r\n");
            // USER
            server.EnqueueResponse("+OK\r\n");
            // PASS
            server.EnqueueResponse("+OK\r\n");
              }

              // NOOP
              server.EnqueueResponse("+OK\r\n");

              using (var response = request.GetResponse()) {
              }

              if (req == 0) {
            server.DequeueRequest(); // CAPA
            server.DequeueRequest(); // USER
            server.DequeueRequest(); // PASS
              }

              StringAssert.StartsWith("NOOP", server.DequeueRequest(), "request #{0}", req);
            }
              }
        }
Beispiel #16
0
        private void UsingAsyncStartedRequest(Action<PopWebRequest> action)
        {
            using (var server = new PopPseudoServer()) {
            server.Start();

            var request = WebRequest.Create(string.Format("pop://{0}/", server.HostPort)) as PopWebRequest;

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

            var asyncResult = request.BeginGetResponse(null, null);

            Assert.IsNotNull(asyncResult);

            action(request);

            using (var response = request.EndGetResponse(asyncResult)) {
            }
              }
        }
        private void Request(PopPseudoServer server, string method, string methodResponse, Action<PopWebRequest, PopWebResponse> responseAction)
        {
            server.Start();

              var request = WebRequest.Create(string.Format("pop://{0}/", server.HostPort)) as PopWebRequest;

              request.KeepAlive = false;
              request.Timeout = 1000;
              request.Method = method;

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

              server.EnqueueResponse(methodResponse);

              // QUIT
              server.EnqueueResponse("+OK\r\n");

              try {
            using (var response = request.GetResponse() as PopWebResponse) {
              responseAction(request, response);
            }
              }
              catch (WebException ex) {
            if (ex.Status == WebExceptionStatus.ProtocolError)
              responseAction(request, ex.Response as PopWebResponse);
            else
              throw ex;
              }

              server.DequeueRequest(); // CAPA
              server.DequeueRequest(); // USER
              server.DequeueRequest(); // PASS
        }