Repository: james-project Updated Branches: refs/heads/master b3a30c1e5 -> 17c79db46
JAMES-1856 Write integration tests for SMIME Sign Project: http://git-wip-us.apache.org/repos/asf/james-project/repo Commit: http://git-wip-us.apache.org/repos/asf/james-project/commit/17c79db4 Tree: http://git-wip-us.apache.org/repos/asf/james-project/tree/17c79db4 Diff: http://git-wip-us.apache.org/repos/asf/james-project/diff/17c79db4 Branch: refs/heads/master Commit: 17c79db46f8ad1e872e2ee16cfc691ab297b169d Parents: f3a7bec Author: Benoit Tellier <[email protected]> Authored: Mon Nov 14 14:51:13 2016 +0700 Committer: Benoit Tellier <[email protected]> Committed: Thu Nov 17 15:26:18 2016 +0700 ---------------------------------------------------------------------- server/mailet/integration-testing/pom.xml | 5 + .../mailets/CommonMailetConfigurationTest.java | 61 +------ .../mailets/TemporaryFilesystemModule.java | 3 +- .../james/mailets/TemporaryJamesServer.java | 13 +- .../crypto/SMIMESignIntegrationTest.java | 180 +++++++++++++++++++ .../james/mailets/utils/IMAPMessageReader.java | 55 ++++++ .../james/mailets/utils/SMTPMessageSender.java | 81 +++++++++ .../src/test/resources/smime_1.p12 | Bin 0 -> 4069 bytes 8 files changed, 340 insertions(+), 58 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/james-project/blob/17c79db4/server/mailet/integration-testing/pom.xml ---------------------------------------------------------------------- diff --git a/server/mailet/integration-testing/pom.xml b/server/mailet/integration-testing/pom.xml index ed1c69a..44c011d 100644 --- a/server/mailet/integration-testing/pom.xml +++ b/server/mailet/integration-testing/pom.xml @@ -171,6 +171,11 @@ <artifactId>james-server-memory-guice</artifactId> </dependency> <dependency> + <groupId>org.apache.james</groupId> + <artifactId>apache-mailet-crypto</artifactId> + <version>${project.version}</version> + </dependency> + <dependency> <groupId>ch.qos.logback</groupId> <artifactId>logback-classic</artifactId> <version>1.1.6</version> http://git-wip-us.apache.org/repos/asf/james-project/blob/17c79db4/server/mailet/integration-testing/src/test/java/org/apache/james/mailets/CommonMailetConfigurationTest.java ---------------------------------------------------------------------- diff --git a/server/mailet/integration-testing/src/test/java/org/apache/james/mailets/CommonMailetConfigurationTest.java b/server/mailet/integration-testing/src/test/java/org/apache/james/mailets/CommonMailetConfigurationTest.java index d8a8cad..c5a89e4 100644 --- a/server/mailet/integration-testing/src/test/java/org/apache/james/mailets/CommonMailetConfigurationTest.java +++ b/server/mailet/integration-testing/src/test/java/org/apache/james/mailets/CommonMailetConfigurationTest.java @@ -19,21 +19,17 @@ package org.apache.james.mailets; -import java.io.IOException; -import java.nio.channels.SocketChannel; - -import org.apache.commons.net.imap.IMAPClient; -import org.apache.commons.net.smtp.SMTPClient; import org.apache.james.mailbox.model.MailboxConstants; import org.apache.james.mailets.configuration.CommonProcessors; import org.apache.james.mailets.configuration.MailetContainer; +import org.apache.james.mailets.utils.IMAPMessageReader; +import org.apache.james.mailets.utils.SMTPMessageSender; import org.junit.After; import org.junit.Before; import org.junit.Rule; import org.junit.Test; import org.junit.rules.TemporaryFolder; -import com.google.common.base.Throwables; import com.jayway.awaitility.Awaitility; import com.jayway.awaitility.Duration; import com.jayway.awaitility.core.ConditionFactory; @@ -90,54 +86,11 @@ public class CommonMailetConfigurationTest { jamesServer.getServerProbe().addUser(recipient, PASSWORD); jamesServer.getServerProbe().createMailbox(MailboxConstants.USER_NAMESPACE, recipient, "INBOX"); - SMTPClient smtpClient = new SMTPClient(); - try (SocketChannel socketChannel = SocketChannel.open()) { - smtpClient.connect(LOCALHOST_IP, SMTP_PORT); - sendMessage(smtpClient, from, recipient); - - calmlyAwait.atMost(Duration.ONE_MINUTE).until(() -> messageHasBeenSent(smtpClient)); - calmlyAwait.atMost(Duration.ONE_MINUTE).until(() -> userReceivedMessage(recipient)); - } finally { - smtpClient.disconnect(); - } - } - - private void sendMessage(SMTPClient smtpClient, String from, String recipient) { - try { - smtpClient.helo(DEFAULT_DOMAIN); - smtpClient.setSender(from); - smtpClient.rcpt("<" + recipient + ">"); - smtpClient.sendShortMessageData("subject: test\r\n" + - "\r\n" + - "content\r\n" + - ".\r\n"); - } catch (IOException e) { - throw Throwables.propagate(e); - } - } - - private boolean messageHasBeenSent(SMTPClient smtpClient) throws IOException { - return smtpClient.getReplyString() - .contains("250 2.6.0 Message received"); - } - - private boolean userReceivedMessage(String user) { - IMAPClient imapClient = new IMAPClient(); - try { - imapClient.connect(LOCALHOST_IP, IMAP_PORT); - imapClient.login(user, PASSWORD); - imapClient.select("INBOX"); - imapClient.fetch("1:1", "ALL"); - String replyString = imapClient.getReplyString(); - return replyString.contains("OK FETCH completed"); - } catch (IOException e) { - throw Throwables.propagate(e); - } finally { - try { - imapClient.close(); - } catch (IOException e) { - throw Throwables.propagate(e); - } + try (SMTPMessageSender messageSender = SMTPMessageSender.noAuthentication(LOCALHOST_IP, SMTP_PORT, DEFAULT_DOMAIN); + IMAPMessageReader imapMessageReader = new IMAPMessageReader(LOCALHOST_IP, IMAP_PORT)) { + messageSender.sendMessage(from, recipient); + calmlyAwait.atMost(Duration.ONE_MINUTE).until(messageSender::messageHasBeenSent); + calmlyAwait.atMost(Duration.ONE_MINUTE).until(() -> imapMessageReader.userReceivedMessage(recipient, PASSWORD)); } } } http://git-wip-us.apache.org/repos/asf/james-project/blob/17c79db4/server/mailet/integration-testing/src/test/java/org/apache/james/mailets/TemporaryFilesystemModule.java ---------------------------------------------------------------------- diff --git a/server/mailet/integration-testing/src/test/java/org/apache/james/mailets/TemporaryFilesystemModule.java b/server/mailet/integration-testing/src/test/java/org/apache/james/mailets/TemporaryFilesystemModule.java index aeebc0a..e0faf0b 100644 --- a/server/mailet/integration-testing/src/test/java/org/apache/james/mailets/TemporaryFilesystemModule.java +++ b/server/mailet/integration-testing/src/test/java/org/apache/james/mailets/TemporaryFilesystemModule.java @@ -53,7 +53,8 @@ public class TemporaryFilesystemModule extends AbstractModule { "pop3server.xml", "recipientrewritetable.xml", "smtpserver.xml", - "usersrepository.xml"); + "usersrepository.xml", + "smime_1.p12"); private final Supplier<File> workingDirectory; http://git-wip-us.apache.org/repos/asf/james-project/blob/17c79db4/server/mailet/integration-testing/src/test/java/org/apache/james/mailets/TemporaryJamesServer.java ---------------------------------------------------------------------- diff --git a/server/mailet/integration-testing/src/test/java/org/apache/james/mailets/TemporaryJamesServer.java b/server/mailet/integration-testing/src/test/java/org/apache/james/mailets/TemporaryJamesServer.java index 71cedb1..2dd1c87 100644 --- a/server/mailet/integration-testing/src/test/java/org/apache/james/mailets/TemporaryJamesServer.java +++ b/server/mailet/integration-testing/src/test/java/org/apache/james/mailets/TemporaryJamesServer.java @@ -24,6 +24,7 @@ import java.io.FileOutputStream; import java.io.IOException; import java.io.OutputStream; import java.nio.file.Paths; +import java.util.Arrays; import org.apache.commons.configuration.ConfigurationException; import org.apache.commons.io.IOUtils; @@ -34,6 +35,9 @@ import org.apache.james.modules.TestJMAPServerModule; import org.apache.james.utils.ExtendedServerProbe; import org.junit.rules.TemporaryFolder; +import com.google.common.collect.ImmutableList; +import com.google.inject.Module; + public class TemporaryJamesServer { private static final String MAILETCONTAINER_CONFIGURATION_FILENAME = "mailetcontainer.xml"; @@ -43,13 +47,16 @@ public class TemporaryJamesServer { private final GuiceJamesServer jamesServer; - public TemporaryJamesServer(TemporaryFolder temporaryFolder, MailetContainer mailetContainer) throws Exception { + public TemporaryJamesServer(TemporaryFolder temporaryFolder, MailetContainer mailetContainer, Module... additionalModules) throws Exception { appendMailetConfigurations(temporaryFolder, mailetContainer); jamesServer = new GuiceJamesServer() .combineWith(MemoryJamesServerMain.inMemoryServerModule) - .overrideWith(new TestJMAPServerModule(LIMIT_TO_3_MESSAGES), - new TemporaryFilesystemModule(temporaryFolder)); + .overrideWith(ImmutableList.<Module>builder().addAll(Arrays.asList(additionalModules)) + .add(new TestJMAPServerModule(LIMIT_TO_3_MESSAGES)) + .add(new TemporaryFilesystemModule(temporaryFolder)) + .build() + .toArray(new Module[additionalModules.length + 2])); jamesServer.start(); } http://git-wip-us.apache.org/repos/asf/james-project/blob/17c79db4/server/mailet/integration-testing/src/test/java/org/apache/james/mailets/crypto/SMIMESignIntegrationTest.java ---------------------------------------------------------------------- diff --git a/server/mailet/integration-testing/src/test/java/org/apache/james/mailets/crypto/SMIMESignIntegrationTest.java b/server/mailet/integration-testing/src/test/java/org/apache/james/mailets/crypto/SMIMESignIntegrationTest.java new file mode 100644 index 0000000..4ff2af1 --- /dev/null +++ b/server/mailet/integration-testing/src/test/java/org/apache/james/mailets/crypto/SMIMESignIntegrationTest.java @@ -0,0 +1,180 @@ +/**************************************************************** + * Licensed to the Apache Software Foundation (ASF) under one * + * or more contributor license agreements. See the NOTICE file * + * distributed with this work for additional information * + * regarding copyright ownership. The ASF licenses this file * + * to you under the Apache License, Version 2.0 (the * + * "License"); you may not use this file except in compliance * + * with the License. You may obtain a copy of the License at * + * * + * http://www.apache.org/licenses/LICENSE-2.0 * + * * + * Unless required by applicable law or agreed to in writing, * + * software distributed under the License is distributed on an * + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * + * KIND, either express or implied. See the License for the * + * specific language governing permissions and limitations * + * under the License. * + ****************************************************************/ + +package org.apache.james.mailets.crypto; + +import static org.assertj.core.api.Assertions.assertThat; + +import java.time.ZonedDateTime; + +import org.apache.james.mailbox.model.MailboxConstants; +import org.apache.james.mailets.TemporaryJamesServer; +import org.apache.james.mailets.configuration.CommonProcessors; +import org.apache.james.mailets.configuration.MailetConfiguration; +import org.apache.james.mailets.configuration.MailetContainer; +import org.apache.james.mailets.configuration.ProcessorConfiguration; +import org.apache.james.mailets.utils.IMAPMessageReader; +import org.apache.james.mailets.utils.SMTPMessageSender; +import org.apache.james.util.date.ZonedDateTimeProvider; +import org.junit.After; +import org.junit.Before; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.TemporaryFolder; + +import com.jayway.awaitility.Awaitility; +import com.jayway.awaitility.Duration; +import com.jayway.awaitility.core.ConditionFactory; + +public class SMIMESignIntegrationTest { + + + private static final ZonedDateTime DATE_2015 = ZonedDateTime.parse("2015-10-15T14:10:00Z"); + private static final String DEFAULT_DOMAIN = "domain"; + private static final String LOCALHOST_IP = "127.0.0.1"; + private static final int IMAP_PORT = 1143; + private static final int SMTP_PORT = 1025; + private static final int SMTP_SECURE_PORT = 10465; + private static final String PASSWORD = "secret"; + + @Rule + public TemporaryFolder temporaryFolder = new TemporaryFolder(); + + private TemporaryJamesServer jamesServer; + private ConditionFactory calmlyAwait; + public static final String FROM = "user@" + DEFAULT_DOMAIN; + public static final String RECIPIENT = "user2@" + DEFAULT_DOMAIN; + + @Before + public void setup() throws Exception { + temporaryFolder.newFile("smime.pk12"); + + MailetContainer mailetContainer = MailetContainer.builder() + .postmaster("postmaster@" + DEFAULT_DOMAIN) + .threads(5) + .addProcessor(CommonProcessors.root()) + .addProcessor(CommonProcessors.error()) + .addProcessor(ProcessorConfiguration.builder() + .state("transport") + .enableJmx(true) + .addMailet(MailetConfiguration.builder() + .match("SMTPAuthSuccessful") + .clazz("SetMimeHeader") + .addProperty("name", "X-UserIsAuth") + .addProperty("value", "true") + .build()) + .addMailet(MailetConfiguration.builder() + .match("HasMailAttribute=org.apache.james.SMIMECheckSignature") + .clazz("SetMimeHeader") + .addProperty("name", "X-WasSigned") + .addProperty("value", "true") + .build()) + .addMailet(MailetConfiguration.builder() + .match("All") + .clazz("RemoveMimeHeader") + .addProperty("name", "bcc") + .build()) + .addMailet(MailetConfiguration.builder() + .match("All") + .clazz("RecipientRewriteTable") + .build()) + .addMailet(MailetConfiguration.builder() + .match("RecipientIsLocal") + .clazz("org.apache.james.jmap.mailet.VacationMailet") + .build()) + .addMailet(MailetConfiguration.builder() + .clazz("SMIMESign") + .match("SenderIsLocal") + .addProperty("keyStoreFileName", temporaryFolder.getRoot().getAbsoluteFile().getAbsolutePath() + "/conf/smime_1.p12") + .addProperty("keyStorePassword", "totototo") + .addProperty("keyStoreType", "PKCS12") + .addProperty("debug", "true") + .build()) + .addMailet(MailetConfiguration.builder() + .match("RecipientIsLocal") + .clazz("LocalDelivery") + .build()) + .addMailet(MailetConfiguration.builder() + .match("SMTPAuthSuccessful") + .clazz("RemoteDelivery") + .addProperty("outgoingQueue", "outgoing") + .addProperty("delayTime", "5000, 100000, 500000") + .addProperty("maxRetries", "25") + .addProperty("maxDnsProblemRetries", "0") + .addProperty("deliveryThreads", "10") + .addProperty("sendpartial", "true") + .addProperty("bounceProcessor", "bounces") + .build()) + .addMailet(MailetConfiguration.builder() + .match("All") + .clazz("ToProcessor") + .addProperty("processor", "relay-denied") + .build()) + .build()) + .addProcessor(CommonProcessors.spam()) + .addProcessor(CommonProcessors.localAddressError()) + .addProcessor(CommonProcessors.relayDenied()) + .addProcessor(CommonProcessors.bounces()) + .addProcessor(CommonProcessors.sieveManagerCheck()) + .build(); + + jamesServer = new TemporaryJamesServer(temporaryFolder, mailetContainer, + binder -> binder.bind(ZonedDateTimeProvider.class).toInstance(() -> DATE_2015)); + Duration slowPacedPollInterval = Duration.FIVE_HUNDRED_MILLISECONDS; + calmlyAwait = Awaitility.with().pollInterval(slowPacedPollInterval).and().with().pollDelay(slowPacedPollInterval).await(); + + jamesServer.getServerProbe().addDomain(DEFAULT_DOMAIN); + jamesServer.getServerProbe().addUser(FROM, PASSWORD); + jamesServer.getServerProbe().addUser(RECIPIENT, PASSWORD); + jamesServer.getServerProbe().createMailbox(MailboxConstants.USER_NAMESPACE, RECIPIENT, "INBOX"); + } + + @After + public void tearDown() { + jamesServer.shutdown(); + } + + @Test + public void authenticatedMessagesShouldBeSigned() throws Exception { + + try (SMTPMessageSender messageSender = SMTPMessageSender.authentication(LOCALHOST_IP, SMTP_SECURE_PORT, DEFAULT_DOMAIN, FROM, PASSWORD); + IMAPMessageReader imapMessageReader = new IMAPMessageReader(LOCALHOST_IP, IMAP_PORT)) { + messageSender.sendMessage(FROM, RECIPIENT); + calmlyAwait.atMost(Duration.ONE_MINUTE).until(messageSender::messageHasBeenSent); + calmlyAwait.atMost(Duration.ONE_MINUTE).until(() -> imapMessageReader.userReceivedMessage(RECIPIENT, PASSWORD)); + + assertThat(imapMessageReader.readFirstMessageInInbox(RECIPIENT, PASSWORD)) + .containsSequence("Content-Description: S/MIME Cryptographic Signature"); + } + } + + @Test + public void NonAuthenticatedMessagesShouldNotBeSigned() throws Exception { + try (SMTPMessageSender messageSender = SMTPMessageSender.noAuthentication(LOCALHOST_IP, SMTP_PORT, DEFAULT_DOMAIN); + IMAPMessageReader imapMessageReader = new IMAPMessageReader(LOCALHOST_IP, IMAP_PORT)) { + messageSender.sendMessage(FROM, RECIPIENT); + calmlyAwait.atMost(Duration.ONE_MINUTE).until(messageSender::messageHasBeenSent); + calmlyAwait.atMost(Duration.ONE_MINUTE).until(() -> imapMessageReader.userReceivedMessage(RECIPIENT, PASSWORD)); + + assertThat(imapMessageReader.readFirstMessageInInbox(RECIPIENT, PASSWORD)) + .doesNotContain("Content-Description: S/MIME Cryptographic Signature"); + } + } + +} http://git-wip-us.apache.org/repos/asf/james-project/blob/17c79db4/server/mailet/integration-testing/src/test/java/org/apache/james/mailets/utils/IMAPMessageReader.java ---------------------------------------------------------------------- diff --git a/server/mailet/integration-testing/src/test/java/org/apache/james/mailets/utils/IMAPMessageReader.java b/server/mailet/integration-testing/src/test/java/org/apache/james/mailets/utils/IMAPMessageReader.java new file mode 100644 index 0000000..219040c --- /dev/null +++ b/server/mailet/integration-testing/src/test/java/org/apache/james/mailets/utils/IMAPMessageReader.java @@ -0,0 +1,55 @@ +/**************************************************************** + * Licensed to the Apache Software Foundation (ASF) under one * + * or more contributor license agreements. See the NOTICE file * + * distributed with this work for additional information * + * regarding copyright ownership. The ASF licenses this file * + * to you under the Apache License, Version 2.0 (the * + * "License"); you may not use this file except in compliance * + * with the License. You may obtain a copy of the License at * + * * + * http://www.apache.org/licenses/LICENSE-2.0 * + * * + * Unless required by applicable law or agreed to in writing, * + * software distributed under the License is distributed on an * + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * + * KIND, either express or implied. See the License for the * + * specific language governing permissions and limitations * + * under the License. * + ****************************************************************/ + +package org.apache.james.mailets.utils; + +import java.io.Closeable; +import java.io.IOException; + +import org.apache.commons.net.imap.IMAPClient; + +public class IMAPMessageReader implements Closeable { + + private final IMAPClient imapClient; + + public IMAPMessageReader(String host, int port) throws IOException { + imapClient = new IMAPClient(); + imapClient.connect(host, port); + } + + public boolean userReceivedMessage(String user, String password) throws IOException { + imapClient.login(user, password); + imapClient.select("INBOX"); + imapClient.fetch("1:1", "ALL"); + return imapClient.getReplyString() + .contains("OK FETCH completed"); + } + + public String readFirstMessageInInbox(String user, String password) throws IOException { + imapClient.login(user, password); + imapClient.select("INBOX"); + imapClient.fetch("1:1", "(BODY[])"); + return imapClient.getReplyString(); + } + + @Override + public void close() throws IOException { + imapClient.close(); + } +} http://git-wip-us.apache.org/repos/asf/james-project/blob/17c79db4/server/mailet/integration-testing/src/test/java/org/apache/james/mailets/utils/SMTPMessageSender.java ---------------------------------------------------------------------- diff --git a/server/mailet/integration-testing/src/test/java/org/apache/james/mailets/utils/SMTPMessageSender.java b/server/mailet/integration-testing/src/test/java/org/apache/james/mailets/utils/SMTPMessageSender.java new file mode 100644 index 0000000..410bd52 --- /dev/null +++ b/server/mailet/integration-testing/src/test/java/org/apache/james/mailets/utils/SMTPMessageSender.java @@ -0,0 +1,81 @@ +/**************************************************************** + * Licensed to the Apache Software Foundation (ASF) under one * + * or more contributor license agreements. See the NOTICE file * + * distributed with this work for additional information * + * regarding copyright ownership. The ASF licenses this file * + * to you under the Apache License, Version 2.0 (the * + * "License"); you may not use this file except in compliance * + * with the License. You may obtain a copy of the License at * + * * + * http://www.apache.org/licenses/LICENSE-2.0 * + * * + * Unless required by applicable law or agreed to in writing, * + * software distributed under the License is distributed on an * + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * + * KIND, either express or implied. See the License for the * + * specific language governing permissions and limitations * + * under the License. * + ****************************************************************/ + +package org.apache.james.mailets.utils; + +import java.io.Closeable; +import java.io.IOException; +import java.security.InvalidKeyException; +import java.security.NoSuchAlgorithmException; +import java.security.spec.InvalidKeySpecException; + +import org.apache.commons.net.smtp.AuthenticatingSMTPClient; +import org.apache.commons.net.smtp.SMTPClient; + +import com.google.common.base.Throwables; + +public class SMTPMessageSender implements Closeable { + + public static SMTPMessageSender noAuthentication(String ip, int port, String senderDomain) throws IOException { + SMTPClient smtpClient = new SMTPClient(); + smtpClient.connect(ip, port); + return new SMTPMessageSender(smtpClient, senderDomain); + } + + public static SMTPMessageSender authentication(String ip, int port, String senderDomain, String username, String password) + throws NoSuchAlgorithmException, IOException, InvalidKeySpecException, InvalidKeyException { + AuthenticatingSMTPClient smtpClient = new AuthenticatingSMTPClient(); + smtpClient.connect(ip, port); + smtpClient.auth(AuthenticatingSMTPClient.AUTH_METHOD.PLAIN, username, password); + return new SMTPMessageSender(smtpClient, senderDomain); + } + + private final SMTPClient smtpClient; + private final String senderDomain; + + private SMTPMessageSender(SMTPClient smtpClient, String senderDomain) { + this.smtpClient = smtpClient; + this.senderDomain = senderDomain; + } + + public void sendMessage(String from, String recipient) { + try { + smtpClient.helo(senderDomain); + smtpClient.setSender(from); + smtpClient.rcpt("<" + recipient + ">"); + smtpClient.sendShortMessageData("FROM: " + from + "\r\n" + + "subject: test\r\n" + + "\r\n" + + "content\r\n" + + ".\r\n"); + } catch (IOException e) { + throw Throwables.propagate(e); + } + } + + public boolean messageHasBeenSent() throws IOException { + return smtpClient.getReplyString() + .contains("250 2.6.0 Message received"); + } + + @Override + public void close() throws IOException { + smtpClient.disconnect(); + } +} http://git-wip-us.apache.org/repos/asf/james-project/blob/17c79db4/server/mailet/integration-testing/src/test/resources/smime_1.p12 ---------------------------------------------------------------------- diff --git a/server/mailet/integration-testing/src/test/resources/smime_1.p12 b/server/mailet/integration-testing/src/test/resources/smime_1.p12 new file mode 100644 index 0000000..8a91933 Binary files /dev/null and b/server/mailet/integration-testing/src/test/resources/smime_1.p12 differ --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
