1 19 20 package org.apache.james.dnsserver; 21 22 import org.apache.avalon.framework.configuration.Configuration; 23 import org.apache.avalon.framework.configuration.DefaultConfigurationBuilder; 24 import org.apache.avalon.framework.container.ContainerUtil; 25 import org.apache.james.test.mock.avalon.MockLogger; 26 import org.xbill.DNS.Cache; 27 import org.xbill.DNS.DClass; 28 import org.xbill.DNS.Lookup; 29 import org.xbill.DNS.Message; 30 import org.xbill.DNS.Name; 31 import org.xbill.DNS.RRset; 32 import org.xbill.DNS.Record; 33 import org.xbill.DNS.Resolver; 34 import org.xbill.DNS.SOARecord; 35 import org.xbill.DNS.SetResponse; 36 import org.xbill.DNS.Zone; 37 38 import java.io.ByteArrayInputStream ; 39 import java.io.IOException ; 40 import java.net.URL ; 41 import java.util.Collection ; 42 import java.util.Iterator ; 43 44 import junit.framework.TestCase; 45 46 public class DNSServerTest extends TestCase { 47 48 private TestableDNSServer dnsServer; 49 private Cache defaultCache; 50 private Resolver defaultResolver; 51 private Name[] defaultSearchPaths; 52 53 public void testNoMX() throws Exception { 54 dnsServer.setResolver(null); 55 dnsServer.setCache(new ZoneCache("dnstest.com.")); 56 Collection records = dnsServer.findMXRecords("nomx.dnstest.com."); 58 assertEquals(1, records.size()); 59 assertEquals("nomx.dnstest.com.", records.iterator() 60 .next()); 61 } 62 63 public void testBadMX() throws Exception { 64 dnsServer.setResolver(null); 65 dnsServer.setCache(new ZoneCache("dnstest.com.")); 66 Collection records = dnsServer.findMXRecords("badmx.dnstest.com."); 68 assertEquals(1, records.size()); 69 assertEquals("badhost.dnstest.com.", records.iterator() 70 .next()); 71 Iterator it = dnsServer.getSMTPHostAddresses("badmx.dnstest.com."); 72 assertFalse(it.hasNext()); 73 } 74 75 public void testINARecords() throws Exception { 76 dnsServer.setResolver(null); 78 dnsServer.setCache(new ZoneCache("pippo.com.")); 79 Collection records = dnsServer.findMXRecords("www.pippo.com."); 81 assertEquals(1, records.size()); 82 assertEquals("pippo.com.inbound.mxlogic.net.", records.iterator() 83 .next()); 84 } 85 86 public void testMXCatches() throws Exception { 87 dnsServer.setResolver(null); 89 dnsServer.setCache(new ZoneCache("test-zone.com.")); 90 Collection res = dnsServer.findMXRecords("test-zone.com."); 92 try { 93 res.add(new Object ()); 94 fail("MX Collection should not be modifiable"); 95 } catch (UnsupportedOperationException e) { 96 } 97 assertEquals(1,res.size()); 98 assertEquals("mail.test-zone.com.",res.iterator().next()); 99 } 100 101 public void testCNAMEasMXrecords() throws Exception { 102 dnsServer.setResolver(null); 104 dnsServer.setCache(new ZoneCache("brandilyncollins.com.")); 105 Iterator records = dnsServer.getSMTPHostAddresses("brandilyncollins.com."); 107 assertEquals(true, records.hasNext()); 108 } 109 110 protected void setUp() throws Exception { 111 dnsServer = new TestableDNSServer(); 112 DefaultConfigurationBuilder db = new DefaultConfigurationBuilder(); 113 114 Configuration c = db.build( 115 new ByteArrayInputStream ("<dnsserver><autodiscover>true</autodiscover><authoritative>false</authoritative></dnsserver>".getBytes()), 116 "dnsserver"); 117 ContainerUtil.enableLogging(dnsServer, new MockLogger()); 118 ContainerUtil.configure(dnsServer, c); 119 ContainerUtil.initialize(dnsServer); 120 121 122 defaultCache = Lookup.getDefaultCache(DClass.IN); 123 defaultResolver = Lookup.getDefaultResolver(); 124 defaultSearchPaths = Lookup.getDefaultSearchPath(); 125 Lookup.setDefaultCache(null, DClass.IN); 126 Lookup.setDefaultResolver(null); 127 Lookup.setDefaultSearchPath(new Name[] {}); 128 } 129 130 protected void tearDown() throws Exception { 131 dnsServer.setCache(null); 132 ContainerUtil.dispose(dnsServer); 133 Lookup.setDefaultCache(defaultCache, DClass.IN); 134 Lookup.setDefaultResolver(defaultResolver); 135 Lookup.setDefaultSearchPath(defaultSearchPaths); 136 } 137 138 private Zone loadZone(String zoneName) throws IOException { 139 String zoneFilename = zoneName + "zone"; 140 URL zoneResource = getClass().getResource(zoneFilename); 141 assertNotNull("test resource for zone could not be loaded: " + zoneFilename, zoneResource); 142 String zoneFile = zoneResource.getFile(); 143 Zone zone = new Zone(Name.fromString(zoneName),zoneFile); 144 return zone; 145 } 146 147 private final class ZoneCache extends Cache { 148 149 Zone z = null; 150 151 public ZoneCache(String string) throws IOException { 152 z = loadZone(string); 153 } 154 155 public SetResponse addMessage(Message arg0) { 156 throw new UnsupportedOperationException ("ZoneCache is a mock used only for testing purpose"); 157 } 158 159 public synchronized void addNegative(Name arg0, int arg1, SOARecord arg2, int arg3) { 160 throw new UnsupportedOperationException ("ZoneCache is a mock used only for testing purpose"); 161 } 162 163 public synchronized void addRecord(Record arg0, int arg1, Object arg2) { 164 throw new UnsupportedOperationException ("ZoneCache is a mock used only for testing purpose"); 165 } 166 167 public synchronized void addRRset(RRset arg0, int arg1) { 168 throw new UnsupportedOperationException ("ZoneCache is a mock used only for testing purpose"); 169 } 170 171 public synchronized void clearCache() { 172 throw new UnsupportedOperationException ("ZoneCache is a mock used only for testing purpose"); 173 } 174 175 public RRset[] findAnyRecords(Name arg0, int arg1) { 176 throw new UnsupportedOperationException ("ZoneCache is a mock used only for testing purpose"); 177 } 178 179 public RRset[] findRecords(Name arg0, int arg1) { 180 throw new UnsupportedOperationException ("ZoneCache is a mock used only for testing purpose"); 181 } 182 183 public void flushName(Name arg0) { 184 throw new UnsupportedOperationException ("ZoneCache is a mock used only for testing purpose"); 185 } 186 187 public void flushSet(Name arg0, int arg1) { 188 throw new UnsupportedOperationException ("ZoneCache is a mock used only for testing purpose"); 189 } 190 191 public int getDClass() { 192 throw new UnsupportedOperationException ("ZoneCache is a mock used only for testing purpose"); 193 } 194 195 public int getMaxCache() { 196 throw new UnsupportedOperationException ("ZoneCache is a mock used only for testing purpose"); 197 } 198 199 public int getMaxEntries() { 200 throw new UnsupportedOperationException ("ZoneCache is a mock used only for testing purpose"); 201 } 202 203 public int getMaxNCache() { 204 throw new UnsupportedOperationException ("ZoneCache is a mock used only for testing purpose"); 205 } 206 207 public int getSize() { 208 throw new UnsupportedOperationException ("ZoneCache is a mock used only for testing purpose"); 209 } 210 211 protected synchronized SetResponse lookup(Name arg0, int arg1, int arg2) { 212 throw new UnsupportedOperationException ("ZoneCache is a mock used only for testing purpose"); 213 } 214 215 public SetResponse lookupRecords(Name arg0, int arg1, int arg2) { 216 System.out.println("Cache.lookupRecords "+arg0+","+arg1+","+arg2); 217 return z.findRecords(arg0, arg1); 218 } 220 221 public void setCleanInterval(int arg0) { 222 throw new UnsupportedOperationException ("ZoneCache is a mock used only for testing purpose"); 223 } 224 225 public void setMaxCache(int arg0) { 226 throw new UnsupportedOperationException ("ZoneCache is a mock used only for testing purpose"); 227 } 228 229 public void setMaxEntries(int arg0) { 230 throw new UnsupportedOperationException ("ZoneCache is a mock used only for testing purpose"); 231 } 232 233 public void setMaxNCache(int arg0) { 234 throw new UnsupportedOperationException ("ZoneCache is a mock used only for testing purpose"); 235 } 236 } 237 238 private final class TestableDNSServer extends DNSServer { 239 240 public void setResolver(Resolver r) { 241 resolver = r; 242 } 243 244 public Resolver getResolver() { 245 return resolver; 246 } 247 248 public void setCache(Cache c) { 249 cache = c; 250 } 251 252 public Cache getCache() { 253 return cache; 254 } 255 } 256 257 } 258 | Popular Tags |