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