Repository: james-project Updated Branches: refs/heads/master a546da0ed -> 4135794a1
JAMES-2229 Create command will valid mailbox name when only contain delimiter Project: http://git-wip-us.apache.org/repos/asf/james-project/repo Commit: http://git-wip-us.apache.org/repos/asf/james-project/commit/fd69a44a Tree: http://git-wip-us.apache.org/repos/asf/james-project/tree/fd69a44a Diff: http://git-wip-us.apache.org/repos/asf/james-project/diff/fd69a44a Branch: refs/heads/master Commit: fd69a44a415228798db969a926c6c4f194d550e9 Parents: a546da0 Author: quynhn <[email protected]> Authored: Wed Nov 22 14:57:39 2017 +0700 Committer: benwa <[email protected]> Committed: Thu Nov 30 09:34:44 2017 +0700 ---------------------------------------------------------------------- .../imap/decode/parser/CreateCommandParser.java | 13 +++ .../decode/parser/CreateCommandParserTest.java | 87 ++++++++++++++++++++ 2 files changed, 100 insertions(+) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/james-project/blob/fd69a44a/protocols/imap/src/main/java/org/apache/james/imap/decode/parser/CreateCommandParser.java ---------------------------------------------------------------------- diff --git a/protocols/imap/src/main/java/org/apache/james/imap/decode/parser/CreateCommandParser.java b/protocols/imap/src/main/java/org/apache/james/imap/decode/parser/CreateCommandParser.java index 7aed344..b4cd72e 100644 --- a/protocols/imap/src/main/java/org/apache/james/imap/decode/parser/CreateCommandParser.java +++ b/protocols/imap/src/main/java/org/apache/james/imap/decode/parser/CreateCommandParser.java @@ -22,6 +22,7 @@ import org.apache.james.imap.api.ImapCommand; import org.apache.james.imap.api.ImapConstants; import org.apache.james.imap.api.ImapMessage; import org.apache.james.imap.api.ImapSessionUtils; +import org.apache.james.imap.api.display.HumanReadableText; import org.apache.james.imap.api.process.ImapSession; import org.apache.james.imap.decode.ImapRequestLineReader; import org.apache.james.imap.decode.base.AbstractImapCommandParser; @@ -29,6 +30,9 @@ import org.apache.james.imap.message.request.CreateRequest; import org.apache.james.mailbox.MailboxSession; import org.apache.james.protocols.imap.DecodingException; +import com.google.common.base.Splitter; +import com.google.common.base.Strings; + /** * Parse CREATE commands */ @@ -63,7 +67,16 @@ public class CreateCommandParser extends AbstractImapCommandParser { } } request.eol(); + assertMailboxNameJustContainDelimiter(mailboxName, mailboxSession.getPathDelimiter()); return new CreateRequest(command, mailboxName, tag); } + private void assertMailboxNameJustContainDelimiter(String mailboxName, char delimiter) throws DecodingException { + Splitter.on(delimiter) + .splitToList(mailboxName) + .stream() + .filter(s -> !Strings.isNullOrEmpty(s)) + .findAny() + .orElseThrow(() -> new DecodingException(HumanReadableText.ILLEGAL_ARGUMENTS, "Invalid mailbox name")); + } } http://git-wip-us.apache.org/repos/asf/james-project/blob/fd69a44a/protocols/imap/src/test/java/org/apache/james/imap/decode/parser/CreateCommandParserTest.java ---------------------------------------------------------------------- diff --git a/protocols/imap/src/test/java/org/apache/james/imap/decode/parser/CreateCommandParserTest.java b/protocols/imap/src/test/java/org/apache/james/imap/decode/parser/CreateCommandParserTest.java new file mode 100644 index 0000000..7f6a4f4 --- /dev/null +++ b/protocols/imap/src/test/java/org/apache/james/imap/decode/parser/CreateCommandParserTest.java @@ -0,0 +1,87 @@ +/**************************************************************** + * 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.imap.decode.parser; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +import java.io.ByteArrayInputStream; +import java.io.InputStream; +import java.io.OutputStream; + +import org.apache.james.imap.api.ImapCommand; +import org.apache.james.imap.api.ImapSessionUtils; +import org.apache.james.imap.api.process.ImapSession; +import org.apache.james.imap.decode.ImapRequestStreamLineReader; +import org.apache.james.imap.message.request.CreateRequest; +import org.apache.james.mailbox.MailboxSession; +import org.apache.james.mailbox.mock.MockMailboxSession; +import org.apache.james.protocols.imap.DecodingException; +import org.junit.Before; +import org.junit.Test; + +import com.google.common.base.Charsets; + +public class CreateCommandParserTest { + private static final OutputStream outputStream = null; + private static final ImapCommand command = ImapCommand.anyStateCommand("Command"); + private static final String TAG = "A1"; + + private ImapSession mockImapSession; + private MailboxSession mailboxSession; + private CreateCommandParser parser; + + @Before + public void setUp() throws Exception { + mockImapSession = mock(ImapSession.class); + mailboxSession = new MockMailboxSession("userName"); + + when(mockImapSession.getAttribute(ImapSessionUtils.MAILBOX_SESSION_ATTRIBUTE_SESSION_KEY)).thenReturn(mailboxSession); + + parser = new CreateCommandParser(); + } + + @Test(expected = DecodingException.class) + public void decodeShouldThrowWhenCommandHasEmptyMailbox() throws DecodingException { + InputStream inputStream = new ByteArrayInputStream(" \n".getBytes(Charsets.US_ASCII)); + ImapRequestStreamLineReader lineReader = new ImapRequestStreamLineReader(inputStream, outputStream); + + parser.decode(command, lineReader, TAG, mockImapSession); + } + + @Test(expected = DecodingException.class) + public void decodeShouldThrowWhenCommandHasOnlySeparatorMailbox() throws DecodingException { + InputStream inputStream = new ByteArrayInputStream("..\n".getBytes(Charsets.US_ASCII)); + ImapRequestStreamLineReader lineReader = new ImapRequestStreamLineReader(inputStream, outputStream); + + parser.decode(command, lineReader, TAG, mockImapSession); + } + + @Test + public void decodeShouldReturnCreateRequestWhenValidMailboxName() throws Exception { + InputStream inputStream = new ByteArrayInputStream(".AnyMailbox.\n".getBytes(Charsets.US_ASCII)); + ImapRequestStreamLineReader lineReader = new ImapRequestStreamLineReader(inputStream, outputStream); + + CreateRequest imapMessage = (CreateRequest)parser.decode(command, lineReader, TAG, mockImapSession); + assertThat(imapMessage.getMailboxName()).isEqualTo(".AnyMailbox"); + } + +} \ No newline at end of file --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
