Search in sources :

Example 31 with MimeMessage

use of javax.mail.internet.MimeMessage in project nhin-d by DirectProject.

the class NHINDSecurityAndTrustMailet_functionalTest method testProcessOutgoingMessageEndToEnd_tamperedRoutingHeaders_rejectPolicyOn_assertRejected.

public void testProcessOutgoingMessageEndToEnd_tamperedRoutingHeaders_rejectPolicyOn_assertRejected() throws Exception {
    new TestPlan() {

        protected String getMessageToProcess() throws Exception {
            return TestUtils.readMessageResource("PlainOutgoingMessage.txt");
        }

        @Override
        protected Mailet getMailet(String configurationFileName) throws Exception {
            Mailet retVal = null;
            String configfile = TestUtils.getTestConfigFile(configurationFileName);
            Map<String, String> params = new HashMap<String, String>();
            if (configurationFileName.startsWith("http"))
                params.put("ConfigURL", ConfigServiceRunner.getConfigServiceURL());
            else
                params.put("ConfigURL", "file://" + configfile);
            params.put(SecurityAndTrustMailetOptions.REJECT_ON_ROUTING_TAMPER, "true");
            retVal = new NHINDSecurityAndTrustMailet();
            MailetConfig mailetConfig = new MockMailetConfig(params, "NHINDSecurityAndTrustMailet");
            retVal.init(mailetConfig);
            return retVal;
        }

        protected void performInner() throws Exception {
            // encrypt
            String originalMessage = getMessageToProcess();
            MimeMessage msg = EntitySerializer.Default.deserialize(originalMessage);
            // add an MDN request
            msg.setHeader(MDNStandard.Headers.DispositionNotificationTo, msg.getHeader(MailStandard.Headers.From, ","));
            MockMail theMessage = new MockMail(msg);
            Mailet theMailet = getMailet("ValidConfig.xml");
            theMailet.service(theMessage);
            assertNotNull(theMessage);
            assertNotNull(theMessage.getMessage());
            msg = theMessage.getMessage();
            assertTrue(SMIMEStandard.isEncrypted(msg));
            assertEquals(theMessage.getState(), Mail.TRANSPORT);
            // decrypt
            theMailet = getMailet("ValidConfigStateLine.txt");
            theMessage = new MockMail(msg);
            final MailAddress validAddress = new MailAddress(msg.getRecipients(RecipientType.TO)[0].toString());
            final MailAddress injectedAttackAddress = new MailAddress("externUser2@starugh-stateline.com");
            theMessage.setRecipients(Arrays.asList(validAddress, injectedAttackAddress));
            theMailet.service(theMessage);
            // rejected and ghosted
            assertEquals(Mail.GHOST, theMessage.getState());
        }
    }.perform();
}
Also used : MailAddress(org.apache.mailet.MailAddress) BaseTestPlan(org.nhindirect.gateway.testutils.BaseTestPlan) NHINDSecurityAndTrustMailet(org.nhindirect.gateway.smtp.james.mailet.NHINDSecurityAndTrustMailet) MailetConfig(org.apache.mailet.MailetConfig) NHINDSecurityAndTrustMailet(org.nhindirect.gateway.smtp.james.mailet.NHINDSecurityAndTrustMailet) Mailet(org.apache.mailet.Mailet) MimeMessage(javax.mail.internet.MimeMessage) HashMap(java.util.HashMap) Map(java.util.Map)

Example 32 with MimeMessage

use of javax.mail.internet.MimeMessage in project nhin-d by DirectProject.

the class NHINDSecurityAndTrustMailet_service_Test method testService_UseRcpt_AssertRecipientsUsed.

public void testService_UseRcpt_AssertRecipientsUsed() throws Exception {
    final MimeMessage mimeMsg = EntitySerializer.Default.deserialize(TestUtils.readMessageResource("PlainOutgoingMessage.txt"));
    final SmtpAgent mockAgent = mock(SmtpAgent.class);
    when(mockAgent.processMessage((MimeMessage) any(), (NHINDAddressCollection) any(), (NHINDAddress) any())).thenAnswer(new Answer<MessageProcessResult>() {

        public MessageProcessResult answer(InvocationOnMock invocation) throws Throwable {
            usedRecipients = (NHINDAddressCollection) invocation.getArguments()[1];
            usedSender = (NHINDAddress) invocation.getArguments()[2];
            return new MessageProcessResult(new DefaultMessageEnvelope(new Message(mimeMsg), usedRecipients, usedSender), null);
        }
    });
    final Mail mockMail = mock(MockMail.class, CALLS_REAL_METHODS);
    when(mockMail.getRecipients()).thenReturn(Arrays.asList(new MailAddress("you@cerner.com")));
    when(mockMail.getSender()).thenReturn(new MailAddress("me@cerner.com"));
    mockMail.setMessage(mimeMsg);
    NHINDSecurityAndTrustMailet mailet = new NHINDSecurityAndTrustMailet();
    mailet.agent = mockAgent;
    mailet.service(mockMail);
    assertNotNull(usedRecipients);
    assertEquals(1, usedRecipients.size());
    assertEquals("you@cerner.com", usedRecipients.iterator().next().toString());
}
Also used : MailAddress(org.apache.mailet.MailAddress) Message(org.nhindirect.stagent.mail.Message) MimeMessage(javax.mail.internet.MimeMessage) NHINDAddressCollection(org.nhindirect.stagent.NHINDAddressCollection) DefaultMessageEnvelope(org.nhindirect.stagent.DefaultMessageEnvelope) SmtpAgent(org.nhindirect.gateway.smtp.SmtpAgent) MessageProcessResult(org.nhindirect.gateway.smtp.MessageProcessResult) NHINDAddress(org.nhindirect.stagent.NHINDAddress) Mail(org.apache.mailet.Mail) MimeMessage(javax.mail.internet.MimeMessage) InvocationOnMock(org.mockito.invocation.InvocationOnMock)

Example 33 with MimeMessage

use of javax.mail.internet.MimeMessage in project nhin-d by DirectProject.

the class NHINDSecurityAndTrustMailet_service_Test method testService_ProcessThrowsRuntimeException_AssertExceptionAndGhostState.

public void testService_ProcessThrowsRuntimeException_AssertExceptionAndGhostState() throws Exception {
    final MimeMessage mimeMsg = EntitySerializer.Default.deserialize(TestUtils.readMessageResource("PlainOutgoingMessage.txt"));
    final SmtpAgent mockAgent = mock(SmtpAgent.class);
    final Mail mockMail = mock(MockMail.class, CALLS_REAL_METHODS);
    when(mockMail.getRecipients()).thenReturn(null);
    when(mockMail.getSender()).thenReturn(new MailAddress("me@cerner.com"));
    doThrow(new RuntimeException("Just Passing Through")).when(mockAgent).processMessage((MimeMessage) any(), (NHINDAddressCollection) any(), (NHINDAddress) any());
    mockMail.setMessage(mimeMsg);
    NHINDSecurityAndTrustMailet mailet = new NHINDSecurityAndTrustMailet();
    mailet.agent = mockAgent;
    boolean exceptionOccured = false;
    try {
        mailet.service(mockMail);
    } catch (MessagingException e) {
        assertEquals("Failed to process message: Just Passing Through", e.getMessage());
        exceptionOccured = true;
    }
    assertFalse(exceptionOccured);
    assertEquals(Mail.GHOST, mockMail.getState());
}
Also used : Mail(org.apache.mailet.Mail) MailAddress(org.apache.mailet.MailAddress) MimeMessage(javax.mail.internet.MimeMessage) MessagingException(javax.mail.MessagingException) SmtpAgent(org.nhindirect.gateway.smtp.SmtpAgent)

Example 34 with MimeMessage

use of javax.mail.internet.MimeMessage in project nhin-d by DirectProject.

the class TimelyAndReliableLocalDelivery_serviceTest method testService_failedDelivery_assertDSNCreated.

public void testService_failedDelivery_assertDSNCreated() throws Exception {
    new TestPlan() {

        @Override
        protected void setupMocks() {
            theMailet = new TimelyAndReliableLocalDelivery() {

                protected Object createLocalDeliveryClass() throws Exception {
                    Mailet mailet = mock(Mailet.class);
                    doThrow(new RuntimeException()).when(mailet).service((Mail) any());
                    return mailet;
                }
            };
            try {
                MailetConfig config = getMailetConfig();
                theMailet.init(config);
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        }

        @Override
        protected void doAssertions(MockMailetContext context) throws Exception {
            assertEquals(1, context.getSentMessages().size());
            MimeMessage dsnMessage = context.getSentMessages().iterator().next().getMessage();
            assertEquals(TxMessageType.DSN, TxUtil.getMessageType(dsnMessage));
            String originalMessageString = TestUtils.readMessageResource(getMessageToSend());
            MimeMessage originalMsg = EntitySerializer.Default.deserialize(originalMessageString);
            NHINDAddress originalRecipAddress = new NHINDAddress(MailStandard.getHeader(originalMsg, MailStandard.Headers.To));
            NHINDAddress dsnFromAddress = new NHINDAddress(MailStandard.getHeader(dsnMessage, MailStandard.Headers.From));
            assertTrue(dsnFromAddress.getHost().toLowerCase(Locale.getDefault()).contains(originalRecipAddress.getHost().toLowerCase(Locale.getDefault())));
        }
    }.perform();
}
Also used : NHINDAddress(org.nhindirect.stagent.NHINDAddress) BaseTestPlan(org.nhindirect.gateway.testutils.BaseTestPlan) MimeMessage(javax.mail.internet.MimeMessage) MailetConfig(org.apache.mailet.MailetConfig) Mailet(org.apache.mailet.Mailet)

Example 35 with MimeMessage

use of javax.mail.internet.MimeMessage in project nhin-d by DirectProject.

the class IsNotificationTest method testIsNotification_ecryptedMessage_assertNull.

@SuppressWarnings("unchecked")
public void testIsNotification_ecryptedMessage_assertNull() throws Exception {
    MimeMessage msg = new MimeMessage(null, IOUtils.toInputStream(TestUtils.readMessageResource("EncryptedMessage.txt")));
    IsNotSMIMEEncrypted matcher = new IsNotSMIMEEncrypted();
    final Collection<MailAddress> initialRecips = new ArrayList<MailAddress>();
    for (InternetAddress addr : (InternetAddress[]) msg.getAllRecipients()) initialRecips.add(new MailAddress(addr.getAddress()));
    final MockMail mockMail = new MockMail(msg);
    mockMail.setRecipients(initialRecips);
    Collection<MailAddress> matchAddresses = matcher.match(mockMail);
    assertEquals(null, matchAddresses);
}
Also used : InternetAddress(javax.mail.internet.InternetAddress) MailAddress(org.apache.mailet.MailAddress) MimeMessage(javax.mail.internet.MimeMessage) MockMail(org.nhindirect.gateway.smtp.james.mailet.MockMail) ArrayList(java.util.ArrayList)

Aggregations

MimeMessage (javax.mail.internet.MimeMessage)1146 Test (org.junit.Test)374 InternetAddress (javax.mail.internet.InternetAddress)334 MessagingException (javax.mail.MessagingException)299 Session (javax.mail.Session)222 Properties (java.util.Properties)219 MimeMultipart (javax.mail.internet.MimeMultipart)208 MimeBodyPart (javax.mail.internet.MimeBodyPart)178 Date (java.util.Date)153 IOException (java.io.IOException)137 Message (javax.mail.Message)120 MimeMessageHelper (org.springframework.mail.javamail.MimeMessageHelper)107 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)97 ZMimeMessage (com.zimbra.common.zmime.ZMimeMessage)83 InputStream (java.io.InputStream)82 ArrayList (java.util.ArrayList)81 Multipart (javax.mail.Multipart)75 DataHandler (javax.activation.DataHandler)73 ByteArrayOutputStream (java.io.ByteArrayOutputStream)72 BodyPart (javax.mail.BodyPart)70