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.assertTrue;
22  
23  import java.io.IOException;
24  import java.net.InetAddress;
25  
26  import org.junit.jupiter.api.Test;
27  
28  /**
29   *
30   * The POP3* tests all presume the existence of the following parameters: mailserver: localhost (running on the default port 110) account: username=test;
31   * password=password account: username=alwaysempty; password=password. mail: At least four emails in the test account and zero emails in the alwaysempty account
32   *
33   * If this won't work for you, you can change these parameters in the TestSetupParameters class.
34   *
35   * 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
36   * more standards-compliant than others.
37   */
38  class POP3ClientTest {
39      POP3Client p;
40  
41      String user = POP3Constants.user;
42      String emptyUser = POP3Constants.emptyuser;
43      String password = POP3Constants.password;
44      String mailhost = POP3Constants.mailhost;
45  
46      private void connect() throws Exception {
47          p.connect(InetAddress.getByName(mailhost));
48          assertTrue(p.isConnected());
49          assertEquals(POP3.AUTHORIZATION_STATE, p.getState());
50      }
51  
52      private void login() throws Exception {
53          assertTrue(p.login(user, password));
54          assertEquals(POP3.TRANSACTION_STATE, p.getState());
55      }
56  
57      private void reset() throws IOException {
58          // Case where this is the first time reset is called
59          if (p == null) {
60              // Do nothing
61          } else if (p.isConnected()) {
62              p.disconnect();
63          }
64          p = null;
65          p = new POP3Client();
66      }
67  
68      @Test
69      void testInvalidLoginWithBadName() throws Exception {
70          reset();
71          connect();
72  
73          // Try with an invalid user that doesn't exist
74          assertFalse(p.login("badusername", password));
75      }
76  
77      @Test
78      void testInvalidLoginWithBadPassword() throws Exception {
79          reset();
80          connect();
81  
82          // Try with a bad password
83          assertFalse(p.login(user, "badpassword"));
84      }
85  
86      /*
87       * Test to try to run the login method from the disconnected, transaction and update states
88       */
89      @Test
90      void testLoginFromWrongState() throws Exception {
91          reset();
92  
93          // Not currently connected, not in authorization state
94          // Try to login with good name/password
95          assertFalse(p.login(user, password));
96  
97          // Now connect and set the state to 'transaction' and try again
98          connect();
99          p.setState(POP3.TRANSACTION_STATE);
100         assertFalse(p.login(user, password));
101         p.disconnect();
102 
103         // Now connect and set the state to 'update' and try again
104         connect();
105         p.setState(POP3.UPDATE_STATE);
106         assertFalse(p.login(user, password));
107         p.disconnect();
108     }
109 
110     @Test
111     void testLogoutFromAllStates() throws Exception {
112         // From 'transaction' state
113         reset();
114         connect();
115         login();
116         assertTrue(p.logout());
117         assertEquals(POP3.UPDATE_STATE, p.getState());
118 
119         // From 'update' state
120         reset();
121         connect();
122         login();
123         p.setState(POP3.UPDATE_STATE);
124         assertTrue(p.logout());
125     }
126 
127     /*
128      * Simple test to logon to a valid server using a valid user name and password.
129      */
130     @Test
131     void testValidLoginWithNameAndPassword() throws Exception {
132         reset();
133         connect();
134 
135         // Try with a valid user
136         login();
137     }
138 }