Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 9694f905 authored by Henrik Hall's avatar Henrik Hall Committed by Johan Redestig
Browse files

Improved error-handling in Rfc822Tokenizer

The javadoc for the Rfc822Tokenizer states that it will try
to be tolerant to broken syntax instead of returning an error
(as in an unchecked exception). In some rare cases where the
input is clearly incorrect, the tokenizer throws a
StringIndexOutOfBoundsException, which was found during
one of the monkey test runs. This commits fixes that crash,
and teaches the tokenizer to just continue to run anyway. Two
simple junit testcases has also been added for testing the
default and the errornous case.
parent 7bb2581e
Loading
Loading
Loading
Loading
+6 −2
Original line number Diff line number Diff line
@@ -86,7 +86,9 @@ public class Rfc822Tokenizer implements MultiAutoCompleteTextView.Tokenizer {
                        i++;
                        break;
                    } else if (c == '\\') {
                        if (i + 1 < cursor) {
                            name.append(text.charAt(i + 1));
                        }
                        i += 2;
                    } else {
                        name.append(c);
@@ -112,7 +114,9 @@ public class Rfc822Tokenizer implements MultiAutoCompleteTextView.Tokenizer {
                        level++;
                        i++;
                    } else if (c == '\\') {
                        if (i + 1 < cursor) {
                            comment.append(text.charAt(i + 1));
                        }
                        i += 2;
                    } else {
                        comment.append(c);
+20 −0
Original line number Diff line number Diff line
@@ -29,6 +29,8 @@ import android.text.SpannedString;
import android.text.TextPaint;
import android.text.TextUtils;
import android.text.style.StyleSpan;
import android.text.util.Rfc822Token;
import android.text.util.Rfc822Tokenizer;
import android.text.util.Rfc822Validator;
import android.test.MoreAsserts;

@@ -269,6 +271,24 @@ public class TextUtilsTest extends TestCase {
        }
    }

    @SmallTest
    public void testRfc822TokenizerFullAddress() {
        Rfc822Token[] tokens = Rfc822Tokenizer.tokenize("Foo Bar (something) <foo@google.com>");
        assertNotNull(tokens);
        assertEquals(1, tokens.length);
        assertEquals("foo@google.com", tokens[0].getAddress());
        assertEquals("Foo Bar", tokens[0].getName());
        assertEquals("something",tokens[0].getComment());
    }

    @SmallTest
    public void testRfc822TokenizeItemWithError() {
        Rfc822Token[] tokens = Rfc822Tokenizer.tokenize("\"Foo Bar\\");
        assertNotNull(tokens);
        assertEquals(1, tokens.length);
        assertEquals("Foo Bar", tokens[0].getAddress());
    }

    @LargeTest
    public void testEllipsize() {
        CharSequence s1 = "The quick brown fox jumps over \u00FEhe lazy dog.";