Split DecoderUtil tests up

This commit is contained in:
Philip Whitehouse 2017-08-31 12:55:35 +01:00 committed by Philip Whitehouse
parent 26a84d549f
commit 1471e7a063

View file

@ -12,112 +12,173 @@ import static org.junit.Assert.assertEquals;
@RunWith(K9LibRobolectricTestRunner.class) @RunWith(K9LibRobolectricTestRunner.class)
public class DecoderUtilTest { public class DecoderUtilTest {
@Test private String body, expect;
public void testDecodeEncodedWords() { private MimeMessage message;
String body, expect;
MimeMessage message;
@Test
public void decodeEncodedWords_with_unencoded_data_returns_original_text() {
body = "abc"; body = "abc";
expect = "abc"; expect = "abc";
message = null; message = null;
assertEquals(expect, DecoderUtil.decodeEncodedWords(body, message)); assertEquals(expect, DecoderUtil.decodeEncodedWords(body, message));
}
@Test
public void decodeEncodedWords_withAsciiCharset_encoded_data_returns_text() {
body = "=?us-ascii?q?abc?="; body = "=?us-ascii?q?abc?=";
expect = "abc"; expect = "abc";
message = null; message = null;
assertEquals(expect, DecoderUtil.decodeEncodedWords(body, message)); assertEquals(expect, DecoderUtil.decodeEncodedWords(body, message));
}
@Test
public void decodeEncodedWords_withStartOnly_encoding_format_returnAsText() {
body = "=?"; body = "=?";
expect = "=?"; expect = "=?";
message = null; message = null;
assertEquals(expect, DecoderUtil.decodeEncodedWords(body, message)); assertEquals(expect, DecoderUtil.decodeEncodedWords(body, message));
}
@Test
public void decodeEncodedWords_withStartAndSeparatorOnly_returnAsText() {
body = "=??"; body = "=??";
expect = "=??"; expect = "=??";
message = null; message = null;
assertEquals(expect, DecoderUtil.decodeEncodedWords(body, message)); assertEquals(expect, DecoderUtil.decodeEncodedWords(body, message));
}
@Test
public void decodeEncodedWords_withStartAnd2SeparatorOnly_returnAsText() {
body = "=???"; body = "=???";
expect = "=???"; expect = "=???";
message = null; message = null;
assertEquals(expect, DecoderUtil.decodeEncodedWords(body, message)); assertEquals(expect, DecoderUtil.decodeEncodedWords(body, message));
}
@Test
public void decodeEncodedWords_withStartAnd3SeparatorOnly_returnAsText() {
body = "=????"; body = "=????";
expect = "=????"; expect = "=????";
message = null; message = null;
assertEquals(expect, DecoderUtil.decodeEncodedWords(body, message)); assertEquals(expect, DecoderUtil.decodeEncodedWords(body, message));
}
@Test
public void decodeEncodedWords_withSeparatorsOnly_returnAsText() {
body = "=????="; body = "=????=";
expect = "=????="; expect = "=????=";
message = null; message = null;
assertEquals(expect, DecoderUtil.decodeEncodedWords(body, message)); assertEquals(expect, DecoderUtil.decodeEncodedWords(body, message));
}
@Test
public void decodeEncodedWords_withMissingCharset_returnAsText() {
body = "=??q??="; body = "=??q??=";
expect = "=??q??="; expect = "=??q??=";
; ;
message = null; message = null;
assertEquals(expect, DecoderUtil.decodeEncodedWords(body, message)); assertEquals(expect, DecoderUtil.decodeEncodedWords(body, message));
}
@Test
public void decodeEncodedWords_withTextAndMissingCharset_returnAsText() {
body = "=??q?a?="; body = "=??q?a?=";
expect = "a"; expect = "a";
message = null; message = null;
assertEquals(expect, DecoderUtil.decodeEncodedWords(body, message)); assertEquals(expect, DecoderUtil.decodeEncodedWords(body, message));
}
@Test
public void decodeEncodedWords_withNoTextCharsetOrEncoding_returnAsText() {
body = "=??="; body = "=??=";
expect = "=??="; expect = "=??=";
message = null; message = null;
assertEquals(expect, DecoderUtil.decodeEncodedWords(body, message)); assertEquals(expect, DecoderUtil.decodeEncodedWords(body, message));
}
@Test
public void decodeEncodedWords_with_MissingEncodingAndData_returnAsText() {
body = "=?x?="; body = "=?x?=";
expect = "=?x?="; expect = "=?x?=";
message = null; message = null;
assertEquals(expect, DecoderUtil.decodeEncodedWords(body, message)); assertEquals(expect, DecoderUtil.decodeEncodedWords(body, message));
}
@Test
public void decodeEncodedWords_withMissingEncoding_returnAsText() {
body = "=?x??="; body = "=?x??=";
expect = "=?x??="; expect = "=?x??=";
message = null; message = null;
assertEquals(expect, DecoderUtil.decodeEncodedWords(body, message)); assertEquals(expect, DecoderUtil.decodeEncodedWords(body, message));
}
@Test
public void decodeEncodedWords_with_incompleteEncodingFormat_returnAsText() {
body = "=?x?q?="; body = "=?x?q?=";
expect = "=?x?q?="; expect = "=?x?q?=";
message = null; message = null;
assertEquals(expect, DecoderUtil.decodeEncodedWords(body, message)); assertEquals(expect, DecoderUtil.decodeEncodedWords(body, message));
}
@Test
public void decodeEncodedWords_with_unrecognisedEncoding_withEmptyEncodedData_returnAsText() {
body = "=?x?q??="; body = "=?x?q??=";
expect = "=?x?q??="; expect = "=?x?q??=";
message = null; message = null;
assertEquals(expect, DecoderUtil.decodeEncodedWords(body, message)); assertEquals(expect, DecoderUtil.decodeEncodedWords(body, message));
}
@Test
public void decodeEncodedWords_withUnrecognisedEncoding_withEncodedData_return_encoded_data() {
body = "=?x?q?X?="; body = "=?x?q?X?=";
expect = "X"; expect = "X";
message = null; message = null;
assertEquals(expect, DecoderUtil.decodeEncodedWords(body, message)); assertEquals(expect, DecoderUtil.decodeEncodedWords(body, message));
}
// invalid base64 string @Test
public void decodeEncodedWords_withInvalidBase64String_returnsEmptyString() {
body = "=?us-ascii?b?abc?="; body = "=?us-ascii?b?abc?=";
expect = ""; expect = "";
message = null; message = null;
assertEquals(expect, DecoderUtil.decodeEncodedWords(body, message)); assertEquals(expect, DecoderUtil.decodeEncodedWords(body, message));
}
// broken encoded header @Test
public void decodeEncodedWords_withBrokenEncoding_returnsEncodedData() {
body = "=?us-ascii?q?abc?= =?"; body = "=?us-ascii?q?abc?= =?";
expect = "abc =?"; expect = "abc =?";
message = null; message = null;
assertEquals(expect, DecoderUtil.decodeEncodedWords(body, message)); assertEquals(expect, DecoderUtil.decodeEncodedWords(body, message));
}
@Test
public void decodeEncodedWords_withUnrecognisedCharset_returnsEncodedData() {
body = "=?x?= =?"; body = "=?x?= =?";
expect = "=?x?= =?"; expect = "=?x?= =?";
message = null; message = null;
assertEquals(expect, DecoderUtil.decodeEncodedWords(body, message)); assertEquals(expect, DecoderUtil.decodeEncodedWords(body, message));
}
//Multi encoded header @Test
public void decodeEncodedWords_withMultipleEncodedSections_decodesAll() {
body = "=?utf-8?B?5Liq5Lq66YKu566xOkJVRyAjMzAyNDY6OumCruS7tuato+aWh+mZhOS7tuWQ?=\n" + body = "=?utf-8?B?5Liq5Lq66YKu566xOkJVRyAjMzAyNDY6OumCruS7tuato+aWh+mZhOS7tuWQ?=\n" +
"=?utf-8?B?jeensOecgeeVpeaYvuekuuS8mOWMlg==?="; "=?utf-8?B?jeensOecgeeVpeaYvuekuuS8mOWMlg==?=";
expect = "个人邮箱:BUG #30246::邮件正文附件<E99984><E4BBB6>称省略显示优化"; expect = "个人邮箱:BUG #30246::邮件正文附件<E99984><E4BBB6>称省略显示优化";
message = null; message = null;
assertEquals(expect, DecoderUtil.decodeEncodedWords(body, message)); assertEquals(expect, DecoderUtil.decodeEncodedWords(body, message));
}
//Non utf-8 encoding @Test
public void decodeEncodedWords_withGB2312_decodes_correctly() {
body = "=?gb2312?B?Obv9t9az6cnu29rHsLqju6rHyLPHSlfN8rrAvsa16qOsuPzT0DIwvNIzOTnU?= " + body = "=?gb2312?B?Obv9t9az6cnu29rHsLqju6rHyLPHSlfN8rrAvsa16qOsuPzT0DIwvNIzOTnU?= " +
"=?gb2312?B?qr6r0aG439DHytTLr77Gteq1yMTjwLSjoaOoQUSjqQ?="; "=?gb2312?B?qr6r0aG439DHytTLr77Gteq1yMTjwLSjoaOoQUSjqQ?=";
expect = "9积分抽深圳前海华侨城JW万豪酒店更有20家399<EFBFBD><EFBFBD>精选高星试睡酒店等你来AD<EFBFBD>"; expect = "9积分抽深圳前海华侨城JW万豪酒店更有20家399<EFBFBD><EFBFBD>精选高星试睡酒店等你来AD<EFBFBD>";