Source Code Cross Referenced for SMTPServerTest.java in  » Web-Mail » james-2.3.1 » org » apache » james » smtpserver » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Web Mail » james 2.3.1 » org.apache.james.smtpserver 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


0001:        /****************************************************************
0002:         * Licensed to the Apache Software Foundation (ASF) under one   *
0003:         * or more contributor license agreements.  See the NOTICE file *
0004:         * distributed with this work for additional information        *
0005:         * regarding copyright ownership.  The ASF licenses this file   *
0006:         * to you under the Apache License, Version 2.0 (the            *
0007:         * "License"); you may not use this file except in compliance   *
0008:         * with the License.  You may obtain a copy of the License at   *
0009:         *                                                              *
0010:         *   http://www.apache.org/licenses/LICENSE-2.0                 *
0011:         *                                                              *
0012:         * Unless required by applicable law or agreed to in writing,   *
0013:         * software distributed under the License is distributed on an  *
0014:         * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY       *
0015:         * KIND, either express or implied.  See the License for the    *
0016:         * specific language governing permissions and limitations      *
0017:         * under the License.                                           *
0018:         ****************************************************************/package org.apache.james.smtpserver;
0019:
0020:        import org.apache.avalon.cornerstone.services.sockets.SocketManager;
0021:        import org.apache.avalon.cornerstone.services.store.Store;
0022:        import org.apache.avalon.cornerstone.services.threads.ThreadManager;
0023:        import org.apache.avalon.framework.container.ContainerUtil;
0024:        import org.apache.commons.net.smtp.SMTPClient;
0025:        import org.apache.commons.net.smtp.SMTPReply;
0026:        import org.apache.james.Constants;
0027:        import org.apache.james.core.MailImpl;
0028:        import org.apache.james.services.DNSServer;
0029:        import org.apache.james.services.JamesConnectionManager;
0030:        import org.apache.james.services.MailServer;
0031:        import org.apache.james.services.UsersRepository;
0032:        import org.apache.james.test.mock.avalon.MockLogger;
0033:        import org.apache.james.test.mock.avalon.MockServiceManager;
0034:        import org.apache.james.test.mock.avalon.MockSocketManager;
0035:        import org.apache.james.test.mock.avalon.MockStore;
0036:        import org.apache.james.test.mock.avalon.MockThreadManager;
0037:        import org.apache.james.test.mock.james.InMemorySpoolRepository;
0038:        import org.apache.james.test.mock.james.MockMailServer;
0039:        import org.apache.james.test.mock.mailet.MockMailContext;
0040:        import org.apache.james.test.mock.mailet.MockMailetConfig;
0041:        import org.apache.james.test.util.Util;
0042:        import org.apache.james.transport.mailets.RemoteDelivery;
0043:        import org.apache.james.userrepository.MockUsersRepository;
0044:        import org.apache.james.util.Base64;
0045:        import org.apache.james.util.connection.SimpleConnectionManager;
0046:        import org.apache.mailet.MailAddress;
0047:
0048:        import javax.mail.MessagingException;
0049:        import javax.mail.Session;
0050:        import javax.mail.internet.MimeMessage;
0051:
0052:        import java.io.ByteArrayInputStream;
0053:        import java.io.ByteArrayOutputStream;
0054:        import java.io.IOException;
0055:        import java.io.Writer;
0056:        import java.net.InetAddress;
0057:        import java.net.Socket;
0058:        import java.net.UnknownHostException;
0059:        import java.util.ArrayList;
0060:        import java.util.Arrays;
0061:        import java.util.Collection;
0062:        import java.util.Iterator;
0063:        import java.util.List;
0064:        import java.util.Properties;
0065:
0066:        import junit.framework.TestCase;
0067:
0068:        /**
0069:         * Tests the org.apache.james.smtpserver.SMTPServer unit 
0070:         */
0071:        public class SMTPServerTest extends TestCase {
0072:            private int m_smtpListenerPort = Util.getNonPrivilegedPort();
0073:            private MockMailServer m_mailServer;
0074:            private SMTPTestConfiguration m_testConfiguration;
0075:            private SMTPServer m_smtpServer;
0076:            private MockUsersRepository m_usersRepository = new MockUsersRepository();
0077:
0078:            public SMTPServerTest() {
0079:                super ("SMTPServerTest");
0080:            }
0081:
0082:            public void verifyLastMail(String sender, String recipient,
0083:                    MimeMessage msg) throws IOException, MessagingException {
0084:                Object[] mailData = m_mailServer.getLastMail();
0085:                assertNotNull("mail received by mail server", mailData);
0086:
0087:                if (sender == null && recipient == null && msg == null)
0088:                    fail("no verification can be done with all arguments null");
0089:
0090:                if (sender != null)
0091:                    assertEquals("sender verfication", sender,
0092:                            ((MailAddress) mailData[0]).toString());
0093:                if (recipient != null)
0094:                    assertTrue("recipient verfication",
0095:                            ((Collection) mailData[1])
0096:                                    .contains(new MailAddress(recipient)));
0097:                if (msg != null) {
0098:                    ByteArrayOutputStream bo1 = new ByteArrayOutputStream();
0099:                    msg.writeTo(bo1);
0100:                    ByteArrayOutputStream bo2 = new ByteArrayOutputStream();
0101:                    ((MimeMessage) mailData[2]).writeTo(bo2);
0102:                    assertEquals(bo1.toString(), bo2.toString());
0103:                    assertEquals("message verification", msg,
0104:                            ((MimeMessage) mailData[2]));
0105:                }
0106:            }
0107:
0108:            protected void setUp() throws Exception {
0109:                m_smtpServer = new SMTPServer();
0110:                ContainerUtil.enableLogging(m_smtpServer, new MockLogger());
0111:                m_serviceManager = setUpServiceManager();
0112:                ContainerUtil.service(m_smtpServer, m_serviceManager);
0113:                m_testConfiguration = new SMTPTestConfiguration(
0114:                        m_smtpListenerPort);
0115:            }
0116:
0117:            private void finishSetUp(SMTPTestConfiguration testConfiguration)
0118:                    throws Exception {
0119:                testConfiguration.init();
0120:                ContainerUtil.configure(m_smtpServer, testConfiguration);
0121:                ContainerUtil.initialize(m_smtpServer);
0122:                m_mailServer.setMaxMessageSizeBytes(m_testConfiguration
0123:                        .getMaxMessageSize() * 1024);
0124:            }
0125:
0126:            private MockServiceManager setUpServiceManager() throws Exception {
0127:                m_serviceManager = new MockServiceManager();
0128:                SimpleConnectionManager connectionManager = new SimpleConnectionManager();
0129:                ContainerUtil
0130:                        .enableLogging(connectionManager, new MockLogger());
0131:                m_serviceManager.put(JamesConnectionManager.ROLE,
0132:                        connectionManager);
0133:                m_serviceManager.put("org.apache.mailet.MailetContext",
0134:                        new MockMailContext());
0135:                m_mailServer = new MockMailServer();
0136:                m_serviceManager.put(MailServer.ROLE, m_mailServer);
0137:                m_serviceManager.put(UsersRepository.ROLE, m_usersRepository);
0138:                m_serviceManager.put(SocketManager.ROLE, new MockSocketManager(
0139:                        m_smtpListenerPort));
0140:                m_serviceManager.put(ThreadManager.ROLE,
0141:                        new MockThreadManager());
0142:                // Mock DNS Server
0143:                DNSServer dns = new DNSServer() {
0144:
0145:                    public Collection findMXRecords(String hostname) {
0146:                        List res = new ArrayList();
0147:                        if (hostname == null) {
0148:                            return res;
0149:                        }
0150:                        ;
0151:                        if ("james.apache.org".equals(hostname)) {
0152:                            res.add("nagoya.apache.org");
0153:                        }
0154:                        return res;
0155:                    }
0156:
0157:                    public Iterator getSMTPHostAddresses(String domainName) {
0158:                        throw new UnsupportedOperationException(
0159:                                "Unimplemented mock service");
0160:                    }
0161:
0162:                    public InetAddress[] getAllByName(String host)
0163:                            throws UnknownHostException {
0164:                        return new InetAddress[] { getByName(host) };
0165:                        //                throw new UnsupportedOperationException("getByName not implemented in mock for host: "+host);
0166:                    }
0167:
0168:                    public InetAddress getByName(String host)
0169:                            throws UnknownHostException {
0170:                        return InetAddress.getByName(host);
0171:                        //                throw new UnsupportedOperationException("getByName not implemented in mock for host: "+host);
0172:                    }
0173:
0174:                    public Collection findTXTRecords(String hostname) {
0175:                        List res = new ArrayList();
0176:                        if (hostname == null) {
0177:                            return res;
0178:                        }
0179:                        ;
0180:                        if ("2.0.0.127.bl.spamcop.net".equals(hostname)) {
0181:                            res
0182:                                    .add("Blocked - see http://www.spamcop.net/bl.shtml?127.0.0.2");
0183:                        }
0184:                        return res;
0185:                    }
0186:
0187:                };
0188:                m_serviceManager.put(DNSServer.ROLE, dns);
0189:                m_serviceManager.put(Store.ROLE, new MockStore());
0190:                return m_serviceManager;
0191:            }
0192:
0193:            public void testSimpleMailSendWithEHLO() throws Exception {
0194:                finishSetUp(m_testConfiguration);
0195:
0196:                SMTPClient smtpProtocol = new SMTPClient();
0197:                smtpProtocol.connect("127.0.0.1", m_smtpListenerPort);
0198:
0199:                // no message there, yet
0200:                assertNull("no mail received by mail server", m_mailServer
0201:                        .getLastMail());
0202:
0203:                smtpProtocol.sendCommand("EHLO " + InetAddress.getLocalHost());
0204:                String[] capabilityRes = smtpProtocol.getReplyStrings();
0205:
0206:                List capabilitieslist = new ArrayList();
0207:                for (int i = 1; i < capabilityRes.length; i++) {
0208:                    capabilitieslist.add(capabilityRes[i].substring(4));
0209:                }
0210:
0211:                assertEquals("capabilities", 2, capabilitieslist.size());
0212:                assertTrue("capabilities present PIPELINING", capabilitieslist
0213:                        .contains("PIPELINING"));
0214:                assertTrue("capabilities present ENHANCEDSTATUSCODES",
0215:                        capabilitieslist.contains("ENHANCEDSTATUSCODES"));
0216:                //assertTrue("capabilities present 8BITMIME", capabilitieslist.contains("8BITMIME"));
0217:
0218:                smtpProtocol.setSender("mail@localhost");
0219:                smtpProtocol.addRecipient("mail@localhost");
0220:
0221:                smtpProtocol
0222:                        .sendShortMessageData("Subject: test\r\n\r\nBody\r\n\r\n.\r\n");
0223:                smtpProtocol.quit();
0224:                smtpProtocol.disconnect();
0225:
0226:                // mail was propagated by SMTPServer
0227:                assertNotNull("mail received by mail server", m_mailServer
0228:                        .getLastMail());
0229:            }
0230:
0231:            public void testEmptyMessage() throws Exception {
0232:                finishSetUp(m_testConfiguration);
0233:
0234:                SMTPClient smtp = new SMTPClient();
0235:                smtp.connect("127.0.0.1", m_smtpListenerPort);
0236:
0237:                // no message there, yet
0238:                assertNull("no mail received by mail server", m_mailServer
0239:                        .getLastMail());
0240:
0241:                smtp.helo(InetAddress.getLocalHost().toString());
0242:
0243:                smtp.setSender("mail@localhost");
0244:
0245:                smtp.addRecipient("mail@localhost");
0246:
0247:                smtp.sendShortMessageData("");
0248:
0249:                smtp.quit();
0250:
0251:                smtp.disconnect();
0252:
0253:                // mail was propagated by SMTPServer
0254:                assertNotNull("mail received by mail server", m_mailServer
0255:                        .getLastMail());
0256:
0257:                // added to check a NPE in the test (JAMES-474) due to MockMailServer
0258:                // not cloning the message (added a MimeMessageCopyOnWriteProxy there)
0259:                System.gc();
0260:
0261:                int size = ((MimeMessage) m_mailServer.getLastMail()[2])
0262:                        .getSize();
0263:
0264:                assertEquals(size, 2);
0265:            }
0266:
0267:            public void testSimpleMailSendWithHELO() throws Exception {
0268:                finishSetUp(m_testConfiguration);
0269:
0270:                SMTPClient smtpProtocol = new SMTPClient();
0271:                smtpProtocol.connect("127.0.0.1", m_smtpListenerPort);
0272:
0273:                // no message there, yet
0274:                assertNull("no mail received by mail server", m_mailServer
0275:                        .getLastMail());
0276:
0277:                smtpProtocol.helo(InetAddress.getLocalHost().toString());
0278:
0279:                smtpProtocol.setSender("mail@localhost");
0280:
0281:                smtpProtocol.addRecipient("mail@localhost");
0282:
0283:                smtpProtocol
0284:                        .sendShortMessageData("Subject: test mail\r\n\r\nTest body\r\n.\r\n");
0285:
0286:                smtpProtocol.quit();
0287:                smtpProtocol.disconnect();
0288:
0289:                // mail was propagated by SMTPServer
0290:                assertNotNull("mail received by mail server", m_mailServer
0291:                        .getLastMail());
0292:            }
0293:
0294:            public void testTwoSimultaneousMails() throws Exception {
0295:                finishSetUp(m_testConfiguration);
0296:
0297:                SMTPClient smtpProtocol1 = new SMTPClient();
0298:                smtpProtocol1.connect("127.0.0.1", m_smtpListenerPort);
0299:                SMTPClient smtpProtocol2 = new SMTPClient();
0300:                smtpProtocol2.connect("127.0.0.1", m_smtpListenerPort);
0301:
0302:                assertTrue("first connection taken", smtpProtocol1
0303:                        .isConnected());
0304:                assertTrue("second connection taken", smtpProtocol2
0305:                        .isConnected());
0306:
0307:                // no message there, yet
0308:                assertNull("no mail received by mail server", m_mailServer
0309:                        .getLastMail());
0310:
0311:                smtpProtocol1.helo(InetAddress.getLocalHost().toString());
0312:                smtpProtocol2.helo(InetAddress.getLocalHost().toString());
0313:
0314:                String sender1 = "mail_sender1@localhost";
0315:                String recipient1 = "mail_recipient1@localhost";
0316:                smtpProtocol1.setSender(sender1);
0317:                smtpProtocol1.addRecipient(recipient1);
0318:
0319:                String sender2 = "mail_sender2@localhost";
0320:                String recipient2 = "mail_recipient2@localhost";
0321:                smtpProtocol2.setSender(sender2);
0322:                smtpProtocol2.addRecipient(recipient2);
0323:
0324:                smtpProtocol1
0325:                        .sendShortMessageData("Subject: test\r\n\r\nTest body\r\n.\r\n");
0326:                verifyLastMail(sender1, recipient1, null);
0327:
0328:                smtpProtocol2
0329:                        .sendShortMessageData("Subject: test\r\n\r\nTest body\r\n.\r\n");
0330:                verifyLastMail(sender2, recipient2, null);
0331:
0332:                smtpProtocol1.quit();
0333:                smtpProtocol2.quit();
0334:
0335:                smtpProtocol1.disconnect();
0336:                smtpProtocol2.disconnect();
0337:            }
0338:
0339:            public void testTwoMailsInSequence() throws Exception {
0340:                finishSetUp(m_testConfiguration);
0341:
0342:                SMTPClient smtpProtocol1 = new SMTPClient();
0343:                smtpProtocol1.connect("127.0.0.1", m_smtpListenerPort);
0344:
0345:                assertTrue("first connection taken", smtpProtocol1
0346:                        .isConnected());
0347:
0348:                // no message there, yet
0349:                assertNull("no mail received by mail server", m_mailServer
0350:                        .getLastMail());
0351:
0352:                smtpProtocol1.helo(InetAddress.getLocalHost().toString());
0353:
0354:                String sender1 = "mail_sender1@localhost";
0355:                String recipient1 = "mail_recipient1@localhost";
0356:                smtpProtocol1.setSender(sender1);
0357:                smtpProtocol1.addRecipient(recipient1);
0358:
0359:                smtpProtocol1
0360:                        .sendShortMessageData("Subject: test\r\n\r\nTest body\r\n");
0361:                verifyLastMail(sender1, recipient1, null);
0362:
0363:                String sender2 = "mail_sender2@localhost";
0364:                String recipient2 = "mail_recipient2@localhost";
0365:                smtpProtocol1.setSender(sender2);
0366:                smtpProtocol1.addRecipient(recipient2);
0367:
0368:                smtpProtocol1
0369:                        .sendShortMessageData("Subject: test2\r\n\r\nTest body2\r\n");
0370:                verifyLastMail(sender2, recipient2, null);
0371:
0372:                smtpProtocol1.quit();
0373:                smtpProtocol1.disconnect();
0374:            }
0375:
0376:            public void testHeloResolv() throws Exception {
0377:                m_testConfiguration.setHeloResolv();
0378:                m_testConfiguration.setAuthorizedAddresses("192.168.0.1");
0379:                finishSetUp(m_testConfiguration);
0380:
0381:                SMTPClient smtpProtocol1 = new SMTPClient();
0382:                smtpProtocol1.connect("127.0.0.1", m_smtpListenerPort);
0383:
0384:                assertTrue("first connection taken", smtpProtocol1
0385:                        .isConnected());
0386:
0387:                // no message there, yet
0388:                assertNull("no mail received by mail server", m_mailServer
0389:                        .getLastMail());
0390:
0391:                String helo1 = "abgsfe3rsf.de";
0392:                String helo2 = "james.apache.org";
0393:
0394:                smtpProtocol1.sendCommand("helo", helo1);
0395:                // this should give a 501 code cause the helo could not resolved
0396:                assertEquals("expected error: helo could not resolved", 501,
0397:                        smtpProtocol1.getReplyCode());
0398:
0399:                smtpProtocol1.sendCommand("helo", helo2);
0400:                // helo is resolvable. so this should give a 250 code
0401:                assertEquals("Helo accepted", 250, smtpProtocol1.getReplyCode());
0402:
0403:                smtpProtocol1.quit();
0404:            }
0405:
0406:            public void testHeloResolvDefault() throws Exception {
0407:                finishSetUp(m_testConfiguration);
0408:
0409:                SMTPClient smtpProtocol1 = new SMTPClient();
0410:                smtpProtocol1.connect("127.0.0.1", m_smtpListenerPort);
0411:
0412:                smtpProtocol1.helo("abgsfe3rsf.de");
0413:                // helo should not be checked. so this should give a 250 code
0414:                assertEquals("Helo accepted", 250, smtpProtocol1.getReplyCode());
0415:
0416:                smtpProtocol1.quit();
0417:            }
0418:
0419:            public void testSenderDomainResolv() throws Exception {
0420:                m_testConfiguration.setSenderDomainResolv();
0421:                m_testConfiguration.setAuthorizedAddresses("192.168.0.1/32");
0422:                finishSetUp(m_testConfiguration);
0423:
0424:                SMTPClient smtpProtocol1 = new SMTPClient();
0425:                smtpProtocol1.connect("127.0.0.1", m_smtpListenerPort);
0426:
0427:                assertTrue("first connection taken", smtpProtocol1
0428:                        .isConnected());
0429:
0430:                // no message there, yet
0431:                assertNull("no mail received by mail server", m_mailServer
0432:                        .getLastMail());
0433:
0434:                smtpProtocol1.helo(InetAddress.getLocalHost().toString());
0435:
0436:                String sender1 = "mail_sender1@xfwrqqfgfe.de";
0437:                String sender2 = "mail_sender2@james.apache.org";
0438:
0439:                smtpProtocol1.setSender(sender1);
0440:                assertEquals("expected 501 error", 501, smtpProtocol1
0441:                        .getReplyCode());
0442:
0443:                smtpProtocol1.setSender(sender2);
0444:
0445:                smtpProtocol1.quit();
0446:
0447:            }
0448:
0449:            public void testSenderDomainResolvDefault() throws Exception {
0450:                finishSetUp(m_testConfiguration);
0451:
0452:                SMTPClient smtpProtocol1 = new SMTPClient();
0453:                smtpProtocol1.connect("127.0.0.1", m_smtpListenerPort);
0454:
0455:                smtpProtocol1.helo(InetAddress.getLocalHost().toString());
0456:
0457:                String sender1 = "mail_sender1@xfwrqqfgfe.de";
0458:
0459:                smtpProtocol1.setSender(sender1);
0460:
0461:                smtpProtocol1.quit();
0462:            }
0463:
0464:            public void testSenderDomainResolvRelayClientDefault()
0465:                    throws Exception {
0466:                m_testConfiguration.setSenderDomainResolv();
0467:                finishSetUp(m_testConfiguration);
0468:
0469:                SMTPClient smtpProtocol1 = new SMTPClient();
0470:                smtpProtocol1.connect("127.0.0.1", m_smtpListenerPort);
0471:
0472:                assertTrue("first connection taken", smtpProtocol1
0473:                        .isConnected());
0474:
0475:                // no message there, yet
0476:                assertNull("no mail received by mail server", m_mailServer
0477:                        .getLastMail());
0478:
0479:                smtpProtocol1.helo(InetAddress.getLocalHost().toString());
0480:
0481:                String sender1 = "mail_sender1@xfwrqqfgfe.de";
0482:
0483:                // Both mail shold
0484:                smtpProtocol1.setSender(sender1);
0485:
0486:                smtpProtocol1.quit();
0487:
0488:            }
0489:
0490:            public void testSenderDomainResolvRelayClient() throws Exception {
0491:                m_testConfiguration.setSenderDomainResolv();
0492:                m_testConfiguration.setCheckAuthClients(true);
0493:                finishSetUp(m_testConfiguration);
0494:
0495:                SMTPClient smtpProtocol1 = new SMTPClient();
0496:                smtpProtocol1.connect("127.0.0.1", m_smtpListenerPort);
0497:
0498:                assertTrue("first connection taken", smtpProtocol1
0499:                        .isConnected());
0500:
0501:                // no message there, yet
0502:                assertNull("no mail received by mail server", m_mailServer
0503:                        .getLastMail());
0504:
0505:                smtpProtocol1.helo(InetAddress.getLocalHost().toString());
0506:
0507:                String sender1 = "mail_sender1@xfwrqqfgfe.de";
0508:                String sender2 = "mail_sender2@james.apache.org";
0509:
0510:                smtpProtocol1.setSender(sender1);
0511:                assertEquals("expected 501 error", 501, smtpProtocol1
0512:                        .getReplyCode());
0513:
0514:                smtpProtocol1.setSender(sender2);
0515:
0516:                smtpProtocol1.quit();
0517:
0518:            }
0519:
0520:            public void testMaxRcpt() throws Exception {
0521:                m_testConfiguration.setMaxRcpt(1);
0522:                finishSetUp(m_testConfiguration);
0523:
0524:                SMTPClient smtpProtocol1 = new SMTPClient();
0525:                smtpProtocol1.connect("127.0.0.1", m_smtpListenerPort);
0526:
0527:                assertTrue("first connection taken", smtpProtocol1
0528:                        .isConnected());
0529:
0530:                // no message there, yet
0531:                assertNull("no mail received by mail server", m_mailServer
0532:                        .getLastMail());
0533:
0534:                smtpProtocol1.helo(InetAddress.getLocalHost().toString());
0535:
0536:                String sender1 = "mail_sender1@james.apache.org";
0537:                String rcpt1 = "test@localhost";
0538:                String rcpt2 = "test2@localhost";
0539:
0540:                smtpProtocol1.setSender(sender1);
0541:                smtpProtocol1.addRecipient(rcpt1);
0542:
0543:                smtpProtocol1.addRecipient(rcpt2);
0544:                assertEquals("expected 452 error", 452, smtpProtocol1
0545:                        .getReplyCode());
0546:
0547:                smtpProtocol1
0548:                        .sendShortMessageData("Subject: test\r\n\r\nTest body\r\n");
0549:
0550:                // After the data is send the rcpt count is set back to 0.. So a new mail with rcpt should be accepted
0551:
0552:                smtpProtocol1.setSender(sender1);
0553:
0554:                smtpProtocol1.addRecipient(rcpt1);
0555:
0556:                smtpProtocol1
0557:                        .sendShortMessageData("Subject: test\r\n\r\nTest body\r\n");
0558:
0559:                smtpProtocol1.quit();
0560:
0561:            }
0562:
0563:            public void testMaxRcptDefault() throws Exception {
0564:                finishSetUp(m_testConfiguration);
0565:
0566:                SMTPClient smtpProtocol1 = new SMTPClient();
0567:                smtpProtocol1.connect("127.0.0.1", m_smtpListenerPort);
0568:
0569:                smtpProtocol1.helo(InetAddress.getLocalHost().toString());
0570:
0571:                String sender1 = "mail_sender1@james.apache.org";
0572:                String rcpt1 = "test@localhost";
0573:
0574:                smtpProtocol1.setSender(sender1);
0575:
0576:                smtpProtocol1.addRecipient(rcpt1);
0577:
0578:                smtpProtocol1
0579:                        .sendShortMessageData("Subject: test\r\n\r\nTest body\r\n");
0580:
0581:                smtpProtocol1.quit();
0582:            }
0583:
0584:            public void testEhloResolv() throws Exception {
0585:                m_testConfiguration.setEhloResolv();
0586:                m_testConfiguration.setAuthorizedAddresses("192.168.0.1");
0587:                finishSetUp(m_testConfiguration);
0588:
0589:                SMTPClient smtpProtocol1 = new SMTPClient();
0590:                smtpProtocol1.connect("127.0.0.1", m_smtpListenerPort);
0591:
0592:                assertTrue("first connection taken", smtpProtocol1
0593:                        .isConnected());
0594:
0595:                // no message there, yet
0596:                assertNull("no mail received by mail server", m_mailServer
0597:                        .getLastMail());
0598:
0599:                String ehlo1 = "abgsfe3rsf.de";
0600:                String ehlo2 = "james.apache.org";
0601:
0602:                smtpProtocol1.sendCommand("ehlo", ehlo1);
0603:                // this should give a 501 code cause the ehlo could not resolved
0604:                assertEquals("expected error: ehlo could not resolved", 501,
0605:                        smtpProtocol1.getReplyCode());
0606:
0607:                smtpProtocol1.sendCommand("ehlo", ehlo2);
0608:                // ehlo is resolvable. so this should give a 250 code
0609:                assertEquals("ehlo accepted", 250, smtpProtocol1.getReplyCode());
0610:
0611:                smtpProtocol1.quit();
0612:            }
0613:
0614:            public void testEhloResolvDefault() throws Exception {
0615:                finishSetUp(m_testConfiguration);
0616:
0617:                SMTPClient smtpProtocol1 = new SMTPClient();
0618:                smtpProtocol1.connect("127.0.0.1", m_smtpListenerPort);
0619:
0620:                smtpProtocol1.sendCommand("ehlo", "abgsfe3rsf.de");
0621:                // ehlo should not be checked. so this should give a 250 code
0622:                assertEquals("ehlo accepted", 250, smtpProtocol1.getReplyCode());
0623:
0624:                smtpProtocol1.quit();
0625:            }
0626:
0627:            public void testEhloResolvIgnoreClientDisabled() throws Exception {
0628:                m_testConfiguration.setEhloResolv();
0629:                m_testConfiguration.setCheckAuthNetworks(true);
0630:                finishSetUp(m_testConfiguration);
0631:
0632:                SMTPClient smtpProtocol1 = new SMTPClient();
0633:                smtpProtocol1.connect("127.0.0.1", m_smtpListenerPort);
0634:
0635:                assertTrue("first connection taken", smtpProtocol1
0636:                        .isConnected());
0637:
0638:                // no message there, yet
0639:                assertNull("no mail received by mail server", m_mailServer
0640:                        .getLastMail());
0641:
0642:                String ehlo1 = "abgsfe3rsf.de";
0643:                String ehlo2 = "james.apache.org";
0644:
0645:                smtpProtocol1.sendCommand("ehlo", ehlo1);
0646:                // this should give a 501 code cause the ehlo could not resolved
0647:                assertEquals("expected error: ehlo could not resolved", 501,
0648:                        smtpProtocol1.getReplyCode());
0649:
0650:                smtpProtocol1.sendCommand("ehlo", ehlo2);
0651:                // ehlo is resolvable. so this should give a 250 code
0652:                assertEquals("ehlo accepted", 250, smtpProtocol1.getReplyCode());
0653:
0654:                smtpProtocol1.quit();
0655:            }
0656:
0657:            public void testHeloEnforcement() throws Exception {
0658:                finishSetUp(m_testConfiguration);
0659:
0660:                SMTPClient smtpProtocol1 = new SMTPClient();
0661:                smtpProtocol1.connect("127.0.0.1", m_smtpListenerPort);
0662:
0663:                assertTrue("first connection taken", smtpProtocol1
0664:                        .isConnected());
0665:
0666:                // no message there, yet
0667:                assertNull("no mail received by mail server", m_mailServer
0668:                        .getLastMail());
0669:
0670:                String sender1 = "mail_sender1@localhost";
0671:                smtpProtocol1.setSender(sender1);
0672:                assertEquals("expected 503 error", 503, smtpProtocol1
0673:                        .getReplyCode());
0674:
0675:                smtpProtocol1.helo(InetAddress.getLocalHost().toString());
0676:
0677:                smtpProtocol1.setSender(sender1);
0678:
0679:                smtpProtocol1.quit();
0680:            }
0681:
0682:            public void testHeloEnforcementDisabled() throws Exception {
0683:                m_testConfiguration.setHeloEhloEnforcement(false);
0684:                finishSetUp(m_testConfiguration);
0685:
0686:                SMTPClient smtpProtocol1 = new SMTPClient();
0687:                smtpProtocol1.connect("127.0.0.1", m_smtpListenerPort);
0688:
0689:                assertTrue("first connection taken", smtpProtocol1
0690:                        .isConnected());
0691:
0692:                // no message there, yet
0693:                assertNull("no mail received by mail server", m_mailServer
0694:                        .getLastMail());
0695:
0696:                String sender1 = "mail_sender1@localhost";
0697:
0698:                smtpProtocol1.setSender(sender1);
0699:
0700:                smtpProtocol1.quit();
0701:            }
0702:
0703:            public void testAuth() throws Exception {
0704:                m_testConfiguration.setAuthorizedAddresses("128.0.0.1/8");
0705:                m_testConfiguration.setAuthorizingAnnounce();
0706:                finishSetUp(m_testConfiguration);
0707:
0708:                SMTPClient smtpProtocol = new SMTPClient();
0709:                smtpProtocol.connect("127.0.0.1", m_smtpListenerPort);
0710:
0711:                smtpProtocol.sendCommand("ehlo", InetAddress.getLocalHost()
0712:                        .toString());
0713:                String[] capabilityRes = smtpProtocol.getReplyStrings();
0714:
0715:                List capabilitieslist = new ArrayList();
0716:                for (int i = 1; i < capabilityRes.length; i++) {
0717:                    capabilitieslist.add(capabilityRes[i].substring(4));
0718:                }
0719:
0720:                assertTrue("anouncing auth required", capabilitieslist
0721:                        .contains("AUTH LOGIN PLAIN"));
0722:                // is this required or just for compatibility? assertTrue("anouncing auth required", capabilitieslist.contains("AUTH=LOGIN PLAIN"));
0723:
0724:                String userName = "test_user_smtp";
0725:                String noexistUserName = "noexist_test_user_smtp";
0726:                String sender = "test_user_smtp@localhost";
0727:                smtpProtocol.sendCommand("AUTH FOO", null);
0728:                assertEquals(
0729:                        "expected error: unrecognized authentication type",
0730:                        504, smtpProtocol.getReplyCode());
0731:
0732:                smtpProtocol.setSender(sender);
0733:
0734:                smtpProtocol.addRecipient("mail@sample.com");
0735:                assertEquals("expected 530 error", 530, smtpProtocol
0736:                        .getReplyCode());
0737:
0738:                assertFalse("user not existing", m_usersRepository
0739:                        .contains(noexistUserName));
0740:
0741:                smtpProtocol.sendCommand("AUTH PLAIN");
0742:                smtpProtocol.sendCommand(Base64.encodeAsString("\0"
0743:                        + noexistUserName + "\0pwd\0"));
0744:                //        smtpProtocol.sendCommand(noexistUserName+"pwd".toCharArray());
0745:                assertEquals("expected error", 535, smtpProtocol.getReplyCode());
0746:
0747:                m_usersRepository.addUser(userName, "pwd");
0748:
0749:                smtpProtocol.sendCommand("AUTH PLAIN");
0750:                smtpProtocol.sendCommand(Base64.encodeAsString("\0" + userName
0751:                        + "\0wrongpwd\0"));
0752:                assertEquals("expected error", 535, smtpProtocol.getReplyCode());
0753:
0754:                smtpProtocol.sendCommand("AUTH PLAIN");
0755:                smtpProtocol.sendCommand(Base64.encodeAsString("\0" + userName
0756:                        + "\0pwd\0"));
0757:                assertEquals("authenticated", 235, smtpProtocol.getReplyCode());
0758:
0759:                smtpProtocol.sendCommand("AUTH PLAIN");
0760:                assertEquals(
0761:                        "expected error: User has previously authenticated.",
0762:                        503, smtpProtocol.getReplyCode());
0763:
0764:                smtpProtocol.addRecipient("mail@sample.com");
0765:                smtpProtocol
0766:                        .sendShortMessageData("Subject: test\r\n\r\nTest body\r\n");
0767:
0768:                smtpProtocol.quit();
0769:
0770:                // mail was propagated by SMTPServer
0771:                assertNotNull("mail received by mail server", m_mailServer
0772:                        .getLastMail());
0773:            }
0774:
0775:            public void testAuthWithEmptySender() throws Exception {
0776:                m_testConfiguration.setAuthorizedAddresses("128.0.0.1/8");
0777:                m_testConfiguration.setAuthorizingAnnounce();
0778:                finishSetUp(m_testConfiguration);
0779:
0780:                SMTPClient smtpProtocol = new SMTPClient();
0781:                smtpProtocol.connect("127.0.0.1", m_smtpListenerPort);
0782:
0783:                smtpProtocol.sendCommand("ehlo " + InetAddress.getLocalHost());
0784:
0785:                String userName = "test_user_smtp";
0786:                m_usersRepository.addUser(userName, "pwd");
0787:
0788:                smtpProtocol.setSender("");
0789:
0790:                smtpProtocol.sendCommand("AUTH PLAIN");
0791:                smtpProtocol.sendCommand(Base64.encodeAsString("\0" + userName
0792:                        + "\0pwd\0"));
0793:                assertEquals("authenticated", 235, smtpProtocol.getReplyCode());
0794:
0795:                smtpProtocol.addRecipient("mail@sample.com");
0796:                assertEquals("expected error", 503, smtpProtocol.getReplyCode());
0797:
0798:                smtpProtocol.quit();
0799:            }
0800:
0801:            public void testNoRecepientSpecified() throws Exception {
0802:                finishSetUp(m_testConfiguration);
0803:
0804:                SMTPClient smtpProtocol = new SMTPClient();
0805:                smtpProtocol.connect("127.0.0.1", m_smtpListenerPort);
0806:
0807:                smtpProtocol.sendCommand("ehlo " + InetAddress.getLocalHost());
0808:
0809:                smtpProtocol.setSender("mail@sample.com");
0810:
0811:                // left out for test smtpProtocol.rcpt(new Address("mail@localhost"));
0812:
0813:                smtpProtocol
0814:                        .sendShortMessageData("Subject: test\r\n\r\nTest body\r\n");
0815:                assertTrue("sending succeeded without recepient", SMTPReply
0816:                        .isNegativePermanent(smtpProtocol.getReplyCode()));
0817:
0818:                smtpProtocol.quit();
0819:
0820:                // mail was propagated by SMTPServer
0821:                assertNull("no mail received by mail server", m_mailServer
0822:                        .getLastMail());
0823:            }
0824:
0825:            public void testMultipleMailsAndRset() throws Exception {
0826:                finishSetUp(m_testConfiguration);
0827:
0828:                SMTPClient smtpProtocol = new SMTPClient();
0829:                smtpProtocol.connect("127.0.0.1", m_smtpListenerPort);
0830:
0831:                smtpProtocol.sendCommand("ehlo " + InetAddress.getLocalHost());
0832:
0833:                smtpProtocol.setSender("mail@sample.com");
0834:
0835:                smtpProtocol.reset();
0836:
0837:                smtpProtocol.setSender("mail@sample.com");
0838:
0839:                smtpProtocol.quit();
0840:
0841:                // mail was propagated by SMTPServer
0842:                assertNull("no mail received by mail server", m_mailServer
0843:                        .getLastMail());
0844:            }
0845:
0846:            public void testRelayingDenied() throws Exception {
0847:                m_testConfiguration.setAuthorizedAddresses("128.0.0.1/8");
0848:                finishSetUp(m_testConfiguration);
0849:
0850:                SMTPClient smtpProtocol = new SMTPClient();
0851:                smtpProtocol.connect("127.0.0.1", m_smtpListenerPort);
0852:
0853:                smtpProtocol.sendCommand("ehlo " + InetAddress.getLocalHost());
0854:
0855:                smtpProtocol.setSender("mail@sample.com");
0856:
0857:                smtpProtocol.addRecipient("maila@sample.com");
0858:                assertEquals("expected 550 error", 550, smtpProtocol
0859:                        .getReplyCode());
0860:            }
0861:
0862:            public void testHandleAnnouncedMessageSizeLimitExceeded()
0863:                    throws Exception {
0864:                m_testConfiguration.setMaxMessageSize(1); // set message limit to 1kb
0865:                finishSetUp(m_testConfiguration);
0866:
0867:                SMTPClient smtpProtocol = new SMTPClient();
0868:                smtpProtocol.connect("127.0.0.1", m_smtpListenerPort);
0869:
0870:                smtpProtocol.sendCommand("ehlo " + InetAddress.getLocalHost());
0871:
0872:                smtpProtocol.sendCommand(
0873:                        "MAIL FROM:<mail@localhost> SIZE=1025", null);
0874:                assertEquals("expected error: max msg size exceeded", 552,
0875:                        smtpProtocol.getReplyCode());
0876:
0877:                smtpProtocol.addRecipient("mail@localhost");
0878:                assertEquals("expected error", 503, smtpProtocol.getReplyCode());
0879:            }
0880:
0881:            public void testHandleMessageSizeLimitExceeded() throws Exception {
0882:                m_testConfiguration.setMaxMessageSize(1); // set message limit to 1kb 
0883:                finishSetUp(m_testConfiguration);
0884:
0885:                SMTPClient smtpProtocol = new SMTPClient();
0886:                smtpProtocol.connect("127.0.0.1", m_smtpListenerPort);
0887:
0888:                smtpProtocol.sendCommand("ehlo " + InetAddress.getLocalHost());
0889:
0890:                smtpProtocol.setSender("mail@localhost");
0891:                smtpProtocol.addRecipient("mail@localhost");
0892:
0893:                Writer wr = smtpProtocol.sendMessageData();
0894:                // create Body with more than 1kb . 502
0895:                wr
0896:                        .write("1234567810123456782012345678301234567840123456785012345678601234567870123456788012345678901234567100");
0897:                wr
0898:                        .write("1234567810123456782012345678301234567840123456785012345678601234567870123456788012345678901234567100");
0899:                wr
0900:                        .write("1234567810123456782012345678301234567840123456785012345678601234567870123456788012345678901234567100");
0901:                wr
0902:                        .write("1234567810123456782012345678301234567840123456785012345678601234567870123456788012345678901234567100");
0903:                wr
0904:                        .write("1234567810123456782012345678301234567840123456785012345678601234567870123456788012345678901234567100\r\n");
0905:                // second line
0906:                wr
0907:                        .write("1234567810123456782012345678301234567840123456785012345678601234567870123456788012345678901234567100");
0908:                wr
0909:                        .write("1234567810123456782012345678301234567840123456785012345678601234567870123456788012345678901234567100");
0910:                wr
0911:                        .write("1234567810123456782012345678301234567840123456785012345678601234567870123456788012345678901234567100");
0912:                wr
0913:                        .write("1234567810123456782012345678301234567840123456785012345678601234567870123456788012345678901234567100");
0914:                wr
0915:                        .write("1234567810123456782012345678301234567840123456785012345678601234567870123456788012345678901234567100");
0916:                wr.write("123456781012345678201\r\n"); // 521 + CRLF = 523 + 502 => 1025
0917:                wr.close();
0918:
0919:                assertFalse(smtpProtocol.completePendingCommand());
0920:
0921:                assertEquals("expected 552 error", 552, smtpProtocol
0922:                        .getReplyCode());
0923:
0924:            }
0925:
0926:            public void testHandleMessageSizeLimitRespected() throws Exception {
0927:                m_testConfiguration.setMaxMessageSize(1); // set message limit to 1kb 
0928:                finishSetUp(m_testConfiguration);
0929:
0930:                SMTPClient smtpProtocol = new SMTPClient();
0931:                smtpProtocol.connect("127.0.0.1", m_smtpListenerPort);
0932:
0933:                smtpProtocol.sendCommand("ehlo " + InetAddress.getLocalHost());
0934:
0935:                smtpProtocol.setSender("mail@localhost");
0936:                smtpProtocol.addRecipient("mail@localhost");
0937:
0938:                Writer wr = smtpProtocol.sendMessageData();
0939:                // create Body with less than 1kb
0940:                wr
0941:                        .write("1234567810123456782012345678301234567840123456785012345678601234567870123456788012345678901234567100");
0942:                wr
0943:                        .write("1234567810123456782012345678301234567840123456785012345678601234567870123456788012345678901234567100");
0944:                wr
0945:                        .write("1234567810123456782012345678301234567840123456785012345678601234567870123456788012345678901234567100");
0946:                wr
0947:                        .write("1234567810123456782012345678301234567840123456785012345678601234567870123456788012345678901234567100");
0948:                wr
0949:                        .write("1234567810123456782012345678301234567840123456785012345678601234567870123456788012345678901234567100");
0950:                wr
0951:                        .write("1234567810123456782012345678301234567840123456785012345678601234567870123456788012345678901234567100");
0952:                wr
0953:                        .write("1234567810123456782012345678301234567840123456785012345678601234567870123456788012345678901234567100");
0954:                wr
0955:                        .write("1234567810123456782012345678301234567840123456785012345678601234567870123456788012345678901234567100");
0956:                wr
0957:                        .write("1234567810123456782012345678301234567840123456785012345678601234567870123456788012345678901234567100");
0958:                wr
0959:                        .write("1234567810123456782012345678301234567840123456785012345678601234567870123456788012345678901234567100");
0960:                wr.write("1234567810123456782012\r\n"); // 1022 + CRLF = 1024
0961:                wr.close();
0962:
0963:                assertTrue(smtpProtocol.completePendingCommand());
0964:
0965:                assertEquals("expected 250 ok", 250, smtpProtocol
0966:                        .getReplyCode());
0967:
0968:            }
0969:
0970:            /*
0971:              What we want to see is that for a given connection limit and a
0972:              given backlog, that connection limit requests are handled, and
0973:              that up to the backlog number of connections are queued.  More
0974:              connections than that would eventually error out (unless space
0975:              connections than that should error out until space opens up in
0976:              the queue.
0977:
0978:              For example:
0979:
0980:                # telnet localhost <m_smtpListenPort>
0981:                Trying 127.0.0.1...
0982:                telnet: Unable to connect to remote host: Connection refused
0983:
0984:              is the immediate response if the backlog is full.
0985:             */
0986:
0987:            public void testConnectionLimitExceeded() throws Exception {
0988:                final int acceptLimit = 1;
0989:                final int backlog = 1;
0990:
0991:                m_testConfiguration.setConnectionLimit(acceptLimit); // allow no more than <acceptLimit> connection(s) in the service
0992:                m_testConfiguration.setConnectionBacklog(backlog); // allow <backlog> additional connection(s) in the queue
0993:                finishSetUp(m_testConfiguration);
0994:
0995:                final SMTPClient[] client = new SMTPClient[acceptLimit];
0996:                for (int i = 0; i < client.length; i++) {
0997:                    client[i] = new SMTPClient(); // should connect to worker
0998:                    try {
0999:                        client[i].connect("127.0.0.1", m_smtpListenerPort);
1000:                    } catch (Exception _) {
1001:                    }
1002:                    assertTrue("client #" + (i + 1) + " established", client[i]
1003:                            .isConnected());
1004:                }
1005:
1006:                // Cannot use SMTPClient.  It appears that even though the
1007:                // client's socket is established, since the client won't be
1008:                // able to connect to the protocol handler, the connect call
1009:                // hangs.
1010:                // Different TCP/IP stacks may provide a "grace" margin above
1011:                // and beyond the specified backlog.  So we won't try to be
1012:                // precise.  Instead we will compute some upper limit, loop
1013:                // until we get a connection error (or hit the limit), and
1014:                // then test for the expected behavior.
1015:                //
1016:                // See: http://www.phrack.org/archives/48/P48-13
1017:                final Socket connection[] = new Socket[Math.max(
1018:                        ((backlog * 3) / 2) + 1, backlog + 3)];
1019:
1020:                final java.net.SocketAddress server = new java.net.InetSocketAddress(
1021:                        "localhost", m_smtpListenerPort);
1022:
1023:                for (int i = 0; i < connection.length; i++) {
1024:                    connection[i] = new Socket();
1025:                    try {
1026:                        connection[i].connect(server, 1000);
1027:                    } catch (Exception _) {
1028:                        assertTrue("Accepted connections " + i
1029:                                + " did not meet or exceed backlog of "
1030:                                + backlog + ".", i >= backlog);
1031:                        connection[i] = null; // didn't open, so don't try to close later
1032:                        break; // OK to fail, since we've at least reached the backlog          
1033:                    }
1034:                    assertTrue("connection #" + (i + 1) + " established",
1035:                            connection[i].isConnected());
1036:                }
1037:
1038:                try {
1039:                    final Socket shouldFail = new Socket();
1040:                    shouldFail.connect(server, 1000);
1041:                    fail("connection # "
1042:                            + (client.length + connection.length + 1)
1043:                            + " did not fail.");
1044:                } catch (Exception _) {
1045:                }
1046:
1047:                client[0].quit();
1048:                client[0].disconnect();
1049:
1050:                Thread.sleep(100);
1051:
1052:                // now should be able to connect (backlog)
1053:                try {
1054:                    final Socket shouldWork = new Socket();
1055:                    shouldWork.connect(server, 1000);
1056:                    assertTrue(
1057:                            "Additional connection established after close.",
1058:                            shouldWork.isConnected());
1059:                    shouldWork.close();
1060:                } catch (Exception e) {
1061:                    fail("Could not establish additional connection after close."
1062:                            + e.getMessage());
1063:                }
1064:
1065:                // close the pending connections first, so that the server doesn't see them
1066:                for (int i = 0; i < connection.length; i++)
1067:                    if (connection[i] != null)
1068:                        connection[i].close();
1069:
1070:                // close the remaining clients
1071:                for (int i = 1; i < client.length; i++) {
1072:                    client[i].quit();
1073:                    client[i].disconnect();
1074:                }
1075:            }
1076:
1077:            // RemoteDelivery tests.
1078:
1079:            InMemorySpoolRepository outgoingSpool;
1080:            private MockServiceManager m_serviceManager;
1081:
1082:            private Properties getStandardParameters() {
1083:                Properties parameters = new Properties();
1084:                parameters.put("delayTime", "500 msec, 500 msec, 500 msec"); // msec, sec, minute, hour
1085:                parameters.put("maxRetries", "3");
1086:                parameters.put("deliveryThreads", "1");
1087:                parameters.put("debug", "true");
1088:                parameters.put("sendpartial", "false");
1089:                parameters.put("bounceProcessor", "bounce");
1090:                parameters.put("outgoing", "mocked://outgoing/");
1091:                return parameters;
1092:            }
1093:
1094:            /**
1095:             * This has been created to test javamail 1.4 introduced bug.
1096:             * http://issues.apache.org/jira/browse/JAMES-490
1097:             */
1098:            public void testDeliveryToSelfWithGatewayAndBind() throws Exception {
1099:                finishSetUp(m_testConfiguration);
1100:                outgoingSpool = new InMemorySpoolRepository();
1101:                ((MockStore) m_serviceManager.lookup(Store.ROLE)).add(
1102:                        "outgoing", outgoingSpool);
1103:
1104:                RemoteDelivery rd = new RemoteDelivery();
1105:
1106:                MockMailContext mmc = new MockMailContext();
1107:                mmc.setAttribute(Constants.AVALON_COMPONENT_MANAGER,
1108:                        m_serviceManager);
1109:                mmc.setAttribute(Constants.HELLO_NAME, "localhost");
1110:                MockMailetConfig mci = new MockMailetConfig("Test", mmc,
1111:                        getStandardParameters());
1112:                mci.setProperty("bind", "127.0.0.1");
1113:                mci.setProperty("gateway", "127.0.0.1");
1114:                mci.setProperty("gatewayPort", "" + m_smtpListenerPort);
1115:                rd.init(mci);
1116:                String sources = "Content-Type: text/plain;\r\nSubject: test\r\n\r\nBody";
1117:                String sender = "test@localhost";
1118:                String recipient = "test@localhost";
1119:                MimeMessage mm = new MimeMessage(Session
1120:                        .getDefaultInstance(new Properties()),
1121:                        new ByteArrayInputStream(sources.getBytes()));
1122:                MailImpl mail = new MailImpl("name", new MailAddress(sender),
1123:                        Arrays.asList(new MailAddress[] { new MailAddress(
1124:                                recipient) }), mm);
1125:
1126:                rd.service(mail);
1127:
1128:                while (outgoingSpool.size() > 0) {
1129:                    Thread.sleep(1000);
1130:                }
1131:
1132:                verifyLastMail(sender, recipient, null);
1133:
1134:                assertEquals(((String) mm.getContent()).trim(),
1135:                        ((String) ((MimeMessage) m_mailServer.getLastMail()[2])
1136:                                .getContent()).trim());
1137:
1138:                mail.dispose();
1139:            }
1140:
1141:            /**
1142:             * This is useful code to run tests on javamail bugs 
1143:             * http://issues.apache.org/jira/browse/JAMES-52
1144:             * 
1145:             * This 
1146:             * @throws Exception
1147:             */
1148:            public void test8bitmimeFromStream() throws Exception {
1149:                finishSetUp(m_testConfiguration);
1150:                outgoingSpool = new InMemorySpoolRepository();
1151:                ((MockStore) m_serviceManager.lookup(Store.ROLE)).add(
1152:                        "outgoing", outgoingSpool);
1153:
1154:                RemoteDelivery rd = new RemoteDelivery();
1155:
1156:                MockMailContext mmc = new MockMailContext();
1157:                mmc.setAttribute(Constants.AVALON_COMPONENT_MANAGER,
1158:                        m_serviceManager);
1159:                mmc.setAttribute(Constants.HELLO_NAME, "localhost");
1160:                MockMailetConfig mci = new MockMailetConfig("Test", mmc,
1161:                        getStandardParameters());
1162:                mci.setProperty("gateway", "127.0.0.1");
1163:                mci.setProperty("gatewayPort", "" + m_smtpListenerPort);
1164:                rd.init(mci);
1165:
1166:                String sources = "Content-Type: text/plain;\r\nContent-Transfer-Encoding: quoted-printable\r\nSubject: test\r\n\r\nBody=80\r\n";
1167:                //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";
1168:                //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";
1169:                String sender = "test@localhost";
1170:                String recipient = "test@localhost";
1171:                MimeMessage mm = new MimeMessage(Session
1172:                        .getDefaultInstance(new Properties()),
1173:                        new ByteArrayInputStream(sources.getBytes()));
1174:                MailImpl mail = new MailImpl("name", new MailAddress(sender),
1175:                        Arrays.asList(new MailAddress[] { new MailAddress(
1176:                                recipient) }), mm);
1177:
1178:                rd.service(mail);
1179:
1180:                while (outgoingSpool.size() > 0) {
1181:                    Thread.sleep(1000);
1182:                }
1183:
1184:                // verifyLastMail(sender, recipient, mm);
1185:                verifyLastMail(sender, recipient, null);
1186:
1187:                // THIS WOULD FAIL BECAUSE OF THE JAVAMAIL BUG
1188:                // assertEquals(mm.getContent(),((MimeMessage) m_mailServer.getLastMail()[2]).getContent());
1189:
1190:                mail.dispose();
1191:            }
1192:
1193:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.