KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > james > smtpserver > SMTPServerTest


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

19
20 package org.apache.james.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 JavaDoc;
52 import javax.mail.Session JavaDoc;
53 import javax.mail.internet.MimeMessage JavaDoc;
54
55 import java.io.BufferedReader JavaDoc;
56 import java.io.ByteArrayInputStream JavaDoc;
57 import java.io.ByteArrayOutputStream JavaDoc;
58 import java.io.IOException JavaDoc;
59 import java.io.InputStreamReader JavaDoc;
60 import java.io.OutputStream JavaDoc;
61 import java.io.Writer JavaDoc;
62 import java.net.InetAddress JavaDoc;
63 import java.net.Socket JavaDoc;
64 import java.net.UnknownHostException JavaDoc;
65 import java.util.ArrayList JavaDoc;
66 import java.util.Arrays JavaDoc;
67 import java.util.Collection JavaDoc;
68 import java.util.Iterator JavaDoc;
69 import java.util.List JavaDoc;
70 import java.util.Properties JavaDoc;
71
72 import junit.framework.TestCase;
73
74 /**
75  * Tests the org.apache.james.smtpserver.SMTPServer unit
76  */

77 public class SMTPServerTest extends TestCase {
78     
79     private final class AlterableDNSServer implements DNSServer {
80         
81         private InetAddress JavaDoc localhostByName = null;
82         
83         public Collection JavaDoc findMXRecords(String JavaDoc hostname) {
84             List JavaDoc res = new ArrayList JavaDoc();
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 JavaDoc getSMTPHostAddresses(String JavaDoc domainName) {
95             throw new UnsupportedOperationException JavaDoc("Unimplemented mock service");
96         }
97
98         public InetAddress JavaDoc[] getAllByName(String JavaDoc host) throws UnknownHostException JavaDoc {
99             return new InetAddress JavaDoc[] {getByName(host)};
100         }
101
102         public InetAddress JavaDoc getByName(String JavaDoc host) throws UnknownHostException JavaDoc {
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 JavaDoc();
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 JavaDoc("getByName not implemented in mock for host: "+host);
124             //return InetAddress.getByName(host);
125
}
126
127         public Collection JavaDoc findTXTRecords(String JavaDoc hostname) {
128             List JavaDoc res = new ArrayList JavaDoc();
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 JavaDoc getLocalhostByName() {
139             return localhostByName;
140         }
141
142         public void setLocalhostByName(InetAddress JavaDoc localhostByName) {
143             this.localhostByName = localhostByName;
144         }
145
146         public String JavaDoc getHostName(InetAddress JavaDoc addr) {
147             return addr.getHostName();
148         }
149
150     public InetAddress JavaDoc getLocalHost() throws UnknownHostException JavaDoc {
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 JavaDoc sender, String JavaDoc recipient, MimeMessage JavaDoc msg) throws IOException JavaDoc, MessagingException JavaDoc {
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 JavaDoc) mailData.getRecipients()).contains(new MailAddress(recipient)));
174         if (msg != null) {
175             ByteArrayOutputStream JavaDoc bo1 = new ByteArrayOutputStream JavaDoc();
176             msg.writeTo(bo1);
177             ByteArrayOutputStream JavaDoc bo2 = new ByteArrayOutputStream JavaDoc();
178             ((MimeMessage JavaDoc) mailData.getMessage()).writeTo(bo2);
179             assertEquals(bo1.toString(),bo2.toString());
180             assertEquals("message verification", msg, ((MimeMessage JavaDoc) mailData.getMessage()));
181         }
182     }
183     
184     protected void setUp() throws Exception JavaDoc {
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 JavaDoc {
193         ContainerUtil.dispose(m_mailServer);
194         super.tearDown();
195     }
196
197     private void finishSetUp(SMTPTestConfiguration testConfiguration) throws Exception JavaDoc {
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 JavaDoc {
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 JavaDoc {
222         finishSetUp(m_testConfiguration);
223         
224         SMTPClient smtpProtocol = new SMTPClient();
225         smtpProtocol.connect("127.0.0.1", m_smtpListenerPort);
226
227         // no message there, yet
228
assertNull("no mail received by mail server", m_mailServer.getLastMail());
229
230         smtpProtocol.sendCommand("EHLO "+InetAddress.getLocalHost());
231         String JavaDoc[] capabilityRes = smtpProtocol.getReplyStrings();
232         
233         List JavaDoc capabilitieslist = new ArrayList JavaDoc();
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         // mail was propagated by SMTPServer
251
assertNotNull("mail received by mail server", m_mailServer.getLastMail());
252     }
253
254     public void testEmptyMessage() throws Exception JavaDoc {
255         finishSetUp(m_testConfiguration);
256
257         SMTPClient smtp = new SMTPClient();
258         smtp.connect("127.0.0.1", m_smtpListenerPort);
259
260         // no message there, yet
261
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         // mail was propagated by SMTPServer
276
assertNotNull("mail received by mail server", m_mailServer.getLastMail());
277
278         // added to check a NPE in the test (JAMES-474) due to MockMailServer
279
// not cloning the message (added a MimeMessageCopyOnWriteProxy there)
280
System.gc();
281
282         int size = ((MimeMessage JavaDoc) m_mailServer.getLastMail().getMessage()).getSize();
283
284         assertEquals(size, 2);
285     }
286
287     public void testSimpleMailSendWithHELO() throws Exception JavaDoc {
288         finishSetUp(m_testConfiguration);
289
290         SMTPClient smtpProtocol = new SMTPClient();
291         smtpProtocol.connect("127.0.0.1", m_smtpListenerPort);
292
293         // no message there, yet
294
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         // mail was propagated by SMTPServer
308
assertNotNull("mail received by mail server", m_mailServer.getLastMail());
309     }
310
311     public void testTwoSimultaneousMails() throws Exception JavaDoc {
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         // no message there, yet
323
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 JavaDoc sender1 = "mail_sender1@localhost";
329         String JavaDoc recipient1 = "mail_recipient1@localhost";
330         smtpProtocol1.setSender(sender1);
331         smtpProtocol1.addRecipient(recipient1);
332
333         String JavaDoc sender2 = "mail_sender2@localhost";
334         String JavaDoc 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 JavaDoc {
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         // no message there, yet
360
assertNull("no mail received by mail server", m_mailServer.getLastMail());
361
362         smtpProtocol1.helo(InetAddress.getLocalHost().toString());
363
364         String JavaDoc sender1 = "mail_sender1@localhost";
365         String JavaDoc 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 JavaDoc sender2 = "mail_sender2@localhost";
373         String JavaDoc 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 JavaDoc {
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         // no message there, yet
396
assertNull("no mail received by mail server", m_mailServer.getLastMail());
397
398         String JavaDoc helo1 = "abgsfe3rsf.de";
399         String JavaDoc helo2 = "james.apache.org";
400         String JavaDoc mail = "sender@james.apache.org";
401         String JavaDoc rcpt = "rcpt@localhost";
402         
403         smtpProtocol1.sendCommand("helo",helo1);
404         smtpProtocol1.setSender(mail);
405         smtpProtocol1.addRecipient(rcpt);
406         
407         // this should give a 501 code cause the helo could not resolved
408
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         // helo is resolvable. so this should give a 250 code
415
assertEquals("Helo accepted", 250, smtpProtocol1.getReplyCode());
416
417         smtpProtocol1.quit();
418     }
419     
420     public void testHeloResolvDefault() throws Exception JavaDoc {
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         // helo should not be checked. so this should give a 250 code
428
assertEquals("Helo accepted", 250, smtpProtocol1.getReplyCode());
429
430         smtpProtocol1.quit();
431     }
432     
433     public void testReverseEqualsHelo() throws Exception JavaDoc {
434         m_testConfiguration.setReverseEqualsHelo();
435         m_testConfiguration.setAuthorizedAddresses("192.168.0.1");
436         // temporary alter the loopback resolution
437
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             // no message there, yet
447
assertNull("no mail received by mail server", m_mailServer
448                     .getLastMail());
449     
450             String JavaDoc helo1 = "abgsfe3rsf.de";
451             String JavaDoc helo2 = "james.apache.org";
452             String JavaDoc mail = "sender";
453             String JavaDoc rcpt = "recipient";
454     
455             smtpProtocol1.sendCommand("helo", helo1);
456             smtpProtocol1.setSender(mail);
457             smtpProtocol1.addRecipient(rcpt);
458             
459             // this should give a 501 code cause the helo not equal reverse of ip
460
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             // helo is resolvable. so this should give a 250 code
468
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 JavaDoc {
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         // no message there, yet
488
assertNull("no mail received by mail server", m_mailServer.getLastMail());
489
490         smtpProtocol1.helo(InetAddress.getLocalHost().toString());
491
492         String JavaDoc 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 JavaDoc {
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 JavaDoc sender1 = "mail_sender1@xfwrqqfgfe.de";
512         
513         smtpProtocol1.setSender(sender1);
514
515         smtpProtocol1.quit();
516     }
517     
518     public void testSenderDomainResolvRelayClientDefault() throws Exception JavaDoc {
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         // no message there, yet
529
assertNull("no mail received by mail server", m_mailServer.getLastMail());
530
531         smtpProtocol1.helo(InetAddress.getLocalHost().toString());
532
533         String JavaDoc sender1 = "mail_sender1@xfwrqqfgfe.de";
534         
535         // Both mail shold
536
smtpProtocol1.setSender(sender1);
537
538         smtpProtocol1.quit();
539         
540     }
541     
542     public void testSenderDomainResolvRelayClient() throws Exception JavaDoc {
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         // no message there, yet
554
assertNull("no mail received by mail server", m_mailServer.getLastMail());
555
556         smtpProtocol1.helo(InetAddress.getLocalHost().toString());
557
558         String JavaDoc sender1 = "mail_sender1@xfwrqqfgfe.de";
559         String JavaDoc 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 JavaDoc {
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         // no message there, yet
581
assertNull("no mail received by mail server", m_mailServer.getLastMail());
582
583         smtpProtocol1.helo(InetAddress.getLocalHost().toString());
584
585         String JavaDoc sender1 = "mail_sender1@james.apache.org";
586         String JavaDoc rcpt1 = "test@localhost";
587         String JavaDoc 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         // After the data is send the rcpt count is set back to 0.. So a new mail with rcpt should be accepted
598

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 JavaDoc {
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 JavaDoc sender1 = "mail_sender1@james.apache.org";
618         String JavaDoc 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 JavaDoc {
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         // no message there, yet
641
assertNull("no mail received by mail server", m_mailServer.getLastMail());
642
643         String JavaDoc ehlo1 = "abgsfe3rsf.de";
644         String JavaDoc ehlo2 = "james.apache.org";
645         String JavaDoc mail = "test@account";
646         String JavaDoc rcpt = "test";
647         
648         smtpProtocol1.sendCommand("ehlo", ehlo1);
649         smtpProtocol1.setSender(mail);
650         smtpProtocol1.addRecipient(rcpt);
651         
652         // this should give a 501 code cause the ehlo could not resolved
653
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         // ehlo is resolvable. so this should give a 250 code
660
assertEquals("ehlo accepted", 250, smtpProtocol1.getReplyCode());
661
662         smtpProtocol1.quit();
663     }
664     
665     public void testEhloResolvDefault() throws Exception JavaDoc {
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         // ehlo should not be checked. so this should give a 250 code
673
assertEquals("ehlo accepted", 250, smtpProtocol1.getReplyCode());
674
675         smtpProtocol1.quit();
676     }
677     
678     public void testEhloResolvIgnoreClientDisabled() throws Exception JavaDoc {
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         // no message there, yet
690
assertNull("no mail received by mail server", m_mailServer.getLastMail());
691
692         String JavaDoc ehlo1 = "abgsfe3rsf.de";
693         String JavaDoc ehlo2 = "james.apache.org";
694         String JavaDoc mail = "sender@localhost";
695         String JavaDoc rcpt = "test";
696         
697         smtpProtocol1.sendCommand("ehlo", ehlo1);
698         smtpProtocol1.setSender(mail);
699         smtpProtocol1.addRecipient(rcpt);
700         
701         // this should give a 501 code cause the ehlo could not resolved
702
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         // ehlo is resolvable. so this should give a 250 code
708
assertEquals("ehlo accepted", 250, smtpProtocol1.getReplyCode());
709
710         smtpProtocol1.quit();
711     }
712     
713     public void testReverseEqualsEhlo() throws Exception JavaDoc {
714         m_testConfiguration.setReverseEqualsEhlo();
715         m_testConfiguration.setAuthorizedAddresses("192.168.0.1");
716         // temporary alter the loopback resolution
717
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             // no message there, yet
727
assertNull("no mail received by mail server", m_mailServer
728                     .getLastMail());
729     
730             String JavaDoc ehlo1 = "abgsfe3rsf.de";
731             String JavaDoc ehlo2 = "james.apache.org";
732             String JavaDoc mail = "sender";
733             String JavaDoc rcpt = "recipient";
734             
735             smtpProtocol1.sendCommand("ehlo", ehlo1);
736             smtpProtocol1.setSender(mail);
737             smtpProtocol1.addRecipient(rcpt);
738             
739             // this should give a 501 code cause the ehlo not equals reverse of ip
740
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             // ehlo is resolvable. so this should give a 250 code
748
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 JavaDoc {
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         // no message there, yet
765
assertNull("no mail received by mail server", m_mailServer.getLastMail());
766
767         String JavaDoc 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 JavaDoc {
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         // no message there, yet
788
assertNull("no mail received by mail server", m_mailServer.getLastMail());
789
790         String JavaDoc sender1 = "mail_sender1@localhost";
791         
792         smtpProtocol1.setSender(sender1);
793         
794         smtpProtocol1.quit();
795     }
796
797     public void testAuth() throws Exception JavaDoc {
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 JavaDoc[] capabilityRes = smtpProtocol.getReplyStrings();
807         
808         List JavaDoc capabilitieslist = new ArrayList JavaDoc();
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         // is this required or just for compatibility? assertTrue("anouncing auth required", capabilitieslist.contains("AUTH=LOGIN PLAIN"));
815

816         String JavaDoc userName = "test_user_smtp";
817         String JavaDoc noexistUserName = "noexist_test_user_smtp";
818         String JavaDoc 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 // smtpProtocol.sendCommand(noexistUserName+"pwd".toCharArray());
832
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         // mail was propagated by SMTPServer
853
assertNotNull("mail received by mail server", m_mailServer.getLastMail());
854     }
855
856     public void testAuthWithEmptySender() throws Exception JavaDoc {
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 JavaDoc 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 JavaDoc {
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         // left out for test smtpProtocol.rcpt(new Address("mail@localhost"));
892

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         // mail was propagated by SMTPServer
899
assertNull("no mail received by mail server", m_mailServer.getLastMail());
900     }
901
902     public void testMultipleMailsAndRset() throws Exception JavaDoc {
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         // mail was propagated by SMTPServer
919
assertNull("no mail received by mail server", m_mailServer.getLastMail());
920     }
921
922     public void testRelayingDenied() throws Exception JavaDoc {
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 JavaDoc {
938         m_testConfiguration.setMaxMessageSize(1); // set message limit to 1kb
939
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 JavaDoc {
954         m_testConfiguration.setMaxMessageSize(1); // set message limit to 1kb
955
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 JavaDoc wr = smtpProtocol.sendMessageData();
966         // create Body with more than 1kb . 502
967
wr.write("1234567810123456782012345678301234567840123456785012345678601234567870123456788012345678901234567100");
968         wr.write("1234567810123456782012345678301234567840123456785012345678601234567870123456788012345678901234567100");
969         wr.write("1234567810123456782012345678301234567840123456785012345678601234567870123456788012345678901234567100");
970         wr.write("1234567810123456782012345678301234567840123456785012345678601234567870123456788012345678901234567100");
971         wr.write("1234567810123456782012345678301234567840123456785012345678601234567870123456788012345678901234567100\r\n");
972         // second line
973
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"); // 521 + CRLF = 523 + 502 => 1025
979
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 JavaDoc {
988         m_testConfiguration.setMaxMessageSize(1); // set message limit to 1kb
989
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 JavaDoc wr = smtpProtocol.sendMessageData();
1000        // create Body with less than 1kb
1001
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"); // 1022 + CRLF = 1024
1012
wr.close();
1013        
1014        assertTrue(smtpProtocol.completePendingCommand());
1015
1016        assertEquals("expected 250 ok", 250, smtpProtocol.getReplyCode());
1017
1018    }
1019
1020    /*
1021      What we want to see is that for a given connection limit and a
1022      given backlog, that connection limit requests are handled, and
1023      that up to the backlog number of connections are queued. More
1024      connections than that should error out until space opens up in
1025      the queue.
1026
1027      For example:
1028
1029        # telnet localhost <m_smtpListenPort>
1030        Trying 127.0.0.1...
1031        telnet: Unable to connect to remote host: Connection refused
1032
1033      is the immediate response if the backlog is full.
1034    */

1035
1036    public void testConnectionLimitExceeded() throws Exception JavaDoc {
1037        final int acceptLimit = 1;
1038        final int backlog = 1;
1039
1040        m_testConfiguration.setConnectionLimit(acceptLimit); // allow no more than <acceptLimit> connection(s) in the service
1041
m_testConfiguration.setConnectionBacklog(backlog); // allow <backlog> additional connection(s) in the queue
1042
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(); // should connect to worker
1047
try {
1048                client[i].connect("127.0.0.1", m_smtpListenerPort);
1049            } catch (Exception JavaDoc _) {
1050            }
1051            assertTrue("client #" + (i+1) + " established", client[i].isConnected());
1052        }
1053
1054        // Cannot use SMTPClient. It appears that even though the
1055
// client's socket is established, since the client won't be
1056
// able to connect to the protocol handler, the connect call
1057
// hangs.
1058

1059        // Different TCP/IP stacks may provide a "grace" margin above
1060
// and beyond the specified backlog. So we won't try to be
1061
// precise. Instead we will compute some upper limit, loop
1062
// until we get a connection error (or hit the limit), and
1063
// then test for the expected behavior.
1064
//
1065
// See: http://www.phrack.org/archives/48/P48-13
1066
final Socket JavaDoc connection[] = new Socket JavaDoc[Math.max(((backlog * 3) / 2) + 1, backlog + 3)];
1067
1068        final java.net.SocketAddress JavaDoc server = new java.net.InetSocketAddress JavaDoc("localhost", m_smtpListenerPort);
1069
1070        for (int i = 0; i < connection.length; i++) {
1071            connection[i] = new Socket JavaDoc();
1072            try {
1073                connection[i].connect(server, 1000);
1074            } catch (Exception JavaDoc _) {
1075                assertTrue("Accepted connections " + i + " did not meet or exceed backlog of " + backlog + ".", i >= backlog);
1076                connection[i] = null; // didn't open, so don't try to close later
1077
break; // OK to fail, since we've at least reached the backlog
1078
}
1079            assertTrue("connection #" + (i+1) + " established", connection[i].isConnected());
1080        }
1081
1082        try {
1083            final Socket JavaDoc shouldFail = new Socket JavaDoc();
1084            shouldFail.connect(server, 1000);
1085            fail("connection # " + (client.length + connection.length + 1) + " did not fail.");
1086        } catch (Exception JavaDoc _) {
1087        }
1088
1089        client[0].quit();
1090        client[0].disconnect();
1091
1092        Thread.sleep(100);
1093        
1094        // now should be able to connect (backlog)
1095
try {
1096            final Socket JavaDoc shouldWork = new Socket JavaDoc();
1097            shouldWork.connect(server, 1000);
1098            assertTrue("Additional connection established after close.", shouldWork.isConnected());
1099            shouldWork.close();
1100        } catch (Exception JavaDoc e) {
1101            fail("Could not establish additional connection after close." + e.getMessage());
1102        }
1103
1104        // close the pending connections first, so that the server doesn't see them
1105
for (int i = 0; i < connection.length; i++) if (connection[i] != null) connection[i].close();
1106
1107        // close the remaining clients
1108
for (int i = 1; i < client.length; i++) {
1109            client[i].quit();
1110            client[i].disconnect();
1111        }
1112    }
1113    
1114    // RemoteDelivery tests.
1115

1116    InMemorySpoolRepository outgoingSpool;
1117    private MockServiceManager m_serviceManager;
1118    private AlterableDNSServer m_dnsServer;
1119    
1120    private Properties JavaDoc getStandardParameters() {
1121        Properties JavaDoc parameters = new Properties JavaDoc();
1122        parameters.put("delayTime", "500 msec, 500 msec, 500 msec"); // msec, sec, minute, hour
1123
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    /**
1134     * This has been created to test javamail 1.4 introduced bug.
1135     * http://issues.apache.org/jira/browse/JAMES-490
1136     */

1137    public void testDeliveryToSelfWithGatewayAndBind() throws Exception JavaDoc {
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 JavaDoc sources = "Content-Type: text/plain;\r\nSubject: test\r\n\r\nBody";
1153        String JavaDoc sender = "test@localhost";
1154        String JavaDoc recipient = "test@localhost";
1155        MimeMessage JavaDoc mm = new MimeMessage JavaDoc(Session.getDefaultInstance(new Properties JavaDoc()),new ByteArrayInputStream JavaDoc(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 JavaDoc) mm.getContent()).trim(),((String JavaDoc) ((MimeMessage JavaDoc) m_mailServer.getLastMail().getMessage()).getContent()).trim());
1167        
1168        mail.dispose();
1169    }
1170
1171    
1172    /**
1173     * This is useful code to run tests on javamail bugs
1174     * http://issues.apache.org/jira/browse/JAMES-52
1175     *
1176     * This one passes with javamail 1.4.1EA
1177     * @throws Exception
1178     */

1179    public void test8bitmimeFromStream() throws Exception JavaDoc {
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;\r\nContent-Transfer-Encoding: quoted-printable\r\nSubject: test\r\n\r\nBody=80\r\n";
1195
String JavaDoc 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 sources = "Content-Type: text/plain; charset=iso-8859-15\r\nContent-Transfer-Encoding: 8bit\r\nSubject: test\r\n\r\nBody\u20AC\r\n";
1197
String JavaDoc sender = "test@localhost";
1198        String JavaDoc recipient = "test@localhost";
1199        MimeMessage JavaDoc mm = new MimeMessage JavaDoc(Session.getDefaultInstance(new Properties JavaDoc()),new ByteArrayInputStream JavaDoc(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, mm);
1209
verifyLastMail(sender, recipient, null);
1210        
1211        // THIS WOULD FAIL BECAUSE OF THE JAVAMAIL BUG
1212
assertEquals(mm.getContent(),m_mailServer.getLastMail().getMessage().getContent());
1213        
1214        mail.dispose();
1215    }
1216    
1217    /**
1218     * This is useful code to run tests on javamail bugs
1219     * http://issues.apache.org/jira/browse/JAMES-52
1220     *
1221     * This one passes with javamail 1.4.1EA
1222     * @throws Exception
1223     */

1224    public void test8bitmimeFromStreamWith8bitContent() throws Exception JavaDoc {
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;\r\nContent-Transfer-Encoding: quoted-printable\r\nSubject: test\r\n\r\nBody=80\r\n";
1240
//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";
1241
String JavaDoc 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 JavaDoc sender = "test@localhost";
1243        String JavaDoc recipient = "test@localhost";
1244        MimeMessage JavaDoc mm = new MimeMessage JavaDoc(Session.getDefaultInstance(new Properties JavaDoc()),new ByteArrayInputStream JavaDoc(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, mm);
1254
verifyLastMail(sender, recipient, null);
1255        
1256        // THIS WOULD FAIL BECAUSE OF THE JAVAMAIL BUG
1257
assertEquals(mm.getContent(),m_mailServer.getLastMail().getMessage().getContent());
1258        
1259        mail.dispose();
1260    }
1261    
1262    /**
1263     * This is useful code to run tests on javamail bugs
1264     * http://issues.apache.org/jira/browse/JAMES-52
1265     *
1266     * This one passes with javamail 1.4.1EA
1267     * @throws Exception
1268     */

1269    public void test8bitmimeFromStreamWithoutContentTransferEncoding() throws Exception JavaDoc {
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 JavaDoc sources = "Content-Type: text/plain;\r\nSubject: test\r\n\r\nBody\u03B2\r\n";
1285        //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";
1286
//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";
1287
String JavaDoc sender = "test@localhost";
1288        String JavaDoc recipient = "test@localhost";
1289        MimeMessage JavaDoc mm = new MimeMessage JavaDoc(Session.getDefaultInstance(new Properties JavaDoc()),new ByteArrayInputStream JavaDoc(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, mm);
1299
verifyLastMail(sender, recipient, null);
1300        
1301        // THIS WOULD FAIL BECAUSE OF THE JAVAMAIL BUG
1302
assertEquals(mm.getContent(),m_mailServer.getLastMail().getMessage().getContent());
1303        
1304        mail.dispose();
1305    }
1306    
1307    /**
1308     * This is useful code to run tests on javamail bugs
1309     * http://issues.apache.org/jira/browse/JAMES-52
1310     *
1311     * This one passes with javamail 1.4.1EA
1312     * @throws Exception
1313     */

1314    public void test8bitmimeFromStreamWithoutContentTransferEncodingSentAs8bit() throws Exception JavaDoc {
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 JavaDoc sources = "Content-Type: text/plain;\r\nSubject: test\r\n\r\nBody=32=48\r\n";
1330        //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";
1331
//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";
1332
String JavaDoc sender = "test@localhost";
1333        String JavaDoc recipient = "test@localhost";
1334        MimeMessage JavaDoc mm = new MimeMessage JavaDoc(Session.getDefaultInstance(new Properties JavaDoc()),new ByteArrayInputStream JavaDoc(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, mm);
1344
verifyLastMail(sender, recipient, null);
1345        
1346        // THIS WOULD FAIL BECAUSE OF THE JAVAMAIL BUG
1347
assertEquals(mm.getContent(),m_mailServer.getLastMail().getMessage().getContent());
1348        
1349        mail.dispose();
1350    }
1351    
1352    /**
1353     * This is useful code to run tests on javamail bugs
1354     * http://issues.apache.org/jira/browse/JAMES-52
1355     *
1356     * This one passes with javamail 1.4.1EA
1357     * @throws Exception
1358     */

1359    public void test8bitmimeWith8bitmimeDisabledInServer() throws Exception JavaDoc {
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;\r\nSubject: test\r\n\r\nBody=32=48\r\n";
1376
//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";
1377
String JavaDoc 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 JavaDoc sender = "test@localhost";
1379        String JavaDoc recipient = "test@localhost";
1380        MimeMessage JavaDoc mm = new MimeMessage JavaDoc(Session.getDefaultInstance(new Properties JavaDoc()),new ByteArrayInputStream JavaDoc(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, mm);
1390
verifyLastMail(sender, recipient, null);
1391        
1392        // THIS WOULD FAIL BECAUSE OF THE JAVAMAIL BUG
1393
assertEquals(mm.getContent(),m_mailServer.getLastMail().getMessage().getContent());
1394        
1395        mail.dispose();
1396    }
1397    
1398    // Check if auth users get not rejected cause rbl. See JAMES-566
1399
public void testDNSRBLNotRejectAuthUser() throws Exception JavaDoc {
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 JavaDoc[] capabilityRes = smtpProtocol.getReplyStrings();
1412
1413        List JavaDoc capabilitieslist = new ArrayList JavaDoc();
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        // is this required or just for compatibility? assertTrue("anouncing
1421
// auth required", capabilitieslist.contains("AUTH=LOGIN PLAIN"));
1422

1423        String JavaDoc userName = "test_user_smtp";
1424        String JavaDoc 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        // mail was propagated by SMTPServer
1444
assertNotNull("mail received by mail server", m_mailServer
1445                .getLastMail());
1446    }
1447    
1448   
1449    public void testDNSRBLRejectWorks() throws Exception JavaDoc {
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 JavaDoc 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        // mail was rejected by SMTPServer
1474
assertNull("mail reject by mail server", m_mailServer
1475                .getLastMail());
1476    }
1477    
1478    
1479    public void testAddressBracketsEnforcementDisabled() throws Exception JavaDoc {
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 JavaDoc {
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    // See http://www.ietf.org/rfc/rfc2920.txt 4: Examples
1529
public void testPipelining() throws Exception JavaDoc {
1530        StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
1531        finishSetUp(m_testConfiguration);
1532        Socket JavaDoc client = new Socket JavaDoc("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 JavaDoc out = client.getOutputStream();
1553        
1554        out.write(buf.toString().getBytes());
1555        out.flush();
1556      
1557        BufferedReader JavaDoc in = new BufferedReader JavaDoc(new InputStreamReader JavaDoc(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    // See http://www.ietf.org/rfc/rfc2920.txt 4: Examples
1571
public void testRejectAllRCPTPipelining() throws Exception JavaDoc {
1572        StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
1573        m_testConfiguration.setAuthorizedAddresses("");
1574        finishSetUp(m_testConfiguration);
1575        Socket JavaDoc client = new Socket JavaDoc("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 JavaDoc out = client.getOutputStream();
1598        
1599        out.write(buf.toString().getBytes());
1600        out.flush();
1601      
1602        BufferedReader JavaDoc in = new BufferedReader JavaDoc(new InputStreamReader JavaDoc(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 JavaDoc {
1616        StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
1617        m_testConfiguration.setAuthorizedAddresses("");
1618        finishSetUp(m_testConfiguration);
1619        Socket JavaDoc client = new Socket JavaDoc("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 JavaDoc out = client.getOutputStream();
1642        
1643        out.write(buf.toString().getBytes());
1644        out.flush();
1645      
1646        BufferedReader JavaDoc in = new BufferedReader JavaDoc(new InputStreamReader JavaDoc(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