KickJava   Java API By Example, From Geeks To Geeks.

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


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.util.ArrayList JavaDoc;
23 import java.util.Collection JavaDoc;
24 import java.util.HashMap JavaDoc;
25 import java.util.Map JavaDoc;
26
27 import javax.mail.internet.ParseException JavaDoc;
28
29 import org.apache.avalon.framework.container.ContainerUtil;
30 import org.apache.james.services.AbstractDNSServer;
31 import org.apache.james.services.DNSServer;
32 import org.apache.james.smtpserver.core.filter.fastfail.ValidSenderDomainHandler;
33 import org.apache.james.test.mock.avalon.MockLogger;
34 import org.apache.james.util.junkscore.JunkScore;
35 import org.apache.james.util.junkscore.JunkScoreImpl;
36 import org.apache.mailet.MailAddress;
37
38 import junit.framework.TestCase;
39
40 public class ValidSenderDomainHandlerTest extends TestCase {
41     
42     private String JavaDoc response = null;
43
44     public void setUp() {
45         response = null;
46     }
47     
48     private DNSServer setupDNSServer() {
49         DNSServer dns = new AbstractDNSServer(){
50
51             public Collection JavaDoc findMXRecords(String JavaDoc hostname) {
52                 Collection JavaDoc mx = new ArrayList JavaDoc();
53                 if (hostname.equals("test.james.apache.org")) {
54                     mx.add("mail.james.apache.org");
55                 }
56                 return mx;
57             }
58             
59         };
60         return dns;
61     }
62     
63     private SMTPSession setupMockedSession(final MailAddress sender) {
64         SMTPSession session = new AbstractSMTPSession() {
65             HashMap JavaDoc state = new HashMap JavaDoc();
66             boolean processing = false;
67             
68             public Map JavaDoc getState() {
69
70                 state.put(SMTPSession.SENDER, sender);
71
72                 return state;
73             }
74             
75             public boolean isRelayingAllowed() {
76                 return false;
77             }
78             
79             public void writeResponse(String JavaDoc resp) {
80                 response = resp;
81             }
82             
83             public void setStopHandlerProcessing(boolean processing) {
84                 this.processing = processing;
85             }
86             
87             public boolean getStopHandlerProcessing() {
88                 return processing;
89             }
90             
91         };
92         return session;
93     }
94     
95     private String JavaDoc getResponse() {
96         return response;
97     }
98     
99     // Test for JAMES-580
100
public void testNullSenderNotReject() {
101         ValidSenderDomainHandler handler = new ValidSenderDomainHandler();
102         ContainerUtil.enableLogging(handler, new MockLogger());
103         
104         handler.setDnsServer(setupDNSServer());
105         handler.onCommand(setupMockedSession(null));
106         
107         assertNull("Not blocked cause its a nullsender",getResponse());
108     }
109     
110     public void testInvalidSenderDomainAddJunkScore() throws ParseException JavaDoc {
111         ValidSenderDomainHandler handler = new ValidSenderDomainHandler();
112         SMTPSession session = setupMockedSession(new MailAddress("invalid@invalid"));
113         ContainerUtil.enableLogging(handler, new MockLogger());
114         session.getState().put(JunkScore.JUNK_SCORE, new JunkScoreImpl());
115         handler.setAction("JunkScore");
116         handler.setScore(20);
117         handler.setDnsServer(setupDNSServer());
118         handler.onCommand(session);
119         
120         assertNull("Not blocked cause we use JunkScore",getResponse());
121         assertEquals("JunkScore is stored",((JunkScore) session.getState().get(JunkScore.JUNK_SCORE)).getStoredScore("ValidSenderDomainCheck"),20.0,0d);
122     }
123     
124     public void testInvalidSenderDomainReject() throws ParseException JavaDoc {
125         ValidSenderDomainHandler handler = new ValidSenderDomainHandler();
126         SMTPSession session = setupMockedSession(new MailAddress("invalid@invalid"));
127         ContainerUtil.enableLogging(handler, new MockLogger());
128         handler.setDnsServer(setupDNSServer());
129         handler.onCommand(session);
130         
131         assertNotNull("Blocked cause we use reject action",getResponse());
132     }
133 }
134
Popular Tags