📄 smtpservertest.java
字号:
/**************************************************************** * Licensed to the Apache Software Foundation (ASF) under one * * or more contributor license agreements. See the NOTICE file * * distributed with this work for additional information * * regarding copyright ownership. The ASF licenses this file * * to you under the Apache License, Version 2.0 (the * * "License"); you may not use this file except in compliance * * with the License. You may obtain a copy of the License at * * * * http://www.apache.org/licenses/LICENSE-2.0 * * * * Unless required by applicable law or agreed to in writing, * * software distributed under the License is distributed on an * * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * * KIND, either express or implied. See the License for the * * specific language governing permissions and limitations * * under the License. * ****************************************************************/package org.apache.james.smtpserver;import org.apache.avalon.cornerstone.services.sockets.SocketManager;import org.apache.avalon.cornerstone.services.store.Store;import org.apache.avalon.cornerstone.services.threads.ThreadManager;import org.apache.avalon.framework.container.ContainerUtil;import org.apache.commons.net.smtp.SMTPClient;import org.apache.commons.net.smtp.SMTPReply;import org.apache.james.Constants;import org.apache.james.core.MailImpl;import org.apache.james.services.DNSServer;import org.apache.james.services.JamesConnectionManager;import org.apache.james.services.MailServer;import org.apache.james.services.UsersRepository;import org.apache.james.test.mock.avalon.MockLogger;import org.apache.james.test.mock.avalon.MockServiceManager;import org.apache.james.test.mock.avalon.MockSocketManager;import org.apache.james.test.mock.avalon.MockStore;import org.apache.james.test.mock.avalon.MockThreadManager;import org.apache.james.test.mock.james.InMemorySpoolRepository;import org.apache.james.test.mock.james.MockMailServer;import org.apache.james.test.mock.mailet.MockMailContext;import org.apache.james.test.mock.mailet.MockMailetConfig;import org.apache.james.test.util.Util;import org.apache.james.transport.mailets.RemoteDelivery;import org.apache.james.userrepository.MockUsersRepository;import org.apache.james.util.Base64;import org.apache.james.util.connection.SimpleConnectionManager;import org.apache.mailet.MailAddress;import javax.mail.MessagingException;import javax.mail.Session;import javax.mail.internet.MimeMessage;import java.io.ByteArrayInputStream;import java.io.ByteArrayOutputStream;import java.io.IOException;import java.io.Writer;import java.net.InetAddress;import java.net.Socket;import java.net.UnknownHostException;import java.util.ArrayList;import java.util.Arrays;import java.util.Collection;import java.util.Iterator;import java.util.List;import java.util.Properties;import junit.framework.TestCase;/** * Tests the org.apache.james.smtpserver.SMTPServer unit */public class SMTPServerTest extends TestCase { private int m_smtpListenerPort = Util.getNonPrivilegedPort(); private MockMailServer m_mailServer; private SMTPTestConfiguration m_testConfiguration; private SMTPServer m_smtpServer; private MockUsersRepository m_usersRepository = new MockUsersRepository(); public SMTPServerTest() { super("SMTPServerTest"); } public void verifyLastMail(String sender, String recipient, MimeMessage msg) throws IOException, MessagingException { Object[] mailData = m_mailServer.getLastMail(); assertNotNull("mail received by mail server", mailData); if (sender == null && recipient == null && msg == null) fail("no verification can be done with all arguments null"); if (sender != null) assertEquals("sender verfication", sender, ((MailAddress)mailData[0]).toString()); if (recipient != null) assertTrue("recipient verfication", ((Collection) mailData[1]).contains(new MailAddress(recipient))); if (msg != null) { ByteArrayOutputStream bo1 = new ByteArrayOutputStream(); msg.writeTo(bo1); ByteArrayOutputStream bo2 = new ByteArrayOutputStream(); ((MimeMessage) mailData[2]).writeTo(bo2); assertEquals(bo1.toString(),bo2.toString()); assertEquals("message verification", msg, ((MimeMessage) mailData[2])); } } protected void setUp() throws Exception { m_smtpServer = new SMTPServer(); ContainerUtil.enableLogging(m_smtpServer,new MockLogger()); m_serviceManager = setUpServiceManager(); ContainerUtil.service(m_smtpServer, m_serviceManager); m_testConfiguration = new SMTPTestConfiguration(m_smtpListenerPort); } private void finishSetUp(SMTPTestConfiguration testConfiguration) throws Exception { testConfiguration.init(); ContainerUtil.configure(m_smtpServer, testConfiguration); ContainerUtil.initialize(m_smtpServer); m_mailServer.setMaxMessageSizeBytes(m_testConfiguration.getMaxMessageSize()*1024); } private MockServiceManager setUpServiceManager() throws Exception { m_serviceManager = new MockServiceManager(); SimpleConnectionManager connectionManager = new SimpleConnectionManager(); ContainerUtil.enableLogging(connectionManager, new MockLogger()); m_serviceManager.put(JamesConnectionManager.ROLE, connectionManager); m_serviceManager.put("org.apache.mailet.MailetContext", new MockMailContext()); m_mailServer = new MockMailServer(); m_serviceManager.put(MailServer.ROLE, m_mailServer); m_serviceManager.put(UsersRepository.ROLE, m_usersRepository); m_serviceManager.put(SocketManager.ROLE, new MockSocketManager(m_smtpListenerPort)); m_serviceManager.put(ThreadManager.ROLE, new MockThreadManager()); // Mock DNS Server DNSServer dns = new DNSServer() { public Collection findMXRecords(String hostname) { List res = new ArrayList(); if (hostname == null) { return res; }; if ("james.apache.org".equals(hostname)) { res.add("nagoya.apache.org"); } return res; } public Iterator getSMTPHostAddresses(String domainName) { throw new UnsupportedOperationException("Unimplemented mock service"); } public InetAddress[] getAllByName(String host) throws UnknownHostException { return new InetAddress[] {getByName(host)};// throw new UnsupportedOperationException("getByName not implemented in mock for host: "+host); } public InetAddress getByName(String host) throws UnknownHostException { return InetAddress.getByName(host);// throw new UnsupportedOperationException("getByName not implemented in mock for host: "+host); } public Collection findTXTRecords(String hostname) { List res = new ArrayList(); if (hostname == null) { return res; }; if ("2.0.0.127.bl.spamcop.net".equals(hostname)) { res.add("Blocked - see http://www.spamcop.net/bl.shtml?127.0.0.2"); } return res; } }; m_serviceManager.put(DNSServer.ROLE, dns); m_serviceManager.put(Store.ROLE, new MockStore()); return m_serviceManager; } public void testSimpleMailSendWithEHLO() throws Exception { finishSetUp(m_testConfiguration); SMTPClient smtpProtocol = new SMTPClient(); smtpProtocol.connect("127.0.0.1", m_smtpListenerPort); // no message there, yet assertNull("no mail received by mail server", m_mailServer.getLastMail()); smtpProtocol.sendCommand("EHLO "+InetAddress.getLocalHost()); String[] capabilityRes = smtpProtocol.getReplyStrings(); List capabilitieslist = new ArrayList(); for (int i = 1; i < capabilityRes.length; i++) { capabilitieslist.add(capabilityRes[i].substring(4)); } assertEquals("capabilities", 2, capabilitieslist.size()); assertTrue("capabilities present PIPELINING", capabilitieslist.contains("PIPELINING")); assertTrue("capabilities present ENHANCEDSTATUSCODES", capabilitieslist.contains("ENHANCEDSTATUSCODES")); //assertTrue("capabilities present 8BITMIME", capabilitieslist.contains("8BITMIME")); smtpProtocol.setSender("mail@localhost"); smtpProtocol.addRecipient("mail@localhost"); smtpProtocol.sendShortMessageData("Subject: test\r\n\r\nBody\r\n\r\n.\r\n"); smtpProtocol.quit(); smtpProtocol.disconnect(); // mail was propagated by SMTPServer assertNotNull("mail received by mail server", m_mailServer.getLastMail()); } public void testEmptyMessage() throws Exception { finishSetUp(m_testConfiguration); SMTPClient smtp = new SMTPClient(); smtp.connect("127.0.0.1", m_smtpListenerPort); // no message there, yet assertNull("no mail received by mail server", m_mailServer.getLastMail()); smtp.helo(InetAddress.getLocalHost().toString()); smtp.setSender("mail@localhost"); smtp.addRecipient("mail@localhost"); smtp.sendShortMessageData(""); smtp.quit(); smtp.disconnect(); // mail was propagated by SMTPServer assertNotNull("mail received by mail server", m_mailServer.getLastMail()); // added to check a NPE in the test (JAMES-474) due to MockMailServer // not cloning the message (added a MimeMessageCopyOnWriteProxy there) System.gc(); int size = ((MimeMessage) m_mailServer.getLastMail()[2]).getSize(); assertEquals(size, 2); } public void testSimpleMailSendWithHELO() throws Exception { finishSetUp(m_testConfiguration); SMTPClient smtpProtocol = new SMTPClient(); smtpProtocol.connect("127.0.0.1", m_smtpListenerPort); // no message there, yet assertNull("no mail received by mail server", m_mailServer.getLastMail()); smtpProtocol.helo(InetAddress.getLocalHost().toString()); smtpProtocol.setSender("mail@localhost"); smtpProtocol.addRecipient("mail@localhost"); smtpProtocol.sendShortMessageData("Subject: test mail\r\n\r\nTest body\r\n.\r\n"); smtpProtocol.quit(); smtpProtocol.disconnect(); // mail was propagated by SMTPServer assertNotNull("mail received by mail server", m_mailServer.getLastMail()); } public void testTwoSimultaneousMails() throws Exception { finishSetUp(m_testConfiguration); SMTPClient smtpProtocol1 = new SMTPClient(); smtpProtocol1.connect("127.0.0.1", m_smtpListenerPort); SMTPClient smtpProtocol2 = new SMTPClient(); smtpProtocol2.connect("127.0.0.1", m_smtpListenerPort); assertTrue("first connection taken",smtpProtocol1.isConnected()); assertTrue("second connection taken",smtpProtocol2.isConnected()); // no message there, yet assertNull("no mail received by mail server", m_mailServer.getLastMail()); smtpProtocol1.helo(InetAddress.getLocalHost().toString()); smtpProtocol2.helo(InetAddress.getLocalHost().toString()); String sender1 = "mail_sender1@localhost"; String recipient1 = "mail_recipient1@localhost"; smtpProtocol1.setSender(sender1); smtpProtocol1.addRecipient(recipient1); String sender2 = "mail_sender2@localhost"; String recipient2 = "mail_recipient2@localhost"; smtpProtocol2.setSender(sender2); smtpProtocol2.addRecipient(recipient2); smtpProtocol1.sendShortMessageData("Subject: test\r\n\r\nTest body\r\n.\r\n"); verifyLastMail(sender1, recipient1, null); smtpProtocol2.sendShortMessageData("Subject: test\r\n\r\nTest body\r\n.\r\n"); verifyLastMail(sender2, recipient2, null); smtpProtocol1.quit(); smtpProtocol2.quit(); smtpProtocol1.disconnect(); smtpProtocol2.disconnect(); } public void testTwoMailsInSequence() throws Exception { finishSetUp(m_testConfiguration); SMTPClient smtpProtocol1 = new SMTPClient(); smtpProtocol1.connect("127.0.0.1", m_smtpListenerPort); assertTrue("first connection taken", smtpProtocol1.isConnected()); // no message there, yet assertNull("no mail received by mail server", m_mailServer.getLastMail()); smtpProtocol1.helo(InetAddress.getLocalHost().toString()); String sender1 = "mail_sender1@localhost"; String recipient1 = "mail_recipient1@localhost"; smtpProtocol1.setSender(sender1); smtpProtocol1.addRecipient(recipient1); smtpProtocol1.sendShortMessageData("Subject: test\r\n\r\nTest body\r\n"); verifyLastMail(sender1, recipient1, null); String sender2 = "mail_sender2@localhost"; String recipient2 = "mail_recipient2@localhost"; smtpProtocol1.setSender(sender2); smtpProtocol1.addRecipient(recipient2); smtpProtocol1.sendShortMessageData("Subject: test2\r\n\r\nTest body2\r\n"); verifyLastMail(sender2, recipient2, null); smtpProtocol1.quit(); smtpProtocol1.disconnect(); } public void testHeloResolv() throws Exception { m_testConfiguration.setHeloResolv(); m_testConfiguration.setAuthorizedAddresses("192.168.0.1"); finishSetUp(m_testConfiguration); SMTPClient smtpProtocol1 = new SMTPClient(); smtpProtocol1.connect("127.0.0.1", m_smtpListenerPort); assertTrue("first connection taken", smtpProtocol1.isConnected()); // no message there, yet assertNull("no mail received by mail server", m_mailServer.getLastMail()); String helo1 = "abgsfe3rsf.de"; String helo2 = "james.apache.org"; smtpProtocol1.sendCommand("helo",helo1);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -