-
Notifications
You must be signed in to change notification settings - Fork 182
/
EncodingTest.java
245 lines (210 loc) · 10.3 KB
/
EncodingTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
package com.icegreen.greenmail.test.specificmessages;
import com.icegreen.greenmail.junit.GreenMailRule;
import com.icegreen.greenmail.util.EncodingUtil;
import com.icegreen.greenmail.util.GreenMailUtil;
import com.icegreen.greenmail.util.ServerSetupTest;
import com.sun.mail.imap.IMAPFolder;
import com.sun.mail.imap.IMAPStore;
import javax.mail.*;
import javax.mail.event.MessageCountEvent;
import javax.mail.event.MessageCountListener;
import javax.mail.internet.*;
import org.junit.Rule;
import org.junit.Test;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import static org.assertj.core.api.Assertions.assertThat;
/**
* Tests for encoding scenarios.
*/
public class EncodingTest {
@Rule
public GreenMailRule greenMail = new GreenMailRule(ServerSetupTest.SMTP_IMAP);
/**
* Structure of test message and content type:
* <p>
* Message (multipart/alternative)
* \--> Message (text/plain)
*/
@Test
public void testTextPlainWithUTF8() throws MessagingException, IOException {
greenMail.setUser("foo@localhost", "pwd");
final Session session = greenMail.getSmtp().createSession();
MimeMultipart multipart = new MimeMultipart("alternative");
MimeBodyPart textQP = new MimeBodyPart();
textQP.setContent("QP Content with umlaut \u00FC", "text/javascript; charset=utf-8");
textQP.setHeader("Content-Transfer-Encoding", "QUOTED-PRINTABLE");
multipart.addBodyPart(textQP);
MimeBodyPart html = new MimeBodyPart();
html.setContent(MimeUtility.encodeText("<!doctype html>" +
"<html lang=en>" +
"<head>" +
"<meta charset=utf-8>" +
"<title>Title with Umlaut \u00FC</title>" +
"</head>" +
"<body>" +
"<p>8BIT Content with umlaut ü</p>" +
"</body>" +
"</html>", "UTF-8", "B"), "text/html; charset=utf-8");
html.setHeader("Content-Transfer-Encoding", "8BIT");
multipart.addBodyPart(html);
MimeBodyPart text = new MimeBodyPart();
text.setText(MimeUtility.encodeText("8BIT Content with umlaut \u00FC", "UTF-8", "B"), "utf-8");
text.setHeader("Content-Transfer-Encoding", "8BIT");
multipart.addBodyPart(text);
MimeBodyPart text2QP = new MimeBodyPart();
text2QP.setText(MimeUtility.encodeText("8BIT Content with umlaut \u00FC", "UTF-8", "Q"), "utf-8");
text2QP.setHeader("Content-Transfer-Encoding", "8BIT");
multipart.addBodyPart(text2QP);
// New main message, containing body part
MimeMessage message = new MimeMessage(session);
message.setRecipient(Message.RecipientType.TO, new InternetAddress("foo@localhost"));
message.setSubject("Subject ä", "UTF-8");
message.setFrom(new InternetAddress("foo@localhost"));
message.setContent(multipart);
message.saveChanges();
GreenMailUtil.sendMimeMessage(message);
final IMAPStore store = greenMail.getImap().createStore();
store.connect("foo@localhost", "pwd");
try {
Folder inboxFolder = store.getFolder("INBOX");
inboxFolder.open(Folder.READ_WRITE);
Message[] messages = inboxFolder.getMessages();
MimeMessage msg = (MimeMessage) messages[0];
assertThat(msg.getContentType().startsWith("multipart/alternative")).isTrue();
Multipart multipartReceived = (Multipart) msg.getContent();
assertThat(multipartReceived.getContentType().startsWith("multipart/alternative")).isTrue();
// QP-encoded
final BodyPart bodyPart0 = multipartReceived.getBodyPart(0);
assertThat(bodyPart0.getContentType()).isEqualTo("TEXT/JAVASCRIPT; charset=utf-8");
assertThat(textQP.getContent()).isEqualTo(EncodingUtil.toString((InputStream) bodyPart0.getContent(), StandardCharsets.UTF_8));
// 8-BIT-encoded
final BodyPart bodyPart1 = multipartReceived.getBodyPart(1);
assertThat(bodyPart1.getContentType()).isEqualTo("TEXT/HTML; charset=utf-8");
assertThat(bodyPart1.getContent()).isEqualTo(html.getContent()); // Fails
final BodyPart bodyPart2 = multipartReceived.getBodyPart(2);
assertThat(bodyPart2.getContentType()).isEqualTo("TEXT/PLAIN; charset=utf-8");
assertThat(bodyPart2.getContent()).isEqualTo(text.getContent());
final BodyPart bodyPart3 = multipartReceived.getBodyPart(3);
assertThat(bodyPart3.getContentType()).isEqualTo("TEXT/PLAIN; charset=utf-8");
assertThat(bodyPart3.getContent()).isEqualTo(text2QP.getContent());
} finally {
store.close();
}
}
@Test
public void testTextPlainWithUTF8AndGreenMailApi() throws MessagingException, IOException {
String content = "This is a test with ünicöde: \uD83C\uDF36";
String subject = "Some sübject";
GreenMailUtil.sendTextEmailTest(
"to@localhost", "from@localhost", subject,
content
);
MimeMessage msg = greenMail.getReceivedMessages()[0];
assertThat(msg.getSubject()).isEqualTo(subject);
assertThat(msg.getContentType()).isEqualTo("text/plain; charset=UTF-8");
assertThat(msg.getContent()).isEqualTo(content);
}
@Test(timeout = 10000)
public void testTextPlainWithUTF8SenderAndReceiverAndGreenMailApi() throws MessagingException, IOException {
// this intermediate `InternetAddress` is needed because of:
// https://stackoverflow.com/questions/31859901/java-mail-sender-address-gets-non-ascii-chars-removed/31865820#31865820
InternetAddress address = new InternetAddress("\"кирилица\" <to@localhost>");
InternetAddress toAddress = new InternetAddress(address.getAddress(), address.getPersonal());
address = new InternetAddress("\"кирилица\" <from@localhost>");
InternetAddress fromAddress = new InternetAddress(address.getAddress(), address.getPersonal());
sendMessage(fromAddress, toAddress);
MimeMessage receivedMessage = greenMail.getReceivedMessages()[0];
assertThat(receivedMessage.getFrom()[0].toString()).isEqualTo(fromAddress.toString());
assertThat(Arrays.stream(receivedMessage.getAllRecipients()).map(Object::toString).toArray())
.isEqualTo(new String[]{toAddress.toString()});
greenMail.setUser("to@localhost", "pwd");
final IMAPStore store = greenMail.getImap().createStore();
store.connect("to@localhost", "pwd");
try {
Folder inboxFolder = store.getFolder("INBOX");
inboxFolder.open(Folder.READ_ONLY);
Message[] messages = new Message[]{null};
MessageCountListener listener = new MessageCountListener() {
@Override
public void messagesRemoved(MessageCountEvent e) {
}
@Override
public void messagesAdded(MessageCountEvent e) {
messages[0] = e.getMessages()[0];
}
};
inboxFolder.addMessageCountListener(listener);
new Thread(() -> {
try {
Thread.sleep(100);
} catch (InterruptedException e1) {
// Ignore
}
try {
sendMessage(fromAddress, toAddress);
} catch (MessagingException ex) {
assertThat(false).isTrue();
}
}).start();
((IMAPFolder) inboxFolder).idle(true);
assertThat(messages[0].getFrom()[0].toString()).isEqualTo(fromAddress.toString());
assertThat(Arrays.stream(messages[0].getAllRecipients()).map(Object::toString).toArray())
.isEqualTo(new String[]{toAddress.toString()});
inboxFolder.close();
} finally {
store.close();
}
}
private void sendMessage(InternetAddress fromAddress, InternetAddress toAddress) throws MessagingException {
final Session session = greenMail.getSmtp().createSession();
MimeMessage message = new MimeMessage(session);
message.setRecipient(Message.RecipientType.TO, toAddress);
message.setSubject("Some subject", "UTF-8");
message.setFrom(fromAddress);
message.setContent("This is a test", "text/plain; charset=UTF-8");
message.saveChanges();
GreenMailUtil.sendMimeMessage(message);
}
@Test
public void testAttachmentWithLongEncodedUTF8Name() throws MessagingException, IOException {
// Prepare mail
greenMail.setUser("to@localhost", "pwd");
String fileName = "кирилица testimage_ünicöde_\uD83C\uDF36";
final String fileNameEncoded = MimeUtility.encodeText(fileName);
GreenMailUtil.sendAttachmentEmail(
"to@localhost", "from@localhost", "subject", "body",
new byte[]{0, 1, 2}, "image/gif",
fileNameEncoded,
"testimage_description", greenMail.getSmtp().getServerSetup());
greenMail.waitForIncomingEmail(1);
// Verify
final IMAPStore store = greenMail.getImap().createStore();
store.connect("to@localhost", "pwd");
try {
Folder inboxFolder = store.getFolder("INBOX");
inboxFolder.open(Folder.READ_ONLY);
try {
Message[] messages = inboxFolder.getMessages();
assertThat(messages).hasSize(1);
final MimeMultipart content = (MimeMultipart) messages[0].getContent();
assertThat(messages[0].getContent() != null).isTrue();
String receivedFileName = "";
for (int i = 0; i < content.getCount(); i++) {
final MimeBodyPart bodyPart = (MimeBodyPart) content.getBodyPart(i);
if (bodyPart.getContentType().startsWith("IMAGE/GIF")) {
receivedFileName = bodyPart.getFileName();
}
}
assertThat(receivedFileName).isEqualTo(fileNameEncoded);
assertThat(MimeUtility.decodeText(receivedFileName)).isEqualTo(fileName);
} finally {
inboxFolder.close();
}
} finally {
store.close();
}
}
}