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

View file

@ -761,6 +761,33 @@ public class SmtpTransportTest {
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,
MessagingException {
return startServerAndCreateSmtpTransport(server, AuthType.PLAIN, ConnectionSecurity.NONE);