1 19 20 21 22 package org.apache.james.vut; 23 24 import java.net.InetAddress ; 25 import java.net.UnknownHostException ; 26 import java.util.Collection ; 27 import java.util.Iterator ; 28 import java.util.Map ; 29 30 import org.apache.avalon.framework.configuration.ConfigurationException; 31 import org.apache.avalon.framework.container.ContainerUtil; 32 import org.apache.avalon.framework.service.ServiceException; 33 import org.apache.james.services.AbstractDNSServer; 34 import org.apache.james.services.DNSServer; 35 import org.apache.james.services.VirtualUserTable; 36 37 import junit.framework.TestCase; 38 39 public abstract class AbstractVirtualUserTableTest extends TestCase { 40 41 protected AbstractVirtualUserTable virtualUserTable; 42 protected final static int REGEX_TYPE = 0; 43 protected final static int ERROR_TYPE = 1; 44 protected final static int ADDRESS_TYPE = 2; 45 protected final static int ALIASDOMAIN_TYPE = 3; 46 47 protected void setUp() throws Exception { 48 virtualUserTable = getVirtalUserTable(); 49 } 50 51 protected void tearDown() throws Exception { 52 Map mappings = virtualUserTable.getAllMappings(); 53 54 if (mappings != null) { 55 Iterator mappingsIt = virtualUserTable.getAllMappings().keySet().iterator(); 56 57 58 while(mappingsIt.hasNext()) { 59 String key = mappingsIt.next().toString(); 60 String args[] = key.split("@"); 61 62 Collection map = (Collection ) mappings.get(key); 63 64 Iterator mapIt = map.iterator(); 65 66 while (mapIt.hasNext()) { 67 try { 68 removeMapping(args[0], args[1], mapIt.next().toString()); 69 } catch (InvalidMappingException e) { 70 e.printStackTrace(); 71 } 72 } 73 } 74 } 75 ContainerUtil.dispose(virtualUserTable); 76 } 77 78 private void removeMapping(String user, String domain, String rawMapping) throws InvalidMappingException { 79 if (rawMapping.startsWith(VirtualUserTable.ERROR_PREFIX)) { 80 removeMapping(user, domain, rawMapping.substring(VirtualUserTable.ERROR_PREFIX.length()), ERROR_TYPE); 81 } else if (rawMapping.startsWith(VirtualUserTable.REGEX_PREFIX)) { 82 removeMapping(user, domain, rawMapping.substring(VirtualUserTable.REGEX_PREFIX.length()), REGEX_TYPE); 83 } else if (rawMapping.startsWith(VirtualUserTable.ALIASDOMAIN_PREFIX)) { 84 removeMapping(user, domain, rawMapping.substring(VirtualUserTable.ALIASDOMAIN_PREFIX.length()), ALIASDOMAIN_TYPE); 85 } else { 86 removeMapping(user, domain, rawMapping, ADDRESS_TYPE); 87 } 88 } 89 90 protected abstract AbstractVirtualUserTable getVirtalUserTable() throws ServiceException, ConfigurationException, Exception ; 91 92 protected abstract boolean addMapping(String user , String domain, String mapping,int type)throws InvalidMappingException; 93 94 protected abstract boolean removeMapping(String user, String domain, String mapping, int type) throws InvalidMappingException; 95 96 97 protected DNSServer setUpDNSServer() { 98 DNSServer dns = new AbstractDNSServer() { 99 public String getHostName(InetAddress inet) { 100 return "test"; 101 } 102 103 public InetAddress [] getAllByName(String name) throws UnknownHostException { 104 return new InetAddress [] { InetAddress.getByName("127.0.0.1")}; 105 } 106 107 public InetAddress getLocalHost() throws UnknownHostException { 108 return InetAddress.getLocalHost(); 109 } 110 }; 111 return dns; 112 } 113 114 public void testStoreAndRetrieveRegexMapping() throws ErrorMappingException { 115 String user = "test"; 116 String domain = "localhost"; 117 String regex = "(.*):{$1}@localhost"; 118 String regex2 = "(.+):{$1}@test"; 119 String invalidRegex = ".*):"; 120 boolean catched = false; 121 try { 122 123 assertNull("No mapping",virtualUserTable.getMappings(user, domain)); 124 125 assertTrue("Added virtual mapping", addMapping(user, domain, regex, REGEX_TYPE)); 126 assertTrue("Added virtual mapping", addMapping(user, domain, regex2, REGEX_TYPE)); 127 128 assertEquals("Two mappings",virtualUserTable.getMappings(user, domain).size(), 2); 129 assertEquals("One mappingline",virtualUserTable.getAllMappings().size(),1); 130 131 assertEquals("Three domains",virtualUserTable.getDomains().size(), 3); 133 assertTrue("Contains Domain",virtualUserTable.containsDomain(domain)); 134 135 assertTrue("remove virtual mapping", removeMapping(user, domain, regex, REGEX_TYPE)); 136 137 try { 138 assertTrue("Added virtual mapping", virtualUserTable.addRegexMapping(user, domain, invalidRegex)); 139 } catch (InvalidMappingException e) { 140 catched = true; 141 } 142 assertTrue("Invalid Mapping throw exception" , catched); 143 144 assertTrue("remove virtual mapping", removeMapping(user, domain, regex2, REGEX_TYPE)); 145 assertNull("No mapping",virtualUserTable.getMappings(user, domain)); 146 147 assertNull("No mappings",virtualUserTable.getAllMappings()); 148 149 } catch (InvalidMappingException e) { 150 fail("Storing failed"); 151 } 152 153 } 154 155 156 public void testStoreAndRetrieveAddressMapping() throws ErrorMappingException { 157 String user = "test"; 158 String domain = "localhost"; 159 String address = "test@localhost2"; 160 String address2 = "test@james"; 161 String invalidAddress= ".*@localhost2:"; 162 boolean catched = false; 163 164 try { 165 166 assertNull("No mapping",virtualUserTable.getMappings(user, domain)); 167 168 assertTrue("Added virtual mapping", addMapping(user, domain, address, ADDRESS_TYPE)); 169 assertTrue("Added virtual mapping", addMapping(user, domain, address2, ADDRESS_TYPE)); 170 171 assertEquals("Two mappings",virtualUserTable.getMappings(user, domain).size(),2); 172 assertEquals("One mappingline",virtualUserTable.getAllMappings().size(),1); 173 174 assertTrue("remove virtual mapping", removeMapping(user, domain, address, ADDRESS_TYPE)); 175 176 if (virtualUserTable instanceof JDBCVirtualUserTable) { 177 try { 178 assertTrue("Added virtual mapping", addMapping(user, domain, invalidAddress, ADDRESS_TYPE)); 179 } catch (InvalidMappingException e) { 180 catched = true; 181 } 182 assertTrue("Invalid Mapping throw exception" , catched); 183 } 184 185 assertTrue("remove virtual mapping", removeMapping(user, domain, address2, ADDRESS_TYPE)); 186 187 assertNull("No mapping",virtualUserTable.getMappings(user, domain)); 188 assertNull("No mappings",virtualUserTable.getAllMappings()); 189 190 } catch (InvalidMappingException e) { 191 fail("Storing failed"); 192 } 193 194 195 } 196 197 public void testStoreAndRetrieveErrorMapping() throws ErrorMappingException { 198 String user = "test"; 199 String domain = "localhost"; 200 String error = "bounce!"; 201 boolean catched = false; 202 203 try { 204 205 assertNull("No mapping",virtualUserTable.getMappings(user, domain)); 206 207 assertTrue("Added virtual mapping", addMapping(user, domain, error, ERROR_TYPE)); 208 assertEquals("One mappingline",virtualUserTable.getAllMappings().size(),1); 209 210 try { 211 virtualUserTable.getMappings(user, domain); 212 } catch (ErrorMappingException e) { 213 catched = true; 214 } 215 assertTrue("Error Mapping throw exception" , catched); 216 217 assertTrue("remove virtual mapping", removeMapping(user, domain, error, ERROR_TYPE)); 218 assertNull("No mapping",virtualUserTable.getMappings(user, domain)); 219 assertNull("No mappings",virtualUserTable.getAllMappings()); 220 221 } catch (InvalidMappingException e) { 222 fail("Storing failed"); 223 } 224 225 } 226 227 public void testStoreAndRetrieveWildCardAddressMapping() throws ErrorMappingException { 228 String user = "test"; 229 String user2 = "test2"; 230 String domain = "localhost"; 231 String address = "test@localhost2"; 232 String address2 = "test@james"; 233 234 235 try { 236 237 assertNull("No mapping",virtualUserTable.getMappings(user, domain)); 238 239 assertTrue("Added virtual mapping", addMapping(null, domain, address, ADDRESS_TYPE)); 240 assertTrue("Added virtual mapping", addMapping(user, domain, address2, ADDRESS_TYPE)); 241 242 243 assertTrue("One mappings",virtualUserTable.getMappings(user, domain).size() == 1); 244 assertTrue("One mappings",virtualUserTable.getMappings(user2, domain).size() == 1); 245 246 assertTrue("remove virtual mapping", removeMapping(user, domain, address2, ADDRESS_TYPE)); 247 assertTrue("remove virtual mapping", removeMapping(null, domain, address, ADDRESS_TYPE)); 248 assertNull("No mapping",virtualUserTable.getMappings(user, domain)); 249 assertNull("No mapping",virtualUserTable.getMappings(user2, domain)); 250 251 } catch (InvalidMappingException e) { 252 fail("Storing failed"); 253 } 254 255 } 256 257 public void testRecursiveMapping() throws ErrorMappingException { 258 String user1 = "user1"; 259 String user2 = "user2"; 260 String user3 = "user3"; 261 String domain1 = "domain1"; 262 String domain2 = "domain2"; 263 String domain3 = "domain3"; 264 boolean exception1 = false; 265 266 virtualUserTable.setRecursiveMapping(true); 267 268 try { 269 assertNull("No mappings",virtualUserTable.getAllMappings()); 270 271 assertTrue("Add mapping", addMapping(user1, domain1, user2 + "@" + domain2, ADDRESS_TYPE)); 272 assertTrue("Add mapping", addMapping(user2, domain2, user3 + "@" + domain3, ADDRESS_TYPE)); 273 assertEquals("Recursive mapped", virtualUserTable.getMappings(user1, domain1).iterator().next(),user3 + "@" + domain3); 274 275 assertTrue("Add mapping", addMapping(user3, domain3, user1 + "@" + domain1, ADDRESS_TYPE)); 276 try { 277 virtualUserTable.getMappings(user1, domain1); 278 } catch (ErrorMappingException e) { 279 exception1 = true; 280 } 281 assertTrue("Exception thrown on to many mappings", exception1); 282 283 virtualUserTable.setRecursiveMapping(false); 285 assertEquals("Not recursive mapped", virtualUserTable.getMappings(user1, domain1).iterator().next(),user2 + "@" + domain2); 286 287 } catch (InvalidMappingException e) { 288 fail("Storing failed"); 289 } 290 } 291 292 293 public void testAliasDomainMapping() throws ErrorMappingException { 294 String domain = "realdomain"; 295 String aliasDomain = "aliasdomain"; 296 String user = "user"; 297 String user2 = "user2"; 298 299 assertNull("No mappings",virtualUserTable.getAllMappings()); 300 try { 301 assertTrue("Add mapping",addMapping(null, aliasDomain, user2 + "@" + domain, ADDRESS_TYPE)); 302 assertTrue("Add aliasDomain mapping", addMapping(null, aliasDomain, domain, ALIASDOMAIN_TYPE)); 303 304 Iterator mappings = virtualUserTable.getMappings(user, aliasDomain).iterator(); 305 assertEquals("Domain mapped as first ", mappings.next(), user + "@" + domain); 306 assertEquals("Address mapped as second ", mappings.next(), user2 + "@" + domain); 307 308 assertTrue("Remove mapping", removeMapping(null, aliasDomain, user2 + "@" + domain, ADDRESS_TYPE)); 309 assertTrue("Remove aliasDomain mapping", removeMapping(null, aliasDomain, domain, ALIASDOMAIN_TYPE)); 310 311 } catch (InvalidMappingException e) { 312 fail("Storing failed"); 313 } 314 } 315 316 } 317 | Popular Tags |