Пример #1
0
        public void TestStartingConnectionWritesWelcome()
        {
            var netStream      = new MockNetworkStream();
            var smtpStream     = new SmtpServerStream(netStream);
            var smtpConnection = new DummySmtpServerConnection(smtpStream, new MailBag(), Substitute.For <IDummySmtpServerConnectionCollection>());

            smtpConnection.Start();
            var readData = ReadResponseLine(netStream);

            Assert.That(readData, Is.EqualTo(SmtpServerResponses.Welcome));
        }
Пример #2
0
        public void TestSendingRecipientBeforeHello()
        {
            var netStream      = new MockNetworkStream();
            var smtpStream     = new SmtpServerStream(netStream);
            var smtpConnection = new DummySmtpServerConnection(smtpStream, new MailBag(), Substitute.For <IDummySmtpServerConnectionCollection>());

            smtpConnection.Start();
            ReadResponseLine(netStream);

            WriteRequest(netStream, SmtpServerRequests.Recipient);
            var mailResponse = ReadResponseLine(netStream);

            Assert.That(mailResponse, Is.EqualTo(SmtpServerResponses.BadCommandSequence));
        }
Пример #3
0
        public void TestSendingHelloWritesGreeting()
        {
            var netStream      = new MockNetworkStream();
            var smtpStream     = new SmtpServerStream(netStream);
            var smtpConnection = new DummySmtpServerConnection(smtpStream, new MailBag(), Substitute.For <IDummySmtpServerConnectionCollection>());

            smtpConnection.Start();
            ReadResponseLine(netStream);

            WriteRequest(netStream, SmtpServerRequests.Hello);
            var helloResponse = ReadResponseLine(netStream);

            Assert.That(helloResponse, Is.EqualTo(SmtpServerResponses.Greeting("unit-test")));
        }
Пример #4
0
        public void TestSendingDataBeforeMail()
        {
            var netStream      = new MockNetworkStream();
            var smtpStream     = new SmtpServerStream(netStream);
            var smtpConnection = new DummySmtpServerConnection(smtpStream, new MailBag(), Substitute.For <IDummySmtpServerConnectionCollection>());

            smtpConnection.Start();
            ReadResponseLine(netStream); // Welcome

            WriteRequest(netStream, SmtpServerRequests.Hello);
            ReadResponseLine(netStream); // Greeting to hello

            WriteRequest(netStream, SmtpServerRequests.Data);
            var dataResponse = ReadResponseLine(netStream);

            Assert.That(dataResponse, Is.EqualTo(SmtpServerResponses.BadCommandSequence));
        }
Пример #5
0
        private static string ReadResponseLine(MockNetworkStream stream_)
        {
            var readBuffer        = new byte[512];
            var dataBuffer        = new StringBuilder();
            var receivingResponse = true;

            while (receivingResponse)
            {
                var readCount      = stream_.ReadResponse(readBuffer, 0, readBuffer.Length);
                var commandDataStr = Encoding.ASCII.GetString(readBuffer, 0, readCount);
                dataBuffer.Append(commandDataStr);

                if (commandDataStr.EndsWith("\r\n"))
                {
                    receivingResponse = false;
                }
            }

            return(dataBuffer.ToString());
        }
Пример #6
0
        public void TestReadMailDataWhenTerminatorSplitAcrossPackets()
        {
            var lines = new[]
            {
                "Date: Thu, 21 May 1998 05:33:29 -0700\r\n",
                "From: John Q. Public <*****@*****.**>\r\n",
                "Subject:  The Next Meeting of the Board\r\n",
                "To: [email protected]\r\n",
                "\r\n",
                "Bill:\r\n",
                "The next meeting of the board of directors will be\r\n",
                "on Tuesday.\r\n",
                "\r\n",
                ".",
                "\r\n"
            };
            var netStream  = new MockNetworkStream();
            var smtpStream = new SmtpServerStream(netStream);

            using (smtpStream)
            {
                var readTask = smtpStream.ReadDataAsync();

                var expectedData = new StringBuilder();
                foreach (var line in lines)
                {
                    expectedData.Append(line);
                    var bytes = Encoding.ASCII.GetBytes(line);
                    netStream.WriteRequest(bytes, 0, bytes.Length);
                }

                expectedData.Replace("\r\n.\r\n", string.Empty);
                readTask.Wait();
                var data = readTask.Result;
                Assert.That(data, Is.EqualTo(expectedData.ToString()));
            }
        }
Пример #7
0
        public void TestSendingMail()
        {
            var netStream      = new MockNetworkStream();
            var smtpStream     = new SmtpServerStream(netStream);
            var smtpConnection = new DummySmtpServerConnection(smtpStream, new MailBag(), Substitute.For <IDummySmtpServerConnectionCollection>());

            smtpConnection.Start();
            ReadResponseLine(netStream); // Welcome

            WriteRequest(netStream, SmtpServerRequests.Hello);
            ReadResponseLine(netStream); // Greeting to hello

            WriteRequest(netStream, SmtpServerRequests.Mail);
            var mailResponse = ReadResponseLine(netStream);

            Assert.That(mailResponse, Is.EqualTo(SmtpServerResponses.Ok));

            WriteRequest(netStream, SmtpServerRequests.Recipient);
            var recipientResponse = ReadResponseLine(netStream);

            Assert.That(recipientResponse, Is.EqualTo(SmtpServerResponses.Ok));

            WriteRequest(netStream, SmtpServerRequests.AdditionalRecipient);
            recipientResponse = ReadResponseLine(netStream);
            Assert.That(recipientResponse, Is.EqualTo(SmtpServerResponses.Ok));

            WriteRequest(netStream, SmtpServerRequests.Data);
            var dataResponse = ReadResponseLine(netStream);

            Assert.That(dataResponse, Is.EqualTo(SmtpServerResponses.StartMailInput));

            WriteRequest(netStream, SmtpServerRequests.MailBody);
            var mailBodyResponse = ReadResponseLine(netStream);

            Assert.That(mailBodyResponse, Is.EqualTo(SmtpServerResponses.Ok));
        }
Пример #8
0
        private static void WriteRequest(MockNetworkStream stream_, string request_)
        {
            var bytes = Encoding.ASCII.GetBytes(request_);

            stream_.WriteRequest(bytes, 0, bytes.Length);
        }