public new void TestFixtureSetUp()
        {
            SslSetup.SetupSSLPorts(_application);


            Thread.Sleep(1000);
        }
        public void POP3ServerSupportingStartTLS_StartTLSOptional()
        {
            var messages = new List <string>();

            string message = "From: [email protected]\r\n" +
                             "To: [email protected]\r\n" +
                             "Subject: Test\r\n" +
                             "\r\n" +
                             "Hello!";

            messages.Add(message);

            int port = TestSetup.GetNextFreePort();

            using (var pop3Server = new POP3Server(1, port, messages, eConnectionSecurity.eCSSTARTTLSRequired))
            {
                pop3Server.SetCertificate(SslSetup.GetCertificate());
                pop3Server.StartListen();

                TestSetup.DeleteCurrentDefaultLog();

                Account account = SingletonProvider <TestSetup> .Instance.AddAccount(_domain, "*****@*****.**", "test");

                FetchAccount fa = account.FetchAccounts.Add();

                fa.Enabled             = true;
                fa.MinutesBetweenFetch = 10;
                fa.Name                  = "Test";
                fa.Username              = "******";
                fa.Password              = "******";
                fa.ConnectionSecurity    = eConnectionSecurity.eCSSTARTTLSOptional;
                fa.ServerAddress         = "localhost";
                fa.Port                  = port;
                fa.ProcessMIMERecipients = false;
                fa.Save();

                fa.DownloadNow();
                pop3Server.WaitForCompletion();

                POP3ClientSimulator.AssertMessageCount("*****@*****.**", "test", 1);

                fa.Delete();
            }
        }
Exemple #3
0
        public bool ProcessCommand(string command)
        {
            if (command.ToLower().StartsWith("quit"))
            {
                // Remove the messages...
                DeletedMessages.Sort();
                DeletedMessages.Reverse();
                foreach (int deletedMessage in DeletedMessages)
                {
                    _messages.RemoveAt(deletedMessage - 1);
                }

                return(false);
            }

            if (command.ToLower().StartsWith("user"))
            {
                Send("+OK\r\n");
                return(true);
            }

            if (command.ToLower().StartsWith("capa"))
            {
                string capabilities = "USER\r\nUIDL\r\nTOP\r\n";

                if (_connectionSecurity == eConnectionSecurity.eCSSTARTTLSRequired ||
                    _connectionSecurity == eConnectionSecurity.eCSSTARTTLSOptional)
                {
                    capabilities += "STLS\r\n";
                }

                string response = "+OK CAPA list follows\r\n" + capabilities + "." + "\r\n";
                Send(response);
                return(true);
            }

            if (command.ToLower().StartsWith("stls"))
            {
                Send("+OK Begin TLS negotiation\r\n");
                _tcpConnection.HandshakeAsServer(SslSetup.GetCertificate());
                return(true);
            }

            if (command.ToLower().StartsWith("pass"))
            {
                Send("+OK\r\n");
                return(true);
            }

            if (command.ToLower().StartsWith("uidl"))
            {
                if (!SupportsUIDL)
                {
                    Send("-ERR unhandled command\r\n");
                    return(true);
                }

                Send("+OK\r\n");

                for (int i = 0; i < _messages.Count; i++)
                {
                    Send(string.Format("{0} UniqueID-{1}\r\n", i + 1, _messages[i].GetHashCode()));
                }

                Send(".\r\n");
                return(true);
            }

            if (command.ToLower().StartsWith("retr"))
            {
                command = command.Substring(5);
                command = command.TrimEnd('\n');
                command = command.TrimEnd('\r');

                int messageID = Convert.ToInt32(command);

                RetrievedMessages.Add(messageID);

                string message = _messages[messageID - 1];

                switch (SendBufferMode)
                {
                case BufferMode.Split:
                {
                    Send("+OK\r\n");
                    Send(message);
                    Send("\r\n.\r\n");
                    break;
                }

                case BufferMode.SingleBuffer:
                {
                    Send("+OK\r\n" + message + "\r\n.\r\n");
                    break;
                }

                case BufferMode.MessageAndTerminatonTogether:
                {
                    Send("+OK\r\n");
                    Send(message + "\r\n.\r\n");
                    break;
                }
                }


                if (DisconnectAfterRetrCompletion)
                {
                    return(false);
                }

                return(true);
            }

            if (command.ToLower().StartsWith("dele"))
            {
                command = command.Substring(5);
                command = command.TrimEnd('\n');
                command = command.TrimEnd('\r');

                int messageID = Convert.ToInt32(command);

                DeletedMessages.Add(messageID);

                Send("+OK\r\n");

                return(true);
            }

            return(true);
        }
        public void POP3ServerNOTSupportingStartTLS_StartTLSRequired()
        {
            var messages = new List <string>();

            string message = "From: [email protected]\r\n" +
                             "To: [email protected]\r\n" +
                             "Subject: Test\r\n" +
                             "\r\n" +
                             "Hello!";

            messages.Add(message);

            int port = TestSetup.GetNextFreePort();

            using (var pop3Server = new POP3Server(1, port, messages, eConnectionSecurity.eCSNone))
            {
                pop3Server.SetCertificate(SslSetup.GetCertificate());
                pop3Server.StartListen();

                TestSetup.DeleteCurrentDefaultLog();

                Account account = SingletonProvider <TestSetup> .Instance.AddAccount(_domain, "*****@*****.**", "test");

                FetchAccount fa = account.FetchAccounts.Add();

                fa.Enabled             = true;
                fa.MinutesBetweenFetch = 10;
                fa.Name                  = "Test";
                fa.Username              = "******";
                fa.Password              = "******";
                fa.ConnectionSecurity    = eConnectionSecurity.eCSSTARTTLSRequired;
                fa.ServerAddress         = "localhost";
                fa.Port                  = port;
                fa.ProcessMIMERecipients = false;
                fa.Save();

                fa.DownloadNow();

                string expectedMessage =
                    string.Format(
                        "The download of messages from external account {0} failed. The external aAccount is configured to use STARTTLS connection security, but the POP3 server does not support it.", fa.Name);

                string contentSoFar = "";

                for (int i = 0; i <= 10; i++)
                {
                    if (i == 5)
                    {
                        CustomAssert.Fail("No connection: " + contentSoFar);
                    }

                    contentSoFar = TestSetup.ReadCurrentDefaultLog();
                    if (contentSoFar.Contains(expectedMessage))
                    {
                        break;
                    }

                    Thread.Sleep(1000);
                }

                pop3Server.WaitForCompletion();

                fa.Delete();
            }
        }
        private bool ProcessCommand(string command)
        {
            if (command.ToUpper().StartsWith("HELO"))
            {
                Send("250 Test Server - Helo\r\n");
                return(false);
            }

            if (command.ToUpper().StartsWith("EHLO"))
            {
                var response = new StringBuilder();

                if (_connectionSecurity == eConnectionSecurity.eCSSTARTTLSRequired ||
                    _connectionSecurity == eConnectionSecurity.eCSSTARTTLSOptional)
                {
                    response.AppendLine("250-STARTTLS");
                }

                response.AppendLine("250 AUTH LOGIN PLAIN");

                Send(response.ToString());
                return(false);
            }

            if (command.ToUpper().StartsWith("STARTTLS"))
            {
                Send("220 Ready to start TLS\r\n");
                _tcpConnection.HandshakeAsServer(SslSetup.GetCertificate());
                return(false);
            }

            if (command.ToUpper().StartsWith("AUTH LOGIN"))
            {
                if (_connectionSecurity == eConnectionSecurity.eCSSTARTTLSRequired &&
                    !_tcpConnection.IsSslConnection)
                {
                    Send("503 STARTTLS required..\r\n");
                    return(false);
                }

                Send("334 VXNlcm5hbWU6\r\n");
                _expectingUsername = true;
                return(false);
            }

            if (command.ToUpper().StartsWith("MAIL"))
            {
                if (_connectionSecurity == eConnectionSecurity.eCSSTARTTLSRequired &&
                    !_tcpConnection.IsSslConnection)
                {
                    Send("503 STARTTLS required..\r\n");
                    return(false);
                }

                Send(_mailFromresult.ToString() + "\r\n");

                if (_mailFromresult == 250)
                {
                    _hasMailFrom = true;
                }

                return(false);
            }

            if (command.ToUpper().StartsWith("RCPT"))
            {
                if (!_hasMailFrom)
                {
                    Send("503 must have sender first.\r\n");
                    return(false);
                }
                int StartPos = command.IndexOf("<") + 1;
                int EndPos   = command.LastIndexOf(">");
                int length   = EndPos - StartPos;

                string address = command.Substring(StartPos, length);

                if (!_currentRecipientResult.ContainsKey(address))
                {
                    throw new Exception("Unexpected address");
                }

                string result = _currentRecipientResult[address].ToString();

                Send(result + " " + address + "\r\n");

                RcptTosReceived++;

                return(false);
            }

            if (command.ToUpper().StartsWith("DATA"))
            {
                Send("354 Test Server - Give it to me...\r\n");
                _transmittingData = true;
                _messageData      = "";
                return(false);
            }

            if (command.ToUpper().StartsWith("QUIT"))
            {
                if (_simulatedError != SimulatedErrorType.DisconnectWithoutReplyOnQuit)
                {
                    Send(_quitResult.ToString() + " Test Server - Goodbye\r\n");
                }

                Disconnect();

                return(true);
            }

            if (_transmittingData)
            {
                if (_simulatedError == SimulatedErrorType.DisconnectAfterDeliveryStarted)
                {
                    // We've received some message data. Disconenct!
                    Disconnect();
                    return(true);
                }


                _messageData += command;
            }

            if (_messageData.IndexOf("\r\n.\r\n") > 0)
            {
                // remove the ending...
                _messageData = _messageData.Replace("\r\n.\r\n", "\r\n");

                Send("250 Test Server - Queued for delivery\r\n");

                if (_simulatedError == SimulatedErrorType.DisconnectAfterMessageAccept)
                {
                    Disconnect();
                    return(true);
                }

                _transmittingData = false;
                return(false);
            }

            if (_expectingUsername)
            {
                _expectingUsername = false;
                Send("334 UGFzc3dvcmQ6\r\n");
                _expectingPassword = true;
                return(false);
            }

            if (_expectingPassword)
            {
                if (SimulatedError == SimulatedErrorType.ForceAuthenticationFailure)
                {
                    Send("535 Authentication failed. Restarting authentication process.\r\n");
                }
                else
                {
                    Send("235 authenticated.\r\n");
                }

                _expectingPassword = false;

                return(false);
            }

            return(false);
        }