hungphan227 commented on code in PR #2366: URL: https://github.com/apache/james-project/pull/2366#discussion_r1704873421
########## server/mailet/integration-testing/src/test/java/org/apache/james/mailets/crypto/SMIMECheckSignatureIntegrationTest.java: ########## @@ -0,0 +1,142 @@ +/**************************************************************** + * 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.apache.james.mailets.configuration.Constants.DEFAULT_DOMAIN; +import static org.apache.james.mailets.configuration.Constants.LOCALHOST_IP; +import static org.apache.james.mailets.configuration.Constants.awaitAtMostOneMinute; +import static org.assertj.core.api.Assertions.assertThat; + +import java.io.File; +import java.time.ZonedDateTime; + +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.modules.protocols.ImapGuiceProbe; +import org.apache.james.modules.protocols.SmtpGuiceProbe; +import org.apache.james.transport.mailets.SMIMECheckSignature; +import org.apache.james.transport.matchers.All; +import org.apache.james.util.ClassLoaderUtils; +import org.apache.james.util.date.ZonedDateTimeProvider; +import org.apache.james.utils.DataProbeImpl; +import org.apache.james.utils.SMTPMessageSender; +import org.apache.james.utils.TestIMAPClient; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.RegisterExtension; +import org.junit.jupiter.api.io.TempDir; + +public class SMIMECheckSignatureIntegrationTest { + private static final ZonedDateTime DATE_2015 = ZonedDateTime.parse("2015-10-15T14:10:00Z"); + private static final String FROM = "user@" + DEFAULT_DOMAIN; + public static final String RECIPIENT = "user2@" + DEFAULT_DOMAIN; + private static final String PASSWORD = "secret"; + + @RegisterExtension + public TestIMAPClient testIMAPClient = new TestIMAPClient(); + @RegisterExtension + public SMTPMessageSender messageSender = new SMTPMessageSender(DEFAULT_DOMAIN); + + private TemporaryJamesServer jamesServer; + + @BeforeEach + public void setup(@TempDir File temporaryFolder) throws Exception { + MailetContainer mailetContainer = MailetContainer.builder() + .putProcessor(CommonProcessors.root()) + .putProcessor(CommonProcessors.error()) + .putProcessor(ProcessorConfiguration.transport() + .addMailet(MailetConfiguration.BCC_STRIPPER) + .addMailet(MailetConfiguration.builder() + .mailet(SMIMECheckSignature.class) + .matcher(All.class) + .addProperty("keyStoreFileName", temporaryFolder.toPath().resolve("conf").resolve("smime_cert_keystore").toAbsolutePath().toString()) + .addProperty("keyStorePassword", "secret") + .addProperty("keyStoreType", "PKCS12") + .addProperty("debug", "true")) + .addMailet(MailetConfiguration.LOCAL_DELIVERY)) + .build(); + + jamesServer = TemporaryJamesServer.builder() + .withBase(MemoryJamesServerMain.SMTP_AND_IMAP_MODULE) + .withOverrides(binder -> binder.bind(ZonedDateTimeProvider.class).toInstance(() -> DATE_2015)) + .withMailetContainer(mailetContainer) + .build(temporaryFolder); + jamesServer.start(); + + jamesServer.getProbe(DataProbeImpl.class) + .fluent() + .addDomain(DEFAULT_DOMAIN) + .addUser(FROM, PASSWORD) + .addUser(RECIPIENT, PASSWORD); + } + + @AfterEach + public void tearDown() { + jamesServer.shutdown(); + } + + @Test + public void checkSMIMESignatureShouldAddGoodSMIMEStatus() throws Exception { + messageSender.connect(LOCALHOST_IP, jamesServer.getProbe(SmtpGuiceProbe.class).getSmtpAuthRequiredPort()) + .authenticate(FROM, PASSWORD) + .sendMessageWithHeaders(FROM, RECIPIENT, + ClassLoaderUtils.getSystemResourceAsString("smime-test-resource-set/mail_with_signature.eml")); + + testIMAPClient.connect(LOCALHOST_IP, jamesServer.getProbe(ImapGuiceProbe.class).getImapPort()) + .login(RECIPIENT, PASSWORD) + .select(TestIMAPClient.INBOX) + .awaitMessage(awaitAtMostOneMinute); + assertThat(testIMAPClient.readFirstMessage()).containsSequence("X-SMIME-Status: Good signature"); + } + + @Test + public void checkSMIMESignatureShouldAddBadSMIMEStatus() throws Exception { + messageSender.connect(LOCALHOST_IP, jamesServer.getProbe(SmtpGuiceProbe.class).getSmtpAuthRequiredPort()) + .authenticate(FROM, PASSWORD) + .sendMessageWithHeaders(FROM, RECIPIENT, + ClassLoaderUtils.getSystemResourceAsString("eml/mail_with_bad_signature.eml")); + + testIMAPClient.connect(LOCALHOST_IP, jamesServer.getProbe(ImapGuiceProbe.class).getImapPort()) + .login(RECIPIENT, PASSWORD) + .select(TestIMAPClient.INBOX) + .awaitMessage(awaitAtMostOneMinute); + assertThat(testIMAPClient.readFirstMessage()).containsSequence("X-SMIME-Status: Bad signature"); + } + + @Test + public void checkSMIMESignatureShouldAddNotSignedStatus() throws Exception { + messageSender.connect(LOCALHOST_IP, jamesServer.getProbe(SmtpGuiceProbe.class).getSmtpAuthRequiredPort()) + .authenticate(FROM, PASSWORD) + .sendMessageWithHeaders(FROM, RECIPIENT, + ClassLoaderUtils.getSystemResourceAsString("eml/mail_with_no_signature.eml")); + + testIMAPClient.connect(LOCALHOST_IP, jamesServer.getProbe(ImapGuiceProbe.class).getImapPort()) + .login(RECIPIENT, PASSWORD) + .select(TestIMAPClient.INBOX) + .awaitMessage(awaitAtMostOneMinute); + assertThat(testIMAPClient.readFirstMessage()).containsSequence("X-SMIME-Status: Not signed"); + } + Review Comment: Do you mean the signature is valid but not trusted? -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
