Added more test and removed some unused line

This commit is contained in:
yesalam 2017-03-23 22:12:17 +05:30 committed by Vincent Breitmoser
parent 8a552d46a0
commit 65b66a05fd
2 changed files with 48 additions and 20 deletions

View file

@ -81,7 +81,6 @@ public class SmtpTransport extends Transport {
private int largestAcceptableMessage; private int largestAcceptableMessage;
private boolean retryXoauthWithNewToken; private boolean retryXoauthWithNewToken;
private boolean isPipeliningSupported; private boolean isPipeliningSupported;
private Queue<String> pipelinedCommand;
public SmtpTransport(StoreConfig storeConfig, TrustedSocketFactory trustedSocketFactory, public SmtpTransport(StoreConfig storeConfig, TrustedSocketFactory trustedSocketFactory,
@ -440,23 +439,21 @@ public class SmtpTransport extends Transport {
try { try {
String fromAddress = from[0].getAddress(); String fromAddress = from[0].getAddress();
if (isPipeliningSupported) { if (isPipeliningSupported) {
pipelinedCommand = new LinkedList<>(); Queue<String> pipelinedCommands = new LinkedList<>();
if (is8bitEncodingAllowed) { if (is8bitEncodingAllowed) {
executeCommandPipelined("MAIL FROM:<%s> BODY=8BITMIME", fromAddress); pipelinedCommands.add(String.format("MAIL FROM:<%s> BODY=8BITMIME", fromAddress));
} else { } else {
executeCommandPipelined("MAIL FROM:<%s>", fromAddress); pipelinedCommands.add(String.format("MAIL FROM:<%s>", fromAddress));
} }
for (String address : addresses) { for (String address : addresses) {
executeCommandPipelined("RCPT TO:<%s>", address); pipelinedCommands.add(String.format("RCPT TO:<%s>", address));
} }
executeCommandPipelined("DATA"); pipelinedCommands.add("DATA");
CommandResponse commandResponse = readPipelinedResponse(); executePipelinedCommands(pipelinedCommands);
if (commandResponse.replyCode != 354) { readPipelinedResponse(pipelinedCommands);
String replyText = TextUtils.join(" ", commandResponse.results);
throw new NegativeSmtpReplyException(commandResponse.replyCode, replyText);
}
} else { } else {
if (is8bitEncodingAllowed) { if (is8bitEncodingAllowed) {
executeCommand("MAIL FROM:<%s> BODY=8BITMIME", fromAddress); executeCommand("MAIL FROM:<%s> BODY=8BITMIME", fromAddress);
@ -647,20 +644,18 @@ public class SmtpTransport extends Transport {
return line; return line;
} }
private void executeCommandPipelined(String format, Object... args) throws IOException { private void executePipelinedCommands(Queue<String> pipelinedCommands) throws IOException {
if (format != null) { for (String command : pipelinedCommands) {
String command = String.format(Locale.ROOT, format, args);
pipelinedCommand.add(command);
writeLine(command, false); writeLine(command, false);
} }
} }
private CommandResponse readPipelinedResponse() throws IOException, MessagingException { private void readPipelinedResponse(Queue<String> pipelinedCommands) throws IOException, MessagingException {
int noOfPipelinedResponse = pipelinedCommands.size();
String responseLine = null; String responseLine = null;
List<String> results = null; List<String> results = new ArrayList<>();
int noOfPipelinedResponse = pipelinedCommand.size();
while (noOfPipelinedResponse > 0) { while (noOfPipelinedResponse > 0) {
results = new ArrayList<>(); results.clear();
responseLine = readCommandResponseLine(results); responseLine = readCommandResponseLine(results);
try { try {
responseLineToCommandResponse(responseLine, results); responseLineToCommandResponse(responseLine, results);
@ -675,7 +670,13 @@ public class SmtpTransport extends Transport {
} }
noOfPipelinedResponse-- ; noOfPipelinedResponse-- ;
} }
return responseLineToCommandResponse(responseLine, results);
try {
responseLineToCommandResponse(responseLine, results);
} catch (NegativeSmtpReplyException exception) {
throw exception;
}
} }
private CommandResponse responseLineToCommandResponse(String line, List<String> results) throws MessagingException { private CommandResponse responseLineToCommandResponse(String line, List<String> results) throws MessagingException {

View file

@ -761,6 +761,33 @@ public class SmtpTransportTest {
server.verifyInteractionCompleted(); server.verifyInteractionCompleted();
} }
@Test
public void sendMessagePipelining_without354ReplyforData_shouldThrow() throws Exception {
Message message = getDefaultMessage();
MockSmtpServer server = createServerAndSetupForPlainAuthentication("PIPELINING");
server.expect("MAIL FROM:<user@localhost>");
server.expect("RCPT TO:<user2@localhost>");
server.expect("DATA");
server.output("250 OK");
server.output("550 remote mail to <user2@localhost> not allowed");
server.output("554 no valid recipients given");
server.expect("QUIT");
server.output("221 BYE");
server.closeConnection();
SmtpTransport transport = startServerAndCreateSmtpTransport(server);
try {
transport.sendMessage(message);
fail("Expected exception");
} catch (NegativeSmtpReplyException e) {
assertEquals(554, e.getReplyCode());
assertEquals("no valid recipients given", e.getReplyText());
}
server.verifyConnectionClosed();
server.verifyInteractionCompleted();
}
private SmtpTransport startServerAndCreateSmtpTransport(MockSmtpServer server) throws IOException, private SmtpTransport startServerAndCreateSmtpTransport(MockSmtpServer server) throws IOException,
MessagingException { MessagingException {
return startServerAndCreateSmtpTransport(server, AuthType.PLAIN, ConnectionSecurity.NONE); return startServerAndCreateSmtpTransport(server, AuthType.PLAIN, ConnectionSecurity.NONE);