Skip to content

Commit

Permalink
JAMES-4054 Update SMIMECheckSignatureIntegrationTest to include IsSMI…
Browse files Browse the repository at this point in the history
…MESigned matcher (#2409)
  • Loading branch information
hungphan227 authored Sep 17, 2024
1 parent a120c9f commit d8d8fc4
Show file tree
Hide file tree
Showing 8 changed files with 112 additions and 6 deletions.
5 changes: 5 additions & 0 deletions mailet/crypto/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,11 @@
<groupId>${james.groupId}</groupId>
<artifactId>apache-mailet-base</artifactId>
</dependency>
<dependency>
<groupId>${james.groupId}</groupId>
<artifactId>apache-mailet-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>${james.groupId}</groupId>
<artifactId>james-server-core</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,7 @@ public Collection<MailAddress> match(Mail mail) throws MessagingException {
if (message == null) {
return null;
}



if (message.isMimeType("multipart/signed")
|| message.isMimeType("application/pkcs7-signature")
|| message.isMimeType("application/x-pkcs7-signature")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
/****************************************************************
* 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.transport.matcher;

import static org.apache.mailet.base.MailAddressFixture.RECIPIENT1;
import static org.apache.mailet.base.MailAddressFixture.SENDER;
import static org.assertj.core.api.Assertions.assertThat;

import org.apache.james.core.builder.MimeMessageBuilder;
import org.apache.mailet.base.test.FakeMail;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;

public class IsSMIMESignedTest {
private IsSMIMESigned isSMIMESigned;

@BeforeEach
void beforeEach() {
isSMIMESigned = new IsSMIMESigned();
}

@ParameterizedTest
@ValueSource(strings = {"multipart/signed",
"application/pkcs7-signature",
"application/x-pkcs7-signature",
"application/pkcs7-mime; smime-type=signed-data; name=\"smime.p7m\"",
"application/x-pkcs7-mime; smime-type=signed-data; name=\"smime.p7m\""})
void matchShouldReturnNonEmptyListWhenMessageContentTypeIsSMIMERelated(String contentType) throws Exception {
FakeMail mail = FakeMail.builder()
.name("mail")
.mimeMessage(MimeMessageBuilder.mimeMessageBuilder().addHeader("Content-Type", contentType))
.sender(SENDER)
.recipient(RECIPIENT1)
.build();
assertThat(isSMIMESigned.match(mail)).isNotEmpty();
}

@Test
void matchShouldReturnNullWhenMessageContentTypeIsNotSMIMERelated() throws Exception {
FakeMail mail = FakeMail.builder()
.name("mail")
.mimeMessage(MimeMessageBuilder.mimeMessageBuilder().addHeader("Content-Type", "text/plain"))
.sender(SENDER)
.recipient(RECIPIENT1)
.build();
assertThat(isSMIMESigned.match(mail)).isNull();
}

@Test
void matchShouldReturnNullWhenMailIsNull() throws Exception {
assertThat(isSMIMESigned.match(null)).isNull();
}

@Test
void matchShouldReturnNullWhenMessageIsNull() throws Exception {
FakeMail mail = FakeMail.builder()
.name("mail")
.build();
assertThat(isSMIMESigned.match(mail)).isNull();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@ To: user2@james.org
Subject: test
Message-ID: <df73c05a-2e18-3e22-040c-9eda6abe02ae@open-paas.org>
Date: Fri, 1 Nov 2019 10:21:39 +0700
Content-Type: application/x-pkcs7-mime; smime-type=signed-data; name="smime.p7m"

test SMIME
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
From: user@james.org
To: user2@james.org
Subject: test
Message-ID: <df73c05a-2e18-3e22-040c-9eda6abe02ae@open-paas.org>
Date: Fri, 1 Nov 2019 10:21:39 +0700

test SMIME
Original file line number Diff line number Diff line change
Expand Up @@ -115,4 +115,18 @@ public void checkSMIMESignatureShouldAddNotSignedStatusWhenNoSignature() throws
.awaitMessage(awaitAtMostOneMinute);
assertThat(testIMAPClient().readFirstMessage()).containsSequence("X-SMIME-Status: Not signed");
}

@Test
public void checkSMIMESignatureShouldDoNothingWhenItIsNonSMIMEMail() throws Exception {
messageSender().connect(LOCALHOST_IP, jamesServer().getProbe(SmtpGuiceProbe.class).getSmtpAuthRequiredPort())
.authenticate(FROM, PASSWORD)
.sendMessageWithHeaders(FROM, RECIPIENT,
ClassLoaderUtils.getSystemResourceAsString("eml/non_smime_mail.eml"));

testIMAPClient().connect(LOCALHOST_IP, jamesServer().getProbe(ImapGuiceProbe.class).getImapPort())
.login(RECIPIENT, PASSWORD)
.select(TestIMAPClient.INBOX)
.awaitMessage(awaitAtMostOneMinute);
assertThat(testIMAPClient().readFirstMessage()).doesNotContain("X-SMIME-Status");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
import org.apache.james.mailets.configuration.MailetContainer;
import org.apache.james.mailets.configuration.ProcessorConfiguration;
import org.apache.james.transport.mailets.SMIMECheckSignature;
import org.apache.james.transport.matchers.All;
import org.apache.james.transport.matcher.IsSMIMESigned;
import org.apache.james.util.date.ZonedDateTimeProvider;
import org.apache.james.utils.DataProbeImpl;
import org.apache.james.utils.SMTPMessageSender;
Expand All @@ -58,7 +58,7 @@ public void setup(@TempDir File temporaryFolder) throws Exception {
.addMailet(MailetConfiguration.BCC_STRIPPER)
.addMailet(MailetConfiguration.builder()
.mailet(SMIMECheckSignature.class)
.matcher(All.class)
.matcher(IsSMIMESigned.class)
.addProperty("fileType", "keystore")
.addProperty("keyStoreFileName", FileSystem.CLASSPATH_PROTOCOL + "trusted_cert_keystore")
.addProperty("keyStorePassword", "secret")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
import org.apache.james.mailets.configuration.MailetContainer;
import org.apache.james.mailets.configuration.ProcessorConfiguration;
import org.apache.james.transport.mailets.SMIMECheckSignature;
import org.apache.james.transport.matchers.All;
import org.apache.james.transport.matcher.IsSMIMESigned;
import org.apache.james.util.date.ZonedDateTimeProvider;
import org.apache.james.utils.DataProbeImpl;
import org.apache.james.utils.SMTPMessageSender;
Expand All @@ -58,7 +58,7 @@ public void setup(@TempDir File temporaryFolder) throws Exception {
.addMailet(MailetConfiguration.BCC_STRIPPER)
.addMailet(MailetConfiguration.builder()
.mailet(SMIMECheckSignature.class)
.matcher(All.class)
.matcher(IsSMIMESigned.class)
.addProperty("fileType", "pem")
.addProperty("pemFileName", FileSystem.CLASSPATH_PROTOCOL + "trusted_certificate.pem")
.addProperty("debug", "true"))
Expand Down

0 comments on commit d8d8fc4

Please sign in to comment.