001: /****************************************************************
002: * Licensed to the Apache Software Foundation (ASF) under one *
003: * or more contributor license agreements. See the NOTICE file *
004: * distributed with this work for additional information *
005: * regarding copyright ownership. The ASF licenses this file *
006: * to you under the Apache License, Version 2.0 (the *
007: * "License"); you may not use this file except in compliance *
008: * with the License. You may obtain a copy of the License at *
009: * *
010: * http://www.apache.org/licenses/LICENSE-2.0 *
011: * *
012: * Unless required by applicable law or agreed to in writing, *
013: * software distributed under the License is distributed on an *
014: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY *
015: * KIND, either express or implied. See the License for the *
016: * specific language governing permissions and limitations *
017: * under the License. *
018: ****************************************************************/package org.apache.james.pop3server;
019:
020: import org.apache.avalon.cornerstone.services.sockets.SocketManager;
021: import org.apache.avalon.cornerstone.services.threads.ThreadManager;
022: import org.apache.avalon.framework.container.ContainerUtil;
023: import org.apache.commons.net.pop3.POP3Client;
024: import org.apache.commons.net.pop3.POP3MessageInfo;
025: import org.apache.james.core.MailImpl;
026: import org.apache.james.core.MimeMessageCopyOnWriteProxy;
027: import org.apache.james.core.MimeMessageInputStreamSource;
028: import org.apache.james.services.JamesConnectionManager;
029: import org.apache.james.services.MailServer;
030: import org.apache.james.services.UsersRepository;
031: import org.apache.james.test.mock.avalon.MockLogger;
032: import org.apache.james.test.mock.avalon.MockServiceManager;
033: import org.apache.james.test.mock.avalon.MockSocketManager;
034: import org.apache.james.test.mock.avalon.MockThreadManager;
035: import org.apache.james.test.mock.james.MockMailRepository;
036: import org.apache.james.test.mock.james.MockMailServer;
037: import org.apache.james.test.util.Util;
038: import org.apache.james.userrepository.MockUsersRepository;
039: import org.apache.james.util.connection.SimpleConnectionManager;
040: import org.apache.mailet.MailAddress;
041:
042: import javax.mail.MessagingException;
043: import javax.mail.internet.MimeMessage;
044: import javax.mail.util.SharedByteArrayInputStream;
045:
046: import java.io.Reader;
047: import java.util.ArrayList;
048:
049: import junit.framework.TestCase;
050:
051: /**
052: * Tests the org.apache.james.smtpserver.SMTPServer unit
053: */
054: public class POP3ServerTest extends TestCase {
055: private int m_pop3ListenerPort = Util.getNonPrivilegedPort();
056:
057: private MockMailServer m_mailServer;
058:
059: private POP3TestConfiguration m_testConfiguration;
060:
061: private POP3Server m_pop3Server;
062:
063: private MockUsersRepository m_usersRepository = new MockUsersRepository();
064: private POP3Client m_pop3Protocol = null;
065:
066: public POP3ServerTest() {
067: super ("POP3ServerTest");
068: }
069:
070: protected void setUp() throws Exception {
071: m_pop3Server = new POP3Server();
072: ContainerUtil.enableLogging(m_pop3Server, new MockLogger());
073: ContainerUtil.service(m_pop3Server, setUpServiceManager());
074: m_testConfiguration = new POP3TestConfiguration(
075: m_pop3ListenerPort);
076: }
077:
078: private void finishSetUp(POP3TestConfiguration testConfiguration)
079: throws Exception {
080: testConfiguration.init();
081: ContainerUtil.configure(m_pop3Server, testConfiguration);
082: ContainerUtil.initialize(m_pop3Server);
083: }
084:
085: private MockServiceManager setUpServiceManager() {
086: MockServiceManager serviceManager = new MockServiceManager();
087: SimpleConnectionManager connectionManager = new SimpleConnectionManager();
088: ContainerUtil
089: .enableLogging(connectionManager, new MockLogger());
090: serviceManager.put(JamesConnectionManager.ROLE,
091: connectionManager);
092: m_mailServer = new MockMailServer();
093: serviceManager.put(MailServer.ROLE, m_mailServer);
094: serviceManager.put(UsersRepository.ROLE, m_usersRepository);
095: serviceManager.put(SocketManager.ROLE, new MockSocketManager(
096: m_pop3ListenerPort));
097: serviceManager.put(ThreadManager.ROLE, new MockThreadManager());
098: return serviceManager;
099: }
100:
101: protected void tearDown() throws Exception {
102: super .tearDown();
103: if (m_pop3Protocol != null) {
104: m_pop3Protocol.sendCommand("quit");
105: m_pop3Protocol.disconnect();
106: }
107: m_pop3Server.dispose();
108: }
109:
110: public void testAuthenticationFail() throws Exception {
111: finishSetUp(m_testConfiguration);
112:
113: m_pop3Protocol = new POP3Client();
114: m_pop3Protocol.connect("127.0.0.1", m_pop3ListenerPort);
115:
116: m_usersRepository.addUser("known", "test2");
117:
118: m_pop3Protocol.login("known", "test");
119: assertEquals(0, m_pop3Protocol.getState());
120: assertTrue(m_pop3Protocol.getReplyString().startsWith("-ERR"));
121: }
122:
123: public void testUnknownUser() throws Exception {
124: finishSetUp(m_testConfiguration);
125:
126: m_pop3Protocol = new POP3Client();
127: m_pop3Protocol.connect("127.0.0.1", m_pop3ListenerPort);
128:
129: m_pop3Protocol.login("unknown", "test");
130: assertEquals(0, m_pop3Protocol.getState());
131: assertTrue(m_pop3Protocol.getReplyString().startsWith("-ERR"));
132: }
133:
134: public void testKnownUserEmptyInbox() throws Exception {
135: finishSetUp(m_testConfiguration);
136:
137: m_pop3Protocol = new POP3Client();
138: m_pop3Protocol.connect("127.0.0.1", m_pop3ListenerPort);
139:
140: m_usersRepository.addUser("foo", "bar");
141: m_mailServer.setUserInbox("foo", new MockMailRepository());
142:
143: m_pop3Protocol.login("foo", "bar");
144: System.err.println(m_pop3Protocol.getState());
145: assertEquals(1, m_pop3Protocol.getState());
146:
147: POP3MessageInfo[] entries = m_pop3Protocol.listMessages();
148: assertEquals(1, m_pop3Protocol.getState());
149:
150: assertNotNull(entries);
151: assertEquals(entries.length, 0);
152: }
153:
154: public void testNotAsciiCharsInPassword() throws Exception {
155: finishSetUp(m_testConfiguration);
156:
157: m_pop3Protocol = new POP3Client();
158: m_pop3Protocol.connect("127.0.0.1", m_pop3ListenerPort);
159:
160: String pass = "bar" + (new String(new char[] { 200, 210 }))
161: + "foo";
162: m_usersRepository.addUser("foo", pass);
163: m_mailServer.setUserInbox("foo", new MockMailRepository());
164:
165: m_pop3Protocol.login("foo", pass);
166: assertEquals(1, m_pop3Protocol.getState());
167: }
168:
169: public void testKnownUserInboxWithMessages() throws Exception {
170: finishSetUp(m_testConfiguration);
171:
172: m_pop3Protocol = new POP3Client();
173: m_pop3Protocol.connect("127.0.0.1", m_pop3ListenerPort);
174:
175: m_usersRepository.addUser("foo2", "bar2");
176: MockMailRepository mailRep = new MockMailRepository();
177:
178: setupTestMails(mailRep);
179:
180: m_mailServer.setUserInbox("foo2", mailRep);
181:
182: m_pop3Protocol.login("foo2", "bar2");
183: assertEquals(1, m_pop3Protocol.getState());
184:
185: POP3MessageInfo[] entries = m_pop3Protocol.listMessages();
186:
187: assertNotNull(entries);
188: assertEquals(2, entries.length);
189: assertEquals(1, m_pop3Protocol.getState());
190:
191: Reader r = m_pop3Protocol.retrieveMessageTop(entries[0].number,
192: 0);
193: assertNotNull(r);
194: r.close();
195:
196: Reader r2 = m_pop3Protocol.retrieveMessage(entries[0].number);
197: assertNotNull(r2);
198: r2.close();
199:
200: boolean deleted = m_pop3Protocol
201: .deleteMessage(entries[0].number);
202:
203: assertTrue(deleted);
204: assertEquals(1, m_pop3Protocol.getState());
205:
206: m_pop3Protocol.sendCommand("quit");
207: m_pop3Protocol.disconnect();
208:
209: m_pop3Protocol.connect("127.0.0.1", m_pop3ListenerPort);
210:
211: m_pop3Protocol.login("foo2", "bar2");
212: assertEquals(1, m_pop3Protocol.getState());
213:
214: entries = null;
215:
216: POP3MessageInfo stats = m_pop3Protocol.status();
217: assertEquals(92, stats.size);
218: assertEquals(1, stats.number);
219:
220: entries = m_pop3Protocol.listMessages();
221:
222: assertNotNull(entries);
223: assertEquals(1, entries.length);
224: assertEquals(1, m_pop3Protocol.getState());
225:
226: Reader r3 = m_pop3Protocol.retrieveMessageTop(
227: entries[0].number, 0);
228: assertNotNull(r3);
229: r3.close();
230: }
231:
232: private void setupTestMails(MockMailRepository mailRep)
233: throws MessagingException {
234: ArrayList recipients = new ArrayList();
235: recipients.add(new MailAddress("recipient@test.com"));
236: MimeMessage mw = new MimeMessageCopyOnWriteProxy(
237: new MimeMessageInputStreamSource(
238: "test",
239: new SharedByteArrayInputStream(
240: ("Return-path: return@test.com\r\n"
241: + "Content-Transfer-Encoding: plain\r\n"
242: + "Subject: test\r\n\r\n"
243: + "Body Text\r\n").getBytes())));
244: MailImpl m = new MailImpl("name", new MailAddress(
245: "from@test.com"), recipients, mw);
246: mailRep.store(m);
247: MimeMessage mw2 = new MimeMessageCopyOnWriteProxy(
248: new MimeMessageInputStreamSource("test2",
249: new SharedByteArrayInputStream(("").getBytes())));
250: MailImpl mailimpl2 = new MailImpl("name2", new MailAddress(
251: "from@test.com"), recipients, mw2);
252: mailRep.store(mailimpl2);
253: m.dispose();
254: mailimpl2.dispose();
255: }
256:
257: public void testTwoSimultaneousMails() throws Exception {
258: finishSetUp(m_testConfiguration);
259:
260: // make two user/repositories, open both
261: m_usersRepository.addUser("foo1", "bar1");
262: MockMailRepository mailRep1 = new MockMailRepository();
263: setupTestMails(mailRep1);
264: m_mailServer.setUserInbox("foo1", mailRep1);
265:
266: m_usersRepository.addUser("foo2", "bar2");
267: MockMailRepository mailRep2 = new MockMailRepository();
268: //do not setupTestMails, this is done later
269: m_mailServer.setUserInbox("foo2", mailRep2);
270:
271: POP3Client pop3Protocol2 = null;
272: try {
273: // open two connections
274: m_pop3Protocol = new POP3Client();
275: m_pop3Protocol.connect("127.0.0.1", m_pop3ListenerPort);
276: pop3Protocol2 = new POP3Client();
277: pop3Protocol2.connect("127.0.0.1", m_pop3ListenerPort);
278:
279: assertEquals("first connection taken", 0, m_pop3Protocol
280: .getState());
281: assertEquals("second connection taken", 0, pop3Protocol2
282: .getState());
283:
284: // open two accounts
285: m_pop3Protocol.login("foo1", "bar1");
286:
287: pop3Protocol2.login("foo2", "bar2");
288:
289: POP3MessageInfo[] entries = m_pop3Protocol.listMessages();
290: assertEquals("foo1 has mails", 2, entries.length);
291:
292: entries = pop3Protocol2.listMessages();
293: assertEquals("foo2 has no mails", 0, entries.length);
294:
295: } finally {
296: // put both to rest, field var is handled by tearDown()
297: if (pop3Protocol2 != null) {
298: pop3Protocol2.sendCommand("quit");
299: pop3Protocol2.disconnect();
300: }
301: }
302: }
303:
304: }
|