target
stringlengths 20
113k
| src_fm
stringlengths 11
86.3k
| src_fm_fc
stringlengths 21
86.4k
| src_fm_fc_co
stringlengths 30
86.4k
| src_fm_fc_ms
stringlengths 42
86.8k
| src_fm_fc_ms_ff
stringlengths 43
86.8k
|
---|---|---|---|---|---|
@Test public void shouldRemoveMetaRefreshWithSpacesInAttributeValue() { String html = "<html>" + "<head><meta http-equiv=\"refresh \" content=\"1; URL=http: "<body>Message</body>" + "</html>"; Document result = htmlSanitizer.sanitize(html); assertEquals("<html><head></head><body>Message</body></html>", toCompactString(result)); } | public Document sanitize(String html) { Document dirtyDocument = Jsoup.parse(html); Document cleanedDocument = cleaner.clean(dirtyDocument); headCleaner.clean(dirtyDocument, cleanedDocument); return cleanedDocument; } | HtmlSanitizer { public Document sanitize(String html) { Document dirtyDocument = Jsoup.parse(html); Document cleanedDocument = cleaner.clean(dirtyDocument); headCleaner.clean(dirtyDocument, cleanedDocument); return cleanedDocument; } } | HtmlSanitizer { public Document sanitize(String html) { Document dirtyDocument = Jsoup.parse(html); Document cleanedDocument = cleaner.clean(dirtyDocument); headCleaner.clean(dirtyDocument, cleanedDocument); return cleanedDocument; } HtmlSanitizer(); } | HtmlSanitizer { public Document sanitize(String html) { Document dirtyDocument = Jsoup.parse(html); Document cleanedDocument = cleaner.clean(dirtyDocument); headCleaner.clean(dirtyDocument, cleanedDocument); return cleanedDocument; } HtmlSanitizer(); Document sanitize(String html); } | HtmlSanitizer { public Document sanitize(String html) { Document dirtyDocument = Jsoup.parse(html); Document cleanedDocument = cleaner.clean(dirtyDocument); headCleaner.clean(dirtyDocument, cleanedDocument); return cleanedDocument; } HtmlSanitizer(); Document sanitize(String html); } |
@Test public void shouldRemoveMultipleMetaRefreshTags() { String html = "<html>" + "<head><meta http-equiv=\"refresh\" content=\"1; URL=http: "<body><meta http-equiv=\"refresh\" content=\"1; URL=http: "</html>"; Document result = htmlSanitizer.sanitize(html); assertEquals("<html><head></head><body>Message</body></html>", toCompactString(result)); } | public Document sanitize(String html) { Document dirtyDocument = Jsoup.parse(html); Document cleanedDocument = cleaner.clean(dirtyDocument); headCleaner.clean(dirtyDocument, cleanedDocument); return cleanedDocument; } | HtmlSanitizer { public Document sanitize(String html) { Document dirtyDocument = Jsoup.parse(html); Document cleanedDocument = cleaner.clean(dirtyDocument); headCleaner.clean(dirtyDocument, cleanedDocument); return cleanedDocument; } } | HtmlSanitizer { public Document sanitize(String html) { Document dirtyDocument = Jsoup.parse(html); Document cleanedDocument = cleaner.clean(dirtyDocument); headCleaner.clean(dirtyDocument, cleanedDocument); return cleanedDocument; } HtmlSanitizer(); } | HtmlSanitizer { public Document sanitize(String html) { Document dirtyDocument = Jsoup.parse(html); Document cleanedDocument = cleaner.clean(dirtyDocument); headCleaner.clean(dirtyDocument, cleanedDocument); return cleanedDocument; } HtmlSanitizer(); Document sanitize(String html); } | HtmlSanitizer { public Document sanitize(String html) { Document dirtyDocument = Jsoup.parse(html); Document cleanedDocument = cleaner.clean(dirtyDocument); headCleaner.clean(dirtyDocument, cleanedDocument); return cleanedDocument; } HtmlSanitizer(); Document sanitize(String html); } |
@Test public void shouldRemoveMetaRefreshButKeepOtherMetaTags() { String html = "<html>" + "<head>" + "<meta http-equiv=\"content-type\" content=\"text/html; charset=UTF-8\">" + "<meta http-equiv=\"refresh\" content=\"1; URL=http: "</head>" + "<body>Message</body>" + "</html>"; Document result = htmlSanitizer.sanitize(html); assertEquals("<html><head><meta http-equiv=\"content-type\" content=\"text/html; charset=UTF-8\"></head>" + "<body>Message</body></html>", toCompactString(result)); } | public Document sanitize(String html) { Document dirtyDocument = Jsoup.parse(html); Document cleanedDocument = cleaner.clean(dirtyDocument); headCleaner.clean(dirtyDocument, cleanedDocument); return cleanedDocument; } | HtmlSanitizer { public Document sanitize(String html) { Document dirtyDocument = Jsoup.parse(html); Document cleanedDocument = cleaner.clean(dirtyDocument); headCleaner.clean(dirtyDocument, cleanedDocument); return cleanedDocument; } } | HtmlSanitizer { public Document sanitize(String html) { Document dirtyDocument = Jsoup.parse(html); Document cleanedDocument = cleaner.clean(dirtyDocument); headCleaner.clean(dirtyDocument, cleanedDocument); return cleanedDocument; } HtmlSanitizer(); } | HtmlSanitizer { public Document sanitize(String html) { Document dirtyDocument = Jsoup.parse(html); Document cleanedDocument = cleaner.clean(dirtyDocument); headCleaner.clean(dirtyDocument, cleanedDocument); return cleanedDocument; } HtmlSanitizer(); Document sanitize(String html); } | HtmlSanitizer { public Document sanitize(String html) { Document dirtyDocument = Jsoup.parse(html); Document cleanedDocument = cleaner.clean(dirtyDocument); headCleaner.clean(dirtyDocument, cleanedDocument); return cleanedDocument; } HtmlSanitizer(); Document sanitize(String html); } |
@Test public void shouldProduceValidHtmlFromHtmlWithXmlDeclaration() { String html = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + "<html><head></head><body></body></html>"; Document result = htmlSanitizer.sanitize(html); assertEquals("<html><head></head><body></body></html>", toCompactString(result)); } | public Document sanitize(String html) { Document dirtyDocument = Jsoup.parse(html); Document cleanedDocument = cleaner.clean(dirtyDocument); headCleaner.clean(dirtyDocument, cleanedDocument); return cleanedDocument; } | HtmlSanitizer { public Document sanitize(String html) { Document dirtyDocument = Jsoup.parse(html); Document cleanedDocument = cleaner.clean(dirtyDocument); headCleaner.clean(dirtyDocument, cleanedDocument); return cleanedDocument; } } | HtmlSanitizer { public Document sanitize(String html) { Document dirtyDocument = Jsoup.parse(html); Document cleanedDocument = cleaner.clean(dirtyDocument); headCleaner.clean(dirtyDocument, cleanedDocument); return cleanedDocument; } HtmlSanitizer(); } | HtmlSanitizer { public Document sanitize(String html) { Document dirtyDocument = Jsoup.parse(html); Document cleanedDocument = cleaner.clean(dirtyDocument); headCleaner.clean(dirtyDocument, cleanedDocument); return cleanedDocument; } HtmlSanitizer(); Document sanitize(String html); } | HtmlSanitizer { public Document sanitize(String html) { Document dirtyDocument = Jsoup.parse(html); Document cleanedDocument = cleaner.clean(dirtyDocument); headCleaner.clean(dirtyDocument, cleanedDocument); return cleanedDocument; } HtmlSanitizer(); Document sanitize(String html); } |
@Test public void shouldNormalizeTables() { String html = "<html><head></head><body><table><tr><td></td><td></td></tr></table></body></html>"; Document result = htmlSanitizer.sanitize(html); assertEquals("<html><head></head><body><table><tbody>" + "<tr><td></td><td></td></tr>" + "</tbody></table></body></html>", toCompactString(result)); } | public Document sanitize(String html) { Document dirtyDocument = Jsoup.parse(html); Document cleanedDocument = cleaner.clean(dirtyDocument); headCleaner.clean(dirtyDocument, cleanedDocument); return cleanedDocument; } | HtmlSanitizer { public Document sanitize(String html) { Document dirtyDocument = Jsoup.parse(html); Document cleanedDocument = cleaner.clean(dirtyDocument); headCleaner.clean(dirtyDocument, cleanedDocument); return cleanedDocument; } } | HtmlSanitizer { public Document sanitize(String html) { Document dirtyDocument = Jsoup.parse(html); Document cleanedDocument = cleaner.clean(dirtyDocument); headCleaner.clean(dirtyDocument, cleanedDocument); return cleanedDocument; } HtmlSanitizer(); } | HtmlSanitizer { public Document sanitize(String html) { Document dirtyDocument = Jsoup.parse(html); Document cleanedDocument = cleaner.clean(dirtyDocument); headCleaner.clean(dirtyDocument, cleanedDocument); return cleanedDocument; } HtmlSanitizer(); Document sanitize(String html); } | HtmlSanitizer { public Document sanitize(String html) { Document dirtyDocument = Jsoup.parse(html); Document cleanedDocument = cleaner.clean(dirtyDocument); headCleaner.clean(dirtyDocument, cleanedDocument); return cleanedDocument; } HtmlSanitizer(); Document sanitize(String html); } |
@Test public void findEncryptedPartsShouldReturnMultipleEncryptedParts() throws Exception { MimeMessage message = new MimeMessage(); MimeMultipart multipartMixed = MimeMultipart.newInstance(); multipartMixed.setSubType("mixed"); MimeMessageHelper.setBody(message, multipartMixed); MimeMultipart multipartEncryptedOne = MimeMultipart.newInstance(); multipartEncryptedOne.setSubType("encrypted"); MimeBodyPart bodyPartOne = new MimeBodyPart(multipartEncryptedOne); setContentTypeWithProtocol(bodyPartOne, MIME_TYPE_MULTIPART_ENCRYPTED, PROTCOL_PGP_ENCRYPTED); multipartMixed.addBodyPart(bodyPartOne); MimeBodyPart bodyPartTwo = new MimeBodyPart(null, "text/plain"); multipartMixed.addBodyPart(bodyPartTwo); MimeMultipart multipartEncryptedThree = MimeMultipart.newInstance(); multipartEncryptedThree.setSubType("encrypted"); MimeBodyPart bodyPartThree = new MimeBodyPart(multipartEncryptedThree); setContentTypeWithProtocol(bodyPartThree, MIME_TYPE_MULTIPART_ENCRYPTED, PROTCOL_PGP_ENCRYPTED); multipartMixed.addBodyPart(bodyPartThree); List<Part> encryptedParts = MessageDecryptVerifier.findEncryptedParts(message); assertEquals(2, encryptedParts.size()); assertSame(bodyPartOne, encryptedParts.get(0)); assertSame(bodyPartThree, encryptedParts.get(1)); } | public static List<Part> findEncryptedParts(Part startPart) { List<Part> encryptedParts = new ArrayList<>(); Stack<Part> partsToCheck = new Stack<>(); partsToCheck.push(startPart); while (!partsToCheck.isEmpty()) { Part part = partsToCheck.pop(); Body body = part.getBody(); if (isPartMultipartEncrypted(part)) { encryptedParts.add(part); continue; } 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; } | MessageDecryptVerifier { public static List<Part> findEncryptedParts(Part startPart) { List<Part> encryptedParts = new ArrayList<>(); Stack<Part> partsToCheck = new Stack<>(); partsToCheck.push(startPart); while (!partsToCheck.isEmpty()) { Part part = partsToCheck.pop(); Body body = part.getBody(); if (isPartMultipartEncrypted(part)) { encryptedParts.add(part); continue; } 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; } } | MessageDecryptVerifier { public static List<Part> findEncryptedParts(Part startPart) { List<Part> encryptedParts = new ArrayList<>(); Stack<Part> partsToCheck = new Stack<>(); partsToCheck.push(startPart); while (!partsToCheck.isEmpty()) { Part part = partsToCheck.pop(); Body body = part.getBody(); if (isPartMultipartEncrypted(part)) { encryptedParts.add(part); continue; } 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; } } | MessageDecryptVerifier { public static List<Part> findEncryptedParts(Part startPart) { List<Part> encryptedParts = new ArrayList<>(); Stack<Part> partsToCheck = new Stack<>(); partsToCheck.push(startPart); while (!partsToCheck.isEmpty()) { Part part = partsToCheck.pop(); Body body = part.getBody(); if (isPartMultipartEncrypted(part)) { encryptedParts.add(part); continue; } 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; } static Part findPrimaryEncryptedOrSignedPart(Part part, List<Part> outputExtraParts); static List<Part> findEncryptedParts(Part startPart); static List<Part> findSignedParts(Part startPart, MessageCryptoAnnotations messageCryptoAnnotations); static List<Part> findPgpInlineParts(Part startPart); static byte[] getSignatureData(Part part); static boolean isPgpMimeEncryptedOrSignedPart(Part part); static boolean isSMimeEncryptedOrSignedPart(Part part); static boolean isPartPgpInlineEncrypted(@Nullable Part part); } | MessageDecryptVerifier { public static List<Part> findEncryptedParts(Part startPart) { List<Part> encryptedParts = new ArrayList<>(); Stack<Part> partsToCheck = new Stack<>(); partsToCheck.push(startPart); while (!partsToCheck.isEmpty()) { Part part = partsToCheck.pop(); Body body = part.getBody(); if (isPartMultipartEncrypted(part)) { encryptedParts.add(part); continue; } 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; } static Part findPrimaryEncryptedOrSignedPart(Part part, List<Part> outputExtraParts); static List<Part> findEncryptedParts(Part startPart); static List<Part> findSignedParts(Part startPart, MessageCryptoAnnotations messageCryptoAnnotations); static List<Part> findPgpInlineParts(Part startPart); static byte[] getSignatureData(Part part); static boolean isPgpMimeEncryptedOrSignedPart(Part part); static boolean isSMimeEncryptedOrSignedPart(Part part); static boolean isPartPgpInlineEncrypted(@Nullable Part part); } |
@Test public void shouldHtmlEncodeXmlDirectives() { String html = "<html><head></head><body><table>" + "<tr><td><!==><!==>Hmailserver service shutdown:</td><td><!==><!==>Ok</td></tr>" + "</table></body></html>"; Document result = htmlSanitizer.sanitize(html); assertEquals("<html><head></head><body><table><tbody>" + "<tr><td>Hmailserver service shutdown:</td><td>Ok</td></tr>" + "</tbody></table></body></html>", toCompactString(result)); } | public Document sanitize(String html) { Document dirtyDocument = Jsoup.parse(html); Document cleanedDocument = cleaner.clean(dirtyDocument); headCleaner.clean(dirtyDocument, cleanedDocument); return cleanedDocument; } | HtmlSanitizer { public Document sanitize(String html) { Document dirtyDocument = Jsoup.parse(html); Document cleanedDocument = cleaner.clean(dirtyDocument); headCleaner.clean(dirtyDocument, cleanedDocument); return cleanedDocument; } } | HtmlSanitizer { public Document sanitize(String html) { Document dirtyDocument = Jsoup.parse(html); Document cleanedDocument = cleaner.clean(dirtyDocument); headCleaner.clean(dirtyDocument, cleanedDocument); return cleanedDocument; } HtmlSanitizer(); } | HtmlSanitizer { public Document sanitize(String html) { Document dirtyDocument = Jsoup.parse(html); Document cleanedDocument = cleaner.clean(dirtyDocument); headCleaner.clean(dirtyDocument, cleanedDocument); return cleanedDocument; } HtmlSanitizer(); Document sanitize(String html); } | HtmlSanitizer { public Document sanitize(String html) { Document dirtyDocument = Jsoup.parse(html); Document cleanedDocument = cleaner.clean(dirtyDocument); headCleaner.clean(dirtyDocument, cleanedDocument); return cleanedDocument; } HtmlSanitizer(); Document sanitize(String html); } |
@Test public void uriInMiddleOfInput() throws Exception { String prefix = "prefix "; String uri = "bitcoin:12A1MyfXbW6RhdRAZEqofac5jCQQjwEPBu?amount=1.2"; String text = prefix + uri; parser.linkifyUri(text, prefix.length(), outputBuffer); assertLinkOnly(uri, outputBuffer); } | @Override public int linkifyUri(String text, int startPos, StringBuffer outputBuffer) { Matcher matcher = BITCOIN_URI_PATTERN.matcher(text); if (!matcher.find(startPos) || matcher.start() != startPos) { return startPos; } String bitcoinUri = matcher.group(); outputBuffer.append("<a href=\"") .append(bitcoinUri) .append("\">") .append(bitcoinUri) .append("</a>"); return matcher.end(); } | BitcoinUriParser implements UriParser { @Override public int linkifyUri(String text, int startPos, StringBuffer outputBuffer) { Matcher matcher = BITCOIN_URI_PATTERN.matcher(text); if (!matcher.find(startPos) || matcher.start() != startPos) { return startPos; } String bitcoinUri = matcher.group(); outputBuffer.append("<a href=\"") .append(bitcoinUri) .append("\">") .append(bitcoinUri) .append("</a>"); return matcher.end(); } } | BitcoinUriParser implements UriParser { @Override public int linkifyUri(String text, int startPos, StringBuffer outputBuffer) { Matcher matcher = BITCOIN_URI_PATTERN.matcher(text); if (!matcher.find(startPos) || matcher.start() != startPos) { return startPos; } String bitcoinUri = matcher.group(); outputBuffer.append("<a href=\"") .append(bitcoinUri) .append("\">") .append(bitcoinUri) .append("</a>"); return matcher.end(); } } | BitcoinUriParser implements UriParser { @Override public int linkifyUri(String text, int startPos, StringBuffer outputBuffer) { Matcher matcher = BITCOIN_URI_PATTERN.matcher(text); if (!matcher.find(startPos) || matcher.start() != startPos) { return startPos; } String bitcoinUri = matcher.group(); outputBuffer.append("<a href=\"") .append(bitcoinUri) .append("\">") .append(bitcoinUri) .append("</a>"); return matcher.end(); } @Override int linkifyUri(String text, int startPos, StringBuffer outputBuffer); } | BitcoinUriParser implements UriParser { @Override public int linkifyUri(String text, int startPos, StringBuffer outputBuffer) { Matcher matcher = BITCOIN_URI_PATTERN.matcher(text); if (!matcher.find(startPos) || matcher.start() != startPos) { return startPos; } String bitcoinUri = matcher.group(); outputBuffer.append("<a href=\"") .append(bitcoinUri) .append("\">") .append(bitcoinUri) .append("</a>"); return matcher.end(); } @Override int linkifyUri(String text, int startPos, StringBuffer outputBuffer); } |
@Test public void build_shouldSucceed() throws Exception { MessageBuilder messageBuilder = createSimpleMessageBuilder(); messageBuilder.buildAsync(callback); MimeMessage message = getMessageFromCallback(); assertEquals("text/plain", message.getMimeType()); assertEquals(TEST_SUBJECT, message.getSubject()); assertEquals(TEST_IDENTITY_ADDRESS, message.getFrom()[0]); assertArrayEquals(TEST_TO, message.getRecipients(RecipientType.TO)); assertArrayEquals(TEST_CC, message.getRecipients(RecipientType.CC)); assertArrayEquals(TEST_BCC, message.getRecipients(RecipientType.BCC)); assertEquals(MESSAGE_HEADERS + MESSAGE_CONTENT, getMessageContents(message)); } | final public void buildAsync(Callback callback) { synchronized (callbackLock) { asyncCallback = callback; queuedMimeMessage = null; queuedException = null; queuedPendingIntent = null; } new AsyncTask<Void,Void,Void>() { @Override protected Void doInBackground(Void... params) { buildMessageInternal(); return null; } @Override protected void onPostExecute(Void aVoid) { deliverResult(); } }.execute(); } | MessageBuilder { final public void buildAsync(Callback callback) { synchronized (callbackLock) { asyncCallback = callback; queuedMimeMessage = null; queuedException = null; queuedPendingIntent = null; } new AsyncTask<Void,Void,Void>() { @Override protected Void doInBackground(Void... params) { buildMessageInternal(); return null; } @Override protected void onPostExecute(Void aVoid) { deliverResult(); } }.execute(); } } | MessageBuilder { final public void buildAsync(Callback callback) { synchronized (callbackLock) { asyncCallback = callback; queuedMimeMessage = null; queuedException = null; queuedPendingIntent = null; } new AsyncTask<Void,Void,Void>() { @Override protected Void doInBackground(Void... params) { buildMessageInternal(); return null; } @Override protected void onPostExecute(Void aVoid) { deliverResult(); } }.execute(); } protected MessageBuilder(Context context, MessageIdGenerator messageIdGenerator, BoundaryGenerator boundaryGenerator); } | MessageBuilder { final public void buildAsync(Callback callback) { synchronized (callbackLock) { asyncCallback = callback; queuedMimeMessage = null; queuedException = null; queuedPendingIntent = null; } new AsyncTask<Void,Void,Void>() { @Override protected Void doInBackground(Void... params) { buildMessageInternal(); return null; } @Override protected void onPostExecute(Void aVoid) { deliverResult(); } }.execute(); } protected MessageBuilder(Context context, MessageIdGenerator messageIdGenerator, BoundaryGenerator boundaryGenerator); MessageBuilder setSubject(String subject); MessageBuilder setSentDate(Date sentDate); MessageBuilder setHideTimeZone(boolean hideTimeZone); MessageBuilder setTo(List<Address> to); MessageBuilder setCc(List<Address> cc); MessageBuilder setBcc(List<Address> bcc); MessageBuilder setInReplyTo(String inReplyTo); MessageBuilder setReferences(String references); MessageBuilder setRequestReadReceipt(boolean requestReadReceipt); MessageBuilder setIdentity(Identity identity); MessageBuilder setMessageFormat(SimpleMessageFormat messageFormat); MessageBuilder setText(String text); MessageBuilder setAttachments(List<Attachment> attachments); MessageBuilder setSignature(String signature); MessageBuilder setQuoteStyle(QuoteStyle quoteStyle); MessageBuilder setQuotedTextMode(QuotedTextMode quotedTextMode); MessageBuilder setQuotedText(String quotedText); MessageBuilder setQuotedHtmlContent(InsertableHtmlContent quotedHtmlContent); MessageBuilder setReplyAfterQuote(boolean isReplyAfterQuote); MessageBuilder setSignatureBeforeQuotedText(boolean isSignatureBeforeQuotedText); MessageBuilder setIdentityChanged(boolean identityChanged); MessageBuilder setSignatureChanged(boolean signatureChanged); MessageBuilder setCursorPosition(int cursorPosition); MessageBuilder setMessageReference(MessageReference messageReference); MessageBuilder setDraft(boolean isDraft); MessageBuilder setIsPgpInlineEnabled(boolean isPgpInlineEnabled); boolean isDraft(); final void buildAsync(Callback callback); final void onActivityResult(final int requestCode, int resultCode, final Intent data, Callback callback); final void detachCallback(); final void reattachCallback(Callback callback); } | MessageBuilder { final public void buildAsync(Callback callback) { synchronized (callbackLock) { asyncCallback = callback; queuedMimeMessage = null; queuedException = null; queuedPendingIntent = null; } new AsyncTask<Void,Void,Void>() { @Override protected Void doInBackground(Void... params) { buildMessageInternal(); return null; } @Override protected void onPostExecute(Void aVoid) { deliverResult(); } }.execute(); } protected MessageBuilder(Context context, MessageIdGenerator messageIdGenerator, BoundaryGenerator boundaryGenerator); MessageBuilder setSubject(String subject); MessageBuilder setSentDate(Date sentDate); MessageBuilder setHideTimeZone(boolean hideTimeZone); MessageBuilder setTo(List<Address> to); MessageBuilder setCc(List<Address> cc); MessageBuilder setBcc(List<Address> bcc); MessageBuilder setInReplyTo(String inReplyTo); MessageBuilder setReferences(String references); MessageBuilder setRequestReadReceipt(boolean requestReadReceipt); MessageBuilder setIdentity(Identity identity); MessageBuilder setMessageFormat(SimpleMessageFormat messageFormat); MessageBuilder setText(String text); MessageBuilder setAttachments(List<Attachment> attachments); MessageBuilder setSignature(String signature); MessageBuilder setQuoteStyle(QuoteStyle quoteStyle); MessageBuilder setQuotedTextMode(QuotedTextMode quotedTextMode); MessageBuilder setQuotedText(String quotedText); MessageBuilder setQuotedHtmlContent(InsertableHtmlContent quotedHtmlContent); MessageBuilder setReplyAfterQuote(boolean isReplyAfterQuote); MessageBuilder setSignatureBeforeQuotedText(boolean isSignatureBeforeQuotedText); MessageBuilder setIdentityChanged(boolean identityChanged); MessageBuilder setSignatureChanged(boolean signatureChanged); MessageBuilder setCursorPosition(int cursorPosition); MessageBuilder setMessageReference(MessageReference messageReference); MessageBuilder setDraft(boolean isDraft); MessageBuilder setIsPgpInlineEnabled(boolean isPgpInlineEnabled); boolean isDraft(); final void buildAsync(Callback callback); final void onActivityResult(final int requestCode, int resultCode, final Intent data, Callback callback); final void detachCallback(); final void reattachCallback(Callback callback); } |
@Test public void build_usingHtmlFormat_shouldUseMultipartAlternativeInCorrectOrder() { MessageBuilder messageBuilder = createHtmlMessageBuilder(); messageBuilder.buildAsync(callback); MimeMessage message = getMessageFromCallback(); assertEquals(MimeMultipart.class, message.getBody().getClass()); assertEquals("multipart/alternative", ((MimeMultipart) message.getBody()).getMimeType()); List<BodyPart> parts = ((MimeMultipart) message.getBody()).getBodyParts(); assertEquals("text/plain", parts.get(0).getMimeType()); assertEquals("text/html", parts.get(1).getMimeType()); } | final public void buildAsync(Callback callback) { synchronized (callbackLock) { asyncCallback = callback; queuedMimeMessage = null; queuedException = null; queuedPendingIntent = null; } new AsyncTask<Void,Void,Void>() { @Override protected Void doInBackground(Void... params) { buildMessageInternal(); return null; } @Override protected void onPostExecute(Void aVoid) { deliverResult(); } }.execute(); } | MessageBuilder { final public void buildAsync(Callback callback) { synchronized (callbackLock) { asyncCallback = callback; queuedMimeMessage = null; queuedException = null; queuedPendingIntent = null; } new AsyncTask<Void,Void,Void>() { @Override protected Void doInBackground(Void... params) { buildMessageInternal(); return null; } @Override protected void onPostExecute(Void aVoid) { deliverResult(); } }.execute(); } } | MessageBuilder { final public void buildAsync(Callback callback) { synchronized (callbackLock) { asyncCallback = callback; queuedMimeMessage = null; queuedException = null; queuedPendingIntent = null; } new AsyncTask<Void,Void,Void>() { @Override protected Void doInBackground(Void... params) { buildMessageInternal(); return null; } @Override protected void onPostExecute(Void aVoid) { deliverResult(); } }.execute(); } protected MessageBuilder(Context context, MessageIdGenerator messageIdGenerator, BoundaryGenerator boundaryGenerator); } | MessageBuilder { final public void buildAsync(Callback callback) { synchronized (callbackLock) { asyncCallback = callback; queuedMimeMessage = null; queuedException = null; queuedPendingIntent = null; } new AsyncTask<Void,Void,Void>() { @Override protected Void doInBackground(Void... params) { buildMessageInternal(); return null; } @Override protected void onPostExecute(Void aVoid) { deliverResult(); } }.execute(); } protected MessageBuilder(Context context, MessageIdGenerator messageIdGenerator, BoundaryGenerator boundaryGenerator); MessageBuilder setSubject(String subject); MessageBuilder setSentDate(Date sentDate); MessageBuilder setHideTimeZone(boolean hideTimeZone); MessageBuilder setTo(List<Address> to); MessageBuilder setCc(List<Address> cc); MessageBuilder setBcc(List<Address> bcc); MessageBuilder setInReplyTo(String inReplyTo); MessageBuilder setReferences(String references); MessageBuilder setRequestReadReceipt(boolean requestReadReceipt); MessageBuilder setIdentity(Identity identity); MessageBuilder setMessageFormat(SimpleMessageFormat messageFormat); MessageBuilder setText(String text); MessageBuilder setAttachments(List<Attachment> attachments); MessageBuilder setSignature(String signature); MessageBuilder setQuoteStyle(QuoteStyle quoteStyle); MessageBuilder setQuotedTextMode(QuotedTextMode quotedTextMode); MessageBuilder setQuotedText(String quotedText); MessageBuilder setQuotedHtmlContent(InsertableHtmlContent quotedHtmlContent); MessageBuilder setReplyAfterQuote(boolean isReplyAfterQuote); MessageBuilder setSignatureBeforeQuotedText(boolean isSignatureBeforeQuotedText); MessageBuilder setIdentityChanged(boolean identityChanged); MessageBuilder setSignatureChanged(boolean signatureChanged); MessageBuilder setCursorPosition(int cursorPosition); MessageBuilder setMessageReference(MessageReference messageReference); MessageBuilder setDraft(boolean isDraft); MessageBuilder setIsPgpInlineEnabled(boolean isPgpInlineEnabled); boolean isDraft(); final void buildAsync(Callback callback); final void onActivityResult(final int requestCode, int resultCode, final Intent data, Callback callback); final void detachCallback(); final void reattachCallback(Callback callback); } | MessageBuilder { final public void buildAsync(Callback callback) { synchronized (callbackLock) { asyncCallback = callback; queuedMimeMessage = null; queuedException = null; queuedPendingIntent = null; } new AsyncTask<Void,Void,Void>() { @Override protected Void doInBackground(Void... params) { buildMessageInternal(); return null; } @Override protected void onPostExecute(Void aVoid) { deliverResult(); } }.execute(); } protected MessageBuilder(Context context, MessageIdGenerator messageIdGenerator, BoundaryGenerator boundaryGenerator); MessageBuilder setSubject(String subject); MessageBuilder setSentDate(Date sentDate); MessageBuilder setHideTimeZone(boolean hideTimeZone); MessageBuilder setTo(List<Address> to); MessageBuilder setCc(List<Address> cc); MessageBuilder setBcc(List<Address> bcc); MessageBuilder setInReplyTo(String inReplyTo); MessageBuilder setReferences(String references); MessageBuilder setRequestReadReceipt(boolean requestReadReceipt); MessageBuilder setIdentity(Identity identity); MessageBuilder setMessageFormat(SimpleMessageFormat messageFormat); MessageBuilder setText(String text); MessageBuilder setAttachments(List<Attachment> attachments); MessageBuilder setSignature(String signature); MessageBuilder setQuoteStyle(QuoteStyle quoteStyle); MessageBuilder setQuotedTextMode(QuotedTextMode quotedTextMode); MessageBuilder setQuotedText(String quotedText); MessageBuilder setQuotedHtmlContent(InsertableHtmlContent quotedHtmlContent); MessageBuilder setReplyAfterQuote(boolean isReplyAfterQuote); MessageBuilder setSignatureBeforeQuotedText(boolean isSignatureBeforeQuotedText); MessageBuilder setIdentityChanged(boolean identityChanged); MessageBuilder setSignatureChanged(boolean signatureChanged); MessageBuilder setCursorPosition(int cursorPosition); MessageBuilder setMessageReference(MessageReference messageReference); MessageBuilder setDraft(boolean isDraft); MessageBuilder setIsPgpInlineEnabled(boolean isPgpInlineEnabled); boolean isDraft(); final void buildAsync(Callback callback); final void onActivityResult(final int requestCode, int resultCode, final Intent data, Callback callback); final void detachCallback(); final void reattachCallback(Callback callback); } |
@Test public void findFirstTextPart_withTextPlainPart() throws Exception { Part part = createTextPart("text/plain"); Part result = textPartFinder.findFirstTextPart(part); assertEquals(part, result); } | @Nullable public Part findFirstTextPart(@NonNull Part part) { String mimeType = part.getMimeType(); Body body = part.getBody(); if (body instanceof Multipart) { Multipart multipart = (Multipart) body; if (isSameMimeType(mimeType, "multipart/alternative")) { return findTextPartInMultipartAlternative(multipart); } else { return findTextPartInMultipart(multipart); } } else if (isSameMimeType(mimeType, "text/plain") || isSameMimeType(mimeType, "text/html")) { return part; } return null; } | TextPartFinder { @Nullable public Part findFirstTextPart(@NonNull Part part) { String mimeType = part.getMimeType(); Body body = part.getBody(); if (body instanceof Multipart) { Multipart multipart = (Multipart) body; if (isSameMimeType(mimeType, "multipart/alternative")) { return findTextPartInMultipartAlternative(multipart); } else { return findTextPartInMultipart(multipart); } } else if (isSameMimeType(mimeType, "text/plain") || isSameMimeType(mimeType, "text/html")) { return part; } return null; } } | TextPartFinder { @Nullable public Part findFirstTextPart(@NonNull Part part) { String mimeType = part.getMimeType(); Body body = part.getBody(); if (body instanceof Multipart) { Multipart multipart = (Multipart) body; if (isSameMimeType(mimeType, "multipart/alternative")) { return findTextPartInMultipartAlternative(multipart); } else { return findTextPartInMultipart(multipart); } } else if (isSameMimeType(mimeType, "text/plain") || isSameMimeType(mimeType, "text/html")) { return part; } return null; } } | TextPartFinder { @Nullable public Part findFirstTextPart(@NonNull Part part) { String mimeType = part.getMimeType(); Body body = part.getBody(); if (body instanceof Multipart) { Multipart multipart = (Multipart) body; if (isSameMimeType(mimeType, "multipart/alternative")) { return findTextPartInMultipartAlternative(multipart); } else { return findTextPartInMultipart(multipart); } } else if (isSameMimeType(mimeType, "text/plain") || isSameMimeType(mimeType, "text/html")) { return part; } return null; } @Nullable Part findFirstTextPart(@NonNull Part part); } | TextPartFinder { @Nullable public Part findFirstTextPart(@NonNull Part part) { String mimeType = part.getMimeType(); Body body = part.getBody(); if (body instanceof Multipart) { Multipart multipart = (Multipart) body; if (isSameMimeType(mimeType, "multipart/alternative")) { return findTextPartInMultipartAlternative(multipart); } else { return findTextPartInMultipart(multipart); } } else if (isSameMimeType(mimeType, "text/plain") || isSameMimeType(mimeType, "text/html")) { return part; } return null; } @Nullable Part findFirstTextPart(@NonNull Part part); } |
@Test public void findFirstTextPart_withTextHtmlPart() throws Exception { Part part = createTextPart("text/html"); Part result = textPartFinder.findFirstTextPart(part); assertEquals(part, result); } | @Nullable public Part findFirstTextPart(@NonNull Part part) { String mimeType = part.getMimeType(); Body body = part.getBody(); if (body instanceof Multipart) { Multipart multipart = (Multipart) body; if (isSameMimeType(mimeType, "multipart/alternative")) { return findTextPartInMultipartAlternative(multipart); } else { return findTextPartInMultipart(multipart); } } else if (isSameMimeType(mimeType, "text/plain") || isSameMimeType(mimeType, "text/html")) { return part; } return null; } | TextPartFinder { @Nullable public Part findFirstTextPart(@NonNull Part part) { String mimeType = part.getMimeType(); Body body = part.getBody(); if (body instanceof Multipart) { Multipart multipart = (Multipart) body; if (isSameMimeType(mimeType, "multipart/alternative")) { return findTextPartInMultipartAlternative(multipart); } else { return findTextPartInMultipart(multipart); } } else if (isSameMimeType(mimeType, "text/plain") || isSameMimeType(mimeType, "text/html")) { return part; } return null; } } | TextPartFinder { @Nullable public Part findFirstTextPart(@NonNull Part part) { String mimeType = part.getMimeType(); Body body = part.getBody(); if (body instanceof Multipart) { Multipart multipart = (Multipart) body; if (isSameMimeType(mimeType, "multipart/alternative")) { return findTextPartInMultipartAlternative(multipart); } else { return findTextPartInMultipart(multipart); } } else if (isSameMimeType(mimeType, "text/plain") || isSameMimeType(mimeType, "text/html")) { return part; } return null; } } | TextPartFinder { @Nullable public Part findFirstTextPart(@NonNull Part part) { String mimeType = part.getMimeType(); Body body = part.getBody(); if (body instanceof Multipart) { Multipart multipart = (Multipart) body; if (isSameMimeType(mimeType, "multipart/alternative")) { return findTextPartInMultipartAlternative(multipart); } else { return findTextPartInMultipart(multipart); } } else if (isSameMimeType(mimeType, "text/plain") || isSameMimeType(mimeType, "text/html")) { return part; } return null; } @Nullable Part findFirstTextPart(@NonNull Part part); } | TextPartFinder { @Nullable public Part findFirstTextPart(@NonNull Part part) { String mimeType = part.getMimeType(); Body body = part.getBody(); if (body instanceof Multipart) { Multipart multipart = (Multipart) body; if (isSameMimeType(mimeType, "multipart/alternative")) { return findTextPartInMultipartAlternative(multipart); } else { return findTextPartInMultipart(multipart); } } else if (isSameMimeType(mimeType, "text/plain") || isSameMimeType(mimeType, "text/html")) { return part; } return null; } @Nullable Part findFirstTextPart(@NonNull Part part); } |
@Test public void findFirstTextPart_withoutTextPart() throws Exception { Part part = createPart("image/jpeg"); Part result = textPartFinder.findFirstTextPart(part); assertNull(result); } | @Nullable public Part findFirstTextPart(@NonNull Part part) { String mimeType = part.getMimeType(); Body body = part.getBody(); if (body instanceof Multipart) { Multipart multipart = (Multipart) body; if (isSameMimeType(mimeType, "multipart/alternative")) { return findTextPartInMultipartAlternative(multipart); } else { return findTextPartInMultipart(multipart); } } else if (isSameMimeType(mimeType, "text/plain") || isSameMimeType(mimeType, "text/html")) { return part; } return null; } | TextPartFinder { @Nullable public Part findFirstTextPart(@NonNull Part part) { String mimeType = part.getMimeType(); Body body = part.getBody(); if (body instanceof Multipart) { Multipart multipart = (Multipart) body; if (isSameMimeType(mimeType, "multipart/alternative")) { return findTextPartInMultipartAlternative(multipart); } else { return findTextPartInMultipart(multipart); } } else if (isSameMimeType(mimeType, "text/plain") || isSameMimeType(mimeType, "text/html")) { return part; } return null; } } | TextPartFinder { @Nullable public Part findFirstTextPart(@NonNull Part part) { String mimeType = part.getMimeType(); Body body = part.getBody(); if (body instanceof Multipart) { Multipart multipart = (Multipart) body; if (isSameMimeType(mimeType, "multipart/alternative")) { return findTextPartInMultipartAlternative(multipart); } else { return findTextPartInMultipart(multipart); } } else if (isSameMimeType(mimeType, "text/plain") || isSameMimeType(mimeType, "text/html")) { return part; } return null; } } | TextPartFinder { @Nullable public Part findFirstTextPart(@NonNull Part part) { String mimeType = part.getMimeType(); Body body = part.getBody(); if (body instanceof Multipart) { Multipart multipart = (Multipart) body; if (isSameMimeType(mimeType, "multipart/alternative")) { return findTextPartInMultipartAlternative(multipart); } else { return findTextPartInMultipart(multipart); } } else if (isSameMimeType(mimeType, "text/plain") || isSameMimeType(mimeType, "text/html")) { return part; } return null; } @Nullable Part findFirstTextPart(@NonNull Part part); } | TextPartFinder { @Nullable public Part findFirstTextPart(@NonNull Part part) { String mimeType = part.getMimeType(); Body body = part.getBody(); if (body instanceof Multipart) { Multipart multipart = (Multipart) body; if (isSameMimeType(mimeType, "multipart/alternative")) { return findTextPartInMultipartAlternative(multipart); } else { return findTextPartInMultipart(multipart); } } else if (isSameMimeType(mimeType, "text/plain") || isSameMimeType(mimeType, "text/html")) { return part; } return null; } @Nullable Part findFirstTextPart(@NonNull Part part); } |
@Test public void findFirstTextPart_withMultipartAlternative() throws Exception { BodyPart expected = createTextPart("text/plain"); Part part = createMultipart("multipart/alternative", expected, createTextPart("text/html")); Part result = textPartFinder.findFirstTextPart(part); assertEquals(expected, result); } | @Nullable public Part findFirstTextPart(@NonNull Part part) { String mimeType = part.getMimeType(); Body body = part.getBody(); if (body instanceof Multipart) { Multipart multipart = (Multipart) body; if (isSameMimeType(mimeType, "multipart/alternative")) { return findTextPartInMultipartAlternative(multipart); } else { return findTextPartInMultipart(multipart); } } else if (isSameMimeType(mimeType, "text/plain") || isSameMimeType(mimeType, "text/html")) { return part; } return null; } | TextPartFinder { @Nullable public Part findFirstTextPart(@NonNull Part part) { String mimeType = part.getMimeType(); Body body = part.getBody(); if (body instanceof Multipart) { Multipart multipart = (Multipart) body; if (isSameMimeType(mimeType, "multipart/alternative")) { return findTextPartInMultipartAlternative(multipart); } else { return findTextPartInMultipart(multipart); } } else if (isSameMimeType(mimeType, "text/plain") || isSameMimeType(mimeType, "text/html")) { return part; } return null; } } | TextPartFinder { @Nullable public Part findFirstTextPart(@NonNull Part part) { String mimeType = part.getMimeType(); Body body = part.getBody(); if (body instanceof Multipart) { Multipart multipart = (Multipart) body; if (isSameMimeType(mimeType, "multipart/alternative")) { return findTextPartInMultipartAlternative(multipart); } else { return findTextPartInMultipart(multipart); } } else if (isSameMimeType(mimeType, "text/plain") || isSameMimeType(mimeType, "text/html")) { return part; } return null; } } | TextPartFinder { @Nullable public Part findFirstTextPart(@NonNull Part part) { String mimeType = part.getMimeType(); Body body = part.getBody(); if (body instanceof Multipart) { Multipart multipart = (Multipart) body; if (isSameMimeType(mimeType, "multipart/alternative")) { return findTextPartInMultipartAlternative(multipart); } else { return findTextPartInMultipart(multipart); } } else if (isSameMimeType(mimeType, "text/plain") || isSameMimeType(mimeType, "text/html")) { return part; } return null; } @Nullable Part findFirstTextPart(@NonNull Part part); } | TextPartFinder { @Nullable public Part findFirstTextPart(@NonNull Part part) { String mimeType = part.getMimeType(); Body body = part.getBody(); if (body instanceof Multipart) { Multipart multipart = (Multipart) body; if (isSameMimeType(mimeType, "multipart/alternative")) { return findTextPartInMultipartAlternative(multipart); } else { return findTextPartInMultipart(multipart); } } else if (isSameMimeType(mimeType, "text/plain") || isSameMimeType(mimeType, "text/html")) { return part; } return null; } @Nullable Part findFirstTextPart(@NonNull Part part); } |
@Test public void findFirstTextPart_withMultipartAlternativeHtmlPartFirst() throws Exception { BodyPart expected = createTextPart("text/plain"); Part part = createMultipart("multipart/alternative", createTextPart("text/html"), expected); Part result = textPartFinder.findFirstTextPart(part); assertEquals(expected, result); } | @Nullable public Part findFirstTextPart(@NonNull Part part) { String mimeType = part.getMimeType(); Body body = part.getBody(); if (body instanceof Multipart) { Multipart multipart = (Multipart) body; if (isSameMimeType(mimeType, "multipart/alternative")) { return findTextPartInMultipartAlternative(multipart); } else { return findTextPartInMultipart(multipart); } } else if (isSameMimeType(mimeType, "text/plain") || isSameMimeType(mimeType, "text/html")) { return part; } return null; } | TextPartFinder { @Nullable public Part findFirstTextPart(@NonNull Part part) { String mimeType = part.getMimeType(); Body body = part.getBody(); if (body instanceof Multipart) { Multipart multipart = (Multipart) body; if (isSameMimeType(mimeType, "multipart/alternative")) { return findTextPartInMultipartAlternative(multipart); } else { return findTextPartInMultipart(multipart); } } else if (isSameMimeType(mimeType, "text/plain") || isSameMimeType(mimeType, "text/html")) { return part; } return null; } } | TextPartFinder { @Nullable public Part findFirstTextPart(@NonNull Part part) { String mimeType = part.getMimeType(); Body body = part.getBody(); if (body instanceof Multipart) { Multipart multipart = (Multipart) body; if (isSameMimeType(mimeType, "multipart/alternative")) { return findTextPartInMultipartAlternative(multipart); } else { return findTextPartInMultipart(multipart); } } else if (isSameMimeType(mimeType, "text/plain") || isSameMimeType(mimeType, "text/html")) { return part; } return null; } } | TextPartFinder { @Nullable public Part findFirstTextPart(@NonNull Part part) { String mimeType = part.getMimeType(); Body body = part.getBody(); if (body instanceof Multipart) { Multipart multipart = (Multipart) body; if (isSameMimeType(mimeType, "multipart/alternative")) { return findTextPartInMultipartAlternative(multipart); } else { return findTextPartInMultipart(multipart); } } else if (isSameMimeType(mimeType, "text/plain") || isSameMimeType(mimeType, "text/html")) { return part; } return null; } @Nullable Part findFirstTextPart(@NonNull Part part); } | TextPartFinder { @Nullable public Part findFirstTextPart(@NonNull Part part) { String mimeType = part.getMimeType(); Body body = part.getBody(); if (body instanceof Multipart) { Multipart multipart = (Multipart) body; if (isSameMimeType(mimeType, "multipart/alternative")) { return findTextPartInMultipartAlternative(multipart); } else { return findTextPartInMultipart(multipart); } } else if (isSameMimeType(mimeType, "text/plain") || isSameMimeType(mimeType, "text/html")) { return part; } return null; } @Nullable Part findFirstTextPart(@NonNull Part part); } |
@Test public void findFirstTextPart_withMultipartAlternativeContainingOnlyTextHtmlPart() throws Exception { BodyPart expected = createTextPart("text/html"); Part part = createMultipart("multipart/alternative", createPart("image/gif"), expected, createTextPart("text/html")); Part result = textPartFinder.findFirstTextPart(part); assertEquals(expected, result); } | @Nullable public Part findFirstTextPart(@NonNull Part part) { String mimeType = part.getMimeType(); Body body = part.getBody(); if (body instanceof Multipart) { Multipart multipart = (Multipart) body; if (isSameMimeType(mimeType, "multipart/alternative")) { return findTextPartInMultipartAlternative(multipart); } else { return findTextPartInMultipart(multipart); } } else if (isSameMimeType(mimeType, "text/plain") || isSameMimeType(mimeType, "text/html")) { return part; } return null; } | TextPartFinder { @Nullable public Part findFirstTextPart(@NonNull Part part) { String mimeType = part.getMimeType(); Body body = part.getBody(); if (body instanceof Multipart) { Multipart multipart = (Multipart) body; if (isSameMimeType(mimeType, "multipart/alternative")) { return findTextPartInMultipartAlternative(multipart); } else { return findTextPartInMultipart(multipart); } } else if (isSameMimeType(mimeType, "text/plain") || isSameMimeType(mimeType, "text/html")) { return part; } return null; } } | TextPartFinder { @Nullable public Part findFirstTextPart(@NonNull Part part) { String mimeType = part.getMimeType(); Body body = part.getBody(); if (body instanceof Multipart) { Multipart multipart = (Multipart) body; if (isSameMimeType(mimeType, "multipart/alternative")) { return findTextPartInMultipartAlternative(multipart); } else { return findTextPartInMultipart(multipart); } } else if (isSameMimeType(mimeType, "text/plain") || isSameMimeType(mimeType, "text/html")) { return part; } return null; } } | TextPartFinder { @Nullable public Part findFirstTextPart(@NonNull Part part) { String mimeType = part.getMimeType(); Body body = part.getBody(); if (body instanceof Multipart) { Multipart multipart = (Multipart) body; if (isSameMimeType(mimeType, "multipart/alternative")) { return findTextPartInMultipartAlternative(multipart); } else { return findTextPartInMultipart(multipart); } } else if (isSameMimeType(mimeType, "text/plain") || isSameMimeType(mimeType, "text/html")) { return part; } return null; } @Nullable Part findFirstTextPart(@NonNull Part part); } | TextPartFinder { @Nullable public Part findFirstTextPart(@NonNull Part part) { String mimeType = part.getMimeType(); Body body = part.getBody(); if (body instanceof Multipart) { Multipart multipart = (Multipart) body; if (isSameMimeType(mimeType, "multipart/alternative")) { return findTextPartInMultipartAlternative(multipart); } else { return findTextPartInMultipart(multipart); } } else if (isSameMimeType(mimeType, "text/plain") || isSameMimeType(mimeType, "text/html")) { return part; } return null; } @Nullable Part findFirstTextPart(@NonNull Part part); } |
@Test public void findEncrypted__withMultipartEncrypted__shouldReturnRoot() throws Exception { Message message = messageFromBody( multipart("encrypted", bodypart("application/pgp-encrypted"), bodypart("application/octet-stream") ) ); List<Part> encryptedParts = MessageDecryptVerifier.findEncryptedParts(message); assertEquals(1, encryptedParts.size()); assertSame(message, encryptedParts.get(0)); } | public static List<Part> findEncryptedParts(Part startPart) { List<Part> encryptedParts = new ArrayList<>(); Stack<Part> partsToCheck = new Stack<>(); partsToCheck.push(startPart); while (!partsToCheck.isEmpty()) { Part part = partsToCheck.pop(); Body body = part.getBody(); if (isPartMultipartEncrypted(part)) { encryptedParts.add(part); continue; } 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; } | MessageDecryptVerifier { public static List<Part> findEncryptedParts(Part startPart) { List<Part> encryptedParts = new ArrayList<>(); Stack<Part> partsToCheck = new Stack<>(); partsToCheck.push(startPart); while (!partsToCheck.isEmpty()) { Part part = partsToCheck.pop(); Body body = part.getBody(); if (isPartMultipartEncrypted(part)) { encryptedParts.add(part); continue; } 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; } } | MessageDecryptVerifier { public static List<Part> findEncryptedParts(Part startPart) { List<Part> encryptedParts = new ArrayList<>(); Stack<Part> partsToCheck = new Stack<>(); partsToCheck.push(startPart); while (!partsToCheck.isEmpty()) { Part part = partsToCheck.pop(); Body body = part.getBody(); if (isPartMultipartEncrypted(part)) { encryptedParts.add(part); continue; } 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; } } | MessageDecryptVerifier { public static List<Part> findEncryptedParts(Part startPart) { List<Part> encryptedParts = new ArrayList<>(); Stack<Part> partsToCheck = new Stack<>(); partsToCheck.push(startPart); while (!partsToCheck.isEmpty()) { Part part = partsToCheck.pop(); Body body = part.getBody(); if (isPartMultipartEncrypted(part)) { encryptedParts.add(part); continue; } 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; } static Part findPrimaryEncryptedOrSignedPart(Part part, List<Part> outputExtraParts); static List<Part> findEncryptedParts(Part startPart); static List<Part> findSignedParts(Part startPart, MessageCryptoAnnotations messageCryptoAnnotations); static List<Part> findPgpInlineParts(Part startPart); static byte[] getSignatureData(Part part); static boolean isPgpMimeEncryptedOrSignedPart(Part part); static boolean isSMimeEncryptedOrSignedPart(Part part); static boolean isPartPgpInlineEncrypted(@Nullable Part part); } | MessageDecryptVerifier { public static List<Part> findEncryptedParts(Part startPart) { List<Part> encryptedParts = new ArrayList<>(); Stack<Part> partsToCheck = new Stack<>(); partsToCheck.push(startPart); while (!partsToCheck.isEmpty()) { Part part = partsToCheck.pop(); Body body = part.getBody(); if (isPartMultipartEncrypted(part)) { encryptedParts.add(part); continue; } 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; } static Part findPrimaryEncryptedOrSignedPart(Part part, List<Part> outputExtraParts); static List<Part> findEncryptedParts(Part startPart); static List<Part> findSignedParts(Part startPart, MessageCryptoAnnotations messageCryptoAnnotations); static List<Part> findPgpInlineParts(Part startPart); static byte[] getSignatureData(Part part); static boolean isPgpMimeEncryptedOrSignedPart(Part part); static boolean isSMimeEncryptedOrSignedPart(Part part); static boolean isPartPgpInlineEncrypted(@Nullable Part part); } |
@Test public void findFirstTextPart_withMultipartAlternativeNotContainingTextPart() throws Exception { Part part = createMultipart("multipart/alternative", createPart("image/gif"), createPart("application/pdf")); Part result = textPartFinder.findFirstTextPart(part); assertNull(result); } | @Nullable public Part findFirstTextPart(@NonNull Part part) { String mimeType = part.getMimeType(); Body body = part.getBody(); if (body instanceof Multipart) { Multipart multipart = (Multipart) body; if (isSameMimeType(mimeType, "multipart/alternative")) { return findTextPartInMultipartAlternative(multipart); } else { return findTextPartInMultipart(multipart); } } else if (isSameMimeType(mimeType, "text/plain") || isSameMimeType(mimeType, "text/html")) { return part; } return null; } | TextPartFinder { @Nullable public Part findFirstTextPart(@NonNull Part part) { String mimeType = part.getMimeType(); Body body = part.getBody(); if (body instanceof Multipart) { Multipart multipart = (Multipart) body; if (isSameMimeType(mimeType, "multipart/alternative")) { return findTextPartInMultipartAlternative(multipart); } else { return findTextPartInMultipart(multipart); } } else if (isSameMimeType(mimeType, "text/plain") || isSameMimeType(mimeType, "text/html")) { return part; } return null; } } | TextPartFinder { @Nullable public Part findFirstTextPart(@NonNull Part part) { String mimeType = part.getMimeType(); Body body = part.getBody(); if (body instanceof Multipart) { Multipart multipart = (Multipart) body; if (isSameMimeType(mimeType, "multipart/alternative")) { return findTextPartInMultipartAlternative(multipart); } else { return findTextPartInMultipart(multipart); } } else if (isSameMimeType(mimeType, "text/plain") || isSameMimeType(mimeType, "text/html")) { return part; } return null; } } | TextPartFinder { @Nullable public Part findFirstTextPart(@NonNull Part part) { String mimeType = part.getMimeType(); Body body = part.getBody(); if (body instanceof Multipart) { Multipart multipart = (Multipart) body; if (isSameMimeType(mimeType, "multipart/alternative")) { return findTextPartInMultipartAlternative(multipart); } else { return findTextPartInMultipart(multipart); } } else if (isSameMimeType(mimeType, "text/plain") || isSameMimeType(mimeType, "text/html")) { return part; } return null; } @Nullable Part findFirstTextPart(@NonNull Part part); } | TextPartFinder { @Nullable public Part findFirstTextPart(@NonNull Part part) { String mimeType = part.getMimeType(); Body body = part.getBody(); if (body instanceof Multipart) { Multipart multipart = (Multipart) body; if (isSameMimeType(mimeType, "multipart/alternative")) { return findTextPartInMultipartAlternative(multipart); } else { return findTextPartInMultipart(multipart); } } else if (isSameMimeType(mimeType, "text/plain") || isSameMimeType(mimeType, "text/html")) { return part; } return null; } @Nullable Part findFirstTextPart(@NonNull Part part); } |
@Test public void findFirstTextPart_withMultipartAlternativeContainingMultipartRelatedContainingTextPlain() throws Exception { BodyPart expected = createTextPart("text/plain"); Part part = createMultipart("multipart/alternative", createMultipart("multipart/related", expected, createPart("image/jpeg")), createTextPart("text/html")); Part result = textPartFinder.findFirstTextPart(part); assertEquals(expected, result); } | @Nullable public Part findFirstTextPart(@NonNull Part part) { String mimeType = part.getMimeType(); Body body = part.getBody(); if (body instanceof Multipart) { Multipart multipart = (Multipart) body; if (isSameMimeType(mimeType, "multipart/alternative")) { return findTextPartInMultipartAlternative(multipart); } else { return findTextPartInMultipart(multipart); } } else if (isSameMimeType(mimeType, "text/plain") || isSameMimeType(mimeType, "text/html")) { return part; } return null; } | TextPartFinder { @Nullable public Part findFirstTextPart(@NonNull Part part) { String mimeType = part.getMimeType(); Body body = part.getBody(); if (body instanceof Multipart) { Multipart multipart = (Multipart) body; if (isSameMimeType(mimeType, "multipart/alternative")) { return findTextPartInMultipartAlternative(multipart); } else { return findTextPartInMultipart(multipart); } } else if (isSameMimeType(mimeType, "text/plain") || isSameMimeType(mimeType, "text/html")) { return part; } return null; } } | TextPartFinder { @Nullable public Part findFirstTextPart(@NonNull Part part) { String mimeType = part.getMimeType(); Body body = part.getBody(); if (body instanceof Multipart) { Multipart multipart = (Multipart) body; if (isSameMimeType(mimeType, "multipart/alternative")) { return findTextPartInMultipartAlternative(multipart); } else { return findTextPartInMultipart(multipart); } } else if (isSameMimeType(mimeType, "text/plain") || isSameMimeType(mimeType, "text/html")) { return part; } return null; } } | TextPartFinder { @Nullable public Part findFirstTextPart(@NonNull Part part) { String mimeType = part.getMimeType(); Body body = part.getBody(); if (body instanceof Multipart) { Multipart multipart = (Multipart) body; if (isSameMimeType(mimeType, "multipart/alternative")) { return findTextPartInMultipartAlternative(multipart); } else { return findTextPartInMultipart(multipart); } } else if (isSameMimeType(mimeType, "text/plain") || isSameMimeType(mimeType, "text/html")) { return part; } return null; } @Nullable Part findFirstTextPart(@NonNull Part part); } | TextPartFinder { @Nullable public Part findFirstTextPart(@NonNull Part part) { String mimeType = part.getMimeType(); Body body = part.getBody(); if (body instanceof Multipart) { Multipart multipart = (Multipart) body; if (isSameMimeType(mimeType, "multipart/alternative")) { return findTextPartInMultipartAlternative(multipart); } else { return findTextPartInMultipart(multipart); } } else if (isSameMimeType(mimeType, "text/plain") || isSameMimeType(mimeType, "text/html")) { return part; } return null; } @Nullable Part findFirstTextPart(@NonNull Part part); } |
@Test public void findFirstTextPart_withMultipartAlternativeContainingMultipartRelatedContainingTextHtmlFirst() throws Exception { BodyPart expected = createTextPart("text/plain"); Part part = createMultipart("multipart/alternative", createMultipart("multipart/related", createTextPart("text/html"), createPart("image/jpeg")), expected); Part result = textPartFinder.findFirstTextPart(part); assertEquals(expected, result); } | @Nullable public Part findFirstTextPart(@NonNull Part part) { String mimeType = part.getMimeType(); Body body = part.getBody(); if (body instanceof Multipart) { Multipart multipart = (Multipart) body; if (isSameMimeType(mimeType, "multipart/alternative")) { return findTextPartInMultipartAlternative(multipart); } else { return findTextPartInMultipart(multipart); } } else if (isSameMimeType(mimeType, "text/plain") || isSameMimeType(mimeType, "text/html")) { return part; } return null; } | TextPartFinder { @Nullable public Part findFirstTextPart(@NonNull Part part) { String mimeType = part.getMimeType(); Body body = part.getBody(); if (body instanceof Multipart) { Multipart multipart = (Multipart) body; if (isSameMimeType(mimeType, "multipart/alternative")) { return findTextPartInMultipartAlternative(multipart); } else { return findTextPartInMultipart(multipart); } } else if (isSameMimeType(mimeType, "text/plain") || isSameMimeType(mimeType, "text/html")) { return part; } return null; } } | TextPartFinder { @Nullable public Part findFirstTextPart(@NonNull Part part) { String mimeType = part.getMimeType(); Body body = part.getBody(); if (body instanceof Multipart) { Multipart multipart = (Multipart) body; if (isSameMimeType(mimeType, "multipart/alternative")) { return findTextPartInMultipartAlternative(multipart); } else { return findTextPartInMultipart(multipart); } } else if (isSameMimeType(mimeType, "text/plain") || isSameMimeType(mimeType, "text/html")) { return part; } return null; } } | TextPartFinder { @Nullable public Part findFirstTextPart(@NonNull Part part) { String mimeType = part.getMimeType(); Body body = part.getBody(); if (body instanceof Multipart) { Multipart multipart = (Multipart) body; if (isSameMimeType(mimeType, "multipart/alternative")) { return findTextPartInMultipartAlternative(multipart); } else { return findTextPartInMultipart(multipart); } } else if (isSameMimeType(mimeType, "text/plain") || isSameMimeType(mimeType, "text/html")) { return part; } return null; } @Nullable Part findFirstTextPart(@NonNull Part part); } | TextPartFinder { @Nullable public Part findFirstTextPart(@NonNull Part part) { String mimeType = part.getMimeType(); Body body = part.getBody(); if (body instanceof Multipart) { Multipart multipart = (Multipart) body; if (isSameMimeType(mimeType, "multipart/alternative")) { return findTextPartInMultipartAlternative(multipart); } else { return findTextPartInMultipart(multipart); } } else if (isSameMimeType(mimeType, "text/plain") || isSameMimeType(mimeType, "text/html")) { return part; } return null; } @Nullable Part findFirstTextPart(@NonNull Part part); } |
@Test public void findFirstTextPart_withMultipartMixedContainingTextPlain() throws Exception { BodyPart expected = createTextPart("text/plain"); Part part = createMultipart("multipart/mixed", createPart("image/jpeg"), expected); Part result = textPartFinder.findFirstTextPart(part); assertEquals(expected, result); } | @Nullable public Part findFirstTextPart(@NonNull Part part) { String mimeType = part.getMimeType(); Body body = part.getBody(); if (body instanceof Multipart) { Multipart multipart = (Multipart) body; if (isSameMimeType(mimeType, "multipart/alternative")) { return findTextPartInMultipartAlternative(multipart); } else { return findTextPartInMultipart(multipart); } } else if (isSameMimeType(mimeType, "text/plain") || isSameMimeType(mimeType, "text/html")) { return part; } return null; } | TextPartFinder { @Nullable public Part findFirstTextPart(@NonNull Part part) { String mimeType = part.getMimeType(); Body body = part.getBody(); if (body instanceof Multipart) { Multipart multipart = (Multipart) body; if (isSameMimeType(mimeType, "multipart/alternative")) { return findTextPartInMultipartAlternative(multipart); } else { return findTextPartInMultipart(multipart); } } else if (isSameMimeType(mimeType, "text/plain") || isSameMimeType(mimeType, "text/html")) { return part; } return null; } } | TextPartFinder { @Nullable public Part findFirstTextPart(@NonNull Part part) { String mimeType = part.getMimeType(); Body body = part.getBody(); if (body instanceof Multipart) { Multipart multipart = (Multipart) body; if (isSameMimeType(mimeType, "multipart/alternative")) { return findTextPartInMultipartAlternative(multipart); } else { return findTextPartInMultipart(multipart); } } else if (isSameMimeType(mimeType, "text/plain") || isSameMimeType(mimeType, "text/html")) { return part; } return null; } } | TextPartFinder { @Nullable public Part findFirstTextPart(@NonNull Part part) { String mimeType = part.getMimeType(); Body body = part.getBody(); if (body instanceof Multipart) { Multipart multipart = (Multipart) body; if (isSameMimeType(mimeType, "multipart/alternative")) { return findTextPartInMultipartAlternative(multipart); } else { return findTextPartInMultipart(multipart); } } else if (isSameMimeType(mimeType, "text/plain") || isSameMimeType(mimeType, "text/html")) { return part; } return null; } @Nullable Part findFirstTextPart(@NonNull Part part); } | TextPartFinder { @Nullable public Part findFirstTextPart(@NonNull Part part) { String mimeType = part.getMimeType(); Body body = part.getBody(); if (body instanceof Multipart) { Multipart multipart = (Multipart) body; if (isSameMimeType(mimeType, "multipart/alternative")) { return findTextPartInMultipartAlternative(multipart); } else { return findTextPartInMultipart(multipart); } } else if (isSameMimeType(mimeType, "text/plain") || isSameMimeType(mimeType, "text/html")) { return part; } return null; } @Nullable Part findFirstTextPart(@NonNull Part part); } |
@Test public void findFirstTextPart_withMultipartMixedContainingTextHtmlFirst() throws Exception { BodyPart expected = createTextPart("text/html"); Part part = createMultipart("multipart/mixed", expected, createTextPart("text/plain")); Part result = textPartFinder.findFirstTextPart(part); assertEquals(expected, result); } | @Nullable public Part findFirstTextPart(@NonNull Part part) { String mimeType = part.getMimeType(); Body body = part.getBody(); if (body instanceof Multipart) { Multipart multipart = (Multipart) body; if (isSameMimeType(mimeType, "multipart/alternative")) { return findTextPartInMultipartAlternative(multipart); } else { return findTextPartInMultipart(multipart); } } else if (isSameMimeType(mimeType, "text/plain") || isSameMimeType(mimeType, "text/html")) { return part; } return null; } | TextPartFinder { @Nullable public Part findFirstTextPart(@NonNull Part part) { String mimeType = part.getMimeType(); Body body = part.getBody(); if (body instanceof Multipart) { Multipart multipart = (Multipart) body; if (isSameMimeType(mimeType, "multipart/alternative")) { return findTextPartInMultipartAlternative(multipart); } else { return findTextPartInMultipart(multipart); } } else if (isSameMimeType(mimeType, "text/plain") || isSameMimeType(mimeType, "text/html")) { return part; } return null; } } | TextPartFinder { @Nullable public Part findFirstTextPart(@NonNull Part part) { String mimeType = part.getMimeType(); Body body = part.getBody(); if (body instanceof Multipart) { Multipart multipart = (Multipart) body; if (isSameMimeType(mimeType, "multipart/alternative")) { return findTextPartInMultipartAlternative(multipart); } else { return findTextPartInMultipart(multipart); } } else if (isSameMimeType(mimeType, "text/plain") || isSameMimeType(mimeType, "text/html")) { return part; } return null; } } | TextPartFinder { @Nullable public Part findFirstTextPart(@NonNull Part part) { String mimeType = part.getMimeType(); Body body = part.getBody(); if (body instanceof Multipart) { Multipart multipart = (Multipart) body; if (isSameMimeType(mimeType, "multipart/alternative")) { return findTextPartInMultipartAlternative(multipart); } else { return findTextPartInMultipart(multipart); } } else if (isSameMimeType(mimeType, "text/plain") || isSameMimeType(mimeType, "text/html")) { return part; } return null; } @Nullable Part findFirstTextPart(@NonNull Part part); } | TextPartFinder { @Nullable public Part findFirstTextPart(@NonNull Part part) { String mimeType = part.getMimeType(); Body body = part.getBody(); if (body instanceof Multipart) { Multipart multipart = (Multipart) body; if (isSameMimeType(mimeType, "multipart/alternative")) { return findTextPartInMultipartAlternative(multipart); } else { return findTextPartInMultipart(multipart); } } else if (isSameMimeType(mimeType, "text/plain") || isSameMimeType(mimeType, "text/html")) { return part; } return null; } @Nullable Part findFirstTextPart(@NonNull Part part); } |
@Test public void findFirstTextPart_withMultipartMixedNotContainingTextPart() throws Exception { Part part = createMultipart("multipart/mixed", createPart("image/jpeg"), createPart("image/gif")); Part result = textPartFinder.findFirstTextPart(part); assertNull(result); } | @Nullable public Part findFirstTextPart(@NonNull Part part) { String mimeType = part.getMimeType(); Body body = part.getBody(); if (body instanceof Multipart) { Multipart multipart = (Multipart) body; if (isSameMimeType(mimeType, "multipart/alternative")) { return findTextPartInMultipartAlternative(multipart); } else { return findTextPartInMultipart(multipart); } } else if (isSameMimeType(mimeType, "text/plain") || isSameMimeType(mimeType, "text/html")) { return part; } return null; } | TextPartFinder { @Nullable public Part findFirstTextPart(@NonNull Part part) { String mimeType = part.getMimeType(); Body body = part.getBody(); if (body instanceof Multipart) { Multipart multipart = (Multipart) body; if (isSameMimeType(mimeType, "multipart/alternative")) { return findTextPartInMultipartAlternative(multipart); } else { return findTextPartInMultipart(multipart); } } else if (isSameMimeType(mimeType, "text/plain") || isSameMimeType(mimeType, "text/html")) { return part; } return null; } } | TextPartFinder { @Nullable public Part findFirstTextPart(@NonNull Part part) { String mimeType = part.getMimeType(); Body body = part.getBody(); if (body instanceof Multipart) { Multipart multipart = (Multipart) body; if (isSameMimeType(mimeType, "multipart/alternative")) { return findTextPartInMultipartAlternative(multipart); } else { return findTextPartInMultipart(multipart); } } else if (isSameMimeType(mimeType, "text/plain") || isSameMimeType(mimeType, "text/html")) { return part; } return null; } } | TextPartFinder { @Nullable public Part findFirstTextPart(@NonNull Part part) { String mimeType = part.getMimeType(); Body body = part.getBody(); if (body instanceof Multipart) { Multipart multipart = (Multipart) body; if (isSameMimeType(mimeType, "multipart/alternative")) { return findTextPartInMultipartAlternative(multipart); } else { return findTextPartInMultipart(multipart); } } else if (isSameMimeType(mimeType, "text/plain") || isSameMimeType(mimeType, "text/html")) { return part; } return null; } @Nullable Part findFirstTextPart(@NonNull Part part); } | TextPartFinder { @Nullable public Part findFirstTextPart(@NonNull Part part) { String mimeType = part.getMimeType(); Body body = part.getBody(); if (body instanceof Multipart) { Multipart multipart = (Multipart) body; if (isSameMimeType(mimeType, "multipart/alternative")) { return findTextPartInMultipartAlternative(multipart); } else { return findTextPartInMultipart(multipart); } } else if (isSameMimeType(mimeType, "text/plain") || isSameMimeType(mimeType, "text/html")) { return part; } return null; } @Nullable Part findFirstTextPart(@NonNull Part part); } |
@Test public void findFirstTextPart_withMultipartMixedContainingMultipartAlternative() throws Exception { BodyPart expected = createTextPart("text/plain"); Part part = createMultipart("multipart/mixed", createPart("image/jpeg"), createMultipart("multipart/alternative", expected, createTextPart("text/html")), createTextPart("text/plain")); Part result = textPartFinder.findFirstTextPart(part); assertEquals(expected, result); } | @Nullable public Part findFirstTextPart(@NonNull Part part) { String mimeType = part.getMimeType(); Body body = part.getBody(); if (body instanceof Multipart) { Multipart multipart = (Multipart) body; if (isSameMimeType(mimeType, "multipart/alternative")) { return findTextPartInMultipartAlternative(multipart); } else { return findTextPartInMultipart(multipart); } } else if (isSameMimeType(mimeType, "text/plain") || isSameMimeType(mimeType, "text/html")) { return part; } return null; } | TextPartFinder { @Nullable public Part findFirstTextPart(@NonNull Part part) { String mimeType = part.getMimeType(); Body body = part.getBody(); if (body instanceof Multipart) { Multipart multipart = (Multipart) body; if (isSameMimeType(mimeType, "multipart/alternative")) { return findTextPartInMultipartAlternative(multipart); } else { return findTextPartInMultipart(multipart); } } else if (isSameMimeType(mimeType, "text/plain") || isSameMimeType(mimeType, "text/html")) { return part; } return null; } } | TextPartFinder { @Nullable public Part findFirstTextPart(@NonNull Part part) { String mimeType = part.getMimeType(); Body body = part.getBody(); if (body instanceof Multipart) { Multipart multipart = (Multipart) body; if (isSameMimeType(mimeType, "multipart/alternative")) { return findTextPartInMultipartAlternative(multipart); } else { return findTextPartInMultipart(multipart); } } else if (isSameMimeType(mimeType, "text/plain") || isSameMimeType(mimeType, "text/html")) { return part; } return null; } } | TextPartFinder { @Nullable public Part findFirstTextPart(@NonNull Part part) { String mimeType = part.getMimeType(); Body body = part.getBody(); if (body instanceof Multipart) { Multipart multipart = (Multipart) body; if (isSameMimeType(mimeType, "multipart/alternative")) { return findTextPartInMultipartAlternative(multipart); } else { return findTextPartInMultipart(multipart); } } else if (isSameMimeType(mimeType, "text/plain") || isSameMimeType(mimeType, "text/html")) { return part; } return null; } @Nullable Part findFirstTextPart(@NonNull Part part); } | TextPartFinder { @Nullable public Part findFirstTextPart(@NonNull Part part) { String mimeType = part.getMimeType(); Body body = part.getBody(); if (body instanceof Multipart) { Multipart multipart = (Multipart) body; if (isSameMimeType(mimeType, "multipart/alternative")) { return findTextPartInMultipartAlternative(multipart); } else { return findTextPartInMultipart(multipart); } } else if (isSameMimeType(mimeType, "text/plain") || isSameMimeType(mimeType, "text/html")) { return part; } return null; } @Nullable Part findFirstTextPart(@NonNull Part part); } |
@Test public void findFirstTextPart_withMultipartMixedContainingMultipartAlternativeWithTextPlainPartLast() throws Exception { BodyPart expected = createTextPart("text/plain"); Part part = createMultipart("multipart/mixed", createMultipart("multipart/alternative", createTextPart("text/html"), expected)); Part result = textPartFinder.findFirstTextPart(part); assertEquals(expected, result); } | @Nullable public Part findFirstTextPart(@NonNull Part part) { String mimeType = part.getMimeType(); Body body = part.getBody(); if (body instanceof Multipart) { Multipart multipart = (Multipart) body; if (isSameMimeType(mimeType, "multipart/alternative")) { return findTextPartInMultipartAlternative(multipart); } else { return findTextPartInMultipart(multipart); } } else if (isSameMimeType(mimeType, "text/plain") || isSameMimeType(mimeType, "text/html")) { return part; } return null; } | TextPartFinder { @Nullable public Part findFirstTextPart(@NonNull Part part) { String mimeType = part.getMimeType(); Body body = part.getBody(); if (body instanceof Multipart) { Multipart multipart = (Multipart) body; if (isSameMimeType(mimeType, "multipart/alternative")) { return findTextPartInMultipartAlternative(multipart); } else { return findTextPartInMultipart(multipart); } } else if (isSameMimeType(mimeType, "text/plain") || isSameMimeType(mimeType, "text/html")) { return part; } return null; } } | TextPartFinder { @Nullable public Part findFirstTextPart(@NonNull Part part) { String mimeType = part.getMimeType(); Body body = part.getBody(); if (body instanceof Multipart) { Multipart multipart = (Multipart) body; if (isSameMimeType(mimeType, "multipart/alternative")) { return findTextPartInMultipartAlternative(multipart); } else { return findTextPartInMultipart(multipart); } } else if (isSameMimeType(mimeType, "text/plain") || isSameMimeType(mimeType, "text/html")) { return part; } return null; } } | TextPartFinder { @Nullable public Part findFirstTextPart(@NonNull Part part) { String mimeType = part.getMimeType(); Body body = part.getBody(); if (body instanceof Multipart) { Multipart multipart = (Multipart) body; if (isSameMimeType(mimeType, "multipart/alternative")) { return findTextPartInMultipartAlternative(multipart); } else { return findTextPartInMultipart(multipart); } } else if (isSameMimeType(mimeType, "text/plain") || isSameMimeType(mimeType, "text/html")) { return part; } return null; } @Nullable Part findFirstTextPart(@NonNull Part part); } | TextPartFinder { @Nullable public Part findFirstTextPart(@NonNull Part part) { String mimeType = part.getMimeType(); Body body = part.getBody(); if (body instanceof Multipart) { Multipart multipart = (Multipart) body; if (isSameMimeType(mimeType, "multipart/alternative")) { return findTextPartInMultipartAlternative(multipart); } else { return findTextPartInMultipart(multipart); } } else if (isSameMimeType(mimeType, "text/plain") || isSameMimeType(mimeType, "text/html")) { return part; } return null; } @Nullable Part findFirstTextPart(@NonNull Part part); } |
@Test public void findFirstTextPart_withMultipartAlternativeContainingEmptyTextPlainPart() throws Exception { BodyPart expected = createEmptyPart("text/plain"); Part part = createMultipart("multipart/alternative", expected, createTextPart("text/html")); Part result = textPartFinder.findFirstTextPart(part); assertEquals(expected, result); } | @Nullable public Part findFirstTextPart(@NonNull Part part) { String mimeType = part.getMimeType(); Body body = part.getBody(); if (body instanceof Multipart) { Multipart multipart = (Multipart) body; if (isSameMimeType(mimeType, "multipart/alternative")) { return findTextPartInMultipartAlternative(multipart); } else { return findTextPartInMultipart(multipart); } } else if (isSameMimeType(mimeType, "text/plain") || isSameMimeType(mimeType, "text/html")) { return part; } return null; } | TextPartFinder { @Nullable public Part findFirstTextPart(@NonNull Part part) { String mimeType = part.getMimeType(); Body body = part.getBody(); if (body instanceof Multipart) { Multipart multipart = (Multipart) body; if (isSameMimeType(mimeType, "multipart/alternative")) { return findTextPartInMultipartAlternative(multipart); } else { return findTextPartInMultipart(multipart); } } else if (isSameMimeType(mimeType, "text/plain") || isSameMimeType(mimeType, "text/html")) { return part; } return null; } } | TextPartFinder { @Nullable public Part findFirstTextPart(@NonNull Part part) { String mimeType = part.getMimeType(); Body body = part.getBody(); if (body instanceof Multipart) { Multipart multipart = (Multipart) body; if (isSameMimeType(mimeType, "multipart/alternative")) { return findTextPartInMultipartAlternative(multipart); } else { return findTextPartInMultipart(multipart); } } else if (isSameMimeType(mimeType, "text/plain") || isSameMimeType(mimeType, "text/html")) { return part; } return null; } } | TextPartFinder { @Nullable public Part findFirstTextPart(@NonNull Part part) { String mimeType = part.getMimeType(); Body body = part.getBody(); if (body instanceof Multipart) { Multipart multipart = (Multipart) body; if (isSameMimeType(mimeType, "multipart/alternative")) { return findTextPartInMultipartAlternative(multipart); } else { return findTextPartInMultipart(multipart); } } else if (isSameMimeType(mimeType, "text/plain") || isSameMimeType(mimeType, "text/html")) { return part; } return null; } @Nullable Part findFirstTextPart(@NonNull Part part); } | TextPartFinder { @Nullable public Part findFirstTextPart(@NonNull Part part) { String mimeType = part.getMimeType(); Body body = part.getBody(); if (body instanceof Multipart) { Multipart multipart = (Multipart) body; if (isSameMimeType(mimeType, "multipart/alternative")) { return findTextPartInMultipartAlternative(multipart); } else { return findTextPartInMultipart(multipart); } } else if (isSameMimeType(mimeType, "text/plain") || isSameMimeType(mimeType, "text/html")) { return part; } return null; } @Nullable Part findFirstTextPart(@NonNull Part part); } |
@Test public void findFirstTextPart_withMultipartMixedContainingEmptyTextHtmlPart() throws Exception { BodyPart expected = createEmptyPart("text/html"); Part part = createMultipart("multipart/mixed", expected, createTextPart("text/plain")); Part result = textPartFinder.findFirstTextPart(part); assertEquals(expected, result); } | @Nullable public Part findFirstTextPart(@NonNull Part part) { String mimeType = part.getMimeType(); Body body = part.getBody(); if (body instanceof Multipart) { Multipart multipart = (Multipart) body; if (isSameMimeType(mimeType, "multipart/alternative")) { return findTextPartInMultipartAlternative(multipart); } else { return findTextPartInMultipart(multipart); } } else if (isSameMimeType(mimeType, "text/plain") || isSameMimeType(mimeType, "text/html")) { return part; } return null; } | TextPartFinder { @Nullable public Part findFirstTextPart(@NonNull Part part) { String mimeType = part.getMimeType(); Body body = part.getBody(); if (body instanceof Multipart) { Multipart multipart = (Multipart) body; if (isSameMimeType(mimeType, "multipart/alternative")) { return findTextPartInMultipartAlternative(multipart); } else { return findTextPartInMultipart(multipart); } } else if (isSameMimeType(mimeType, "text/plain") || isSameMimeType(mimeType, "text/html")) { return part; } return null; } } | TextPartFinder { @Nullable public Part findFirstTextPart(@NonNull Part part) { String mimeType = part.getMimeType(); Body body = part.getBody(); if (body instanceof Multipart) { Multipart multipart = (Multipart) body; if (isSameMimeType(mimeType, "multipart/alternative")) { return findTextPartInMultipartAlternative(multipart); } else { return findTextPartInMultipart(multipart); } } else if (isSameMimeType(mimeType, "text/plain") || isSameMimeType(mimeType, "text/html")) { return part; } return null; } } | TextPartFinder { @Nullable public Part findFirstTextPart(@NonNull Part part) { String mimeType = part.getMimeType(); Body body = part.getBody(); if (body instanceof Multipart) { Multipart multipart = (Multipart) body; if (isSameMimeType(mimeType, "multipart/alternative")) { return findTextPartInMultipartAlternative(multipart); } else { return findTextPartInMultipart(multipart); } } else if (isSameMimeType(mimeType, "text/plain") || isSameMimeType(mimeType, "text/html")) { return part; } return null; } @Nullable Part findFirstTextPart(@NonNull Part part); } | TextPartFinder { @Nullable public Part findFirstTextPart(@NonNull Part part) { String mimeType = part.getMimeType(); Body body = part.getBody(); if (body instanceof Multipart) { Multipart multipart = (Multipart) body; if (isSameMimeType(mimeType, "multipart/alternative")) { return findTextPartInMultipartAlternative(multipart); } else { return findTextPartInMultipart(multipart); } } else if (isSameMimeType(mimeType, "text/plain") || isSameMimeType(mimeType, "text/html")) { return part; } return null; } @Nullable Part findFirstTextPart(@NonNull Part part); } |
@Test public void findEncrypted__withMultipartMixedSubEncrypted__shouldReturnRoot() throws Exception { Message message = messageFromBody( multipart("mixed", multipart("encrypted", bodypart("application/pgp-encrypted"), bodypart("application/octet-stream") ) ) ); List<Part> encryptedParts = MessageDecryptVerifier.findEncryptedParts(message); assertEquals(1, encryptedParts.size()); assertSame(getPart(message, 0), encryptedParts.get(0)); } | public static List<Part> findEncryptedParts(Part startPart) { List<Part> encryptedParts = new ArrayList<>(); Stack<Part> partsToCheck = new Stack<>(); partsToCheck.push(startPart); while (!partsToCheck.isEmpty()) { Part part = partsToCheck.pop(); Body body = part.getBody(); if (isPartMultipartEncrypted(part)) { encryptedParts.add(part); continue; } 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; } | MessageDecryptVerifier { public static List<Part> findEncryptedParts(Part startPart) { List<Part> encryptedParts = new ArrayList<>(); Stack<Part> partsToCheck = new Stack<>(); partsToCheck.push(startPart); while (!partsToCheck.isEmpty()) { Part part = partsToCheck.pop(); Body body = part.getBody(); if (isPartMultipartEncrypted(part)) { encryptedParts.add(part); continue; } 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; } } | MessageDecryptVerifier { public static List<Part> findEncryptedParts(Part startPart) { List<Part> encryptedParts = new ArrayList<>(); Stack<Part> partsToCheck = new Stack<>(); partsToCheck.push(startPart); while (!partsToCheck.isEmpty()) { Part part = partsToCheck.pop(); Body body = part.getBody(); if (isPartMultipartEncrypted(part)) { encryptedParts.add(part); continue; } 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; } } | MessageDecryptVerifier { public static List<Part> findEncryptedParts(Part startPart) { List<Part> encryptedParts = new ArrayList<>(); Stack<Part> partsToCheck = new Stack<>(); partsToCheck.push(startPart); while (!partsToCheck.isEmpty()) { Part part = partsToCheck.pop(); Body body = part.getBody(); if (isPartMultipartEncrypted(part)) { encryptedParts.add(part); continue; } 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; } static Part findPrimaryEncryptedOrSignedPart(Part part, List<Part> outputExtraParts); static List<Part> findEncryptedParts(Part startPart); static List<Part> findSignedParts(Part startPart, MessageCryptoAnnotations messageCryptoAnnotations); static List<Part> findPgpInlineParts(Part startPart); static byte[] getSignatureData(Part part); static boolean isPgpMimeEncryptedOrSignedPart(Part part); static boolean isSMimeEncryptedOrSignedPart(Part part); static boolean isPartPgpInlineEncrypted(@Nullable Part part); } | MessageDecryptVerifier { public static List<Part> findEncryptedParts(Part startPart) { List<Part> encryptedParts = new ArrayList<>(); Stack<Part> partsToCheck = new Stack<>(); partsToCheck.push(startPart); while (!partsToCheck.isEmpty()) { Part part = partsToCheck.pop(); Body body = part.getBody(); if (isPartMultipartEncrypted(part)) { encryptedParts.add(part); continue; } 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; } static Part findPrimaryEncryptedOrSignedPart(Part part, List<Part> outputExtraParts); static List<Part> findEncryptedParts(Part startPart); static List<Part> findSignedParts(Part startPart, MessageCryptoAnnotations messageCryptoAnnotations); static List<Part> findPgpInlineParts(Part startPart); static byte[] getSignatureData(Part part); static boolean isPgpMimeEncryptedOrSignedPart(Part part); static boolean isSMimeEncryptedOrSignedPart(Part part); static boolean isPartPgpInlineEncrypted(@Nullable Part part); } |
@Test(expected = IllegalArgumentException.class) public void extractInfo__withGenericPart_shouldThrow() throws Exception { Part part = mock(Part.class); attachmentInfoExtractor.extractAttachmentInfo(part); } | @WorkerThread public AttachmentViewInfo extractAttachmentInfo(Part part) throws MessagingException { Uri uri; long size; boolean isContentAvailable; if (part instanceof LocalPart) { LocalPart localPart = (LocalPart) part; String accountUuid = localPart.getAccountUuid(); long messagePartId = localPart.getPartId(); size = localPart.getSize(); isContentAvailable = part.getBody() != null; uri = AttachmentProvider.getAttachmentUri(accountUuid, messagePartId); } else if (part instanceof LocalMessage) { LocalMessage localMessage = (LocalMessage) part; String accountUuid = localMessage.getAccount().getUuid(); long messagePartId = localMessage.getMessagePartId(); size = localMessage.getSize(); isContentAvailable = part.getBody() != null; uri = AttachmentProvider.getAttachmentUri(accountUuid, messagePartId); } else { Body body = part.getBody(); if (body instanceof DeferredFileBody) { DeferredFileBody decryptedTempFileBody = (DeferredFileBody) body; size = decryptedTempFileBody.getSize(); uri = getDecryptedFileProviderUri(decryptedTempFileBody, part.getMimeType()); isContentAvailable = true; } else { throw new IllegalArgumentException("Unsupported part type provided"); } } return extractAttachmentInfo(part, uri, size, isContentAvailable); } | AttachmentInfoExtractor { @WorkerThread public AttachmentViewInfo extractAttachmentInfo(Part part) throws MessagingException { Uri uri; long size; boolean isContentAvailable; if (part instanceof LocalPart) { LocalPart localPart = (LocalPart) part; String accountUuid = localPart.getAccountUuid(); long messagePartId = localPart.getPartId(); size = localPart.getSize(); isContentAvailable = part.getBody() != null; uri = AttachmentProvider.getAttachmentUri(accountUuid, messagePartId); } else if (part instanceof LocalMessage) { LocalMessage localMessage = (LocalMessage) part; String accountUuid = localMessage.getAccount().getUuid(); long messagePartId = localMessage.getMessagePartId(); size = localMessage.getSize(); isContentAvailable = part.getBody() != null; uri = AttachmentProvider.getAttachmentUri(accountUuid, messagePartId); } else { Body body = part.getBody(); if (body instanceof DeferredFileBody) { DeferredFileBody decryptedTempFileBody = (DeferredFileBody) body; size = decryptedTempFileBody.getSize(); uri = getDecryptedFileProviderUri(decryptedTempFileBody, part.getMimeType()); isContentAvailable = true; } else { throw new IllegalArgumentException("Unsupported part type provided"); } } return extractAttachmentInfo(part, uri, size, isContentAvailable); } } | AttachmentInfoExtractor { @WorkerThread public AttachmentViewInfo extractAttachmentInfo(Part part) throws MessagingException { Uri uri; long size; boolean isContentAvailable; if (part instanceof LocalPart) { LocalPart localPart = (LocalPart) part; String accountUuid = localPart.getAccountUuid(); long messagePartId = localPart.getPartId(); size = localPart.getSize(); isContentAvailable = part.getBody() != null; uri = AttachmentProvider.getAttachmentUri(accountUuid, messagePartId); } else if (part instanceof LocalMessage) { LocalMessage localMessage = (LocalMessage) part; String accountUuid = localMessage.getAccount().getUuid(); long messagePartId = localMessage.getMessagePartId(); size = localMessage.getSize(); isContentAvailable = part.getBody() != null; uri = AttachmentProvider.getAttachmentUri(accountUuid, messagePartId); } else { Body body = part.getBody(); if (body instanceof DeferredFileBody) { DeferredFileBody decryptedTempFileBody = (DeferredFileBody) body; size = decryptedTempFileBody.getSize(); uri = getDecryptedFileProviderUri(decryptedTempFileBody, part.getMimeType()); isContentAvailable = true; } else { throw new IllegalArgumentException("Unsupported part type provided"); } } return extractAttachmentInfo(part, uri, size, isContentAvailable); } @VisibleForTesting AttachmentInfoExtractor(Context context); } | AttachmentInfoExtractor { @WorkerThread public AttachmentViewInfo extractAttachmentInfo(Part part) throws MessagingException { Uri uri; long size; boolean isContentAvailable; if (part instanceof LocalPart) { LocalPart localPart = (LocalPart) part; String accountUuid = localPart.getAccountUuid(); long messagePartId = localPart.getPartId(); size = localPart.getSize(); isContentAvailable = part.getBody() != null; uri = AttachmentProvider.getAttachmentUri(accountUuid, messagePartId); } else if (part instanceof LocalMessage) { LocalMessage localMessage = (LocalMessage) part; String accountUuid = localMessage.getAccount().getUuid(); long messagePartId = localMessage.getMessagePartId(); size = localMessage.getSize(); isContentAvailable = part.getBody() != null; uri = AttachmentProvider.getAttachmentUri(accountUuid, messagePartId); } else { Body body = part.getBody(); if (body instanceof DeferredFileBody) { DeferredFileBody decryptedTempFileBody = (DeferredFileBody) body; size = decryptedTempFileBody.getSize(); uri = getDecryptedFileProviderUri(decryptedTempFileBody, part.getMimeType()); isContentAvailable = true; } else { throw new IllegalArgumentException("Unsupported part type provided"); } } return extractAttachmentInfo(part, uri, size, isContentAvailable); } @VisibleForTesting AttachmentInfoExtractor(Context context); static AttachmentInfoExtractor getInstance(); @WorkerThread List<AttachmentViewInfo> extractAttachmentInfoForView(List<Part> attachmentParts); @WorkerThread AttachmentViewInfo extractAttachmentInfo(Part part); AttachmentViewInfo extractAttachmentInfoForDatabase(Part part); } | AttachmentInfoExtractor { @WorkerThread public AttachmentViewInfo extractAttachmentInfo(Part part) throws MessagingException { Uri uri; long size; boolean isContentAvailable; if (part instanceof LocalPart) { LocalPart localPart = (LocalPart) part; String accountUuid = localPart.getAccountUuid(); long messagePartId = localPart.getPartId(); size = localPart.getSize(); isContentAvailable = part.getBody() != null; uri = AttachmentProvider.getAttachmentUri(accountUuid, messagePartId); } else if (part instanceof LocalMessage) { LocalMessage localMessage = (LocalMessage) part; String accountUuid = localMessage.getAccount().getUuid(); long messagePartId = localMessage.getMessagePartId(); size = localMessage.getSize(); isContentAvailable = part.getBody() != null; uri = AttachmentProvider.getAttachmentUri(accountUuid, messagePartId); } else { Body body = part.getBody(); if (body instanceof DeferredFileBody) { DeferredFileBody decryptedTempFileBody = (DeferredFileBody) body; size = decryptedTempFileBody.getSize(); uri = getDecryptedFileProviderUri(decryptedTempFileBody, part.getMimeType()); isContentAvailable = true; } else { throw new IllegalArgumentException("Unsupported part type provided"); } } return extractAttachmentInfo(part, uri, size, isContentAvailable); } @VisibleForTesting AttachmentInfoExtractor(Context context); static AttachmentInfoExtractor getInstance(); @WorkerThread List<AttachmentViewInfo> extractAttachmentInfoForView(List<Part> attachmentParts); @WorkerThread AttachmentViewInfo extractAttachmentInfo(Part part); AttachmentViewInfo extractAttachmentInfoForDatabase(Part part); } |
@Test public void extractInfo__fromLocalBodyPart__shouldReturnProvidedValues() throws Exception { LocalBodyPart part = new LocalBodyPart(TEST_ACCOUNT_UUID, null, TEST_ID, TEST_SIZE); part.setHeader(MimeHeader.HEADER_CONTENT_TYPE, TEST_MIME_TYPE); AttachmentViewInfo attachmentViewInfo = attachmentInfoExtractor.extractAttachmentInfo(part); assertEquals(AttachmentProvider.getAttachmentUri(TEST_ACCOUNT_UUID, TEST_ID), attachmentViewInfo.internalUri); assertEquals(TEST_SIZE, attachmentViewInfo.size); assertEquals(TEST_MIME_TYPE, attachmentViewInfo.mimeType); } | @WorkerThread public AttachmentViewInfo extractAttachmentInfo(Part part) throws MessagingException { Uri uri; long size; boolean isContentAvailable; if (part instanceof LocalPart) { LocalPart localPart = (LocalPart) part; String accountUuid = localPart.getAccountUuid(); long messagePartId = localPart.getPartId(); size = localPart.getSize(); isContentAvailable = part.getBody() != null; uri = AttachmentProvider.getAttachmentUri(accountUuid, messagePartId); } else if (part instanceof LocalMessage) { LocalMessage localMessage = (LocalMessage) part; String accountUuid = localMessage.getAccount().getUuid(); long messagePartId = localMessage.getMessagePartId(); size = localMessage.getSize(); isContentAvailable = part.getBody() != null; uri = AttachmentProvider.getAttachmentUri(accountUuid, messagePartId); } else { Body body = part.getBody(); if (body instanceof DeferredFileBody) { DeferredFileBody decryptedTempFileBody = (DeferredFileBody) body; size = decryptedTempFileBody.getSize(); uri = getDecryptedFileProviderUri(decryptedTempFileBody, part.getMimeType()); isContentAvailable = true; } else { throw new IllegalArgumentException("Unsupported part type provided"); } } return extractAttachmentInfo(part, uri, size, isContentAvailable); } | AttachmentInfoExtractor { @WorkerThread public AttachmentViewInfo extractAttachmentInfo(Part part) throws MessagingException { Uri uri; long size; boolean isContentAvailable; if (part instanceof LocalPart) { LocalPart localPart = (LocalPart) part; String accountUuid = localPart.getAccountUuid(); long messagePartId = localPart.getPartId(); size = localPart.getSize(); isContentAvailable = part.getBody() != null; uri = AttachmentProvider.getAttachmentUri(accountUuid, messagePartId); } else if (part instanceof LocalMessage) { LocalMessage localMessage = (LocalMessage) part; String accountUuid = localMessage.getAccount().getUuid(); long messagePartId = localMessage.getMessagePartId(); size = localMessage.getSize(); isContentAvailable = part.getBody() != null; uri = AttachmentProvider.getAttachmentUri(accountUuid, messagePartId); } else { Body body = part.getBody(); if (body instanceof DeferredFileBody) { DeferredFileBody decryptedTempFileBody = (DeferredFileBody) body; size = decryptedTempFileBody.getSize(); uri = getDecryptedFileProviderUri(decryptedTempFileBody, part.getMimeType()); isContentAvailable = true; } else { throw new IllegalArgumentException("Unsupported part type provided"); } } return extractAttachmentInfo(part, uri, size, isContentAvailable); } } | AttachmentInfoExtractor { @WorkerThread public AttachmentViewInfo extractAttachmentInfo(Part part) throws MessagingException { Uri uri; long size; boolean isContentAvailable; if (part instanceof LocalPart) { LocalPart localPart = (LocalPart) part; String accountUuid = localPart.getAccountUuid(); long messagePartId = localPart.getPartId(); size = localPart.getSize(); isContentAvailable = part.getBody() != null; uri = AttachmentProvider.getAttachmentUri(accountUuid, messagePartId); } else if (part instanceof LocalMessage) { LocalMessage localMessage = (LocalMessage) part; String accountUuid = localMessage.getAccount().getUuid(); long messagePartId = localMessage.getMessagePartId(); size = localMessage.getSize(); isContentAvailable = part.getBody() != null; uri = AttachmentProvider.getAttachmentUri(accountUuid, messagePartId); } else { Body body = part.getBody(); if (body instanceof DeferredFileBody) { DeferredFileBody decryptedTempFileBody = (DeferredFileBody) body; size = decryptedTempFileBody.getSize(); uri = getDecryptedFileProviderUri(decryptedTempFileBody, part.getMimeType()); isContentAvailable = true; } else { throw new IllegalArgumentException("Unsupported part type provided"); } } return extractAttachmentInfo(part, uri, size, isContentAvailable); } @VisibleForTesting AttachmentInfoExtractor(Context context); } | AttachmentInfoExtractor { @WorkerThread public AttachmentViewInfo extractAttachmentInfo(Part part) throws MessagingException { Uri uri; long size; boolean isContentAvailable; if (part instanceof LocalPart) { LocalPart localPart = (LocalPart) part; String accountUuid = localPart.getAccountUuid(); long messagePartId = localPart.getPartId(); size = localPart.getSize(); isContentAvailable = part.getBody() != null; uri = AttachmentProvider.getAttachmentUri(accountUuid, messagePartId); } else if (part instanceof LocalMessage) { LocalMessage localMessage = (LocalMessage) part; String accountUuid = localMessage.getAccount().getUuid(); long messagePartId = localMessage.getMessagePartId(); size = localMessage.getSize(); isContentAvailable = part.getBody() != null; uri = AttachmentProvider.getAttachmentUri(accountUuid, messagePartId); } else { Body body = part.getBody(); if (body instanceof DeferredFileBody) { DeferredFileBody decryptedTempFileBody = (DeferredFileBody) body; size = decryptedTempFileBody.getSize(); uri = getDecryptedFileProviderUri(decryptedTempFileBody, part.getMimeType()); isContentAvailable = true; } else { throw new IllegalArgumentException("Unsupported part type provided"); } } return extractAttachmentInfo(part, uri, size, isContentAvailable); } @VisibleForTesting AttachmentInfoExtractor(Context context); static AttachmentInfoExtractor getInstance(); @WorkerThread List<AttachmentViewInfo> extractAttachmentInfoForView(List<Part> attachmentParts); @WorkerThread AttachmentViewInfo extractAttachmentInfo(Part part); AttachmentViewInfo extractAttachmentInfoForDatabase(Part part); } | AttachmentInfoExtractor { @WorkerThread public AttachmentViewInfo extractAttachmentInfo(Part part) throws MessagingException { Uri uri; long size; boolean isContentAvailable; if (part instanceof LocalPart) { LocalPart localPart = (LocalPart) part; String accountUuid = localPart.getAccountUuid(); long messagePartId = localPart.getPartId(); size = localPart.getSize(); isContentAvailable = part.getBody() != null; uri = AttachmentProvider.getAttachmentUri(accountUuid, messagePartId); } else if (part instanceof LocalMessage) { LocalMessage localMessage = (LocalMessage) part; String accountUuid = localMessage.getAccount().getUuid(); long messagePartId = localMessage.getMessagePartId(); size = localMessage.getSize(); isContentAvailable = part.getBody() != null; uri = AttachmentProvider.getAttachmentUri(accountUuid, messagePartId); } else { Body body = part.getBody(); if (body instanceof DeferredFileBody) { DeferredFileBody decryptedTempFileBody = (DeferredFileBody) body; size = decryptedTempFileBody.getSize(); uri = getDecryptedFileProviderUri(decryptedTempFileBody, part.getMimeType()); isContentAvailable = true; } else { throw new IllegalArgumentException("Unsupported part type provided"); } } return extractAttachmentInfo(part, uri, size, isContentAvailable); } @VisibleForTesting AttachmentInfoExtractor(Context context); static AttachmentInfoExtractor getInstance(); @WorkerThread List<AttachmentViewInfo> extractAttachmentInfoForView(List<Part> attachmentParts); @WorkerThread AttachmentViewInfo extractAttachmentInfo(Part part); AttachmentViewInfo extractAttachmentInfoForDatabase(Part part); } |
@Test public void extractInfoForDb__withNoHeaders__shouldReturnEmptyValues() throws Exception { MimeBodyPart part = new MimeBodyPart(); AttachmentViewInfo attachmentViewInfo = attachmentInfoExtractor.extractAttachmentInfoForDatabase(part); assertEquals(Uri.EMPTY, attachmentViewInfo.internalUri); assertEquals(AttachmentViewInfo.UNKNOWN_SIZE, attachmentViewInfo.size); assertEquals("noname.txt", attachmentViewInfo.displayName); assertEquals("text/plain", attachmentViewInfo.mimeType); assertFalse(attachmentViewInfo.inlineAttachment); } | public AttachmentViewInfo extractAttachmentInfoForDatabase(Part part) throws MessagingException { boolean isContentAvailable = part.getBody() != null; return extractAttachmentInfo(part, Uri.EMPTY, AttachmentViewInfo.UNKNOWN_SIZE, isContentAvailable); } | AttachmentInfoExtractor { public AttachmentViewInfo extractAttachmentInfoForDatabase(Part part) throws MessagingException { boolean isContentAvailable = part.getBody() != null; return extractAttachmentInfo(part, Uri.EMPTY, AttachmentViewInfo.UNKNOWN_SIZE, isContentAvailable); } } | AttachmentInfoExtractor { public AttachmentViewInfo extractAttachmentInfoForDatabase(Part part) throws MessagingException { boolean isContentAvailable = part.getBody() != null; return extractAttachmentInfo(part, Uri.EMPTY, AttachmentViewInfo.UNKNOWN_SIZE, isContentAvailable); } @VisibleForTesting AttachmentInfoExtractor(Context context); } | AttachmentInfoExtractor { public AttachmentViewInfo extractAttachmentInfoForDatabase(Part part) throws MessagingException { boolean isContentAvailable = part.getBody() != null; return extractAttachmentInfo(part, Uri.EMPTY, AttachmentViewInfo.UNKNOWN_SIZE, isContentAvailable); } @VisibleForTesting AttachmentInfoExtractor(Context context); static AttachmentInfoExtractor getInstance(); @WorkerThread List<AttachmentViewInfo> extractAttachmentInfoForView(List<Part> attachmentParts); @WorkerThread AttachmentViewInfo extractAttachmentInfo(Part part); AttachmentViewInfo extractAttachmentInfoForDatabase(Part part); } | AttachmentInfoExtractor { public AttachmentViewInfo extractAttachmentInfoForDatabase(Part part) throws MessagingException { boolean isContentAvailable = part.getBody() != null; return extractAttachmentInfo(part, Uri.EMPTY, AttachmentViewInfo.UNKNOWN_SIZE, isContentAvailable); } @VisibleForTesting AttachmentInfoExtractor(Context context); static AttachmentInfoExtractor getInstance(); @WorkerThread List<AttachmentViewInfo> extractAttachmentInfoForView(List<Part> attachmentParts); @WorkerThread AttachmentViewInfo extractAttachmentInfo(Part part); AttachmentViewInfo extractAttachmentInfoForDatabase(Part part); } |
@Test public void extractInfoForDb__withTextMimeType__shouldReturnTxtExtension() throws Exception { MimeBodyPart part = new MimeBodyPart(); part.setHeader(MimeHeader.HEADER_CONTENT_TYPE, "text/plain"); AttachmentViewInfo attachmentViewInfo = attachmentInfoExtractor.extractAttachmentInfoForDatabase(part); assertEquals("noname.txt", attachmentViewInfo.displayName); assertEquals("text/plain", attachmentViewInfo.mimeType); } | public AttachmentViewInfo extractAttachmentInfoForDatabase(Part part) throws MessagingException { boolean isContentAvailable = part.getBody() != null; return extractAttachmentInfo(part, Uri.EMPTY, AttachmentViewInfo.UNKNOWN_SIZE, isContentAvailable); } | AttachmentInfoExtractor { public AttachmentViewInfo extractAttachmentInfoForDatabase(Part part) throws MessagingException { boolean isContentAvailable = part.getBody() != null; return extractAttachmentInfo(part, Uri.EMPTY, AttachmentViewInfo.UNKNOWN_SIZE, isContentAvailable); } } | AttachmentInfoExtractor { public AttachmentViewInfo extractAttachmentInfoForDatabase(Part part) throws MessagingException { boolean isContentAvailable = part.getBody() != null; return extractAttachmentInfo(part, Uri.EMPTY, AttachmentViewInfo.UNKNOWN_SIZE, isContentAvailable); } @VisibleForTesting AttachmentInfoExtractor(Context context); } | AttachmentInfoExtractor { public AttachmentViewInfo extractAttachmentInfoForDatabase(Part part) throws MessagingException { boolean isContentAvailable = part.getBody() != null; return extractAttachmentInfo(part, Uri.EMPTY, AttachmentViewInfo.UNKNOWN_SIZE, isContentAvailable); } @VisibleForTesting AttachmentInfoExtractor(Context context); static AttachmentInfoExtractor getInstance(); @WorkerThread List<AttachmentViewInfo> extractAttachmentInfoForView(List<Part> attachmentParts); @WorkerThread AttachmentViewInfo extractAttachmentInfo(Part part); AttachmentViewInfo extractAttachmentInfoForDatabase(Part part); } | AttachmentInfoExtractor { public AttachmentViewInfo extractAttachmentInfoForDatabase(Part part) throws MessagingException { boolean isContentAvailable = part.getBody() != null; return extractAttachmentInfo(part, Uri.EMPTY, AttachmentViewInfo.UNKNOWN_SIZE, isContentAvailable); } @VisibleForTesting AttachmentInfoExtractor(Context context); static AttachmentInfoExtractor getInstance(); @WorkerThread List<AttachmentViewInfo> extractAttachmentInfoForView(List<Part> attachmentParts); @WorkerThread AttachmentViewInfo extractAttachmentInfo(Part part); AttachmentViewInfo extractAttachmentInfoForDatabase(Part part); } |
@Test public void extractInfoForDb__withContentTypeAndName__shouldReturnNamedAttachment() throws Exception { MimeBodyPart part = new MimeBodyPart(); part.setHeader(MimeHeader.HEADER_CONTENT_TYPE, TEST_MIME_TYPE + "; name=\"filename.ext\""); AttachmentViewInfo attachmentViewInfo = attachmentInfoExtractor.extractAttachmentInfoForDatabase(part); assertEquals(Uri.EMPTY, attachmentViewInfo.internalUri); assertEquals(TEST_MIME_TYPE, attachmentViewInfo.mimeType); assertEquals("filename.ext", attachmentViewInfo.displayName); assertFalse(attachmentViewInfo.inlineAttachment); } | public AttachmentViewInfo extractAttachmentInfoForDatabase(Part part) throws MessagingException { boolean isContentAvailable = part.getBody() != null; return extractAttachmentInfo(part, Uri.EMPTY, AttachmentViewInfo.UNKNOWN_SIZE, isContentAvailable); } | AttachmentInfoExtractor { public AttachmentViewInfo extractAttachmentInfoForDatabase(Part part) throws MessagingException { boolean isContentAvailable = part.getBody() != null; return extractAttachmentInfo(part, Uri.EMPTY, AttachmentViewInfo.UNKNOWN_SIZE, isContentAvailable); } } | AttachmentInfoExtractor { public AttachmentViewInfo extractAttachmentInfoForDatabase(Part part) throws MessagingException { boolean isContentAvailable = part.getBody() != null; return extractAttachmentInfo(part, Uri.EMPTY, AttachmentViewInfo.UNKNOWN_SIZE, isContentAvailable); } @VisibleForTesting AttachmentInfoExtractor(Context context); } | AttachmentInfoExtractor { public AttachmentViewInfo extractAttachmentInfoForDatabase(Part part) throws MessagingException { boolean isContentAvailable = part.getBody() != null; return extractAttachmentInfo(part, Uri.EMPTY, AttachmentViewInfo.UNKNOWN_SIZE, isContentAvailable); } @VisibleForTesting AttachmentInfoExtractor(Context context); static AttachmentInfoExtractor getInstance(); @WorkerThread List<AttachmentViewInfo> extractAttachmentInfoForView(List<Part> attachmentParts); @WorkerThread AttachmentViewInfo extractAttachmentInfo(Part part); AttachmentViewInfo extractAttachmentInfoForDatabase(Part part); } | AttachmentInfoExtractor { public AttachmentViewInfo extractAttachmentInfoForDatabase(Part part) throws MessagingException { boolean isContentAvailable = part.getBody() != null; return extractAttachmentInfo(part, Uri.EMPTY, AttachmentViewInfo.UNKNOWN_SIZE, isContentAvailable); } @VisibleForTesting AttachmentInfoExtractor(Context context); static AttachmentInfoExtractor getInstance(); @WorkerThread List<AttachmentViewInfo> extractAttachmentInfoForView(List<Part> attachmentParts); @WorkerThread AttachmentViewInfo extractAttachmentInfo(Part part); AttachmentViewInfo extractAttachmentInfoForDatabase(Part part); } |
@Test public void extractInfoForDb__withContentTypeAndEncodedWordName__shouldReturnDecodedName() throws Exception { Part part = new MimeBodyPart(); part.addRawHeader(MimeHeader.HEADER_CONTENT_TYPE, MimeHeader.HEADER_CONTENT_TYPE + ": " +TEST_MIME_TYPE + "; name=\"=?ISO-8859-1?Q?Sm=F8rrebr=F8d?=\""); AttachmentViewInfo attachmentViewInfo = attachmentInfoExtractor.extractAttachmentInfoForDatabase(part); assertEquals("Smørrebrød", attachmentViewInfo.displayName); } | public AttachmentViewInfo extractAttachmentInfoForDatabase(Part part) throws MessagingException { boolean isContentAvailable = part.getBody() != null; return extractAttachmentInfo(part, Uri.EMPTY, AttachmentViewInfo.UNKNOWN_SIZE, isContentAvailable); } | AttachmentInfoExtractor { public AttachmentViewInfo extractAttachmentInfoForDatabase(Part part) throws MessagingException { boolean isContentAvailable = part.getBody() != null; return extractAttachmentInfo(part, Uri.EMPTY, AttachmentViewInfo.UNKNOWN_SIZE, isContentAvailable); } } | AttachmentInfoExtractor { public AttachmentViewInfo extractAttachmentInfoForDatabase(Part part) throws MessagingException { boolean isContentAvailable = part.getBody() != null; return extractAttachmentInfo(part, Uri.EMPTY, AttachmentViewInfo.UNKNOWN_SIZE, isContentAvailable); } @VisibleForTesting AttachmentInfoExtractor(Context context); } | AttachmentInfoExtractor { public AttachmentViewInfo extractAttachmentInfoForDatabase(Part part) throws MessagingException { boolean isContentAvailable = part.getBody() != null; return extractAttachmentInfo(part, Uri.EMPTY, AttachmentViewInfo.UNKNOWN_SIZE, isContentAvailable); } @VisibleForTesting AttachmentInfoExtractor(Context context); static AttachmentInfoExtractor getInstance(); @WorkerThread List<AttachmentViewInfo> extractAttachmentInfoForView(List<Part> attachmentParts); @WorkerThread AttachmentViewInfo extractAttachmentInfo(Part part); AttachmentViewInfo extractAttachmentInfoForDatabase(Part part); } | AttachmentInfoExtractor { public AttachmentViewInfo extractAttachmentInfoForDatabase(Part part) throws MessagingException { boolean isContentAvailable = part.getBody() != null; return extractAttachmentInfo(part, Uri.EMPTY, AttachmentViewInfo.UNKNOWN_SIZE, isContentAvailable); } @VisibleForTesting AttachmentInfoExtractor(Context context); static AttachmentInfoExtractor getInstance(); @WorkerThread List<AttachmentViewInfo> extractAttachmentInfoForView(List<Part> attachmentParts); @WorkerThread AttachmentViewInfo extractAttachmentInfo(Part part); AttachmentViewInfo extractAttachmentInfoForDatabase(Part part); } |
@Test public void extractInfoForDb__withDispositionAttach__shouldReturnNamedAttachment() throws Exception { MimeBodyPart part = new MimeBodyPart(); part.setHeader(MimeHeader.HEADER_CONTENT_DISPOSITION, "attachment" + "; filename=\"filename.ext\"; meaningless=\"dummy\""); AttachmentViewInfo attachmentViewInfo = attachmentInfoExtractor.extractAttachmentInfoForDatabase(part); assertEquals(Uri.EMPTY, attachmentViewInfo.internalUri); assertEquals("filename.ext", attachmentViewInfo.displayName); assertFalse(attachmentViewInfo.inlineAttachment); } | public AttachmentViewInfo extractAttachmentInfoForDatabase(Part part) throws MessagingException { boolean isContentAvailable = part.getBody() != null; return extractAttachmentInfo(part, Uri.EMPTY, AttachmentViewInfo.UNKNOWN_SIZE, isContentAvailable); } | AttachmentInfoExtractor { public AttachmentViewInfo extractAttachmentInfoForDatabase(Part part) throws MessagingException { boolean isContentAvailable = part.getBody() != null; return extractAttachmentInfo(part, Uri.EMPTY, AttachmentViewInfo.UNKNOWN_SIZE, isContentAvailable); } } | AttachmentInfoExtractor { public AttachmentViewInfo extractAttachmentInfoForDatabase(Part part) throws MessagingException { boolean isContentAvailable = part.getBody() != null; return extractAttachmentInfo(part, Uri.EMPTY, AttachmentViewInfo.UNKNOWN_SIZE, isContentAvailable); } @VisibleForTesting AttachmentInfoExtractor(Context context); } | AttachmentInfoExtractor { public AttachmentViewInfo extractAttachmentInfoForDatabase(Part part) throws MessagingException { boolean isContentAvailable = part.getBody() != null; return extractAttachmentInfo(part, Uri.EMPTY, AttachmentViewInfo.UNKNOWN_SIZE, isContentAvailable); } @VisibleForTesting AttachmentInfoExtractor(Context context); static AttachmentInfoExtractor getInstance(); @WorkerThread List<AttachmentViewInfo> extractAttachmentInfoForView(List<Part> attachmentParts); @WorkerThread AttachmentViewInfo extractAttachmentInfo(Part part); AttachmentViewInfo extractAttachmentInfoForDatabase(Part part); } | AttachmentInfoExtractor { public AttachmentViewInfo extractAttachmentInfoForDatabase(Part part) throws MessagingException { boolean isContentAvailable = part.getBody() != null; return extractAttachmentInfo(part, Uri.EMPTY, AttachmentViewInfo.UNKNOWN_SIZE, isContentAvailable); } @VisibleForTesting AttachmentInfoExtractor(Context context); static AttachmentInfoExtractor getInstance(); @WorkerThread List<AttachmentViewInfo> extractAttachmentInfoForView(List<Part> attachmentParts); @WorkerThread AttachmentViewInfo extractAttachmentInfo(Part part); AttachmentViewInfo extractAttachmentInfoForDatabase(Part part); } |
@Test public void extractInfoForDb__withDispositionInlineAndContentId__shouldReturnInlineAttachment() throws Exception { Part part = new MimeBodyPart(); part.addRawHeader(MimeHeader.HEADER_CONTENT_ID, MimeHeader.HEADER_CONTENT_ID + ": " + TEST_CONTENT_ID); part.addRawHeader(MimeHeader.HEADER_CONTENT_DISPOSITION, MimeHeader.HEADER_CONTENT_DISPOSITION + ": " + "inline" + ";\n filename=\"filename.ext\";\n meaningless=\"dummy\""); AttachmentViewInfo attachmentViewInfo = attachmentInfoExtractor.extractAttachmentInfoForDatabase(part); assertTrue(attachmentViewInfo.inlineAttachment); } | public AttachmentViewInfo extractAttachmentInfoForDatabase(Part part) throws MessagingException { boolean isContentAvailable = part.getBody() != null; return extractAttachmentInfo(part, Uri.EMPTY, AttachmentViewInfo.UNKNOWN_SIZE, isContentAvailable); } | AttachmentInfoExtractor { public AttachmentViewInfo extractAttachmentInfoForDatabase(Part part) throws MessagingException { boolean isContentAvailable = part.getBody() != null; return extractAttachmentInfo(part, Uri.EMPTY, AttachmentViewInfo.UNKNOWN_SIZE, isContentAvailable); } } | AttachmentInfoExtractor { public AttachmentViewInfo extractAttachmentInfoForDatabase(Part part) throws MessagingException { boolean isContentAvailable = part.getBody() != null; return extractAttachmentInfo(part, Uri.EMPTY, AttachmentViewInfo.UNKNOWN_SIZE, isContentAvailable); } @VisibleForTesting AttachmentInfoExtractor(Context context); } | AttachmentInfoExtractor { public AttachmentViewInfo extractAttachmentInfoForDatabase(Part part) throws MessagingException { boolean isContentAvailable = part.getBody() != null; return extractAttachmentInfo(part, Uri.EMPTY, AttachmentViewInfo.UNKNOWN_SIZE, isContentAvailable); } @VisibleForTesting AttachmentInfoExtractor(Context context); static AttachmentInfoExtractor getInstance(); @WorkerThread List<AttachmentViewInfo> extractAttachmentInfoForView(List<Part> attachmentParts); @WorkerThread AttachmentViewInfo extractAttachmentInfo(Part part); AttachmentViewInfo extractAttachmentInfoForDatabase(Part part); } | AttachmentInfoExtractor { public AttachmentViewInfo extractAttachmentInfoForDatabase(Part part) throws MessagingException { boolean isContentAvailable = part.getBody() != null; return extractAttachmentInfo(part, Uri.EMPTY, AttachmentViewInfo.UNKNOWN_SIZE, isContentAvailable); } @VisibleForTesting AttachmentInfoExtractor(Context context); static AttachmentInfoExtractor getInstance(); @WorkerThread List<AttachmentViewInfo> extractAttachmentInfoForView(List<Part> attachmentParts); @WorkerThread AttachmentViewInfo extractAttachmentInfo(Part part); AttachmentViewInfo extractAttachmentInfoForDatabase(Part part); } |
@Test public void extractInfoForDb__withDispositionSizeParam__shouldReturnThatSize() throws Exception { MimeBodyPart part = new MimeBodyPart(); part.setHeader(MimeHeader.HEADER_CONTENT_DISPOSITION, "attachment" + "; size=\"" + TEST_SIZE + "\""); AttachmentViewInfo attachmentViewInfo = attachmentInfoExtractor.extractAttachmentInfoForDatabase(part); assertEquals(TEST_SIZE, attachmentViewInfo.size); } | public AttachmentViewInfo extractAttachmentInfoForDatabase(Part part) throws MessagingException { boolean isContentAvailable = part.getBody() != null; return extractAttachmentInfo(part, Uri.EMPTY, AttachmentViewInfo.UNKNOWN_SIZE, isContentAvailable); } | AttachmentInfoExtractor { public AttachmentViewInfo extractAttachmentInfoForDatabase(Part part) throws MessagingException { boolean isContentAvailable = part.getBody() != null; return extractAttachmentInfo(part, Uri.EMPTY, AttachmentViewInfo.UNKNOWN_SIZE, isContentAvailable); } } | AttachmentInfoExtractor { public AttachmentViewInfo extractAttachmentInfoForDatabase(Part part) throws MessagingException { boolean isContentAvailable = part.getBody() != null; return extractAttachmentInfo(part, Uri.EMPTY, AttachmentViewInfo.UNKNOWN_SIZE, isContentAvailable); } @VisibleForTesting AttachmentInfoExtractor(Context context); } | AttachmentInfoExtractor { public AttachmentViewInfo extractAttachmentInfoForDatabase(Part part) throws MessagingException { boolean isContentAvailable = part.getBody() != null; return extractAttachmentInfo(part, Uri.EMPTY, AttachmentViewInfo.UNKNOWN_SIZE, isContentAvailable); } @VisibleForTesting AttachmentInfoExtractor(Context context); static AttachmentInfoExtractor getInstance(); @WorkerThread List<AttachmentViewInfo> extractAttachmentInfoForView(List<Part> attachmentParts); @WorkerThread AttachmentViewInfo extractAttachmentInfo(Part part); AttachmentViewInfo extractAttachmentInfoForDatabase(Part part); } | AttachmentInfoExtractor { public AttachmentViewInfo extractAttachmentInfoForDatabase(Part part) throws MessagingException { boolean isContentAvailable = part.getBody() != null; return extractAttachmentInfo(part, Uri.EMPTY, AttachmentViewInfo.UNKNOWN_SIZE, isContentAvailable); } @VisibleForTesting AttachmentInfoExtractor(Context context); static AttachmentInfoExtractor getInstance(); @WorkerThread List<AttachmentViewInfo> extractAttachmentInfoForView(List<Part> attachmentParts); @WorkerThread AttachmentViewInfo extractAttachmentInfo(Part part); AttachmentViewInfo extractAttachmentInfoForDatabase(Part part); } |
@Test public void extractInfoForDb__withDispositionInvalidSizeParam__shouldReturnUnknownSize() throws Exception { MimeBodyPart part = new MimeBodyPart(); part.setHeader(MimeHeader.HEADER_CONTENT_DISPOSITION, "attachment" + "; size=\"notanint\""); AttachmentViewInfo attachmentViewInfo = attachmentInfoExtractor.extractAttachmentInfoForDatabase(part); assertEquals(AttachmentViewInfo.UNKNOWN_SIZE, attachmentViewInfo.size); } | public AttachmentViewInfo extractAttachmentInfoForDatabase(Part part) throws MessagingException { boolean isContentAvailable = part.getBody() != null; return extractAttachmentInfo(part, Uri.EMPTY, AttachmentViewInfo.UNKNOWN_SIZE, isContentAvailable); } | AttachmentInfoExtractor { public AttachmentViewInfo extractAttachmentInfoForDatabase(Part part) throws MessagingException { boolean isContentAvailable = part.getBody() != null; return extractAttachmentInfo(part, Uri.EMPTY, AttachmentViewInfo.UNKNOWN_SIZE, isContentAvailable); } } | AttachmentInfoExtractor { public AttachmentViewInfo extractAttachmentInfoForDatabase(Part part) throws MessagingException { boolean isContentAvailable = part.getBody() != null; return extractAttachmentInfo(part, Uri.EMPTY, AttachmentViewInfo.UNKNOWN_SIZE, isContentAvailable); } @VisibleForTesting AttachmentInfoExtractor(Context context); } | AttachmentInfoExtractor { public AttachmentViewInfo extractAttachmentInfoForDatabase(Part part) throws MessagingException { boolean isContentAvailable = part.getBody() != null; return extractAttachmentInfo(part, Uri.EMPTY, AttachmentViewInfo.UNKNOWN_SIZE, isContentAvailable); } @VisibleForTesting AttachmentInfoExtractor(Context context); static AttachmentInfoExtractor getInstance(); @WorkerThread List<AttachmentViewInfo> extractAttachmentInfoForView(List<Part> attachmentParts); @WorkerThread AttachmentViewInfo extractAttachmentInfo(Part part); AttachmentViewInfo extractAttachmentInfoForDatabase(Part part); } | AttachmentInfoExtractor { public AttachmentViewInfo extractAttachmentInfoForDatabase(Part part) throws MessagingException { boolean isContentAvailable = part.getBody() != null; return extractAttachmentInfo(part, Uri.EMPTY, AttachmentViewInfo.UNKNOWN_SIZE, isContentAvailable); } @VisibleForTesting AttachmentInfoExtractor(Context context); static AttachmentInfoExtractor getInstance(); @WorkerThread List<AttachmentViewInfo> extractAttachmentInfoForView(List<Part> attachmentParts); @WorkerThread AttachmentViewInfo extractAttachmentInfo(Part part); AttachmentViewInfo extractAttachmentInfoForDatabase(Part part); } |
@Test public void findEncrypted__withMultipartMixedSubEncryptedAndEncrypted__shouldReturnBoth() throws Exception { Message message = messageFromBody( multipart("mixed", multipart("encrypted", bodypart("application/pgp-encrypted"), bodypart("application/octet-stream") ), multipart("encrypted", bodypart("application/pgp-encrypted"), bodypart("application/octet-stream") ) ) ); List<Part> encryptedParts = MessageDecryptVerifier.findEncryptedParts(message); assertEquals(2, encryptedParts.size()); assertSame(getPart(message, 0), encryptedParts.get(0)); assertSame(getPart(message, 1), encryptedParts.get(1)); } | public static List<Part> findEncryptedParts(Part startPart) { List<Part> encryptedParts = new ArrayList<>(); Stack<Part> partsToCheck = new Stack<>(); partsToCheck.push(startPart); while (!partsToCheck.isEmpty()) { Part part = partsToCheck.pop(); Body body = part.getBody(); if (isPartMultipartEncrypted(part)) { encryptedParts.add(part); continue; } 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; } | MessageDecryptVerifier { public static List<Part> findEncryptedParts(Part startPart) { List<Part> encryptedParts = new ArrayList<>(); Stack<Part> partsToCheck = new Stack<>(); partsToCheck.push(startPart); while (!partsToCheck.isEmpty()) { Part part = partsToCheck.pop(); Body body = part.getBody(); if (isPartMultipartEncrypted(part)) { encryptedParts.add(part); continue; } 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; } } | MessageDecryptVerifier { public static List<Part> findEncryptedParts(Part startPart) { List<Part> encryptedParts = new ArrayList<>(); Stack<Part> partsToCheck = new Stack<>(); partsToCheck.push(startPart); while (!partsToCheck.isEmpty()) { Part part = partsToCheck.pop(); Body body = part.getBody(); if (isPartMultipartEncrypted(part)) { encryptedParts.add(part); continue; } 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; } } | MessageDecryptVerifier { public static List<Part> findEncryptedParts(Part startPart) { List<Part> encryptedParts = new ArrayList<>(); Stack<Part> partsToCheck = new Stack<>(); partsToCheck.push(startPart); while (!partsToCheck.isEmpty()) { Part part = partsToCheck.pop(); Body body = part.getBody(); if (isPartMultipartEncrypted(part)) { encryptedParts.add(part); continue; } 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; } static Part findPrimaryEncryptedOrSignedPart(Part part, List<Part> outputExtraParts); static List<Part> findEncryptedParts(Part startPart); static List<Part> findSignedParts(Part startPart, MessageCryptoAnnotations messageCryptoAnnotations); static List<Part> findPgpInlineParts(Part startPart); static byte[] getSignatureData(Part part); static boolean isPgpMimeEncryptedOrSignedPart(Part part); static boolean isSMimeEncryptedOrSignedPart(Part part); static boolean isPartPgpInlineEncrypted(@Nullable Part part); } | MessageDecryptVerifier { public static List<Part> findEncryptedParts(Part startPart) { List<Part> encryptedParts = new ArrayList<>(); Stack<Part> partsToCheck = new Stack<>(); partsToCheck.push(startPart); while (!partsToCheck.isEmpty()) { Part part = partsToCheck.pop(); Body body = part.getBody(); if (isPartMultipartEncrypted(part)) { encryptedParts.add(part); continue; } 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; } static Part findPrimaryEncryptedOrSignedPart(Part part, List<Part> outputExtraParts); static List<Part> findEncryptedParts(Part startPart); static List<Part> findSignedParts(Part startPart, MessageCryptoAnnotations messageCryptoAnnotations); static List<Part> findPgpInlineParts(Part startPart); static byte[] getSignatureData(Part part); static boolean isPgpMimeEncryptedOrSignedPart(Part part); static boolean isSMimeEncryptedOrSignedPart(Part part); static boolean isPartPgpInlineEncrypted(@Nullable Part part); } |
@Test public void extractInfoForDb__withNoBody__shouldReturnContentNotAvailable() throws Exception { MimeBodyPart part = new MimeBodyPart(); AttachmentViewInfo attachmentViewInfo = attachmentInfoExtractor.extractAttachmentInfoForDatabase(part); assertFalse(attachmentViewInfo.isContentAvailable()); } | public AttachmentViewInfo extractAttachmentInfoForDatabase(Part part) throws MessagingException { boolean isContentAvailable = part.getBody() != null; return extractAttachmentInfo(part, Uri.EMPTY, AttachmentViewInfo.UNKNOWN_SIZE, isContentAvailable); } | AttachmentInfoExtractor { public AttachmentViewInfo extractAttachmentInfoForDatabase(Part part) throws MessagingException { boolean isContentAvailable = part.getBody() != null; return extractAttachmentInfo(part, Uri.EMPTY, AttachmentViewInfo.UNKNOWN_SIZE, isContentAvailable); } } | AttachmentInfoExtractor { public AttachmentViewInfo extractAttachmentInfoForDatabase(Part part) throws MessagingException { boolean isContentAvailable = part.getBody() != null; return extractAttachmentInfo(part, Uri.EMPTY, AttachmentViewInfo.UNKNOWN_SIZE, isContentAvailable); } @VisibleForTesting AttachmentInfoExtractor(Context context); } | AttachmentInfoExtractor { public AttachmentViewInfo extractAttachmentInfoForDatabase(Part part) throws MessagingException { boolean isContentAvailable = part.getBody() != null; return extractAttachmentInfo(part, Uri.EMPTY, AttachmentViewInfo.UNKNOWN_SIZE, isContentAvailable); } @VisibleForTesting AttachmentInfoExtractor(Context context); static AttachmentInfoExtractor getInstance(); @WorkerThread List<AttachmentViewInfo> extractAttachmentInfoForView(List<Part> attachmentParts); @WorkerThread AttachmentViewInfo extractAttachmentInfo(Part part); AttachmentViewInfo extractAttachmentInfoForDatabase(Part part); } | AttachmentInfoExtractor { public AttachmentViewInfo extractAttachmentInfoForDatabase(Part part) throws MessagingException { boolean isContentAvailable = part.getBody() != null; return extractAttachmentInfo(part, Uri.EMPTY, AttachmentViewInfo.UNKNOWN_SIZE, isContentAvailable); } @VisibleForTesting AttachmentInfoExtractor(Context context); static AttachmentInfoExtractor getInstance(); @WorkerThread List<AttachmentViewInfo> extractAttachmentInfoForView(List<Part> attachmentParts); @WorkerThread AttachmentViewInfo extractAttachmentInfo(Part part); AttachmentViewInfo extractAttachmentInfoForDatabase(Part part); } |
@Test public void extractInfoForDb__withNoBody__shouldReturnContentAvailable() throws Exception { MimeBodyPart part = new MimeBodyPart(); part.setBody(new TextBody("data")); AttachmentViewInfo attachmentViewInfo = attachmentInfoExtractor.extractAttachmentInfoForDatabase(part); assertTrue(attachmentViewInfo.isContentAvailable()); } | public AttachmentViewInfo extractAttachmentInfoForDatabase(Part part) throws MessagingException { boolean isContentAvailable = part.getBody() != null; return extractAttachmentInfo(part, Uri.EMPTY, AttachmentViewInfo.UNKNOWN_SIZE, isContentAvailable); } | AttachmentInfoExtractor { public AttachmentViewInfo extractAttachmentInfoForDatabase(Part part) throws MessagingException { boolean isContentAvailable = part.getBody() != null; return extractAttachmentInfo(part, Uri.EMPTY, AttachmentViewInfo.UNKNOWN_SIZE, isContentAvailable); } } | AttachmentInfoExtractor { public AttachmentViewInfo extractAttachmentInfoForDatabase(Part part) throws MessagingException { boolean isContentAvailable = part.getBody() != null; return extractAttachmentInfo(part, Uri.EMPTY, AttachmentViewInfo.UNKNOWN_SIZE, isContentAvailable); } @VisibleForTesting AttachmentInfoExtractor(Context context); } | AttachmentInfoExtractor { public AttachmentViewInfo extractAttachmentInfoForDatabase(Part part) throws MessagingException { boolean isContentAvailable = part.getBody() != null; return extractAttachmentInfo(part, Uri.EMPTY, AttachmentViewInfo.UNKNOWN_SIZE, isContentAvailable); } @VisibleForTesting AttachmentInfoExtractor(Context context); static AttachmentInfoExtractor getInstance(); @WorkerThread List<AttachmentViewInfo> extractAttachmentInfoForView(List<Part> attachmentParts); @WorkerThread AttachmentViewInfo extractAttachmentInfo(Part part); AttachmentViewInfo extractAttachmentInfoForDatabase(Part part); } | AttachmentInfoExtractor { public AttachmentViewInfo extractAttachmentInfoForDatabase(Part part) throws MessagingException { boolean isContentAvailable = part.getBody() != null; return extractAttachmentInfo(part, Uri.EMPTY, AttachmentViewInfo.UNKNOWN_SIZE, isContentAvailable); } @VisibleForTesting AttachmentInfoExtractor(Context context); static AttachmentInfoExtractor getInstance(); @WorkerThread List<AttachmentViewInfo> extractAttachmentInfoForView(List<Part> attachmentParts); @WorkerThread AttachmentViewInfo extractAttachmentInfo(Part part); AttachmentViewInfo extractAttachmentInfoForDatabase(Part part); } |
@Test public void extractInfo__withDeferredFileBody() throws Exception { attachmentInfoExtractor = new AttachmentInfoExtractor(context) { @Nullable @Override protected Uri getDecryptedFileProviderUri(DeferredFileBody decryptedTempFileBody, String mimeType) { return TEST_URI; } }; DeferredFileBody body = mock(DeferredFileBody.class); when(body.getSize()).thenReturn(TEST_SIZE); MimeBodyPart part = new MimeBodyPart(); part.setBody(body); part.setHeader(MimeHeader.HEADER_CONTENT_TYPE, TEST_MIME_TYPE); AttachmentViewInfo attachmentViewInfo = attachmentInfoExtractor.extractAttachmentInfo(part); assertEquals(TEST_URI, attachmentViewInfo.internalUri); assertEquals(TEST_SIZE, attachmentViewInfo.size); assertEquals(TEST_MIME_TYPE, attachmentViewInfo.mimeType); assertFalse(attachmentViewInfo.inlineAttachment); assertTrue(attachmentViewInfo.isContentAvailable()); } | @WorkerThread public AttachmentViewInfo extractAttachmentInfo(Part part) throws MessagingException { Uri uri; long size; boolean isContentAvailable; if (part instanceof LocalPart) { LocalPart localPart = (LocalPart) part; String accountUuid = localPart.getAccountUuid(); long messagePartId = localPart.getPartId(); size = localPart.getSize(); isContentAvailable = part.getBody() != null; uri = AttachmentProvider.getAttachmentUri(accountUuid, messagePartId); } else if (part instanceof LocalMessage) { LocalMessage localMessage = (LocalMessage) part; String accountUuid = localMessage.getAccount().getUuid(); long messagePartId = localMessage.getMessagePartId(); size = localMessage.getSize(); isContentAvailable = part.getBody() != null; uri = AttachmentProvider.getAttachmentUri(accountUuid, messagePartId); } else { Body body = part.getBody(); if (body instanceof DeferredFileBody) { DeferredFileBody decryptedTempFileBody = (DeferredFileBody) body; size = decryptedTempFileBody.getSize(); uri = getDecryptedFileProviderUri(decryptedTempFileBody, part.getMimeType()); isContentAvailable = true; } else { throw new IllegalArgumentException("Unsupported part type provided"); } } return extractAttachmentInfo(part, uri, size, isContentAvailable); } | AttachmentInfoExtractor { @WorkerThread public AttachmentViewInfo extractAttachmentInfo(Part part) throws MessagingException { Uri uri; long size; boolean isContentAvailable; if (part instanceof LocalPart) { LocalPart localPart = (LocalPart) part; String accountUuid = localPart.getAccountUuid(); long messagePartId = localPart.getPartId(); size = localPart.getSize(); isContentAvailable = part.getBody() != null; uri = AttachmentProvider.getAttachmentUri(accountUuid, messagePartId); } else if (part instanceof LocalMessage) { LocalMessage localMessage = (LocalMessage) part; String accountUuid = localMessage.getAccount().getUuid(); long messagePartId = localMessage.getMessagePartId(); size = localMessage.getSize(); isContentAvailable = part.getBody() != null; uri = AttachmentProvider.getAttachmentUri(accountUuid, messagePartId); } else { Body body = part.getBody(); if (body instanceof DeferredFileBody) { DeferredFileBody decryptedTempFileBody = (DeferredFileBody) body; size = decryptedTempFileBody.getSize(); uri = getDecryptedFileProviderUri(decryptedTempFileBody, part.getMimeType()); isContentAvailable = true; } else { throw new IllegalArgumentException("Unsupported part type provided"); } } return extractAttachmentInfo(part, uri, size, isContentAvailable); } } | AttachmentInfoExtractor { @WorkerThread public AttachmentViewInfo extractAttachmentInfo(Part part) throws MessagingException { Uri uri; long size; boolean isContentAvailable; if (part instanceof LocalPart) { LocalPart localPart = (LocalPart) part; String accountUuid = localPart.getAccountUuid(); long messagePartId = localPart.getPartId(); size = localPart.getSize(); isContentAvailable = part.getBody() != null; uri = AttachmentProvider.getAttachmentUri(accountUuid, messagePartId); } else if (part instanceof LocalMessage) { LocalMessage localMessage = (LocalMessage) part; String accountUuid = localMessage.getAccount().getUuid(); long messagePartId = localMessage.getMessagePartId(); size = localMessage.getSize(); isContentAvailable = part.getBody() != null; uri = AttachmentProvider.getAttachmentUri(accountUuid, messagePartId); } else { Body body = part.getBody(); if (body instanceof DeferredFileBody) { DeferredFileBody decryptedTempFileBody = (DeferredFileBody) body; size = decryptedTempFileBody.getSize(); uri = getDecryptedFileProviderUri(decryptedTempFileBody, part.getMimeType()); isContentAvailable = true; } else { throw new IllegalArgumentException("Unsupported part type provided"); } } return extractAttachmentInfo(part, uri, size, isContentAvailable); } @VisibleForTesting AttachmentInfoExtractor(Context context); } | AttachmentInfoExtractor { @WorkerThread public AttachmentViewInfo extractAttachmentInfo(Part part) throws MessagingException { Uri uri; long size; boolean isContentAvailable; if (part instanceof LocalPart) { LocalPart localPart = (LocalPart) part; String accountUuid = localPart.getAccountUuid(); long messagePartId = localPart.getPartId(); size = localPart.getSize(); isContentAvailable = part.getBody() != null; uri = AttachmentProvider.getAttachmentUri(accountUuid, messagePartId); } else if (part instanceof LocalMessage) { LocalMessage localMessage = (LocalMessage) part; String accountUuid = localMessage.getAccount().getUuid(); long messagePartId = localMessage.getMessagePartId(); size = localMessage.getSize(); isContentAvailable = part.getBody() != null; uri = AttachmentProvider.getAttachmentUri(accountUuid, messagePartId); } else { Body body = part.getBody(); if (body instanceof DeferredFileBody) { DeferredFileBody decryptedTempFileBody = (DeferredFileBody) body; size = decryptedTempFileBody.getSize(); uri = getDecryptedFileProviderUri(decryptedTempFileBody, part.getMimeType()); isContentAvailable = true; } else { throw new IllegalArgumentException("Unsupported part type provided"); } } return extractAttachmentInfo(part, uri, size, isContentAvailable); } @VisibleForTesting AttachmentInfoExtractor(Context context); static AttachmentInfoExtractor getInstance(); @WorkerThread List<AttachmentViewInfo> extractAttachmentInfoForView(List<Part> attachmentParts); @WorkerThread AttachmentViewInfo extractAttachmentInfo(Part part); AttachmentViewInfo extractAttachmentInfoForDatabase(Part part); } | AttachmentInfoExtractor { @WorkerThread public AttachmentViewInfo extractAttachmentInfo(Part part) throws MessagingException { Uri uri; long size; boolean isContentAvailable; if (part instanceof LocalPart) { LocalPart localPart = (LocalPart) part; String accountUuid = localPart.getAccountUuid(); long messagePartId = localPart.getPartId(); size = localPart.getSize(); isContentAvailable = part.getBody() != null; uri = AttachmentProvider.getAttachmentUri(accountUuid, messagePartId); } else if (part instanceof LocalMessage) { LocalMessage localMessage = (LocalMessage) part; String accountUuid = localMessage.getAccount().getUuid(); long messagePartId = localMessage.getMessagePartId(); size = localMessage.getSize(); isContentAvailable = part.getBody() != null; uri = AttachmentProvider.getAttachmentUri(accountUuid, messagePartId); } else { Body body = part.getBody(); if (body instanceof DeferredFileBody) { DeferredFileBody decryptedTempFileBody = (DeferredFileBody) body; size = decryptedTempFileBody.getSize(); uri = getDecryptedFileProviderUri(decryptedTempFileBody, part.getMimeType()); isContentAvailable = true; } else { throw new IllegalArgumentException("Unsupported part type provided"); } } return extractAttachmentInfo(part, uri, size, isContentAvailable); } @VisibleForTesting AttachmentInfoExtractor(Context context); static AttachmentInfoExtractor getInstance(); @WorkerThread List<AttachmentViewInfo> extractAttachmentInfoForView(List<Part> attachmentParts); @WorkerThread AttachmentViewInfo extractAttachmentInfo(Part part); AttachmentViewInfo extractAttachmentInfoForDatabase(Part part); } |
@Test public void createPreview_withEncryptedMessage() throws Exception { Message message = createDummyMessage(); when(encryptionDetector.isEncrypted(message)).thenReturn(true); PreviewResult result = previewCreator.createPreview(message); assertFalse(result.isPreviewTextAvailable()); assertEquals(PreviewType.ENCRYPTED, result.getPreviewType()); verifyNoMoreInteractions(textPartFinder); verifyNoMoreInteractions(previewTextExtractor); } | public PreviewResult createPreview(@NonNull Message message) { if (encryptionDetector.isEncrypted(message)) { return PreviewResult.encrypted(); } return extractText(message); } | MessagePreviewCreator { public PreviewResult createPreview(@NonNull Message message) { if (encryptionDetector.isEncrypted(message)) { return PreviewResult.encrypted(); } return extractText(message); } } | MessagePreviewCreator { public PreviewResult createPreview(@NonNull Message message) { if (encryptionDetector.isEncrypted(message)) { return PreviewResult.encrypted(); } return extractText(message); } MessagePreviewCreator(TextPartFinder textPartFinder, PreviewTextExtractor previewTextExtractor,
EncryptionDetector encryptionDetector); } | MessagePreviewCreator { public PreviewResult createPreview(@NonNull Message message) { if (encryptionDetector.isEncrypted(message)) { return PreviewResult.encrypted(); } return extractText(message); } MessagePreviewCreator(TextPartFinder textPartFinder, PreviewTextExtractor previewTextExtractor,
EncryptionDetector encryptionDetector); static MessagePreviewCreator newInstance(); PreviewResult createPreview(@NonNull Message message); } | MessagePreviewCreator { public PreviewResult createPreview(@NonNull Message message) { if (encryptionDetector.isEncrypted(message)) { return PreviewResult.encrypted(); } return extractText(message); } MessagePreviewCreator(TextPartFinder textPartFinder, PreviewTextExtractor previewTextExtractor,
EncryptionDetector encryptionDetector); static MessagePreviewCreator newInstance(); PreviewResult createPreview(@NonNull Message message); } |
@Test public void createPreview_withoutTextPart() throws Exception { Message message = createDummyMessage(); when(encryptionDetector.isEncrypted(message)).thenReturn(false); when(textPartFinder.findFirstTextPart(message)).thenReturn(null); PreviewResult result = previewCreator.createPreview(message); assertFalse(result.isPreviewTextAvailable()); assertEquals(PreviewType.NONE, result.getPreviewType()); verifyNoMoreInteractions(previewTextExtractor); } | public PreviewResult createPreview(@NonNull Message message) { if (encryptionDetector.isEncrypted(message)) { return PreviewResult.encrypted(); } return extractText(message); } | MessagePreviewCreator { public PreviewResult createPreview(@NonNull Message message) { if (encryptionDetector.isEncrypted(message)) { return PreviewResult.encrypted(); } return extractText(message); } } | MessagePreviewCreator { public PreviewResult createPreview(@NonNull Message message) { if (encryptionDetector.isEncrypted(message)) { return PreviewResult.encrypted(); } return extractText(message); } MessagePreviewCreator(TextPartFinder textPartFinder, PreviewTextExtractor previewTextExtractor,
EncryptionDetector encryptionDetector); } | MessagePreviewCreator { public PreviewResult createPreview(@NonNull Message message) { if (encryptionDetector.isEncrypted(message)) { return PreviewResult.encrypted(); } return extractText(message); } MessagePreviewCreator(TextPartFinder textPartFinder, PreviewTextExtractor previewTextExtractor,
EncryptionDetector encryptionDetector); static MessagePreviewCreator newInstance(); PreviewResult createPreview(@NonNull Message message); } | MessagePreviewCreator { public PreviewResult createPreview(@NonNull Message message) { if (encryptionDetector.isEncrypted(message)) { return PreviewResult.encrypted(); } return extractText(message); } MessagePreviewCreator(TextPartFinder textPartFinder, PreviewTextExtractor previewTextExtractor,
EncryptionDetector encryptionDetector); static MessagePreviewCreator newInstance(); PreviewResult createPreview(@NonNull Message message); } |
@Test public void createPreview_withEmptyTextPart() throws Exception { Message message = createDummyMessage(); Part textPart = createEmptyPart("text/plain"); when(encryptionDetector.isEncrypted(message)).thenReturn(false); when(textPartFinder.findFirstTextPart(message)).thenReturn(textPart); PreviewResult result = previewCreator.createPreview(message); assertFalse(result.isPreviewTextAvailable()); assertEquals(PreviewType.NONE, result.getPreviewType()); verifyNoMoreInteractions(previewTextExtractor); } | public PreviewResult createPreview(@NonNull Message message) { if (encryptionDetector.isEncrypted(message)) { return PreviewResult.encrypted(); } return extractText(message); } | MessagePreviewCreator { public PreviewResult createPreview(@NonNull Message message) { if (encryptionDetector.isEncrypted(message)) { return PreviewResult.encrypted(); } return extractText(message); } } | MessagePreviewCreator { public PreviewResult createPreview(@NonNull Message message) { if (encryptionDetector.isEncrypted(message)) { return PreviewResult.encrypted(); } return extractText(message); } MessagePreviewCreator(TextPartFinder textPartFinder, PreviewTextExtractor previewTextExtractor,
EncryptionDetector encryptionDetector); } | MessagePreviewCreator { public PreviewResult createPreview(@NonNull Message message) { if (encryptionDetector.isEncrypted(message)) { return PreviewResult.encrypted(); } return extractText(message); } MessagePreviewCreator(TextPartFinder textPartFinder, PreviewTextExtractor previewTextExtractor,
EncryptionDetector encryptionDetector); static MessagePreviewCreator newInstance(); PreviewResult createPreview(@NonNull Message message); } | MessagePreviewCreator { public PreviewResult createPreview(@NonNull Message message) { if (encryptionDetector.isEncrypted(message)) { return PreviewResult.encrypted(); } return extractText(message); } MessagePreviewCreator(TextPartFinder textPartFinder, PreviewTextExtractor previewTextExtractor,
EncryptionDetector encryptionDetector); static MessagePreviewCreator newInstance(); PreviewResult createPreview(@NonNull Message message); } |
@Test public void createPreview_withTextPart() throws Exception { Message message = createDummyMessage(); Part textPart = createTextPart("text/plain"); when(encryptionDetector.isEncrypted(message)).thenReturn(false); when(textPartFinder.findFirstTextPart(message)).thenReturn(textPart); when(previewTextExtractor.extractPreview(textPart)).thenReturn("expected"); PreviewResult result = previewCreator.createPreview(message); assertTrue(result.isPreviewTextAvailable()); assertEquals(PreviewType.TEXT, result.getPreviewType()); assertEquals("expected", result.getPreviewText()); } | public PreviewResult createPreview(@NonNull Message message) { if (encryptionDetector.isEncrypted(message)) { return PreviewResult.encrypted(); } return extractText(message); } | MessagePreviewCreator { public PreviewResult createPreview(@NonNull Message message) { if (encryptionDetector.isEncrypted(message)) { return PreviewResult.encrypted(); } return extractText(message); } } | MessagePreviewCreator { public PreviewResult createPreview(@NonNull Message message) { if (encryptionDetector.isEncrypted(message)) { return PreviewResult.encrypted(); } return extractText(message); } MessagePreviewCreator(TextPartFinder textPartFinder, PreviewTextExtractor previewTextExtractor,
EncryptionDetector encryptionDetector); } | MessagePreviewCreator { public PreviewResult createPreview(@NonNull Message message) { if (encryptionDetector.isEncrypted(message)) { return PreviewResult.encrypted(); } return extractText(message); } MessagePreviewCreator(TextPartFinder textPartFinder, PreviewTextExtractor previewTextExtractor,
EncryptionDetector encryptionDetector); static MessagePreviewCreator newInstance(); PreviewResult createPreview(@NonNull Message message); } | MessagePreviewCreator { public PreviewResult createPreview(@NonNull Message message) { if (encryptionDetector.isEncrypted(message)) { return PreviewResult.encrypted(); } return extractText(message); } MessagePreviewCreator(TextPartFinder textPartFinder, PreviewTextExtractor previewTextExtractor,
EncryptionDetector encryptionDetector); static MessagePreviewCreator newInstance(); PreviewResult createPreview(@NonNull Message message); } |
@Test public void createPreview_withPreviewTextExtractorThrowing() throws Exception { Message message = createDummyMessage(); Part textPart = createTextPart("text/plain"); when(encryptionDetector.isEncrypted(message)).thenReturn(false); when(textPartFinder.findFirstTextPart(message)).thenReturn(textPart); when(previewTextExtractor.extractPreview(textPart)).thenThrow(new PreviewExtractionException("")); PreviewResult result = previewCreator.createPreview(message); assertFalse(result.isPreviewTextAvailable()); assertEquals(PreviewType.ERROR, result.getPreviewType()); } | public PreviewResult createPreview(@NonNull Message message) { if (encryptionDetector.isEncrypted(message)) { return PreviewResult.encrypted(); } return extractText(message); } | MessagePreviewCreator { public PreviewResult createPreview(@NonNull Message message) { if (encryptionDetector.isEncrypted(message)) { return PreviewResult.encrypted(); } return extractText(message); } } | MessagePreviewCreator { public PreviewResult createPreview(@NonNull Message message) { if (encryptionDetector.isEncrypted(message)) { return PreviewResult.encrypted(); } return extractText(message); } MessagePreviewCreator(TextPartFinder textPartFinder, PreviewTextExtractor previewTextExtractor,
EncryptionDetector encryptionDetector); } | MessagePreviewCreator { public PreviewResult createPreview(@NonNull Message message) { if (encryptionDetector.isEncrypted(message)) { return PreviewResult.encrypted(); } return extractText(message); } MessagePreviewCreator(TextPartFinder textPartFinder, PreviewTextExtractor previewTextExtractor,
EncryptionDetector encryptionDetector); static MessagePreviewCreator newInstance(); PreviewResult createPreview(@NonNull Message message); } | MessagePreviewCreator { public PreviewResult createPreview(@NonNull Message message) { if (encryptionDetector.isEncrypted(message)) { return PreviewResult.encrypted(); } return extractText(message); } MessagePreviewCreator(TextPartFinder textPartFinder, PreviewTextExtractor previewTextExtractor,
EncryptionDetector encryptionDetector); static MessagePreviewCreator newInstance(); PreviewResult createPreview(@NonNull Message message); } |
@Test public void isEncrypted_withTextPlain_shouldReturnFalse() throws Exception { Message message = createTextMessage("text/plain", "plain text"); boolean encrypted = encryptionDetector.isEncrypted(message); assertFalse(encrypted); } | public boolean isEncrypted(@NonNull Message message) { return isPgpMimeOrSMimeEncrypted(message) || containsInlinePgpEncryptedText(message); } | EncryptionDetector { public boolean isEncrypted(@NonNull Message message) { return isPgpMimeOrSMimeEncrypted(message) || containsInlinePgpEncryptedText(message); } } | EncryptionDetector { public boolean isEncrypted(@NonNull Message message) { return isPgpMimeOrSMimeEncrypted(message) || containsInlinePgpEncryptedText(message); } EncryptionDetector(TextPartFinder textPartFinder); } | EncryptionDetector { public boolean isEncrypted(@NonNull Message message) { return isPgpMimeOrSMimeEncrypted(message) || containsInlinePgpEncryptedText(message); } EncryptionDetector(TextPartFinder textPartFinder); boolean isEncrypted(@NonNull Message message); } | EncryptionDetector { public boolean isEncrypted(@NonNull Message message) { return isPgpMimeOrSMimeEncrypted(message) || containsInlinePgpEncryptedText(message); } EncryptionDetector(TextPartFinder textPartFinder); boolean isEncrypted(@NonNull Message message); } |
@Test public void isEncrypted_withMultipartEncrypted_shouldReturnTrue() throws Exception { Message message = createMultipartMessage("multipart/encrypted", createPart("application/octet-stream"), createPart("application/octet-stream")); boolean encrypted = encryptionDetector.isEncrypted(message); assertTrue(encrypted); } | public boolean isEncrypted(@NonNull Message message) { return isPgpMimeOrSMimeEncrypted(message) || containsInlinePgpEncryptedText(message); } | EncryptionDetector { public boolean isEncrypted(@NonNull Message message) { return isPgpMimeOrSMimeEncrypted(message) || containsInlinePgpEncryptedText(message); } } | EncryptionDetector { public boolean isEncrypted(@NonNull Message message) { return isPgpMimeOrSMimeEncrypted(message) || containsInlinePgpEncryptedText(message); } EncryptionDetector(TextPartFinder textPartFinder); } | EncryptionDetector { public boolean isEncrypted(@NonNull Message message) { return isPgpMimeOrSMimeEncrypted(message) || containsInlinePgpEncryptedText(message); } EncryptionDetector(TextPartFinder textPartFinder); boolean isEncrypted(@NonNull Message message); } | EncryptionDetector { public boolean isEncrypted(@NonNull Message message) { return isPgpMimeOrSMimeEncrypted(message) || containsInlinePgpEncryptedText(message); } EncryptionDetector(TextPartFinder textPartFinder); boolean isEncrypted(@NonNull Message message); } |
@Test public void findEncrypted__withMultipartMixedSubTextAndEncrypted__shouldReturnEncrypted() throws Exception { Message message = messageFromBody( multipart("mixed", bodypart("text/plain"), multipart("encrypted", bodypart("application/pgp-encrypted"), bodypart("application/octet-stream") ) ) ); List<Part> encryptedParts = MessageDecryptVerifier.findEncryptedParts(message); assertEquals(1, encryptedParts.size()); assertSame(getPart(message, 1), encryptedParts.get(0)); } | public static List<Part> findEncryptedParts(Part startPart) { List<Part> encryptedParts = new ArrayList<>(); Stack<Part> partsToCheck = new Stack<>(); partsToCheck.push(startPart); while (!partsToCheck.isEmpty()) { Part part = partsToCheck.pop(); Body body = part.getBody(); if (isPartMultipartEncrypted(part)) { encryptedParts.add(part); continue; } 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; } | MessageDecryptVerifier { public static List<Part> findEncryptedParts(Part startPart) { List<Part> encryptedParts = new ArrayList<>(); Stack<Part> partsToCheck = new Stack<>(); partsToCheck.push(startPart); while (!partsToCheck.isEmpty()) { Part part = partsToCheck.pop(); Body body = part.getBody(); if (isPartMultipartEncrypted(part)) { encryptedParts.add(part); continue; } 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; } } | MessageDecryptVerifier { public static List<Part> findEncryptedParts(Part startPart) { List<Part> encryptedParts = new ArrayList<>(); Stack<Part> partsToCheck = new Stack<>(); partsToCheck.push(startPart); while (!partsToCheck.isEmpty()) { Part part = partsToCheck.pop(); Body body = part.getBody(); if (isPartMultipartEncrypted(part)) { encryptedParts.add(part); continue; } 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; } } | MessageDecryptVerifier { public static List<Part> findEncryptedParts(Part startPart) { List<Part> encryptedParts = new ArrayList<>(); Stack<Part> partsToCheck = new Stack<>(); partsToCheck.push(startPart); while (!partsToCheck.isEmpty()) { Part part = partsToCheck.pop(); Body body = part.getBody(); if (isPartMultipartEncrypted(part)) { encryptedParts.add(part); continue; } 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; } static Part findPrimaryEncryptedOrSignedPart(Part part, List<Part> outputExtraParts); static List<Part> findEncryptedParts(Part startPart); static List<Part> findSignedParts(Part startPart, MessageCryptoAnnotations messageCryptoAnnotations); static List<Part> findPgpInlineParts(Part startPart); static byte[] getSignatureData(Part part); static boolean isPgpMimeEncryptedOrSignedPart(Part part); static boolean isSMimeEncryptedOrSignedPart(Part part); static boolean isPartPgpInlineEncrypted(@Nullable Part part); } | MessageDecryptVerifier { public static List<Part> findEncryptedParts(Part startPart) { List<Part> encryptedParts = new ArrayList<>(); Stack<Part> partsToCheck = new Stack<>(); partsToCheck.push(startPart); while (!partsToCheck.isEmpty()) { Part part = partsToCheck.pop(); Body body = part.getBody(); if (isPartMultipartEncrypted(part)) { encryptedParts.add(part); continue; } 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; } static Part findPrimaryEncryptedOrSignedPart(Part part, List<Part> outputExtraParts); static List<Part> findEncryptedParts(Part startPart); static List<Part> findSignedParts(Part startPart, MessageCryptoAnnotations messageCryptoAnnotations); static List<Part> findPgpInlineParts(Part startPart); static byte[] getSignatureData(Part part); static boolean isPgpMimeEncryptedOrSignedPart(Part part); static boolean isSMimeEncryptedOrSignedPart(Part part); static boolean isPartPgpInlineEncrypted(@Nullable Part part); } |
@Test public void isEncrypted_withSMimePart_shouldReturnTrue() throws Exception { Message message = createMessage("application/pkcs7-mime"); boolean encrypted = encryptionDetector.isEncrypted(message); assertTrue(encrypted); } | public boolean isEncrypted(@NonNull Message message) { return isPgpMimeOrSMimeEncrypted(message) || containsInlinePgpEncryptedText(message); } | EncryptionDetector { public boolean isEncrypted(@NonNull Message message) { return isPgpMimeOrSMimeEncrypted(message) || containsInlinePgpEncryptedText(message); } } | EncryptionDetector { public boolean isEncrypted(@NonNull Message message) { return isPgpMimeOrSMimeEncrypted(message) || containsInlinePgpEncryptedText(message); } EncryptionDetector(TextPartFinder textPartFinder); } | EncryptionDetector { public boolean isEncrypted(@NonNull Message message) { return isPgpMimeOrSMimeEncrypted(message) || containsInlinePgpEncryptedText(message); } EncryptionDetector(TextPartFinder textPartFinder); boolean isEncrypted(@NonNull Message message); } | EncryptionDetector { public boolean isEncrypted(@NonNull Message message) { return isPgpMimeOrSMimeEncrypted(message) || containsInlinePgpEncryptedText(message); } EncryptionDetector(TextPartFinder textPartFinder); boolean isEncrypted(@NonNull Message message); } |
@Test public void isEncrypted_withMultipartMixedContainingSMimePart_shouldReturnTrue() throws Exception { Message message = createMultipartMessage("multipart/mixed", createPart("application/pkcs7-mime"), createPart("text/plain")); boolean encrypted = encryptionDetector.isEncrypted(message); assertTrue(encrypted); } | public boolean isEncrypted(@NonNull Message message) { return isPgpMimeOrSMimeEncrypted(message) || containsInlinePgpEncryptedText(message); } | EncryptionDetector { public boolean isEncrypted(@NonNull Message message) { return isPgpMimeOrSMimeEncrypted(message) || containsInlinePgpEncryptedText(message); } } | EncryptionDetector { public boolean isEncrypted(@NonNull Message message) { return isPgpMimeOrSMimeEncrypted(message) || containsInlinePgpEncryptedText(message); } EncryptionDetector(TextPartFinder textPartFinder); } | EncryptionDetector { public boolean isEncrypted(@NonNull Message message) { return isPgpMimeOrSMimeEncrypted(message) || containsInlinePgpEncryptedText(message); } EncryptionDetector(TextPartFinder textPartFinder); boolean isEncrypted(@NonNull Message message); } | EncryptionDetector { public boolean isEncrypted(@NonNull Message message) { return isPgpMimeOrSMimeEncrypted(message) || containsInlinePgpEncryptedText(message); } EncryptionDetector(TextPartFinder textPartFinder); boolean isEncrypted(@NonNull Message message); } |
@Test public void isEncrypted_withInlinePgp_shouldReturnTrue() throws Exception { Message message = createTextMessage("text/plain", "" + "-----BEGIN PGP MESSAGE-----" + CRLF + "some encrypted stuff here" + CRLF + "-----END PGP MESSAGE-----"); when(textPartFinder.findFirstTextPart(message)).thenReturn(message); boolean encrypted = encryptionDetector.isEncrypted(message); assertTrue(encrypted); } | public boolean isEncrypted(@NonNull Message message) { return isPgpMimeOrSMimeEncrypted(message) || containsInlinePgpEncryptedText(message); } | EncryptionDetector { public boolean isEncrypted(@NonNull Message message) { return isPgpMimeOrSMimeEncrypted(message) || containsInlinePgpEncryptedText(message); } } | EncryptionDetector { public boolean isEncrypted(@NonNull Message message) { return isPgpMimeOrSMimeEncrypted(message) || containsInlinePgpEncryptedText(message); } EncryptionDetector(TextPartFinder textPartFinder); } | EncryptionDetector { public boolean isEncrypted(@NonNull Message message) { return isPgpMimeOrSMimeEncrypted(message) || containsInlinePgpEncryptedText(message); } EncryptionDetector(TextPartFinder textPartFinder); boolean isEncrypted(@NonNull Message message); } | EncryptionDetector { public boolean isEncrypted(@NonNull Message message) { return isPgpMimeOrSMimeEncrypted(message) || containsInlinePgpEncryptedText(message); } EncryptionDetector(TextPartFinder textPartFinder); boolean isEncrypted(@NonNull Message message); } |
@Test public void isEncrypted_withPlainTextAndPreambleWithInlinePgp_shouldReturnFalse() throws Exception { Message message = createTextMessage("text/plain", "" + "preamble" + CRLF + "-----BEGIN PGP MESSAGE-----" + CRLF + "some encrypted stuff here" + CRLF + "-----END PGP MESSAGE-----" + CRLF + "epilogue"); when(textPartFinder.findFirstTextPart(message)).thenReturn(message); boolean encrypted = encryptionDetector.isEncrypted(message); assertFalse(encrypted); } | public boolean isEncrypted(@NonNull Message message) { return isPgpMimeOrSMimeEncrypted(message) || containsInlinePgpEncryptedText(message); } | EncryptionDetector { public boolean isEncrypted(@NonNull Message message) { return isPgpMimeOrSMimeEncrypted(message) || containsInlinePgpEncryptedText(message); } } | EncryptionDetector { public boolean isEncrypted(@NonNull Message message) { return isPgpMimeOrSMimeEncrypted(message) || containsInlinePgpEncryptedText(message); } EncryptionDetector(TextPartFinder textPartFinder); } | EncryptionDetector { public boolean isEncrypted(@NonNull Message message) { return isPgpMimeOrSMimeEncrypted(message) || containsInlinePgpEncryptedText(message); } EncryptionDetector(TextPartFinder textPartFinder); boolean isEncrypted(@NonNull Message message); } | EncryptionDetector { public boolean isEncrypted(@NonNull Message message) { return isPgpMimeOrSMimeEncrypted(message) || containsInlinePgpEncryptedText(message); } EncryptionDetector(TextPartFinder textPartFinder); boolean isEncrypted(@NonNull Message message); } |
@Test public void isEncrypted_withQuotedInlinePgp_shouldReturnFalse() throws Exception { Message message = createTextMessage("text/plain", "" + "good talk!" + CRLF + CRLF + "> -----BEGIN PGP MESSAGE-----" + CRLF + "> some encrypted stuff here" + CRLF + "> -----END PGP MESSAGE-----" + CRLF + CRLF + "-- " + CRLF + "my signature"); when(textPartFinder.findFirstTextPart(message)).thenReturn(message); boolean encrypted = encryptionDetector.isEncrypted(message); assertFalse(encrypted); } | public boolean isEncrypted(@NonNull Message message) { return isPgpMimeOrSMimeEncrypted(message) || containsInlinePgpEncryptedText(message); } | EncryptionDetector { public boolean isEncrypted(@NonNull Message message) { return isPgpMimeOrSMimeEncrypted(message) || containsInlinePgpEncryptedText(message); } } | EncryptionDetector { public boolean isEncrypted(@NonNull Message message) { return isPgpMimeOrSMimeEncrypted(message) || containsInlinePgpEncryptedText(message); } EncryptionDetector(TextPartFinder textPartFinder); } | EncryptionDetector { public boolean isEncrypted(@NonNull Message message) { return isPgpMimeOrSMimeEncrypted(message) || containsInlinePgpEncryptedText(message); } EncryptionDetector(TextPartFinder textPartFinder); boolean isEncrypted(@NonNull Message message); } | EncryptionDetector { public boolean isEncrypted(@NonNull Message message) { return isPgpMimeOrSMimeEncrypted(message) || containsInlinePgpEncryptedText(message); } EncryptionDetector(TextPartFinder textPartFinder); boolean isEncrypted(@NonNull Message message); } |
@Test(expected = PreviewExtractionException.class) public void extractPreview_withEmptyBody_shouldThrow() throws Exception { Part part = new MimeBodyPart(null, "text/plain"); previewTextExtractor.extractPreview(part); } | @NonNull public String extractPreview(@NonNull Part textPart) throws PreviewExtractionException { String text = MessageExtractor.getTextFromPart(textPart, MAX_CHARACTERS_CHECKED_FOR_PREVIEW); if (text == null) { throw new PreviewExtractionException("Couldn't get text from part"); } String plainText = convertFromHtmlIfNecessary(textPart, text); return stripTextForPreview(plainText); } | PreviewTextExtractor { @NonNull public String extractPreview(@NonNull Part textPart) throws PreviewExtractionException { String text = MessageExtractor.getTextFromPart(textPart, MAX_CHARACTERS_CHECKED_FOR_PREVIEW); if (text == null) { throw new PreviewExtractionException("Couldn't get text from part"); } String plainText = convertFromHtmlIfNecessary(textPart, text); return stripTextForPreview(plainText); } } | PreviewTextExtractor { @NonNull public String extractPreview(@NonNull Part textPart) throws PreviewExtractionException { String text = MessageExtractor.getTextFromPart(textPart, MAX_CHARACTERS_CHECKED_FOR_PREVIEW); if (text == null) { throw new PreviewExtractionException("Couldn't get text from part"); } String plainText = convertFromHtmlIfNecessary(textPart, text); return stripTextForPreview(plainText); } } | PreviewTextExtractor { @NonNull public String extractPreview(@NonNull Part textPart) throws PreviewExtractionException { String text = MessageExtractor.getTextFromPart(textPart, MAX_CHARACTERS_CHECKED_FOR_PREVIEW); if (text == null) { throw new PreviewExtractionException("Couldn't get text from part"); } String plainText = convertFromHtmlIfNecessary(textPart, text); return stripTextForPreview(plainText); } @NonNull String extractPreview(@NonNull Part textPart); } | PreviewTextExtractor { @NonNull public String extractPreview(@NonNull Part textPart) throws PreviewExtractionException { String text = MessageExtractor.getTextFromPart(textPart, MAX_CHARACTERS_CHECKED_FOR_PREVIEW); if (text == null) { throw new PreviewExtractionException("Couldn't get text from part"); } String plainText = convertFromHtmlIfNecessary(textPart, text); return stripTextForPreview(plainText); } @NonNull String extractPreview(@NonNull Part textPart); } |
@Test public void extractPreview_withSimpleTextPlain() throws Exception { String text = "The quick brown fox jumps over the lazy dog"; Part part = createTextPart("text/plain", text); String preview = previewTextExtractor.extractPreview(part); assertEquals(text, preview); } | @NonNull public String extractPreview(@NonNull Part textPart) throws PreviewExtractionException { String text = MessageExtractor.getTextFromPart(textPart, MAX_CHARACTERS_CHECKED_FOR_PREVIEW); if (text == null) { throw new PreviewExtractionException("Couldn't get text from part"); } String plainText = convertFromHtmlIfNecessary(textPart, text); return stripTextForPreview(plainText); } | PreviewTextExtractor { @NonNull public String extractPreview(@NonNull Part textPart) throws PreviewExtractionException { String text = MessageExtractor.getTextFromPart(textPart, MAX_CHARACTERS_CHECKED_FOR_PREVIEW); if (text == null) { throw new PreviewExtractionException("Couldn't get text from part"); } String plainText = convertFromHtmlIfNecessary(textPart, text); return stripTextForPreview(plainText); } } | PreviewTextExtractor { @NonNull public String extractPreview(@NonNull Part textPart) throws PreviewExtractionException { String text = MessageExtractor.getTextFromPart(textPart, MAX_CHARACTERS_CHECKED_FOR_PREVIEW); if (text == null) { throw new PreviewExtractionException("Couldn't get text from part"); } String plainText = convertFromHtmlIfNecessary(textPart, text); return stripTextForPreview(plainText); } } | PreviewTextExtractor { @NonNull public String extractPreview(@NonNull Part textPart) throws PreviewExtractionException { String text = MessageExtractor.getTextFromPart(textPart, MAX_CHARACTERS_CHECKED_FOR_PREVIEW); if (text == null) { throw new PreviewExtractionException("Couldn't get text from part"); } String plainText = convertFromHtmlIfNecessary(textPart, text); return stripTextForPreview(plainText); } @NonNull String extractPreview(@NonNull Part textPart); } | PreviewTextExtractor { @NonNull public String extractPreview(@NonNull Part textPart) throws PreviewExtractionException { String text = MessageExtractor.getTextFromPart(textPart, MAX_CHARACTERS_CHECKED_FOR_PREVIEW); if (text == null) { throw new PreviewExtractionException("Couldn't get text from part"); } String plainText = convertFromHtmlIfNecessary(textPart, text); return stripTextForPreview(plainText); } @NonNull String extractPreview(@NonNull Part textPart); } |
@Test public void extractPreview_withSimpleTextHtml() throws Exception { String text = "<b>The quick brown fox jumps over the lazy dog</b>"; Part part = createTextPart("text/html", text); String preview = previewTextExtractor.extractPreview(part); assertEquals("The quick brown fox jumps over the lazy dog", preview); } | @NonNull public String extractPreview(@NonNull Part textPart) throws PreviewExtractionException { String text = MessageExtractor.getTextFromPart(textPart, MAX_CHARACTERS_CHECKED_FOR_PREVIEW); if (text == null) { throw new PreviewExtractionException("Couldn't get text from part"); } String plainText = convertFromHtmlIfNecessary(textPart, text); return stripTextForPreview(plainText); } | PreviewTextExtractor { @NonNull public String extractPreview(@NonNull Part textPart) throws PreviewExtractionException { String text = MessageExtractor.getTextFromPart(textPart, MAX_CHARACTERS_CHECKED_FOR_PREVIEW); if (text == null) { throw new PreviewExtractionException("Couldn't get text from part"); } String plainText = convertFromHtmlIfNecessary(textPart, text); return stripTextForPreview(plainText); } } | PreviewTextExtractor { @NonNull public String extractPreview(@NonNull Part textPart) throws PreviewExtractionException { String text = MessageExtractor.getTextFromPart(textPart, MAX_CHARACTERS_CHECKED_FOR_PREVIEW); if (text == null) { throw new PreviewExtractionException("Couldn't get text from part"); } String plainText = convertFromHtmlIfNecessary(textPart, text); return stripTextForPreview(plainText); } } | PreviewTextExtractor { @NonNull public String extractPreview(@NonNull Part textPart) throws PreviewExtractionException { String text = MessageExtractor.getTextFromPart(textPart, MAX_CHARACTERS_CHECKED_FOR_PREVIEW); if (text == null) { throw new PreviewExtractionException("Couldn't get text from part"); } String plainText = convertFromHtmlIfNecessary(textPart, text); return stripTextForPreview(plainText); } @NonNull String extractPreview(@NonNull Part textPart); } | PreviewTextExtractor { @NonNull public String extractPreview(@NonNull Part textPart) throws PreviewExtractionException { String text = MessageExtractor.getTextFromPart(textPart, MAX_CHARACTERS_CHECKED_FOR_PREVIEW); if (text == null) { throw new PreviewExtractionException("Couldn't get text from part"); } String plainText = convertFromHtmlIfNecessary(textPart, text); return stripTextForPreview(plainText); } @NonNull String extractPreview(@NonNull Part textPart); } |
@Test public void extractPreview_withLongTextPlain() throws Exception { String text = "" + "10--------20--------30--------40--------50--------" + "60--------70--------80--------90--------100-------" + "110-------120-------130-------140-------150-------" + "160-------170-------180-------190-------200-------" + "210-------220-------230-------240-------250-------" + "260-------270-------280-------290-------300-------" + "310-------320-------330-------340-------350-------" + "360-------370-------380-------390-------400-------" + "410-------420-------430-------440-------450-------" + "460-------470-------480-------490-------500-------" + "510-------520-------"; Part part = createTextPart("text/plain", text); String preview = previewTextExtractor.extractPreview(part); assertEquals(text.substring(0, 511) + "…", preview); } | @NonNull public String extractPreview(@NonNull Part textPart) throws PreviewExtractionException { String text = MessageExtractor.getTextFromPart(textPart, MAX_CHARACTERS_CHECKED_FOR_PREVIEW); if (text == null) { throw new PreviewExtractionException("Couldn't get text from part"); } String plainText = convertFromHtmlIfNecessary(textPart, text); return stripTextForPreview(plainText); } | PreviewTextExtractor { @NonNull public String extractPreview(@NonNull Part textPart) throws PreviewExtractionException { String text = MessageExtractor.getTextFromPart(textPart, MAX_CHARACTERS_CHECKED_FOR_PREVIEW); if (text == null) { throw new PreviewExtractionException("Couldn't get text from part"); } String plainText = convertFromHtmlIfNecessary(textPart, text); return stripTextForPreview(plainText); } } | PreviewTextExtractor { @NonNull public String extractPreview(@NonNull Part textPart) throws PreviewExtractionException { String text = MessageExtractor.getTextFromPart(textPart, MAX_CHARACTERS_CHECKED_FOR_PREVIEW); if (text == null) { throw new PreviewExtractionException("Couldn't get text from part"); } String plainText = convertFromHtmlIfNecessary(textPart, text); return stripTextForPreview(plainText); } } | PreviewTextExtractor { @NonNull public String extractPreview(@NonNull Part textPart) throws PreviewExtractionException { String text = MessageExtractor.getTextFromPart(textPart, MAX_CHARACTERS_CHECKED_FOR_PREVIEW); if (text == null) { throw new PreviewExtractionException("Couldn't get text from part"); } String plainText = convertFromHtmlIfNecessary(textPart, text); return stripTextForPreview(plainText); } @NonNull String extractPreview(@NonNull Part textPart); } | PreviewTextExtractor { @NonNull public String extractPreview(@NonNull Part textPart) throws PreviewExtractionException { String text = MessageExtractor.getTextFromPart(textPart, MAX_CHARACTERS_CHECKED_FOR_PREVIEW); if (text == null) { throw new PreviewExtractionException("Couldn't get text from part"); } String plainText = convertFromHtmlIfNecessary(textPart, text); return stripTextForPreview(plainText); } @NonNull String extractPreview(@NonNull Part textPart); } |
@Test public void extractPreview_shouldStripSignature() throws Exception { String text = "" + "Some text\r\n" + "-- \r\n" + "Signature"; Part part = createTextPart("text/plain", text); String preview = previewTextExtractor.extractPreview(part); assertEquals("Some text", preview); } | @NonNull public String extractPreview(@NonNull Part textPart) throws PreviewExtractionException { String text = MessageExtractor.getTextFromPart(textPart, MAX_CHARACTERS_CHECKED_FOR_PREVIEW); if (text == null) { throw new PreviewExtractionException("Couldn't get text from part"); } String plainText = convertFromHtmlIfNecessary(textPart, text); return stripTextForPreview(plainText); } | PreviewTextExtractor { @NonNull public String extractPreview(@NonNull Part textPart) throws PreviewExtractionException { String text = MessageExtractor.getTextFromPart(textPart, MAX_CHARACTERS_CHECKED_FOR_PREVIEW); if (text == null) { throw new PreviewExtractionException("Couldn't get text from part"); } String plainText = convertFromHtmlIfNecessary(textPart, text); return stripTextForPreview(plainText); } } | PreviewTextExtractor { @NonNull public String extractPreview(@NonNull Part textPart) throws PreviewExtractionException { String text = MessageExtractor.getTextFromPart(textPart, MAX_CHARACTERS_CHECKED_FOR_PREVIEW); if (text == null) { throw new PreviewExtractionException("Couldn't get text from part"); } String plainText = convertFromHtmlIfNecessary(textPart, text); return stripTextForPreview(plainText); } } | PreviewTextExtractor { @NonNull public String extractPreview(@NonNull Part textPart) throws PreviewExtractionException { String text = MessageExtractor.getTextFromPart(textPart, MAX_CHARACTERS_CHECKED_FOR_PREVIEW); if (text == null) { throw new PreviewExtractionException("Couldn't get text from part"); } String plainText = convertFromHtmlIfNecessary(textPart, text); return stripTextForPreview(plainText); } @NonNull String extractPreview(@NonNull Part textPart); } | PreviewTextExtractor { @NonNull public String extractPreview(@NonNull Part textPart) throws PreviewExtractionException { String text = MessageExtractor.getTextFromPart(textPart, MAX_CHARACTERS_CHECKED_FOR_PREVIEW); if (text == null) { throw new PreviewExtractionException("Couldn't get text from part"); } String plainText = convertFromHtmlIfNecessary(textPart, text); return stripTextForPreview(plainText); } @NonNull String extractPreview(@NonNull Part textPart); } |
@Test public void findEncrypted__withMultipartMixedSubEncryptedAndText__shouldReturnEncrypted() throws Exception { Message message = messageFromBody( multipart("mixed", multipart("encrypted", bodypart("application/pgp-encrypted"), bodypart("application/octet-stream") ), bodypart("text/plain") ) ); List<Part> encryptedParts = MessageDecryptVerifier.findEncryptedParts(message); assertEquals(1, encryptedParts.size()); assertSame(getPart(message, 0), encryptedParts.get(0)); } | public static List<Part> findEncryptedParts(Part startPart) { List<Part> encryptedParts = new ArrayList<>(); Stack<Part> partsToCheck = new Stack<>(); partsToCheck.push(startPart); while (!partsToCheck.isEmpty()) { Part part = partsToCheck.pop(); Body body = part.getBody(); if (isPartMultipartEncrypted(part)) { encryptedParts.add(part); continue; } 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; } | MessageDecryptVerifier { public static List<Part> findEncryptedParts(Part startPart) { List<Part> encryptedParts = new ArrayList<>(); Stack<Part> partsToCheck = new Stack<>(); partsToCheck.push(startPart); while (!partsToCheck.isEmpty()) { Part part = partsToCheck.pop(); Body body = part.getBody(); if (isPartMultipartEncrypted(part)) { encryptedParts.add(part); continue; } 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; } } | MessageDecryptVerifier { public static List<Part> findEncryptedParts(Part startPart) { List<Part> encryptedParts = new ArrayList<>(); Stack<Part> partsToCheck = new Stack<>(); partsToCheck.push(startPart); while (!partsToCheck.isEmpty()) { Part part = partsToCheck.pop(); Body body = part.getBody(); if (isPartMultipartEncrypted(part)) { encryptedParts.add(part); continue; } 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; } } | MessageDecryptVerifier { public static List<Part> findEncryptedParts(Part startPart) { List<Part> encryptedParts = new ArrayList<>(); Stack<Part> partsToCheck = new Stack<>(); partsToCheck.push(startPart); while (!partsToCheck.isEmpty()) { Part part = partsToCheck.pop(); Body body = part.getBody(); if (isPartMultipartEncrypted(part)) { encryptedParts.add(part); continue; } 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; } static Part findPrimaryEncryptedOrSignedPart(Part part, List<Part> outputExtraParts); static List<Part> findEncryptedParts(Part startPart); static List<Part> findSignedParts(Part startPart, MessageCryptoAnnotations messageCryptoAnnotations); static List<Part> findPgpInlineParts(Part startPart); static byte[] getSignatureData(Part part); static boolean isPgpMimeEncryptedOrSignedPart(Part part); static boolean isSMimeEncryptedOrSignedPart(Part part); static boolean isPartPgpInlineEncrypted(@Nullable Part part); } | MessageDecryptVerifier { public static List<Part> findEncryptedParts(Part startPart) { List<Part> encryptedParts = new ArrayList<>(); Stack<Part> partsToCheck = new Stack<>(); partsToCheck.push(startPart); while (!partsToCheck.isEmpty()) { Part part = partsToCheck.pop(); Body body = part.getBody(); if (isPartMultipartEncrypted(part)) { encryptedParts.add(part); continue; } 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; } static Part findPrimaryEncryptedOrSignedPart(Part part, List<Part> outputExtraParts); static List<Part> findEncryptedParts(Part startPart); static List<Part> findSignedParts(Part startPart, MessageCryptoAnnotations messageCryptoAnnotations); static List<Part> findPgpInlineParts(Part startPart); static byte[] getSignatureData(Part part); static boolean isPgpMimeEncryptedOrSignedPart(Part part); static boolean isSMimeEncryptedOrSignedPart(Part part); static boolean isPartPgpInlineEncrypted(@Nullable Part part); } |
@Test public void extractPreview_shouldStripHorizontalLine() throws Exception { String text = "" + "line 1\r\n" + "----\r\n" + "line 2"; Part part = createTextPart("text/plain", text); String preview = previewTextExtractor.extractPreview(part); assertEquals("line 1 line 2", preview); } | @NonNull public String extractPreview(@NonNull Part textPart) throws PreviewExtractionException { String text = MessageExtractor.getTextFromPart(textPart, MAX_CHARACTERS_CHECKED_FOR_PREVIEW); if (text == null) { throw new PreviewExtractionException("Couldn't get text from part"); } String plainText = convertFromHtmlIfNecessary(textPart, text); return stripTextForPreview(plainText); } | PreviewTextExtractor { @NonNull public String extractPreview(@NonNull Part textPart) throws PreviewExtractionException { String text = MessageExtractor.getTextFromPart(textPart, MAX_CHARACTERS_CHECKED_FOR_PREVIEW); if (text == null) { throw new PreviewExtractionException("Couldn't get text from part"); } String plainText = convertFromHtmlIfNecessary(textPart, text); return stripTextForPreview(plainText); } } | PreviewTextExtractor { @NonNull public String extractPreview(@NonNull Part textPart) throws PreviewExtractionException { String text = MessageExtractor.getTextFromPart(textPart, MAX_CHARACTERS_CHECKED_FOR_PREVIEW); if (text == null) { throw new PreviewExtractionException("Couldn't get text from part"); } String plainText = convertFromHtmlIfNecessary(textPart, text); return stripTextForPreview(plainText); } } | PreviewTextExtractor { @NonNull public String extractPreview(@NonNull Part textPart) throws PreviewExtractionException { String text = MessageExtractor.getTextFromPart(textPart, MAX_CHARACTERS_CHECKED_FOR_PREVIEW); if (text == null) { throw new PreviewExtractionException("Couldn't get text from part"); } String plainText = convertFromHtmlIfNecessary(textPart, text); return stripTextForPreview(plainText); } @NonNull String extractPreview(@NonNull Part textPart); } | PreviewTextExtractor { @NonNull public String extractPreview(@NonNull Part textPart) throws PreviewExtractionException { String text = MessageExtractor.getTextFromPart(textPart, MAX_CHARACTERS_CHECKED_FOR_PREVIEW); if (text == null) { throw new PreviewExtractionException("Couldn't get text from part"); } String plainText = convertFromHtmlIfNecessary(textPart, text); return stripTextForPreview(plainText); } @NonNull String extractPreview(@NonNull Part textPart); } |
@Test public void extractPreview_shouldStripQuoteHeaderAndQuotedText() throws Exception { String text = "" + "some text\r\n" + "On 01/02/03 someone wrote\r\n" + "> some quoted text\r\n" + "# some other quoted text\r\n"; Part part = createTextPart("text/plain", text); String preview = previewTextExtractor.extractPreview(part); assertEquals("some text", preview); } | @NonNull public String extractPreview(@NonNull Part textPart) throws PreviewExtractionException { String text = MessageExtractor.getTextFromPart(textPart, MAX_CHARACTERS_CHECKED_FOR_PREVIEW); if (text == null) { throw new PreviewExtractionException("Couldn't get text from part"); } String plainText = convertFromHtmlIfNecessary(textPart, text); return stripTextForPreview(plainText); } | PreviewTextExtractor { @NonNull public String extractPreview(@NonNull Part textPart) throws PreviewExtractionException { String text = MessageExtractor.getTextFromPart(textPart, MAX_CHARACTERS_CHECKED_FOR_PREVIEW); if (text == null) { throw new PreviewExtractionException("Couldn't get text from part"); } String plainText = convertFromHtmlIfNecessary(textPart, text); return stripTextForPreview(plainText); } } | PreviewTextExtractor { @NonNull public String extractPreview(@NonNull Part textPart) throws PreviewExtractionException { String text = MessageExtractor.getTextFromPart(textPart, MAX_CHARACTERS_CHECKED_FOR_PREVIEW); if (text == null) { throw new PreviewExtractionException("Couldn't get text from part"); } String plainText = convertFromHtmlIfNecessary(textPart, text); return stripTextForPreview(plainText); } } | PreviewTextExtractor { @NonNull public String extractPreview(@NonNull Part textPart) throws PreviewExtractionException { String text = MessageExtractor.getTextFromPart(textPart, MAX_CHARACTERS_CHECKED_FOR_PREVIEW); if (text == null) { throw new PreviewExtractionException("Couldn't get text from part"); } String plainText = convertFromHtmlIfNecessary(textPart, text); return stripTextForPreview(plainText); } @NonNull String extractPreview(@NonNull Part textPart); } | PreviewTextExtractor { @NonNull public String extractPreview(@NonNull Part textPart) throws PreviewExtractionException { String text = MessageExtractor.getTextFromPart(textPart, MAX_CHARACTERS_CHECKED_FOR_PREVIEW); if (text == null) { throw new PreviewExtractionException("Couldn't get text from part"); } String plainText = convertFromHtmlIfNecessary(textPart, text); return stripTextForPreview(plainText); } @NonNull String extractPreview(@NonNull Part textPart); } |
@Test public void extractPreview_shouldStripGenericQuoteHeader() throws Exception { String text = "" + "Am 13.12.2015 um 23:42 schrieb Hans:\r\n" + "> hallo\r\n" + "hi there\r\n"; Part part = createTextPart("text/plain", text); String preview = previewTextExtractor.extractPreview(part); assertEquals("hi there", preview); } | @NonNull public String extractPreview(@NonNull Part textPart) throws PreviewExtractionException { String text = MessageExtractor.getTextFromPart(textPart, MAX_CHARACTERS_CHECKED_FOR_PREVIEW); if (text == null) { throw new PreviewExtractionException("Couldn't get text from part"); } String plainText = convertFromHtmlIfNecessary(textPart, text); return stripTextForPreview(plainText); } | PreviewTextExtractor { @NonNull public String extractPreview(@NonNull Part textPart) throws PreviewExtractionException { String text = MessageExtractor.getTextFromPart(textPart, MAX_CHARACTERS_CHECKED_FOR_PREVIEW); if (text == null) { throw new PreviewExtractionException("Couldn't get text from part"); } String plainText = convertFromHtmlIfNecessary(textPart, text); return stripTextForPreview(plainText); } } | PreviewTextExtractor { @NonNull public String extractPreview(@NonNull Part textPart) throws PreviewExtractionException { String text = MessageExtractor.getTextFromPart(textPart, MAX_CHARACTERS_CHECKED_FOR_PREVIEW); if (text == null) { throw new PreviewExtractionException("Couldn't get text from part"); } String plainText = convertFromHtmlIfNecessary(textPart, text); return stripTextForPreview(plainText); } } | PreviewTextExtractor { @NonNull public String extractPreview(@NonNull Part textPart) throws PreviewExtractionException { String text = MessageExtractor.getTextFromPart(textPart, MAX_CHARACTERS_CHECKED_FOR_PREVIEW); if (text == null) { throw new PreviewExtractionException("Couldn't get text from part"); } String plainText = convertFromHtmlIfNecessary(textPart, text); return stripTextForPreview(plainText); } @NonNull String extractPreview(@NonNull Part textPart); } | PreviewTextExtractor { @NonNull public String extractPreview(@NonNull Part textPart) throws PreviewExtractionException { String text = MessageExtractor.getTextFromPart(textPart, MAX_CHARACTERS_CHECKED_FOR_PREVIEW); if (text == null) { throw new PreviewExtractionException("Couldn't get text from part"); } String plainText = convertFromHtmlIfNecessary(textPart, text); return stripTextForPreview(plainText); } @NonNull String extractPreview(@NonNull Part textPart); } |
@Test public void extractPreview_shouldStripHorizontalRules() throws Exception { String text = "line 1" + "------------------------------\r\n" + "line 2"; Part part = createTextPart("text/plain", text); String preview = previewTextExtractor.extractPreview(part); assertEquals("line 1 line 2", preview); } | @NonNull public String extractPreview(@NonNull Part textPart) throws PreviewExtractionException { String text = MessageExtractor.getTextFromPart(textPart, MAX_CHARACTERS_CHECKED_FOR_PREVIEW); if (text == null) { throw new PreviewExtractionException("Couldn't get text from part"); } String plainText = convertFromHtmlIfNecessary(textPart, text); return stripTextForPreview(plainText); } | PreviewTextExtractor { @NonNull public String extractPreview(@NonNull Part textPart) throws PreviewExtractionException { String text = MessageExtractor.getTextFromPart(textPart, MAX_CHARACTERS_CHECKED_FOR_PREVIEW); if (text == null) { throw new PreviewExtractionException("Couldn't get text from part"); } String plainText = convertFromHtmlIfNecessary(textPart, text); return stripTextForPreview(plainText); } } | PreviewTextExtractor { @NonNull public String extractPreview(@NonNull Part textPart) throws PreviewExtractionException { String text = MessageExtractor.getTextFromPart(textPart, MAX_CHARACTERS_CHECKED_FOR_PREVIEW); if (text == null) { throw new PreviewExtractionException("Couldn't get text from part"); } String plainText = convertFromHtmlIfNecessary(textPart, text); return stripTextForPreview(plainText); } } | PreviewTextExtractor { @NonNull public String extractPreview(@NonNull Part textPart) throws PreviewExtractionException { String text = MessageExtractor.getTextFromPart(textPart, MAX_CHARACTERS_CHECKED_FOR_PREVIEW); if (text == null) { throw new PreviewExtractionException("Couldn't get text from part"); } String plainText = convertFromHtmlIfNecessary(textPart, text); return stripTextForPreview(plainText); } @NonNull String extractPreview(@NonNull Part textPart); } | PreviewTextExtractor { @NonNull public String extractPreview(@NonNull Part textPart) throws PreviewExtractionException { String text = MessageExtractor.getTextFromPart(textPart, MAX_CHARACTERS_CHECKED_FOR_PREVIEW); if (text == null) { throw new PreviewExtractionException("Couldn't get text from part"); } String plainText = convertFromHtmlIfNecessary(textPart, text); return stripTextForPreview(plainText); } @NonNull String extractPreview(@NonNull Part textPart); } |
@Test public void extractPreview_shouldReplaceUrl() throws Exception { String text = "some url: https: Part part = createTextPart("text/plain", text); String preview = previewTextExtractor.extractPreview(part); assertEquals("some url: ...", preview); } | @NonNull public String extractPreview(@NonNull Part textPart) throws PreviewExtractionException { String text = MessageExtractor.getTextFromPart(textPart, MAX_CHARACTERS_CHECKED_FOR_PREVIEW); if (text == null) { throw new PreviewExtractionException("Couldn't get text from part"); } String plainText = convertFromHtmlIfNecessary(textPart, text); return stripTextForPreview(plainText); } | PreviewTextExtractor { @NonNull public String extractPreview(@NonNull Part textPart) throws PreviewExtractionException { String text = MessageExtractor.getTextFromPart(textPart, MAX_CHARACTERS_CHECKED_FOR_PREVIEW); if (text == null) { throw new PreviewExtractionException("Couldn't get text from part"); } String plainText = convertFromHtmlIfNecessary(textPart, text); return stripTextForPreview(plainText); } } | PreviewTextExtractor { @NonNull public String extractPreview(@NonNull Part textPart) throws PreviewExtractionException { String text = MessageExtractor.getTextFromPart(textPart, MAX_CHARACTERS_CHECKED_FOR_PREVIEW); if (text == null) { throw new PreviewExtractionException("Couldn't get text from part"); } String plainText = convertFromHtmlIfNecessary(textPart, text); return stripTextForPreview(plainText); } } | PreviewTextExtractor { @NonNull public String extractPreview(@NonNull Part textPart) throws PreviewExtractionException { String text = MessageExtractor.getTextFromPart(textPart, MAX_CHARACTERS_CHECKED_FOR_PREVIEW); if (text == null) { throw new PreviewExtractionException("Couldn't get text from part"); } String plainText = convertFromHtmlIfNecessary(textPart, text); return stripTextForPreview(plainText); } @NonNull String extractPreview(@NonNull Part textPart); } | PreviewTextExtractor { @NonNull public String extractPreview(@NonNull Part textPart) throws PreviewExtractionException { String text = MessageExtractor.getTextFromPart(textPart, MAX_CHARACTERS_CHECKED_FOR_PREVIEW); if (text == null) { throw new PreviewExtractionException("Couldn't get text from part"); } String plainText = convertFromHtmlIfNecessary(textPart, text); return stripTextForPreview(plainText); } @NonNull String extractPreview(@NonNull Part textPart); } |
@Test public void extractPreview_shouldCollapseAndTrimWhitespace() throws Exception { String text = " whitespace is\t\tfun "; Part part = createTextPart("text/plain", text); String preview = previewTextExtractor.extractPreview(part); assertEquals("whitespace is fun", preview); } | @NonNull public String extractPreview(@NonNull Part textPart) throws PreviewExtractionException { String text = MessageExtractor.getTextFromPart(textPart, MAX_CHARACTERS_CHECKED_FOR_PREVIEW); if (text == null) { throw new PreviewExtractionException("Couldn't get text from part"); } String plainText = convertFromHtmlIfNecessary(textPart, text); return stripTextForPreview(plainText); } | PreviewTextExtractor { @NonNull public String extractPreview(@NonNull Part textPart) throws PreviewExtractionException { String text = MessageExtractor.getTextFromPart(textPart, MAX_CHARACTERS_CHECKED_FOR_PREVIEW); if (text == null) { throw new PreviewExtractionException("Couldn't get text from part"); } String plainText = convertFromHtmlIfNecessary(textPart, text); return stripTextForPreview(plainText); } } | PreviewTextExtractor { @NonNull public String extractPreview(@NonNull Part textPart) throws PreviewExtractionException { String text = MessageExtractor.getTextFromPart(textPart, MAX_CHARACTERS_CHECKED_FOR_PREVIEW); if (text == null) { throw new PreviewExtractionException("Couldn't get text from part"); } String plainText = convertFromHtmlIfNecessary(textPart, text); return stripTextForPreview(plainText); } } | PreviewTextExtractor { @NonNull public String extractPreview(@NonNull Part textPart) throws PreviewExtractionException { String text = MessageExtractor.getTextFromPart(textPart, MAX_CHARACTERS_CHECKED_FOR_PREVIEW); if (text == null) { throw new PreviewExtractionException("Couldn't get text from part"); } String plainText = convertFromHtmlIfNecessary(textPart, text); return stripTextForPreview(plainText); } @NonNull String extractPreview(@NonNull Part textPart); } | PreviewTextExtractor { @NonNull public String extractPreview(@NonNull Part textPart) throws PreviewExtractionException { String text = MessageExtractor.getTextFromPart(textPart, MAX_CHARACTERS_CHECKED_FOR_PREVIEW); if (text == null) { throw new PreviewExtractionException("Couldn't get text from part"); } String plainText = convertFromHtmlIfNecessary(textPart, text); return stripTextForPreview(plainText); } @NonNull String extractPreview(@NonNull Part textPart); } |
@Test public void build__withCryptoProviderUnconfigured__shouldThrow() throws MessagingException { cryptoStatusBuilder.setCryptoMode(CryptoMode.NO_CHOICE); cryptoStatusBuilder.setCryptoProviderState(CryptoProviderState.UNCONFIGURED); pgpMessageBuilder.setCryptoStatus(cryptoStatusBuilder.build()); Callback mockCallback = mock(Callback.class); pgpMessageBuilder.buildAsync(mockCallback); verify(mockCallback).onMessageBuildException(any(MessagingException.class)); verifyNoMoreInteractions(mockCallback); } | public void setCryptoStatus(ComposeCryptoStatus cryptoStatus) { this.cryptoStatus = cryptoStatus; } | PgpMessageBuilder extends MessageBuilder { public void setCryptoStatus(ComposeCryptoStatus cryptoStatus) { this.cryptoStatus = cryptoStatus; } } | PgpMessageBuilder extends MessageBuilder { public void setCryptoStatus(ComposeCryptoStatus cryptoStatus) { this.cryptoStatus = cryptoStatus; } @VisibleForTesting PgpMessageBuilder(Context context, MessageIdGenerator messageIdGenerator, BoundaryGenerator boundaryGenerator,
AutocryptOperations autocryptOperations, AutocryptOpenPgpApiInteractor autocryptOpenPgpApiInteractor); } | PgpMessageBuilder extends MessageBuilder { public void setCryptoStatus(ComposeCryptoStatus cryptoStatus) { this.cryptoStatus = cryptoStatus; } @VisibleForTesting PgpMessageBuilder(Context context, MessageIdGenerator messageIdGenerator, BoundaryGenerator boundaryGenerator,
AutocryptOperations autocryptOperations, AutocryptOpenPgpApiInteractor autocryptOpenPgpApiInteractor); static PgpMessageBuilder newInstance(); void setOpenPgpApi(OpenPgpApi openPgpApi); @Override void buildMessageOnActivityResult(int requestCode, @NonNull Intent userInteractionResult); void setCryptoStatus(ComposeCryptoStatus cryptoStatus); } | PgpMessageBuilder extends MessageBuilder { public void setCryptoStatus(ComposeCryptoStatus cryptoStatus) { this.cryptoStatus = cryptoStatus; } @VisibleForTesting PgpMessageBuilder(Context context, MessageIdGenerator messageIdGenerator, BoundaryGenerator boundaryGenerator,
AutocryptOperations autocryptOperations, AutocryptOpenPgpApiInteractor autocryptOpenPgpApiInteractor); static PgpMessageBuilder newInstance(); void setOpenPgpApi(OpenPgpApi openPgpApi); @Override void buildMessageOnActivityResult(int requestCode, @NonNull Intent userInteractionResult); void setCryptoStatus(ComposeCryptoStatus cryptoStatus); } |
@Test public void build__withCryptoProviderUninitialized__shouldThrow() throws MessagingException { cryptoStatusBuilder.setCryptoMode(CryptoMode.NO_CHOICE); cryptoStatusBuilder.setCryptoProviderState(CryptoProviderState.UNINITIALIZED); pgpMessageBuilder.setCryptoStatus(cryptoStatusBuilder.build()); Callback mockCallback = mock(Callback.class); pgpMessageBuilder.buildAsync(mockCallback); verify(mockCallback).onMessageBuildException(any(MessagingException.class)); verifyNoMoreInteractions(mockCallback); } | public void setCryptoStatus(ComposeCryptoStatus cryptoStatus) { this.cryptoStatus = cryptoStatus; } | PgpMessageBuilder extends MessageBuilder { public void setCryptoStatus(ComposeCryptoStatus cryptoStatus) { this.cryptoStatus = cryptoStatus; } } | PgpMessageBuilder extends MessageBuilder { public void setCryptoStatus(ComposeCryptoStatus cryptoStatus) { this.cryptoStatus = cryptoStatus; } @VisibleForTesting PgpMessageBuilder(Context context, MessageIdGenerator messageIdGenerator, BoundaryGenerator boundaryGenerator,
AutocryptOperations autocryptOperations, AutocryptOpenPgpApiInteractor autocryptOpenPgpApiInteractor); } | PgpMessageBuilder extends MessageBuilder { public void setCryptoStatus(ComposeCryptoStatus cryptoStatus) { this.cryptoStatus = cryptoStatus; } @VisibleForTesting PgpMessageBuilder(Context context, MessageIdGenerator messageIdGenerator, BoundaryGenerator boundaryGenerator,
AutocryptOperations autocryptOperations, AutocryptOpenPgpApiInteractor autocryptOpenPgpApiInteractor); static PgpMessageBuilder newInstance(); void setOpenPgpApi(OpenPgpApi openPgpApi); @Override void buildMessageOnActivityResult(int requestCode, @NonNull Intent userInteractionResult); void setCryptoStatus(ComposeCryptoStatus cryptoStatus); } | PgpMessageBuilder extends MessageBuilder { public void setCryptoStatus(ComposeCryptoStatus cryptoStatus) { this.cryptoStatus = cryptoStatus; } @VisibleForTesting PgpMessageBuilder(Context context, MessageIdGenerator messageIdGenerator, BoundaryGenerator boundaryGenerator,
AutocryptOperations autocryptOperations, AutocryptOpenPgpApiInteractor autocryptOpenPgpApiInteractor); static PgpMessageBuilder newInstance(); void setOpenPgpApi(OpenPgpApi openPgpApi); @Override void buildMessageOnActivityResult(int requestCode, @NonNull Intent userInteractionResult); void setCryptoStatus(ComposeCryptoStatus cryptoStatus); } |
@Test public void build__withCryptoProviderError__shouldThrow() throws MessagingException { cryptoStatusBuilder.setCryptoMode(CryptoMode.NO_CHOICE); cryptoStatusBuilder.setCryptoProviderState(CryptoProviderState.ERROR); pgpMessageBuilder.setCryptoStatus(cryptoStatusBuilder.build()); Callback mockCallback = mock(Callback.class); pgpMessageBuilder.buildAsync(mockCallback); verify(mockCallback).onMessageBuildException(any(MessagingException.class)); verifyNoMoreInteractions(mockCallback); } | public void setCryptoStatus(ComposeCryptoStatus cryptoStatus) { this.cryptoStatus = cryptoStatus; } | PgpMessageBuilder extends MessageBuilder { public void setCryptoStatus(ComposeCryptoStatus cryptoStatus) { this.cryptoStatus = cryptoStatus; } } | PgpMessageBuilder extends MessageBuilder { public void setCryptoStatus(ComposeCryptoStatus cryptoStatus) { this.cryptoStatus = cryptoStatus; } @VisibleForTesting PgpMessageBuilder(Context context, MessageIdGenerator messageIdGenerator, BoundaryGenerator boundaryGenerator,
AutocryptOperations autocryptOperations, AutocryptOpenPgpApiInteractor autocryptOpenPgpApiInteractor); } | PgpMessageBuilder extends MessageBuilder { public void setCryptoStatus(ComposeCryptoStatus cryptoStatus) { this.cryptoStatus = cryptoStatus; } @VisibleForTesting PgpMessageBuilder(Context context, MessageIdGenerator messageIdGenerator, BoundaryGenerator boundaryGenerator,
AutocryptOperations autocryptOperations, AutocryptOpenPgpApiInteractor autocryptOpenPgpApiInteractor); static PgpMessageBuilder newInstance(); void setOpenPgpApi(OpenPgpApi openPgpApi); @Override void buildMessageOnActivityResult(int requestCode, @NonNull Intent userInteractionResult); void setCryptoStatus(ComposeCryptoStatus cryptoStatus); } | PgpMessageBuilder extends MessageBuilder { public void setCryptoStatus(ComposeCryptoStatus cryptoStatus) { this.cryptoStatus = cryptoStatus; } @VisibleForTesting PgpMessageBuilder(Context context, MessageIdGenerator messageIdGenerator, BoundaryGenerator boundaryGenerator,
AutocryptOperations autocryptOperations, AutocryptOpenPgpApiInteractor autocryptOpenPgpApiInteractor); static PgpMessageBuilder newInstance(); void setOpenPgpApi(OpenPgpApi openPgpApi); @Override void buildMessageOnActivityResult(int requestCode, @NonNull Intent userInteractionResult); void setCryptoStatus(ComposeCryptoStatus cryptoStatus); } |
@Test public void build__withCryptoProviderLostConnection__shouldThrow() throws MessagingException { cryptoStatusBuilder.setCryptoMode(CryptoMode.NO_CHOICE); cryptoStatusBuilder.setCryptoProviderState(CryptoProviderState.LOST_CONNECTION); pgpMessageBuilder.setCryptoStatus(cryptoStatusBuilder.build()); Callback mockCallback = mock(Callback.class); pgpMessageBuilder.buildAsync(mockCallback); verify(mockCallback).onMessageBuildException(any(MessagingException.class)); verifyNoMoreInteractions(mockCallback); } | public void setCryptoStatus(ComposeCryptoStatus cryptoStatus) { this.cryptoStatus = cryptoStatus; } | PgpMessageBuilder extends MessageBuilder { public void setCryptoStatus(ComposeCryptoStatus cryptoStatus) { this.cryptoStatus = cryptoStatus; } } | PgpMessageBuilder extends MessageBuilder { public void setCryptoStatus(ComposeCryptoStatus cryptoStatus) { this.cryptoStatus = cryptoStatus; } @VisibleForTesting PgpMessageBuilder(Context context, MessageIdGenerator messageIdGenerator, BoundaryGenerator boundaryGenerator,
AutocryptOperations autocryptOperations, AutocryptOpenPgpApiInteractor autocryptOpenPgpApiInteractor); } | PgpMessageBuilder extends MessageBuilder { public void setCryptoStatus(ComposeCryptoStatus cryptoStatus) { this.cryptoStatus = cryptoStatus; } @VisibleForTesting PgpMessageBuilder(Context context, MessageIdGenerator messageIdGenerator, BoundaryGenerator boundaryGenerator,
AutocryptOperations autocryptOperations, AutocryptOpenPgpApiInteractor autocryptOpenPgpApiInteractor); static PgpMessageBuilder newInstance(); void setOpenPgpApi(OpenPgpApi openPgpApi); @Override void buildMessageOnActivityResult(int requestCode, @NonNull Intent userInteractionResult); void setCryptoStatus(ComposeCryptoStatus cryptoStatus); } | PgpMessageBuilder extends MessageBuilder { public void setCryptoStatus(ComposeCryptoStatus cryptoStatus) { this.cryptoStatus = cryptoStatus; } @VisibleForTesting PgpMessageBuilder(Context context, MessageIdGenerator messageIdGenerator, BoundaryGenerator boundaryGenerator,
AutocryptOperations autocryptOperations, AutocryptOpenPgpApiInteractor autocryptOpenPgpApiInteractor); static PgpMessageBuilder newInstance(); void setOpenPgpApi(OpenPgpApi openPgpApi); @Override void buildMessageOnActivityResult(int requestCode, @NonNull Intent userInteractionResult); void setCryptoStatus(ComposeCryptoStatus cryptoStatus); } |
@Test public void findSigned__withSimpleMultipartSigned__shouldReturnRoot() throws Exception { Message message = messageFromBody( multipart("signed", bodypart("text/plain"), bodypart("application/pgp-signature") ) ); List<Part> signedParts = MessageDecryptVerifier.findSignedParts(message, messageCryptoAnnotations); assertEquals(1, signedParts.size()); assertSame(message, signedParts.get(0)); } | public static List<Part> findSignedParts(Part startPart, MessageCryptoAnnotations messageCryptoAnnotations) { List<Part> signedParts = new ArrayList<>(); Stack<Part> partsToCheck = new Stack<>(); partsToCheck.push(startPart); while (!partsToCheck.isEmpty()) { Part part = partsToCheck.pop(); if (messageCryptoAnnotations.has(part)) { CryptoResultAnnotation resultAnnotation = messageCryptoAnnotations.get(part); MimeBodyPart replacementData = resultAnnotation.getReplacementData(); if (replacementData != null) { part = replacementData; } } Body body = part.getBody(); if (isPartMultipartSigned(part)) { signedParts.add(part); continue; } 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 signedParts; } | MessageDecryptVerifier { public static List<Part> findSignedParts(Part startPart, MessageCryptoAnnotations messageCryptoAnnotations) { List<Part> signedParts = new ArrayList<>(); Stack<Part> partsToCheck = new Stack<>(); partsToCheck.push(startPart); while (!partsToCheck.isEmpty()) { Part part = partsToCheck.pop(); if (messageCryptoAnnotations.has(part)) { CryptoResultAnnotation resultAnnotation = messageCryptoAnnotations.get(part); MimeBodyPart replacementData = resultAnnotation.getReplacementData(); if (replacementData != null) { part = replacementData; } } Body body = part.getBody(); if (isPartMultipartSigned(part)) { signedParts.add(part); continue; } 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 signedParts; } } | MessageDecryptVerifier { public static List<Part> findSignedParts(Part startPart, MessageCryptoAnnotations messageCryptoAnnotations) { List<Part> signedParts = new ArrayList<>(); Stack<Part> partsToCheck = new Stack<>(); partsToCheck.push(startPart); while (!partsToCheck.isEmpty()) { Part part = partsToCheck.pop(); if (messageCryptoAnnotations.has(part)) { CryptoResultAnnotation resultAnnotation = messageCryptoAnnotations.get(part); MimeBodyPart replacementData = resultAnnotation.getReplacementData(); if (replacementData != null) { part = replacementData; } } Body body = part.getBody(); if (isPartMultipartSigned(part)) { signedParts.add(part); continue; } 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 signedParts; } } | MessageDecryptVerifier { public static List<Part> findSignedParts(Part startPart, MessageCryptoAnnotations messageCryptoAnnotations) { List<Part> signedParts = new ArrayList<>(); Stack<Part> partsToCheck = new Stack<>(); partsToCheck.push(startPart); while (!partsToCheck.isEmpty()) { Part part = partsToCheck.pop(); if (messageCryptoAnnotations.has(part)) { CryptoResultAnnotation resultAnnotation = messageCryptoAnnotations.get(part); MimeBodyPart replacementData = resultAnnotation.getReplacementData(); if (replacementData != null) { part = replacementData; } } Body body = part.getBody(); if (isPartMultipartSigned(part)) { signedParts.add(part); continue; } 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 signedParts; } static Part findPrimaryEncryptedOrSignedPart(Part part, List<Part> outputExtraParts); static List<Part> findEncryptedParts(Part startPart); static List<Part> findSignedParts(Part startPart, MessageCryptoAnnotations messageCryptoAnnotations); static List<Part> findPgpInlineParts(Part startPart); static byte[] getSignatureData(Part part); static boolean isPgpMimeEncryptedOrSignedPart(Part part); static boolean isSMimeEncryptedOrSignedPart(Part part); static boolean isPartPgpInlineEncrypted(@Nullable Part part); } | MessageDecryptVerifier { public static List<Part> findSignedParts(Part startPart, MessageCryptoAnnotations messageCryptoAnnotations) { List<Part> signedParts = new ArrayList<>(); Stack<Part> partsToCheck = new Stack<>(); partsToCheck.push(startPart); while (!partsToCheck.isEmpty()) { Part part = partsToCheck.pop(); if (messageCryptoAnnotations.has(part)) { CryptoResultAnnotation resultAnnotation = messageCryptoAnnotations.get(part); MimeBodyPart replacementData = resultAnnotation.getReplacementData(); if (replacementData != null) { part = replacementData; } } Body body = part.getBody(); if (isPartMultipartSigned(part)) { signedParts.add(part); continue; } 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 signedParts; } static Part findPrimaryEncryptedOrSignedPart(Part part, List<Part> outputExtraParts); static List<Part> findEncryptedParts(Part startPart); static List<Part> findSignedParts(Part startPart, MessageCryptoAnnotations messageCryptoAnnotations); static List<Part> findPgpInlineParts(Part startPart); static byte[] getSignatureData(Part part); static boolean isPgpMimeEncryptedOrSignedPart(Part part); static boolean isSMimeEncryptedOrSignedPart(Part part); static boolean isPartPgpInlineEncrypted(@Nullable Part part); } |
@Test public void buildCleartext__withNoSigningKey__shouldBuildTrivialMessage() { cryptoStatusBuilder.setCryptoMode(CryptoMode.NO_CHOICE); cryptoStatusBuilder.setOpenPgpKeyId(null); pgpMessageBuilder.setCryptoStatus(cryptoStatusBuilder.build()); Callback mockCallback = mock(Callback.class); pgpMessageBuilder.buildAsync(mockCallback); ArgumentCaptor<MimeMessage> captor = ArgumentCaptor.forClass(MimeMessage.class); verify(mockCallback).onMessageBuildSuccess(captor.capture(), eq(false)); verifyNoMoreInteractions(mockCallback); MimeMessage message = captor.getValue(); assertEquals("text/plain", message.getMimeType()); } | public void setCryptoStatus(ComposeCryptoStatus cryptoStatus) { this.cryptoStatus = cryptoStatus; } | PgpMessageBuilder extends MessageBuilder { public void setCryptoStatus(ComposeCryptoStatus cryptoStatus) { this.cryptoStatus = cryptoStatus; } } | PgpMessageBuilder extends MessageBuilder { public void setCryptoStatus(ComposeCryptoStatus cryptoStatus) { this.cryptoStatus = cryptoStatus; } @VisibleForTesting PgpMessageBuilder(Context context, MessageIdGenerator messageIdGenerator, BoundaryGenerator boundaryGenerator,
AutocryptOperations autocryptOperations, AutocryptOpenPgpApiInteractor autocryptOpenPgpApiInteractor); } | PgpMessageBuilder extends MessageBuilder { public void setCryptoStatus(ComposeCryptoStatus cryptoStatus) { this.cryptoStatus = cryptoStatus; } @VisibleForTesting PgpMessageBuilder(Context context, MessageIdGenerator messageIdGenerator, BoundaryGenerator boundaryGenerator,
AutocryptOperations autocryptOperations, AutocryptOpenPgpApiInteractor autocryptOpenPgpApiInteractor); static PgpMessageBuilder newInstance(); void setOpenPgpApi(OpenPgpApi openPgpApi); @Override void buildMessageOnActivityResult(int requestCode, @NonNull Intent userInteractionResult); void setCryptoStatus(ComposeCryptoStatus cryptoStatus); } | PgpMessageBuilder extends MessageBuilder { public void setCryptoStatus(ComposeCryptoStatus cryptoStatus) { this.cryptoStatus = cryptoStatus; } @VisibleForTesting PgpMessageBuilder(Context context, MessageIdGenerator messageIdGenerator, BoundaryGenerator boundaryGenerator,
AutocryptOperations autocryptOperations, AutocryptOpenPgpApiInteractor autocryptOpenPgpApiInteractor); static PgpMessageBuilder newInstance(); void setOpenPgpApi(OpenPgpApi openPgpApi); @Override void buildMessageOnActivityResult(int requestCode, @NonNull Intent userInteractionResult); void setCryptoStatus(ComposeCryptoStatus cryptoStatus); } |
@Test public void buildCleartext__shouldSucceed() { cryptoStatusBuilder.setCryptoMode(CryptoMode.NO_CHOICE); pgpMessageBuilder.setCryptoStatus(cryptoStatusBuilder.build()); Callback mockCallback = mock(Callback.class); pgpMessageBuilder.buildAsync(mockCallback); ArgumentCaptor<MimeMessage> captor = ArgumentCaptor.forClass(MimeMessage.class); verify(mockCallback).onMessageBuildSuccess(captor.capture(), eq(false)); verifyNoMoreInteractions(mockCallback); MimeMessage message = captor.getValue(); assertMessageHasAutocryptHeader(message, SENDER_EMAIL, false, AUTOCRYPT_KEY_MATERIAL); } | public void setCryptoStatus(ComposeCryptoStatus cryptoStatus) { this.cryptoStatus = cryptoStatus; } | PgpMessageBuilder extends MessageBuilder { public void setCryptoStatus(ComposeCryptoStatus cryptoStatus) { this.cryptoStatus = cryptoStatus; } } | PgpMessageBuilder extends MessageBuilder { public void setCryptoStatus(ComposeCryptoStatus cryptoStatus) { this.cryptoStatus = cryptoStatus; } @VisibleForTesting PgpMessageBuilder(Context context, MessageIdGenerator messageIdGenerator, BoundaryGenerator boundaryGenerator,
AutocryptOperations autocryptOperations, AutocryptOpenPgpApiInteractor autocryptOpenPgpApiInteractor); } | PgpMessageBuilder extends MessageBuilder { public void setCryptoStatus(ComposeCryptoStatus cryptoStatus) { this.cryptoStatus = cryptoStatus; } @VisibleForTesting PgpMessageBuilder(Context context, MessageIdGenerator messageIdGenerator, BoundaryGenerator boundaryGenerator,
AutocryptOperations autocryptOperations, AutocryptOpenPgpApiInteractor autocryptOpenPgpApiInteractor); static PgpMessageBuilder newInstance(); void setOpenPgpApi(OpenPgpApi openPgpApi); @Override void buildMessageOnActivityResult(int requestCode, @NonNull Intent userInteractionResult); void setCryptoStatus(ComposeCryptoStatus cryptoStatus); } | PgpMessageBuilder extends MessageBuilder { public void setCryptoStatus(ComposeCryptoStatus cryptoStatus) { this.cryptoStatus = cryptoStatus; } @VisibleForTesting PgpMessageBuilder(Context context, MessageIdGenerator messageIdGenerator, BoundaryGenerator boundaryGenerator,
AutocryptOperations autocryptOperations, AutocryptOpenPgpApiInteractor autocryptOpenPgpApiInteractor); static PgpMessageBuilder newInstance(); void setOpenPgpApi(OpenPgpApi openPgpApi); @Override void buildMessageOnActivityResult(int requestCode, @NonNull Intent userInteractionResult); void setCryptoStatus(ComposeCryptoStatus cryptoStatus); } |
@Test public void buildSign__withNoDetachedSignatureInResult__shouldThrow() throws MessagingException { cryptoStatusBuilder.setCryptoMode(CryptoMode.SIGN_ONLY); pgpMessageBuilder.setCryptoStatus(cryptoStatusBuilder.build()); Intent returnIntent = new Intent(); returnIntent.putExtra(OpenPgpApi.RESULT_CODE, OpenPgpApi.RESULT_CODE_SUCCESS); when(openPgpApi.executeApi(any(Intent.class), any(OpenPgpDataSource.class), any(OutputStream.class))) .thenReturn(returnIntent); Callback mockCallback = mock(Callback.class); pgpMessageBuilder.buildAsync(mockCallback); verify(mockCallback).onMessageBuildException(any(MessagingException.class)); verifyNoMoreInteractions(mockCallback); } | public void setCryptoStatus(ComposeCryptoStatus cryptoStatus) { this.cryptoStatus = cryptoStatus; } | PgpMessageBuilder extends MessageBuilder { public void setCryptoStatus(ComposeCryptoStatus cryptoStatus) { this.cryptoStatus = cryptoStatus; } } | PgpMessageBuilder extends MessageBuilder { public void setCryptoStatus(ComposeCryptoStatus cryptoStatus) { this.cryptoStatus = cryptoStatus; } @VisibleForTesting PgpMessageBuilder(Context context, MessageIdGenerator messageIdGenerator, BoundaryGenerator boundaryGenerator,
AutocryptOperations autocryptOperations, AutocryptOpenPgpApiInteractor autocryptOpenPgpApiInteractor); } | PgpMessageBuilder extends MessageBuilder { public void setCryptoStatus(ComposeCryptoStatus cryptoStatus) { this.cryptoStatus = cryptoStatus; } @VisibleForTesting PgpMessageBuilder(Context context, MessageIdGenerator messageIdGenerator, BoundaryGenerator boundaryGenerator,
AutocryptOperations autocryptOperations, AutocryptOpenPgpApiInteractor autocryptOpenPgpApiInteractor); static PgpMessageBuilder newInstance(); void setOpenPgpApi(OpenPgpApi openPgpApi); @Override void buildMessageOnActivityResult(int requestCode, @NonNull Intent userInteractionResult); void setCryptoStatus(ComposeCryptoStatus cryptoStatus); } | PgpMessageBuilder extends MessageBuilder { public void setCryptoStatus(ComposeCryptoStatus cryptoStatus) { this.cryptoStatus = cryptoStatus; } @VisibleForTesting PgpMessageBuilder(Context context, MessageIdGenerator messageIdGenerator, BoundaryGenerator boundaryGenerator,
AutocryptOperations autocryptOperations, AutocryptOpenPgpApiInteractor autocryptOpenPgpApiInteractor); static PgpMessageBuilder newInstance(); void setOpenPgpApi(OpenPgpApi openPgpApi); @Override void buildMessageOnActivityResult(int requestCode, @NonNull Intent userInteractionResult); void setCryptoStatus(ComposeCryptoStatus cryptoStatus); } |
@Test public void buildSign__withDetachedSignatureInResult__shouldSucceed() throws MessagingException { cryptoStatusBuilder.setCryptoMode(CryptoMode.SIGN_ONLY); pgpMessageBuilder.setCryptoStatus(cryptoStatusBuilder.build()); ArgumentCaptor<Intent> capturedApiIntent = ArgumentCaptor.forClass(Intent.class); Intent returnIntent = new Intent(); returnIntent.putExtra(OpenPgpApi.RESULT_CODE, OpenPgpApi.RESULT_CODE_SUCCESS); returnIntent.putExtra(OpenPgpApi.RESULT_DETACHED_SIGNATURE, new byte[] { 1, 2, 3 }); when(openPgpApi.executeApi(capturedApiIntent.capture(), any(OpenPgpDataSource.class), any(OutputStream.class))) .thenReturn(returnIntent); Callback mockCallback = mock(Callback.class); pgpMessageBuilder.buildAsync(mockCallback); Intent expectedIntent = new Intent(OpenPgpApi.ACTION_DETACHED_SIGN); expectedIntent.putExtra(OpenPgpApi.EXTRA_SIGN_KEY_ID, TEST_KEY_ID); expectedIntent.putExtra(OpenPgpApi.EXTRA_REQUEST_ASCII_ARMOR, true); assertIntentEqualsActionAndExtras(expectedIntent, capturedApiIntent.getValue()); ArgumentCaptor<MimeMessage> captor = ArgumentCaptor.forClass(MimeMessage.class); verify(mockCallback).onMessageBuildSuccess(captor.capture(), eq(false)); verifyNoMoreInteractions(mockCallback); MimeMessage message = captor.getValue(); Assert.assertEquals("message must be multipart/signed", "multipart/signed", message.getMimeType()); MimeMultipart multipart = (MimeMultipart) message.getBody(); Assert.assertEquals("multipart/signed must consist of two parts", 2, multipart.getCount()); BodyPart contentBodyPart = multipart.getBodyPart(0); Assert.assertEquals("first part must have content type text/plain", "text/plain", MimeUtility.getHeaderParameter(contentBodyPart.getContentType(), null)); assertTrue("signed message body must be TextBody", contentBodyPart.getBody() instanceof TextBody); Assert.assertEquals(MimeUtil.ENC_QUOTED_PRINTABLE, ((TextBody) contentBodyPart.getBody()).getEncoding()); assertContentOfBodyPartEquals("content must match the message text", contentBodyPart, TEST_MESSAGE_TEXT); BodyPart signatureBodyPart = multipart.getBodyPart(1); String contentType = signatureBodyPart.getContentType(); Assert.assertEquals("second part must be pgp signature", "application/pgp-signature", MimeUtility.getHeaderParameter(contentType, null)); Assert.assertEquals("second part must be called signature.asc", "signature.asc", MimeUtility.getHeaderParameter(contentType, "name")); assertContentOfBodyPartEquals("content must match the supplied detached signature", signatureBodyPart, new byte[] { 1, 2, 3 }); assertMessageHasAutocryptHeader(message, SENDER_EMAIL, false, AUTOCRYPT_KEY_MATERIAL); } | public void setCryptoStatus(ComposeCryptoStatus cryptoStatus) { this.cryptoStatus = cryptoStatus; } | PgpMessageBuilder extends MessageBuilder { public void setCryptoStatus(ComposeCryptoStatus cryptoStatus) { this.cryptoStatus = cryptoStatus; } } | PgpMessageBuilder extends MessageBuilder { public void setCryptoStatus(ComposeCryptoStatus cryptoStatus) { this.cryptoStatus = cryptoStatus; } @VisibleForTesting PgpMessageBuilder(Context context, MessageIdGenerator messageIdGenerator, BoundaryGenerator boundaryGenerator,
AutocryptOperations autocryptOperations, AutocryptOpenPgpApiInteractor autocryptOpenPgpApiInteractor); } | PgpMessageBuilder extends MessageBuilder { public void setCryptoStatus(ComposeCryptoStatus cryptoStatus) { this.cryptoStatus = cryptoStatus; } @VisibleForTesting PgpMessageBuilder(Context context, MessageIdGenerator messageIdGenerator, BoundaryGenerator boundaryGenerator,
AutocryptOperations autocryptOperations, AutocryptOpenPgpApiInteractor autocryptOpenPgpApiInteractor); static PgpMessageBuilder newInstance(); void setOpenPgpApi(OpenPgpApi openPgpApi); @Override void buildMessageOnActivityResult(int requestCode, @NonNull Intent userInteractionResult); void setCryptoStatus(ComposeCryptoStatus cryptoStatus); } | PgpMessageBuilder extends MessageBuilder { public void setCryptoStatus(ComposeCryptoStatus cryptoStatus) { this.cryptoStatus = cryptoStatus; } @VisibleForTesting PgpMessageBuilder(Context context, MessageIdGenerator messageIdGenerator, BoundaryGenerator boundaryGenerator,
AutocryptOperations autocryptOperations, AutocryptOpenPgpApiInteractor autocryptOpenPgpApiInteractor); static PgpMessageBuilder newInstance(); void setOpenPgpApi(OpenPgpApi openPgpApi); @Override void buildMessageOnActivityResult(int requestCode, @NonNull Intent userInteractionResult); void setCryptoStatus(ComposeCryptoStatus cryptoStatus); } |
@Test public void buildSign__withUserInteractionResult__shouldReturnUserInteraction() throws MessagingException { cryptoStatusBuilder.setCryptoMode(CryptoMode.SIGN_ONLY); pgpMessageBuilder.setCryptoStatus(cryptoStatusBuilder.build()); Intent returnIntent = mock(Intent.class); when(returnIntent.getIntExtra(eq(OpenPgpApi.RESULT_CODE), anyInt())) .thenReturn(OpenPgpApi.RESULT_CODE_USER_INTERACTION_REQUIRED); final PendingIntent mockPendingIntent = mock(PendingIntent.class); when(returnIntent.getParcelableExtra(eq(OpenPgpApi.RESULT_INTENT))) .thenReturn(mockPendingIntent); when(openPgpApi.executeApi(any(Intent.class), any(OpenPgpDataSource.class), any(OutputStream.class))) .thenReturn(returnIntent); Callback mockCallback = mock(Callback.class); pgpMessageBuilder.buildAsync(mockCallback); ArgumentCaptor<PendingIntent> captor = ArgumentCaptor.forClass(PendingIntent.class); verify(mockCallback).onMessageBuildReturnPendingIntent(captor.capture(), anyInt()); verifyNoMoreInteractions(mockCallback); PendingIntent pendingIntent = captor.getValue(); Assert.assertSame(pendingIntent, mockPendingIntent); } | public void setCryptoStatus(ComposeCryptoStatus cryptoStatus) { this.cryptoStatus = cryptoStatus; } | PgpMessageBuilder extends MessageBuilder { public void setCryptoStatus(ComposeCryptoStatus cryptoStatus) { this.cryptoStatus = cryptoStatus; } } | PgpMessageBuilder extends MessageBuilder { public void setCryptoStatus(ComposeCryptoStatus cryptoStatus) { this.cryptoStatus = cryptoStatus; } @VisibleForTesting PgpMessageBuilder(Context context, MessageIdGenerator messageIdGenerator, BoundaryGenerator boundaryGenerator,
AutocryptOperations autocryptOperations, AutocryptOpenPgpApiInteractor autocryptOpenPgpApiInteractor); } | PgpMessageBuilder extends MessageBuilder { public void setCryptoStatus(ComposeCryptoStatus cryptoStatus) { this.cryptoStatus = cryptoStatus; } @VisibleForTesting PgpMessageBuilder(Context context, MessageIdGenerator messageIdGenerator, BoundaryGenerator boundaryGenerator,
AutocryptOperations autocryptOperations, AutocryptOpenPgpApiInteractor autocryptOpenPgpApiInteractor); static PgpMessageBuilder newInstance(); void setOpenPgpApi(OpenPgpApi openPgpApi); @Override void buildMessageOnActivityResult(int requestCode, @NonNull Intent userInteractionResult); void setCryptoStatus(ComposeCryptoStatus cryptoStatus); } | PgpMessageBuilder extends MessageBuilder { public void setCryptoStatus(ComposeCryptoStatus cryptoStatus) { this.cryptoStatus = cryptoStatus; } @VisibleForTesting PgpMessageBuilder(Context context, MessageIdGenerator messageIdGenerator, BoundaryGenerator boundaryGenerator,
AutocryptOperations autocryptOperations, AutocryptOpenPgpApiInteractor autocryptOpenPgpApiInteractor); static PgpMessageBuilder newInstance(); void setOpenPgpApi(OpenPgpApi openPgpApi); @Override void buildMessageOnActivityResult(int requestCode, @NonNull Intent userInteractionResult); void setCryptoStatus(ComposeCryptoStatus cryptoStatus); } |
@Test public void buildSign__withReturnAfterUserInteraction__shouldSucceed() throws MessagingException { cryptoStatusBuilder.setCryptoMode(CryptoMode.SIGN_ONLY); pgpMessageBuilder.setCryptoStatus(cryptoStatusBuilder.build()); int returnedRequestCode; { Intent returnIntent = spy(new Intent()); returnIntent.putExtra(OpenPgpApi.RESULT_CODE, OpenPgpApi.RESULT_CODE_USER_INTERACTION_REQUIRED); PendingIntent mockPendingIntent = mock(PendingIntent.class); when(returnIntent.getParcelableExtra(eq(OpenPgpApi.RESULT_INTENT))) .thenReturn(mockPendingIntent); when(openPgpApi.executeApi(any(Intent.class), any(OpenPgpDataSource.class), any(OutputStream.class))) .thenReturn(returnIntent); Callback mockCallback = mock(Callback.class); pgpMessageBuilder.buildAsync(mockCallback); verify(returnIntent).getIntExtra(eq(OpenPgpApi.RESULT_CODE), anyInt()); ArgumentCaptor<PendingIntent> piCaptor = ArgumentCaptor.forClass(PendingIntent.class); ArgumentCaptor<Integer> rcCaptor = ArgumentCaptor.forClass(Integer.class); verify(mockCallback).onMessageBuildReturnPendingIntent(piCaptor.capture(), rcCaptor.capture()); verifyNoMoreInteractions(mockCallback); returnedRequestCode = rcCaptor.getValue(); Assert.assertSame(mockPendingIntent, piCaptor.getValue()); } { Intent returnIntent = spy(new Intent()); returnIntent.putExtra(OpenPgpApi.RESULT_CODE, OpenPgpApi.RESULT_CODE_SUCCESS); Intent mockReturnIntent = mock(Intent.class); when(openPgpApi.executeApi(same(mockReturnIntent), any(OpenPgpDataSource.class), any(OutputStream.class))) .thenReturn(returnIntent); Callback mockCallback = mock(Callback.class); pgpMessageBuilder.onActivityResult(returnedRequestCode, Activity.RESULT_OK, mockReturnIntent, mockCallback); verify(openPgpApi).executeApi(same(mockReturnIntent), any(OpenPgpDataSource.class), any(OutputStream.class)); verify(returnIntent).getIntExtra(eq(OpenPgpApi.RESULT_CODE), anyInt()); } } | public void setCryptoStatus(ComposeCryptoStatus cryptoStatus) { this.cryptoStatus = cryptoStatus; } | PgpMessageBuilder extends MessageBuilder { public void setCryptoStatus(ComposeCryptoStatus cryptoStatus) { this.cryptoStatus = cryptoStatus; } } | PgpMessageBuilder extends MessageBuilder { public void setCryptoStatus(ComposeCryptoStatus cryptoStatus) { this.cryptoStatus = cryptoStatus; } @VisibleForTesting PgpMessageBuilder(Context context, MessageIdGenerator messageIdGenerator, BoundaryGenerator boundaryGenerator,
AutocryptOperations autocryptOperations, AutocryptOpenPgpApiInteractor autocryptOpenPgpApiInteractor); } | PgpMessageBuilder extends MessageBuilder { public void setCryptoStatus(ComposeCryptoStatus cryptoStatus) { this.cryptoStatus = cryptoStatus; } @VisibleForTesting PgpMessageBuilder(Context context, MessageIdGenerator messageIdGenerator, BoundaryGenerator boundaryGenerator,
AutocryptOperations autocryptOperations, AutocryptOpenPgpApiInteractor autocryptOpenPgpApiInteractor); static PgpMessageBuilder newInstance(); void setOpenPgpApi(OpenPgpApi openPgpApi); @Override void buildMessageOnActivityResult(int requestCode, @NonNull Intent userInteractionResult); void setCryptoStatus(ComposeCryptoStatus cryptoStatus); } | PgpMessageBuilder extends MessageBuilder { public void setCryptoStatus(ComposeCryptoStatus cryptoStatus) { this.cryptoStatus = cryptoStatus; } @VisibleForTesting PgpMessageBuilder(Context context, MessageIdGenerator messageIdGenerator, BoundaryGenerator boundaryGenerator,
AutocryptOperations autocryptOperations, AutocryptOpenPgpApiInteractor autocryptOpenPgpApiInteractor); static PgpMessageBuilder newInstance(); void setOpenPgpApi(OpenPgpApi openPgpApi); @Override void buildMessageOnActivityResult(int requestCode, @NonNull Intent userInteractionResult); void setCryptoStatus(ComposeCryptoStatus cryptoStatus); } |
@Test public void buildEncrypt__withoutRecipients__shouldThrow() throws MessagingException { cryptoStatusBuilder .setCryptoMode(CryptoMode.CHOICE_ENABLED) .setRecipients(new ArrayList<Recipient>()); pgpMessageBuilder.setCryptoStatus(cryptoStatusBuilder.build()); Intent returnIntent = spy(new Intent()); returnIntent.putExtra(OpenPgpApi.RESULT_CODE, OpenPgpApi.RESULT_CODE_SUCCESS); when(openPgpApi.executeApi(any(Intent.class), any(OpenPgpDataSource.class), any(OutputStream.class))) .thenReturn(returnIntent); Callback mockCallback = mock(Callback.class); pgpMessageBuilder.buildAsync(mockCallback); verify(mockCallback).onMessageBuildException(any(MessagingException.class)); verifyNoMoreInteractions(mockCallback); } | public void setCryptoStatus(ComposeCryptoStatus cryptoStatus) { this.cryptoStatus = cryptoStatus; } | PgpMessageBuilder extends MessageBuilder { public void setCryptoStatus(ComposeCryptoStatus cryptoStatus) { this.cryptoStatus = cryptoStatus; } } | PgpMessageBuilder extends MessageBuilder { public void setCryptoStatus(ComposeCryptoStatus cryptoStatus) { this.cryptoStatus = cryptoStatus; } @VisibleForTesting PgpMessageBuilder(Context context, MessageIdGenerator messageIdGenerator, BoundaryGenerator boundaryGenerator,
AutocryptOperations autocryptOperations, AutocryptOpenPgpApiInteractor autocryptOpenPgpApiInteractor); } | PgpMessageBuilder extends MessageBuilder { public void setCryptoStatus(ComposeCryptoStatus cryptoStatus) { this.cryptoStatus = cryptoStatus; } @VisibleForTesting PgpMessageBuilder(Context context, MessageIdGenerator messageIdGenerator, BoundaryGenerator boundaryGenerator,
AutocryptOperations autocryptOperations, AutocryptOpenPgpApiInteractor autocryptOpenPgpApiInteractor); static PgpMessageBuilder newInstance(); void setOpenPgpApi(OpenPgpApi openPgpApi); @Override void buildMessageOnActivityResult(int requestCode, @NonNull Intent userInteractionResult); void setCryptoStatus(ComposeCryptoStatus cryptoStatus); } | PgpMessageBuilder extends MessageBuilder { public void setCryptoStatus(ComposeCryptoStatus cryptoStatus) { this.cryptoStatus = cryptoStatus; } @VisibleForTesting PgpMessageBuilder(Context context, MessageIdGenerator messageIdGenerator, BoundaryGenerator boundaryGenerator,
AutocryptOperations autocryptOperations, AutocryptOpenPgpApiInteractor autocryptOpenPgpApiInteractor); static PgpMessageBuilder newInstance(); void setOpenPgpApi(OpenPgpApi openPgpApi); @Override void buildMessageOnActivityResult(int requestCode, @NonNull Intent userInteractionResult); void setCryptoStatus(ComposeCryptoStatus cryptoStatus); } |
@Test public void buildEncrypt__shouldSucceed() throws MessagingException { ComposeCryptoStatus cryptoStatus = cryptoStatusBuilder .setCryptoMode(CryptoMode.CHOICE_ENABLED) .setRecipients(Collections.singletonList(new Recipient("test", "[email protected]", "labru", -1, "key"))) .build(); pgpMessageBuilder.setCryptoStatus(cryptoStatus); ArgumentCaptor<Intent> capturedApiIntent = ArgumentCaptor.forClass(Intent.class); Intent returnIntent = new Intent(); returnIntent.putExtra(OpenPgpApi.RESULT_CODE, OpenPgpApi.RESULT_CODE_SUCCESS); when(openPgpApi.executeApi(capturedApiIntent.capture(), any(OpenPgpDataSource.class), any(OutputStream.class))).thenReturn(returnIntent); Callback mockCallback = mock(Callback.class); pgpMessageBuilder.buildAsync(mockCallback); Intent expectedApiIntent = new Intent(OpenPgpApi.ACTION_SIGN_AND_ENCRYPT); expectedApiIntent.putExtra(OpenPgpApi.EXTRA_SIGN_KEY_ID, TEST_KEY_ID); expectedApiIntent.putExtra(OpenPgpApi.EXTRA_KEY_IDS, new long[] { TEST_KEY_ID }); expectedApiIntent.putExtra(OpenPgpApi.EXTRA_REQUEST_ASCII_ARMOR, true); expectedApiIntent.putExtra(OpenPgpApi.EXTRA_USER_IDS, cryptoStatus.getRecipientAddresses()); assertIntentEqualsActionAndExtras(expectedApiIntent, capturedApiIntent.getValue()); ArgumentCaptor<MimeMessage> captor = ArgumentCaptor.forClass(MimeMessage.class); verify(mockCallback).onMessageBuildSuccess(captor.capture(), eq(false)); verifyNoMoreInteractions(mockCallback); MimeMessage message = captor.getValue(); Assert.assertEquals("message must be multipart/encrypted", "multipart/encrypted", message.getMimeType()); MimeMultipart multipart = (MimeMultipart) message.getBody(); Assert.assertEquals("multipart/encrypted must consist of two parts", 2, multipart.getCount()); BodyPart dummyBodyPart = multipart.getBodyPart(0); Assert.assertEquals("first part must be pgp encrypted dummy part", "application/pgp-encrypted", dummyBodyPart.getContentType()); assertContentOfBodyPartEquals("content must match the supplied detached signature", dummyBodyPart, "Version: 1"); BodyPart encryptedBodyPart = multipart.getBodyPart(1); Assert.assertEquals("second part must be octet-stream of encrypted data", "application/octet-stream; name=\"encrypted.asc\"", encryptedBodyPart.getContentType()); assertTrue("message body must be BinaryTempFileBody", encryptedBodyPart.getBody() instanceof BinaryTempFileBody); Assert.assertEquals(MimeUtil.ENC_7BIT, ((BinaryTempFileBody) encryptedBodyPart.getBody()).getEncoding()); assertMessageHasAutocryptHeader(message, SENDER_EMAIL, false, AUTOCRYPT_KEY_MATERIAL); } | public void setCryptoStatus(ComposeCryptoStatus cryptoStatus) { this.cryptoStatus = cryptoStatus; } | PgpMessageBuilder extends MessageBuilder { public void setCryptoStatus(ComposeCryptoStatus cryptoStatus) { this.cryptoStatus = cryptoStatus; } } | PgpMessageBuilder extends MessageBuilder { public void setCryptoStatus(ComposeCryptoStatus cryptoStatus) { this.cryptoStatus = cryptoStatus; } @VisibleForTesting PgpMessageBuilder(Context context, MessageIdGenerator messageIdGenerator, BoundaryGenerator boundaryGenerator,
AutocryptOperations autocryptOperations, AutocryptOpenPgpApiInteractor autocryptOpenPgpApiInteractor); } | PgpMessageBuilder extends MessageBuilder { public void setCryptoStatus(ComposeCryptoStatus cryptoStatus) { this.cryptoStatus = cryptoStatus; } @VisibleForTesting PgpMessageBuilder(Context context, MessageIdGenerator messageIdGenerator, BoundaryGenerator boundaryGenerator,
AutocryptOperations autocryptOperations, AutocryptOpenPgpApiInteractor autocryptOpenPgpApiInteractor); static PgpMessageBuilder newInstance(); void setOpenPgpApi(OpenPgpApi openPgpApi); @Override void buildMessageOnActivityResult(int requestCode, @NonNull Intent userInteractionResult); void setCryptoStatus(ComposeCryptoStatus cryptoStatus); } | PgpMessageBuilder extends MessageBuilder { public void setCryptoStatus(ComposeCryptoStatus cryptoStatus) { this.cryptoStatus = cryptoStatus; } @VisibleForTesting PgpMessageBuilder(Context context, MessageIdGenerator messageIdGenerator, BoundaryGenerator boundaryGenerator,
AutocryptOperations autocryptOperations, AutocryptOpenPgpApiInteractor autocryptOpenPgpApiInteractor); static PgpMessageBuilder newInstance(); void setOpenPgpApi(OpenPgpApi openPgpApi); @Override void buildMessageOnActivityResult(int requestCode, @NonNull Intent userInteractionResult); void setCryptoStatus(ComposeCryptoStatus cryptoStatus); } |
@Test public void buildEncrypt__withInlineEnabled__shouldSucceed() throws MessagingException { ComposeCryptoStatus cryptoStatus = cryptoStatusBuilder .setCryptoMode(CryptoMode.CHOICE_ENABLED) .setRecipients(Collections.singletonList(new Recipient("test", "[email protected]", "labru", -1, "key"))) .setEnablePgpInline(true) .build(); pgpMessageBuilder.setCryptoStatus(cryptoStatus); ArgumentCaptor<Intent> capturedApiIntent = ArgumentCaptor.forClass(Intent.class); Intent returnIntent = new Intent(); returnIntent.putExtra(OpenPgpApi.RESULT_CODE, OpenPgpApi.RESULT_CODE_SUCCESS); when(openPgpApi.executeApi(capturedApiIntent.capture(), any(OpenPgpDataSource.class), any(OutputStream.class))) .thenReturn(returnIntent); Callback mockCallback = mock(Callback.class); pgpMessageBuilder.buildAsync(mockCallback); Intent expectedApiIntent = new Intent(OpenPgpApi.ACTION_SIGN_AND_ENCRYPT); expectedApiIntent.putExtra(OpenPgpApi.EXTRA_SIGN_KEY_ID, TEST_KEY_ID); expectedApiIntent.putExtra(OpenPgpApi.EXTRA_KEY_IDS, new long[] { TEST_KEY_ID }); expectedApiIntent.putExtra(OpenPgpApi.EXTRA_REQUEST_ASCII_ARMOR, true); expectedApiIntent.putExtra(OpenPgpApi.EXTRA_USER_IDS, cryptoStatus.getRecipientAddresses()); assertIntentEqualsActionAndExtras(expectedApiIntent, capturedApiIntent.getValue()); ArgumentCaptor<MimeMessage> captor = ArgumentCaptor.forClass(MimeMessage.class); verify(mockCallback).onMessageBuildSuccess(captor.capture(), eq(false)); verifyNoMoreInteractions(mockCallback); MimeMessage message = captor.getValue(); Assert.assertEquals("text/plain", message.getMimeType()); assertTrue("message body must be BinaryTempFileBody", message.getBody() instanceof BinaryTempFileBody); Assert.assertEquals(MimeUtil.ENC_7BIT, ((BinaryTempFileBody) message.getBody()).getEncoding()); assertMessageHasAutocryptHeader(message, SENDER_EMAIL, false, AUTOCRYPT_KEY_MATERIAL); } | public void setCryptoStatus(ComposeCryptoStatus cryptoStatus) { this.cryptoStatus = cryptoStatus; } | PgpMessageBuilder extends MessageBuilder { public void setCryptoStatus(ComposeCryptoStatus cryptoStatus) { this.cryptoStatus = cryptoStatus; } } | PgpMessageBuilder extends MessageBuilder { public void setCryptoStatus(ComposeCryptoStatus cryptoStatus) { this.cryptoStatus = cryptoStatus; } @VisibleForTesting PgpMessageBuilder(Context context, MessageIdGenerator messageIdGenerator, BoundaryGenerator boundaryGenerator,
AutocryptOperations autocryptOperations, AutocryptOpenPgpApiInteractor autocryptOpenPgpApiInteractor); } | PgpMessageBuilder extends MessageBuilder { public void setCryptoStatus(ComposeCryptoStatus cryptoStatus) { this.cryptoStatus = cryptoStatus; } @VisibleForTesting PgpMessageBuilder(Context context, MessageIdGenerator messageIdGenerator, BoundaryGenerator boundaryGenerator,
AutocryptOperations autocryptOperations, AutocryptOpenPgpApiInteractor autocryptOpenPgpApiInteractor); static PgpMessageBuilder newInstance(); void setOpenPgpApi(OpenPgpApi openPgpApi); @Override void buildMessageOnActivityResult(int requestCode, @NonNull Intent userInteractionResult); void setCryptoStatus(ComposeCryptoStatus cryptoStatus); } | PgpMessageBuilder extends MessageBuilder { public void setCryptoStatus(ComposeCryptoStatus cryptoStatus) { this.cryptoStatus = cryptoStatus; } @VisibleForTesting PgpMessageBuilder(Context context, MessageIdGenerator messageIdGenerator, BoundaryGenerator boundaryGenerator,
AutocryptOperations autocryptOperations, AutocryptOpenPgpApiInteractor autocryptOpenPgpApiInteractor); static PgpMessageBuilder newInstance(); void setOpenPgpApi(OpenPgpApi openPgpApi); @Override void buildMessageOnActivityResult(int requestCode, @NonNull Intent userInteractionResult); void setCryptoStatus(ComposeCryptoStatus cryptoStatus); } |
@Test public void buildSign__withInlineEnabled__shouldSucceed() throws MessagingException { ComposeCryptoStatus cryptoStatus = cryptoStatusBuilder .setCryptoMode(CryptoMode.SIGN_ONLY) .setRecipients(Collections.singletonList(new Recipient("test", "[email protected]", "labru", -1, "key"))) .setEnablePgpInline(true) .build(); pgpMessageBuilder.setCryptoStatus(cryptoStatus); ArgumentCaptor<Intent> capturedApiIntent = ArgumentCaptor.forClass(Intent.class); Intent returnIntent = new Intent(); returnIntent.putExtra(OpenPgpApi.RESULT_CODE, OpenPgpApi.RESULT_CODE_SUCCESS); when(openPgpApi.executeApi(capturedApiIntent.capture(), any(OpenPgpDataSource.class), any(OutputStream.class))) .thenReturn(returnIntent); Callback mockCallback = mock(Callback.class); pgpMessageBuilder.buildAsync(mockCallback); Intent expectedApiIntent = new Intent(OpenPgpApi.ACTION_SIGN); expectedApiIntent.putExtra(OpenPgpApi.EXTRA_SIGN_KEY_ID, TEST_KEY_ID); expectedApiIntent.putExtra(OpenPgpApi.EXTRA_REQUEST_ASCII_ARMOR, true); assertIntentEqualsActionAndExtras(expectedApiIntent, capturedApiIntent.getValue()); ArgumentCaptor<MimeMessage> captor = ArgumentCaptor.forClass(MimeMessage.class); verify(mockCallback).onMessageBuildSuccess(captor.capture(), eq(false)); verifyNoMoreInteractions(mockCallback); MimeMessage message = captor.getValue(); Assert.assertEquals("message must be text/plain", "text/plain", message.getMimeType()); assertMessageHasAutocryptHeader(message, SENDER_EMAIL, false, AUTOCRYPT_KEY_MATERIAL); } | public void setCryptoStatus(ComposeCryptoStatus cryptoStatus) { this.cryptoStatus = cryptoStatus; } | PgpMessageBuilder extends MessageBuilder { public void setCryptoStatus(ComposeCryptoStatus cryptoStatus) { this.cryptoStatus = cryptoStatus; } } | PgpMessageBuilder extends MessageBuilder { public void setCryptoStatus(ComposeCryptoStatus cryptoStatus) { this.cryptoStatus = cryptoStatus; } @VisibleForTesting PgpMessageBuilder(Context context, MessageIdGenerator messageIdGenerator, BoundaryGenerator boundaryGenerator,
AutocryptOperations autocryptOperations, AutocryptOpenPgpApiInteractor autocryptOpenPgpApiInteractor); } | PgpMessageBuilder extends MessageBuilder { public void setCryptoStatus(ComposeCryptoStatus cryptoStatus) { this.cryptoStatus = cryptoStatus; } @VisibleForTesting PgpMessageBuilder(Context context, MessageIdGenerator messageIdGenerator, BoundaryGenerator boundaryGenerator,
AutocryptOperations autocryptOperations, AutocryptOpenPgpApiInteractor autocryptOpenPgpApiInteractor); static PgpMessageBuilder newInstance(); void setOpenPgpApi(OpenPgpApi openPgpApi); @Override void buildMessageOnActivityResult(int requestCode, @NonNull Intent userInteractionResult); void setCryptoStatus(ComposeCryptoStatus cryptoStatus); } | PgpMessageBuilder extends MessageBuilder { public void setCryptoStatus(ComposeCryptoStatus cryptoStatus) { this.cryptoStatus = cryptoStatus; } @VisibleForTesting PgpMessageBuilder(Context context, MessageIdGenerator messageIdGenerator, BoundaryGenerator boundaryGenerator,
AutocryptOperations autocryptOperations, AutocryptOpenPgpApiInteractor autocryptOpenPgpApiInteractor); static PgpMessageBuilder newInstance(); void setOpenPgpApi(OpenPgpApi openPgpApi); @Override void buildMessageOnActivityResult(int requestCode, @NonNull Intent userInteractionResult); void setCryptoStatus(ComposeCryptoStatus cryptoStatus); } |
@Test public void findSigned__withComplexMultipartSigned__shouldReturnRoot() throws Exception { Message message = messageFromBody( multipart("signed", multipart("mixed", bodypart("text/plain"), bodypart("application/pdf") ), bodypart("application/pgp-signature") ) ); List<Part> signedParts = MessageDecryptVerifier.findSignedParts(message, messageCryptoAnnotations); assertEquals(1, signedParts.size()); assertSame(message, signedParts.get(0)); } | public static List<Part> findSignedParts(Part startPart, MessageCryptoAnnotations messageCryptoAnnotations) { List<Part> signedParts = new ArrayList<>(); Stack<Part> partsToCheck = new Stack<>(); partsToCheck.push(startPart); while (!partsToCheck.isEmpty()) { Part part = partsToCheck.pop(); if (messageCryptoAnnotations.has(part)) { CryptoResultAnnotation resultAnnotation = messageCryptoAnnotations.get(part); MimeBodyPart replacementData = resultAnnotation.getReplacementData(); if (replacementData != null) { part = replacementData; } } Body body = part.getBody(); if (isPartMultipartSigned(part)) { signedParts.add(part); continue; } 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 signedParts; } | MessageDecryptVerifier { public static List<Part> findSignedParts(Part startPart, MessageCryptoAnnotations messageCryptoAnnotations) { List<Part> signedParts = new ArrayList<>(); Stack<Part> partsToCheck = new Stack<>(); partsToCheck.push(startPart); while (!partsToCheck.isEmpty()) { Part part = partsToCheck.pop(); if (messageCryptoAnnotations.has(part)) { CryptoResultAnnotation resultAnnotation = messageCryptoAnnotations.get(part); MimeBodyPart replacementData = resultAnnotation.getReplacementData(); if (replacementData != null) { part = replacementData; } } Body body = part.getBody(); if (isPartMultipartSigned(part)) { signedParts.add(part); continue; } 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 signedParts; } } | MessageDecryptVerifier { public static List<Part> findSignedParts(Part startPart, MessageCryptoAnnotations messageCryptoAnnotations) { List<Part> signedParts = new ArrayList<>(); Stack<Part> partsToCheck = new Stack<>(); partsToCheck.push(startPart); while (!partsToCheck.isEmpty()) { Part part = partsToCheck.pop(); if (messageCryptoAnnotations.has(part)) { CryptoResultAnnotation resultAnnotation = messageCryptoAnnotations.get(part); MimeBodyPart replacementData = resultAnnotation.getReplacementData(); if (replacementData != null) { part = replacementData; } } Body body = part.getBody(); if (isPartMultipartSigned(part)) { signedParts.add(part); continue; } 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 signedParts; } } | MessageDecryptVerifier { public static List<Part> findSignedParts(Part startPart, MessageCryptoAnnotations messageCryptoAnnotations) { List<Part> signedParts = new ArrayList<>(); Stack<Part> partsToCheck = new Stack<>(); partsToCheck.push(startPart); while (!partsToCheck.isEmpty()) { Part part = partsToCheck.pop(); if (messageCryptoAnnotations.has(part)) { CryptoResultAnnotation resultAnnotation = messageCryptoAnnotations.get(part); MimeBodyPart replacementData = resultAnnotation.getReplacementData(); if (replacementData != null) { part = replacementData; } } Body body = part.getBody(); if (isPartMultipartSigned(part)) { signedParts.add(part); continue; } 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 signedParts; } static Part findPrimaryEncryptedOrSignedPart(Part part, List<Part> outputExtraParts); static List<Part> findEncryptedParts(Part startPart); static List<Part> findSignedParts(Part startPart, MessageCryptoAnnotations messageCryptoAnnotations); static List<Part> findPgpInlineParts(Part startPart); static byte[] getSignatureData(Part part); static boolean isPgpMimeEncryptedOrSignedPart(Part part); static boolean isSMimeEncryptedOrSignedPart(Part part); static boolean isPartPgpInlineEncrypted(@Nullable Part part); } | MessageDecryptVerifier { public static List<Part> findSignedParts(Part startPart, MessageCryptoAnnotations messageCryptoAnnotations) { List<Part> signedParts = new ArrayList<>(); Stack<Part> partsToCheck = new Stack<>(); partsToCheck.push(startPart); while (!partsToCheck.isEmpty()) { Part part = partsToCheck.pop(); if (messageCryptoAnnotations.has(part)) { CryptoResultAnnotation resultAnnotation = messageCryptoAnnotations.get(part); MimeBodyPart replacementData = resultAnnotation.getReplacementData(); if (replacementData != null) { part = replacementData; } } Body body = part.getBody(); if (isPartMultipartSigned(part)) { signedParts.add(part); continue; } 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 signedParts; } static Part findPrimaryEncryptedOrSignedPart(Part part, List<Part> outputExtraParts); static List<Part> findEncryptedParts(Part startPart); static List<Part> findSignedParts(Part startPart, MessageCryptoAnnotations messageCryptoAnnotations); static List<Part> findPgpInlineParts(Part startPart); static byte[] getSignatureData(Part part); static boolean isPgpMimeEncryptedOrSignedPart(Part part); static boolean isSMimeEncryptedOrSignedPart(Part part); static boolean isPartPgpInlineEncrypted(@Nullable Part part); } |
@Test public void buildSignWithAttach__withInlineEnabled__shouldThrow() throws MessagingException { ComposeCryptoStatus cryptoStatus = cryptoStatusBuilder .setCryptoMode(CryptoMode.SIGN_ONLY) .setEnablePgpInline(true) .build(); pgpMessageBuilder.setCryptoStatus(cryptoStatus); pgpMessageBuilder.setAttachments(Collections.singletonList(Attachment.createAttachment(null, 0, null))); Callback mockCallback = mock(Callback.class); pgpMessageBuilder.buildAsync(mockCallback); verify(mockCallback).onMessageBuildException(any(MessagingException.class)); verifyNoMoreInteractions(mockCallback); verifyNoMoreInteractions(openPgpApi); } | public void setCryptoStatus(ComposeCryptoStatus cryptoStatus) { this.cryptoStatus = cryptoStatus; } | PgpMessageBuilder extends MessageBuilder { public void setCryptoStatus(ComposeCryptoStatus cryptoStatus) { this.cryptoStatus = cryptoStatus; } } | PgpMessageBuilder extends MessageBuilder { public void setCryptoStatus(ComposeCryptoStatus cryptoStatus) { this.cryptoStatus = cryptoStatus; } @VisibleForTesting PgpMessageBuilder(Context context, MessageIdGenerator messageIdGenerator, BoundaryGenerator boundaryGenerator,
AutocryptOperations autocryptOperations, AutocryptOpenPgpApiInteractor autocryptOpenPgpApiInteractor); } | PgpMessageBuilder extends MessageBuilder { public void setCryptoStatus(ComposeCryptoStatus cryptoStatus) { this.cryptoStatus = cryptoStatus; } @VisibleForTesting PgpMessageBuilder(Context context, MessageIdGenerator messageIdGenerator, BoundaryGenerator boundaryGenerator,
AutocryptOperations autocryptOperations, AutocryptOpenPgpApiInteractor autocryptOpenPgpApiInteractor); static PgpMessageBuilder newInstance(); void setOpenPgpApi(OpenPgpApi openPgpApi); @Override void buildMessageOnActivityResult(int requestCode, @NonNull Intent userInteractionResult); void setCryptoStatus(ComposeCryptoStatus cryptoStatus); } | PgpMessageBuilder extends MessageBuilder { public void setCryptoStatus(ComposeCryptoStatus cryptoStatus) { this.cryptoStatus = cryptoStatus; } @VisibleForTesting PgpMessageBuilder(Context context, MessageIdGenerator messageIdGenerator, BoundaryGenerator boundaryGenerator,
AutocryptOperations autocryptOperations, AutocryptOpenPgpApiInteractor autocryptOpenPgpApiInteractor); static PgpMessageBuilder newInstance(); void setOpenPgpApi(OpenPgpApi openPgpApi); @Override void buildMessageOnActivityResult(int requestCode, @NonNull Intent userInteractionResult); void setCryptoStatus(ComposeCryptoStatus cryptoStatus); } |
@Test public void buildEncryptWithAttach__withInlineEnabled__shouldThrow() throws MessagingException { ComposeCryptoStatus cryptoStatus = cryptoStatusBuilder .setCryptoMode(CryptoMode.CHOICE_ENABLED) .setEnablePgpInline(true) .build(); pgpMessageBuilder.setCryptoStatus(cryptoStatus); pgpMessageBuilder.setAttachments(Collections.singletonList(Attachment.createAttachment(null, 0, null))); Callback mockCallback = mock(Callback.class); pgpMessageBuilder.buildAsync(mockCallback); verify(mockCallback).onMessageBuildException(any(MessagingException.class)); verifyNoMoreInteractions(mockCallback); verifyNoMoreInteractions(openPgpApi); } | public void setCryptoStatus(ComposeCryptoStatus cryptoStatus) { this.cryptoStatus = cryptoStatus; } | PgpMessageBuilder extends MessageBuilder { public void setCryptoStatus(ComposeCryptoStatus cryptoStatus) { this.cryptoStatus = cryptoStatus; } } | PgpMessageBuilder extends MessageBuilder { public void setCryptoStatus(ComposeCryptoStatus cryptoStatus) { this.cryptoStatus = cryptoStatus; } @VisibleForTesting PgpMessageBuilder(Context context, MessageIdGenerator messageIdGenerator, BoundaryGenerator boundaryGenerator,
AutocryptOperations autocryptOperations, AutocryptOpenPgpApiInteractor autocryptOpenPgpApiInteractor); } | PgpMessageBuilder extends MessageBuilder { public void setCryptoStatus(ComposeCryptoStatus cryptoStatus) { this.cryptoStatus = cryptoStatus; } @VisibleForTesting PgpMessageBuilder(Context context, MessageIdGenerator messageIdGenerator, BoundaryGenerator boundaryGenerator,
AutocryptOperations autocryptOperations, AutocryptOpenPgpApiInteractor autocryptOpenPgpApiInteractor); static PgpMessageBuilder newInstance(); void setOpenPgpApi(OpenPgpApi openPgpApi); @Override void buildMessageOnActivityResult(int requestCode, @NonNull Intent userInteractionResult); void setCryptoStatus(ComposeCryptoStatus cryptoStatus); } | PgpMessageBuilder extends MessageBuilder { public void setCryptoStatus(ComposeCryptoStatus cryptoStatus) { this.cryptoStatus = cryptoStatus; } @VisibleForTesting PgpMessageBuilder(Context context, MessageIdGenerator messageIdGenerator, BoundaryGenerator boundaryGenerator,
AutocryptOperations autocryptOperations, AutocryptOpenPgpApiInteractor autocryptOpenPgpApiInteractor); static PgpMessageBuilder newInstance(); void setOpenPgpApi(OpenPgpApi openPgpApi); @Override void buildMessageOnActivityResult(int requestCode, @NonNull Intent userInteractionResult); void setCryptoStatus(ComposeCryptoStatus cryptoStatus); } |
@Test public void buildOpportunisticEncrypt__withNoKeysAndNoSignOnly__shouldNotBeSigned() throws MessagingException { ComposeCryptoStatus cryptoStatus = cryptoStatusBuilder .setRecipients(Collections.singletonList(new Recipient("test", "[email protected]", "labru", -1, "key"))) .setCryptoMode(CryptoMode.NO_CHOICE) .build(); pgpMessageBuilder.setCryptoStatus(cryptoStatus); Intent returnIntent = new Intent(); returnIntent.putExtra(OpenPgpApi.RESULT_CODE, OpenPgpApi.RESULT_CODE_ERROR); returnIntent.putExtra(OpenPgpApi.RESULT_ERROR, new OpenPgpError(OpenPgpError.OPPORTUNISTIC_MISSING_KEYS, "Missing keys")); when(openPgpApi.executeApi(any(Intent.class), any(OpenPgpDataSource.class), any(OutputStream.class))) .thenReturn(returnIntent); Callback mockCallback = mock(Callback.class); pgpMessageBuilder.buildAsync(mockCallback); ArgumentCaptor<MimeMessage> captor = ArgumentCaptor.forClass(MimeMessage.class); verify(mockCallback).onMessageBuildSuccess(captor.capture(), eq(false)); verifyNoMoreInteractions(mockCallback); MimeMessage message = captor.getValue(); Assert.assertEquals("text/plain", message.getMimeType()); } | public void setCryptoStatus(ComposeCryptoStatus cryptoStatus) { this.cryptoStatus = cryptoStatus; } | PgpMessageBuilder extends MessageBuilder { public void setCryptoStatus(ComposeCryptoStatus cryptoStatus) { this.cryptoStatus = cryptoStatus; } } | PgpMessageBuilder extends MessageBuilder { public void setCryptoStatus(ComposeCryptoStatus cryptoStatus) { this.cryptoStatus = cryptoStatus; } @VisibleForTesting PgpMessageBuilder(Context context, MessageIdGenerator messageIdGenerator, BoundaryGenerator boundaryGenerator,
AutocryptOperations autocryptOperations, AutocryptOpenPgpApiInteractor autocryptOpenPgpApiInteractor); } | PgpMessageBuilder extends MessageBuilder { public void setCryptoStatus(ComposeCryptoStatus cryptoStatus) { this.cryptoStatus = cryptoStatus; } @VisibleForTesting PgpMessageBuilder(Context context, MessageIdGenerator messageIdGenerator, BoundaryGenerator boundaryGenerator,
AutocryptOperations autocryptOperations, AutocryptOpenPgpApiInteractor autocryptOpenPgpApiInteractor); static PgpMessageBuilder newInstance(); void setOpenPgpApi(OpenPgpApi openPgpApi); @Override void buildMessageOnActivityResult(int requestCode, @NonNull Intent userInteractionResult); void setCryptoStatus(ComposeCryptoStatus cryptoStatus); } | PgpMessageBuilder extends MessageBuilder { public void setCryptoStatus(ComposeCryptoStatus cryptoStatus) { this.cryptoStatus = cryptoStatus; } @VisibleForTesting PgpMessageBuilder(Context context, MessageIdGenerator messageIdGenerator, BoundaryGenerator boundaryGenerator,
AutocryptOperations autocryptOperations, AutocryptOpenPgpApiInteractor autocryptOpenPgpApiInteractor); static PgpMessageBuilder newInstance(); void setOpenPgpApi(OpenPgpApi openPgpApi); @Override void buildMessageOnActivityResult(int requestCode, @NonNull Intent userInteractionResult); void setCryptoStatus(ComposeCryptoStatus cryptoStatus); } |
@Test public void buildSign__withNoDetachedSignatureExtra__shouldFail() throws MessagingException { ComposeCryptoStatus cryptoStatus = cryptoStatusBuilder .setCryptoMode(CryptoMode.SIGN_ONLY) .build(); pgpMessageBuilder.setCryptoStatus(cryptoStatus); Intent returnIntentSigned = new Intent(); returnIntentSigned.putExtra(OpenPgpApi.RESULT_CODE, OpenPgpApi.RESULT_CODE_SUCCESS); when(openPgpApi.executeApi(any(Intent.class), any(OpenPgpDataSource.class), any(OutputStream.class))) .thenReturn(returnIntentSigned); Callback mockCallback = mock(Callback.class); pgpMessageBuilder.buildAsync(mockCallback); verify(mockCallback).onMessageBuildException(any(MessagingException.class)); verifyNoMoreInteractions(mockCallback); } | public void setCryptoStatus(ComposeCryptoStatus cryptoStatus) { this.cryptoStatus = cryptoStatus; } | PgpMessageBuilder extends MessageBuilder { public void setCryptoStatus(ComposeCryptoStatus cryptoStatus) { this.cryptoStatus = cryptoStatus; } } | PgpMessageBuilder extends MessageBuilder { public void setCryptoStatus(ComposeCryptoStatus cryptoStatus) { this.cryptoStatus = cryptoStatus; } @VisibleForTesting PgpMessageBuilder(Context context, MessageIdGenerator messageIdGenerator, BoundaryGenerator boundaryGenerator,
AutocryptOperations autocryptOperations, AutocryptOpenPgpApiInteractor autocryptOpenPgpApiInteractor); } | PgpMessageBuilder extends MessageBuilder { public void setCryptoStatus(ComposeCryptoStatus cryptoStatus) { this.cryptoStatus = cryptoStatus; } @VisibleForTesting PgpMessageBuilder(Context context, MessageIdGenerator messageIdGenerator, BoundaryGenerator boundaryGenerator,
AutocryptOperations autocryptOperations, AutocryptOpenPgpApiInteractor autocryptOpenPgpApiInteractor); static PgpMessageBuilder newInstance(); void setOpenPgpApi(OpenPgpApi openPgpApi); @Override void buildMessageOnActivityResult(int requestCode, @NonNull Intent userInteractionResult); void setCryptoStatus(ComposeCryptoStatus cryptoStatus); } | PgpMessageBuilder extends MessageBuilder { public void setCryptoStatus(ComposeCryptoStatus cryptoStatus) { this.cryptoStatus = cryptoStatus; } @VisibleForTesting PgpMessageBuilder(Context context, MessageIdGenerator messageIdGenerator, BoundaryGenerator boundaryGenerator,
AutocryptOperations autocryptOperations, AutocryptOpenPgpApiInteractor autocryptOpenPgpApiInteractor); static PgpMessageBuilder newInstance(); void setOpenPgpApi(OpenPgpApi openPgpApi); @Override void buildMessageOnActivityResult(int requestCode, @NonNull Intent userInteractionResult); void setCryptoStatus(ComposeCryptoStatus cryptoStatus); } |
@Test public void getDefaultDeletePolicy_withImap_shouldReturn_ON_DELETE() { DeletePolicy result = AccountCreator.getDefaultDeletePolicy(Type.IMAP); assertEquals(DeletePolicy.ON_DELETE, result); } | public static DeletePolicy getDefaultDeletePolicy(Type type) { switch (type) { case IMAP: { return DeletePolicy.ON_DELETE; } case POP3: { return DeletePolicy.NEVER; } case WebDAV: { return DeletePolicy.ON_DELETE; } case EWS: { return DeletePolicy.ON_DELETE; } case SMTP: { throw new IllegalStateException("Delete policy doesn't apply to SMTP"); } } throw new AssertionError("Unhandled case: " + type); } | AccountCreator { public static DeletePolicy getDefaultDeletePolicy(Type type) { switch (type) { case IMAP: { return DeletePolicy.ON_DELETE; } case POP3: { return DeletePolicy.NEVER; } case WebDAV: { return DeletePolicy.ON_DELETE; } case EWS: { return DeletePolicy.ON_DELETE; } case SMTP: { throw new IllegalStateException("Delete policy doesn't apply to SMTP"); } } throw new AssertionError("Unhandled case: " + type); } } | AccountCreator { public static DeletePolicy getDefaultDeletePolicy(Type type) { switch (type) { case IMAP: { return DeletePolicy.ON_DELETE; } case POP3: { return DeletePolicy.NEVER; } case WebDAV: { return DeletePolicy.ON_DELETE; } case EWS: { return DeletePolicy.ON_DELETE; } case SMTP: { throw new IllegalStateException("Delete policy doesn't apply to SMTP"); } } throw new AssertionError("Unhandled case: " + type); } } | AccountCreator { public static DeletePolicy getDefaultDeletePolicy(Type type) { switch (type) { case IMAP: { return DeletePolicy.ON_DELETE; } case POP3: { return DeletePolicy.NEVER; } case WebDAV: { return DeletePolicy.ON_DELETE; } case EWS: { return DeletePolicy.ON_DELETE; } case SMTP: { throw new IllegalStateException("Delete policy doesn't apply to SMTP"); } } throw new AssertionError("Unhandled case: " + type); } static DeletePolicy getDefaultDeletePolicy(Type type); static int getDefaultPort(ConnectionSecurity securityType, Type storeType); } | AccountCreator { public static DeletePolicy getDefaultDeletePolicy(Type type) { switch (type) { case IMAP: { return DeletePolicy.ON_DELETE; } case POP3: { return DeletePolicy.NEVER; } case WebDAV: { return DeletePolicy.ON_DELETE; } case EWS: { return DeletePolicy.ON_DELETE; } case SMTP: { throw new IllegalStateException("Delete policy doesn't apply to SMTP"); } } throw new AssertionError("Unhandled case: " + type); } static DeletePolicy getDefaultDeletePolicy(Type type); static int getDefaultPort(ConnectionSecurity securityType, Type storeType); } |
@Test public void getDefaultDeletePolicy_withPop3_shouldReturn_NEVER() { DeletePolicy result = AccountCreator.getDefaultDeletePolicy(Type.POP3); assertEquals(DeletePolicy.NEVER, result); } | public static DeletePolicy getDefaultDeletePolicy(Type type) { switch (type) { case IMAP: { return DeletePolicy.ON_DELETE; } case POP3: { return DeletePolicy.NEVER; } case WebDAV: { return DeletePolicy.ON_DELETE; } case EWS: { return DeletePolicy.ON_DELETE; } case SMTP: { throw new IllegalStateException("Delete policy doesn't apply to SMTP"); } } throw new AssertionError("Unhandled case: " + type); } | AccountCreator { public static DeletePolicy getDefaultDeletePolicy(Type type) { switch (type) { case IMAP: { return DeletePolicy.ON_DELETE; } case POP3: { return DeletePolicy.NEVER; } case WebDAV: { return DeletePolicy.ON_DELETE; } case EWS: { return DeletePolicy.ON_DELETE; } case SMTP: { throw new IllegalStateException("Delete policy doesn't apply to SMTP"); } } throw new AssertionError("Unhandled case: " + type); } } | AccountCreator { public static DeletePolicy getDefaultDeletePolicy(Type type) { switch (type) { case IMAP: { return DeletePolicy.ON_DELETE; } case POP3: { return DeletePolicy.NEVER; } case WebDAV: { return DeletePolicy.ON_DELETE; } case EWS: { return DeletePolicy.ON_DELETE; } case SMTP: { throw new IllegalStateException("Delete policy doesn't apply to SMTP"); } } throw new AssertionError("Unhandled case: " + type); } } | AccountCreator { public static DeletePolicy getDefaultDeletePolicy(Type type) { switch (type) { case IMAP: { return DeletePolicy.ON_DELETE; } case POP3: { return DeletePolicy.NEVER; } case WebDAV: { return DeletePolicy.ON_DELETE; } case EWS: { return DeletePolicy.ON_DELETE; } case SMTP: { throw new IllegalStateException("Delete policy doesn't apply to SMTP"); } } throw new AssertionError("Unhandled case: " + type); } static DeletePolicy getDefaultDeletePolicy(Type type); static int getDefaultPort(ConnectionSecurity securityType, Type storeType); } | AccountCreator { public static DeletePolicy getDefaultDeletePolicy(Type type) { switch (type) { case IMAP: { return DeletePolicy.ON_DELETE; } case POP3: { return DeletePolicy.NEVER; } case WebDAV: { return DeletePolicy.ON_DELETE; } case EWS: { return DeletePolicy.ON_DELETE; } case SMTP: { throw new IllegalStateException("Delete policy doesn't apply to SMTP"); } } throw new AssertionError("Unhandled case: " + type); } static DeletePolicy getDefaultDeletePolicy(Type type); static int getDefaultPort(ConnectionSecurity securityType, Type storeType); } |
@Test public void getDefaultDeletePolicy_withWebDav_shouldReturn_ON_DELETE() { DeletePolicy result = AccountCreator.getDefaultDeletePolicy(Type.WebDAV); assertEquals(DeletePolicy.ON_DELETE, result); } | public static DeletePolicy getDefaultDeletePolicy(Type type) { switch (type) { case IMAP: { return DeletePolicy.ON_DELETE; } case POP3: { return DeletePolicy.NEVER; } case WebDAV: { return DeletePolicy.ON_DELETE; } case EWS: { return DeletePolicy.ON_DELETE; } case SMTP: { throw new IllegalStateException("Delete policy doesn't apply to SMTP"); } } throw new AssertionError("Unhandled case: " + type); } | AccountCreator { public static DeletePolicy getDefaultDeletePolicy(Type type) { switch (type) { case IMAP: { return DeletePolicy.ON_DELETE; } case POP3: { return DeletePolicy.NEVER; } case WebDAV: { return DeletePolicy.ON_DELETE; } case EWS: { return DeletePolicy.ON_DELETE; } case SMTP: { throw new IllegalStateException("Delete policy doesn't apply to SMTP"); } } throw new AssertionError("Unhandled case: " + type); } } | AccountCreator { public static DeletePolicy getDefaultDeletePolicy(Type type) { switch (type) { case IMAP: { return DeletePolicy.ON_DELETE; } case POP3: { return DeletePolicy.NEVER; } case WebDAV: { return DeletePolicy.ON_DELETE; } case EWS: { return DeletePolicy.ON_DELETE; } case SMTP: { throw new IllegalStateException("Delete policy doesn't apply to SMTP"); } } throw new AssertionError("Unhandled case: " + type); } } | AccountCreator { public static DeletePolicy getDefaultDeletePolicy(Type type) { switch (type) { case IMAP: { return DeletePolicy.ON_DELETE; } case POP3: { return DeletePolicy.NEVER; } case WebDAV: { return DeletePolicy.ON_DELETE; } case EWS: { return DeletePolicy.ON_DELETE; } case SMTP: { throw new IllegalStateException("Delete policy doesn't apply to SMTP"); } } throw new AssertionError("Unhandled case: " + type); } static DeletePolicy getDefaultDeletePolicy(Type type); static int getDefaultPort(ConnectionSecurity securityType, Type storeType); } | AccountCreator { public static DeletePolicy getDefaultDeletePolicy(Type type) { switch (type) { case IMAP: { return DeletePolicy.ON_DELETE; } case POP3: { return DeletePolicy.NEVER; } case WebDAV: { return DeletePolicy.ON_DELETE; } case EWS: { return DeletePolicy.ON_DELETE; } case SMTP: { throw new IllegalStateException("Delete policy doesn't apply to SMTP"); } } throw new AssertionError("Unhandled case: " + type); } static DeletePolicy getDefaultDeletePolicy(Type type); static int getDefaultPort(ConnectionSecurity securityType, Type storeType); } |
@Test public void getDefaultPort_withNoConnectionSecurityAndImap_shouldReturnDefaultPort() { int result = AccountCreator.getDefaultPort(ConnectionSecurity.NONE, Type.IMAP); assertEquals(Type.IMAP.defaultPort, result); } | public static int getDefaultPort(ConnectionSecurity securityType, Type storeType) { switch (securityType) { case NONE: case STARTTLS_REQUIRED: { return storeType.defaultPort; } case SSL_TLS_REQUIRED: { return storeType.defaultTlsPort; } } throw new AssertionError("Unhandled ConnectionSecurity type encountered: " + securityType); } | AccountCreator { public static int getDefaultPort(ConnectionSecurity securityType, Type storeType) { switch (securityType) { case NONE: case STARTTLS_REQUIRED: { return storeType.defaultPort; } case SSL_TLS_REQUIRED: { return storeType.defaultTlsPort; } } throw new AssertionError("Unhandled ConnectionSecurity type encountered: " + securityType); } } | AccountCreator { public static int getDefaultPort(ConnectionSecurity securityType, Type storeType) { switch (securityType) { case NONE: case STARTTLS_REQUIRED: { return storeType.defaultPort; } case SSL_TLS_REQUIRED: { return storeType.defaultTlsPort; } } throw new AssertionError("Unhandled ConnectionSecurity type encountered: " + securityType); } } | AccountCreator { public static int getDefaultPort(ConnectionSecurity securityType, Type storeType) { switch (securityType) { case NONE: case STARTTLS_REQUIRED: { return storeType.defaultPort; } case SSL_TLS_REQUIRED: { return storeType.defaultTlsPort; } } throw new AssertionError("Unhandled ConnectionSecurity type encountered: " + securityType); } static DeletePolicy getDefaultDeletePolicy(Type type); static int getDefaultPort(ConnectionSecurity securityType, Type storeType); } | AccountCreator { public static int getDefaultPort(ConnectionSecurity securityType, Type storeType) { switch (securityType) { case NONE: case STARTTLS_REQUIRED: { return storeType.defaultPort; } case SSL_TLS_REQUIRED: { return storeType.defaultTlsPort; } } throw new AssertionError("Unhandled ConnectionSecurity type encountered: " + securityType); } static DeletePolicy getDefaultDeletePolicy(Type type); static int getDefaultPort(ConnectionSecurity securityType, Type storeType); } |
@Test public void getDefaultPort_withStartTlsAndImap_shouldReturnDefaultPort() { int result = AccountCreator.getDefaultPort(ConnectionSecurity.STARTTLS_REQUIRED, Type.IMAP); assertEquals(Type.IMAP.defaultPort, result); } | public static int getDefaultPort(ConnectionSecurity securityType, Type storeType) { switch (securityType) { case NONE: case STARTTLS_REQUIRED: { return storeType.defaultPort; } case SSL_TLS_REQUIRED: { return storeType.defaultTlsPort; } } throw new AssertionError("Unhandled ConnectionSecurity type encountered: " + securityType); } | AccountCreator { public static int getDefaultPort(ConnectionSecurity securityType, Type storeType) { switch (securityType) { case NONE: case STARTTLS_REQUIRED: { return storeType.defaultPort; } case SSL_TLS_REQUIRED: { return storeType.defaultTlsPort; } } throw new AssertionError("Unhandled ConnectionSecurity type encountered: " + securityType); } } | AccountCreator { public static int getDefaultPort(ConnectionSecurity securityType, Type storeType) { switch (securityType) { case NONE: case STARTTLS_REQUIRED: { return storeType.defaultPort; } case SSL_TLS_REQUIRED: { return storeType.defaultTlsPort; } } throw new AssertionError("Unhandled ConnectionSecurity type encountered: " + securityType); } } | AccountCreator { public static int getDefaultPort(ConnectionSecurity securityType, Type storeType) { switch (securityType) { case NONE: case STARTTLS_REQUIRED: { return storeType.defaultPort; } case SSL_TLS_REQUIRED: { return storeType.defaultTlsPort; } } throw new AssertionError("Unhandled ConnectionSecurity type encountered: " + securityType); } static DeletePolicy getDefaultDeletePolicy(Type type); static int getDefaultPort(ConnectionSecurity securityType, Type storeType); } | AccountCreator { public static int getDefaultPort(ConnectionSecurity securityType, Type storeType) { switch (securityType) { case NONE: case STARTTLS_REQUIRED: { return storeType.defaultPort; } case SSL_TLS_REQUIRED: { return storeType.defaultTlsPort; } } throw new AssertionError("Unhandled ConnectionSecurity type encountered: " + securityType); } static DeletePolicy getDefaultDeletePolicy(Type type); static int getDefaultPort(ConnectionSecurity securityType, Type storeType); } |
@Test public void getDefaultPort_withTlsAndImap_shouldReturnDefaultTlsPort() { int result = AccountCreator.getDefaultPort(ConnectionSecurity.SSL_TLS_REQUIRED, Type.IMAP); assertEquals(Type.IMAP.defaultTlsPort, result); } | public static int getDefaultPort(ConnectionSecurity securityType, Type storeType) { switch (securityType) { case NONE: case STARTTLS_REQUIRED: { return storeType.defaultPort; } case SSL_TLS_REQUIRED: { return storeType.defaultTlsPort; } } throw new AssertionError("Unhandled ConnectionSecurity type encountered: " + securityType); } | AccountCreator { public static int getDefaultPort(ConnectionSecurity securityType, Type storeType) { switch (securityType) { case NONE: case STARTTLS_REQUIRED: { return storeType.defaultPort; } case SSL_TLS_REQUIRED: { return storeType.defaultTlsPort; } } throw new AssertionError("Unhandled ConnectionSecurity type encountered: " + securityType); } } | AccountCreator { public static int getDefaultPort(ConnectionSecurity securityType, Type storeType) { switch (securityType) { case NONE: case STARTTLS_REQUIRED: { return storeType.defaultPort; } case SSL_TLS_REQUIRED: { return storeType.defaultTlsPort; } } throw new AssertionError("Unhandled ConnectionSecurity type encountered: " + securityType); } } | AccountCreator { public static int getDefaultPort(ConnectionSecurity securityType, Type storeType) { switch (securityType) { case NONE: case STARTTLS_REQUIRED: { return storeType.defaultPort; } case SSL_TLS_REQUIRED: { return storeType.defaultTlsPort; } } throw new AssertionError("Unhandled ConnectionSecurity type encountered: " + securityType); } static DeletePolicy getDefaultDeletePolicy(Type type); static int getDefaultPort(ConnectionSecurity securityType, Type storeType); } | AccountCreator { public static int getDefaultPort(ConnectionSecurity securityType, Type storeType) { switch (securityType) { case NONE: case STARTTLS_REQUIRED: { return storeType.defaultPort; } case SSL_TLS_REQUIRED: { return storeType.defaultTlsPort; } } throw new AssertionError("Unhandled ConnectionSecurity type encountered: " + securityType); } static DeletePolicy getDefaultDeletePolicy(Type type); static int getDefaultPort(ConnectionSecurity securityType, Type storeType); } |
@Test public void findEncrypted__withMultipartMixedSubSigned__shouldReturnSigned() throws Exception { Message message = messageFromBody( multipart("mixed", multipart("signed", bodypart("text/plain"), bodypart("application/pgp-signature") ) ) ); List<Part> signedParts = MessageDecryptVerifier.findSignedParts(message, messageCryptoAnnotations); assertEquals(1, signedParts.size()); assertSame(getPart(message, 0), signedParts.get(0)); } | public static List<Part> findSignedParts(Part startPart, MessageCryptoAnnotations messageCryptoAnnotations) { List<Part> signedParts = new ArrayList<>(); Stack<Part> partsToCheck = new Stack<>(); partsToCheck.push(startPart); while (!partsToCheck.isEmpty()) { Part part = partsToCheck.pop(); if (messageCryptoAnnotations.has(part)) { CryptoResultAnnotation resultAnnotation = messageCryptoAnnotations.get(part); MimeBodyPart replacementData = resultAnnotation.getReplacementData(); if (replacementData != null) { part = replacementData; } } Body body = part.getBody(); if (isPartMultipartSigned(part)) { signedParts.add(part); continue; } 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 signedParts; } | MessageDecryptVerifier { public static List<Part> findSignedParts(Part startPart, MessageCryptoAnnotations messageCryptoAnnotations) { List<Part> signedParts = new ArrayList<>(); Stack<Part> partsToCheck = new Stack<>(); partsToCheck.push(startPart); while (!partsToCheck.isEmpty()) { Part part = partsToCheck.pop(); if (messageCryptoAnnotations.has(part)) { CryptoResultAnnotation resultAnnotation = messageCryptoAnnotations.get(part); MimeBodyPart replacementData = resultAnnotation.getReplacementData(); if (replacementData != null) { part = replacementData; } } Body body = part.getBody(); if (isPartMultipartSigned(part)) { signedParts.add(part); continue; } 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 signedParts; } } | MessageDecryptVerifier { public static List<Part> findSignedParts(Part startPart, MessageCryptoAnnotations messageCryptoAnnotations) { List<Part> signedParts = new ArrayList<>(); Stack<Part> partsToCheck = new Stack<>(); partsToCheck.push(startPart); while (!partsToCheck.isEmpty()) { Part part = partsToCheck.pop(); if (messageCryptoAnnotations.has(part)) { CryptoResultAnnotation resultAnnotation = messageCryptoAnnotations.get(part); MimeBodyPart replacementData = resultAnnotation.getReplacementData(); if (replacementData != null) { part = replacementData; } } Body body = part.getBody(); if (isPartMultipartSigned(part)) { signedParts.add(part); continue; } 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 signedParts; } } | MessageDecryptVerifier { public static List<Part> findSignedParts(Part startPart, MessageCryptoAnnotations messageCryptoAnnotations) { List<Part> signedParts = new ArrayList<>(); Stack<Part> partsToCheck = new Stack<>(); partsToCheck.push(startPart); while (!partsToCheck.isEmpty()) { Part part = partsToCheck.pop(); if (messageCryptoAnnotations.has(part)) { CryptoResultAnnotation resultAnnotation = messageCryptoAnnotations.get(part); MimeBodyPart replacementData = resultAnnotation.getReplacementData(); if (replacementData != null) { part = replacementData; } } Body body = part.getBody(); if (isPartMultipartSigned(part)) { signedParts.add(part); continue; } 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 signedParts; } static Part findPrimaryEncryptedOrSignedPart(Part part, List<Part> outputExtraParts); static List<Part> findEncryptedParts(Part startPart); static List<Part> findSignedParts(Part startPart, MessageCryptoAnnotations messageCryptoAnnotations); static List<Part> findPgpInlineParts(Part startPart); static byte[] getSignatureData(Part part); static boolean isPgpMimeEncryptedOrSignedPart(Part part); static boolean isSMimeEncryptedOrSignedPart(Part part); static boolean isPartPgpInlineEncrypted(@Nullable Part part); } | MessageDecryptVerifier { public static List<Part> findSignedParts(Part startPart, MessageCryptoAnnotations messageCryptoAnnotations) { List<Part> signedParts = new ArrayList<>(); Stack<Part> partsToCheck = new Stack<>(); partsToCheck.push(startPart); while (!partsToCheck.isEmpty()) { Part part = partsToCheck.pop(); if (messageCryptoAnnotations.has(part)) { CryptoResultAnnotation resultAnnotation = messageCryptoAnnotations.get(part); MimeBodyPart replacementData = resultAnnotation.getReplacementData(); if (replacementData != null) { part = replacementData; } } Body body = part.getBody(); if (isPartMultipartSigned(part)) { signedParts.add(part); continue; } 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 signedParts; } static Part findPrimaryEncryptedOrSignedPart(Part part, List<Part> outputExtraParts); static List<Part> findEncryptedParts(Part startPart); static List<Part> findSignedParts(Part startPart, MessageCryptoAnnotations messageCryptoAnnotations); static List<Part> findPgpInlineParts(Part startPart); static byte[] getSignatureData(Part part); static boolean isPgpMimeEncryptedOrSignedPart(Part part); static boolean isSMimeEncryptedOrSignedPart(Part part); static boolean isPartPgpInlineEncrypted(@Nullable Part part); } |
@Test public void compare_withTwoNullArguments_shouldReturnZero() throws Exception { Message messageLeft = null; Message messageRight = null; int result = comparator.compare(messageLeft, messageRight); assertEquals("result must be 0 when both arguments are null", 0, result); } | @Override public int compare(Message messageLeft, Message messageRight) { Long uidLeft = getUidForMessage(messageLeft); Long uidRight = getUidForMessage(messageRight); if (uidLeft == null && uidRight == null) { return 0; } else if (uidLeft == null) { return 1; } else if (uidRight == null) { return -1; } return uidRight.compareTo(uidLeft); } | UidReverseComparator implements Comparator<Message> { @Override public int compare(Message messageLeft, Message messageRight) { Long uidLeft = getUidForMessage(messageLeft); Long uidRight = getUidForMessage(messageRight); if (uidLeft == null && uidRight == null) { return 0; } else if (uidLeft == null) { return 1; } else if (uidRight == null) { return -1; } return uidRight.compareTo(uidLeft); } } | UidReverseComparator implements Comparator<Message> { @Override public int compare(Message messageLeft, Message messageRight) { Long uidLeft = getUidForMessage(messageLeft); Long uidRight = getUidForMessage(messageRight); if (uidLeft == null && uidRight == null) { return 0; } else if (uidLeft == null) { return 1; } else if (uidRight == null) { return -1; } return uidRight.compareTo(uidLeft); } } | UidReverseComparator implements Comparator<Message> { @Override public int compare(Message messageLeft, Message messageRight) { Long uidLeft = getUidForMessage(messageLeft); Long uidRight = getUidForMessage(messageRight); if (uidLeft == null && uidRight == null) { return 0; } else if (uidLeft == null) { return 1; } else if (uidRight == null) { return -1; } return uidRight.compareTo(uidLeft); } @Override int compare(Message messageLeft, Message messageRight); } | UidReverseComparator implements Comparator<Message> { @Override public int compare(Message messageLeft, Message messageRight) { Long uidLeft = getUidForMessage(messageLeft); Long uidRight = getUidForMessage(messageRight); if (uidLeft == null && uidRight == null) { return 0; } else if (uidLeft == null) { return 1; } else if (uidRight == null) { return -1; } return uidRight.compareTo(uidLeft); } @Override int compare(Message messageLeft, Message messageRight); } |
@Test public void compare_withNullArgumentAndMessageWithNullUid_shouldReturnZero() throws Exception { Message messageLeft = null; Message messageRight = createMessageWithNullUid(); int result = comparator.compare(messageLeft, messageRight); assertEquals("result must be 0 when both arguments are not a message with valid UID", 0, result); } | @Override public int compare(Message messageLeft, Message messageRight) { Long uidLeft = getUidForMessage(messageLeft); Long uidRight = getUidForMessage(messageRight); if (uidLeft == null && uidRight == null) { return 0; } else if (uidLeft == null) { return 1; } else if (uidRight == null) { return -1; } return uidRight.compareTo(uidLeft); } | UidReverseComparator implements Comparator<Message> { @Override public int compare(Message messageLeft, Message messageRight) { Long uidLeft = getUidForMessage(messageLeft); Long uidRight = getUidForMessage(messageRight); if (uidLeft == null && uidRight == null) { return 0; } else if (uidLeft == null) { return 1; } else if (uidRight == null) { return -1; } return uidRight.compareTo(uidLeft); } } | UidReverseComparator implements Comparator<Message> { @Override public int compare(Message messageLeft, Message messageRight) { Long uidLeft = getUidForMessage(messageLeft); Long uidRight = getUidForMessage(messageRight); if (uidLeft == null && uidRight == null) { return 0; } else if (uidLeft == null) { return 1; } else if (uidRight == null) { return -1; } return uidRight.compareTo(uidLeft); } } | UidReverseComparator implements Comparator<Message> { @Override public int compare(Message messageLeft, Message messageRight) { Long uidLeft = getUidForMessage(messageLeft); Long uidRight = getUidForMessage(messageRight); if (uidLeft == null && uidRight == null) { return 0; } else if (uidLeft == null) { return 1; } else if (uidRight == null) { return -1; } return uidRight.compareTo(uidLeft); } @Override int compare(Message messageLeft, Message messageRight); } | UidReverseComparator implements Comparator<Message> { @Override public int compare(Message messageLeft, Message messageRight) { Long uidLeft = getUidForMessage(messageLeft); Long uidRight = getUidForMessage(messageRight); if (uidLeft == null && uidRight == null) { return 0; } else if (uidLeft == null) { return 1; } else if (uidRight == null) { return -1; } return uidRight.compareTo(uidLeft); } @Override int compare(Message messageLeft, Message messageRight); } |
@Test public void compare_withMessageWithNullUidAndNullArgument_shouldReturnZero() throws Exception { Message messageLeft = createMessageWithNullUid(); Message messageRight = null; int result = comparator.compare(messageLeft, messageRight); assertEquals("result must be 0 when both arguments are not a message with valid UID", 0, result); } | @Override public int compare(Message messageLeft, Message messageRight) { Long uidLeft = getUidForMessage(messageLeft); Long uidRight = getUidForMessage(messageRight); if (uidLeft == null && uidRight == null) { return 0; } else if (uidLeft == null) { return 1; } else if (uidRight == null) { return -1; } return uidRight.compareTo(uidLeft); } | UidReverseComparator implements Comparator<Message> { @Override public int compare(Message messageLeft, Message messageRight) { Long uidLeft = getUidForMessage(messageLeft); Long uidRight = getUidForMessage(messageRight); if (uidLeft == null && uidRight == null) { return 0; } else if (uidLeft == null) { return 1; } else if (uidRight == null) { return -1; } return uidRight.compareTo(uidLeft); } } | UidReverseComparator implements Comparator<Message> { @Override public int compare(Message messageLeft, Message messageRight) { Long uidLeft = getUidForMessage(messageLeft); Long uidRight = getUidForMessage(messageRight); if (uidLeft == null && uidRight == null) { return 0; } else if (uidLeft == null) { return 1; } else if (uidRight == null) { return -1; } return uidRight.compareTo(uidLeft); } } | UidReverseComparator implements Comparator<Message> { @Override public int compare(Message messageLeft, Message messageRight) { Long uidLeft = getUidForMessage(messageLeft); Long uidRight = getUidForMessage(messageRight); if (uidLeft == null && uidRight == null) { return 0; } else if (uidLeft == null) { return 1; } else if (uidRight == null) { return -1; } return uidRight.compareTo(uidLeft); } @Override int compare(Message messageLeft, Message messageRight); } | UidReverseComparator implements Comparator<Message> { @Override public int compare(Message messageLeft, Message messageRight) { Long uidLeft = getUidForMessage(messageLeft); Long uidRight = getUidForMessage(messageRight); if (uidLeft == null && uidRight == null) { return 0; } else if (uidLeft == null) { return 1; } else if (uidRight == null) { return -1; } return uidRight.compareTo(uidLeft); } @Override int compare(Message messageLeft, Message messageRight); } |
@Test public void compare_withTwoMessagesWithNullUid_shouldReturnZero() throws Exception { Message messageLeft = createMessageWithNullUid(); Message messageRight = createMessageWithNullUid(); int result = comparator.compare(messageLeft, messageRight); assertEquals("result must be 0 when both arguments are a message with a null UID", 0, result); } | @Override public int compare(Message messageLeft, Message messageRight) { Long uidLeft = getUidForMessage(messageLeft); Long uidRight = getUidForMessage(messageRight); if (uidLeft == null && uidRight == null) { return 0; } else if (uidLeft == null) { return 1; } else if (uidRight == null) { return -1; } return uidRight.compareTo(uidLeft); } | UidReverseComparator implements Comparator<Message> { @Override public int compare(Message messageLeft, Message messageRight) { Long uidLeft = getUidForMessage(messageLeft); Long uidRight = getUidForMessage(messageRight); if (uidLeft == null && uidRight == null) { return 0; } else if (uidLeft == null) { return 1; } else if (uidRight == null) { return -1; } return uidRight.compareTo(uidLeft); } } | UidReverseComparator implements Comparator<Message> { @Override public int compare(Message messageLeft, Message messageRight) { Long uidLeft = getUidForMessage(messageLeft); Long uidRight = getUidForMessage(messageRight); if (uidLeft == null && uidRight == null) { return 0; } else if (uidLeft == null) { return 1; } else if (uidRight == null) { return -1; } return uidRight.compareTo(uidLeft); } } | UidReverseComparator implements Comparator<Message> { @Override public int compare(Message messageLeft, Message messageRight) { Long uidLeft = getUidForMessage(messageLeft); Long uidRight = getUidForMessage(messageRight); if (uidLeft == null && uidRight == null) { return 0; } else if (uidLeft == null) { return 1; } else if (uidRight == null) { return -1; } return uidRight.compareTo(uidLeft); } @Override int compare(Message messageLeft, Message messageRight); } | UidReverseComparator implements Comparator<Message> { @Override public int compare(Message messageLeft, Message messageRight) { Long uidLeft = getUidForMessage(messageLeft); Long uidRight = getUidForMessage(messageRight); if (uidLeft == null && uidRight == null) { return 0; } else if (uidLeft == null) { return 1; } else if (uidRight == null) { return -1; } return uidRight.compareTo(uidLeft); } @Override int compare(Message messageLeft, Message messageRight); } |
@Test public void compare_withNullArgumentAndMessageWithInvalidUid_shouldReturnZero() throws Exception { Message messageLeft = null; Message messageRight = createMessageWithInvalidUid(); int result = comparator.compare(messageLeft, messageRight); assertEquals("result must be 0 when both arguments are not a message with valid UID", 0, result); } | @Override public int compare(Message messageLeft, Message messageRight) { Long uidLeft = getUidForMessage(messageLeft); Long uidRight = getUidForMessage(messageRight); if (uidLeft == null && uidRight == null) { return 0; } else if (uidLeft == null) { return 1; } else if (uidRight == null) { return -1; } return uidRight.compareTo(uidLeft); } | UidReverseComparator implements Comparator<Message> { @Override public int compare(Message messageLeft, Message messageRight) { Long uidLeft = getUidForMessage(messageLeft); Long uidRight = getUidForMessage(messageRight); if (uidLeft == null && uidRight == null) { return 0; } else if (uidLeft == null) { return 1; } else if (uidRight == null) { return -1; } return uidRight.compareTo(uidLeft); } } | UidReverseComparator implements Comparator<Message> { @Override public int compare(Message messageLeft, Message messageRight) { Long uidLeft = getUidForMessage(messageLeft); Long uidRight = getUidForMessage(messageRight); if (uidLeft == null && uidRight == null) { return 0; } else if (uidLeft == null) { return 1; } else if (uidRight == null) { return -1; } return uidRight.compareTo(uidLeft); } } | UidReverseComparator implements Comparator<Message> { @Override public int compare(Message messageLeft, Message messageRight) { Long uidLeft = getUidForMessage(messageLeft); Long uidRight = getUidForMessage(messageRight); if (uidLeft == null && uidRight == null) { return 0; } else if (uidLeft == null) { return 1; } else if (uidRight == null) { return -1; } return uidRight.compareTo(uidLeft); } @Override int compare(Message messageLeft, Message messageRight); } | UidReverseComparator implements Comparator<Message> { @Override public int compare(Message messageLeft, Message messageRight) { Long uidLeft = getUidForMessage(messageLeft); Long uidRight = getUidForMessage(messageRight); if (uidLeft == null && uidRight == null) { return 0; } else if (uidLeft == null) { return 1; } else if (uidRight == null) { return -1; } return uidRight.compareTo(uidLeft); } @Override int compare(Message messageLeft, Message messageRight); } |
@Test public void compare_withMessageWithInvalidUidAndNullArgument_shouldReturnZero() throws Exception { Message messageLeft = createMessageWithInvalidUid(); Message messageRight = null; int result = comparator.compare(messageLeft, messageRight); assertEquals("result must be 0 when both arguments are not a message with valid UID", 0, result); } | @Override public int compare(Message messageLeft, Message messageRight) { Long uidLeft = getUidForMessage(messageLeft); Long uidRight = getUidForMessage(messageRight); if (uidLeft == null && uidRight == null) { return 0; } else if (uidLeft == null) { return 1; } else if (uidRight == null) { return -1; } return uidRight.compareTo(uidLeft); } | UidReverseComparator implements Comparator<Message> { @Override public int compare(Message messageLeft, Message messageRight) { Long uidLeft = getUidForMessage(messageLeft); Long uidRight = getUidForMessage(messageRight); if (uidLeft == null && uidRight == null) { return 0; } else if (uidLeft == null) { return 1; } else if (uidRight == null) { return -1; } return uidRight.compareTo(uidLeft); } } | UidReverseComparator implements Comparator<Message> { @Override public int compare(Message messageLeft, Message messageRight) { Long uidLeft = getUidForMessage(messageLeft); Long uidRight = getUidForMessage(messageRight); if (uidLeft == null && uidRight == null) { return 0; } else if (uidLeft == null) { return 1; } else if (uidRight == null) { return -1; } return uidRight.compareTo(uidLeft); } } | UidReverseComparator implements Comparator<Message> { @Override public int compare(Message messageLeft, Message messageRight) { Long uidLeft = getUidForMessage(messageLeft); Long uidRight = getUidForMessage(messageRight); if (uidLeft == null && uidRight == null) { return 0; } else if (uidLeft == null) { return 1; } else if (uidRight == null) { return -1; } return uidRight.compareTo(uidLeft); } @Override int compare(Message messageLeft, Message messageRight); } | UidReverseComparator implements Comparator<Message> { @Override public int compare(Message messageLeft, Message messageRight) { Long uidLeft = getUidForMessage(messageLeft); Long uidRight = getUidForMessage(messageRight); if (uidLeft == null && uidRight == null) { return 0; } else if (uidLeft == null) { return 1; } else if (uidRight == null) { return -1; } return uidRight.compareTo(uidLeft); } @Override int compare(Message messageLeft, Message messageRight); } |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.