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