KickJava   Java API By Example, From Geeks To Geeks.

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


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 java.net.InetAddress JavaDoc;
23 import java.net.UnknownHostException JavaDoc;
24 import java.util.ArrayList JavaDoc;
25 import java.util.Collection JavaDoc;
26 import java.util.HashMap JavaDoc;
27 import java.util.Map JavaDoc;
28
29 import javax.mail.internet.ParseException JavaDoc;
30
31 import org.apache.avalon.framework.container.ContainerUtil;
32 import org.apache.james.services.AbstractDNSServer;
33 import org.apache.james.services.DNSServer;
34 import org.apache.james.smtpserver.core.filter.fastfail.ValidRcptMX;
35 import org.apache.james.test.mock.avalon.MockLogger;
36 import org.apache.james.util.junkscore.JunkScore;
37 import org.apache.james.util.junkscore.JunkScoreImpl;
38 import org.apache.mailet.MailAddress;
39
40 import junit.framework.TestCase;
41
42 public class ValidRcptMXTest extends TestCase {
43     private String JavaDoc response = null;
44
45     private final static String JavaDoc INVALID_HOST = "invalid.host.de";
46
47     private final static String JavaDoc INVALID_MX = "mx." + INVALID_HOST;
48
49     private final static String JavaDoc LOOPBACK = "127.0.0.1";
50
51     protected void setUp() throws Exception JavaDoc {
52         response = null;
53
54         super.setUp();
55     }
56
57     private SMTPSession setupMockedSMTPSession(final MailAddress rcpt) {
58         SMTPSession session = new AbstractSMTPSession() {
59             HashMap JavaDoc state = new HashMap JavaDoc();
60             boolean stopProcessing = false;
61
62             public Map JavaDoc getState() {
63                 state.put(SMTPSession.CURRENT_RECIPIENT, rcpt);
64                 return state;
65             }
66
67             public void writeResponse(String JavaDoc resp) {
68                 response = resp;
69             }
70             
71             public String JavaDoc getRemoteIPAddress() {
72                 return "127.0.0.1";
73             }
74             
75             public void setStopHandlerProcessing(boolean stopProcessing) {
76                 this.stopProcessing = stopProcessing;
77             }
78
79         };
80         return session;
81     }
82
83     private DNSServer setupMockedDNSServer() {
84         DNSServer dns = new AbstractDNSServer() {
85
86             public Collection JavaDoc findMXRecords(String JavaDoc hostname) {
87                 Collection JavaDoc mx = new ArrayList JavaDoc();
88
89                 if (hostname.equals(INVALID_HOST)) {
90                     mx.add(INVALID_MX);
91                 }
92                 return mx;
93             }
94             
95             public InetAddress JavaDoc getByName(String JavaDoc host) throws UnknownHostException JavaDoc {
96                 if (host.equals(INVALID_MX) || host.equals(LOOPBACK)) {
97                     return InetAddress.getByName(LOOPBACK);
98                 } else if (host.equals("255.255.255.255")) {
99                     return InetAddress.getByName("255.255.255.255");
100                 }
101                 throw new UnknownHostException JavaDoc("Unknown host");
102             }
103
104         };
105
106         return dns;
107     }
108
109     public void testRejectLoopbackMX() throws ParseException JavaDoc {
110         Collection JavaDoc bNetworks = new ArrayList JavaDoc();
111         bNetworks.add("127.0.0.1");
112         
113         DNSServer dns = setupMockedDNSServer();
114         ValidRcptMX handler = new ValidRcptMX();
115
116         ContainerUtil.enableLogging(handler, new MockLogger());
117
118         handler.setDNSServer(dns);
119         handler.setBannedNetworks(bNetworks, dns);
120         handler.onCommand(setupMockedSMTPSession(new MailAddress("test@" + INVALID_HOST)));
121
122         assertNotNull("Reject", response);
123     }
124     
125
126     public void testAddJunkScoreLoopbackMX() throws ParseException JavaDoc {
127         Collection JavaDoc bNetworks = new ArrayList JavaDoc();
128         bNetworks.add("127.0.0.1");
129         
130         SMTPSession session = setupMockedSMTPSession(new MailAddress("test@" + INVALID_HOST));
131         session.getState().put(JunkScore.JUNK_SCORE, new JunkScoreImpl());
132         
133         DNSServer dns = setupMockedDNSServer();
134         ValidRcptMX handler = new ValidRcptMX();
135         handler.setScore(20);
136         handler.setAction("junkScore");
137
138         ContainerUtil.enableLogging(handler, new MockLogger());
139
140         handler.setDNSServer(dns);
141         handler.setBannedNetworks(bNetworks, dns);
142         handler.onCommand(session);
143
144         assertNull("Not Reject", response);
145         assertEquals("JunkScore added",((JunkScore) session.getState().get(JunkScore.JUNK_SCORE)).getStoredScore("ValidRcptMXCheck"),20.0, 0d);
146     }
147 }
148
Popular Tags