Ejemplo n.º 1
0
        public bool ProcessResponse(SMTPTransaction transaction, string response, out string challenge)
        {
            if (string.IsNullOrEmpty(response))
            {
                challenge = null;
                return(false);
            }

            var parts = response.Split('\0');

            if (parts.Length != 3)
            {
                challenge = null;
                return(false);
            }

            var username = parts[1];
            var password = parts[2];

            transaction.SetProperty("Username", username, true);
            transaction.SetProperty("Password", password, true);

            challenge = null;

            // TODO
            // var password = response;
            // var username = transaction.GetProperty<string>("Username");

            return(true);
        }
Ejemplo n.º 2
0
        public void TestSetAndGetProperty(bool permanent)
        {
            using (ShimsContext.Create())
            {
                var server      = new ShimSMTPServer();
                var settings    = new StubIReceiveSettings();
                var transaction = new SMTPTransaction(server, settings);

                const int    value1 = 5;
                const string value2 = "foo";
                const bool   value3 = false;
                var          value4 = new object();

                transaction.SetProperty("foo", value1, permanent);
                var actualValue1 = transaction.GetProperty <int>("foo");
                Assert.Equal(value1, actualValue1);

                transaction.SetProperty("foo", value2, permanent);
                var actualValue2 = transaction.GetProperty <string>("foo");
                Assert.Equal(value2, actualValue2);

                transaction.SetProperty("foo", value3, permanent);
                var actualValue3 = transaction.GetProperty <bool>("foo");
                Assert.Equal(value3, actualValue3);

                transaction.SetProperty("foo", value4, permanent);
                var actualValue4 = transaction.GetProperty <object>("foo");
                Assert.Equal(value4, actualValue4);
            }
        }
Ejemplo n.º 3
0
        public void TestGetListProperty(bool permanent)
        {
            using (ShimsContext.Create())
            {
                var server      = new ShimSMTPServer();
                var settings    = new StubIReceiveSettings();
                var transaction = new SMTPTransaction(server, settings);

                var list = transaction.GetListProperty <string>("foo", permanent);

                Assert.NotNull(list);
                Assert.Empty(list);

                list.Add("fubar");

                list = transaction.GetListProperty <string>("foo", permanent);

                Assert.NotNull(list);
                Assert.Contains("fubar", list);

                transaction.SetProperty("foo", null, permanent);

                list = transaction.GetListProperty <string>("foo", permanent);

                Assert.NotNull(list);
                Assert.Empty(list);
            }
        }
Ejemplo n.º 4
0
        public override SMTPResponse DoExecute(SMTPTransaction transaction, string parameters)
        {
            if (transaction.GetProperty <bool>("MailInProgress"))
            {
                return(new SMTPResponse(SMTPStatusCode.BadSequence));
            }

            var match = FromRegex.Match(parameters);

            if (!match.Success)
            {
                return(new SMTPResponse(SMTPStatusCode.SyntaxError));
            }

            var path = match.Groups[1].Value.Equals("<>") ? MailPath.Empty : MailPath.FromMatch(match);

            transaction.SetProperty("ReversePath", path);
            transaction.SetProperty("MailInProgress", true);

            return(new SMTPResponse(SMTPStatusCode.Okay));
        }
Ejemplo n.º 5
0
        public void TestResetProperties()
        {
            using (ShimsContext.Create())
            {
                var server      = new ShimSMTPServer();
                var settings    = new StubIReceiveSettings();
                var transaction = new SMTPTransaction(server, settings);

                transaction.SetProperty("foo", "bar", false);
                transaction.SetProperty("foo2", "baz", true);
                Assert.True(transaction.HasProperty("foo"));
                Assert.Equal("bar", transaction.GetProperty <string>("foo"));
                Assert.True(transaction.HasProperty("foo2"));
                Assert.Equal("baz", transaction.GetProperty <string>("foo2"));

                transaction.Reset();
                Assert.False(transaction.HasProperty("foo"));
                Assert.Equal(default(string), transaction.GetProperty <string>("foo"));
                Assert.True(transaction.HasProperty("foo2"));
                Assert.Equal("baz", transaction.GetProperty <string>("foo2"));
            }
        }
Ejemplo n.º 6
0
        public void TestHasProperty()
        {
            using (ShimsContext.Create())
            {
                var server      = new ShimSMTPServer();
                var settings    = new StubIReceiveSettings();
                var transaction = new SMTPTransaction(server, settings);

                Assert.False(transaction.HasProperty("foo"));

                transaction.SetProperty("foo", 5, false);
                Assert.True(transaction.HasProperty("foo"));

                transaction.SetProperty("foo", null, false);
                Assert.False(transaction.HasProperty("foo"));

                transaction.SetProperty("foo", 5, true);
                Assert.True(transaction.HasProperty("foo"));

                transaction.SetProperty("foo", null, true);
                Assert.False(transaction.HasProperty("foo"));
            }
        }
Ejemplo n.º 7
0
        public void TestOvershadowingProperties()
        {
            using (ShimsContext.Create())
            {
                var server      = new ShimSMTPServer();
                var settings    = new StubIReceiveSettings();
                var transaction = new SMTPTransaction(server, settings);

                const string value1 = "bar";
                const string value2 = "baz";

                transaction.SetProperty("foo", value1, true);
                Assert.Equal(value1, transaction.GetProperty <string>("foo"));

                transaction.SetProperty("foo", value2, false);
                Assert.Equal(value2, transaction.GetProperty <string>("foo"));

                transaction.SetProperty("foo", null, false);
                Assert.Equal(value1, transaction.GetProperty <string>("foo"));

                transaction.SetProperty("foo", null, true);
                Assert.Equal(default(string), transaction.GetProperty <string>("foo"));
            }
        }
Ejemplo n.º 8
0
        public bool ProcessResponse(SMTPTransaction transaction, string response, out string challenge)
        {
            if (response == null)
            {
                challenge = "Username:"******"Username"))
            {
                transaction.SetProperty("Username", response, true);
                challenge = "Password:"******"Password", response, true);
            challenge = null;

            // TODO
            // var password = response;
            // var username = transaction.GetProperty<string>("Username");

            return(true);
        }
Ejemplo n.º 9
0
        public static SMTPResponse HandleResponse(SMTPTransaction transaction, string decodedReponse, IAuthMethod method)
        {
            string challenge;

            if (!method.ProcessResponse(transaction, decodedReponse, out challenge))
            {
                return(new SMTPResponse(SMTPStatusCode.AuthFailed, challenge != null ? new[] { challenge } : new string[0]));
            }

            if (challenge != null)
            {
                transaction.StartDataMode(DataLineHandler, s => DataHandler(transaction, s, method));

                return(new SMTPResponse(SMTPStatusCode.AuthContinue, Base64Encode(challenge)));
            }

            transaction.SetProperty("Authenticated", true, true);

            return(new SMTPResponse(SMTPStatusCode.AuthSuccess));
        }
Ejemplo n.º 10
0
 public void Abort(SMTPTransaction transaction)
 {
     transaction.SetProperty("Username", null, true);
     transaction.SetProperty("Password", null, true);
 }