KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > james > pop3server > POP3ServerTest


1 /****************************************************************
2  * Licensed to the Apache Software Foundation (ASF) under one *
3  * or more contributor license agreements. See the NOTICE file *
4  * distributed with this work for additional information *
5  * regarding copyright ownership. The ASF licenses this file *
6  * to you under the Apache License, Version 2.0 (the *
7  * "License"); you may not use this file except in compliance *
8  * with the License. You may obtain a copy of the License at *
9  * *
10  * http://www.apache.org/licenses/LICENSE-2.0 *
11  * *
12  * Unless required by applicable law or agreed to in writing, *
13  * software distributed under the License is distributed on an *
14  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY *
15  * KIND, either express or implied. See the License for the *
16  * specific language governing permissions and limitations *
17  * under the License. *
18  ****************************************************************/

19
20 package org.apache.james.pop3server;
21
22 import org.apache.avalon.cornerstone.services.sockets.SocketManager;
23 import org.apache.avalon.cornerstone.services.threads.ThreadManager;
24 import org.apache.avalon.framework.container.ContainerUtil;
25 import org.apache.avalon.framework.service.ServiceException;
26 import org.apache.commons.net.pop3.POP3Client;
27 import org.apache.commons.net.pop3.POP3MessageInfo;
28 import org.apache.james.core.MailImpl;
29 import org.apache.james.services.AbstractDNSServer;
30 import org.apache.james.services.DNSServer;
31 import org.apache.james.services.JamesConnectionManager;
32 import org.apache.james.services.MailRepository;
33 import org.apache.james.services.MailServer;
34 import org.apache.james.services.UsersRepository;
35 import org.apache.james.test.mock.avalon.MockLogger;
36 import org.apache.james.test.mock.avalon.MockServiceManager;
37 import org.apache.james.test.mock.avalon.MockSocketManager;
38 import org.apache.james.test.mock.avalon.MockThreadManager;
39 import org.apache.james.test.mock.james.InMemorySpoolRepository;
40 import org.apache.james.test.mock.james.MockMailServer;
41 import org.apache.james.test.util.Util;
42 import org.apache.james.userrepository.MockUsersRepository;
43 import org.apache.james.util.POP3BeforeSMTPHelper;
44 import org.apache.james.util.connection.SimpleConnectionManager;
45 import org.apache.mailet.MailAddress;
46
47 import javax.mail.MessagingException JavaDoc;
48 import javax.mail.util.SharedByteArrayInputStream;
49
50 import java.io.InputStream JavaDoc;
51 import java.io.Reader JavaDoc;
52 import java.net.InetAddress JavaDoc;
53 import java.net.UnknownHostException JavaDoc;
54 import java.util.ArrayList JavaDoc;
55
56 import junit.framework.TestCase;
57
58 /**
59  * Tests the org.apache.james.smtpserver.SMTPServer unit
60  */

61 public class POP3ServerTest extends TestCase {
62     private int m_pop3ListenerPort = Util.getNonPrivilegedPort();
63
64     private MockMailServer m_mailServer;
65
66     private POP3TestConfiguration m_testConfiguration;
67
68     private POP3Server m_pop3Server;
69
70     private MockUsersRepository m_usersRepository = new MockUsersRepository();
71     private POP3Client m_pop3Protocol = null;
72
73     private MailImpl testMail1;
74
75     private MailImpl testMail2;
76
77     public POP3ServerTest() {
78         super("POP3ServerTest");
79     }
80
81     protected void setUp() throws Exception JavaDoc {
82         m_pop3Server = new POP3Server();
83         ContainerUtil.enableLogging(m_pop3Server, new MockLogger());
84         ContainerUtil.service(m_pop3Server, setUpServiceManager());
85         m_testConfiguration = new POP3TestConfiguration(m_pop3ListenerPort);
86     }
87
88     private void finishSetUp(POP3TestConfiguration testConfiguration)
89             throws Exception JavaDoc {
90         testConfiguration.init();
91         ContainerUtil.configure(m_pop3Server, testConfiguration);
92         ContainerUtil.initialize(m_pop3Server);
93     }
94
95     private MockServiceManager setUpServiceManager() throws ServiceException {
96         MockServiceManager serviceManager = new MockServiceManager();
97         SimpleConnectionManager connectionManager = new SimpleConnectionManager();
98         ContainerUtil.enableLogging(connectionManager, new MockLogger());
99         ContainerUtil.service(connectionManager, serviceManager);
100         serviceManager.put(JamesConnectionManager.ROLE, connectionManager);
101         m_mailServer = new MockMailServer();
102         serviceManager
103                 .put(MailServer.ROLE, m_mailServer);
104         serviceManager.put(UsersRepository.ROLE,
105                 m_usersRepository);
106         serviceManager.put(SocketManager.ROLE, new MockSocketManager(
107                 m_pop3ListenerPort));
108         serviceManager.put(ThreadManager.ROLE, new MockThreadManager());
109         serviceManager.put(DNSServer.ROLE, setUpDNSServer());
110         return serviceManager;
111     }
112
113     private DNSServer setUpDNSServer() {
114         DNSServer dns = new AbstractDNSServer() {
115             public String JavaDoc getHostName(InetAddress JavaDoc addr) {
116                 return "localhost";
117             }
118             
119             public InetAddress JavaDoc getLocalHost() throws UnknownHostException JavaDoc {
120                 return InetAddress.getLocalHost();
121             }
122         
123         };
124         return dns;
125     }
126     protected void tearDown() throws Exception JavaDoc {
127         if (m_pop3Protocol != null) {
128             m_pop3Protocol.sendCommand("quit");
129             m_pop3Protocol.disconnect();
130         }
131         m_pop3Server.dispose();
132         ContainerUtil.dispose(m_mailServer);
133         if (testMail1 != null) testMail1.dispose();
134         if (testMail2 != null) testMail2.dispose();
135         super.tearDown();
136     }
137
138     public void testAuthenticationFail() throws Exception JavaDoc {
139         finishSetUp(m_testConfiguration);
140         
141         m_pop3Protocol = new POP3Client();
142         m_pop3Protocol.connect("127.0.0.1", m_pop3ListenerPort);
143
144         m_usersRepository.addUser("known", "test2");
145
146         m_pop3Protocol.login("known", "test");
147         assertEquals(0, m_pop3Protocol.getState());
148         assertTrue(m_pop3Protocol.getReplyString().startsWith("-ERR"));
149     }
150
151     public void testUnknownUser() throws Exception JavaDoc {
152         finishSetUp(m_testConfiguration);
153
154         m_pop3Protocol = new POP3Client();
155         m_pop3Protocol.connect("127.0.0.1", m_pop3ListenerPort);
156
157         m_pop3Protocol.login("unknown", "test");
158         assertEquals(0, m_pop3Protocol.getState());
159         assertTrue(m_pop3Protocol.getReplyString().startsWith("-ERR"));
160     }
161
162     public void testKnownUserEmptyInbox() throws Exception JavaDoc {
163         finishSetUp(m_testConfiguration);
164
165         m_pop3Protocol = new POP3Client();
166         m_pop3Protocol.connect("127.0.0.1",m_pop3ListenerPort);
167
168         m_usersRepository.addUser("foo", "bar");
169         InMemorySpoolRepository mockMailRepository = new InMemorySpoolRepository();
170         m_mailServer.setUserInbox("foo", mockMailRepository);
171
172         // not authenticated
173
POP3MessageInfo[] entries = m_pop3Protocol.listMessages();
174         assertNull(entries);
175
176         m_pop3Protocol.login("foo", "bar");
177         System.err.println(m_pop3Protocol.getState());
178         assertEquals(1, m_pop3Protocol.getState());
179
180         entries = m_pop3Protocol.listMessages();
181         assertEquals(1, m_pop3Protocol.getState());
182
183         assertNotNull(entries);
184         assertEquals(entries.length, 0);
185         
186         POP3MessageInfo p3i = m_pop3Protocol.listMessage(1);
187         assertEquals(1, m_pop3Protocol.getState());
188         assertNull(p3i);
189
190         ContainerUtil.dispose(mockMailRepository);
191     }
192
193     public void testNotAsciiCharsInPassword() throws Exception JavaDoc {
194         finishSetUp(m_testConfiguration);
195
196         m_pop3Protocol = new POP3Client();
197         m_pop3Protocol.connect("127.0.0.1",m_pop3ListenerPort);
198
199         String JavaDoc pass = "bar" + (new String JavaDoc(new char[] { 200, 210 })) + "foo";
200         m_usersRepository.addUser("foo", pass);
201         InMemorySpoolRepository mockMailRepository = new InMemorySpoolRepository();
202         m_mailServer.setUserInbox("foo", mockMailRepository);
203
204         m_pop3Protocol.login("foo", pass);
205         assertEquals(1, m_pop3Protocol.getState());
206         ContainerUtil.dispose(mockMailRepository);
207     }
208
209
210     public void testUnknownCommand() throws Exception JavaDoc {
211         finishSetUp(m_testConfiguration);
212
213         m_pop3Protocol = new POP3Client();
214         m_pop3Protocol.connect("127.0.0.1",m_pop3ListenerPort);
215         
216         m_pop3Protocol.sendCommand("unkn");
217         assertEquals(0, m_pop3Protocol.getState());
218         assertEquals("Expected -ERR as result for an unknown command", m_pop3Protocol.getReplyString().substring(0,4),"-ERR");
219     }
220
221     public void testUidlCommand() throws Exception JavaDoc {
222         finishSetUp(m_testConfiguration);
223
224         m_usersRepository.addUser("foo", "bar");
225         InMemorySpoolRepository mockMailRepository = new InMemorySpoolRepository();
226         m_mailServer.setUserInbox("foo", mockMailRepository);
227
228         m_pop3Protocol = new POP3Client();
229         m_pop3Protocol.connect("127.0.0.1",m_pop3ListenerPort);
230
231         m_pop3Protocol.sendCommand("uidl");
232         assertEquals(0, m_pop3Protocol.getState());
233
234         m_pop3Protocol.login("foo", "bar");
235
236         POP3MessageInfo[] list = m_pop3Protocol.listUniqueIdentifiers();
237         assertEquals("Found unexpected messages", 0, list.length);
238
239         m_pop3Protocol.disconnect();
240         
241         setupTestMails(mockMailRepository);
242         
243         m_pop3Protocol.connect("127.0.0.1",m_pop3ListenerPort);
244         m_pop3Protocol.login("foo", "bar");
245
246         list = m_pop3Protocol.listUniqueIdentifiers();
247         assertEquals("Expected 2 messages, found: "+list.length, 2, list.length);
248         assertEquals("name", list[0].identifier);
249         assertEquals("name2", list[1].identifier);
250         
251         POP3MessageInfo p3i = m_pop3Protocol.listUniqueIdentifier(1);
252         assertNotNull(p3i);
253         assertEquals("name", p3i.identifier);
254
255     }
256
257     public void testMiscCommandsWithWithoutAuth() throws Exception JavaDoc {
258         finishSetUp(m_testConfiguration);
259
260         m_usersRepository.addUser("foo", "bar");
261         InMemorySpoolRepository mockMailRepository = new InMemorySpoolRepository();
262         m_mailServer.setUserInbox("foo", mockMailRepository);
263
264         m_pop3Protocol = new POP3Client();
265         m_pop3Protocol.connect("127.0.0.1",m_pop3ListenerPort);
266
267         m_pop3Protocol.sendCommand("noop");
268         assertEquals(0, m_pop3Protocol.getState());
269         assertEquals("-ERR", m_pop3Protocol.getReplyString().substring(0,4));
270
271         m_pop3Protocol.sendCommand("stat");
272         assertEquals(0, m_pop3Protocol.getState());
273         assertEquals("-ERR", m_pop3Protocol.getReplyString().substring(0,4));
274
275         m_pop3Protocol.sendCommand("pass");
276         assertEquals(0, m_pop3Protocol.getState());
277         assertEquals("-ERR", m_pop3Protocol.getReplyString().substring(0,4));
278
279         m_pop3Protocol.sendCommand("auth");
280         assertEquals(0, m_pop3Protocol.getState());
281         assertEquals("-ERR", m_pop3Protocol.getReplyString().substring(0,4));
282
283         m_pop3Protocol.sendCommand("rset");
284         assertEquals(0, m_pop3Protocol.getState());
285         assertEquals("-ERR", m_pop3Protocol.getReplyString().substring(0,4));
286         
287         m_pop3Protocol.login("foo", "bar");
288
289         POP3MessageInfo[] list = m_pop3Protocol.listUniqueIdentifiers();
290         assertEquals("Found unexpected messages", 0, list.length);
291
292         m_pop3Protocol.sendCommand("noop");
293         assertEquals(1, m_pop3Protocol.getState());
294
295         m_pop3Protocol.sendCommand("pass");
296         assertEquals(1, m_pop3Protocol.getState());
297         assertEquals("-ERR", m_pop3Protocol.getReplyString().substring(0,4));
298
299         m_pop3Protocol.sendCommand("auth");
300         assertEquals(1, m_pop3Protocol.getState());
301         assertEquals("-ERR", m_pop3Protocol.getReplyString().substring(0,4));
302
303         m_pop3Protocol.sendCommand("user");
304         assertEquals(1, m_pop3Protocol.getState());
305         assertEquals("-ERR", m_pop3Protocol.getReplyString().substring(0,4));
306
307         m_pop3Protocol.sendCommand("rset");
308         assertEquals(1, m_pop3Protocol.getState());
309         
310     }
311
312     public void testKnownUserInboxWithMessages() throws Exception JavaDoc {
313         finishSetUp(m_testConfiguration);
314
315         m_pop3Protocol = new POP3Client();
316         m_pop3Protocol.connect("127.0.0.1",m_pop3ListenerPort);
317
318         m_usersRepository.addUser("foo2", "bar2");
319         InMemorySpoolRepository mailRep = new InMemorySpoolRepository();
320
321         setupTestMails(mailRep);
322
323         m_mailServer.setUserInbox("foo2", mailRep);
324         
325         m_pop3Protocol.sendCommand("retr","1");
326         assertEquals(0, m_pop3Protocol.getState());
327         assertEquals("-ERR", m_pop3Protocol.getReplyString().substring(0,4));
328
329         m_pop3Protocol.login("foo2", "bar2");
330         assertEquals(1, m_pop3Protocol.getState());
331
332         POP3MessageInfo[] entries = m_pop3Protocol.listMessages();
333
334         assertNotNull(entries);
335         assertEquals(2, entries.length);
336         assertEquals(1, m_pop3Protocol.getState());
337
338         Reader JavaDoc r = m_pop3Protocol.retrieveMessageTop(entries[0].number, 0);
339         assertNotNull(r);
340         r.close();
341
342         Reader JavaDoc r2 = m_pop3Protocol.retrieveMessage(entries[0].number);
343         assertNotNull(r2);
344         r2.close();
345
346         // existing message
347
boolean deleted = m_pop3Protocol.deleteMessage(entries[0].number);
348         assertTrue(deleted);
349
350         // already deleted message
351
deleted = m_pop3Protocol.deleteMessage(entries[0].number);
352         assertFalse(deleted);
353
354         // unexisting message
355
deleted = m_pop3Protocol.deleteMessage(10);
356         assertFalse(deleted);
357
358         m_pop3Protocol.sendCommand("quit");
359         m_pop3Protocol.disconnect();
360
361         m_pop3Protocol.connect("127.0.0.1",m_pop3ListenerPort);
362
363         m_pop3Protocol.login("foo2", "bar2");
364         assertEquals(1, m_pop3Protocol.getState());
365
366         entries = null;
367
368         POP3MessageInfo stats = m_pop3Protocol.status();
369         assertEquals(1, stats.number);
370         assertEquals(5, stats.size);
371
372         entries = m_pop3Protocol.listMessages();
373
374         assertNotNull(entries);
375         assertEquals(1, entries.length);
376         assertEquals(1, m_pop3Protocol.getState());
377
378         // top without arguments
379
m_pop3Protocol.sendCommand("top");
380         assertEquals("-ERR", m_pop3Protocol.getReplyString().substring(0,4));
381         
382         Reader JavaDoc r3 = m_pop3Protocol.retrieveMessageTop(entries[0].number, 0);
383         assertNotNull(r3);
384         r3.close();
385         ContainerUtil.dispose(mailRep);
386     }
387
388     private void setupTestMails(MailRepository mailRep) throws MessagingException JavaDoc {
389         ArrayList JavaDoc recipients = new ArrayList JavaDoc();
390         recipients.add(new MailAddress("recipient@test.com"));
391         InputStream mw = new SharedByteArrayInputStream(
392                                 ("Return-path: return@test.com\r\n"+
393                                  "Content-Transfer-Encoding: plain\r\n"+
394                                  "Subject: test\r\n\r\n"+
395                                  "Body Text POP3ServerTest.setupTestMails\r\n").getBytes());
396         testMail1 = new MailImpl("name", new MailAddress("from@test.com"),
397                         recipients, mw);
398         mailRep.store(testMail1);
399         InputStream mw2 = new SharedByteArrayInputStream(
400                                 ("EMPTY").getBytes());
401         testMail2 = new MailImpl("name2", new MailAddress("from@test.com"),
402                                 recipients, mw2);
403         mailRep.store(testMail2);
404     }
405
406     public void testTwoSimultaneousMails() throws Exception JavaDoc {
407         finishSetUp(m_testConfiguration);
408
409         // make two user/repositories, open both
410
m_usersRepository.addUser("foo1", "bar1");
411         InMemorySpoolRepository mailRep1 = new InMemorySpoolRepository();
412         setupTestMails(mailRep1);
413         m_mailServer.setUserInbox("foo1", mailRep1);
414
415         m_usersRepository.addUser("foo2", "bar2");
416         InMemorySpoolRepository mailRep2 = new InMemorySpoolRepository();
417         //do not setupTestMails, this is done later
418
m_mailServer.setUserInbox("foo2", mailRep2);
419
420         POP3Client pop3Protocol2 = null;
421         try {
422             // open two connections
423
m_pop3Protocol = new POP3Client();
424             m_pop3Protocol.connect("127.0.0.1", m_pop3ListenerPort);
425             pop3Protocol2 = new POP3Client();
426             pop3Protocol2.connect("127.0.0.1", m_pop3ListenerPort);
427
428             assertEquals("first connection taken", 0, m_pop3Protocol.getState());
429             assertEquals("second connection taken", 0, pop3Protocol2.getState());
430
431             // open two accounts
432
m_pop3Protocol.login("foo1", "bar1");
433
434             pop3Protocol2.login("foo2", "bar2");
435
436             POP3MessageInfo[] entries = m_pop3Protocol.listMessages();
437             assertEquals("foo1 has mails", 2, entries.length);
438
439             entries = pop3Protocol2.listMessages();
440             assertEquals("foo2 has no mails", 0, entries.length);
441
442         } finally {
443             // put both to rest, field var is handled by tearDown()
444
if (pop3Protocol2 != null) {
445                 pop3Protocol2.sendCommand("quit");
446                 pop3Protocol2.disconnect();
447             }
448         }
449     }
450     
451     public void testIpStored() throws Exception JavaDoc {
452         finishSetUp(m_testConfiguration);
453
454         m_pop3Protocol = new POP3Client();
455         m_pop3Protocol.connect("127.0.0.1",m_pop3ListenerPort);
456
457         String JavaDoc pass = "password";
458         m_usersRepository.addUser("foo", pass);
459         InMemorySpoolRepository mockMailRepository = new InMemorySpoolRepository();
460         m_mailServer.setUserInbox("foo", mockMailRepository);
461
462         m_pop3Protocol.login("foo", pass);
463         assertEquals(1, m_pop3Protocol.getState());
464         assertTrue(POP3BeforeSMTPHelper.isAuthorized("127.0.0.1"));
465         ContainerUtil.dispose(mockMailRepository);
466     }
467     
468     
469
470     /*
471      * See JAMES-649
472      * The same happens when using RETR
473      *
474      * Comment to not broke the builds!
475      *
476     public void testOOMTop() throws Exception {
477         finishSetUp(m_testConfiguration);
478
479         int messageCount = 30000;
480         m_pop3Protocol = new POP3Client();
481         m_pop3Protocol.connect("127.0.0.1",m_pop3ListenerPort);
482
483         m_usersRepository.addUser("foo", "bar");
484         InMemorySpoolRepository mockMailRepository = new InMemorySpoolRepository();
485         
486         Mail m = new MailImpl();
487         m.setMessage(Util.createMimeMessage("X-TEST", "test"));
488         for (int i = 1; i < messageCount+1; i++ ) {
489             m.setName("test" + i);
490             mockMailRepository.store(m);
491         }
492
493         m_mailServer.setUserInbox("foo", mockMailRepository);
494
495         // not authenticated
496         POP3MessageInfo[] entries = m_pop3Protocol.listMessages();
497         assertNull(entries);
498
499         m_pop3Protocol.login("foo", "bar");
500         System.err.println(m_pop3Protocol.getState());
501         assertEquals(1, m_pop3Protocol.getState());
502
503         entries = m_pop3Protocol.listMessages();
504         assertEquals(1, m_pop3Protocol.getState());
505
506         assertNotNull(entries);
507         assertEquals(entries.length, messageCount);
508         
509         for (int i = 1; i < messageCount+1; i++ ) {
510             Reader r = m_pop3Protocol.retrieveMessageTop(i, 100);
511             assertNotNull(r);
512             r.close();
513         }
514         
515         ContainerUtil.dispose(mockMailRepository);
516     }
517     */

518     
519
520 }
521
Popular Tags