JAMES-2246 Adding tests for SMTP identity verifications
Project: http://git-wip-us.apache.org/repos/asf/james-project/repo Commit: http://git-wip-us.apache.org/repos/asf/james-project/commit/138e8386 Tree: http://git-wip-us.apache.org/repos/asf/james-project/tree/138e8386 Diff: http://git-wip-us.apache.org/repos/asf/james-project/diff/138e8386 Branch: refs/heads/master Commit: 138e83869142fbc03a2860a1ad544f48d5bfae08 Parents: 0ce56b7 Author: benwa <btell...@linagora.com> Authored: Tue Dec 5 15:14:18 2017 +0700 Committer: benwa <btell...@linagora.com> Committed: Fri Dec 8 17:35:50 2017 +0700 ---------------------------------------------------------------------- .../smtp/SmtpIdentityVerificationTest.java | 165 +++++++++++++++++++ 1 file changed, 165 insertions(+) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/james-project/blob/138e8386/server/mailet/integration-testing/src/test/java/org/apache/james/smtp/SmtpIdentityVerificationTest.java ---------------------------------------------------------------------- diff --git a/server/mailet/integration-testing/src/test/java/org/apache/james/smtp/SmtpIdentityVerificationTest.java b/server/mailet/integration-testing/src/test/java/org/apache/james/smtp/SmtpIdentityVerificationTest.java new file mode 100644 index 0000000..114d902 --- /dev/null +++ b/server/mailet/integration-testing/src/test/java/org/apache/james/smtp/SmtpIdentityVerificationTest.java @@ -0,0 +1,165 @@ +/**************************************************************** + * 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.smtp; + +import org.apache.james.MemoryJamesServerMain; +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.configuration.SmtpConfiguration; +import org.apache.james.probe.DataProbe; +import org.apache.james.transport.mailets.LocalDelivery; +import org.apache.james.transport.mailets.RemoveMimeHeader; +import org.apache.james.transport.mailets.ToProcessor; +import org.apache.james.transport.matchers.All; +import org.apache.james.transport.matchers.RecipientIsLocal; +import org.apache.james.utils.DataProbeImpl; +import org.apache.james.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.jayway.awaitility.Awaitility; +import com.jayway.awaitility.Duration; +import com.jayway.awaitility.core.ConditionFactory; + +public class SmtpIdentityVerificationTest { + private static final String DEFAULT_DOMAIN = "james.org"; + private static final String LOCALHOST_IP = "127.0.0.1"; + private static final int SMTP_PORT = 1025; + private static final String PASSWORD = "secret"; + private static final String ATTACKER_PASSWORD = "secret"; + + private static final String JAMES_APACHE_ORG = "james.org"; + private static final String ATTACKER = "attacker@" + JAMES_APACHE_ORG; + private static final String USER = "user@" + JAMES_APACHE_ORG; + + @Rule + public TemporaryFolder temporaryFolder = new TemporaryFolder(); + + private TemporaryJamesServer jamesServer; + private ConditionFactory calmlyAwait; + + @Before + public void setup() throws Exception { + Duration slowPacedPollInterval = Duration.FIVE_HUNDRED_MILLISECONDS; + calmlyAwait = Awaitility.with() + .pollInterval(slowPacedPollInterval) + .and() + .with() + .pollDelay(slowPacedPollInterval) + .await(); + } + + private void createJamesServer(SmtpConfiguration.Builder smtpConfiguration) throws Exception { + MailetContainer mailetContainer = MailetContainer.builder() + .postmaster("postmaster@" + DEFAULT_DOMAIN) + .threads(5) + .addProcessor(ProcessorConfiguration.builder() + .state("root") + .addMailet(MailetConfiguration.builder() + .matcher(All.class) + .mailet(ToProcessor.class) + .addProperty("processor", "transport") + .build()) + .build()) + .addProcessor(CommonProcessors.error()) + .addProcessor(ProcessorConfiguration.builder() + .state("transport") + .enableJmx(true) + .addMailet(MailetConfiguration.builder() + .matcher(All.class) + .mailet(RemoveMimeHeader.class) + .addProperty("name", "bcc") + .build()) + .addMailet(MailetConfiguration.builder() + .matcher(RecipientIsLocal.class) + .mailet(LocalDelivery.class) + .build()) + .build()) + .addProcessor(CommonProcessors.localAddressError()) + .addProcessor(CommonProcessors.relayDenied()) + .addProcessor(CommonProcessors.bounces()) + .build(); + jamesServer = TemporaryJamesServer.builder() + .withBase(MemoryJamesServerMain.SMTP_ONLY_MODULE) + .withSmtpConfiguration(smtpConfiguration.build()) + .build(temporaryFolder, mailetContainer); + + DataProbe dataProbe = jamesServer.getProbe(DataProbeImpl.class); + dataProbe.addDomain(JAMES_APACHE_ORG); + dataProbe.addUser(USER, PASSWORD); + dataProbe.addUser(ATTACKER, ATTACKER_PASSWORD); + } + + @After + public void tearDown() { + if (jamesServer != null) { + jamesServer.shutdown(); + } + } + + @Test + public void smtpShouldAcceptMessageWhenIdentityIsMatching() throws Exception { + createJamesServer(SmtpConfiguration.builder() + .requireAuthentication() + .verifyIdentity()); + + try (SMTPMessageSender messageSender = + SMTPMessageSender.authentication(LOCALHOST_IP, SMTP_PORT, JAMES_APACHE_ORG, USER, PASSWORD)) { + + messageSender.sendMessage(USER, USER); + calmlyAwait.atMost(Duration.ONE_MINUTE).until(messageSender::messageHasBeenSent); + } + } + + @Test + public void smtpShouldAcceptMessageWhenIdentityIsNotMatchingButNotChecked() throws Exception { + createJamesServer(SmtpConfiguration.builder() + .requireAuthentication() + .doNotVerifyIdentity()); + + try (SMTPMessageSender messageSender = + SMTPMessageSender.authentication(LOCALHOST_IP, SMTP_PORT, JAMES_APACHE_ORG, ATTACKER, ATTACKER_PASSWORD)) { + + messageSender.sendMessage(USER, USER); + calmlyAwait.atMost(Duration.ONE_MINUTE).until(messageSender::messageHasBeenSent); + } + } + + @Test + public void smtpShouldRejectMessageWhenIdentityIsNotMatching() throws Exception { + createJamesServer(SmtpConfiguration.builder() + .requireAuthentication() + .verifyIdentity()); + + try (SMTPMessageSender messageSender = + SMTPMessageSender.authentication(LOCALHOST_IP, SMTP_PORT, JAMES_APACHE_ORG, ATTACKER, ATTACKER_PASSWORD)) { + + messageSender.sendMessage(USER, USER); + calmlyAwait.atMost(Duration.ONE_MINUTE).until(messageSender::messageSendingFailed); + } + } + +} --------------------------------------------------------------------- To unsubscribe, e-mail: server-dev-unsubscr...@james.apache.org For additional commands, e-mail: server-dev-h...@james.apache.org