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();
}
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());
}
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());
}
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();
}
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);
}
Aggregations