1
0
mirror of https://github.com/moparisthebest/k-9 synced 2024-11-30 13:12:25 -05:00

Convert remaining tests to JUnit4

This commit is contained in:
Jan Berkel 2015-01-14 04:05:42 +01:00
parent 4808406739
commit dfb025033d
10 changed files with 184 additions and 90 deletions

View File

@ -1,21 +0,0 @@
package com.fsck.k9.activity;
import android.test.ActivityInstrumentationTestCase2;
import com.fsck.k9.activity.Accounts;
/**
* This is a simple framework for a test of an Application. See
* {@link android.test.ApplicationTestCase ApplicationTestCase} for more information on
* how to write and extend Application tests.
* <p/>
* To run this test, you can type:
* adb shell am instrument -w \
* -e class com.fsck.k9.activity.AccountsTest \
* com.fsck.k9.tests/android.test.InstrumentationTestRunner
*/
public class AccountsTest extends ActivityInstrumentationTestCase2<Accounts> {
public AccountsTest() {
super("com.fsck.k9", Accounts.class);
}
}

View File

@ -1,14 +1,24 @@
package com.fsck.k9.activity; package com.fsck.k9.activity;
import android.support.test.runner.AndroidJUnit4;
import com.fsck.k9.mail.Flag; import com.fsck.k9.mail.Flag;
import com.fsck.k9.mail.MessagingException; import com.fsck.k9.mail.MessagingException;
import junit.framework.TestCase; import org.junit.Test;
import org.junit.runner.RunWith;
public class MessageReferenceTest extends TestCase import static junit.framework.Assert.assertEquals;
import static junit.framework.Assert.assertNotNull;
import static junit.framework.Assert.assertNull;
import static junit.framework.Assert.assertTrue;
@RunWith(AndroidJUnit4.class)
public class MessageReferenceTest
{ {
/** /**
* Typically happens during forwards. (You have a reference, but no flag since we don't currently consider FORWARDED a flag.) * Typically happens during forwards. (You have a reference, but no flag since we don't currently consider FORWARDED a flag.)
*/ */
@Test
public void testIdentityStringNoFlag() public void testIdentityStringNoFlag()
{ {
MessageReference mr = new MessageReference(); MessageReference mr = new MessageReference();
@ -22,6 +32,7 @@ public class MessageReferenceTest extends TestCase
/** /**
* Typically happens during replies. * Typically happens during replies.
*/ */
@Test
public void testIdentityString() public void testIdentityString()
{ {
MessageReference mr = new MessageReference(); MessageReference mr = new MessageReference();
@ -33,6 +44,7 @@ public class MessageReferenceTest extends TestCase
assertEquals("!:byBoYWkh:Zm9sZGVy:MTAxMDEwMTA=:ANSWERED", mr.toIdentityString()); assertEquals("!:byBoYWkh:Zm9sZGVy:MTAxMDEwMTA=:ANSWERED", mr.toIdentityString());
} }
@Test
public void testParseIdentityStringNoFlag() throws MessagingException public void testParseIdentityStringNoFlag() throws MessagingException
{ {
MessageReference mr = new MessageReference("!:byBoYWkh:Zm9sZGVy:MTAxMDEwMTA="); MessageReference mr = new MessageReference("!:byBoYWkh:Zm9sZGVy:MTAxMDEwMTA=");
@ -42,6 +54,7 @@ public class MessageReferenceTest extends TestCase
assertNull(mr.flag); assertNull(mr.flag);
} }
@Test
public void testParseIdentityString() throws MessagingException public void testParseIdentityString() throws MessagingException
{ {
MessageReference mr = new MessageReference("!:byBoYWkh:Zm9sZGVy:MTAxMDEwMTA=:ANSWERED"); MessageReference mr = new MessageReference("!:byBoYWkh:Zm9sZGVy:MTAxMDEwMTA=:ANSWERED");
@ -51,12 +64,14 @@ public class MessageReferenceTest extends TestCase
assertEquals(Flag.ANSWERED, mr.flag); assertEquals(Flag.ANSWERED, mr.flag);
} }
@Test
public void testBadVersion() throws MessagingException public void testBadVersion() throws MessagingException
{ {
MessageReference mr = new MessageReference("@:byBoYWkh:Zm9sZGVy:MTAxMDEwMTA=:ANSWERED"); MessageReference mr = new MessageReference("@:byBoYWkh:Zm9sZGVy:MTAxMDEwMTA=:ANSWERED");
assertNull(mr.accountUuid); assertNull(mr.accountUuid);
} }
@Test
public void testNull() throws MessagingException public void testNull() throws MessagingException
{ {
try try
@ -69,6 +84,7 @@ public class MessageReferenceTest extends TestCase
} }
} }
@Test
public void testCorruption() throws MessagingException public void testCorruption() throws MessagingException
{ {
MessageReference mr = new MessageReference("!:%^&%^*$&$by&(BYWkh:Zm9%^@sZGVy:MT-35#$AxMDEwMTA=:ANSWERED"); MessageReference mr = new MessageReference("!:%^&%^*$&$by&(BYWkh:Zm9%^@sZGVy:MT-35#$AxMDEwMTA=:ANSWERED");

View File

@ -1,27 +1,37 @@
package com.fsck.k9.helper; package com.fsck.k9.helper;
import junit.framework.TestCase; import android.support.test.runner.AndroidJUnit4;
import java.lang.String; import org.junit.Test;
import org.junit.runner.RunWith;
public class FileHelperTest extends TestCase { import static junit.framework.Assert.assertEquals;
@RunWith(AndroidJUnit4.class)
public class FileHelperTest {
@Test
public void testSanitize1() { public void testSanitize1() {
checkSanitization(".._bla_", "../bla_"); checkSanitization(".._bla_", "../bla_");
} }
@Test
public void testSanitize2() { public void testSanitize2() {
checkSanitization("_etc_bla", "/etc/bla"); checkSanitization("_etc_bla", "/etc/bla");
} }
@Test
public void testSanitize3() { public void testSanitize3() {
checkSanitization("_пPп", "+пPп"); checkSanitization("_пPп", "+пPп");
} }
@Test
public void testSanitize4() { public void testSanitize4() {
checkSanitization(".東京_!", ".東京?!"); checkSanitization(".東京_!", ".東京?!");
} }
@Test
public void testSanitize5() { public void testSanitize5() {
checkSanitization("Plan 9", "Plan 9"); checkSanitization("Plan 9", "Plan 9");
} }

View File

@ -1,16 +1,24 @@
package com.fsck.k9.helper; package com.fsck.k9.helper;
import junit.framework.TestCase;
import java.io.BufferedWriter; import java.io.BufferedWriter;
import java.io.File; import java.io.File;
import java.io.FileWriter; import java.io.FileWriter;
public class HtmlConverterTest extends TestCase { import android.support.test.runner.AndroidJUnit4;
import org.junit.Test;
import org.junit.runner.RunWith;
import static junit.framework.Assert.assertEquals;
@RunWith(AndroidJUnit4.class)
public class HtmlConverterTest {
// Useful if you want to write stuff to a file for debugging in a browser. // Useful if you want to write stuff to a file for debugging in a browser.
private static final boolean WRITE_TO_FILE = Boolean.parseBoolean(System.getProperty("k9.htmlConverterTest.writeToFile", "false")); private static final boolean WRITE_TO_FILE = Boolean.parseBoolean(System.getProperty("k9.htmlConverterTest.writeToFile", "false"));
private static final String OUTPUT_FILE = "C:/temp/parse.html"; private static final String OUTPUT_FILE = "C:/temp/parse.html";
@Test
public void testTextQuoteToHtmlBlockquote() { public void testTextQuoteToHtmlBlockquote() {
String message = "Panama!\r\n" + String message = "Panama!\r\n" +
"\r\n" + "\r\n" +
@ -29,25 +37,30 @@ public class HtmlConverterTest extends TestCase {
+ "Panama!<br />" + "Panama!<br />"
+ "<br />" + "<br />"
+ "Bob Barker &lt;bob@aol.com&gt; wrote:<br />" + "Bob Barker &lt;bob@aol.com&gt; wrote:<br />"
+ "<blockquote class=\"gmail_quote\" style=\"margin: 0pt 0pt 1ex 0.8ex; border-left: 1px solid #729fcf; padding-left: 1ex;\">" +
"<blockquote class=\"gmail_quote\" style=\"margin: 0pt 0pt 1ex 0.8ex; border-left: 1px solid #729fcf; padding-left: 1ex;\">"
+ " a canal<br />" + " a canal<br />"
+ "<br />" + "<br />"
+ " Dorothy Jo Gideon &lt;dorothy@aol.com&gt; espoused:<br />" + " Dorothy Jo Gideon &lt;dorothy@aol.com&gt; espoused:<br />"
+ "<blockquote class=\"gmail_quote\" style=\"margin: 0pt 0pt 1ex 0.8ex; border-left: 1px solid #ad7fa8; padding-left: 1ex;\">" +
"<blockquote class=\"gmail_quote\" style=\"margin: 0pt 0pt 1ex 0.8ex; border-left: 1px solid #ad7fa8; padding-left: 1ex;\">"
+ "A man, a plan...<br />" + "A man, a plan...<br />"
+ "</blockquote>" + "</blockquote>"
+ " Too easy!<br />" + " Too easy!<br />"
+ "</blockquote>" + "</blockquote>"
+ "<br />" + "<br />"
+ "Nice job :)<br />" + "Nice job :)<br />"
+ "<blockquote class=\"gmail_quote\" style=\"margin: 0pt 0pt 1ex 0.8ex; border-left: 1px solid #729fcf; padding-left: 1ex;\">" +
+ "<blockquote class=\"gmail_quote\" style=\"margin: 0pt 0pt 1ex 0.8ex; border-left: 1px solid #ad7fa8; padding-left: 1ex;\">" "<blockquote class=\"gmail_quote\" style=\"margin: 0pt 0pt 1ex 0.8ex; border-left: 1px solid #729fcf; padding-left: 1ex;\">"
+
"<blockquote class=\"gmail_quote\" style=\"margin: 0pt 0pt 1ex 0.8ex; border-left: 1px solid #ad7fa8; padding-left: 1ex;\">"
+ " Guess!" + " Guess!"
+ "</blockquote>" + "</blockquote>"
+ "</blockquote>" + "</blockquote>"
+ "</pre>", result); + "</pre>", result);
} }
@Test
public void testTextQuoteToHtmlBlockquoteIndented() { public void testTextQuoteToHtmlBlockquoteIndented() {
String message = "*facepalm*\r\n" + String message = "*facepalm*\r\n" +
"\r\n" + "\r\n" +
@ -73,6 +86,7 @@ public class HtmlConverterTest extends TestCase {
} }
@Test
public void testQuoteDepthColor() { public void testQuoteDepthColor() {
assertEquals(HtmlConverter.getQuoteColor(1), HtmlConverter.QUOTE_COLOR_LEVEL_1); assertEquals(HtmlConverter.getQuoteColor(1), HtmlConverter.QUOTE_COLOR_LEVEL_1);
assertEquals(HtmlConverter.getQuoteColor(2), HtmlConverter.QUOTE_COLOR_LEVEL_2); assertEquals(HtmlConverter.getQuoteColor(2), HtmlConverter.QUOTE_COLOR_LEVEL_2);
@ -135,6 +149,7 @@ public class HtmlConverterTest extends TestCase {
} }
} }
@Test
public void testPreserveSpacesAtFirst() { public void testPreserveSpacesAtFirst() {
String message = "foo\r\n" String message = "foo\r\n"
+ " bar\r\n" + " bar\r\n"
@ -148,6 +163,7 @@ public class HtmlConverterTest extends TestCase {
+ "</pre>", result); + "</pre>", result);
} }
@Test
public void testPreserveSpacesAtFirstForSpecialCharacters() { public void testPreserveSpacesAtFirstForSpecialCharacters() {
String message = String message =
" \r\n" " \r\n"
@ -168,6 +184,7 @@ public class HtmlConverterTest extends TestCase {
+ "</pre>", result); + "</pre>", result);
} }
@Test
public void testLinkifyBitcoinAndHttpUri() { public void testLinkifyBitcoinAndHttpUri() {
String text = "bitcoin:19W6QZkx8SYPG7BBCS7odmWGRxqRph5jFU http://example.com/"; String text = "bitcoin:19W6QZkx8SYPG7BBCS7odmWGRxqRph5jFU http://example.com/";

View File

@ -1,21 +1,31 @@
package com.fsck.k9.helper; package com.fsck.k9.helper;
import android.content.Context;
import android.graphics.Color; import android.graphics.Color;
import android.test.AndroidTestCase; import android.support.test.InstrumentationRegistry;
import android.support.test.runner.AndroidJUnit4;
import android.text.SpannableString; import android.text.SpannableString;
import com.fsck.k9.mail.Address; import com.fsck.k9.mail.Address;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
public class MessageHelperTest extends AndroidTestCase { import static junit.framework.Assert.assertEquals;
import static junit.framework.Assert.assertTrue;
@RunWith(AndroidJUnit4.class)
public class MessageHelperTest {
private Contacts contacts; private Contacts contacts;
private Contacts mockContacts; private Contacts mockContacts;
@Override @Before
public void setUp() throws Exception { public void setUp() throws Exception {
super.setUp(); Context context = InstrumentationRegistry.getTargetContext();
contacts = new Contacts(getContext()); contacts = new Contacts(context);
mockContacts = new Contacts(getContext()) { mockContacts = new Contacts(context) {
@Override public String getNameForAddress(String address) { @Override public String getNameForAddress(String address) {
if ("test@testor.com".equals(address)) { if ("test@testor.com".equals(address)) {
return "Tim Testor"; return "Tim Testor";
@ -26,16 +36,19 @@ public class MessageHelperTest extends AndroidTestCase {
}; };
} }
@Test
public void testToFriendlyShowsPersonalPartIfItExists() throws Exception { public void testToFriendlyShowsPersonalPartIfItExists() throws Exception {
Address address = new Address("test@testor.com", "Tim Testor"); Address address = new Address("test@testor.com", "Tim Testor");
assertEquals("Tim Testor", MessageHelper.toFriendly(address, contacts)); assertEquals("Tim Testor", MessageHelper.toFriendly(address, contacts));
} }
@Test
public void testToFriendlyShowsEmailPartIfNoPersonalPartExists() throws Exception { public void testToFriendlyShowsEmailPartIfNoPersonalPartExists() throws Exception {
Address address = new Address("test@testor.com"); Address address = new Address("test@testor.com");
assertEquals("test@testor.com", MessageHelper.toFriendly(address, contacts)); assertEquals("test@testor.com", MessageHelper.toFriendly(address, contacts));
} }
@Test
public void testToFriendlyArray() throws Exception { public void testToFriendlyArray() throws Exception {
Address address1 = new Address("test@testor.com", "Tim Testor"); Address address1 = new Address("test@testor.com", "Tim Testor");
Address address2 = new Address("foo@bar.com", "Foo Bar"); Address address2 = new Address("foo@bar.com", "Foo Bar");
@ -43,11 +56,13 @@ public class MessageHelperTest extends AndroidTestCase {
assertEquals("Tim Testor,Foo Bar", MessageHelper.toFriendly(addresses, contacts).toString()); assertEquals("Tim Testor,Foo Bar", MessageHelper.toFriendly(addresses, contacts).toString());
} }
@Test
public void testToFriendlyWithContactLookup() throws Exception { public void testToFriendlyWithContactLookup() throws Exception {
Address address = new Address("test@testor.com"); Address address = new Address("test@testor.com");
assertEquals("Tim Testor", MessageHelper.toFriendly(address, mockContacts).toString()); assertEquals("Tim Testor", MessageHelper.toFriendly(address, mockContacts).toString());
} }
@Test
public void testToFriendlyWithChangeContactColor() throws Exception { public void testToFriendlyWithChangeContactColor() throws Exception {
Address address = new Address("test@testor.com"); Address address = new Address("test@testor.com");
CharSequence friendly = MessageHelper.toFriendly(address, mockContacts, true, true, Color.RED); CharSequence friendly = MessageHelper.toFriendly(address, mockContacts, true, true, Color.RED);
@ -55,6 +70,7 @@ public class MessageHelperTest extends AndroidTestCase {
assertEquals("Tim Testor", friendly.toString()); assertEquals("Tim Testor", friendly.toString());
} }
@Test
public void testToFriendlyWithoutCorrespondentNames() throws Exception { public void testToFriendlyWithoutCorrespondentNames() throws Exception {
Address address = new Address("test@testor.com", "Tim Testor"); Address address = new Address("test@testor.com", "Tim Testor");
CharSequence friendly = MessageHelper.toFriendly(address, mockContacts, false, false, 0); CharSequence friendly = MessageHelper.toFriendly(address, mockContacts, false, false, 0);

View File

@ -8,12 +8,8 @@ import java.io.OutputStream;
import java.util.Date; import java.util.Date;
import java.util.TimeZone; import java.util.TimeZone;
import com.fsck.k9.mail.internet.MimeMessageHelper; import android.support.test.InstrumentationRegistry;
import org.apache.commons.io.IOUtils; import android.support.test.runner.AndroidJUnit4;
import org.apache.james.mime4j.codec.Base64InputStream;
import org.apache.james.mime4j.util.MimeUtil;
import android.test.AndroidTestCase;
import com.fsck.k9.mail.Message.RecipientType; import com.fsck.k9.mail.Message.RecipientType;
import com.fsck.k9.mail.internet.BinaryTempFileBody; import com.fsck.k9.mail.internet.BinaryTempFileBody;
@ -22,13 +18,23 @@ import com.fsck.k9.mail.internet.CharsetSupport;
import com.fsck.k9.mail.internet.MimeBodyPart; import com.fsck.k9.mail.internet.MimeBodyPart;
import com.fsck.k9.mail.internet.MimeHeader; import com.fsck.k9.mail.internet.MimeHeader;
import com.fsck.k9.mail.internet.MimeMessage; import com.fsck.k9.mail.internet.MimeMessage;
import com.fsck.k9.mail.internet.MimeMessageHelper;
import com.fsck.k9.mail.internet.MimeMultipart; import com.fsck.k9.mail.internet.MimeMultipart;
import com.fsck.k9.mail.internet.TextBody; import com.fsck.k9.mail.internet.TextBody;
import org.apache.commons.io.IOUtils;
import org.apache.james.mime4j.util.MimeUtil;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
public class MessageTest extends AndroidTestCase { import static org.junit.Assert.assertEquals;
@Override import static org.junit.Assert.assertNotNull;
@RunWith(AndroidJUnit4.class)
public class MessageTest {
@Before
public void setUp() throws Exception { public void setUp() throws Exception {
super.setUp();
TimeZone.setDefault(TimeZone.getTimeZone("Asia/Tokyo")); TimeZone.setDefault(TimeZone.getTimeZone("Asia/Tokyo"));
} }
@ -272,10 +278,7 @@ public class MessageTest extends AndroidTestCase {
private int mMimeBoundary; private int mMimeBoundary;
public MessageTest() { @Test
super();
}
public void testSetSendDateSetsSentDate() throws Exception { public void testSetSendDateSetsSentDate() throws Exception {
Message message = sampleMessage(); Message message = sampleMessage();
final int milliseconds = 0; final int milliseconds = 0;
@ -286,23 +289,26 @@ public class MessageTest extends AndroidTestCase {
assertEquals(milliseconds, sentDate.getTime()); assertEquals(milliseconds, sentDate.getTime());
} }
@Test
public void testSetSendDateFormatsHeaderCorrectlyWithCurrentTimeZone() throws Exception { public void testSetSendDateFormatsHeaderCorrectlyWithCurrentTimeZone() throws Exception {
Message message = sampleMessage(); Message message = sampleMessage();
message.setSentDate(new Date(0), false); message.setSentDate(new Date(0), false);
assertEquals("Thu, 01 Jan 1970 09:00:00 +0900", message.getHeader("Date")[0]); assertEquals("Thu, 01 Jan 1970 09:00:00 +0900", message.getHeader("Date")[0]);
} }
@Test
public void testSetSendDateFormatsHeaderCorrectlyWithoutTimeZone() throws Exception { public void testSetSendDateFormatsHeaderCorrectlyWithoutTimeZone() throws Exception {
Message message = sampleMessage(); Message message = sampleMessage();
message.setSentDate(new Date(0), true); message.setSentDate(new Date(0), true);
assertEquals("Thu, 01 Jan 1970 00:00:00 +0000", message.getHeader("Date")[0]); assertEquals("Thu, 01 Jan 1970 00:00:00 +0000", message.getHeader("Date")[0]);
} }
@Test
public void testMessage() throws MessagingException, IOException { public void testMessage() throws MessagingException, IOException {
MimeMessage message; MimeMessage message;
ByteArrayOutputStream out; ByteArrayOutputStream out;
BinaryTempFileBody.setTempDirectory(getContext().getCacheDir()); BinaryTempFileBody.setTempDirectory(InstrumentationRegistry.getTargetContext().getCacheDir());
mMimeBoundary = 101; mMimeBoundary = 101;
message = nestedMessage(nestedMessage(sampleMessage())); message = nestedMessage(nestedMessage(sampleMessage()));

View File

@ -6,10 +6,13 @@ import java.io.ByteArrayOutputStream;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import android.test.AndroidTestCase; import android.support.test.InstrumentationRegistry;
import android.support.test.runner.AndroidJUnit4;
import com.fsck.k9.mail.internet.BinaryTempFileBody; import com.fsck.k9.mail.internet.BinaryTempFileBody;
import com.fsck.k9.mail.internet.MimeMessage; import com.fsck.k9.mail.internet.MimeMessage;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.spongycastle.openpgp.PGPCompressedData; import org.spongycastle.openpgp.PGPCompressedData;
import org.spongycastle.openpgp.PGPException; import org.spongycastle.openpgp.PGPException;
import org.spongycastle.openpgp.PGPObjectFactory; import org.spongycastle.openpgp.PGPObjectFactory;
@ -22,8 +25,11 @@ import org.spongycastle.openpgp.bc.BcPGPObjectFactory;
import org.spongycastle.openpgp.bc.BcPGPPublicKeyRingCollection; import org.spongycastle.openpgp.bc.BcPGPPublicKeyRingCollection;
import org.spongycastle.openpgp.operator.bc.BcPGPContentVerifierBuilderProvider; import org.spongycastle.openpgp.operator.bc.BcPGPContentVerifierBuilderProvider;
import static junit.framework.Assert.assertTrue;
public class PgpMimeMessageTest extends AndroidTestCase {
@RunWith(AndroidJUnit4.class)
public class PgpMimeMessageTest {
private static final String PUBLIC_KEY = "-----BEGIN PGP PUBLIC KEY BLOCK-----\n" + private static final String PUBLIC_KEY = "-----BEGIN PGP PUBLIC KEY BLOCK-----\n" +
"Version: GnuPG v1\n" + "Version: GnuPG v1\n" +
"\n" + "\n" +
@ -150,6 +156,7 @@ public class PgpMimeMessageTest extends AndroidTestCase {
"-----END PGP PUBLIC KEY BLOCK-----\n"; "-----END PGP PUBLIC KEY BLOCK-----\n";
@Test
public void testSignedMessage() throws IOException, MessagingException, PGPException { public void testSignedMessage() throws IOException, MessagingException, PGPException {
String messageSource = "Date: Mon, 08 Dec 2014 17:44:18 +0100\r\n" + String messageSource = "Date: Mon, 08 Dec 2014 17:44:18 +0100\r\n" +
"From: cketti <cketti@googlemail.com>\r\n" + "From: cketti <cketti@googlemail.com>\r\n" +
@ -209,7 +216,7 @@ public class PgpMimeMessageTest extends AndroidTestCase {
"\r\n" + "\r\n" +
"--24Bem7EnUI1Ipn9jNXuLgsetqa6wOkIxM--\r\n"; "--24Bem7EnUI1Ipn9jNXuLgsetqa6wOkIxM--\r\n";
BinaryTempFileBody.setTempDirectory(getContext().getCacheDir()); BinaryTempFileBody.setTempDirectory(InstrumentationRegistry.getTargetContext().getCacheDir());
InputStream messageInputStream = new ByteArrayInputStream(messageSource.getBytes()); InputStream messageInputStream = new ByteArrayInputStream(messageSource.getBytes());
MimeMessage message; MimeMessage message;

View File

@ -6,14 +6,22 @@ import java.io.ByteArrayOutputStream;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import android.support.test.InstrumentationRegistry;
import android.support.test.runner.AndroidJUnit4;
import android.test.AndroidTestCase; import android.test.AndroidTestCase;
import com.fsck.k9.mail.internet.BinaryTempFileBody; import com.fsck.k9.mail.internet.BinaryTempFileBody;
import com.fsck.k9.mail.internet.MimeMessage; import com.fsck.k9.mail.internet.MimeMessage;
import org.junit.Test;
import org.junit.runner.RunWith;
import static junit.framework.Assert.assertEquals;
public class ReconstructMessageTest extends AndroidTestCase { @RunWith(AndroidJUnit4.class)
public class ReconstructMessageTest {
@Test
public void testMessage() throws IOException, MessagingException { public void testMessage() throws IOException, MessagingException {
String messageSource = String messageSource =
"From: from@example.com\r\n" + "From: from@example.com\r\n" +
@ -45,7 +53,7 @@ public class ReconstructMessageTest extends AndroidTestCase {
"------Boundary--\r\n" + "------Boundary--\r\n" +
"Hi, I'm the epilogue"; "Hi, I'm the epilogue";
BinaryTempFileBody.setTempDirectory(getContext().getCacheDir()); BinaryTempFileBody.setTempDirectory(InstrumentationRegistry.getTargetContext().getCacheDir());
InputStream messageInputStream = new ByteArrayInputStream(messageSource.getBytes()); InputStream messageInputStream = new ByteArrayInputStream(messageSource.getBytes());
MimeMessage message; MimeMessage message;

View File

@ -1,18 +1,29 @@
package com.fsck.k9.mail.ssl; package com.fsck.k9.mail.ssl;
import javax.net.ssl.X509TrustManager;
import java.io.ByteArrayInputStream; import java.io.ByteArrayInputStream;
import java.io.File; import java.io.File;
import java.security.cert.CertificateException; import java.security.cert.CertificateException;
import java.security.cert.CertificateFactory; import java.security.cert.CertificateFactory;
import java.security.cert.X509Certificate; import java.security.cert.X509Certificate;
import android.test.AndroidTestCase;
import android.support.test.InstrumentationRegistry;
import android.support.test.runner.AndroidJUnit4;
import javax.net.ssl.X509TrustManager;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import static junit.framework.Assert.assertFalse;
import static junit.framework.Assert.assertTrue;
/** /**
* Test the functionality of {@link TrustManagerFactory}. * Test the functionality of {@link TrustManagerFactory}.
*/ */
public class TrustManagerFactoryTest extends AndroidTestCase { @RunWith(AndroidJUnit4.class)
public class TrustManagerFactoryTest {
public static final String MATCHING_HOST = "k9.example.com"; public static final String MATCHING_HOST = "k9.example.com";
public static final String NOT_MATCHING_HOST = "bla.example.com"; public static final String NOT_MATCHING_HOST = "bla.example.com";
public static final int PORT1 = 993; public static final int PORT1 = 993;
@ -196,15 +207,16 @@ public class TrustManagerFactoryTest extends AndroidTestCase {
new ByteArrayInputStream(encodedCert.getBytes())); new ByteArrayInputStream(encodedCert.getBytes()));
} }
@Override @Before
public void setUp() throws Exception { public void setUp() throws Exception {
mKeyStoreFile = File.createTempFile("localKeyStore", null, getContext().getCacheDir()); mKeyStoreFile = File.createTempFile("localKeyStore", null,
InstrumentationRegistry.getTargetContext().getCacheDir());
mKeyStore = LocalKeyStore.getInstance(); mKeyStore = LocalKeyStore.getInstance();
mKeyStore.setKeyStoreFile(mKeyStoreFile); mKeyStore.setKeyStoreFile(mKeyStoreFile);
} }
@Override @After
protected void tearDown() { public void tearDown() {
mKeyStoreFile.delete(); mKeyStoreFile.delete();
} }
@ -220,6 +232,7 @@ public class TrustManagerFactoryTest extends AndroidTestCase {
* @throws Exception * @throws Exception
* if anything goes wrong * if anything goes wrong
*/ */
@Test
public void testDifferentCertificatesOnSameServer() throws Exception { public void testDifferentCertificatesOnSameServer() throws Exception {
mKeyStore.addCertificate(NOT_MATCHING_HOST, PORT1, mCert1); mKeyStore.addCertificate(NOT_MATCHING_HOST, PORT1, mCert1);
mKeyStore.addCertificate(NOT_MATCHING_HOST, PORT2, mCert2); mKeyStore.addCertificate(NOT_MATCHING_HOST, PORT2, mCert2);
@ -230,24 +243,28 @@ public class TrustManagerFactoryTest extends AndroidTestCase {
trustManager1.checkServerTrusted(new X509Certificate[] { mCert1 }, "authType"); trustManager1.checkServerTrusted(new X509Certificate[] { mCert1 }, "authType");
} }
@Test
public void testSelfSignedCertificateMatchingHost() throws Exception { public void testSelfSignedCertificateMatchingHost() throws Exception {
mKeyStore.addCertificate(MATCHING_HOST, PORT1, mCert1); mKeyStore.addCertificate(MATCHING_HOST, PORT1, mCert1);
X509TrustManager trustManager = TrustManagerFactory.get(MATCHING_HOST, PORT1); X509TrustManager trustManager = TrustManagerFactory.get(MATCHING_HOST, PORT1);
trustManager.checkServerTrusted(new X509Certificate[] { mCert1 }, "authType"); trustManager.checkServerTrusted(new X509Certificate[] { mCert1 }, "authType");
} }
@Test
public void testSelfSignedCertificateNotMatchingHost() throws Exception { public void testSelfSignedCertificateNotMatchingHost() throws Exception {
mKeyStore.addCertificate(NOT_MATCHING_HOST, PORT1, mCert1); mKeyStore.addCertificate(NOT_MATCHING_HOST, PORT1, mCert1);
X509TrustManager trustManager = TrustManagerFactory.get(NOT_MATCHING_HOST, PORT1); X509TrustManager trustManager = TrustManagerFactory.get(NOT_MATCHING_HOST, PORT1);
trustManager.checkServerTrusted(new X509Certificate[] { mCert1 }, "authType"); trustManager.checkServerTrusted(new X509Certificate[] { mCert1 }, "authType");
} }
@Test
public void testWrongCertificate() throws Exception { public void testWrongCertificate() throws Exception {
mKeyStore.addCertificate(MATCHING_HOST, PORT1, mCert1); mKeyStore.addCertificate(MATCHING_HOST, PORT1, mCert1);
X509TrustManager trustManager = TrustManagerFactory.get(MATCHING_HOST, PORT1); X509TrustManager trustManager = TrustManagerFactory.get(MATCHING_HOST, PORT1);
assertCertificateRejection(trustManager, new X509Certificate[] { mCert2 }); assertCertificateRejection(trustManager, new X509Certificate[] { mCert2 });
} }
@Test
public void testCertificateOfOtherHost() throws Exception { public void testCertificateOfOtherHost() throws Exception {
mKeyStore.addCertificate(MATCHING_HOST, PORT1, mCert1); mKeyStore.addCertificate(MATCHING_HOST, PORT1, mCert1);
mKeyStore.addCertificate(MATCHING_HOST, PORT2, mCert2); mKeyStore.addCertificate(MATCHING_HOST, PORT2, mCert2);
@ -256,11 +273,13 @@ public class TrustManagerFactoryTest extends AndroidTestCase {
assertCertificateRejection(trustManager, new X509Certificate[] { mCert2 }); assertCertificateRejection(trustManager, new X509Certificate[] { mCert2 });
} }
@Test
public void testUntrustedCertificateChain() throws Exception { public void testUntrustedCertificateChain() throws Exception {
X509TrustManager trustManager = TrustManagerFactory.get(MATCHING_HOST, PORT1); X509TrustManager trustManager = TrustManagerFactory.get(MATCHING_HOST, PORT1);
assertCertificateRejection(trustManager, new X509Certificate[] { mCert3, mCaCert }); assertCertificateRejection(trustManager, new X509Certificate[] { mCert3, mCaCert });
} }
@Test
public void testLocallyTrustedCertificateChain() throws Exception { public void testLocallyTrustedCertificateChain() throws Exception {
mKeyStore.addCertificate(MATCHING_HOST, PORT1, mCert3); mKeyStore.addCertificate(MATCHING_HOST, PORT1, mCert3);
@ -268,6 +287,7 @@ public class TrustManagerFactoryTest extends AndroidTestCase {
trustManager.checkServerTrusted(new X509Certificate[] { mCert3, mCaCert }, "authType"); trustManager.checkServerTrusted(new X509Certificate[] { mCert3, mCaCert }, "authType");
} }
@Test
public void testLocallyTrustedCertificateChainNotMatchingHost() throws Exception { public void testLocallyTrustedCertificateChainNotMatchingHost() throws Exception {
mKeyStore.addCertificate(NOT_MATCHING_HOST, PORT1, mCert3); mKeyStore.addCertificate(NOT_MATCHING_HOST, PORT1, mCert3);
@ -275,17 +295,20 @@ public class TrustManagerFactoryTest extends AndroidTestCase {
trustManager.checkServerTrusted(new X509Certificate[] { mCert3, mCaCert }, "authType"); trustManager.checkServerTrusted(new X509Certificate[] { mCert3, mCaCert }, "authType");
} }
@Test
public void testGloballyTrustedCertificateChain() throws Exception { public void testGloballyTrustedCertificateChain() throws Exception {
X509TrustManager trustManager = TrustManagerFactory.get("www.linux.com", PORT1); X509TrustManager trustManager = TrustManagerFactory.get("www.linux.com", PORT1);
X509Certificate[] certificates = new X509Certificate[] { mLinuxComCert, mLinuxComFirstParentCert}; X509Certificate[] certificates = new X509Certificate[] { mLinuxComCert, mLinuxComFirstParentCert};
trustManager.checkServerTrusted(certificates, "authType"); trustManager.checkServerTrusted(certificates, "authType");
} }
@Test
public void testGloballyTrustedCertificateNotMatchingHost() throws Exception { public void testGloballyTrustedCertificateNotMatchingHost() throws Exception {
X509TrustManager trustManager = TrustManagerFactory.get(NOT_MATCHING_HOST, PORT1); X509TrustManager trustManager = TrustManagerFactory.get(NOT_MATCHING_HOST, PORT1);
assertCertificateRejection(trustManager, new X509Certificate[] { mLinuxComCert, mLinuxComFirstParentCert}); assertCertificateRejection(trustManager, new X509Certificate[] { mLinuxComCert, mLinuxComFirstParentCert});
} }
@Test
public void testGloballyTrustedCertificateNotMatchingHostOverride() throws Exception { public void testGloballyTrustedCertificateNotMatchingHostOverride() throws Exception {
mKeyStore.addCertificate(MATCHING_HOST, PORT1, mLinuxComCert); mKeyStore.addCertificate(MATCHING_HOST, PORT1, mLinuxComCert);
@ -306,6 +329,7 @@ public class TrustManagerFactoryTest extends AndroidTestCase {
assertFalse("The certificate should have been rejected but wasn't", certificateValid); assertFalse("The certificate should have been rejected but wasn't", certificateValid);
} }
@Test
public void testKeyStoreLoading() throws Exception { public void testKeyStoreLoading() throws Exception {
mKeyStore.addCertificate(MATCHING_HOST, PORT1, mCert1); mKeyStore.addCertificate(MATCHING_HOST, PORT1, mCert1);
mKeyStore.addCertificate(NOT_MATCHING_HOST, PORT2, mCert2); mKeyStore.addCertificate(NOT_MATCHING_HOST, PORT2, mCert2);

View File

@ -3,22 +3,30 @@ package com.fsck.k9.mailstore;
import java.util.Date; import java.util.Date;
import java.util.Locale; import java.util.Locale;
import java.util.TimeZone; import java.util.TimeZone;
import android.test.AndroidTestCase;
import android.support.test.InstrumentationRegistry;
import android.support.test.runner.AndroidJUnit4;
import com.fsck.k9.activity.K9ActivityCommon; import com.fsck.k9.activity.K9ActivityCommon;
import com.fsck.k9.mail.Address; import com.fsck.k9.mail.Address;
import com.fsck.k9.mail.MessagingException;
import com.fsck.k9.mail.Message.RecipientType; import com.fsck.k9.mail.Message.RecipientType;
import com.fsck.k9.mail.MessagingException;
import com.fsck.k9.mail.internet.MimeBodyPart; import com.fsck.k9.mail.internet.MimeBodyPart;
import com.fsck.k9.mail.internet.MimeMessage; import com.fsck.k9.mail.internet.MimeMessage;
import com.fsck.k9.mail.internet.MimeMessageHelper; import com.fsck.k9.mail.internet.MimeMessageHelper;
import com.fsck.k9.mail.internet.MimeMultipart; import com.fsck.k9.mail.internet.MimeMultipart;
import com.fsck.k9.mail.internet.TextBody; import com.fsck.k9.mail.internet.TextBody;
import org.junit.Test;
import org.junit.runner.RunWith;
import static com.fsck.k9.mailstore.LocalMessageExtractor.extractTextAndAttachments; import static com.fsck.k9.mailstore.LocalMessageExtractor.extractTextAndAttachments;
import static junit.framework.Assert.assertEquals;
public class LocalMessageExtractorTest extends AndroidTestCase {
@RunWith(AndroidJUnit4.class)
public class LocalMessageExtractorTest {
@Test
public void testSimplePlainTextMessage() throws MessagingException { public void testSimplePlainTextMessage() throws MessagingException {
String bodyText = "K-9 Mail rocks :>"; String bodyText = "K-9 Mail rocks :>";
@ -30,7 +38,7 @@ public class LocalMessageExtractorTest extends AndroidTestCase {
MimeMessageHelper.setBody(message, body); MimeMessageHelper.setBody(message, body);
// Extract text // Extract text
ViewableContainer container = extractTextAndAttachments(getContext(), message); ViewableContainer container = extractTextAndAttachments(InstrumentationRegistry.getTargetContext(), message);
String expectedText = bodyText; String expectedText = bodyText;
String expectedHtml = String expectedHtml =
@ -42,6 +50,7 @@ public class LocalMessageExtractorTest extends AndroidTestCase {
assertEquals(expectedHtml, container.html); assertEquals(expectedHtml, container.html);
} }
@Test
public void testSimpleHtmlMessage() throws MessagingException { public void testSimpleHtmlMessage() throws MessagingException {
String bodyText = "<strong>K-9 Mail</strong> rocks :&gt;"; String bodyText = "<strong>K-9 Mail</strong> rocks :&gt;";
@ -54,7 +63,7 @@ public class LocalMessageExtractorTest extends AndroidTestCase {
MimeMessageHelper.setBody(message, body); MimeMessageHelper.setBody(message, body);
// Extract text // Extract text
ViewableContainer container = extractTextAndAttachments(getContext(), message); ViewableContainer container = extractTextAndAttachments(InstrumentationRegistry.getTargetContext(), message);
String expectedText = "K-9 Mail rocks :>"; String expectedText = "K-9 Mail rocks :>";
String expectedHtml = String expectedHtml =
@ -64,6 +73,7 @@ public class LocalMessageExtractorTest extends AndroidTestCase {
assertEquals(expectedHtml, container.html); assertEquals(expectedHtml, container.html);
} }
@Test
public void testMultipartPlainTextMessage() throws MessagingException { public void testMultipartPlainTextMessage() throws MessagingException {
String bodyText1 = "text body 1"; String bodyText1 = "text body 1";
String bodyText2 = "text body 2"; String bodyText2 = "text body 2";
@ -84,7 +94,7 @@ public class LocalMessageExtractorTest extends AndroidTestCase {
MimeMessageHelper.setBody(message, multipart); MimeMessageHelper.setBody(message, multipart);
// Extract text // Extract text
ViewableContainer container = extractTextAndAttachments(getContext(), message); ViewableContainer container = extractTextAndAttachments(InstrumentationRegistry.getTargetContext(), message);
String expectedText = String expectedText =
bodyText1 + "\r\n\r\n" + bodyText1 + "\r\n\r\n" +
@ -105,8 +115,9 @@ public class LocalMessageExtractorTest extends AndroidTestCase {
assertEquals(expectedHtml, container.html); assertEquals(expectedHtml, container.html);
} }
@Test
public void testTextPlusRfc822Message() throws MessagingException { public void testTextPlusRfc822Message() throws MessagingException {
K9ActivityCommon.setLanguage(getContext(), "en"); K9ActivityCommon.setLanguage(InstrumentationRegistry.getTargetContext(), "en");
Locale.setDefault(Locale.US); Locale.setDefault(Locale.US);
TimeZone.setDefault(TimeZone.getTimeZone("GMT+01:00")); TimeZone.setDefault(TimeZone.getTimeZone("GMT+01:00"));
@ -140,7 +151,7 @@ public class LocalMessageExtractorTest extends AndroidTestCase {
MimeMessageHelper.setBody(message, multipart); MimeMessageHelper.setBody(message, multipart);
// Extract text // Extract text
ViewableContainer container = extractTextAndAttachments(getContext(), message); ViewableContainer container = extractTextAndAttachments(InstrumentationRegistry.getTargetContext(), message);
String expectedText = String expectedText =
bodyText + bodyText +