1
0
mirror of https://github.com/moparisthebest/k-9 synced 2024-11-27 11:42:16 -05:00
This commit is contained in:
Jan Berkel 2014-12-23 10:13:57 +01:00
parent c96a11212e
commit fe8e779b32
7 changed files with 155 additions and 125 deletions

View File

@ -1,17 +1,20 @@
package com.fsck.k9.mail; package com.fsck.k9.mail;
import org.junit.Test; import org.junit.Test;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
public class AddressTest { public class AddressTest {
/** /**
* test the possibility to parse "From:" fields with no email. * test the possibility to parse "From:" fields with no email.
* for example: From: News for Vector Limited - Google Finance * for example: From: News for Vector Limited - Google Finance
* http://code.google.com/p/k9mail/issues/detail?id=3814 * http://code.google.com/p/k9mail/issues/detail?id=3814
*/ */
@Test public void testParseWithMissingEmail() { @Test
Address[] addresses = Address.parse("NAME ONLY"); public void testParseWithMissingEmail() {
Address[] addresses = Address.parse("NAME ONLY");
assertEquals(1, addresses.length); assertEquals(1, addresses.length);
assertEquals(null, addresses[0].getAddress()); assertEquals(null, addresses[0].getAddress());
assertEquals("NAME ONLY", addresses[0].getPersonal()); assertEquals("NAME ONLY", addresses[0].getPersonal());
@ -20,17 +23,20 @@ public class AddressTest {
/** /**
* test name + valid email * test name + valid email
*/ */
@Test public void testPraseWithValidEmail() { @Test
Address[] addresses = Address.parse("Max Mustermann <maxmuster@mann.com>"); public void testPraseWithValidEmail() {
Address[] addresses = Address.parse("Max Mustermann <maxmuster@mann.com>");
assertEquals(1, addresses.length); assertEquals(1, addresses.length);
assertEquals("maxmuster@mann.com", addresses[0].getAddress()); assertEquals("maxmuster@mann.com", addresses[0].getAddress());
assertEquals("Max Mustermann", addresses[0].getPersonal()); assertEquals("Max Mustermann", addresses[0].getPersonal());
} }
/** /**
* test with multi email addresses * test with multi email addresses
*/ */
@Test public void testPraseWithValidEmailMulti() { @Test
Address[] addresses = Address.parse("lorem@ipsum.us,mark@twain.com"); public void testPraseWithValidEmailMulti() {
Address[] addresses = Address.parse("lorem@ipsum.us,mark@twain.com");
assertEquals(2, addresses.length); assertEquals(2, addresses.length);
assertEquals("lorem@ipsum.us", addresses[0].getAddress()); assertEquals("lorem@ipsum.us", addresses[0].getAddress());
assertEquals(null, addresses[0].getPersonal()); assertEquals(null, addresses[0].getPersonal());

View File

@ -5,9 +5,10 @@ import org.junit.Test;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
public class Address_quoteAtoms
{ public class Address_quoteAtoms {
@Test public void testNoQuote() { @Test
public void testNoQuote() {
// Alpha // Alpha
noQuote("a"); noQuote("a");
noQuote("aa"); noQuote("aa");

View File

@ -8,7 +8,8 @@ import java.io.ByteArrayOutputStream;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
public class EOLConvertingOutputStreamTest {
public class EOLConvertingOutputStreamTest {
private EOLConvertingOutputStream subject; private EOLConvertingOutputStream subject;
private ByteArrayOutputStream out; private ByteArrayOutputStream out;
@ -18,37 +19,43 @@ public class EOLConvertingOutputStreamTest {
subject = new EOLConvertingOutputStream(out); subject = new EOLConvertingOutputStream(out);
} }
@Test public void testFilterWithoutCRorLF() throws Exception { @Test
public void testFilterWithoutCRorLF() throws Exception {
subject.write("Unchanged".getBytes()); subject.write("Unchanged".getBytes());
subject.flush(); subject.flush();
assertEquals("Unchanged", out.toString()); assertEquals("Unchanged", out.toString());
} }
@Test public void testFilterWithCRLF() throws Exception { @Test
public void testFilterWithCRLF() throws Exception {
subject.write("Filter\r\nNext Line".getBytes()); subject.write("Filter\r\nNext Line".getBytes());
subject.flush(); subject.flush();
assertEquals("Filter\r\nNext Line", out.toString()); assertEquals("Filter\r\nNext Line", out.toString());
} }
@Test public void testFilterWithJustCR() throws Exception { @Test
public void testFilterWithJustCR() throws Exception {
subject.write("\n\n\n".getBytes()); subject.write("\n\n\n".getBytes());
subject.flush(); subject.flush();
assertEquals("\r\n\r\n\r\n", out.toString()); assertEquals("\r\n\r\n\r\n", out.toString());
} }
@Test public void testFilterWithCR() throws Exception { @Test
public void testFilterWithCR() throws Exception {
subject.write("Filter\rNext Line".getBytes()); subject.write("Filter\rNext Line".getBytes());
subject.flush(); subject.flush();
assertEquals("Filter\r\nNext Line", out.toString()); assertEquals("Filter\r\nNext Line", out.toString());
} }
@Test public void testFilterWithLF() throws Exception { @Test
public void testFilterWithLF() throws Exception {
subject.write("Filter\nNext Line".getBytes()); subject.write("Filter\nNext Line".getBytes());
subject.flush(); subject.flush();
assertEquals("Filter\r\nNext Line", out.toString()); assertEquals("Filter\r\nNext Line", out.toString());
} }
@Test public void testFlushWithCR() throws Exception { @Test
public void testFlushWithCR() throws Exception {
subject.write("Flush\r".getBytes()); subject.write("Flush\r".getBytes());
subject.flush(); subject.flush();
assertEquals("Flush\r\n", out.toString()); assertEquals("Flush\r\n", out.toString());
@ -56,14 +63,16 @@ public class EOLConvertingOutputStreamTest {
assertEquals("Flush\r\n\r\n\r\n", out.toString()); assertEquals("Flush\r\n\r\n\r\n", out.toString());
} }
@Test public void testFlushWithCRNotFollowedByLF() throws Exception { @Test
public void testFlushWithCRNotFollowedByLF() throws Exception {
subject.write("Flush\r".getBytes()); subject.write("Flush\r".getBytes());
subject.flush(); subject.flush();
subject.write("Next line".getBytes()); subject.write("Next line".getBytes());
assertEquals("Flush\r\nNext line", out.toString()); assertEquals("Flush\r\nNext line", out.toString());
} }
@Test public void testFlushWithLF() throws Exception { @Test
public void testFlushWithLF() throws Exception {
subject.write("Flush\n".getBytes()); subject.write("Flush\n".getBytes());
subject.flush(); subject.flush();
subject.write("\n".getBytes()); subject.write("\n".getBytes());

View File

@ -5,9 +5,11 @@ import org.junit.Test;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
public class CharsetSupportTest {
@Test public void testFixupCharset() throws Exception { public class CharsetSupportTest {
@Test
public void testFixupCharset() throws Exception {
String charsetOnMail; String charsetOnMail;
String expect; String expect;
@ -21,7 +23,7 @@ public class CharsetSupportTest {
MimeMessage message; MimeMessage message;
message= new MimeMessage(); message = new MimeMessage();
message.setHeader("From", "aaa@docomo.ne.jp"); message.setHeader("From", "aaa@docomo.ne.jp");
charsetOnMail = "shift_jis"; charsetOnMail = "shift_jis";
expect = "x-docomo-shift_jis-2007"; expect = "x-docomo-shift_jis-2007";

View File

@ -5,9 +5,11 @@ import org.junit.Test;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
public class DecoderUtilTest { public class DecoderUtilTest {
@Test public void testDecodeEncodedWords() { @Test
public void testDecodeEncodedWords() {
String body, expect; String body, expect;
MimeMessage message; MimeMessage message;
@ -47,7 +49,8 @@ public class DecoderUtilTest {
assertEquals(expect, DecoderUtil.decodeEncodedWords(body, message)); assertEquals(expect, DecoderUtil.decodeEncodedWords(body, message));
body = "=??q??="; body = "=??q??=";
expect = "=??q??=";; expect = "=??q??=";
;
message = null; message = null;
assertEquals(expect, DecoderUtil.decodeEncodedWords(body, message)); assertEquals(expect, DecoderUtil.decodeEncodedWords(body, message));

View File

@ -1,5 +1,6 @@
package com.fsck.k9.mail.internet; package com.fsck.k9.mail.internet;
import java.io.ByteArrayInputStream; import java.io.ByteArrayInputStream;
import java.io.File; import java.io.File;
import java.io.InputStream; import java.io.InputStream;
@ -21,22 +22,23 @@ import com.fsck.k9.mail.Multipart;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
public class MimeMessageParseTest { public class MimeMessageParseTest {
@Before @Before
public void setup() { public void setup() {
BinaryTempFileBody.setTempDirectory(new File(System.getProperty("java.io.tmpdir"))); BinaryTempFileBody.setTempDirectory(new File(System.getProperty("java.io.tmpdir")));
} }
@Test public void testSinglePart7BitNoRecurse() throws Exception { @Test
public void testSinglePart7BitNoRecurse() throws Exception {
MimeMessage msg = parseWithoutRecurse(toStream( MimeMessage msg = parseWithoutRecurse(toStream(
"From: <adam@example.org>\r\n" + "From: <adam@example.org>\r\n" +
"To: <eva@example.org>\r\n" + "To: <eva@example.org>\r\n" +
"Subject: Testmail\r\n" + "Subject: Testmail\r\n" +
"MIME-Version: 1.0\r\n" + "MIME-Version: 1.0\r\n" +
"Content-type: text/plain\r\n" + "Content-type: text/plain\r\n" +
"Content-Transfer-Encoding: 7bit\r\n" + "Content-Transfer-Encoding: 7bit\r\n" +
"\r\n" + "\r\n" +
"this is some test text.")); "this is some test text."));
checkAddresses(msg.getFrom(), "adam@example.org"); checkAddresses(msg.getFrom(), "adam@example.org");
checkAddresses(msg.getRecipients(RecipientType.TO), "eva@example.org"); checkAddresses(msg.getRecipients(RecipientType.TO), "eva@example.org");
@ -45,16 +47,17 @@ public class MimeMessageParseTest {
assertEquals("this is some test text.", streamToString(msg.getBody().getInputStream())); assertEquals("this is some test text.", streamToString(msg.getBody().getInputStream()));
} }
@Test public void testSinglePart8BitRecurse() throws Exception { @Test
public void testSinglePart8BitRecurse() throws Exception {
MimeMessage msg = parseWithRecurse(toStream( MimeMessage msg = parseWithRecurse(toStream(
"From: <adam@example.org>\r\n" + "From: <adam@example.org>\r\n" +
"To: <eva@example.org>\r\n" + "To: <eva@example.org>\r\n" +
"Subject: Testmail\r\n" + "Subject: Testmail\r\n" +
"MIME-Version: 1.0\r\n" + "MIME-Version: 1.0\r\n" +
"Content-type: text/plain; encoding=ISO-8859-1\r\n" + "Content-type: text/plain; encoding=ISO-8859-1\r\n" +
"Content-Transfer-Encoding: 8bit\r\n" + "Content-Transfer-Encoding: 8bit\r\n" +
"\r\n" + "\r\n" +
"gefährliche Umlaute")); "gefährliche Umlaute"));
checkAddresses(msg.getFrom(), "adam@example.org"); checkAddresses(msg.getFrom(), "adam@example.org");
checkAddresses(msg.getRecipients(RecipientType.TO), "eva@example.org"); checkAddresses(msg.getRecipients(RecipientType.TO), "eva@example.org");
@ -63,16 +66,17 @@ public class MimeMessageParseTest {
assertEquals("gefährliche Umlaute", streamToString(msg.getBody().getInputStream())); assertEquals("gefährliche Umlaute", streamToString(msg.getBody().getInputStream()));
} }
@Test public void testSinglePartBase64NoRecurse() throws Exception { @Test
public void testSinglePartBase64NoRecurse() throws Exception {
MimeMessage msg = parseWithoutRecurse(toStream( MimeMessage msg = parseWithoutRecurse(toStream(
"From: <adam@example.org>\r\n" + "From: <adam@example.org>\r\n" +
"To: <eva@example.org>\r\n" + "To: <eva@example.org>\r\n" +
"Subject: Testmail\r\n" + "Subject: Testmail\r\n" +
"MIME-Version: 1.0\r\n" + "MIME-Version: 1.0\r\n" +
"Content-type: text/plain\r\n" + "Content-type: text/plain\r\n" +
"Content-Transfer-Encoding: base64\r\n" + "Content-Transfer-Encoding: base64\r\n" +
"\r\n" + "\r\n" +
"dGhpcyBpcyBzb21lIG1vcmUgdGVzdCB0ZXh0Lg==\r\n")); "dGhpcyBpcyBzb21lIG1vcmUgdGVzdCB0ZXh0Lg==\r\n"));
checkAddresses(msg.getFrom(), "adam@example.org"); checkAddresses(msg.getFrom(), "adam@example.org");
checkAddresses(msg.getRecipients(RecipientType.TO), "eva@example.org"); checkAddresses(msg.getRecipients(RecipientType.TO), "eva@example.org");
@ -81,26 +85,27 @@ public class MimeMessageParseTest {
assertEquals("this is some more test text.", streamToString(msg.getBody().getInputStream())); assertEquals("this is some more test text.", streamToString(msg.getBody().getInputStream()));
} }
@Test public void testMultipartSingleLayerNoRecurse() throws Exception { @Test
public void testMultipartSingleLayerNoRecurse() throws Exception {
MimeMessage msg = parseWithoutRecurse(toStream( MimeMessage msg = parseWithoutRecurse(toStream(
"From: <x@example.org>\r\n" + "From: <x@example.org>\r\n" +
"To: <y@example.org>\r\n" + "To: <y@example.org>\r\n" +
"Subject: Testmail 2\r\n" + "Subject: Testmail 2\r\n" +
"MIME-Version: 1.0\n" + "MIME-Version: 1.0\n" +
"Content-Type: multipart/mixed; boundary=frontier\n" + "Content-Type: multipart/mixed; boundary=frontier\n" +
"\n" + "\n" +
"This is a message with multiple parts in MIME format.\n" + "This is a message with multiple parts in MIME format.\n" +
"--frontier\n" + "--frontier\n" +
"Content-Type: text/plain\n" + "Content-Type: text/plain\n" +
"\n" + "\n" +
"This is the body of the message.\n" + "This is the body of the message.\n" +
"--frontier\n" + "--frontier\n" +
"Content-Type: application/octet-stream\n" + "Content-Type: application/octet-stream\n" +
"Content-Transfer-Encoding: base64\n" + "Content-Transfer-Encoding: base64\n" +
"\n" + "\n" +
"PGh0bWw+CiAgPGhlYWQ+CiAgPC9oZWFkPgogIDxib2R5PgogICAgPHA+VGhpcyBpcyB0aGUg\n" + "PGh0bWw+CiAgPGhlYWQ+CiAgPC9oZWFkPgogIDxib2R5PgogICAgPHA+VGhpcyBpcyB0aGUg\n" +
"Ym9keSBvZiB0aGUgbWVzc2FnZS48L3A+CiAgPC9ib2R5Pgo8L2h0bWw+Cg=\n" + "Ym9keSBvZiB0aGUgbWVzc2FnZS48L3A+CiAgPC9ib2R5Pgo8L2h0bWw+Cg=\n" +
"--frontier--")); "--frontier--"));
checkAddresses(msg.getFrom(), "x@example.org"); checkAddresses(msg.getFrom(), "x@example.org");
checkAddresses(msg.getRecipients(RecipientType.TO), "y@example.org"); checkAddresses(msg.getRecipients(RecipientType.TO), "y@example.org");
@ -109,35 +114,36 @@ public class MimeMessageParseTest {
checkLeafParts(msg, checkLeafParts(msg,
"This is the body of the message.", "This is the body of the message.",
"<html>\n" + "<html>\n" +
" <head>\n" + " <head>\n" +
" </head>\n" + " </head>\n" +
" <body>\n" + " <body>\n" +
" <p>This is the body of the message.</p>\n" + " <p>This is the body of the message.</p>\n" +
" </body>\n" + " </body>\n" +
"</html>\n" + "</html>\n" +
""); "");
} }
@Test public void testMultipartSingleLayerRecurse() throws Exception { @Test
public void testMultipartSingleLayerRecurse() throws Exception {
MimeMessage msg = parseWithRecurse(toStream( MimeMessage msg = parseWithRecurse(toStream(
"From: <x@example.org>\r\n" + "From: <x@example.org>\r\n" +
"To: <y@example.org>\r\n" + "To: <y@example.org>\r\n" +
"Subject: Testmail 2\r\n" + "Subject: Testmail 2\r\n" +
"MIME-Version: 1.0\n" + "MIME-Version: 1.0\n" +
"Content-Type: multipart/mixed; boundary=frontier\n" + "Content-Type: multipart/mixed; boundary=frontier\n" +
"\n" + "\n" +
"This is a message with multiple parts in MIME format.\n" + "This is a message with multiple parts in MIME format.\n" +
"--frontier\n" + "--frontier\n" +
"Content-Type: text/plain\n" + "Content-Type: text/plain\n" +
"\n" + "\n" +
"This is the body of the message.\n" + "This is the body of the message.\n" +
"--frontier\n" + "--frontier\n" +
"Content-Type: application/octet-stream\n" + "Content-Type: application/octet-stream\n" +
"Content-Transfer-Encoding: base64\n" + "Content-Transfer-Encoding: base64\n" +
"\n" + "\n" +
"PGh0bWw+CiAgPGhlYWQ+CiAgPC9oZWFkPgogIDxib2R5PgogICAgPHA+VGhpcyBpcyB0aGUg\n" + "PGh0bWw+CiAgPGhlYWQ+CiAgPC9oZWFkPgogIDxib2R5PgogICAgPHA+VGhpcyBpcyB0aGUg\n" +
"Ym9keSBvZiB0aGUgbWVzc2FnZS48L3A+CiAgPC9ib2R5Pgo8L2h0bWw+Cg=\n" + "Ym9keSBvZiB0aGUgbWVzc2FnZS48L3A+CiAgPC9ib2R5Pgo8L2h0bWw+Cg=\n" +
"--frontier--")); "--frontier--"));
checkAddresses(msg.getFrom(), "x@example.org"); checkAddresses(msg.getFrom(), "x@example.org");
checkAddresses(msg.getRecipients(RecipientType.TO), "y@example.org"); checkAddresses(msg.getRecipients(RecipientType.TO), "y@example.org");
@ -146,41 +152,42 @@ public class MimeMessageParseTest {
checkLeafParts(msg, checkLeafParts(msg,
"This is the body of the message.", "This is the body of the message.",
"<html>\n" + "<html>\n" +
" <head>\n" + " <head>\n" +
" </head>\n" + " </head>\n" +
" <body>\n" + " <body>\n" +
" <p>This is the body of the message.</p>\n" + " <p>This is the body of the message.</p>\n" +
" </body>\n" + " </body>\n" +
"</html>\n" + "</html>\n" +
""); "");
} }
@Test public void testMultipartTwoLayersRecurse() throws Exception { @Test
public void testMultipartTwoLayersRecurse() throws Exception {
MimeMessage msg = parseWithRecurse(toStream( MimeMessage msg = parseWithRecurse(toStream(
"From: <x@example.org>\r\n" + "From: <x@example.org>\r\n" +
"To: <y@example.org>\r\n" + "To: <y@example.org>\r\n" +
"Subject: Testmail 2\r\n" + "Subject: Testmail 2\r\n" +
"MIME-Version: 1.0\n" + "MIME-Version: 1.0\n" +
"Content-Type: multipart/mixed; boundary=1\n" + "Content-Type: multipart/mixed; boundary=1\n" +
"\n" + "\n" +
"This is a message with multiple parts in MIME format.\n" + "This is a message with multiple parts in MIME format.\n" +
"--1\n" + "--1\n" +
"Content-Type: text/plain\n" + "Content-Type: text/plain\n" +
"\n" + "\n" +
"some text in the first part\n" + "some text in the first part\n" +
"--1\n" + "--1\n" +
"Content-Type: multipart/alternative; boundary=2\n" + "Content-Type: multipart/alternative; boundary=2\n" +
"\n" + "\n" +
"--2\n" + "--2\n" +
"Content-Type: text/plain\n" + "Content-Type: text/plain\n" +
"\n" + "\n" +
"alternative 1\n" + "alternative 1\n" +
"--2\n" + "--2\n" +
"Content-Type: text/plain\n" + "Content-Type: text/plain\n" +
"\n" + "\n" +
"alternative 2\n" + "alternative 2\n" +
"--2--\n" + "--2--\n" +
"--1--")); "--1--"));
checkAddresses(msg.getFrom(), "x@example.org"); checkAddresses(msg.getFrom(), "x@example.org");
checkAddresses(msg.getRecipients(RecipientType.TO), "y@example.org"); checkAddresses(msg.getRecipients(RecipientType.TO), "y@example.org");

View File

@ -5,8 +5,10 @@ import org.junit.Test;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
public class MimeUtilityTest {
@Test public void testGetHeaderParameter() { public class MimeUtilityTest {
@Test
public void testGetHeaderParameter() {
String result; String result;
/* Test edge cases */ /* Test edge cases */
@ -32,7 +34,7 @@ public class MimeUtilityTest {
result = MimeUtility.getHeaderParameter("name=\"value\"", "name"); result = MimeUtility.getHeaderParameter("name=\"value\"", "name");
assertEquals("value", result); assertEquals("value", result);
result = MimeUtility.getHeaderParameter("name = \"value\"" , "name"); result = MimeUtility.getHeaderParameter("name = \"value\"", "name");
assertEquals("value", result); assertEquals("value", result);
result = MimeUtility.getHeaderParameter("name=\"\"", "name"); result = MimeUtility.getHeaderParameter("name=\"\"", "name");