1 /************************************************************************ 2 * Copyright (c) 1999-2006 The Apache Software Foundation. * 3 * All rights reserved. * 4 * ------------------------------------------------------------------- * 5 * Licensed under the Apache License, Version 2.0 (the "License"); you * 6 * may not use this file except in compliance with the License. You * 7 * may obtain a copy of the License at: * 8 * * 9 * http://www.apache.org/licenses/LICENSE-2.0 * 10 * * 11 * Unless required by applicable law or agreed to in writing, software * 12 * distributed under the License is distributed on an "AS IS" BASIS, * 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or * 14 * implied. See the License for the specific language governing * 15 * permissions and limitations under the License. * 16 ***********************************************************************/ 17 package org.apache.james.dnsserver; 18 19 import org.apache.avalon.framework.configuration.Configuration; 20 import org.apache.avalon.framework.configuration.DefaultConfigurationBuilder; 21 import org.apache.james.test.mock.avalon.MockLogger; 22 import org.xbill.DNS.Name; 23 import org.xbill.DNS.Record; 24 import org.xbill.DNS.Resolver; 25 import org.xbill.DNS.SetResponse; 26 import org.xbill.DNS.TextParseException; 27 import org.xbill.DNS.Zone; 28 29 import java.io.ByteArrayInputStream; 30 import java.util.Collection; 31 import java.util.Iterator; 32 33 import junit.framework.TestCase; 34 35 public class DNSServerTest extends TestCase { 36 37 private TestableDNSServer dnsServer; 38 39 /*** 40 * Please note that this is an hardcoded test that works because 41 * www.pippo.com. is an alias to pippo.com and pippo.com has 42 * "pippo.com.inbound.mxlogic.net." as its mx record. 43 * This is the first domain with a record proving a previous james bug. 44 * This test will be invalidated by any change in the pippo.com dns records 45 * 46 * @param args 47 * @throws Exception 48 */ 49 public void testINARecords() throws Exception { 50 Zone z = new Zone(Name.fromString("pippo.com."),getClass().getResource("pippo-com.zone").getFile()); 51 dnsServer.setResolver(null); 52 dnsServer.setLookupper(new ZoneLookupper(z)); 53 Collection records = dnsServer.findMXRecords("www.pippo.com."); 54 assertEquals(1, records.size()); 55 assertEquals("pippo.com.inbound.mxlogic.net.", records.iterator() 56 .next()); 57 } 58 59 /*** 60 * @throws Exception 61 */ 62 public void testMXCatches() throws Exception { 63 Zone z = new Zone(Name.fromString("test-zone.com."),getClass().getResource("test-zone-com.zone").getFile()); 64 dnsServer.setResolver(null); 65 dnsServer.setLookupper(new ZoneLookupper(z)); 66 Collection res = dnsServer.findMXRecords("test-zone.com."); 67 try { 68 res.add(new Object()); 69 fail("MX Collection should not be modifiable"); 70 } catch (UnsupportedOperationException e) { 71 } 72 assertEquals(1,res.size()); 73 assertEquals("mail.test-zone.com.",res.iterator().next()); 74 } 75 76 /*** 77 * Please note that this is an hardcoded test that works because 78 * brandilyncollins.com. has an MX record that point to mxmail.register.com 79 * and this is a CNAME to the real address. 80 * This test will be invalidated by any change in the brandilyncollins.com dns records 81 * 82 * @param args 83 * @throws Exception 84 */ 85 public void testCNAMEasMXrecords() throws Exception { 86 Zone z = new Zone(Name.fromString("brandilyncollins.com."),getClass().getResource("brandilyncollins-com.zone").getFile()); 87 dnsServer.setResolver(null); 88 dnsServer.setLookupper(new ZoneLookupper(z)); 89 Iterator records = dnsServer.getSMTPHostAddresses("brandilyncollins.com."); 90 assertEquals(true, records.hasNext()); 91 } 92 93 protected void setUp() throws Exception { 94 dnsServer = new TestableDNSServer(); 95 DefaultConfigurationBuilder db = new DefaultConfigurationBuilder(); 96 97 Configuration c = db.build( 98 new ByteArrayInputStream("<dnsserver><autodiscover>true</autodiscover><authoritative>false</authoritative></dnsserver>".getBytes()), 99 "dnsserver"); 100 dnsServer.enableLogging(new MockLogger()); 101 dnsServer.configure(c); 102 dnsServer.initialize(); 103 } 104 105 protected void tearDown() throws Exception { 106 dnsServer.setLookupper(null); 107 dnsServer.dispose(); 108 } 109 110 private class ZoneLookupper implements Lookupper { 111 private final Zone z; 112 113 private ZoneLookupper(Zone z) { 114 super(); 115 this.z = z; 116 } 117 118 public SetResponse lookup(Name name, int type) { 119 SetResponse s = z.findRecords(name,type); 120 System.out.println("Zone Lookup: "+name+" "+type+" = "+s); 121 return s; 122 } 123 } 124 125 private interface Lookupper { 126 SetResponse lookup(Name name, int type); 127 } 128 129 private final class TestableDNSServer extends DNSServer { 130 131 private Lookupper lookupper; 132 133 public void setLookupper(Lookupper l) { 134 this.lookupper = l; 135 } 136 137 public Record[] lookup(String name, int type) { 138 if (lookupper != null) { 139 try { 140 SetResponse lookup = lookupper.lookup(Name.fromString(name), type); 141 if (lookup != null && lookup.isSuccessful()) { 142 return processSetResponse(lookup); 143 } else { 144 return null; 145 } 146 } catch (TextParseException e) { 147 e.printStackTrace(); 148 return null; 149 } 150 } else { 151 return super.lookup(name, type); 152 } 153 } 154 155 public void setResolver(Resolver r) { 156 resolver = r; 157 } 158 159 public Resolver getResolver() { 160 return resolver; 161 } 162 } 163 164 }