This is an automated email from the ASF dual-hosted git repository.
chibenwa pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/james-jsieve.git
The following commit(s) were added to refs/heads/master by this push:
new 046c74f [FIX] MailUtils: probe the case of the name looked up, let
adapters declare it (#43)
046c74f is described below
commit 046c74f38b08569a0cc56a71edf14a07841e45fb
Author: Benoit TELLIER <[email protected]>
AuthorDate: Fri Nov 27 13:04:22 2026 +0100
[FIX] MailUtils: probe the case of the name looked up, let adapters declare
it (#43)
To tell whether the getHeader of the adapter ignores case, getMatchingHeader
asked it an absent case spelling of the first family of header names that
did not hold every spelling. The mail chooses that family: holding every
spelling of the small names, it made the probe fetch the values of one
large header of another name, on every lookup, and the evaluation budget
charged only the values of the name looked up. A script testing many
names could copy a large header thousands of times within its budget.
MailAdapter#getHeaderIgnoresCase lets an adapter declare whether its
getHeader ignores case; then no probe is made. ScriptCheckMailAdapter
declares it matches the exact name; James's adapter should declare it
ignores case.
An adapter that declares nothing is probed with an absent spelling of the
family of the name looked up first. An adapter ignoring case answers the
values of that family, which the lookup answers, so they are not asked
again and the budget charges them. Only when the mail holds every case
spelling of that name, a crafted mail, is another family probed:
SieveContext
now runs the lookups within MailUtils#lookUp, so that probe is charged and
its answer kept for the evaluation.
When the mail holds every case spelling of every name, no probe can tell,
and the adapter was deemed to ignore case: one matching the exact name
lost the values of all spellings but one. Within an evaluation every
spelling is now asked, each repeated answer charged: two spellings
answering different values prove the adapter matches the exact name, and
all values are answered. When every spelling answers the same values,
those of one spelling are, losing at most their repetitions, and the
other lookups of the evaluation do not probe again. Out of an
evaluation nothing bounds that, and the adapter is still deemed to ignore
case.
MailUtils#lookUp throws the exhaustion of the budget within the lookup
even when the adapter wraps it, unchecked or not, or swallows it.
SieveContext now keeps the lookups by the ASCII upper case of the name, as
it does the parsed addresses: a name is looked up once whatever its case.
---
.../main/java/org/apache/jsieve/SieveContext.java | 25 +-
.../java/org/apache/jsieve/mail/MailAdapter.java | 17 ++
.../java/org/apache/jsieve/mail/MailUtils.java | 260 +++++++++++++++++++--
.../org/apache/jsieve/EvaluationBudgetTest.java | 173 ++++++++++++++
.../jsieve/util/check/ScriptCheckMailAdapter.java | 9 +
5 files changed, 458 insertions(+), 26 deletions(-)
diff --git a/core/src/main/java/org/apache/jsieve/SieveContext.java
b/core/src/main/java/org/apache/jsieve/SieveContext.java
index 9e9c89e..082ccce 100644
--- a/core/src/main/java/org/apache/jsieve/SieveContext.java
+++ b/core/src/main/java/org/apache/jsieve/SieveContext.java
@@ -28,6 +28,7 @@ import org.apache.jsieve.comparators.AsciiCasemap;
import org.apache.jsieve.exception.InternetAddressException;
import org.apache.jsieve.exception.SieveException;
import org.apache.jsieve.mail.MailAdapter;
+import org.apache.jsieve.mail.MailUtils;
/**
* Context for sieve operations.
@@ -132,12 +133,19 @@ public abstract class SieveContext {
}
/**
- * Header values already looked up, by the header name of the script.
+ * Header values already looked up, by the ASCII upper case of the header
+ * name of the script.
*/
private Map<String, List<String>> matchingHeaders;
/**
- * Addresses already parsed, by the header name of the script.
+ * The state the header lookups of the evaluation share in {@link
MailUtils}.
+ */
+ private MailUtils.HeaderLookups headerLookups;
+
+ /**
+ * Addresses already parsed, by the ASCII upper case of the header name of
+ * the script.
*/
private Map<String, MailAdapter.Address[]> parsedAddresses;
@@ -150,7 +158,7 @@ public abstract class SieveContext {
/**
* Looks up the values of the headers of the mail with the given name, see
* {@link MailAdapter#getMatchingHeader(String)}. A name is looked up once
- * per context, that is per evaluation. A lookup is charged to the
+ * per context, that is per evaluation, whatever its ASCII case. A lookup
is charged to the
* evaluation budget: the header names of the mail it walks, the name, and
* the values it returns.
*
@@ -163,12 +171,17 @@ public abstract class SieveContext {
if (matchingHeaders == null) {
matchingHeaders = new HashMap<>();
}
- List<String> values = matchingHeaders.get(name);
+ // The spellings of a name that differ in ASCII case are one name
+ final String key = AsciiCasemap.toAsciiUpperCase(name);
+ List<String> values = matchingHeaders.get(key);
if (values == null) {
chargeComparison(headerNamesLength(mail) + name.length() + 1L);
- values = mail.getMatchingHeader(name);
+ if (headerLookups == null) {
+ headerLookups = new
MailUtils.HeaderLookups(this::chargeComparison);
+ }
+ values = MailUtils.lookUp(headerLookups, () ->
mail.getMatchingHeader(name));
chargeComparison(length(values));
- matchingHeaders.put(name, values);
+ matchingHeaders.put(key, values);
}
return values;
}
diff --git a/core/src/main/java/org/apache/jsieve/mail/MailAdapter.java
b/core/src/main/java/org/apache/jsieve/mail/MailAdapter.java
index e367676..f05d177 100644
--- a/core/src/main/java/org/apache/jsieve/mail/MailAdapter.java
+++ b/core/src/main/java/org/apache/jsieve/mail/MailAdapter.java
@@ -20,6 +20,7 @@
package org.apache.jsieve.mail;
import java.util.List;
+import java.util.Optional;
import org.apache.jsieve.SieveContext;
import org.apache.jsieve.exception.InternetAddressException;
@@ -106,6 +107,22 @@ public interface MailAdapter {
*/
public List<String> getHeader(String name) throws SieveMailException;
+ /**
+ * Tells whether {@link #getHeader(String)} matches the name ignoring its
+ * ASCII case, as the getHeader of JavaMail does: then each case spelling
+ * of a name answers the values of all of them. An adapter that knows it
+ * should answer it: {@link MailUtils#getMatchingHeader(MailAdapter,
String)}
+ * otherwise tells it by asking getHeader a case spelling the mail does not
+ * hold, and when a crafted mail holds every case spelling of every name,
+ * by asking getHeader every spelling of the name looked up.
+ *
+ * @return whether getHeader ignores the ASCII case of the name, or empty
+ * when unknown, the default
+ */
+ default Optional<Boolean> getHeaderIgnoresCase() {
+ return Optional.empty();
+ }
+
/**
* <p>
* Method getMatchingHeader answers a List of all of the headers in the
diff --git a/core/src/main/java/org/apache/jsieve/mail/MailUtils.java
b/core/src/main/java/org/apache/jsieve/mail/MailUtils.java
index a122e3d..432b115 100644
--- a/core/src/main/java/org/apache/jsieve/mail/MailUtils.java
+++ b/core/src/main/java/org/apache/jsieve/mail/MailUtils.java
@@ -24,9 +24,11 @@ import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
+import java.util.Optional;
import java.util.Set;
import org.apache.jsieve.comparators.AsciiCasemap;
+import org.apache.jsieve.exception.SieveException;
/**
* Class MailUtils implements utility methods that are useful when processing
@@ -41,6 +43,114 @@ public class MailUtils {
super();
}
+ /**
+ * Charges the evaluation budget.
+ */
+ @FunctionalInterface
+ public interface Charge {
+ /**
+ * @param cost not negative
+ * @throws SieveException when the budget is exhausted
+ */
+ void charge(long cost) throws SieveException;
+ }
+
+ /**
+ * A lookup run by {@link #lookUp(HeaderLookups, Lookup)}.
+ */
+ @FunctionalInterface
+ public interface Lookup<T> {
+ T get() throws SieveException;
+ }
+
+ /**
+ * The header lookups of one evaluation: what they fetch beyond the values
+ * they answer is charged to its budget, and whether the getHeader of an
+ * adapter that does not declare it ignores case is probed at most once.
+ * Like the context of the evaluation, it must not be shared between
+ * threads.
+ */
+ public static final class HeaderLookups {
+ private final Charge charge;
+ private Boolean getHeaderIgnoresCase;
+ // No probe could tell: the mail holds every case spelling of every
name
+ private boolean undecided;
+ private SieveException exhausted;
+
+ /**
+ * @param charge charges the budget of the evaluation, not null
+ */
+ public HeaderLookups(Charge charge) {
+ this.charge = charge;
+ }
+ }
+
+ private static final ThreadLocal<HeaderLookups> CURRENT_LOOKUPS = new
ThreadLocal<HeaderLookups>();
+
+ /**
+ * Runs a lookup of the adapter, such as {@link
MailAdapter#getMatchingHeader(String)},
+ * on behalf of the given evaluation: the calls it makes to
+ * {@link #getMatchingHeader(MailAdapter, String)} charge its budget and
+ * share what they learnt of the adapter. When the budget is exhausted
+ * within the lookup, its exception is thrown, whatever the adapter did
+ * with it.
+ *
+ * @throws SieveException when the budget is exhausted or the mail cannot
be read
+ */
+ public static <T> T lookUp(HeaderLookups lookups, Lookup<T> lookup) throws
SieveException {
+ final HeaderLookups previous = CURRENT_LOOKUPS.get();
+ CURRENT_LOOKUPS.set(lookups);
+ try {
+ final T result = lookup.get();
+ if (lookups.exhausted != null) {
+ throw lookups.exhausted;
+ }
+ return result;
+ } catch (SieveException | RuntimeException e) {
+ if (lookups.exhausted != null) {
+ throw lookups.exhausted;
+ }
+ throw e;
+ } finally {
+ if (previous == null) {
+ CURRENT_LOOKUPS.remove();
+ } else {
+ CURRENT_LOOKUPS.set(previous);
+ }
+ }
+ }
+
+ /**
+ * Carries the exhaustion of the budget through the adapter, whose
+ * getMatchingHeader only throws SieveMailException.
+ */
+ private static final class BudgetExhausted extends SieveMailException {
+ private static final long serialVersionUID = 1L;
+
+ BudgetExhausted(SieveException budgetException) {
+ super(budgetException.getMessage());
+ }
+ }
+
+ private static void charge(HeaderLookups lookups, List<String> values)
throws SieveMailException {
+ if (lookups == null) {
+ return;
+ }
+ long cost = 0;
+ for (String value : values) {
+ cost += value.length() + 1L;
+ }
+ if (lookups.exhausted == null) {
+ try {
+ lookups.charge.charge(cost);
+ return;
+ } catch (SieveException e) {
+ lookups.exhausted = e;
+ }
+ }
+ throw new BudgetExhausted(lookups.exhausted);
+ }
+
/**
* <p>
* Method getMatchingHeader answers a List of all of the headers in the
mail
@@ -54,6 +164,11 @@ public class MailUtils {
* From" and " from " are considered equal.
* </p>
*
+ * <p>
+ * Run within {@link #lookUp(HeaderLookups, Lookup)}, the values it fetches
+ * beyond those it answers are charged to the budget of the evaluation.
+ * </p>
+ *
* @param name
* @return List
* @throws SieveMailException
@@ -72,46 +187,151 @@ public class MailUtils {
if (matchingNames.isEmpty()) {
return matchedHeaderValues;
}
- final boolean ignoresCase = matchingNames.size() > 1
- && getHeaderIgnoresCase(mail, headerNames, matchingNames.get(0));
+ if (matchingNames.size() == 1) {
+ matchedHeaderValues.addAll(mail.getHeader(matchingNames.get(0)));
+ return matchedHeaderValues;
+ }
+ final HeaderLookups lookups = CURRENT_LOOKUPS.get();
+ final String probedFamily =
AsciiCasemap.toAsciiUpperCase(matchingNames.get(0));
+ Boolean ignoresCase = mail.getHeaderIgnoresCase()
+ .orElse(lookups == null ? null : lookups.getHeaderIgnoresCase);
+ if (ignoresCase == null && lookups != null && lookups.undecided) {
+ return lookUpEachSpelling(mail, matchingNames, lookups);
+ }
+ List<String> probedValues = null;
+ if (ignoresCase == null) {
+ final Set<String> names = new HashSet<String>(headerNames);
+ probedValues = probeFamily(mail, headerNames, names, probedFamily);
+ if (probedValues == null) {
+ ignoresCase = probeOtherFamilies(mail, headerNames, names,
probedFamily, lookups);
+ } else {
+ ignoresCase = !probedValues.isEmpty();
+ }
+ if (ignoresCase == null && lookups != null) {
+ return lookUpEachSpelling(mail, matchingNames, lookups);
+ }
+ if (ignoresCase == null) {
+ // Out of an evaluation, nothing bounds asking every spelling
+ ignoresCase = true;
+ } else if (lookups != null) {
+ lookups.getHeaderIgnoresCase = ignoresCase;
+ }
+ }
// Spellings differing in their whitespace are asked each, whatever
the adapter
final Set<String> askedSpellings = new HashSet<String>();
for (String headerName : matchingNames) {
- if (!ignoresCase ||
askedSpellings.add(AsciiCasemap.toAsciiUpperCase(headerName))) {
+ final String family = AsciiCasemap.toAsciiUpperCase(headerName);
+ if (!ignoresCase) {
matchedHeaderValues.addAll(mail.getHeader(headerName));
+ } else if (askedSpellings.add(family)) {
+ if (probedValues != null && family.equals(probedFamily)) {
+ matchedHeaderValues.addAll(probedValues);
+ } else {
+ matchedHeaderValues.addAll(mail.getHeader(headerName));
+ }
}
}
return matchedHeaderValues;
}
/**
- * Whether the getHeader of the adapter ignores the case of the name, as
- * the one of JavaMail does: then each spelling of a name answers the
- * values of all of them, and asking each of k spellings would answer k
- * times k values. The adapter is asked a case spelling of a header name
- * that the mail does not hold: an adapter matching the exact name answers
- * no value for it. The spellings of the given name are tried first, then
- * those of the other names, each family of spellings at most as many
- * times as it has members, so that this is linear in the header names.
- * When the mail holds every case spelling of every name, which no mail
- * but a crafted one does, the adapter is deemed to ignore case: only the
- * values of one spelling are looked up.
+ * Tells, for an adapter that does not declare it, whether its getHeader
+ * ignores the case of the name, as the one of JavaMail does: then each
+ * spelling of a name answers the values of all of them, and asking each
+ * of k spellings would answer k times k values. The adapter is asked a
+ * case spelling of the given family that the mail does not hold: an
+ * adapter matching the exact name answers no value for it, one ignoring
+ * case answers the values of the family, which the lookup answers and
+ * then does not ask again. Finding the absent spelling tries at most as
+ * many spellings as the family has members, so this is linear in the
+ * header names.
+ *
+ * @return the values answered for the absent spelling, or null when the
+ * mail holds every case spelling of the family
*/
- private static boolean getHeaderIgnoresCase(MailAdapter mail, List<String>
headerNames, String name)
- throws SieveMailException {
- final Set<String> names = new HashSet<String>(headerNames);
+ private static List<String> probeFamily(MailAdapter mail, List<String>
headerNames, Set<String> names,
+ String family) throws SieveMailException {
+ int members = 0;
+ for (String headerName : headerNames) {
+ if (AsciiCasemap.toAsciiUpperCase(headerName).equals(family)) {
+ members++;
+ }
+ }
+ final String absent = absentSpelling(family, members, names);
+ return absent == null ? null : mail.getHeader(absent);
+ }
+
+ /**
+ * Tells whether the getHeader of an adapter that does not declare it
+ * ignores case, when the mail holds every case spelling of the name looked
+ * up, which no mail but a crafted one does: an absent spelling of the
+ * first other family that has one is asked. Its values are not those of
+ * the name looked up: they are charged, and the answer is kept for the
+ * other lookups of the evaluation.
+ *
+ * @return whether getHeader ignores case, or null when the mail holds
+ * every case spelling of every name
+ */
+ private static Boolean probeOtherFamilies(MailAdapter mail, List<String>
headerNames, Set<String> names,
+ String probedFamily, HeaderLookups lookups) throws
SieveMailException {
final Map<String, Integer> families = new LinkedHashMap<String,
Integer>();
- families.put(AsciiCasemap.toAsciiUpperCase(name), 0);
for (String headerName : headerNames) {
families.merge(AsciiCasemap.toAsciiUpperCase(headerName), 1,
Integer::sum);
}
+ families.remove(probedFamily);
for (Map.Entry<String, Integer> family : families.entrySet()) {
final String absent = absentSpelling(family.getKey(),
family.getValue(), names);
if (absent != null) {
- return !mail.getHeader(absent).isEmpty();
+ final List<String> values = mail.getHeader(absent);
+ charge(lookups, values);
+ return !values.isEmpty();
+ }
+ }
+ return null;
+ }
+
+ /**
+ * Looks up the values of every spelling, when the mail holds every case
+ * spelling of every name and no probe can tell whether the getHeader of
+ * the adapter ignores case. Every spelling answering the values another
+ * spelling of its family answered is charged as it is: an adapter ignoring
+ * case answers the values of the whole family to each of its k spellings.
+ * The other values, answered, are charged by the evaluation. Two
spellings of a family answering different
+ * values prove the adapter matches the exact name: the values of every
+ * spelling are answered. When every spelling of each family answers the
+ * same values, both adapters answer each value: those of one spelling of
+ * each family are answered, and an adapter matching the exact name loses
+ * only their repetitions, and the other lookups of the evaluation ask
+ * every spelling without probing again.
+ */
+ private static List<String> lookUpEachSpelling(MailAdapter mail,
List<String> matchingNames,
+ HeaderLookups lookups) throws SieveMailException {
+ final Map<String, List<String>> familyValues = new
LinkedHashMap<String, List<String>>();
+ final List<List<String>> spellingValues = new
ArrayList<List<String>>();
+ boolean exactName = false;
+ for (String headerName : matchingNames) {
+ final List<String> values = mail.getHeader(headerName);
+ final List<String> first =
familyValues.putIfAbsent(AsciiCasemap.toAsciiUpperCase(headerName), values);
+ if (first != null && first.equals(values)) {
+ charge(lookups, values);
+ // Kept once: the repetitions of an adapter ignoring case hold
no memory
+ spellingValues.add(first);
+ } else {
+ exactName |= first != null;
+ spellingValues.add(values);
}
}
- return true;
+ if (exactName) {
+ lookups.getHeaderIgnoresCase = false;
+ } else {
+ // The other lookups of the evaluation do not probe again
+ lookups.undecided = true;
+ }
+ final List<String> matchedHeaderValues = new ArrayList<String>(32);
+ for (List<String> values : exactName ? spellingValues :
familyValues.values()) {
+ matchedHeaderValues.addAll(values);
+ }
+ return matchedHeaderValues;
}
/**
diff --git a/core/src/test/java/org/apache/jsieve/EvaluationBudgetTest.java
b/core/src/test/java/org/apache/jsieve/EvaluationBudgetTest.java
index 113c5e5..08a4d2a 100644
--- a/core/src/test/java/org/apache/jsieve/EvaluationBudgetTest.java
+++ b/core/src/test/java/org/apache/jsieve/EvaluationBudgetTest.java
@@ -28,6 +28,7 @@ import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
+import java.util.Optional;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
@@ -204,6 +205,121 @@ public class EvaluationBudgetTest {
assertThat(mail.getActions()).singleElement().isInstanceOf(ActionDiscard.class);
}
+ @Test(timeout = 10_000)
+ public void anAdapterDeclaringItIgnoresCaseShouldNotBeProbed() throws
Exception {
+ GetHeaderCountingMailAdapter mail = new
GetHeaderCountingMailAdapter(Optional.of(true));
+ StringBuilder names = new StringBuilder();
+ for (int i = 0; i < 1_000; i++) {
+ mail.getMessage().addHeader("A-" + i, "upper");
+ mail.getMessage().addHeader("a-" + i, "lower");
+ names.append(i == 0 ? "" : ",
").append("\"A-").append(i).append("\", \"a-").append(i).append('"');
+ }
+ mail.getMessage().addHeader("B", "b".repeat(1_000_000));
+
+ factoryWithBudget(100_000_000).interpret(mail, script("if exists [" +
names + "] { discard; }"));
+
+
assertThat(mail.getActions()).singleElement().isInstanceOf(ActionDiscard.class);
+ assertThat(mail.returnedCharacters).isLessThan(1_000_000);
+ }
+
+ @Test(timeout = 10_000)
+ public void
anotherNameShouldBeProbedOncePerEvaluationWhenTheAdapterDoesNotDeclareItsCase()
throws Exception {
+ GetHeaderCountingMailAdapter mail = new
GetHeaderCountingMailAdapter(Optional.empty());
+ StringBuilder names = new StringBuilder();
+ for (int i = 0; i < 1_000; i++) {
+ mail.getMessage().addHeader("A-" + i, "upper");
+ mail.getMessage().addHeader("a-" + i, "lower");
+ names.append(i == 0 ? "" : ",
").append("\"A-").append(i).append('"');
+ }
+ mail.getMessage().addHeader("B", "b".repeat(1_000_000));
+
+ factoryWithBudget(100_000_000).interpret(mail, script("if exists [" +
names + "] { discard; }"));
+
+
assertThat(mail.getActions()).singleElement().isInstanceOf(ActionDiscard.class);
+ assertThat(mail.returnedCharacters).isLessThan(2_000_000);
+ }
+
+ @Test
+ public void
everySpellingShouldBeLookedUpWhenNoNameCanBeProbedAndTheAdapterMatchesTheExactName()
throws Exception {
+ SieveMailAdapter mail = (SieveMailAdapter) JUnitUtils.createMail();
+ mail.getMessage().addHeader("X", "a");
+ mail.getMessage().addHeader("x", "b");
+
+ factoryWithBudget(10_000_000).interpret(mail, script("if allof (header
:is \"x\" \"a\", header :is \"X\" \"b\") { discard; }"));
+
+
assertThat(mail.getActions()).singleElement().isInstanceOf(ActionDiscard.class);
+ }
+
+ @Test
+ public void
eachValueShouldBeLookedUpOnceWhenNoNameCanBeProbedAndTheAdapterIgnoresCase()
throws Exception {
+ SieveMailAdapter mail = new CaseInsensitiveMailAdapter();
+ mail.getMessage().addHeader("X", "a");
+ mail.getMessage().addHeader("x", "b");
+
+ assertThat(MailUtils.getMatchingHeader(mail,
"x")).containsExactlyInAnyOrder("a", "b");
+ }
+
+ @Test(timeout = 10_000)
+ public void lookingUpEverySpellingShouldBeChargedWhenNoNameCanBeProbed()
throws Exception {
+ SieveMailAdapter mail = mailWithSpellings(new
CaseInsensitiveMailAdapter(), "XXXXXXXXXXXX", 4_096);
+
+ assertThatThrownBy(() ->
factoryWithBudget(100_000_000).interpret(mail, script("if exists
\"XXXXXXXXXXXX\" { discard; }")))
+ .isInstanceOf(SieveException.class)
+ .hasMessageContaining("budget");
+ }
+
+ @Test(timeout = 10_000)
+ public void
anExhaustedBudgetShouldFailTheEvaluationWhenTheAdapterSwallowsIt() throws
Exception {
+ SieveMailAdapter mail = mailWithSpellings(new SwallowingMailAdapter(),
"XXXXXXXXXXXX", 4_096);
+
+ assertThatThrownBy(() ->
factoryWithBudget(100_000_000).interpret(mail, script("if exists
\"XXXXXXXXXXXX\" { discard; }")))
+ .isInstanceOf(SieveException.class)
+ .hasMessageContaining("budget");
+ }
+
+ @Test(timeout = 10_000)
+ public void
anExhaustedBudgetShouldFailTheEvaluationWhenTheAdapterWrapsItUnchecked() throws
Exception {
+ SieveMailAdapter mail = mailWithSpellings(new UncheckedMailAdapter(),
"XXXXXXXXXXXX", 4_096);
+
+ assertThatThrownBy(() ->
factoryWithBudget(100_000_000).interpret(mail, script("if exists
\"XXXXXXXXXXXX\" { discard; }")))
+ .isInstanceOf(SieveException.class)
+ .hasMessageContaining("budget");
+ }
+
+ @Test(timeout = 10_000)
+ public void everySpellingShouldNotBeAskedOutOfAnEvaluation() throws
Exception {
+ GetHeaderCountingMailAdapter mail = new
GetHeaderCountingMailAdapter(Optional.empty());
+ mailWithSpellings(mail, "XXXXXXXXXXXX", 4_096);
+
+ assertThat(MailUtils.getMatchingHeader(mail,
"XXXXXXXXXXXX")).hasSize(4_096);
+ assertThat(mail.calls).isEqualTo(1);
+ }
+
+ @Test
+ public void
theProbedValuesShouldBeTheValuesLookedUpWhenTheAdapterIgnoresCase() throws
Exception {
+ GetHeaderCountingMailAdapter mail = new
GetHeaderCountingMailAdapter(Optional.empty());
+ mail.getMessage().addHeader("X-Spam", "yes");
+ mail.getMessage().addHeader("x-spam", "no");
+ mail.getMessage().addHeader("B", "b".repeat(1_000));
+
+ assertThat(MailUtils.getMatchingHeader(mail,
"x-spam")).containsExactlyInAnyOrder("yes", "no");
+ assertThat(mail.calls).isEqualTo(1);
+ assertThat(mail.returnedCharacters).isEqualTo(5);
+ }
+
+ @Test
+ public void
everyCaseSpellingOfANameShouldBeLookedUpWhenTheAdapterMatchesTheExactName()
throws Exception {
+ SieveMailAdapter mail = (SieveMailAdapter) JUnitUtils.createMail();
+ mail.getMessage().addHeader("To", "[email protected]");
+ mail.getMessage().addHeader("TO", "[email protected]");
+ mail.getMessage().addHeader("tO", "[email protected]");
+ mail.getMessage().addHeader("to", "[email protected]");
+ mail.getMessage().addHeader("Subject", "hello");
+
+ assertThat(MailUtils.getMatchingHeader(mail, "to"))
+ .containsExactlyInAnyOrder("[email protected]", "[email protected]",
"[email protected]", "[email protected]");
+ }
+
@Test
public void
theSpellingsOfANameShouldEachBeLookedUpWhenTheAdapterMatchesTheExactName()
throws Exception {
SieveMailAdapter mail = mailWithSpellings((SieveMailAdapter)
JUnitUtils.createMail(), "XXXXXXXXXXXXXX", 100);
@@ -426,6 +542,63 @@ public class EvaluationBudgetTest {
}
}
+ /**
+ * Ignores case, and answers no value when a lookup fails.
+ */
+ private static class SwallowingMailAdapter extends
CaseInsensitiveMailAdapter {
+ @Override
+ public List<String> getMatchingHeader(String name) {
+ try {
+ return super.getMatchingHeader(name);
+ } catch (SieveMailException e) {
+ return Collections.emptyList();
+ }
+ }
+ }
+
+ /**
+ * Ignores case, and wraps the failure of a lookup in an unchecked
exception.
+ */
+ private static class UncheckedMailAdapter extends
CaseInsensitiveMailAdapter {
+ @Override
+ public List<String> getMatchingHeader(String name) {
+ try {
+ return super.getMatchingHeader(name);
+ } catch (SieveMailException e) {
+ throw new IllegalStateException(e);
+ }
+ }
+ }
+
+ /**
+ * Ignores case, declares it or not, and counts the calls of getHeader and
+ * the characters they return.
+ */
+ private static class GetHeaderCountingMailAdapter extends
CaseInsensitiveMailAdapter {
+ private final Optional<Boolean> declaredIgnoresCase;
+ long returnedCharacters;
+ int calls;
+
+ GetHeaderCountingMailAdapter(Optional<Boolean> declaredIgnoresCase) {
+ this.declaredIgnoresCase = declaredIgnoresCase;
+ }
+
+ @Override
+ public Optional<Boolean> getHeaderIgnoresCase() {
+ return declaredIgnoresCase;
+ }
+
+ @Override
+ public List<String> getHeader(String name) throws SieveMailException {
+ calls++;
+ List<String> values = super.getHeader(name);
+ for (String value : values) {
+ returnedCharacters += value.length();
+ }
+ return values;
+ }
+ }
+
/**
* Answers the fields of every case of the name, as JavaMail does and as
* James's adapter relies on.
diff --git
a/util/src/main/java/org/apache/jsieve/util/check/ScriptCheckMailAdapter.java
b/util/src/main/java/org/apache/jsieve/util/check/ScriptCheckMailAdapter.java
index b6a96b8..fc0a78d 100644
---
a/util/src/main/java/org/apache/jsieve/util/check/ScriptCheckMailAdapter.java
+++
b/util/src/main/java/org/apache/jsieve/util/check/ScriptCheckMailAdapter.java
@@ -25,6 +25,7 @@ import java.util.Collections;
import java.util.Enumeration;
import java.util.LinkedList;
import java.util.List;
+import java.util.Optional;
import java.util.regex.Pattern;
import javax.mail.Header;
@@ -185,6 +186,14 @@ public class ScriptCheckMailAdapter implements MailAdapter
{
return result;
}
+ /**
+ * getHeader matches the exact name.
+ */
+ @Override
+ public Optional<Boolean> getHeaderIgnoresCase() {
+ return Optional.of(false);
+ }
+
/**
* Method getHeaderNames answers a List of all of the headers in the
* receiver. No duplicates are allowed.
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]