Esempio n. 1
0
        public void TestDataHandler(string line)
        {
            using (ShimsContext.Create())
            {
                SMTPTransaction actualTransaction = null;
                string          decodedReponse    = null;
                IAuthMethod     actualAuthMethod  = null;
                var             expectedResponse  = new SMTPResponse(SMTPStatusCode.Okay);

                ShimAUTHHandler.HandleResponseSMTPTransactionStringIAuthMethod = (transaction, data, authMethod) =>
                {
                    actualTransaction = transaction;
                    decodedReponse    = data;
                    actualAuthMethod  = authMethod;

                    return(expectedResponse);
                };

                var method  = new StubIAuthMethod();
                var encoded = Convert.ToBase64String(Encoding.ASCII.GetBytes(line));

                var response = AUTHHandler.DataHandler(Transaction, encoded, method);

                Assert.Same(method, actualAuthMethod);
                Assert.Same(expectedResponse, response);
                Assert.Equal(Transaction, actualTransaction);
                Assert.Equal(line, decodedReponse);
            }
        }
Esempio n. 2
0
        private static bool Check_Response(Socket s, SMTPResponse response_expected)
        {
            string sResponse;
            int    response;

            byte[] bytes = new byte[2048];
            while (s.Available == 0)
            {
                System.Threading.Thread.Sleep(100);
            }

            int rcvd = s.Receive(bytes, 0, s.Available, SocketFlags.None);

            sResponse = Convert.ToBase64String(bytes, 0, rcvd);

            sResponse = RijndaelSimple.Decrypt(sResponse,
                                               passPhrase,
                                               saltValue,
                                               hashAlgorithm,
                                               passwordIterations,
                                               initVector,
                                               keySize);
            //MessageBox.Show(sResponse);
            response = Convert.ToInt32(sResponse.Substring(0, 3));
            if (response != (int)response_expected)
            {
                return(false);
            }
            return(true);
        }
Esempio n. 3
0
        private bool DoEHLO(out SMTPResponse reponse)
        {
            _stream.Write("EHLO {0}", ClientName);

            reponse = ReadResponse();

            return(reponse.Code == SMTPStatusCode.Okay);
        }
Esempio n. 4
0
        public void TestOnExecuteTrigger(bool overwriteReponse)
        {
            const string    expectedParams    = "fu bar";
            SMTPTransaction actualTransaction = null;
            string          actualParams      = null;
            var             responseA         = new SMTPResponse(SMTPStatusCode.Okay);
            var             responseB         = new SMTPResponse(SMTPStatusCode.TransactionFailed);
            var             expectedResponse  = overwriteReponse ? responseB : responseA;

            var handler = new StubCommandHandlerBase
            {
                DoExecuteSMTPTransactionString = (transaction, s) =>
                {
                    actualTransaction = transaction;
                    actualParams      = s;

                    return(responseA);
                }
            };

            object          actualSender     = null;
            string          eventParams      = null;
            SMTPTransaction eventTransaction = null;
            ICommandHandler eventHandler     = null;
            SMTPResponse    eventResponse    = null;

            handler.OnExecute += (sender, args) =>
            {
                actualSender     = sender;
                eventParams      = args.Parameters;
                eventTransaction = args.Transaction;
                eventHandler     = args.Handler;
                eventResponse    = args.Response;

                if (overwriteReponse)
                {
                    args.Response = responseB;
                }
            };

            var response = handler.Execute(Transaction, expectedParams);

            if (!overwriteReponse)
            {
                Assert.Equal(expectedParams, actualParams);
                Assert.Equal(Transaction, actualTransaction);
            }


            Assert.Same(expectedResponse, response);

            Assert.Same(handler, actualSender);
            Assert.Equal(Transaction, eventTransaction);
            Assert.Equal(expectedParams, eventParams);
            Assert.Equal(handler, eventHandler);
            Assert.Null(eventResponse);
        }
Esempio n. 5
0
        private async Task Write(SMTPResponse response)
        {
            if (_stream.CanWrite)
            {
                Log(LogEventType.Outgoing, response.ToString());

                await _writer.WriteLineAsync(response.ToString());

                _writer.Flush();
            }
        }
Esempio n. 6
0
        private static bool Check_Response(Socket s, SMTPResponse response_expected)
        {
            string sResponse;
            int    response;

            byte[] bytes = new byte[1024];
            try
            {
                s.Receive(bytes, 0, s.Available, SocketFlags.None);
                sResponse = Encoding.ASCII.GetString(bytes);
                response  = Convert.ToInt32(sResponse.Substring(0, 3));

                if (response != (int)response_expected)
                {
                    return(false);
                }
                return(true);
                //return !(s.Poll(1, SelectMode.SelectRead) && s.Available == 0);
                //if (!(s.Poll(1, SelectMode.SelectRead) && s.Available == 0))
                //{

                //    System.Threading.Thread.Sleep(100);

                //    s.Receive(bytes, 0, s.Available, SocketFlags.None);
                //    sResponse = Encoding.ASCII.GetString(bytes);
                //    response = BitConverter.ToInt32(bytes, 0);
                //    if (response != (int)response_expected)
                //    {
                //        return false;

                //    }
                //    else
                //    {
                //        return true;
                //    }

                //}
                //else
                //{
                //    return false;
                //}
            }
            catch (SocketException) { return(false); }
        }
Esempio n. 7
0
        private bool Check_Response(Socket s, SMTPResponse response_expected, out string sResponse)
        {
            byte[] bytes = new byte[1024];
            while (s.Available == 0)
            {
                System.Threading.Thread.Sleep(100);
            }

            int intCount = s.Receive(bytes, 0, s.Available, SocketFlags.None);

            sResponse = Encoding.ASCII.GetString(bytes, 0, intCount);
            int response = Convert.ToInt32(sResponse.Substring(0, 3));

            if (response != (int)response_expected)
            {
                return(false);
            }
            return(true);
        }
Esempio n. 8
0
        public void TestExecuteSuccess()
        {
            const string command          = "Test";
            var          expectedResponse = new SMTPResponse(SMTPStatusCode.NotAvailiable, "Fu", "bar");
            const string expectedParams   = "Fubar blubb";

            using (ShimsContext.Create())
            {
                var server      = new ShimSMTPServer();
                var settings    = new StubIReceiveSettings();
                var transaction = new SMTPTransaction(server, settings);

                SMTPTransaction actualTransaction = null;
                string          actualParams      = null;

                var handler = new StubICommandHandler
                {
                    ExecuteSMTPTransactionString = (smtpTransaction, s) =>
                    {
                        actualTransaction = smtpTransaction;
                        actualParams      = s;

                        return(expectedResponse);
                    }
                };

                server.GetHandlerString = s =>
                {
                    if (s.Equals(command, StringComparison.InvariantCultureIgnoreCase))
                    {
                        return(handler);
                    }
                    throw new InvalidOperationException("Invalid name.");
                };

                var response = transaction.ExecuteCommand(new SMTPCommand(command, expectedParams));

                Assert.Same(expectedResponse, response);
                Assert.Equal(expectedParams, actualParams);
                Assert.Same(transaction, actualTransaction);
            }
        }
Esempio n. 9
0
        private static bool Check_Response_old(Socket s, SMTPResponse response_expected)
        {
            string sResponse;
            int    response;

            byte[] bytes = new byte[2048];
            while (s.Available == 0)
            {
                System.Threading.Thread.Sleep(100);
            }

            s.Receive(bytes, 0, s.Available, SocketFlags.None);
            sResponse = Encoding.ASCII.GetString(bytes, 0, (int)bytes.Length);
            //MessageBox.Show(sResponse);
            response = Convert.ToInt32(sResponse.Substring(0, 3));
            if (response != (int)response_expected)
            {
                return(false);
            }
            return(true);
        }
Esempio n. 10
0
        private static bool CheckResponse(Socket s, SMTPResponse responseExpected)
        {
            byte[] bytes = new byte[1024];
            int    count = 0;

            while (s.Available == 0)
            {
                // Make sure it would work fine with SMTP being not-accessible
                if (++count >= 50)
                {
                    return(true);
                }
                Thread.Sleep(100);
            }

            s.Receive(bytes, 0, s.Available, SocketFlags.None);
            string sResponse = Encoding.ASCII.GetString(bytes);
            int    response  = Convert.ToInt32(sResponse.Substring(0, 3));

            return(response == (int)responseExpected);
        }
Esempio n. 11
0
        public SMTPTransaction StartTransaction(IPAddress address, IReceiveSettings settings, out SMTPResponse response)
        {
            var transaction = new SMTPTransaction(this, settings);

            if (OnConnect != null)
            {
                var args = new ConnectEventArgs(address);
                OnConnect(transaction, args);

                if (args.Cancel)
                {
                    response = new SMTPResponse(args.ResponseCode ?? SMTPStatusCode.TransactionFailed);
                    transaction.Close();
                    return(transaction);
                }
            }

            response = new SMTPResponse(SMTPStatusCode.Ready, settings.Banner);

            return(transaction);
        }
Esempio n. 12
0
        public void TestExecutSuccess(string parameters, string expectedDecoded)
        {
            using (ShimsContext.Create())
            {
                AddTransactionProperty("Authenticated", false);
                AddCoreListProperty("EHLOLines", new List <Func <SMTPTransaction, string> >());

                SMTPTransaction actualTransaction = null;
                string          decodedReponse    = null;
                IAuthMethod     actualAuthMethod  = null;
                var             expectedResponse  = new SMTPResponse(SMTPStatusCode.Okay);

                ShimAUTHHandler.HandleResponseSMTPTransactionStringIAuthMethod = (transaction, data, authMethod) =>
                {
                    actualTransaction = transaction;
                    decodedReponse    = data;
                    actualAuthMethod  = authMethod;

                    return(expectedResponse);
                };

                var method = new StubIAuthMethod();
                var loader = new StubIAuthMethodLoader
                {
                    GetModules =
                        () => new List <Tuple <string, IAuthMethod> > {
                        new Tuple <string, IAuthMethod>("TEST", method)
                    }
                };

                var handler = new AUTHHandler(loader);
                handler.Initialize(Core);
                var response = handler.DoExecute(Transaction, parameters);

                Assert.Same(method, actualAuthMethod);
                Assert.Same(expectedResponse, response);
                Assert.Equal(Transaction, actualTransaction);
                Assert.Equal(expectedDecoded, decodedReponse);
            }
        }
Esempio n. 13
0
        public void TestDataHandling(bool expectedResult)
        {
            using (ShimsContext.Create())
            {
                var server      = new ShimSMTPServer();
                var settings    = new StubIReceiveSettings();
                var transaction = new SMTPTransaction(server, settings);

                Assert.False(transaction.InDataMode);

                const string  expectedData          = "Some data";
                string        actualData            = null;
                const string  expectedLine          = "Some line";
                string        actualLine            = null;
                var           expectedStringBuilder = new StringBuilder();
                StringBuilder actualStringBuilder   = null;
                var           expectedResponse      = new SMTPResponse(SMTPStatusCode.Okay);

                transaction.StartDataMode((line, builder) =>
                {
                    actualLine          = line;
                    actualStringBuilder = builder;
                    return(expectedResult);
                }, data =>
                {
                    actualData = data;
                    return(expectedResponse);
                });

                var actualResult   = transaction.HandleDataLine(expectedLine, expectedStringBuilder);
                var actualResponse = transaction.HandleData(expectedData);

                Assert.Equal(expectedResult, actualResult);
                Assert.Same(expectedResponse, actualResponse);
                Assert.Same(expectedStringBuilder, actualStringBuilder);
                Assert.Equal(expectedData, actualData);
                Assert.Equal(expectedLine, actualLine);
            }
        }
Esempio n. 14
0
        private static bool Check_Response(StreamReader netStream, SMTPResponse response_expected)
        {
            int response;

            int read = 0;

            StringBuilder sResponse = new StringBuilder();

            do
            {
                char[] buffer = new char[1024];
                read = netStream.Read(buffer, 0, buffer.Length);
                sResponse.Append(buffer);
            }while (read == 1024);

            response = Convert.ToInt32(sResponse.ToString().Substring(0, 3));

            if (response != (int)response_expected)
            {
                return(false);
            }
            return(true);
        }
Esempio n. 15
0
        private bool checkResponse(Socket s, SMTPResponse response_expected)
        {
            try {
                string sResponse;
                int    response;
                byte[] bytes = new byte[1024];
//			while (s.Available==0) {
//				System.Threading.Thread.Sleep(100);
//			}
                int len = s.Receive(bytes, SocketFlags.None);
                sResponse = Encoding.ASCII.GetString(bytes, 0, len);
                response  = Convert.ToInt32(sResponse.Substring(0, 3));
                if (response != (int)response_expected)
                {
                    return(false);
                }
                else
                {
                    return(true);
                }
            } catch (Exception e) {
                throw(e);
            }
        }
Esempio n. 16
0
        private static bool Check_Response(Socket s, SMTPResponse response_expected)
        {
            string sResponse;
            int    response;

            byte[] bytes = new byte[1024];
            while (s.Available == 0)
            {
                System.Threading.Thread.Sleep(100);
            }

            s.Receive(bytes, 0, s.Available, SocketFlags.None);
            sResponse = Encoding.ASCII.GetString(bytes);

            sResponse = sResponse.Replace("\0", "");
            output.Append(sResponse);

            response = Convert.ToInt32(sResponse.Substring(0, 3));
            if (response != (int)response_expected)
            {
                return(false);
            }
            return(true);
        }
Esempio n. 17
0
 private bool Check_Response(Socket s, SMTPResponse response_expected)
 {
   return Check_Response(s, response_expected, -1);
 }
Esempio n. 18
0
        private static bool Check_Response_old(Socket s, SMTPResponse response_expected)
        {
            string sResponse;
            int response;
            byte[] bytes = new byte[2048];
            while (s.Available == 0)
            {
                System.Threading.Thread.Sleep(100);
            }

            s.Receive(bytes, 0, s.Available, SocketFlags.None);
            sResponse = Encoding.ASCII.GetString(bytes, 0, (int)bytes.Length);
            //MessageBox.Show(sResponse);
            response = Convert.ToInt32(sResponse.Substring(0, 3));
            if (response != (int)response_expected)
                return false;
            return true;
        }
Esempio n. 19
0
        private static bool Check_Response(Socket s, SMTPResponse response_expected)
        {
            string sResponse;
            int response;
            byte[] bytes = new byte[2048];
            while (s.Available == 0)
            {
                System.Threading.Thread.Sleep(100);
            }

            int rcvd = s.Receive(bytes, 0, s.Available, SocketFlags.None);
            sResponse = Convert.ToBase64String(bytes, 0, rcvd);

            sResponse = RijndaelSimple.Decrypt(sResponse,
                                                    passPhrase,
                                                    saltValue,
                                                    hashAlgorithm,
                                                    passwordIterations,
                                                    initVector,
                                                    keySize);
            //MessageBox.Show(sResponse);
            response = Convert.ToInt32(sResponse.Substring(0, 3));
            if (response != (int)response_expected)
                return false;
            return true;
        }
Esempio n. 20
0
        public void TestToString(SMTPStatusCode code, string[] args, string expected)
        {
            var result = new SMTPResponse(code, args).ToString();

            Assert.Equal(expected, result);
        }
Esempio n. 21
0
        private static bool Check_Response(Socket s, SMTPResponse response_expected)
        {
            string sResponse;
            int response;
            byte[] bytes = new byte[1024];
            try
            {
                s.Receive(bytes, 0, s.Available, SocketFlags.None);
                 sResponse = Encoding.ASCII.GetString(bytes);
                 response = Convert.ToInt32(sResponse.Substring(0, 3));

                 if (response != (int)response_expected)
                     return false;
                 return true;
                //return !(s.Poll(1, SelectMode.SelectRead) && s.Available == 0);
                //if (!(s.Poll(1, SelectMode.SelectRead) && s.Available == 0))
                //{

                //    System.Threading.Thread.Sleep(100);

                //    s.Receive(bytes, 0, s.Available, SocketFlags.None);
                //    sResponse = Encoding.ASCII.GetString(bytes);
                //    response = BitConverter.ToInt32(bytes, 0);
                //    if (response != (int)response_expected)
                //    {
                //        return false;

                //    }
                //    else
                //    {
                //        return true;
                //    }

                //}
                //else
                //{
                //    return false;
                //}

            }
            catch (SocketException) { return false; }
        }
Esempio n. 22
0
    // I was seeing my SMTP server hang waiting for a response to the QUIT, so I added
    // this timeout. Pass -1 to wait forever. Timeout is in milliseconds. 
    private bool Check_Response(Socket s, SMTPResponse response_expected, int timeout)
    {
      string sResponse;
      int response;
      byte[] bytes = new byte[1024];

      int start = Environment.TickCount; 
      while (s.Available==0)
      {
        System.Threading.Thread.Sleep(101);

        if (timeout != -1)
        {
          if ((Environment.TickCount - start) > timeout)
          {
            // We timed out
            return false; 
          }
        }
      }

    			
      s.Receive(bytes, 0, s.Available, SocketFlags.None);
      sResponse = Encoding.ASCII.GetString(bytes);
      if (sw != null)
        sw.WriteLine(">>{0}", sResponse);
      response = Convert.ToInt32(sResponse.Substring(0,3));
      if(response != (int)response_expected)
        return false;
      return true;
    }
Esempio n. 23
0
        private static String Check_Response(Socket s, SMTPResponse eResponseExpectedParam)
        {
            string sResponse;
            int iResponse;
            byte[] bytes = new byte[1024];
            while (s.Available == 0)
                System.Threading.Thread.Sleep(100);

            s.Receive(bytes, 0, s.Available, SocketFlags.None);
            sResponse = Encoding.ASCII.GetString(bytes);
            iResponse = Convert.ToInt32(sResponse.Substring(0, 3));
            if (iResponse != (int)eResponseExpectedParam)
                return sResponse;

            return "";
        }
Esempio n. 24
0
        public void CreateDefect(V1Connector CurrentV1Connection, List <Message> CurrentEmails)
        {
            V1XMLHelper  V1XML        = new V1XMLHelper();
            V1Logging    Logs         = new V1Logging();
            string       ProjectScope = "Something for Testing";
            string       EmailBody;
            MessagePart  MessageParts;
            SMTPResponse V1Response = new SMTPResponse();
            MailChecker  MailCheck  = new MailChecker();

            IServices services = new Services(CurrentV1Connection);


            try
            {
                foreach (Message MailItem in CurrentEmails)
                {
                    for (int ToCtr = 0; ToCtr < MailItem.Headers.To.Count; ToCtr++)
                    {
                        ProjectScope = V1XML.GetProjectScope(MailItem.Headers.To[ToCtr].Address);
                        if (ProjectScope != null)
                        {
                            Oid                  projectId     = services.GetOid(ProjectScope);
                            IAssetType           defectType    = services.Meta.GetAssetType("Defect");
                            Asset                NewDefect     = services.New(defectType, projectId);
                            IAttributeDefinition nameAttribute = defectType.GetAttributeDefinition("Name");
                            NewDefect.SetAttributeValue(nameAttribute, MailItem.Headers.Subject.ToString());

                            MessageParts = MailItem.FindFirstHtmlVersion();
                            if (MessageParts == null)
                            {
                                MessageParts = MailItem.FindFirstPlainTextVersion();
                            }
                            EmailBody = MessageParts.GetBodyAsText();

                            Logs.LogEvent("Operation - Creating Defect for " + MailItem.Headers.To[ToCtr].Address);

                            IAttributeDefinition descriptionAttribute = defectType.GetAttributeDefinition("Description");
                            NewDefect.SetAttributeValue(descriptionAttribute, EmailBody);
                            IAttributeDefinition FoundByAttribute = defectType.GetAttributeDefinition("FoundBy");
                            NewDefect.SetAttributeValue(FoundByAttribute, MailItem.Headers.From.ToString());
                            services.Save(NewDefect);

                            IAttributeDefinition DefectIDAttribute = defectType.GetAttributeDefinition("Number");
                            Query IDQuery = new Query(NewDefect.Oid);
                            IDQuery.Selection.Add(DefectIDAttribute);
                            QueryResult ResultID = services.Retrieve(IDQuery);
                            Asset       defect   = ResultID.Assets[0];

                            //NewDefect.GetAttribute(DefectIDAttribute).Value
                            Logs.LogEvent("Operation - Sending Response to Defect Sender.");
                            //Commented out the Response back to the Sender per John Waedekin
                            //V1Response.SendResponse(MailItem, defect.GetAttribute(DefectIDAttribute).Value + " " + NewDefect.GetAttribute(nameAttribute).Value, ProjectScope);
                            MailCheck.DeleteMessagesOnServer(MailItem, ProjectScope);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Logs.LogEvent("ERROR - Creating Defect - " + ex.InnerException.Message);
            }
        }