View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *   http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  
18  package org.apache.commons.net.imap;
19  
20  import static org.junit.jupiter.api.Assertions.assertEquals;
21  import static org.junit.jupiter.api.Assertions.assertFalse;
22  import static org.junit.jupiter.api.Assertions.assertThrows;
23  import static org.junit.jupiter.api.Assertions.assertTrue;
24  
25  import java.io.IOException;
26  import java.util.stream.Stream;
27  
28  import org.apache.commons.net.MalformedServerReplyException;
29  import org.junit.jupiter.api.Test;
30  import org.junit.jupiter.params.ParameterizedTest;
31  import org.junit.jupiter.params.provider.Arguments;
32  import org.junit.jupiter.params.provider.MethodSource;
33  
34  public class IMAPReplyTest {
35  
36      private static Stream<String> invalidLiteralCommands() {
37          return Stream.of(
38                  "",
39                  "{",
40                  "}",
41                  "{}",
42                  "{foobar}",
43                  "STORE +FLAGS.SILENT \\DELETED {",
44                  "STORE +FLAGS.SILENT \\DELETED }",
45                  "STORE +FLAGS.SILENT \\DELETED {-1}",
46                  "STORE +FLAGS.SILENT \\DELETED {-10}",
47                  "STORE +FLAGS.SILENT \\DELETED {-2147483648}"
48          );
49      }
50  
51      private static Stream<Arguments> literalCommands() {
52          return Stream.of(
53                  Arguments.of(310, "A003 APPEND saved-messages (\\Seen) {310}"),
54                  Arguments.of(6, "A284 SEARCH CHARSET UTF-8 TEXT {6}"),
55                  Arguments.of(7, "FRED FOOBAR {7}"),
56                  Arguments.of(102856, "A044 BLURDYBLOOP {102856}"),
57                  Arguments.of(342, "* 12 FETCH (BODY[HEADER] {342}"),
58                  Arguments.of(0, "X999 LOGIN {0}"),
59                  Arguments.of(Integer.MAX_VALUE, "X999 LOGIN {2147483647}")
60          );
61      }
62  
63      @Test
64      public void getReplyCodeBadLine() throws IOException {
65          final String badLine = "A044 BAD No such command as \"FOOBAR\"";
66          assertEquals(IMAPReply.BAD, IMAPReply.getReplyCode(badLine));
67      }
68  
69      @Test
70      public void getReplyCodeContinuationLine() throws IOException {
71          final String continuationLine = "+ Ready for additional command text";
72          assertEquals(IMAPReply.CONT, IMAPReply.getReplyCode(continuationLine));
73      }
74  
75      @Test
76      public void getReplyCodeMalformedLine() {
77          final String malformedTaggedLine = "A064 FOO-BAR 0";
78          final MalformedServerReplyException replyException = assertThrows(MalformedServerReplyException.class, () -> IMAPReply.getReplyCode(malformedTaggedLine));
79          assertEquals("Received unexpected IMAP protocol response from server: 'A064 FOO-BAR 0'.", replyException.getMessage());
80      }
81  
82      @Test
83      public void getReplyCodeNoLine() throws IOException {
84          final String noLine = "A223 NO COPY failed: disk is full";
85          assertEquals(IMAPReply.NO, IMAPReply.getReplyCode(noLine));
86      }
87  
88      @Test
89      public void getReplyCodeOkLine() throws IOException {
90          final String okLine = "A001 OK LOGIN completed";
91          assertEquals(IMAPReply.OK, IMAPReply.getReplyCode(okLine));
92      }
93  
94      @Test
95      public void getUntaggedReplyCodeBadLine() throws IOException {
96          final String badLine = "* BAD Empty command line";
97          assertEquals(IMAPReply.BAD, IMAPReply.getUntaggedReplyCode(badLine));
98      }
99  
100     @Test
101     public void getUntaggedReplyCodeContinuationLine() throws IOException {
102         final String continuationLine = "+ Ready for additional command text";
103         assertEquals(IMAPReply.CONT, IMAPReply.getUntaggedReplyCode(continuationLine));
104     }
105 
106     @Test
107     public void getUntaggedReplyCodeMalformedLine() {
108         // invalid experimental comm response (missing X prefix)
109         final String malformedUntaggedLine = "* FOO-BAR hello-world";
110         final MalformedServerReplyException replyException = assertThrows(MalformedServerReplyException.class, () -> IMAPReply.getUntaggedReplyCode(malformedUntaggedLine));
111         assertEquals("Received unexpected IMAP protocol response from server: '* FOO-BAR hello-world'.", replyException.getMessage());
112     }
113 
114     @Test
115     public void getUntaggedReplyCodeNoLine() throws IOException {
116         final String noLine = "* NO Disk is 98% full, please delete unnecessary data";
117         assertEquals(IMAPReply.NO, IMAPReply.getUntaggedReplyCode(noLine));
118     }
119 
120     @Test
121     public void getUntaggedReplyCodeOkLine() throws IOException {
122         final String okLine = "* OK Salvage successful, no data lost";
123         assertEquals(IMAPReply.OK, IMAPReply.getUntaggedReplyCode(okLine));
124     }
125 
126     @Test
127     public void isContinuationReplyCode() {
128         final int replyCode = 3;
129         assertTrue(IMAPReply.isContinuation(replyCode));
130     }
131 
132     @Test
133     public void isContinuationReplyCodeInvalidCode() {
134         final int invalidContinuationReplyCode = 1;
135         assertFalse(IMAPReply.isContinuation(invalidContinuationReplyCode));
136     }
137 
138     @Test
139     public void isContinuationReplyLine() {
140         final String replyLine = "+FLAGS completed";
141         assertTrue(IMAPReply.isContinuation(replyLine));
142     }
143 
144     @Test
145     public void isContinuationReplyLineInvalidLine() {
146         final String invalidContinuationReplyLine = "* 22 EXPUNGE";
147         assertFalse(IMAPReply.isContinuation(invalidContinuationReplyLine));
148     }
149 
150     @Test
151     public void isSuccessReplyCode() {
152         final int successfulReplyCode = 0;
153         assertTrue(IMAPReply.isSuccess(successfulReplyCode));
154     }
155 
156     @Test
157     public void isSuccessReplyCodeUnsuccessfulCode() {
158         final int unsuccessfulReplyCode = 2;
159         assertFalse(IMAPReply.isSuccess(unsuccessfulReplyCode));
160     }
161 
162     @Test
163     public void isUntaggedReplyLine() {
164         final String replyLine = "* 18 EXISTS";
165         assertTrue(IMAPReply.isUntagged(replyLine));
166     }
167 
168     @Test
169     public void isUntaggedReplyLineInvalidLine() {
170         final String taggedLine = "a001 OK LOGOUT completed";
171         assertFalse(IMAPReply.isUntagged(taggedLine));
172     }
173 
174     @ParameterizedTest(name = "reply line `{1}` contains literal {0}")
175     @MethodSource("literalCommands")
176     public void literalCount(final int expectedLiteral, final String replyLine) {
177         assertEquals(expectedLiteral, IMAPReply.literalCount(replyLine));
178     }
179 
180     @ParameterizedTest(name = "reply line `{0}` does not contain any literal")
181     @MethodSource("invalidLiteralCommands")
182     public void literalCountInvalid(final String replyLine) {
183         assertEquals(-1, IMAPReply.literalCount(replyLine));
184     }
185 
186 }