1 19 20 package org.apache.james.smtpserver; 21 22 import org.apache.avalon.cornerstone.services.sockets.SocketManager; 23 import org.apache.avalon.cornerstone.services.store.Store; 24 import org.apache.avalon.cornerstone.services.threads.ThreadManager; 25 import org.apache.avalon.framework.container.ContainerUtil; 26 import org.apache.commons.net.smtp.SMTPClient; 27 import org.apache.commons.net.smtp.SMTPReply; 28 import org.apache.james.Constants; 29 import org.apache.james.core.MailImpl; 30 import org.apache.james.services.DNSServer; 31 import org.apache.james.services.JamesConnectionManager; 32 import org.apache.james.services.MailServer; 33 import org.apache.james.services.UsersRepository; 34 import org.apache.james.test.mock.avalon.MockLogger; 35 import org.apache.james.test.mock.avalon.MockServiceManager; 36 import org.apache.james.test.mock.avalon.MockSocketManager; 37 import org.apache.james.test.mock.avalon.MockStore; 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.mock.mailet.MockMailContext; 42 import org.apache.james.test.mock.mailet.MockMailetConfig; 43 import org.apache.james.test.util.Util; 44 import org.apache.james.transport.mailets.RemoteDelivery; 45 import org.apache.james.userrepository.MockUsersRepository; 46 import org.apache.james.util.Base64; 47 import org.apache.james.util.connection.SimpleConnectionManager; 48 import org.apache.mailet.Mail; 49 import org.apache.mailet.MailAddress; 50 51 import javax.mail.MessagingException ; 52 import javax.mail.Session ; 53 import javax.mail.internet.MimeMessage ; 54 55 import java.io.BufferedReader ; 56 import java.io.ByteArrayInputStream ; 57 import java.io.ByteArrayOutputStream ; 58 import java.io.IOException ; 59 import java.io.InputStreamReader ; 60 import java.io.OutputStream ; 61 import java.io.Writer ; 62 import java.net.InetAddress ; 63 import java.net.Socket ; 64 import java.net.UnknownHostException ; 65 import java.util.ArrayList ; 66 import java.util.Arrays ; 67 import java.util.Collection ; 68 import java.util.Iterator ; 69 import java.util.List ; 70 import java.util.Properties ; 71 72 import junit.framework.TestCase; 73 74 77 public class SMTPServerTest extends TestCase { 78 79 private final class AlterableDNSServer implements DNSServer { 80 81 private InetAddress localhostByName = null; 82 83 public Collection findMXRecords(String hostname) { 84 List res = new ArrayList (); 85 if (hostname == null) { 86 return res; 87 }; 88 if ("james.apache.org".equals(hostname)) { 89 res.add("nagoya.apache.org"); 90 } 91 return res; 92 } 93 94 public Iterator getSMTPHostAddresses(String domainName) { 95 throw new UnsupportedOperationException ("Unimplemented mock service"); 96 } 97 98 public InetAddress [] getAllByName(String host) throws UnknownHostException { 99 return new InetAddress [] {getByName(host)}; 100 } 101 102 public InetAddress getByName(String host) throws UnknownHostException { 103 if (getLocalhostByName() != null) { 104 if ("127.0.0.1".equals(host)) return getLocalhostByName(); 105 } 106 107 if ("1.0.0.127.bl.spamcop.net.".equals(host)) { 108 return InetAddress.getByName("localhost"); 109 } 110 111 if ("james.apache.org".equals(host)) { 112 return InetAddress.getByName("james.apache.org"); 113 } 114 115 if ("abgsfe3rsf.de".equals(host)) { 116 throw new UnknownHostException (); 117 } 118 119 if ("128.0.0.1".equals(host) || "192.168.0.1".equals(host) || "127.0.0.1".equals(host) || "127.0.0.0".equals(host) || "255.0.0.0".equals(host) || "255.255.255.255".equals(host)) { 120 return InetAddress.getByName(host); 121 } 122 123 throw new UnsupportedOperationException ("getByName not implemented in mock for host: "+host); 124 } 126 127 public Collection findTXTRecords(String hostname) { 128 List res = new ArrayList (); 129 if (hostname == null) { 130 return res; 131 }; 132 if ("2.0.0.127.bl.spamcop.net.".equals(hostname)) { 133 res.add("Blocked - see http://www.spamcop.net/bl.shtml?127.0.0.2"); 134 } 135 return res; 136 } 137 138 public InetAddress getLocalhostByName() { 139 return localhostByName; 140 } 141 142 public void setLocalhostByName(InetAddress localhostByName) { 143 this.localhostByName = localhostByName; 144 } 145 146 public String getHostName(InetAddress addr) { 147 return addr.getHostName(); 148 } 149 150 public InetAddress getLocalHost() throws UnknownHostException { 151 return InetAddress.getLocalHost(); 152 } 153 } 154 155 156 private int m_smtpListenerPort = Util.getNonPrivilegedPort(); 157 private MockMailServer m_mailServer; 158 private SMTPTestConfiguration m_testConfiguration; 159 private SMTPServer m_smtpServer; 160 private MockUsersRepository m_usersRepository = new MockUsersRepository(); 161 162 public SMTPServerTest() { 163 super("SMTPServerTest"); 164 } 165 166 public void verifyLastMail(String sender, String recipient, MimeMessage msg) throws IOException , MessagingException { 167 Mail mailData = m_mailServer.getLastMail(); 168 assertNotNull("mail received by mail server", mailData); 169 170 if (sender == null && recipient == null && msg == null) fail("no verification can be done with all arguments null"); 171 172 if (sender != null) assertEquals("sender verfication", sender, ((MailAddress)mailData.getSender()).toString()); 173 if (recipient != null) assertTrue("recipient verfication", ((Collection ) mailData.getRecipients()).contains(new MailAddress(recipient))); 174 if (msg != null) { 175 ByteArrayOutputStream bo1 = new ByteArrayOutputStream (); 176 msg.writeTo(bo1); 177 ByteArrayOutputStream bo2 = new ByteArrayOutputStream (); 178 ((MimeMessage ) mailData.getMessage()).writeTo(bo2); 179 assertEquals(bo1.toString(),bo2.toString()); 180 assertEquals("message verification", msg, ((MimeMessage ) mailData.getMessage())); 181 } 182 } 183 184 protected void setUp() throws Exception { 185 m_smtpServer = new SMTPServer(); 186 ContainerUtil.enableLogging(m_smtpServer,new MockLogger()); 187 m_serviceManager = setUpServiceManager(); 188 ContainerUtil.service(m_smtpServer, m_serviceManager); 189 m_testConfiguration = new SMTPTestConfiguration(m_smtpListenerPort); 190 } 191 192 protected void tearDown() throws Exception { 193 ContainerUtil.dispose(m_mailServer); 194 super.tearDown(); 195 } 196 197 private void finishSetUp(SMTPTestConfiguration testConfiguration) throws Exception { 198 testConfiguration.init(); 199 ContainerUtil.configure(m_smtpServer, testConfiguration); 200 ContainerUtil.initialize(m_smtpServer); 201 m_mailServer.setMaxMessageSizeBytes(m_testConfiguration.getMaxMessageSize()*1024); 202 } 203 204 private MockServiceManager setUpServiceManager() throws Exception { 205 m_serviceManager = new MockServiceManager(); 206 SimpleConnectionManager connectionManager = new SimpleConnectionManager(); 207 ContainerUtil.enableLogging(connectionManager, new MockLogger()); 208 m_serviceManager.put(JamesConnectionManager.ROLE, connectionManager); 209 m_serviceManager.put("org.apache.mailet.MailetContext", new MockMailContext()); 210 m_mailServer = new MockMailServer(); 211 m_serviceManager.put(MailServer.ROLE, m_mailServer); 212 m_serviceManager.put(UsersRepository.ROLE, m_usersRepository); 213 m_serviceManager.put(SocketManager.ROLE, new MockSocketManager(m_smtpListenerPort)); 214 m_serviceManager.put(ThreadManager.ROLE, new MockThreadManager()); 215 m_dnsServer = new AlterableDNSServer(); 216 m_serviceManager.put(DNSServer.ROLE, m_dnsServer); 217 m_serviceManager.put(Store.ROLE, new MockStore()); 218 return m_serviceManager; 219 } 220 221 public void testSimpleMailSendWithEHLO() throws Exception { 222 finishSetUp(m_testConfiguration); 223 224 SMTPClient smtpProtocol = new SMTPClient(); 225 smtpProtocol.connect("127.0.0.1", m_smtpListenerPort); 226 227 assertNull("no mail received by mail server", m_mailServer.getLastMail()); 229 230 smtpProtocol.sendCommand("EHLO "+InetAddress.getLocalHost()); 231 String [] capabilityRes = smtpProtocol.getReplyStrings(); 232 233 List capabilitieslist = new ArrayList (); 234 for (int i = 1; i < capabilityRes.length; i++) { 235 capabilitieslist.add(capabilityRes[i].substring(4)); 236 } 237 238 assertEquals("capabilities", 3, capabilitieslist.size()); 239 assertTrue("capabilities present PIPELINING", capabilitieslist.contains("PIPELINING")); 240 assertTrue("capabilities present ENHANCEDSTATUSCODES", capabilitieslist.contains("ENHANCEDSTATUSCODES")); 241 assertTrue("capabilities present 8BITMIME", capabilitieslist.contains("8BITMIME")); 242 243 smtpProtocol.setSender("mail@localhost"); 244 smtpProtocol.addRecipient("mail@localhost"); 245 246 smtpProtocol.sendShortMessageData("Subject: test\r\n\r\nBody\r\n\r\n.\r\n"); 247 smtpProtocol.quit(); 248 smtpProtocol.disconnect(); 249 250 assertNotNull("mail received by mail server", m_mailServer.getLastMail()); 252 } 253 254 public void testEmptyMessage() throws Exception { 255 finishSetUp(m_testConfiguration); 256 257 SMTPClient smtp = new SMTPClient(); 258 smtp.connect("127.0.0.1", m_smtpListenerPort); 259 260 assertNull("no mail received by mail server", m_mailServer.getLastMail()); 262 263 smtp.helo(InetAddress.getLocalHost().toString()); 264 265 smtp.setSender("mail@localhost"); 266 267 smtp.addRecipient("mail@localhost"); 268 269 smtp.sendShortMessageData(""); 270 271 smtp.quit(); 272 273 smtp.disconnect(); 274 275 assertNotNull("mail received by mail server", m_mailServer.getLastMail()); 277 278 System.gc(); 281 282 int size = ((MimeMessage ) m_mailServer.getLastMail().getMessage()).getSize(); 283 284 assertEquals(size, 2); 285 } 286 287 public void testSimpleMailSendWithHELO() throws Exception { 288 finishSetUp(m_testConfiguration); 289 290 SMTPClient smtpProtocol = new SMTPClient(); 291 smtpProtocol.connect("127.0.0.1", m_smtpListenerPort); 292 293 assertNull("no mail received by mail server", m_mailServer.getLastMail()); 295 296 smtpProtocol.helo(InetAddress.getLocalHost().toString()); 297 298 smtpProtocol.setSender("mail@localhost"); 299 300 smtpProtocol.addRecipient("mail@localhost"); 301 302 smtpProtocol.sendShortMessageData("Subject: test mail\r\n\r\nTest body testSimpleMailSendWithHELO\r\n.\r\n"); 303 304 smtpProtocol.quit(); 305 smtpProtocol.disconnect(); 306 307 assertNotNull("mail received by mail server", m_mailServer.getLastMail()); 309 } 310 311 public void testTwoSimultaneousMails() throws Exception { 312 finishSetUp(m_testConfiguration); 313 314 SMTPClient smtpProtocol1 = new SMTPClient(); 315 smtpProtocol1.connect("127.0.0.1", m_smtpListenerPort); 316 SMTPClient smtpProtocol2 = new SMTPClient(); 317 smtpProtocol2.connect("127.0.0.1", m_smtpListenerPort); 318 319 assertTrue("first connection taken",smtpProtocol1.isConnected()); 320 assertTrue("second connection taken",smtpProtocol2.isConnected()); 321 322 assertNull("no mail received by mail server", m_mailServer.getLastMail()); 324 325 smtpProtocol1.helo(InetAddress.getLocalHost().toString()); 326 smtpProtocol2.helo(InetAddress.getLocalHost().toString()); 327 328 String sender1 = "mail_sender1@localhost"; 329 String recipient1 = "mail_recipient1@localhost"; 330 smtpProtocol1.setSender(sender1); 331 smtpProtocol1.addRecipient(recipient1); 332 333 String sender2 = "mail_sender2@localhost"; 334 String recipient2 = "mail_recipient2@localhost"; 335 smtpProtocol2.setSender(sender2); 336 smtpProtocol2.addRecipient(recipient2); 337 338 smtpProtocol1.sendShortMessageData("Subject: test\r\n\r\nTest body testTwoSimultaneousMails1\r\n.\r\n"); 339 verifyLastMail(sender1, recipient1, null); 340 341 smtpProtocol2.sendShortMessageData("Subject: test\r\n\r\nTest body testTwoSimultaneousMails2\r\n.\r\n"); 342 verifyLastMail(sender2, recipient2, null); 343 344 smtpProtocol1.quit(); 345 smtpProtocol2.quit(); 346 347 smtpProtocol1.disconnect(); 348 smtpProtocol2.disconnect(); 349 } 350 351 public void testTwoMailsInSequence() throws Exception { 352 finishSetUp(m_testConfiguration); 353 354 SMTPClient smtpProtocol1 = new SMTPClient(); 355 smtpProtocol1.connect("127.0.0.1", m_smtpListenerPort); 356 357 assertTrue("first connection taken", smtpProtocol1.isConnected()); 358 359 assertNull("no mail received by mail server", m_mailServer.getLastMail()); 361 362 smtpProtocol1.helo(InetAddress.getLocalHost().toString()); 363 364 String sender1 = "mail_sender1@localhost"; 365 String recipient1 = "mail_recipient1@localhost"; 366 smtpProtocol1.setSender(sender1); 367 smtpProtocol1.addRecipient(recipient1); 368 369 smtpProtocol1.sendShortMessageData("Subject: test\r\n\r\nTest body testTwoMailsInSequence1\r\n"); 370 verifyLastMail(sender1, recipient1, null); 371 372 String sender2 = "mail_sender2@localhost"; 373 String recipient2 = "mail_recipient2@localhost"; 374 smtpProtocol1.setSender(sender2); 375 smtpProtocol1.addRecipient(recipient2); 376 377 smtpProtocol1.sendShortMessageData("Subject: test2\r\n\r\nTest body2 testTwoMailsInSequence2\r\n"); 378 verifyLastMail(sender2, recipient2, null); 379 380 smtpProtocol1.quit(); 381 smtpProtocol1.disconnect(); 382 } 383 384 public void testHeloResolv() throws Exception { 385 m_testConfiguration.setHeloResolv(); 386 m_testConfiguration.setAuthorizedAddresses("192.168.0.1"); 387 finishSetUp(m_testConfiguration); 388 389 390 SMTPClient smtpProtocol1 = new SMTPClient(); 391 smtpProtocol1.connect("127.0.0.1", m_smtpListenerPort); 392 393 assertTrue("first connection taken", smtpProtocol1.isConnected()); 394 395 assertNull("no mail received by mail server", m_mailServer.getLastMail()); 397 398 String helo1 = "abgsfe3rsf.de"; 399 String helo2 = "james.apache.org"; 400 String mail = "sender@james.apache.org"; 401 String rcpt = "rcpt@localhost"; 402 403 smtpProtocol1.sendCommand("helo",helo1); 404 smtpProtocol1.setSender(mail); 405 smtpProtocol1.addRecipient(rcpt); 406 407 assertEquals("expected error: helo could not resolved", 501, smtpProtocol1.getReplyCode()); 409 410 smtpProtocol1.sendCommand("helo", helo2); 411 smtpProtocol1.setSender(mail); 412 smtpProtocol1.addRecipient(rcpt); 413 414 assertEquals("Helo accepted", 250, smtpProtocol1.getReplyCode()); 416 417 smtpProtocol1.quit(); 418 } 419 420 public void testHeloResolvDefault() throws Exception { 421 finishSetUp(m_testConfiguration); 422 423 SMTPClient smtpProtocol1 = new SMTPClient(); 424 smtpProtocol1.connect("127.0.0.1", m_smtpListenerPort); 425 426 smtpProtocol1.helo("abgsfe3rsf.de"); 427 assertEquals("Helo accepted", 250, smtpProtocol1.getReplyCode()); 429 430 smtpProtocol1.quit(); 431 } 432 433 public void testReverseEqualsHelo() throws Exception { 434 m_testConfiguration.setReverseEqualsHelo(); 435 m_testConfiguration.setAuthorizedAddresses("192.168.0.1"); 436 m_dnsServer.setLocalhostByName(InetAddress.getByName("james.apache.org")); 438 try { 439 finishSetUp(m_testConfiguration); 440 441 SMTPClient smtpProtocol1 = new SMTPClient(); 442 smtpProtocol1.connect("127.0.0.1", m_smtpListenerPort); 443 444 assertTrue("first connection taken", smtpProtocol1.isConnected()); 445 446 assertNull("no mail received by mail server", m_mailServer 448 .getLastMail()); 449 450 String helo1 = "abgsfe3rsf.de"; 451 String helo2 = "james.apache.org"; 452 String mail = "sender"; 453 String rcpt = "recipient"; 454 455 smtpProtocol1.sendCommand("helo", helo1); 456 smtpProtocol1.setSender(mail); 457 smtpProtocol1.addRecipient(rcpt); 458 459 assertEquals("expected error: helo not equals reverse of ip", 501, 461 smtpProtocol1.getReplyCode()); 462 463 smtpProtocol1.sendCommand("helo", helo2); 464 smtpProtocol1.setSender(mail); 465 smtpProtocol1.addRecipient(rcpt); 466 467 assertEquals("Helo accepted", 250, smtpProtocol1.getReplyCode()); 469 470 smtpProtocol1.quit(); 471 } finally { 472 m_dnsServer.setLocalhostByName(null); 473 } 474 } 475 476 public void testSenderDomainResolv() throws Exception { 477 m_testConfiguration.setSenderDomainResolv(); 478 m_testConfiguration.setAuthorizedAddresses("192.168.0.1/32"); 479 finishSetUp(m_testConfiguration); 480 481 SMTPClient smtpProtocol1 = new SMTPClient(); 482 smtpProtocol1.connect("127.0.0.1", m_smtpListenerPort); 483 484 485 assertTrue("first connection taken", smtpProtocol1.isConnected()); 486 487 assertNull("no mail received by mail server", m_mailServer.getLastMail()); 489 490 smtpProtocol1.helo(InetAddress.getLocalHost().toString()); 491 492 String sender1 = "mail_sender1@xfwrqqfgfe.de"; 493 494 smtpProtocol1.setSender(sender1); 495 assertEquals("expected 501 error", 501, smtpProtocol1.getReplyCode()); 496 497 smtpProtocol1.addRecipient("test@localhost"); 498 assertEquals("Recipient not accepted cause no valid sender", 503, smtpProtocol1.getReplyCode()); 499 smtpProtocol1.quit(); 500 501 } 502 503 public void testSenderDomainResolvDefault() throws Exception { 504 finishSetUp(m_testConfiguration); 505 506 SMTPClient smtpProtocol1 = new SMTPClient(); 507 smtpProtocol1.connect("127.0.0.1", m_smtpListenerPort); 508 509 smtpProtocol1.helo(InetAddress.getLocalHost().toString()); 510 511 String sender1 = "mail_sender1@xfwrqqfgfe.de"; 512 513 smtpProtocol1.setSender(sender1); 514 515 smtpProtocol1.quit(); 516 } 517 518 public void testSenderDomainResolvRelayClientDefault() throws Exception { 519 m_testConfiguration.setSenderDomainResolv(); 520 finishSetUp(m_testConfiguration); 521 522 SMTPClient smtpProtocol1 = new SMTPClient(); 523 smtpProtocol1.connect("127.0.0.1", m_smtpListenerPort); 524 525 526 assertTrue("first connection taken", smtpProtocol1.isConnected()); 527 528 assertNull("no mail received by mail server", m_mailServer.getLastMail()); 530 531 smtpProtocol1.helo(InetAddress.getLocalHost().toString()); 532 533 String sender1 = "mail_sender1@xfwrqqfgfe.de"; 534 535 smtpProtocol1.setSender(sender1); 537 538 smtpProtocol1.quit(); 539 540 } 541 542 public void testSenderDomainResolvRelayClient() throws Exception { 543 m_testConfiguration.setSenderDomainResolv(); 544 m_testConfiguration.setCheckAuthClients(true); 545 finishSetUp(m_testConfiguration); 546 547 SMTPClient smtpProtocol1 = new SMTPClient(); 548 smtpProtocol1.connect("127.0.0.1", m_smtpListenerPort); 549 550 551 assertTrue("first connection taken", smtpProtocol1.isConnected()); 552 553 assertNull("no mail received by mail server", m_mailServer.getLastMail()); 555 556 smtpProtocol1.helo(InetAddress.getLocalHost().toString()); 557 558 String sender1 = "mail_sender1@xfwrqqfgfe.de"; 559 String sender2 = "mail_sender2@james.apache.org"; 560 561 smtpProtocol1.setSender(sender1); 562 assertEquals("expected 501 error", 501, smtpProtocol1.getReplyCode()); 563 564 smtpProtocol1.setSender(sender2); 565 566 smtpProtocol1.quit(); 567 568 } 569 570 public void testMaxRcpt() throws Exception { 571 m_testConfiguration.setMaxRcpt(1); 572 finishSetUp(m_testConfiguration); 573 574 575 SMTPClient smtpProtocol1 = new SMTPClient(); 576 smtpProtocol1.connect("127.0.0.1", m_smtpListenerPort); 577 578 assertTrue("first connection taken", smtpProtocol1.isConnected()); 579 580 assertNull("no mail received by mail server", m_mailServer.getLastMail()); 582 583 smtpProtocol1.helo(InetAddress.getLocalHost().toString()); 584 585 String sender1 = "mail_sender1@james.apache.org"; 586 String rcpt1 = "test@localhost"; 587 String rcpt2 = "test2@localhost"; 588 589 smtpProtocol1.setSender(sender1); 590 smtpProtocol1.addRecipient(rcpt1); 591 592 smtpProtocol1.addRecipient(rcpt2); 593 assertEquals("expected 452 error", 452, smtpProtocol1.getReplyCode()); 594 595 smtpProtocol1.sendShortMessageData("Subject: test\r\n\r\nTest body testMaxRcpt1\r\n"); 596 597 599 smtpProtocol1.setSender(sender1); 600 601 smtpProtocol1.addRecipient(rcpt1); 602 603 smtpProtocol1.sendShortMessageData("Subject: test\r\n\r\nTest body testMaxRcpt2\r\n"); 604 605 smtpProtocol1.quit(); 606 607 } 608 609 public void testMaxRcptDefault() throws Exception { 610 finishSetUp(m_testConfiguration); 611 612 SMTPClient smtpProtocol1 = new SMTPClient(); 613 smtpProtocol1.connect("127.0.0.1", m_smtpListenerPort); 614 615 smtpProtocol1.helo(InetAddress.getLocalHost().toString()); 616 617 String sender1 = "mail_sender1@james.apache.org"; 618 String rcpt1 = "test@localhost"; 619 620 smtpProtocol1.setSender(sender1); 621 622 smtpProtocol1.addRecipient(rcpt1); 623 624 smtpProtocol1.sendShortMessageData("Subject: test\r\n\r\nTest body testMaxRcptDefault\r\n"); 625 626 smtpProtocol1.quit(); 627 } 628 629 public void testEhloResolv() throws Exception { 630 m_testConfiguration.setEhloResolv(); 631 m_testConfiguration.setAuthorizedAddresses("192.168.0.1"); 632 finishSetUp(m_testConfiguration); 633 634 635 SMTPClient smtpProtocol1 = new SMTPClient(); 636 smtpProtocol1.connect("127.0.0.1", m_smtpListenerPort); 637 638 assertTrue("first connection taken", smtpProtocol1.isConnected()); 639 640 assertNull("no mail received by mail server", m_mailServer.getLastMail()); 642 643 String ehlo1 = "abgsfe3rsf.de"; 644 String ehlo2 = "james.apache.org"; 645 String mail = "test@account"; 646 String rcpt = "test"; 647 648 smtpProtocol1.sendCommand("ehlo", ehlo1); 649 smtpProtocol1.setSender(mail); 650 smtpProtocol1.addRecipient(rcpt); 651 652 assertEquals("expected error: ehlo could not resolved", 501, smtpProtocol1.getReplyCode()); 654 655 smtpProtocol1.sendCommand("ehlo", ehlo2); 656 smtpProtocol1.setSender(mail); 657 smtpProtocol1.addRecipient(rcpt); 658 659 assertEquals("ehlo accepted", 250, smtpProtocol1.getReplyCode()); 661 662 smtpProtocol1.quit(); 663 } 664 665 public void testEhloResolvDefault() throws Exception { 666 finishSetUp(m_testConfiguration); 667 668 SMTPClient smtpProtocol1 = new SMTPClient(); 669 smtpProtocol1.connect("127.0.0.1", m_smtpListenerPort); 670 671 smtpProtocol1.sendCommand("ehlo","abgsfe3rsf.de"); 672 assertEquals("ehlo accepted", 250, smtpProtocol1.getReplyCode()); 674 675 smtpProtocol1.quit(); 676 } 677 678 public void testEhloResolvIgnoreClientDisabled() throws Exception { 679 m_testConfiguration.setEhloResolv(); 680 m_testConfiguration.setCheckAuthNetworks(true); 681 finishSetUp(m_testConfiguration); 682 683 684 SMTPClient smtpProtocol1 = new SMTPClient(); 685 smtpProtocol1.connect("127.0.0.1", m_smtpListenerPort); 686 687 assertTrue("first connection taken", smtpProtocol1.isConnected()); 688 689 assertNull("no mail received by mail server", m_mailServer.getLastMail()); 691 692 String ehlo1 = "abgsfe3rsf.de"; 693 String ehlo2 = "james.apache.org"; 694 String mail = "sender@localhost"; 695 String rcpt = "test"; 696 697 smtpProtocol1.sendCommand("ehlo", ehlo1); 698 smtpProtocol1.setSender(mail); 699 smtpProtocol1.addRecipient(rcpt); 700 701 assertEquals("expected error: ehlo could not resolved", 501, smtpProtocol1.getReplyCode()); 703 704 smtpProtocol1.sendCommand("ehlo", ehlo2); 705 smtpProtocol1.setSender(mail); 706 smtpProtocol1.addRecipient(rcpt); 707 assertEquals("ehlo accepted", 250, smtpProtocol1.getReplyCode()); 709 710 smtpProtocol1.quit(); 711 } 712 713 public void testReverseEqualsEhlo() throws Exception { 714 m_testConfiguration.setReverseEqualsEhlo(); 715 m_testConfiguration.setAuthorizedAddresses("192.168.0.1"); 716 m_dnsServer.setLocalhostByName(m_dnsServer.getByName("james.apache.org")); 718 try { 719 finishSetUp(m_testConfiguration); 720 721 SMTPClient smtpProtocol1 = new SMTPClient(); 722 smtpProtocol1.connect("127.0.0.1", m_smtpListenerPort); 723 724 assertTrue("first connection taken", smtpProtocol1.isConnected()); 725 726 assertNull("no mail received by mail server", m_mailServer 728 .getLastMail()); 729 730 String ehlo1 = "abgsfe3rsf.de"; 731 String ehlo2 = "james.apache.org"; 732 String mail = "sender"; 733 String rcpt = "recipient"; 734 735 smtpProtocol1.sendCommand("ehlo", ehlo1); 736 smtpProtocol1.setSender(mail); 737 smtpProtocol1.addRecipient(rcpt); 738 739 assertEquals("expected error: ehlo not equals reverse of ip", 501, 741 smtpProtocol1.getReplyCode()); 742 743 smtpProtocol1.sendCommand("ehlo", ehlo2); 744 smtpProtocol1.setSender(mail); 745 smtpProtocol1.addRecipient(rcpt); 746 747 assertEquals("ehlo accepted", 250, smtpProtocol1.getReplyCode()); 749 750 smtpProtocol1.quit(); 751 } finally { 752 m_dnsServer.setLocalhostByName(null); 753 } 754 } 755 756 public void testHeloEnforcement() throws Exception { 757 finishSetUp(m_testConfiguration); 758 759 SMTPClient smtpProtocol1 = new SMTPClient(); 760 smtpProtocol1.connect("127.0.0.1", m_smtpListenerPort); 761 762 assertTrue("first connection taken", smtpProtocol1.isConnected()); 763 764 assertNull("no mail received by mail server", m_mailServer.getLastMail()); 766 767 String sender1 = "mail_sender1@localhost"; 768 smtpProtocol1.setSender(sender1); 769 assertEquals("expected 503 error", 503, smtpProtocol1.getReplyCode()); 770 771 smtpProtocol1.helo(InetAddress.getLocalHost().toString()); 772 773 smtpProtocol1.setSender(sender1); 774 775 smtpProtocol1.quit(); 776 } 777 778 public void testHeloEnforcementDisabled() throws Exception { 779 m_testConfiguration.setHeloEhloEnforcement(false); 780 finishSetUp(m_testConfiguration); 781 782 SMTPClient smtpProtocol1 = new SMTPClient(); 783 smtpProtocol1.connect("127.0.0.1", m_smtpListenerPort); 784 785 assertTrue("first connection taken", smtpProtocol1.isConnected()); 786 787 assertNull("no mail received by mail server", m_mailServer.getLastMail()); 789 790 String sender1 = "mail_sender1@localhost"; 791 792 smtpProtocol1.setSender(sender1); 793 794 smtpProtocol1.quit(); 795 } 796 797 public void testAuth() throws Exception { 798 m_testConfiguration.setAuthorizedAddresses("128.0.0.1/8"); 799 m_testConfiguration.setAuthorizingAnnounce(); 800 finishSetUp(m_testConfiguration); 801 802 SMTPClient smtpProtocol = new SMTPClient(); 803 smtpProtocol.connect("127.0.0.1", m_smtpListenerPort); 804 805 smtpProtocol.sendCommand("ehlo", InetAddress.getLocalHost().toString()); 806 String [] capabilityRes = smtpProtocol.getReplyStrings(); 807 808 List capabilitieslist = new ArrayList (); 809 for (int i = 1; i < capabilityRes.length; i++) { 810 capabilitieslist.add(capabilityRes[i].substring(4)); 811 } 812 813 assertTrue("anouncing auth required", capabilitieslist.contains("AUTH LOGIN PLAIN")); 814 816 String userName = "test_user_smtp"; 817 String noexistUserName = "noexist_test_user_smtp"; 818 String sender ="test_user_smtp@localhost"; 819 smtpProtocol.sendCommand("AUTH FOO", null); 820 assertEquals("expected error: unrecognized authentication type", 504, smtpProtocol.getReplyCode()); 821 822 smtpProtocol.setSender(sender); 823 824 smtpProtocol.addRecipient("mail@sample.com"); 825 assertEquals("expected 530 error", 530, smtpProtocol.getReplyCode()); 826 827 assertFalse("user not existing", m_usersRepository.contains(noexistUserName)); 828 829 smtpProtocol.sendCommand("AUTH PLAIN"); 830 smtpProtocol.sendCommand(Base64.encodeAsString("\0"+noexistUserName+"\0pwd\0")); 831 assertEquals("expected error", 535, smtpProtocol.getReplyCode()); 833 834 m_usersRepository.addUser(userName, "pwd"); 835 836 smtpProtocol.sendCommand("AUTH PLAIN"); 837 smtpProtocol.sendCommand(Base64.encodeAsString("\0"+userName+"\0wrongpwd\0")); 838 assertEquals("expected error", 535, smtpProtocol.getReplyCode()); 839 840 smtpProtocol.sendCommand("AUTH PLAIN"); 841 smtpProtocol.sendCommand(Base64.encodeAsString("\0"+userName+"\0pwd\0")); 842 assertEquals("authenticated", 235, smtpProtocol.getReplyCode()); 843 844 smtpProtocol.sendCommand("AUTH PLAIN"); 845 assertEquals("expected error: User has previously authenticated.", 503, smtpProtocol.getReplyCode()); 846 847 smtpProtocol.addRecipient("mail@sample.com"); 848 smtpProtocol.sendShortMessageData("Subject: test\r\n\r\nTest body testAuth\r\n"); 849 850 smtpProtocol.quit(); 851 852 assertNotNull("mail received by mail server", m_mailServer.getLastMail()); 854 } 855 856 public void testAuthWithEmptySender() throws Exception { 857 m_testConfiguration.setAuthorizedAddresses("128.0.0.1/8"); 858 m_testConfiguration.setAuthorizingAnnounce(); 859 finishSetUp(m_testConfiguration); 860 861 SMTPClient smtpProtocol = new SMTPClient(); 862 smtpProtocol.connect("127.0.0.1", m_smtpListenerPort); 863 864 smtpProtocol.sendCommand("ehlo "+InetAddress.getLocalHost()); 865 866 String userName = "test_user_smtp"; 867 m_usersRepository.addUser(userName, "pwd"); 868 869 smtpProtocol.setSender(""); 870 871 smtpProtocol.sendCommand("AUTH PLAIN"); 872 smtpProtocol.sendCommand(Base64.encodeAsString("\0"+userName+"\0pwd\0")); 873 assertEquals("authenticated", 235, smtpProtocol.getReplyCode()); 874 875 smtpProtocol.addRecipient("mail@sample.com"); 876 assertEquals("expected error", 503, smtpProtocol.getReplyCode()); 877 878 smtpProtocol.quit(); 879 } 880 881 public void testNoRecepientSpecified() throws Exception { 882 finishSetUp(m_testConfiguration); 883 884 SMTPClient smtpProtocol = new SMTPClient(); 885 smtpProtocol.connect("127.0.0.1", m_smtpListenerPort); 886 887 smtpProtocol.sendCommand("ehlo "+InetAddress.getLocalHost()); 888 889 smtpProtocol.setSender("mail@sample.com"); 890 891 893 smtpProtocol.sendShortMessageData("Subject: test\r\n\r\nTest body testNoRecepientSpecified\r\n"); 894 assertTrue("sending succeeded without recepient", SMTPReply.isNegativePermanent(smtpProtocol.getReplyCode())); 895 896 smtpProtocol.quit(); 897 898 assertNull("no mail received by mail server", m_mailServer.getLastMail()); 900 } 901 902 public void testMultipleMailsAndRset() throws Exception { 903 finishSetUp(m_testConfiguration); 904 905 SMTPClient smtpProtocol = new SMTPClient(); 906 smtpProtocol.connect("127.0.0.1", m_smtpListenerPort); 907 908 smtpProtocol.sendCommand("ehlo "+InetAddress.getLocalHost()); 909 910 smtpProtocol.setSender("mail@sample.com"); 911 912 smtpProtocol.reset(); 913 914 smtpProtocol.setSender("mail@sample.com"); 915 916 smtpProtocol.quit(); 917 918 assertNull("no mail received by mail server", m_mailServer.getLastMail()); 920 } 921 922 public void testRelayingDenied() throws Exception { 923 m_testConfiguration.setAuthorizedAddresses("128.0.0.1/8"); 924 finishSetUp(m_testConfiguration); 925 926 SMTPClient smtpProtocol = new SMTPClient(); 927 smtpProtocol.connect("127.0.0.1", m_smtpListenerPort); 928 929 smtpProtocol.sendCommand("ehlo "+InetAddress.getLocalHost()); 930 931 smtpProtocol.setSender("mail@sample.com"); 932 933 smtpProtocol.addRecipient("maila@sample.com"); 934 assertEquals("expected 550 error", 550, smtpProtocol.getReplyCode()); 935 } 936 937 public void testHandleAnnouncedMessageSizeLimitExceeded() throws Exception { 938 m_testConfiguration.setMaxMessageSize(1); finishSetUp(m_testConfiguration); 940 941 SMTPClient smtpProtocol = new SMTPClient(); 942 smtpProtocol.connect("127.0.0.1", m_smtpListenerPort); 943 944 smtpProtocol.sendCommand("ehlo "+InetAddress.getLocalHost()); 945 946 smtpProtocol.sendCommand("MAIL FROM:<mail@localhost> SIZE=1025", null); 947 assertEquals("expected error: max msg size exceeded", 552, smtpProtocol.getReplyCode()); 948 949 smtpProtocol.addRecipient("mail@localhost"); 950 assertEquals("expected error", 503, smtpProtocol.getReplyCode()); 951 } 952 953 public void testHandleMessageSizeLimitExceeded() throws Exception { 954 m_testConfiguration.setMaxMessageSize(1); finishSetUp(m_testConfiguration); 956 957 SMTPClient smtpProtocol = new SMTPClient(); 958 smtpProtocol.connect("127.0.0.1", m_smtpListenerPort); 959 960 smtpProtocol.sendCommand("ehlo "+InetAddress.getLocalHost()); 961 962 smtpProtocol.setSender("mail@localhost"); 963 smtpProtocol.addRecipient("mail@localhost"); 964 965 Writer wr = smtpProtocol.sendMessageData(); 966 wr.write("1234567810123456782012345678301234567840123456785012345678601234567870123456788012345678901234567100"); 968 wr.write("1234567810123456782012345678301234567840123456785012345678601234567870123456788012345678901234567100"); 969 wr.write("1234567810123456782012345678301234567840123456785012345678601234567870123456788012345678901234567100"); 970 wr.write("1234567810123456782012345678301234567840123456785012345678601234567870123456788012345678901234567100"); 971 wr.write("1234567810123456782012345678301234567840123456785012345678601234567870123456788012345678901234567100\r\n"); 972 wr.write("1234567810123456782012345678301234567840123456785012345678601234567870123456788012345678901234567100"); 974 wr.write("1234567810123456782012345678301234567840123456785012345678601234567870123456788012345678901234567100"); 975 wr.write("1234567810123456782012345678301234567840123456785012345678601234567870123456788012345678901234567100"); 976 wr.write("1234567810123456782012345678301234567840123456785012345678601234567870123456788012345678901234567100"); 977 wr.write("1234567810123456782012345678301234567840123456785012345678601234567870123456788012345678901234567100"); 978 wr.write("123456781012345678201\r\n"); wr.close(); 980 981 assertFalse(smtpProtocol.completePendingCommand()); 982 983 assertEquals("expected 552 error", 552, smtpProtocol.getReplyCode()); 984 985 } 986 987 public void testHandleMessageSizeLimitRespected() throws Exception { 988 m_testConfiguration.setMaxMessageSize(1); finishSetUp(m_testConfiguration); 990 991 SMTPClient smtpProtocol = new SMTPClient(); 992 smtpProtocol.connect("127.0.0.1", m_smtpListenerPort); 993 994 smtpProtocol.sendCommand("ehlo "+InetAddress.getLocalHost()); 995 996 smtpProtocol.setSender("mail@localhost"); 997 smtpProtocol.addRecipient("mail@localhost"); 998 999 Writer wr = smtpProtocol.sendMessageData(); 1000 wr.write("1234567810123456782012345678301234567840123456785012345678601234567870123456788012345678901234567100"); 1002 wr.write("1234567810123456782012345678301234567840123456785012345678601234567870123456788012345678901234567100"); 1003 wr.write("1234567810123456782012345678301234567840123456785012345678601234567870123456788012345678901234567100"); 1004 wr.write("1234567810123456782012345678301234567840123456785012345678601234567870123456788012345678901234567100"); 1005 wr.write("1234567810123456782012345678301234567840123456785012345678601234567870123456788012345678901234567100"); 1006 wr.write("1234567810123456782012345678301234567840123456785012345678601234567870123456788012345678901234567100"); 1007 wr.write("1234567810123456782012345678301234567840123456785012345678601234567870123456788012345678901234567100"); 1008 wr.write("1234567810123456782012345678301234567840123456785012345678601234567870123456788012345678901234567100"); 1009 wr.write("1234567810123456782012345678301234567840123456785012345678601234567870123456788012345678901234567100"); 1010 wr.write("1234567810123456782012345678301234567840123456785012345678601234567870123456788012345678901234567100"); 1011 wr.write("1234567810123456782012\r\n"); wr.close(); 1013 1014 assertTrue(smtpProtocol.completePendingCommand()); 1015 1016 assertEquals("expected 250 ok", 250, smtpProtocol.getReplyCode()); 1017 1018 } 1019 1020 1035 1036 public void testConnectionLimitExceeded() throws Exception { 1037 final int acceptLimit = 1; 1038 final int backlog = 1; 1039 1040 m_testConfiguration.setConnectionLimit(acceptLimit); m_testConfiguration.setConnectionBacklog(backlog); finishSetUp(m_testConfiguration); 1043 1044 final SMTPClient[] client = new SMTPClient[acceptLimit]; 1045 for (int i = 0; i < client.length; i++) { 1046 client[i] = new SMTPClient(); try { 1048 client[i].connect("127.0.0.1", m_smtpListenerPort); 1049 } catch (Exception _) { 1050 } 1051 assertTrue("client #" + (i+1) + " established", client[i].isConnected()); 1052 } 1053 1054 1059 final Socket connection[] = new Socket [Math.max(((backlog * 3) / 2) + 1, backlog + 3)]; 1067 1068 final java.net.SocketAddress server = new java.net.InetSocketAddress ("localhost", m_smtpListenerPort); 1069 1070 for (int i = 0; i < connection.length; i++) { 1071 connection[i] = new Socket (); 1072 try { 1073 connection[i].connect(server, 1000); 1074 } catch (Exception _) { 1075 assertTrue("Accepted connections " + i + " did not meet or exceed backlog of " + backlog + ".", i >= backlog); 1076 connection[i] = null; break; } 1079 assertTrue("connection #" + (i+1) + " established", connection[i].isConnected()); 1080 } 1081 1082 try { 1083 final Socket shouldFail = new Socket (); 1084 shouldFail.connect(server, 1000); 1085 fail("connection # " + (client.length + connection.length + 1) + " did not fail."); 1086 } catch (Exception _) { 1087 } 1088 1089 client[0].quit(); 1090 client[0].disconnect(); 1091 1092 Thread.sleep(100); 1093 1094 try { 1096 final Socket shouldWork = new Socket (); 1097 shouldWork.connect(server, 1000); 1098 assertTrue("Additional connection established after close.", shouldWork.isConnected()); 1099 shouldWork.close(); 1100 } catch (Exception e) { 1101 fail("Could not establish additional connection after close." + e.getMessage()); 1102 } 1103 1104 for (int i = 0; i < connection.length; i++) if (connection[i] != null) connection[i].close(); 1106 1107 for (int i = 1; i < client.length; i++) { 1109 client[i].quit(); 1110 client[i].disconnect(); 1111 } 1112 } 1113 1114 1116 InMemorySpoolRepository outgoingSpool; 1117 private MockServiceManager m_serviceManager; 1118 private AlterableDNSServer m_dnsServer; 1119 1120 private Properties getStandardParameters() { 1121 Properties parameters = new Properties (); 1122 parameters.put("delayTime", "500 msec, 500 msec, 500 msec"); parameters.put("maxRetries", "3"); 1124 parameters.put("deliveryThreads", "1"); 1125 parameters.put("debug", "true"); 1126 parameters.put("sendpartial", "false"); 1127 parameters.put("bounceProcessor", "bounce"); 1128 parameters.put("outgoing", "mocked://outgoing/"); 1129 return parameters; 1130 } 1131 1132 1133 1137 public void testDeliveryToSelfWithGatewayAndBind() throws Exception { 1138 finishSetUp(m_testConfiguration); 1139 outgoingSpool = new InMemorySpoolRepository(); 1140 ((MockStore) m_serviceManager.lookup(Store.ROLE)).add("outgoing", outgoingSpool); 1141 1142 RemoteDelivery rd = new RemoteDelivery(); 1143 1144 MockMailContext mmc = new MockMailContext(); 1145 mmc.setAttribute(Constants.AVALON_COMPONENT_MANAGER,m_serviceManager); 1146 mmc.setAttribute(Constants.HELLO_NAME,"localhost"); 1147 MockMailetConfig mci = new MockMailetConfig("Test",mmc,getStandardParameters()); 1148 mci.setProperty("bind", "127.0.0.1"); 1149 mci.setProperty("gateway","127.0.0.1"); 1150 mci.setProperty("gatewayPort",""+m_smtpListenerPort); 1151 rd.init(mci); 1152 String sources = "Content-Type: text/plain;\r\nSubject: test\r\n\r\nBody"; 1153 String sender = "test@localhost"; 1154 String recipient = "test@localhost"; 1155 MimeMessage mm = new MimeMessage (Session.getDefaultInstance(new Properties ()),new ByteArrayInputStream (sources.getBytes())); 1156 MailImpl mail = new MailImpl("name",new MailAddress(sender),Arrays.asList(new MailAddress[] {new MailAddress(recipient)}),mm); 1157 1158 rd.service(mail); 1159 1160 while (outgoingSpool.size() > 0) { 1161 Thread.sleep(1000); 1162 } 1163 1164 verifyLastMail(sender, recipient, null); 1165 1166 assertEquals(((String ) mm.getContent()).trim(),((String ) ((MimeMessage ) m_mailServer.getLastMail().getMessage()).getContent()).trim()); 1167 1168 mail.dispose(); 1169 } 1170 1171 1172 1179 public void test8bitmimeFromStream() throws Exception { 1180 finishSetUp(m_testConfiguration); 1181 outgoingSpool = new InMemorySpoolRepository(); 1182 ((MockStore) m_serviceManager.lookup(Store.ROLE)).add("outgoing", outgoingSpool); 1183 1184 RemoteDelivery rd = new RemoteDelivery(); 1185 1186 MockMailContext mmc = new MockMailContext(); 1187 mmc.setAttribute(Constants.AVALON_COMPONENT_MANAGER,m_serviceManager); 1188 mmc.setAttribute(Constants.HELLO_NAME,"localhost"); 1189 MockMailetConfig mci = new MockMailetConfig("Test",mmc,getStandardParameters()); 1190 mci.setProperty("gateway","127.0.0.1"); 1191 mci.setProperty("gatewayPort",""+m_smtpListenerPort); 1192 rd.init(mci); 1193 1194 String sources = "Content-Type: text/plain; charset=iso-8859-15\r\nContent-Transfer-Encoding: quoted-printable\r\nSubject: test\r\n\r\nBody=80\r\n"; 1196 String sender = "test@localhost"; 1198 String recipient = "test@localhost"; 1199 MimeMessage mm = new MimeMessage (Session.getDefaultInstance(new Properties ()),new ByteArrayInputStream (sources.getBytes())); 1200 MailImpl mail = new MailImpl("name",new MailAddress(sender),Arrays.asList(new MailAddress[] {new MailAddress(recipient)}),mm); 1201 1202 rd.service(mail); 1203 1204 while (outgoingSpool.size() > 0) { 1205 Thread.sleep(1000); 1206 } 1207 1208 verifyLastMail(sender, recipient, null); 1210 1211 assertEquals(mm.getContent(),m_mailServer.getLastMail().getMessage().getContent()); 1213 1214 mail.dispose(); 1215 } 1216 1217 1224 public void test8bitmimeFromStreamWith8bitContent() throws Exception { 1225 finishSetUp(m_testConfiguration); 1226 outgoingSpool = new InMemorySpoolRepository(); 1227 ((MockStore) m_serviceManager.lookup(Store.ROLE)).add("outgoing", outgoingSpool); 1228 1229 RemoteDelivery rd = new RemoteDelivery(); 1230 1231 MockMailContext mmc = new MockMailContext(); 1232 mmc.setAttribute(Constants.AVALON_COMPONENT_MANAGER,m_serviceManager); 1233 mmc.setAttribute(Constants.HELLO_NAME,"localhost"); 1234 MockMailetConfig mci = new MockMailetConfig("Test",mmc,getStandardParameters()); 1235 mci.setProperty("gateway","127.0.0.1"); 1236 mci.setProperty("gatewayPort",""+m_smtpListenerPort); 1237 rd.init(mci); 1238 1239 String sources = "Content-Type: text/plain; charset=iso-8859-15\r\nContent-Transfer-Encoding: 8bit\r\nSubject: test\r\n\r\nBody\u20AC\r\n"; 1242 String sender = "test@localhost"; 1243 String recipient = "test@localhost"; 1244 MimeMessage mm = new MimeMessage (Session.getDefaultInstance(new Properties ()),new ByteArrayInputStream (sources.getBytes())); 1245 MailImpl mail = new MailImpl("name",new MailAddress(sender),Arrays.asList(new MailAddress[] {new MailAddress(recipient)}),mm); 1246 1247 rd.service(mail); 1248 1249 while (outgoingSpool.size() > 0) { 1250 Thread.sleep(1000); 1251 } 1252 1253 verifyLastMail(sender, recipient, null); 1255 1256 assertEquals(mm.getContent(),m_mailServer.getLastMail().getMessage().getContent()); 1258 1259 mail.dispose(); 1260 } 1261 1262 1269 public void test8bitmimeFromStreamWithoutContentTransferEncoding() throws Exception { 1270 finishSetUp(m_testConfiguration); 1271 outgoingSpool = new InMemorySpoolRepository(); 1272 ((MockStore) m_serviceManager.lookup(Store.ROLE)).add("outgoing", outgoingSpool); 1273 1274 RemoteDelivery rd = new RemoteDelivery(); 1275 1276 MockMailContext mmc = new MockMailContext(); 1277 mmc.setAttribute(Constants.AVALON_COMPONENT_MANAGER,m_serviceManager); 1278 mmc.setAttribute(Constants.HELLO_NAME,"localhost"); 1279 MockMailetConfig mci = new MockMailetConfig("Test",mmc,getStandardParameters()); 1280 mci.setProperty("gateway","127.0.0.1"); 1281 mci.setProperty("gatewayPort",""+m_smtpListenerPort); 1282 rd.init(mci); 1283 1284 String sources = "Content-Type: text/plain;\r\nSubject: test\r\n\r\nBody\u03B2\r\n"; 1285 String sender = "test@localhost"; 1288 String recipient = "test@localhost"; 1289 MimeMessage mm = new MimeMessage (Session.getDefaultInstance(new Properties ()),new ByteArrayInputStream (sources.getBytes())); 1290 MailImpl mail = new MailImpl("name",new MailAddress(sender),Arrays.asList(new MailAddress[] {new MailAddress(recipient)}),mm); 1291 1292 rd.service(mail); 1293 1294 while (outgoingSpool.size() > 0) { 1295 Thread.sleep(1000); 1296 } 1297 1298 verifyLastMail(sender, recipient, null); 1300 1301 assertEquals(mm.getContent(),m_mailServer.getLastMail().getMessage().getContent()); 1303 1304 mail.dispose(); 1305 } 1306 1307 1314 public void test8bitmimeFromStreamWithoutContentTransferEncodingSentAs8bit() throws Exception { 1315 finishSetUp(m_testConfiguration); 1316 outgoingSpool = new InMemorySpoolRepository(); 1317 ((MockStore) m_serviceManager.lookup(Store.ROLE)).add("outgoing", outgoingSpool); 1318 1319 RemoteDelivery rd = new RemoteDelivery(); 1320 1321 MockMailContext mmc = new MockMailContext(); 1322 mmc.setAttribute(Constants.AVALON_COMPONENT_MANAGER,m_serviceManager); 1323 mmc.setAttribute(Constants.HELLO_NAME,"localhost"); 1324 MockMailetConfig mci = new MockMailetConfig("Test",mmc,getStandardParameters()); 1325 mci.setProperty("gateway","127.0.0.1"); 1326 mci.setProperty("gatewayPort",""+m_smtpListenerPort); 1327 rd.init(mci); 1328 1329 String sources = "Content-Type: text/plain;\r\nSubject: test\r\n\r\nBody=32=48\r\n"; 1330 String sender = "test@localhost"; 1333 String recipient = "test@localhost"; 1334 MimeMessage mm = new MimeMessage (Session.getDefaultInstance(new Properties ()),new ByteArrayInputStream (sources.getBytes())); 1335 MailImpl mail = new MailImpl("name",new MailAddress(sender),Arrays.asList(new MailAddress[] {new MailAddress(recipient)}),mm); 1336 1337 rd.service(mail); 1338 1339 while (outgoingSpool.size() > 0) { 1340 Thread.sleep(1000); 1341 } 1342 1343 verifyLastMail(sender, recipient, null); 1345 1346 assertEquals(mm.getContent(),m_mailServer.getLastMail().getMessage().getContent()); 1348 1349 mail.dispose(); 1350 } 1351 1352 1359 public void test8bitmimeWith8bitmimeDisabledInServer() throws Exception { 1360 finishSetUp(m_testConfiguration); 1361 outgoingSpool = new InMemorySpoolRepository(); 1362 ((MockStore) m_serviceManager.lookup(Store.ROLE)).add("outgoing", outgoingSpool); 1363 1364 RemoteDelivery rd = new RemoteDelivery(); 1365 1366 MockMailContext mmc = new MockMailContext(); 1367 mmc.setAttribute(Constants.AVALON_COMPONENT_MANAGER,m_serviceManager); 1368 mmc.setAttribute(Constants.HELLO_NAME,"localhost"); 1369 MockMailetConfig mci = new MockMailetConfig("Test",mmc,getStandardParameters()); 1370 mci.setProperty("gateway","127.0.0.1"); 1371 mci.setProperty("gatewayPort",""+m_smtpListenerPort); 1372 mci.setProperty("mail.smtp.allow8bitmime", "false"); 1373 rd.init(mci); 1374 1375 String sources = "Content-Type: text/plain; charset=iso-8859-15\r\nContent-Transfer-Encoding: 8bit\r\nSubject: test\r\n\r\nBody\u20AC\r\n"; 1378 String sender = "test@localhost"; 1379 String recipient = "test@localhost"; 1380 MimeMessage mm = new MimeMessage (Session.getDefaultInstance(new Properties ()),new ByteArrayInputStream (sources.getBytes())); 1381 MailImpl mail = new MailImpl("name",new MailAddress(sender),Arrays.asList(new MailAddress[] {new MailAddress(recipient)}),mm); 1382 1383 rd.service(mail); 1384 1385 while (outgoingSpool.size() > 0) { 1386 Thread.sleep(1000); 1387 } 1388 1389 verifyLastMail(sender, recipient, null); 1391 1392 assertEquals(mm.getContent(),m_mailServer.getLastMail().getMessage().getContent()); 1394 1395 mail.dispose(); 1396 } 1397 1398 public void testDNSRBLNotRejectAuthUser() throws Exception { 1400 m_testConfiguration.setAuthorizedAddresses("192.168.0.1/32"); 1401 m_testConfiguration.setAuthorizingAnnounce(); 1402 m_testConfiguration.useRBL(true); 1403 finishSetUp(m_testConfiguration); 1404 1405 m_dnsServer.setLocalhostByName(InetAddress.getByName("127.0.0.1")); 1406 1407 SMTPClient smtpProtocol = new SMTPClient(); 1408 smtpProtocol.connect("127.0.0.1", m_smtpListenerPort); 1409 1410 smtpProtocol.sendCommand("ehlo", InetAddress.getLocalHost().toString()); 1411 String [] capabilityRes = smtpProtocol.getReplyStrings(); 1412 1413 List capabilitieslist = new ArrayList (); 1414 for (int i = 1; i < capabilityRes.length; i++) { 1415 capabilitieslist.add(capabilityRes[i].substring(4)); 1416 } 1417 1418 assertTrue("anouncing auth required", capabilitieslist 1419 .contains("AUTH LOGIN PLAIN")); 1420 1423 String userName = "test_user_smtp"; 1424 String sender = "test_user_smtp@localhost"; 1425 1426 smtpProtocol.setSender(sender); 1427 1428 m_usersRepository.addUser(userName, "pwd"); 1429 1430 smtpProtocol.sendCommand("AUTH PLAIN"); 1431 smtpProtocol.sendCommand(Base64.encodeAsString("\0" + userName 1432 + "\0pwd\0")); 1433 assertEquals("authenticated", 235, smtpProtocol.getReplyCode()); 1434 1435 smtpProtocol.addRecipient("mail@sample.com"); 1436 assertEquals("authenticated.. not reject", 250, smtpProtocol 1437 .getReplyCode()); 1438 1439 smtpProtocol.sendShortMessageData("Subject: test\r\n\r\nTest body testDNSRBLNotRejectAuthUser\r\n"); 1440 1441 smtpProtocol.quit(); 1442 1443 assertNotNull("mail received by mail server", m_mailServer 1445 .getLastMail()); 1446 } 1447 1448 1449 public void testDNSRBLRejectWorks() throws Exception { 1450 m_testConfiguration.setAuthorizedAddresses("192.168.0.1/32"); 1451 m_testConfiguration.useRBL(true); 1452 finishSetUp(m_testConfiguration); 1453 1454 m_dnsServer.setLocalhostByName(InetAddress.getByName("127.0.0.1")); 1455 1456 SMTPClient smtpProtocol = new SMTPClient(); 1457 smtpProtocol.connect("127.0.0.1", m_smtpListenerPort); 1458 1459 smtpProtocol.sendCommand("ehlo", InetAddress.getLocalHost().toString()); 1460 1461 String sender = "test_user_smtp@localhost"; 1462 1463 smtpProtocol.setSender(sender); 1464 1465 smtpProtocol.addRecipient("mail@sample.com"); 1466 assertEquals("reject", 550, smtpProtocol 1467 .getReplyCode()); 1468 1469 smtpProtocol.sendShortMessageData("Subject: test\r\n\r\nTest body testDNSRBLRejectWorks\r\n"); 1470 1471 smtpProtocol.quit(); 1472 1473 assertNull("mail reject by mail server", m_mailServer 1475 .getLastMail()); 1476 } 1477 1478 1479 public void testAddressBracketsEnforcementDisabled() throws Exception { 1480 m_testConfiguration.setAddressBracketsEnforcement(false); 1481 finishSetUp(m_testConfiguration); 1482 SMTPClient smtpProtocol = new SMTPClient(); 1483 smtpProtocol.connect("127.0.0.1", m_smtpListenerPort); 1484 1485 smtpProtocol.sendCommand("ehlo", InetAddress.getLocalHost().toString()); 1486 1487 smtpProtocol.sendCommand("mail from:", "test@localhost"); 1488 assertEquals("accept", 250,smtpProtocol.getReplyCode()); 1489 1490 smtpProtocol.sendCommand("rcpt to:", "mail@sample.com"); 1491 assertEquals("accept", 250,smtpProtocol.getReplyCode()); 1492 1493 smtpProtocol.quit(); 1494 1495 smtpProtocol.connect("127.0.0.1", m_smtpListenerPort); 1496 1497 smtpProtocol.sendCommand("ehlo", InetAddress.getLocalHost().toString()); 1498 1499 smtpProtocol.sendCommand("mail from:", "<test@localhost>"); 1500 assertEquals("accept", 250,smtpProtocol.getReplyCode()); 1501 1502 smtpProtocol.sendCommand("rcpt to:", "<mail@sample.com>"); 1503 assertEquals("accept", 250,smtpProtocol.getReplyCode()); 1504 1505 smtpProtocol.quit(); 1506 } 1507 1508 public void testAddressBracketsEnforcementEnabled() throws Exception { 1509 finishSetUp(m_testConfiguration); 1510 SMTPClient smtpProtocol = new SMTPClient(); 1511 smtpProtocol.connect("127.0.0.1", m_smtpListenerPort); 1512 1513 smtpProtocol.sendCommand("ehlo", InetAddress.getLocalHost().toString()); 1514 1515 smtpProtocol.sendCommand("mail from:", "test@localhost"); 1516 assertEquals("reject", 501,smtpProtocol.getReplyCode()); 1517 smtpProtocol.sendCommand("mail from:", "<test@localhost>"); 1518 assertEquals("accept", 250,smtpProtocol.getReplyCode()); 1519 1520 smtpProtocol.sendCommand("rcpt to:", "mail@sample.com"); 1521 assertEquals("reject", 501,smtpProtocol.getReplyCode()); 1522 smtpProtocol.sendCommand("rcpt to:", "<mail@sample.com>"); 1523 assertEquals("accept", 250,smtpProtocol.getReplyCode()); 1524 1525 smtpProtocol.quit(); 1526 } 1527 1528 public void testPipelining() throws Exception { 1530 StringBuffer buf = new StringBuffer (); 1531 finishSetUp(m_testConfiguration); 1532 Socket client = new Socket ("127.0.0.1",m_smtpListenerPort); 1533 1534 buf.append("HELO TEST"); 1535 buf.append("\r\n"); 1536 buf.append("MAIL FROM: <test@localhost>"); 1537 buf.append("\r\n"); 1538 buf.append("RCPT TO: <test2@localhost>"); 1539 buf.append("\r\n"); 1540 buf.append("DATA"); 1541 buf.append("\r\n"); 1542 buf.append("Subject: test"); 1543 buf.append("\r\n");; 1544 buf.append("\r\n"); 1545 buf.append("content"); 1546 buf.append("\r\n"); 1547 buf.append("."); 1548 buf.append("\r\n"); 1549 buf.append("quit"); 1550 buf.append("\r\n"); 1551 1552 OutputStream out = client.getOutputStream(); 1553 1554 out.write(buf.toString().getBytes()); 1555 out.flush(); 1556 1557 BufferedReader in = new BufferedReader (new InputStreamReader (client.getInputStream())); 1558 1559 assertEquals("Connection made" , 220, Integer.parseInt(in.readLine().split(" ")[0])); 1560 assertEquals("HELO accepted" , 250, Integer.parseInt(in.readLine().split(" ")[0])); 1561 assertEquals("MAIL FROM accepted" , 250, Integer.parseInt(in.readLine().split(" ")[0])); 1562 assertEquals("RCPT TO accepted" , 250, Integer.parseInt(in.readLine().split(" ")[0])); 1563 assertEquals("DATA accepted" , 354, Integer.parseInt(in.readLine().split(" ")[0])); 1564 assertEquals("Message accepted" , 250, Integer.parseInt(in.readLine().split(" ")[0])); 1565 in.close(); 1566 out.close(); 1567 client.close(); 1568 } 1569 1570 public void testRejectAllRCPTPipelining() throws Exception { 1572 StringBuffer buf = new StringBuffer (); 1573 m_testConfiguration.setAuthorizedAddresses(""); 1574 finishSetUp(m_testConfiguration); 1575 Socket client = new Socket ("127.0.0.1",m_smtpListenerPort); 1576 1577 buf.append("HELO TEST"); 1578 buf.append("\r\n"); 1579 buf.append("MAIL FROM: <test@localhost>"); 1580 buf.append("\r\n"); 1581 buf.append("RCPT TO: <test@invalid>"); 1582 buf.append("\r\n"); 1583 buf.append("RCPT TO: <test2@invalid>"); 1584 buf.append("\r\n"); 1585 buf.append("DATA"); 1586 buf.append("\r\n"); 1587 buf.append("Subject: test"); 1588 buf.append("\r\n");; 1589 buf.append("\r\n"); 1590 buf.append("content"); 1591 buf.append("\r\n"); 1592 buf.append("."); 1593 buf.append("\r\n"); 1594 buf.append("quit"); 1595 buf.append("\r\n"); 1596 1597 OutputStream out = client.getOutputStream(); 1598 1599 out.write(buf.toString().getBytes()); 1600 out.flush(); 1601 1602 BufferedReader in = new BufferedReader (new InputStreamReader (client.getInputStream())); 1603 1604 assertEquals("Connection made" , 220, Integer.parseInt(in.readLine().split(" ")[0])); 1605 assertEquals("HELO accepted" , 250, Integer.parseInt(in.readLine().split(" ")[0])); 1606 assertEquals("MAIL FROM accepted" , 250, Integer.parseInt(in.readLine().split(" ")[0])); 1607 assertEquals("RCPT TO rejected" , 550, Integer.parseInt(in.readLine().split(" ")[0])); 1608 assertEquals("RCPT TO rejected" , 550, Integer.parseInt(in.readLine().split(" ")[0])); 1609 assertEquals("DATA not accepted" , 503, Integer.parseInt(in.readLine().split(" ")[0])); 1610 in.close(); 1611 out.close(); 1612 client.close(); 1613 } 1614 1615 public void testRejectOneRCPTPipelining() throws Exception { 1616 StringBuffer buf = new StringBuffer (); 1617 m_testConfiguration.setAuthorizedAddresses(""); 1618 finishSetUp(m_testConfiguration); 1619 Socket client = new Socket ("127.0.0.1",m_smtpListenerPort); 1620 1621 buf.append("HELO TEST"); 1622 buf.append("\r\n"); 1623 buf.append("MAIL FROM: <test@localhost>"); 1624 buf.append("\r\n"); 1625 buf.append("RCPT TO: <test@invalid>"); 1626 buf.append("\r\n"); 1627 buf.append("RCPT TO: <test2@localhost>"); 1628 buf.append("\r\n"); 1629 buf.append("DATA"); 1630 buf.append("\r\n"); 1631 buf.append("Subject: test"); 1632 buf.append("\r\n");; 1633 buf.append("\r\n"); 1634 buf.append("content"); 1635 buf.append("\r\n"); 1636 buf.append("."); 1637 buf.append("\r\n"); 1638 buf.append("quit"); 1639 buf.append("\r\n"); 1640 1641 OutputStream out = client.getOutputStream(); 1642 1643 out.write(buf.toString().getBytes()); 1644 out.flush(); 1645 1646 BufferedReader in = new BufferedReader (new InputStreamReader (client.getInputStream())); 1647 1648 assertEquals("Connection made" , 220, Integer.parseInt(in.readLine().split(" ")[0])); 1649 assertEquals("HELO accepted" , 250, Integer.parseInt(in.readLine().split(" ")[0])); 1650 assertEquals("MAIL FROM accepted" , 250, Integer.parseInt(in.readLine().split(" ")[0])); 1651 assertEquals("RCPT TO rejected" , 550, Integer.parseInt(in.readLine().split(" ")[0])); 1652 assertEquals("RCPT accepted" , 250, Integer.parseInt(in.readLine().split(" ")[0])); 1653 assertEquals("DATA accepted" , 354, Integer.parseInt(in.readLine().split(" ")[0])); 1654 assertEquals("Message accepted" , 250, Integer.parseInt(in.readLine().split(" ")[0])); 1655 in.close(); 1656 out.close(); 1657 client.close(); 1658 } 1659} 1660 | Popular Tags |