JAMES-2652 Add Overflow test for POP3 RETR command
Project: http://git-wip-us.apache.org/repos/asf/james-project/repo Commit: http://git-wip-us.apache.org/repos/asf/james-project/commit/7329ac6e Tree: http://git-wip-us.apache.org/repos/asf/james-project/tree/7329ac6e Diff: http://git-wip-us.apache.org/repos/asf/james-project/diff/7329ac6e Branch: refs/heads/master Commit: 7329ac6e7f7389abb695a73596d99fc62759dac2 Parents: fb43afc Author: Gautier DI FOLCO <gdifo...@linagora.com> Authored: Fri Jan 25 15:34:09 2019 +0100 Committer: Benoit Tellier <btell...@linagora.com> Committed: Wed Jan 30 17:55:50 2019 +0700 ---------------------------------------------------------------------- protocols/pop3/pom.xml | 28 +++++++-- .../protocols/pop3/core/RetrCmdHandler.java | 4 +- .../protocols/pop3/core/RetrCmdHandlerTest.java | 63 ++++++++++++++++++++ 3 files changed, 90 insertions(+), 5 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/james-project/blob/7329ac6e/protocols/pop3/pom.xml ---------------------------------------------------------------------- diff --git a/protocols/pop3/pom.xml b/protocols/pop3/pom.xml index 2d3479b..4c579ba 100644 --- a/protocols/pop3/pom.xml +++ b/protocols/pop3/pom.xml @@ -58,13 +58,33 @@ <scope>test</scope> </dependency> <dependency> - <groupId>junit</groupId> - <artifactId>junit</artifactId> + <groupId>org.assertj</groupId> + <artifactId>assertj-core</artifactId> <scope>test</scope> </dependency> <dependency> - <groupId>org.assertj</groupId> - <artifactId>assertj-core</artifactId> + <groupId>org.junit.jupiter</groupId> + <artifactId>junit-jupiter-engine</artifactId> + <scope>test</scope> + </dependency> + <dependency> + <groupId>org.junit.jupiter</groupId> + <artifactId>junit-jupiter-params</artifactId> + <scope>test</scope> + </dependency> + <dependency> + <groupId>org.junit.platform</groupId> + <artifactId>junit-platform-launcher</artifactId> + <scope>test</scope> + </dependency> + <dependency> + <groupId>org.junit.vintage</groupId> + <artifactId>junit-vintage-engine</artifactId> + <scope>test</scope> + </dependency> + <dependency> + <groupId>org.mockito</groupId> + <artifactId>mockito-core</artifactId> <scope>test</scope> </dependency> </dependencies> http://git-wip-us.apache.org/repos/asf/james-project/blob/7329ac6e/protocols/pop3/src/main/java/org/apache/james/protocols/pop3/core/RetrCmdHandler.java ---------------------------------------------------------------------- diff --git a/protocols/pop3/src/main/java/org/apache/james/protocols/pop3/core/RetrCmdHandler.java b/protocols/pop3/src/main/java/org/apache/james/protocols/pop3/core/RetrCmdHandler.java index 53b84d7..65bf8e0 100644 --- a/protocols/pop3/src/main/java/org/apache/james/protocols/pop3/core/RetrCmdHandler.java +++ b/protocols/pop3/src/main/java/org/apache/james/protocols/pop3/core/RetrCmdHandler.java @@ -35,6 +35,7 @@ import org.apache.james.protocols.pop3.POP3Session; import org.apache.james.protocols.pop3.POP3StreamResponse; import org.apache.james.protocols.pop3.mailbox.MessageMetaData; +import com.google.common.annotations.VisibleForTesting; import com.google.common.collect.ImmutableSet; /** @@ -43,7 +44,8 @@ import com.google.common.collect.ImmutableSet; public class RetrCmdHandler implements CommandHandler<POP3Session> { private static final Collection<String> COMMANDS = ImmutableSet.of("RETR"); - private static final Response SYNTAX_ERROR = new POP3Response(POP3Response.ERR_RESPONSE, "Usage: RETR [mail number]").immutable(); + @VisibleForTesting + static final Response SYNTAX_ERROR = new POP3Response(POP3Response.ERR_RESPONSE, "Usage: RETR [mail number]").immutable(); private static final Response ERROR_MESSAGE_RETRIEVE = new POP3Response(POP3Response.ERR_RESPONSE, "Error while retrieving message.").immutable(); @Override http://git-wip-us.apache.org/repos/asf/james-project/blob/7329ac6e/protocols/pop3/src/test/java/org/apache/james/protocols/pop3/core/RetrCmdHandlerTest.java ---------------------------------------------------------------------- diff --git a/protocols/pop3/src/test/java/org/apache/james/protocols/pop3/core/RetrCmdHandlerTest.java b/protocols/pop3/src/test/java/org/apache/james/protocols/pop3/core/RetrCmdHandlerTest.java new file mode 100644 index 0000000..92d1d6b --- /dev/null +++ b/protocols/pop3/src/test/java/org/apache/james/protocols/pop3/core/RetrCmdHandlerTest.java @@ -0,0 +1,63 @@ +/**************************************************************** + * Licensed to the Apache Software Foundation (ASF) under one * + * or more contributor license agreements. See the NOTICE file * + * distributed with this work for additional information * + * regarding copyright ownership. The ASF licenses this file * + * to you under the Apache License, Version 2.0 (the * + * "License"); you may not use this file except in compliance * + * with the License. You may obtain a copy of the License at * + * * + * http://www.apache.org/licenses/LICENSE-2.0 * + * * + * Unless required by applicable law or agreed to in writing, * + * software distributed under the License is distributed on an * + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * + * KIND, either express or implied. See the License for the * + * specific language governing permissions and limitations * + * under the License. * + ****************************************************************/ + +package org.apache.james.protocols.pop3.core; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +import org.apache.james.protocols.api.Request; +import org.apache.james.protocols.pop3.POP3Session; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.ValueSource; + +import java.util.Collections; +import java.util.stream.Collectors; + +class RetrCmdHandlerTest { + + @ParameterizedTest + @ValueSource(ints = {8, 16, 32, 64, 128, 256}) + void onCommandShouldNotThrowOnMessageHexNumberOverflow(int pad) { + POP3Session session = mock(POP3Session.class); + when(session.getHandlerState()).thenReturn(POP3Session.TRANSACTION); + + Request request = mock(Request.class); + String overflowedNumber = Collections.nCopies(pad, "\\xff").stream().collect(Collectors.joining()); + when(request.getArgument()).thenReturn(overflowedNumber); + + assertThat(new RetrCmdHandler().onCommand(session, request)) + .isEqualTo(RetrCmdHandler.SYNTAX_ERROR); + } + + @Test + void onCommandShouldNotThrowOnMessageDecNumberOverflow() { + POP3Session session = mock(POP3Session.class); + when(session.getHandlerState()).thenReturn(POP3Session.TRANSACTION); + + Request request = mock(Request.class); + String overflowedNumber = Long.toString(Long.MAX_VALUE); + when(request.getArgument()).thenReturn(overflowedNumber); + + assertThat(new RetrCmdHandler().onCommand(session, request)) + .isEqualTo(RetrCmdHandler.SYNTAX_ERROR); + } +} --------------------------------------------------------------------- To unsubscribe, e-mail: server-dev-unsubscr...@james.apache.org For additional commands, e-mail: server-dev-h...@james.apache.org