Skip to content
Permalink
Browse files
JSIEVE-107 Added unit test to verify issue JSIEVE-107
  • Loading branch information
Daniele Depetrini authored and chibenwa committed Aug 22, 2017
1 parent cc1688d commit 697927f69dd575b4173f53113a2badfc793bcee2
Showing 5 changed files with 148 additions and 10 deletions.
@@ -51,6 +51,13 @@
<artifactId>apache-jsieve-core</artifactId>
</dependency>

<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<version>${assertj.version}</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
@@ -19,6 +19,18 @@

package org.apache.jsieve.util.check;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Enumeration;
import java.util.LinkedList;
import java.util.List;

import javax.mail.Header;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.internet.MimeUtility;

import org.apache.jsieve.SieveContext;
import org.apache.jsieve.exception.SieveException;
import org.apache.jsieve.mail.Action;
@@ -28,14 +40,6 @@
import org.apache.jsieve.parser.address.SieveAddressBuilder;
import org.apache.jsieve.parser.generated.address.ParseException;

import javax.mail.internet.MimeUtility;
import javax.mail.Header;
import javax.mail.Message;
import javax.mail.MessagingException;
import java.io.IOException;
import java.util.*;
import java.io.UnsupportedEncodingException;

/**
* Checks script execution for an email. The wrapped email is set by called
* {@link #setMail}. Actions are recorded on {@link #executedActions} and can
@@ -168,7 +172,7 @@ public List<String> getHeaderNames() throws SieveMailException {
if (mail != null) {
try {
results = new ArrayList<String>();
for (final Enumeration en = mail.getAllHeaders(); en
for (final Enumeration<?> en = mail.getAllHeaders(); en
.hasMoreElements(); ) {
final Header header = (Header) en.nextElement();
final String name = header.getName();
@@ -265,7 +269,7 @@ public Address[] parseAddresses(final String headerName,
try {
final SieveAddressBuilder builder = new SieveAddressBuilder();

for (Enumeration en = message.getAllHeaders(); en.hasMoreElements(); ) {
for (Enumeration<?> en = message.getAllHeaders(); en.hasMoreElements(); ) {
final Header header = (Header) en.nextElement();
final String name = header.getName();
if (name.trim().equalsIgnoreCase(headerName)) {
@@ -0,0 +1,102 @@
/****************************************************************
* 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.jsieve.util;

import static org.assertj.core.api.Assertions.assertThat;

import java.io.ByteArrayInputStream;

import javax.mail.Session;
import javax.mail.internet.MimeMessage;

import org.apache.jsieve.ConfigurationManager;
import org.apache.jsieve.SieveFactory;
import org.apache.jsieve.mail.ActionRedirect;
import org.apache.jsieve.parser.generated.Node;
import org.apache.jsieve.util.check.ScriptCheckMailAdapter;
import org.junit.Before;
import org.junit.Test;

public class ScriptCheckMailAdapterTest {

private static final String FOLDED_SUBJECT_MATCH = "if header :is \"subject\" \"Test with long long long long long long long long long "
+ "long long long long long long long long long long long very very long folded subject email\" { "
+ "redirect \"test@test.test\";" + "}";

private static final String ENCODED_SUBJECT_MATCH = "if header :is \"subject\" \"Test però £ ù €\" { "
+ "redirect \"test@test.test\";" + "}";

private SieveFactory sieveFactory;


@Before
public void setUp() throws Exception {
this.sieveFactory = new ConfigurationManager().build();
}

@Test
public void askedActionMustBeExecutedWhenFoldedHeaderMatchs() throws Exception {

//Given
// Load test message file
MimeMessage message = new MimeMessage(Session.getDefaultInstance(System.getProperties()),
ClassLoader.getSystemResourceAsStream("org/apache/jsieve/util/FoldedSubjectEmail.eml"));

// Associate it to the adapter
ScriptCheckMailAdapter testee = new ScriptCheckMailAdapter();
testee.setMail(message);

//When
// Parse sieve script
Node parsedSieve = sieveFactory.parse(new ByteArrayInputStream(FOLDED_SUBJECT_MATCH.getBytes("UTF-8")));

// Evaluate the script against the message
sieveFactory.evaluate(testee, parsedSieve);

//Then
// Test is OK if we have the "redirect" action
assertThat(testee.getActions()).hasSize(1);
assertThat(testee.getActions().get(0)).isInstanceOf(ActionRedirect.class);
}

@Test
public void askedActionMustBeExecutedWhenEncodedHeaderMatchs() throws Exception {

//Given
// Load test message file
MimeMessage message = new MimeMessage(Session.getDefaultInstance(System.getProperties()),
ClassLoader.getSystemResourceAsStream("org/apache/jsieve/util/EncodedSubjectEmail.eml"));

// Associate it to the adapter
ScriptCheckMailAdapter testee = new ScriptCheckMailAdapter();
testee.setMail(message);

//When
// Parse sieve script
Node parsedSieve = sieveFactory.parse(new ByteArrayInputStream(ENCODED_SUBJECT_MATCH.getBytes("UTF-8")));

// Evaluate the script against the message
sieveFactory.evaluate(testee, parsedSieve);

//Then
// Test is OK if we have the "redirect" action
assertThat(testee.getActions()).hasSize(1);
assertThat(testee.getActions().get(0)).isInstanceOf(ActionRedirect.class);
}
}
@@ -0,0 +1,12 @@
Return-Path: <depetrini@libero.it>
To: Daniele Depetrini <depetrini@libero.it>
From: Daniele Depetrini <depetrini@libero.it>
Subject: =?UTF-8?B?VGVzdCBwZXLDsiDCoyDDuSDigqw=?=
Message-ID: <2325dfff-55af-9391-6a91-1213426e381e@libero.it>
Date: Wed, 4 Jan 2017 22:42:32 +0100
MIME-Version: 1.0
Content-Type: text/plain; charset=iso-8859-15
Content-Transfer-Encoding: 7bit

frff

@@ -0,0 +1,13 @@
Return-Path: <depetrini@libero.it>
To: Daniele Depetrini <depetrini@libero.it>
From: Daniele Depetrini <depetrini@libero.it>
Subject: Test with long long long long long long long long long long long long
long long long long long long long long very very long folded subject email
Message-ID: <4cb56dec-b7a4-8476-0327-6a1b52963414@libero.it>
Date: Wed, 4 Jan 2017 17:17:38 +0100
MIME-Version: 1.0
Content-Type: text/plain; charset=iso-8859-15
Content-Transfer-Encoding: 7bit

asdfasdfa

0 comments on commit 697927f

Please sign in to comment.