public void TestFilterCertificateByPolicy_notCompliant_assertNoCertsFiltered()
        {
            string            configPath = GetSettingsPath("TestSmtpAgentConfigWithCertPolicy.xml");
            SmtpAgentSettings settings   = SmtpAgentSettings.LoadSettings(configPath);

            CleanMessages(settings);
            CleanMonitor();

            SetPolicyTestSettings(settings);

            //
            // Mock all policy resolvers
            //
            Mock <PrivatePolicyResolver> mockPrivatePolicyResolver;
            Mock <PublicPolicyResolver>  mockPublicPolicyResolver;
            Mock <TrustPolicyResolver>   mockTrustPolicyResolver;
            Mock <IPolicyFilter>         mockPolicyFilter;

            MockPolicyResolvers(settings, out mockPrivatePolicyResolver, out mockPublicPolicyResolver, out mockTrustPolicyResolver, out mockPolicyFilter);

            Mock <IPolicyExpression> policyExpression = new Mock <IPolicyExpression>();

            mockPrivatePolicyResolver.Setup(r => r.GetIncomingPolicy(It.IsAny <MailAddress>()))
            .Returns(new List <IPolicyExpression>()
            {
                policyExpression.Object
            });
            mockPolicyFilter.Setup(r => r.IsCompliant(It.IsAny <X509Certificate2>(), It.IsAny <IPolicyExpression>()))
            .Returns(false);

            SmtpAgent smtpAgent = SmtpAgentFactory.Create(settings);

            //
            // Process loopback messages.  Leaves message in bad message folder
            // Go ahead and pick them up and Process them as if they where being handled
            // by the SmtpAgent by way of (IIS)SMTP hand off.
            //
            var sendingMessage = LoadMessage(TestMessage);

            Assert.Throws <AgentException>(() => RunEndToEndTest(sendingMessage, smtpAgent));

            Assert.Equal(1, BadMessages().Count());

            mockPrivatePolicyResolver.Verify(r => r.GetOutgoingPolicy(new MailAddress("*****@*****.**")), Times.Exactly(1));
            mockPolicyFilter.Verify(p => p.IsCompliant(It.IsAny <X509Certificate2>(), It.IsAny <IPolicyExpression>()), Times.Exactly(1));
            //
            // These two are never called.  These code paths do not exist.
            //
            mockPublicPolicyResolver.Verify(r => r.GetIncomingPolicy(It.IsAny <MailAddress>()), Times.Never());
            mockTrustPolicyResolver.Verify(r => r.GetOutgoingPolicy(It.IsAny <MailAddress>()), Times.Never());
        }
Exemple #2
0
        public void Test_PluginRouter_Under_Parallel_Load()
        {
            string configPath = GetSettingsPath("TestReceiverPlugin.xml");
            var    settings   = SmtpAgentSettings.LoadSettings(configPath);

            settings.InternalMessage.PickupFolder = TestPickupFolder;

            //
            // Create the SmtpAgent.  This is the adapter between IIS SMTP and the DirectAgent (security and trust code)
            //
            m_agent = SmtpAgentFactory.Create(settings);
            CleanMessages(m_agent.Settings);

            //
            // Mocks use the AddressMemoryStore
            //
            AddressMemoryStore.Clear();
            AddressMemoryStore.AddRange(new Address[]
            {
                new Address {
                    EmailAddress = "*****@*****.**", Status = EntityStatus.Enabled, Type = "STUB"
                }
            });

            var mockAddressClientSettings = MockAddressClientSettings();

            m_agent.Settings.AddressManager = mockAddressClientSettings.Object;


            DirectAgent agentA = new DirectAgent("redmond.hsgincubator.com");

            Parallel.For(0, 50, new ParallelOptions {
                MaxDegreeOfParallelism = 10
            }, i =>
            {
                var message = string.Format(TestMessageLoad, Guid.NewGuid().ToString("N"), i);

                //
                // Prep and encrypted message.
                //
                var outMessage = agentA.ProcessOutgoing(message).SerializeMessage();
                var cdoMessage = new CDOSmtpMessage(base.LoadMessage(outMessage));

                m_agent.ProcessMessage(cdoMessage);
            });


            Assert.Equal(49, Directory.GetFiles(TestIncomingFolder).Length);
            Assert.Equal(1, Directory.GetFiles(TestPickupFolder).Length);
        }
Exemple #3
0
        public void TestEndToEndInternalMessage()
        {
            string            configPath = GetSettingsPath("TestSmtpAgentAuditConfig.xml");
            SmtpAgentSettings settings   = SmtpAgentSettings.LoadSettings(configPath);

            m_agent = SmtpAgentFactory.Create(settings);

            Assert.True(IoC.Resolve <IAuditor <IBuildAuditLogMessage> >() != null);
            Assert.Equal(0, AuditEventCount);
            m_agent.Settings.InternalMessage.EnableRelay = true;
            Assert.Null(Record.Exception(() => RunEndToEndTest(this.LoadMessage(string.Format(TestMessage, Guid.NewGuid())))));
            Assert.Null(Record.Exception(() => RunEndToEndTest(this.LoadMessage(CrossDomainMessage))));
            m_agent.Settings.InternalMessage.EnableRelay = false;
            Assert.Equal(4, AuditEventCount);
        }
Exemple #4
0
        public void TestBad_DatabaseAuditorSettings()
        {
            string            configPath = GetSettingsPath("TestSmtpAgentAuditConfig_BadAuditor_Defaults.xml");
            SmtpAgentSettings settings   = SmtpAgentSettings.LoadSettings(configPath);

            m_agent = SmtpAgentFactory.Create(settings);

            Assert.Equal(0, AuditEventCount);
            m_agent.Settings.InternalMessage.EnableRelay = true;
            //
            // Failes to load DatabaseAuditor.Auditor and defaults to EventLogAuditor
            //
            Assert.True(IoC.Resolve <IAuditor <IBuildAuditLogMessage> >() is SmtpAgentEventLogAuditor);
            Assert.Null(Record.Exception(() => RunEndToEndTest(this.LoadMessage(string.Format(TestMessage, Guid.NewGuid())))));
            m_agent.Settings.InternalMessage.EnableRelay = false;
            Assert.Equal(0, AuditEventCount);
        }
Exemple #5
0
        public void TestContainer()
        {
            SmtpAgentSettings settings = null;

            Assert.DoesNotThrow(() => settings = SmtpAgentSettings.LoadSettings(Fullpath("TestPlugin.xml")));
            Assert.NotNull(settings.Container);
            Assert.True(settings.Container.HasComponents);

            ResetSmtpAgentFactory();
            SmtpAgent agent = SmtpAgentFactory.Create(Fullpath("TestPlugin.xml"));

            ILogFactory logFactory = null;

            Assert.DoesNotThrow(() => logFactory = IoC.Resolve <ILogFactory>());

            IAuditor auditor = null;

            Assert.DoesNotThrow(() => auditor = IoC.Resolve <IAuditor>());
            Assert.True(auditor is DummyAuditor);
        }
Exemple #6
0
        public void Test_DatabaseAuditor_FullAuditMessageBuilder()
        {
            string            configPath = GetSettingsPath("TestSmtpAgentAuditConfig.xml");
            SmtpAgentSettings settings   = SmtpAgentSettings.LoadSettings(configPath);

            SimpleComponentSettings[] components = new SimpleComponentSettings[1];


            SimpleComponentSettings localAuditComponent = new SimpleComponentSettings();

            localAuditComponent.Scope   = InstanceScope.Singleton;
            localAuditComponent.Service = "Health.Direct.SmtpAgent.Diagnostics.IAuditor`1[[Health.Direct.SmtpAgent.Diagnostics.IBuildAuditLogMessage, Health.Direct.SmtpAgent]], Health.Direct.SmtpAgent";
            localAuditComponent.Type    = "Health.Direct.DatabaseAuditor.Auditor`1[[Health.Direct.DatabaseAuditor.FullAuditMessageBuilder, Health.Direct.DatabaseAuditor]], Health.Direct.DatabaseAuditor";
            components[0] = localAuditComponent;

            settings.Container.Components = components;
            m_agent = SmtpAgentFactory.Create(settings);


            var auditor = IoC.Resolve <IAuditor <IBuildAuditLogMessage> >();

            Assert.True(auditor is DatabaseAuditor.Auditor <FullAuditMessageBuilder>);

            Assert.Equal(0, AuditEventCount);
            m_agent.Settings.InternalMessage.EnableRelay = true;
            Assert.Null(Record.Exception(() => RunEndToEndTest(this.LoadMessage(string.Format(TestMessage, Guid.NewGuid())))));
            Assert.Null(Record.Exception(() => RunEndToEndTest(this.LoadMessage(CrossDomainMessage))));
            m_agent.Settings.InternalMessage.EnableRelay = false;
            Assert.Equal(4, AuditEventCount);

            using (var db = new AuditContext().CreateContext(m_settings))
            {
                foreach (AuditEvent auditEvent in db.AuditEvents)
                {
                    Console.WriteLine(auditEvent.Message);
                }
            }
        }
Exemple #7
0
        public void TestUnavailableTokenSettingsIncoming()
        {
            var agent     = SmtpAgentFactory.Create(GetSettingsPath("TestSmtpAgentConfigService_HsmCryptographer.xml"));
            var messageId = Guid.NewGuid();
            var message   = LoadMessage(string.Format(TestMessageDualToHsm, messageId));

            agent.ProcessMessage(message);

            //nothing in bad folder
            Assert.False(FileMessages(agent.Settings.BadMessage.CopyFolder)
                         .Select(File.ReadAllText)
                         .Any(m => m.Contains(messageId.ToString())));

            agent   = SmtpAgentFactory.Create(GetSettingsPath("TestSmtpAgentConfigService_HsmCryptographerBadUrl.xml"));
            message = LoadMessage(message);
            VerifyOutgoingMessage(message);
            Assert.Throws <SmtpAgentSettingsException>(() => agent.ProcessMessage(message));

            //message in bad folder
            Assert.True(FileMessages(agent.Settings.BadMessage.CopyFolder)
                        .Select(File.ReadAllText)
                        .Any(m => m.Contains(messageId.ToString())));
        }
Exemple #8
0
        public void TestMissing_DatabaseAuditorSettings()
        {
            string            configPath = GetSettingsPath("TestSmtpAgentAuditConfig.xml");
            SmtpAgentSettings settings   = SmtpAgentSettings.LoadSettings(configPath);

            SimpleComponentSettings[]      components         = new SimpleComponentSettings[1];
            Mock <SimpleComponentSettings> auditComponentMock = new Mock <SimpleComponentSettings>()
            {
                CallBase = true
            };

            auditComponentMock.SetupAllProperties();
            components[0]                 = auditComponentMock.Object;
            components[0].Scope           = InstanceScope.Singleton;
            components[0].Service         = "Health.Direct.SmtpAgent.Diagnostics.IAuditor`1[[Health.Direct.SmtpAgent.Diagnostics.IBuildAuditLogMessage, Health.Direct.SmtpAgent]], Health.Direct.SmtpAgent";
            components[0].Type            = "Health.Direct.SmtpAgent.Tests.LocalTestAuditorSettingsMissing`1[[Health.Direct.SmtpAgent.Tests.LocalBuildAuditLogMessage, Health.Direct.SmtpAgent.Tests]], Health.Direct.SmtpAgent.Tests";
            settings.Container.Components = components;


            m_agent = SmtpAgentFactory.Create(settings);

            //
            // Not really asserting the exception.  It would take some architecture changes to get the IoC in a more testable state.
            //
            auditComponentMock.Verify(c => c.CreateInstance(), Times.Once);

            Assert.Equal(0, AuditEventCount);
            m_agent.Settings.InternalMessage.EnableRelay = true;

            //
            // Failes to find connection string info in DatabaseAuditorSettings.xml file so loads default EventLogAuditor
            //
            Assert.True(IoC.Resolve <IAuditor <IBuildAuditLogMessage> >() is SmtpAgentEventLogAuditor);
            Assert.DoesNotThrow(() => RunEndToEndTest(this.LoadMessage(string.Format(TestMessage, Guid.NewGuid()))));
            m_agent.Settings.InternalMessage.EnableRelay = false;
            Assert.Equal(0, AuditEventCount);
        }
 public TestSmtpAgentDSNs()
 {
     m_agent = SmtpAgentFactory.Create(GetSettingsPath("TestSmtpAgentConfig.xml"));
 }
        public void TestFilterCertificateByPolicy_outgoingPolicyCompliant_assertNoCertsFiltered()
        {
            string            configPath = GetSettingsPath("TestSmtpAgentConfigWithCertPolicy.xml");
            SmtpAgentSettings settings   = SmtpAgentSettings.LoadSettings(configPath);

            CleanMessages(settings);
            CleanMonitor();

            SetPolicyTestSettings(settings);

            //
            // Mock all policy resolvers
            //
            Mock <PrivatePolicyResolver> mockPrivatePolicyResolver;
            Mock <PublicPolicyResolver>  mockPublicPolicyResolver;
            Mock <TrustPolicyResolver>   mockTrustPolicyResolver;
            Mock <IPolicyFilter>         mockPolicyFilter;

            MockPolicyResolvers(settings, out mockPrivatePolicyResolver, out mockPublicPolicyResolver, out mockTrustPolicyResolver, out mockPolicyFilter);

            Mock <IPolicyExpression> policyExpression = new Mock <IPolicyExpression>();

            mockPrivatePolicyResolver.Setup(r => r.GetIncomingPolicy(It.IsAny <MailAddress>())).Returns(new List <IPolicyExpression>()
            {
                policyExpression.Object
            });
            mockPolicyFilter.Setup(r => r.IsCompliant(It.IsAny <X509Certificate2>(), It.IsAny <IPolicyExpression>())).Returns(true);

            SmtpAgent smtpAgent = SmtpAgentFactory.Create(settings);

            //
            // Process loopback messages.  Leaves un-encrypted mdns in pickup folder
            // Go ahead and pick them up and Process them as if they where being handled
            // by the SmtpAgent by way of (IIS)SMTP hand off.
            //
            var sendingMessage = LoadMessage(TestMessage);

            Assert.Null(Record.Exception(() => RunEndToEndTest(sendingMessage, smtpAgent)));

            //
            // grab the clear text mdns and delete others.
            //
            bool foundMdns = false;

            foreach (var pickupMessage in PickupMessages())
            {
                string messageText = File.ReadAllText(pickupMessage);
                if (messageText.Contains("disposition-notification"))
                {
                    foundMdns = true;
                    Assert.Null(Record.Exception(() => RunMdnOutBoundProcessingTest(LoadMessage(messageText), smtpAgent)));
                }
            }
            Assert.True(foundMdns);

            mockPublicPolicyResolver.Verify(r => r.GetOutgoingPolicy(new MailAddress("*****@*****.**")), Times.Exactly(1));
            mockPublicPolicyResolver.Verify(r => r.GetOutgoingPolicy(new MailAddress("*****@*****.**")), Times.Exactly(1));
            mockPolicyFilter.Verify(p => p.IsCompliant(It.IsAny <X509Certificate2>(), It.IsAny <IPolicyExpression>()), Times.Exactly(2));
            //
            // These two are never called.  These code paths do not exist.
            //
            mockPublicPolicyResolver.Verify(r => r.GetIncomingPolicy(It.IsAny <MailAddress>()), Times.Never());
            mockTrustPolicyResolver.Verify(r => r.GetOutgoingPolicy(It.IsAny <MailAddress>()), Times.Never());
        }
Exemple #11
0
 public TestSmtpAgentMdns()
 {
     m_agent = SmtpAgentFactory.Create(GetSettingsPath("TestSmtpAgentConfigService_HsmCryptographer.xml"));
 }
Exemple #12
0
 public TestSmtpAgentMDNs()
 {
     //m_agent = SmtpAgentFactory.Create(base.GetSettingsPath("TestSmtpAgentConfigService.xml"));
     //m_agent = SmtpAgentFactory.Create(base.GetSettingsPath("TestSmtpAgentConfigServiceProd.xml"));
     m_agent = SmtpAgentFactory.Create(GetSettingsPath("TestSmtpAgentConfig.xml"));
 }
Exemple #13
0
 SmtpAgent LoadAgent(string configFile)
 {
     return(SmtpAgentFactory.Create(GetSettingsPath(configFile)));
 }
Exemple #14
0
 public TestNotifications()
 {
     m_agent    = SmtpAgentFactory.Create(MakeFilePath("SmtpAgentTestFiles\\TestSmtpAgentConfig.xml"));
     m_producer = new NotificationProducer(m_agent.Settings.Notifications);
 }