Exemplo n.º 1
0
        public bool TestConnect(int iPort)
        {
            bool bRetVal = _tcpConnection.Connect(iPort);

            _tcpConnection.Disconnect();
            return(bRetVal);
        }
Exemplo n.º 2
0
        public void Send100MBMessage()
        {
            long memoryUsage = Shared.GetCurrentMemoryUsage();

             _application.Settings.MaxMessageSize = 0;

             TcpConnection socket = new TcpConnection();
             socket.Connect(25);
             socket.Receive();

             socket.Send("HELO test.com\r\n");
             socket.Receive();

             // Build a large string...
             StringBuilder sb = new StringBuilder();
             sb.Append("A01");
             for (int i = 0; i < 10000; i++)
             {
            sb.Append("01234567890012345678900123456789001234567890012345678900123456789001234567890012345678900123456789001234567890\r\n");
             }
             int length = sb.Length;

             sb.Append(Environment.NewLine);

             socket.Send("MAIL FROM: [email protected]\r\n");
             socket.Receive();
             socket.Send("RCPT TO: [email protected]\r\n");
             socket.Receive();
             socket.Send("DATA\r\n");
             socket.Receive();

             for (int i = 1; i <= 100; i++)
             {
            Shared.AssertLowMemoryUsage(memoryUsage + 30);

            try
            {
               socket.Send(sb.ToString());
            }
            catch (Exception)
            {
               return;
            }

            if ((i % 10) == 0)
            {
               TestTracer.WriteTraceInfo("{0}/{1}", i, 1000);
            }
             }

             socket.Send("\r\n.\r\n");
             string result = socket.Receive();
             Assert.IsTrue(result.StartsWith("250"));

             socket.Send("QUIT\r\n");
             result = socket.Receive();
             socket.Disconnect();

             Pop3ClientSimulator.AssertMessageCount(_domain.Accounts[0].Address, "test", 1);
        }
Exemplo n.º 3
0
        public void TestExcessiveDataInIMAPConversation()
        {
            var sb = new StringBuilder();
             for (int i = 0; i < 100000; i++)
             {
            sb.Append("1234567890");
             }

             sb.Append(".com\r\n");

             string command = "A03 NOOP " + sb;

             var socket = new TcpConnection();
             CustomAssert.IsTrue(socket.Connect(143));
             socket.Receive();
             socket.Send(command);

             try
             {
            string response = socket.Receive();
            CustomAssert.IsTrue(response.StartsWith("* BYE"));
             }
             catch (System.IO.IOException ex)
             {
            AssertIsConnectionTerminatedException(ex);
             }

             socket.Disconnect();
        }
Exemplo n.º 4
0
        public void SetupSSLPort()
        {
            _application.Settings.Logging.Enabled = true;
             _application.Settings.Logging.LogTCPIP = true;
             _application.Settings.Logging.EnableLiveLogging(true);

             var cs = new TcpConnection();
             if (!cs.Connect(250))
            CustomAssert.Fail("Could not connect to SSL server.");

             cs.Disconnect();

             for (int i = 0; i <= 40; i++)
             {
            CustomAssert.IsTrue(i != 40);

            string liveLog = _application.Settings.Logging.LiveLog;
            if (liveLog.Contains("SSL handshake with client failed."))
               break;

            Thread.Sleep(250);
             }

             _application.Settings.Logging.EnableLiveLogging(false);
        }
Exemplo n.º 5
0
        public void TestManyTCPIPConnections()
        {
            LogHandler.DeleteCurrentDefaultLog();

             const int count = 1000;

             List<TcpConnection> sockets = new List<TcpConnection>();

             for (int i = 1; i <= count; i++)
             {
            TcpConnection socket = new TcpConnection();
            Assert.IsTrue(socket.Connect(25));

            if ((i % 10) == 0)
            {
               TestTracer.WriteTraceInfo("{0}/{1}", i, 1000);
            }

            sockets.Add(socket);
             }

             foreach (TcpConnection socket in sockets)
             {
            socket.Disconnect();
             }

             RetryHelper.TryAction(() =>
            {
               string log = LogHandler.ReadCurrentDefaultLog();

               string connectionCreated = "TCP - 127.0.0.1 connected to 127.0.0.1:25.";
               string connectionEnded = "Ending session ";

               var created = Regex.Matches(log, connectionCreated);
               var ended = Regex.Matches(log, connectionEnded);

               Assert.AreEqual(count, created.Count);
               Assert.AreEqual(count, ended.Count);

            }, TimeSpan.FromSeconds(1),TimeSpan.FromSeconds(30));
        }
Exemplo n.º 6
0
        private void AssertValidMailRcptToCommand(string comamnd)
        {
            var smtpClientSimulator = new TcpConnection();
             smtpClientSimulator.Connect(25);
             Assert.IsTrue(smtpClientSimulator.Receive().StartsWith("220"));
             smtpClientSimulator.Send("HELO test\r\n");
             Assert.IsTrue(smtpClientSimulator.Receive().StartsWith("250"));
             smtpClientSimulator.Send("MAIL FROM: <*****@*****.**>\r\n");
             Assert.IsTrue(smtpClientSimulator.Receive().StartsWith("250"));

             string result = smtpClientSimulator.SendAndReceive(comamnd + "\r\n");

             smtpClientSimulator.Disconnect();

             Assert.AreEqual("250 OK\r\n", result);
        }
Exemplo n.º 7
0
        public void TestPOP3DownloadOfLargeMessage()
        {
            Send100MBMessage();

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

             TcpConnection socket = new TcpConnection();
             socket.Connect(110);

             // Receive welcome message.
             socket.Receive();

             socket.Send("USER " + "*****@*****.**" + "\r\n");
             socket.ReadUntil("+OK");

             socket.Send("PASS " + "test" + "\r\n");
             socket.ReadUntil("+OK");

             socket.Send("RETR 1\r\n");

             var endOfResponseSB = new StringBuilder();

             string endOfResponse = "";
             while (endOfResponse.IndexOf("\r\n.\r\n") < 0)
             {
            if (endOfResponse.IndexOf("-ERR no such message") >= 0)
            {
               socket.Disconnect();
               Assert.Fail("Nope");
            }

            endOfResponseSB.Append(socket.Receive());

            if (endOfResponseSB.Length > 100)
               endOfResponseSB.Remove(0, endOfResponseSB.Length - 100);

            endOfResponse = endOfResponseSB.ToString();
             }

             socket.Send("DELE 1\r\n");
             socket.ReadUntil("+OK");

             socket.Send("QUIT\r\n");
             socket.ReadUntil("+OK");

             socket.Disconnect();
        }
Exemplo n.º 8
0
        public void TestLongLineInSMTPConversation()
        {
            var sb = new StringBuilder();
             for (int i = 0; i < 400; i++)
             {
            sb.Append("1234567890");
             }

             sb.Append(".com");

             string command = "HELO " + sb;

             var socket = new TcpConnection();
             CustomAssert.IsTrue(socket.Connect(25));
             socket.Receive();
             socket.Send(command + "\r\n");

             string response = socket.Receive();
             CustomAssert.IsTrue(response.StartsWith("500"));

             socket.Disconnect();
        }
Exemplo n.º 9
0
        public void TestTooManyInvalidCommandsTempError()
        {
            Account account = SingletonProvider<TestSetup>.Instance.AddAccount(_domain, "*****@*****.**", "test");

             for (int i = 0; i < 10; i++)
            SingletonProvider<TestSetup>.Instance.AddAccount(_domain, string.Format("test{0}@test.com", i), "test");

             Application application = SingletonProvider<TestSetup>.Instance.GetApp();
             Settings settings = _settings;

             settings.DisconnectInvalidClients = true;
             settings.MaxNumberOfInvalidCommands = 5;

             settings.AntiSpam.GreyListingEnabled = true;

             var sim = new TcpConnection();
             sim.Connect(25);
             string res = sim.Receive();
             sim.Send("EHLO test.com\r\n");
             res = sim.Receive();
             sim.Send("MAIL FROM: <*****@*****.**>\r\n");
             res = sim.Receive();
             for (int i = 1; i < 10; i++)
             {
            string address = string.Format("test{0}@test.com", i);

            sim.Send("RCPT TO: " + address + "\r\n");

            res = sim.Receive();

            CustomAssert.AreEqual("451 Please try again later.\r\n", res);
             }

             sim.Disconnect();
        }
Exemplo n.º 10
0
        public void TestRcptToSyntax()
        {
            Account account1 = SingletonProvider<TestSetup>.Instance.AddAccount(_domain, "*****@*****.**", "test");

             var oSMTP = new TcpConnection();
             oSMTP.Connect(25);

             CustomAssert.IsTrue(oSMTP.Receive().StartsWith("220"));
             oSMTP.Send("HELO test\r\n");
             CustomAssert.IsTrue(oSMTP.Receive().StartsWith("250"));

             // A few tests of invalid syntax.
             CustomAssert.IsTrue(oSMTP.SendAndReceive("MAIL FROM: <*****@*****.**>\r\n").StartsWith("250"));
             CustomAssert.IsFalse(oSMTP.SendAndReceive("RCPT TO: [email protected]>\r\n").StartsWith("250"));
             CustomAssert.IsFalse(oSMTP.SendAndReceive("RCPT TO: <[email protected]\r\n").StartsWith("250"));
             CustomAssert.IsFalse(oSMTP.SendAndReceive("RCPT TO <[email protected]\r\n").StartsWith("250"));
             CustomAssert.IsFalse(oSMTP.SendAndReceive("RCPT TO<[email protected]\r\n").StartsWith("250"));

             CustomAssert.IsTrue(oSMTP.SendAndReceive("RCPT TO: <*****@*****.**>\r\n").StartsWith("250"));
             CustomAssert.IsTrue(oSMTP.SendAndReceive("RCPT TO: [email protected]\r\n").StartsWith("250"));

             oSMTP.Disconnect();
        }
Exemplo n.º 11
0
        public void TestHelo()
        {
            var oSimulator = new TcpConnection();

             oSimulator.Connect(25);

             string sWelcome = oSimulator.Receive();

             oSimulator.Send("HELO\r\n");
             string sResponse = oSimulator.Receive();

             if (!sResponse.StartsWith("501"))
            throw new Exception("Invalid response to HELO");

             oSimulator.Send("HELO   \r\n");
             sResponse = oSimulator.Receive();

             if (!sResponse.StartsWith("501"))
            throw new Exception("Invalid response to HELO");

             oSimulator.Send("HELO TEST.COM\r\n");
             sResponse = oSimulator.Receive();

             if (!sResponse.StartsWith("250"))
            throw new Exception("Invalid response to HELO");

             oSimulator.Send("HELO   TEST.COM\r\n");
             sResponse = oSimulator.Receive();

             if (!sResponse.StartsWith("250"))
            throw new Exception("Invalid response to HELO");

             oSimulator.Send("EHLO TEST.COM\r\n");
             sResponse = oSimulator.Receive();

             if (!sResponse.StartsWith("250"))
            throw new Exception("Invalid response to HELO");

             oSimulator.Send("EHLO    TEST.COM\r\n");
             sResponse = oSimulator.Receive();

             if (!sResponse.StartsWith("250"))
            throw new Exception("Invalid response to HELO");

             oSimulator.Disconnect();
        }
Exemplo n.º 12
0
        public void TestLongSMTPCommand()
        {
            var socket = new TcpConnection();
             socket.Connect(25);

             // Build a large string...
             var sb = new StringBuilder();
             sb.Append("A01");
             for (int i = 0; i < 1000000; i++)
             {
            sb.Append("01234567890");
             }

             sb.Append(Environment.NewLine);

             for (int i = 0; i < 100; i++)
             {
            try
            {
               socket.Send(sb.ToString());
            }
            catch (Exception)
            {
               return;
            }

            if ((i % 10) == 0)
            {
               TestTracer.WriteTraceInfo("{0}/{1}", i, 100);
            }
             }

             socket.Send("\r\n");
             socket.Receive();

             socket.Disconnect();
        }
Exemplo n.º 13
0
        public void TestLongSMTPDataSessionWithoutNewline()
        {
            long memoryUsage = Shared.GetCurrentMemoryUsage();

             var socket = new TcpConnection();
             socket.Connect(25);
             socket.Receive();

             socket.Send("HELO test.com\r\n");
             socket.Receive();

             // Build a large string...
             var sb = new StringBuilder();
             sb.Append("A01");
             for (int i = 0; i < 10000; i++)
             {
            sb.Append("01234567890012345678900123456789001234567890012345678900123456789001234567890012345678900123456789001234567890");
             }

             socket.Send("MAIL FROM: [email protected]\r\n");
             socket.Receive();
             socket.Send("RCPT TO: [email protected]\r\n");
             socket.Receive();
             socket.Send("DATA\r\n");
             socket.Receive();

             for (int i = 1; i <= 1000; i++)
             {
            Shared.AssertLowMemoryUsage(memoryUsage + 30);

            try
            {
               socket.Send(sb.ToString());
            }
            catch (Exception)
            {
               return;
            }

            if ((i%10) == 0)
            {
               TestTracer.WriteTraceInfo("{0}/{1}", i, 1000);
            }
             }

             socket.Send("\r\n.\r\n");
             string result = socket.Receive();

             Assert.IsTrue(result.StartsWith("554 Too long line was received. Transmission aborted."));

             socket.Send("QUIT\r\n");
             socket.Receive();

             socket.Disconnect();
        }
Exemplo n.º 14
0
        public void TestLongSMTPDataSessionIncludingNewline()
        {
            long memoryUsage = Shared.GetCurrentMemoryUsage();

             _application.Settings.MaxMessageSize = 0;

             var socket = new TcpConnection();
             socket.Connect(25);
             socket.Receive();

             socket.Send("HELO test.com\r\n");
             socket.Receive();

             // Build a large string...
             var sb = new StringBuilder();
             sb.Append("A01");
             for (int i = 0; i < 10000; i++)
             {
            sb.Append("01234567890012345678900123456789001234567890012345678900123456789001234567890012345678900123456789001234567890\r\n");
             }

             sb.Append(Environment.NewLine);

             socket.Send("MAIL FROM: [email protected]\r\n");
             socket.Receive();
             socket.Send("RCPT TO: [email protected]\r\n");
             socket.Receive();
             socket.Send("DATA\r\n");
             socket.Receive();

             for (int i = 0; i < 100; i++)
             {
            Shared.AssertLowMemoryUsage(memoryUsage + 30);

            try
            {
               socket.Send(sb.ToString());
            }
            catch (Exception)
            {
               return;
            }
             }

             socket.Send("\r\n.\r\n");
             string result = socket.Receive();
             Assert.IsTrue(result.StartsWith("250"));

             socket.Send("QUIT\r\n");
             socket.Receive();
             socket.Disconnect();

             POP3ClientSimulator.AssertMessageCount(_domain.Accounts[0].Address, "test", 1);
        }
Exemplo n.º 15
0
        public void TestExcessiveDataInPOP3Conversation()
        {
            var sb = new StringBuilder();
             for (int i = 0; i < 100000; i++)
             {
            sb.Append("1234567890");
             }

             string command = "HELP " + sb;

             var socket = new TcpConnection();
             CustomAssert.IsTrue(socket.Connect(110));
             socket.Receive();
             socket.Send(command + "\r\n");

             try
             {
            string response = socket.Receive();
            CustomAssert.IsTrue(response.StartsWith("-ERR"));

            socket.Disconnect();
             }
             catch (IOException ex)
             {
            AssertIsConnectionTerminatedException(ex);
             }
        }
Exemplo n.º 16
0
        public void TestEHLOKeywords()
        {
            Application application = SingletonProvider<TestSetup>.Instance.GetApp();
             Settings settings = _settings;
             settings.HostName = "examplify.com";

             var socket = new TcpConnection();
             CustomAssert.IsTrue(socket.Connect(25));
             string result = socket.Receive();
             socket.Send("EHLO example.com\r\n");
             result = socket.Receive();
             socket.Disconnect();
             CustomAssert.IsTrue(result.Contains("250-" + settings.HostName));
        }
Exemplo n.º 17
0
        private void AssertInvalidMailFromCommand(string command, string expectedResponse)
        {
            var smtpClientSimulator = new TcpConnection();
             smtpClientSimulator.Connect(25);
             Assert.IsTrue(smtpClientSimulator.Receive().StartsWith("220"));
             smtpClientSimulator.Send("HELO test\r\n");
             Assert.IsTrue(smtpClientSimulator.Receive().StartsWith("250"));

             string result = smtpClientSimulator.SendAndReceive(command+ "\r\n");

             smtpClientSimulator.Disconnect();

             Assert.AreEqual(expectedResponse + "\r\n", result);
        }
Exemplo n.º 18
0
        public void TestMailFromSyntaxValidation()
        {
            Account account1 = SingletonProvider<TestSetup>.Instance.AddAccount(_domain, "*****@*****.**", "test");

             var oSMTP = new TcpConnection();
             oSMTP.Connect(25);

             CustomAssert.IsTrue(oSMTP.Receive().StartsWith("220"));
             oSMTP.Send("HELO test\r\n");
             CustomAssert.IsTrue(oSMTP.Receive().StartsWith("250"));

             // A few tests of invalid syntax.
             CustomAssert.IsFalse(oSMTP.SendAndReceive("MAIL FROM: <[email protected]\r\n").StartsWith("250"));
             CustomAssert.IsFalse(oSMTP.SendAndReceive("MAIL FROM: [email protected]>\r\n").StartsWith("250"));
             CustomAssert.IsFalse(oSMTP.SendAndReceive("MAIL FROM: <    [email protected]    \r\n").StartsWith("250"));
             CustomAssert.IsFalse(oSMTP.SendAndReceive("MAIL FROM: <        \r\n").StartsWith("250"));
             CustomAssert.IsFalse(oSMTP.SendAndReceive("MAIL FROM: >        \r\n").StartsWith("250"));
             CustomAssert.IsFalse(oSMTP.SendAndReceive("MAIL FROM: <[email protected]\r\n").StartsWith("250"));
             CustomAssert.IsFalse(oSMTP.SendAndReceive("MAIL FROM <*****@*****.**>\r\n").StartsWith("250"));
             CustomAssert.IsFalse(oSMTP.SendAndReceive("MAIL FROM  [email protected]\r\n").StartsWith("250"));

             // Valid syntax, < and >
             CustomAssert.IsTrue(oSMTP.SendAndReceive("MAIL FROM: <*****@*****.**>\r\n").StartsWith("250"));
             CustomAssert.IsTrue(oSMTP.SendAndReceive("RSET\r\n").StartsWith("250"));

             CustomAssert.IsTrue(oSMTP.SendAndReceive("MAIL FROM: [email protected]\r\n").StartsWith("250"));
             CustomAssert.IsTrue(oSMTP.SendAndReceive("RSET\r\n").StartsWith("250"));

             CustomAssert.IsTrue(oSMTP.SendAndReceive("MAIL FROM:    [email protected]   \r\n").StartsWith("250"));
             CustomAssert.IsTrue(oSMTP.SendAndReceive("RSET\r\n").StartsWith("250"));

             CustomAssert.IsTrue(oSMTP.SendAndReceive("MAIL FROM:[email protected]\r\n").StartsWith("250"));
             CustomAssert.IsTrue(oSMTP.SendAndReceive("RSET\r\n").StartsWith("250"));

             CustomAssert.IsTrue(oSMTP.SendAndReceive("MAIL FROM:<*****@*****.**>\r\n").StartsWith("250"));
             CustomAssert.IsTrue(oSMTP.SendAndReceive("RSET\r\n").StartsWith("250"));

             oSMTP.Disconnect();
        }
Exemplo n.º 19
0
        public void MailFromWithAuthParameterShouldBeAccepted()
        {
            Account account1 = SingletonProvider<TestSetup>.Instance.AddAccount(_domain, "*****@*****.**", "test");

             var smtpClientSimulator = new TcpConnection();
             smtpClientSimulator.Connect(25);

             Assert.IsTrue(smtpClientSimulator.Receive().StartsWith("220"));
             smtpClientSimulator.Send("HELO test\r\n");
             Assert.IsTrue(smtpClientSimulator.Receive().StartsWith("250"));

             // A few tests of invalid syntax.
             Assert.IsTrue(smtpClientSimulator.SendAndReceive("MAIL FROM: <*****@*****.**> AUTH=<>\r\n").StartsWith("250"));

             smtpClientSimulator.Disconnect();
        }
Exemplo n.º 20
0
        public void TestSameRecipientMultipleTimes()
        {
            Logging logging = SingletonProvider<TestSetup>.Instance.GetApp().Settings.Logging;
             logging.AWStatsEnabled = true;
             logging.Enabled = true;

             if (File.Exists(logging.CurrentAwstatsLog))
            File.Delete(logging.CurrentAwstatsLog);

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

             var oSMTP = new TcpConnection();
             oSMTP.Connect(25);
             CustomAssert.IsTrue(oSMTP.Receive().StartsWith("220"));
             oSMTP.Send("HELO test\r\n");
             CustomAssert.IsTrue(oSMTP.Receive().StartsWith("250"));
             oSMTP.Send("MAIL FROM: [email protected]\r\n");
             CustomAssert.IsTrue(oSMTP.Receive().StartsWith("250"));
             oSMTP.Send("RCPT TO: [email protected]\r\n");
             CustomAssert.IsTrue(oSMTP.Receive().StartsWith("250"));
             oSMTP.Send("RCPT TO: [email protected]\r\n");
             CustomAssert.IsTrue(oSMTP.Receive().StartsWith("250"));

             oSMTP.Disconnect();
        }
Exemplo n.º 21
0
        public void TestRcptToSyntax()
        {
            SingletonProvider<TestSetup>.Instance.AddAccount(_domain, "*****@*****.**", "test");

             var smtpClientSimulator = new TcpConnection();
             smtpClientSimulator.Connect(25);

             Assert.IsTrue(smtpClientSimulator.Receive().StartsWith("220"));
             smtpClientSimulator.Send("HELO test\r\n");
             Assert.IsTrue(smtpClientSimulator.Receive().StartsWith("250"));

             // A few tests of invalid syntax.
             Assert.IsTrue(smtpClientSimulator.SendAndReceive("MAIL FROM: <*****@*****.**>\r\n").StartsWith("250"));
             Assert.IsFalse(smtpClientSimulator.SendAndReceive("RCPT TO: [email protected]>\r\n").StartsWith("250"));
             Assert.IsFalse(smtpClientSimulator.SendAndReceive("RCPT TO: <[email protected]\r\n").StartsWith("250"));
             Assert.IsFalse(smtpClientSimulator.SendAndReceive("RCPT TO <[email protected]\r\n").StartsWith("250"));
             Assert.IsFalse(smtpClientSimulator.SendAndReceive("RCPT TO<[email protected]\r\n").StartsWith("250"));

             Assert.IsTrue(smtpClientSimulator.SendAndReceive("RCPT TO: <*****@*****.**>\r\n").StartsWith("250"));
             Assert.IsTrue(smtpClientSimulator.SendAndReceive("RCPT TO: [email protected]\r\n").StartsWith("250"));

             smtpClientSimulator.Disconnect();
        }
Exemplo n.º 22
0
 public void Disconnect()
 {
     _tcpConnection.Disconnect();
 }
Exemplo n.º 23
0
        public void TestLongLineInPOP3Conversation()
        {
            var sb = new StringBuilder();
             for (int i = 0; i < 400; i++)
             {
            sb.Append("1234567890");
             }

             sb.Append(".com");

             string command = "NOOP " + sb;

             var socket = new TcpConnection();
             Assert.IsTrue(socket.Connect(110));
             socket.Receive();
             socket.Send(command + "\r\n");

             string response = socket.Receive();
             Assert.IsTrue(response.StartsWith("-ERR Line too long."));

             socket.Disconnect();
        }