1 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 ; 48 import javax.mail.util.SharedByteArrayInputStream; 49 50 import java.io.InputStream ; 51 import java.io.Reader ; 52 import java.net.InetAddress ; 53 import java.net.UnknownHostException ; 54 import java.util.ArrayList ; 55 56 import junit.framework.TestCase; 57 58 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 { 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 { 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 getHostName(InetAddress addr) { 116 return "localhost"; 117 } 118 119 public InetAddress getLocalHost() throws UnknownHostException { 120 return InetAddress.getLocalHost(); 121 } 122 123 }; 124 return dns; 125 } 126 protected void tearDown() throws Exception { 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 { 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 { 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 { 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 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 { 194 finishSetUp(m_testConfiguration); 195 196 m_pop3Protocol = new POP3Client(); 197 m_pop3Protocol.connect("127.0.0.1",m_pop3ListenerPort); 198 199 String pass = "bar" + (new String (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 { 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 { 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 { 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 { 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 r = m_pop3Protocol.retrieveMessageTop(entries[0].number, 0); 339 assertNotNull(r); 340 r.close(); 341 342 Reader r2 = m_pop3Protocol.retrieveMessage(entries[0].number); 343 assertNotNull(r2); 344 r2.close(); 345 346 boolean deleted = m_pop3Protocol.deleteMessage(entries[0].number); 348 assertTrue(deleted); 349 350 deleted = m_pop3Protocol.deleteMessage(entries[0].number); 352 assertFalse(deleted); 353 354 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 m_pop3Protocol.sendCommand("top"); 380 assertEquals("-ERR", m_pop3Protocol.getReplyString().substring(0,4)); 381 382 Reader 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 { 389 ArrayList recipients = new ArrayList (); 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 { 407 finishSetUp(m_testConfiguration); 408 409 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 m_mailServer.setUserInbox("foo2", mailRep2); 419 420 POP3Client pop3Protocol2 = null; 421 try { 422 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 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 if (pop3Protocol2 != null) { 445 pop3Protocol2.sendCommand("quit"); 446 pop3Protocol2.disconnect(); 447 } 448 } 449 } 450 451 public void testIpStored() throws Exception { 452 finishSetUp(m_testConfiguration); 453 454 m_pop3Protocol = new POP3Client(); 455 m_pop3Protocol.connect("127.0.0.1",m_pop3ListenerPort); 456 457 String 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 518 519 520 } 521 | Popular Tags |