mirror of
https://github.com/moparisthebest/k-9
synced 2024-11-27 03:32:16 -05:00
Add method to find multipart/encrypted parts
This commit is contained in:
parent
0e03f262b3
commit
8f7f656355
@ -120,9 +120,6 @@ public abstract class Message implements Part, CompositeBody {
|
||||
@Override
|
||||
public abstract Body getBody();
|
||||
|
||||
@Override
|
||||
public abstract String getContentType() throws MessagingException;
|
||||
|
||||
@Override
|
||||
public abstract void addHeader(String name, String value) throws MessagingException;
|
||||
|
||||
|
@ -15,7 +15,7 @@ public interface Part {
|
||||
|
||||
Body getBody();
|
||||
|
||||
String getContentType() throws MessagingException;
|
||||
String getContentType();
|
||||
|
||||
String getDisposition() throws MessagingException;
|
||||
|
||||
@ -25,7 +25,7 @@ public interface Part {
|
||||
|
||||
boolean isMimeType(String mimeType) throws MessagingException;
|
||||
|
||||
String getMimeType() throws MessagingException;
|
||||
String getMimeType();
|
||||
|
||||
void setBody(Body body);
|
||||
|
||||
|
@ -85,7 +85,7 @@ public class MimeBodyPart extends BodyPart {
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getContentType() throws MessagingException {
|
||||
public String getContentType() {
|
||||
String contentType = getFirstHeader(MimeHeader.HEADER_CONTENT_TYPE);
|
||||
return (contentType == null) ? "text/plain" : contentType;
|
||||
}
|
||||
@ -111,7 +111,7 @@ public class MimeBodyPart extends BodyPart {
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getMimeType() throws MessagingException {
|
||||
public String getMimeType() {
|
||||
return MimeUtility.getHeaderParameter(getContentType(), null);
|
||||
}
|
||||
|
||||
|
@ -163,7 +163,7 @@ public class MimeMessage extends Message {
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getContentType() throws MessagingException {
|
||||
public String getContentType() {
|
||||
String contentType = getFirstHeader(MimeHeader.HEADER_CONTENT_TYPE);
|
||||
return (contentType == null) ? "text/plain" : contentType;
|
||||
}
|
||||
@ -172,12 +172,14 @@ public class MimeMessage extends Message {
|
||||
public String getDisposition() throws MessagingException {
|
||||
return getFirstHeader(MimeHeader.HEADER_CONTENT_DISPOSITION);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getContentId() throws MessagingException {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getMimeType() throws MessagingException {
|
||||
public String getMimeType() {
|
||||
return MimeUtility.getHeaderParameter(getContentType(), null);
|
||||
}
|
||||
|
||||
|
@ -0,0 +1,78 @@
|
||||
package com.fsck.k9.crypto;
|
||||
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import android.support.test.runner.AndroidJUnit4;
|
||||
|
||||
import com.fsck.k9.mail.Part;
|
||||
import com.fsck.k9.mail.internet.MimeBodyPart;
|
||||
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.TextBody;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
|
||||
import static junit.framework.Assert.assertEquals;
|
||||
import static junit.framework.Assert.assertSame;
|
||||
|
||||
|
||||
@RunWith(AndroidJUnit4.class)
|
||||
public class MessageDecryptorTest {
|
||||
|
||||
@Test
|
||||
public void findEncryptedPartsShouldReturnEmptyListForEmptyMessage() throws Exception {
|
||||
MimeMessage emptyMessage = new MimeMessage();
|
||||
|
||||
List<Part> encryptedParts = MessageDecryptor.findEncryptedParts(emptyMessage);
|
||||
assertEquals(0, encryptedParts.size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void findEncryptedPartsShouldReturnEmptyListForSimpleMessage() throws Exception {
|
||||
MimeMessage message = new MimeMessage();
|
||||
message.setBody(new TextBody("message text"));
|
||||
|
||||
List<Part> encryptedParts = MessageDecryptor.findEncryptedParts(message);
|
||||
assertEquals(0, encryptedParts.size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void findEncryptedPartsShouldReturnEmptyEncryptedPart() throws Exception {
|
||||
MimeMessage message = new MimeMessage();
|
||||
MimeMultipart mulitpartEncrypted = new MimeMultipart();
|
||||
mulitpartEncrypted.setSubType("encrypted");
|
||||
MimeMessageHelper.setBody(message, mulitpartEncrypted);
|
||||
|
||||
List<Part> encryptedParts = MessageDecryptor.findEncryptedParts(message);
|
||||
assertEquals(1, encryptedParts.size());
|
||||
assertSame(message, encryptedParts.get(0));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void findEncryptedPartsShouldReturnMultipleEncryptedParts() throws Exception {
|
||||
MimeMessage message = new MimeMessage();
|
||||
MimeMultipart multipartMixed = new MimeMultipart();
|
||||
multipartMixed.setSubType("mixed");
|
||||
MimeMessageHelper.setBody(message, multipartMixed);
|
||||
|
||||
MimeMultipart mulitpartEncryptedOne = new MimeMultipart();
|
||||
mulitpartEncryptedOne.setSubType("encrypted");
|
||||
MimeBodyPart bodyPartOne = new MimeBodyPart(mulitpartEncryptedOne);
|
||||
multipartMixed.addBodyPart(bodyPartOne);
|
||||
|
||||
MimeBodyPart bodyPartTwo = new MimeBodyPart(null, "text/plain");
|
||||
multipartMixed.addBodyPart(bodyPartTwo);
|
||||
|
||||
MimeMultipart mulitpartEncryptedThree = new MimeMultipart();
|
||||
mulitpartEncryptedThree.setSubType("encrypted");
|
||||
MimeBodyPart bodyPartThree = new MimeBodyPart(mulitpartEncryptedThree);
|
||||
multipartMixed.addBodyPart(bodyPartThree);
|
||||
|
||||
List<Part> encryptedParts = MessageDecryptor.findEncryptedParts(message);
|
||||
assertEquals(2, encryptedParts.size());
|
||||
assertSame(bodyPartOne, encryptedParts.get(0));
|
||||
assertSame(bodyPartThree, encryptedParts.get(1));
|
||||
}
|
||||
}
|
@ -0,0 +1,41 @@
|
||||
package com.fsck.k9.crypto;
|
||||
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Stack;
|
||||
|
||||
import com.fsck.k9.mail.Body;
|
||||
import com.fsck.k9.mail.BodyPart;
|
||||
import com.fsck.k9.mail.MessagingException;
|
||||
import com.fsck.k9.mail.Multipart;
|
||||
import com.fsck.k9.mail.Part;
|
||||
|
||||
|
||||
public class MessageDecryptor {
|
||||
private static final String MULTIPART_ENCRYPTED = "multipart/encrypted";
|
||||
|
||||
public static List<Part> findEncryptedParts(Part startPart) {
|
||||
List<Part> encryptedParts = new ArrayList<Part>();
|
||||
Stack<Part> partsToCheck = new Stack<Part>();
|
||||
partsToCheck.push(startPart);
|
||||
|
||||
while (!partsToCheck.isEmpty()) {
|
||||
Part part = partsToCheck.pop();
|
||||
String mimeType = part.getMimeType();
|
||||
Body body = part.getBody();
|
||||
|
||||
if (MULTIPART_ENCRYPTED.equals(mimeType)) {
|
||||
encryptedParts.add(part);
|
||||
} else if (body instanceof Multipart) {
|
||||
Multipart multipart = (Multipart) body;
|
||||
for (int i = multipart.getCount() - 1; i >= 0; i--) {
|
||||
BodyPart bodyPart = multipart.getBodyPart(i);
|
||||
partsToCheck.push(bodyPart);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return encryptedParts;
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user