Exemplo n.º 1
0
        public void TestBeginGetResponse()
        {
            using (var server = new ImapPseudoServer()) {
            server.Start();

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

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

            // greeting
            server.EnqueueResponse("* OK [CAPABILITY IMAP4rev1] ready\r\n");
            // LOGIN
            server.EnqueueResponse("* CAPABILITY IMAP4rev1\r\n" +
                               "0000 OK done\r\n");
            // NOOP
            server.EnqueueResponse("0001 OK done\r\n");
            // LOGOUT
            server.EnqueueResponse("* BYE logging out\r\n" +
                               "0002 OK done\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.Contains("LOGIN", server.DequeueRequest());
            StringAssert.Contains("NOOP", server.DequeueRequest());
            StringAssert.Contains("LOGOUT", server.DequeueRequest());
              }
        }
Exemplo n.º 2
0
        public void TestAuthenticateAnonymousMechanismSpecifiedAuthenticationFailure()
        {
            using (var server = new ImapPseudoServer()) {
            server.Start();

            var prof = new Profile(null, "user%40imap.example.net;auth=anonymous", server.HostPort);

            // greeting
            server.EnqueueResponse("* OK ready\r\n");
            // CAPABILITY response
            server.EnqueueResponse("* CAPABILITY IMAP4rev1 AUTH=ANONYMOUS\r\n" +
                               "0000 OK done\r\n");
            // AUTHENTICATE response
            server.EnqueueResponse("+ \r\n");
            server.EnqueueResponse("0001 NO done\r\n");

            try {
              Assert.IsNull(ImapSessionCreator.CreateSession(prof, null, null));
              Assert.Fail("ImapAuthenticationException not thrown");
            }
            catch (ImapAuthenticationException ex) {
              Assert.IsNull(ex.InnerException);
              Assert.IsNotNull(ex.Result);
              Assert.AreEqual(Protocol.Client.ImapCommandResultCode.No, ex.Result.Code);
            }

            server.DequeueRequest(); // CAPABILITY
            StringAssert.Contains("AUTHENTICATE ANONYMOUS", server.DequeueRequest());
            StringAssert.Contains("*****@*****.**", Base64.GetDecodedString(server.DequeueRequest()));
              }
        }
Exemplo n.º 3
0
        public void TestCreateSessionAppropriateMechanismNotFound()
        {
            using (var server = new ImapPseudoServer()) {
            server.Start();

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

            request.Credentials = credential;
            request.UseTlsIfAvailable = true;
            request.KeepAlive = false;
            request.Timeout = 1000;

            // greeting
            server.EnqueueResponse("* OK ready\r\n");
            // CAPABILITY response
            server.EnqueueResponse("* CAPABILITY IMAP4rev1 LOGINDISABLED\r\n" +
                               "0000 OK done\r\n");

            try {
              using (var response = request.GetResponse()) {
              }
            }
            catch (WebException ex) {
              WebExceptionAssertion(WebExceptionStatus.RequestCanceled, ex);
            }

            server.DequeueRequest(); // CAPABILITY
              }
        }
Exemplo n.º 4
0
        public void TestAuthenticateAnonymousMechanismSpecified()
        {
            using (var server = new ImapPseudoServer()) {
            server.Start();

            var prof = new Profile(null, "user%40imap.example.net;auth=anonymous", server.HostPort);

            // greeting
            server.EnqueueResponse("* OK ready\r\n");
            // CAPABILITY response
            server.EnqueueResponse("* CAPABILITY IMAP4rev1 AUTH=ANONYMOUS\r\n" +
                               "0000 OK done\r\n");
            // AUTHENTICATE response
            server.EnqueueResponse("+ \r\n");
            server.EnqueueResponse("* CAPABILITY IMAP4rev1\r\n" +
                               "0001 OK done\r\n");

            using (var session = ImapSessionCreator.CreateSession(prof, null, null)) {
              Assert.AreEqual(ImapSessionState.Authenticated, session.State);
              Assert.AreEqual(prof.Authority, session.Authority);
            }

            server.DequeueRequest(); // CAPABILITY
            StringAssert.Contains("AUTHENTICATE ANONYMOUS", server.DequeueRequest());
            StringAssert.Contains("*****@*****.**", Base64.GetDecodedString(server.DequeueRequest()));
              }
        }
Exemplo n.º 5
0
        public void TestGetResponseStreamResponseHasNoStream()
        {
            using (var server = new ImapPseudoServer()) {
            server.Start();

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

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

            // greeting
            server.EnqueueResponse("* OK ready\r\n");
            // CAPABILITY
            server.EnqueueResponse("* CAPABILITY IMAP4rev1\r\n" +
                               "0000 OK done\r\n");
            // LOGIN
            server.EnqueueResponse("* CAPABILITY IMAP4rev1\r\n" +
                               "0001 OK done\r\n");
            // NOOP
            server.EnqueueResponse("0002 OK done\r\n");
            // LOGOUT
            server.EnqueueResponse("* BYE logging out\r\n" +
                               "0003 OK done\r\n");

            using (var response = request.GetResponse()) {
              try {
            response.GetResponseStream();
            Assert.Fail("InvalidOperationException not thrown");
              }
              catch (InvalidOperationException) {
              }
            }
              }
        }
Exemplo n.º 6
0
        public void TestBeginGetResponseCallback()
        {
            using (var server = new ImapPseudoServer()) {
            server.Start();

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

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

            // greeting
            server.EnqueueResponse("* OK [CAPABILITY IMAP4rev1] ready\r\n");
            // LOGIN
            server.EnqueueResponse("* CAPABILITY IMAP4rev1\r\n" +
                               "0000 OK done\r\n");

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

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

            Thread.Sleep(1000);

            StringAssert.Contains("LOGIN", server.DequeueRequest());

            // NOOP
            server.EnqueueResponse("0001 OK done\r\n");
            // LOGOUT
            server.EnqueueResponse("* BYE logging out\r\n" +
                               "0002 OK done\r\n");

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

            StringAssert.Contains("NOOP", server.DequeueRequest());
            StringAssert.Contains("LOGOUT", server.DequeueRequest());
              }
        }
Exemplo n.º 7
0
        public void TestBeginGetRequestStreamCallback()
        {
            using (var server = new ImapPseudoServer()) {
            server.Start();

            // greeting
            server.EnqueueResponse("* OK ready\r\n");
            // CAPABILITY
            server.EnqueueResponse("* CAPABILITY IMAP4rev1\r\n" +
                               "0000 OK done\r\n");
            // LOGIN
            server.EnqueueResponse("* CAPABILITY IMAP4rev1\r\n" +
                               "0001 OK done\r\n");
            // APPEND
            server.EnqueueResponse("+ continue\r\n");
            server.EnqueueResponse(string.Empty);

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

            request.Method = "APPEND";
            request.Timeout = 3000;
            request.ReadWriteTimeout = 3000;
            request.AllowInsecureLogin = true;

            var asyncResult = request.BeginGetRequestStream((AsyncCallback)BeginGetRequestStreamCallbackProc, request);

            Thread.Sleep(100); // XXX

            if (!asyncResult.AsyncWaitHandle.WaitOne(5000))
              Assert.Fail("wait time out");

            server.EnqueueResponse("0002 OK done\r\n");

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

            server.DequeueRequest(); // CAPABILITY
            server.DequeueRequest(); // LOGIN

            var appendCommand = server.DequeueAll();

            StringAssert.StartsWith("0002 APPEND \"INBOX\" ", appendCommand);
            StringAssert.EndsWith(" {12}\r\ntest message\r\n", appendCommand);
              }
        }
Exemplo n.º 8
0
        public void TestCloseMailboxFailure()
        {
            using (var server = new ImapPseudoServer()) {
            server.Start();

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

            request1.Credentials = credential;
            request1.Timeout = 1000;
            request1.Method = "CHECK";
            request1.KeepAlive = true;
            request1.AllowInsecureLogin = true;

            // greeting
            server.EnqueueResponse("* OK ready\r\n");
            // CAPABILITY
            server.EnqueueResponse("* CAPABILITY IMAP4rev1\r\n" +
                               "0000 OK done\r\n");
            // LOGIN
            server.EnqueueResponse("* CAPABILITY IMAP4rev1\r\n" +
                               "0001 OK done\r\n");
            // SELECT
            server.EnqueueResponse("0002 OK [READ-WRITE] done\r\n");
            // CHECK
            server.EnqueueResponse("0003 OK done\r\n");

            using (var response = request1.GetResponse()) {
              Assert.AreEqual(response.ResponseUri, request1.RequestUri);
            }

            server.DequeueRequest(); // CAPABILITY
            server.DequeueRequest(); // LOGIN
            StringAssert.Contains("SELECT \"INBOX\"", server.DequeueRequest()); // SELECT
            server.DequeueRequest(); // CHECK

            var request2 = WebRequest.Create(string.Format("imap://{0}@{1}/INBOX/child", credential.UserName, server.HostPort)) as ImapWebRequest;

            request2.Credentials = credential;
            request2.Timeout = 1000;
            request2.Method = "CHECK";
            request2.KeepAlive = false;

            // NOOP
            server.EnqueueResponse("0004 OK done\r\n");
            // CLOSE
            server.EnqueueResponse("0005 OK done\r\n");
            // SELECT
            server.EnqueueResponse("0006 OK [READ-WRITE] done\r\n");
            // CHECK
            server.EnqueueResponse("0007 OK done\r\n");
            // CLOSE
            server.EnqueueResponse("0008 BAD invalid argument\r\n");

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

            server.DequeueRequest(); // NOOP

            StringAssert.Contains("CLOSE", server.DequeueRequest());
              }
        }
Exemplo n.º 9
0
        private void TestExceptionWhileCloseSession(string test)
        {
            using (var server = new ImapPseudoServer()) {
            server.Start();

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

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

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

            // greeting
            server.EnqueueResponse("* OK ready\r\n");
            // CAPABILITY
            server.EnqueueResponse("* CAPABILITY IMAP4rev1\r\n" +
                               "0000 OK done\r\n");
            // LOGIN
            server.EnqueueResponse("* CAPABILITY IMAP4rev1\r\n" +
                               "0001 OK done\r\n");
            // NOOP
            server.EnqueueResponse("0002 OK done\r\n");

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

            Thread.Sleep(500);

            server.DequeueRequest(); // CAPABILITY
            server.DequeueRequest(); // LOGIN
            StringAssert.Contains("NOOP", server.DequeueRequest());

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

            using (var response = request.EndGetResponse(asyncResult) as ImapWebResponse) {
              Assert.IsTrue(response.Result.Succeeded);
            }
              }
        }
Exemplo n.º 10
0
        private void SelectRequestMailboxReselect(bool readOnlyFirst)
        {
            using (var server = new ImapPseudoServer()) {
            server.Start();

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

            firstRequest.Credentials = credential;
            firstRequest.Timeout = 1000;
            firstRequest.Method = "CHECK";
            firstRequest.KeepAlive = true;
            firstRequest.ReadOnly = readOnlyFirst;
            firstRequest.AllowInsecureLogin = true;

            // greeting
            server.EnqueueResponse("* OK ready\r\n");
            // CAPABILITY
            server.EnqueueResponse("* CAPABILITY IMAP4rev1\r\n" +
                               "0000 OK done\r\n");
            // LOGIN
            server.EnqueueResponse("* CAPABILITY IMAP4rev1\r\n" +
                               "0001 OK done\r\n");
            // SELECT/EXAMINE
            if (readOnlyFirst)
              server.EnqueueResponse("0002 OK [READ-ONLY] done\r\n");
            else
              server.EnqueueResponse("0002 OK [READ-WRITE] done\r\n");
            // CHECK
            server.EnqueueResponse("0003 OK done\r\n");

            using (var response = firstRequest.GetResponse()) {
              Assert.AreEqual(response.ResponseUri, firstRequest.RequestUri);
            }

            server.DequeueRequest(); // CAPABILITY
            StringAssert.Contains("LOGIN", server.DequeueRequest());
            if (readOnlyFirst)
              StringAssert.Contains("EXAMINE \"INBOX\"", server.DequeueRequest());
            else
              StringAssert.Contains("SELECT \"INBOX\"", server.DequeueRequest());
            StringAssert.Contains("CHECK", server.DequeueRequest());

            var secondRequest = WebRequest.Create(firstRequest.RequestUri) as ImapWebRequest;

            secondRequest.Credentials = credential;
            secondRequest.Timeout = 1000;
            secondRequest.Method = "CHECK";
            secondRequest.KeepAlive = false;
            secondRequest.ReadOnly = !readOnlyFirst;

            // NOOP
            server.EnqueueResponse("0004 OK done\r\n");
            // CLOSE
            server.EnqueueResponse("0005 OK done\r\n");
            // SELECT/EXAMINE
            if (readOnlyFirst)
              server.EnqueueResponse("0006 OK [READ-WRITE] done\r\n");
            else
              server.EnqueueResponse("0006 OK [READ-ONLY] done\r\n");
            // CHECK
            server.EnqueueResponse("0007 OK done\r\n");
            // CLOSE
            server.EnqueueResponse("0008 OK done\r\n");
            // LOGOUT
            server.EnqueueResponse("* BYE logging out\r\n" +
                               "0009 OK done\r\n");

            using (var response = secondRequest.GetResponse()) {
              Assert.AreEqual(response.ResponseUri, secondRequest.RequestUri);
            }

            server.DequeueRequest(); // NOOP
            StringAssert.Contains("CLOSE", server.DequeueRequest());
            if (readOnlyFirst)
              StringAssert.Contains("SELECT \"INBOX\"", server.DequeueRequest());
            else
              StringAssert.Contains("EXAMINE \"INBOX\"", server.DequeueRequest());
            StringAssert.Contains("CHECK", server.DequeueRequest());
              }
        }
Exemplo n.º 11
0
        public void TestCreateSessionSecureChannelFailureStartTlsBad()
        {
            using (var server = new ImapPseudoServer()) {
            server.Start();

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

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

            // greeting
            server.EnqueueResponse("* OK ready\r\n");
            // CAPABILITY response
            server.EnqueueResponse("* CAPABILITY IMAP4rev1 STARTTLS LOGINDISABLED\r\n" +
                               "0000 OK done\r\n");
            // STARTTLS response
            server.EnqueueResponse("0001 BAD done\r\n");

            try {
              using (var response = request.GetResponse()) {
              }
            }
            catch (WebException ex) {
              WebExceptionAssertion(WebExceptionStatus.SecureChannelFailure, ex);
              Assert.IsNull(ex.InnerException);
            }
              }
        }
Exemplo n.º 12
0
        public void TestGetResponseOk()
        {
            using (var server = new ImapPseudoServer()) {
            server.Start();

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

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

            // greeting
            server.EnqueueResponse("* OK ready\r\n");
            // CAPABILITY
            server.EnqueueResponse("* CAPABILITY IMAP4rev1\r\n" +
                               "0000 OK done\r\n");
            // LOGIN
            server.EnqueueResponse("* CAPABILITY IMAP4rev1\r\n" +
                               "0001 OK done\r\n");
            // NOOP
            server.EnqueueResponse("0002 OK done\r\n");
            // LOGOUT
            server.EnqueueResponse("* BYE logging out\r\n" +
                               "0003 OK done\r\n");

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

              var r = response as ImapWebResponse;

              Assert.IsNotNull(r.Result);
            }

            server.DequeueRequest(); // CAPABILITY
            server.DequeueRequest(); // LOGIN
            StringAssert.Contains("NOOP", server.DequeueRequest());
              }
        }
Exemplo n.º 13
0
        public void TestGetResponseNoWithExpectedResponseCode()
        {
            using (var server = new ImapPseudoServer()) {
            server.Start();

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

            request.Timeout = 1000;
            request.Method = "SELECT";
            request.ExpectedErrorResponseCodes = new[] {ImapResponseCode.PrivacyRequired};
            request.KeepAlive = false;

            // greeting
            server.EnqueueResponse("* OK ready\r\n");
            // CAPABILITY
            server.EnqueueResponse("* CAPABILITY IMAP4rev1\r\n" +
                               "0000 OK done\r\n");
            // LOGIN
            server.EnqueueResponse("* CAPABILITY IMAP4rev1\r\n" +
                               "0001 OK done\r\n");
            // SELECT
            server.EnqueueResponse("0002 NO [PRIVACYREQUIRED] failed\r\n");
            // LOGOUT
            server.EnqueueResponse("* BYE logging out\r\n" +
                               "0003 OK done\r\n");

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

            server.DequeueRequest(); // CAPABILITY
            server.DequeueRequest(); // LOGIN
            StringAssert.EndsWith("SELECT \"INBOX\"\r\n", server.DequeueRequest());
              }
        }
Exemplo n.º 14
0
        public void TestGetResponseKeepAliveTrue()
        {
            using (var server = new ImapPseudoServer()) {
            server.Start();

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

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

              if (req == 0) {
            // greeting
            server.EnqueueResponse("* OK ready\r\n");
            // CAPABILITY
            server.EnqueueResponse("* CAPABILITY IMAP4rev1\r\n" +
                                   "0000 OK done\r\n");
            // LOGIN
            server.EnqueueResponse("* CAPABILITY IMAP4rev1\r\n" +
                                   "0001 OK done\r\n");
            // NOOP
            server.EnqueueResponse("0002 OK done\r\n");
              }
              else if (req == 1) {
            // NOOP
            server.EnqueueResponse("0003 OK done\r\n");
              }
              else if (req == 2) {
            // NOOP
            server.EnqueueResponse("0004 OK done\r\n");
              }

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

              if (req == 0) {
            server.DequeueRequest(); // CAPABILITY
            server.DequeueRequest(); // LOGIN
              }

              StringAssert.Contains("NOOP", server.DequeueRequest(), "request #{0}", req);
            }
              }
        }
Exemplo n.º 15
0
        public void TestUsingSaslMechanismsNonEmpty()
        {
            using (var server = new ImapPseudoServer()) {
            server.Start();

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

            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 ready\r\n");
            // CAPABILITY
            server.EnqueueResponse("* CAPABILITY IMAP4rev1 AUTH=DIGEST-MD5 AUTH=PLAIN AUTH=NTLM AUTH=CRAM-MD5 AUTH=LOGIN\r\n" +
                               "0000 OK done\r\n");
            // AUTHENTICATE PLAIN response
            server.EnqueueResponse("+ \r\n");
            server.EnqueueResponse("0001 NO failure\r\n");
            // AUTHENTICATE LOGIN response
            server.EnqueueResponse("+ \r\n");
            server.EnqueueResponse("0002 NO failure\r\n");
            // LOGIN response
            server.EnqueueResponse("0003 NO failure\r\n");

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

            server.DequeueRequest(); // CAPABILITY
            StringAssert.Contains("AUTHENTICATE PLAIN", server.DequeueRequest());
            server.DequeueRequest(); // AUTHENTICATE PLAIN client response
            StringAssert.Contains("AUTHENTICATE LOGIN", server.DequeueRequest());
              }
        }
Exemplo n.º 16
0
        public void TestCreateSessionInternalError()
        {
            using (var server = new ImapPseudoServer()) {
            server.Start();

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

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

            // greeting
            server.EnqueueResponse("* OK ready\r\n");
            // CAPABILITY response
            server.EnqueueResponse("* CAPABILITY IMAP4rev1 AUTH=DIGEST-MD5 AUTH=CRAM-MD5 AUTH=NTLM AUTH=PLAIN AUTH=LOGIN\r\n" +
                               "0000 OK done\r\n");
            // AUTHENTICATE 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(ImapException), ex.InnerException);
            }
              }
        }
Exemplo n.º 17
0
        public void TestCreateSessionUseTlsIfAvailable()
        {
            using (var server = new ImapPseudoServer()) {
            server.Start();

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

            request.Credentials = credential;
            request.UseTlsIfAvailable = true;
            request.KeepAlive = false;
            request.Method = "NOOP";
            request.Timeout = 1000;

            // greeting
            server.EnqueueResponse("* OK ready\r\n");
            // CAPABILITY response
            server.EnqueueResponse("* CAPABILITY IMAP4rev1 STARTTLS LOGINDISABLED\r\n" +
                               "0000 OK done\r\n");
            // STARTTLS response
            server.EnqueueResponse("0001 OK done\r\n");
            // CAPABILITY response
            server.EnqueueResponse("* CAPABILITY IMAP4rev1\r\n" +
                               "0002 OK done\r\n");
            // LOGIN response
            server.EnqueueResponse("* CAPABILITY IMAP4rev1\r\n" +
                               "0003 OK done\r\n");
            // NOOP response
            server.EnqueueResponse("0004 OK done\r\n");
            // LOGOUT response
            server.EnqueueResponse("* BYE logging out\r\n" +
                               "0005 OK done\r\n");

            var defaultCallback = ImapSessionManager.CreateSslStreamCallback;

            try {
              var streamUpgraded = false;

              ImapSessionManager.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(); // CAPABILITY
              StringAssert.StartsWith("0001 STARTTLS", server.DequeueRequest());
            }
            finally {
              ImapSessionManager.CreateSslStreamCallback = defaultCallback;
            }
              }
        }
Exemplo n.º 18
0
        public void TestCreateSessionTimeout()
        {
            using (var server = new ImapPseudoServer()) {
            server.Start();

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

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

            // greeting
            server.EnqueueResponse("* OK ready\r\n");
            // CAPABILITY response (not respond tagged response)
            server.EnqueueResponse("* CAPABILITY IMAP4rev1\r\n");

            try {
              using (var response = request.GetResponse()) {
              }
            }
            catch (WebException ex) {
              WebExceptionAssertion(WebExceptionStatus.Timeout, ex);
            }
              }
        }
Exemplo n.º 19
0
        public void TestCreateSessionSendReceiveID()
        {
            var defaultClientId = new Dictionary<string, string>(ImapWebRequestDefaults.ClientID);

              using (var server = new ImapPseudoServer()) {
            server.Start();

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

            request.Credentials = credential;
            request.KeepAlive = false;
            request.Method = "NOOP";
            request.Timeout = 1000;
            request.AllowInsecureLogin = true;

            // greeting
            server.EnqueueResponse("* OK [CAPABILITY IMAP4rev1 ID] ready\r\n");
            // LOGIN
            server.EnqueueResponse("0000 OK [CAPABILITY IMAP4rev1 ID] done\r\n");
            // ID
            server.EnqueueResponse("* ID (\"name\" \"ImapPseudoServer\")\r\n" +
                               "0001 OK done\r\n");
            // NOOP response
            server.EnqueueResponse("0002 OK done\r\n");
            // LOGOUT response
            server.EnqueueResponse("* BYE logging out\r\n" +
                               "0003 OK done\r\n");

            try {
              ImapWebRequestDefaults.ClientID.Clear();
              ImapWebRequestDefaults.ClientID["name"] = "test-client";
              ImapWebRequestDefaults.ClientID["version"] = "1.0";

              using (var response = request.GetResponse() as ImapWebResponse) {
            Assert.IsNotNull(response.ServerID);
            Assert.IsTrue(response.ServerID.IsReadOnly);
            Assert.AreEqual(1, response.ServerID.Count);
            Assert.AreEqual("ImapPseudoServer", response.ServerID["name"]);
            Assert.AreEqual("ImapPseudoServer", response.ServerID["NAME"]);

            server.DequeueRequest(); // LOGIN
            StringAssert.StartsWith("0001 ID (\"name\" \"test-client\" \"version\" \"1.0\")",
                                    server.DequeueRequest());
              }
            }
            finally {
              ImapWebRequestDefaults.ClientID.Clear();

              foreach (var pair in defaultClientId) {
            ImapWebRequestDefaults.ClientID.Add(pair.Key, pair.Value);
              }
            }
              }
        }
Exemplo n.º 20
0
        private void AuthenticateWithAppropriateMechanism(string userinfo)
        {
            using (var server = new ImapPseudoServer()) {
            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 ready\r\n");
            // CAPABILITY response
            server.EnqueueResponse("* CAPABILITY IMAP4rev1 AUTH=DIGEST-MD5 AUTH=CRAM-MD5 AUTH=NTLM AUTH=PLAIN AUTH=LOGIN AUTH=ANONYMOUS\r\n" +
                               "0000 OK done\r\n");
            // AUTHENTICATE response
            server.EnqueueResponse("+ \r\n");
            server.EnqueueResponse("* CAPABILITY IMAP4rev1\r\n" +
                               "0001 OK done\r\n");

            using (var session = ImapSessionCreator.CreateSession(prof, null, null)) {
              Assert.AreEqual(ImapSessionState.Authenticated, session.State);
              Assert.AreEqual(prof.Authority, session.Authority);
            }

            server.DequeueRequest(); // CAPABILITY
            StringAssert.Contains("AUTHENTICATE LOGIN", server.DequeueRequest());
              }
        }
Exemplo n.º 21
0
        public void TestCreateSessionSecureChannelFailureStartTlsException()
        {
            using (var server = new ImapPseudoServer()) {
            server.Start();

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

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

            // greeting
            server.EnqueueResponse("* OK ready\r\n");
            // CAPABILITY response
            server.EnqueueResponse("* CAPABILITY IMAP4rev1 STARTTLS LOGINDISABLED\r\n" +
                               "0000 OK done\r\n");
            // STARTTLS response
            server.EnqueueResponse("0001 OK done\r\n");

            var defaultCallback = ImapSessionManager.CreateSslStreamCallback;

            try {
              ImapSessionManager.CreateSslStreamCallback = delegate(ConnectionBase connection, Stream baseStream) {
            throw new System.Security.Authentication.AuthenticationException();
              };

              try {
            using (var response = request.GetResponse()) {
            }
              }
              catch (WebException ex) {
            WebExceptionAssertion(WebExceptionStatus.SecureChannelFailure, ex);

            var upgradeException = ex.InnerException as Smdn.Net.Imap4.Protocol.ImapUpgradeConnectionException;

            Assert.IsNotNull(upgradeException);
            Assert.IsNotNull(upgradeException.InnerException);
            Assert.IsInstanceOfType(typeof(System.Security.Authentication.AuthenticationException),
                                    upgradeException.InnerException);
              }
            }
            finally {
              ImapSessionManager.CreateSslStreamCallback = defaultCallback;
            }
              }
        }
Exemplo n.º 22
0
        private void AuthenticateSpecificMechanism(NetworkCredential credential)
        {
            using (var server = new ImapPseudoServer()) {
            server.Start();

            var prof = new Profile(new NetworkCredential("imap-user", (string)null), "imap", 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("* CAPABILITY IMAP4rev1\r\n" +
                               "0000 OK done\r\n");
            // AUTHENTICATE X-PSEUDO-MECHANISM response
            server.EnqueueResponse("+ \r\n");
            server.EnqueueResponse("* CAPABILITY IMAP4rev1\r\n" +
                               "0001 OK done\r\n");

            using (var authMechanism = new SaslPseudoMechanism(1)) {
              authMechanism.Credential = credential;

              using (var session = ImapSessionCreator.CreateSession(prof, authMechanism, null)) {
            Assert.AreEqual(ImapSessionState.Authenticated, session.State);

            if (credential == null)
              Assert.AreEqual(new Uri(string.Format("imap://AUTH=X-PSEUDO-MECHANISM@{0}/", server.HostPort)),
                              session.Authority);
            else
              Assert.AreEqual(new Uri(string.Format("imap://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("AUTHENTICATE X-PSEUDO-MECHANISM", server.DequeueRequest());
              }
        }
Exemplo n.º 23
0
        public void TestGetResponseKeepAliveFalse()
        {
            using (var server = new ImapPseudoServer()) {
            for (var req = 0; req < 3; req++) {
              server.Start();

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

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

              // greeting
              server.EnqueueResponse("* OK ready\r\n");
              // CAPABILITY
              server.EnqueueResponse("* CAPABILITY IMAP4rev1\r\n" +
                                 "0000 OK done\r\n");
              // LOGIN
              server.EnqueueResponse("* CAPABILITY IMAP4rev1\r\n" +
                                 "0001 OK done\r\n");
              // NOOP
              server.EnqueueResponse("0002 OK done\r\n");
              // LOGOUT
              server.EnqueueResponse("* BYE logging out\r\n" +
                                 "0003 OK done\r\n");

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

              server.DequeueRequest(); // CAPABILITY
              server.DequeueRequest(); // LOGIN
              StringAssert.Contains("NOOP", server.DequeueRequest(), "request #{0}", req);
              StringAssert.Contains("LOGOUT", server.DequeueRequest(), "request #{0}", req);

              server.Stop();
            }
              }
        }
Exemplo n.º 24
0
        private void GetResponseTimeout(int timeout, int readWriteTimeout)
        {
            using (var server = new ImapPseudoServer()) {
            server.Start();

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

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

            // greeting
            server.EnqueueResponse("* OK ready\r\n");
            // CAPABILITY
            server.EnqueueResponse("* CAPABILITY IMAP4rev1\r\n" +
                               "0000 OK done\r\n");
            // LOGIN
            server.EnqueueResponse("* CAPABILITY IMAP4rev1\r\n" +
                               "0001 OK done\r\n");
            // not respond to NOOP

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

            server.DequeueRequest(); // CAPABILITY
            server.DequeueRequest(); // LOGIN
            StringAssert.Contains("NOOP", server.DequeueRequest());
              }
        }
Exemplo n.º 25
0
        public void TestGetResponseNo()
        {
            using (var server = new ImapPseudoServer()) {
            server.Start();

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

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

            // greeting
            server.EnqueueResponse("* OK ready\r\n");
            // CAPABILITY
            server.EnqueueResponse("* CAPABILITY IMAP4rev1\r\n" +
                               "0000 OK done\r\n");
            // LOGIN
            server.EnqueueResponse("* CAPABILITY IMAP4rev1\r\n" +
                               "0001 OK done\r\n");
            // SUBSCRIBE
            server.EnqueueResponse("0002 NO failure\r\n");
            // LOGOUT
            server.EnqueueResponse("* BYE logging out\r\n" +
                               "0003 OK done\r\n");

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

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

              var r = ex.Response as ImapWebResponse;

              Assert.IsNotNull(r.Result);
            }

            server.DequeueRequest(); // CAPABILITY
            server.DequeueRequest(); // LOGIN
            StringAssert.Contains("SUBSCRIBE", server.DequeueRequest());
              }
        }
Exemplo n.º 26
0
        private void KeepAliveAndDisconnected(bool disconnectFromServer)
        {
            using (var server = new ImapPseudoServer()) {
            for (var req = 0; req < 2; req++) {
              server.Start();

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

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

              // greeting
              server.EnqueueResponse("* OK ready\r\n");
              // CAPABILITY
              server.EnqueueResponse("* CAPABILITY IMAP4rev1\r\n" +
                                 "0000 OK done\r\n");
              // LOGIN
              server.EnqueueResponse("* CAPABILITY IMAP4rev1\r\n" +
                                 "0001 OK done\r\n");
              // NOOP
              server.EnqueueResponse("0002 OK done\r\n");

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

              server.DequeueRequest(); // CAPABILITY
              server.DequeueRequest(); // LOGIN
              StringAssert.Contains("NOOP", server.DequeueRequest(), "request #{0}", req);

              if (disconnectFromServer) {
            server.Stop();
              }
              else {
            // LOGOUT
            server.EnqueueResponse("* BYE logging out\r\n" +
                                   "0003 OK done\r\n");

            ImapSessionManager.DisconnectFrom(request.RequestUri);

            server.DequeueRequest(); // LOGOUT

            server.Stop();
              }
            }
              }
        }
Exemplo n.º 27
0
        public void TestGetResponseNoWithUnxpectedResponseCode()
        {
            using (var server = new ImapPseudoServer()) {
            server.Start();

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

            request.Timeout = 1000;
            request.Method = "SELECT";
            request.ExpectedErrorResponseCodes = new[] {ImapResponseCode.PrivacyRequired};
            request.KeepAlive = false;

            // greeting
            server.EnqueueResponse("* OK ready\r\n");
            // CAPABILITY
            server.EnqueueResponse("* CAPABILITY IMAP4rev1\r\n" +
                               "0000 OK done\r\n");
            // LOGIN
            server.EnqueueResponse("* CAPABILITY IMAP4rev1\r\n" +
                               "0001 OK done\r\n");
            // SELECT
            server.EnqueueResponse("0002 NO [NOPERM] failed\r\n");
            // LOGOUT
            server.EnqueueResponse("* BYE logging out\r\n" +
                               "0003 OK done\r\n");

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

              var response = ex.Response as ImapWebResponse;

              Assert.IsNotNull(response);
              Assert.AreEqual(response.ResponseCode, ImapResponseCode.NoPerm);
            }

            server.DequeueRequest(); // CAPABILITY
            server.DequeueRequest(); // LOGIN
            StringAssert.EndsWith("SELECT \"INBOX\"\r\n", server.DequeueRequest());
              }
        }
Exemplo n.º 28
0
        private void SelectRequestMailboxFailure(bool readOnly)
        {
            using (var server = new ImapPseudoServer()) {
            server.Start();

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

            request.Credentials = credential;
            request.Timeout = 1000;
            request.Method = "CHECK";
            request.KeepAlive = false;
            request.ReadOnly = readOnly;
            request.AllowInsecureLogin = true;

            // greeting
            server.EnqueueResponse("* OK ready\r\n");
            // CAPABILITY
            server.EnqueueResponse("* CAPABILITY IMAP4rev1\r\n" +
                               "0000 OK done\r\n");
            // LOGIN
            server.EnqueueResponse("* CAPABILITY IMAP4rev1\r\n" +
                               "0001 OK done\r\n");
            // SELECT/EXAMINE
            server.EnqueueResponse("0002 NO can't select\r\n");
            // LOGOUT
            server.EnqueueResponse("* BYE logging out\r\n" +
                               "0003 OK done\r\n");

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

            server.DequeueRequest(); // CAPABILITY
            server.DequeueRequest(); // LOGIN
            if (readOnly)
              StringAssert.Contains("EXAMINE \"INBOX\"", server.DequeueRequest());
            else
              StringAssert.Contains("SELECT \"INBOX\"", server.DequeueRequest());
              }
        }
Exemplo n.º 29
0
        private void AuthenticateWithSpecifiedSaslMechanisms(string[] mechanisms)
        {
            using (var server = new ImapPseudoServer()) {
            server.Start();

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

            request.UsingSaslMechanisms = mechanisms;
            request.Credentials = credential;
            request.Timeout = 1000;
            request.Method = "NOOP";
            request.KeepAlive = true;
            request.AllowInsecureLogin = true;

            // greeting
            server.EnqueueResponse("* OK ready\r\n");
            // CAPABILITY
            server.EnqueueResponse("* CAPABILITY IMAP4rev1 AUTH=DIGEST-MD5 AUTH=NTLM AUTH=CRAM-MD5 AUTH=PLAIN AUTH=LOGIN\r\n" +
                               "0000 OK done\r\n");
            // LOGIN
            server.EnqueueResponse("* CAPABILITY IMAP4rev1\r\n" +
                               "0001 OK done\r\n");
            // NOOP
            server.EnqueueResponse("0002 OK done\r\n");
            // LOGOUT
            server.EnqueueResponse("* BYE logging out\r\n" +
                               "0003 OK done\r\n");

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

            server.DequeueRequest(); // CAPABILITY
            server.DequeueRequest(); // LOGIN
            StringAssert.Contains("NOOP", server.DequeueRequest());
              }
        }
Exemplo n.º 30
0
        private void Request(ImapPseudoServer server, string method, string[] capabilities, string[] methodResponses, Action<ImapWebRequest, ImapWebResponse> responseAction)
        {
            server.Start();

              var request = WebRequest.Create(string.Format("imap://user@{0}/", server.HostPort)) as ImapWebRequest;

              Assert.AreEqual("ImapServerWebRequest", request.GetType().Name);

              request.Credentials = new NetworkCredential("user", "pass");
              request.KeepAlive = false;
              request.Timeout = 1000;
              request.Method = method;
              request.AllowInsecureLogin = true;

              // greeting
              server.EnqueueResponse("* OK ready\r\n");
              // CAPABILITY
              server.EnqueueResponse("* CAPABILITY IMAP4rev1\r\n" +
                             "0000 OK done\r\n");
              // LOGIN
              server.EnqueueResponse(string.Format("* CAPABILITY {0}\r\n", string.Join(" ", capabilities)) +
                             "0001 OK done\r\n");

              int commandTag = 2;

              foreach (var methodResponse in methodResponses) {
            server.EnqueueResponse(methodResponse);

            commandTag++;
              }

              // LOGOUT
              server.EnqueueResponse("* BYE logging out\r\n" +
                             string.Format("{0:x4} OK done\r\n", commandTag));

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

              server.DequeueRequest(); // CAPABILITY
              server.DequeueRequest(); // LOGIN
        }