KickJava   Java API By Example, From Geeks To Geeks.

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


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
21
22
23 package org.apache.james.smtpserver;
24
25 import java.util.ArrayList JavaDoc;
26 import java.util.Collection JavaDoc;
27 import java.util.HashMap JavaDoc;
28 import java.util.Map JavaDoc;
29
30 import org.apache.avalon.framework.container.ContainerUtil;
31 import org.apache.james.services.MailServer;
32 import org.apache.james.services.UsersRepository;
33 import org.apache.james.services.VirtualUserTable;
34 import org.apache.james.smtpserver.core.filter.fastfail.ValidRcptHandler;
35 import org.apache.james.test.mock.avalon.MockLogger;
36 import org.apache.james.test.mock.avalon.MockServiceManager;
37 import org.apache.james.userrepository.MockUsersRepository;
38 import org.apache.james.vut.ErrorMappingException;
39 import org.apache.mailet.MailAddress;
40 import org.apache.oro.text.regex.MalformedPatternException;
41
42 import junit.framework.TestCase;
43
44 public class ValidRcptHandlerTest extends TestCase {
45     
46     private final static String JavaDoc VALID_USER = "postmaster";
47     private final static String JavaDoc INVALID_USER = "invalid";
48     private final static String JavaDoc USER1 = "user1";
49     private final static String JavaDoc USER2 = "user2";
50     private String JavaDoc response = null;
51     private MockServiceManager serviceMan;
52     
53     public void setUp() {
54         response = null;
55     }
56     
57     private SMTPSession setupMockedSMTPSession(final SMTPHandlerConfigurationData conf, final MailAddress rcpt, final boolean relayingAllowed, final boolean authRequired, final String JavaDoc username) {
58         SMTPSession session = new AbstractSMTPSession() {
59             HashMap JavaDoc state = new HashMap JavaDoc();
60             boolean stop = false;
61         
62             public boolean isAuthRequired() {
63                 return authRequired;
64             }
65         
66             public String JavaDoc getUser() {
67                 return username;
68             }
69         
70             public boolean isRelayingAllowed() {
71                 return relayingAllowed;
72             }
73         
74             public SMTPHandlerConfigurationData getConfigurationData() {
75                 return conf;
76             }
77         
78             public Map JavaDoc getState() {
79                 state.put(SMTPSession.CURRENT_RECIPIENT,rcpt);
80
81                 return state;
82             }
83         
84             public void writeResponse(String JavaDoc resp) {
85                 response = resp;
86             }
87         
88             public void setStopHandlerProcessing(boolean stop) {
89                 this.stop = stop;
90             }
91         
92             public boolean getStopHandlerProcessing() {
93                 return stop;
94             }
95         };
96     
97         return session;
98     }
99     
100     private MockServiceManager setUpServiceManager() throws Exception JavaDoc {
101         serviceMan = new MockServiceManager();
102         serviceMan.put(VirtualUserTable.ROLE, setUpVirtualUserTable());
103         return serviceMan;
104     }
105     
106     private VirtualUserTable setUpVirtualUserTable() {
107         VirtualUserTable table = new VirtualUserTable() {
108  
109             public Collection JavaDoc getMappings(String JavaDoc user, String JavaDoc domain) throws ErrorMappingException {
110                 Collection JavaDoc mappings = new ArrayList JavaDoc();
111                 if (user.equals(USER1)) {
112                     mappings.add("address@localhost");
113                 } else if (user.equals(USER2)) {
114                     throw new ErrorMappingException("BOUNCE!");
115                 }
116                 return mappings;
117             }
118         };
119         return table;
120     }
121     
122     private SMTPHandlerConfigurationData setupMockedSMTPConfiguration() {
123         SMTPHandlerConfigurationData conf = new SMTPHandlerConfigurationData() {
124             UsersRepository user = new MockUsersRepository();
125         
126             public String JavaDoc getHelloName() {
127                 throw new UnsupportedOperationException JavaDoc("Unimplemented Stub Method");
128             }
129
130             public MailServer getMailServer() {
131                 throw new UnsupportedOperationException JavaDoc("Unimplemented Stub Method");
132             }
133
134             public long getMaxMessageSize() {
135                 throw new UnsupportedOperationException JavaDoc("Unimplemented Stub Method");
136             }
137
138             public int getResetLength() {
139                 throw new UnsupportedOperationException JavaDoc("Unimplemented Stub Method");
140             }
141
142             public String JavaDoc getSMTPGreeting() {
143                 throw new UnsupportedOperationException JavaDoc("Unimplemented Stub Method");
144             }
145
146             public UsersRepository getUsersRepository() {
147                 user.addUser(VALID_USER,"xxx");
148                 return user;
149             }
150
151             public boolean isAuthRequired(String JavaDoc remoteIP) {
152                 throw new UnsupportedOperationException JavaDoc("Unimplemented Stub Method");
153             }
154
155             public boolean isAuthRequired() {
156                 throw new UnsupportedOperationException JavaDoc("Unimplemented Stub Method");
157             }
158
159             public boolean isRelayingAllowed(String JavaDoc remoteIP) {
160                 throw new UnsupportedOperationException JavaDoc("Unimplemented Stub Method");
161             }
162
163             public boolean isVerifyIdentity() {
164                 throw new UnsupportedOperationException JavaDoc("Unimplemented Stub Method");
165             }
166
167             public boolean useHeloEhloEnforcement() {
168                 throw new UnsupportedOperationException JavaDoc("Unimplemented Stub Method");
169             }
170
171             public boolean useAddressBracketsEnforcement() {
172                 return true;
173             }
174         
175         };
176     
177         return conf;
178     }
179     
180     public void testRejectInvalidUser() throws Exception JavaDoc {
181         ValidRcptHandler handler = new ValidRcptHandler();
182         ContainerUtil.service(handler, setUpServiceManager());
183         SMTPSession session = setupMockedSMTPSession(setupMockedSMTPConfiguration(),new MailAddress(INVALID_USER + "@localhost"),false,false,null);
184         ContainerUtil.enableLogging(handler,new MockLogger());
185     
186         handler.onCommand(session);
187     
188         assertTrue("Rejected",session.getStopHandlerProcessing());
189         assertNotNull("Rejected",response);
190     }
191     
192     public void testNotRejectInvalidUserAuth() throws Exception JavaDoc {
193         ValidRcptHandler handler = new ValidRcptHandler();
194         ContainerUtil.service(handler, setUpServiceManager());
195         SMTPSession session = setupMockedSMTPSession(setupMockedSMTPConfiguration(),new MailAddress(INVALID_USER + "@localhost"),false,true,"authedUser");
196         ContainerUtil.enableLogging(handler,new MockLogger());
197     
198         handler.onCommand(session);
199     
200         assertFalse("Not rejected",session.getStopHandlerProcessing());
201         assertNull("Not rejected",response);
202     }
203     
204     public void testNotRejectInvalidUserRelay() throws Exception JavaDoc {
205         ValidRcptHandler handler = new ValidRcptHandler();
206         ContainerUtil.service(handler, setUpServiceManager());
207         SMTPSession session = setupMockedSMTPSession(setupMockedSMTPConfiguration(),new MailAddress(INVALID_USER + "@localhost"),true,false,null);
208         ContainerUtil.enableLogging(handler,new MockLogger());
209     
210         handler.onCommand(session);
211     
212         assertFalse("Not rejected",session.getStopHandlerProcessing());
213         assertNull("Not rejected",response);
214     }
215     
216     public void testNotRejectValidUser() throws Exception JavaDoc {
217         ValidRcptHandler handler = new ValidRcptHandler();
218         ContainerUtil.service(handler, setUpServiceManager());
219         SMTPSession session = setupMockedSMTPSession(setupMockedSMTPConfiguration(),new MailAddress(VALID_USER + "@localhost"),false,false,null);
220         ContainerUtil.enableLogging(handler,new MockLogger());
221     
222         handler.onCommand(session);
223     
224         assertFalse("Not rejected",session.getStopHandlerProcessing());
225         assertNull("Not rejected",response);
226     }
227     
228     public void testNotRejectValidUserRecipient() throws Exception JavaDoc {
229         String JavaDoc recipient = "recip@domain";
230         ValidRcptHandler handler = new ValidRcptHandler();
231         ContainerUtil.service(handler, setUpServiceManager());
232         SMTPSession session = setupMockedSMTPSession(setupMockedSMTPConfiguration(),new MailAddress(recipient),false,false,null);
233         ContainerUtil.enableLogging(handler,new MockLogger());
234     
235         handler.setValidRecipients(recipient);
236         handler.onCommand(session);
237         
238     
239         assertFalse("Not rejected",session.getStopHandlerProcessing());
240         assertNull("Not rejected",response);
241     }
242     
243     public void testNotRejectValidUserDomain() throws Exception JavaDoc {
244         String JavaDoc domain = "domain";
245         String JavaDoc recipient = "recip@" + domain;
246
247         ValidRcptHandler handler = new ValidRcptHandler();
248         ContainerUtil.service(handler, setUpServiceManager());
249         SMTPSession session = setupMockedSMTPSession(setupMockedSMTPConfiguration(),new MailAddress(recipient),false,false,null);
250         ContainerUtil.enableLogging(handler,new MockLogger());
251     
252         handler.setValidDomains(domain);
253         handler.onCommand(session);
254         
255     
256         assertFalse("Not rejected",session.getStopHandlerProcessing());
257         assertNull("Not rejected",response);
258     }
259     
260     public void testNotRejectValidUserRegex() throws Exception JavaDoc {
261         String JavaDoc domain = "domain";
262         String JavaDoc recipient = "recip@" + domain;
263
264         ValidRcptHandler handler = new ValidRcptHandler();
265         ContainerUtil.service(handler, setUpServiceManager());
266         SMTPSession session = setupMockedSMTPSession(setupMockedSMTPConfiguration(),new MailAddress(recipient),false,false,null);
267         ContainerUtil.enableLogging(handler,new MockLogger());
268     
269         handler.setValidRegex("reci.*");
270         handler.onCommand(session);
271         
272     
273         assertFalse("Not rejected",session.getStopHandlerProcessing());
274         assertNull("Not rejected",response);
275     }
276     
277     public void testInvalidRegex() throws Exception JavaDoc{
278         boolean exception = false;
279         ValidRcptHandler handler = new ValidRcptHandler();
280         ContainerUtil.service(handler, setUpServiceManager());
281         ContainerUtil.enableLogging(handler,new MockLogger());
282     
283         try {
284             handler.setValidRegex("(.*");
285         } catch (MalformedPatternException e) {
286             exception = true;
287         }
288
289         assertTrue("Invalid Config",exception);
290     }
291     
292     public void testHasAddressMapping() throws Exception JavaDoc {
293         SMTPSession session = setupMockedSMTPSession(setupMockedSMTPConfiguration(),new MailAddress(USER1 + "@localhost"),false,false,null);
294     
295         ValidRcptHandler handler = new ValidRcptHandler();
296         ContainerUtil.service(handler, setUpServiceManager());
297         ContainerUtil.enableLogging(handler,new MockLogger());
298         handler.onCommand(session);
299     
300         assertNull("No reject",response);
301         assertFalse("Not stop processing",session.getStopHandlerProcessing());
302     }
303     
304     public void testHasErrorMapping() throws Exception JavaDoc {
305         SMTPSession session = setupMockedSMTPSession(setupMockedSMTPConfiguration(),new MailAddress(USER2 + "@localhost"),false,false,null);
306
307         ValidRcptHandler handler = new ValidRcptHandler();
308         ContainerUtil.service(handler, setUpServiceManager());
309         ContainerUtil.enableLogging(handler,new MockLogger());
310         handler.onCommand(session);
311      
312        assertNull("Valid Error mapping",session.getState().get("VALID_USER"));
313        assertNotNull("Error mapping",response);
314        assertTrue("Stop processing",session.getStopHandlerProcessing());
315     }
316 }
317
Popular Tags