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    *      https://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  package org.apache.commons.net.pop3;
18  
19  import static org.junit.jupiter.api.Assertions.assertEquals;
20  import static org.junit.jupiter.api.Assertions.assertFalse;
21  import static org.junit.jupiter.api.Assertions.assertNotNull;
22  import static org.junit.jupiter.api.Assertions.assertNull;
23  import static org.junit.jupiter.api.Assertions.assertTrue;
24  
25  import java.io.IOException;
26  import java.io.Reader;
27  import java.net.InetAddress;
28  
29  import org.junit.jupiter.api.Test;
30  
31  /**
32   *
33   * The POP3* tests all presume the existence of the following parameters: mailserver: localhost (running on the default port 110) account: username=test;
34   * password=password account: username=alwaysempty; password=password. mail: At least four emails in the test account and zero emails in the alwaysempty account
35   *
36   * If this won't work for you, you can change these parameters in the TestSetupParameters class.
37   *
38   * The tests were originally run on a default installation of James. Your mileage may vary based on the POP3 server you run the tests against. Some servers are
39   * more standards-compliant than others.
40   */
41  class POP3ClientCommandsTest {
42      POP3Client pop3Client;
43  
44      String user = POP3Constants.user;
45      String emptyUser = POP3Constants.emptyuser;
46      String password = POP3Constants.password;
47      String mailhost = POP3Constants.mailhost;
48  
49      private void connect() throws Exception {
50          pop3Client.connect(InetAddress.getByName(mailhost));
51          assertTrue(pop3Client.isConnected());
52          assertEquals(POP3.AUTHORIZATION_STATE, pop3Client.getState());
53      }
54  
55      private void login() throws Exception {
56          assertTrue(pop3Client.login(user, password));
57          assertEquals(POP3.TRANSACTION_STATE, pop3Client.getState());
58      }
59  
60      private void reset() throws IOException {
61          // Case where this is the first time reset is called
62          if (pop3Client == null) {
63              // Do nothing
64          } else if (pop3Client.isConnected()) {
65              pop3Client.disconnect();
66          }
67          pop3Client = null;
68          pop3Client = new POP3Client();
69      }
70  
71      @Test
72      void testDelete() throws Exception {
73          reset();
74          connect();
75          login();
76          // Get the original number of messages
77          POP3MessageInfo[] msg = pop3Client.listMessages();
78          final int numMessages = msg.length;
79          int numDeleted = 0;
80  
81          // Now delete some and logout
82          for (int i = 0; i < numMessages - 3; i++) {
83              pop3Client.deleteMessage(i + 1);
84              numDeleted++;
85          }
86          // Check to see that they are marked as deleted
87          assertEquals(numMessages, numDeleted + 3);
88  
89          // Logout and come back in
90          pop3Client.logout();
91          reset();
92          connect();
93          login();
94  
95          // Get the new number of messages, because of
96          // reset, new number should match old number
97          msg = pop3Client.listMessages();
98          assertEquals(numMessages - numDeleted, msg.length);
99      }
100 
101     @Test
102     void testDeleteWithReset() throws Exception {
103         reset();
104         connect();
105         login();
106         // Get the original number of messages
107         POP3MessageInfo[] msg = pop3Client.listMessages();
108         final int numMessages = msg.length;
109         int numDeleted = 0;
110 
111         // Now delete some and logout
112         for (int i = 0; i < numMessages - 1; i++) {
113             pop3Client.deleteMessage(i + 1);
114             numDeleted++;
115         }
116         // Check to see that they are marked as deleted
117         assertEquals(numMessages, numDeleted + 1);
118 
119         // Now reset to unmark the messages as deleted
120         pop3Client.reset();
121 
122         // Logout and come back in
123         pop3Client.logout();
124         reset();
125         connect();
126         login();
127 
128         // Get the new number of messages, because of
129         // reset, new number should match old number
130         msg = pop3Client.listMessages();
131         assertEquals(numMessages, msg.length);
132     }
133 
134     @Test
135     void testListMessageOnEmptyMailbox() throws Exception {
136         reset();
137         connect();
138         assertTrue(pop3Client.login(emptyUser, password));
139 
140         // The first message is always at index 1
141         final POP3MessageInfo msg = pop3Client.listMessage(1);
142         assertNull(msg);
143     }
144 
145     @Test
146     void testListMessageOnFullMailbox() throws Exception {
147         reset();
148         connect();
149         login();
150 
151         // The first message is always at index 1
152         POP3MessageInfo msg = pop3Client.listMessage(1);
153         assertNotNull(msg);
154         assertEquals(1, msg.number);
155         assertTrue(msg.size > 0);
156         assertNull(msg.identifier);
157 
158         // Now retrieve a message from index 0
159         msg = pop3Client.listMessage(0);
160         assertNull(msg);
161 
162         // Now retrieve a msg that is not there
163         msg = pop3Client.listMessage(100000);
164         assertNull(msg);
165 
166         // Now retrieve a msg with a negative index
167         msg = pop3Client.listMessage(-2);
168         assertNull(msg);
169 
170         // Now try to get a valid message from the update state
171         pop3Client.setState(POP3.UPDATE_STATE);
172         msg = pop3Client.listMessage(1);
173         assertNull(msg);
174     }
175 
176     @Test
177     void testListMessagesOnEmptyMailbox() throws Exception {
178         reset();
179         connect();
180         assertTrue(pop3Client.login(emptyUser, password));
181 
182         POP3MessageInfo[] msg = pop3Client.listMessages();
183         assertEquals(0, msg.length);
184 
185         // Now test from the update state
186         pop3Client.setState(POP3.UPDATE_STATE);
187         msg = pop3Client.listMessages();
188         assertNull(msg);
189     }
190 
191     @Test
192     void testListMessagesOnFullMailbox() throws Exception {
193         reset();
194         connect();
195         login();
196 
197         POP3MessageInfo[] msg = pop3Client.listMessages();
198         assertTrue(msg.length > 0);
199 
200         for (int i = 0; i < msg.length; i++) {
201             assertNotNull(msg[i]);
202             assertEquals(i + 1, msg[i].number);
203             assertTrue(msg[i].size > 0);
204             assertNull(msg[i].identifier);
205         }
206 
207         // Now test from the update state
208         pop3Client.setState(POP3.UPDATE_STATE);
209         msg = pop3Client.listMessages();
210         assertNull(msg);
211     }
212 
213     @Test
214     void testListUniqueIdentifierOnEmptyMailbox() throws Exception {
215         reset();
216         connect();
217         assertTrue(pop3Client.login(emptyUser, password));
218 
219         // The first message is always at index 1
220         final POP3MessageInfo msg = pop3Client.listUniqueIdentifier(1);
221         assertNull(msg);
222     }
223 
224     @Test
225     void testListUniqueIDOnFullMailbox() throws Exception {
226         reset();
227         connect();
228         login();
229 
230         // The first message is always at index 1
231         POP3MessageInfo msg = pop3Client.listUniqueIdentifier(1);
232         assertNotNull(msg);
233         assertEquals(1, msg.number);
234         assertNotNull(msg.identifier);
235 
236         // Now retrieve a message from index 0
237         msg = pop3Client.listUniqueIdentifier(0);
238         assertNull(msg);
239 
240         // Now retrieve a msg that is not there
241         msg = pop3Client.listUniqueIdentifier(100000);
242         assertNull(msg);
243 
244         // Now retrieve a msg with a negative index
245         msg = pop3Client.listUniqueIdentifier(-2);
246         assertNull(msg);
247 
248         // Now try to get a valid message from the update state
249         pop3Client.setState(POP3.UPDATE_STATE);
250         msg = pop3Client.listUniqueIdentifier(1);
251         assertNull(msg);
252     }
253 
254     @Test
255     void testListUniqueIDsOnEmptyMailbox() throws Exception {
256         reset();
257         connect();
258         assertTrue(pop3Client.login(emptyUser, password));
259 
260         POP3MessageInfo[] msg = pop3Client.listUniqueIdentifiers();
261         assertEquals(0, msg.length);
262 
263         // Now test from the update state
264         pop3Client.setState(POP3.UPDATE_STATE);
265         msg = pop3Client.listUniqueIdentifiers();
266         assertNull(msg);
267     }
268 
269     @Test
270     void testListUniqueIDsOnFullMailbox() throws Exception {
271         reset();
272         connect();
273         login();
274 
275         POP3MessageInfo[] msg = pop3Client.listUniqueIdentifiers();
276         assertTrue(msg.length > 0);
277 
278         for (int i = 0; i < msg.length; i++) {
279             assertNotNull(msg[i]);
280             assertEquals(i + 1, msg[i].number);
281             assertNotNull(msg[i].identifier);
282         }
283 
284         // Now test from the update state
285         pop3Client.setState(POP3.UPDATE_STATE);
286         msg = pop3Client.listUniqueIdentifiers();
287         assertNull(msg);
288     }
289 
290     @Test
291     void testNoopCommand() throws Exception {
292         reset();
293         connect();
294 
295         // Should fail before authorization
296         assertFalse(pop3Client.noop());
297 
298         // Should pass in transaction state
299         login();
300         assertTrue(pop3Client.noop());
301 
302         // Should fail in update state
303         pop3Client.setState(POP3.UPDATE_STATE);
304         assertFalse(pop3Client.noop());
305     }
306 
307     @Test
308     void testResetAndDeleteShouldFails() throws Exception {
309         reset();
310         connect();
311         login();
312 
313         pop3Client.setState(POP3.UPDATE_STATE);
314         assertFalse(pop3Client.reset());
315 
316         assertFalse(pop3Client.deleteMessage(1));
317     }
318 
319     @Test
320     void testRetrieveMessageOnEmptyMailbox() throws Exception {
321         reset();
322         connect();
323         assertTrue(pop3Client.login(emptyUser, password));
324         assertNull(pop3Client.retrieveMessage(1));
325     }
326 
327     @Test
328     void testRetrieveMessageOnFullMailbox() throws Exception {
329         reset();
330         connect();
331         login();
332         int reportedSize = 0;
333         int actualSize = 0;
334 
335         final POP3MessageInfo[] msg = pop3Client.listMessages();
336         assertTrue(msg.length > 0);
337 
338         for (int i = msg.length; i > 0; i--) {
339             reportedSize = msg[i - 1].size;
340             final Reader r = pop3Client.retrieveMessage(i);
341             assertNotNull(r);
342 
343             int delaycount = 0;
344             if (!r.ready()) {
345                 // Give the reader time to get the message
346                 // from the server
347                 Thread.sleep(500);
348                 delaycount++;
349                 // but don't wait too long
350                 if (delaycount == 4) {
351                     break;
352                 }
353             }
354             while (r.ready()) {
355                 r.read();
356                 actualSize++;
357             }
358             // Due to variations in line termination
359             // on different platforms, the actual
360             // size may vary slightly. On Win2KPro, the
361             // actual size is 2 bytes larger than the reported
362             // size.
363             assertTrue(actualSize >= reportedSize);
364         }
365     }
366 
367     @Test
368     void testRetrieveMessageShouldFails() throws Exception {
369         reset();
370         connect();
371         login();
372 
373         // Try to get message 0
374         assertNull(pop3Client.retrieveMessage(0));
375 
376         // Try to get a negative message
377         assertNull(pop3Client.retrieveMessage(-2));
378 
379         // Try to get a message that is not there
380         assertNull(pop3Client.retrieveMessage(100000));
381 
382         // Change states and try to get a valid message
383         pop3Client.setState(POP3.UPDATE_STATE);
384         assertNull(pop3Client.retrieveMessage(1));
385     }
386 
387     @Test
388     void testRetrieveMessageTopOnEmptyMailbox() throws Exception {
389         reset();
390         connect();
391         assertTrue(pop3Client.login(emptyUser, password));
392         assertNull(pop3Client.retrieveMessageTop(1, 10));
393     }
394 
395     @Test
396     void testRetrieveMessageTopOnFullMailbox() throws Exception {
397         reset();
398         connect();
399         login();
400         final int numLines = 10;
401 
402         final POP3MessageInfo[] msg = pop3Client.listMessages();
403         assertTrue(msg.length > 0);
404 
405         for (int i = 0; i < msg.length; i++) {
406             try (Reader reader = pop3Client.retrieveMessageTop(i + 1, numLines)) {
407                 assertNotNull(reader);
408             }
409         }
410     }
411 
412     @Test
413     void testRetrieveMessageTopShouldFails() throws Exception {
414         reset();
415         connect();
416         login();
417 
418         // Try to get message 0
419         assertNull(pop3Client.retrieveMessageTop(0, 10));
420 
421         // Try to get a negative message
422         assertNull(pop3Client.retrieveMessageTop(-2, 10));
423 
424         // Try to get a message that is not there
425         assertNull(pop3Client.retrieveMessageTop(100000, 10));
426 
427         // Change states and try to get a valid message
428         pop3Client.setState(POP3.UPDATE_STATE);
429         assertNull(pop3Client.retrieveMessageTop(1, 10));
430     }
431 
432     @Test
433     void testRetrieveOverSizedMessageTopOnFullMailbox() throws Exception {
434         reset();
435         connect();
436         login();
437         int actualSize = 0;
438 
439         final POP3MessageInfo msg = pop3Client.listMessage(1);
440         final int reportedSize = msg.size;
441 
442         // Now try to retrieve more lines than exist in the message
443         final Reader r = pop3Client.retrieveMessageTop(1, 100000);
444         assertNotNull(r);
445 
446         int delaycount = 0;
447         while (!r.ready()) {
448             // Give the reader time to get the message
449             // from the server
450             Thread.sleep(500);
451             delaycount++;
452             // but don't wait too long
453             if (delaycount == 4) {
454                 break;
455             }
456         }
457         while (r.ready()) {
458             r.read();
459             actualSize++;
460         }
461         // Due to variations in line termination
462         // on different platforms, the actual
463         // size may vary slightly. On Win2KPro, the
464         // actual size is 2 bytes larger than the reported
465         // size.
466         assertTrue(actualSize >= reportedSize);
467     }
468 
469     @Test
470     void testStatus() throws Exception {
471         reset();
472         connect();
473 
474         // Should fail in authorization state
475         assertNull(pop3Client.status());
476 
477         // Should pass on a mailbox with mail in it
478         login();
479         final POP3MessageInfo msg = pop3Client.status();
480         assertTrue(msg.number > 0);
481         assertTrue(msg.size > 0);
482         assertNull(msg.identifier);
483         pop3Client.logout();
484 
485         // Should also pass on a mailbox with no mail in it
486         reset();
487         connect();
488         assertTrue(pop3Client.login(emptyUser, password));
489         final POP3MessageInfo msg2 = pop3Client.status();
490         assertEquals(0, msg2.number);
491         assertEquals(0, msg2.size);
492         assertNull(msg2.identifier);
493         pop3Client.logout();
494 
495         // Should fail in the 'update' state
496         reset();
497         connect();
498         login();
499         pop3Client.setState(POP3.UPDATE_STATE);
500         assertNull(pop3Client.status());
501     }
502 }